Bug 207248

Summary: [patch] daemon(8): Add option to redirect stdout and stderr to a file
Product: Base System Reporter: Christian Schwarz <me>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed Overcome By Events    
Severity: Affects Many People CC: dch, markj, me
Priority: Normal Keywords: feature, needs-qa, patch
Version: CURRENTFlags: koobs: mfc-stable10?
Hardware: Any   
OS: Any   
Description Flags
Patch for daemon(8) and its man page enabling IO redirection. none

Description Christian Schwarz 2016-02-16 14:22:18 UTC
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.
Comment 1 Dave Cottlehuber freebsd_committer 2016-09-19 16:55:52 UTC
Super idea!

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.
Comment 2 Dave Cottlehuber freebsd_committer 2017-04-22 08:53:16 UTC
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?
Comment 3 Dave Cottlehuber freebsd_committer 2017-04-22 08:56:00 UTC
specifically `-o /var/log/thing.log -m 3`

     -o	output_file
	     Append output from	the daemonized process to output_file.	If the
	     file does not exist, it is	created	with permissions 0600.

     -m	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.	The default is 3.

from https://www.freebsd.org/cgi/man.cgi?query=daemon&apropos=0&sektion=0&manpath=FreeBSD+12-current&arch=default&format=html
Comment 4 Mark Johnston freebsd_committer 2020-11-24 22:28:03 UTC
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.