Path: blob/master/src/infrastructure/daemon/PhabricatorDaemon.php
12241 views
<?php12abstract class PhabricatorDaemon extends PhutilDaemon {34protected function willRun() {5parent::willRun();67$phabricator = phutil_get_library_root('phabricator');8$root = dirname($phabricator);9require_once $root.'/scripts/__init_script__.php';10}1112protected function willSleep($duration) {13LiskDAO::closeInactiveConnections(60);14return;15}1617public function getViewer() {18return PhabricatorUser::getOmnipotentUser();19}202122/**23* Format a command so it executes as the daemon user, if a daemon user is24* defined. This wraps the provided command in `sudo -u ...`, roughly.25*26* @param PhutilCommandString Command to execute.27* @return PhutilCommandString `sudo` version of the command.28*/29public static function sudoCommandAsDaemonUser($command) {30$user = PhabricatorEnv::getEnvConfig('phd.user');31if (!$user) {32// No daemon user is set, so just run this as ourselves.33return $command;34}3536// We may reach this method while already running as the daemon user: for37// example, active and passive synchronization of clustered repositories38// run the same commands through the same code, but as different users.3940// By default, `sudo` won't let you sudo to yourself, so we can get into41// trouble if we're already running as the daemon user unless the host has42// been configured to let the daemon user run commands as itself.4344// Since this is silly and more complicated than doing this check, don't45// use `sudo` if we're already running as the correct user.46if (function_exists('posix_getuid')) {47$uid = posix_getuid();48$info = posix_getpwuid($uid);49if ($info && $info['name'] == $user) {50return $command;51}52}5354// Get the absolute path so we're safe against the caller wiping out55// PATH.56$sudo = Filesystem::resolveBinary('sudo');57if (!$sudo) {58throw new Exception(pht("Unable to find 'sudo'!"));59}6061// Flags here are:62//63// -E: Preserve the environment.64// -n: Non-interactive. Exit with an error instead of prompting.65// -u: Which user to sudo to.6667return csprintf('%s -E -n -u %s -- %C', $sudo, $user, $command);68}6970}717273