SignalHandlerTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog;
  11. use Monolog\Handler\StreamHandler;
  12. use Monolog\Handler\TestHandler;
  13. use Psr\Log\LogLevel;
  14. use Monolog\Test\TestCase;
  15. /**
  16. * @author Robert Gust-Bardon <robert@gust-bardon.org>
  17. * @covers Monolog\SignalHandler
  18. */
  19. class SignalHandlerTest extends TestCase
  20. {
  21. private bool $asyncSignalHandling;
  22. private array $blockedSignals = [];
  23. private array $signalHandlers = [];
  24. protected function setUp(): void
  25. {
  26. $this->signalHandlers = [];
  27. if (extension_loaded('pcntl')) {
  28. if (function_exists('pcntl_async_signals')) {
  29. $this->asyncSignalHandling = pcntl_async_signals();
  30. }
  31. if (function_exists('pcntl_sigprocmask')) {
  32. pcntl_sigprocmask(SIG_BLOCK, [], $this->blockedSignals);
  33. }
  34. }
  35. }
  36. protected function tearDown(): void
  37. {
  38. if ($this->asyncSignalHandling !== null) {
  39. pcntl_async_signals($this->asyncSignalHandling);
  40. }
  41. if ($this->blockedSignals !== null) {
  42. pcntl_sigprocmask(SIG_SETMASK, $this->blockedSignals);
  43. }
  44. if ($this->signalHandlers) {
  45. pcntl_signal_dispatch();
  46. foreach ($this->signalHandlers as $signo => $handler) {
  47. pcntl_signal($signo, $handler);
  48. }
  49. }
  50. }
  51. private function setSignalHandler($signo, $handler = SIG_DFL)
  52. {
  53. if (function_exists('pcntl_signal_get_handler')) {
  54. $this->signalHandlers[$signo] = pcntl_signal_get_handler($signo);
  55. } else {
  56. $this->signalHandlers[$signo] = SIG_DFL;
  57. }
  58. $this->assertTrue(pcntl_signal($signo, $handler));
  59. }
  60. public function testHandleSignal()
  61. {
  62. $logger = new Logger('test', [$handler = new TestHandler]);
  63. $errHandler = new SignalHandler($logger);
  64. $signo = 2; // SIGINT.
  65. $siginfo = ['signo' => $signo, 'errno' => 0, 'code' => 0];
  66. $errHandler->handleSignal($signo, $siginfo);
  67. $this->assertCount(1, $handler->getRecords());
  68. $this->assertTrue($handler->hasCriticalRecords());
  69. $records = $handler->getRecords();
  70. $this->assertSame($siginfo, $records[0]['context']);
  71. }
  72. /**
  73. * @depends testHandleSignal
  74. * @requires extension pcntl
  75. * @requires extension posix
  76. * @requires function pcntl_signal
  77. * @requires function pcntl_signal_dispatch
  78. * @requires function posix_getpid
  79. * @requires function posix_kill
  80. */
  81. public function testRegisterSignalHandler()
  82. {
  83. // SIGCONT and SIGURG should be ignored by default.
  84. if (!defined('SIGCONT') || !defined('SIGURG')) {
  85. $this->markTestSkipped('This test requires the SIGCONT and SIGURG pcntl constants.');
  86. }
  87. $this->setSignalHandler(SIGCONT, SIG_IGN);
  88. $this->setSignalHandler(SIGURG, SIG_IGN);
  89. $logger = new Logger('test', [$handler = new TestHandler]);
  90. $errHandler = new SignalHandler($logger);
  91. $pid = posix_getpid();
  92. $this->assertTrue(posix_kill($pid, SIGURG));
  93. $this->assertTrue(pcntl_signal_dispatch());
  94. $this->assertCount(0, $handler->getRecords());
  95. $errHandler->registerSignalHandler(SIGURG, LogLevel::INFO, false, false, false);
  96. $this->assertTrue(posix_kill($pid, SIGCONT));
  97. $this->assertTrue(pcntl_signal_dispatch());
  98. $this->assertCount(0, $handler->getRecords());
  99. $this->assertTrue(posix_kill($pid, SIGURG));
  100. $this->assertTrue(pcntl_signal_dispatch());
  101. $this->assertCount(1, $handler->getRecords());
  102. $this->assertTrue($handler->hasInfoThatContains('SIGURG'));
  103. }
  104. /**
  105. * @dataProvider defaultPreviousProvider
  106. * @depends testRegisterSignalHandler
  107. * @requires function pcntl_fork
  108. * @requires function pcntl_sigprocmask
  109. * @requires function pcntl_waitpid
  110. */
  111. public function testRegisterDefaultPreviousSignalHandler($signo, $callPrevious, $expected)
  112. {
  113. $this->setSignalHandler($signo, SIG_DFL);
  114. $path = tempnam(sys_get_temp_dir(), 'monolog-');
  115. $this->assertNotFalse($path);
  116. $pid = pcntl_fork();
  117. if ($pid === 0) { // Child.
  118. $streamHandler = new StreamHandler($path);
  119. $streamHandler->setFormatter($this->getIdentityFormatter());
  120. $logger = new Logger('test', [$streamHandler]);
  121. $errHandler = new SignalHandler($logger);
  122. $errHandler->registerSignalHandler($signo, LogLevel::INFO, $callPrevious, false, false);
  123. pcntl_sigprocmask(SIG_SETMASK, [SIGCONT]);
  124. posix_kill(posix_getpid(), $signo);
  125. pcntl_signal_dispatch();
  126. // If $callPrevious is true, SIGINT should terminate by this line.
  127. pcntl_sigprocmask(SIG_BLOCK, [], $oldset);
  128. file_put_contents($path, implode(' ', $oldset), FILE_APPEND);
  129. posix_kill(posix_getpid(), $signo);
  130. pcntl_signal_dispatch();
  131. exit();
  132. }
  133. $this->assertNotSame(-1, $pid);
  134. $this->assertNotSame(-1, pcntl_waitpid($pid, $status));
  135. $this->assertNotSame(-1, $status);
  136. $this->assertSame($expected, file_get_contents($path));
  137. }
  138. public function defaultPreviousProvider()
  139. {
  140. if (!defined('SIGCONT') || !defined('SIGINT') || !defined('SIGURG')) {
  141. return [];
  142. }
  143. return [
  144. [SIGINT, false, 'Program received signal SIGINT'.SIGCONT.'Program received signal SIGINT'],
  145. [SIGINT, true, 'Program received signal SIGINT'],
  146. [SIGURG, false, 'Program received signal SIGURG'.SIGCONT.'Program received signal SIGURG'],
  147. [SIGURG, true, 'Program received signal SIGURG'.SIGCONT.'Program received signal SIGURG'],
  148. ];
  149. }
  150. /**
  151. * @dataProvider callablePreviousProvider
  152. * @depends testRegisterSignalHandler
  153. * @requires function pcntl_signal_get_handler
  154. */
  155. public function testRegisterCallablePreviousSignalHandler($callPrevious)
  156. {
  157. $this->setSignalHandler(SIGURG, SIG_IGN);
  158. $logger = new Logger('test', [$handler = new TestHandler]);
  159. $errHandler = new SignalHandler($logger);
  160. $previousCalled = 0;
  161. pcntl_signal(SIGURG, function ($signo, array $siginfo = null) use (&$previousCalled) {
  162. ++$previousCalled;
  163. });
  164. $errHandler->registerSignalHandler(SIGURG, LogLevel::INFO, $callPrevious, false, false);
  165. $this->assertTrue(posix_kill(posix_getpid(), SIGURG));
  166. $this->assertTrue(pcntl_signal_dispatch());
  167. $this->assertCount(1, $handler->getRecords());
  168. $this->assertTrue($handler->hasInfoThatContains('SIGURG'));
  169. $this->assertSame($callPrevious ? 1 : 0, $previousCalled);
  170. }
  171. public function callablePreviousProvider()
  172. {
  173. return [
  174. [false],
  175. [true],
  176. ];
  177. }
  178. /**
  179. * @dataProvider restartSyscallsProvider
  180. * @depends testRegisterDefaultPreviousSignalHandler
  181. * @requires function pcntl_fork
  182. * @requires function pcntl_waitpid
  183. */
  184. public function testRegisterSyscallRestartingSignalHandler($restartSyscalls)
  185. {
  186. $this->setSignalHandler(SIGURG, SIG_IGN);
  187. $parentPid = posix_getpid();
  188. $microtime = microtime(true);
  189. $pid = pcntl_fork();
  190. if ($pid === 0) { // Child.
  191. usleep(100000);
  192. posix_kill($parentPid, SIGURG);
  193. usleep(100000);
  194. exit();
  195. }
  196. $this->assertNotSame(-1, $pid);
  197. $logger = new Logger('test', [$handler = new TestHandler]);
  198. $errHandler = new SignalHandler($logger);
  199. $errHandler->registerSignalHandler(SIGURG, LogLevel::INFO, false, $restartSyscalls, false);
  200. if ($restartSyscalls) {
  201. // pcntl_wait is expected to be restarted after the signal handler.
  202. $this->assertNotSame(-1, pcntl_waitpid($pid, $status));
  203. } else {
  204. // pcntl_wait is expected to be interrupted when the signal handler is invoked.
  205. $this->assertSame(-1, pcntl_waitpid($pid, $status));
  206. }
  207. $this->assertSame($restartSyscalls, microtime(true) - $microtime > 0.15);
  208. $this->assertTrue(pcntl_signal_dispatch());
  209. $this->assertCount(1, $handler->getRecords());
  210. if ($restartSyscalls) {
  211. // The child has already exited.
  212. $this->assertSame(-1, pcntl_waitpid($pid, $status));
  213. } else {
  214. // The child has not exited yet.
  215. $this->assertNotSame(-1, pcntl_waitpid($pid, $status));
  216. }
  217. }
  218. public function restartSyscallsProvider()
  219. {
  220. return [
  221. [false],
  222. [true],
  223. [false],
  224. [true],
  225. ];
  226. }
  227. /**
  228. * @dataProvider asyncProvider
  229. * @depends testRegisterDefaultPreviousSignalHandler
  230. * @requires function pcntl_async_signals
  231. */
  232. public function testRegisterAsyncSignalHandler($initialAsync, $desiredAsync, $expectedBefore, $expectedAfter)
  233. {
  234. $this->setSignalHandler(SIGURG, SIG_IGN);
  235. pcntl_async_signals($initialAsync);
  236. $logger = new Logger('test', [$handler = new TestHandler]);
  237. $errHandler = new SignalHandler($logger);
  238. $errHandler->registerSignalHandler(SIGURG, LogLevel::INFO, false, false, $desiredAsync);
  239. $this->assertTrue(posix_kill(posix_getpid(), SIGURG));
  240. $this->assertCount($expectedBefore, $handler->getRecords());
  241. $this->assertTrue(pcntl_signal_dispatch());
  242. $this->assertCount($expectedAfter, $handler->getRecords());
  243. }
  244. public function asyncProvider()
  245. {
  246. return [
  247. [false, false, 0, 1],
  248. [false, null, 0, 1],
  249. [false, true, 1, 1],
  250. [true, false, 0, 1],
  251. [true, null, 1, 1],
  252. [true, true, 1, 1],
  253. ];
  254. }
  255. }