forked from dimitri/libphp-pgq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemDaemon.php
530 lines (463 loc) · 12.8 KB
/
SystemDaemon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
defined("PIDFILE_PREFIX") || define("PIDFILE_PREFIX", "/tmp");
if (!defined('E_RECOVERABLE_ERROR')) {
define('E_RECOVERABLE_ERROR', 4096);
}
if (!defined('E_DEPRECATED')) {
define('E_DEPRECATED', 8192);
}
if (!defined('E_USER_DEPRECATED')) {
define('E_USER_DEPRECATED', 16384);
}
require_once("pgq/SimpleLogger.php");
declare(ticks = 1);
/**
* Classes implementing SystemDaemon must implement following methods:
* - php_error_hook()
* - kill_hook($pid)
* - config()
* - process()
*
* None take argument.
*
* php_error_hook() allows you to define what to in case of PHP error
* of E_ERROR or E_USER_ERROR severity. SystemDaemon will not consider
* them as fatal by default, it'll just log them and continue. You're
* free to disagree by defining a custom php_error_hook().
*
* config() will get called at init stage and upon receiving a SIGHUP,
* which can be made with the reload command. The config() call is
* made between two process() run.
*
* config() should provide a way to set at least those parameters:
* $this->delay, the sleeping time between two process() call
* $this->logfile, where logs are sent to
* $this->loglevel, the verbosity.
*
* process() will get run form an infinite loop, which you still can
* break out of by sendig SIGTERM or SIGINT to the daemon, which can
* be made with the kill and stop command.
*
* You can implement a kill_hook($pid) function which will get called before
* signaling the running daemon.
*
* The stop command (or kill -INT <pid>) won't have immediate effect
* but will rather get the daemon to quit at next loop beginning:
* current processing is not canceled. The kill command (or kill -TERM
* <pid>) will have immediate effect.
*
* Supported command line commands are:
* start, stop, kill, reload, restart, status, logless, logmore
*
* The logmore and logless commands resp. send SIGUSR1 and SIGUSR2
* signals to the running daemon and have immediate effect.
*
* Classic usage is to define a daemon class implementing both
* config() and process() methods, then
* MyDaemonInstance = MyDaemon($argc, $argv);
*
* Here the daemon starts, forking and all, and exit() the father:
* you're done. If you save your daemon PHP code into foo.php, you now
* control the running daemon instance with thos commands:
* php foo.php start
* php foo.php status
* php foo.php reload
* php foo.php logmore
* php foo.php logless
* php foo.php stop
*/
abstract class SystemDaemon
{
protected $loglevel = DEBUG;
protected $logfile;
protected $delay = 15;
protected $log;
protected $commands = array("start", "stop", "kill", "restart",
"status", "reload",
"logmore", "logless");
protected $name;
protected $fullname;
protected $debug = false; // if true, stdin/out/err won't be closed
protected $pidfile;
protected $sid;
protected $killed = False;
protected $huped = False;
public function __construct( $argc, &$argv)
{
$this->fullname = $argv[0];
$this->name = basename($this->fullname);
$this->pidfile = sprintf("%s/%s.pid", PIDFILE_PREFIX, $this->name);
$this->log = new SimpleLogger(WARNING, STDOUT);
$this->main($argc, $argv);
}
/**
* Implement those functions when inheriting from this class.
*/
protected function config() { }
protected function process() { }
protected function php_error_hook() { }
protected function kill_hook($pid) { }
/**
* main is responsible of command line parsing and daemon interactions
*/
public function main($argc, $argv) {
if( $argc != 2 ) {
fprintf(STDERR, $this->usage($this->name));
exit(1);
}
switch( $argv[1] ) {
case "start":
$pid = $this->getpid();
if( $pid !== false ) {
printf("Trying to start already running daemon '%s' [%s] \n",
$this->name, $pid);
exit(4);
}
else
$this->start();
break;
case "stop":
$pid = $this->checkpid(4);
posix_kill($pid, SIGINT);
break;
case "kill":
$pid = $this->checkpid(4);
$this->kill_hook($pid);
posix_kill($pid, SIGTERM);
break;
case "restart":
$pid = $this->checkpid(4);
posix_kill($pid, SIGINT);
while( file_exists($this->pidfile) ) {
sleep(1);
}
$this->start();
break;
case "status":
$this->status();
break;
case "reload":
$pid = $this->checkpid(4);
posix_kill($pid, SIGHUP);
break;
case "logmore":
$pid = $this->checkpid(4);
posix_kill($pid, SIGUSR1);
break;
case "logless":
$pid = $this->checkpid(4);
posix_kill($pid, SIGUSR2);
break;
default:
printf($this->usage($this->name));
exit(1);
break;
}
}
/**
* start the daemon, fork(), exit from the father, start a new
* session group from the child, and close STDIN, STDOUT and STDERR.
*
* The first $this->config() call is made here.
*/
public function start()
{
/**
* UNIX daemon startup.
*/
$pid = pcntl_fork();
if ($pid < 0) {
fprintf(STDERR, "fork failure: %s",
posix_strerror(posix_get_last_error()));
exit;
}
else if ( $pid ) {
/**
* Father exit, and let is daemon child work.
*/
exit;
}
else {
/**
* The child works.
*/
$this->sid = posix_setsid();
if( $this->sid < 0 ) {
fprintf(STDERR, "setsid() failure: %s",
posix_strerror(posix_get_last_error()));
exit;
}
if (!$this->debug) {
// don't forget a daemon gets to close those
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);
// reopen ids 0, 1 and 2 so that hard coded libs have no problem
fopen("/dev/null", "a+"); // STDIN
fopen("/dev/null", "a+"); // STDOUT
fopen("/dev/null", "a+"); // STDERR
}
/**
* config() provides log filename and loglevel
*/
$this->config();
unset( $this->log );
$this->log = new SimpleLogger($this->loglevel, $this->logfile);
if( ! $this->log->check() ) {
fprintf(STDERR, "FATAL: could not open logfile '%s': %s",
$this->logfile,
posix_strerror(posix_get_last_error()));
exit;
}
$this->log->notice("Init done (config & logger)");
// Redefine PHP language error handlers
set_error_handler( array( $this, "phpFault" ) );
set_exception_handler( array( $this, "exceptFault" ) );
$this->createpidfile();
/**
* Handle following signals
*/
pcntl_signal(SIGTERM, array(&$this, "handleSignals"));
pcntl_signal(SIGINT, array(&$this, "handleSignals"));
pcntl_signal(SIGHUP, array(&$this, "handleSignals"));
pcntl_signal(SIGUSR1, array(&$this, "handleSignals"));
pcntl_signal(SIGUSR2, array(&$this, "handleSignals"));
/**
* Now we're ready to run.
*/
$this->run();
}
}
/**
* At quitting time, drop the pidfile and write to the logs we're done.
*/
public function stop()
{
$this->droppidfile();
$this->log->debug("Quitting...");
exit(0);
}
/**
* status will simply print out if daemon is running, and under which pid.
*/
public function status()
{
$pid = $this->getpid();
if( $pid === False )
printf("SystemDaemon %s is not running.\n", $this->name);
else {
printf("SystemDaemon %s is running with pid %d\n", $this->name, $pid);
}
}
/**
* Print out the supported commands.
*/
public function usage($progname)
{
return sprintf("%s: %s\n", $progname, implode("|", $this->commands));
}
/**
* checkpid() will call getpid() and exit with the given error code
* when the daemon is not running.
*/
public function checkpid($errcode) {
$pid = $this->getpid();
if( $pid === false ) {
fprintf(STDERR, "No daemon '%s' running \n", $this->name);
exit($errcode);
}
return $pid;
}
/**
* getpid() ensure that the daemon is running, returning its pid
* when it's the case and False when it's no more running.
*
* Current implementation assumes a Linux environment and abuse
* /proc facility to ensure that pidfile content matches our daemon.
*/
public function getpid() {
if( file_exists($this->pidfile) )
{
$pid = file_get_contents($this->pidfile);
if( ! file_exists(sprintf("/proc/%s", $pid)) ) {
$this->droppidfile();
return false;
}
/**
* Both file_get_contents() and fopen()/fgetc() methods are
* unable to get the /proc/... file content
*/
$cmdline = sprintf("/proc/%s/cmdline", $pid);
unset($cmd);
exec(sprintf("cat %s", $cmdline), $cmd);
$cmd = explode("\0", $cmd[0]);
$cmd = $cmd[1];
if( basename($cmd) == basename($this->fullname) )
return $pid;
else {
printf("pidfile: /proc/%s/cmdline does not match '%s' \n",
$pid, $this->fullname);
$this->droppidfile();
return false;
}
}
else
return false;
}
/**
* startup time utility to write our pid to pidfile.
*
* We check for stale pidfile and remove it if necessary.
*/
public function createpidfile() {
if( file_exists($this->pidfile) ) {
$this->error("Pidfile '%s' already exists", $this->pidfile);
$this->droppidfile();
}
$fd = fopen($this->pidfile, "w+");
if( $fd !== false ) {
if( fwrite($fd, getmypid()) === False ) {
$this->log->fatal("Pidfile fwrite('%s') failed: %s",
$this->pidfile,
posix_strerror(posix_get_last_error()));
$this->droppidfile();
exit(2);
}
if( fclose($fd) === False ) {
$this->log->fatal("Pidfile fclose('%s') failed: %s",
$this->pidfile,
posix_strerror(posix_get_last_error()));
$this->droppidfile();
exit(2);
}
}
else {
$this->log->fatal("Pidfile fopen('%s') failed: %s",
$this->pidfile,
posix_strerror(posix_get_last_error()));
exit(2);
}
$this->log->notice("Pidfile '%s' created with '%s'",
$this->pidfile, getmypid());
}
/**
* drop our pidfile
*/
public function droppidfile()
{
$this->log->notice("rm %s", $this->pidfile);
if( file_exists($this->pidfile) ) {
if( ! unlink($this->pidfile) )
$this->log->error("Cound not unlink '%s'", $this->pidfile);
}
else
$this->log->error("Pidfile '%s' does not exist", $this->pidfile);
}
/**
* The run() function leads the daemon work, by calling user
* function process() and sleeping $this->delay, as long as we
* didn't get INT or TERM signal.
*/
public function run()
{
$this->log->notice("run");
while( ! $this->killed )
{
if( $this->huped ) {
$this->config();
// Don't forget to forward the loglevel change if any
if( $this->loglevel )
$this->log->loglevel = $this->loglevel;
// And to force logfile reopening (be nice to log rotating)
$this->log->reopen();
$this->huped = False;
}
$this->process();
if( ! $this->killed ) {
$this->log->debug("sleeping %d seconds", $this->delay);
sleep($this->delay);
}
}
$this->stop();
}
/**
* React to supported user signals
*/
public function handleSignals($sig) {
switch($sig) {
case SIGTERM:
$this->log->warning("Received TERM signal.");
$this->stop();
break;
case SIGINT:
$this->log->warning("Received INT signal.");
$this->killed = True;
break;
case SIGHUP:
$this->log->warning("Received HUP signal");
$this->huped = True;
break;
case SIGUSR1:
$this->log->warning("Received USR1 signal, logging more");
$this->log->logmore();
break;
case SIGUSR2:
$this->log->warning("Received USR1 signal, logging less");
$this->log->logless();
break;
}
}
/**
* Register our own PHP language error handlers
*/
function phpFault( $errno, $errstr, $errfile, $errline )
{
$message = "PHP: ".strip_tags($errstr)." in {$errfile}:{$errline}";
switch( (int)$errno )
{
case E_PARSE:
case E_CORE_ERROR:
case E_CORE_WARNING:
case E_COMPILE_ERROR:
case E_COMPILE_WARNING:
$this->log->fatal( $message );
$this->stop();
break;
case E_USER_ERROR:
case E_ERROR:
case E_RECOVERABLE_ERROR:
$this->log->error( $message );
$this->php_error_hook();
break;
case E_WARNING:
case E_USER_WARNING:
$this->log->warning( $message );
return true;
break;
case E_STRICT:
case E_DEPRECATED:
case E_USER_DEPRECATED:
case E_NOTICE:
case E_USER_NOTICE:
$this->log->notice( $message );
return true;
break;
}
return false;
}
/**
* We also support non-catched exceptions and consider them as fatal
* errors.
*/
function exceptFault( $exception )
{
$trace = $exception->getTrace();
$message = $exception->getMessage();
if( is_array($trace) && count($trace) > 0 ) {
$message .= "; source: " . $trace[0]["file"] . ":" . $trace[0]["line"];
}
$this->log->fatal($message);
$this->stop();
}
}
?>