Пример #1
0
 /**
  * Actions on early startup.
  * @return void
  */
 public static function init()
 {
     Daemon::initSettings();
     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 (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.');
         $error = TRUE;
     }
     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();
         }
     } 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";
     }
 }
Пример #2
0
 public static function init()
 {
     Daemon::initSettings();
     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($args[$k = 'configfile'])) {
         Daemon::$settings[$k] = $args[$k];
     }
     if (isset(Daemon::$settings['configfile']) && !Daemon::loadConfig(Daemon::$settings['configfile'])) {
         $error = TRUE;
     }
     if (!Daemon::loadSettings($args)) {
         $error = TRUE;
     }
     if (version_compare(PHP_VERSION, '5.3.0', '>=') === 1) {
         Daemon::log('PHP >= 5.3.0 required.');
         $error = TRUE;
     }
     if (isset(Daemon::$settings['locale'])) {
         setlocale(LC_ALL, explode(',', Daemon::$settings['locale']));
     }
     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::$settings['user'])) {
         Daemon::log('You must set \'user\' parameter.');
         $error = TRUE;
     }
     if (!isset(Daemon::$settings['path'])) {
         Daemon::log('You must set \'path\' parameter (path to your application resolver).');
         $error = TRUE;
     }
     Daemon_Bootstrap::$pidfile = realpath(Daemon::$settings['pidfile']);
     if (!Daemon_Bootstrap::$pidfile) {
         Daemon_Bootstrap::$pidfile = Daemon::$settings['pidfile'];
     }
     if (!file_exists(Daemon_Bootstrap::$pidfile)) {
         if (!touch(Daemon_Bootstrap::$pidfile)) {
             Daemon::log('Couldn\'t create pid-file \'' . Daemon_Bootstrap::$pidfile . '\'.');
             $error = TRUE;
         }
         Daemon_Bootstrap::$pid = 0;
     } elseif (!is_file(Daemon_Bootstrap::$pidfile)) {
         Daemon::log('Pid-file \'' . Daemon_Bootstrap::$pidfile . '\' must be a regular file.');
         Daemon_Bootstrap::$pid = FALSE;
         $error = TRUE;
     } elseif (!is_writable(Daemon_Bootstrap::$pidfile)) {
         Daemon::log('Pid-file \'' . Daemon_Bootstrap::$pidfile . '\' must be writable.');
         $error = TRUE;
     } elseif (!is_readable(Daemon_Bootstrap::$pidfile)) {
         Daemon::log('Pid-file \'' . Daemon_Bootstrap::$pidfile . '\' must be readable.');
         Daemon_Bootstrap::$pid = FALSE;
         $error = TRUE;
     } else {
         Daemon_Bootstrap::$pid = (int) file_get_contents(Daemon_Bootstrap::$pidfile);
     }
     if (Daemon::$settings['chroot'] !== '/') {
         if (posix_getuid() != 0) {
             Daemon::log('You must have the root privileges to change root.');
             $error = TRUE;
         }
     }
     if (!@is_file(Daemon::$pathReal)) {
         Daemon::log('Your application resolver \'' . Daemon::$settings['path'] . '\' is not available.');
         $error = TRUE;
     }
     if (isset(Daemon::$settings['group']) && is_callable('posix_getgid')) {
         if (($sg = posix_getgrnam(Daemon::$settings['group'])) === FALSE) {
             Daemon::log('Unexisting group \'' . Daemon::$settings['group'] . '\'. 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::$settings['user']) && is_callable('posix_getuid')) {
         if (($su = posix_getpwnam(Daemon::$settings['user'])) === FALSE) {
             Daemon::log('Unexisting user \'' . Daemon::$settings['user'] . '\', 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::$settings['minspareworkers']) && isset(Daemon::$settings['maxspareworkers'])) {
         if (Daemon::$settings['minspareworkers'] > Daemon::$settings['maxspareworkers']) {
             Daemon::log('\'minspareworkers\' cannot be greater than \'maxspareworkers\'.');
             $error = TRUE;
         }
     }
     if (isset(Daemon::$settings['minworkers']) && isset(Daemon::$settings['maxworkers'])) {
         if (Daemon::$settings['minworkers'] > Daemon::$settings['maxworkers']) {
             Daemon::$settings['maxworkers'] = Daemon::$settings['minworkers'];
         }
     }
     if ($runmode == 'start') {
         if ($error === FALSE) {
             Daemon_Bootstrap::start();
         }
     } elseif ($runmode == 'status' or $runmode == 'fullstatus') {
         $status = Daemon_Bootstrap::$pid && posix_kill(Daemon_Bootstrap::$pid, SIGTTIN);
         echo '[STATUS] phpDaemon ' . Daemon::$version . ' is ' . ($status ? 'running' : 'NOT running') . ' (' . Daemon_Bootstrap::$pidfile . ").\n";
         if ($status && $runmode == 'fullstatus') {
             echo 'Uptime: ' . Daemon::date_period_text(filemtime(Daemon_Bootstrap::$pidfile), time()) . "\n";
             Daemon::$shm_wstate = Daemon::shmop_open(Daemon::$settings['ipcwstate'], 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') {
         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::$settings as $name => &$value) {
             $row = array('parameter' => $name, 'value' => var_export($value, TRUE));
             if (isset(Daemon::$parsedSettings[$name])) {
                 $row['value'] .= ' (' . Daemon::$parsedSettings[$name] . ')';
             }
             $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";
     } elseif ($runmode == 'help') {
         echo 'phpDaemon ' . Daemon::$version . ". Made in Russia. http://phpdaemon.googlecode.com/\nusage: " . Daemon::$runName . " (start|(hard)stop|update|reload|(hard)restart|fullstatus|status|configtest|help) ...\n\n\tAlso you can use some optional parameters to override the same config variables.\n\t--pid-file='/path/to/pid-file'  -  (Pid-file)\n\t--max-requests=" . Daemon::$settings['maxrequests'] . "  -  (Maximum requests to worker before respawn)\n\t--path='" . Daemon::$settings['path'] . "'  -  (You can set a path to your application resolver)\n\t--config-file='" . Daemon::$settings['configfile'] . "'  -  (You can set a path to configuration file manually)\n\t--logging=1  -  (Logging. 1-Enable, 0-Disable)\n\t--log-storage='" . Daemon::$settings['logstorage'] . "'  -  (Log storage. This field has special syntax, but it allows simple path.)\n\t--user='******'user'] . "'  -  (You can set user of master process (aka sudo).) \n\t--group='" . Daemon::$settings['group'] . "'  -  (You can set user of master process (aka sudo).)\n\t--manual  -  (Open the manual pages.)\n\t--help  -  (This help information.)\n  \n\n";
     } else {
         if ($runmode !== '') {
             echo 'Unrecognized command: ' . $runmode . "\n";
         }
         echo 'usage: ' . Daemon::$runName . " (start|(hard)stop|update|reload|(hard)restart|fullstatus|status|configtest|help) ...\n";
         exit;
     }
 }