SignalHandlerTest.php 10 KB

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