VividCortex/godaemon — pull request #3 by gkristic (2013-09-13 19:43:29+00:00)
Title: Add standard output/error capturing capability

Capturing capability was added for standard output and standard error, upon request. Even though the original Daemonize() function was kept, to avoid changing the API, it's use is discouraged in favor of the new MakeDaemon() (which Daemonize() internally calls). The new function uses a structured type for parameters, that allow for non-disruptive future addition of extra options. This reflects the use of process attributes at os.StartProcess(), for instance.

The old child parameter is now ignored. It's effect was broader than what was stated in the documentation (resetting an environment variable). In fact, it was conceived to avoid further forks AFTER the current Daemonize() call, in case a third process spawned from the child (with os.StartProcess()) wanted to daemonize also. That is, a process spawned from a daemon would avoid the double-fork technique when trying itself to Daemonize(). But, if that very same program is started from a non-Daemonize()-d process, then it would daemonize as usual.

This was a trick we once may have required at VividCortex, but is now useless. And it's also the wrong place to do it, cause it depends on the spawned process having received a copy of the environment, which doesn't need to be the case. So it's better to take that off sooner than later.

Besides the capturing capability, this fix also reserves the first three descriptors, opening /dev/null unless there's a better choice (i.e., unless capturing is required for the output and error streams). This prevents the daemon from opening another file, socket, or whatever in FDs that are ultimately used for os.Stdin, os.Stdout and os.Stderr. Otherwise, data written to the outputs can give unexpected results if other resource shares the FD.

Closes #2.