diff options
author | Eric Wong <e@80x24.org> | 2023-10-07 21:24:08 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2023-10-08 18:54:47 +0000 |
commit | 1661ff8e21f3cb1df1a3fc00d917f404f4eae734 (patch) | |
tree | bd2d06840b241b5f4546688fa07b1bf7790f4222 /lib/PublicInbox/ProcessPipe.pm | |
parent | 27423677034478018a202e5ddff02c5d2e0de061 (diff) | |
download | public-inbox-1661ff8e21f3cb1df1a3fc00d917f404f4eae734.tar.gz |
Since we deal with pipes (of either direction) and bidirectional stream sockets for this class, it's better to remove the `Pipe' from the name and replace it with `IO' to communicate that it works for any form of IO::Handle-like object tied to a process.
Diffstat (limited to 'lib/PublicInbox/ProcessPipe.pm')
-rw-r--r-- | lib/PublicInbox/ProcessPipe.pm | 85 |
1 files changed, 0 insertions, 85 deletions
diff --git a/lib/PublicInbox/ProcessPipe.pm b/lib/PublicInbox/ProcessPipe.pm deleted file mode 100644 index ba2c1ecb..00000000 --- a/lib/PublicInbox/ProcessPipe.pm +++ /dev/null @@ -1,85 +0,0 @@ -# Copyright (C) all contributors <meta@public-inbox.org> -# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt> - -# a tied handle for auto reaping of children tied to a read-only pipe, see perltie(1) -# DO NOT use this as-is for bidirectional pipes/sockets (e.g. in PublicInbox::Git), -# both ends of the pipe must be at the same level of the Perl object hierarchy -# to ensure orderly destruction. -package PublicInbox::ProcessPipe; -use v5.12; -use PublicInbox::DS qw(awaitpid); -use Symbol qw(gensym); - -sub maybe_new { - my ($cls, $pid, $fh, $opt) = @_; - return ($fh, $pid) if wantarray; - my $s = gensym; - tie *$s, $cls, $pid, $fh, @{$opt->{cb_arg} // []}; - $s; -} - -sub waitcb { # awaitpid callback - my ($pid, $err_ref, $cb, @args) = @_; - $$err_ref = $?; # sets >{pp_chld_err} for _close - $cb->($pid, @args) if $cb; -} - -sub TIEHANDLE { - my ($cls, $pid, $fh, @cb_arg) = @_; - my $self = bless { pid => $pid, fh => $fh, ppid => $$ }, $cls; - # we share $err (and not $self) with awaitpid to avoid a ref cycle - $self->{pp_chld_err} = \(my $err); - awaitpid($pid, \&waitcb, \$err, @cb_arg); - $self; -} - -# for IO::Uncompress::Gunzip -sub BINMODE { - my $self = shift; - binmode($self->{fh}, @_); -} - -sub READ { read($_[0]->{fh}, $_[1], $_[2], $_[3] || 0) } - -sub READLINE { readline($_[0]->{fh}) } - -sub WRITE { - use bytes qw(length); - syswrite($_[0]->{fh}, $_[1], $_[2] // length($_[1]), $_[3] // 0); -} - -sub PRINT { - my $self = shift; - print { $self->{fh} } @_; -} - -sub FILENO { fileno($_[0]->{fh}) } - -sub _close ($;$) { - my ($self, $wait) = @_; - my ($fh, $pid) = delete(@$self{qw(fh pid)}); - my $ret = (defined($fh) && $wait) ? close($fh) : ($fh = ''); - return $ret unless defined($pid) && $self->{ppid} == $$; - if ($wait) { # caller cares about the exit status: - # synchronous wait via defined(wantarray) on awaitpid: - defined(${$self->{pp_chld_err}}) or $wait = awaitpid($pid); - ($? = ${$self->{pp_chld_err}}) and $ret = ''; - } else { - awaitpid($pid); # depends on $in_loop or not - } - $ret; -} - -# if caller uses close(), assume they want to check $? immediately so -# we'll waitpid() synchronously. n.b. wantarray doesn't seem to -# propagate `undef' down to tied methods, otherwise I'd rely on that. -sub CLOSE { _close($_[0], 1) } - -# if relying on DESTROY, assume the caller doesn't care about $? and -# we can let the event loop call waitpid() whenever it gets SIGCHLD -sub DESTROY { - _close($_[0]); - undef; -} - -1; |