]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Sigfd.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / Sigfd.pm
index 2d27f6a1a7175deeff74d89d8c72017c68d07a09..db0bf523557c75ed79b0337fff69ffa1efeac96d 100644 (file)
@@ -1,26 +1,29 @@
-# Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# Wraps a signalfd (or similar) for PublicInbox::DS
+# fields: (sig: hashref similar to %SIG, but signal numbers as keys)
 package PublicInbox::Sigfd;
 use strict;
 use parent qw(PublicInbox::DS);
-use fields qw(sig); # hashref similar to %SIG, but signal numbers as keys
 use PublicInbox::Syscall qw(signalfd EPOLLIN EPOLLET SFD_NONBLOCK);
-use POSIX ();
+use POSIX qw(:signal_h);
 use IO::Handle ();
 
 # returns a coderef to unblock signals if neither signalfd or kqueue
 # are available.
 sub new {
        my ($class, $sig, $flags) = @_;
-       my $self = fields::new($class);
        my %signo = map {;
                my $cb = $sig->{$_};
+               # SIGWINCH is 28 on FreeBSD, NetBSD, OpenBSD
                my $num = ($_ eq 'WINCH' && $^O =~ /linux|bsd/i) ? 28 : do {
                        my $m = "SIG$_";
                        POSIX->$m;
                };
                $num => $cb;
        } keys %$sig;
+       my $self = bless { sig => \%signo }, $class;
        my $io;
        my $fd = signalfd(-1, [keys %signo], $flags);
        if (defined $fd && $fd >= 0) {
@@ -34,9 +37,8 @@ sub new {
                $self->SUPER::new($io, EPOLLIN | EPOLLET);
        } else { # master main loop
                $self->{sock} = $io;
+               $self;
        }
-       $self->{sig} = \%signo;
-       $self;
 }
 
 # PublicInbox::Daemon in master main loop (blocking)
@@ -61,4 +63,14 @@ sub event_step {
        while (wait_once($_[0])) {} # non-blocking
 }
 
+sub sig_setmask { sigprocmask(SIG_SETMASK, @_) or die "sigprocmask: $!" }
+
+sub block_signals () {
+       my $oldset = POSIX::SigSet->new;
+       my $newset = POSIX::SigSet->new;
+       $newset->fillset or die "fillset: $!";
+       sig_setmask($newset, $oldset);
+       $oldset;
+}
+
 1;