git: 799d67ec407f - stable/13 - daemon: set supervise_enabled during argument processing

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 17 Mar 2023 21:01:42 UTC
The branch stable/13 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=799d67ec407f6d93b00b7d8a364a3284aeb5d07f

commit 799d67ec407f6d93b00b7d8a364a3284aeb5d07f
Author:     Ihor Antonov <ihor@antonovs.family>
AuthorDate: 2023-03-03 05:17:02 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2023-03-17 21:01:03 +0000

    daemon: set supervise_enabled during argument processing
    
    Now when supervsion mode has it's own variable there is really no
    reason to set it separately from the rest of the variables. Move
    initialization of supervise_enabled var to the argument processing
    switch loop, where it belongs.
    
    Reviewed by:    kevans
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/672
    
    (cherry picked from commit f907027b49d93170ed2e92cf0d183cd643b1f70e)
---
 usr.sbin/daemon/daemon.c | 54 +++++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 23 deletions(-)

diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c
index 08ae5c74b8c2..3bbf092b500c 100644
--- a/usr.sbin/daemon/daemon.c
+++ b/usr.sbin/daemon/daemon.c
@@ -179,6 +179,23 @@ main(int argc, char *argv[])
 	sigemptyset(&mask_term);
 	sigemptyset(&mask_orig);
 
+	/*
+	 * Supervision mode is enabled if one of the following options are used:
+	 * --child-pidfile -p
+	 * --supervisor-pidfile -P
+	 * --restart -r / --restart-delay -R
+	 * --syslog -S
+	 * --syslog-facility -l
+	 * --syslog-priority -s
+	 * --syslog-tag -T
+	 *
+	 * In supervision mode daemon executes the command in a forked process
+	 * and observes the child by waiting for SIGCHILD. In supervision mode
+	 * daemon must never exit before the child, this is necessary  to prevent
+	 * orphaning the child and leaving a stale pid file.
+	 * To achieve this daemon catches SIGTERM and
+	 * forwards it to the child, expecting to get SIGCHLD eventually.
+	 */
 	while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
 		switch (ch) {
 		case 'c':
@@ -197,6 +214,7 @@ main(int argc, char *argv[])
 				errx(5, "unrecognized syslog facility");
 			}
 			logparams.syslog_enabled = true;
+			supervision_enabled = true;
 			break;
 		case 'm':
 			stdmask = strtol(optarg, &p, 10);
@@ -206,15 +224,25 @@ main(int argc, char *argv[])
 			break;
 		case 'o':
 			logparams.output_filename = optarg;
+			/*
+			 * TODO: setting output filename doesn't have to turn
+			 * the supervision mode on. For non-supervised mode
+			 * daemon could open the specified file and set it's
+			 * descriptor as both stderr and stout before execve()
+			 */
+			supervision_enabled = true;
 			break;
 		case 'p':
 			child_pidfile = optarg;
+			supervision_enabled = true;
 			break;
 		case 'P':
 			parent_pidfile = optarg;
+			supervision_enabled = true;
 			break;
 		case 'r':
 			restart_enabled = true;
+			supervision_enabled = true;
 			break;
 		case 'R':
 			restart_enabled = true;
@@ -230,9 +258,11 @@ main(int argc, char *argv[])
 				errx(4, "unrecognized syslog priority");
 			}
 			logparams.syslog_enabled = true;
+			supervision_enabled = true;
 			break;
 		case 'S':
 			logparams.syslog_enabled = true;
+			supervision_enabled = true;
 			break;
 		case 't':
 			title = optarg;
@@ -240,6 +270,7 @@ main(int argc, char *argv[])
 		case 'T':
 			logparams.syslog_tag = optarg;
 			logparams.syslog_enabled = true;
+			supervision_enabled = true;
 			break;
 		case 'u':
 			user = optarg;
@@ -286,29 +317,6 @@ main(int argc, char *argv[])
 	/* Write out parent pidfile if needed. */
 	pidfile_write(parent_pidfh);
 
-	/*
-	 * Supervision mode is enabled if one of the following options are used:
-	 * --child-pidfile -p
-	 * --supervisor-pidfile -P
-	 * --restart -r / --restart-delay -R
-	 * --syslog -S
-	 * --syslog-facility -l
-	 * --syslog-priority -s
-	 * --syslog-tag -T
-	 *
-	 * In supervision mode daemon executes the command in a forked process
-	 * and observes the child by waiting for SIGCHILD. In supervision mode
-	 * daemon must never exit before the child, this is necessary  to prevent
-	 * orphaning the child and leaving a stale pid file.
-	 * To achieve this daemon catches SIGTERM and
-	 * forwards it to the child, expecting to get SIGCHLD eventually.
-	 */
-	supervision_enabled = child_pidfile != NULL ||
-		parent_pidfile != NULL ||
-		restart_enabled  == true ||
-		logparams.output_fd != -1 ||
-		logparams.syslog_enabled == true;
-
 	if (supervision_enabled) {
 		struct sigaction act_term = { 0 };
 		struct sigaction act_chld = { 0 };