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;
10 my $true = which('true');
11 ok($true, "'true' command found with which()");
15 my $pid = spawn(['true']);
16 ok($pid, 'spawned process');
17 is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
18 is($?, 0, 'true exited successfully');
21 { # ensure waitpid(-1, 0) and SIGCHLD works in spawned process
23 $| = 1; # unbuffer stdout
24 defined(my $pid = fork) or die "fork: $!";
25 if ($pid == 0) { exit }
27 my $waited = waitpid(-1, 0);
28 $waited == $pid or die "mismatched child $pid != $waited";
29 $? == 0 or die "child err: $>";
30 $SIG{CHLD} = sub { print "HI\n"; exit };
32 select(undef, undef, undef, 0.01) while 1;
35 my $oldset = PublicInbox::DS::block_signals();
36 my $rd = popen_rd([$^X, '-e', $script]);
37 diag 'waiting for child to reap grandchild...';
38 chomp(my $line = readline($rd));
39 my ($rdy, $pid) = split(' ', $line);
40 is($rdy, 'RDY', 'got ready signal, waitpid(-1) works in child');
41 ok(kill('CHLD', $pid), 'sent SIGCHLD to child');
42 is(readline($rd), "HI\n", '$SIG{CHLD} works in child');
43 ok(close $rd, 'popen_rd close works');
44 PublicInbox::DS::sig_setmask($oldset);
49 pipe $r, $w or die "pipe failed: $!";
50 my $pid = spawn(['echo', 'hello world'], undef, { 1 => fileno($w) });
51 close $w or die "close pipe[1] failed: $!";
52 is(<$r>, "hello world\n", 'read stdout of spawned from pipe');
53 is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
54 is($?, 0, 'true exited successfully');
59 pipe $r, $w or die "pipe failed: $!";
60 my $pid = spawn(['sh', '-c', 'echo $HELLO'],
61 { 'HELLO' => 'world' }, { 1 => $w });
62 close $w or die "close pipe[1] failed: $!";
63 is(<$r>, "world\n", 'read stdout of spawned from pipe');
64 is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
65 is($?, 0, 'sh exited successfully');
69 my $fh = popen_rd([qw(echo hello)]);
70 ok(fileno($fh) >= 0, 'tied fileno works');
72 is($l, "hello\n", 'tied readline works');
74 ok(!$l, 'tied readline works for EOF');
78 my $fh = popen_rd([qw(printf foo\nbar)]);
79 ok(fileno($fh) >= 0, 'tied fileno works');
80 my $tfh = (tied *$fh)->{fh};
81 is($tfh->blocking(0), 1, '->blocking was true');
82 is($tfh->blocking, 0, '->blocking is false');
83 is($tfh->blocking(1), 0, '->blocking was true');
84 is($tfh->blocking, 1, '->blocking is true');
86 is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
90 my $fh = popen_rd([qw(echo hello)]);
92 is(sysread($fh, $buf, 6), 6, 'sysread got 6 bytes');
93 is($buf, "hello\n", 'tied gets works');
94 is(sysread($fh, $buf, 6), 0, 'sysread got EOF');
96 ok(close($fh), 'close succeeds');
97 is($?, 0, '$? set properly');
101 my $fh = popen_rd([qw(false)]);
102 ok(!close($fh), 'close fails on false');
103 isnt($?, 0, '$? set properly: '.$?);
106 { # ->CLOSE vs ->DESTROY waitpid caller distinction
108 my $fh = popen_rd(['true'], undef, { cb => sub { @c = caller } });
109 ok(close($fh), '->CLOSE fired and successful');
110 ok(scalar(@c), 'callback fired by ->CLOSE');
111 ok(grep(!m[/PublicInbox/DS\.pm\z], @c), 'callback not invoked by DS');
114 $fh = popen_rd(['true'], undef, { cb => sub { @c = caller } });
115 undef $fh; # ->DESTROY
116 ok(scalar(@c), 'callback fired by ->DESTROY');
117 ok(grep(!m[/PublicInbox/ProcessPipe\.pm\z], @c),
118 'callback not invoked by ProcessPipe');
121 { # children don't wait on siblings
123 pipe(my ($r, $w)) or BAIL_OUT $!;
124 my $cb = sub { warn "x=$$\n" };
125 my $fh = popen_rd(['cat'], undef, { 0 => $r, cb => $cb });
127 my $pid = fork // BAIL_OUT $!;
128 local $SIG{__WARN__} = sub { _exit(1) };
130 local $SIG{__DIE__} = sub { _exit(2) };
135 is($?, 0, 'forked process exited');
137 local $SIG{__WARN__} = sub { push @w, @_ };
140 is($?, 0, 'cat exited');
141 is_deeply(\@w, [ "x=$$\n" ], 'callback fired from owner');
146 require BSD::Resource;
147 defined(BSD::Resource::RLIMIT_CPU())
148 } or skip 'BSD::Resource::RLIMIT_CPU missing', 3;
150 pipe($r, $w) or die "pipe: $!";
151 my $cmd = ['sh', '-c', 'while true; do :; done'];
153 my $opt = { RLIMIT_CPU => [ 1, 1 ], RLIMIT_CORE => [ 0, 0 ], 1 => $fd };
154 my $pid = spawn($cmd, undef, $opt);
155 close $w or die "close(w): $!";
157 vec($rset, fileno($r), 1) = 1;
158 ok(select($rset, undef, undef, 5), 'child died before timeout');
159 is(waitpid($pid, 0), $pid, 'XCPU child process reaped');
160 isnt($?, 0, 'non-zero exit status');