]> Sergey Matveev's repositories - public-inbox.git/blob - script/lei
lei: test SIGPIPE, stop xsearch workers on client abort
[public-inbox.git] / script / lei
1 #!perl -w
2 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Socket qw(AF_UNIX SOCK_SEQPACKET MSG_EOR pack_sockaddr_un);
7 use Errno qw(EINTR ECONNRESET);
8 use PublicInbox::CmdIPC4;
9 my $narg = 4;
10 my ($sock, $pwd);
11 my $recv_cmd = PublicInbox::CmdIPC4->can('recv_cmd4');
12 my $send_cmd = PublicInbox::CmdIPC4->can('send_cmd4') // do {
13         require PublicInbox::Spawn; # takes ~50ms even if built *sigh*
14         $recv_cmd = PublicInbox::Spawn->can('recv_cmd4');
15         PublicInbox::Spawn->can('send_cmd4');
16 };
17
18 sub sigchld {
19         my ($sig) = @_;
20         my $flags = $sig ? POSIX::WNOHANG() : 0;
21         while (waitpid(-1, $flags) > 0) {}
22 }
23
24 sub exec_cmd {
25         my ($fds, $argc, @argv) = @_;
26         my @m = (*STDIN{IO}, '<&=',  *STDOUT{IO}, '>&=', *STDERR{IO}, '>&=');
27         my @rdr;
28         for my $fd (@$fds) {
29                 my ($old_io, $mode) = splice(@m, 0, 2);
30                 open(my $tmpfh, $mode, $fd) or die "open $mode$fd: $!";
31                 push @rdr, $old_io, $mode, $tmpfh;
32         }
33         require POSIX; # WNOHANG
34         $SIG{CHLD} = \&sigchld;
35         my $pid = fork // die "fork: $!";
36         if ($pid == 0) {
37                 my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
38                 while (my ($old_io, $mode, $tmpfh) = splice(@rdr, 0, 3)) {
39                         open $old_io, $mode, $tmpfh or die "open $mode: $!";
40                 }
41                 %ENV = (%ENV, %env);
42                 exec(@argv);
43                 die "exec: @argv: $!";
44         }
45 }
46
47 if ($send_cmd && eval {
48         my $path = do {
49                 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
50                 if ($runtime_dir eq '/lei') {
51                         require File::Spec;
52                         $runtime_dir = File::Spec->tmpdir."/lei-$<";
53                 }
54                 unless (-d $runtime_dir) {
55                         require File::Path;
56                         File::Path::mkpath($runtime_dir, 0, 0700);
57                 }
58                 "$runtime_dir/$narg.seq.sock";
59         };
60         my $addr = pack_sockaddr_un($path);
61         socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
62         unless (connect($sock, $addr)) { # start the daemon if not started
63                 local $ENV{PERL5LIB} = join(':', @INC);
64                 open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
65                         -E PublicInbox::LEI::lazy_start(@ARGV)],
66                         $path, $! + 0, $narg) or die "popen: $!";
67                 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
68                 close($daemon) or warn <<"";
69 lei-daemon could not start, exited with \$?=$?
70
71                 # try connecting again anyways, unlink+bind may be racy
72                 connect($sock, $addr) or die <<"";
73 connect($path): $! (after attempted daemon start)
74 Falling back to (slow) one-shot mode
75
76         }
77         require Cwd;
78         $pwd = $ENV{PWD} // '';
79         my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=$pwd): $!";
80         if ($pwd ne $cwd) { # prefer ENV{PWD} if it's a symlink to real cwd
81                 my @st_cwd = stat($cwd) or die "stat(cwd=$cwd): $!";
82                 my @st_pwd = stat($pwd); # PWD invalid, use cwd
83                 # make sure st_dev/st_ino match for {PWD} to be valid
84                 $pwd = $cwd if (!@st_pwd || $st_pwd[1] != $st_cwd[1] ||
85                                         $st_pwd[0] != $st_cwd[0]);
86         } else {
87                 $pwd = $cwd;
88         }
89         1;
90 }) { # (Socket::MsgHdr|Inline::C), $sock, $pwd are all available:
91         $ENV{PWD} = $pwd;
92         my $buf = join("\0", scalar(@ARGV), @ARGV);
93         while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
94         $buf .= "\0\0";
95         $send_cmd->($sock, [ 0, 1, 2 ], $buf, MSG_EOR);
96         $SIG{TERM} = $SIG{INT} = $SIG{QUIT} = sub {
97                 my ($sig) = @_; # 'TERM', not an integer :<
98                 $SIG{$sig} = 'DEFAULT';
99                 kill($sig, $$); # exit($signo + 128)
100         };
101         my $x_it_code = 0;
102         while (1) {
103                 my (@fds) = $recv_cmd->($sock, $buf, 4096 * 33);
104                 if (scalar(@fds) == 1 && !defined($fds[0])) {
105                         last if $! == ECONNRESET;
106                         next if $! == EINTR;
107                         die "recvmsg: $!";
108                 }
109                 last if $buf eq '';
110                 if ($buf =~ /\Ax_it ([0-9]+)\z/) {
111                         $x_it_code = $1 + 0;
112                         last;
113                 } elsif ($buf =~ /\Aexec (.+)\z/) {
114                         exec_cmd(\@fds, split(/\0/, $1));
115                 } else {
116                         sigchld();
117                         die $buf;
118                 }
119         }
120         sigchld();
121         if (my $sig = ($x_it_code & 127)) {
122                 kill $sig, $$;
123                 sleep;
124         }
125         exit($x_it_code >> 8);
126 } else { # for systems lacking Socket::MsgHdr or Inline::C
127         warn $@ if $@;
128         require PublicInbox::LEI;
129         PublicInbox::LEI::oneshot(__PACKAGE__);
130 }