2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE SIG_UNBLOCK SIG_SETMASK sigprocmask);
8 use PublicInbox::OnDestroy;
10 # undo systemd (and similar) blocking SIGPIPE, since lei expects to be run
11 # from an interactive terminal:
12 # https://public-inbox.org/meta/20220227080422.gyqowrxomzu6gyin@sourcephile.fr/
13 my $set = POSIX::SigSet->new;
14 my $old = POSIX::SigSet->new;
15 $set->emptyset or xbail "sigemptyset $!";
16 $old->emptyset or xbail "sigemptyset $!";
17 $set->addset(SIGPIPE);
18 sigprocmask(SIG_UNBLOCK, $set, $old) or xbail "SIG_UNBLOCK: $!";
19 my $cleanup = PublicInbox::OnDestroy->new($$, sub {
20 sigprocmask(SIG_SETMASK, $old);
24 my $f = "$ENV{HOME}/big.eml";
26 for my $out ([], [qw(-f mboxcl2)], [qw(-f text)]) {
27 pipe(my ($r, $w)) or BAIL_OUT $!;
29 if ($^O eq 'linux' && fcntl($w, 1031, 4096)) {
33 open my $fh, '>', $f or xbail "open $f: $!";
34 print $fh <<'EOM' or xbail;
36 Message-ID: <big@example.com>
39 print $fh (' '.('x' x 72)."\n") x (($size / 73) + 1);
41 close $fh or xbail "close: $!";
44 lei_ok(qw(import), $f) if $imported++ == 0;
45 open my $errfh, '+>>', "$ENV{HOME}/stderr.log" or xbail $!;
46 my $opt = { run_mode => 0, 2 => $errfh, 1 => $w };
47 my $cmd = [qw(lei q -q -t), @$out, 'z:1..'];
48 my $tp = start_script($cmd, undef, $opt);
50 vec(my $rvec = '', fileno($r), 1) = 1;
51 if (!select($rvec, undef, undef, 30)) {
52 seek($errfh, 0, 0) or xbail $!;
53 my $s = do { local $/; <$errfh> };
54 xbail "lei q had no output after 30s, stderr=$s";
56 is(sysread($r, my $buf, 1), 1, 'read one byte');
57 close $r; # trigger SIGPIPE
59 ok(WIFSIGNALED($?), "signaled @$out");
60 is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
61 seek($errfh, 0, 0) or xbail $!;
62 my $s = do { local $/; <$errfh> };
63 is($s, '', "quiet after sigpipe @$out");