/**
* Daemonize
*
* Several rules or characteristics that most daemons possess:
* 1) Check is daemon already running
* 2) Fork child process
* 3) Sets identity
* 4) Make current process a session laeder
* 5) Write process ID to file
* 6) Change home path
* 7) umask(0)
*
* @access private
* @since 1.0
* @return void
*/
private function _daemonize() {
ob_end_flush();
if ($this->_isDaemonRunning()) {
// Deamon is already running. Exiting
return false;
}
if (!$this->_fork()) {
// Coudn't fork. Exiting.
return false;
}
if (!$this->_setIdentity() && $this->requireSetIdentity) {
// Required identity set failed. Exiting
return false;
}
if (!posix_setsid()) {
$this->_logMessage('Could not make the current process a session leader', self:LOG_ERROR);
return false;
}
if (!$fp = fopen($this->pidFileLocation, 'w')) {
$this->_logMessage('Could not write to PID file', self:LOG_ERROR);
return false;
} else {
fputs($fp, $this->_pid);
fclose($fp);
}
case SIGCHLD: // Halt
$this->_logMessage('Halt signal');
while (pcntl_waitpid(-1, $status, WNOHANG) > 0);
break;
case SIGUSR1: // User-defined
$this->_logMessage('User-defined signal 1');
$this->_sigHandlerUser1();
break;
case SIGUSR2: // User-defined
$this->_logMessage('User-defined signal 2');
$this->_sigHandlerUser2();
break;
}
}