From nobody Mon Jun 16 08:54:08 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 4bLP1s1VpDz5VppT; Mon, 16 Jun 2025 08:54:09 +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 4bLP1r5K3pz3wZb; Mon, 16 Jun 2025 08:54:08 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750064048; 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=Zw1JZksf3Ov17OtrZbbkoppFRxibyMksUk33SvckVOE=; b=dJsAnm9n25L23uNv2rOM/pypsxLgj+mcbdIqFzZW+GUiXJ2vsxpe96NQFQaYCFz0SccCbO ShdJ5/kFPHbuPeIxJ0GzORGGNYwEyk63zmik4WG1ORn4sUKaziHdejKXhVNyRR/cAaHuIH sPBvyvqFFOD0NUElxEDEIsg28xDhCQdOBOtGQwE59/RNSUavi9nGDtSBrmpVdSkpARf9jC tuWo7RO3KASQvXpA9FtT3JsRQPFDOmG0VWu3gtYM+zSYA1Jr9Scbydjr5WLmxmH0kEyLo4 tF78qOqZG++vSITYomT1Q2YZ9L0TC6VqUnGGZP9FaikXd5R+oBn7j+vIKvr6fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750064048; 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=Zw1JZksf3Ov17OtrZbbkoppFRxibyMksUk33SvckVOE=; b=X0QnkDGZsMO3t5a8TO2NeqjHIxtfbZidJG4UCRP0N/TpH7jemH235mcz3EtWBMnTkXc98B iaqHpUs72MWcRFm351o284E4r5gyroAeBKcStH3Ced7cl5kADAlUKpUTgKD9YH7YYyeMVu zgk8tAPVHrMiQ6Qnyq2GSBO7BYd/GB6tO+DWGSc9UndS8962SeL2OZTz9NbQq6ernade45 4+7i6viJSfAHFNmoOD4lhtGwlvVUKD9z+spfPHS345q+JdTDi53kSzVyCuuOtxYIyl/1+W G0rRvQwHdOFe7r64Ws021j/lflailB9IAVVl/zpa0oq6eChrehFSV5pjjWiCNQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1750064048; a=rsa-sha256; cv=none; b=kq/rRb5Ih/OojOi71WWU8rPCXXwjwzmgP5qeXmVD77Et+a4LQex2X0YANADB8bLxrlxNlw Qu9EsF3A9CvqKp0fJf15CRBpRbKkA+rB21GqgP84qGymqt/cGMv3AL9+wVXkk86beg5RyQ i9xxK15/R/l19d63S3TNoxT/evFBR75AtJnC2H69utySrpaAOfHBGgS3nKPb/sN+erPf7a N4utSuabo2pq5UrR7GYzAw8s7t2WmopahHORnujQmDgh9qxnAYfakk4xIlM7LAvyRHTq+n N9AXu/MwhNtnB6AYY61wsWtilhQWR7myiEEzxhQUbMN0KFlttHS2zQ0xNt/N4w== 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 4bLP1r4c5Qzrvf; Mon, 16 Jun 2025 08:54:08 +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 55G8s8AR043452; Mon, 16 Jun 2025 08:54:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 55G8s89e043449; Mon, 16 Jun 2025 08:54:08 GMT (envelope-from git) Date: Mon, 16 Jun 2025 08:54:08 GMT Message-Id: <202506160854.55G8s89e043449@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: de24ba56a663 - stable/14 - timeout(1): Kill self with the same signal that terminated the child 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: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: de24ba56a663c608edf8091900a49b48d088896c Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=de24ba56a663c608edf8091900a49b48d088896c commit de24ba56a663c608edf8091900a49b48d088896c Author: Aaron LI AuthorDate: 2025-04-03 02:51:06 +0000 Commit: Konstantin Belousov CommitDate: 2025-06-16 08:51:53 +0000 timeout(1): Kill self with the same signal that terminated the child (cherry picked from commit 613310263ae1f0f65df498dc8e98f460b781946d) --- bin/timeout/timeout.1 | 51 ++++++++++++++++++++++++--------------------------- bin/timeout/timeout.c | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/bin/timeout/timeout.1 b/bin/timeout/timeout.1 index 44525daaec59..43a484dac76f 100644 --- a/bin/timeout/timeout.1 +++ b/bin/timeout/timeout.1 @@ -146,37 +146,33 @@ hours days .El .Sh EXIT STATUS -If the timeout was not reached, the exit status of -.Ar command -is returned. -.Pp -If the timeout was reached and -.Fl -preserve-status -is set, the exit status of -.Ar command -is returned. -If +If the time limit was reached and the .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 +option is not specified, the exit status is 124. +Otherwise, +.Nm +exits with the same exit status as the +.Ar command . +For example, +.Nm +will terminate itself with the same signal if the .Ar command -is an otherwise invalid program, the exit status returned is 126. +is terminated by a signal. .Pp -If +If an error occurred, the following exit values are returned: +.Bl -tag -offset indent with indent -compact +.It 125 +An error other than the two described below occurred. +For example, an invalid duration or signal was specified. +.It 126 +The .Ar command -refers to a non-existing program, the exit status returned is 127. -.Pp -If +was found but could not be executed. +.It 127 +The .Ar command -exits after receiving a signal, the exit status returned is the signal number -plus 128. +could not be found. +.El .Sh EXAMPLES Run .Xr sleep 1 @@ -202,7 +198,8 @@ $ echo $? .Pp Same as above but preserving status. The exit status is 128 + signal number (15 for -.Dv SIGTERM ) : +.Dv SIGTERM ) +for most shells: .Bd -literal -offset indent $ timeout --preserve-status 2 sleep 4 $ echo $? diff --git a/bin/timeout/timeout.c b/bin/timeout/timeout.c index 6e93e9e2911c..e9c4e22fc7d3 100644 --- a/bin/timeout/timeout.c +++ b/bin/timeout/timeout.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -239,6 +240,34 @@ set_interval(double iv) err(EXIT_FAILURE, "setitimer()"); } +/* + * In order to avoid any possible ambiguity that a shell may not set '$?' to + * '128+signal_number', POSIX.1-2024 requires that timeout mimic the wait + * status of the child process by terminating itself with the same signal, + * while disabling core generation. + */ +static void __dead2 +kill_self(int signo) +{ + sigset_t mask; + struct rlimit rl; + + /* Reset the signal disposition and make sure it's unblocked. */ + signal(signo, SIG_DFL); + sigfillset(&mask); + sigdelset(&mask, signo); + sigprocmask(SIG_SETMASK, &mask, NULL); + + /* Disable core generation. */ + memset(&rl, 0, sizeof(rl)); + setrlimit(RLIMIT_CORE, &rl); + + logv("killing self with signal %s(%d)", sys_signame[signo], signo); + kill(getpid(), signo); + err(128 + signo, "signal %s(%d) failed to kill self", + sys_signame[signo], signo); +} + int main(int argc, char **argv) { @@ -430,10 +459,12 @@ main(int argc, char **argv) if (timedout && !preserve) { pstat = EXIT_TIMEOUT; } else { + if (WIFSIGNALED(pstat)) + kill_self(WTERMSIG(pstat)); + /* NOTREACHED */ + if (WIFEXITED(pstat)) pstat = WEXITSTATUS(pstat); - else if (WIFSIGNALED(pstat)) - pstat = 128 + WTERMSIG(pstat); } return (pstat);