]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/IPC.pm
ipc: generic IPC dispatch based on Storable
[public-inbox.git] / lib / PublicInbox / IPC.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # base class for remote IPC calls, requires Storable
5 # TODO: this ought to be usable in SearchIdxShard
6 package PublicInbox::IPC;
7 use strict;
8 use v5.10.1;
9 use Socket qw(AF_UNIX SOCK_STREAM);
10 use Carp qw(confess croak);
11 use PublicInbox::Sigfd;
12
13 sub _get_rec ($) {
14         my ($sock) = @_;
15         local $/ = "\n";
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";
20         thaw($buf);
21 }
22
23 sub _send_rec ($$) {
24         my ($sock, $ref) = @_;
25         my $buf = freeze($ref);
26         print $sock length($buf), "\n", $buf or croak "print: $!";
27 }
28
29 sub ipc_return ($$$) {
30         my ($s2, $ret, $exc) = @_;
31         _send_rec($s2, $exc ? bless(\$exc, 'PublicInbox::IPC::Die') : $ret);
32 }
33
34 sub ipc_worker_loop ($$) {
35         my ($self, $s2) = @_;
36         $self->ipc_atfork_child if $self->can('ipc_atfork_child');
37         $s2->autoflush(1);
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, $@);
46                 } else {
47                         my $ret = eval { $self->$sub(@args) };
48                         ipc_return($s2, \$ret, $@);
49                 }
50         }
51 }
52
53 sub ipc_worker_spawn ($$$) {
54         my ($self, $ident, $oldset) = @_;
55         eval { require Storable; Storable->import(qw(freeze thaw)); };
56         if ($@) {
57                 state $w //= warn "Storable (part of Perl) missing: $@\n";
58                 return;
59         }
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};
63         my ($s1, $s2);
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: $!";
67         if ($pid == 0) {
68                 undef $s1;
69                 local $0 = $ident;
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');
75                 exit;
76         }
77         PublicInbox::Sigfd::sig_setmask($sigset) unless $oldset;
78         $s1->autoflush(1);
79         $self->{-ipc_sock} = $s1;
80         $self->{-ipc_worker_pid} = $pid;
81 }
82
83 sub ipc_reap_worker { # dwaitpid callback
84         my ($self, $pid) = @_;
85         warn "PID:$pid died with \$?=$?\n" if $?;
86 }
87
88 sub ipc_worker_stop {
89         my ($self) = @_;
90         my $pid;
91         if (delete $self->{-ipc_sock}) {
92                 $pid = delete $self->{-ipc_worker_pid} or die "no PID?";
93         } else {
94                 $pid = delete $self->{-ipc_worker_pid} and
95                         die "unexpected PID:$pid";
96         }
97         return unless $pid;
98         eval { PublicInbox::DS::dwaitpid($pid, \&ipc_reap_worker, $self) };
99         if ($@) {
100                 my $wp = waitpid($pid, 0);
101                 $pid == $wp or die "waitpid($pid) returned $wp: \$?=$?";
102                 ipc_reap_worker($self, $pid);
103         }
104 }
105
106 # use this if we have multiple readers reading curl or "pigz -dc"
107 # and writing to the same store
108 sub ipc_lock_init {
109         my ($self, $f) = @_;
110         require PublicInbox::Lock;
111         $self->{-ipc_lock} //= bless { lock_path => $f }, 'PublicInbox::Lock'
112 }
113
114 sub ipc_do {
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;
124         } else {
125                 $self->$sub(@args);
126         }
127 }
128
129 1;