123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709 |
- #include "libbb.h"
- #include "shell_common.h"
- const char defifsvar[] ALIGN1 = "IFS= \t\n";
- const char defoptindvar[] ALIGN1 = "OPTIND=1";
- int FAST_FUNC varcmp(const char *p, const char *q)
- {
- int c, d;
- while ((c = *p) == (d = *q)) {
- if (c == '\0' || c == '=')
- goto out;
- p++;
- q++;
- }
- if (c == '=')
- c = '\0';
- if (d == '=')
- d = '\0';
- out:
- return c - d;
- }
- const char* FAST_FUNC
- shell_builtin_read(struct builtin_read_params *params)
- {
- 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;
- char **argv;
- const char *ifs;
- int read_flags;
- errno = err = 0;
- argv = params->argv;
- pp = argv;
- while (*pp) {
- if (!*pp[0] || endofname(*pp)[0] != '\0') {
-
- bb_error_msg("read: '%s': bad variable name", *pp);
- return (const char *)(uintptr_t)1;
- }
- pp++;
- }
- nchars = 0;
- if (params->opt_n) {
- nchars = bb_strtou(params->opt_n, NULL, 10);
- if (nchars < 0 || errno)
- return "invalid count";
-
- }
- end_ms = 0;
- if (params->opt_t && !ENABLE_FEATURE_SH_READ_FRAC) {
- end_ms = bb_strtou(params->opt_t, NULL, 10);
- if (errno)
- return "invalid timeout";
- if (end_ms > UINT_MAX / 2048)
- end_ms = UINT_MAX / 2048;
- end_ms *= 1000;
- }
- if (params->opt_t && ENABLE_FEATURE_SH_READ_FRAC) {
-
- char *p;
-
- int frac_digits = 3 + 1;
- end_ms = bb_strtou(params->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 (params->opt_u) {
- fd = bb_strtou(params->opt_u, NULL, 10);
- if (fd < 0 || errno)
- return "invalid file descriptor";
- }
- if (params->opt_t && end_ms == 0) {
-
- int r;
- pfd[0].events = POLLIN;
- r = poll(pfd, 1, 0);
-
- return (const char *)(uintptr_t)(r <= 0);
- }
- if (params->opt_p && isatty(fd)) {
- fputs(params->opt_p, stderr);
- fflush_all();
- }
- ifs = params->ifs;
- if (ifs == NULL)
- ifs = defifs;
- read_flags = params->read_flags;
- 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 (params->opt_t)
- end_ms += (unsigned)monotonic_ms();
- buffer = NULL;
- bufpos = 0;
- delim = params->opt_d ? params->opt_d[0] : '\n';
- do {
- char c;
- int timeout;
- if ((bufpos & 0xff) == 0)
- buffer = xrealloc(buffer, bufpos + 0x101);
- timeout = -1;
- if (params->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 (!(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 (c == '\0')
- continue;
-
- if (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;
- params->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';
-
- if (bufpos >= 0
- && strchr(ifs, buffer[bufpos]) != NULL
- ) {
-
-
- while (--bufpos >= 0
- && isspace(buffer[bufpos])
- && strchr(ifs, buffer[bufpos]) != NULL
- ) {
- continue;
- }
-
- if (strcspn(buffer, ifs) >= ++bufpos) {
-
-
- buffer[bufpos] = '\0';
- }
- }
-
- params->setvar(*argv, buffer);
-
- while (*++argv)
- params->setvar(*argv, "");
- } else {
-
- buffer[bufpos] = '\0';
- params->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;
- };
- static const struct limits limits_tbl[] ALIGN2 = {
- { RLIMIT_CORE, 9, },
- { RLIMIT_DATA, 10, },
- #ifdef RLIMIT_NICE
- { RLIMIT_NICE, 0, },
- #define LIMIT_F_IDX 3
- #else
- #define LIMIT_F_IDX 2
- #endif
- { RLIMIT_FSIZE, 9, },
- #ifdef RLIMIT_SIGPENDING
- { RLIMIT_SIGPENDING, 0, },
- #endif
- #ifdef RLIMIT_MEMLOCK
- { RLIMIT_MEMLOCK, 10, },
- #endif
- #ifdef RLIMIT_RSS
- { RLIMIT_RSS, 10, },
- #endif
- #ifdef RLIMIT_NOFILE
- { RLIMIT_NOFILE, 0, },
- #endif
- #ifdef RLIMIT_MSGQUEUE
- { RLIMIT_MSGQUEUE, 0, },
- #endif
- #ifdef RLIMIT_RTPRIO
- { RLIMIT_RTPRIO, 0, },
- #endif
- #ifdef RLIMIT_STACK
- { RLIMIT_STACK, 10, },
- #endif
- #ifdef RLIMIT_CPU
- { RLIMIT_CPU, 0, },
- #endif
- #ifdef RLIMIT_NPROC
- { RLIMIT_NPROC, 0, },
- #endif
- #ifdef RLIMIT_AS
- { RLIMIT_AS, 10, },
- #endif
- #ifdef RLIMIT_LOCKS
- { RLIMIT_LOCKS, 0, },
- #endif
- };
- static const char limits_help[] ALIGN1 =
- "core file size (blocks)"
- "\0""data seg size (kb)"
- #ifdef RLIMIT_NICE
- "\0""scheduling priority"
- #endif
- "\0""file size (blocks)"
- #ifdef RLIMIT_SIGPENDING
- "\0""pending signals"
- #endif
- #ifdef RLIMIT_MEMLOCK
- "\0""max locked memory (kb)"
- #endif
- #ifdef RLIMIT_RSS
- "\0""max memory size (kb)"
- #endif
- #ifdef RLIMIT_NOFILE
- "\0""open files"
- #endif
- #ifdef RLIMIT_MSGQUEUE
- "\0""POSIX message queues (bytes)"
- #endif
- #ifdef RLIMIT_RTPRIO
- "\0""real-time priority"
- #endif
- #ifdef RLIMIT_STACK
- "\0""stack size (kb)"
- #endif
- #ifdef RLIMIT_CPU
- "\0""cpu time (seconds)"
- #endif
- #ifdef RLIMIT_NPROC
- "\0""max user processes"
- #endif
- #ifdef RLIMIT_AS
- "\0""virtual memory (kb)"
- #endif
- #ifdef RLIMIT_LOCKS
- "\0""file locks"
- #endif
- ;
- static const char limit_chars[] ALIGN1 =
- "c"
- "d"
- #ifdef RLIMIT_NICE
- "e"
- #endif
- "f"
- #ifdef RLIMIT_SIGPENDING
- "i"
- #endif
- #ifdef RLIMIT_MEMLOCK
- "l"
- #endif
- #ifdef RLIMIT_RSS
- "m"
- #endif
- #ifdef RLIMIT_NOFILE
- "n"
- #endif
- #ifdef RLIMIT_MSGQUEUE
- "q"
- #endif
- #ifdef RLIMIT_RTPRIO
- "r"
- #endif
- #ifdef RLIMIT_STACK
- "s"
- #endif
- #ifdef RLIMIT_CPU
- "t"
- #endif
- #ifdef RLIMIT_NPROC
- "u"
- #endif
- #ifdef RLIMIT_AS
- "v"
- #endif
- #ifdef RLIMIT_LOCKS
- "x"
- #endif
- ;
- static const char ulimit_opt_string[] ALIGN1 = "-HSa"
- "c::"
- "d::"
- #ifdef RLIMIT_NICE
- "e::"
- #endif
- "f::"
- #ifdef RLIMIT_SIGPENDING
- "i::"
- #endif
- #ifdef RLIMIT_MEMLOCK
- "l::"
- #endif
- #ifdef RLIMIT_RSS
- "m::"
- #endif
- #ifdef RLIMIT_NOFILE
- "n::"
- #endif
- #ifdef RLIMIT_MSGQUEUE
- "q::"
- #endif
- #ifdef RLIMIT_RTPRIO
- "r::"
- #endif
- #ifdef RLIMIT_STACK
- "s::"
- #endif
- #ifdef RLIMIT_CPU
- "t::"
- #endif
- #ifdef RLIMIT_NPROC
- "u::"
- #endif
- #ifdef RLIMIT_AS
- "v::"
- #endif
- #ifdef RLIMIT_LOCKS
- "x::"
- #endif
- ;
- enum {
- OPT_hard = (1 << 0),
- OPT_soft = (1 << 1),
- OPT_all = (1 << 2),
- };
- static void printlim(unsigned opts, const struct rlimit *limit,
- const struct limits *l)
- {
- rlim_t val;
- val = limit->rlim_max;
- if (opts & OPT_soft)
- 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)
- {
- struct rlimit limit;
- unsigned opt_cnt;
- unsigned opts;
- unsigned argc;
- unsigned i;
-
-
- GETOPT_RESET();
- argc = string_array_len(argv);
-
- opt_cnt = 0;
- opts = 0;
- while (1) {
- 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') {
- opts |= OPT_all;
- continue;
- }
- if (opt_char == '?') {
-
- return EXIT_FAILURE;
- }
- opt_cnt++;
- }
- if (!(opts & (OPT_hard | OPT_soft)))
- opts |= (OPT_hard | OPT_soft);
- if (opts & OPT_all) {
- const char *help = limits_help;
- for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) {
- getrlimit(limits_tbl[i].cmd, &limit);
- printf("%-32s(-%c) ", help, limit_chars[i]);
- printlim(opts, &limit, &limits_tbl[i]);
- help += strlen(help) + 1;
- }
- return EXIT_SUCCESS;
- }
-
- GETOPT_RESET();
- while (1) {
- char *val_str;
- int opt_char = getopt(argc, argv, ulimit_opt_string);
- if (opt_char == -1)
- break;
- if (opt_char == 'H')
- continue;
- if (opt_char == 'S')
- continue;
-
- if (opt_char == 1)
- opt_char = 'f';
- i = strchrnul(limit_chars, opt_char) - limit_chars;
-
- val_str = optarg;
- if (!val_str && argv[optind] && argv[optind][0] != '-')
- val_str = argv[optind++];
- getrlimit(limits_tbl[i].cmd, &limit);
- if (!val_str) {
- if (opt_cnt > 1)
- printf("%-32s(-%c) ", nth_string(limits_help, i), limit_chars[i]);
- printlim(opts, &limit, &limits_tbl[i]);
- } else {
- rlim_t val = RLIM_INFINITY;
- if (strcmp(val_str, "unlimited") != 0) {
- 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 <<= limits_tbl[i].factor_shift;
- }
-
- if (opts & OPT_hard)
- limit.rlim_max = val;
- if (opts & OPT_soft)
- limit.rlim_cur = val;
- if (setrlimit(limits_tbl[i].cmd, &limit) < 0) {
- bb_simple_perror_msg("error setting limit");
- return EXIT_FAILURE;
- }
- }
- }
- if (opt_cnt == 0) {
-
- getrlimit(RLIMIT_FSIZE, &limit);
- printlim(opts, &limit, &limits_tbl[LIMIT_F_IDX]);
- }
- return EXIT_SUCCESS;
- }
|