X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=data%2FPVE%2FDaemon.pm;h=752522940aa6170cf1bcf45e5380d6a93b5da3d7;hb=a8ba2293d48b39a1530b004bb618bb99260e4e7d;hp=f03e7cf9f0aec047e9eb5a896dd698212913e624;hpb=3206cdf6778e6389f68f42a889b672f7dcc93ef4;p=pve-common.git diff --git a/data/PVE/Daemon.pm b/data/PVE/Daemon.pm index f03e7cf..7525229 100644 --- a/data/PVE/Daemon.pm +++ b/data/PVE/Daemon.pm @@ -38,10 +38,30 @@ my $daemon_initialized = 0; # we only allow one instance my $close_daemon_lock = sub { my ($self) = @_; + return if !$self->{daemon_lock_fh}; + close $self->{daemon_lock_fh}; delete $self->{daemon_lock_fh}; }; +# call this if you fork() from child +# Note: we already call this for workers, so it is only required +# if you fork inside a simple daemon (max_workers == 0). +sub after_fork_cleanup { + my ($self) = @_; + + &$close_daemon_lock($self); + + PVE::INotify::inotify_close(); + + for my $sig (qw(CHLD HUP INT TERM QUIT)) { + $SIG{$sig} = 'DEFAULT'; # restore default handler + # AnyEvent signals only works if $SIG{XX} is + # undefined (perl event loop) + delete $SIG{$sig}; # so that we can handle events with AnyEvent + } +} + my $lockpidfile = sub { my ($self) = @_; @@ -129,16 +149,7 @@ my $start_workers = sub { } else { $0 = "$self->{name} worker"; - &$close_daemon_lock($self); - - PVE::INotify::inotify_close(); - - for my $sig (qw(CHLD HUP INT TERM QUIT)) { - $SIG{$sig} = 'DEFAULT'; # restore default handler - # AnyEvent signals only works if $SIG{XX} is - # undefined (perl event loop) - delete $SIG{$sig}; # so that we can handle events with AnyEvent - } + $self->after_fork_cleanup(); eval { $self->run(); }; if (my $err = $@) { @@ -211,10 +222,6 @@ my $server_run = sub { # run in background my $spid; - my $restart = $ENV{RESTART_PVE_DAEMON}; - - delete $ENV{RESTART_PVE_DAEMON}; - $self->{debug} = 1 if $debug; $self->init(); @@ -224,7 +231,7 @@ my $server_run = sub { open STDOUT, '>/dev/null' || die "can't write /dev/null"; } - if (!$restart && !$debug) { + if (!$self->{env_restart_pve_daemon} && !$debug) { PVE::INotify::inotify_close(); $spid = fork(); if (!defined ($spid)) { @@ -241,7 +248,7 @@ my $server_run = sub { POSIX::setsid(); - if ($restart) { + if ($self->{env_restart_pve_daemon}) { syslog('info' , "restarting server"); } else { syslog('info' , "starting server"); @@ -333,7 +340,11 @@ my $server_run = sub { sub new { my ($this, $name, $cmdline, %params) = @_; - die "please run as root\n" if !$ENV{RESTART_PVE_DAEMON} && ($> != 0); + my $restart = $ENV{RESTART_PVE_DAEMON}; + + delete $ENV{RESTART_PVE_DAEMON}; + + die "please run as root\n" if !$restart && ($> != 0); die "missing name" if !$name; @@ -349,6 +360,7 @@ sub new { my $self = bless { name => $name, run_dir => '/var/run', + env_restart_pve_daemon => $restart, workers => {}, }, $class; @@ -543,7 +555,7 @@ sub register_start_command { my $reload_daemon = sub { my ($self, $use_hup) = @_; - if (my $restart = $ENV{RESTART_PVE_DAEMON}) { + if ($self->{env_restart_pve_daemon}) { $self->start(); } else { my ($running, $pid) = $self->running();