]> Sergey Matveev's repositories - public-inbox.git/blob - t/spawn.t
spawn: support send_fd+recv_fd w/o IO::FDPass
[public-inbox.git] / t / spawn.t
1 # Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Spawn qw(which spawn popen_rd);
7 use PublicInbox::Sigfd;
8 use Socket qw(AF_UNIX SOCK_STREAM);
9
10 SKIP: {
11         my $recv_fd = PublicInbox::Spawn->can('recv_fd');
12         my $send_fd = PublicInbox::Spawn->can('send_fd');
13         skip 'Inline::C not enabled', 3 unless $send_fd && $recv_fd;
14         my ($s1, $s2);
15         socketpair($s1, $s2, AF_UNIX, SOCK_STREAM, 0) or BAIL_OUT $!;
16         pipe(my ($r, $w)) or BAIL_OUT $!;
17         ok($send_fd->(fileno($s1), fileno($r)), 'pipe sent');
18         my $rfd = $recv_fd->(fileno($s2));
19         like($rfd, qr/\A\d+\z/, 'got FD');
20         open(my $rfh, '<&=', $rfd) or BAIL_OUT $!;
21         my @old = stat($r);
22         my @new = stat($rfh);
23         is("$old[0]\0$old[1]", "$new[0]\0$new[1]",
24                 'device/inode matches on received FD');
25 }
26
27 {
28         my $true = which('true');
29         ok($true, "'true' command found with which()");
30 }
31
32 {
33         my $pid = spawn(['true']);
34         ok($pid, 'spawned process');
35         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
36         is($?, 0, 'true exited successfully');
37 }
38
39 { # ensure waitpid(-1, 0) and SIGCHLD works in spawned process
40         my $script = <<'EOF';
41 $| = 1; # unbuffer stdout
42 defined(my $pid = fork) or die "fork: $!";
43 if ($pid == 0) { exit }
44 elsif ($pid > 0) {
45         my $waited = waitpid(-1, 0);
46         $waited == $pid or die "mismatched child $pid != $waited";
47         $? == 0 or die "child err: $>";
48         $SIG{CHLD} = sub { print "HI\n"; exit };
49         print "RDY $$\n";
50         select(undef, undef, undef, 0.01) while 1;
51 }
52 EOF
53         my $oldset = PublicInbox::Sigfd::block_signals();
54         my $rd = popen_rd([$^X, '-e', $script]);
55         diag 'waiting for child to reap grandchild...';
56         chomp(my $line = readline($rd));
57         my ($rdy, $pid) = split(' ', $line);
58         is($rdy, 'RDY', 'got ready signal, waitpid(-1) works in child');
59         ok(kill('CHLD', $pid), 'sent SIGCHLD to child');
60         is(readline($rd), "HI\n", '$SIG{CHLD} works in child');
61         ok(close $rd, 'popen_rd close works');
62         PublicInbox::Sigfd::sig_setmask($oldset);
63 }
64
65 {
66         my ($r, $w);
67         pipe $r, $w or die "pipe failed: $!";
68         my $pid = spawn(['echo', 'hello world'], undef, { 1 => fileno($w) });
69         close $w or die "close pipe[1] failed: $!";
70         is(<$r>, "hello world\n", 'read stdout of spawned from pipe');
71         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
72         is($?, 0, 'true exited successfully');
73 }
74
75 {
76         my ($r, $w);
77         pipe $r, $w or die "pipe failed: $!";
78         my $pid = spawn(['sh', '-c', 'echo $HELLO'],
79                 { 'HELLO' => 'world' }, { 1 => $w });
80         close $w or die "close pipe[1] failed: $!";
81         is(<$r>, "world\n", 'read stdout of spawned from pipe');
82         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
83         is($?, 0, 'sh exited successfully');
84 }
85
86 {
87         my $fh = popen_rd([qw(echo hello)]);
88         ok(fileno($fh) >= 0, 'tied fileno works');
89         my $l = <$fh>;
90         is($l, "hello\n", 'tied readline works');
91         $l = <$fh>;
92         ok(!$l, 'tied readline works for EOF');
93 }
94
95 {
96         my $fh = popen_rd([qw(printf foo\nbar)]);
97         ok(fileno($fh) >= 0, 'tied fileno works');
98         my @line = <$fh>;
99         is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
100 }
101
102 {
103         my $fh = popen_rd([qw(echo hello)]);
104         my $buf;
105         is(sysread($fh, $buf, 6), 6, 'sysread got 6 bytes');
106         is($buf, "hello\n", 'tied gets works');
107         is(sysread($fh, $buf, 6), 0, 'sysread got EOF');
108         $? = 1;
109         ok(close($fh), 'close succeeds');
110         is($?, 0, '$? set properly');
111 }
112
113 {
114         my $fh = popen_rd([qw(false)]);
115         ok(!close($fh), 'close fails on false');
116         isnt($?, 0, '$? set properly: '.$?);
117 }
118
119 { # ->CLOSE vs ->DESTROY waitpid caller distinction
120         my @c;
121         my $fh = popen_rd(['true'], undef, { cb => sub { @c = caller } });
122         ok(close($fh), '->CLOSE fired and successful');
123         ok(scalar(@c), 'callback fired by ->CLOSE');
124         ok(grep(!m[/PublicInbox/DS\.pm\z], @c), 'callback not invoked by DS');
125
126         @c = ();
127         $fh = popen_rd(['true'], undef, { cb => sub { @c = caller } });
128         undef $fh; # ->DESTROY
129         ok(scalar(@c), 'callback fired by ->DESTROY');
130         ok(grep(!m[/PublicInbox/ProcessPipe\.pm\z], @c),
131                 'callback not invoked by ProcessPipe');
132 }
133
134 { # children don't wait on siblings
135         use POSIX qw(_exit);
136         pipe(my ($r, $w)) or BAIL_OUT $!;
137         my $cb = sub { warn "x=$$\n" };
138         my $fh = popen_rd(['cat'], undef, { 0 => $r, cb => $cb });
139         my $pp = tied *$fh;
140         my $pid = fork // BAIL_OUT $!;
141         local $SIG{__WARN__} = sub { _exit(1) };
142         if ($pid == 0) {
143                 local $SIG{__DIE__} = sub { _exit(2) };
144                 undef $fh;
145                 _exit(0);
146         }
147         waitpid($pid, 0);
148         is($?, 0, 'forked process exited');
149         my @w;
150         local $SIG{__WARN__} = sub { push @w, @_ };
151         close $w;
152         close $fh;
153         is($?, 0, 'cat exited');
154         is_deeply(\@w, [ "x=$$\n" ], 'callback fired from owner');
155 }
156
157 SKIP: {
158         eval {
159                 require BSD::Resource;
160                 defined(BSD::Resource::RLIMIT_CPU())
161         } or skip 'BSD::Resource::RLIMIT_CPU missing', 3;
162         my ($r, $w);
163         pipe($r, $w) or die "pipe: $!";
164         my $cmd = ['sh', '-c', 'while true; do :; done'];
165         my $fd = fileno($w);
166         my $opt = { RLIMIT_CPU => [ 1, 1 ], RLIMIT_CORE => [ 0, 0 ], 1 => $fd };
167         my $pid = spawn($cmd, undef, $opt);
168         close $w or die "close(w): $!";
169         my $rset = '';
170         vec($rset, fileno($r), 1) = 1;
171         ok(select($rset, undef, undef, 5), 'child died before timeout');
172         is(waitpid($pid, 0), $pid, 'XCPU child process reaped');
173         isnt($?, 0, 'non-zero exit status');
174 }
175
176 done_testing();
177
178 1;