2 # Copyright (C) all contributors <meta@public-inbox.org>
6 use POSIX qw(:signal_h);
8 require_ok 'PublicInbox::Sigfd';
10 my ($linux_sigfd, $has_sigfd);
14 if ($^O ne 'linux' && !eval { require IO::KQueue }) {
15 skip 'signalfd requires Linux or IO::KQueue to emulate', 10;
18 my $old = PublicInbox::DS::block_signals();
21 local $SIG{USR2} = sub { $hit->{USR2}++ };
22 local $SIG{HUP} = sub { $hit->{HUP}++ };
23 local $SIG{TERM} = sub { $hit->{TERM}++ };
24 local $SIG{INT} = sub { $hit->{INT}++ };
25 local $SIG{WINCH} = sub { $hit->{WINCH}++ };
26 for my $s (qw(USR2 HUP TERM INT WINCH)) {
27 $sig->{$s} = sub { die "SHOULD NOT BE CALLED ($s)" }
31 ok(!defined($hit->{USR2}), 'no USR2 yet') or diag explain($hit);
32 PublicInbox::DS->Reset;
33 ok($PublicInbox::Syscall::SIGNUM{WINCH}, 'SIGWINCH number defined');
34 my $sigfd = PublicInbox::Sigfd->new($sig);
36 $linux_sigfd = 1 if $^O eq 'linux';
38 ok($sigfd, 'Sigfd->new works');
42 my $fd = fileno($sigfd->{sock});
43 ok($fd >= 0, 'fileno(Sigfd->{sock}) works');
45 vec($rvec, $fd, 1) = 1;
46 is(select($rvec, undef, undef, undef), 1, 'select() works');
47 ok($sigfd->wait_once, 'wait_once reported success');
48 for my $s (qw(HUP INT)) {
49 is $hit->{$s}, 1, "sigfd fired $s";
52 skip 'Linux sigfd-only behavior', 1 if !$linux_sigfd;
54 'USR2 sent before signalfd created received';
56 PublicInbox::DS->Reset;
59 my $nbsig = PublicInbox::Sigfd->new($sig);
60 ok($nbsig, 'Sigfd->new SFD_NONBLOCK works');
61 is($nbsig->wait_once, undef, 'nonblocking ->wait_once');
62 ok($! == Errno::EAGAIN, 'got EAGAIN');
64 local @PublicInbox::DS::post_loop_do = (sub {}); # loop once
65 PublicInbox::DS::event_loop();
66 is $hit->{HUP}, 2, 'HUP sigfd fired in event loop' or
67 diag explain($hit); # sometimes fails on FreeBSD 11.x
70 PublicInbox::DS::event_loop();
71 PublicInbox::DS->Reset;
72 is $hit->{TERM}, 1, 'TERM sigfd fired in event loop';
73 is $hit->{HUP}, 3, 'HUP sigfd fired in event loop';
74 ok $hit->{WINCH}, 'WINCH sigfd fired in event loop';
76 my $restore = PublicInbox::DS::allow_sigs 'HUP';
78 select undef, undef, undef, 0;
79 is $hit->{HUP}, 4, 'HUP sigfd fired after allow_sigs';
83 vec($rvec = '', fileno($nbsig->{sock}), 1) = 1;
84 ok select($rvec, undef, undef, 1),
85 'select reports sigfd readiness';
86 is $hit->{HUP}, 4, 'HUP not fired when sigs blocked';
88 is $hit->{HUP}, 5, 'HUP fires only on ->event_step';
91 is $hit->{HUP}, 5, 'HUP not fired, yet';
92 $restore = PublicInbox::DS::allow_sigs 'HUP';
93 select(undef, undef, undef, 0);
94 is $hit->{HUP}, 6, 'HUP fires from allow_sigs';
96 skip('signalfd disabled?', 10);
98 PublicInbox::DS::sig_setmask($old);