# 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 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 %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) {
$io = IO::Handle->new_from_fd($fd, 'r+');
} elsif (eval { require PublicInbox::DSKQXS }) {
$io = PublicInbox::DSKQXS->signalfd([keys %signo], $flags);
} else {
return; # wake up every second to check for signals
}
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;
}
}
# PublicInbox::Daemon in master main loop (blocking)
sub wait_once ($) {
my ($self) = @_;
# 128 == sizeof(struct signalfd_siginfo)
my $r = sysread($self->{sock}, my $buf, 128 * 64);
if (defined($r)) {
my $nr = $r / 128 - 1; # $nr may be -1
for my $off (0..$nr) {
# the first uint32_t of signalfd_siginfo: ssi_signo
my $signo = unpack('L', substr($buf, 128 * $off, 4));
my $cb = $self->{sig}->{$signo};
$cb->($signo) if $cb ne 'IGNORE';
}
}
$r;
}
# called by PublicInbox::DS in epoll_wait loop
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;