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>
6 use PublicInbox::Spawn qw(which spawn popen_rd);
7 use PublicInbox::Sigfd;
8 use Socket qw(AF_UNIX SOCK_STREAM);
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;
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]),
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');
27 open my $new, $_, shift(@fds) or BAIL_OUT "open $! $i => $_";
28 ($new, shift(@orig), $i++);
30 while (my ($new, $old, $fd) = splice(@cmp, 0, 3)) {
33 is("$old[0]\0$old[1]", "$new[0]\0$new[1]",
34 "device/inode matches on received FD:$fd");
39 my $true = which('true');
40 ok($true, "'true' command found with which()");
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');
50 { # ensure waitpid(-1, 0) and SIGCHLD works in spawned process
52 $| = 1; # unbuffer stdout
53 defined(my $pid = fork) or die "fork: $!";
54 if ($pid == 0) { exit }
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 };
61 select(undef, undef, undef, 0.01) while 1;
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);
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');
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');
98 my $fh = popen_rd([qw(echo hello)]);
99 ok(fileno($fh) >= 0, 'tied fileno works');
101 is($l, "hello\n", 'tied readline works');
103 ok(!$l, 'tied readline works for EOF');
107 my $fh = popen_rd([qw(printf foo\nbar)]);
108 ok(fileno($fh) >= 0, 'tied fileno works');
110 is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
114 my $fh = popen_rd([qw(echo hello)]);
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');
120 ok(close($fh), 'close succeeds');
121 is($?, 0, '$? set properly');
125 my $fh = popen_rd([qw(false)]);
126 ok(!close($fh), 'close fails on false');
127 isnt($?, 0, '$? set properly: '.$?);
130 { # ->CLOSE vs ->DESTROY waitpid caller distinction
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');
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');
145 { # children don't wait on siblings
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 });
151 my $pid = fork // BAIL_OUT $!;
152 local $SIG{__WARN__} = sub { _exit(1) };
154 local $SIG{__DIE__} = sub { _exit(2) };
159 is($?, 0, 'forked process exited');
161 local $SIG{__WARN__} = sub { push @w, @_ };
164 is($?, 0, 'cat exited');
165 is_deeply(\@w, [ "x=$$\n" ], 'callback fired from owner');
170 require BSD::Resource;
171 defined(BSD::Resource::RLIMIT_CPU())
172 } or skip 'BSD::Resource::RLIMIT_CPU missing', 3;
174 pipe($r, $w) or die "pipe: $!";
175 my $cmd = ['sh', '-c', 'while true; do :; done'];
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): $!";
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');