blob: 12c4a073c4f68b1f9691c598ec62ddc4c7e37750 [file] [log] [blame]
Denys Vlasenko73067272010-01-12 22:11:24 +01001/* vi: set sw=4 ts=4: */
2/*
3 * Adapted from ash applet code
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Copyright (c) 1989, 1991, 1993, 1994
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
12 * was re-ported from NetBSD and debianized.
13 *
14 * Copyright (c) 2010 Denys Vlasenko
15 * Split from ash.c
16 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020017 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denys Vlasenko73067272010-01-12 22:11:24 +010018 */
19#include "libbb.h"
20#include "shell_common.h"
21
Denys Vlasenko73067272010-01-12 22:11:24 +010022const char defifsvar[] ALIGN1 = "IFS= \t\n";
Denys Vlasenkoe627ac92016-09-30 14:36:59 +020023const char defoptindvar[] ALIGN1 = "OPTIND=1";
Denys Vlasenko25d9b912010-01-13 18:22:35 +010024
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020025/* read builtin */
26
Denys Vlasenko25ce3ee2013-07-14 01:23:06 +020027/* Needs to be interruptible: shell must handle traps and shell-special signals
Denys Vlasenko80542ba2011-05-08 21:23:43 +020028 * while inside read. To implement this, be sure to not loop on EINTR
29 * and return errno == EINTR reliably.
30 */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020031//TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
32//string. hush naturally has it, and ash has setvareq().
33//Here we can simply store "VAR=" at buffer start and store read data directly
34//after "=", then pass buffer to setvar() to consume.
35const char* FAST_FUNC
Denys Vlasenko19358cc2018-08-05 15:42:29 +020036shell_builtin_read(struct builtin_read_params *params)
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020037{
Denys Vlasenkoeae12682017-07-20 16:09:31 +020038 struct pollfd pfd[1];
39#define fd (pfd[0].fd) /* -u FD */
Denys Vlasenko80542ba2011-05-08 21:23:43 +020040 unsigned err;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020041 unsigned end_ms; /* -t TIMEOUT */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020042 int nchars; /* -n NUM */
43 char **pp;
44 char *buffer;
Denys Vlasenkocde46f72017-08-09 14:04:07 +020045 char delim;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020046 struct termios tty, old_tty;
47 const char *retval;
48 int bufpos; /* need to be able to hold -1 */
49 int startword;
50 smallint backslash;
Denys Vlasenko19358cc2018-08-05 15:42:29 +020051 char **argv;
52 const char *ifs;
53 int read_flags;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020054
Denys Vlasenko80542ba2011-05-08 21:23:43 +020055 errno = err = 0;
56
Denys Vlasenko19358cc2018-08-05 15:42:29 +020057 argv = params->argv;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020058 pp = argv;
59 while (*pp) {
Denys Vlasenkod8bd7012019-05-14 18:53:24 +020060 if (endofname(*pp)[0] != '\0') {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020061 /* Mimic bash message */
62 bb_error_msg("read: '%s': not a valid identifier", *pp);
63 return (const char *)(uintptr_t)1;
64 }
65 pp++;
66 }
67
68 nchars = 0; /* if != 0, -n is in effect */
Denys Vlasenko19358cc2018-08-05 15:42:29 +020069 if (params->opt_n) {
70 nchars = bb_strtou(params->opt_n, NULL, 10);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020071 if (nchars < 0 || errno)
72 return "invalid count";
73 /* note: "-n 0": off (bash 3.2 does this too) */
74 }
Denys Vlasenkoeae12682017-07-20 16:09:31 +020075
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020076 end_ms = 0;
Denys Vlasenko19358cc2018-08-05 15:42:29 +020077 if (params->opt_t && !ENABLE_FEATURE_SH_READ_FRAC) {
78 end_ms = bb_strtou(params->opt_t, NULL, 10);
Denys Vlasenkoeae12682017-07-20 16:09:31 +020079 if (errno)
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020080 return "invalid timeout";
Denys Vlasenkoeae12682017-07-20 16:09:31 +020081 if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */
82 end_ms = UINT_MAX / 2048;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020083 end_ms *= 1000;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +020084 }
Denys Vlasenko19358cc2018-08-05 15:42:29 +020085 if (params->opt_t && ENABLE_FEATURE_SH_READ_FRAC) {
Denys Vlasenkoeae12682017-07-20 16:09:31 +020086 /* bash 4.3 (maybe earlier) supports -t N.NNNNNN */
87 char *p;
88 /* Eat up to three fractional digits */
89 int frac_digits = 3 + 1;
90
Denys Vlasenko19358cc2018-08-05 15:42:29 +020091 end_ms = bb_strtou(params->opt_t, &p, 10);
Denys Vlasenkoeae12682017-07-20 16:09:31 +020092 if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */
93 end_ms = UINT_MAX / 2048;
94
95 if (errno) {
96 /* EINVAL = number is ok, but not NUL terminated */
97 if (errno != EINVAL || *p != '.')
98 return "invalid timeout";
99 /* Do not check the rest: bash allows "0.123456xyz" */
100 while (*++p && --frac_digits) {
101 end_ms *= 10;
102 end_ms += (*p - '0');
103 if ((unsigned char)(*p - '0') > 9)
104 return "invalid timeout";
105 }
106 }
107 while (--frac_digits > 0) {
108 end_ms *= 10;
109 }
110 }
111
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200112 fd = STDIN_FILENO;
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200113 if (params->opt_u) {
114 fd = bb_strtou(params->opt_u, NULL, 10);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200115 if (fd < 0 || errno)
116 return "invalid file descriptor";
117 }
118
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200119 if (params->opt_t && end_ms == 0) {
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200120 /* "If timeout is 0, read returns immediately, without trying
121 * to read any data. The exit status is 0 if input is available
122 * on the specified file descriptor, non-zero otherwise."
123 * bash seems to ignore -p PROMPT for this use case.
124 */
125 int r;
126 pfd[0].events = POLLIN;
127 r = poll(pfd, 1, /*timeout:*/ 0);
128 /* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */
129 return (const char *)(uintptr_t)(r <= 0);
130 }
131
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200132 if (params->opt_p && isatty(fd)) {
133 fputs(params->opt_p, stderr);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200134 fflush_all();
135 }
136
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200137 ifs = params->ifs;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200138 if (ifs == NULL)
139 ifs = defifs;
140
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200141 read_flags = params->read_flags;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200142 if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
143 tcgetattr(fd, &tty);
144 old_tty = tty;
145 if (nchars) {
146 tty.c_lflag &= ~ICANON;
Denys Vlasenko7ce209b2012-01-15 22:58:06 +0100147 // Setting it to more than 1 breaks poll():
148 // it blocks even if there's data. !??
149 //tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
Denys Vlasenko01ccdd12017-01-11 16:17:59 +0100150 /* reads will block only if < 1 char is available */
Denys Vlasenko7ce209b2012-01-15 22:58:06 +0100151 tty.c_cc[VMIN] = 1;
152 /* no timeout (reads block forever) */
153 tty.c_cc[VTIME] = 0;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200154 }
155 if (read_flags & BUILTIN_READ_SILENT) {
156 tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
157 }
158 /* This forces execution of "restoring" tcgetattr later */
159 read_flags |= BUILTIN_READ_SILENT;
160 /* if tcgetattr failed, tcsetattr will fail too.
161 * Ignoring, it's harmless. */
162 tcsetattr(fd, TCSANOW, &tty);
163 }
164
165 retval = (const char *)(uintptr_t)0;
166 startword = 1;
167 backslash = 0;
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200168 if (params->opt_t)
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200169 end_ms += (unsigned)monotonic_ms();
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200170 buffer = NULL;
171 bufpos = 0;
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200172 delim = params->opt_d ? params->opt_d[0] : '\n';
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200173 do {
174 char c;
Denys Vlasenko10c01312011-05-11 11:49:21 +0200175 int timeout;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200176
Denys Vlasenko10c01312011-05-11 11:49:21 +0200177 if ((bufpos & 0xff) == 0)
Denys Vlasenko9e71e3c2012-09-06 13:28:10 +0200178 buffer = xrealloc(buffer, bufpos + 0x101);
Denys Vlasenko80542ba2011-05-08 21:23:43 +0200179
Denys Vlasenko10c01312011-05-11 11:49:21 +0200180 timeout = -1;
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200181 if (params->opt_t) {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200182 timeout = end_ms - (unsigned)monotonic_ms();
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200183 /* ^^^^^^^^^^^^^ all values are unsigned,
184 * wrapping math is used here, good even if
185 * 32-bit unix time wrapped (year 2038+).
186 */
Denys Vlasenko10c01312011-05-11 11:49:21 +0200187 if (timeout <= 0) { /* already late? */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200188 retval = (const char *)(uintptr_t)1;
189 goto ret;
190 }
191 }
192
Denys Vlasenko10c01312011-05-11 11:49:21 +0200193 /* We must poll even if timeout is -1:
194 * we want to be interrupted if signal arrives,
195 * regardless of SA_RESTART-ness of that signal!
196 */
197 errno = 0;
Denys Vlasenko10c01312011-05-11 11:49:21 +0200198 pfd[0].events = POLLIN;
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200199 if (poll(pfd, 1, timeout) <= 0) {
Denys Vlasenko10c01312011-05-11 11:49:21 +0200200 /* timed out, or EINTR */
201 err = errno;
202 retval = (const char *)(uintptr_t)1;
203 goto ret;
204 }
205 if (read(fd, &buffer[bufpos], 1) != 1) {
Denys Vlasenko80542ba2011-05-08 21:23:43 +0200206 err = errno;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200207 retval = (const char *)(uintptr_t)1;
208 break;
209 }
Denys Vlasenko10c01312011-05-11 11:49:21 +0200210
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200211 c = buffer[bufpos];
212 if (c == '\0')
213 continue;
Denys Vlasenkof5470412017-05-22 19:34:45 +0200214 if (!(read_flags & BUILTIN_READ_RAW)) {
215 if (backslash) {
216 backslash = 0;
217 if (c != '\n')
218 goto put;
219 continue;
220 }
221 if (c == '\\') {
222 backslash = 1;
223 continue;
224 }
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200225 }
Denys Vlasenkocde46f72017-08-09 14:04:07 +0200226 if (c == delim) /* '\n' or -d CHAR */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200227 break;
228
229 /* $IFS splitting. NOT done if we run "read"
230 * without variable names (bash compat).
231 * Thus, "read" and "read REPLY" are not the same.
232 */
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200233 if (!params->opt_d && argv[0]) {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200234/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
235 const char *is_ifs = strchr(ifs, c);
236 if (startword && is_ifs) {
237 if (isspace(c))
238 continue;
239 /* it is a non-space ifs char */
240 startword--;
241 if (startword == 1) /* first one? */
242 continue; /* yes, it is not next word yet */
243 }
244 startword = 0;
245 if (argv[1] != NULL && is_ifs) {
246 buffer[bufpos] = '\0';
247 bufpos = 0;
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200248 params->setvar(*argv, buffer);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200249 argv++;
250 /* can we skip one non-space ifs char? (2: yes) */
251 startword = isspace(c) ? 2 : 1;
252 continue;
253 }
254 }
255 put:
256 bufpos++;
257 } while (--nchars);
258
259 if (argv[0]) {
260 /* Remove trailing space $IFS chars */
Denys Vlasenko44257ad2018-04-11 17:18:34 +0200261 while (--bufpos >= 0
262 && isspace(buffer[bufpos])
263 && strchr(ifs, buffer[bufpos]) != NULL
264 ) {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200265 continue;
Denys Vlasenko44257ad2018-04-11 17:18:34 +0200266 }
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200267 buffer[bufpos + 1] = '\0';
Denys Vlasenko44257ad2018-04-11 17:18:34 +0200268
269 /* Last variable takes the entire remainder with delimiters
270 * (sans trailing whitespace $IFS),
271 * but ***only "if there are fewer vars than fields"(c)***!
272 * The "X:Y:" case below: there are two fields,
273 * and therefore last delimiter (:) is eaten:
274 * IFS=": "
275 * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:|
276 * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z|
277 * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:|
278 * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y|
279 */
280 if (bufpos >= 0
281 && strchr(ifs, buffer[bufpos]) != NULL
282 ) {
283 /* There _is_ a non-whitespace IFS char */
284 /* Skip whitespace IFS char before it */
285 while (--bufpos >= 0
286 && isspace(buffer[bufpos])
287 && strchr(ifs, buffer[bufpos]) != NULL
288 ) {
289 continue;
290 }
291 /* Are there $IFS chars? */
292 if (strcspn(buffer, ifs) >= ++bufpos) {
293 /* No: last var takes one field, not more */
294 /* So, drop trailing IFS delims */
295 buffer[bufpos] = '\0';
296 }
297 }
298
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200299 /* Use the remainder as a value for the next variable */
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200300 params->setvar(*argv, buffer);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200301 /* Set the rest to "" */
302 while (*++argv)
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200303 params->setvar(*argv, "");
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200304 } else {
305 /* Note: no $IFS removal */
306 buffer[bufpos] = '\0';
Denys Vlasenko19358cc2018-08-05 15:42:29 +0200307 params->setvar("REPLY", buffer);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200308 }
309
310 ret:
311 free(buffer);
312 if (read_flags & BUILTIN_READ_SILENT)
313 tcsetattr(fd, TCSANOW, &old_tty);
Denys Vlasenko80542ba2011-05-08 21:23:43 +0200314
315 errno = err;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200316 return retval;
Denys Vlasenkoeae12682017-07-20 16:09:31 +0200317#undef fd
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200318}
319
320/* ulimit builtin */
321
322struct limits {
323 uint8_t cmd; /* RLIMIT_xxx fit into it */
324 uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200325};
326
327static const struct limits limits_tbl[] = {
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200328 { RLIMIT_CORE, 9, }, // -c
329 { RLIMIT_DATA, 10, }, // -d
330 { RLIMIT_NICE, 0, }, // -e
331 { RLIMIT_FSIZE, 9, }, // -f
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200332#define LIMIT_F_IDX 3
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200333#ifdef RLIMIT_SIGPENDING
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200334 { RLIMIT_SIGPENDING, 0, }, // -i
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200335#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200336#ifdef RLIMIT_MEMLOCK
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200337 { RLIMIT_MEMLOCK, 10, }, // -l
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200338#endif
339#ifdef RLIMIT_RSS
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200340 { RLIMIT_RSS, 10, }, // -m
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200341#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200342#ifdef RLIMIT_NOFILE
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200343 { RLIMIT_NOFILE, 0, }, // -n
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200344#endif
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200345#ifdef RLIMIT_MSGQUEUE
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200346 { RLIMIT_MSGQUEUE, 0, }, // -q
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200347#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200348#ifdef RLIMIT_RTPRIO
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200349 { RLIMIT_RTPRIO, 0, }, // -r
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200350#endif
351#ifdef RLIMIT_STACK
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200352 { RLIMIT_STACK, 10, }, // -s
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200353#endif
354#ifdef RLIMIT_CPU
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200355 { RLIMIT_CPU, 0, }, // -t
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200356#endif
357#ifdef RLIMIT_NPROC
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200358 { RLIMIT_NPROC, 0, }, // -u
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200359#endif
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200360#ifdef RLIMIT_AS
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200361 { RLIMIT_AS, 10, }, // -v
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200362#endif
363#ifdef RLIMIT_LOCKS
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200364 { RLIMIT_LOCKS, 0, }, // -x
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200365#endif
366};
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200367// bash also shows:
Denys Vlasenkoa92a9602019-04-27 21:23:39 +0200368//pipe size (512 bytes, -p) 8
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200369
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200370static const char limits_help[] ALIGN1 =
371 "core file size (blocks)" // -c
372 "\0""data seg size (kb)" // -d
373 "\0""scheduling priority" // -e
374 "\0""file size (blocks)" // -f
375#ifdef RLIMIT_SIGPENDING
376 "\0""pending signals" // -i
377#endif
378#ifdef RLIMIT_MEMLOCK
379 "\0""max locked memory (kb)" // -l
380#endif
381#ifdef RLIMIT_RSS
382 "\0""max memory size (kb)" // -m
383#endif
384#ifdef RLIMIT_NOFILE
385 "\0""open files" // -n
386#endif
387#ifdef RLIMIT_MSGQUEUE
388 "\0""POSIX message queues (bytes)" // -q
389#endif
390#ifdef RLIMIT_RTPRIO
391 "\0""real-time priority" // -r
392#endif
393#ifdef RLIMIT_STACK
394 "\0""stack size (kb)" // -s
395#endif
396#ifdef RLIMIT_CPU
397 "\0""cpu time (seconds)" // -t
398#endif
399#ifdef RLIMIT_NPROC
400 "\0""max user processes" // -u
401#endif
402#ifdef RLIMIT_AS
403 "\0""virtual memory (kb)" // -v
404#endif
405#ifdef RLIMIT_LOCKS
406 "\0""file locks" // -x
407#endif
408;
409
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200410static const char limit_chars[] ALIGN1 =
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200411 "c"
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200412 "d"
413 "e"
414 "f"
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200415#ifdef RLIMIT_SIGPENDING
416 "i"
417#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200418#ifdef RLIMIT_MEMLOCK
419 "l"
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200420#endif
421#ifdef RLIMIT_RSS
422 "m"
423#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200424#ifdef RLIMIT_NOFILE
425 "n"
426#endif
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200427#ifdef RLIMIT_MSGQUEUE
428 "q"
429#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200430#ifdef RLIMIT_RTPRIO
431 "r"
432#endif
433#ifdef RLIMIT_STACK
434 "s"
435#endif
436#ifdef RLIMIT_CPU
437 "t"
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200438#endif
439#ifdef RLIMIT_NPROC
Denys Vlasenkoa92a9602019-04-27 21:23:39 +0200440 "u"
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200441#endif
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200442#ifdef RLIMIT_AS
443 "v"
444#endif
445#ifdef RLIMIT_LOCKS
Denys Vlasenkoa92a9602019-04-27 21:23:39 +0200446 "x"
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200447#endif
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200448;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200449
450/* "-": treat args as parameters of option with ASCII code 1 */
Denys Vlasenko3e134eb2016-04-22 18:09:21 +0200451static const char ulimit_opt_string[] ALIGN1 = "-HSa"
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200452 "c::"
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200453 "d::"
454 "e::"
455 "f::"
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200456#ifdef RLIMIT_SIGPENDING
457 "i::"
458#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200459#ifdef RLIMIT_MEMLOCK
460 "l::"
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200461#endif
462#ifdef RLIMIT_RSS
463 "m::"
464#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200465#ifdef RLIMIT_NOFILE
466 "n::"
467#endif
Denys Vlasenko93f0b392019-04-28 11:25:11 +0200468#ifdef RLIMIT_MSGQUEUE
469 "q::"
470#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200471#ifdef RLIMIT_RTPRIO
472 "r::"
473#endif
474#ifdef RLIMIT_STACK
475 "s::"
476#endif
477#ifdef RLIMIT_CPU
478 "t::"
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200479#endif
480#ifdef RLIMIT_NPROC
Denys Vlasenkoa92a9602019-04-27 21:23:39 +0200481 "u::"
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200482#endif
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200483#ifdef RLIMIT_AS
484 "v::"
485#endif
486#ifdef RLIMIT_LOCKS
Denys Vlasenkoa92a9602019-04-27 21:23:39 +0200487 "x::"
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200488#endif
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200489;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200490
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200491enum {
492 OPT_hard = (1 << 0),
493 OPT_soft = (1 << 1),
494 OPT_all = (1 << 2),
495};
496
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200497static void printlim(unsigned opts, const struct rlimit *limit,
498 const struct limits *l)
499{
500 rlim_t val;
501
502 val = limit->rlim_max;
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200503 if (opts & OPT_soft)
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200504 val = limit->rlim_cur;
505
506 if (val == RLIM_INFINITY)
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200507 puts("unlimited");
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200508 else {
509 val >>= l->factor_shift;
510 printf("%llu\n", (long long) val);
511 }
512}
513
514int FAST_FUNC
515shell_builtin_ulimit(char **argv)
516{
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200517 struct rlimit limit;
518 unsigned opt_cnt;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200519 unsigned opts;
520 unsigned argc;
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200521 unsigned i;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200522
523 /* We can't use getopt32: need to handle commands like
524 * ulimit 123 -c2 -l 456
525 */
526
Denys Vlasenko60161812017-08-29 14:32:17 +0200527 /* In case getopt() was already called:
528 * reset libc getopt() internal state.
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200529 */
Kaarle Ritvanen835ad3a2017-04-12 00:58:46 +0300530 GETOPT_RESET();
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200531
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200532// bash 4.4.23:
533//
534// -H and/or -S change meaning even of options *before* them: ulimit -f 2000 -H
535// sets hard limit, ulimit -a -H prints hard limits.
536//
537// -a is equivalent for requesting all limits to be shown.
538//
539// If -a is specified, attempts to set limits are ignored:
540// ulimit -m 1000; ulimit -m 2000 -a
541// shows 1000, not 2000. HOWEVER, *implicit* -f form "ulimit 2000 -a"
542// DOES set -f limit [we don't implement this quirk], "ulimit -a 2000" does not.
543// Options are still parsed: ulimit -az complains about unknown -z opt.
544//
545// -a is not cumulative: "ulimit -a -a" = "ulimit -a -f -m" = "ulimit -a"
546//
547// -HSa can be combined in one argument and with one other option (example: -Sm),
548// but other options can't: limit value is an optional argument,
549// thus "-mf" means "-m f", f is the parameter of -m.
550//
551// Limit can be set and then printed: ulimit -m 2000 -m
552// If set more than once, they are set and printed in order:
553// try ulimit -m -m 1000 -m -m 2000 -m -m 3000 -m
554//
555// Limits are shown in the order of options given:
556// ulimit -m -f is not the same as ulimit -f -m.
557//
558// If both -S and -H are given, show soft limit.
559//
560// Short printout (limit value only) is printed only if just one option
561// is given: ulimit -m. ulimit -f -m prints verbose lines.
562// ulimit -f -f prints same verbose line twice.
563// ulimit -m 10000 -f prints verbose line for -f.
564
Denys Vlasenkod4e4fdb2017-07-03 21:31:16 +0200565 argc = string_array_len(argv);
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200566
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200567 /* First pass over options: detect -H/-S/-a status,
568 * and "bare ulimit" and "only one option" cases
569 * by counting other opts.
570 */
571 opt_cnt = 0;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200572 opts = 0;
573 while (1) {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200574 int opt_char = getopt(argc, argv, ulimit_opt_string);
575
576 if (opt_char == -1)
577 break;
578 if (opt_char == 'H') {
579 opts |= OPT_hard;
580 continue;
581 }
582 if (opt_char == 'S') {
583 opts |= OPT_soft;
584 continue;
585 }
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200586 if (opt_char == 'a') {
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200587 opts |= OPT_all;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200588 continue;
589 }
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200590 if (opt_char == '?') {
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200591 /* bad option. getopt already complained. */
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200592 return EXIT_FAILURE;
593 }
594 opt_cnt++;
595 } /* while (there are options) */
596
597 if (!(opts & (OPT_hard | OPT_soft)))
598 opts |= (OPT_hard | OPT_soft);
599 if (opts & OPT_all) {
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200600 const char *help = limits_help;
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200601 for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) {
602 getrlimit(limits_tbl[i].cmd, &limit);
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200603 printf("%-32s(-%c) ", help, limit_chars[i]);
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200604 printlim(opts, &limit, &limits_tbl[i]);
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200605 help += strlen(help) + 1;
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200606 }
607 return EXIT_SUCCESS;
608 }
609
610 /* Second pass: set or print limits, in order */
611 GETOPT_RESET();
612 while (1) {
613 char *val_str;
614 int opt_char = getopt(argc, argv, ulimit_opt_string);
615
616 if (opt_char == -1)
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200617 break;
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200618 if (opt_char == 'H')
619 continue;
620 if (opt_char == 'S')
621 continue;
622 //if (opt_char == 'a') - impossible
623
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200624 if (opt_char == 1) /* if "ulimit NNN", -f is assumed */
625 opt_char = 'f';
626 i = strchrnul(limit_chars, opt_char) - limit_chars;
627 //if (i >= ARRAY_SIZE(limits_tbl)) - bad option, impossible
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200628
629 val_str = optarg;
630 if (!val_str && argv[optind] && argv[optind][0] != '-')
631 val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
632
633 getrlimit(limits_tbl[i].cmd, &limit);
634 if (!val_str) {
635 if (opt_cnt > 1)
Denys Vlasenko3ef513e2019-10-21 16:47:09 +0200636 printf("%-32s(-%c) ", nth_string(limits_help, i), limit_chars[i]);
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200637 printlim(opts, &limit, &limits_tbl[i]);
638 } else {
639 rlim_t val = RLIM_INFINITY;
640 if (strcmp(val_str, "unlimited") != 0) {
641 if (sizeof(val) == sizeof(int))
642 val = bb_strtou(val_str, NULL, 10);
643 else if (sizeof(val) == sizeof(long))
644 val = bb_strtoul(val_str, NULL, 10);
645 else
646 val = bb_strtoull(val_str, NULL, 10);
647 if (errno) {
648 bb_error_msg("invalid number '%s'", val_str);
649 return EXIT_FAILURE;
650 }
651 val <<= limits_tbl[i].factor_shift;
652 }
653//bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
654 /* from man bash: "If neither -H nor -S
655 * is specified, both the soft and hard
656 * limits are set. */
657 if (opts & OPT_hard)
658 limit.rlim_max = val;
659 if (opts & OPT_soft)
660 limit.rlim_cur = val;
661//bb_error_msg("setrlimit(%d, %lld, %lld)", limits_tbl[i].cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
662 if (setrlimit(limits_tbl[i].cmd, &limit) < 0) {
James Byrne69374872019-07-02 11:35:03 +0200663 bb_simple_perror_msg("error setting limit");
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200664 return EXIT_FAILURE;
665 }
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200666 }
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200667 } /* while (there are options) */
668
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200669 if (opt_cnt == 0) {
670 /* "bare ulimit": treat it as if it was -f */
Denys Vlasenko57e1b0a2019-04-28 11:20:09 +0200671 getrlimit(limits_tbl[LIMIT_F_IDX].cmd, &limit);
672 printlim(opts, &limit, &limits_tbl[LIMIT_F_IDX]);
Denys Vlasenkoa4d76ea2019-04-27 21:01:35 +0200673 }
674
675 return EXIT_SUCCESS;
Denys Vlasenko599ae1e2010-05-23 17:49:50 +0200676}