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