2019-12-08 01:50:18 +01:00
|
|
|
<?php
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2019-12-08 02:33:53 +01:00
|
|
|
namespace daemons;
|
|
|
|
|
2019-12-08 01:50:18 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Class defining the global structur of a Linux Daemon.
|
2019-12-08 01:50:18 +01:00
|
|
|
*/
|
|
|
|
abstract class AbstractDaemon
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
protected $name;
|
|
|
|
protected $uniq;
|
2020-01-08 18:20:17 +01:00
|
|
|
protected $logger;
|
|
|
|
protected $no_parent;
|
2020-01-17 18:36:53 +01:00
|
|
|
protected $pid_dir;
|
2020-01-17 18:19:25 +01:00
|
|
|
private $is_running = true;
|
|
|
|
private $signals = [
|
2019-12-08 01:50:18 +01:00
|
|
|
SIGTERM,
|
|
|
|
SIGINT,
|
|
|
|
SIGCHLD,
|
2020-01-17 18:19:25 +01:00
|
|
|
SIGHUP,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used to handle POSIX signals and fork from the current process.
|
|
|
|
*
|
|
|
|
* @param string $name : The name of the class
|
|
|
|
* @param object $logger : A PSR3 logger instance
|
|
|
|
* @param string $pid_dir : Directory for the pid files
|
|
|
|
* @param bool $no_parent : Should the daemon be disconnected from his parent process
|
|
|
|
* @param array $signals :An array containing additional POSIX signals to handle [optionel]
|
|
|
|
* @param bool $uniq : Must the process be uniq ?
|
|
|
|
*/
|
|
|
|
protected function __construct(string $name, object $logger, string $pid_dir = '/var/run', $no_parent = false, array $signals = [], bool $uniq = false)
|
2019-12-08 01:50:18 +01:00
|
|
|
{
|
2019-12-12 00:56:30 +01:00
|
|
|
$this->name = $name;
|
|
|
|
$this->logger = $logger;
|
2020-01-08 18:20:17 +01:00
|
|
|
$this->no_parent = $no_parent;
|
|
|
|
$this->pid_dir = $pid_dir;
|
2019-12-08 01:50:18 +01:00
|
|
|
$this->signals = array_merge($this->signals, $signals);
|
2019-12-12 00:56:30 +01:00
|
|
|
$this->uniq = $uniq;
|
2019-12-08 01:50:18 +01:00
|
|
|
|
|
|
|
//Allow script to run indefinitly
|
|
|
|
set_time_limit(0);
|
|
|
|
|
|
|
|
//Register signals
|
2020-01-17 18:19:25 +01:00
|
|
|
$this->register_signals();
|
2019-12-08 01:50:18 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* True if the daemon is running.
|
|
|
|
*/
|
|
|
|
public function is_running()
|
2019-12-08 01:50:18 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
return $this->is_running;
|
2019-12-08 01:50:18 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* Used to handle properly SIGINT, SIGTERM, SIGCHLD and SIGHUP.
|
|
|
|
*
|
|
|
|
* @param int $signal
|
2019-12-08 01:50:18 +01:00
|
|
|
* @param mixed $signinfo
|
2020-01-17 18:19:25 +01:00
|
|
|
*/
|
2019-12-08 01:50:18 +01:00
|
|
|
protected function handle_signal(int $signal, $signinfo)
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
if (SIGTERM === $signal || SIGINT === $signal)
|
|
|
|
{ //Stop the daemon
|
2019-12-17 14:38:16 +01:00
|
|
|
$this->is_running = false;
|
2019-12-08 01:50:18 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
elseif (SIGHUP === $signal)
|
|
|
|
{ //Restart the daemon
|
|
|
|
$this->on_stop();
|
|
|
|
$this->on_start();
|
2019-12-08 01:50:18 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
elseif (SIGCHLD === $signal)
|
|
|
|
{ //On daemon child stopping
|
2019-12-17 14:38:16 +01:00
|
|
|
pcntl_waitpid(-1, $status, WNOHANG);
|
2019-12-08 01:50:18 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
else
|
|
|
|
{ //All the other signals
|
|
|
|
$this->handle_other_signals($signal);
|
|
|
|
}
|
2019-12-08 01:50:18 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* Launch the infinite loop executing the "run" abstract method.
|
|
|
|
*/
|
|
|
|
protected function start()
|
2019-12-08 01:50:18 +01:00
|
|
|
{
|
2019-12-12 00:56:30 +01:00
|
|
|
//If process must be uniq and a process with the same pid file is already running
|
2020-01-17 18:19:25 +01:00
|
|
|
if (file_exists($this->pid_dir.'/'.$this->name.'.pid') && $this->uniq)
|
2019-12-08 01:50:18 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$this->logger->info('Another process named '.$this->name.' is already running.');
|
|
|
|
|
2019-12-12 00:56:30 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-08 18:20:17 +01:00
|
|
|
//If we must make the daemon independant from any parent, we do a fork and die operation
|
|
|
|
if ($this->no_parent)
|
2019-12-12 00:56:30 +01:00
|
|
|
{
|
2020-01-08 18:20:17 +01:00
|
|
|
$pid = pcntl_fork(); //Fork current process into a child, so we can kill current process and keep only the child with parent PID = 1
|
2019-12-12 00:56:30 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
if (-1 === $pid)
|
|
|
|
{ //Impossible to run script
|
|
|
|
$this->logger->critical('Impossible to create a subprocess.');
|
|
|
|
|
2020-01-08 18:20:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
if ($pid)
|
|
|
|
{ //Current script
|
2020-01-08 18:20:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
$this->logger->info("Process {$this->name} started as a child with pid ".getmypid().'.');
|
2020-01-08 18:20:17 +01:00
|
|
|
|
|
|
|
//Child script
|
|
|
|
$sid = posix_setsid(); //Try to make the child process a main process
|
2020-01-17 18:19:25 +01:00
|
|
|
if (-1 === $sid)
|
|
|
|
{ //Error
|
|
|
|
$this->logger->critical("Cannot make the child process with pid {$pid} independent.");
|
2020-01-08 18:20:17 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
$this->logger->info('The child process with pid '.getmypid().' is now independent.');
|
|
|
|
}
|
2019-12-12 00:56:30 +01:00
|
|
|
|
|
|
|
//Create pid dir if not exists
|
|
|
|
if (!file_exists($this->pid_dir))
|
|
|
|
{
|
|
|
|
$success = mkdir($this->pid_dir, 0777, true);
|
|
|
|
if (!$success)
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$this->logger->critical('Cannot create PID directory : '.$this->pid_dir);
|
2019-12-12 00:56:30 +01:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Set process name
|
|
|
|
cli_set_process_title($this->name);
|
|
|
|
|
|
|
|
//Write the pid of the process into a file
|
2020-01-17 18:19:25 +01:00
|
|
|
file_put_contents($this->pid_dir.'/'.$this->name.'.pid', getmypid());
|
2020-01-08 18:20:17 +01:00
|
|
|
|
|
|
|
//Really start the daemon
|
2019-12-12 00:56:30 +01:00
|
|
|
$this->on_start();
|
2020-01-08 18:20:17 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
try
|
2019-12-12 00:56:30 +01:00
|
|
|
{
|
|
|
|
while ($this->is_running)
|
|
|
|
{
|
|
|
|
pcntl_signal_dispatch(); //Call dispatcher for signals
|
|
|
|
$this->run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$this->logger->critical('Exception : '.$e->getMessage().' in '.$e->getFile().' line '.$e->getLine());
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
2020-01-08 18:20:17 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
//Stop the daemon
|
2019-12-12 00:56:30 +01:00
|
|
|
$this->on_stop();
|
|
|
|
|
|
|
|
//Delete pid file
|
2020-01-17 18:19:25 +01:00
|
|
|
if (file_exists($this->pid_dir.'/'.$this->name.'.pid'))
|
2019-12-12 00:56:30 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
unlink($this->pid_dir.'/'.$this->name.'.pid');
|
2019-12-12 00:56:30 +01:00
|
|
|
}
|
2019-12-08 01:50:18 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* Override to implement the code that run infinetly (actually, it run one time but repeat the operation infinetly.
|
|
|
|
*/
|
|
|
|
abstract protected function run();
|
2019-12-08 01:50:18 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* Override to execute code before the ''run'' method on daemon start.
|
|
|
|
*/
|
|
|
|
abstract protected function on_start();
|
2019-12-08 01:50:18 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* Override to execute code after the ''run'' method on daemon shutdown.
|
|
|
|
*/
|
|
|
|
abstract protected function on_stop();
|
2019-12-08 01:50:18 +01:00
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
/**
|
|
|
|
* Override to handle additional POSIX signals.
|
|
|
|
*
|
|
|
|
* @param int $signal : Signal sent by interrupt
|
|
|
|
*/
|
|
|
|
abstract protected function handle_other_signals(int $signal);
|
2019-12-08 01:50:18 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Used to register POSIX signals.
|
|
|
|
*/
|
|
|
|
private function register_signals()
|
|
|
|
{
|
|
|
|
//Enable a tick at every 1 instruction, allowing us to run a function frequently, for exemple looking at signal status
|
|
|
|
declare(ticks=1);
|
|
|
|
|
|
|
|
foreach ($this->signals as $signal)
|
|
|
|
{
|
|
|
|
//For each signal define the method handle_signal of the current class as the way to handle it
|
|
|
|
@pcntl_signal($signal, [
|
|
|
|
'self',
|
|
|
|
'handle_signal',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 01:50:18 +01:00
|
|
|
}
|