Drupal investigation

WindowsPipes.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\Pipes;
  11. use Symfony\Component\Process\Process;
  12. use Symfony\Component\Process\Exception\RuntimeException;
  13. /**
  14. * WindowsPipes implementation uses temporary files as handles.
  15. *
  16. * @see https://bugs.php.net/bug.php?id=51800
  17. * @see https://bugs.php.net/bug.php?id=65650
  18. *
  19. * @author Romain Neutron <imprec@gmail.com>
  20. *
  21. * @internal
  22. */
  23. class WindowsPipes extends AbstractPipes
  24. {
  25. /** @var array */
  26. private $files = array();
  27. /** @var array */
  28. private $fileHandles = array();
  29. /** @var array */
  30. private $readBytes = array(
  31. Process::STDOUT => 0,
  32. Process::STDERR => 0,
  33. );
  34. /** @var bool */
  35. private $disableOutput;
  36. public function __construct($disableOutput, $input)
  37. {
  38. $this->disableOutput = (bool) $disableOutput;
  39. if (!$this->disableOutput) {
  40. // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
  41. // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
  42. //
  43. // @see https://bugs.php.net/bug.php?id=51800
  44. $pipes = array(
  45. Process::STDOUT => Process::OUT,
  46. Process::STDERR => Process::ERR,
  47. );
  48. $tmpCheck = false;
  49. $tmpDir = sys_get_temp_dir();
  50. $lastError = 'unknown reason';
  51. set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
  52. for ($i = 0;; ++$i) {
  53. foreach ($pipes as $pipe => $name) {
  54. $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
  55. if (file_exists($file) && !unlink($file)) {
  56. continue 2;
  57. }
  58. $h = fopen($file, 'xb');
  59. if (!$h) {
  60. $error = $lastError;
  61. if ($tmpCheck || $tmpCheck = unlink(tempnam(false, 'sf_check_'))) {
  62. continue;
  63. }
  64. restore_error_handler();
  65. throw new RuntimeException(sprintf('A temporary file could not be opened to write the process output: %s', $error));
  66. }
  67. if (!$h || !$this->fileHandles[$pipe] = fopen($file, 'rb')) {
  68. continue 2;
  69. }
  70. if (isset($this->files[$pipe])) {
  71. unlink($this->files[$pipe]);
  72. }
  73. $this->files[$pipe] = $file;
  74. }
  75. break;
  76. }
  77. restore_error_handler();
  78. }
  79. parent::__construct($input);
  80. }
  81. public function __destruct()
  82. {
  83. $this->close();
  84. $this->removeFiles();
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getDescriptors()
  90. {
  91. if ($this->disableOutput) {
  92. $nullstream = fopen('NUL', 'c');
  93. return array(
  94. array('pipe', 'r'),
  95. $nullstream,
  96. $nullstream,
  97. );
  98. }
  99. // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
  100. // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
  101. // So we redirect output within the commandline and pass the nul device to the process
  102. return array(
  103. array('pipe', 'r'),
  104. array('file', 'NUL', 'w'),
  105. array('file', 'NUL', 'w'),
  106. );
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getFiles()
  112. {
  113. return $this->files;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function readAndWrite($blocking, $close = false)
  119. {
  120. $this->unblock();
  121. $w = $this->write();
  122. $read = $r = $e = array();
  123. if ($blocking) {
  124. if ($w) {
  125. @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
  126. } elseif ($this->fileHandles) {
  127. usleep(Process::TIMEOUT_PRECISION * 1E6);
  128. }
  129. }
  130. foreach ($this->fileHandles as $type => $fileHandle) {
  131. $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
  132. if (isset($data[0])) {
  133. $this->readBytes[$type] += strlen($data);
  134. $read[$type] = $data;
  135. }
  136. if ($close) {
  137. fclose($fileHandle);
  138. unset($this->fileHandles[$type]);
  139. }
  140. }
  141. return $read;
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function areOpen()
  147. {
  148. return $this->pipes && $this->fileHandles;
  149. }
  150. /**
  151. * {@inheritdoc}
  152. */
  153. public function close()
  154. {
  155. parent::close();
  156. foreach ($this->fileHandles as $handle) {
  157. fclose($handle);
  158. }
  159. $this->fileHandles = array();
  160. }
  161. /**
  162. * Creates a new WindowsPipes instance.
  163. *
  164. * @param Process $process The process
  165. * @param $input
  166. *
  167. * @return static
  168. */
  169. public static function create(Process $process, $input)
  170. {
  171. return new static($process->isOutputDisabled(), $input);
  172. }
  173. /**
  174. * Removes temporary files.
  175. */
  176. private function removeFiles()
  177. {
  178. foreach ($this->files as $filename) {
  179. if (file_exists($filename)) {
  180. @unlink($filename);
  181. }
  182. }
  183. $this->files = array();
  184. }
  185. }