1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # base class for remote IPC calls, requires Storable
5 # TODO: this ought to be usable in SearchIdxShard
6 package PublicInbox::IPC;
9 use Socket qw(AF_UNIX SOCK_STREAM);
10 use Carp qw(confess croak);
11 use PublicInbox::Sigfd;
16 defined(my $len = <$sock>) or return;
17 chop($len) eq "\n" or croak "no LF byte in $len";
18 defined(my $r = read($sock, my $buf, $len)) or croak "read error: $!";
19 $r == $len or croak "short read: $r != $len";
24 my ($sock, $ref) = @_;
25 my $buf = freeze($ref);
26 print $sock length($buf), "\n", $buf or croak "print: $!";
29 sub ipc_return ($$$) {
30 my ($s2, $ret, $exc) = @_;
31 _send_rec($s2, $exc ? bless(\$exc, 'PublicInbox::IPC::Die') : $ret);
34 sub ipc_worker_loop ($$) {
36 $self->ipc_atfork_child if $self->can('ipc_atfork_child');
38 while (my $rec = _get_rec($s2)) {
39 my ($wantarray, $sub, @args) = @$rec;
40 if (!defined($wantarray)) { # no waiting if client doesn't care
41 eval { $self->$sub(@args) };
42 eval { warn "die: $@ (from nowait $sub)\n" } if $@;
43 } elsif ($wantarray) {
44 my @ret = eval { $self->$sub(@args) };
45 ipc_return($s2, \@ret, $@);
47 my $ret = eval { $self->$sub(@args) };
48 ipc_return($s2, \$ret, $@);
53 sub ipc_worker_spawn ($$$) {
54 my ($self, $ident, $oldset) = @_;
55 eval { require Storable; Storable->import(qw(freeze thaw)); };
57 state $w //= warn "Storable (part of Perl) missing: $@\n";
60 my $pid = $self->{-ipc_worker_pid};
61 confess "BUG: already spawned PID:$pid" if $pid;
62 confess "BUG: already have worker socket" if $self->{-ipc_sock};
64 socketpair($s1, $s2, AF_UNIX, SOCK_STREAM, 0) or die "socketpair: $!";
65 my $sigset = $oldset // PublicInbox::Sigfd::block_signals();
66 defined($pid = fork) or die "fork: $!";
70 $SIG{$_} = 'IGNORE' for (qw(TERM INT QUIT));
71 PublicInbox::Sigfd::sig_setmask($oldset);
72 eval { ipc_worker_loop($self, $s2) };
73 die "worker $ident died: $@\n" if $@;
74 $self->ipc_at_worker_exit if $self->can('ipc_at_worker_exit');
77 PublicInbox::Sigfd::sig_setmask($sigset) unless $oldset;
79 $self->{-ipc_sock} = $s1;
80 $self->{-ipc_worker_pid} = $pid;
83 sub ipc_reap_worker { # dwaitpid callback
84 my ($self, $pid) = @_;
85 warn "PID:$pid died with \$?=$?\n" if $?;
91 if (delete $self->{-ipc_sock}) {
92 $pid = delete $self->{-ipc_worker_pid} or die "no PID?";
94 $pid = delete $self->{-ipc_worker_pid} and
95 die "unexpected PID:$pid";
98 eval { PublicInbox::DS::dwaitpid($pid, \&ipc_reap_worker, $self) };
100 my $wp = waitpid($pid, 0);
101 $pid == $wp or die "waitpid($pid) returned $wp: \$?=$?";
102 ipc_reap_worker($self, $pid);
106 # use this if we have multiple readers reading curl or "pigz -dc"
107 # and writing to the same store
110 require PublicInbox::Lock;
111 $self->{-ipc_lock} //= bless { lock_path => $f }, 'PublicInbox::Lock'
115 my ($self, $sub, @args) = @_;
116 if (my $s1 = $self->{-ipc_sock}) {
117 my $ipc_lock = $self->{-ipc_lock};
118 my $lock = $ipc_lock ? $ipc_lock->lock_for_scope : undef;
119 _send_rec($s1, [ wantarray, $sub, @args ]);
120 return unless defined(wantarray);
121 my $ret = _get_rec($s1) // die "no response on $sub";
122 die $$ret if ref($ret) eq 'PublicInbox::IPC::Die';
123 wantarray ? @$ret : $$ret;