1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # a tied handle for auto reaping of children tied to a pipe, see perltie(1)
5 package PublicInbox::ProcessPipe;
8 use PublicInbox::DS qw(awaitpid);
10 sub waitcb { # awaitpid callback
11 my ($pid, $err_ref, $cb, @args) = @_;
12 $$err_ref = $?; # sets >{pp_chld_err} for _close
13 $cb->($pid, @args) if $cb;
17 my ($cls, $pid, $fh, @cb_arg) = @_;
18 my $self = bless { pid => $pid, fh => $fh, ppid => $$ }, $cls;
19 # we share $err (and not $self) with awaitpid to avoid a ref cycle
20 $self->{pp_chld_err} = \(my $err);
21 awaitpid($pid, \&waitcb, \$err, @cb_arg);
25 sub BINMODE { binmode(shift->{fh}) } # for IO::Uncompress::Gunzip
27 sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
29 sub READLINE { readline($_[0]->{fh}) }
33 syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0);
38 print { $self->{fh} } @_;
41 sub FILENO { fileno($_[0]->{fh}) }
44 my ($self, $wait) = @_;
45 my ($fh, $pid) = delete(@$self{qw(fh pid)});
46 my $ret = defined($fh) ? close($fh) : '';
47 return $ret unless defined($pid) && $self->{ppid} == $$;
48 if ($wait) { # caller cares about the exit status:
49 # synchronous wait via defined(wantarray) on awaitpid:
50 defined(${$self->{pp_chld_err}}) or $wait = awaitpid($pid);
51 ($? = ${$self->{pp_chld_err}}) and $ret = '';
53 awaitpid($pid); # depends on $in_loop or not
58 # if caller uses close(), assume they want to check $? immediately so
59 # we'll waitpid() synchronously. n.b. wantarray doesn't seem to
60 # propagate `undef' down to tied methods, otherwise I'd rely on that.
61 sub CLOSE { _close($_[0], 1) }
63 # if relying on DESTROY, assume the caller doesn't care about $? and
64 # we can let the event loop call waitpid() whenever it gets SIGCHLD