/** * @throws Exception */ private function open() { if ($this->isOpen()) { throw new Exception('Process already opened', Exception::ALREADY_OPEN); } $spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w")); $resource = proc_open($this->command, $spec, $this->pipes, $this->workingDirectory, $this->env); if ($this->workaroundLinuxBug()) { $status = proc_get_status($resource); posix_setpgid($status['pid'], $status['pid']); } if ($resource === false) { throw new Exception(sprintf('Unable to proc_open cmd: %s', $this->command), Exception::OPEN_FAILED); } $this->resource = $resource; }
public static function timeOutExec($cmd, $stdin = '', $timeout) { $start = time(); $stdout = ''; $stderr = ''; file_put_contents('debug.txt', time() . ':cmd:' . $cmd . "\n\n" . print_r(json_decode($cmd, true)) . "\n", FILE_APPEND); file_put_contents('debug.txt', time() . ':stdin:' . $stdin . "\n", FILE_APPEND); $process = proc_open($cmd, [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes); if (!is_resource($process)) { return array('return' => '1', 'stdout' => $stdout, 'stderr' => $stderr); } $status = proc_get_status($process); posix_setpgid($status['pid'], $status['pid']); //seperate pgid(process group id) from parent's pgid stream_set_blocking($pipes[0], 0); stream_set_blocking($pipes[1], 0); stream_set_blocking($pipes[2], 0); fwrite($pipes[0], $stdin); fclose($pipes[0]); while (1) { $stdout .= stream_get_contents($pipes[1]); $stderr .= stream_get_contents($pipes[2]); if (time() - $start > $timeout) { //proc_terminate($process, 9); //only terminate subprocess, won't terminate sub-subprocess posix_kill(-$status['pid'], 9); //sends SIGKILL to all processes inside group(negative means GPID, all subprocesses share the top process group, except nested my_timeout_exec) file_put_contents('debug.txt', time() . ":kill group {$status['pid']}\n", FILE_APPEND); return array('return' => '1', 'stdout' => $stdout, 'stderr' => $stderr); } $status = proc_get_status($process); //file_put_contents('debug.txt', time().':status:'.var_export($status, true)."\n"; if (!$status['running']) { fclose($pipes[1]); fclose($pipes[2]); proc_close($process); return $status['exitcode']; } usleep(100000); } }
VERIFY(posix_getpgrp()); VERIFY(posix_getpid()); VERIFY(posix_getppid()); $ret = posix_getpwnam("root"); VERIFY($ret != false); VERIFY(count((array) $ret) != 0); VS(posix_getpwnam(""), false); VS(posix_getpwnam(-1), false); $ret = posix_getpwuid(0); VERIFY($ret != false); VERIFY(count((array) $ret) != 0); VS(posix_getpwuid(-1), false); $ret = posix_getrlimit(); VERIFY($ret != false); VERIFY(count((array) $ret) != 0); VERIFY(posix_getsid(posix_getpid())); $tmpfifo = tempnam('/tmp', 'vmmkfifotest'); unlink($tmpfifo); VERIFY(posix_mkfifo($tmpfifo, 0)); $tmpnod = tempnam('/tmp', 'vmmknodtest'); unlink($tmpnod); VERIFY(posix_mknod($tmpnod, 0)); VERIFY(posix_setpgid(0, 0)); VERIFY(posix_setsid()); VERIFY(strlen(posix_strerror(1))); $ret = posix_times(); VERIFY($ret != false); VERIFY(count((array) $ret) != 0); $ret = posix_uname(); VERIFY($ret != false); VERIFY(count((array) $ret) != 0);
public static function run($cmd) { posix_setpgid(0, 0); exec($cmd, $output, $returnCode); return $returnCode === 0; }
} else { echo "posix_setsid failed\n"; } $getpgid = posix_getpgid($pid); if ($getpgid > 0) { echo "posix_getpgid succeeded\n"; } else { echo "posix_getpgid failed\n"; } $getsid = posix_getsid($pid); if ($getsid > 0) { echo "posix_getsid succeeded\n"; } else { echo "posix_getsid failed\n"; } $setpgid = posix_setpgid($pid, $getpgid); if ($setpgid > 0) { echo "posix_setpgid succeeded\n"; } else { echo "posix_setpgid failed\n"; } $uname = posix_uname(); echo "uname=\n"; print_r($uname); $times = posix_times(); foreach ($times as $k => $v) { if ($v < 0) { echo "times[{$k}] is negative\n"; } else { echo "times[{$k}] is greater than or equal to 0\n"; }
/** * Set process group id for job control * * @param int $pid The process id. * @param int $pgid The process group id. * * @return bool */ public function setpgid(int $pid, int $pgid) : bool { return posix_setpgid($pid, $pgid); }
/** * Set the process group ID for job control * * @param int $pid The process id. * @param int $pgid The process group id. * * @return bool */ public function setpgid($pid, $pgid) { return posix_setpgid($pid, $pgid); }