123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- #include "libbb.h"
- #include "shell_common.h"
- #include <sys/resource.h> /* getrlimit */
- const char defifsvar[] ALIGN1 = "IFS= \t\n";
- const char defoptindvar[] ALIGN1 = "OPTIND=1";
- int FAST_FUNC is_well_formed_var_name(const char *s, char terminator)
- {
- if (!s || !(isalpha(*s) || *s == '_'))
- return 0;
- do
- s++;
- while (isalnum(*s) || *s == '_');
- return *s == terminator;
- }
- const char* FAST_FUNC
- shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
- char **argv,
- const char *ifs,
- int read_flags,
- const char *opt_n,
- const char *opt_p,
- const char *opt_t,
- const char *opt_u,
- const char *opt_d
- )
- {
- struct pollfd pfd[1];
- #define fd (pfd[0].fd)
- unsigned err;
- unsigned end_ms;
- int nchars;
- char **pp;
- char *buffer;
- char delim;
- struct termios tty, old_tty;
- const char *retval;
- int bufpos;
- int startword;
- smallint backslash;
- errno = err = 0;
- pp = argv;
- while (*pp) {
- if (!is_well_formed_var_name(*pp, '\0')) {
-
- bb_error_msg("read: '%s': not a valid identifier", *pp);
- return (const char *)(uintptr_t)1;
- }
- pp++;
- }
- nchars = 0;
- if (opt_n) {
- nchars = bb_strtou(opt_n, NULL, 10);
- if (nchars < 0 || errno)
- return "invalid count";
-
- }
- end_ms = 0;
- if (opt_t && !ENABLE_FEATURE_SH_READ_FRAC) {
- end_ms = bb_strtou(opt_t, NULL, 10);
- if (errno)
- return "invalid timeout";
- if (end_ms > UINT_MAX / 2048)
- end_ms = UINT_MAX / 2048;
- end_ms *= 1000;
- }
- if (opt_t && ENABLE_FEATURE_SH_READ_FRAC) {
-
- char *p;
-
- int frac_digits = 3 + 1;
- end_ms = bb_strtou(opt_t, &p, 10);
- if (end_ms > UINT_MAX / 2048)
- end_ms = UINT_MAX / 2048;
- if (errno) {
-
- if (errno != EINVAL || *p != '.')
- return "invalid timeout";
-
- while (*++p && --frac_digits) {
- end_ms *= 10;
- end_ms += (*p - '0');
- if ((unsigned char)(*p - '0') > 9)
- return "invalid timeout";
- }
- }
- while (--frac_digits > 0) {
- end_ms *= 10;
- }
- }
- fd = STDIN_FILENO;
- if (opt_u) {
- fd = bb_strtou(opt_u, NULL, 10);
- if (fd < 0 || errno)
- return "invalid file descriptor";
- }
- if (opt_t && end_ms == 0) {
-
- int r;
- pfd[0].events = POLLIN;
- r = poll(pfd, 1, 0);
-
- return (const char *)(uintptr_t)(r <= 0);
- }
- if (opt_p && isatty(fd)) {
- fputs(opt_p, stderr);
- fflush_all();
- }
- if (ifs == NULL)
- ifs = defifs;
- if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
- tcgetattr(fd, &tty);
- old_tty = tty;
- if (nchars) {
- tty.c_lflag &= ~ICANON;
-
-
-
-
- tty.c_cc[VMIN] = 1;
-
- tty.c_cc[VTIME] = 0;
- }
- if (read_flags & BUILTIN_READ_SILENT) {
- tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
- }
-
- read_flags |= BUILTIN_READ_SILENT;
-
- tcsetattr(fd, TCSANOW, &tty);
- }
- retval = (const char *)(uintptr_t)0;
- startword = 1;
- backslash = 0;
- if (opt_t)
- end_ms += (unsigned)monotonic_ms();
- buffer = NULL;
- bufpos = 0;
- delim = opt_d ? *opt_d : '\n';
- do {
- char c;
- int timeout;
- if ((bufpos & 0xff) == 0)
- buffer = xrealloc(buffer, bufpos + 0x101);
- timeout = -1;
- if (opt_t) {
- timeout = end_ms - (unsigned)monotonic_ms();
-
- if (timeout <= 0) {
- retval = (const char *)(uintptr_t)1;
- goto ret;
- }
- }
-
- errno = 0;
- pfd[0].events = POLLIN;
- if (poll(pfd, 1, timeout) <= 0) {
-
- err = errno;
- retval = (const char *)(uintptr_t)1;
- goto ret;
- }
- if (read(fd, &buffer[bufpos], 1) != 1) {
- err = errno;
- retval = (const char *)(uintptr_t)1;
- break;
- }
- c = buffer[bufpos];
- if (c == '\0')
- continue;
- if (!(read_flags & BUILTIN_READ_RAW)) {
- if (backslash) {
- backslash = 0;
- if (c != '\n')
- goto put;
- continue;
- }
- if (c == '\\') {
- backslash = 1;
- continue;
- }
- }
- if (c == delim)
- break;
-
- if (!opt_d && argv[0]) {
- const char *is_ifs = strchr(ifs, c);
- if (startword && is_ifs) {
- if (isspace(c))
- continue;
-
- startword--;
- if (startword == 1)
- continue;
- }
- startword = 0;
- if (argv[1] != NULL && is_ifs) {
- buffer[bufpos] = '\0';
- bufpos = 0;
- setvar(*argv, buffer);
- argv++;
-
- startword = isspace(c) ? 2 : 1;
- continue;
- }
- }
- put:
- bufpos++;
- } while (--nchars);
- if (argv[0]) {
-
- while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
- continue;
- buffer[bufpos + 1] = '\0';
-
- setvar(*argv, buffer);
-
- while (*++argv)
- setvar(*argv, "");
- } else {
-
- buffer[bufpos] = '\0';
- setvar("REPLY", buffer);
- }
- ret:
- free(buffer);
- if (read_flags & BUILTIN_READ_SILENT)
- tcsetattr(fd, TCSANOW, &old_tty);
- errno = err;
- return retval;
- #undef fd
- }
- struct limits {
- uint8_t cmd;
- uint8_t factor_shift;
- char option;
- const char *name;
- };
- static const struct limits limits_tbl[] = {
- #ifdef RLIMIT_FSIZE
- { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" },
- #endif
- #ifdef RLIMIT_CPU
- { RLIMIT_CPU, 0, 't', "cpu time (seconds)" },
- #endif
- #ifdef RLIMIT_DATA
- { RLIMIT_DATA, 10, 'd', "data seg size (kb)" },
- #endif
- #ifdef RLIMIT_STACK
- { RLIMIT_STACK, 10, 's', "stack size (kb)" },
- #endif
- #ifdef RLIMIT_CORE
- { RLIMIT_CORE, 9, 'c', "core file size (blocks)" },
- #endif
- #ifdef RLIMIT_RSS
- { RLIMIT_RSS, 10, 'm', "resident set size (kb)" },
- #endif
- #ifdef RLIMIT_MEMLOCK
- { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" },
- #endif
- #ifdef RLIMIT_NPROC
- { RLIMIT_NPROC, 0, 'p', "processes" },
- #endif
- #ifdef RLIMIT_NOFILE
- { RLIMIT_NOFILE, 0, 'n', "file descriptors" },
- #endif
- #ifdef RLIMIT_AS
- { RLIMIT_AS, 10, 'v', "address space (kb)" },
- #endif
- #ifdef RLIMIT_LOCKS
- { RLIMIT_LOCKS, 0, 'w', "locks" },
- #endif
- #ifdef RLIMIT_NICE
- { RLIMIT_NICE, 0, 'e', "scheduling priority" },
- #endif
- #ifdef RLIMIT_RTPRIO
- { RLIMIT_RTPRIO, 0, 'r', "real-time priority" },
- #endif
- };
- enum {
- OPT_hard = (1 << 0),
- OPT_soft = (1 << 1),
- };
- static const char ulimit_opt_string[] ALIGN1 = "-HSa"
- #ifdef RLIMIT_FSIZE
- "f::"
- #endif
- #ifdef RLIMIT_CPU
- "t::"
- #endif
- #ifdef RLIMIT_DATA
- "d::"
- #endif
- #ifdef RLIMIT_STACK
- "s::"
- #endif
- #ifdef RLIMIT_CORE
- "c::"
- #endif
- #ifdef RLIMIT_RSS
- "m::"
- #endif
- #ifdef RLIMIT_MEMLOCK
- "l::"
- #endif
- #ifdef RLIMIT_NPROC
- "p::"
- #endif
- #ifdef RLIMIT_NOFILE
- "n::"
- #endif
- #ifdef RLIMIT_AS
- "v::"
- #endif
- #ifdef RLIMIT_LOCKS
- "w::"
- #endif
- #ifdef RLIMIT_NICE
- "e::"
- #endif
- #ifdef RLIMIT_RTPRIO
- "r::"
- #endif
- ;
- static void printlim(unsigned opts, const struct rlimit *limit,
- const struct limits *l)
- {
- rlim_t val;
- val = limit->rlim_max;
- if (!(opts & OPT_hard))
- val = limit->rlim_cur;
- if (val == RLIM_INFINITY)
- puts("unlimited");
- else {
- val >>= l->factor_shift;
- printf("%llu\n", (long long) val);
- }
- }
- int FAST_FUNC
- shell_builtin_ulimit(char **argv)
- {
- unsigned opts;
- unsigned argc;
-
-
- GETOPT_RESET();
- argc = string_array_len(argv);
- opts = 0;
- while (1) {
- struct rlimit limit;
- const struct limits *l;
- int opt_char = getopt(argc, argv, ulimit_opt_string);
- if (opt_char == -1)
- break;
- if (opt_char == 'H') {
- opts |= OPT_hard;
- continue;
- }
- if (opt_char == 'S') {
- opts |= OPT_soft;
- continue;
- }
- if (opt_char == 'a') {
- for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
- getrlimit(l->cmd, &limit);
- printf("-%c: %-30s ", l->option, l->name);
- printlim(opts, &limit, l);
- }
- continue;
- }
- if (opt_char == 1)
- opt_char = 'f';
- for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
- if (opt_char == l->option) {
- char *val_str;
- getrlimit(l->cmd, &limit);
- val_str = optarg;
- if (!val_str && argv[optind] && argv[optind][0] != '-')
- val_str = argv[optind++];
- if (val_str) {
- rlim_t val;
- if (strcmp(val_str, "unlimited") == 0)
- val = RLIM_INFINITY;
- else {
- if (sizeof(val) == sizeof(int))
- val = bb_strtou(val_str, NULL, 10);
- else if (sizeof(val) == sizeof(long))
- val = bb_strtoul(val_str, NULL, 10);
- else
- val = bb_strtoull(val_str, NULL, 10);
- if (errno) {
- bb_error_msg("invalid number '%s'", val_str);
- return EXIT_FAILURE;
- }
- val <<= l->factor_shift;
- }
-
- if (!opts)
- opts = OPT_hard + OPT_soft;
- if (opts & OPT_hard)
- limit.rlim_max = val;
- if (opts & OPT_soft)
- limit.rlim_cur = val;
- if (setrlimit(l->cmd, &limit) < 0) {
- bb_perror_msg("error setting limit");
- return EXIT_FAILURE;
- }
- } else {
- printlim(opts, &limit, l);
- }
- break;
- }
- }
- if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) {
-
- break;
- }
- }
- return 0;
- }
|