27 private $previousSignalHandler = array();
28 private $signalLevelMap = array();
29 private $signalRestartSyscalls = array();
33 $this->logger = $logger;
36 public function registerSignalHandler($signo, $level = LogLevel::CRITICAL, $callPrevious =
true, $restartSyscalls =
true, $async =
true)
38 if (!extension_loaded(
'pcntl') || !function_exists(
'pcntl_signal')) {
43 if (function_exists(
'pcntl_signal_get_handler')) {
44 $handler = pcntl_signal_get_handler($signo);
48 $this->previousSignalHandler[$signo] =
$handler;
50 $this->previousSignalHandler[$signo] =
true;
53 unset($this->previousSignalHandler[$signo]);
55 $this->signalLevelMap[$signo] = $level;
56 $this->signalRestartSyscalls[$signo] = $restartSyscalls;
58 if (function_exists(
'pcntl_async_signals') && $async !== null) {
59 pcntl_async_signals($async);
62 pcntl_signal($signo, array($this,
'handleSignal'), $restartSyscalls);
69 static $signals = array();
71 if (!$signals && extension_loaded(
'pcntl')) {
73 $constants = $pcntl->getConstants();
76 $constants = get_defined_constants(
true);
77 $constants = $constants[
'Core'];
79 foreach ($constants as
$name => $value) {
80 if (substr(
$name, 0, 3) ===
'SIG' &&
$name[3] !==
'_' && is_int($value)) {
81 $signals[$value] =
$name;
87 $level = isset($this->signalLevelMap[$signo]) ? $this->signalLevelMap[$signo] : LogLevel::CRITICAL;
88 $signal = isset($signals[$signo]) ? $signals[$signo] : $signo;
89 $context = isset($siginfo) ? $siginfo : array();
90 $this->logger->log($level, sprintf(
'Program received signal %s', $signal),
$context);
92 if (!isset($this->previousSignalHandler[$signo])) {
96 if ($this->previousSignalHandler[$signo] ===
true || $this->previousSignalHandler[$signo] === SIG_DFL) {
97 if (extension_loaded(
'pcntl') && function_exists(
'pcntl_signal') && function_exists(
'pcntl_sigprocmask') && function_exists(
'pcntl_signal_dispatch')
98 && extension_loaded(
'posix') && function_exists(
'posix_getpid') && function_exists(
'posix_kill')) {
99 $restartSyscalls = isset($this->restartSyscalls[$signo]) ? $this->restartSyscalls[$signo] :
true;
100 pcntl_signal($signo, SIG_DFL, $restartSyscalls);
101 pcntl_sigprocmask(SIG_UNBLOCK, array($signo), $oldset);
102 posix_kill(posix_getpid(), $signo);
103 pcntl_signal_dispatch();
104 pcntl_sigprocmask(SIG_SETMASK, $oldset);
105 pcntl_signal($signo, array($this,
'handleSignal'), $restartSyscalls);
107 } elseif (is_callable($this->previousSignalHandler[$signo])) {
108 if (PHP_VERSION_ID >= 70100) {
109 $this->previousSignalHandler[$signo]($signo, $siginfo);
111 $this->previousSignalHandler[$signo]($signo);
handleSignal($signo, array $siginfo=null)
__construct(LoggerInterface $logger)
registerSignalHandler($signo, $level=LogLevel::CRITICAL, $callPrevious=true, $restartSyscalls=true, $async=true)
Describes a logger instance.
Monolog POSIX signal handler.