1 # Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
5 use POSIX qw(:signal_h);
7 use PublicInbox::Syscall qw(SFD_NONBLOCK);
8 require_ok 'PublicInbox::Sigfd';
12 if ($^O ne 'linux' && !eval { require IO::KQueue }) {
13 skip 'signalfd requires Linux or IO::KQueue to emulate', 10;
16 my $old = PublicInbox::DS::block_signals();
19 local $SIG{HUP} = sub { $hit->{HUP}->{normal}++ };
20 local $SIG{TERM} = sub { $hit->{TERM}->{normal}++ };
21 local $SIG{INT} = sub { $hit->{INT}->{normal}++ };
22 for my $s (qw(HUP TERM INT)) {
23 $sig->{$s} = sub { $hit->{$s}->{sigfd}++ };
25 my $sigfd = PublicInbox::Sigfd->new($sig, 0);
27 ok($sigfd, 'Sigfd->new works');
28 kill('HUP', $$) or die "kill $!";
29 kill('INT', $$) or die "kill $!";
30 my $fd = fileno($sigfd->{sock});
31 ok($fd >= 0, 'fileno(Sigfd->{sock}) works');
33 vec($rvec, $fd, 1) = 1;
34 is(select($rvec, undef, undef, undef), 1, 'select() works');
35 ok($sigfd->wait_once, 'wait_once reported success');
36 for my $s (qw(HUP INT)) {
37 is($hit->{$s}->{sigfd}, 1, "sigfd fired $s");
38 is($hit->{$s}->{normal}, undef,
39 'normal $SIG{$s} not fired');
43 my $nbsig = PublicInbox::Sigfd->new($sig, SFD_NONBLOCK);
44 ok($nbsig, 'Sigfd->new SFD_NONBLOCK works');
45 is($nbsig->wait_once, undef, 'nonblocking ->wait_once');
46 ok($! == Errno::EAGAIN, 'got EAGAIN');
47 kill('HUP', $$) or die "kill $!";
48 PublicInbox::DS->SetPostLoopCallback(sub {}); # loop once
49 PublicInbox::DS->EventLoop;
50 is($hit->{HUP}->{sigfd}, 2, 'HUP sigfd fired in event loop') or
51 diag explain($hit); # sometimes fails on FreeBSD 11.x
52 kill('TERM', $$) or die "kill $!";
53 kill('HUP', $$) or die "kill $!";
54 PublicInbox::DS->EventLoop;
55 PublicInbox::DS->Reset;
56 is($hit->{TERM}->{sigfd}, 1, 'TERM sigfd fired in event loop');
57 is($hit->{HUP}->{sigfd}, 3, 'HUP sigfd fired in event loop');
59 skip('signalfd disabled?', 10);
61 sigprocmask(SIG_SETMASK, $old) or die "sigprocmask $!";