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";
24 const char defoptindvar[] ALIGN1 = "OPTIND=1";
27 int FAST_FUNC is_well_formed_var_name(const char *s, char terminator)
29 if (!s || !(isalpha(*s) || *s == '_'))
34 while (isalnum(*s) || *s == '_');
36 return *s == terminator;
41 /* Needs to be interruptible: shell must handle traps and shell-special signals
42 * while inside read. To implement this, be sure to not loop on EINTR
43 * and return errno == EINTR reliably.
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.
50 shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
61 unsigned end_ms; /* -t TIMEOUT */
63 int nchars; /* -n NUM */
66 struct termios tty, old_tty;
68 int bufpos; /* need to be able to hold -1 */
76 if (!is_well_formed_var_name(*pp, '\0')) {
77 /* Mimic bash message */
78 bb_error_msg("read: '%s': not a valid identifier", *pp);
79 return (const char *)(uintptr_t)1;
84 nchars = 0; /* if != 0, -n is in effect */
86 nchars = bb_strtou(opt_n, NULL, 10);
87 if (nchars < 0 || errno)
88 return "invalid count";
89 /* note: "-n 0": off (bash 3.2 does this too) */
93 end_ms = bb_strtou(opt_t, NULL, 10);
94 if (errno || end_ms > UINT_MAX / 2048)
95 return "invalid timeout";
97 #if 0 /* even bash has no -t N.NNN support */
98 ts.tv_sec = bb_strtou(opt_t, &p, 10);
100 /* EINVAL means number is ok, but not terminated by NUL */
101 if (*p == '.' && errno == EINVAL) {
105 ts.tv_usec = bb_strtou(p, &p2, 10);
107 return "invalid timeout";
109 /* normalize to usec */
111 return "invalid timeout";
115 } else if (ts.tv_sec < 0 || errno) {
116 return "invalid timeout";
118 if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
119 return "invalid timeout";
125 fd = bb_strtou(opt_u, NULL, 10);
127 return "invalid file descriptor";
130 if (opt_p && isatty(fd)) {
131 fputs(opt_p, stderr);
138 if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
142 tty.c_lflag &= ~ICANON;
143 // Setting it to more than 1 breaks poll():
144 // it blocks even if there's data. !??
145 //tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
146 /* reads would block only if < 1 char is available */
148 /* no timeout (reads block forever) */
151 if (read_flags & BUILTIN_READ_SILENT) {
152 tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
154 /* This forces execution of "restoring" tcgetattr later */
155 read_flags |= BUILTIN_READ_SILENT;
156 /* if tcgetattr failed, tcsetattr will fail too.
157 * Ignoring, it's harmless. */
158 tcsetattr(fd, TCSANOW, &tty);
161 retval = (const char *)(uintptr_t)0;
164 if (end_ms) /* NB: end_ms stays nonzero: */
165 end_ms = ((unsigned)monotonic_ms() + end_ms) | 1;
170 struct pollfd pfd[1];
173 if ((bufpos & 0xff) == 0)
174 buffer = xrealloc(buffer, bufpos + 0x101);
178 timeout = end_ms - (unsigned)monotonic_ms();
179 if (timeout <= 0) { /* already late? */
180 retval = (const char *)(uintptr_t)1;
185 /* We must poll even if timeout is -1:
186 * we want to be interrupted if signal arrives,
187 * regardless of SA_RESTART-ness of that signal!
191 pfd[0].events = POLLIN;
192 if (poll(pfd, 1, timeout) != 1) {
193 /* timed out, or EINTR */
195 retval = (const char *)(uintptr_t)1;
198 if (read(fd, &buffer[bufpos], 1) != 1) {
200 retval = (const char *)(uintptr_t)1;
213 if (!(read_flags & BUILTIN_READ_RAW) && c == '\\') {
220 /* $IFS splitting. NOT done if we run "read"
221 * without variable names (bash compat).
222 * Thus, "read" and "read REPLY" are not the same.
225 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
226 const char *is_ifs = strchr(ifs, c);
227 if (startword && is_ifs) {
230 /* it is a non-space ifs char */
232 if (startword == 1) /* first one? */
233 continue; /* yes, it is not next word yet */
236 if (argv[1] != NULL && is_ifs) {
237 buffer[bufpos] = '\0';
239 setvar(*argv, buffer);
241 /* can we skip one non-space ifs char? (2: yes) */
242 startword = isspace(c) ? 2 : 1;
251 /* Remove trailing space $IFS chars */
252 while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
254 buffer[bufpos + 1] = '\0';
255 /* Use the remainder as a value for the next variable */
256 setvar(*argv, buffer);
257 /* Set the rest to "" */
261 /* Note: no $IFS removal */
262 buffer[bufpos] = '\0';
263 setvar("REPLY", buffer);
268 if (read_flags & BUILTIN_READ_SILENT)
269 tcsetattr(fd, TCSANOW, &old_tty);
278 uint8_t cmd; /* RLIMIT_xxx fit into it */
279 uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
284 static const struct limits limits_tbl[] = {
286 { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" },
289 { RLIMIT_CPU, 0, 't', "cpu time (seconds)" },
292 { RLIMIT_DATA, 10, 'd', "data seg size (kb)" },
295 { RLIMIT_STACK, 10, 's', "stack size (kb)" },
298 { RLIMIT_CORE, 9, 'c', "core file size (blocks)" },
301 { RLIMIT_RSS, 10, 'm', "resident set size (kb)" },
303 #ifdef RLIMIT_MEMLOCK
304 { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" },
307 { RLIMIT_NPROC, 0, 'p', "processes" },
310 { RLIMIT_NOFILE, 0, 'n', "file descriptors" },
313 { RLIMIT_AS, 10, 'v', "address space (kb)" },
316 { RLIMIT_LOCKS, 0, 'w', "locks" },
319 { RLIMIT_NICE, 0, 'e', "scheduling priority" },
322 { RLIMIT_RTPRIO, 0, 'r', "real-time priority" },
331 /* "-": treat args as parameters of option with ASCII code 1 */
332 static const char ulimit_opt_string[] ALIGN1 = "-HSa"
351 #ifdef RLIMIT_MEMLOCK
374 static void printlim(unsigned opts, const struct rlimit *limit,
375 const struct limits *l)
379 val = limit->rlim_max;
380 if (!(opts & OPT_hard))
381 val = limit->rlim_cur;
383 if (val == RLIM_INFINITY)
386 val >>= l->factor_shift;
387 printf("%llu\n", (long long) val);
392 shell_builtin_ulimit(char **argv)
397 /* We can't use getopt32: need to handle commands like
398 * ulimit 123 -c2 -l 456
401 /* In case getopt was already called:
402 * reset the libc getopt() function, which keeps internal state.
406 #else /* BSD style */
410 /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
419 const struct limits *l;
420 int opt_char = getopt(argc, argv, ulimit_opt_string);
424 if (opt_char == 'H') {
428 if (opt_char == 'S') {
433 if (opt_char == 'a') {
434 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
435 getrlimit(l->cmd, &limit);
436 printf("-%c: %-30s ", l->option, l->name);
437 printlim(opts, &limit, l);
444 for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
445 if (opt_char == l->option) {
448 getrlimit(l->cmd, &limit);
451 if (!val_str && argv[optind] && argv[optind][0] != '-')
452 val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
456 if (strcmp(val_str, "unlimited") == 0)
459 if (sizeof(val) == sizeof(int))
460 val = bb_strtou(val_str, NULL, 10);
461 else if (sizeof(val) == sizeof(long))
462 val = bb_strtoul(val_str, NULL, 10);
464 val = bb_strtoull(val_str, NULL, 10);
466 bb_error_msg("invalid number '%s'", val_str);
469 val <<= l->factor_shift;
471 //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
472 /* from man bash: "If neither -H nor -S
473 * is specified, both the soft and hard
476 opts = OPT_hard + OPT_soft;
478 limit.rlim_max = val;
480 limit.rlim_cur = val;
481 //bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
482 if (setrlimit(l->cmd, &limit) < 0) {
483 bb_perror_msg("error setting limit");
487 printlim(opts, &limit, l);
491 } /* for (every possible opt) */
493 if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) {
494 /* bad option. getopt already complained. */
497 } /* while (there are options) */