From: Dietmar Maurer Date: Wed, 31 Dec 2014 09:19:54 +0000 (+0100) Subject: after_fork_cleanup: move code into separate sub X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=eead1ad86acb47579aebb104f0d826be3a5d9724;ds=sidebyside after_fork_cleanup: move code into separate sub --- diff --git a/data/PVE/Daemon.pm b/data/PVE/Daemon.pm index f03e7cf..3aea787 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 = $@) {