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