]> git.proxmox.com Git - pve-common.git/blob - data/PVE/Daemon.pm
Daemon: move ENV manipulation into new()
[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 $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
36 my $daemon_initialized = 0; # we only allow one instance
37
38 my $close_daemon_lock = sub {
39 my ($self) = @_;
40
41 return if !$self->{daemon_lock_fh};
42
43 close $self->{daemon_lock_fh};
44 delete $self->{daemon_lock_fh};
45 };
46
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).
50 sub 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
65 my $lockpidfile = sub {
66 my ($self) = @_;
67
68 my $lkfn = $self->{pidfile} . ".lock";
69
70 $self->{daemon_lock_fh} = IO::File->new(">>$lkfn");
71 if (!$self->{daemon_lock_fh}) {
72 my $msg = "can't aquire lock on file '$lkfn' - $!";
73 syslog ('err', $msg);
74 die "ERROR: $msg\n";
75 }
76
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);
84 my $msg = "can't aquire lock '$lkfn' - $!";
85 syslog ('err', $msg);
86 die "ERROR: $msg\n";
87 }
88 };
89
90 my $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
104 my $server_cleanup = sub {
105 my ($self) = @_;
106
107 unlink $self->{pidfile} . ".lock";
108 unlink $self->{pidfile};
109 };
110
111 my $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
123 my $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
152 $self->after_fork_cleanup();
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
166 my $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
217 my $server_run = sub {
218 my ($self, $debug) = @_;
219
220 &$lockpidfile($self);
221
222 # run in background
223 my $spid;
224
225 $self->{debug} = 1 if $debug;
226
227 $self->init();
228
229 if (!$debug) {
230 open STDIN, '</dev/null' || die "can't read /dev/null";
231 open STDOUT, '>/dev/null' || die "can't write /dev/null";
232 }
233
234 if (!$self->{env_restart_pve_daemon} && !$debug) {
235 PVE::INotify::inotify_close();
236 $spid = fork();
237 if (!defined ($spid)) {
238 my $msg = "can't put server into background - fork failed";
239 syslog('err', $msg);
240 die "ERROR: $msg\n";
241 } elsif ($spid) { # parent
242 exit (0);
243 }
244 PVE::INotify::inotify_init();
245 }
246
247 &$writepidfile($self);
248
249 POSIX::setsid();
250
251 if ($self->{env_restart_pve_daemon}) {
252 syslog('info' , "restarting server");
253 } else {
254 syslog('info' , "starting server");
255 }
256
257 open STDERR, '>&STDOUT' || die "can't close STDERR\n";
258
259 my $old_sig_term = $SIG{TERM};
260 local $SIG{TERM} = sub {
261 local ($@, $!, $?); # do not overwrite error vars
262 syslog('info', "received signal TERM");
263 &$terminate_server($self);
264 &$server_cleanup($self);
265 &$old_sig_term(@_) if $old_sig_term;
266 };
267
268 my $old_sig_quit = $SIG{QUIT};
269 local $SIG{QUIT} = sub {
270 local ($@, $!, $?); # do not overwrite error vars
271 syslog('info', "received signal QUIT");
272 &$terminate_server($self);
273 &$server_cleanup($self);
274 &$old_sig_quit(@_) if $old_sig_quit;
275 };
276
277 my $old_sig_int = $SIG{INT};
278 local $SIG{INT} = sub {
279 local ($@, $!, $?); # do not overwrite error vars
280 syslog('info', "received signal INT");
281 $SIG{INT} = 'DEFAULT'; # allow to terminate now
282 &$terminate_server($self);
283 &$server_cleanup($self);
284 &$old_sig_int(@_) if $old_sig_int;
285 };
286
287 $SIG{HUP} = sub {
288 local ($@, $!, $?); # do not overwrite error vars
289 syslog('info', "received signal HUP");
290 if ($self->{max_workers}) {
291 &$terminate_server($self);
292 $self->{got_hup_signal} = 1;
293 } elsif ($self->can('hup')) {
294 eval { $self->hup() };
295 warn $@ if $@;
296 }
297 };
298
299 eval {
300 if ($self->{max_workers}) {
301 my $old_sig_chld = $SIG{CHLD};
302 local $SIG{CHLD} = sub {
303 local ($@, $!, $?); # do not overwrite error vars
304 &$finish_workers($self);
305 &$old_sig_chld(@_) if $old_sig_chld;
306 };
307
308 for (;;) { # forever
309 &$start_workers($self);
310 sleep(5);
311 &$finish_workers($self);
312 last if $self->{terminate};
313 }
314
315 } else {
316 $self->run();
317 }
318 };
319 my $err = $@;
320
321 if ($err) {
322 syslog ('err', "ERROR: $err");
323
324 # fixme: kill all workers
325
326 if (my $wait_time = $self->{restart_on_error}) {
327 $self->restart_daemon($wait_time);
328 } else {
329 $self->exit_daemon(-1);
330 }
331 }
332
333 if ($self->{got_hup_signal}) {
334 $self->restart_daemon();
335 } else {
336 $self->exit_daemon(0);
337 }
338 };
339
340 sub new {
341 my ($this, $name, $cmdline, %params) = @_;
342
343 my $restart = $ENV{RESTART_PVE_DAEMON};
344
345 delete $ENV{RESTART_PVE_DAEMON};
346
347 die "please run as root\n" if !$restart && ($> != 0);
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
360 my $self = bless {
361 name => $name,
362 run_dir => '/var/run',
363 env_restart_pve_daemon => $restart,
364 workers => {},
365 }, $class;
366
367 foreach my $opt (keys %params) {
368 my $value = $params{$opt};
369 if ($opt eq 'restart_on_error') {
370 $self->{$opt} = $value;
371 } elsif ($opt eq 'stop_wait_time') {
372 $self->{$opt} = $value;
373 } elsif ($opt eq 'run_dir') {
374 $self->{$opt} = $value;
375 } elsif ($opt eq 'max_workers') {
376 $self->{$opt} = $value;
377 } else {
378 die "unknown option '$opt'";
379 }
380 }
381
382 $self->{pidfile} = "$self->{run_dir}/${name}.pid";
383
384 $self->{nodename} = PVE::INotify::nodename();
385
386 $self->{cmdline} = [];
387
388 foreach my $el (@$cmdline) {
389 $el =~ m/^(.*)$/; # untaint
390 push @{$self->{cmdline}}, $1;
391 }
392
393 $0 = $name;
394
395 return $self;
396 }
397
398 sub exit_daemon {
399 my ($self, $status) = @_;
400
401 syslog("info", "server stopped");
402
403 &$server_cleanup($self);
404
405 exit($status);
406 }
407
408 sub restart_daemon {
409 my ($self, $waittime) = @_;
410
411 syslog('info', "server shutdown (restart)");
412
413 $ENV{RESTART_PVE_DAEMON} = 1;
414
415 sleep($waittime) if $waittime; # avoid high server load due to restarts
416
417 PVE::INotify::inotify_close();
418
419 exec (@{$self->{cmdline}});
420
421 exit (-1); # never reached?
422 }
423
424 # please overwrite in subclass
425 # this is called at startup - before forking
426 sub init {
427 my ($self) = @_;
428
429 }
430
431 # please overwrite in subclass
432 sub shutdown {
433 my ($self) = @_;
434
435 syslog('info' , "server closing");
436
437 if (!$self->{max_workers}) {
438 # wait for children
439 1 while (waitpid(-1, POSIX::WNOHANG()) > 0);
440 }
441 }
442
443 # please define in subclass
444 #sub hup {
445 # my ($self) = @_;
446 #
447 # syslog('info' , "received signal HUP (restart)");
448 #}
449
450 # please overwrite in subclass
451 sub run {
452 my ($self) = @_;
453
454 for (;;) { # forever
455 syslog('info' , "server is running");
456 sleep(5);
457 }
458 }
459
460 sub start {
461 my ($self, $debug) = @_;
462
463 &$server_run($self, $debug);
464 }
465
466 my $read_pid = sub {
467 my ($self) = @_;
468
469 my $pid_str = PVE::Tools::file_read_firstline($self->{pidfile});
470
471 return 0 if !$pid_str;
472
473 return 0 if $pid_str !~ m/^(\d+)$/; # untaint
474
475 my $pid = int($1);
476
477 return $pid;
478 };
479
480 sub running {
481 my ($self) = @_;
482
483 my $pid = &$read_pid($self);
484
485 if ($pid) {
486 my $res = PVE::ProcFSTools::check_process_running($pid) ? 1 : 0;
487 return wantarray ? ($res, $pid) : $res;
488 }
489
490 return wantarray ? (0, 0) : 0;
491 }
492
493 sub stop {
494 my ($self) = @_;
495
496 my $pid = &$read_pid($self);
497
498 return if !$pid;
499
500 if (PVE::ProcFSTools::check_process_running($pid)) {
501 kill(15, $pid); # send TERM signal
502 # give some time
503 my $wait_time = $self->{stop_wait_time} || 5;
504 my $running = 1;
505 for (my $i = 0; $i < $wait_time; $i++) {
506 $running = PVE::ProcFSTools::check_process_running($pid);
507 last if !$running;
508 sleep (1);
509 }
510
511 syslog('err', "server still running - send KILL") if $running;
512
513 # to be sure
514 kill(9, $pid);
515 waitpid($pid, 0);
516 }
517
518 if (-f $self->{pidfile}) {
519 # try to get the lock
520 &$lockpidfile($self);
521 &$server_cleanup($self);
522 }
523 }
524
525 sub register_start_command {
526 my ($self, $class, $description) = @_;
527
528 $class->register_method({
529 name => 'start',
530 path => 'start',
531 method => 'POST',
532 description => $description || "Start the daemon.",
533 parameters => {
534 additionalProperties => 0,
535 properties => {
536 debug => {
537 description => "Debug mode - stay in foreground",
538 type => "boolean",
539 optional => 1,
540 default => 0,
541 },
542 },
543 },
544 returns => { type => 'null' },
545
546 code => sub {
547 my ($param) = @_;
548
549 $self->start($param->{debug});
550
551 return undef;
552 }});
553 }
554
555 my $reload_daemon = sub {
556 my ($self, $use_hup) = @_;
557
558 if ($self->{env_restart_pve_daemon}) {
559 $self->start();
560 } else {
561 my ($running, $pid) = $self->running();
562 if (!$running) {
563 $self->start();
564 } else {
565 if ($use_hup) {
566 syslog('info', "send HUP to $pid");
567 kill 1, $pid;
568 } else {
569 $self->stop();
570 $self->start();
571 }
572 }
573 }
574 };
575
576 sub register_restart_command {
577 my ($self, $class, $use_hup, $description) = @_;
578
579 $class->register_method({
580 name => 'restart',
581 path => 'restart',
582 method => 'POST',
583 description => $description || "Restart the daemon (or start if not running).",
584 parameters => {
585 additionalProperties => 0,
586 properties => {},
587 },
588 returns => { type => 'null' },
589
590 code => sub {
591 my ($param) = @_;
592
593 &$reload_daemon($self, $use_hup);
594
595 return undef;
596 }});
597 }
598
599 sub register_reload_command {
600 my ($self, $class, $description) = @_;
601
602 $class->register_method({
603 name => 'reload',
604 path => 'reload',
605 method => 'POST',
606 description => $description || "Reload daemon configuration (or start if not running).",
607 parameters => {
608 additionalProperties => 0,
609 properties => {},
610 },
611 returns => { type => 'null' },
612
613 code => sub {
614 my ($param) = @_;
615
616 &$reload_daemon($self, 1);
617
618 return undef;
619 }});
620 }
621
622 sub register_stop_command {
623 my ($self, $class, $description) = @_;
624
625 $class->register_method({
626 name => 'stop',
627 path => 'stop',
628 method => 'POST',
629 description => $description || "Stop the daemon.",
630 parameters => {
631 additionalProperties => 0,
632 properties => {},
633 },
634 returns => { type => 'null' },
635
636 code => sub {
637 my ($param) = @_;
638
639 $self->stop();
640
641 return undef;
642 }});
643 }
644
645 sub register_status_command {
646 my ($self, $class, $description) = @_;
647
648 $class->register_method({
649 name => 'status',
650 path => 'status',
651 method => 'GET',
652 description => "Get daemon status.",
653 parameters => {
654 additionalProperties => 0,
655 properties => {},
656 },
657 returns => {
658 type => 'string',
659 enum => ['stopped', 'running'],
660 },
661 code => sub {
662 my ($param) = @_;
663
664 return $self->running() ? 'running' : 'stopped';
665 }});
666 }
667
668 1;
669