]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Daemon.pm
ds: block signals when reaping
[public-inbox.git] / lib / PublicInbox / Daemon.pm
index eeac3bd204460d17a4b01450f9f9c1e83e585177..4b738b7c56c7bbc06ce8777ce2a7a63e7dbb75f5 100644 (file)
@@ -1,7 +1,9 @@
-# Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2021 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 httpd, imapd, and nntpd servers.
-# This may be used for read-only IMAP server if we decide to implement it.
+#
+# Contains common daemon code for the httpd, imapd, and nntpd servers
+# and designed for handling thousands of untrusted clients over slow
+# and/or lossy connections.
 package PublicInbox::Daemon;
 use strict;
 use warnings;
@@ -14,7 +16,7 @@ sub SO_ACCEPTFILTER () { 0x1000 }
 STDOUT->autoflush(1);
 STDERR->autoflush(1);
 use PublicInbox::DS qw(now);
-use PublicInbox::Syscall qw($SFD_NONBLOCK);
+use PublicInbox::Syscall qw(SFD_NONBLOCK);
 require PublicInbox::Listener;
 use PublicInbox::EOFpipe;
 use PublicInbox::Sigfd;
@@ -75,7 +77,7 @@ sub accept_tls_opt ($) {
 sub daemon_prepare ($) {
        my ($default_listen) = @_;
        my $listener_names = {}; # sockname => IO::Handle
-       $oldset = PublicInbox::Sigfd::block_signals();
+       $oldset = PublicInbox::DS::block_signals();
        @CMD = ($0, @ARGV);
        my ($prog) = ($CMD[0] =~ m!([^/]+)\z!g);
        my $help = <<EOF;
@@ -419,11 +421,8 @@ sub upgrade { # $_[0] = signal name or number (unused)
 }
 
 sub kill_workers ($) {
-       my ($s) = @_;
-
-       while (my ($pid, $id) = each %pids) {
-               kill $s, $pid;
-       }
+       my ($sig) = @_;
+       kill $sig, keys(%pids);
 }
 
 sub upgrade_aborted ($) {
@@ -516,7 +515,7 @@ EOF
        };
        my $sigfd = PublicInbox::Sigfd->new($sig, 0);
        local %SIG = (%SIG, %$sig) if !$sigfd;
-       PublicInbox::Sigfd::sig_setmask($oldset) if !$sigfd;
+       PublicInbox::DS::sig_setmask($oldset) if !$sigfd;
        while (1) { # main loop
                my $n = scalar keys %pids;
                unless (@listeners) {
@@ -532,7 +531,7 @@ EOF
                }
                my $want = $worker_processes - 1;
                if ($n <= $want) {
-                       PublicInbox::Sigfd::block_signals() if !$sigfd;
+                       PublicInbox::DS::block_signals() if !$sigfd;
                        for my $i ($n..$want) {
                                my $pid = fork;
                                if (!defined $pid) {
@@ -545,7 +544,7 @@ EOF
                                        $pids{$pid} = $i;
                                }
                        }
-                       PublicInbox::Sigfd::sig_setmask($oldset) if !$sigfd;
+                       PublicInbox::DS::sig_setmask($oldset) if !$sigfd;
                }
 
                if ($sigfd) { # Linux and IO::KQueue users:
@@ -628,12 +627,12 @@ sub daemon_loop ($$$$) {
                # this calls epoll_create:
                PublicInbox::Listener->new($_, $tls_cb || $post_accept)
        } @listeners;
-       my $sigfd = PublicInbox::Sigfd->new($sig, $SFD_NONBLOCK);
+       my $sigfd = PublicInbox::Sigfd->new($sig, SFD_NONBLOCK);
        local %SIG = (%SIG, %$sig) if !$sigfd;
        if (!$sigfd) {
                # wake up every second to accept signals if we don't
                # have signalfd or IO::KQueue:
-               PublicInbox::Sigfd::sig_setmask($oldset);
+               PublicInbox::DS::sig_setmask($oldset);
                PublicInbox::DS->SetLoopTimeout(1000);
        }
        PublicInbox::DS->EventLoop;