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 read-only pipe, see perltie(1)
5 # DO NOT use this as-is for bidirectional pipes/sockets (e.g. in PublicInbox::Git),
6 # both ends of the pipe must be at the same level of the Perl object hierarchy
7 # to ensure orderly destruction.
8 package PublicInbox::ProcessPipe;
10 use PublicInbox::DS qw(awaitpid);
12 sub waitcb { # awaitpid callback
13 my ($pid, $err_ref, $cb, @args) = @_;
14 $$err_ref = $?; # sets >{pp_chld_err} for _close
15 $cb->($pid, @args) if $cb;
19 my ($cls, $pid, $fh, @cb_arg) = @_;
20 my $self = bless { pid => $pid, fh => $fh, ppid => $$ }, $cls;
21 # we share $err (and not $self) with awaitpid to avoid a ref cycle
22 $self->{pp_chld_err} = \(my $err);
23 awaitpid($pid, \&waitcb, \$err, @cb_arg);
27 sub BINMODE { binmode(shift->{fh}) } # for IO::Uncompress::Gunzip
29 sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
31 sub READLINE { readline($_[0]->{fh}) }
35 syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0);
40 print { $self->{fh} } @_;
43 sub FILENO { fileno($_[0]->{fh}) }
46 my ($self, $wait) = @_;
47 my ($fh, $pid) = delete(@$self{qw(fh pid)});
48 my $ret = defined($fh) ? close($fh) : '';
49 return $ret unless defined($pid) && $self->{ppid} == $$;
50 if ($wait) { # caller cares about the exit status:
51 # synchronous wait via defined(wantarray) on awaitpid:
52 defined(${$self->{pp_chld_err}}) or $wait = awaitpid($pid);
53 ($? = ${$self->{pp_chld_err}}) and $ret = '';
55 awaitpid($pid); # depends on $in_loop or not
60 # if caller uses close(), assume they want to check $? immediately so
61 # we'll waitpid() synchronously. n.b. wantarray doesn't seem to
62 # propagate `undef' down to tied methods, otherwise I'd rely on that.
63 sub CLOSE { _close($_[0], 1) }
65 # if relying on DESTROY, assume the caller doesn't care about $? and
66 # we can let the event loop call waitpid() whenever it gets SIGCHLD