]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ProcessPipe.pm
068631c6944cc1efe2f024c222ada984aac60ca4
[public-inbox.git] / lib / PublicInbox / ProcessPipe.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # a tied handle for auto reaping of children tied to a pipe, see perltie(1)
5 package PublicInbox::ProcessPipe;
6 use v5.12;
7 use Carp qw(carp);
8 use PublicInbox::DS qw(awaitpid);
9
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;
14 }
15
16 sub TIEHANDLE {
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);
22         $self;
23 }
24
25 sub BINMODE { binmode(shift->{fh}) } # for IO::Uncompress::Gunzip
26
27 sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
28
29 sub READLINE { readline($_[0]->{fh}) }
30
31 sub WRITE {
32         use bytes qw(length);
33         syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0);
34 }
35
36 sub PRINT {
37         my $self = shift;
38         print { $self->{fh} } @_;
39 }
40
41 sub FILENO { fileno($_[0]->{fh}) }
42
43 sub _close ($;$) {
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 = '';
52         } else {
53                 awaitpid($pid); # depends on $in_loop or not
54         }
55         $ret;
56 }
57
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) }
62
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
65 sub DESTROY {
66         _close($_[0]);
67         undef;
68 }
69
70 1;