diff options
author | Elliott Hughes <enh@google.com> | 2019-03-09 17:41:49 -0800 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-03-10 23:00:19 -0500 |
commit | 502b10c2ab6bb273bf280ba355fa30869b955d56 (patch) | |
tree | 5f88f0ccaf0e0ed3c7ca75338b680d5e442811e3 /toys | |
parent | ac9eea4afb88ca0785c1ea4c665b1744afc3009a (diff) | |
download | toybox-502b10c2ab6bb273bf280ba355fa30869b955d56.tar.gz |
timeout: --foreground, --preserve-status, and --signal.
--signal is simply a synonym for the exiting -s.
--foreground disables functionality we didn't yet have: putting the
child into a new process group. I've added the functionality and the
flag to disable it.
--preserve-status also makes it clear that our exit statuses didn't match
the coreutils version. In addition to callers that use --preserve-status
to get away from this madness, I also have callers that check for
specific exit values. This patch implements --preserve-status but also
fixes all the other exit statuses.
(The "125" exit value is broken for toybox in the same way that
`toybox grep --whoops ; echo $?` is. To fix this, we'd need some way to
signal that command-line parsing failures should exit with a different
value than the usual 1 --- 2 for grep, 125 for timeout. I've done as much
as grep manages, and left a TODO.)
Also add timeout tests. I couldn't think of an easy test for
--foreground, so I tested that manually with strace.
Also add some newlines to the `toybox --help` output to make it easier
to find the different sections, and expand the section on durations to
call out that fractions are supported as a matter of policy.
As long as timeout and sleep have text describing the duration syntax,
make them the same. (Personally I'd remove both in favor of the `toybox
--help` output, but as long as they're duplicated, keep them consistent.)
Also remove the SLEEP_FLOAT variant --- xparsetime means that sleep no
longer requires floating point to support sub-second resolution.
Diffstat (limited to 'toys')
-rw-r--r-- | toys/other/timeout.c | 38 | ||||
-rw-r--r-- | toys/posix/sleep.c | 14 |
2 files changed, 34 insertions, 18 deletions
diff --git a/toys/other/timeout.c b/toys/other/timeout.c index 4c5bc48d..20722b7e 100644 --- a/toys/other/timeout.c +++ b/toys/other/timeout.c @@ -4,24 +4,26 @@ * * No standard -USE_TIMEOUT(NEWTOY(timeout, "<2^vk:s: ", TOYFLAG_USR|TOYFLAG_BIN)) +USE_TIMEOUT(NEWTOY(timeout, "<2^(foreground)(preserve-status)vk:s(signal):", TOYFLAG_USR|TOYFLAG_BIN)) config TIMEOUT bool "timeout" default y depends on TOYBOX_FLOAT help - usage: timeout [-k LENGTH] [-s SIGNAL] LENGTH COMMAND... + usage: timeout [-k DURATION] [-s SIGNAL] DURATION COMMAND... Run command line as a child process, sending child a signal if the command doesn't exit soon enough. - Length can be a decimal fraction. An optional suffix can be "m" + DURATION can be a decimal fraction. An optional suffix can be "m" (minutes), "h" (hours), "d" (days), or "s" (seconds, the default). -s Send specified signal (default TERM) -k Send KILL signal if child still running this long after first signal -v Verbose + --foreground Don't create new process group + --preserve-status Exit with the child's exit status */ #define FOR_timeout @@ -38,10 +40,11 @@ GLOBALS( static void handler(int i) { - if (toys.optflags & FLAG_v) + if (FLAG(v)) fprintf(stderr, "timeout pid %d signal %d\n", TT.pid, TT.nextsig); + kill(TT.pid, TT.nextsig); - + if (TT.k) { TT.k = 0; TT.nextsig = SIGKILL; @@ -63,6 +66,11 @@ void xparsetimeval(char *s, struct timeval *tv) void timeout_main(void) { + // If timeout fails to parse its arguments, it exits with 125. + // TODO: this and grep both have a bug where built-in error checking like + // "too few arguments" will exit 1 instead of the custom value. + toys.exitval = 125; + // Parse early to get any errors out of the way. xparsetimeval(*toys.optargs, &TT.itv.it_value); if (TT.k) xparsetimeval(TT.k, &TT.ktv); @@ -71,10 +79,24 @@ void timeout_main(void) if (TT.s && -1 == (TT.nextsig = sig_to_num(TT.s))) error_exit("bad -s: '%s'", TT.s); - if (!(TT.pid = XVFORK())) xexec(toys.optargs+1); - else { + if (!FLAG(foreground)) setpgid(0, 0); + + if (!(TT.pid = XVFORK())) { + char **argv = toys.optargs+1; + + execvp(argv[0], argv); + perror_msg("failed to run '%s'", argv[0]); + toys.exitval = (errno == ENOENT) ? 127 : 126; + _xexit(); + } else { + int status; + xsignal(SIGALRM, handler); setitimer(ITIMER_REAL, &TT.itv, (void *)toybuf); - toys.exitval = xwaitpid(TT.pid); + + while (-1 == waitpid(TT.pid, &status, 0) && errno == EINTR); + if (WIFEXITED(status)) toys.exitval = WEXITSTATUS(status); + else if (WTERMSIG(status)==SIGKILL) toys.exitval = 137; + else toys.exitval = FLAG(preserve_status) ? 128+WTERMSIG(status) : 124; } } diff --git a/toys/posix/sleep.c b/toys/posix/sleep.c index 0381c107..846df80c 100644 --- a/toys/posix/sleep.c +++ b/toys/posix/sleep.c @@ -11,18 +11,12 @@ config SLEEP bool "sleep" default y help - usage: sleep LENGTH + usage: sleep DURATION - Wait before exiting. An optional suffix can be "m" (minutes), "h" (hours), - "d" (days), or "s" (seconds, the default). + Wait before exiting. - -config SLEEP_FLOAT - bool - default y - depends on SLEEP && TOYBOX_FLOAT - help - Length can be a decimal fraction. + DURATION can be a decimal fraction. An optional suffix can be "m" + (minutes), "h" (hours), "d" (days), or "s" (seconds, the default). */ #include "toys.h" |