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 my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //= (
13 $ENV{XDG_CACHE_HOME} //
14 ( ($ENV{HOME} // '/nonexistent').'/.cache' )
15 ).'/public-inbox/inline-c';
16 if (!-d $inline_dir) {
18 File::Path::make_path($inline_dir);
20 require PublicInbox::Spawn; # takes ~50ms even if built *sigh*
21 $recv_cmd = PublicInbox::Spawn->can('recv_cmd4');
22 PublicInbox::Spawn->can('send_cmd4');
23 } // die 'please install Inline::C or Socket::MsgHdr';
27 my $flags = scalar(@_) ? POSIX::WNOHANG() : 0;
28 for my $pid (keys %pids) {
29 delete($pids{$pid}) if waitpid($pid, $flags) == $pid;
34 my ($fds, $argc, @argv) = @_;
37 my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO});
40 open(my $newfh, '+<&=', $fd) or die "open +<&=$fd: $!";
41 push @rdr, shift(@old), $newfh;
44 my @non_std; # ex. $op_p from lei_edit_search
45 while (my ($io, $newfh) = splice(@rdr, 0, 2)) {
47 open $io, '+<&', $newfh or die "open +<&=: $!";
48 push @non_std, $io unless $old_io;
52 fcntl($_, Fcntl::F_SETFD(), 0) for @non_std;
54 my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
55 @ENV{keys %env} = values %env;
57 warn "exec: @argv: $!\n";
60 $SIG{CHLD} = $sigchld;
61 my $pid = fork // die "fork: $!";
63 $do_exec->() if $fds->[1]; # git-credential, pager
65 # parent backgrounds on MUA
66 POSIX::setsid() > 0 or die "setsid: $!";
68 return; # continue $recv_cmd in background
73 $do_exec->(); # MUA reuses stdout
77 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
78 if ($runtime_dir eq '/lei') {
80 $runtime_dir = File::Spec->tmpdir."/lei-$<";
82 unless (-d $runtime_dir) {
84 File::Path::make_path($runtime_dir, { mode => 0700 });
86 my $path = "$runtime_dir/$narg.seq.sock";
87 my $addr = pack_sockaddr_un($path);
88 socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
89 unless (connect($sock, $addr)) { # start the daemon if not started
90 local $ENV{PERL5LIB} = join(':', @INC);
91 open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
92 -E PublicInbox::LEI::lazy_start(@ARGV)],
93 $path, $! + 0, $narg) or die "popen: $!";
94 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
95 close($daemon) or warn <<"";
96 lei-daemon could not start, exited with \$?=$?
98 # try connecting again anyways, unlink+bind may be racy
99 connect($sock, $addr) or die <<"";
100 connect($path): $! (after attempted daemon start)
103 # (Socket::MsgHdr|Inline::C), $sock are all available:
104 open my $dh, '<', '.' or die "open(.) $!";
105 my $buf = join("\0", scalar(@ARGV), @ARGV);
106 while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
108 my $n = $send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR);
110 die "sendmsg: $! (check RLIMIT_NOFILE)\n" if $!{ETOOMANYREFS};
115 my (@fds) = $recv_cmd->($sock, my $buf, 4096 * 33);
116 if (scalar(@fds) == 1 && !defined($fds[0])) {
121 if ($buf =~ /\Aexec (.+)\z/) {
122 $exec_cmd->(\@fds, split(/\0/, $1));
123 } elsif ($buf eq '-WINCH') {
124 kill($buf, @parent); # for MUA
125 } elsif ($buf =~ /\Ax_it ([0-9]+)\z/) {
126 $x_it_code ||= $1 + 0;
128 } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
129 $x_it_code ||= $1 + 0;
136 if (my $sig = ($x_it_code & 127)) {
140 exit($x_it_code >> 8);