public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 97e9c268f8d1e91aa0f8c1225112dc42cd623768 1872 bytes (raw)
$ git show v1.7.0:lib/PublicInbox/ProcessPipe.pm	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
# Copyright (C) 2016-2021 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 pipe, see perltie(1)
package PublicInbox::ProcessPipe;
use strict;
use v5.10.1;
use Carp qw(carp);

sub TIEHANDLE {
	my ($class, $pid, $fh, $cb, $arg) = @_;
	bless { pid => $pid, fh => $fh, ppid => $$, cb => $cb, arg => $arg },
		$class;
}

sub BINMODE { binmode(shift->{fh}) } # for IO::Uncompress::Gunzip

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 = delete $self->{fh};
	my $ret = defined($fh) ? close($fh) : '';
	my ($pid, $cb, $arg) = delete @$self{qw(pid cb arg)};
	return $ret unless defined($pid) && $self->{ppid} == $$;
	if ($wait) { # caller cares about the exit status:
		my $wp = waitpid($pid, 0);
		if ($wp == $pid) {
			$ret = '' if $?;
			if ($cb) {
				eval { $cb->($arg, $pid) };
				carp "E: cb(arg, $pid): $@" if $@;
			}
		} else {
			carp "waitpid($pid, 0) = $wp, \$!=$!, \$?=$?";
		}
	} else { # caller just undef-ed it, let event loop deal with it
		require PublicInbox::DS;
		PublicInbox::DS::dwaitpid($pid, $cb, $arg);
	}
	$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;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git