hush: use ash's ulimit builtin; make it more more bash0like while at it

Based on a patch by Tobias Klauser <tklauser@distanz.ch>

function                                             old     new   delta
shell_builtin_ulimit                                   -     498    +498
limits_tbl                                            33      88     +55
ulimit_opt_string                                      -      38     +38
bltins1                                              288     300     +12
limits_name                                          127       -    -127
ulimitcmd                                            415       7    -408
------------------------------------------------------------------------------
(add/remove: 3/1 grow/shrink: 2/1 up/down: 603/-535)           Total: 68 bytes
   text    data     bss     dec     hex filename
 839229     453    6828  846510   ceaae busybox_old
 839423     453    6828  846704   ceb70 busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2010-03-06 20:12:00 +01:00
parent cbcc1236f8
commit f3c742f925
5 changed files with 253 additions and 215 deletions

View File

@@ -51,6 +51,7 @@
#include "shell_common.h"
#include "builtin_read.h"
#include "builtin_ulimit.h"
#include "math.h"
#if ENABLE_ASH_RANDOM_SUPPORT
# include "random.h"
@@ -12614,219 +12615,10 @@ umaskcmd(int argc UNUSED_PARAM, char **argv)
return 0;
}
/*
* ulimit builtin
*
* This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and
* Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with
* ash by J.T. Conklin.
*
* Public domain.
*/
struct limits {
uint8_t cmd; /* RLIMIT_xxx fit into it */
uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
char option;
};
static const struct limits limits_tbl[] = {
#ifdef RLIMIT_CPU
{ RLIMIT_CPU, 0, 't' },
#endif
#ifdef RLIMIT_FSIZE
{ RLIMIT_FSIZE, 9, 'f' },
#endif
#ifdef RLIMIT_DATA
{ RLIMIT_DATA, 10, 'd' },
#endif
#ifdef RLIMIT_STACK
{ RLIMIT_STACK, 10, 's' },
#endif
#ifdef RLIMIT_CORE
{ RLIMIT_CORE, 9, 'c' },
#endif
#ifdef RLIMIT_RSS
{ RLIMIT_RSS, 10, 'm' },
#endif
#ifdef RLIMIT_MEMLOCK
{ RLIMIT_MEMLOCK, 10, 'l' },
#endif
#ifdef RLIMIT_NPROC
{ RLIMIT_NPROC, 0, 'p' },
#endif
#ifdef RLIMIT_NOFILE
{ RLIMIT_NOFILE, 0, 'n' },
#endif
#ifdef RLIMIT_AS
{ RLIMIT_AS, 10, 'v' },
#endif
#ifdef RLIMIT_LOCKS
{ RLIMIT_LOCKS, 0, 'w' },
#endif
};
static const char limits_name[] =
#ifdef RLIMIT_CPU
"time(seconds)" "\0"
#endif
#ifdef RLIMIT_FSIZE
"file(blocks)" "\0"
#endif
#ifdef RLIMIT_DATA
"data(kb)" "\0"
#endif
#ifdef RLIMIT_STACK
"stack(kb)" "\0"
#endif
#ifdef RLIMIT_CORE
"coredump(blocks)" "\0"
#endif
#ifdef RLIMIT_RSS
"memory(kb)" "\0"
#endif
#ifdef RLIMIT_MEMLOCK
"locked memory(kb)" "\0"
#endif
#ifdef RLIMIT_NPROC
"process" "\0"
#endif
#ifdef RLIMIT_NOFILE
"nofiles" "\0"
#endif
#ifdef RLIMIT_AS
"vmemory(kb)" "\0"
#endif
#ifdef RLIMIT_LOCKS
"locks" "\0"
#endif
;
enum limtype { SOFT = 0x1, HARD = 0x2 };
static void
printlim(enum limtype how, const struct rlimit *limit,
const struct limits *l)
{
rlim_t val;
val = limit->rlim_max;
if (how & SOFT)
val = limit->rlim_cur;
if (val == RLIM_INFINITY)
out1fmt("unlimited\n");
else {
val >>= l->factor_shift;
out1fmt("%lld\n", (long long) val);
}
}
static int FAST_FUNC
ulimitcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
ulimitcmd(int argc UNUSED_PARAM, char **argv)
{
rlim_t val;
enum limtype how = SOFT | HARD;
const struct limits *l;
int set, all = 0;
int optc, what;
struct rlimit limit;
what = 'f';
while ((optc = nextopt("HSa"
#ifdef RLIMIT_CPU
"t"
#endif
#ifdef RLIMIT_FSIZE
"f"
#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
)) != '\0')
switch (optc) {
case 'H':
how = HARD;
break;
case 'S':
how = SOFT;
break;
case 'a':
all = 1;
break;
default:
what = optc;
}
for (l = limits_tbl; l->option != what; l++)
continue;
set = *argptr ? 1 : 0;
val = 0;
if (set) {
char *p = *argptr;
if (all || argptr[1])
ash_msg_and_raise_error("too many arguments");
if (strncmp(p, "unlimited\n", 9) == 0)
val = RLIM_INFINITY;
else {
if (sizeof(val) == sizeof(int))
val = bb_strtou(p, NULL, 10);
else if (sizeof(val) == sizeof(long))
val = bb_strtoul(p, NULL, 10);
else
val = bb_strtoull(p, NULL, 10);
if (errno)
ash_msg_and_raise_error("bad number");
val <<= l->factor_shift;
}
}
if (all) {
const char *lname = limits_name;
for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
getrlimit(l->cmd, &limit);
out1fmt("%-20s ", lname);
lname += strlen(lname) + 1;
printlim(how, &limit, l);
}
return 0;
}
getrlimit(l->cmd, &limit);
if (set) {
if (how & HARD)
limit.rlim_max = val;
if (how & SOFT)
limit.rlim_cur = val;
if (setrlimit(l->cmd, &limit) < 0)
ash_msg_and_raise_error("error setting limit (%m)");
} else {
printlim(how, &limit, l);
}
return 0;
return shell_builtin_ulimit(argv);
}
/* ============ main() and helpers */