]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Daemon.pm
daemon: drop listener sockets ASAP on termination
[public-inbox.git] / lib / PublicInbox / Daemon.pm
index b8482d36c6de884c200872417d22f96459730994..c3199cd24f0a9859261408fe479edf535b916544 100644 (file)
@@ -1,14 +1,13 @@
 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
-package PublicInbox::Daemon; # empty class :p
-
 # 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.
-package main;
+package PublicInbox::Daemon;
 use strict;
 use warnings;
 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
 use IO::Handle;
+use IO::Socket;
 STDOUT->autoflush(1);
 STDERR->autoflush(1);
 require Danga::Socket;
@@ -54,25 +53,43 @@ sub daemon_prepare ($) {
 
        foreach my $l (@cfg_listen) {
                next if $listener_names{$l}; # already inherited
-               require IO::Socket::INET6; # works for IPv4, too
-               my %o = (
-                       LocalAddr => $l,
-                       ReuseAddr => 1,
-                       Proto => 'tcp',
-               );
-               if (my $s = IO::Socket::INET6->new(%o)) {
+               my (%o, $sock_pkg);
+               if (index($l, '/') == 0) {
+                       $sock_pkg = 'IO::Socket::UNIX';
+                       eval "use $sock_pkg";
+                       die $@ if $@;
+                       %o = (Type => SOCK_STREAM, Peer => $l);
+                       if (-S $l) {
+                               my $c = $sock_pkg->new(%o);
+                               if (!defined($c) && $!{ECONNREFUSED}) {
+                                       unlink $l or die
+"failed to unlink stale socket=$l: $!\n";
+                               } # else: let the bind fail
+                       }
+                       $o{Local} = delete $o{Peer};
+               } else {
+                       $sock_pkg = 'IO::Socket::INET6'; # works for IPv4, too
+                       eval "use $sock_pkg";
+                       die $@ if $@;
+                       %o = (LocalAddr => $l, ReuseAddr => 1, Proto => 'tcp');
+               }
+               $o{Listen} = 1024;
+               my $prev = umask 0000;
+               my $s = eval { $sock_pkg->new(%o) };
+               warn "error binding $l: $!\n" unless $s;
+               umask $prev;
+
+               if ($s) {
                        $listener_names{sockname($s)} = $s;
                        push @listeners, $s;
-               } else {
-                       warn "error binding $l: $!\n";
                }
        }
-       die 'No listeners bound' unless @listeners;
+       die "No listeners bound\n" unless @listeners;
 }
 
 sub daemonize () {
-       chdir '/' or die "chdir failed: $!\n";
-       open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!\n";
+       chdir '/' or die "chdir failed: $!";
+       open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!";
 
        return unless (defined $pid_file || defined $group || defined $user
                        || $daemonize);
@@ -120,6 +137,7 @@ sub worker_quit () {
        # killing again terminates immediately:
        exit unless @listeners;
 
+       $_->close foreach @listeners; # call Danga::Socket::close
        @listeners = ();
 
        # give slow clients 30s to finish reading/writing whatever
@@ -159,20 +177,28 @@ sub reopen_logs {
 
 sub sockname ($) {
        my ($s) = @_;
-       my $n = getsockname($s) or return;
-       my ($port, $addr);
-       if (length($n) >= 28) {
-               require Socket6;
-               ($port, $addr) = Socket6::unpack_sockaddr_in6($n);
-       } else {
-               ($port, $addr) = Socket::sockaddr_in($n);
-       }
-       if (length($addr) == 4) {
-               $n = Socket::inet_ntoa($addr)
-       } else {
-               $n = '['.Socket6::inet_ntop(Socket6::AF_INET6(), $addr).']';
-       }
-       $n .= ":$port";
+       my $addr = getsockname($s) or return;
+       my ($host, $port) = host_with_port($addr);
+       "$host:$port";
+}
+
+sub host_with_port ($) {
+       my ($addr) = @_;
+       my ($port, $host);
+
+       # this eval will die on Unix sockets:
+       eval {
+               if (length($addr) >= 28) {
+                       require Socket6;
+                       ($port, $host) = Socket6::unpack_sockaddr_in6($addr);
+                       $host = Socket6::inet_ntop(Socket6::AF_INET6(), $host);
+                       $host = "[$host]";
+               } else {
+                       ($port, $host) = Socket::sockaddr_in($addr);
+                       $host = Socket::inet_ntoa($host);
+               }
+       };
+       $@ ? ('127.0.0.1', 0) : ($host, $port);
 }
 
 sub inherit () {
@@ -181,8 +207,7 @@ sub inherit () {
        my $end = $fds + 2; # LISTEN_FDS_START - 1
        my @rv = ();
        foreach my $fd (3..$end) {
-               my $s = IO::Handle->new;
-               $s->fdopen($fd, 'r');
+               my $s = IO::Handle->new_from_fd($fd, 'r');
                if (my $k = sockname($s)) {
                        $listener_names{$k} = $s;
                        push @rv, $s;
@@ -238,12 +263,10 @@ sub do_fork () {
        my $new = POSIX::SigSet->new;
        $new->fillset;
        my $old = POSIX::SigSet->new;
-       POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new, $old) or
-                               die "SIG_BLOCK: $!\n";
+       POSIX::sigprocmask(&POSIX::SIG_BLOCK, $new, $old) or die "SIG_BLOCK: $!";
        my $pid = fork;
        my $err = $!;
-       POSIX::sigprocmask(&POSIX::SIG_SETMASK, $old) or
-                               die "SIG_SETMASK: $!\n";
+       POSIX::sigprocmask(&POSIX::SIG_SETMASK, $old) or die "SIG_SETMASK: $!";
        ($pid, $err);
 }
 
@@ -254,7 +277,7 @@ sub upgrade_aborted ($) {
        return unless $pid_file;
 
        my $file = $pid_file;
-       $file =~ s/\.oldbin\z// or die "BUG: no '.oldbin' suffix in $file\n";
+       $file =~ s/\.oldbin\z// or die "BUG: no '.oldbin' suffix in $file";
        unlink_pid_file_safe_ish($$, $pid_file);
        $pid_file = $file;
        eval { write_pid($pid_file) };
@@ -289,8 +312,8 @@ sub unlink_pid_file_safe_ish ($$) {
 }
 
 sub master_loop {
-       pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!\n";
-       pipe(my ($r, $w)) or die "failed to create self-pipe: $!\n";
+       pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!";
+       pipe(my ($r, $w)) or die "failed to create self-pipe: $!";
        IO::Handle::blocking($w, 0);
        my $set_workers = $worker_processes;
        my @caught;
@@ -379,13 +402,15 @@ sub daemon_loop ($$) {
        $SIG{USR1} = *reopen_logs;
        $SIG{HUP} = $refresh;
        # this calls epoll_create:
-       PublicInbox::Listener->new($_, $post_accept) for @listeners;
+       @listeners = map {
+               PublicInbox::Listener->new($_, $post_accept)
+       } @listeners;
        Danga::Socket->EventLoop;
        $parent_pipe = undef;
 }
 
 
-sub daemon_run ($$$) {
+sub run ($$$) {
        my ($default, $refresh, $post_accept) = @_;
        daemon_prepare($default);
        daemonize();