diff --git a/bin/timeout/timeout.1 b/bin/timeout/timeout.1 --- a/bin/timeout/timeout.1 +++ b/bin/timeout/timeout.1 @@ -24,7 +24,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd June 17, 2024 +.Dd December 28, 2024 .Dt TIMEOUT 1 .Os .Sh NAME @@ -32,10 +32,11 @@ .Nd run a command with a time limit .Sh SYNOPSIS .Nm -.Op Fl -signal Ar sig | Fl s Ar sig -.Op Fl -preserve-status +.Op Fl k Ar time | Fl -kill-after Ar time +.Op Fl s Ar sig | Fl -signal Ar sig .Op Fl -kill-after Ar time | Fl k Ar time .Op Fl -foreground +.Op Fl -preserve-status .Ar duration .Ar command .Op Ar args ... @@ -62,18 +63,6 @@ .Pp The options are as follows: .Bl -tag -width indent -.It Fl -preserve-status -Exit with the same status as -.Ar command , -even if it times out and is killed. -.It Fl -foreground -Do not propagate timeout to the children of -.Ar command . -.It Fl s Ar sig , Fl -signal Ar sig -Specify the signal to send on timeout. -By default, -.Dv SIGTERM -is sent. .It Fl k Ar time , Fl -kill-after Ar time Send a .Dv SIGKILL @@ -82,8 +71,21 @@ is still running after .Ar time after the first signal was sent. +.It Fl s Ar sig , Fl -signal Ar sig +Specify the signal to send on timeout. +By default, +.Dv SIGTERM +is sent. +.It Fl -foreground +Do not propagate timeout to the children of +.Ar command . +.It Fl -preserve-status +Exit with the same status as +.Ar command , +even if it times out and is killed. .El .Sh DURATION FORMAT +The .Ar duration and .Ar time @@ -92,7 +94,7 @@ Values without an explicit unit are interpreted as seconds. .Pp Supported unit symbols are: -.Bl -tag -width indent -compact +.Bl -tag -offset indent -width indent -compact .It Cm s seconds .It Cm m @@ -116,10 +118,15 @@ .Fl -preserve-status is not set, an exit status of 124 is returned. .Pp +If an invalid parameter is passed to +.Fl s +or +.Fl k , +the exit status returned is 125. +.Pp If .Ar command -exits after receiving a signal, the exit status returned is the signal number -plus 128. +is an otherwise invalid program, the exit status returned is 126. .Pp If .Ar command @@ -127,13 +134,8 @@ .Pp If .Ar command -is an otherwise invalid program, the exit status returned is 126. -.Pp -If an invalid parameter is passed to -.Fl s -or -.Fl k , -the exit status returned is 125. +exits after receiving a signal, the exit status returned is the signal number +plus 128. .Sh EXAMPLES Run .Xr sleep 1 @@ -206,6 +208,17 @@ .Nm command first appeared in .Fx 10.3 . +.Pp +The +.Fx +work is compatible with GNU +.Nm +by +.An Padraig Brady , +from GNU Coreutils 8.21. +The +.Nm +utility first appeared in GNU Coreutils 7.0. .Sh AUTHORS .An Baptiste Daroussin Aq Mt bapt@FreeBSD.org and diff --git a/bin/timeout/timeout.c b/bin/timeout/timeout.c --- a/bin/timeout/timeout.c +++ b/bin/timeout/timeout.c @@ -41,6 +41,9 @@ #include #define EXIT_TIMEOUT 124 +#define EXIT_INVALID 125 +#define EXIT_CMD_ERROR 126 +#define EXIT_CMD_NOENT 127 static sig_atomic_t sig_chld = 0; static sig_atomic_t sig_term = 0; @@ -51,9 +54,9 @@ usage(void) { - fprintf(stderr, "Usage: %s [--signal sig | -s sig] [--preserve-status]" - " [--kill-after time | -k time] [--foreground] " - " \n", getprogname()); + fprintf(stderr, "Usage: %s [-k time | --kill-after time]" + " [-s sig | --signal sig] [--foreground] [--preserve-status]" + " \n", getprogname()); exit(EXIT_FAILURE); } @@ -66,13 +69,13 @@ ret = strtod(duration, &end); if (ret == 0 && end == duration) - errx(125, "invalid duration"); + errx(EXIT_INVALID, "invalid duration"); if (end == NULL || *end == '\0') return (ret); if (end != NULL && *(end + 1) != '\0') - errx(125, "invalid duration"); + errx(EXIT_INVALID, "invalid duration"); switch (*end) { case 's': @@ -87,11 +90,11 @@ ret *= 60 * 60 * 24; break; default: - errx(125, "invalid duration"); + errx(EXIT_INVALID, "invalid duration"); } if (ret < 0 || ret >= 100000000UL) - errx(125, "invalid duration"); + errx(EXIT_INVALID, "invalid duration"); return (ret); } @@ -106,6 +109,7 @@ if (errstr == NULL) return (sig); + if (strncasecmp(str, "SIG", 3) == 0) str += 3; @@ -114,7 +118,7 @@ return (i); } - errx(125, "invalid signal"); + errx(EXIT_INVALID, "invalid signal"); } static void @@ -149,7 +153,7 @@ memset(&tim, 0, sizeof(tim)); tim.it_value.tv_sec = (time_t)iv; - iv -= (time_t)iv; + iv -= (double)tim.it_value.tv_sec; tim.it_value.tv_usec = (suseconds_t)(iv * 1000000UL); if (setitimer(ITIMER_REAL, &tim, NULL) == -1) @@ -160,10 +164,10 @@ main(int argc, char **argv) { int ch; - unsigned long i; int foreground, preserve; int error, pstat, status; int killsig = SIGTERM; + size_t i; pid_t pid, cpid; double first_kill; double second_kill; @@ -240,11 +244,13 @@ signals.sa_handler = sig_handler; signals.sa_flags = SA_RESTART; - for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i++) + for (i = 0; i < sizeof(signums) / sizeof(signums[0]); i++) { if (signums[i] != -1 && signums[i] != 0 && sigaction(signums[i], &signals, NULL) == -1) err(EXIT_FAILURE, "sigaction()"); + } + /* Don't stop if background child needs TTY */ signal(SIGTTIN, SIG_IGN); signal(SIGTTOU, SIG_IGN); @@ -259,9 +265,9 @@ error = execvp(argv[0], argv); if (error == -1) { if (errno == ENOENT) - err(127, "exec(%s)", argv[0]); + err(EXIT_CMD_NOENT, "exec(%s)", argv[0]); else - err(126, "exec(%s)", argv[0]); + err(EXIT_CMD_ERROR, "exec(%s)", argv[0]); } } @@ -294,7 +300,7 @@ break; } else { procctl(P_PID, getpid(), - PROC_REAP_STATUS, &info); + PROC_REAP_STATUS, &info); if (info.rs_children == 0) break; } @@ -313,7 +319,7 @@ if (do_second_kill) { set_interval(second_kill); - second_kill = 0; + do_second_kill = false; sig_ign = killsig; killsig = SIGKILL; } else @@ -330,7 +336,7 @@ if (do_second_kill) { set_interval(second_kill); - second_kill = 0; + do_second_kill = false; sig_ign = killsig; killsig = SIGKILL; } else