WindowsPipes.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. if (file_exists($file.'.lock')) {
  53. continue 2;
  54. }
  55. restore_error_handler();
  56. throw new RuntimeException('A temporary file could not be opened to write the process output: '.$lastError);
  57. }
  58. if (!flock($h, LOCK_EX | LOCK_NB)) {
  59. continue 2;
  60. }
  61. if (isset($this->lockHandles[$pipe])) {
  62. flock($this->lockHandles[$pipe], LOCK_UN);
  63. fclose($this->lockHandles[$pipe]);
  64. }
  65. $this->lockHandles[$pipe] = $h;
  66. if (!fclose(fopen($file, 'w')) || !$h = fopen($file, 'r')) {
  67. flock($this->lockHandles[$pipe], LOCK_UN);
  68. fclose($this->lockHandles[$pipe]);
  69. unset($this->lockHandles[$pipe]);
  70. continue 2;
  71. }
  72. $this->fileHandles[$pipe] = $h;
  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. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getDescriptors(): array
  89. {
  90. if (!$this->haveReadSupport) {
  91. $nullstream = fopen('NUL', 'c');
  92. return [
  93. ['pipe', 'r'],
  94. $nullstream,
  95. $nullstream,
  96. ];
  97. }
  98. // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/51800)
  99. // We're not using file handles as it can produce corrupted output https://bugs.php.net/65650
  100. // So we redirect output within the commandline and pass the nul device to the process
  101. return [
  102. ['pipe', 'r'],
  103. ['file', 'NUL', 'w'],
  104. ['file', 'NUL', 'w'],
  105. ];
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getFiles(): array
  111. {
  112. return $this->files;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function readAndWrite(bool $blocking, bool $close = false): array
  118. {
  119. $this->unblock();
  120. $w = $this->write();
  121. $read = $r = $e = [];
  122. if ($blocking) {
  123. if ($w) {
  124. @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
  125. } elseif ($this->fileHandles) {
  126. usleep(Process::TIMEOUT_PRECISION * 1E6);
  127. }
  128. }
  129. foreach ($this->fileHandles as $type => $fileHandle) {
  130. $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
  131. if (isset($data[0])) {
  132. $this->readBytes[$type] += \strlen($data);
  133. $read[$type] = $data;
  134. }
  135. if ($close) {
  136. ftruncate($fileHandle, 0);
  137. fclose($fileHandle);
  138. flock($this->lockHandles[$type], LOCK_UN);
  139. fclose($this->lockHandles[$type]);
  140. unset($this->fileHandles[$type], $this->lockHandles[$type]);
  141. }
  142. }
  143. return $read;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function haveReadSupport(): bool
  149. {
  150. return $this->haveReadSupport;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function areOpen(): bool
  156. {
  157. return $this->pipes && $this->fileHandles;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function close()
  163. {
  164. parent::close();
  165. foreach ($this->fileHandles as $type => $handle) {
  166. ftruncate($handle, 0);
  167. fclose($handle);
  168. flock($this->lockHandles[$type], LOCK_UN);
  169. fclose($this->lockHandles[$type]);
  170. }
  171. $this->fileHandles = $this->lockHandles = [];
  172. }
  173. }