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 | |
| 25 | |
| 26 | int FAST_FUNC is_well_formed_var_name(const char *s, char terminator) |
| 27 | { |
| 28 | if (!s || !(isalpha(*s) || *s == '_')) |
| 29 | return 0; |
| 30 | |
| 31 | do |
| 32 | s++; |
| 33 | while (isalnum(*s) || *s == '_'); |
| 34 | |
| 35 | return *s == terminator; |
| 36 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 37 | |
| 38 | /* read builtin */ |
| 39 | |
Denys Vlasenko | 25ce3ee | 2013-07-14 01:23:06 +0200 | [diff] [blame] | 40 | /* Needs to be interruptible: shell must handle traps and shell-special signals |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 41 | * while inside read. To implement this, be sure to not loop on EINTR |
| 42 | * and return errno == EINTR reliably. |
| 43 | */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 44 | //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL" |
| 45 | //string. hush naturally has it, and ash has setvareq(). |
| 46 | //Here we can simply store "VAR=" at buffer start and store read data directly |
| 47 | //after "=", then pass buffer to setvar() to consume. |
| 48 | const char* FAST_FUNC |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 49 | shell_builtin_read(struct builtin_read_params *params) |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 50 | { |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 51 | struct pollfd pfd[1]; |
| 52 | #define fd (pfd[0].fd) /* -u FD */ |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 53 | unsigned err; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 54 | unsigned end_ms; /* -t TIMEOUT */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 55 | int nchars; /* -n NUM */ |
| 56 | char **pp; |
| 57 | char *buffer; |
Denys Vlasenko | cde46f7 | 2017-08-09 14:04:07 +0200 | [diff] [blame] | 58 | char delim; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 59 | struct termios tty, old_tty; |
| 60 | const char *retval; |
| 61 | int bufpos; /* need to be able to hold -1 */ |
| 62 | int startword; |
| 63 | smallint backslash; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 64 | char **argv; |
| 65 | const char *ifs; |
| 66 | int read_flags; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 67 | |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 68 | errno = err = 0; |
| 69 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 70 | argv = params->argv; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 71 | pp = argv; |
| 72 | while (*pp) { |
| 73 | if (!is_well_formed_var_name(*pp, '\0')) { |
| 74 | /* Mimic bash message */ |
| 75 | bb_error_msg("read: '%s': not a valid identifier", *pp); |
| 76 | return (const char *)(uintptr_t)1; |
| 77 | } |
| 78 | pp++; |
| 79 | } |
| 80 | |
| 81 | nchars = 0; /* if != 0, -n is in effect */ |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 82 | if (params->opt_n) { |
| 83 | nchars = bb_strtou(params->opt_n, NULL, 10); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 84 | if (nchars < 0 || errno) |
| 85 | return "invalid count"; |
| 86 | /* note: "-n 0": off (bash 3.2 does this too) */ |
| 87 | } |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 88 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 89 | end_ms = 0; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 90 | if (params->opt_t && !ENABLE_FEATURE_SH_READ_FRAC) { |
| 91 | end_ms = bb_strtou(params->opt_t, NULL, 10); |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 92 | if (errno) |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 93 | return "invalid timeout"; |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 94 | if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */ |
| 95 | end_ms = UINT_MAX / 2048; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 96 | end_ms *= 1000; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 97 | } |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 98 | if (params->opt_t && ENABLE_FEATURE_SH_READ_FRAC) { |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 99 | /* bash 4.3 (maybe earlier) supports -t N.NNNNNN */ |
| 100 | char *p; |
| 101 | /* Eat up to three fractional digits */ |
| 102 | int frac_digits = 3 + 1; |
| 103 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 104 | end_ms = bb_strtou(params->opt_t, &p, 10); |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 105 | if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */ |
| 106 | end_ms = UINT_MAX / 2048; |
| 107 | |
| 108 | if (errno) { |
| 109 | /* EINVAL = number is ok, but not NUL terminated */ |
| 110 | if (errno != EINVAL || *p != '.') |
| 111 | return "invalid timeout"; |
| 112 | /* Do not check the rest: bash allows "0.123456xyz" */ |
| 113 | while (*++p && --frac_digits) { |
| 114 | end_ms *= 10; |
| 115 | end_ms += (*p - '0'); |
| 116 | if ((unsigned char)(*p - '0') > 9) |
| 117 | return "invalid timeout"; |
| 118 | } |
| 119 | } |
| 120 | while (--frac_digits > 0) { |
| 121 | end_ms *= 10; |
| 122 | } |
| 123 | } |
| 124 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 125 | fd = STDIN_FILENO; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 126 | if (params->opt_u) { |
| 127 | fd = bb_strtou(params->opt_u, NULL, 10); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 128 | if (fd < 0 || errno) |
| 129 | return "invalid file descriptor"; |
| 130 | } |
| 131 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 132 | if (params->opt_t && end_ms == 0) { |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 133 | /* "If timeout is 0, read returns immediately, without trying |
| 134 | * to read any data. The exit status is 0 if input is available |
| 135 | * on the specified file descriptor, non-zero otherwise." |
| 136 | * bash seems to ignore -p PROMPT for this use case. |
| 137 | */ |
| 138 | int r; |
| 139 | pfd[0].events = POLLIN; |
| 140 | r = poll(pfd, 1, /*timeout:*/ 0); |
| 141 | /* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */ |
| 142 | return (const char *)(uintptr_t)(r <= 0); |
| 143 | } |
| 144 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 145 | if (params->opt_p && isatty(fd)) { |
| 146 | fputs(params->opt_p, stderr); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 147 | fflush_all(); |
| 148 | } |
| 149 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 150 | ifs = params->ifs; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 151 | if (ifs == NULL) |
| 152 | ifs = defifs; |
| 153 | |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 154 | read_flags = params->read_flags; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 155 | if (nchars || (read_flags & BUILTIN_READ_SILENT)) { |
| 156 | tcgetattr(fd, &tty); |
| 157 | old_tty = tty; |
| 158 | if (nchars) { |
| 159 | tty.c_lflag &= ~ICANON; |
Denys Vlasenko | 7ce209b | 2012-01-15 22:58:06 +0100 | [diff] [blame] | 160 | // Setting it to more than 1 breaks poll(): |
| 161 | // it blocks even if there's data. !?? |
| 162 | //tty.c_cc[VMIN] = nchars < 256 ? nchars : 255; |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 163 | /* reads will block only if < 1 char is available */ |
Denys Vlasenko | 7ce209b | 2012-01-15 22:58:06 +0100 | [diff] [blame] | 164 | tty.c_cc[VMIN] = 1; |
| 165 | /* no timeout (reads block forever) */ |
| 166 | tty.c_cc[VTIME] = 0; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 167 | } |
| 168 | if (read_flags & BUILTIN_READ_SILENT) { |
| 169 | tty.c_lflag &= ~(ECHO | ECHOK | ECHONL); |
| 170 | } |
| 171 | /* This forces execution of "restoring" tcgetattr later */ |
| 172 | read_flags |= BUILTIN_READ_SILENT; |
| 173 | /* if tcgetattr failed, tcsetattr will fail too. |
| 174 | * Ignoring, it's harmless. */ |
| 175 | tcsetattr(fd, TCSANOW, &tty); |
| 176 | } |
| 177 | |
| 178 | retval = (const char *)(uintptr_t)0; |
| 179 | startword = 1; |
| 180 | backslash = 0; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 181 | if (params->opt_t) |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 182 | end_ms += (unsigned)monotonic_ms(); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 183 | buffer = NULL; |
| 184 | bufpos = 0; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 185 | delim = params->opt_d ? params->opt_d[0] : '\n'; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 186 | do { |
| 187 | char c; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 188 | int timeout; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 189 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 190 | if ((bufpos & 0xff) == 0) |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 191 | buffer = xrealloc(buffer, bufpos + 0x101); |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 192 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 193 | timeout = -1; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 194 | if (params->opt_t) { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 195 | timeout = end_ms - (unsigned)monotonic_ms(); |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 196 | /* ^^^^^^^^^^^^^ all values are unsigned, |
| 197 | * wrapping math is used here, good even if |
| 198 | * 32-bit unix time wrapped (year 2038+). |
| 199 | */ |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 200 | if (timeout <= 0) { /* already late? */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 201 | retval = (const char *)(uintptr_t)1; |
| 202 | goto ret; |
| 203 | } |
| 204 | } |
| 205 | |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 206 | /* We must poll even if timeout is -1: |
| 207 | * we want to be interrupted if signal arrives, |
| 208 | * regardless of SA_RESTART-ness of that signal! |
| 209 | */ |
| 210 | errno = 0; |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 211 | pfd[0].events = POLLIN; |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 212 | if (poll(pfd, 1, timeout) <= 0) { |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 213 | /* timed out, or EINTR */ |
| 214 | err = errno; |
| 215 | retval = (const char *)(uintptr_t)1; |
| 216 | goto ret; |
| 217 | } |
| 218 | if (read(fd, &buffer[bufpos], 1) != 1) { |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 219 | err = errno; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 220 | retval = (const char *)(uintptr_t)1; |
| 221 | break; |
| 222 | } |
Denys Vlasenko | 10c0131 | 2011-05-11 11:49:21 +0200 | [diff] [blame] | 223 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 224 | c = buffer[bufpos]; |
| 225 | if (c == '\0') |
| 226 | continue; |
Denys Vlasenko | f547041 | 2017-05-22 19:34:45 +0200 | [diff] [blame] | 227 | if (!(read_flags & BUILTIN_READ_RAW)) { |
| 228 | if (backslash) { |
| 229 | backslash = 0; |
| 230 | if (c != '\n') |
| 231 | goto put; |
| 232 | continue; |
| 233 | } |
| 234 | if (c == '\\') { |
| 235 | backslash = 1; |
| 236 | continue; |
| 237 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 238 | } |
Denys Vlasenko | cde46f7 | 2017-08-09 14:04:07 +0200 | [diff] [blame] | 239 | if (c == delim) /* '\n' or -d CHAR */ |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 240 | break; |
| 241 | |
| 242 | /* $IFS splitting. NOT done if we run "read" |
| 243 | * without variable names (bash compat). |
| 244 | * Thus, "read" and "read REPLY" are not the same. |
| 245 | */ |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 246 | if (!params->opt_d && argv[0]) { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 247 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */ |
| 248 | const char *is_ifs = strchr(ifs, c); |
| 249 | if (startword && is_ifs) { |
| 250 | if (isspace(c)) |
| 251 | continue; |
| 252 | /* it is a non-space ifs char */ |
| 253 | startword--; |
| 254 | if (startword == 1) /* first one? */ |
| 255 | continue; /* yes, it is not next word yet */ |
| 256 | } |
| 257 | startword = 0; |
| 258 | if (argv[1] != NULL && is_ifs) { |
| 259 | buffer[bufpos] = '\0'; |
| 260 | bufpos = 0; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 261 | params->setvar(*argv, buffer); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 262 | argv++; |
| 263 | /* can we skip one non-space ifs char? (2: yes) */ |
| 264 | startword = isspace(c) ? 2 : 1; |
| 265 | continue; |
| 266 | } |
| 267 | } |
| 268 | put: |
| 269 | bufpos++; |
| 270 | } while (--nchars); |
| 271 | |
| 272 | if (argv[0]) { |
| 273 | /* Remove trailing space $IFS chars */ |
Denys Vlasenko | 44257ad | 2018-04-11 17:18:34 +0200 | [diff] [blame] | 274 | while (--bufpos >= 0 |
| 275 | && isspace(buffer[bufpos]) |
| 276 | && strchr(ifs, buffer[bufpos]) != NULL |
| 277 | ) { |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 278 | continue; |
Denys Vlasenko | 44257ad | 2018-04-11 17:18:34 +0200 | [diff] [blame] | 279 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 280 | buffer[bufpos + 1] = '\0'; |
Denys Vlasenko | 44257ad | 2018-04-11 17:18:34 +0200 | [diff] [blame] | 281 | |
| 282 | /* Last variable takes the entire remainder with delimiters |
| 283 | * (sans trailing whitespace $IFS), |
| 284 | * but ***only "if there are fewer vars than fields"(c)***! |
| 285 | * The "X:Y:" case below: there are two fields, |
| 286 | * and therefore last delimiter (:) is eaten: |
| 287 | * IFS=": " |
| 288 | * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:| |
| 289 | * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z| |
| 290 | * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:| |
| 291 | * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y| |
| 292 | */ |
| 293 | if (bufpos >= 0 |
| 294 | && strchr(ifs, buffer[bufpos]) != NULL |
| 295 | ) { |
| 296 | /* There _is_ a non-whitespace IFS char */ |
| 297 | /* Skip whitespace IFS char before it */ |
| 298 | while (--bufpos >= 0 |
| 299 | && isspace(buffer[bufpos]) |
| 300 | && strchr(ifs, buffer[bufpos]) != NULL |
| 301 | ) { |
| 302 | continue; |
| 303 | } |
| 304 | /* Are there $IFS chars? */ |
| 305 | if (strcspn(buffer, ifs) >= ++bufpos) { |
| 306 | /* No: last var takes one field, not more */ |
| 307 | /* So, drop trailing IFS delims */ |
| 308 | buffer[bufpos] = '\0'; |
| 309 | } |
| 310 | } |
| 311 | |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 312 | /* Use the remainder as a value for the next variable */ |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 313 | params->setvar(*argv, buffer); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 314 | /* Set the rest to "" */ |
| 315 | while (*++argv) |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 316 | params->setvar(*argv, ""); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 317 | } else { |
| 318 | /* Note: no $IFS removal */ |
| 319 | buffer[bufpos] = '\0'; |
Denys Vlasenko | 19358cc | 2018-08-05 15:42:29 +0200 | [diff] [blame] | 320 | params->setvar("REPLY", buffer); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | ret: |
| 324 | free(buffer); |
| 325 | if (read_flags & BUILTIN_READ_SILENT) |
| 326 | tcsetattr(fd, TCSANOW, &old_tty); |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 327 | |
| 328 | errno = err; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 329 | return retval; |
Denys Vlasenko | eae1268 | 2017-07-20 16:09:31 +0200 | [diff] [blame] | 330 | #undef fd |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* ulimit builtin */ |
| 334 | |
| 335 | struct limits { |
| 336 | uint8_t cmd; /* RLIMIT_xxx fit into it */ |
| 337 | uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */ |
| 338 | char option; |
| 339 | const char *name; |
| 340 | }; |
| 341 | |
| 342 | static const struct limits limits_tbl[] = { |
| 343 | #ifdef RLIMIT_FSIZE |
| 344 | { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" }, |
| 345 | #endif |
| 346 | #ifdef RLIMIT_CPU |
| 347 | { RLIMIT_CPU, 0, 't', "cpu time (seconds)" }, |
| 348 | #endif |
| 349 | #ifdef RLIMIT_DATA |
| 350 | { RLIMIT_DATA, 10, 'd', "data seg size (kb)" }, |
| 351 | #endif |
| 352 | #ifdef RLIMIT_STACK |
| 353 | { RLIMIT_STACK, 10, 's', "stack size (kb)" }, |
| 354 | #endif |
| 355 | #ifdef RLIMIT_CORE |
| 356 | { RLIMIT_CORE, 9, 'c', "core file size (blocks)" }, |
| 357 | #endif |
| 358 | #ifdef RLIMIT_RSS |
| 359 | { RLIMIT_RSS, 10, 'm', "resident set size (kb)" }, |
| 360 | #endif |
| 361 | #ifdef RLIMIT_MEMLOCK |
| 362 | { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" }, |
| 363 | #endif |
| 364 | #ifdef RLIMIT_NPROC |
| 365 | { RLIMIT_NPROC, 0, 'p', "processes" }, |
| 366 | #endif |
| 367 | #ifdef RLIMIT_NOFILE |
| 368 | { RLIMIT_NOFILE, 0, 'n', "file descriptors" }, |
| 369 | #endif |
| 370 | #ifdef RLIMIT_AS |
| 371 | { RLIMIT_AS, 10, 'v', "address space (kb)" }, |
| 372 | #endif |
| 373 | #ifdef RLIMIT_LOCKS |
| 374 | { RLIMIT_LOCKS, 0, 'w', "locks" }, |
| 375 | #endif |
Denys Vlasenko | e32d05b | 2011-04-04 02:12:14 +0200 | [diff] [blame] | 376 | #ifdef RLIMIT_NICE |
| 377 | { RLIMIT_NICE, 0, 'e', "scheduling priority" }, |
| 378 | #endif |
| 379 | #ifdef RLIMIT_RTPRIO |
| 380 | { RLIMIT_RTPRIO, 0, 'r', "real-time priority" }, |
| 381 | #endif |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | enum { |
| 385 | OPT_hard = (1 << 0), |
| 386 | OPT_soft = (1 << 1), |
| 387 | }; |
| 388 | |
| 389 | /* "-": treat args as parameters of option with ASCII code 1 */ |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 390 | static const char ulimit_opt_string[] ALIGN1 = "-HSa" |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 391 | #ifdef RLIMIT_FSIZE |
| 392 | "f::" |
| 393 | #endif |
| 394 | #ifdef RLIMIT_CPU |
| 395 | "t::" |
| 396 | #endif |
| 397 | #ifdef RLIMIT_DATA |
| 398 | "d::" |
| 399 | #endif |
| 400 | #ifdef RLIMIT_STACK |
| 401 | "s::" |
| 402 | #endif |
| 403 | #ifdef RLIMIT_CORE |
| 404 | "c::" |
| 405 | #endif |
| 406 | #ifdef RLIMIT_RSS |
| 407 | "m::" |
| 408 | #endif |
| 409 | #ifdef RLIMIT_MEMLOCK |
| 410 | "l::" |
| 411 | #endif |
| 412 | #ifdef RLIMIT_NPROC |
| 413 | "p::" |
| 414 | #endif |
| 415 | #ifdef RLIMIT_NOFILE |
| 416 | "n::" |
| 417 | #endif |
| 418 | #ifdef RLIMIT_AS |
| 419 | "v::" |
| 420 | #endif |
| 421 | #ifdef RLIMIT_LOCKS |
| 422 | "w::" |
| 423 | #endif |
Denys Vlasenko | e32d05b | 2011-04-04 02:12:14 +0200 | [diff] [blame] | 424 | #ifdef RLIMIT_NICE |
| 425 | "e::" |
| 426 | #endif |
| 427 | #ifdef RLIMIT_RTPRIO |
| 428 | "r::" |
| 429 | #endif |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 430 | ; |
| 431 | |
| 432 | static void printlim(unsigned opts, const struct rlimit *limit, |
| 433 | const struct limits *l) |
| 434 | { |
| 435 | rlim_t val; |
| 436 | |
| 437 | val = limit->rlim_max; |
| 438 | if (!(opts & OPT_hard)) |
| 439 | val = limit->rlim_cur; |
| 440 | |
| 441 | if (val == RLIM_INFINITY) |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 442 | puts("unlimited"); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 443 | else { |
| 444 | val >>= l->factor_shift; |
| 445 | printf("%llu\n", (long long) val); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | int FAST_FUNC |
| 450 | shell_builtin_ulimit(char **argv) |
| 451 | { |
| 452 | unsigned opts; |
| 453 | unsigned argc; |
| 454 | |
| 455 | /* We can't use getopt32: need to handle commands like |
| 456 | * ulimit 123 -c2 -l 456 |
| 457 | */ |
| 458 | |
Denys Vlasenko | 6016181 | 2017-08-29 14:32:17 +0200 | [diff] [blame] | 459 | /* In case getopt() was already called: |
| 460 | * reset libc getopt() internal state. |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 461 | */ |
Kaarle Ritvanen | 835ad3a | 2017-04-12 00:58:46 +0300 | [diff] [blame] | 462 | GETOPT_RESET(); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 463 | |
Denys Vlasenko | d4e4fdb | 2017-07-03 21:31:16 +0200 | [diff] [blame] | 464 | argc = string_array_len(argv); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 465 | |
| 466 | opts = 0; |
| 467 | while (1) { |
| 468 | struct rlimit limit; |
| 469 | const struct limits *l; |
| 470 | int opt_char = getopt(argc, argv, ulimit_opt_string); |
| 471 | |
| 472 | if (opt_char == -1) |
| 473 | break; |
| 474 | if (opt_char == 'H') { |
| 475 | opts |= OPT_hard; |
| 476 | continue; |
| 477 | } |
| 478 | if (opt_char == 'S') { |
| 479 | opts |= OPT_soft; |
| 480 | continue; |
| 481 | } |
| 482 | |
| 483 | if (opt_char == 'a') { |
| 484 | for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { |
| 485 | getrlimit(l->cmd, &limit); |
| 486 | printf("-%c: %-30s ", l->option, l->name); |
| 487 | printlim(opts, &limit, l); |
| 488 | } |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | if (opt_char == 1) |
| 493 | opt_char = 'f'; |
| 494 | for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { |
| 495 | if (opt_char == l->option) { |
| 496 | char *val_str; |
| 497 | |
| 498 | getrlimit(l->cmd, &limit); |
| 499 | |
| 500 | val_str = optarg; |
| 501 | if (!val_str && argv[optind] && argv[optind][0] != '-') |
| 502 | val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */ |
| 503 | if (val_str) { |
| 504 | rlim_t val; |
| 505 | |
| 506 | if (strcmp(val_str, "unlimited") == 0) |
| 507 | val = RLIM_INFINITY; |
| 508 | else { |
| 509 | if (sizeof(val) == sizeof(int)) |
| 510 | val = bb_strtou(val_str, NULL, 10); |
| 511 | else if (sizeof(val) == sizeof(long)) |
| 512 | val = bb_strtoul(val_str, NULL, 10); |
| 513 | else |
| 514 | val = bb_strtoull(val_str, NULL, 10); |
| 515 | if (errno) { |
Denys Vlasenko | b32a543 | 2010-08-29 13:29:02 +0200 | [diff] [blame] | 516 | bb_error_msg("invalid number '%s'", val_str); |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 517 | return EXIT_FAILURE; |
| 518 | } |
| 519 | val <<= l->factor_shift; |
| 520 | } |
| 521 | //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val); |
Alexander Shishkin | 17e0e43 | 2010-07-27 08:40:55 +0200 | [diff] [blame] | 522 | /* from man bash: "If neither -H nor -S |
| 523 | * is specified, both the soft and hard |
| 524 | * limits are set. */ |
| 525 | if (!opts) |
| 526 | opts = OPT_hard + OPT_soft; |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 527 | if (opts & OPT_hard) |
| 528 | limit.rlim_max = val; |
Alexander Shishkin | 17e0e43 | 2010-07-27 08:40:55 +0200 | [diff] [blame] | 529 | if (opts & OPT_soft) |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 530 | limit.rlim_cur = val; |
| 531 | //bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max); |
| 532 | if (setrlimit(l->cmd, &limit) < 0) { |
| 533 | bb_perror_msg("error setting limit"); |
| 534 | return EXIT_FAILURE; |
| 535 | } |
| 536 | } else { |
| 537 | printlim(opts, &limit, l); |
| 538 | } |
| 539 | break; |
| 540 | } |
| 541 | } /* for (every possible opt) */ |
| 542 | |
| 543 | if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) { |
| 544 | /* bad option. getopt already complained. */ |
| 545 | break; |
| 546 | } |
Denys Vlasenko | 599ae1e | 2010-05-23 17:49:50 +0200 | [diff] [blame] | 547 | } /* while (there are options) */ |
| 548 | |
| 549 | return 0; |
| 550 | } |