1 # Copyright (C) 2015-2020 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::Sigfd::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::Sigfd::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');
81 is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
85 my $fh = popen_rd([qw(echo hello)]);
87 is(sysread($fh, $buf, 6), 6, 'sysread got 6 bytes');
88 is($buf, "hello\n", 'tied gets works');
89 is(sysread($fh, $buf, 6), 0, 'sysread got EOF');
91 ok(close($fh), 'close succeeds');
92 is($?, 0, '$? set properly');
96 my $fh = popen_rd([qw(false)]);
97 ok(!close($fh), 'close fails on false');
98 isnt($?, 0, '$? set properly: '.$?);
103 require BSD::Resource;
104 defined(BSD::Resource::RLIMIT_CPU())
105 } or skip 'BSD::Resource::RLIMIT_CPU missing', 3;
107 pipe($r, $w) or die "pipe: $!";
108 my $cmd = ['sh', '-c', 'while true; do :; done'];
110 my $opt = { RLIMIT_CPU => [ 1, 1 ], RLIMIT_CORE => [ 0, 0 ], 1 => $fd };
111 my $pid = spawn($cmd, undef, $opt);
112 close $w or die "close(w): $!";
114 vec($rset, fileno($r), 1) = 1;
115 ok(select($rset, undef, undef, 5), 'child died before timeout');
116 is(waitpid($pid, 0), $pid, 'XCPU child process reaped');
117 isnt($?, 0, 'non-zero exit status');