WindowsPipes.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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(mixed $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 (!($h = fopen($file, 'w')) || !fclose($h) || !$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 __sleep(): array
  82. {
  83. throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  84. }
  85. public function __wakeup()
  86. {
  87. throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  88. }
  89. public function __destruct()
  90. {
  91. $this->close();
  92. }
  93. public function getDescriptors(): array
  94. {
  95. if (!$this->haveReadSupport) {
  96. $nullstream = fopen('NUL', 'c');
  97. return [
  98. ['pipe', 'r'],
  99. $nullstream,
  100. $nullstream,
  101. ];
  102. }
  103. // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/51800)
  104. // We're not using file handles as it can produce corrupted output https://bugs.php.net/65650
  105. // So we redirect output within the commandline and pass the nul device to the process
  106. return [
  107. ['pipe', 'r'],
  108. ['file', 'NUL', 'w'],
  109. ['file', 'NUL', 'w'],
  110. ];
  111. }
  112. public function getFiles(): array
  113. {
  114. return $this->files;
  115. }
  116. public function readAndWrite(bool $blocking, bool $close = false): array
  117. {
  118. $this->unblock();
  119. $w = $this->write();
  120. $read = $r = $e = [];
  121. if ($blocking) {
  122. if ($w) {
  123. @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
  124. } elseif ($this->fileHandles) {
  125. usleep(Process::TIMEOUT_PRECISION * 1E6);
  126. }
  127. }
  128. foreach ($this->fileHandles as $type => $fileHandle) {
  129. $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
  130. if (isset($data[0])) {
  131. $this->readBytes[$type] += \strlen($data);
  132. $read[$type] = $data;
  133. }
  134. if ($close) {
  135. ftruncate($fileHandle, 0);
  136. fclose($fileHandle);
  137. flock($this->lockHandles[$type], \LOCK_UN);
  138. fclose($this->lockHandles[$type]);
  139. unset($this->fileHandles[$type], $this->lockHandles[$type]);
  140. }
  141. }
  142. return $read;
  143. }
  144. public function haveReadSupport(): bool
  145. {
  146. return $this->haveReadSupport;
  147. }
  148. public function areOpen(): bool
  149. {
  150. return $this->pipes && $this->fileHandles;
  151. }
  152. public function close()
  153. {
  154. parent::close();
  155. foreach ($this->fileHandles as $type => $handle) {
  156. ftruncate($handle, 0);
  157. fclose($handle);
  158. flock($this->lockHandles[$type], \LOCK_UN);
  159. fclose($this->lockHandles[$type]);
  160. }
  161. $this->fileHandles = $this->lockHandles = [];
  162. }
  163. }