Created attachment 167080 [details]
Patch for daemon(8) and its man page enabling IO redirection.
Some applications that are daemonized using daemon(8) do not provide means to write log files. stdout / stderr is their only way to provide output.
While this behavior is debatable, we already built a tool that deals with applications that do not daemonize themselves, daemon(8).
Currently, daemon(8) only has a flag `-f` to redirect both stdout and stderr to /dev/null.
Setting the flag causes the daemon(3) call to be called with daemon(*, 0).
However, daemon(3) does not provide means to specify alternative redirection destinations.
The patch attached to this bug report adds flags `-e`, `-o` and `-a` to daemon(8) that can be used instead of `-f` to redirect stderr and stdout to specified file paths.
`-a` is used to toggle appending instead of overwriting the specified path.
The man page has been updated to reflect the changes made to the source code.
The flags have been chosen semantically and syntactically identical to `http://software.clapper.org/daemonize/` which is a tool available both in many Linux distributions and in the ports tree.
However, I would like to see the features implemented in this patch in the base system. (Naturally, ) I think the code provided by this patch is much more straight forward and cleaner than the aforementioned third party tool.
I've come across a couple of daemons just this week that fall into this category. Both are golang ports "designed" to run in docker/systemd land where stdio is handled by some parent container, e.g. security/vault being one of them.
I can't tell if this is already the case, but it would be ideal for the redirected io to go into a file owned by whatever `-u $USER` was passed into daemon, instead of root, good for managing disk quotas and smaller systems where out of control log files have caused systems to stop due to disk full issues.
I can't test until 110.0 has landed but I'd also want to know if log rotation plays nicely with this or not.
worth mentioning https://svnweb.freebsd.org/base?view=revision&revision=307769 now in 12.0-CURRENT which looks like it provides the same functionality. Maybe this can be closed?
specifically `-o /var/log/thing.log -m 3`
Append output from the daemonized process to output_file. If the
file does not exist, it is created with permissions 0600.
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. The default is 3.
Indeed, daemon(8) can now redirect output to a file. This is not quite the same as the proposed functionality but is hopefully sufficient. Please re-open if not.