SignalHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Psr\Log\LoggerInterface;
  12. use Psr\Log\LogLevel;
  13. use ReflectionExtension;
  14. /**
  15. * Monolog POSIX signal handler
  16. *
  17. * @author Robert Gust-Bardon <robert@gust-bardon.org>
  18. */
  19. class SignalHandler
  20. {
  21. private $logger;
  22. private $previousSignalHandler = array();
  23. private $signalLevelMap = array();
  24. private $signalRestartSyscalls = array();
  25. public function __construct(LoggerInterface $logger)
  26. {
  27. $this->logger = $logger;
  28. }
  29. public function registerSignalHandler($signo, $level = LogLevel::CRITICAL, $callPrevious = true, $restartSyscalls = true, $async = true)
  30. {
  31. if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) {
  32. return $this;
  33. }
  34. if ($callPrevious) {
  35. if (function_exists('pcntl_signal_get_handler')) {
  36. $handler = pcntl_signal_get_handler($signo);
  37. if ($handler === false) {
  38. return $this;
  39. }
  40. $this->previousSignalHandler[$signo] = $handler;
  41. } else {
  42. $this->previousSignalHandler[$signo] = true;
  43. }
  44. } else {
  45. unset($this->previousSignalHandler[$signo]);
  46. }
  47. $this->signalLevelMap[$signo] = $level;
  48. $this->signalRestartSyscalls[$signo] = $restartSyscalls;
  49. if (function_exists('pcntl_async_signals') && $async !== null) {
  50. pcntl_async_signals($async);
  51. }
  52. pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls);
  53. return $this;
  54. }
  55. public function handleSignal($signo, array $siginfo = null)
  56. {
  57. static $signals = array();
  58. if (!$signals && extension_loaded('pcntl')) {
  59. $pcntl = new ReflectionExtension('pcntl');
  60. $constants = $pcntl->getConstants();
  61. if (!$constants) {
  62. // HHVM 3.24.2 returns an empty array.
  63. $constants = get_defined_constants(true);
  64. $constants = $constants['Core'];
  65. }
  66. foreach ($constants as $name => $value) {
  67. if (substr($name, 0, 3) === 'SIG' && $name[3] !== '_' && is_int($value)) {
  68. $signals[$value] = $name;
  69. }
  70. }
  71. unset($constants);
  72. }
  73. $level = isset($this->signalLevelMap[$signo]) ? $this->signalLevelMap[$signo] : LogLevel::CRITICAL;
  74. $signal = isset($signals[$signo]) ? $signals[$signo] : $signo;
  75. $context = isset($siginfo) ? $siginfo : array();
  76. $this->logger->log($level, sprintf('Program received signal %s', $signal), $context);
  77. if (!isset($this->previousSignalHandler[$signo])) {
  78. return;
  79. }
  80. if ($this->previousSignalHandler[$signo] === true || $this->previousSignalHandler[$signo] === SIG_DFL) {
  81. if (extension_loaded('pcntl') && function_exists('pcntl_signal') && function_exists('pcntl_sigprocmask') && function_exists('pcntl_signal_dispatch')
  82. && extension_loaded('posix') && function_exists('posix_getpid') && function_exists('posix_kill')) {
  83. $restartSyscalls = isset($this->signalRestartSyscalls[$signo]) ? $this->signalRestartSyscalls[$signo] : true;
  84. pcntl_signal($signo, SIG_DFL, $restartSyscalls);
  85. pcntl_sigprocmask(SIG_UNBLOCK, array($signo), $oldset);
  86. posix_kill(posix_getpid(), $signo);
  87. pcntl_signal_dispatch();
  88. pcntl_sigprocmask(SIG_SETMASK, $oldset);
  89. pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls);
  90. }
  91. } elseif (is_callable($this->previousSignalHandler[$signo])) {
  92. if (PHP_VERSION_ID >= 70100) {
  93. $this->previousSignalHandler[$signo]($signo, $siginfo);
  94. } else {
  95. $this->previousSignalHandler[$signo]($signo);
  96. }
  97. }
  98. }
  99. }