Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 1 | /* 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 Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 17 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 18 | */ |
| 19 | #include "libbb.h" |
| 20 | #include "shell_common.h" |
| 21 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 22 | const char defifsvar[] ALIGN1 = "IFS= \t\n"; |
Denys Vlasenko | e627ac9 | 2016-09-30 14:36:59 +0200 | [diff] [blame] | 23 | const char defoptindvar[] ALIGN1 = "OPTIND=1"; |
Denys Vlasenko | 25d9b91 | 2010-01-13 18:22:35 +0100 | [diff] [blame] | 24 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 25 | /* read builtin */ |
| 26 | |
Denys Vlasenko | 25ce3ee | 2013-07-14 01:23:06 +0200 | [diff] [blame] | 27 | /* Needs to be interruptible: shell must handle traps and shell-special signals |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 28 | * while inside read. To implement this, be sure to not loop on EINTR |
| 29 | * and return errno == EINTR reliably. |
| 30 | */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 31 | //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. |
| 35 | const char* FAST_FUNC |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 36 | shell_builtin_read(struct builtin_read_params *params) |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 37 | { |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 38 | struct pollfd pfd[1]; |
| 39 | #define fd (pfd[0].fd) /* -u FD */ |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 40 | unsigned err; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 41 | unsigned end_ms; /* -t TIMEOUT */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 42 | int nchars; /* -n NUM */ |
| 43 | char **pp; |
| 44 | char *buffer; |
Denys Vlasenko | cde46f7 | 2017-08-09 14:04:07 +0200 | [diff] [blame] | 45 | char delim; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 46 | 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 Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 51 | char **argv; |
| 52 | const char *ifs; |
| 53 | int read_flags; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 54 | |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 55 | errno = err = 0; |
| 56 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 57 | argv = params->argv; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 58 | pp = argv; |
| 59 | while (*pp) { |
Denys Vlasenko | d8bd701 | 2019-05-14 18:53:24 +0200 | [diff] [blame] | 60 | if (endofname(*pp)[0] != '\0') { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 61 | /* Mimic bash message */ |
Denys Vlasenko | 457825f | 2021-06-06 12:07:11 +0200 | [diff] [blame] | 62 | bb_error_msg("read: '%s': bad variable name", *pp); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 63 | return (const char *)(uintptr_t)1; |
| 64 | } |
| 65 | pp++; |
| 66 | } |
| 67 | |
| 68 | nchars = 0; /* if != 0, -n is in effect */ |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 69 | if (params->opt_n) { |
| 70 | nchars = bb_strtou(params->opt_n, NULL, 10); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 71 | if (nchars < 0 || errno) |
| 72 | return "invalid count"; |
| 73 | /* note: "-n 0": off (bash 3.2 does this too) */ |
| 74 | } |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 75 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 76 | end_ms = 0; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 77 | if (params->opt_t && !ENABLE_FEATURE_SH_READ_FRAC) { |
| 78 | end_ms = bb_strtou(params->opt_t, NULL, 10); |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 79 | if (errno) |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 80 | return "invalid timeout"; |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 81 | if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */ |
| 82 | end_ms = UINT_MAX / 2048; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 83 | end_ms *= 1000; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 84 | } |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 85 | if (params->opt_t && ENABLE_FEATURE_SH_READ_FRAC) { |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 86 | /* 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 Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 91 | end_ms = bb_strtou(params->opt_t, &p, 10); |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 92 | 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 Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 112 | fd = STDIN_FILENO; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 113 | if (params->opt_u) { |
| 114 | fd = bb_strtou(params->opt_u, NULL, 10); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 115 | if (fd < 0 || errno) |
| 116 | return "invalid file descriptor"; |
| 117 | } |
| 118 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 119 | if (params->opt_t && end_ms == 0) { |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 120 | /* "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 Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 132 | if (params->opt_p && isatty(fd)) { |
| 133 | fputs(params->opt_p, stderr); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 134 | fflush_all(); |
| 135 | } |
| 136 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 137 | ifs = params->ifs; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 138 | if (ifs == NULL) |
| 139 | ifs = defifs; |
| 140 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 141 | read_flags = params->read_flags; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 142 | if (nchars || (read_flags & BUILTIN_READ_SILENT)) { |
| 143 | tcgetattr(fd, &tty); |
| 144 | old_tty = tty; |
| 145 | if (nchars) { |
| 146 | tty.c_lflag &= ~ICANON; |
Denys Vlasenko | 7ce209b | 2012-01-15 22:58:06 +0100 | [diff] [blame] | 147 | // 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 Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 150 | /* reads will block only if < 1 char is available */ |
Denys Vlasenko | 7ce209b | 2012-01-15 22:58:06 +0100 | [diff] [blame] | 151 | tty.c_cc[VMIN] = 1; |
| 152 | /* no timeout (reads block forever) */ |
| 153 | tty.c_cc[VTIME] = 0; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 154 | } |
| 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 Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 168 | if (params->opt_t) |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 169 | end_ms += (unsigned)monotonic_ms(); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 170 | buffer = NULL; |
| 171 | bufpos = 0; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 172 | delim = params->opt_d ? params->opt_d[0] : '\n'; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 173 | do { |
| 174 | char c; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 175 | int timeout; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 176 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 177 | if ((bufpos & 0xff) == 0) |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 178 | buffer = xrealloc(buffer, bufpos + 0x101); |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 179 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 180 | timeout = -1; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 181 | if (params->opt_t) { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 182 | timeout = end_ms - (unsigned)monotonic_ms(); |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 183 | /* ^^^^^^^^^^^^^ all values are unsigned, |
| 184 | * wrapping math is used here, good even if |
| 185 | * 32-bit unix time wrapped (year 2038+). |
| 186 | */ |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 187 | if (timeout <= 0) { /* already late? */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 188 | retval = (const char *)(uintptr_t)1; |
| 189 | goto ret; |
| 190 | } |
| 191 | } |
| 192 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 193 | /* 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 Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 198 | pfd[0].events = POLLIN; |
Denys Vlasenko | a277506 | 2022-01-16 23:54:46 +0100 | [diff] [blame] | 199 | //TODO race with a signal arriving just before the poll! |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 200 | if (poll(pfd, 1, timeout) <= 0) { |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 201 | /* timed out, or EINTR */ |
| 202 | err = errno; |
| 203 | retval = (const char *)(uintptr_t)1; |
| 204 | goto ret; |
| 205 | } |
| 206 | if (read(fd, &buffer[bufpos], 1) != 1) { |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 207 | err = errno; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 208 | retval = (const char *)(uintptr_t)1; |
| 209 | break; |
| 210 | } |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 211 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 212 | c = buffer[bufpos]; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 213 | if (!(read_flags & BUILTIN_READ_RAW)) { |
| 214 | if (backslash) { |
| 215 | backslash = 0; |
| 216 | if (c != '\n') |
| 217 | goto put; |
| 218 | continue; |
| 219 | } |
| 220 | if (c == '\\') { |
| 221 | backslash = 1; |
| 222 | continue; |
| 223 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 224 | } |
Denys Vlasenko | cde46f7 | 2017-08-09 14:04:07 +0200 | [diff] [blame] | 225 | if (c == delim) /* '\n' or -d CHAR */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 226 | break; |
Christian Eggers | 3992502 | 2020-06-29 17:57:24 +0200 | [diff] [blame] | 227 | if (c == '\0') |
| 228 | continue; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 229 | |
| 230 | /* $IFS splitting. NOT done if we run "read" |
| 231 | * without variable names (bash compat). |
| 232 | * Thus, "read" and "read REPLY" are not the same. |
| 233 | */ |
Eicke Herbertz | 1b30c63 | 2021-06-05 11:42:06 +0000 | [diff] [blame] | 234 | if (argv[0]) { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 235 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */ |
| 236 | const char *is_ifs = strchr(ifs, c); |
| 237 | if (startword && is_ifs) { |
| 238 | if (isspace(c)) |
| 239 | continue; |
| 240 | /* it is a non-space ifs char */ |
| 241 | startword--; |
| 242 | if (startword == 1) /* first one? */ |
| 243 | continue; /* yes, it is not next word yet */ |
| 244 | } |
| 245 | startword = 0; |
| 246 | if (argv[1] != NULL && is_ifs) { |
| 247 | buffer[bufpos] = '\0'; |
| 248 | bufpos = 0; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 249 | params->setvar(*argv, buffer); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 250 | argv++; |
| 251 | /* can we skip one non-space ifs char? (2: yes) */ |
| 252 | startword = isspace(c) ? 2 : 1; |
| 253 | continue; |
| 254 | } |
| 255 | } |
| 256 | put: |
| 257 | bufpos++; |
| 258 | } while (--nchars); |
| 259 | |
| 260 | if (argv[0]) { |
| 261 | /* Remove trailing space $IFS chars */ |
Denys Vlasenko | 44257ad | 2018-04-11 17:18:34 +0200 | [diff] [blame] | 262 | while (--bufpos >= 0 |
| 263 | && isspace(buffer[bufpos]) |
| 264 | && strchr(ifs, buffer[bufpos]) != NULL |
| 265 | ) { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 266 | continue; |
Denys Vlasenko | 44257ad | 2018-04-11 17:18:34 +0200 | [diff] [blame] | 267 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 268 | buffer[bufpos + 1] = '\0'; |
Denys Vlasenko | 44257ad | 2018-04-11 17:18:34 +0200 | [diff] [blame] | 269 | |
| 270 | /* Last variable takes the entire remainder with delimiters |
| 271 | * (sans trailing whitespace $IFS), |
| 272 | * but ***only "if there are fewer vars than fields"(c)***! |
| 273 | * The "X:Y:" case below: there are two fields, |
| 274 | * and therefore last delimiter (:) is eaten: |
| 275 | * IFS=": " |
| 276 | * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:| |
| 277 | * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z| |
| 278 | * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:| |
| 279 | * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y| |
| 280 | */ |
| 281 | if (bufpos >= 0 |
| 282 | && strchr(ifs, buffer[bufpos]) != NULL |
| 283 | ) { |
| 284 | /* There _is_ a non-whitespace IFS char */ |
| 285 | /* Skip whitespace IFS char before it */ |
| 286 | while (--bufpos >= 0 |
| 287 | && isspace(buffer[bufpos]) |
| 288 | && strchr(ifs, buffer[bufpos]) != NULL |
| 289 | ) { |
| 290 | continue; |
| 291 | } |
| 292 | /* Are there $IFS chars? */ |
| 293 | if (strcspn(buffer, ifs) >= ++bufpos) { |
| 294 | /* No: last var takes one field, not more */ |
| 295 | /* So, drop trailing IFS delims */ |
| 296 | buffer[bufpos] = '\0'; |
| 297 | } |
| 298 | } |
| 299 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 300 | /* Use the remainder as a value for the next variable */ |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 301 | params->setvar(*argv, buffer); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 302 | /* Set the rest to "" */ |
| 303 | while (*++argv) |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 304 | params->setvar(*argv, ""); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 305 | } else { |
| 306 | /* Note: no $IFS removal */ |
| 307 | buffer[bufpos] = '\0'; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 308 | params->setvar("REPLY", buffer); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | ret: |
| 312 | free(buffer); |
| 313 | if (read_flags & BUILTIN_READ_SILENT) |
| 314 | tcsetattr(fd, TCSANOW, &old_tty); |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 315 | |
| 316 | errno = err; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 317 | return retval; |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 318 | #undef fd |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* ulimit builtin */ |
| 322 | |
| 323 | struct limits { |
| 324 | uint8_t cmd; /* RLIMIT_xxx fit into it */ |
| 325 | uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 326 | }; |
| 327 | |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 328 | /* Order of entries matches order in which bash prints "ulimit -a" */ |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 329 | static const struct limits limits_tbl[] ALIGN2 = { |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 330 | { RLIMIT_CORE, 9, }, // -c |
| 331 | { RLIMIT_DATA, 10, }, // -d |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 332 | #ifdef RLIMIT_NICE |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 333 | { RLIMIT_NICE, 0, }, // -e |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 334 | #define LIMIT_F_IDX 3 |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 335 | #else |
| 336 | /* for example, Hurd */ |
| 337 | #define LIMIT_F_IDX 2 |
| 338 | #endif |
| 339 | { RLIMIT_FSIZE, 9, }, // -f |
Denys Vlasenko | 93f0b39 | 2019-04-28 11:25:11 +0200 | [diff] [blame] | 340 | #ifdef RLIMIT_SIGPENDING |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 341 | { RLIMIT_SIGPENDING, 0, }, // -i |
Denys Vlasenko | 93f0b39 | 2019-04-28 11:25:11 +0200 | [diff] [blame] | 342 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 343 | #ifdef RLIMIT_MEMLOCK |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 344 | { RLIMIT_MEMLOCK, 10, }, // -l |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 345 | #endif |
| 346 | #ifdef RLIMIT_RSS |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 347 | { RLIMIT_RSS, 10, }, // -m |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 348 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 349 | #ifdef RLIMIT_NOFILE |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 350 | { RLIMIT_NOFILE, 0, }, // -n |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 351 | #endif |
Denys Vlasenko | 93f0b39 | 2019-04-28 11:25:11 +0200 | [diff] [blame] | 352 | #ifdef RLIMIT_MSGQUEUE |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 353 | { RLIMIT_MSGQUEUE, 0, }, // -q |
Denys Vlasenko | 93f0b39 | 2019-04-28 11:25:11 +0200 | [diff] [blame] | 354 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 355 | #ifdef RLIMIT_RTPRIO |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 356 | { RLIMIT_RTPRIO, 0, }, // -r |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 357 | #endif |
| 358 | #ifdef RLIMIT_STACK |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 359 | { RLIMIT_STACK, 10, }, // -s |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 360 | #endif |
| 361 | #ifdef RLIMIT_CPU |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 362 | { RLIMIT_CPU, 0, }, // -t |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 363 | #endif |
| 364 | #ifdef RLIMIT_NPROC |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 365 | { RLIMIT_NPROC, 0, }, // -u |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 366 | #endif |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 367 | #ifdef RLIMIT_AS |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 368 | { RLIMIT_AS, 10, }, // -v |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 369 | #endif |
| 370 | #ifdef RLIMIT_LOCKS |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 371 | { RLIMIT_LOCKS, 0, }, // -x |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 372 | #endif |
| 373 | }; |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 374 | // 1) bash also shows: |
Denys Vlasenko | a92a960 | 2019-04-27 21:23:39 +0200 | [diff] [blame] | 375 | //pipe size (512 bytes, -p) 8 |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 376 | // 2) RLIMIT_RTTIME ("timeout for RT tasks in us") is not in the table |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 377 | |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 378 | static const char limits_help[] ALIGN1 = |
| 379 | "core file size (blocks)" // -c |
| 380 | "\0""data seg size (kb)" // -d |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 381 | #ifdef RLIMIT_NICE |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 382 | "\0""scheduling priority" // -e |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 383 | #endif |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 384 | "\0""file size (blocks)" // -f |
| 385 | #ifdef RLIMIT_SIGPENDING |
| 386 | "\0""pending signals" // -i |
| 387 | #endif |
| 388 | #ifdef RLIMIT_MEMLOCK |
| 389 | "\0""max locked memory (kb)" // -l |
| 390 | #endif |
| 391 | #ifdef RLIMIT_RSS |
| 392 | "\0""max memory size (kb)" // -m |
| 393 | #endif |
| 394 | #ifdef RLIMIT_NOFILE |
| 395 | "\0""open files" // -n |
| 396 | #endif |
| 397 | #ifdef RLIMIT_MSGQUEUE |
| 398 | "\0""POSIX message queues (bytes)" // -q |
| 399 | #endif |
| 400 | #ifdef RLIMIT_RTPRIO |
| 401 | "\0""real-time priority" // -r |
| 402 | #endif |
| 403 | #ifdef RLIMIT_STACK |
| 404 | "\0""stack size (kb)" // -s |
| 405 | #endif |
| 406 | #ifdef RLIMIT_CPU |
| 407 | "\0""cpu time (seconds)" // -t |
| 408 | #endif |
| 409 | #ifdef RLIMIT_NPROC |
| 410 | "\0""max user processes" // -u |
| 411 | #endif |
| 412 | #ifdef RLIMIT_AS |
| 413 | "\0""virtual memory (kb)" // -v |
| 414 | #endif |
| 415 | #ifdef RLIMIT_LOCKS |
| 416 | "\0""file locks" // -x |
| 417 | #endif |
| 418 | ; |
| 419 | |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 420 | static const char limit_chars[] ALIGN1 = |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 421 | "c" |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 422 | "d" |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 423 | #ifdef RLIMIT_NICE |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 424 | "e" |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 425 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 426 | "f" |
Denys Vlasenko | 93f0b39 | 2019-04-28 11:25:11 +0200 | [diff] [blame] | 427 | #ifdef RLIMIT_SIGPENDING |
| 428 | "i" |
| 429 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 430 | #ifdef RLIMIT_MEMLOCK |
| 431 | "l" |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 432 | #endif |
| 433 | #ifdef RLIMIT_RSS |
| 434 | "m" |
| 435 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 436 | #ifdef RLIMIT_NOFILE |
| 437 | "n" |
| 438 | #endif |
Denys Vlasenko | 93f0b39 | 2019-04-28 11:25:11 +0200 | [diff] [blame] | 439 | #ifdef RLIMIT_MSGQUEUE |
| 440 | "q" |
| 441 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 442 | #ifdef RLIMIT_RTPRIO |
| 443 | "r" |
| 444 | #endif |
| 445 | #ifdef RLIMIT_STACK |
| 446 | "s" |
| 447 | #endif |
| 448 | #ifdef RLIMIT_CPU |
| 449 | "t" |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 450 | #endif |
| 451 | #ifdef RLIMIT_NPROC |
Denys Vlasenko | a92a960 | 2019-04-27 21:23:39 +0200 | [diff] [blame] | 452 | "u" |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 453 | #endif |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 454 | #ifdef RLIMIT_AS |
| 455 | "v" |
| 456 | #endif |
| 457 | #ifdef RLIMIT_LOCKS |
Denys Vlasenko | a92a960 | 2019-04-27 21:23:39 +0200 | [diff] [blame] | 458 | "x" |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 459 | #endif |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 460 | ; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 461 | |
| 462 | /* "-": treat args as parameters of option with ASCII code 1 */ |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 463 | static const char ulimit_opt_string[] ALIGN1 = "-HSa" |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 464 | "c::" |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 465 | "d::" |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 466 | #ifdef RLIMIT_NICE |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 467 | "e::" |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 468 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 469 | "f::" |
Denys Vlasenko | 93f0b39 | 2019-04-28 11:25:11 +0200 | [diff] [blame] | 470 | #ifdef RLIMIT_SIGPENDING |
| 471 | "i::" |
| 472 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 473 | #ifdef RLIMIT_MEMLOCK |
| 474 | "l::" |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 475 | #endif |
| 476 | #ifdef RLIMIT_RSS |
| 477 | "m::" |
| 478 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 479 | #ifdef RLIMIT_NOFILE |
| 480 | "n::" |
| 481 | #endif |
Denys Vlasenko | 93f0b39 | 2019-04-28 11:25:11 +0200 | [diff] [blame] | 482 | #ifdef RLIMIT_MSGQUEUE |
| 483 | "q::" |
| 484 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 485 | #ifdef RLIMIT_RTPRIO |
| 486 | "r::" |
| 487 | #endif |
| 488 | #ifdef RLIMIT_STACK |
| 489 | "s::" |
| 490 | #endif |
| 491 | #ifdef RLIMIT_CPU |
| 492 | "t::" |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 493 | #endif |
| 494 | #ifdef RLIMIT_NPROC |
Denys Vlasenko | a92a960 | 2019-04-27 21:23:39 +0200 | [diff] [blame] | 495 | "u::" |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 496 | #endif |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 497 | #ifdef RLIMIT_AS |
| 498 | "v::" |
| 499 | #endif |
| 500 | #ifdef RLIMIT_LOCKS |
Denys Vlasenko | a92a960 | 2019-04-27 21:23:39 +0200 | [diff] [blame] | 501 | "x::" |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 502 | #endif |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 503 | ; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 504 | |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 505 | enum { |
| 506 | OPT_hard = (1 << 0), |
| 507 | OPT_soft = (1 << 1), |
| 508 | OPT_all = (1 << 2), |
| 509 | }; |
| 510 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 511 | static void printlim(unsigned opts, const struct rlimit *limit, |
| 512 | const struct limits *l) |
| 513 | { |
| 514 | rlim_t val; |
| 515 | |
| 516 | val = limit->rlim_max; |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 517 | if (opts & OPT_soft) |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 518 | val = limit->rlim_cur; |
| 519 | |
| 520 | if (val == RLIM_INFINITY) |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 521 | puts("unlimited"); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 522 | else { |
| 523 | val >>= l->factor_shift; |
| 524 | printf("%llu\n", (long long) val); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | int FAST_FUNC |
| 529 | shell_builtin_ulimit(char **argv) |
| 530 | { |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 531 | struct rlimit limit; |
| 532 | unsigned opt_cnt; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 533 | unsigned opts; |
| 534 | unsigned argc; |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 535 | unsigned i; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 536 | |
| 537 | /* We can't use getopt32: need to handle commands like |
| 538 | * ulimit 123 -c2 -l 456 |
| 539 | */ |
| 540 | |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 541 | /* In case getopt() was already called: |
| 542 | * reset libc getopt() internal state. |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 543 | */ |
Kaarle Ritvanen | 835ad3a | 2017-04-12 00:58:46 +0300 | [diff] [blame] | 544 | GETOPT_RESET(); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 545 | |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 546 | // bash 4.4.23: |
| 547 | // |
| 548 | // -H and/or -S change meaning even of options *before* them: ulimit -f 2000 -H |
| 549 | // sets hard limit, ulimit -a -H prints hard limits. |
| 550 | // |
| 551 | // -a is equivalent for requesting all limits to be shown. |
| 552 | // |
| 553 | // If -a is specified, attempts to set limits are ignored: |
| 554 | // ulimit -m 1000; ulimit -m 2000 -a |
| 555 | // shows 1000, not 2000. HOWEVER, *implicit* -f form "ulimit 2000 -a" |
| 556 | // DOES set -f limit [we don't implement this quirk], "ulimit -a 2000" does not. |
| 557 | // Options are still parsed: ulimit -az complains about unknown -z opt. |
| 558 | // |
| 559 | // -a is not cumulative: "ulimit -a -a" = "ulimit -a -f -m" = "ulimit -a" |
| 560 | // |
| 561 | // -HSa can be combined in one argument and with one other option (example: -Sm), |
| 562 | // but other options can't: limit value is an optional argument, |
| 563 | // thus "-mf" means "-m f", f is the parameter of -m. |
| 564 | // |
| 565 | // Limit can be set and then printed: ulimit -m 2000 -m |
| 566 | // If set more than once, they are set and printed in order: |
| 567 | // try ulimit -m -m 1000 -m -m 2000 -m -m 3000 -m |
| 568 | // |
| 569 | // Limits are shown in the order of options given: |
| 570 | // ulimit -m -f is not the same as ulimit -f -m. |
| 571 | // |
| 572 | // If both -S and -H are given, show soft limit. |
| 573 | // |
| 574 | // Short printout (limit value only) is printed only if just one option |
| 575 | // is given: ulimit -m. ulimit -f -m prints verbose lines. |
| 576 | // ulimit -f -f prints same verbose line twice. |
| 577 | // ulimit -m 10000 -f prints verbose line for -f. |
| 578 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 579 | argc = string_array_len(argv); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 580 | |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 581 | /* First pass over options: detect -H/-S/-a status, |
| 582 | * and "bare ulimit" and "only one option" cases |
| 583 | * by counting other opts. |
| 584 | */ |
| 585 | opt_cnt = 0; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 586 | opts = 0; |
| 587 | while (1) { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 588 | int opt_char = getopt(argc, argv, ulimit_opt_string); |
| 589 | |
| 590 | if (opt_char == -1) |
| 591 | break; |
| 592 | if (opt_char == 'H') { |
| 593 | opts |= OPT_hard; |
| 594 | continue; |
| 595 | } |
| 596 | if (opt_char == 'S') { |
| 597 | opts |= OPT_soft; |
| 598 | continue; |
| 599 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 600 | if (opt_char == 'a') { |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 601 | opts |= OPT_all; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 602 | continue; |
| 603 | } |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 604 | if (opt_char == '?') { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 605 | /* bad option. getopt already complained. */ |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 606 | return EXIT_FAILURE; |
| 607 | } |
| 608 | opt_cnt++; |
| 609 | } /* while (there are options) */ |
| 610 | |
| 611 | if (!(opts & (OPT_hard | OPT_soft))) |
| 612 | opts |= (OPT_hard | OPT_soft); |
| 613 | if (opts & OPT_all) { |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 614 | const char *help = limits_help; |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 615 | for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) { |
| 616 | getrlimit(limits_tbl[i].cmd, &limit); |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 617 | printf("%-32s(-%c) ", help, limit_chars[i]); |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 618 | printlim(opts, &limit, &limits_tbl[i]); |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 619 | help += strlen(help) + 1; |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 620 | } |
| 621 | return EXIT_SUCCESS; |
| 622 | } |
| 623 | |
| 624 | /* Second pass: set or print limits, in order */ |
| 625 | GETOPT_RESET(); |
| 626 | while (1) { |
| 627 | char *val_str; |
| 628 | int opt_char = getopt(argc, argv, ulimit_opt_string); |
| 629 | |
| 630 | if (opt_char == -1) |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 631 | break; |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 632 | if (opt_char == 'H') |
| 633 | continue; |
| 634 | if (opt_char == 'S') |
| 635 | continue; |
| 636 | //if (opt_char == 'a') - impossible |
| 637 | |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 638 | if (opt_char == 1) /* if "ulimit NNN", -f is assumed */ |
| 639 | opt_char = 'f'; |
| 640 | i = strchrnul(limit_chars, opt_char) - limit_chars; |
| 641 | //if (i >= ARRAY_SIZE(limits_tbl)) - bad option, impossible |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 642 | |
| 643 | val_str = optarg; |
| 644 | if (!val_str && argv[optind] && argv[optind][0] != '-') |
| 645 | val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */ |
| 646 | |
| 647 | getrlimit(limits_tbl[i].cmd, &limit); |
| 648 | if (!val_str) { |
| 649 | if (opt_cnt > 1) |
Denys Vlasenko | 3ef513e | 2019-10-21 16:47:09 +0200 | [diff] [blame] | 650 | printf("%-32s(-%c) ", nth_string(limits_help, i), limit_chars[i]); |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 651 | printlim(opts, &limit, &limits_tbl[i]); |
| 652 | } else { |
| 653 | rlim_t val = RLIM_INFINITY; |
| 654 | if (strcmp(val_str, "unlimited") != 0) { |
| 655 | if (sizeof(val) == sizeof(int)) |
| 656 | val = bb_strtou(val_str, NULL, 10); |
| 657 | else if (sizeof(val) == sizeof(long)) |
| 658 | val = bb_strtoul(val_str, NULL, 10); |
| 659 | else |
| 660 | val = bb_strtoull(val_str, NULL, 10); |
| 661 | if (errno) { |
| 662 | bb_error_msg("invalid number '%s'", val_str); |
| 663 | return EXIT_FAILURE; |
| 664 | } |
| 665 | val <<= limits_tbl[i].factor_shift; |
| 666 | } |
| 667 | //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val); |
| 668 | /* from man bash: "If neither -H nor -S |
| 669 | * is specified, both the soft and hard |
| 670 | * limits are set. */ |
| 671 | if (opts & OPT_hard) |
| 672 | limit.rlim_max = val; |
| 673 | if (opts & OPT_soft) |
| 674 | limit.rlim_cur = val; |
| 675 | //bb_error_msg("setrlimit(%d, %lld, %lld)", limits_tbl[i].cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max); |
| 676 | if (setrlimit(limits_tbl[i].cmd, &limit) < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 677 | bb_simple_perror_msg("error setting limit"); |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 678 | return EXIT_FAILURE; |
| 679 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 680 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 681 | } /* while (there are options) */ |
| 682 | |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 683 | if (opt_cnt == 0) { |
| 684 | /* "bare ulimit": treat it as if it was -f */ |
Denys Vlasenko | 91e330a | 2020-12-17 12:07:54 +0100 | [diff] [blame] | 685 | getrlimit(RLIMIT_FSIZE, &limit); |
Denys Vlasenko | 57e1b0a | 2019-04-28 11:20:09 +0200 | [diff] [blame] | 686 | printlim(opts, &limit, &limits_tbl[LIMIT_F_IDX]); |
Denys Vlasenko | a4d76ea | 2019-04-27 21:01:35 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | return EXIT_SUCCESS; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 690 | } |