It's my fault that toybox ulimit claim units but doesn't use them; I added them to match bash without noticing that the kernel always uses bytes. I'm assuming we want to resolve that in favor of actually honoring the units, and removing that from the known differences from bash, but I don't have a strong opinion (other than that we should be self-consistent).
One thing that's awkward now is that `ulimit -s 16m` used to do what you'd expect, but now gives you 1024 times that. Given a clean slate, I'd say "bytes are obviously the right choice for input, and toybox already transparently lets you choose your own multipliers". But it seems odd that `ulimit -s 8192` would mean different things to bash or toybox sh? I've added a couple of other existing differences to the list of bash deviations, but not fixed them in this patch because the obvious fix would make the diff unreadable (and because I only noticed those differences playing around on the command line to test this --- no-one else has noticed yet, that I know). --- toys/posix/ulimit.c | 62 +++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 24 deletions(-)
From 032a003bc0cb4842d90f01c0fa5f8a4ff6692c96 Mon Sep 17 00:00:00 2001 From: Elliott Hughes <e...@google.com> Date: Wed, 10 May 2023 18:05:03 -0700 Subject: [PATCH] ulimit: actually use the units we claim. It's my fault that toybox ulimit claim units but doesn't use them; I added them to match bash without noticing that the kernel always uses bytes. I'm assuming we want to resolve that in favor of actually honoring the units, and removing that from the known differences from bash, but I don't have a strong opinion (other than that we should be self-consistent). One thing that's awkward now is that `ulimit -s 16m` used to do what you'd expect, but now gives you 1024 times that. Given a clean slate, I'd say "bytes are obviously the right choice for input, and toybox already transparently lets you choose your own multipliers". But it seems odd that `ulimit -s 8192` would mean different things to bash or toybox sh? I've added a couple of other existing differences to the list of bash deviations, but not fixed them in this patch because the obvious fix would make the diff unreadable (and because I only noticed those differences playing around on the command line to test this --- no-one else has noticed yet, that I know). --- toys/posix/ulimit.c | 62 +++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/toys/posix/ulimit.c b/toys/posix/ulimit.c index 642f8871..d808e7ce 100644 --- a/toys/posix/ulimit.c +++ b/toys/posix/ulimit.c @@ -10,7 +10,7 @@ * do that anymore). Bash uses 1024 byte blocks, so they don't care either. * We consistently use bytes everywhere we can. * - * Deviations from bash: Sizes are in bytes (instead of -p 512 and -f 1024). + * Deviations from bash: bash allows `-c -c 10 -c`, and allows -a and a set. * Bash's -p value has been wrong since 2010 (git 35f3d14dbbc5). * The kernel implementation of RLIMIT_LOCKS (-x) was removed from Linux in * 2003. Bash never implemented -b (it's in the help but unrecognized at @@ -61,19 +61,31 @@ void ulimit_main(void) { struct rlimit rr; int i; - // map and desc are in the same order as flags. + // limits[] is in the same order as flags. // The Linux kernel implementation of RLIMIT_LOCKS (-x) was removed in 2003. char *flags="cdefilmnpqRrstuv"; - char map[] = {RLIMIT_CORE, RLIMIT_DATA, RLIMIT_NICE, - RLIMIT_FSIZE, RLIMIT_SIGPENDING, RLIMIT_MEMLOCK, - RLIMIT_RSS, RLIMIT_NOFILE, 0, RLIMIT_MSGQUEUE, - RLIMIT_RTTIME, RLIMIT_RTPRIO, RLIMIT_STACK, RLIMIT_CPU, RLIMIT_NPROC, - RLIMIT_AS}; - char *desc[]={"core dump size (blocks)", "data size (KiB)", "max nice", - "file size (blocks)", "pending signals", "locked memory (KiB)", - "RSS (KiB)", "open files", "pipe size (512 bytes)", "message queues", - "RT time (us)", "RT priority", "stack (KiB)", "cpu time (s)", "processes", - "address space (KiB)"}; + struct limit { + int resource; + int unit; + char *desc; + } limits[] = { + {RLIMIT_CORE, 1024, "core dump size (blocks)"}, + {RLIMIT_DATA, 1024, "data size (KiB)"}, + {RLIMIT_NICE, 1, "max nice"}, + {RLIMIT_FSIZE, 1024, "file size (blocks)"}, + {RLIMIT_SIGPENDING, 1, "pending signals"}, + {RLIMIT_MEMLOCK, 1024, "locked memory (KiB)"}, + {RLIMIT_RSS, 1024, "RSS (KiB)"}, + {RLIMIT_NOFILE, 1, "open files"}, + {0, 512, "pipe size (512 bytes)"}, + {RLIMIT_MSGQUEUE, 1, "message queues"}, + {RLIMIT_RTTIME, 1, "RT time (us)"}, + {RLIMIT_RTPRIO, 1, "RT priority"}, + {RLIMIT_STACK, 1024, "stack (KiB)"}, + {RLIMIT_CPU, 1, "cpu time (s)"}, + {RLIMIT_NPROC, 1, "processes"}, + {RLIMIT_AS, 1024, "address space"}, + }; if (!(toys.optflags&(FLAG_H-1))) toys.optflags |= FLAG_f; if ((FLAG(a)||FLAG(p)) && toys.optc) error_exit("can't set -ap"); @@ -81,28 +93,30 @@ void ulimit_main(void) // Fetch data if (!FLAG(P)) TT.P = getppid(); - for (i=0; i<sizeof(map); i++) { + for (i=0; i<ARRAY_LEN(limits); i++) { int get = toys.optflags&(FLAG_a|(1<<i)); - if (get && prlimit(TT.P, map[i], 0, &rr)) perror_exit("-%c", flags[i]); + if (get && prlimit(TT.P, limits[i].resource, 0, &rr)) + perror_exit("-%c", flags[i]); if (!toys.optc) { - if (FLAG(a)) printf("-%c: %-25s ", flags[i], desc[i]); + if (FLAG(a)) printf("-%c: %-25s ", flags[i], limits[i].desc); if (get) { if ((1<<i)&FLAG_p) { - if (FLAG(H)) - xreadfile("/proc/sys/fs/pipe-max-size", toybuf, sizeof(toybuf)); - else { - int pp[2]; + int pipe_size, pp[2]; + if (FLAG(H)) { + xreadfile("/proc/sys/fs/pipe-max-size", toybuf, sizeof(toybuf)); + pipe_size = atoi(toybuf) / 512; + } else { xpipe(pp); - sprintf(toybuf, "%d\n", fcntl(*pp, F_GETPIPE_SZ)); + pipe_size = fcntl(*pp, F_GETPIPE_SZ) / 512; } - printf("%s", toybuf); + printf("%d\n", pipe_size); } else { rlim_t rl = FLAG(H) ? rr.rlim_max : rr.rlim_cur; if (rl == RLIM_INFINITY) printf("unlimited\n"); - else printf("%ld\n", (long)rl); + else printf("%ld\n", (long)(rl / limits[i].unit)); } } } @@ -115,10 +129,10 @@ void ulimit_main(void) rlim_t val; if (tolower(**toys.optargs) == 'u') val = RLIM_INFINITY; - else val = atolx_range(*toys.optargs, 0, LONG_MAX); + else val = atolx_range(*toys.optargs, 0, LONG_MAX) * limits[i].unit; if (FLAG(H)) rr.rlim_max = val; else rr.rlim_cur = val; - if (prlimit(TT.P, map[i], &rr, 0)) perror_exit(0); + if (prlimit(TT.P, limits[i].resource, &rr, 0)) perror_exit(0); } } -- 2.40.1.606.ga4b1b128d6-goog
_______________________________________________ Toybox mailing list Toybox@lists.landley.net http://lists.landley.net/listinfo.cgi/toybox-landley.net