X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FSigfd.pm;h=3d964be36b5c36c350f5d5c5f18af85694b228e0;hb=HEAD;hp=2d27f6a1a7175deeff74d89d8c72017c68d07a09;hpb=95bdac7f09c69036efed537a4d03d5bdd2ae4eb6;p=public-inbox.git diff --git a/lib/PublicInbox/Sigfd.pm b/lib/PublicInbox/Sigfd.pm index 2d27f6a1..3d964be3 100644 --- a/lib/PublicInbox/Sigfd.pm +++ b/lib/PublicInbox/Sigfd.pm @@ -1,42 +1,38 @@ -# Copyright (C) 2019-2020 all contributors +# Copyright (C) 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 PublicInbox::Syscall qw(signalfd EPOLLIN EPOLLET %SIGNUM); use POSIX (); -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 ($class, $sig, $nonblock) = @_; my %signo = map {; - my $cb = $sig->{$_}; - my $num = ($_ eq 'WINCH' && $^O =~ /linux|bsd/i) ? 28 : do { - my $m = "SIG$_"; - POSIX->$m; - }; - $num => $cb; + # $num => $cb; + ($SIGNUM{$_} // POSIX->can("SIG$_")->()) => $sig->{$_} } 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)