]>
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 $lockpidfile = sub {
41 my $lkfn = $self->{pidfile
} . ".lock";
43 if (!open (FLCK
, ">>$lkfn")) {
44 my $msg = "can't aquire lock on file '$lkfn' - $!";
49 if (!flock (FLCK
, LOCK_EX
|LOCK_NB
)) {
51 my $msg = "can't aquire lock '$lkfn' - $!";
57 my $writepidfile = sub {
60 my $pidfile = $self->{pidfile
};
62 if (!open (PIDFH
, ">$pidfile")) {
63 my $msg = "can't open pid file '$pidfile' - $!";
71 my $server_cleanup = sub {
74 unlink $self->{pidfile
} . ".lock";
75 unlink $self->{pidfile
};
78 my $finish_workers = sub {
81 foreach my $cpid (keys %{$self->{workers
}}) {
82 my $waitpid = waitpid($cpid, WNOHANG
);
83 if (defined($waitpid) && ($waitpid == $cpid)) {
84 delete ($self->{workers
}->{$cpid});
85 syslog
('info', "worker $cpid finished");
90 my $start_workers = sub {
93 return if $self->{terminate
};
96 foreach my $cpid (keys %{$self->{workers
}}) {
100 my $need = $self->{max_workers
} - $count;
102 return if $need <= 0;
104 syslog
('info', "starting $need worker(s)");
109 if (!defined ($pid)) {
110 syslog
('err', "can't fork worker");
112 } elsif ($pid) { # parent
113 $self->{workers
}->{$pid} = 1;
114 syslog
('info', "worker $pid started");
117 $0 = "$self->{name} worker";
121 PVE
::INotify
::inotify_close
();
123 for my $sig (qw(CHLD HUP INT TERM QUIT)) {
124 $SIG{$sig} = 'DEFAULT'; # restore default handler
125 # AnyEvent signals only works if $SIG{XX} is
126 # undefined (perl event loop)
127 delete $SIG{$sig}; # so that we can handle events with AnyEvent
130 eval { $self->run(); };
133 sleep(5); # avoid fast restarts
136 syslog
('info', "worker exit");
142 my $terminate_server = sub {
145 $self->{terminate
} = 1; # set flag to avoid worker restart
147 if (!$self->{max_workers
}) {
148 eval { $self->shutdown(); };
153 eval { $self->shutdown(); };
156 # we have workers - terminate them
158 foreach my $cpid (keys %{$self->{workers
}}) {
159 kill(15, $cpid); # TERM childs
162 # nicely shutdown childs (give them max 10 seconds to shut down)
163 my $previous_alarm = alarm(10);
165 local $SIG{ALRM
} = sub { die "timeout\n" };
167 while ((my $pid = waitpid (-1, 0)) > 0) {
168 if (defined($self->{workers
}->{$pid})) {
169 delete($self->{workers
}->{$pid});
170 syslog
('info', "worker $pid finished");
173 alarm(0); # avoid race condition
177 alarm ($previous_alarm);
180 syslog
('err', "error stopping workers (will kill them now) - $err");
181 foreach my $cpid (keys %{$self->{workers
}}) {
182 # KILL childs still alive!
183 if (kill (0, $cpid)) {
184 delete($self->{workers
}->{$cpid});
185 syslog
("err", "kill worker $cpid");
193 my $server_run = sub {
194 my ($self, $debug) = @_;
196 &$lockpidfile($self);
201 my $restart = $ENV{RESTART_PVE_DAEMON
};
203 delete $ENV{RESTART_PVE_DAEMON
};
205 $self->{debug
} = 1 if $debug;
210 open STDIN
, '</dev/null' || die "can't read /dev/null";
211 open STDOUT
, '>/dev/null' || die "can't write /dev/null";
214 if (!$restart && !$debug) {
215 PVE
::INotify
::inotify_close
();
217 if (!defined ($spid)) {
218 my $msg = "can't put server into background - fork failed";
221 } elsif ($spid) { # parent
224 PVE
::INotify
::inotify_init
();
227 &$writepidfile($self);
232 syslog
('info' , "restarting server");
234 syslog
('info' , "starting server");
237 open STDERR
, '>&STDOUT' || die "can't close STDERR\n";
239 my $old_sig_term = $SIG{TERM
};
240 local $SIG{TERM
} = sub {
241 local ($@, $!, $?); # do not overwrite error vars
242 syslog
('info', "received signal TERM");
243 &$terminate_server($self);
244 &$server_cleanup($self);
245 &$old_sig_term(@_) if $old_sig_term;
248 my $old_sig_quit = $SIG{QUIT
};
249 local $SIG{QUIT
} = sub {
250 local ($@, $!, $?); # do not overwrite error vars
251 syslog
('info', "received signal QUIT");
252 &$terminate_server($self);
253 &$server_cleanup($self);
254 &$old_sig_quit(@_) if $old_sig_quit;
257 my $old_sig_int = $SIG{INT
};
258 local $SIG{INT
} = sub {
259 local ($@, $!, $?); # do not overwrite error vars
260 syslog
('info', "received signal INT");
261 $SIG{INT
} = 'DEFAULT'; # allow to terminate now
262 &$terminate_server($self);
263 &$server_cleanup($self);
264 &$old_sig_int(@_) if $old_sig_int;
268 local ($@, $!, $?); # do not overwrite error vars
269 syslog
('info', "received signal HUP");
270 if ($self->{max_workers
}) {
271 &$terminate_server($self);
272 $self->{got_hup_signal
} = 1;
273 } elsif ($self->can('hup')) {
274 eval { $self->hup() };
280 if ($self->{max_workers
}) {
281 my $old_sig_chld = $SIG{CHLD
};
282 local $SIG{CHLD
} = sub {
283 local ($@, $!, $?); # do not overwrite error vars
284 &$finish_workers($self);
285 &$old_sig_chld(@_) if $old_sig_chld;
289 &$start_workers($self);
291 &$finish_workers($self);
292 last if $self->{terminate
};
302 syslog
('err', "ERROR: $err");
304 # fixme: kill all workers
306 if (my $wait_time = $self->{restart_on_error
}) {
307 $self->restart_daemon($wait_time);
309 $self->exit_daemon(-1);
313 if ($self->{got_hup_signal
}) {
314 $self->restart_daemon();
316 $self->exit_daemon(0);
321 my ($this, $name, $cmdline, %params) = @_;
323 die "please run as root\n" if !$ENV{RESTART_PVE_DAEMON
} && ($> != 0);
325 die "missing name" if !$name;
327 die "can't create more that one PVE::Daemon" if $daemon_initialized;
328 $daemon_initialized = 1;
330 PVE
::INotify
::inotify_init
();
334 my $class = ref($this) || $this;
338 run_dir
=> '/var/run',
342 foreach my $opt (keys %params) {
343 my $value = $params{$opt};
344 if ($opt eq 'restart_on_error') {
345 $self->{$opt} = $value;
346 } elsif ($opt eq 'stop_wait_time') {
347 $self->{$opt} = $value;
348 } elsif ($opt eq 'run_dir') {
349 $self->{$opt} = $value;
350 } elsif ($opt eq 'max_workers') {
351 $self->{$opt} = $value;
353 die "unknown option '$opt'";
357 $self->{pidfile
} = "$self->{run_dir}/${name}.pid";
359 $self->{nodename
} = PVE
::INotify
::nodename
();
361 $self->{cmdline
} = [];
363 foreach my $el (@$cmdline) {
364 $el =~ m/^(.*)$/; # untaint
365 push @{$self->{cmdline
}}, $1;
374 my ($self, $status) = @_;
376 syslog
("info", "server stopped");
378 &$server_cleanup($self);
384 my ($self, $waittime) = @_;
386 syslog
('info', "server shutdown (restart)");
388 $ENV{RESTART_PVE_DAEMON
} = 1;
390 sleep($waittime) if $waittime; # avoid high server load due to restarts
392 PVE
::INotify
::inotify_close
();
394 exec (@{$self->{cmdline
}});
396 exit (-1); # never reached?
399 # please overwrite in subclass
400 # this is called at startup - before forking
406 # please overwrite in subclass
410 syslog
('info' , "server closing");
412 if (!$self->{max_workers
}) {
414 1 while (waitpid(-1, POSIX
::WNOHANG
()) > 0);
418 # please define in subclass
422 # syslog('info' , "received signal HUP (restart)");
425 # please overwrite in subclass
430 syslog
('info' , "server is running");
436 my ($self, $debug) = @_;
438 &$server_run($self, $debug);
444 my $pid_str = PVE
::Tools
::file_read_firstline
($self->{pidfile
});
446 return 0 if !$pid_str;
448 return 0 if $pid_str !~ m/^(\d+)$/; # untaint
458 my $pid = &$read_pid($self);
461 my $res = PVE
::ProcFSTools
::check_process_running
($pid) ?
1 : 0;
462 return wantarray ?
($res, $pid) : $res;
465 return wantarray ?
(0, 0) : 0;
471 my $pid = &$read_pid($self);
475 if (PVE
::ProcFSTools
::check_process_running
($pid)) {
476 kill(15, $pid); # send TERM signal
478 my $wait_time = $self->{stop_wait_time
} || 5;
480 for (my $i = 0; $i < $wait_time; $i++) {
481 $running = PVE
::ProcFSTools
::check_process_running
($pid);
486 syslog
('err', "server still running - send KILL") if $running;
493 if (-f
$self->{pidfile
}) {
494 # try to get the lock
495 &$lockpidfile($self);
496 &$server_cleanup($self);
500 sub register_start_command
{
501 my ($self, $class, $description) = @_;
503 $class->register_method({
507 description
=> $description || "Start the daemon.",
509 additionalProperties
=> 0,
512 description
=> "Debug mode - stay in foreground",
519 returns
=> { type
=> 'null' },
524 $self->start($param->{debug
});
530 my $reload_daemon = sub {
531 my ($self, $use_hup) = @_;
533 if (my $restart = $ENV{RESTART_PVE_DAEMON
}) {
536 my ($running, $pid) = $self->running();
541 syslog
('info', "send HUP to $pid");
551 sub register_restart_command
{
552 my ($self, $class, $use_hup, $description) = @_;
554 $class->register_method({
558 description
=> $description || "Restart the daemon (or start if not running).",
560 additionalProperties
=> 0,
563 returns
=> { type
=> 'null' },
568 &$reload_daemon($self, $use_hup);
574 sub register_reload_command
{
575 my ($self, $class, $description) = @_;
577 $class->register_method({
581 description
=> $description || "Reload daemon configuration (or start if not running).",
583 additionalProperties
=> 0,
586 returns
=> { type
=> 'null' },
591 &$reload_daemon($self, 1);
597 sub register_stop_command
{
598 my ($self, $class, $description) = @_;
600 $class->register_method({
604 description
=> $description || "Stop the daemon.",
606 additionalProperties
=> 0,
609 returns
=> { type
=> 'null' },
620 sub register_status_command
{
621 my ($self, $class, $description) = @_;
623 $class->register_method({
627 description
=> "Get daemon status.",
629 additionalProperties
=> 0,
634 enum
=> ['stopped', 'running'],
639 return $self->running() ?
'running' : 'stopped';