chg: [internal] Avoid shell_exec

pull/7958/head
Jakub Onderka 2021-11-12 20:38:32 +01:00
parent 553036de94
commit 44279f090a
2 changed files with 18 additions and 2 deletions

View File

@ -291,11 +291,10 @@ class BackgroundJobsTool
foreach ($procs as $proc) {
if ($proc->offsetGet('group') === self::MISP_WORKERS_PROCESS_GROUP) {
if ($proc->offsetGet('pid') > 0) {
$user = trim(shell_exec(sprintf("ps -o uname='' -p %s", (int) $proc->offsetGet('pid'))) ?? '');
$workers[] = new Worker([
'pid' => $proc->offsetGet('pid'),
'queue' => explode("_", $proc->offsetGet('name'))[0],
'user' => $user,
'user' => $this->processUser((int) $proc->offsetGet('pid')),
'createdAt' => $proc->offsetGet('start'),
'updatedAt' => $proc->offsetGet('now'),
'status' => $this->convertProcessStatus($proc->offsetGet('state'))
@ -676,4 +675,20 @@ class BackgroundJobsTool
return Worker::STATUS_FAILED;
}
}
/**
* Get effective user name
* @param int $pid
* @return string
*/
private function processUser(int $pid)
{
if (function_exists('posix_getpwuid') && file_exists("/proc/$pid/status")) {
$content = file_get_contents("/proc/$pid/status");
preg_match("/Uid:\t([0-9]+)\t([0-9]+)/", $content, $matches);
return posix_getpwuid((int)$matches[2])['name'];
} else {
return trim(shell_exec(sprintf("ps -o uname='' -p %s", $pid)) ?? '');
}
}
}

View File

@ -19,6 +19,7 @@
"cakephp/debug_kit": "^2.2.0"
},
"suggest": {
"ext-posix": "For process info fetching",
"ext-gd": "For creating image thumbnails",
"ext-openssl": "Enabling the openssl extension allows you to access HTTPS URLs",
"ext-redis": "For working background jobs and feed and warninglist caches",