// We will follow systemd's recommended daemonizing process as much as possible:
# Close all open file descriptors except standard input, output, and error (i.e. the first three file descriptors 0, 1, 2). This ensures that no accidentally passed file descriptor stays around in the daemon process. On Linux, this is best implemented by iterating through /proc/self/fd, with a fallback of iterating from file descriptor 3 to the value returned by getrlimit() for RLIMIT_NOFILE.
// We should have no open file descriptors at this time. Even if we did, I'm not certain how they should be closed from PHP
# Reset all signal handlers to their default. This is best done by iterating through the available signals up to the limit of _NSIG and resetting them to SIG_DFL.
// We have not yet set any signal handlers, so this should be fine
# In the child, call fork() again, to ensure that the daemon can never re-acquire a terminal again. (This relevant if the program — and all its dependencies — does not carefully specify `O_NOCTTY` on each and every single `open()` call that might potentially open a TTY device node.)
switch(@pcntl_fork()){
case-1:
// Unable to fork
thrownewException("forkFailed",['instance'=>2]);
case0:
// We do some things out of order because as far as I know there's no way to reconnect stdin, stdout, and stderr without closing the channel to the parent first
# In the daemon process, write the daemon PID (as returned by getpid()) to a PID file, for example /run/foobar.pid (for a hypothetical daemon "foobar") to ensure that the daemon cannot be started more than once. This must be implemented in race-free fashion so that the PID file is only updated when it is verified at the same time that the PID previously stored in the PID file no longer exists or belongs to a foreign process.
$this->writePID($pidfile);
# In the daemon process, drop privileges, if possible and applicable.
// already done
# From the daemon process, notify the original process started that initialization is complete. This can be implemented via an unnamed pipe or similar communication channel that is created before the first fork() and hence available in both the original and the daemon process.
fwrite($pipe[1],(string)posix_getpid());
fclose($pipe[1]);
// now everything else is done in order, but beyond this point any errors cannot be reported back to the original process
# In the daemon process, connect /dev/null to standard input, output, and error.
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
global$STDIN,$STDOUT,$STDERR;
$STDIN=fopen("/dev/null","r");
$STDOUT=fopen("/dev/null","w");
$STDERR=fopen("/dev/null","w");
# In the daemon process, reset the umask to 0, so that the file modes passed to open(), mkdir() and suchlike directly control the access mode of the created files and directories.
umask(0);
# In the daemon process, change the current directory to the root directory (/), in order to avoid that the daemon involuntarily blocks mount points from being unmounted.
chdir("/");
return;
default:
# Call exit() in the first child, so that only the second child (the actual daemon process) stays around. This ensures that the daemon process is re-parented to init/PID 1, as all daemons should be.
exit;
}
}catch(AbstractException$e){
// transmit the exception back to the original process, which will re-create the exception if necessary
# Call exit() in the original process. The process that invoked the daemon must be able to rely on that this exit() happens after initialization is complete and all external communication channels are established and accessible.