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);
9 my $true = which('true');
10 ok($true, "'true' command found with which()");
14 my $pid = spawn(['true']);
15 ok($pid, 'spawned process');
16 is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
17 is($?, 0, 'true exited successfully');
22 pipe $r, $w or die "pipe failed: $!";
23 my $pid = spawn(['echo', 'hello world'], undef, { 1 => fileno($w) });
24 close $w or die "close pipe[1] failed: $!";
25 is(<$r>, "hello world\n", 'read stdout of spawned from pipe');
26 is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
27 is($?, 0, 'true exited successfully');
32 pipe $r, $w or die "pipe failed: $!";
33 my $pid = spawn(['sh', '-c', 'echo $HELLO'],
34 { 'HELLO' => 'world' }, { 1 => $w });
35 close $w or die "close pipe[1] failed: $!";
36 is(<$r>, "world\n", 'read stdout of spawned from pipe');
37 is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
38 is($?, 0, 'sh exited successfully');
42 my $fh = popen_rd([qw(echo hello)]);
43 ok(fileno($fh) >= 0, 'tied fileno works');
45 is($l, "hello\n", 'tied readline works');
47 ok(!$l, 'tied readline works for EOF');
51 my $fh = popen_rd([qw(printf foo\nbar)]);
52 ok(fileno($fh) >= 0, 'tied fileno works');
54 is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
58 my $fh = popen_rd([qw(echo hello)]);
60 is(sysread($fh, $buf, 6), 6, 'sysread got 6 bytes');
61 is($buf, "hello\n", 'tied gets works');
62 is(sysread($fh, $buf, 6), 0, 'sysread got EOF');
64 ok(close($fh), 'close succeeds');
65 is($?, 0, '$? set properly');
69 my $fh = popen_rd([qw(false)]);
70 ok(!close($fh), 'close fails on false');
71 isnt($?, 0, '$? set properly: '.$?);
76 require BSD::Resource;
77 defined(BSD::Resource::RLIMIT_CPU())
78 } or skip 'BSD::Resource::RLIMIT_CPU missing', 3;
80 pipe($r, $w) or die "pipe: $!";
81 my $cmd = ['sh', '-c', 'while true; do :; done'];
83 my $opt = { RLIMIT_CPU => [ 1, 1 ], RLIMIT_CORE => [ 0, 0 ], 1 => $fd };
84 my $pid = spawn($cmd, undef, $opt);
85 close $w or die "close(w): $!";
87 vec($rset, fileno($r), 1) = 1;
88 ok(select($rset, undef, undef, 5), 'child died before timeout');
89 is(waitpid($pid, 0), $pid, 'XCPU child process reaped');
90 isnt($?, 0, 'non-zero exit status');