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>
6 use Socket qw(AF_UNIX SOCK_SEQPACKET MSG_EOR pack_sockaddr_un);
7 use PublicInbox::CmdIPC4;
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');
19 my $flags = scalar(@_) ? POSIX::WNOHANG() : 0;
20 for my $pid (keys %pids) {
21 delete($pids{$pid}) if waitpid($pid, $flags) == $pid;
26 my ($fds, $argc, @argv) = @_;
29 my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO});
32 open(my $newfh, '+<&=', $fd) or die "open +<&=$fd: $!";
33 push @rdr, shift(@old), $newfh;
36 my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
37 @ENV{keys %env} = values %env;
39 warn "exec: @argv: $!\n";
42 $SIG{CHLD} = $sigchld;
43 my $pid = fork // die "fork: $!";
45 while (my ($io, $newfh) = splice(@rdr, 0, 2)) {
46 open $io, '+<&', $newfh or die "open +<&=: $!";
48 $do_exec->() if scalar(@$fds); # git-credential, pager
50 # parent backgrounds on MUA
51 POSIX::setsid() > 0 or die "setsid: $!";
53 return; # continue $recv_cmd in background
58 $do_exec->(); # MUA reuses all FDs
62 if ($send_cmd && eval {
64 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
65 if ($runtime_dir eq '/lei') {
67 $runtime_dir = File::Spec->tmpdir."/lei-$<";
69 unless (-d $runtime_dir) {
71 File::Path::mkpath($runtime_dir, 0, 0700);
73 "$runtime_dir/$narg.seq.sock";
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 \$?=$?
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
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" }
97 my $n = $send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR);
99 die "sendmsg: $! (check RLIMIT_NOFILE)\n" if $!{ETOOMANYREFS};
103 }) { # connected and request sent to lei-daemon, wait for responses or EOF
106 my (@fds) = $recv_cmd->($sock, my $buf, 4096 * 33);
107 if (scalar(@fds) == 1 && !defined($fds[0])) {
109 last if $!{ECONNRESET};
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/) {
120 } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
128 if (my $sig = ($x_it_code & 127)) {
132 exit($x_it_code >> 8);
133 } else { # for systems lacking Socket::MsgHdr or Inline::C
135 require PublicInbox::LEI;
136 PublicInbox::LEI::oneshot(__PACKAGE__);