aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/daemon
diff options
context:
space:
mode:
authorIhor Antonov <ihor@antonovs.family>2023-03-02 03:00:42 +0000
committerKyle Evans <kevans@FreeBSD.org>2023-03-02 03:00:42 +0000
commit203df05b692b5e2b92c990d8971f5797e3a7a139 (patch)
tree9c2e5afbf88f07dae7287e315a18a23e02625f93 /usr.sbin/daemon
parent259ed21d21f81b656f06447716d41c190576477e (diff)
Diffstat (limited to 'usr.sbin/daemon')
-rw-r--r--usr.sbin/daemon/daemon.c35
1 files 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 };