]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Daemon.pm
daemon: unlink .oldbin PID file correctly
[public-inbox.git] / lib / PublicInbox / Daemon.pm
index 9db472a19479b5fdfd271c6c60c6597116cadbe9..4ff7cad493990d81c6ce4d2e896f63cfe7a87032 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # contains common daemon code for the nntpd and httpd servers.
 # This may be used for read-only IMAP server if we decide to implement it.
@@ -6,7 +6,7 @@ package PublicInbox::Daemon;
 use strict;
 use warnings;
 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
-use IO::Handle;
+use IO::Handle; # ->autoflush
 use IO::Socket;
 use POSIX qw(WNOHANG :signal_h);
 use Socket qw(IPPROTO_TCP SOL_SOCKET);
@@ -247,7 +247,7 @@ sub daemonize () {
 
        write_pid($pid_file);
        # for ->DESTROY:
-       bless { pid => $$, pid_file => $pid_file }, __PACKAGE__;
+       bless { pid => $$, pid_file => \$pid_file }, __PACKAGE__;
 }
 
 sub worker_quit { # $_[0] = signal name or number (unused)
@@ -403,6 +403,9 @@ sub upgrade { # $_[0] = signal name or number (unused)
                $ENV{LISTEN_FDS} = scalar @listeners;
                $ENV{LISTEN_PID} = $$;
                foreach my $s (@listeners) {
+                       # @listeners are globs with workers, PI::L w/o workers
+                       $s = $s->{sock} if ref($s) eq 'PublicInbox::Listener';
+
                        my $fl = fcntl($s, F_GETFD, 0);
                        fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
                }
@@ -566,11 +569,12 @@ sub defer_accept ($$) {
        my ($s, $af_name) = @_;
        return unless defined $af_name;
        if ($^O eq 'linux') {
-               my $x = getsockopt($s, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT());
+               my $TCP_DEFER_ACCEPT = 9; # Socket::TCP_DEFER_ACCEPT is in 5.14+
+               my $x = getsockopt($s, IPPROTO_TCP, $TCP_DEFER_ACCEPT);
                return unless defined $x; # may be Unix socket
                my $sec = unpack('i', $x);
                return if $sec > 0; # systemd users may set a higher value
-               setsockopt($s, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT(), 1);
+               setsockopt($s, IPPROTO_TCP, $TCP_DEFER_ACCEPT, 1);
        } elsif ($^O eq 'freebsd') {
                my $x = getsockopt($s, SOL_SOCKET, SO_ACCEPTFILTER);
                return if defined $x; # don't change if set
@@ -601,11 +605,10 @@ sub daemon_loop ($$$$) {
                WINCH => 'IGNORE',
                CHLD => \&PublicInbox::DS::enqueue_reap,
        };
-       my $parent_pipe;
        if ($worker_processes > 0) {
                $refresh->(); # preload by default
                my $fh = master_loop(); # returns if in child process
-               $parent_pipe = PublicInbox::ParentPipe->new($fh, *worker_quit);
+               PublicInbox::ParentPipe->new($fh, \&worker_quit);
        } else {
                reopen_logs();
                $set_user->() if $set_user;
@@ -637,6 +640,7 @@ sub daemon_loop ($$$$) {
 
 sub run ($$$;$) {
        my ($default, $refresh, $post_accept, $nntpd) = @_;
+       local $SIG{PIPE} = 'IGNORE';
        daemon_prepare($default);
        my $af_default = $default =~ /:8080\z/ ? 'httpready' : undef;
        my $for_destroy = daemonize();
@@ -659,7 +663,7 @@ sub write_pid ($) {
 }
 
 sub DESTROY {
-       unlink_pid_file_safe_ish($_[0]->{pid}, $_[0]->{pid_file});
+       unlink_pid_file_safe_ish($_[0]->{pid}, ${$_[0]->{pid_file}});
 }
 
 1;