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');
22 my $pid = spawn(['true'], undef, { pgid => 0 });
23 ok($pid, 'spawned process with new pgid');
24 is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
25 is($?, 0, 'true exited successfully');
26 pipe(my ($r, $w)) or BAIL_OUT;
27 $pid = eval { spawn(['true'], undef, { pgid => 1, 2 => $w }) };
29 my $err = do { local $/; <$r> };
32 waitpid($pid, 0) if defined $pid;
33 isnt($?, 0, 'child error (pure-Perl)');
35 ok($@, 'exception raised');
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::DS::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::DS::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');
98 my $tfh = (tied *$fh)->{fh};
99 is($tfh->blocking(0), 1, '->blocking was true');
100 is($tfh->blocking, 0, '->blocking is false');
101 is($tfh->blocking(1), 0, '->blocking was true');
102 is($tfh->blocking, 1, '->blocking is true');
104 is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
108 my $fh = popen_rd([qw(echo hello)]);
110 is(sysread($fh, $buf, 6), 6, 'sysread got 6 bytes');
111 is($buf, "hello\n", 'tied gets works');
112 is(sysread($fh, $buf, 6), 0, 'sysread got EOF');
114 ok(close($fh), 'close succeeds');
115 is($?, 0, '$? set properly');
119 my $fh = popen_rd([qw(false)]);
120 ok(!close($fh), 'close fails on false');
121 isnt($?, 0, '$? set properly: '.$?);
125 local $ENV{GIT_CONFIG} = '/path/to/this/better/not/exist';
126 my $fh = popen_rd([qw(env)], { GIT_CONFIG => undef });
127 ok(!grep(/^GIT_CONFIG=/, <$fh>), 'GIT_CONFIG clobbered');
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');