Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Don't use sudo when the target is user is already the current user
Browse files Browse the repository at this point in the history
Also, improve the current process owner's username
  • Loading branch information
wa0x6e committed Oct 25, 2013
1 parent 711669f commit 00d23dd
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions Console/Command/CakeResqueShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public function start($args = null, $scheduler = false) {
$resqueBin = $scheduler ? './bin/resque-scheduler.php' : $this->_getResqueBinFile($this->_resqueLibrary);

$cmd = implode(' ', array(
sprintf("nohup sudo -u %s \\\n", $this->_runtime['user']),
sprintf("nohup %s \\\n", ($this->_runtime['user'] === $this->__getProcessOwner()) ? "" : "sudo -u " . $this->_runtime['user']),
sprintf("bash -c \"cd %s; \\\n", escapeshellarg($libraryPath)),
implode(' ', $envVars),
" \\\n",
Expand Down Expand Up @@ -1135,7 +1135,15 @@ protected function _validate($args = null) {

$this->_runtime['queue'] = isset($this->_runtime['queue']) ? $this->_runtime['queue'] : Configure::read('CakeResque.Worker.queue');

$this->_runtime['user'] = isset($this->_runtime['user']) ? $this->_runtime['user'] : get_current_user();
// Validate user
if (isset($this->_runtime['user'])) {
$this->_runtime['user'] = $this->_runtime['user'];
} elseif (Configure::read('CakeResque.Worker.user')) {
$this->_runtime['user'] = Configure::read('CakeResque.Worker.user');
} else {
$user = $this->__getProcessOwner();
$this->_runtime['user'] = empty($user) ? get_current_user() : $user;
}

$this->_runtime['verbose'] = isset($this->params['verbose']) ? $this->params['verbose'] : Configure::read('CakeResque.Worker.verbose');

Expand Down Expand Up @@ -1250,4 +1258,24 @@ protected function _checkStartedWorker($pidFile) {
}
return false;
}

/**
* @since 4.0.0
* @codeCoverageIgnore
* @return string Username of the current process owner if found, else false
*/
private function __getProcessOwner() {
if (function_exists('posix_getpwuid')) {
$a = posix_getpwuid(posix_getuid());
return $a['name'];
} else {
$user = trim(exec('whoami', $o, $code));
if ($code === 0) {
return $user;
}
return false;
}

return false;
}
}

0 comments on commit 00d23dd

Please sign in to comment.