1 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
5 use POSIX qw(:signal_h);
7 use PublicInbox::Syscall qw(SFD_NONBLOCK);
8 require_ok 'PublicInbox::Sigfd';
11 if ($^O ne 'linux' && !eval { require IO::KQueue }) {
12 skip 'signalfd requires Linux or IO::KQueue to emulate', 10;
14 my $new = POSIX::SigSet->new;
15 $new->fillset or die "sigfillset: $!";
16 my $old = POSIX::SigSet->new;
17 sigprocmask(SIG_SETMASK, $new, $old) or die "sigprocmask $!";
20 local $SIG{HUP} = sub { $hit->{HUP}->{normal}++ };
21 local $SIG{TERM} = sub { $hit->{TERM}->{normal}++ };
22 local $SIG{INT} = sub { $hit->{INT}->{normal}++ };
23 for my $s (qw(HUP TERM INT)) {
24 $sig->{$s} = sub { $hit->{$s}->{sigfd}++ };
26 my $sigfd = PublicInbox::Sigfd->new($sig, 0);
28 require PublicInbox::DS;
29 ok($sigfd, 'Sigfd->new works');
30 kill('HUP', $$) or die "kill $!";
31 kill('INT', $$) or die "kill $!";
32 my $fd = fileno($sigfd->{sock});
33 ok($fd >= 0, 'fileno(Sigfd->{sock}) works');
35 vec($rvec, $fd, 1) = 1;
36 is(select($rvec, undef, undef, undef), 1, 'select() works');
37 ok($sigfd->wait_once, 'wait_once reported success');
38 for my $s (qw(HUP INT)) {
39 is($hit->{$s}->{sigfd}, 1, "sigfd fired $s");
40 is($hit->{$s}->{normal}, undef,
41 'normal $SIG{$s} not fired');
45 my $nbsig = PublicInbox::Sigfd->new($sig, SFD_NONBLOCK);
46 ok($nbsig, 'Sigfd->new SFD_NONBLOCK works');
47 is($nbsig->wait_once, undef, 'nonblocking ->wait_once');
48 ok($! == Errno::EAGAIN, 'got EAGAIN');
49 kill('HUP', $$) or die "kill $!";
50 PublicInbox::DS->SetPostLoopCallback(sub {}); # loop once
51 PublicInbox::DS->EventLoop;
52 is($hit->{HUP}->{sigfd}, 2, 'HUP sigfd fired in event loop');
53 kill('TERM', $$) or die "kill $!";
54 kill('HUP', $$) or die "kill $!";
55 PublicInbox::DS->EventLoop;
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');
60 skip('signalfd disabled?', 10);
62 sigprocmask(SIG_SETMASK, $old) or die "sigprocmask $!";