]> git.proxmox.com Git - pve-common.git/blame - data/PVE/Daemon.pm
after_fork_cleanup: move code into separate sub
[pve-common.git] / data / PVE / Daemon.pm
CommitLineData
390802ab
DM
1package PVE::Daemon;
2
3# Abstract class to implement Daemons
4#
5# Features:
6# * lock and write PID file /var/run/$name.pid to make sure onyl
7# one instance is running.
8# * correctly daemonize (redirect STDIN/STDOUT)
4fc69176
DM
9# * restart by stop/start, exec, or signal HUP
10# * daemon restart on error (option 'restart_on_error')
b2132054 11# * handle worker processes (option 'max_workers')
390802ab
DM
12
13use strict;
14use warnings;
15use PVE::SafeSyslog;
16use PVE::INotify;
17
18use POSIX ":sys_wait_h";
19use Fcntl ':flock';
20use Getopt::Long;
21use Time::HiRes qw (gettimeofday);
22
23use base qw(PVE::CLIHandler);
24
25$SIG{'__WARN__'} = sub {
26 my $err = $@;
27 my $t = $_[0];
28 chomp $t;
29 print "$t\n";
30 syslog('warning', "WARNING: %s", $t);
31 $@ = $err;
32};
33
34$ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
35
36my $daemon_initialized = 0; # we only allow one instance
37
3206cdf6
DM
38my $close_daemon_lock = sub {
39 my ($self) = @_;
40
eead1ad8
DM
41 return if !$self->{daemon_lock_fh};
42
3206cdf6
DM
43 close $self->{daemon_lock_fh};
44 delete $self->{daemon_lock_fh};
45};
46
eead1ad8
DM
47# call this if you fork() from child
48# Note: we already call this for workers, so it is only required
49# if you fork inside a simple daemon (max_workers == 0).
50sub after_fork_cleanup {
51 my ($self) = @_;
52
53 &$close_daemon_lock($self);
54
55 PVE::INotify::inotify_close();
56
57 for my $sig (qw(CHLD HUP INT TERM QUIT)) {
58 $SIG{$sig} = 'DEFAULT'; # restore default handler
59 # AnyEvent signals only works if $SIG{XX} is
60 # undefined (perl event loop)
61 delete $SIG{$sig}; # so that we can handle events with AnyEvent
62 }
63}
64
390802ab
DM
65my $lockpidfile = sub {
66 my ($self) = @_;
67
68 my $lkfn = $self->{pidfile} . ".lock";
69
3206cdf6
DM
70 $self->{daemon_lock_fh} = IO::File->new(">>$lkfn");
71 if (!$self->{daemon_lock_fh}) {
390802ab
DM
72 my $msg = "can't aquire lock on file '$lkfn' - $!";
73 syslog ('err', $msg);
74 die "ERROR: $msg\n";
75 }
76
3206cdf6
DM
77 for (my $i = 0; $i < 5; $i ++) {
78 return if flock ($self->{daemon_lock_fh}, LOCK_EX|LOCK_NB);
79 sleep(1);
80 }
81
82 if (!flock ($self->{daemon_lock_fh}, LOCK_EX|LOCK_NB)) {
83 &$close_daemon_lock($self);
390802ab
DM
84 my $msg = "can't aquire lock '$lkfn' - $!";
85 syslog ('err', $msg);
86 die "ERROR: $msg\n";
87 }
88};
89
90my $writepidfile = sub {
91 my ($self) = @_;
92
93 my $pidfile = $self->{pidfile};
94
95 if (!open (PIDFH, ">$pidfile")) {
96 my $msg = "can't open pid file '$pidfile' - $!";
97 syslog ('err', $msg);
98 die "ERROR: $msg\n";
99 }
100 print PIDFH "$$\n";
101 close (PIDFH);
102};
103
104my $server_cleanup = sub {
105 my ($self) = @_;
106
107 unlink $self->{pidfile} . ".lock";
108 unlink $self->{pidfile};
109};
110
b2132054
DM
111my $finish_workers = sub {
112 my ($self) = @_;
113
114 foreach my $cpid (keys %{$self->{workers}}) {
115 my $waitpid = waitpid($cpid, WNOHANG);
116 if (defined($waitpid) && ($waitpid == $cpid)) {
117 delete ($self->{workers}->{$cpid});
118 syslog('info', "worker $cpid finished");
119 }
120 }
121};
122
123my $start_workers = sub {
124 my ($self) = @_;
125
126 return if $self->{terminate};
127
128 my $count = 0;
129 foreach my $cpid (keys %{$self->{workers}}) {
130 $count++;
131 }
132
133 my $need = $self->{max_workers} - $count;
134
135 return if $need <= 0;
136
137 syslog('info', "starting $need worker(s)");
138
139 while ($need > 0) {
140 my $pid = fork;
141
142 if (!defined ($pid)) {
143 syslog('err', "can't fork worker");
144 sleep (1);
145 } elsif ($pid) { # parent
146 $self->{workers}->{$pid} = 1;
147 syslog('info', "worker $pid started");
148 $need--;
149 } else {
150 $0 = "$self->{name} worker";
151
eead1ad8 152 $self->after_fork_cleanup();
b2132054
DM
153
154 eval { $self->run(); };
155 if (my $err = $@) {
156 syslog('err', $err);
157 sleep(5); # avoid fast restarts
158 }
159
160 syslog('info', "worker exit");
161 exit (0);
162 }
163 }
164};
165
166my $terminate_server = sub {
167 my ($self) = @_;
168
169 $self->{terminate} = 1; # set flag to avoid worker restart
170
171 if (!$self->{max_workers}) {
172 eval { $self->shutdown(); };
173 warn $@ if $@;
174 return;
175 }
176
177 eval { $self->shutdown(); };
178 warn $@ if $@;
179
180 # we have workers - terminate them
181
182 foreach my $cpid (keys %{$self->{workers}}) {
183 kill(15, $cpid); # TERM childs
184 }
185
186 # nicely shutdown childs (give them max 10 seconds to shut down)
187 my $previous_alarm = alarm(10);
188 eval {
189 local $SIG{ALRM} = sub { die "timeout\n" };
190
191 while ((my $pid = waitpid (-1, 0)) > 0) {
192 if (defined($self->{workers}->{$pid})) {
193 delete($self->{workers}->{$pid});
194 syslog('info', "worker $pid finished");
195 }
196 }
197 alarm(0); # avoid race condition
198 };
199 my $err = $@;
200
201 alarm ($previous_alarm);
202
203 if ($err) {
204 syslog('err', "error stopping workers (will kill them now) - $err");
205 foreach my $cpid (keys %{$self->{workers}}) {
206 # KILL childs still alive!
207 if (kill (0, $cpid)) {
208 delete($self->{workers}->{$cpid});
209 syslog("err", "kill worker $cpid");
210 kill(9, $cpid);
211 # fixme: waitpid?
212 }
213 }
214 }
215};
216
390802ab
DM
217my $server_run = sub {
218 my ($self, $debug) = @_;
219
220 &$lockpidfile($self);
221
222 # run in background
223 my $spid;
224
225 my $restart = $ENV{RESTART_PVE_DAEMON};
226
227 delete $ENV{RESTART_PVE_DAEMON};
228
229 $self->{debug} = 1 if $debug;
230
231 $self->init();
232
233 if (!$debug) {
234 open STDIN, '</dev/null' || die "can't read /dev/null";
235 open STDOUT, '>/dev/null' || die "can't write /dev/null";
236 }
237
238 if (!$restart && !$debug) {
239 PVE::INotify::inotify_close();
240 $spid = fork();
241 if (!defined ($spid)) {
242 my $msg = "can't put server into background - fork failed";
243 syslog('err', $msg);
244 die "ERROR: $msg\n";
245 } elsif ($spid) { # parent
246 exit (0);
247 }
248 PVE::INotify::inotify_init();
249 }
250
251 &$writepidfile($self);
252
891b9097
DM
253 POSIX::setsid();
254
390802ab
DM
255 if ($restart) {
256 syslog('info' , "restarting server");
257 } else {
258 syslog('info' , "starting server");
259 }
260
261 open STDERR, '>&STDOUT' || die "can't close STDERR\n";
262
b2132054
DM
263 my $old_sig_term = $SIG{TERM};
264 local $SIG{TERM} = sub {
265 local ($@, $!, $?); # do not overwrite error vars
266 syslog('info', "received signal TERM");
267 &$terminate_server($self);
268 &$server_cleanup($self);
269 &$old_sig_term(@_) if $old_sig_term;
270 };
390802ab 271
b2132054
DM
272 my $old_sig_quit = $SIG{QUIT};
273 local $SIG{QUIT} = sub {
274 local ($@, $!, $?); # do not overwrite error vars
275 syslog('info', "received signal QUIT");
276 &$terminate_server($self);
277 &$server_cleanup($self);
278 &$old_sig_quit(@_) if $old_sig_quit;
279 };
390802ab 280
b2132054
DM
281 my $old_sig_int = $SIG{INT};
282 local $SIG{INT} = sub {
283 local ($@, $!, $?); # do not overwrite error vars
284 syslog('info', "received signal INT");
285 $SIG{INT} = 'DEFAULT'; # allow to terminate now
286 &$terminate_server($self);
390802ab 287 &$server_cleanup($self);
b2132054 288 &$old_sig_int(@_) if $old_sig_int;
390802ab
DM
289 };
290
b2132054
DM
291 $SIG{HUP} = sub {
292 local ($@, $!, $?); # do not overwrite error vars
293 syslog('info', "received signal HUP");
294 if ($self->{max_workers}) {
295 &$terminate_server($self);
296 $self->{got_hup_signal} = 1;
297 } elsif ($self->can('hup')) {
bdb5acce
DM
298 eval { $self->hup() };
299 warn $@ if $@;
b2132054
DM
300 }
301 };
302
303 eval {
304 if ($self->{max_workers}) {
305 my $old_sig_chld = $SIG{CHLD};
306 local $SIG{CHLD} = sub {
307 local ($@, $!, $?); # do not overwrite error vars
308 &$finish_workers($self);
309 &$old_sig_chld(@_) if $old_sig_chld;
310 };
311
312 for (;;) { # forever
313 &$start_workers($self);
314 sleep(5);
315 &$finish_workers($self);
316 last if $self->{terminate};
317 }
bdb5acce 318
b2132054
DM
319 } else {
320 $self->run();
321 }
322 };
390802ab
DM
323 my $err = $@;
324
325 if ($err) {
326 syslog ('err', "ERROR: $err");
b2132054
DM
327
328 # fixme: kill all workers
329
390802ab
DM
330 if (my $wait_time = $self->{restart_on_error}) {
331 $self->restart_daemon($wait_time);
332 } else {
333 $self->exit_daemon(-1);
334 }
335 }
336
b2132054
DM
337 if ($self->{got_hup_signal}) {
338 $self->restart_daemon();
339 } else {
340 $self->exit_daemon(0);
341 }
390802ab
DM
342};
343
344sub new {
345 my ($this, $name, $cmdline, %params) = @_;
346
b2132054 347 die "please run as root\n" if !$ENV{RESTART_PVE_DAEMON} && ($> != 0);
390802ab
DM
348
349 die "missing name" if !$name;
350
351 die "can't create more that one PVE::Daemon" if $daemon_initialized;
352 $daemon_initialized = 1;
353
354 PVE::INotify::inotify_init();
355
356 initlog($name);
357
358 my $class = ref($this) || $this;
359
bdb5acce
DM
360 my $self = bless {
361 name => $name,
362 run_dir => '/var/run',
b2132054 363 workers => {},
bdb5acce 364 }, $class;
390802ab
DM
365
366 foreach my $opt (keys %params) {
367 my $value = $params{$opt};
368 if ($opt eq 'restart_on_error') {
369 $self->{$opt} = $value;
370 } elsif ($opt eq 'stop_wait_time') {
371 $self->{$opt} = $value;
bdb5acce
DM
372 } elsif ($opt eq 'run_dir') {
373 $self->{$opt} = $value;
b2132054
DM
374 } elsif ($opt eq 'max_workers') {
375 $self->{$opt} = $value;
390802ab
DM
376 } else {
377 die "unknown option '$opt'";
378 }
379 }
380
bdb5acce
DM
381 $self->{pidfile} = "$self->{run_dir}/${name}.pid";
382
383 $self->{nodename} = PVE::INotify::nodename();
384
b2132054 385 $self->{cmdline} = [];
bdb5acce 386
b2132054
DM
387 foreach my $el (@$cmdline) {
388 $el =~ m/^(.*)$/; # untaint
389 push @{$self->{cmdline}}, $1;
390 }
bdb5acce 391
b2132054 392 $0 = $name;
bdb5acce 393
390802ab
DM
394 return $self;
395}
396
397sub exit_daemon {
398 my ($self, $status) = @_;
399
400 syslog("info", "server stopped");
401
402 &$server_cleanup($self);
403
404 exit($status);
405}
406
407sub restart_daemon {
408 my ($self, $waittime) = @_;
409
410 syslog('info', "server shutdown (restart)");
411
412 $ENV{RESTART_PVE_DAEMON} = 1;
413
414 sleep($waittime) if $waittime; # avoid high server load due to restarts
415
416 PVE::INotify::inotify_close();
417
418 exec (@{$self->{cmdline}});
419
420 exit (-1); # never reached?
421}
422
423# please overwrite in subclass
424# this is called at startup - before forking
425sub init {
426 my ($self) = @_;
427
428}
429
430# please overwrite in subclass
431sub shutdown {
432 my ($self) = @_;
433
434 syslog('info' , "server closing");
435
b2132054
DM
436 if (!$self->{max_workers}) {
437 # wait for children
438 1 while (waitpid(-1, POSIX::WNOHANG()) > 0);
439 }
390802ab
DM
440}
441
bdb5acce
DM
442# please define in subclass
443#sub hup {
444# my ($self) = @_;
445#
446# syslog('info' , "received signal HUP (restart)");
447#}
390802ab
DM
448
449# please overwrite in subclass
450sub run {
451 my ($self) = @_;
452
453 for (;;) { # forever
454 syslog('info' , "server is running");
455 sleep(5);
456 }
457}
458
459sub start {
460 my ($self, $debug) = @_;
461
462 &$server_run($self, $debug);
463}
464
bdb5acce
DM
465my $read_pid = sub {
466 my ($self) = @_;
467
468 my $pid_str = PVE::Tools::file_read_firstline($self->{pidfile});
469
470 return 0 if !$pid_str;
471
472 return 0 if $pid_str !~ m/^(\d+)$/; # untaint
473
474 my $pid = int($1);
475
476 return $pid;
477};
478
390802ab
DM
479sub running {
480 my ($self) = @_;
481
bdb5acce 482 my $pid = &$read_pid($self);
390802ab
DM
483
484 if ($pid) {
485 my $res = PVE::ProcFSTools::check_process_running($pid) ? 1 : 0;
486 return wantarray ? ($res, $pid) : $res;
487 }
488
489 return wantarray ? (0, 0) : 0;
490}
491
492sub stop {
493 my ($self) = @_;
494
bdb5acce
DM
495 my $pid = &$read_pid($self);
496
390802ab
DM
497 return if !$pid;
498
499 if (PVE::ProcFSTools::check_process_running($pid)) {
500 kill(15, $pid); # send TERM signal
501 # give some time
502 my $wait_time = $self->{stop_wait_time} || 5;
503 my $running = 1;
504 for (my $i = 0; $i < $wait_time; $i++) {
505 $running = PVE::ProcFSTools::check_process_running($pid);
506 last if !$running;
507 sleep (1);
508 }
509
510 syslog('err', "server still running - send KILL") if $running;
511
512 # to be sure
513 kill(9, $pid);
514 waitpid($pid, 0);
515 }
516
517 if (-f $self->{pidfile}) {
518 # try to get the lock
519 &$lockpidfile($self);
520 &$server_cleanup($self);
521 }
522}
523
524sub register_start_command {
525 my ($self, $class, $description) = @_;
526
527 $class->register_method({
528 name => 'start',
529 path => 'start',
530 method => 'POST',
531 description => $description || "Start the daemon.",
532 parameters => {
533 additionalProperties => 0,
534 properties => {
535 debug => {
536 description => "Debug mode - stay in foreground",
537 type => "boolean",
538 optional => 1,
539 default => 0,
540 },
541 },
542 },
543 returns => { type => 'null' },
544
545 code => sub {
546 my ($param) = @_;
547
548 $self->start($param->{debug});
549
550 return undef;
551 }});
552}
553
bdb5acce
DM
554my $reload_daemon = sub {
555 my ($self, $use_hup) = @_;
556
557 if (my $restart = $ENV{RESTART_PVE_DAEMON}) {
558 $self->start();
559 } else {
560 my ($running, $pid) = $self->running();
561 if (!$running) {
562 $self->start();
563 } else {
564 if ($use_hup) {
b2132054
DM
565 syslog('info', "send HUP to $pid");
566 kill 1, $pid;
bdb5acce
DM
567 } else {
568 $self->stop();
569 $self->start();
570 }
571 }
572 }
573};
574
390802ab 575sub register_restart_command {
bdb5acce 576 my ($self, $class, $use_hup, $description) = @_;
390802ab
DM
577
578 $class->register_method({
579 name => 'restart',
580 path => 'restart',
581 method => 'POST',
582 description => $description || "Restart the daemon (or start if not running).",
583 parameters => {
584 additionalProperties => 0,
585 properties => {},
586 },
587 returns => { type => 'null' },
588
589 code => sub {
590 my ($param) = @_;
591
bdb5acce
DM
592 &$reload_daemon($self, $use_hup);
593
594 return undef;
595 }});
596}
597
598sub register_reload_command {
599 my ($self, $class, $description) = @_;
600
601 $class->register_method({
602 name => 'reload',
603 path => 'reload',
604 method => 'POST',
605 description => $description || "Reload daemon configuration (or start if not running).",
606 parameters => {
607 additionalProperties => 0,
608 properties => {},
609 },
610 returns => { type => 'null' },
611
612 code => sub {
613 my ($param) = @_;
614
615 &$reload_daemon($self, 1);
390802ab
DM
616
617 return undef;
618 }});
619}
620
621sub register_stop_command {
622 my ($self, $class, $description) = @_;
623
624 $class->register_method({
625 name => 'stop',
626 path => 'stop',
627 method => 'POST',
628 description => $description || "Stop the daemon.",
629 parameters => {
630 additionalProperties => 0,
631 properties => {},
632 },
633 returns => { type => 'null' },
634
635 code => sub {
636 my ($param) = @_;
637
638 $self->stop();
639
640 return undef;
641 }});
642}
643
644sub register_status_command {
645 my ($self, $class, $description) = @_;
646
647 $class->register_method({
648 name => 'status',
649 path => 'status',
650 method => 'GET',
651 description => "Get daemon status.",
652 parameters => {
653 additionalProperties => 0,
654 properties => {},
655 },
656 returns => {
657 type => 'string',
658 enum => ['stopped', 'running'],
659 },
660 code => sub {
661 my ($param) = @_;
662
663 return $self->running() ? 'running' : 'stopped';
664 }});
665}
666
6671;
668