1 /* vi: set sw=4 ts=4: */
3 * Adapted from ash applet code
5 * This code is derived from software contributed to Berkeley by
8 * Copyright (c) 1989, 1991, 1993, 1994
9 * The Regents of the University of California. All rights reserved.
11 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
12 * was re-ported from NetBSD and debianized.
14 * Copyright (c) 2010 Denys Vlasenko
17 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
20 #include "shell_common.h"
21 #include <sys/resource.h> /* getrlimit */
23 const char defifsvar[] ALIGN1 = "IFS= \t\n";
26 int FAST_FUNC is_well_formed_var_name(const char *s, char terminator)
28 if (!s || !(isalpha(*s) || *s == '_'))
33 while (isalnum(*s) || *s == '_');
35 return *s == terminator;
40 /* Needs to be interruptible: shell mush handle traps and shell-special signals
41 * while inside read. To implement this, be sure to not loop on EINTR
42 * and return errno == EINTR reliably.
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.
49 shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
60 unsigned end_ms; /* -t TIMEOUT */
62 int nchars; /* -n NUM */
65 struct termios tty, old_tty;
67 int bufpos; /* need to be able to hold -1 */
75 if (!is_well_formed_var_name(*pp, '\0')) {
76 /* Mimic bash message */
77 bb_error_msg("read: '%s': not a valid identifier", *pp);
78 return (const char *)(uintptr_t)1;
83 nchars = 0; /* if != 0, -n is in effect */
85 nchars = bb_strtou(opt_n, NULL, 10);
86 if (nchars < 0 || errno)
87 return "invalid count";
88 /* note: "-n 0": off (bash 3.2 does this too) */
92 end_ms = bb_strtou(opt_t, NULL, 10);
93 if (errno || end_ms > UINT_MAX / 2048)
94 return "invalid timeout";
96 #if 0 /* even bash has no -t N.NNN support */
97 ts.tv_sec = bb_strtou(opt_t, &p, 10);
99 /* EINVAL means number is ok, but not terminated by NUL */
100 if (*p == '.' && errno == EINVAL) {
104 ts.tv_usec = bb_strtou(p, &p2, 10);
106 return "invalid timeout";
108 /* normalize to usec */
110 return "invalid timeout";
114 } else if (ts.tv_sec < 0 || errno) {
115 return "invalid timeout";
117 if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
118 return "invalid timeout";
124 fd = bb_strtou(opt_u, NULL, 10);
126 return "invalid file descriptor";
129 if (opt_p && isatty(fd)) {
130 fputs(opt_p, stderr);
137 if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
141 tty.c_lflag &= ~ICANON;
142 // Setting it to more than 1 breaks poll():
143 // it blocks even if there's data. !??
144 //tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
145 /* reads would block only if < 1 char is available */
147 /* no timeout (reads block forever) */
150 if (read_flags & BUILTIN_READ_SILENT) {
151 tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
153 /* This forces execution of "restoring" tcgetattr later */
154 read_flags |= BUILTIN_READ_SILENT;
155 /* if tcgetattr failed, tcsetattr will fail too.
156 * Ignoring, it's harmless. */
157 tcsetattr(fd, TCSANOW, &tty);
160 retval = (const char *)(uintptr_t)0;
163 if (end_ms) /* NB: end_ms stays nonzero: */
164 end_ms = ((unsigned)monotonic_ms() + end_ms) | 1;
169 struct pollfd pfd[1];
172 if ((bufpos & 0xff) == 0)
173 buffer = xrealloc(buffer, bufpos + 0x101);
177 timeout = end_ms - (unsigned)monotonic_ms();
178 if (timeout <= 0) { /* already late? */
179 retval = (const char *)(uintptr_t)1;
184 /* We must poll even if timeout is -1:
185 * we want to be interrupted if signal arrives,
186 * regardless of SA_RESTART-ness of that signal!
190 pfd[0].events = POLLIN;
191 if (poll(pfd, 1, timeout) != 1) {
192 /* timed out, or EINTR */
194 retval = (const char *)(uintptr_t)1;
197 if (read(fd, &buffer[bufpos], 1) != 1) {
199 retval = (const char *)(uintptr_t)1;
212 if (!(read_flags & BUILTIN_READ_RAW) && c == '\\') {
219 /* $IFS splitting. NOT done if we run "read"
220 * without variable names (bash compat).
221 * Thus, "read" and "read REPLY" are not the same.
224 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
225 const char *is_ifs = strchr(ifs, c);
226 if (startword && is_ifs) {
229 /* it is a non-space ifs char */
231 if (startword == 1) /* first one? */
232 continue; /* yes, it is not next word yet */
235 if (argv[1] != NULL && is_ifs) {
236 buffer[bufpos] = '\0';
238 setvar(*argv, buffer);
240 /* can we skip one non-space ifs char? (2: yes) */
241 startword = isspace(c) ? 2 : 1;
250 /* Remove trailing space $IFS chars */
251 while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
253 buffer[bufpos + 1] = '\0';
254 /* Use the remainder as a value for the next variable */
255 setvar(*argv, buffer);
256 /* Set the rest to "" */
260 /* Note: no $IFS removal */
261 buffer[bufpos] = '\0';
262 setvar("REPLY", buffer);
267 if (read_flags & BUILTIN_READ_SILENT)
268 tcsetattr(fd, TCSANOW, &old_tty);
277 uint8_t cmd; /* RLIMIT_xxx fit into it */
278 uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
283 static const struct limits limits_tbl[] = {
285 { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" },
288 { RLIMIT_CPU, 0, 't', "cpu time (seconds)" },
291 { RLIMIT_DATA, 10, 'd', "data seg size (kb)" },
294 { RLIMIT_STACK, 10, 's', "stack size (kb)" },
297 { RLIMIT_CORE, 9, 'c', "core file size (blocks)" },
300 { RLIMIT_RSS, 10, 'm', "resident set size (kb)" },
302 #ifdef RLIMIT_MEMLOCK
303 { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" },
306 { RLIMIT_NPROC, 0, 'p', "processes" },
309 { RLIMIT_NOFILE, 0, 'n', "file descriptors" },
312 { RLIMIT_AS, 10, 'v', "address space (kb)" },
315 { RLIMIT_LOCKS, 0, 'w', "locks" },
318 { RLIMIT_NICE, 0, 'e', "scheduling priority" },
321 { RLIMIT_RTPRIO, 0, 'r', "real-time priority" },
330 /* "-": treat args as parameters of option with ASCII code 1 */
331 static const char ulimit_opt_string[] = "-HSa"
350 #ifdef RLIMIT_MEMLOCK
373 static void printlim(unsigned opts, const struct rlimit *limit,
374 const struct limits *l)
378 val = limit->rlim_max;
379 if (!(opts & OPT_hard))
380 val = limit->rlim_cur;
382 if (val == RLIM_INFINITY)
383 printf("unlimited\n");
385 val >>= l->factor_shift;
386 printf("%llu\n", (long long) val);
391 shell_builtin_ulimit(char **argv)
396 /* We can't use getopt32: need to handle commands like
397 * ulimit 123 -c2 -l 456
400 /* In case getopt was already called:
401 * reset the libc getopt() function, which keeps internal state.
405 #else /* BSD style */
409 /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
418 const struct limits *l;
419 int opt_char = getopt(argc, argv, ulimit_opt_string);
423 if (opt_char == 'H') {
427 if (opt_char == 'S') {
432 if (opt_char == 'a') {
433 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
434 getrlimit(l->cmd, &limit);
435 printf("-%c: %-30s ", l->option, l->name);
436 printlim(opts, &limit, l);
443 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
444 if (opt_char == l->option) {
447 getrlimit(l->cmd, &limit);
450 if (!val_str && argv[optind] && argv[optind][0] != '-')
451 val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
455 if (strcmp(val_str, "unlimited") == 0)
458 if (sizeof(val) == sizeof(int))
459 val = bb_strtou(val_str, NULL, 10);
460 else if (sizeof(val) == sizeof(long))
461 val = bb_strtoul(val_str, NULL, 10);
463 val = bb_strtoull(val_str, NULL, 10);
465 bb_error_msg("invalid number '%s'", val_str);
468 val <<= l->factor_shift;
470 //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
471 /* from man bash: "If neither -H nor -S
472 * is specified, both the soft and hard
475 opts = OPT_hard + OPT_soft;
477 limit.rlim_max = val;
479 limit.rlim_cur = val;
480 //bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
481 if (setrlimit(l->cmd, &limit) < 0) {
482 bb_perror_msg("error setting limit");
486 printlim(opts, &limit, l);
490 } /* for (every possible opt) */
492 if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) {
493 /* bad option. getopt already complained. */
497 } /* while (there are options) */