The branch main has been updated by kevans:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=203df05b692b5e2b92c990d8971f5797e3a7a139

commit 203df05b692b5e2b92c990d8971f5797e3a7a139
Author:     Ihor Antonov <[email protected]>
AuthorDate: 2023-03-02 03:00:42 +0000
Commit:     Kyle Evans <[email protected]>
CommitDate: 2023-03-02 03:00:42 +0000

    daemon: add supervision_enabled var
    
    explicitly name a bunch of boolean checks that enable
    supervison mode and improve comments
    
    Reviewed by:    kevans
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/669
---
 usr.sbin/daemon/daemon.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c
index 0925d16a27a0..f15a3f567a8c 100644
--- a/usr.sbin/daemon/daemon.c
+++ b/usr.sbin/daemon/daemon.c
@@ -47,6 +47,7 @@ __FBSDID("$FreeBSD$");
 #include <pwd.h>
 #include <signal.h>
 #include <stdio.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
@@ -141,6 +142,7 @@ usage(int exitcode)
 int
 main(int argc, char *argv[])
 {
+       bool supervision_enabled = false;
        char *p = NULL;
        const char *pidfile = NULL;
        const char *logtag = "daemon";
@@ -275,18 +277,31 @@ main(int argc, char *argv[])
        }
        /* Write out parent pidfile if needed. */
        pidfile_write(ppfh);
+
        /*
-        * If the pidfile or restart option is specified the daemon
-        * executes the command in a forked process and wait on child
-        * exit to remove the pidfile or restart the command. Normally
-        * we don't want the monitoring daemon to be terminated
-        * leaving the running process and the stale pidfile, so we
-        * catch SIGTERM and forward it to the children expecting to
-        * get SIGCHLD eventually. We also must fork() to obtain a
-        * readable pipe with the child for writing to a log file
-        * and syslog.
+        * 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.
         */
-       if (pidfile || ppidfile || restart || outfd != -1 || dosyslog) {
+       supervision_enabled = pidfile  != NULL ||
+               ppidfile != NULL ||
+               restart  != 0    ||
+               outfd    != -1   ||
+               dosyslog != 0;
+
+       if (supervision_enabled) {
                struct sigaction act_term = { 0 };
                struct sigaction act_chld = { 0 };
                struct sigaction act_hup = { 0 };

Reply via email to