From nobody Mon Jun 16 08:54:06 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 4bLP1q1BTBz5Vq8M; Mon, 16 Jun 2025 08:54:07 +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 4bLP1p3XW6z3wlQ; Mon, 16 Jun 2025 08:54:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750064046; 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=8mx5cH5ItL3IUQve1YRj3oAXeGxLcHlgzcefScGukEY=; b=SpUA4edH0efDxAt1mT8JgJlDq638JAl4TLepkPvDb7Haxs2eymjSSbPs8evqL9nVAEujci +w3ktDE5uT6yyr47asVPWf52fiiGQCdnyPttw6IZcb5lPJL89cW8DFXOWT6Y5euHzjj2Fk 91LMwb3YIIUyVs1mC6GFRDK20boSJKl7AzYv/StihL3ZRRSraRFLKO+JgoAQSiRJBbzssr 5G7IKXwsFtBvKy+IGAbWPMNDB8er4+1p9xdZTIsYcXtfLQM6D+dH20C/XshCXEhzjxtbn4 031JZclUAPbt6fWvTQapnO6FbEXH6xVhvJX0V1vLi1TSGRO6MIuGAGr9Tv1vIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1750064046; 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=8mx5cH5ItL3IUQve1YRj3oAXeGxLcHlgzcefScGukEY=; b=sexPJDUkO508V+EEU4zmWLgKbklUUPgmJVTtxHDTu31LsN6cSR1WIVG9cC78uKeYIH3j16 nhkGvONocaAG4FUi6zS5vy/sRbcaa2rjuAyLuOzEQXlEEioRSMn/Zvvy4WDwIu2dhbesp8 xEnjnX92EiYTCD0jGaTm06BQTYiUbTTsO4VYT6m8E67LnTmMwcq8h76x7ibRxxrMvmEXrz lDPparJOm8HXJXdMVlv/NVJFdhYBuS9bRZJgsNslKk2oWhUu1w2GeSpt3kYTEPcwmrAa/q xZFdYZdACuPh7AZaf3nXBjem04zdzZ5lZEUCmrNpsd2ObTsi3eDA0/x/kv3dMw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1750064046; a=rsa-sha256; cv=none; b=f5hmMvpNShqn1+l81/XLpYCPjE0DgOJpKAudBVmtlXRFy7B/miAtb+M1u+aZqs9aBXAlJA 6eVxaZ+PglgwKMY/TXBd998ZS/+eKxWrUJ9+EfMUpMLn+PHnc9Yq6L6s5yiGbtH54sdTD/ VHNpFLAa61NHqc4adKMzEpmzmDWSgCu4RMfcfmsALmgac1GOch2dKd+j1rl1xPJwqw9b25 98xjChutpJj0popbEozyvaXUPTjR4c9gwHkkC8wcSx9HeWz9FghwFYFVfaEJkrePRQZM1T /Uj5Rv4QuPDco08MfJ+eJAc8Sz8smEa45oczcL0pFe6OCpADKsNoH8KCiVC6IQ== 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 4bLP1p2xx8zrFm; Mon, 16 Jun 2025 08:54:06 +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 55G8s66v043386; Mon, 16 Jun 2025 08:54:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 55G8s6d0043383; Mon, 16 Jun 2025 08:54:06 GMT (envelope-from git) Date: Mon, 16 Jun 2025 08:54:06 GMT Message-Id: <202506160854.55G8s6d0043383@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: 1fe93549714d - stable/14 - 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: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 1fe93549714d01e16e76af0d279bd05c5a7e0c7d Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=1fe93549714d01e16e76af0d279bd05c5a7e0c7d commit 1fe93549714d01e16e76af0d279bd05c5a7e0c7d Author: Aaron LI AuthorDate: 2025-04-03 01:07:52 +0000 Commit: Konstantin Belousov CommitDate: 2025-06-16 08:51:53 +0000 timeout(1): Fix the inheritance of signal dispositions (cherry picked from commit 2390cbfe55f55916eca25e8ba94a3320535e01c9) --- 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;