Ingen beskrivning

WindowsPipes.php 5.9KB

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