WindowsPipes.php 5.5 KB

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