X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSigfd.pm;h=5d61e6308d65760925058cc466884ed3bb971ab6;hb=0d38f65c490466837ae091afa7a7b6f59d04ce7c;hp=f500902ea67c8fc533922b0de455ccad679d7ce9;hpb=17a3d433a1e8617a1e957da8efe11875efaa0f62;p=public-inbox.git
diff --git a/lib/PublicInbox/Sigfd.pm b/lib/PublicInbox/Sigfd.pm
index f500902e..5d61e630 100644
--- a/lib/PublicInbox/Sigfd.pm
+++ b/lib/PublicInbox/Sigfd.pm
@@ -1,18 +1,19 @@
# Copyright (C) 2019-2020 all contributors
# License: AGPL-3.0+
+
+# 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 PublicInbox::Syscall qw(signalfd EPOLLIN EPOLLET $SFD_NONBLOCK);
+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
@@ -22,6 +23,7 @@ sub new {
};
$num => $cb;
} keys %$sig;
+ my $self = bless { sig => \%signo }, $class;
my $io;
my $fd = signalfd(-1, [keys %signo], $flags);
if (defined $fd && $fd >= 0) {
@@ -31,13 +33,12 @@ sub new {
} else {
return; # wake up every second to check for signals
}
- if ($flags & SFD_NONBLOCK) { # it can go into the event loop
+ if ($flags & $SFD_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,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;