Author: sobomax
Date: Thu Sep 24 02:44:58 2020
New Revision: 366098
URL: https://svnweb.freebsd.org/changeset/base/366098
Log:
dd a new option (-H) to daemon(8) to catch SIGHUP and re-open output_file
file when
received.
The default system log rotation mechanism (newsyslog(8)) requires ability to
send
signal to a daemon in order to properly complete rotation of the logs in an
"atomic"
manner without having to making a copy and truncating original file.
Unfortunately
our built-in mechanism to convert "dumb" programs into daemons has no way to
handle
this rotation properly. This change adds this ability, to be enabled by
supplying -H
option in addition to the -o option.
Reviewed by: markj, rpokala (manpages)
MFC after:2 weeks
Differential Revision:https://reviews.freebsd.org/D26526
Modified:
head/usr.sbin/daemon/daemon.8
head/usr.sbin/daemon/daemon.c
Modified: head/usr.sbin/daemon/daemon.8
==
--- head/usr.sbin/daemon/daemon.8 Wed Sep 23 23:56:49 2020
(r366097)
+++ head/usr.sbin/daemon/daemon.8 Thu Sep 24 02:44:58 2020
(r366098)
@@ -26,7 +26,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd October 2, 2019
+.Dd September 22, 2020
.Dt DAEMON 8
.Os
.Sh NAME
@@ -34,7 +34,7 @@
.Nd run detached from the controlling terminal
.Sh SYNOPSIS
.Nm
-.Op Fl cfrS
+.Op Fl cfHrS
.Op Fl p Ar child_pidfile
.Op Fl P Ar supervisor_pidfile
.Op Fl t Ar title
@@ -68,6 +68,14 @@ or syslog output, the standard file descriptors are fi
.Pa /dev/null ,
then stdout and/or stderr is redirected to a file or to syslog as
specified by the other options.
+.It Fl H
+Close
+.Pa output_file
+and re-open it when signal SIGHUP is received, for interoperability with
+.Xr newsyslog 1
+and similar log rotation / archival mechanisms. If
+.Fa o
+is not specified, this flag is ignored.
.It Fl S
Enable syslog output.
This is implicitly applied if other syslog parameters are provided.
@@ -77,6 +85,12 @@ tag, respectively.
Append output from the daemonized process to
.Pa output_file .
If the file does not exist, it is created with permissions 0600.
+When this option is used together with options
+.Fl c and
+.Fl H
+the absolute path needs to be provided to ensure
+.Nm
+can re-open the file after a SIGHUP.
.It Fl m Ar output_mask
Redirect output from the child process stdout (1), stderr (2), or both (3).
This value specifies what is sent to syslog and the log file.
Modified: head/usr.sbin/daemon/daemon.c
==
--- head/usr.sbin/daemon/daemon.c Wed Sep 23 23:56:49 2020
(r366097)
+++ head/usr.sbin/daemon/daemon.c Thu Sep 24 02:44:58 2020
(r366098)
@@ -61,11 +61,15 @@ struct log_params {
int logpri;
int noclose;
int outfd;
+ const char *outfn;
};
static void restrict_process(const char *);
static void handle_term(int);
static void handle_chld(int);
+static void handle_hup(int);
+static int open_log(const char *);
+static void reopen_log(struct log_params *);
static int listen_child(int, struct log_params *);
static int get_log_mapping(const char *, const CODE *);
static void open_pid_files(const char *, const char *, struct pidfh **,
@@ -74,7 +78,8 @@ static void do_output(const unsigned char *, size_t, s
static void daemon_sleep(time_t, long);
static void usage(void);
-static volatile sig_atomic_t terminate = 0, child_gone = 0, pid = 0;
+static volatile sig_atomic_t terminate = 0, child_gone = 0, pid = 0,
+ do_log_reopen = 0;
int
main(int argc, char *argv[])
@@ -84,7 +89,7 @@ main(int argc, char *argv[])
sigset_t mask_susp, mask_orig, mask_read, mask_term;
struct log_params logpar;
int pfd[2] = { -1, -1 }, outfd = -1;
- int stdmask, logpri, logfac;
+ int stdmask, logpri, logfac, log_reopen;
struct pidfh *ppfh, *pfh;
char *p;
@@ -97,9 +102,10 @@ main(int argc, char *argv[])
logtag = "daemon";
restart = 0;
dosyslog = 0;
+ log_reopen = 0;
outfn = NULL;
title = NULL;
- while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:R:T:")) != -1) {
+ while ((ch = getopt(argc, argv, "cfHSp:P:ru:o:s:l:t:l:m:R:T:")) != -1) {
switch (ch) {
case 'c':
nochdir = 0;
@@ -107,6 +113,9 @@ main(int argc, char *argv[])
case 'f':
noclose = 0;
break;
+ case 'H':
+ log_reopen = 1;
+ break;
case 'l':
logfac = get_log_mapping(optarg, facilitynames);
if (logfac == -1)
@@ -168,7 +177,7 @@ main(int argc, char *argv[])
title = argv[0];
if (outfn) {
- outfd = open(outfn, O_CREAT | O_WRONLY |