From a4d76ea1373a7cf06d3d0a3c8905646638e97a13 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 27 Apr 2019 21:01:35 +0200 Subject: ash,hush: fix ulimit to be more bash-compat, closes 11791 function old new delta shell_builtin_ulimit 486 651 +165 limit_chars - 14 +14 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/0 up/down: 179/0) Total: 179 bytes Signed-off-by: Denys Vlasenko --- shell/shell_common.c | 265 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 181 insertions(+), 84 deletions(-) (limited to 'shell/shell_common.c') diff --git a/shell/shell_common.c b/shell/shell_common.c index f2bf5ab65..686c18f54 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -335,62 +335,93 @@ shell_builtin_read(struct builtin_read_params *params) struct limits { uint8_t cmd; /* RLIMIT_xxx fit into it */ uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */ - char option; const char *name; }; static const struct limits limits_tbl[] = { -#ifdef RLIMIT_FSIZE - { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" }, -#endif +/* No RLIMIT_FSIZE define guard since -f is the default limit and this must exist */ + { RLIMIT_FSIZE, 9, "file size (blocks)" }, // -f #ifdef RLIMIT_CPU - { RLIMIT_CPU, 0, 't', "cpu time (seconds)" }, + { RLIMIT_CPU, 0, "cpu time (seconds)" }, // -t #endif #ifdef RLIMIT_DATA - { RLIMIT_DATA, 10, 'd', "data seg size (kb)" }, + { RLIMIT_DATA, 10, "data seg size (kb)" }, // -d #endif #ifdef RLIMIT_STACK - { RLIMIT_STACK, 10, 's', "stack size (kb)" }, + { RLIMIT_STACK, 10, "stack size (kb)" }, // -s #endif #ifdef RLIMIT_CORE - { RLIMIT_CORE, 9, 'c', "core file size (blocks)" }, + { RLIMIT_CORE, 9, "core file size (blocks)" }, // -c #endif #ifdef RLIMIT_RSS - { RLIMIT_RSS, 10, 'm', "resident set size (kb)" }, + { RLIMIT_RSS, 10, "resident set size (kb)" }, // -m #endif #ifdef RLIMIT_MEMLOCK - { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" }, + { RLIMIT_MEMLOCK, 10, "locked memory (kb)" }, // -l #endif #ifdef RLIMIT_NPROC - { RLIMIT_NPROC, 0, 'p', "processes" }, + { RLIMIT_NPROC, 0, "processes" }, // -p #endif #ifdef RLIMIT_NOFILE - { RLIMIT_NOFILE, 0, 'n', "file descriptors" }, + { RLIMIT_NOFILE, 0, "file descriptors" }, // -n #endif #ifdef RLIMIT_AS - { RLIMIT_AS, 10, 'v', "address space (kb)" }, + { RLIMIT_AS, 10, "address space (kb)" }, // -v #endif #ifdef RLIMIT_LOCKS - { RLIMIT_LOCKS, 0, 'w', "locks" }, + { RLIMIT_LOCKS, 0, "locks" }, // -w #endif #ifdef RLIMIT_NICE - { RLIMIT_NICE, 0, 'e', "scheduling priority" }, + { RLIMIT_NICE, 0, "scheduling priority" }, // -e #endif #ifdef RLIMIT_RTPRIO - { RLIMIT_RTPRIO, 0, 'r', "real-time priority" }, + { RLIMIT_RTPRIO, 0, "real-time priority" }, // -r #endif }; -enum { - OPT_hard = (1 << 0), - OPT_soft = (1 << 1), -}; +static const char limit_chars[] ALIGN1 = + "f" +#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 +; /* "-": treat args as parameters of option with ASCII code 1 */ static const char ulimit_opt_string[] ALIGN1 = "-HSa" -#ifdef RLIMIT_FSIZE "f::" -#endif #ifdef RLIMIT_CPU "t::" #endif @@ -429,13 +460,19 @@ static const char ulimit_opt_string[] ALIGN1 = "-HSa" #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_hard)) + if (opts & OPT_soft) val = limit->rlim_cur; if (val == RLIM_INFINITY) @@ -449,8 +486,11 @@ static void printlim(unsigned opts, const struct rlimit *limit, int FAST_FUNC shell_builtin_ulimit(char **argv) { + struct rlimit limit; + unsigned opt_cnt; unsigned opts; unsigned argc; + unsigned i; /* We can't use getopt32: need to handle commands like * ulimit 123 -c2 -l 456 @@ -461,12 +501,48 @@ shell_builtin_ulimit(char **argv) */ GETOPT_RESET(); +// bash 4.4.23: +// +// -H and/or -S change meaning even of options *before* them: ulimit -f 2000 -H +// sets hard limit, ulimit -a -H prints hard limits. +// +// -a is equivalent for requesting all limits to be shown. +// +// If -a is specified, attempts to set limits are ignored: +// ulimit -m 1000; ulimit -m 2000 -a +// shows 1000, not 2000. HOWEVER, *implicit* -f form "ulimit 2000 -a" +// DOES set -f limit [we don't implement this quirk], "ulimit -a 2000" does not. +// Options are still parsed: ulimit -az complains about unknown -z opt. +// +// -a is not cumulative: "ulimit -a -a" = "ulimit -a -f -m" = "ulimit -a" +// +// -HSa can be combined in one argument and with one other option (example: -Sm), +// but other options can't: limit value is an optional argument, +// thus "-mf" means "-m f", f is the parameter of -m. +// +// Limit can be set and then printed: ulimit -m 2000 -m +// If set more than once, they are set and printed in order: +// try ulimit -m -m 1000 -m -m 2000 -m -m 3000 -m +// +// Limits are shown in the order of options given: +// ulimit -m -f is not the same as ulimit -f -m. +// +// If both -S and -H are given, show soft limit. +// +// Short printout (limit value only) is printed only if just one option +// is given: ulimit -m. ulimit -f -m prints verbose lines. +// ulimit -f -f prints same verbose line twice. +// ulimit -m 10000 -f prints verbose line for -f. + argc = string_array_len(argv); + /* First pass over options: detect -H/-S/-a status, + * and "bare ulimit" and "only one option" cases + * by counting other opts. + */ + opt_cnt = 0; opts = 0; while (1) { - struct rlimit limit; - const struct limits *l; int opt_char = getopt(argc, argv, ulimit_opt_string); if (opt_char == -1) @@ -479,72 +555,93 @@ shell_builtin_ulimit(char **argv) 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); - } + opts |= OPT_all; continue; } + if (opt_char == '?') { + /* bad option. getopt already complained. */ + return EXIT_FAILURE; + } + opt_cnt++; + } /* while (there are options) */ - 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++]; /* ++ skips NN in "-c NN" case */ - 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; - } -//bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val); - /* from man bash: "If neither -H nor -S - * is specified, both the soft and hard - * limits are set. */ - if (!opts) - opts = OPT_hard + OPT_soft; - if (opts & OPT_hard) - limit.rlim_max = val; - if (opts & OPT_soft) - limit.rlim_cur = val; -//bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max); - if (setrlimit(l->cmd, &limit) < 0) { - bb_perror_msg("error setting limit"); - return EXIT_FAILURE; - } - } else { - printlim(opts, &limit, l); - } - break; - } - } /* for (every possible opt) */ + if (!(opts & (OPT_hard | OPT_soft))) + opts |= (OPT_hard | OPT_soft); + if (opts & OPT_all) { + for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) { + getrlimit(limits_tbl[i].cmd, &limit); + printf("-%c: %-30s ", limit_chars[i], limits_tbl[i].name); + printlim(opts, &limit, &limits_tbl[i]); + } + return EXIT_SUCCESS; + } - if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) { - /* bad option. getopt already complained. */ + /* Second pass: set or print limits, in order */ + 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 == 'a') - impossible + + i = 0; /* if "ulimit NNN", -f is assumed */ + if (opt_char != 1) { + i = strchrnul(limit_chars, opt_char) - limit_chars; + //if (i >= ARRAY_SIZE(limits_tbl)) - bad option, impossible + } + + val_str = optarg; + if (!val_str && argv[optind] && argv[optind][0] != '-') + val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */ + + getrlimit(limits_tbl[i].cmd, &limit); + if (!val_str) { + if (opt_cnt > 1) + printf("-%c: %-30s ", limit_chars[i], limits_tbl[i].name); + 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; + } +//bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val); + /* from man bash: "If neither -H nor -S + * is specified, both the soft and hard + * limits are set. */ + if (opts & OPT_hard) + limit.rlim_max = val; + if (opts & OPT_soft) + limit.rlim_cur = val; +//bb_error_msg("setrlimit(%d, %lld, %lld)", limits_tbl[i].cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max); + if (setrlimit(limits_tbl[i].cmd, &limit) < 0) { + bb_perror_msg("error setting limit"); + return EXIT_FAILURE; + } } } /* while (there are options) */ - return 0; + if (opt_cnt == 0) { + /* "bare ulimit": treat it as if it was -f */ + getrlimit(limits_tbl[0].cmd, &limit); + printlim(opts, &limit, &limits_tbl[0]); + } + + return EXIT_SUCCESS; } -- cgit v1.2.3-55-g6feb From a92a9601f89d59597b268e29e7098597a8766778 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 27 Apr 2019 21:23:39 +0200 Subject: ash,hush: bash compat for ulimit: -w => -x, -p => -u Signed-off-by: Denys Vlasenko --- shell/shell_common.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'shell/shell_common.c') diff --git a/shell/shell_common.c b/shell/shell_common.c index 686c18f54..a992682a8 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -354,22 +354,22 @@ static const struct limits limits_tbl[] = { { RLIMIT_CORE, 9, "core file size (blocks)" }, // -c #endif #ifdef RLIMIT_RSS - { RLIMIT_RSS, 10, "resident set size (kb)" }, // -m + { RLIMIT_RSS, 10, "max memory size (kb)" }, // -m #endif #ifdef RLIMIT_MEMLOCK - { RLIMIT_MEMLOCK, 10, "locked memory (kb)" }, // -l + { RLIMIT_MEMLOCK, 10, "max locked memory (kb)" }, // -l #endif #ifdef RLIMIT_NPROC - { RLIMIT_NPROC, 0, "processes" }, // -p + { RLIMIT_NPROC, 0, "max user processes" }, // -u #endif #ifdef RLIMIT_NOFILE - { RLIMIT_NOFILE, 0, "file descriptors" }, // -n + { RLIMIT_NOFILE, 0, "open files" }, // -n #endif #ifdef RLIMIT_AS - { RLIMIT_AS, 10, "address space (kb)" }, // -v + { RLIMIT_AS, 10, "virtual memory (kb)" }, // -v #endif #ifdef RLIMIT_LOCKS - { RLIMIT_LOCKS, 0, "locks" }, // -w + { RLIMIT_LOCKS, 0, "file locks" }, // -x #endif #ifdef RLIMIT_NICE { RLIMIT_NICE, 0, "scheduling priority" }, // -e @@ -378,6 +378,10 @@ static const struct limits limits_tbl[] = { { RLIMIT_RTPRIO, 0, "real-time priority" }, // -r #endif }; +// bash also has these: +//pending signals (-i) 61858 //RLIMIT_SIGPENDING +//pipe size (512 bytes, -p) 8 +//POSIX message queues (bytes, -q) 819200 //RLIMIT_MSGQUEUE static const char limit_chars[] ALIGN1 = "f" @@ -400,7 +404,7 @@ static const char limit_chars[] ALIGN1 = "l" #endif #ifdef RLIMIT_NPROC - "p" + "u" #endif #ifdef RLIMIT_NOFILE "n" @@ -409,7 +413,7 @@ static const char limit_chars[] ALIGN1 = "v" #endif #ifdef RLIMIT_LOCKS - "w" + "x" #endif #ifdef RLIMIT_NICE "e" @@ -441,7 +445,7 @@ static const char ulimit_opt_string[] ALIGN1 = "-HSa" "l::" #endif #ifdef RLIMIT_NPROC - "p::" + "u::" #endif #ifdef RLIMIT_NOFILE "n::" @@ -450,7 +454,7 @@ static const char ulimit_opt_string[] ALIGN1 = "-HSa" "v::" #endif #ifdef RLIMIT_LOCKS - "w::" + "x::" #endif #ifdef RLIMIT_NICE "e::" @@ -571,7 +575,7 @@ shell_builtin_ulimit(char **argv) if (opts & OPT_all) { for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) { getrlimit(limits_tbl[i].cmd, &limit); - printf("-%c: %-30s ", limit_chars[i], limits_tbl[i].name); + printf("%-32s(-%c) ", limits_tbl[i].name, limit_chars[i]); printlim(opts, &limit, &limits_tbl[i]); } return EXIT_SUCCESS; @@ -604,7 +608,7 @@ shell_builtin_ulimit(char **argv) getrlimit(limits_tbl[i].cmd, &limit); if (!val_str) { if (opt_cnt > 1) - printf("-%c: %-30s ", limit_chars[i], limits_tbl[i].name); + printf("%-32s(-%c) ", limits_tbl[i].name, limit_chars[i]); printlim(opts, &limit, &limits_tbl[i]); } else { rlim_t val = RLIM_INFINITY; -- cgit v1.2.3-55-g6feb From 57e1b0ad5ebd77705841fbcd01a79f2552fbab8e Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 28 Apr 2019 11:20:09 +0200 Subject: ash,hush: bash compat for ulimit: reorder to match Signed-off-by: Denys Vlasenko --- shell/shell_common.c | 125 ++++++++++++++++++++++----------------------------- 1 file changed, 53 insertions(+), 72 deletions(-) (limited to 'shell/shell_common.c') diff --git a/shell/shell_common.c b/shell/shell_common.c index a992682a8..cc518d54b 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -339,44 +339,38 @@ struct limits { }; static const struct limits limits_tbl[] = { -/* No RLIMIT_FSIZE define guard since -f is the default limit and this must exist */ + { RLIMIT_CORE, 9, "core file size (blocks)" }, // -c + { RLIMIT_DATA, 10, "data seg size (kb)" }, // -d + { RLIMIT_NICE, 0, "scheduling priority" }, // -e { RLIMIT_FSIZE, 9, "file size (blocks)" }, // -f -#ifdef RLIMIT_CPU - { RLIMIT_CPU, 0, "cpu time (seconds)" }, // -t +#define LIMIT_F_IDX 3 +#ifdef RLIMIT_MEMLOCK + { RLIMIT_MEMLOCK, 10, "max locked memory (kb)" }, // -l #endif -#ifdef RLIMIT_DATA - { RLIMIT_DATA, 10, "data seg size (kb)" }, // -d +#ifdef RLIMIT_RSS + { RLIMIT_RSS, 10, "max memory size (kb)" }, // -m #endif -#ifdef RLIMIT_STACK - { RLIMIT_STACK, 10, "stack size (kb)" }, // -s +#ifdef RLIMIT_NOFILE + { RLIMIT_NOFILE, 0, "open files" }, // -n #endif -#ifdef RLIMIT_CORE - { RLIMIT_CORE, 9, "core file size (blocks)" }, // -c +#ifdef RLIMIT_RTPRIO + { RLIMIT_RTPRIO, 0, "real-time priority" }, // -r #endif -#ifdef RLIMIT_RSS - { RLIMIT_RSS, 10, "max memory size (kb)" }, // -m +#ifdef RLIMIT_STACK + { RLIMIT_STACK, 10, "stack size (kb)" }, // -s #endif -#ifdef RLIMIT_MEMLOCK - { RLIMIT_MEMLOCK, 10, "max locked memory (kb)" }, // -l +#ifdef RLIMIT_CPU + { RLIMIT_CPU, 0, "cpu time (seconds)" }, // -t #endif #ifdef RLIMIT_NPROC { RLIMIT_NPROC, 0, "max user processes" }, // -u #endif -#ifdef RLIMIT_NOFILE - { RLIMIT_NOFILE, 0, "open files" }, // -n -#endif #ifdef RLIMIT_AS { RLIMIT_AS, 10, "virtual memory (kb)" }, // -v #endif #ifdef RLIMIT_LOCKS { RLIMIT_LOCKS, 0, "file locks" }, // -x #endif -#ifdef RLIMIT_NICE - { RLIMIT_NICE, 0, "scheduling priority" }, // -e -#endif -#ifdef RLIMIT_RTPRIO - { RLIMIT_RTPRIO, 0, "real-time priority" }, // -r -#endif }; // bash also has these: //pending signals (-i) 61858 //RLIMIT_SIGPENDING @@ -384,85 +378,73 @@ static const struct limits limits_tbl[] = { //POSIX message queues (bytes, -q) 819200 //RLIMIT_MSGQUEUE static const char limit_chars[] ALIGN1 = + "c" + "d" + "e" "f" -#ifdef RLIMIT_CPU - "t" +#ifdef RLIMIT_MEMLOCK + "l" #endif -#ifdef RLIMIT_DATA - "d" +#ifdef RLIMIT_RSS + "m" #endif -#ifdef RLIMIT_STACK - "s" +#ifdef RLIMIT_NOFILE + "n" #endif -#ifdef RLIMIT_CORE - "c" +#ifdef RLIMIT_RTPRIO + "r" #endif -#ifdef RLIMIT_RSS - "m" +#ifdef RLIMIT_STACK + "s" #endif -#ifdef RLIMIT_MEMLOCK - "l" +#ifdef RLIMIT_CPU + "t" #endif #ifdef RLIMIT_NPROC "u" #endif -#ifdef RLIMIT_NOFILE - "n" -#endif #ifdef RLIMIT_AS "v" #endif #ifdef RLIMIT_LOCKS "x" #endif -#ifdef RLIMIT_NICE - "e" -#endif -#ifdef RLIMIT_RTPRIO - "r" -#endif ; /* "-": treat args as parameters of option with ASCII code 1 */ static const char ulimit_opt_string[] ALIGN1 = "-HSa" + "c::" + "d::" + "e::" "f::" -#ifdef RLIMIT_CPU - "t::" +#ifdef RLIMIT_MEMLOCK + "l::" #endif -#ifdef RLIMIT_DATA - "d::" +#ifdef RLIMIT_RSS + "m::" #endif -#ifdef RLIMIT_STACK - "s::" +#ifdef RLIMIT_NOFILE + "n::" #endif -#ifdef RLIMIT_CORE - "c::" +#ifdef RLIMIT_RTPRIO + "r::" #endif -#ifdef RLIMIT_RSS - "m::" +#ifdef RLIMIT_STACK + "s::" #endif -#ifdef RLIMIT_MEMLOCK - "l::" +#ifdef RLIMIT_CPU + "t::" #endif #ifdef RLIMIT_NPROC "u::" #endif -#ifdef RLIMIT_NOFILE - "n::" -#endif #ifdef RLIMIT_AS "v::" #endif #ifdef RLIMIT_LOCKS "x::" #endif -#ifdef RLIMIT_NICE - "e::" -#endif -#ifdef RLIMIT_RTPRIO - "r::" -#endif - ; +; enum { OPT_hard = (1 << 0), @@ -595,11 +577,10 @@ shell_builtin_ulimit(char **argv) continue; //if (opt_char == 'a') - impossible - i = 0; /* if "ulimit NNN", -f is assumed */ - if (opt_char != 1) { - i = strchrnul(limit_chars, opt_char) - limit_chars; - //if (i >= ARRAY_SIZE(limits_tbl)) - bad option, impossible - } + if (opt_char == 1) /* if "ulimit NNN", -f is assumed */ + opt_char = 'f'; + i = strchrnul(limit_chars, opt_char) - limit_chars; + //if (i >= ARRAY_SIZE(limits_tbl)) - bad option, impossible val_str = optarg; if (!val_str && argv[optind] && argv[optind][0] != '-') @@ -643,8 +624,8 @@ shell_builtin_ulimit(char **argv) if (opt_cnt == 0) { /* "bare ulimit": treat it as if it was -f */ - getrlimit(limits_tbl[0].cmd, &limit); - printlim(opts, &limit, &limits_tbl[0]); + getrlimit(limits_tbl[LIMIT_F_IDX].cmd, &limit); + printlim(opts, &limit, &limits_tbl[LIMIT_F_IDX]); } return EXIT_SUCCESS; -- cgit v1.2.3-55-g6feb From 93f0b39a0712f3247349f1590757484ca18e725e Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 28 Apr 2019 11:25:11 +0200 Subject: ash,hush: ulimit: add -i RLIMIT_SIGPENDING, -q RLIMIT_MSGQUEUE function old new delta limits_tbl 104 120 +16 ulimit_opt_string 44 50 +6 limit_chars 14 16 +2 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/0 up/down: 24/0) Total: 24 bytes text data bss dec hex filename 981996 485 7296 989777 f1a51 busybox_old 982065 485 7296 989846 f1a96 busybox_unstripped Signed-off-by: Denys Vlasenko --- shell/shell_common.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'shell/shell_common.c') diff --git a/shell/shell_common.c b/shell/shell_common.c index cc518d54b..da3165329 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -344,6 +344,9 @@ static const struct limits limits_tbl[] = { { RLIMIT_NICE, 0, "scheduling priority" }, // -e { RLIMIT_FSIZE, 9, "file size (blocks)" }, // -f #define LIMIT_F_IDX 3 +#ifdef RLIMIT_SIGPENDING + { RLIMIT_SIGPENDING, 0, "pending signals" }, // -i +#endif #ifdef RLIMIT_MEMLOCK { RLIMIT_MEMLOCK, 10, "max locked memory (kb)" }, // -l #endif @@ -353,6 +356,9 @@ static const struct limits limits_tbl[] = { #ifdef RLIMIT_NOFILE { RLIMIT_NOFILE, 0, "open files" }, // -n #endif +#ifdef RLIMIT_MSGQUEUE + { RLIMIT_MSGQUEUE, 0, "POSIX message queues (bytes)" }, // -q +#endif #ifdef RLIMIT_RTPRIO { RLIMIT_RTPRIO, 0, "real-time priority" }, // -r #endif @@ -372,16 +378,17 @@ static const struct limits limits_tbl[] = { { RLIMIT_LOCKS, 0, "file locks" }, // -x #endif }; -// bash also has these: -//pending signals (-i) 61858 //RLIMIT_SIGPENDING +// bash also shows: //pipe size (512 bytes, -p) 8 -//POSIX message queues (bytes, -q) 819200 //RLIMIT_MSGQUEUE static const char limit_chars[] ALIGN1 = "c" "d" "e" "f" +#ifdef RLIMIT_SIGPENDING + "i" +#endif #ifdef RLIMIT_MEMLOCK "l" #endif @@ -391,6 +398,9 @@ static const char limit_chars[] ALIGN1 = #ifdef RLIMIT_NOFILE "n" #endif +#ifdef RLIMIT_MSGQUEUE + "q" +#endif #ifdef RLIMIT_RTPRIO "r" #endif @@ -417,6 +427,9 @@ static const char ulimit_opt_string[] ALIGN1 = "-HSa" "d::" "e::" "f::" +#ifdef RLIMIT_SIGPENDING + "i::" +#endif #ifdef RLIMIT_MEMLOCK "l::" #endif @@ -426,6 +439,9 @@ static const char ulimit_opt_string[] ALIGN1 = "-HSa" #ifdef RLIMIT_NOFILE "n::" #endif +#ifdef RLIMIT_MSGQUEUE + "q::" +#endif #ifdef RLIMIT_RTPRIO "r::" #endif -- cgit v1.2.3-55-g6feb From d8bd7012a30c6ce9efe26d06880ac223143709ad Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 14 May 2019 18:53:24 +0200 Subject: hush: fix "export PS1=xyz" and "local PS1=xyz" messing up prompt function old new delta helper_export_local 215 253 +38 leave_var_nest_level 107 127 +20 run_pipe 1840 1857 +17 handle_changed_special_names 101 105 +4 shell_builtin_read 1399 1398 -1 done_word 767 766 -1 parse_stream 2249 2245 -4 set_local_var 437 430 -7 is_well_formed_var_name 66 - -66 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 4/4 up/down: 79/-79) Total: 0 bytes text data bss dec hex filename 952376 485 7296 960157 ea69d busybox_old 952400 485 7296 960181 ea6b5 busybox_unstripped Signed-off-by: Denys Vlasenko --- shell/hush.c | 55 +++++++++++++++++++++++++++++++++++++--------------- shell/shell_common.c | 15 +------------- shell/shell_common.h | 2 -- 3 files changed, 40 insertions(+), 32 deletions(-) (limited to 'shell/shell_common.c') diff --git a/shell/hush.c b/shell/hush.c index b3ae73b9b..b612c80da 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -2248,9 +2248,12 @@ static const char* FAST_FUNC get_local_var_value(const char *name) return NULL; } +#if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT) \ + || (ENABLE_HUSH_LINENO_VAR || ENABLE_HUSH_GETOPTS) static void handle_changed_special_names(const char *name, unsigned name_len) { if (ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT + && G_interactive_fd && name_len == 3 && name[0] == 'P' && name[1] == 'S' ) { cmdedit_update_prompt(); @@ -2274,6 +2277,10 @@ static void handle_changed_special_names(const char *name, unsigned name_len) #endif } } +#else +/* Do not even bother evaluating arguments */ +# define handle_changed_special_names(...) ((void)0) +#endif /* str holds "NAME=VAL" and is expected to be malloced. * We take ownership of it. @@ -2289,6 +2296,7 @@ static int set_local_var(char *str, unsigned flags) char *free_me = NULL; char *eq_sign; int name_len; + int retval; unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT); eq_sign = strchr(str, '='); @@ -2402,24 +2410,24 @@ static int set_local_var(char *str, unsigned flags) #endif if (flags & SETFLAG_EXPORT) cur->flg_export = 1; + retval = 0; if (cur->flg_export) { if (flags & SETFLAG_UNEXPORT) { cur->flg_export = 0; /* unsetenv was already done */ } else { - int i; debug_printf_env("%s: putenv '%s'/%u\n", __func__, cur->varstr, cur->var_nest_level); - i = putenv(cur->varstr); - /* only now we can free old exported malloced string */ - free(free_me); - return i; + retval = putenv(cur->varstr); + /* fall through to "free(free_me)" - + * only now we can free old exported malloced string + */ } } free(free_me); handle_changed_special_names(cur->varstr, name_len - 1); - return 0; + return retval; } static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) @@ -2492,6 +2500,11 @@ static void add_vars(struct variable *var) } else { debug_printf_env("%s: restoring variable '%s'/%u\n", __func__, var->varstr, var->var_nest_level); } + /* Testcase (interactive): + * f() { local PS1='\w \$ '; }; f + * the below call is needed to notice restored PS1 when f returns. + */ + handle_changed_special_names(var->varstr, endofname(var->varstr) - var->varstr); var = next; } } @@ -4198,7 +4211,7 @@ static int done_word(struct parse_context *ctx) #if ENABLE_HUSH_LOOPS if (ctx->ctx_res_w == RES_FOR) { if (ctx->word.has_quoted_part - || !is_well_formed_var_name(command->argv[0], '\0') + || endofname(command->argv[0])[0] != '\0' ) { /* bash says just "not a valid identifier" */ syntax_error("not a valid identifier in for"); @@ -5372,7 +5385,7 @@ static struct pipe *parse_stream(char **pstring, if ((ctx.is_assignment == MAYBE_ASSIGNMENT || ctx.is_assignment == WORD_IS_KEYWORD) && ch == '=' - && is_well_formed_var_name(ctx.word.data, '=') + && endofname(ctx.word.data)[0] == '=' ) { ctx.is_assignment = DEFINITELY_ASSIGNMENT; debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]); @@ -7598,10 +7611,10 @@ static int save_fd_on_redirect(int fd, int avoid_fd, struct squirrel **sqp) avoid_fd = 9; #if ENABLE_HUSH_INTERACTIVE - if (fd == G.interactive_fd) { + if (fd == G_interactive_fd) { /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */ - G.interactive_fd = xdup_CLOEXEC_and_close(G.interactive_fd, avoid_fd); - debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G.interactive_fd); + G_interactive_fd = xdup_CLOEXEC_and_close(G_interactive_fd, avoid_fd); + debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G_interactive_fd); return 1; /* "we closed fd" */ } #endif @@ -7677,7 +7690,7 @@ static void restore_redirects(struct squirrel *sq) free(sq); } - /* If moved, G.interactive_fd stays on new fd, not restoring it */ + /* If moved, G_interactive_fd stays on new fd, not restoring it */ } #if ENABLE_FEATURE_SH_STANDALONE && BB_MMU @@ -7694,7 +7707,7 @@ static int internally_opened_fd(int fd, struct squirrel *sq) int i; #if ENABLE_HUSH_INTERACTIVE - if (fd == G.interactive_fd) + if (fd == G_interactive_fd) return 1; #endif /* If this one of script's fds? */ @@ -7885,6 +7898,11 @@ static void remove_nested_vars(void) *cur_pp = cur->next; /* Free */ if (!cur->max_len) { + /* Testcase (interactive): + * f() { local PS1='\w \$ '; }; f + * we should forget local PS1: + */ + handle_changed_special_names(cur->varstr, endofname(cur->varstr) - cur->varstr); debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level); free(cur->varstr); } @@ -10687,9 +10705,7 @@ static int helper_export_local(char **argv, unsigned flags) { do { char *name = *argv; - char *name_end = strchrnul(name, '='); - - /* So far we do not check that name is valid (TODO?) */ + const char *name_end = endofname(name); if (*name_end == '\0') { struct variable *var, **vpp; @@ -10747,8 +10763,15 @@ static int helper_export_local(char **argv, unsigned flags) */ name = xasprintf("%s=", name); } else { + if (*name_end != '=') { + bb_error_msg("'%s': bad variable name", name); + /* do not parse following argv[]s: */ + return 1; + } /* (Un)exporting/making local NAME=VALUE */ name = xstrdup(name); + /* Testcase: export PS1='\w \$ ' */ + unbackslash(name); } debug_printf_env("%s: set_local_var('%s')\n", __func__, name); if (set_local_var(name, flags)) diff --git a/shell/shell_common.c b/shell/shell_common.c index da3165329..e0582adfb 100644 --- a/shell/shell_common.c +++ b/shell/shell_common.c @@ -22,19 +22,6 @@ 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; -} - /* read builtin */ /* Needs to be interruptible: shell must handle traps and shell-special signals @@ -70,7 +57,7 @@ shell_builtin_read(struct builtin_read_params *params) argv = params->argv; pp = argv; while (*pp) { - if (!is_well_formed_var_name(*pp, '\0')) { + if (endofname(*pp)[0] != '\0') { /* Mimic bash message */ bb_error_msg("read: '%s': not a valid identifier", *pp); return (const char *)(uintptr_t)1; diff --git a/shell/shell_common.h b/shell/shell_common.h index a1323021d..7b478f1df 100644 --- a/shell/shell_common.h +++ b/shell/shell_common.h @@ -26,8 +26,6 @@ extern const char defifsvar[] ALIGN1; /* "IFS= \t\n" */ extern const char defoptindvar[] ALIGN1; /* "OPTIND=1" */ -int FAST_FUNC is_well_formed_var_name(const char *s, char terminator); - /* Builtins */ struct builtin_read_params { -- cgit v1.2.3-55-g6feb