]> Sergey Matveev's repositories - public-inbox.git/blob - script/lei
script/lei: waitpid for git-credential and pager
[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 PublicInbox::CmdIPC4;
8 my $narg = 5;
9 my $sock;
10 my $recv_cmd = PublicInbox::CmdIPC4->can('recv_cmd4');
11 my $send_cmd = PublicInbox::CmdIPC4->can('send_cmd4') // do {
12         require PublicInbox::Spawn; # takes ~50ms even if built *sigh*
13         $recv_cmd = PublicInbox::Spawn->can('recv_cmd4');
14         PublicInbox::Spawn->can('send_cmd4');
15 };
16
17 my %pids;
18 my $sigchld = sub {
19         my $flags = scalar(@_) ? POSIX::WNOHANG() : 0;
20         for my $pid (keys %pids) {
21                 delete($pids{$pid}) if waitpid($pid, $flags) == $pid;
22         }
23 };
24 my @parent;
25 my $exec_cmd = sub {
26         my ($fds, $argc, @argv) = @_;
27         my $parent = $$;
28         require POSIX;
29         my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO});
30         my @rdr;
31         for my $fd (@$fds) {
32                 open(my $newfh, '+<&=', $fd) or die "open +<&=$fd: $!";
33                 push @rdr, shift(@old), $newfh;
34         }
35         my $do_exec = sub {
36                 my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
37                 @ENV{keys %env} = values %env;
38                 exec(@argv);
39                 warn "exec: @argv: $!\n";
40                 POSIX::_exit(1);
41         };
42         $SIG{CHLD} = $sigchld;
43         my $pid = fork // die "fork: $!";
44         if ($pid == 0) {
45                 while (my ($io, $newfh) = splice(@rdr, 0, 2)) {
46                         open $io, '+<&', $newfh or die "open +<&=: $!";
47                 }
48                 $do_exec->() if scalar(@$fds); # git-credential, pager
49
50                 # parent backgrounds on MUA
51                 POSIX::setsid() > 0 or die "setsid: $!";
52                 @parent = ($parent);
53                 return; # continue $recv_cmd in background
54         }
55         if (scalar(@$fds)) {
56                 $pids{$pid} = undef;
57         } else {
58                 $do_exec->(); # MUA reuses all FDs
59         }
60 };
61
62 if ($send_cmd && eval {
63         my $path = do {
64                 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
65                 if ($runtime_dir eq '/lei') {
66                         require File::Spec;
67                         $runtime_dir = File::Spec->tmpdir."/lei-$<";
68                 }
69                 unless (-d $runtime_dir) {
70                         require File::Path;
71                         File::Path::mkpath($runtime_dir, 0, 0700);
72                 }
73                 "$runtime_dir/$narg.seq.sock";
74         };
75         my $addr = pack_sockaddr_un($path);
76         socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
77         unless (connect($sock, $addr)) { # start the daemon if not started
78                 local $ENV{PERL5LIB} = join(':', @INC);
79                 open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
80                         -E PublicInbox::LEI::lazy_start(@ARGV)],
81                         $path, $! + 0, $narg) or die "popen: $!";
82                 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
83                 close($daemon) or warn <<"";
84 lei-daemon could not start, exited with \$?=$?
85
86                 # try connecting again anyways, unlink+bind may be racy
87                 connect($sock, $addr) or die <<"";
88 connect($path): $! (after attempted daemon start)
89 Falling back to (slow) one-shot mode
90
91         }
92         # (Socket::MsgHdr|Inline::C), $sock are all available:
93         open my $dh, '<', '.' or die "open(.) $!";
94         my $buf = join("\0", scalar(@ARGV), @ARGV);
95         while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
96         $buf .= "\0\0";
97         my $n = $send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR);
98         if (!$n) {
99                 die "sendmsg: $! (check RLIMIT_NOFILE)\n" if $!{ETOOMANYREFS};
100                 die "sendmsg: $!\n";
101         }
102         1;
103 }) { # connected and request sent to lei-daemon, wait for responses or EOF
104         my $x_it_code = 0;
105         while (1) {
106                 my (@fds) = $recv_cmd->($sock, my $buf, 4096 * 33);
107                 if (scalar(@fds) == 1 && !defined($fds[0])) {
108                         next if $!{EINTR};
109                         last if $!{ECONNRESET};
110                         die "recvmsg: $!";
111                 }
112                 last if $buf eq '';
113                 if ($buf =~ /\Aexec (.+)\z/) {
114                         $exec_cmd->(\@fds, split(/\0/, $1));
115                 } elsif ($buf eq '-WINCH') {
116                         kill($buf, @parent); # for MUA
117                 } elsif ($buf =~ /\Ax_it ([0-9]+)\z/) {
118                         $x_it_code = $1 + 0;
119                         last;
120                 } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
121                         $x_it_code = $1 + 0;
122                 } else {
123                         $sigchld->();
124                         die $buf;
125                 }
126         }
127         $sigchld->();
128         if (my $sig = ($x_it_code & 127)) {
129                 kill $sig, $$;
130                 sleep(1) while 1;
131         }
132         exit($x_it_code >> 8);
133 } else { # for systems lacking Socket::MsgHdr or Inline::C
134         warn $@ if $@;
135         require PublicInbox::LEI;
136         PublicInbox::LEI::oneshot(__PACKAGE__);
137 }