PageRenderTime 63ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/updraftplus/vendor/symfony/process/Process.php

https://gitlab.com/code26/selah
PHP | 1562 lines | 1000 code | 147 blank | 415 comment | 95 complexity | a39ba4ec4f69b6c5297ed418e9b2a88e MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process;
  11. use Symfony\Component\Process\Exception\InvalidArgumentException;
  12. use Symfony\Component\Process\Exception\LogicException;
  13. use Symfony\Component\Process\Exception\ProcessFailedException;
  14. use Symfony\Component\Process\Exception\ProcessTimedOutException;
  15. use Symfony\Component\Process\Exception\RuntimeException;
  16. use Symfony\Component\Process\Pipes\PipesInterface;
  17. use Symfony\Component\Process\Pipes\UnixPipes;
  18. use Symfony\Component\Process\Pipes\WindowsPipes;
  19. /**
  20. * Process is a thin wrapper around proc_* functions to easily
  21. * start independent PHP processes.
  22. *
  23. * @author Fabien Potencier <fabien@symfony.com>
  24. * @author Romain Neutron <imprec@gmail.com>
  25. */
  26. class Process implements \IteratorAggregate
  27. {
  28. const ERR = 'err';
  29. const OUT = 'out';
  30. const STATUS_READY = 'ready';
  31. const STATUS_STARTED = 'started';
  32. const STATUS_TERMINATED = 'terminated';
  33. const STDIN = 0;
  34. const STDOUT = 1;
  35. const STDERR = 2;
  36. // Timeout Precision in seconds.
  37. const TIMEOUT_PRECISION = 0.2;
  38. const ITER_NON_BLOCKING = 1; // By default, iterating over outputs is a blocking call, use this flag to make it non-blocking
  39. const ITER_KEEP_OUTPUT = 2; // By default, outputs are cleared while iterating, use this flag to keep them in memory
  40. const ITER_SKIP_OUT = 4; // Use this flag to skip STDOUT while iterating
  41. const ITER_SKIP_ERR = 8; // Use this flag to skip STDERR while iterating
  42. private $callback;
  43. private $hasCallback = false;
  44. private $commandline;
  45. private $cwd;
  46. private $env;
  47. private $input;
  48. private $starttime;
  49. private $lastOutputTime;
  50. private $timeout;
  51. private $idleTimeout;
  52. private $options = array('suppress_errors' => true);
  53. private $exitcode;
  54. private $fallbackStatus = array();
  55. private $processInformation;
  56. private $outputDisabled = false;
  57. private $stdout;
  58. private $stderr;
  59. private $enhanceWindowsCompatibility = true;
  60. private $enhanceSigchildCompatibility;
  61. private $process;
  62. private $status = self::STATUS_READY;
  63. private $incrementalOutputOffset = 0;
  64. private $incrementalErrorOutputOffset = 0;
  65. private $tty;
  66. private $pty;
  67. private $inheritEnv = false;
  68. private $useFileHandles = false;
  69. /** @var PipesInterface */
  70. private $processPipes;
  71. private $latestSignal;
  72. private static $sigchild;
  73. /**
  74. * Exit codes translation table.
  75. *
  76. * User-defined errors must use exit codes in the 64-113 range.
  77. */
  78. public static $exitCodes = array(
  79. 0 => 'OK',
  80. 1 => 'General error',
  81. 2 => 'Misuse of shell builtins',
  82. 126 => 'Invoked command cannot execute',
  83. 127 => 'Command not found',
  84. 128 => 'Invalid exit argument',
  85. // signals
  86. 129 => 'Hangup',
  87. 130 => 'Interrupt',
  88. 131 => 'Quit and dump core',
  89. 132 => 'Illegal instruction',
  90. 133 => 'Trace/breakpoint trap',
  91. 134 => 'Process aborted',
  92. 135 => 'Bus error: "access to undefined portion of memory object"',
  93. 136 => 'Floating point exception: "erroneous arithmetic operation"',
  94. 137 => 'Kill (terminate immediately)',
  95. 138 => 'User-defined 1',
  96. 139 => 'Segmentation violation',
  97. 140 => 'User-defined 2',
  98. 141 => 'Write to pipe with no one reading',
  99. 142 => 'Signal raised by alarm',
  100. 143 => 'Termination (request to terminate)',
  101. // 144 - not defined
  102. 145 => 'Child process terminated, stopped (or continued*)',
  103. 146 => 'Continue if stopped',
  104. 147 => 'Stop executing temporarily',
  105. 148 => 'Terminal stop signal',
  106. 149 => 'Background process attempting to read from tty ("in")',
  107. 150 => 'Background process attempting to write to tty ("out")',
  108. 151 => 'Urgent data available on socket',
  109. 152 => 'CPU time limit exceeded',
  110. 153 => 'File size limit exceeded',
  111. 154 => 'Signal raised by timer counting virtual time: "virtual timer expired"',
  112. 155 => 'Profiling timer expired',
  113. // 156 - not defined
  114. 157 => 'Pollable event',
  115. // 158 - not defined
  116. 159 => 'Bad syscall',
  117. );
  118. /**
  119. * @param string|array $commandline The command line to run
  120. * @param string|null $cwd The working directory or null to use the working dir of the current PHP process
  121. * @param array|null $env The environment variables or null to use the same environment as the current PHP process
  122. * @param mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input
  123. * @param int|float|null $timeout The timeout in seconds or null to disable
  124. * @param array $options An array of options for proc_open
  125. *
  126. * @throws RuntimeException When proc_open is not installed
  127. */
  128. public function __construct($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = null)
  129. {
  130. if (!function_exists('proc_open')) {
  131. throw new RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
  132. }
  133. $this->commandline = $commandline;
  134. $this->cwd = $cwd;
  135. // on Windows, if the cwd changed via chdir(), proc_open defaults to the dir where PHP was started
  136. // on Gnu/Linux, PHP builds with --enable-maintainer-zts are also affected
  137. // @see : https://bugs.php.net/bug.php?id=51800
  138. // @see : https://bugs.php.net/bug.php?id=50524
  139. if (null === $this->cwd && (defined('ZEND_THREAD_SAFE') || '\\' === DIRECTORY_SEPARATOR)) {
  140. $this->cwd = getcwd();
  141. }
  142. if (null !== $env) {
  143. $this->setEnv($env);
  144. }
  145. $this->setInput($input);
  146. $this->setTimeout($timeout);
  147. $this->useFileHandles = '\\' === DIRECTORY_SEPARATOR;
  148. $this->pty = false;
  149. $this->enhanceSigchildCompatibility = '\\' !== DIRECTORY_SEPARATOR && $this->isSigchildEnabled();
  150. if (null !== $options) {
  151. @trigger_error(sprintf('The $options parameter of the %s constructor is deprecated since Symfony 3.3 and will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED);
  152. $this->options = array_replace($this->options, $options);
  153. }
  154. }
  155. public function __destruct()
  156. {
  157. $this->stop(0);
  158. }
  159. public function __clone()
  160. {
  161. $this->resetProcessData();
  162. }
  163. /**
  164. * Runs the process.
  165. *
  166. * The callback receives the type of output (out or err) and
  167. * some bytes from the output in real-time. It allows to have feedback
  168. * from the independent process during execution.
  169. *
  170. * The STDOUT and STDERR are also available after the process is finished
  171. * via the getOutput() and getErrorOutput() methods.
  172. *
  173. * @param callable|null $callback A PHP callback to run whenever there is some
  174. * output available on STDOUT or STDERR
  175. * @param array $env An array of additional env vars to set when running the process
  176. *
  177. * @return int The exit status code
  178. *
  179. * @throws RuntimeException When process can't be launched
  180. * @throws RuntimeException When process stopped after receiving signal
  181. * @throws LogicException In case a callback is provided and output has been disabled
  182. *
  183. * @final since version 3.3
  184. */
  185. public function run($callback = null/*, array $env = array()*/)
  186. {
  187. $env = 1 < func_num_args() ? func_get_arg(1) : null;
  188. $this->start($callback, $env);
  189. return $this->wait();
  190. }
  191. /**
  192. * Runs the process.
  193. *
  194. * This is identical to run() except that an exception is thrown if the process
  195. * exits with a non-zero exit code.
  196. *
  197. * @param callable|null $callback
  198. * @param array $env An array of additional env vars to set when running the process
  199. *
  200. * @return self
  201. *
  202. * @throws RuntimeException if PHP was compiled with --enable-sigchild and the enhanced sigchild compatibility mode is not enabled
  203. * @throws ProcessFailedException if the process didn't terminate successfully
  204. *
  205. * @final since version 3.3
  206. */
  207. public function mustRun(callable $callback = null/*, array $env = array()*/)
  208. {
  209. if (!$this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
  210. throw new RuntimeException('This PHP has been compiled with --enable-sigchild. You must use setEnhanceSigchildCompatibility() to use this method.');
  211. }
  212. $env = 1 < func_num_args() ? func_get_arg(1) : null;
  213. if (0 !== $this->run($callback, $env)) {
  214. throw new ProcessFailedException($this);
  215. }
  216. return $this;
  217. }
  218. /**
  219. * Starts the process and returns after writing the input to STDIN.
  220. *
  221. * This method blocks until all STDIN data is sent to the process then it
  222. * returns while the process runs in the background.
  223. *
  224. * The termination of the process can be awaited with wait().
  225. *
  226. * The callback receives the type of output (out or err) and some bytes from
  227. * the output in real-time while writing the standard input to the process.
  228. * It allows to have feedback from the independent process during execution.
  229. *
  230. * @param callable|null $callback A PHP callback to run whenever there is some
  231. * output available on STDOUT or STDERR
  232. * @param array $env An array of additional env vars to set when running the process
  233. *
  234. * @throws RuntimeException When process can't be launched
  235. * @throws RuntimeException When process is already running
  236. * @throws LogicException In case a callback is provided and output has been disabled
  237. */
  238. public function start(callable $callback = null/*, array $env = array()*/)
  239. {
  240. if ($this->isRunning()) {
  241. throw new RuntimeException('Process is already running');
  242. }
  243. if (2 <= func_num_args()) {
  244. $env = func_get_arg(1);
  245. } else {
  246. if (__CLASS__ !== static::class) {
  247. $r = new \ReflectionMethod($this, __FUNCTION__);
  248. if (__CLASS__ !== $r->getDeclaringClass()->getName() && (2 > $r->getNumberOfParameters() || 'env' !== $r->getParameters()[0]->name)) {
  249. @trigger_error(sprintf('The %s::start() method expects a second "$env" argument since Symfony 3.3. It will be made mandatory in 4.0.', static::class), E_USER_DEPRECATED);
  250. }
  251. }
  252. $env = null;
  253. }
  254. $this->resetProcessData();
  255. $this->starttime = $this->lastOutputTime = microtime(true);
  256. $this->callback = $this->buildCallback($callback);
  257. $this->hasCallback = null !== $callback;
  258. $descriptors = $this->getDescriptors();
  259. $inheritEnv = $this->inheritEnv;
  260. if (is_array($commandline = $this->commandline)) {
  261. $commandline = implode(' ', array_map(array($this, 'escapeArgument'), $commandline));
  262. if ('\\' !== DIRECTORY_SEPARATOR) {
  263. // exec is mandatory to deal with sending a signal to the process
  264. $commandline = 'exec '.$commandline;
  265. }
  266. }
  267. if (null === $env) {
  268. $env = $this->env;
  269. } else {
  270. if ($this->env) {
  271. $env += $this->env;
  272. }
  273. $inheritEnv = true;
  274. }
  275. if (null !== $env && $inheritEnv) {
  276. $env += $this->getDefaultEnv();
  277. } elseif (null !== $env) {
  278. @trigger_error('Not inheriting environment variables is deprecated since Symfony 3.3 and will always happen in 4.0. Set "Process::inheritEnvironmentVariables()" to true instead.', E_USER_DEPRECATED);
  279. } else {
  280. $env = $this->getDefaultEnv();
  281. }
  282. if ('\\' === DIRECTORY_SEPARATOR && $this->enhanceWindowsCompatibility) {
  283. $this->options['bypass_shell'] = true;
  284. $commandline = $this->prepareWindowsCommandLine($commandline, $env);
  285. } elseif (!$this->useFileHandles && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
  286. // last exit code is output on the fourth pipe and caught to work around --enable-sigchild
  287. $descriptors[3] = array('pipe', 'w');
  288. // See https://unix.stackexchange.com/questions/71205/background-process-pipe-input
  289. $commandline = '{ ('.$commandline.') <&3 3<&- 3>/dev/null & } 3<&0;';
  290. $commandline .= 'pid=$!; echo $pid >&3; wait $pid; code=$?; echo $code >&3; exit $code';
  291. // Workaround for the bug, when PTS functionality is enabled.
  292. // @see : https://bugs.php.net/69442
  293. $ptsWorkaround = fopen(__FILE__, 'r');
  294. }
  295. if (defined('HHVM_VERSION')) {
  296. $envPairs = $env;
  297. } else {
  298. $envPairs = array();
  299. foreach ($env as $k => $v) {
  300. $envPairs[] = $k.'='.$v;
  301. }
  302. }
  303. if (!is_dir($this->cwd)) {
  304. @trigger_error('The provided cwd does not exist. Command is currently ran against getcwd(). This behavior is deprecated since Symfony 3.4 and will be removed in 4.0.', E_USER_DEPRECATED);
  305. }
  306. $this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $envPairs, $this->options);
  307. if (!is_resource($this->process)) {
  308. throw new RuntimeException('Unable to launch a new process.');
  309. }
  310. $this->status = self::STATUS_STARTED;
  311. if (isset($descriptors[3])) {
  312. $this->fallbackStatus['pid'] = (int) fgets($this->processPipes->pipes[3]);
  313. }
  314. if ($this->tty) {
  315. return;
  316. }
  317. $this->updateStatus(false);
  318. $this->checkTimeout();
  319. }
  320. /**
  321. * Restarts the process.
  322. *
  323. * Be warned that the process is cloned before being started.
  324. *
  325. * @param callable|null $callback A PHP callback to run whenever there is some
  326. * output available on STDOUT or STDERR
  327. * @param array $env An array of additional env vars to set when running the process
  328. *
  329. * @return $this
  330. *
  331. * @throws RuntimeException When process can't be launched
  332. * @throws RuntimeException When process is already running
  333. *
  334. * @see start()
  335. *
  336. * @final since version 3.3
  337. */
  338. public function restart(callable $callback = null/*, array $env = array()*/)
  339. {
  340. if ($this->isRunning()) {
  341. throw new RuntimeException('Process is already running');
  342. }
  343. $env = 1 < func_num_args() ? func_get_arg(1) : null;
  344. $process = clone $this;
  345. $process->start($callback, $env);
  346. return $process;
  347. }
  348. /**
  349. * Waits for the process to terminate.
  350. *
  351. * The callback receives the type of output (out or err) and some bytes
  352. * from the output in real-time while writing the standard input to the process.
  353. * It allows to have feedback from the independent process during execution.
  354. *
  355. * @param callable|null $callback A valid PHP callback
  356. *
  357. * @return int The exitcode of the process
  358. *
  359. * @throws RuntimeException When process timed out
  360. * @throws RuntimeException When process stopped after receiving signal
  361. * @throws LogicException When process is not yet started
  362. */
  363. public function wait(callable $callback = null)
  364. {
  365. $this->requireProcessIsStarted(__FUNCTION__);
  366. $this->updateStatus(false);
  367. if (null !== $callback) {
  368. if (!$this->processPipes->haveReadSupport()) {
  369. $this->stop(0);
  370. throw new \LogicException('Pass the callback to the Process::start method or enableOutput to use a callback with Process::wait');
  371. }
  372. $this->callback = $this->buildCallback($callback);
  373. }
  374. do {
  375. $this->checkTimeout();
  376. $running = '\\' === DIRECTORY_SEPARATOR ? $this->isRunning() : $this->processPipes->areOpen();
  377. $this->readPipes($running, '\\' !== DIRECTORY_SEPARATOR || !$running);
  378. } while ($running);
  379. while ($this->isRunning()) {
  380. usleep(1000);
  381. }
  382. if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {
  383. throw new RuntimeException(sprintf('The process has been signaled with signal "%s".', $this->processInformation['termsig']));
  384. }
  385. return $this->exitcode;
  386. }
  387. /**
  388. * Returns the Pid (process identifier), if applicable.
  389. *
  390. * @return int|null The process id if running, null otherwise
  391. */
  392. public function getPid()
  393. {
  394. return $this->isRunning() ? $this->processInformation['pid'] : null;
  395. }
  396. /**
  397. * Sends a POSIX signal to the process.
  398. *
  399. * @param int $signal A valid POSIX signal (see http://www.php.net/manual/en/pcntl.constants.php)
  400. *
  401. * @return $this
  402. *
  403. * @throws LogicException In case the process is not running
  404. * @throws RuntimeException In case --enable-sigchild is activated and the process can't be killed
  405. * @throws RuntimeException In case of failure
  406. */
  407. public function signal($signal)
  408. {
  409. $this->doSignal($signal, true);
  410. return $this;
  411. }
  412. /**
  413. * Disables fetching output and error output from the underlying process.
  414. *
  415. * @return $this
  416. *
  417. * @throws RuntimeException In case the process is already running
  418. * @throws LogicException if an idle timeout is set
  419. */
  420. public function disableOutput()
  421. {
  422. if ($this->isRunning()) {
  423. throw new RuntimeException('Disabling output while the process is running is not possible.');
  424. }
  425. if (null !== $this->idleTimeout) {
  426. throw new LogicException('Output can not be disabled while an idle timeout is set.');
  427. }
  428. $this->outputDisabled = true;
  429. return $this;
  430. }
  431. /**
  432. * Enables fetching output and error output from the underlying process.
  433. *
  434. * @return $this
  435. *
  436. * @throws RuntimeException In case the process is already running
  437. */
  438. public function enableOutput()
  439. {
  440. if ($this->isRunning()) {
  441. throw new RuntimeException('Enabling output while the process is running is not possible.');
  442. }
  443. $this->outputDisabled = false;
  444. return $this;
  445. }
  446. /**
  447. * Returns true in case the output is disabled, false otherwise.
  448. *
  449. * @return bool
  450. */
  451. public function isOutputDisabled()
  452. {
  453. return $this->outputDisabled;
  454. }
  455. /**
  456. * Returns the current output of the process (STDOUT).
  457. *
  458. * @return string The process output
  459. *
  460. * @throws LogicException in case the output has been disabled
  461. * @throws LogicException In case the process is not started
  462. */
  463. public function getOutput()
  464. {
  465. $this->readPipesForOutput(__FUNCTION__);
  466. if (false === $ret = stream_get_contents($this->stdout, -1, 0)) {
  467. return '';
  468. }
  469. return $ret;
  470. }
  471. /**
  472. * Returns the output incrementally.
  473. *
  474. * In comparison with the getOutput method which always return the whole
  475. * output, this one returns the new output since the last call.
  476. *
  477. * @return string The process output since the last call
  478. *
  479. * @throws LogicException in case the output has been disabled
  480. * @throws LogicException In case the process is not started
  481. */
  482. public function getIncrementalOutput()
  483. {
  484. $this->readPipesForOutput(__FUNCTION__);
  485. $latest = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
  486. $this->incrementalOutputOffset = ftell($this->stdout);
  487. if (false === $latest) {
  488. return '';
  489. }
  490. return $latest;
  491. }
  492. /**
  493. * Returns an iterator to the output of the process, with the output type as keys (Process::OUT/ERR).
  494. *
  495. * @param int $flags A bit field of Process::ITER_* flags
  496. *
  497. * @throws LogicException in case the output has been disabled
  498. * @throws LogicException In case the process is not started
  499. *
  500. * @return \Generator
  501. */
  502. public function getIterator($flags = 0)
  503. {
  504. $this->readPipesForOutput(__FUNCTION__, false);
  505. $clearOutput = !(self::ITER_KEEP_OUTPUT & $flags);
  506. $blocking = !(self::ITER_NON_BLOCKING & $flags);
  507. $yieldOut = !(self::ITER_SKIP_OUT & $flags);
  508. $yieldErr = !(self::ITER_SKIP_ERR & $flags);
  509. while (null !== $this->callback || ($yieldOut && !feof($this->stdout)) || ($yieldErr && !feof($this->stderr))) {
  510. if ($yieldOut) {
  511. $out = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
  512. if (isset($out[0])) {
  513. if ($clearOutput) {
  514. $this->clearOutput();
  515. } else {
  516. $this->incrementalOutputOffset = ftell($this->stdout);
  517. }
  518. yield self::OUT => $out;
  519. }
  520. }
  521. if ($yieldErr) {
  522. $err = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
  523. if (isset($err[0])) {
  524. if ($clearOutput) {
  525. $this->clearErrorOutput();
  526. } else {
  527. $this->incrementalErrorOutputOffset = ftell($this->stderr);
  528. }
  529. yield self::ERR => $err;
  530. }
  531. }
  532. if (!$blocking && !isset($out[0]) && !isset($err[0])) {
  533. yield self::OUT => '';
  534. }
  535. $this->checkTimeout();
  536. $this->readPipesForOutput(__FUNCTION__, $blocking);
  537. }
  538. }
  539. /**
  540. * Clears the process output.
  541. *
  542. * @return $this
  543. */
  544. public function clearOutput()
  545. {
  546. ftruncate($this->stdout, 0);
  547. fseek($this->stdout, 0);
  548. $this->incrementalOutputOffset = 0;
  549. return $this;
  550. }
  551. /**
  552. * Returns the current error output of the process (STDERR).
  553. *
  554. * @return string The process error output
  555. *
  556. * @throws LogicException in case the output has been disabled
  557. * @throws LogicException In case the process is not started
  558. */
  559. public function getErrorOutput()
  560. {
  561. $this->readPipesForOutput(__FUNCTION__);
  562. if (false === $ret = stream_get_contents($this->stderr, -1, 0)) {
  563. return '';
  564. }
  565. return $ret;
  566. }
  567. /**
  568. * Returns the errorOutput incrementally.
  569. *
  570. * In comparison with the getErrorOutput method which always return the
  571. * whole error output, this one returns the new error output since the last
  572. * call.
  573. *
  574. * @return string The process error output since the last call
  575. *
  576. * @throws LogicException in case the output has been disabled
  577. * @throws LogicException In case the process is not started
  578. */
  579. public function getIncrementalErrorOutput()
  580. {
  581. $this->readPipesForOutput(__FUNCTION__);
  582. $latest = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
  583. $this->incrementalErrorOutputOffset = ftell($this->stderr);
  584. if (false === $latest) {
  585. return '';
  586. }
  587. return $latest;
  588. }
  589. /**
  590. * Clears the process output.
  591. *
  592. * @return $this
  593. */
  594. public function clearErrorOutput()
  595. {
  596. ftruncate($this->stderr, 0);
  597. fseek($this->stderr, 0);
  598. $this->incrementalErrorOutputOffset = 0;
  599. return $this;
  600. }
  601. /**
  602. * Returns the exit code returned by the process.
  603. *
  604. * @return null|int The exit status code, null if the Process is not terminated
  605. *
  606. * @throws RuntimeException In case --enable-sigchild is activated and the sigchild compatibility mode is disabled
  607. */
  608. public function getExitCode()
  609. {
  610. if (!$this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
  611. throw new RuntimeException('This PHP has been compiled with --enable-sigchild. You must use setEnhanceSigchildCompatibility() to use this method.');
  612. }
  613. $this->updateStatus(false);
  614. return $this->exitcode;
  615. }
  616. /**
  617. * Returns a string representation for the exit code returned by the process.
  618. *
  619. * This method relies on the Unix exit code status standardization
  620. * and might not be relevant for other operating systems.
  621. *
  622. * @return null|string A string representation for the exit status code, null if the Process is not terminated
  623. *
  624. * @see http://tldp.org/LDP/abs/html/exitcodes.html
  625. * @see http://en.wikipedia.org/wiki/Unix_signal
  626. */
  627. public function getExitCodeText()
  628. {
  629. if (null === $exitcode = $this->getExitCode()) {
  630. return;
  631. }
  632. return isset(self::$exitCodes[$exitcode]) ? self::$exitCodes[$exitcode] : 'Unknown error';
  633. }
  634. /**
  635. * Checks if the process ended successfully.
  636. *
  637. * @return bool true if the process ended successfully, false otherwise
  638. */
  639. public function isSuccessful()
  640. {
  641. return 0 === $this->getExitCode();
  642. }
  643. /**
  644. * Returns true if the child process has been terminated by an uncaught signal.
  645. *
  646. * It always returns false on Windows.
  647. *
  648. * @return bool
  649. *
  650. * @throws RuntimeException In case --enable-sigchild is activated
  651. * @throws LogicException In case the process is not terminated
  652. */
  653. public function hasBeenSignaled()
  654. {
  655. $this->requireProcessIsTerminated(__FUNCTION__);
  656. if (!$this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
  657. throw new RuntimeException('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved.');
  658. }
  659. return $this->processInformation['signaled'];
  660. }
  661. /**
  662. * Returns the number of the signal that caused the child process to terminate its execution.
  663. *
  664. * It is only meaningful if hasBeenSignaled() returns true.
  665. *
  666. * @return int
  667. *
  668. * @throws RuntimeException In case --enable-sigchild is activated
  669. * @throws LogicException In case the process is not terminated
  670. */
  671. public function getTermSignal()
  672. {
  673. $this->requireProcessIsTerminated(__FUNCTION__);
  674. if ($this->isSigchildEnabled() && (!$this->enhanceSigchildCompatibility || -1 === $this->processInformation['termsig'])) {
  675. throw new RuntimeException('This PHP has been compiled with --enable-sigchild. Term signal can not be retrieved.');
  676. }
  677. return $this->processInformation['termsig'];
  678. }
  679. /**
  680. * Returns true if the child process has been stopped by a signal.
  681. *
  682. * It always returns false on Windows.
  683. *
  684. * @return bool
  685. *
  686. * @throws LogicException In case the process is not terminated
  687. */
  688. public function hasBeenStopped()
  689. {
  690. $this->requireProcessIsTerminated(__FUNCTION__);
  691. return $this->processInformation['stopped'];
  692. }
  693. /**
  694. * Returns the number of the signal that caused the child process to stop its execution.
  695. *
  696. * It is only meaningful if hasBeenStopped() returns true.
  697. *
  698. * @return int
  699. *
  700. * @throws LogicException In case the process is not terminated
  701. */
  702. public function getStopSignal()
  703. {
  704. $this->requireProcessIsTerminated(__FUNCTION__);
  705. return $this->processInformation['stopsig'];
  706. }
  707. /**
  708. * Checks if the process is currently running.
  709. *
  710. * @return bool true if the process is currently running, false otherwise
  711. */
  712. public function isRunning()
  713. {
  714. if (self::STATUS_STARTED !== $this->status) {
  715. return false;
  716. }
  717. $this->updateStatus(false);
  718. return $this->processInformation['running'];
  719. }
  720. /**
  721. * Checks if the process has been started with no regard to the current state.
  722. *
  723. * @return bool true if status is ready, false otherwise
  724. */
  725. public function isStarted()
  726. {
  727. return self::STATUS_READY != $this->status;
  728. }
  729. /**
  730. * Checks if the process is terminated.
  731. *
  732. * @return bool true if process is terminated, false otherwise
  733. */
  734. public function isTerminated()
  735. {
  736. $this->updateStatus(false);
  737. return self::STATUS_TERMINATED == $this->status;
  738. }
  739. /**
  740. * Gets the process status.
  741. *
  742. * The status is one of: ready, started, terminated.
  743. *
  744. * @return string The current process status
  745. */
  746. public function getStatus()
  747. {
  748. $this->updateStatus(false);
  749. return $this->status;
  750. }
  751. /**
  752. * Stops the process.
  753. *
  754. * @param int|float $timeout The timeout in seconds
  755. * @param int $signal A POSIX signal to send in case the process has not stop at timeout, default is SIGKILL (9)
  756. *
  757. * @return int The exit-code of the process
  758. */
  759. public function stop($timeout = 10, $signal = null)
  760. {
  761. $timeoutMicro = microtime(true) + $timeout;
  762. if ($this->isRunning()) {
  763. // given `SIGTERM` may not be defined and that `proc_terminate` uses the constant value and not the constant itself, we use the same here
  764. $this->doSignal(15, false);
  765. do {
  766. usleep(1000);
  767. } while ($this->isRunning() && microtime(true) < $timeoutMicro);
  768. if ($this->isRunning()) {
  769. // Avoid exception here: process is supposed to be running, but it might have stopped just
  770. // after this line. In any case, let's silently discard the error, we cannot do anything.
  771. $this->doSignal($signal ?: 9, false);
  772. }
  773. }
  774. if ($this->isRunning()) {
  775. if (isset($this->fallbackStatus['pid'])) {
  776. unset($this->fallbackStatus['pid']);
  777. return $this->stop(0, $signal);
  778. }
  779. $this->close();
  780. }
  781. return $this->exitcode;
  782. }
  783. /**
  784. * Adds a line to the STDOUT stream.
  785. *
  786. * @internal
  787. *
  788. * @param string $line The line to append
  789. */
  790. public function addOutput($line)
  791. {
  792. $this->lastOutputTime = microtime(true);
  793. fseek($this->stdout, 0, SEEK_END);
  794. fwrite($this->stdout, $line);
  795. fseek($this->stdout, $this->incrementalOutputOffset);
  796. }
  797. /**
  798. * Adds a line to the STDERR stream.
  799. *
  800. * @internal
  801. *
  802. * @param string $line The line to append
  803. */
  804. public function addErrorOutput($line)
  805. {
  806. $this->lastOutputTime = microtime(true);
  807. fseek($this->stderr, 0, SEEK_END);
  808. fwrite($this->stderr, $line);
  809. fseek($this->stderr, $this->incrementalErrorOutputOffset);
  810. }
  811. /**
  812. * Gets the command line to be executed.
  813. *
  814. * @return string The command to execute
  815. */
  816. public function getCommandLine()
  817. {
  818. return is_array($this->commandline) ? implode(' ', array_map(array($this, 'escapeArgument'), $this->commandline)) : $this->commandline;
  819. }
  820. /**
  821. * Sets the command line to be executed.
  822. *
  823. * @param string|array $commandline The command to execute
  824. *
  825. * @return self The current Process instance
  826. */
  827. public function setCommandLine($commandline)
  828. {
  829. $this->commandline = $commandline;
  830. return $this;
  831. }
  832. /**
  833. * Gets the process timeout (max. runtime).
  834. *
  835. * @return float|null The timeout in seconds or null if it's disabled
  836. */
  837. public function getTimeout()
  838. {
  839. return $this->timeout;
  840. }
  841. /**
  842. * Gets the process idle timeout (max. time since last output).
  843. *
  844. * @return float|null The timeout in seconds or null if it's disabled
  845. */
  846. public function getIdleTimeout()
  847. {
  848. return $this->idleTimeout;
  849. }
  850. /**
  851. * Sets the process timeout (max. runtime).
  852. *
  853. * To disable the timeout, set this value to null.
  854. *
  855. * @param int|float|null $timeout The timeout in seconds
  856. *
  857. * @return self The current Process instance
  858. *
  859. * @throws InvalidArgumentException if the timeout is negative
  860. */
  861. public function setTimeout($timeout)
  862. {
  863. $this->timeout = $this->validateTimeout($timeout);
  864. return $this;
  865. }
  866. /**
  867. * Sets the process idle timeout (max. time since last output).
  868. *
  869. * To disable the timeout, set this value to null.
  870. *
  871. * @param int|float|null $timeout The timeout in seconds
  872. *
  873. * @return self The current Process instance
  874. *
  875. * @throws LogicException if the output is disabled
  876. * @throws InvalidArgumentException if the timeout is negative
  877. */
  878. public function setIdleTimeout($timeout)
  879. {
  880. if (null !== $timeout && $this->outputDisabled) {
  881. throw new LogicException('Idle timeout can not be set while the output is disabled.');
  882. }
  883. $this->idleTimeout = $this->validateTimeout($timeout);
  884. return $this;
  885. }
  886. /**
  887. * Enables or disables the TTY mode.
  888. *
  889. * @param bool $tty True to enabled and false to disable
  890. *
  891. * @return self The current Process instance
  892. *
  893. * @throws RuntimeException In case the TTY mode is not supported
  894. */
  895. public function setTty($tty)
  896. {
  897. if ('\\' === DIRECTORY_SEPARATOR && $tty) {
  898. throw new RuntimeException('TTY mode is not supported on Windows platform.');
  899. }
  900. if ($tty) {
  901. static $isTtySupported;
  902. if (null === $isTtySupported) {
  903. $isTtySupported = (bool) @proc_open('echo 1 >/dev/null', array(array('file', '/dev/tty', 'r'), array('file', '/dev/tty', 'w'), array('file', '/dev/tty', 'w')), $pipes);
  904. }
  905. if (!$isTtySupported) {
  906. throw new RuntimeException('TTY mode requires /dev/tty to be read/writable.');
  907. }
  908. }
  909. $this->tty = (bool) $tty;
  910. return $this;
  911. }
  912. /**
  913. * Checks if the TTY mode is enabled.
  914. *
  915. * @return bool true if the TTY mode is enabled, false otherwise
  916. */
  917. public function isTty()
  918. {
  919. return $this->tty;
  920. }
  921. /**
  922. * Sets PTY mode.
  923. *
  924. * @param bool $bool
  925. *
  926. * @return self
  927. */
  928. public function setPty($bool)
  929. {
  930. $this->pty = (bool) $bool;
  931. return $this;
  932. }
  933. /**
  934. * Returns PTY state.
  935. *
  936. * @return bool
  937. */
  938. public function isPty()
  939. {
  940. return $this->pty;
  941. }
  942. /**
  943. * Gets the working directory.
  944. *
  945. * @return string|null The current working directory or null on failure
  946. */
  947. public function getWorkingDirectory()
  948. {
  949. if (null === $this->cwd) {
  950. // getcwd() will return false if any one of the parent directories does not have
  951. // the readable or search mode set, even if the current directory does
  952. return getcwd() ?: null;
  953. }
  954. return $this->cwd;
  955. }
  956. /**
  957. * Sets the current working directory.
  958. *
  959. * @param string $cwd The new working directory
  960. *
  961. * @return self The current Process instance
  962. */
  963. public function setWorkingDirectory($cwd)
  964. {
  965. $this->cwd = $cwd;
  966. return $this;
  967. }
  968. /**
  969. * Gets the environment variables.
  970. *
  971. * @return array The current environment variables
  972. */
  973. public function getEnv()
  974. {
  975. return $this->env;
  976. }
  977. /**
  978. * Sets the environment variables.
  979. *
  980. * An environment variable value should be a string.
  981. * If it is an array, the variable is ignored.
  982. * If it is false or null, it will be removed when
  983. * env vars are otherwise inherited.
  984. *
  985. * That happens in PHP when 'argv' is registered into
  986. * the $_ENV array for instance.
  987. *
  988. * @param array $env The new environment variables
  989. *
  990. * @return self The current Process instance
  991. */
  992. public function setEnv(array $env)
  993. {
  994. // Process can not handle env values that are arrays
  995. $env = array_filter($env, function ($value) {
  996. return !is_array($value);
  997. });
  998. $this->env = $env;
  999. return $this;
  1000. }
  1001. /**
  1002. * Gets the Process input.
  1003. *
  1004. * @return resource|string|\Iterator|null The Process input
  1005. */
  1006. public function getInput()
  1007. {
  1008. return $this->input;
  1009. }
  1010. /**
  1011. * Sets the input.
  1012. *
  1013. * This content will be passed to the underlying process standard input.
  1014. *
  1015. * @param resource|scalar|\Traversable|null $input The content
  1016. *
  1017. * @return self The current Process instance
  1018. *
  1019. * @throws LogicException In case the process is running
  1020. */
  1021. public function setInput($input)
  1022. {
  1023. if ($this->isRunning()) {
  1024. throw new LogicException('Input can not be set while the process is running.');
  1025. }
  1026. $this->input = ProcessUtils::validateInput(__METHOD__, $input);
  1027. return $this;
  1028. }
  1029. /**
  1030. * Gets the options for proc_open.
  1031. *
  1032. * @return array The current options
  1033. *
  1034. * @deprecated since version 3.3, to be removed in 4.0.
  1035. */
  1036. public function getOptions()
  1037. {
  1038. @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
  1039. return $this->options;
  1040. }
  1041. /**
  1042. * Sets the options for proc_open.
  1043. *
  1044. * @param array $options The new options
  1045. *
  1046. * @return self The current Process instance
  1047. *
  1048. * @deprecated since version 3.3, to be removed in 4.0.
  1049. */
  1050. public function setOptions(array $options)
  1051. {
  1052. @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
  1053. $this->options = $options;
  1054. return $this;
  1055. }
  1056. /**
  1057. * Gets whether or not Windows compatibility is enabled.
  1058. *
  1059. * This is true by default.
  1060. *
  1061. * @return bool
  1062. *
  1063. * @deprecated since version 3.3, to be removed in 4.0. Enhanced Windows compatibility will always be enabled.
  1064. */
  1065. public function getEnhanceWindowsCompatibility()
  1066. {
  1067. @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Enhanced Windows compatibility will always be enabled.', __METHOD__), E_USER_DEPRECATED);
  1068. return $this->enhanceWindowsCompatibility;
  1069. }
  1070. /**
  1071. * Sets whether or not Windows compatibility is enabled.
  1072. *
  1073. * @param bool $enhance
  1074. *
  1075. * @return self The current Process instance
  1076. *
  1077. * @deprecated since version 3.3, to be removed in 4.0. Enhanced Windows compatibility will always be enabled.
  1078. */
  1079. public function setEnhanceWindowsCompatibility($enhance)
  1080. {
  1081. @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Enhanced Windows compatibility will always be enabled.', __METHOD__), E_USER_DEPRECATED);
  1082. $this->enhanceWindowsCompatibility = (bool) $enhance;
  1083. return $this;
  1084. }
  1085. /**
  1086. * Returns whether sigchild compatibility mode is activated or not.
  1087. *
  1088. * @return bool
  1089. *
  1090. * @deprecated since version 3.3, to be removed in 4.0. Sigchild compatibility will always be enabled.
  1091. */
  1092. public function getEnhanceSigchildCompatibility()
  1093. {
  1094. @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Sigchild compatibility will always be enabled.', __METHOD__), E_USER_DEPRECATED);
  1095. return $this->enhanceSigchildCompatibility;
  1096. }
  1097. /**
  1098. * Activates sigchild compatibility mode.
  1099. *
  1100. * Sigchild compatibility mode is required to get the exit code and
  1101. * determine the success of a process when PHP has been compiled with
  1102. * the --enable-sigchild option
  1103. *
  1104. * @param bool $enhance
  1105. *
  1106. * @return self The current Process instance
  1107. *
  1108. * @deprecated since version 3.3, to be removed in 4.0.
  1109. */
  1110. public function setEnhanceSigchildCompatibility($enhance)
  1111. {
  1112. @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Sigchild compatibility will always be enabled.', __METHOD__), E_USER_DEPRECATED);
  1113. $this->enhanceSigchildCompatibility = (bool) $enhance;
  1114. return $this;
  1115. }
  1116. /**
  1117. * Sets whether environment variables will be inherited or not.
  1118. *
  1119. * @param bool $inheritEnv
  1120. *
  1121. * @return self The current Process instance
  1122. */
  1123. public function inheritEnvironmentVariables($inheritEnv = true)
  1124. {
  1125. if (!$inheritEnv) {
  1126. @trigger_error('Not inheriting environment variables is deprecated since Symfony 3.3 and will always happen in 4.0. Set "Process::inheritEnvironmentVariables()" to true instead.', E_USER_DEPRECATED);
  1127. }
  1128. $this->inheritEnv = (bool) $inheritEnv;
  1129. return $this;
  1130. }
  1131. /**
  1132. * Returns whether environment variables will be inherited or not.
  1133. *
  1134. * @return bool
  1135. *
  1136. * @deprecated since version 3.3, to be removed in 4.0. Environment variables will always be inherited.
  1137. */
  1138. public function areEnvironmentVariablesInherited()
  1139. {
  1140. @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Environment variables will always be inherited.', __METHOD__), E_USER_DEPRECATED);
  1141. return $this->inheritEnv;
  1142. }
  1143. /**
  1144. * Performs a check between the timeout definition and the time the process started.
  1145. *
  1146. * In case you run a background process (with the start method), you should
  1147. * trigger this method regularly to ensure the process timeout
  1148. *
  1149. * @throws ProcessTimedOutException In case the timeout was reached
  1150. */
  1151. public function checkTimeout()
  1152. {
  1153. if (self::STATUS_STARTED !== $this->status) {
  1154. return;
  1155. }
  1156. if (null !== $this->timeout && $this->timeout < microtime(true) - $this->starttime) {
  1157. $this->stop(0);
  1158. throw new ProcessTimedOutException($this, ProcessTimedOutException::TYPE_GENERAL);
  1159. }
  1160. if (null !== $this->idleTimeout && $this->idleTimeout < microtime(true) - $this->lastOutputTime) {
  1161. $this->stop(0);
  1162. throw new ProcessTimedOutException($this, ProcessTimedOutException::TYPE_IDLE);
  1163. }
  1164. }
  1165. /**
  1166. * Returns whether PTY is supported on the current operating system.
  1167. *
  1168. * @return bool
  1169. */
  1170. public static function isPtySupported()
  1171. {
  1172. static $result;
  1173. if (null !== $result) {
  1174. return $result;
  1175. }
  1176. if ('\\' === DIRECTORY_SEPARATOR) {
  1177. return $result = false;
  1178. }
  1179. return $result = (bool) @proc_open('echo 1 >/dev/null', array(array('pty'), array('pty'), array('pty')), $pipes);
  1180. }
  1181. /**
  1182. * Creates the descriptors needed by the proc_open.
  1183. *
  1184. * @return array
  1185. */
  1186. private function getDescriptors()
  1187. {
  1188. if ($this->input instanceof \Iterator) {
  1189. $this->input->rewind();
  1190. }
  1191. if ('\\' === DIRECTORY_SEPARATOR) {
  1192. $this->processPipes = new WindowsPipes($this->input, !$this->outputDisabled || $this->hasCallback);
  1193. } else {
  1194. $this->processPipes = new UnixPipes($this->isTty(), $this->isPty(), $this->input, !$this->outputDisabled || $this->hasCallback);
  1195. }
  1196. return $this->processPipes->getDescriptors();
  1197. }
  1198. /**
  1199. * Builds up the callback used by wait().
  1200. *
  1201. * The callbacks adds all occurred output to the specific buffer and calls
  1202. * the user callback (if present) with the received output.
  1203. *
  1204. * @param callable|null $callback The user defined PHP callback
  1205. *
  1206. * @return \Closure A PHP closure
  1207. */
  1208. protected function buildCallback(callable $callback = null)
  1209. {
  1210. if ($this->outputDisabled) {
  1211. return function ($type, $data) use ($callback) {
  1212. if (null !== $callback) {
  1213. call_user_func($callback, $type, $data);
  1214. }
  1215. };
  1216. }
  1217. $out = self::OUT;
  1218. return function ($type, $data) use ($callback, $out) {
  1219. if ($out == $type) {
  1220. $this->addOutput($data);
  1221. } else {
  1222. $this->addErrorOutput($data);
  1223. }
  1224. if (null !== $callback) {
  1225. call_user_func($callback, $type, $data);
  1226. }
  1227. };
  1228. }
  1229. /**
  1230. * Updates the status of the process, reads pipes.
  1231. *
  1232. * @param bool $blocking Whether to use a blocking read call
  1233. */
  1234. protected function updateStatus($blocking)
  1235. {
  1236. if (self::STATUS_STARTED !== $this->status) {
  1237. return;
  1238. }
  1239. $this->processInformation = proc_get_status($this->process);
  1240. $running = $this->processInformation['running'];
  1241. $this->readPipes($running && $blocking, '\\' !== DIRECTORY_SEPARATOR || !$running);
  1242. if ($this->fallbackStatus && $this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
  1243. $this->processInformation = $this->fallbackStatus + $this->processInformation;
  1244. }
  1245. if (!$running) {
  1246. $this->close();
  1247. }
  1248. }
  1249. /**
  1250. * Returns whether PHP has been compiled with the '--enable-sigchild' option or not.
  1251. *
  1252. * @return bool
  1253. */
  1254. protected function isSigchildEnabled()
  1255. {
  1256. if (null !== self::$sigchild) {
  1257. return self::$sigchild;
  1258. }
  1259. if (!function_exists('phpinfo') || defined('HHVM_VERSION')) {
  1260. return self::$sigchild = false;
  1261. }
  1262. ob_start();
  1263. phpinfo(INFO_GENERAL);
  1264. return self::$sigchild = false !== strpos(ob_get_clean(), '--enable-sigchild');
  1265. }
  1266. /**
  1267. * Reads pipes for the freshest output.
  1268. *
  1269. * @param string $caller The name of the method that needs fresh outputs
  1270. * @param bool $blocking Whether to use blocking calls or not
  1271. *
  1272. * @throws LogicException in case output has been disabled or process is not started
  1273. */
  1274. private function readPipesForOutput($caller, $blocking = false)
  1275. {
  1276. if ($this->outputDisabled) {
  1277. throw new LogicException('Output has been disabled.');
  1278. }
  1279. $this->requireProcessIsStarted($caller);
  1280. $this->updateStatus($blocking);
  1281. }
  1282. /**
  1283. * Validates and returns the filtered timeout.
  1284. *
  1285. * @param int|float|null $timeout
  1286. *
  1287. * @return float|null
  1288. *
  1289. * @throws InvalidArgumentException if the given timeout is a negative number
  1290. */
  1291. private function validateTimeout($timeout)
  1292. {
  1293. $timeout = (float) $timeout;
  1294. if (0.0 === $timeout) {
  1295. $timeout = null;
  1296. } elseif ($timeout < 0) {
  1297. throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
  1298. }
  1299. return $timeout;
  1300. }
  1301. /**
  1302. * Reads pipes, executes callback.
  1303. *
  1304. * @param bool $blocking Whether to use blocking calls or not
  1305. * @param bool $close Whether to close file handles or not
  1306. */
  1307. private function readPipes($blocking, $close)
  1308. {
  1309. $result = $this->processPipes->readAndWrite($blocking, $close);
  1310. $callback = $this->callback;
  1311. foreach ($result as $type => $data) {
  1312. if (3 !== $type) {
  1313. $callback(self::STDOUT === $type ? self::OUT : self::ERR, $data);
  1314. } elseif (!isset($this->fallbackStatus['signaled'])) {
  1315. $this->fallbackStatus['exitcode'] = (int) $data;
  1316. }
  1317. }
  1318. }
  1319. /**
  1320. * Closes process resource, closes file handles, sets the exitcode.
  1321. *
  1322. * @return int The exitcode
  1323. */
  1324. private function close()
  1325. {
  1326. $this->processPipes->close();
  1327. if (is_resource($this->process)) {
  1328. proc_close($this->process);
  1329. }
  1330. $this->exitcode = $this->processInformation['exitcode'];
  1331. $this->status = self::STATUS_TERMINATED;
  1332. if (-1 === $this->exitcode) {
  1333. if ($this->processInformation['signaled'] && 0 < $this->processInformation['termsig']) {
  1334. // if process has been signaled, no exitcode but a valid termsig, apply Unix convention
  1335. $this->exitcode = 128 + $this->processInformation['termsig'];
  1336. } elseif ($this->enhanceSigchildCompatibility && $this->isSigchildEnabled()) {
  1337. $this->processInformation['signaled'] = true;
  1338. $this->processInformation['termsig'] = -1;
  1339. }
  1340. }
  1341. // Free memory from self-reference callback created by buildCallback
  1342. // Doing so in other contexts like __destruct or by garbage collector is ineffective
  1343. // Now pipes are closed, so the callback is no longer necessary
  1344. $this->callback = null;
  1345. return $this->exitcode;
  1346. }
  1347. /**
  1348. * Resets data related to the latest run of the process.
  1349. */
  1350. private function resetProcessData()
  1351. {
  1352. $this->starttime = null;
  1353. $this->callback = null