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