SignalHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 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. * @phpstan-import-type Level from \Monolog\Logger
  20. * @phpstan-import-type LevelName from \Monolog\Logger
  21. */
  22. class SignalHandler
  23. {
  24. /** @var LoggerInterface */
  25. private $logger;
  26. /** @var array<int, callable|string|int> SIG_DFL, SIG_IGN or previous callable */
  27. private $previousSignalHandler = [];
  28. /** @var array<int, int> */
  29. private $signalLevelMap = [];
  30. /** @var array<int, bool> */
  31. private $signalRestartSyscalls = [];
  32. public function __construct(LoggerInterface $logger)
  33. {
  34. $this->logger = $logger;
  35. }
  36. /**
  37. * @param int|string $level Level or level name
  38. * @param bool $callPrevious
  39. * @param bool $restartSyscalls
  40. * @param bool|null $async
  41. * @return $this
  42. *
  43. * @phpstan-param Level|LevelName|LogLevel::* $level
  44. */
  45. public function registerSignalHandler(int $signo, $level = LogLevel::CRITICAL, bool $callPrevious = true, bool $restartSyscalls = true, ?bool $async = true): self
  46. {
  47. if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) {
  48. return $this;
  49. }
  50. $level = Logger::toMonologLevel($level);
  51. if ($callPrevious) {
  52. $handler = pcntl_signal_get_handler($signo);
  53. $this->previousSignalHandler[$signo] = $handler;
  54. } else {
  55. unset($this->previousSignalHandler[$signo]);
  56. }
  57. $this->signalLevelMap[$signo] = $level;
  58. $this->signalRestartSyscalls[$signo] = $restartSyscalls;
  59. if ($async !== null) {
  60. pcntl_async_signals($async);
  61. }
  62. pcntl_signal($signo, [$this, 'handleSignal'], $restartSyscalls);
  63. return $this;
  64. }
  65. /**
  66. * @param mixed $siginfo
  67. */
  68. public function handleSignal(int $signo, $siginfo = null): void
  69. {
  70. static $signals = [];
  71. if (!$signals && extension_loaded('pcntl')) {
  72. $pcntl = new ReflectionExtension('pcntl');
  73. // HHVM 3.24.2 returns an empty array.
  74. foreach ($pcntl->getConstants() ?: get_defined_constants(true)['Core'] as $name => $value) {
  75. if (substr($name, 0, 3) === 'SIG' && $name[3] !== '_' && is_int($value)) {
  76. $signals[$value] = $name;
  77. }
  78. }
  79. }
  80. $level = $this->signalLevelMap[$signo] ?? LogLevel::CRITICAL;
  81. $signal = $signals[$signo] ?? $signo;
  82. $context = $siginfo ?? [];
  83. $this->logger->log($level, sprintf('Program received signal %s', $signal), $context);
  84. if (!isset($this->previousSignalHandler[$signo])) {
  85. return;
  86. }
  87. if ($this->previousSignalHandler[$signo] === SIG_DFL) {
  88. if (extension_loaded('pcntl') && function_exists('pcntl_signal') && function_exists('pcntl_sigprocmask') && function_exists('pcntl_signal_dispatch')
  89. && extension_loaded('posix') && function_exists('posix_getpid') && function_exists('posix_kill')
  90. ) {
  91. $restartSyscalls = $this->signalRestartSyscalls[$signo] ?? true;
  92. pcntl_signal($signo, SIG_DFL, $restartSyscalls);
  93. pcntl_sigprocmask(SIG_UNBLOCK, [$signo], $oldset);
  94. posix_kill(posix_getpid(), $signo);
  95. pcntl_signal_dispatch();
  96. pcntl_sigprocmask(SIG_SETMASK, $oldset);
  97. pcntl_signal($signo, [$this, 'handleSignal'], $restartSyscalls);
  98. }
  99. } elseif (is_callable($this->previousSignalHandler[$signo])) {
  100. $this->previousSignalHandler[$signo]($signo, $siginfo);
  101. }
  102. }
  103. }