1 /* vi: set sw=4 ts=4: */
5 * Adapted from ash applet code
7 * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
8 * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
13 * Copyright (c) 2010 Tobias Klauser
14 * Split from ash.c and slightly adapted.
16 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
19 #include "builtin_ulimit.h"
23 uint8_t cmd; /* RLIMIT_xxx fit into it */
24 uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
29 static const struct limits limits_tbl[] = {
31 { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" },
34 { RLIMIT_CPU, 0, 't', "cpu time (seconds)" },
37 { RLIMIT_DATA, 10, 'd', "data seg size (kb)" },
40 { RLIMIT_STACK, 10, 's', "stack size (kb)" },
43 { RLIMIT_CORE, 9, 'c', "core file size (blocks)" },
46 { RLIMIT_RSS, 10, 'm', "resident set size (kb)" },
49 { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" },
52 { RLIMIT_NPROC, 0, 'p', "processes" },
55 { RLIMIT_NOFILE, 0, 'n', "file descriptors" },
58 { RLIMIT_AS, 10, 'v', "address space (kb)" },
61 { RLIMIT_LOCKS, 0, 'w', "locks" },
70 /* "-": treat args as parameters of option with ASCII code 1 */
71 static const char ulimit_opt_string[] = "-HSa"
107 static void printlim(unsigned opts, const struct rlimit *limit,
108 const struct limits *l)
112 val = limit->rlim_max;
113 if (!(opts & OPT_hard))
114 val = limit->rlim_cur;
116 if (val == RLIM_INFINITY)
117 printf("unlimited\n");
119 val >>= l->factor_shift;
120 printf("%llu\n", (long long) val);
124 int FAST_FUNC shell_builtin_ulimit(char **argv)
129 /* We can't use getopt32: need to handle commands like
130 * ulimit 123 -c2 -l 456
133 /* In case getopt was already called:
134 * reset the libc getopt() function, which keeps internal state.
138 #else /* BSD style */
142 /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
151 const struct limits *l;
152 int opt_char = getopt(argc, argv, ulimit_opt_string);
156 if (opt_char == 'H') {
160 if (opt_char == 'S') {
165 if (opt_char == 'a') {
166 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
167 getrlimit(l->cmd, &limit);
168 printf("-%c: %-30s ", l->option, l->name);
169 printlim(opts, &limit, l);
176 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
177 if (opt_char == l->option) {
180 getrlimit(l->cmd, &limit);
183 if (!val_str && argv[optind] && argv[optind][0] != '-')
184 val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
188 if (strcmp(val_str, "unlimited") == 0)
191 if (sizeof(val) == sizeof(int))
192 val = bb_strtou(val_str, NULL, 10);
193 else if (sizeof(val) == sizeof(long))
194 val = bb_strtoul(val_str, NULL, 10);
196 val = bb_strtoull(val_str, NULL, 10);
198 bb_error_msg("bad number");
201 val <<= l->factor_shift;
203 //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
205 limit.rlim_max = val;
206 if ((opts & OPT_soft) || opts == 0)
207 limit.rlim_cur = val;
208 //bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
209 if (setrlimit(l->cmd, &limit) < 0) {
210 bb_perror_msg("error setting limit");
214 printlim(opts, &limit, l);
218 } /* for (every possible opt) */
220 if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) {
221 /* bad option. getopt already complained. */
225 } /* while (there are options) */