2010-01-12 21:11:24 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Adapted from ash applet code
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Kenneth Almquist.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1989, 1991, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
|
|
|
|
* was re-ported from NetBSD and debianized.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010 Denys Vlasenko
|
|
|
|
* Split from ash.c
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2010-01-12 21:11:24 +00:00
|
|
|
*/
|
|
|
|
#include "libbb.h"
|
|
|
|
#include "shell_common.h"
|
|
|
|
|
|
|
|
const char defifsvar[] ALIGN1 = "IFS= \t\n";
|
2016-09-30 12:36:59 +00:00
|
|
|
const char defoptindvar[] ALIGN1 = "OPTIND=1";
|
2010-01-13 17:22:35 +00:00
|
|
|
|
2010-05-23 15:49:50 +00:00
|
|
|
/* read builtin */
|
|
|
|
|
2013-07-13 23:23:06 +00:00
|
|
|
/* Needs to be interruptible: shell must handle traps and shell-special signals
|
2011-05-08 19:23:43 +00:00
|
|
|
* while inside read. To implement this, be sure to not loop on EINTR
|
|
|
|
* and return errno == EINTR reliably.
|
|
|
|
*/
|
2010-05-23 15:49:50 +00:00
|
|
|
//TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
|
|
|
|
//string. hush naturally has it, and ash has setvareq().
|
|
|
|
//Here we can simply store "VAR=" at buffer start and store read data directly
|
|
|
|
//after "=", then pass buffer to setvar() to consume.
|
|
|
|
const char* FAST_FUNC
|
2018-08-05 13:42:29 +00:00
|
|
|
shell_builtin_read(struct builtin_read_params *params)
|
2010-05-23 15:49:50 +00:00
|
|
|
{
|
2017-07-20 14:09:31 +00:00
|
|
|
struct pollfd pfd[1];
|
|
|
|
#define fd (pfd[0].fd) /* -u FD */
|
2011-05-08 19:23:43 +00:00
|
|
|
unsigned err;
|
2010-05-23 15:49:50 +00:00
|
|
|
unsigned end_ms; /* -t TIMEOUT */
|
|
|
|
int nchars; /* -n NUM */
|
|
|
|
char **pp;
|
|
|
|
char *buffer;
|
2017-08-09 12:04:07 +00:00
|
|
|
char delim;
|
2010-05-23 15:49:50 +00:00
|
|
|
struct termios tty, old_tty;
|
|
|
|
const char *retval;
|
|
|
|
int bufpos; /* need to be able to hold -1 */
|
|
|
|
int startword;
|
|
|
|
smallint backslash;
|
2018-08-05 13:42:29 +00:00
|
|
|
char **argv;
|
|
|
|
const char *ifs;
|
|
|
|
int read_flags;
|
2010-05-23 15:49:50 +00:00
|
|
|
|
2011-05-08 19:23:43 +00:00
|
|
|
errno = err = 0;
|
|
|
|
|
2018-08-05 13:42:29 +00:00
|
|
|
argv = params->argv;
|
2010-05-23 15:49:50 +00:00
|
|
|
pp = argv;
|
|
|
|
while (*pp) {
|
2019-05-14 16:53:24 +00:00
|
|
|
if (endofname(*pp)[0] != '\0') {
|
2010-05-23 15:49:50 +00:00
|
|
|
/* Mimic bash message */
|
2021-06-06 10:07:11 +00:00
|
|
|
bb_error_msg("read: '%s': bad variable name", *pp);
|
2010-05-23 15:49:50 +00:00
|
|
|
return (const char *)(uintptr_t)1;
|
|
|
|
}
|
|
|
|
pp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
nchars = 0; /* if != 0, -n is in effect */
|
2018-08-05 13:42:29 +00:00
|
|
|
if (params->opt_n) {
|
|
|
|
nchars = bb_strtou(params->opt_n, NULL, 10);
|
2010-05-23 15:49:50 +00:00
|
|
|
if (nchars < 0 || errno)
|
|
|
|
return "invalid count";
|
|
|
|
/* note: "-n 0": off (bash 3.2 does this too) */
|
|
|
|
}
|
2017-07-20 14:09:31 +00:00
|
|
|
|
2010-05-23 15:49:50 +00:00
|
|
|
end_ms = 0;
|
2018-08-05 13:42:29 +00:00
|
|
|
if (params->opt_t && !ENABLE_FEATURE_SH_READ_FRAC) {
|
|
|
|
end_ms = bb_strtou(params->opt_t, NULL, 10);
|
2017-07-20 14:09:31 +00:00
|
|
|
if (errno)
|
2010-05-23 15:49:50 +00:00
|
|
|
return "invalid timeout";
|
2017-07-20 14:09:31 +00:00
|
|
|
if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */
|
|
|
|
end_ms = UINT_MAX / 2048;
|
2010-05-23 15:49:50 +00:00
|
|
|
end_ms *= 1000;
|
2017-07-20 14:09:31 +00:00
|
|
|
}
|
2018-08-05 13:42:29 +00:00
|
|
|
if (params->opt_t && ENABLE_FEATURE_SH_READ_FRAC) {
|
2017-07-20 14:09:31 +00:00
|
|
|
/* bash 4.3 (maybe earlier) supports -t N.NNNNNN */
|
|
|
|
char *p;
|
|
|
|
/* Eat up to three fractional digits */
|
|
|
|
int frac_digits = 3 + 1;
|
|
|
|
|
2018-08-05 13:42:29 +00:00
|
|
|
end_ms = bb_strtou(params->opt_t, &p, 10);
|
2017-07-20 14:09:31 +00:00
|
|
|
if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */
|
|
|
|
end_ms = UINT_MAX / 2048;
|
|
|
|
|
|
|
|
if (errno) {
|
|
|
|
/* EINVAL = number is ok, but not NUL terminated */
|
|
|
|
if (errno != EINVAL || *p != '.')
|
|
|
|
return "invalid timeout";
|
|
|
|
/* Do not check the rest: bash allows "0.123456xyz" */
|
|
|
|
while (*++p && --frac_digits) {
|
|
|
|
end_ms *= 10;
|
|
|
|
end_ms += (*p - '0');
|
|
|
|
if ((unsigned char)(*p - '0') > 9)
|
2010-05-23 15:49:50 +00:00
|
|
|
return "invalid timeout";
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 14:09:31 +00:00
|
|
|
while (--frac_digits > 0) {
|
|
|
|
end_ms *= 10;
|
2010-05-23 15:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-20 14:09:31 +00:00
|
|
|
|
2010-05-23 15:49:50 +00:00
|
|
|
fd = STDIN_FILENO;
|
2018-08-05 13:42:29 +00:00
|
|
|
if (params->opt_u) {
|
|
|
|
fd = bb_strtou(params->opt_u, NULL, 10);
|
2010-05-23 15:49:50 +00:00
|
|
|
if (fd < 0 || errno)
|
|
|
|
return "invalid file descriptor";
|
|
|
|
}
|
|
|
|
|
2018-08-05 13:42:29 +00:00
|
|
|
if (params->opt_t && end_ms == 0) {
|
2017-07-20 14:09:31 +00:00
|
|
|
/* "If timeout is 0, read returns immediately, without trying
|
|
|
|
* to read any data. The exit status is 0 if input is available
|
|
|
|
* on the specified file descriptor, non-zero otherwise."
|
|
|
|
* bash seems to ignore -p PROMPT for this use case.
|
|
|
|
*/
|
|
|
|
int r;
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
r = poll(pfd, 1, /*timeout:*/ 0);
|
|
|
|
/* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */
|
|
|
|
return (const char *)(uintptr_t)(r <= 0);
|
|
|
|
}
|
|
|
|
|
2018-08-05 13:42:29 +00:00
|
|
|
if (params->opt_p && isatty(fd)) {
|
|
|
|
fputs(params->opt_p, stderr);
|
2010-05-23 15:49:50 +00:00
|
|
|
fflush_all();
|
|
|
|
}
|
|
|
|
|
2018-08-05 13:42:29 +00:00
|
|
|
ifs = params->ifs;
|
2010-05-23 15:49:50 +00:00
|
|
|
if (ifs == NULL)
|
|
|
|
ifs = defifs;
|
|
|
|
|
2018-08-05 13:42:29 +00:00
|
|
|
read_flags = params->read_flags;
|
2010-05-23 15:49:50 +00:00
|
|
|
if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
|
|
|
|
tcgetattr(fd, &tty);
|
|
|
|
old_tty = tty;
|
|
|
|
if (nchars) {
|
|
|
|
tty.c_lflag &= ~ICANON;
|
2012-01-15 21:58:06 +00:00
|
|
|
// Setting it to more than 1 breaks poll():
|
|
|
|
// it blocks even if there's data. !??
|
|
|
|
//tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
|
2017-01-11 15:17:59 +00:00
|
|
|
/* reads will block only if < 1 char is available */
|
2012-01-15 21:58:06 +00:00
|
|
|
tty.c_cc[VMIN] = 1;
|
|
|
|
/* no timeout (reads block forever) */
|
|
|
|
tty.c_cc[VTIME] = 0;
|
2010-05-23 15:49:50 +00:00
|
|
|
}
|
|
|
|
if (read_flags & BUILTIN_READ_SILENT) {
|
|
|
|
tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
|
|
|
|
}
|
|
|
|
/* This forces execution of "restoring" tcgetattr later */
|
|
|
|
read_flags |= BUILTIN_READ_SILENT;
|
|
|
|
/* if tcgetattr failed, tcsetattr will fail too.
|
|
|
|
* Ignoring, it's harmless. */
|
|
|
|
tcsetattr(fd, TCSANOW, &tty);
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = (const char *)(uintptr_t)0;
|
|
|
|
startword = 1;
|
|
|
|
backslash = 0;
|
2018-08-05 13:42:29 +00:00
|
|
|
if (params->opt_t)
|
2017-07-20 14:09:31 +00:00
|
|
|
end_ms += (unsigned)monotonic_ms();
|
2010-05-23 15:49:50 +00:00
|
|
|
buffer = NULL;
|
|
|
|
bufpos = 0;
|
2018-08-05 13:42:29 +00:00
|
|
|
delim = params->opt_d ? params->opt_d[0] : '\n';
|
2010-05-23 15:49:50 +00:00
|
|
|
do {
|
|
|
|
char c;
|
2011-05-11 09:49:21 +00:00
|
|
|
int timeout;
|
2010-05-23 15:49:50 +00:00
|
|
|
|
2011-05-11 09:49:21 +00:00
|
|
|
if ((bufpos & 0xff) == 0)
|
2012-09-06 11:28:10 +00:00
|
|
|
buffer = xrealloc(buffer, bufpos + 0x101);
|
2011-05-08 19:23:43 +00:00
|
|
|
|
2011-05-11 09:49:21 +00:00
|
|
|
timeout = -1;
|
2018-08-05 13:42:29 +00:00
|
|
|
if (params->opt_t) {
|
2010-05-23 15:49:50 +00:00
|
|
|
timeout = end_ms - (unsigned)monotonic_ms();
|
2017-07-20 14:09:31 +00:00
|
|
|
/* ^^^^^^^^^^^^^ all values are unsigned,
|
|
|
|
* wrapping math is used here, good even if
|
|
|
|
* 32-bit unix time wrapped (year 2038+).
|
|
|
|
*/
|
2011-05-11 09:49:21 +00:00
|
|
|
if (timeout <= 0) { /* already late? */
|
2010-05-23 15:49:50 +00:00
|
|
|
retval = (const char *)(uintptr_t)1;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-11 09:49:21 +00:00
|
|
|
/* We must poll even if timeout is -1:
|
|
|
|
* we want to be interrupted if signal arrives,
|
|
|
|
* regardless of SA_RESTART-ness of that signal!
|
|
|
|
*/
|
|
|
|
errno = 0;
|
|
|
|
pfd[0].events = POLLIN;
|
2017-07-20 14:09:31 +00:00
|
|
|
if (poll(pfd, 1, timeout) <= 0) {
|
2011-05-11 09:49:21 +00:00
|
|
|
/* timed out, or EINTR */
|
|
|
|
err = errno;
|
|
|
|
retval = (const char *)(uintptr_t)1;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if (read(fd, &buffer[bufpos], 1) != 1) {
|
2011-05-08 19:23:43 +00:00
|
|
|
err = errno;
|
2010-05-23 15:49:50 +00:00
|
|
|
retval = (const char *)(uintptr_t)1;
|
|
|
|
break;
|
|
|
|
}
|
2011-05-11 09:49:21 +00:00
|
|
|
|
2010-05-23 15:49:50 +00:00
|
|
|
c = buffer[bufpos];
|
2017-05-22 17:34:45 +00:00
|
|
|
if (!(read_flags & BUILTIN_READ_RAW)) {
|
|
|
|
if (backslash) {
|
|
|
|
backslash = 0;
|
|
|
|
if (c != '\n')
|
|
|
|
goto put;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '\\') {
|
|
|
|
backslash = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-23 15:49:50 +00:00
|
|
|
}
|
2017-08-09 12:04:07 +00:00
|
|
|
if (c == delim) /* '\n' or -d CHAR */
|
2010-05-23 15:49:50 +00:00
|
|
|
break;
|
2020-06-29 15:57:24 +00:00
|
|
|
if (c == '\0')
|
|
|
|
continue;
|
2010-05-23 15:49:50 +00:00
|
|
|
|
|
|
|
/* $IFS splitting. NOT done if we run "read"
|
|
|
|
* without variable names (bash compat).
|
|
|
|
* Thus, "read" and "read REPLY" are not the same.
|
|
|
|
*/
|
2021-06-05 11:42:06 +00:00
|
|
|
if (argv[0]) {
|
2010-05-23 15:49:50 +00:00
|
|
|
/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
|
|
|
|
const char *is_ifs = strchr(ifs, c);
|
|
|
|
if (startword && is_ifs) {
|
|
|
|
if (isspace(c))
|
|
|
|
continue;
|
|
|
|
/* it is a non-space ifs char */
|
|
|
|
startword--;
|
|
|
|
if (startword == 1) /* first one? */
|
|
|
|
continue; /* yes, it is not next word yet */
|
|
|
|
}
|
|
|
|
startword = 0;
|
|
|
|
if (argv[1] != NULL && is_ifs) {
|
|
|
|
buffer[bufpos] = '\0';
|
|
|
|
bufpos = 0;
|
2018-08-05 13:42:29 +00:00
|
|
|
params->setvar(*argv, buffer);
|
2010-05-23 15:49:50 +00:00
|
|
|
argv++;
|
|
|
|
/* can we skip one non-space ifs char? (2: yes) */
|
|
|
|
startword = isspace(c) ? 2 : 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
put:
|
|
|
|
bufpos++;
|
|
|
|
} while (--nchars);
|
|
|
|
|
|
|
|
if (argv[0]) {
|
|
|
|
/* Remove trailing space $IFS chars */
|
2018-04-11 15:18:34 +00:00
|
|
|
while (--bufpos >= 0
|
|
|
|
&& isspace(buffer[bufpos])
|
|
|
|
&& strchr(ifs, buffer[bufpos]) != NULL
|
|
|
|
) {
|
2010-05-23 15:49:50 +00:00
|
|
|
continue;
|
2018-04-11 15:18:34 +00:00
|
|
|
}
|
2010-05-23 15:49:50 +00:00
|
|
|
buffer[bufpos + 1] = '\0';
|
2018-04-11 15:18:34 +00:00
|
|
|
|
|
|
|
/* Last variable takes the entire remainder with delimiters
|
|
|
|
* (sans trailing whitespace $IFS),
|
|
|
|
* but ***only "if there are fewer vars than fields"(c)***!
|
|
|
|
* The "X:Y:" case below: there are two fields,
|
|
|
|
* and therefore last delimiter (:) is eaten:
|
|
|
|
* IFS=": "
|
|
|
|
* echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:|
|
|
|
|
* echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z|
|
|
|
|
* echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:|
|
|
|
|
* echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y|
|
|
|
|
*/
|
|
|
|
if (bufpos >= 0
|
|
|
|
&& strchr(ifs, buffer[bufpos]) != NULL
|
|
|
|
) {
|
|
|
|
/* There _is_ a non-whitespace IFS char */
|
|
|
|
/* Skip whitespace IFS char before it */
|
|
|
|
while (--bufpos >= 0
|
|
|
|
&& isspace(buffer[bufpos])
|
|
|
|
&& strchr(ifs, buffer[bufpos]) != NULL
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Are there $IFS chars? */
|
|
|
|
if (strcspn(buffer, ifs) >= ++bufpos) {
|
|
|
|
/* No: last var takes one field, not more */
|
|
|
|
/* So, drop trailing IFS delims */
|
|
|
|
buffer[bufpos] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-23 15:49:50 +00:00
|
|
|
/* Use the remainder as a value for the next variable */
|
2018-08-05 13:42:29 +00:00
|
|
|
params->setvar(*argv, buffer);
|
2010-05-23 15:49:50 +00:00
|
|
|
/* Set the rest to "" */
|
|
|
|
while (*++argv)
|
2018-08-05 13:42:29 +00:00
|
|
|
params->setvar(*argv, "");
|
2010-05-23 15:49:50 +00:00
|
|
|
} else {
|
|
|
|
/* Note: no $IFS removal */
|
|
|
|
buffer[bufpos] = '\0';
|
2018-08-05 13:42:29 +00:00
|
|
|
params->setvar("REPLY", buffer);
|
2010-05-23 15:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret:
|
|
|
|
free(buffer);
|
|
|
|
if (read_flags & BUILTIN_READ_SILENT)
|
|
|
|
tcsetattr(fd, TCSANOW, &old_tty);
|
2011-05-08 19:23:43 +00:00
|
|
|
|
|
|
|
errno = err;
|
2010-05-23 15:49:50 +00:00
|
|
|
return retval;
|
2017-07-20 14:09:31 +00:00
|
|
|
#undef fd
|
2010-05-23 15:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ulimit builtin */
|
|
|
|
|
|
|
|
struct limits {
|
|
|
|
uint8_t cmd; /* RLIMIT_xxx fit into it */
|
|
|
|
uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
|
|
|
|
};
|
|
|
|
|
2020-12-17 11:07:54 +00:00
|
|
|
/* Order of entries matches order in which bash prints "ulimit -a" */
|
2020-11-30 12:03:03 +00:00
|
|
|
static const struct limits limits_tbl[] ALIGN2 = {
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_CORE, 9, }, // -c
|
|
|
|
{ RLIMIT_DATA, 10, }, // -d
|
2020-12-17 11:07:54 +00:00
|
|
|
#ifdef RLIMIT_NICE
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_NICE, 0, }, // -e
|
2019-04-28 09:20:09 +00:00
|
|
|
#define LIMIT_F_IDX 3
|
2020-12-17 11:07:54 +00:00
|
|
|
#else
|
|
|
|
/* for example, Hurd */
|
|
|
|
#define LIMIT_F_IDX 2
|
|
|
|
#endif
|
|
|
|
{ RLIMIT_FSIZE, 9, }, // -f
|
2019-04-28 09:25:11 +00:00
|
|
|
#ifdef RLIMIT_SIGPENDING
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_SIGPENDING, 0, }, // -i
|
2019-04-28 09:25:11 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_MEMLOCK
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_MEMLOCK, 10, }, // -l
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_RSS
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_RSS, 10, }, // -m
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_NOFILE
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_NOFILE, 0, }, // -n
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:25:11 +00:00
|
|
|
#ifdef RLIMIT_MSGQUEUE
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_MSGQUEUE, 0, }, // -q
|
2019-04-28 09:25:11 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_RTPRIO
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_RTPRIO, 0, }, // -r
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_STACK
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_STACK, 10, }, // -s
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_CPU
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_CPU, 0, }, // -t
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_NPROC
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_NPROC, 0, }, // -u
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_AS
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_AS, 10, }, // -v
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_LOCKS
|
2019-10-21 14:47:09 +00:00
|
|
|
{ RLIMIT_LOCKS, 0, }, // -x
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
|
|
|
};
|
2020-12-17 11:07:54 +00:00
|
|
|
// 1) bash also shows:
|
2019-04-27 19:23:39 +00:00
|
|
|
//pipe size (512 bytes, -p) 8
|
2020-12-17 11:07:54 +00:00
|
|
|
// 2) RLIMIT_RTTIME ("timeout for RT tasks in us") is not in the table
|
2010-05-23 15:49:50 +00:00
|
|
|
|
2019-10-21 14:47:09 +00:00
|
|
|
static const char limits_help[] ALIGN1 =
|
|
|
|
"core file size (blocks)" // -c
|
|
|
|
"\0""data seg size (kb)" // -d
|
2020-12-17 11:07:54 +00:00
|
|
|
#ifdef RLIMIT_NICE
|
2019-10-21 14:47:09 +00:00
|
|
|
"\0""scheduling priority" // -e
|
2020-12-17 11:07:54 +00:00
|
|
|
#endif
|
2019-10-21 14:47:09 +00:00
|
|
|
"\0""file size (blocks)" // -f
|
|
|
|
#ifdef RLIMIT_SIGPENDING
|
|
|
|
"\0""pending signals" // -i
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_MEMLOCK
|
|
|
|
"\0""max locked memory (kb)" // -l
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_RSS
|
|
|
|
"\0""max memory size (kb)" // -m
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_NOFILE
|
|
|
|
"\0""open files" // -n
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_MSGQUEUE
|
|
|
|
"\0""POSIX message queues (bytes)" // -q
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_RTPRIO
|
|
|
|
"\0""real-time priority" // -r
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_STACK
|
|
|
|
"\0""stack size (kb)" // -s
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_CPU
|
|
|
|
"\0""cpu time (seconds)" // -t
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_NPROC
|
|
|
|
"\0""max user processes" // -u
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
"\0""virtual memory (kb)" // -v
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_LOCKS
|
|
|
|
"\0""file locks" // -x
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
2019-04-27 19:01:35 +00:00
|
|
|
static const char limit_chars[] ALIGN1 =
|
2019-04-28 09:20:09 +00:00
|
|
|
"c"
|
|
|
|
"d"
|
2020-12-17 11:07:54 +00:00
|
|
|
#ifdef RLIMIT_NICE
|
2019-04-28 09:20:09 +00:00
|
|
|
"e"
|
2020-12-17 11:07:54 +00:00
|
|
|
#endif
|
2019-04-27 19:01:35 +00:00
|
|
|
"f"
|
2019-04-28 09:25:11 +00:00
|
|
|
#ifdef RLIMIT_SIGPENDING
|
|
|
|
"i"
|
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_MEMLOCK
|
|
|
|
"l"
|
2019-04-27 19:01:35 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_RSS
|
|
|
|
"m"
|
2019-04-27 19:01:35 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_NOFILE
|
|
|
|
"n"
|
2019-04-27 19:01:35 +00:00
|
|
|
#endif
|
2019-04-28 09:25:11 +00:00
|
|
|
#ifdef RLIMIT_MSGQUEUE
|
|
|
|
"q"
|
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_RTPRIO
|
|
|
|
"r"
|
2019-04-27 19:01:35 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_STACK
|
|
|
|
"s"
|
2019-04-27 19:01:35 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_CPU
|
|
|
|
"t"
|
2019-04-27 19:01:35 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_NPROC
|
2019-04-27 19:23:39 +00:00
|
|
|
"u"
|
2019-04-27 19:01:35 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
"v"
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_LOCKS
|
2019-04-27 19:23:39 +00:00
|
|
|
"x"
|
2019-04-27 19:01:35 +00:00
|
|
|
#endif
|
|
|
|
;
|
2010-05-23 15:49:50 +00:00
|
|
|
|
|
|
|
/* "-": treat args as parameters of option with ASCII code 1 */
|
2016-04-22 16:09:21 +00:00
|
|
|
static const char ulimit_opt_string[] ALIGN1 = "-HSa"
|
2019-04-28 09:20:09 +00:00
|
|
|
"c::"
|
|
|
|
"d::"
|
2020-12-17 11:07:54 +00:00
|
|
|
#ifdef RLIMIT_NICE
|
2019-04-28 09:20:09 +00:00
|
|
|
"e::"
|
2020-12-17 11:07:54 +00:00
|
|
|
#endif
|
2010-05-23 15:49:50 +00:00
|
|
|
"f::"
|
2019-04-28 09:25:11 +00:00
|
|
|
#ifdef RLIMIT_SIGPENDING
|
|
|
|
"i::"
|
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_MEMLOCK
|
|
|
|
"l::"
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_RSS
|
|
|
|
"m::"
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_NOFILE
|
|
|
|
"n::"
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:25:11 +00:00
|
|
|
#ifdef RLIMIT_MSGQUEUE
|
|
|
|
"q::"
|
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_RTPRIO
|
|
|
|
"r::"
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_STACK
|
|
|
|
"s::"
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
#ifdef RLIMIT_CPU
|
|
|
|
"t::"
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_NPROC
|
2019-04-27 19:23:39 +00:00
|
|
|
"u::"
|
2010-05-23 15:49:50 +00:00
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
"v::"
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_LOCKS
|
2019-04-27 19:23:39 +00:00
|
|
|
"x::"
|
2011-04-04 00:12:14 +00:00
|
|
|
#endif
|
2019-04-28 09:20:09 +00:00
|
|
|
;
|
2010-05-23 15:49:50 +00:00
|
|
|
|
2019-04-27 19:01:35 +00:00
|
|
|
enum {
|
|
|
|
OPT_hard = (1 << 0),
|
|
|
|
OPT_soft = (1 << 1),
|
|
|
|
OPT_all = (1 << 2),
|
|
|
|
};
|
|
|
|
|
2010-05-23 15:49:50 +00:00
|
|
|
static void printlim(unsigned opts, const struct rlimit *limit,
|
|
|
|
const struct limits *l)
|
|
|
|
{
|
|
|
|
rlim_t val;
|
|
|
|
|
|
|
|
val = limit->rlim_max;
|
2019-04-27 19:01:35 +00:00
|
|
|
if (opts & OPT_soft)
|
2010-05-23 15:49:50 +00:00
|
|
|
val = limit->rlim_cur;
|
|
|
|
|
|
|
|
if (val == RLIM_INFINITY)
|
2015-10-07 20:42:45 +00:00
|
|
|
puts("unlimited");
|
2010-05-23 15:49:50 +00:00
|
|
|
else {
|
|
|
|
val >>= l->factor_shift;
|
|
|
|
printf("%llu\n", (long long) val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int FAST_FUNC
|
|
|
|
shell_builtin_ulimit(char **argv)
|
|
|
|
{
|
2019-04-27 19:01:35 +00:00
|
|
|
struct rlimit limit;
|
|
|
|
unsigned opt_cnt;
|
2010-05-23 15:49:50 +00:00
|
|
|
unsigned opts;
|
|
|
|
unsigned argc;
|
2019-04-27 19:01:35 +00:00
|
|
|
unsigned i;
|
2010-05-23 15:49:50 +00:00
|
|
|
|
|
|
|
/* We can't use getopt32: need to handle commands like
|
|
|
|
* ulimit 123 -c2 -l 456
|
|
|
|
*/
|
|
|
|
|
2017-08-29 12:32:17 +00:00
|
|
|
/* In case getopt() was already called:
|
|
|
|
* reset libc getopt() internal state.
|
2010-05-23 15:49:50 +00:00
|
|
|
*/
|
2017-04-11 21:58:46 +00:00
|
|
|
GETOPT_RESET();
|
2010-05-23 15:49:50 +00:00
|
|
|
|
2019-04-27 19:01:35 +00:00
|
|
|
// 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.
|
|
|
|
|
2017-07-03 19:31:16 +00:00
|
|
|
argc = string_array_len(argv);
|
2010-05-23 15:49:50 +00:00
|
|
|
|
2019-04-27 19:01:35 +00:00
|
|
|
/* First pass over options: detect -H/-S/-a status,
|
|
|
|
* and "bare ulimit" and "only one option" cases
|
|
|
|
* by counting other opts.
|
|
|
|
*/
|
|
|
|
opt_cnt = 0;
|
2010-05-23 15:49:50 +00:00
|
|
|
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') {
|
2019-04-27 19:01:35 +00:00
|
|
|
opts |= OPT_all;
|
2010-05-23 15:49:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-04-27 19:01:35 +00:00
|
|
|
if (opt_char == '?') {
|
|
|
|
/* bad option. getopt already complained. */
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
opt_cnt++;
|
|
|
|
} /* while (there are options) */
|
2010-05-23 15:49:50 +00:00
|
|
|
|
2019-04-27 19:01:35 +00:00
|
|
|
if (!(opts & (OPT_hard | OPT_soft)))
|
|
|
|
opts |= (OPT_hard | OPT_soft);
|
|
|
|
if (opts & OPT_all) {
|
2019-10-21 14:47:09 +00:00
|
|
|
const char *help = limits_help;
|
2019-04-27 19:01:35 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) {
|
|
|
|
getrlimit(limits_tbl[i].cmd, &limit);
|
2019-10-21 14:47:09 +00:00
|
|
|
printf("%-32s(-%c) ", help, limit_chars[i]);
|
2019-04-27 19:01:35 +00:00
|
|
|
printlim(opts, &limit, &limits_tbl[i]);
|
2019-10-21 14:47:09 +00:00
|
|
|
help += strlen(help) + 1;
|
2019-04-27 19:01:35 +00:00
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2010-05-23 15:49:50 +00:00
|
|
|
|
2019-04-27 19:01:35 +00:00
|
|
|
/* 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)
|
2010-05-23 15:49:50 +00:00
|
|
|
break;
|
2019-04-27 19:01:35 +00:00
|
|
|
if (opt_char == 'H')
|
|
|
|
continue;
|
|
|
|
if (opt_char == 'S')
|
|
|
|
continue;
|
|
|
|
//if (opt_char == 'a') - impossible
|
|
|
|
|
2019-04-28 09:20:09 +00:00
|
|
|
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
|
2019-04-27 19:01:35 +00:00
|
|
|
|
|
|
|
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)
|
2019-10-21 14:47:09 +00:00
|
|
|
printf("%-32s(-%c) ", nth_string(limits_help, i), limit_chars[i]);
|
2019-04-27 19:01:35 +00:00
|
|
|
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) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 09:35:03 +00:00
|
|
|
bb_simple_perror_msg("error setting limit");
|
2019-04-27 19:01:35 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-05-23 15:49:50 +00:00
|
|
|
}
|
|
|
|
} /* while (there are options) */
|
|
|
|
|
2019-04-27 19:01:35 +00:00
|
|
|
if (opt_cnt == 0) {
|
|
|
|
/* "bare ulimit": treat it as if it was -f */
|
2020-12-17 11:07:54 +00:00
|
|
|
getrlimit(RLIMIT_FSIZE, &limit);
|
2019-04-28 09:20:09 +00:00
|
|
|
printlim(opts, &limit, &limits_tbl[LIMIT_F_IDX]);
|
2019-04-27 19:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2010-05-23 15:49:50 +00:00
|
|
|
}
|