PHPDaemon\Thread\IPC::prepareSystemEnv PHP Method

prepareSystemEnv() protected method

Setup settings on start.
protected prepareSystemEnv ( ) : void
return void
    protected function prepareSystemEnv()
    {
        proc_nice(Daemon::$config->ipcthreadpriority->value);
        register_shutdown_function([$this, 'shutdown']);
        $this->setTitle(Daemon::$runName . ': IPC process' . (Daemon::$config->pidfile->value !== Daemon::$config->defaultpidfile->value ? ' (' . Daemon::$config->pidfile->value . ')' : ''));
        if (isset(Daemon::$config->group->value)) {
            $sg = posix_getgrnam(Daemon::$config->group->value);
        }
        if (isset(Daemon::$config->user->value)) {
            $su = posix_getpwnam(Daemon::$config->user->value);
        }
        if (Daemon::$config->chroot->value !== '/') {
            if (posix_getuid() != 0) {
                $this->log('You must have the root privileges to change root.');
                exit(0);
            } elseif (!chroot(Daemon::$config->chroot->value)) {
                Daemon::log('Couldn\'t change root to \'' . Daemon::$config->chroot->value . '\'.');
                exit(0);
            }
        }
        if (isset(Daemon::$config->group->value)) {
            if ($sg === false) {
                $this->log('Couldn\'t change group to \'' . Daemon::$config->group->value . '\'. You must replace config-variable \'group\' with existing group.');
                exit(0);
            } elseif ($sg['gid'] != posix_getgid() && !posix_setgid($sg['gid'])) {
                $this->log('Couldn\'t change group to \'' . Daemon::$config->group->value . "'. Error (" . ($errno = posix_get_last_error()) . '): ' . posix_strerror($errno));
                exit(0);
            }
        }
        if (isset(Daemon::$config->user->value)) {
            if ($su === false) {
                $this->log('Couldn\'t change user to \'' . Daemon::$config->user->value . '\', user not found. You must replace config-variable \'user\' with existing username.');
                exit(0);
            } elseif ($su['uid'] != posix_getuid() && !posix_setuid($su['uid'])) {
                $this->log('Couldn\'t change user to \'' . Daemon::$config->user->value . "'. Error (" . ($errno = posix_get_last_error()) . '): ' . posix_strerror($errno));
                exit(0);
            }
        }
        if (Daemon::$config->cwd->value !== '.') {
            if (!@chdir(Daemon::$config->cwd->value)) {
                $this->log('Couldn\'t change directory to \'' . Daemon::$config->cwd->value . '.');
            }
        }
    }