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;
58 warn "exec: @argv: $!\n";
61 $SIG{CHLD} = $sigchld;
62 my $pid = fork // die "fork: $!";
64 $do_exec->() if $fds->[1]; # git-credential, pager
66 # parent backgrounds on MUA
67 POSIX::setsid() > 0 or die "setsid: $!";
69 return; # continue $recv_cmd in background
74 $do_exec->(); # MUA reuses stdout
78 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
79 if ($runtime_dir eq '/lei') {
81 $runtime_dir = File::Spec->tmpdir."/lei-$<";
83 unless (-d $runtime_dir) {
85 File::Path::make_path($runtime_dir, { mode => 0700 });
87 my $path = "$runtime_dir/$narg.seq.sock";
88 my $addr = pack_sockaddr_un($path);
89 socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
90 unless (connect($sock, $addr)) { # start the daemon if not started
91 local $ENV{PERL5LIB} = join(':', @INC);
92 open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
93 -E PublicInbox::LEI::lazy_start(@ARGV)],
94 $path, $! + 0, $narg) or die "popen: $!";
95 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
96 close($daemon) or warn <<"";
97 lei-daemon could not start, exited with \$?=$?
99 # try connecting again anyways, unlink+bind may be racy
100 connect($sock, $addr) or die <<"";
101 connect($path): $! (after attempted daemon start)
104 # (Socket::MsgHdr|Inline::C), $sock are all available:
105 open my $dh, '<', '.' or die "open(.) $!";
106 my $buf = join("\0", scalar(@ARGV), @ARGV);
107 while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
109 $send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR) or die "sendmsg: $!";
110 $SIG{TSTP} = sub { send($sock, 'STOP', MSG_EOR); kill 'STOP', $$ };
111 $SIG{CONT} = sub { send($sock, 'CONT', MSG_EOR) };
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 eq 'umask') {
126 send($sock, 'u'.pack('V', umask), MSG_EOR) or die "send: $!"
127 } elsif ($buf =~ /\Ax_it ([0-9]+)\z/) {
128 $x_it_code ||= $1 + 0;
130 } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
131 $x_it_code ||= $1 + 0;
132 } elsif ($buf eq 'wait') {
140 if (my $sig = ($x_it_code & 127)) {
142 sleep(1) while 1; # no self-pipe/signalfd, here, so we loop
144 exit($x_it_code >> 8);