]>
git.proxmox.com Git - pve-common.git/blob - data/PVE/Daemon.pm
1436f17bafb06cdf4defea3682331a035590c153
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 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
27 my $daemon_initialized = 0; # we only allow one instance
29 my $close_daemon_lock = sub {
32 return if !$self->{daemon_lock_fh
};
34 close $self->{daemon_lock_fh
};
35 delete $self->{daemon_lock_fh
};
41 print STDERR
"$msg\n";
42 syslog
('err', "%s", $msg);
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
{
51 &$close_daemon_lock($self);
53 PVE
::INotify
::inotify_close
();
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
63 my $lockpidfile = sub {
66 my $lkfn = $self->{pidfile
} . ".lock";
70 if (my $fd = $self->{env_pve_lock_fd
}) {
72 $self->{daemon_lock_fh
} = IO
::Handle-
>new_from_fd($fd, "a");
77 $self->{daemon_lock_fh
} = IO
::File-
>new(">>$lkfn");
80 if (!$self->{daemon_lock_fh
}) {
81 die "can't open lock '$lkfn' - $!\n";
84 for (my $i = 0; $i < $waittime; $i ++) {
85 return if flock ($self->{daemon_lock_fh
}, LOCK_EX
|LOCK_NB
);
89 if (!flock ($self->{daemon_lock_fh
}, LOCK_EX
|LOCK_NB
)) {
90 &$close_daemon_lock($self);
93 my ($running, $pid) = $self->running();
95 die "can't aquire lock '$lkfn' - daemon already started (pid = $pid)\n";
97 die "can't aquire lock '$lkfn' - $err\n";
102 my $writepidfile = sub {
105 my $pidfile = $self->{pidfile
};
107 die "can't open pid file '$pidfile' - $!\n" if !open (PIDFH
, ">$pidfile");
113 my $server_cleanup = sub {
116 unlink $self->{pidfile
} . ".lock";
117 unlink $self->{pidfile
};
120 my $finish_workers = sub {
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");
132 my $start_workers = sub {
135 return if $self->{terminate
};
138 foreach my $cpid (keys %{$self->{workers
}}) {
142 my $need = $self->{max_workers
} - $count;
144 return if $need <= 0;
146 syslog
('info', "starting $need worker(s)");
151 if (!defined ($pid)) {
152 syslog
('err', "can't fork worker");
154 } elsif ($pid) { # parent
155 $self->{workers
}->{$pid} = 1;
156 syslog
('info', "worker $pid started");
159 $0 = "$self->{name} worker";
161 $self->after_fork_cleanup();
163 eval { $self->run(); };
166 sleep(5); # avoid fast restarts
169 syslog
('info', "worker exit");
175 my $terminate_server = sub {
178 $self->{terminate
} = 1; # set flag to avoid worker restart
180 if (!$self->{max_workers
}) {
181 eval { $self->shutdown(); };
186 eval { $self->shutdown(); };
189 # we have workers - terminate them
191 foreach my $cpid (keys %{$self->{workers
}}) {
192 kill(15, $cpid); # TERM childs
195 # nicely shutdown childs (give them max 10 seconds to shut down)
196 my $previous_alarm = alarm(10);
198 local $SIG{ALRM
} = sub { die "timeout\n" };
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");
206 alarm(0); # avoid race condition
210 alarm ($previous_alarm);
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");
226 my $server_run = sub {
227 my ($self, $debug) = @_;
229 # fixme: handle restart lockfd
230 &$lockpidfile($self);
232 # remove FD_CLOEXEC bit to reuse on exec
233 $self->{daemon_lock_fh
}->fcntl(Fcntl
::F_SETFD
(), 0);
235 $ENV{PVE_DAEMON_LOCK_FD
} = $self->{daemon_lock_fh
}->fileno;
240 $self->{debug
} = 1 if $debug;
245 open STDIN
, '</dev/null' || die "can't read /dev/null";
246 open STDOUT
, '>/dev/null' || die "can't write /dev/null";
249 if (!$self->{env_restart_pve_daemon
} && !$debug) {
250 PVE
::INotify
::inotify_close
();
252 if (!defined ($spid)) {
253 die "can't put server into background - fork failed";
254 } elsif ($spid) { # parent
257 PVE
::INotify
::inotify_init
();
260 if ($self->{env_restart_pve_daemon
}) {
261 syslog
('info' , "restarting server");
263 &$writepidfile($self);
264 syslog
('info' , "starting server");
269 open STDERR
, '>&STDOUT' || die "can't close STDERR\n";
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;
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;
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;
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() };
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;
321 &$start_workers($self);
323 &$finish_workers($self);
324 last if $self->{terminate
};
334 syslog
('err', "ERROR: $err");
336 # fixme: kill all workers
338 if (my $wait_time = $self->{restart_on_error
}) {
339 $self->restart_daemon($wait_time);
341 $self->exit_daemon(-1);
345 if ($self->{got_hup_signal
}) {
346 $self->restart_daemon();
348 $self->exit_daemon(0);
353 my ($this, $name, $cmdline, %params) = @_;
355 $name = 'daemon' if !$name; # should not happen
363 my $restart = $ENV{RESTART_PVE_DAEMON
};
364 delete $ENV{RESTART_PVE_DAEMON
};
366 my $lockfd = $ENV{PVE_DAEMON_LOCK_FD
};
367 delete $ENV{PVE_DAEMON_LOCK_FD
};
369 if (defined($lockfd)) {
370 die "unable to parse lock fd '$lockfd'\n"
371 if $lockfd !~ m/^(\d+)$/;
372 $lockfd = $1; # untaint
375 die "please run as root\n" if !$restart && ($> != 0);
377 die "can't create more that one PVE::Daemon" if $daemon_initialized;
378 $daemon_initialized = 1;
380 PVE
::INotify
::inotify_init
();
382 my $class = ref($this) || $this;
386 run_dir
=> '/var/run',
387 env_restart_pve_daemon
=> $restart,
388 env_pve_lock_fd
=> $lockfd,
392 foreach my $opt (keys %params) {
393 my $value = $params{$opt};
394 if ($opt eq 'restart_on_error') {
395 $self->{$opt} = $value;
396 } elsif ($opt eq 'stop_wait_time') {
397 $self->{$opt} = $value;
398 } elsif ($opt eq 'run_dir') {
399 $self->{$opt} = $value;
400 } elsif ($opt eq 'max_workers') {
401 $self->{$opt} = $value;
403 die "unknown daemon option '$opt'\n";
407 $self->{pidfile
} = "$self->{run_dir}/${name}.pid";
409 $self->{nodename
} = PVE
::INotify
::nodename
();
411 $self->{cmdline
} = [];
413 foreach my $el (@$cmdline) {
414 $el =~ m/^(.*)$/; # untaint
415 push @{$self->{cmdline
}}, $1;
429 my ($self, $status) = @_;
431 syslog
("info", "server stopped");
433 &$server_cleanup($self);
439 my ($self, $waittime) = @_;
441 syslog
('info', "server shutdown (restart)");
443 $ENV{RESTART_PVE_DAEMON
} = 1;
445 sleep($waittime) if $waittime; # avoid high server load due to restarts
447 PVE
::INotify
::inotify_close
();
449 exec (@{$self->{cmdline
}});
451 exit (-1); # never reached?
454 # please overwrite in subclass
455 # this is called at startup - before forking
461 # please overwrite in subclass
465 syslog
('info' , "server closing");
467 if (!$self->{max_workers
}) {
469 1 while (waitpid(-1, POSIX
::WNOHANG
()) > 0);
473 # please define in subclass
477 # syslog('info' , "received signal HUP (restart)");
480 # please overwrite in subclass
485 syslog
('info' , "server is running");
491 my ($self, $debug) = @_;
493 eval { &$server_run($self, $debug); };
495 &$log_err("start failed - $err");
503 my $pid_str = PVE
::Tools
::file_read_firstline
($self->{pidfile
});
505 return 0 if !$pid_str;
507 return 0 if $pid_str !~ m/^(\d+)$/; # untaint
517 my $pid = &$read_pid($self);
520 my $res = PVE
::ProcFSTools
::check_process_running
($pid) ?
1 : 0;
521 return wantarray ?
($res, $pid) : $res;
524 return wantarray ?
(0, 0) : 0;
530 my $pid = &$read_pid($self);
534 if (PVE
::ProcFSTools
::check_process_running
($pid)) {
535 kill(15, $pid); # send TERM signal
537 my $wait_time = $self->{stop_wait_time
} || 5;
539 for (my $i = 0; $i < $wait_time; $i++) {
540 $running = PVE
::ProcFSTools
::check_process_running
($pid);
545 syslog
('err', "server still running - send KILL") if $running;
552 if (-f
$self->{pidfile
}) {
554 # try to get the lock
555 &$lockpidfile($self);
556 &$server_cleanup($self);
559 &$log_err("cleanup failed - $err");
564 sub register_start_command
{
565 my ($self, $description) = @_;
567 my $class = ref($self);
569 $class->register_method({
573 description
=> $description || "Start the daemon.",
575 additionalProperties
=> 0,
578 description
=> "Debug mode - stay in foreground",
585 returns
=> { type
=> 'null' },
590 $self->start($param->{debug
});
596 my $reload_daemon = sub {
597 my ($self, $use_hup) = @_;
599 if ($self->{env_restart_pve_daemon
}) {
602 my ($running, $pid) = $self->running();
607 syslog
('info', "send HUP to $pid");
617 sub register_restart_command
{
618 my ($self, $use_hup, $description) = @_;
620 my $class = ref($self);
622 $class->register_method({
626 description
=> $description || "Restart the daemon (or start if not running).",
628 additionalProperties
=> 0,
631 returns
=> { type
=> 'null' },
636 &$reload_daemon($self, $use_hup);
642 sub register_reload_command
{
643 my ($self, $description) = @_;
645 my $class = ref($self);
647 $class->register_method({
651 description
=> $description || "Reload daemon configuration (or start if not running).",
653 additionalProperties
=> 0,
656 returns
=> { type
=> 'null' },
661 &$reload_daemon($self, 1);
667 sub register_stop_command
{
668 my ($self, $description) = @_;
670 my $class = ref($self);
672 $class->register_method({
676 description
=> $description || "Stop the daemon.",
678 additionalProperties
=> 0,
681 returns
=> { type
=> 'null' },
692 sub register_status_command
{
693 my ($self, $description) = @_;
695 my $class = ref($self);
697 $class->register_method({
701 description
=> "Get daemon status.",
703 additionalProperties
=> 0,
708 enum
=> ['stopped', 'running'],
713 return $self->running() ?
'running' : 'stopped';