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