Ejemplo n.º 1
0
 /**
  * Runtime of Master process
  * @return void
  */
 public function run()
 {
     Daemon::$process = $this;
     $this->prepareSystemEnv();
     class_exists('Timer');
     // ensure loading this class
     gc_enable();
     $this->eventBase = event_base_new();
     $this->registerEventSignals();
     FS::initEvent();
     $this->fileWatcher = new FileWatcher();
     $this->workers = new ThreadCollection();
     $this->collections['workers'] = $this->workers;
     Daemon::$appResolver = (require Daemon::$config->path->value);
     $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('IPCManager');
     Daemon::$appResolver->preload(true);
     $this->callbacks = new SplStack();
     $this->spawnWorkers(min(Daemon::$config->startworkers->value, Daemon::$config->maxworkers->value));
     Timer::add(function ($event) use(&$cbs) {
         $self = Daemon::$process;
         static $c = 0;
         ++$c;
         if ($c > 0xfffff) {
             $c = 1;
         }
         if ($c % 10 == 0) {
             $self->workers->removeTerminated(true);
             gc_collect_cycles();
         } else {
             $self->workers->removeTerminated();
         }
         if (isset(Daemon::$config->mpm->value) && is_callable(Daemon::$config->mpm->value)) {
             call_user_func(Daemon::$config->mpm->value);
         } else {
             // default MPM
             $state = Daemon::getStateOfWorkers($self);
             if ($state) {
                 $n = max(min(Daemon::$config->minspareworkers->value - $state['idle'], Daemon::$config->maxworkers->value - $state['alive']), Daemon::$config->minworkers->value - $state['alive']);
                 if ($n > 0) {
                     Daemon::log('Spawning ' . $n . ' worker(s).');
                     $self->spawnWorkers($n);
                     event_base_loopbreak($self->eventBase);
                 }
                 $n = min($state['idle'] - Daemon::$config->maxspareworkers->value, $state['alive'] - Daemon::$config->minworkers->value);
                 if ($n > 0) {
                     Daemon::log('Stopping ' . $n . ' worker(s).');
                     $self->stopWorkers($n);
                 }
             }
         }
         $event->timeout();
     }, 1000000.0 * Daemon::$config->mpmdelay->value, 'MPM');
     while (!$this->breakMainLoop) {
         while (!$this->callbacks->isEmpty()) {
             call_user_func($this->callbacks->shift(), $this);
         }
         event_base_loop($this->eventBase);
     }
 }
 public function run()
 {
     proc_nice(Daemon::$settings['masterpriority']);
     gc_enable();
     register_shutdown_function(array($this, 'onShutdown'));
     $this->collections = array('workers' => new threadCollection());
     Thread::setproctitle(Daemon::$runName . ': master process' . (Daemon::$settings['pidfile'] !== Daemon::$settings['defaultpidfile'] ? ' (' . Daemon::$settings['pidfile'] . ')' : ''));
     Daemon::$appResolver = (require Daemon::$settings['path']);
     Daemon::$appResolver->preloadPrivileged();
     $this->spawnWorkers(min(Daemon::$settings['startworkers'], Daemon::$settings['maxworkers']));
     $mpmLast = time();
     $autoReloadLast = time();
     while (TRUE) {
         pcntl_signal_dispatch();
         $this->sigwait(1, 0);
         clearstatcache();
         if (Daemon::$logpointerpath !== Daemon::parseStoragepath(Daemon::$settings['logstorage'])) {
             $this->sigusr1();
         }
         $c = 1;
         if (time() > $mpmLast + Daemon::$parsedSettings['mpmdelay']) {
             $mpmLast = time();
             ++$c;
             if ($c > 0xfffff) {
                 $c = 0;
             }
             if ($c % 10 == 0) {
                 $this->collections['workers']->removeTerminated(TRUE);
                 gc_collect_cycles();
             } else {
                 $this->collections['workers']->removeTerminated();
             }
             if (isset(Daemon::$settings['mpm']) && is_callable($c = Daemon::$settings['mpm'])) {
                 call_user_func($c);
             } else {
                 $state = Daemon::getStateOfWorkers($this);
                 if ($state) {
                     $n = max(min(Daemon::$settings['minspareworkers'] - $state['idle'], Daemon::$settings['maxworkers'] - $state['alive']), Daemon::$settings['minworkers'] - $state['alive']);
                     if ($n > 0) {
                         Daemon::log('Spawning ' . $n . ' worker(s).');
                         $this->spawnWorkers($n);
                     }
                     $n = min($state['idle'] - Daemon::$settings['maxspareworkers'], $state['alive'] - Daemon::$settings['minworkers']);
                     if ($n > 0) {
                         Daemon::log('Stopping ' . $n . ' worker(s).');
                         $this->stopWorkers($n);
                     }
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
    /**
     * Called when request iterated.
     * @return integer Status.
     */
    public function run()
    {
        $stime = microtime(TRUE);
        $this->header('Content-Type: text/html; charset=utf-8');
        ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Server status.</title> 
</head> 
<body>
<br />Uptime: <b><?php 
        echo Daemon::date_period_text(Daemon::$startTime, time());
        ?>
</b>
<br /><br /><b>State of workers:</b><?php 
        $stat = Daemon::getStateOfWorkers();
        ?>
<br />Idle: <?php 
        echo $stat['idle'];
        ?>
<br />Busy: <?php 
        echo $stat['busy'];
        ?>
<br />Total alive: <?php 
        echo $stat['alive'];
        ?>
<br />Shutdown: <?php 
        echo $stat['shutdown'];
        ?>
<br />Pre-init: <?php 
        echo $stat['preinit'];
        ?>
<br />Wait-init: <?php 
        echo $stat['waitinit'];
        ?>
<br />Init: <?php 
        echo $stat['init'];
        ?>
<br />
<br />Request took: <?php 
        printf('%f', round(microtime(TRUE) - $stime, 6));
        ?>
</body>
</html>
<?php 
        return;
    }
Ejemplo n.º 4
0
    /**
     * Called when request iterated.
     * @return integer Status.
     */
    public function run()
    {
        $stime = microtime(TRUE);
        $this->header('Content-Type: text/html');
        $this->setcookie('testcookie', '1');
        $this->registerShutdownFunction(function () {
            ?>
</html><?php 
        });
        ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>It works!</title>
</head>
<body>
<h1>It works! Be happy! ;-)</h1>
Hello world!
<br />Counter of requests to this Application Instance: <b><?php 
        echo ++$this->appInstance->counter;
        ?>
</b>
<br />Memory usage: <?php 
        $mem = memory_get_usage();
        echo $mem / 1024 / 1024;
        ?>
 MB. (<?php 
        echo $mem;
        ?>
)
<br />Memory real usage: <?php 
        $mem = memory_get_usage(TRUE);
        echo $mem / 1024 / 1024;
        ?>
 MB. (<?php 
        echo $mem;
        ?>
)
<br />Pool size: <?php 
        echo sizeof(Daemon::$process->pool);
        ?>
.
<br />My PID: <?php 
        echo getmypid();
        ?>
.
<?php 
        $user = posix_getpwuid(posix_getuid());
        $group = posix_getgrgid(posix_getgid());
        ?>
<br />My user/group: <?php 
        echo $user['name'] . '/' . $group['name'];
        $displaystate = TRUE;
        if ($displaystate) {
            ?>
<br /><br /><b>State of workers:</b><?php 
            $stat = Daemon::getStateOfWorkers();
            ?>
<br />Idle: <?php 
            echo $stat['idle'];
            ?>
<br />Busy: <?php 
            echo $stat['busy'];
            ?>
<br />Total alive: <?php 
            echo $stat['alive'];
            ?>
<br />Shutdown: <?php 
            echo $stat['shutdown'];
            ?>
<br />Pre-init: <?php 
            echo $stat['preinit'];
            ?>
<br />Wait-init: <?php 
            echo $stat['waitinit'];
            ?>
<br />Init: <?php 
            echo $stat['init'];
            ?>
<br />
<?php 
        }
        ?>
<br /><br />
<br /><br /><form action="<?php 
        echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES);
        ?>
" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" />
<input type="submit" name="submit" value="Upload" />
</form>
<br />
<form action="<?php 
        echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES);
        ?>
" method="post">
<input type="text" name="mytext" value="" />
<input type="submit" name="submit" value="Send" />
</form>
<pre>
<?php 
        var_dump(array('_GET' => $_GET, '_POST' => $_POST, '_COOKIE' => $_COOKIE, '_REQUEST' => $_REQUEST, '_FILES' => $_FILES, '_SERVER' => $_SERVER));
        ?>
</pre>
<br />Request took: <?php 
        printf('%f', round(microtime(TRUE) - $stime, 6));
        ?>
</body><?php 
    }
Ejemplo n.º 5
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";
     }
 }
Ejemplo n.º 6
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;
     }
 }