@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3abstract class PhabricatorDaemon extends PhutilDaemon {
4
5 protected function willRun() {
6 parent::willRun();
7
8 $phabricator = phutil_get_library_root('phabricator');
9 $root = dirname($phabricator);
10 require_once $root.'/scripts/__init_script__.php';
11 }
12
13 protected function willSleep($duration) {
14 LiskDAO::closeInactiveConnections(60);
15 return;
16 }
17
18 public function getViewer() {
19 return PhabricatorUser::getOmnipotentUser();
20 }
21
22
23 /**
24 * Format a command so it executes as the daemon user, if a daemon user is
25 * defined. This wraps the provided command in `sudo -u ...`, roughly.
26 *
27 * @param PhutilCommandString $command Command to execute.
28 * @return PhutilCommandString `sudo` version of the command.
29 */
30 public static function sudoCommandAsDaemonUser($command) {
31 $user = PhabricatorEnv::getEnvConfig('phd.user');
32 if (!$user) {
33 // No daemon user is set, so just run this as ourselves.
34 return $command;
35 }
36
37 // We may reach this method while already running as the daemon user: for
38 // example, active and passive synchronization of clustered repositories
39 // run the same commands through the same code, but as different users.
40
41 // By default, `sudo` won't let you sudo to yourself, so we can get into
42 // trouble if we're already running as the daemon user unless the host has
43 // been configured to let the daemon user run commands as itself.
44
45 // Since this is silly and more complicated than doing this check, don't
46 // use `sudo` if we're already running as the correct user.
47 if (function_exists('posix_getuid')) {
48 $uid = posix_getuid();
49 $info = posix_getpwuid($uid);
50 if ($info && $info['name'] == $user) {
51 return $command;
52 }
53 }
54
55 // Get the absolute path so we're safe against the caller wiping out
56 // PATH.
57 $sudo = Filesystem::resolveBinary('sudo');
58 if (!$sudo) {
59 throw new Exception(pht("Unable to find 'sudo'!"));
60 }
61
62 // Flags here are:
63 //
64 // -E: Preserve the environment.
65 // -n: Non-interactive. Exit with an error instead of prompting.
66 // -u: Which user to sudo to.
67
68 return csprintf('%s -E -n -u %s -- %C', $sudo, $user, $command);
69 }
70
71}