]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ProcessPipe.pm
No ext_urls
[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 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;
9 use v5.12;
10 use PublicInbox::DS qw(awaitpid);
11
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;
16 }
17
18 sub TIEHANDLE {
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);
24         $self;
25 }
26
27 sub BINMODE { binmode(shift->{fh}) } # for IO::Uncompress::Gunzip
28
29 sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) }
30
31 sub READLINE { readline($_[0]->{fh}) }
32
33 sub WRITE {
34         use bytes qw(length);
35         syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0);
36 }
37
38 sub PRINT {
39         my $self = shift;
40         print { $self->{fh} } @_;
41 }
42
43 sub FILENO { fileno($_[0]->{fh}) }
44
45 sub _close ($;$) {
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 = '';
54         } else {
55                 awaitpid($pid); # depends on $in_loop or not
56         }
57         $ret;
58 }
59
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) }
64
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
67 sub DESTROY {
68         _close($_[0]);
69         undef;
70 }
71
72 1;