From nobody Sat Jan 04 07:10:45 2025 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4YQBRp43h8z5jrtg; Sat, 04 Jan 2025 07:10:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4YQBRp0n3Xz53nL; Sat, 4 Jan 2025 07:10:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735974646; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Pjyf213eLN6US2HHl0575fKYOXO6C7HDPLDPnz/3mgc=; b=FKiTWGSD+8C9DiWl57Aq1XxGR9YlI/JyQtE3hxXkBtXYb+LETwmUT1VoJ4X3RAjxqJCQ4/ YpmWH3wJ2ov2JY0wloVEU618VWm/DN6aBcBoaVQKOWBMxT58gOol69+QSQunMA9m0vl50E rh0OqRNsUAyEB1n3I5cuMSVJpWFFXFBE4k2EhxGn+uXlduL6BdnbyxWK3FXSEoj+rLZEEA 2K1uzYdJIpTD1ugZBL13urS3igqWbeIIm3e8UuG1Un+hysHUp4VC1m9ivl0OKtZkmPOTwG l1m03Y+UC0h+ND/HerbjZI0tg4NEZ/PJt31bXBuQEdzO8/fGQfsQeCjoAYAWqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1735974646; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Pjyf213eLN6US2HHl0575fKYOXO6C7HDPLDPnz/3mgc=; b=pxZf+CzCUtiIizE6ipF9fw5DGFxcfCNxoVlKdrifQ3fabZcYt89M9rDuuss//FmMOfmUCy tt2fYh4WSRFi8O9t7LxX1ltPqmhQKdkN410EBvNwqCmcWDHJmdAGhEg5tnbNkINfyC+PFa aA/v0KfbUEnl+ie/QX84V+DSfzDN0yboTeG6m4CBrSil3Y+C8Jg2Ok3h8nV0rLnyNoz/2U 3deazTBTwfJcuN4lW+D4kaIm/I3ZfC/6t10wC/SRxVfXKtJjh+nd3dNFYtyrJqcaW5bRYy BZqc85c39zb95s+gKOXpY7JQ4SrrXaWFBRzKWlnhbifEJS1TDvxgBbHGfuhMew== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1735974646; a=rsa-sha256; cv=none; b=oSc5ZDzdF4iHDICCHxYfV5qcr0D9plNF9XJAndPOtaz6Mff9ZbK5A6v29ccuHjjl5OB5u7 LqpRQwaFMOHNcDdkkiGLxKzB3ilDAS6/sAb5rhtOrEutVtbyPvWWW4w4GBgz00bN6xwDLU zuaNnanIuUJfCNLwLc5UP6Z/mAAudp6Nl+/GpuK/7t8XOjvVH/ioSwtHY1qXwq4ewYjmcC m6fPejX/VYO52Cm4foO67b63Nb6EqqutJqHehM5lg/fx5Ojo3bL0+jXGIn/uCBNsrDVP16 RUoZ7iqjgmbqVO1J+bor8IVlK4KLS4302RhPrxSXDcjy53SrA+oa/wHSbMqYIg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4YQBRp0GBbzjjK; Sat, 04 Jan 2025 07:10:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 5047AjjI093678; Sat, 4 Jan 2025 07:10:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 5047AjTP093675; Sat, 4 Jan 2025 07:10:45 GMT (envelope-from git) Date: Sat, 4 Jan 2025 07:10:45 GMT Message-Id: <202501040710.5047AjTP093675@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Gordon Bergling Subject: git: 9e4acacf88f8 - stable/14 - timeout(1): Some minor tweaks and improvements List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: gbe X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 9e4acacf88f8b6f8afe35d661fab9f7e0ebf875c Auto-Submitted: auto-generated The branch stable/14 has been updated by gbe: URL: https://cgit.FreeBSD.org/src/commit/?id=9e4acacf88f8b6f8afe35d661fab9f7e0ebf875c commit 9e4acacf88f8b6f8afe35d661fab9f7e0ebf875c Author: Gordon Bergling AuthorDate: 2024-12-28 07:13:22 +0000 Commit: Gordon Bergling CommitDate: 2025-01-04 07:10:31 +0000 timeout(1): Some minor tweaks and improvements - Define exit status and macros and use them - Improve the second kill logic by setting 'do_second_kill = false' after configuring the second kill - Minor style tweaks - Reorder options in the man page, as well as the usage help - Reorder the exit status in the man page - Enhance the HISTORY section in the man page (obtained from NetBSD) Reviewed by: bapt, Alexander Ziaee (manpages) Approved by: bapt (src) Obtained from: DragonFlyBSD Differential Revision: https://reviews.freebsd.org/D47866 (cherry picked from commit 06690044dac183ea1d93c2ae227e261da3bdca2a) --- bin/timeout/timeout.1 | 63 +++++++++++++++++++++++++++++++-------------------- bin/timeout/timeout.c | 38 ++++++++++++++++++------------- 2 files changed, 60 insertions(+), 41 deletions(-) diff --git a/bin/timeout/timeout.1 b/bin/timeout/timeout.1 index 10e5fd70c393..b8ec3030b271 100644 --- 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 @@ is 0. .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 @@ signal if 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 @@ unit-specifying suffix. 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 @@ If .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 @@ refers to a non-existing program, the exit status returned is 127. .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 @@ The .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 index 7de255f30150..3803aeeca883 100644 --- 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 @@ static void 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 @@ parse_duration(const char *duration) 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 @@ parse_duration(const char *duration) 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 @@ parse_signal(const char *str) if (errstr == NULL) return (sig); + if (strncasecmp(str, "SIG", 3) == 0) str += 3; @@ -114,7 +118,7 @@ parse_signal(const char *str) return (i); } - errx(125, "invalid signal"); + errx(EXIT_INVALID, "invalid signal"); } static void @@ -149,7 +153,7 @@ set_interval(double iv) 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 @@ int 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; @@ -241,11 +245,13 @@ main(int argc, char **argv) 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); @@ -260,9 +266,9 @@ main(int argc, char **argv) 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]); } } @@ -295,7 +301,7 @@ main(int argc, char **argv) break; } else { procctl(P_PID, getpid(), - PROC_REAP_STATUS, &info); + PROC_REAP_STATUS, &info); if (info.rs_children == 0) break; } @@ -314,7 +320,7 @@ main(int argc, char **argv) if (do_second_kill) { set_interval(second_kill); - second_kill = 0; + do_second_kill = false; sig_ign = killsig; killsig = SIGKILL; } else @@ -331,7 +337,7 @@ main(int argc, char **argv) if (do_second_kill) { set_interval(second_kill); - second_kill = 0; + do_second_kill = false; sig_ign = killsig; killsig = SIGKILL; } else