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 local $SIG{WINCH} = sub { $hit->{WINCH}->{normal}++ };
22 for my $s (qw(HUP TERM INT WINCH)) {
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 kill('WINCH', $$) or die "kill $!";
31 my $fd = fileno($sigfd->{sock});
32 ok($fd >= 0, 'fileno(Sigfd->{sock}) works');
34 vec($rvec, $fd, 1) = 1;
35 is(select($rvec, undef, undef, undef), 1, 'select() works');
36 ok($sigfd->wait_once, 'wait_once reported success');
37 for my $s (qw(HUP INT)) {
38 is($hit->{$s}->{sigfd}, 1, "sigfd fired $s");
39 is($hit->{$s}->{normal}, undef,
40 'normal $SIG{$s} not fired');
44 my $nbsig = PublicInbox::Sigfd->new($sig, 1);
45 ok($nbsig, 'Sigfd->new SFD_NONBLOCK works');
46 is($nbsig->wait_once, undef, 'nonblocking ->wait_once');
47 ok($! == Errno::EAGAIN, 'got EAGAIN');
48 kill('HUP', $$) or die "kill $!";
49 PublicInbox::DS->SetPostLoopCallback(sub {}); # loop once
50 PublicInbox::DS::event_loop();
51 is($hit->{HUP}->{sigfd}, 2, 'HUP sigfd fired in event loop') or
52 diag explain($hit); # sometimes fails on FreeBSD 11.x
53 kill('TERM', $$) or die "kill $!";
54 kill('HUP', $$) or die "kill $!";
55 PublicInbox::DS::event_loop();
56 PublicInbox::DS->Reset;
57 is($hit->{TERM}->{sigfd}, 1, 'TERM sigfd fired in event loop');
58 is($hit->{HUP}->{sigfd}, 3, 'HUP sigfd fired in event loop');
59 is($hit->{WINCH}->{sigfd}, 1, 'WINCH sigfd fired in event loop');
61 skip('signalfd disabled?', 10);
63 sigprocmask(SIG_SETMASK, $old) or die "sigprocmask $!";