]>
git.proxmox.com Git - pve-common.git/blob - data/PVE/Daemon.pm
3 # Abstract class to implement Daemons
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')
18 use POSIX
":sys_wait_h";
21 use Time
::HiRes qw
(gettimeofday
);
23 use base
qw(PVE::CLIHandler);
25 $SIG{'__WARN__'} = sub {
30 syslog
('warning', "WARNING: %s", $t);
34 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
36 my $daemon_initialized = 0; # we only allow one instance
38 my $close_daemon_lock = sub {
41 return if !$self->{daemon_lock_fh
};
43 close $self->{daemon_lock_fh
};
44 delete $self->{daemon_lock_fh
};
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
{
53 &$close_daemon_lock($self);
55 PVE
::INotify
::inotify_close
();
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
65 my $lockpidfile = sub {
68 my $lkfn = $self->{pidfile
} . ".lock";
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' - $!";
77 for (my $i = 0; $i < 5; $i ++) {
78 return if flock ($self->{daemon_lock_fh
}, LOCK_EX
|LOCK_NB
);
82 if (!flock ($self->{daemon_lock_fh
}, LOCK_EX
|LOCK_NB
)) {
83 &$close_daemon_lock($self);
84 my $msg = "can't aquire lock '$lkfn' - $!";
90 my $writepidfile = sub {
93 my $pidfile = $self->{pidfile
};
95 if (!open (PIDFH
, ">$pidfile")) {
96 my $msg = "can't open pid file '$pidfile' - $!";
104 my $server_cleanup = sub {
107 unlink $self->{pidfile
} . ".lock";
108 unlink $self->{pidfile
};
111 my $finish_workers = sub {
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");
123 my $start_workers = sub {
126 return if $self->{terminate
};
129 foreach my $cpid (keys %{$self->{workers
}}) {
133 my $need = $self->{max_workers
} - $count;
135 return if $need <= 0;
137 syslog
('info', "starting $need worker(s)");
142 if (!defined ($pid)) {
143 syslog
('err', "can't fork worker");
145 } elsif ($pid) { # parent
146 $self->{workers
}->{$pid} = 1;
147 syslog
('info', "worker $pid started");
150 $0 = "$self->{name} worker";
152 $self->after_fork_cleanup();
154 eval { $self->run(); };
157 sleep(5); # avoid fast restarts
160 syslog
('info', "worker exit");
166 my $terminate_server = sub {
169 $self->{terminate
} = 1; # set flag to avoid worker restart
171 if (!$self->{max_workers
}) {
172 eval { $self->shutdown(); };
177 eval { $self->shutdown(); };
180 # we have workers - terminate them
182 foreach my $cpid (keys %{$self->{workers
}}) {
183 kill(15, $cpid); # TERM childs
186 # nicely shutdown childs (give them max 10 seconds to shut down)
187 my $previous_alarm = alarm(10);
189 local $SIG{ALRM
} = sub { die "timeout\n" };
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");
197 alarm(0); # avoid race condition
201 alarm ($previous_alarm);
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");
217 my $server_run = sub {
218 my ($self, $debug) = @_;
220 &$lockpidfile($self);
225 my $restart = $ENV{RESTART_PVE_DAEMON
};
227 delete $ENV{RESTART_PVE_DAEMON
};
229 $self->{debug
} = 1 if $debug;
234 open STDIN
, '</dev/null' || die "can't read /dev/null";
235 open STDOUT
, '>/dev/null' || die "can't write /dev/null";
238 if (!$restart && !$debug) {
239 PVE
::INotify
::inotify_close
();
241 if (!defined ($spid)) {
242 my $msg = "can't put server into background - fork failed";
245 } elsif ($spid) { # parent
248 PVE
::INotify
::inotify_init
();
251 &$writepidfile($self);
256 syslog
('info' , "restarting server");
258 syslog
('info' , "starting server");
261 open STDERR
, '>&STDOUT' || die "can't close STDERR\n";
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;
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;
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);
287 &$server_cleanup($self);
288 &$old_sig_int(@_) if $old_sig_int;
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')) {
298 eval { $self->hup() };
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;
313 &$start_workers($self);
315 &$finish_workers($self);
316 last if $self->{terminate
};
326 syslog
('err', "ERROR: $err");
328 # fixme: kill all workers
330 if (my $wait_time = $self->{restart_on_error
}) {
331 $self->restart_daemon($wait_time);
333 $self->exit_daemon(-1);
337 if ($self->{got_hup_signal
}) {
338 $self->restart_daemon();
340 $self->exit_daemon(0);
345 my ($this, $name, $cmdline, %params) = @_;
347 die "please run as root\n" if !$ENV{RESTART_PVE_DAEMON
} && ($> != 0);
349 die "missing name" if !$name;
351 die "can't create more that one PVE::Daemon" if $daemon_initialized;
352 $daemon_initialized = 1;
354 PVE
::INotify
::inotify_init
();
358 my $class = ref($this) || $this;
362 run_dir
=> '/var/run',
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;
372 } elsif ($opt eq 'run_dir') {
373 $self->{$opt} = $value;
374 } elsif ($opt eq 'max_workers') {
375 $self->{$opt} = $value;
377 die "unknown option '$opt'";
381 $self->{pidfile
} = "$self->{run_dir}/${name}.pid";
383 $self->{nodename
} = PVE
::INotify
::nodename
();
385 $self->{cmdline
} = [];
387 foreach my $el (@$cmdline) {
388 $el =~ m/^(.*)$/; # untaint
389 push @{$self->{cmdline
}}, $1;
398 my ($self, $status) = @_;
400 syslog
("info", "server stopped");
402 &$server_cleanup($self);
408 my ($self, $waittime) = @_;
410 syslog
('info', "server shutdown (restart)");
412 $ENV{RESTART_PVE_DAEMON
} = 1;
414 sleep($waittime) if $waittime; # avoid high server load due to restarts
416 PVE
::INotify
::inotify_close
();
418 exec (@{$self->{cmdline
}});
420 exit (-1); # never reached?
423 # please overwrite in subclass
424 # this is called at startup - before forking
430 # please overwrite in subclass
434 syslog
('info' , "server closing");
436 if (!$self->{max_workers
}) {
438 1 while (waitpid(-1, POSIX
::WNOHANG
()) > 0);
442 # please define in subclass
446 # syslog('info' , "received signal HUP (restart)");
449 # please overwrite in subclass
454 syslog
('info' , "server is running");
460 my ($self, $debug) = @_;
462 &$server_run($self, $debug);
468 my $pid_str = PVE
::Tools
::file_read_firstline
($self->{pidfile
});
470 return 0 if !$pid_str;
472 return 0 if $pid_str !~ m/^(\d+)$/; # untaint
482 my $pid = &$read_pid($self);
485 my $res = PVE
::ProcFSTools
::check_process_running
($pid) ?
1 : 0;
486 return wantarray ?
($res, $pid) : $res;
489 return wantarray ?
(0, 0) : 0;
495 my $pid = &$read_pid($self);
499 if (PVE
::ProcFSTools
::check_process_running
($pid)) {
500 kill(15, $pid); # send TERM signal
502 my $wait_time = $self->{stop_wait_time
} || 5;
504 for (my $i = 0; $i < $wait_time; $i++) {
505 $running = PVE
::ProcFSTools
::check_process_running
($pid);
510 syslog
('err', "server still running - send KILL") if $running;
517 if (-f
$self->{pidfile
}) {
518 # try to get the lock
519 &$lockpidfile($self);
520 &$server_cleanup($self);
524 sub register_start_command
{
525 my ($self, $class, $description) = @_;
527 $class->register_method({
531 description
=> $description || "Start the daemon.",
533 additionalProperties
=> 0,
536 description
=> "Debug mode - stay in foreground",
543 returns
=> { type
=> 'null' },
548 $self->start($param->{debug
});
554 my $reload_daemon = sub {
555 my ($self, $use_hup) = @_;
557 if (my $restart = $ENV{RESTART_PVE_DAEMON
}) {
560 my ($running, $pid) = $self->running();
565 syslog
('info', "send HUP to $pid");
575 sub register_restart_command
{
576 my ($self, $class, $use_hup, $description) = @_;
578 $class->register_method({
582 description
=> $description || "Restart the daemon (or start if not running).",
584 additionalProperties
=> 0,
587 returns
=> { type
=> 'null' },
592 &$reload_daemon($self, $use_hup);
598 sub register_reload_command
{
599 my ($self, $class, $description) = @_;
601 $class->register_method({
605 description
=> $description || "Reload daemon configuration (or start if not running).",
607 additionalProperties
=> 0,
610 returns
=> { type
=> 'null' },
615 &$reload_daemon($self, 1);
621 sub register_stop_command
{
622 my ($self, $class, $description) = @_;
624 $class->register_method({
628 description
=> $description || "Stop the daemon.",
630 additionalProperties
=> 0,
633 returns
=> { type
=> 'null' },
644 sub register_status_command
{
645 my ($self, $class, $description) = @_;
647 $class->register_method({
651 description
=> "Get daemon status.",
653 additionalProperties
=> 0,
658 enum
=> ['stopped', 'running'],
663 return $self->running() ?
'running' : 'stopped';