From nobody Wed Apr 16 19:46:36 2025 X-Original-To: dev-commits-src-all@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 4ZdBNs1r2nz5t8Sr; Wed, 16 Apr 2025 19:46:37 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZdBNr2Yq8z3l7C; Wed, 16 Apr 2025 19:46:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744832796; 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=+RicGNWKtEKK9bcmVTvud0mb2fpvII++k2O7Xr2gA3U=; b=vH/AIn/mikvaDttDFHOS5q0DleVVCVcptBKkwqXC/cQQd4/nG5ZFfs0fyuBrMc952arrLd VLTYCqG8ECI6ZHhxdbSPLdzg6hmr/W68msaHl44p/z3KtxMxbUbSLN20oHLXcrwLakkFGH /+cLCmppKwdDjM/5l4aaq9fKZPODeaAjbP3PMnlo8HWOwGUHDBzuKApJnwz/QRFvQIY+5+ AmoBavtN+2ahDVCj2NR1s3Hbc/F0IjfMxZBMk49oaul2SnazE/RFkc7JO4OLXoXHiBvCsD 2PV+zAhjL5lRV+hDMTr43CC+ssEoe8xA8oQLWxsBRkEAADJ9UespAXP3kyzJXg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1744832796; a=rsa-sha256; cv=none; b=EPXnU/wFsXhRgEaKHDNSSaI6KyZybIJPjdCkir4emkp6tMy3SVA2awi+xeOCcVwab9pKXK er/1bJIeEhFYrf5aS9WJZQ4vUUasxeMz9tAw2NIDHHAbJ05MB4JvIV82NGeCkwCH4HL7X3 YaW2YWq+NZk7TvnoiI320A+swaiCcEJjbQB1sSmbnu9xIRGe3pNGdUs4O545Yd1naSursY dDkxmhccjEyEw7WlnymdlwIB2GLbMfu+7vT2XeCY95fMCYfPiOWp9+NWy5FSADtJpN2zZZ aBnI21ELys7TsPjuWoS+wSVl8HA2yPNAt+lORvq4gNCAp/PtLFkjZ8BcvPmtFg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744832796; 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=+RicGNWKtEKK9bcmVTvud0mb2fpvII++k2O7Xr2gA3U=; b=t3vw1sq0360xgnR0ROXTwwmt3iZ26Si09b33IiSXFX3mZvXvoQoCBqLQyWgJODgZ7JCG8B 5h8XIacU5nt1SlqPrOZksiJ0GMhi20fShlrMb2Gn3NiTQQBrApt7ebi1g/Sqkq8fUeFWL9 7h6dLTyNbkA4lofIJjDVuvtFHOkMhgBXwetbUehcPjtIMDPscjJ9R4sPrmzXQzOsn9ZEpx VSUPipT/6eqhuVdyHMBFYxBKztODoco1w7xYzWEM4RyR5w/+f2izMu6Cd1DWlVibZzp2On CowrkbcN8Jl+W9NxscbqxKzVjPXhVL718DQMQeH+UJaNhk3g4fbIVc2/6dL7OQ== 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 4ZdBNr27S1z2Bn; Wed, 16 Apr 2025 19:46:36 +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 53GJkaOj063885; Wed, 16 Apr 2025 19:46:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 53GJkaFN063882; Wed, 16 Apr 2025 19:46:36 GMT (envelope-from git) Date: Wed, 16 Apr 2025 19:46:36 GMT Message-Id: <202504161946.53GJkaFN063882@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: 2390cbfe55f5 - main - timeout(1): Fix the inheritance of signal dispositions List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bapt X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2390cbfe55f55916eca25e8ba94a3320535e01c9 Auto-Submitted: auto-generated The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=2390cbfe55f55916eca25e8ba94a3320535e01c9 commit 2390cbfe55f55916eca25e8ba94a3320535e01c9 Author: Aaron LI AuthorDate: 2025-04-03 01:07:52 +0000 Commit: Baptiste Daroussin CommitDate: 2025-04-16 19:45:38 +0000 timeout(1): Fix the inheritance of signal dispositions POSIX.1-2024 requires that the child process inherit the same signal dispositions as the timeout(1) utility inherited, except for the signal to be sent upon timeout. For example, when timeout(1) is run by nohup(1), the command should be protected from SIGHUP. Obtained-from: DragonFly BSD --- bin/timeout/timeout.1 | 10 +++++++++- bin/timeout/timeout.c | 50 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/bin/timeout/timeout.1 b/bin/timeout/timeout.1 index 14fc19292684..371a167d19f3 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 April 2, 2025 +.Dd April 3, 2025 .Dt TIMEOUT 1 .Os .Sh NAME @@ -65,6 +65,14 @@ Therefore, a signal is never sent if .Ar duration is 0. .Pp +The signal dispositions inherited by the +.Ar command +are the same as the dispositions that +.Nm +inherited, except for the signal that will be sent upon timeout, +which is reset to take the default action and should terminate +the process. +.Pp The options are as follows: .Bl -tag -width indent .It Fl f , Fl -foreground diff --git a/bin/timeout/timeout.c b/bin/timeout/timeout.c index 8a2f0faecd83..1c4cfa6e017d 100644 --- a/bin/timeout/timeout.c +++ b/bin/timeout/timeout.c @@ -224,6 +224,7 @@ main(int argc, char **argv) bool timedout = false; bool do_second_kill = false; bool child_done = false; + sigset_t zeromask, allmask, oldmask; struct sigaction signals; struct procctl_reaper_status info; int signums[] = { @@ -288,6 +289,33 @@ main(int argc, char **argv) err(EXIT_FAILURE, "procctl(PROC_REAP_ACQUIRE)"); } + /* Block all signals to avoid racing against the child. */ + sigfillset(&allmask); + if (sigprocmask(SIG_BLOCK, &allmask, &oldmask) == -1) + err(EXIT_FAILURE, "sigprocmask()"); + + pid = fork(); + if (pid == -1) { + err(EXIT_FAILURE, "fork()"); + } else if (pid == 0) { + /* + * child process + * + * POSIX.1-2024 requires that the child process inherit the + * same signal dispositions as the timeout(1) utility + * inherited, except for the signal to be sent upon timeout. + */ + signal(killsig, SIG_DFL); + if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) + err(EXIT_FAILURE, "sigprocmask(oldmask)"); + + execvp(argv[0], argv); + warn("exec(%s)", argv[0]); + _exit(errno == ENOENT ? EXIT_CMD_NOENT : EXIT_CMD_ERROR); + } + + /* parent continues here */ + memset(&signals, 0, sizeof(signals)); sigemptyset(&signals.sa_mask); @@ -310,29 +338,11 @@ main(int argc, char **argv) signal(SIGTTIN, SIG_IGN); signal(SIGTTOU, SIG_IGN); - pid = fork(); - if (pid == -1) { - err(EXIT_FAILURE, "fork()"); - } else if (pid == 0) { - /* child process */ - signal(SIGTTIN, SIG_DFL); - signal(SIGTTOU, SIG_DFL); - - execvp(argv[0], argv); - warn("exec(%s)", argv[0]); - _exit(errno == ENOENT ? EXIT_CMD_NOENT : EXIT_CMD_ERROR); - } - - /* parent continues here */ - - if (sigprocmask(SIG_BLOCK, &signals.sa_mask, NULL) == -1) - err(EXIT_FAILURE, "sigprocmask()"); - set_interval(first_kill); - sigemptyset(&signals.sa_mask); + sigemptyset(&zeromask); for (;;) { - sigsuspend(&signals.sa_mask); + sigsuspend(&zeromask); if (sig_chld) { sig_chld = 0;