From eead1ad86acb47579aebb104f0d826be3a5d9724 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 31 Dec 2014 10:19:54 +0100 Subject: [PATCH] after_fork_cleanup: move code into separate sub --- data/PVE/Daemon.pm | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) 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 = $@) { -- 2.39.2