From nobody Fri Mar 17 21:01:36 2023 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 4Pdc4c6T8Xz3yqpB; Fri, 17 Mar 2023 21:01:36 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Pdc4c5RlZz3q7d; Fri, 17 Mar 2023 21:01:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679086896; 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=iqtmyKVnvTQ2nqh12OCTAcNAKCHqg3X9xDb76vVi5CE=; b=MFFQqNmd4t+7nG79RXre94J+Z0kdQN5qjIhm3JXazJ7xGbjNHdt/EZPdAEImwe0UIJVtEt BSUbMuxYlpSFkCLh8RAPfvFjbiCIYCSswo6elv/hc62c0Nj21421KOatjq5M9gvrv2+4cZ yLiPxJwf8rm+iNKBQQZz/1CC3c8kVYLHsq7nnN1qEDofNRyxonI8FFFJ9LgupTq75XdbvJ /YaT0I+sGJ7OS4RkH1lVMTw81uEwy1iPwTcebdDwMiyHUCEMkbO/YfB0hnXxk4UivMnIlc TgtBlAIbplpXNlfxa3CEjI3WkbBtjf1N+IKknFga3vGDEcWXaOAeGaiEQPTJqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1679086896; 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=iqtmyKVnvTQ2nqh12OCTAcNAKCHqg3X9xDb76vVi5CE=; b=OdEdVrG3GyFSTAsjAxM/PE3X0FHFP6gr2LmVQJ0M/3AWWrckvmpqpg6mMl61Oh+2k+oxRA D0i18YjrodLxIH1bOp/03mcbUO+7NFSuqpOPphJ1uMaQ9hCyG3U/0we2X1qEDGCOH8YERu 5ylkUsL+x+cOZTO4NCr5eJqybsm258RHtBi2iEXCGI7njK3xLRtGcF9iEWvUtga/AlKh9M kPSmrakdSXH2ExznrzWDZNzUV3tQDalt8FyYlZaD4P3aMsdDWzaS9aSB1R6gtUvHvNv9FO 0GyVM5oinIc4HfeXLtP6KOJ3Xla9LHaDZMJHSaMy12MlfhYi2/KG3Zm2G5kRKA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1679086896; a=rsa-sha256; cv=none; b=VZFeexlX2uNbwHEt3CG2tGrhbXfnAsJp0HjaAFA+uPFvn1sf/h16BuaqDzr21JTYwl4x/c 711DqcdY7jO9CbHIqGXvp/aTd7CgQYp+N0kwDu+CU14k6k9mrOMH+MlYGrMStzI2KnMPjI GYnyhYqiBRB/b6nA+6l0ruhq6nF4Ozghl2ApwPa7ywlb2gV/ciB0FL2fnmr8ikoiCz6lAG vjhk3rqWJWbRIVKPshYufi1gDtuUkg1V+e6Zs2R6DL/TfnlYHAUgCseJN5FM1WZvKIu+CN c9Wh2DleACCuro/8faVdpj4sQhCoWeX/csCW7O6EdIChAW3r50yQswEG6LeloA== 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 4Pdc4c4SCszgcs; Fri, 17 Mar 2023 21:01:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 32HL1aNi015205; Fri, 17 Mar 2023 21:01:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 32HL1aNU015204; Fri, 17 Mar 2023 21:01:36 GMT (envelope-from git) Date: Fri, 17 Mar 2023 21:01:36 GMT Message-Id: <202303172101.32HL1aNU015204@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 204306b0532a - stable/13 - daemon: flatten and simplify fork() logic 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: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 204306b0532a9c9d2fdf07b88e33ef71a921cc29 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=204306b0532a9c9d2fdf07b88e33ef71a921cc29 commit 204306b0532a9c9d2fdf07b88e33ef71a921cc29 Author: Ihor Antonov AuthorDate: 2023-03-03 05:17:02 +0000 Commit: Kyle Evans CommitDate: 2023-03-17 21:01:02 +0000 daemon: flatten and simplify fork() logic Reviewed by: kevans Pull Request: https://github.com/freebsd/freebsd-src/pull/672 (cherry picked from commit 75f61ca92098941f73020f46674f0c40db7270fb) --- usr.sbin/daemon/daemon.c | 53 ++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index 16e22efed68e..964a77deb0f9 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -84,7 +84,7 @@ static void daemon_sleep(time_t, long); static volatile sig_atomic_t terminate = 0; static volatile sig_atomic_t child_gone = 0; -static volatile sig_atomic_t pid = -1; +static volatile sig_atomic_t pid = 0; static volatile sig_atomic_t do_log_reopen = 0; static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h"; @@ -368,34 +368,27 @@ restart: */ child_gone = 0; pid = fork(); - if (pid == -1) { - warn("fork"); - goto exit; - } else if (pid > 0) { - /* - * Unblock SIGTERM after we know we have a valid - * child PID to signal. - */ - if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) { - warn("sigprocmask"); - goto exit; - } - close(pfd[1]); - pfd[1] = -1; - } } - if (pid <= 0) { - /* Now that we are the child, write out the pid. */ + + /* fork failed, this can only happen when supervision is enabled */ + if (pid == -1) { + warn("fork"); + goto exit; + } + + + /* fork succeeded, this is child's branch or supervision is disabled */ + if (pid == 0) { pidfile_write(child_pidfh); if (user != NULL) { restrict_process(user); } /* - * When forking, the child gets the original sigmask, + * In supervision mode, the child gets the original sigmask, * and dup'd pipes. */ - if (pid == 0) { + if (supervision_enabled) { close(pfd[0]); if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) { err(1, "sigprogmask"); @@ -416,12 +409,24 @@ restart: } } execvp(argv[0], argv); - /* - * execvp() failed -- report the error. The child is - * now running, so the exit status doesn't matter. - */ + /* execvp() failed - report error and exit this process */ err(1, "%s", argv[0]); } + + /* + * else: pid > 0 + * fork succeeded, this is the parent branch, this can only happen when + * supervision is enabled + * + * Unblock SIGTERM after we know we have a valid child PID to signal. + */ + if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) { + warn("sigprocmask"); + goto exit; + } + close(pfd[1]); + pfd[1] = -1; + setproctitle("%s[%d]", title, (int)pid); /* * As we have closed the write end of pipe for parent process,