more robust code for setting daemon uid/gid

darcs-hash:20080904191031-84dde-bb457c429c76eedb9bd4ea838b7ccad28844effd.gz
This commit is contained in:
Evan Prodromou 2008-09-04 15:10:31 -04:00
parent 65efe17c4e
commit 32a189220c

View File

@ -85,18 +85,30 @@ class Daemon {
function changeUser() { function changeUser() {
if (common_config('daemon', 'user')) { $username = common_config('daemon', 'user');
$user_info = posix_getpwnam(common_config('daemon', 'user'));
common_log(LOG_INFO, "Setting user to " . common_config('daemon', 'user')); if ($username) {
$user_info = posix_getpwnam($username);
if (!$user_info) {
common_log(LOG_WARNING, 'Ignoring unknown user for daemon: ' . $username);
} else {
common_log(LOG_INFO, "Setting user to " . $username);
posix_setuid($user_info['uid']); posix_setuid($user_info['uid']);
} }
}
if (common_config('daemon', 'group')) { $groupname = common_config('daemon', 'group');
$group_info = posix_getgrnam(common_config('daemon', 'group'));
common_log(LOG_INFO, "Setting group to " . common_config('daemon', 'group')); if ($groupname) {
$group_info = posix_getgrnam($groupname);
if (!$group_info) {
common_log(LOG_WARNING, 'Ignoring unknown group for daemon: ' . $groupname);
} else {
common_log(LOG_INFO, "Setting group to " . $groupname);
posix_setgid($group_info['gid']); posix_setgid($group_info['gid']);
} }
} }
}
function runOnce() { function runOnce() {
if ($this->alreadyRunning()) { if ($this->alreadyRunning()) {