/** * Actions on early startup. * @return void */ public static function init() { Daemon::initSettings(); FS::init(); Daemon::$runName = basename($_SERVER['argv'][0]); $error = FALSE; $argv = $_SERVER['argv']; $runmode = isset($argv[1]) ? str_replace('-', '', $argv[1]) : ''; $args = Daemon_Bootstrap::getArgs($argv); if (!isset(self::$params[$runmode]) && !in_array($runmode, self::$commands)) { if ('' !== $runmode) { echo 'Unrecognized command: ' . $runmode . "\n"; } self::printUsage(); exit; } elseif ('help' === $runmode) { self::printHelp(); exit; } if (isset($args['configfile'])) { Daemon::$config->configfile->setHumanValue($args['configfile']); } if (!Daemon::$config->loadCmdLineArgs($args)) { $error = TRUE; } if (!Daemon::loadConfig(Daemon::$config->configfile->value)) { $error = TRUE; } if (version_compare(PHP_VERSION, '5.3.0', '>=') === 1) { Daemon::log('PHP >= 5.3.0 required.'); $error = TRUE; } if (!Daemon::$useSockets) { Daemon::log('Cannot use socket extension, using stream_* instead. Non-critical error, but the performance compromised.'); } if (extension_loaded('apc') && ini_get('apc.enabled')) { Daemon::log('Detected pecl-apc extension enabled. Please do not use it with phpdaemon to avoid possible fatal error \'Base lambda function for closure not found\' (php bug#52144). Usage of bytecode caching (APC/eAccelerator/xcache/...) in case of phpdaemon makes no sense at all \'cause PHPDaemon includes files just in time itself.'); } if (isset(Daemon::$config->locale->value) && Daemon::$config->locale->value !== '') { setlocale(LC_ALL, explode(',', Daemon::$config->locale->value)); } if (Daemon::$config->autoreimport->value && !is_callable('runkit_import')) { Daemon::log('runkit extension not found. You should install it or disable --auto-reimport. Non-critical error.'); } if (!is_callable('posix_kill')) { Daemon::log('Posix not found. You should compile PHP without \'--disable-posix\'.'); $error = TRUE; } if (!is_callable('pcntl_signal')) { Daemon::log('PCNTL not found. You should compile PHP with \'--enable-pcntl\'.'); $error = TRUE; } if (!is_callable('event_base_new')) { Daemon::log('libevent extension not found. You have to install libevent from pecl (http://pecl.php.net/package/libevent). `svn checkout http://svn.php.net/repository/pecl/libevent pecl-libevent`.'); $error = TRUE; } if (!is_callable('socket_create')) { Daemon::log('Sockets extension not found. You should compile PHP with \'--enable-sockets\'.'); $error = TRUE; } if (!is_callable('shmop_open')) { Daemon::log('Shmop extension not found. You should compile PHP with \'--enable-shmop\'.'); $error = TRUE; } if (!isset(Daemon::$config->user)) { Daemon::log('You must set \'user\' parameter.'); $error = TRUE; } if (!isset(Daemon::$config->path)) { Daemon::log('You must set \'path\' parameter (path to your application resolver).'); $error = TRUE; } if (!file_exists(Daemon::$config->pidfile->value)) { if (!touch(Daemon::$config->pidfile->value)) { Daemon::log('Couldn\'t create pid-file \'' . Daemon::$config->pidfile->value . '\'.'); $error = TRUE; } Daemon_Bootstrap::$pid = 0; } elseif (!is_file(Daemon::$config->pidfile->value)) { Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be a regular file.'); Daemon_Bootstrap::$pid = FALSE; $error = TRUE; } elseif (!is_writable(Daemon::$config->pidfile->value)) { Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be writable.'); $error = TRUE; } elseif (!is_readable(Daemon::$config->pidfile->value)) { Daemon::log('Pid-file \'' . Daemon::$config->pidfile->value . '\' must be readable.'); Daemon_Bootstrap::$pid = FALSE; $error = TRUE; } else { Daemon_Bootstrap::$pid = (int) file_get_contents(Daemon::$config->pidfile->value); } if (Daemon::$config->chroot->value !== '/') { if (posix_getuid() != 0) { Daemon::log('You must have the root privileges to change root.'); $error = TRUE; } } if (!@is_file(Daemon::$config->path->value)) { Daemon::log('Your application resolver \'' . Daemon::$config->path->value . '\' is not available.'); $error = TRUE; } if (isset(Daemon::$config->group->value) && is_callable('posix_getgid')) { if (($sg = posix_getgrnam(Daemon::$config->group->value)) === FALSE) { Daemon::log('Unexisting group \'' . Daemon::$config->group->value . '\'. You have to replace config-variable \'group\' with existing group-name.'); $error = TRUE; } elseif ($sg['gid'] != posix_getgid() && posix_getuid() != 0) { Daemon::log('You must have the root privileges to change group.'); $error = TRUE; } } if (isset(Daemon::$config->user->value) && is_callable('posix_getuid')) { if (($su = posix_getpwnam(Daemon::$config->user->value)) === FALSE) { Daemon::log('Unexisting user \'' . Daemon::$config->user->value . '\', user not found. You have to replace config-variable \'user\' with existing username.'); $error = TRUE; } elseif ($su['uid'] != posix_getuid() && posix_getuid() != 0) { Daemon::log('You must have the root privileges to change user.'); $error = TRUE; } } if (isset(Daemon::$config->minspareworkers->value) && isset(Daemon::$config->maxspareworkers->value)) { if (Daemon::$config->minspareworkers->value > Daemon::$config->maxspareworkers->value) { Daemon::log('\'minspareworkers\' cannot be greater than \'maxspareworkers\'.'); $error = TRUE; } } if (isset(Daemon::$config->minworkers->value) && isset(Daemon::$config->maxworkers->value)) { if (Daemon::$config->minworkers->value > Daemon::$config->maxworkers->value) { Daemon::$config->minworkers->value = Daemon::$config->maxworkers->value; } } if ($runmode == 'start') { if ($error === FALSE) { Daemon_Bootstrap::start(); } else { exit(6); } } elseif ($runmode == 'status' || $runmode == 'fullstatus') { $status = Daemon_Bootstrap::$pid && posix_kill(Daemon_Bootstrap::$pid, SIGTTIN); echo '[STATUS] phpDaemon ' . Daemon::$version . ' is ' . ($status ? 'running' : 'NOT running') . ' (' . Daemon::$config->pidfile->value . ").\n"; if ($status && $runmode == 'fullstatus') { echo 'Uptime: ' . Daemon::date_period_text(filemtime(Daemon::$config->pidfile->value), time()) . "\n"; Daemon::$shm_wstate = Daemon::shmop_open(Daemon::$config->pidfile->value, 0, 'wstate', FALSE); $stat = Daemon::getStateOfWorkers(); echo "State of workers:\n"; echo "\tTotal: " . $stat['alive'] . "\n"; echo "\tIdle: " . $stat['idle'] . "\n"; echo "\tBusy: " . $stat['busy'] . "\n"; echo "\tShutdown: " . $stat['shutdown'] . "\n"; echo "\tPre-init: " . $stat['preinit'] . "\n"; echo "\tWait-init: " . $stat['waitinit'] . "\n"; echo "\tInit: " . $stat['init'] . "\n"; } echo "\n"; } elseif ($runmode == 'update') { if (!Daemon_Bootstrap::$pid || !posix_kill(Daemon_Bootstrap::$pid, SIGHUP)) { echo '[UPDATE] ERROR. It seems that phpDaemon is not running' . (Daemon_Bootstrap::$pid ? ' (PID ' . Daemon_Bootstrap::$pid . ')' : '') . ".\n"; } } elseif ($runmode == 'reopenlog') { if (!Daemon_Bootstrap::$pid || !posix_kill(Daemon_Bootstrap::$pid, SIGUSR1)) { echo '[REOPEN-LOG] ERROR. It seems that phpDaemon is not running' . (Daemon_Bootstrap::$pid ? ' (PID ' . Daemon_Bootstrap::$pid . ')' : '') . ".\n"; } } elseif ($runmode == 'reload') { if (!Daemon_Bootstrap::$pid || !posix_kill(Daemon_Bootstrap::$pid, SIGUSR2)) { echo '[RELOAD] ERROR. It seems that phpDaemon is not running' . (Daemon_Bootstrap::$pid ? ' (PID ' . Daemon_Bootstrap::$pid . ')' : '') . ".\n"; } } elseif ($runmode == 'restart') { if ($error === FALSE) { Daemon_Bootstrap::stop(2); Daemon_Bootstrap::start(); } } elseif ($runmode == 'hardrestart') { Daemon_Bootstrap::stop(3); Daemon_Bootstrap::start(); } elseif ($runmode == 'configtest') { $term = new Terminal(); $term->enable_color = TRUE; echo "\n"; $rows = array(); $rows[] = array('parameter' => 'PARAMETER', 'value' => 'VALUE', '_color' => '37', '_bold' => TRUE); foreach (Daemon::$config as $name => $entry) { if (!$entry instanceof Daemon_ConfigEntry) { continue; } $row = array('parameter' => $name, 'value' => var_export($entry->humanValue, TRUE)); if ($entry->defaultValue != $entry->humanValue) { $row['value'] .= ' (' . var_export($entry->defaultValue, TRUE) . ')'; } $rows[] = $row; } $term->drawtable($rows); echo "\n"; } elseif ($runmode == 'stop') { Daemon_Bootstrap::stop(); } elseif ($runmode == 'hardstop') { echo '[HARDSTOP] Sending SIGINT to ' . Daemon_Bootstrap::$pid . '... '; $ok = Daemon_Bootstrap::$pid && posix_kill(Daemon_Bootstrap::$pid, SIGINT); echo $ok ? 'OK.' : 'ERROR. It seems that phpDaemon is not running.'; if ($ok) { $i = 0; while ($r = posix_kill(Daemon_Bootstrap::$pid, SIGTTIN)) { usleep(500000); if ($i == 9) { echo "\nphpDaemon master-process hasn't finished. Sending SIGKILL... "; posix_kill(Daemon_Bootstrap::$pid, SIGKILL); if (!posix_kill(Daemon_Bootstrap::$pid, SIGTTIN)) { echo " Oh, his blood is on my hands :'("; } else { echo "ERROR: Process alive. Permissions?"; } break; } ++$i; } } echo "\n"; } }
/** * Runtime of Worker process. * @return void */ public function run() { FS::init(); Daemon::$process = $this; if (Daemon::$logpointerAsync) { $oldfd = Daemon::$logpointerAsync->fd; Daemon::$logpointerAsync->fd = null; Daemon::$logpointerAsync = null; } $this->autoReloadLast = time(); $this->reloadDelay = Daemon::$config->mpmdelay->value + 2; $this->setStatus(4); if (Daemon::$config->autogc->value > 0) { gc_enable(); } else { gc_disable(); } $this->prepareSystemEnv(); $this->overrideNativeFuncs(); $this->setStatus(6); $this->eventBase = event_base_new(); $this->registerEventSignals(); FS::init(); // re-init FS::initEvent(); Daemon::openLogs(); $this->fileWatcher = new FileWatcher(); $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('IPCManager'); Daemon::$appResolver->preload(); foreach (Daemon::$appInstances as $app) { foreach ($app as $appInstance) { if (!$appInstance->ready) { $appInstance->ready = TRUE; $appInstance->onReady(); } } } $this->setStatus(1); Timer::add(function ($event) { $self = Daemon::$process; if ($self->checkState() !== TRUE) { $self->closeSockets(); $self->breakMainLoop = TRUE; event_base_loopexit($self->eventBase); return; } $event->timeout(); }, 1000000.0 * 1, 'checkState'); if (Daemon::$config->autoreload->value > 0) { Timer::add(function ($event) { $self = Daemon::$process; static $n = 0; $inc = array_unique(array_map('realpath', get_included_files())); $s = sizeof($inc); if ($s > $n) { $slice = array_slice($inc, $n); Daemon::$process->IPCManager->sendPacket(array('op' => 'addIncludedFiles', 'files' => $slice)); $n = $s; } $event->timeout(); }, 1000000.0 * Daemon::$config->autoreload->value, 'watchIncludedFiles'); } while (!$this->breakMainLoop) { event_base_loop($this->eventBase); } }