]> Sergey Matveev's repositories - public-inbox.git/blob - script/lei
lei: fix git-credential handling
[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         $do_exec->() if !scalar(@$fds); # MUA reuses all FDs
56 };
57
58 if ($send_cmd && eval {
59         my $path = do {
60                 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
61                 if ($runtime_dir eq '/lei') {
62                         require File::Spec;
63                         $runtime_dir = File::Spec->tmpdir."/lei-$<";
64                 }
65                 unless (-d $runtime_dir) {
66                         require File::Path;
67                         File::Path::mkpath($runtime_dir, 0, 0700);
68                 }
69                 "$runtime_dir/$narg.seq.sock";
70         };
71         my $addr = pack_sockaddr_un($path);
72         socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
73         unless (connect($sock, $addr)) { # start the daemon if not started
74                 local $ENV{PERL5LIB} = join(':', @INC);
75                 open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
76                         -E PublicInbox::LEI::lazy_start(@ARGV)],
77                         $path, $! + 0, $narg) or die "popen: $!";
78                 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
79                 close($daemon) or warn <<"";
80 lei-daemon could not start, exited with \$?=$?
81
82                 # try connecting again anyways, unlink+bind may be racy
83                 connect($sock, $addr) or die <<"";
84 connect($path): $! (after attempted daemon start)
85 Falling back to (slow) one-shot mode
86
87         }
88         # (Socket::MsgHdr|Inline::C), $sock are all available:
89         open my $dh, '<', '.' or die "open(.) $!";
90         my $buf = join("\0", scalar(@ARGV), @ARGV);
91         while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
92         $buf .= "\0\0";
93         my $n = $send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR);
94         if (!$n) {
95                 die "sendmsg: $! (check RLIMIT_NOFILE)\n" if $!{ETOOMANYREFS};
96                 die "sendmsg: $!\n";
97         }
98         1;
99 }) { # connected and request sent to lei-daemon, wait for responses or EOF
100         my $x_it_code = 0;
101         while (1) {
102                 my (@fds) = $recv_cmd->($sock, my $buf, 4096 * 33);
103                 if (scalar(@fds) == 1 && !defined($fds[0])) {
104                         next if $!{EINTR};
105                         last if $!{ECONNRESET};
106                         die "recvmsg: $!";
107                 }
108                 last if $buf eq '';
109                 if ($buf =~ /\Aexec (.+)\z/) {
110                         $exec_cmd->(\@fds, split(/\0/, $1));
111                 } elsif ($buf eq '-WINCH') {
112                         kill($buf, @parent); # for MUA
113                 } elsif ($buf =~ /\Ax_it ([0-9]+)\z/) {
114                         $x_it_code = $1 + 0;
115                         last;
116                 } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
117                         $x_it_code = $1 + 0;
118                 } else {
119                         $sigchld->();
120                         die $buf;
121                 }
122         }
123         $sigchld->();
124         if (my $sig = ($x_it_code & 127)) {
125                 kill $sig, $$;
126                 sleep(1) while 1;
127         }
128         exit($x_it_code >> 8);
129 } else { # for systems lacking Socket::MsgHdr or Inline::C
130         warn $@ if $@;
131         require PublicInbox::LEI;
132         PublicInbox::LEI::oneshot(__PACKAGE__);
133 }