WindowsPipes.php 5.9 KB

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