1<?php
2
3/*
4 * This file is part of the Monolog package.
5 *
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Monolog;
13
14use Psr\Log\LoggerInterface;
15use Psr\Log\LogLevel;
16use ReflectionExtension;
17
18/**
19 * Monolog POSIX signal handler
20 *
21 * @author Robert Gust-Bardon <robert@gust-bardon.org>
22 */
23class SignalHandler
24{
25    private $logger;
26
27    private $previousSignalHandler = array();
28    private $signalLevelMap = array();
29    private $signalRestartSyscalls = array();
30
31    public function __construct(LoggerInterface $logger)
32    {
33        $this->logger = $logger;
34    }
35
36    public function registerSignalHandler($signo, $level = LogLevel::CRITICAL, $callPrevious = true, $restartSyscalls = true, $async = true)
37    {
38        if (!extension_loaded('pcntl') || !function_exists('pcntl_signal')) {
39            return $this;
40        }
41
42        if ($callPrevious) {
43            if (function_exists('pcntl_signal_get_handler')) {
44                $handler = pcntl_signal_get_handler($signo);
45                if ($handler === false) {
46                    return $this;
47                }
48                $this->previousSignalHandler[$signo] = $handler;
49            } else {
50                $this->previousSignalHandler[$signo] = true;
51            }
52        } else {
53            unset($this->previousSignalHandler[$signo]);
54        }
55        $this->signalLevelMap[$signo] = $level;
56        $this->signalRestartSyscalls[$signo] = $restartSyscalls;
57
58        if (function_exists('pcntl_async_signals') && $async !== null) {
59            pcntl_async_signals($async);
60        }
61
62        pcntl_signal($signo, array($this, 'handleSignal'), $restartSyscalls);
63
64        return $this;
65    }
66
67    public function handleSignal($signo, array $siginfo = null)
68    {
69        static $signals = array();
70
71        if (!$signals && extension_loaded('pcntl')) {
72            $pcntl = new ReflectionExtension('pcntl');
73            $constants = $pcntl->getConstants();
74            if (!$constants) {
75                // HHVM 3.24.2 returns an empty array.
76                $constants = get_defined_constants(true);
77                $constants = $constants['Core'];
78            }
79            foreach ($constants as $name => $value) {
80                if (substr($name, 0, 3) === 'SIG' && $name[3] !== '_' && is_int($value)) {
81                    $signals[$value] = $name;
82                }
83            }
84            unset($constants);
85        }
86
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);
91
92        if (!isset($this->previousSignalHandler[$signo])) {
93            return;
94        }
95
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);
106                }
107        } elseif (is_callable($this->previousSignalHandler[$signo])) {
108            if (PHP_VERSION_ID >= 70100) {
109                $this->previousSignalHandler[$signo]($signo, $siginfo);
110            } else {
111                $this->previousSignalHandler[$signo]($signo);
112            }
113        }
114    }
115}
116