]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Sigfd.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / Sigfd.pm
index 17456592a7e52718d9ca595b282962f53c898ea3..81e5a1b1dd88e99d25bf37aadbb1f520e8a6503f 100644 (file)
@@ -1,18 +1,18 @@
-# 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 qw(:signal_h);
-use IO::Handle ();
+use PublicInbox::Syscall qw(signalfd EPOLLIN EPOLLET);
+use POSIX ();
 
 # 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 ($class, $sig, $nonblock) = @_;
        my %signo = map {;
                my $cb = $sig->{$_};
                # SIGWINCH is 28 on FreeBSD, NetBSD, OpenBSD
@@ -22,22 +22,22 @@ sub new {
                };
                $num => $cb;
        } keys %$sig;
+       my $self = bless { sig => \%signo }, $class;
        my $io;
-       my $fd = signalfd(-1, [keys %signo], $flags);
+       my $fd = signalfd([keys %signo], $nonblock);
        if (defined $fd && $fd >= 0) {
-               $io = IO::Handle->new_from_fd($fd, 'r+');
+               open($io, '+<&=', $fd) or die "open: $!";
        } elsif (eval { require PublicInbox::DSKQXS }) {
-               $io = PublicInbox::DSKQXS->signalfd([keys %signo], $flags);
+               $io = PublicInbox::DSKQXS->signalfd([keys %signo], $nonblock);
        } else {
                return; # wake up every second to check for signals
        }
-       if ($flags & SFD_NONBLOCK) { # it can go into the event loop
+       if ($nonblock) { # it can go into the event loop
                $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)
@@ -62,14 +62,4 @@ 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;