1 # Copyright (C) 2016-2021 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(dwaitpid);
11 my ($class, $pid, $fh, $cb, $arg) = @_;
12 bless { pid => $pid, fh => $fh, cb => $cb, arg => $arg }, $class;
15 sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
17 sub READLINE { readline($_[0]->{fh}) }
21 syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0);
26 print { $self->{fh} } @_;
29 sub adjust_ret { # dwaitpid callback
30 my ($retref, $pid) = @_;
35 my $fh = delete($_[0]->{fh});
36 my $ret = defined $fh ? close($fh) : '';
37 my ($pid, $cb, $arg) = delete @{$_[0]}{qw(pid cb arg)};
43 dwaitpid $pid, $cb, $arg;
48 sub FILENO { fileno($_[0]->{fh}) }
55 sub pid { $_[0]->{pid} }