SignalHandlerTest.php 10 KB

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