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