2
0

SignalHandlerTest.php 10 KB

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