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_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;
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 $!;
23 is("$old[0]\0$old[1]", "$new[0]\0$new[1]",
24 'device/inode matches on received FD');
28 my $true = which('true');
29 ok($true, "'true' command found with which()");
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');
39 { # ensure waitpid(-1, 0) and SIGCHLD works in spawned process
41 $| = 1; # unbuffer stdout
42 defined(my $pid = fork) or die "fork: $!";
43 if ($pid == 0) { exit }
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 };
50 select(undef, undef, undef, 0.01) while 1;
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);
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');
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');
87 my $fh = popen_rd([qw(echo hello)]);
88 ok(fileno($fh) >= 0, 'tied fileno works');
90 is($l, "hello\n", 'tied readline works');
92 ok(!$l, 'tied readline works for EOF');
96 my $fh = popen_rd([qw(printf foo\nbar)]);
97 ok(fileno($fh) >= 0, 'tied fileno works');
99 is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
103 my $fh = popen_rd([qw(echo hello)]);
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');
109 ok(close($fh), 'close succeeds');
110 is($?, 0, '$? set properly');
114 my $fh = popen_rd([qw(false)]);
115 ok(!close($fh), 'close fails on false');
116 isnt($?, 0, '$? set properly: '.$?);
119 { # ->CLOSE vs ->DESTROY waitpid caller distinction
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');
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');
134 { # children don't wait on siblings
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 });
140 my $pid = fork // BAIL_OUT $!;
141 local $SIG{__WARN__} = sub { _exit(1) };
143 local $SIG{__DIE__} = sub { _exit(2) };
148 is($?, 0, 'forked process exited');
150 local $SIG{__WARN__} = sub { push @w, @_ };
153 is($?, 0, 'cat exited');
154 is_deeply(\@w, [ "x=$$\n" ], 'callback fired from owner');
159 require BSD::Resource;
160 defined(BSD::Resource::RLIMIT_CPU())
161 } or skip 'BSD::Resource::RLIMIT_CPU missing', 3;
163 pipe($r, $w) or die "pipe: $!";
164 my $cmd = ['sh', '-c', 'while true; do :; done'];
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): $!";
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');