2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use Socket qw(AF_UNIX SOCK_SEQPACKET MSG_EOR pack_sockaddr_un);
6 use PublicInbox::CmdIPC4;
9 my $recv_cmd = PublicInbox::CmdIPC4->can('recv_cmd4');
10 my $send_cmd = PublicInbox::CmdIPC4->can('send_cmd4') // do {
11 require PublicInbox::Syscall;
12 $recv_cmd = PublicInbox::Syscall->can('recv_cmd4');
13 PublicInbox::Syscall->can('send_cmd4');
15 my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //= (
16 $ENV{XDG_CACHE_HOME} //
17 ( ($ENV{HOME} // '/nonexistent').'/.cache' )
18 ).'/public-inbox/inline-c';
19 if (!-d $inline_dir) {
21 File::Path::make_path($inline_dir);
23 require PublicInbox::Spawn; # takes ~50ms even if built *sigh*
24 $recv_cmd = PublicInbox::Spawn->can('recv_cmd4');
25 PublicInbox::Spawn->can('send_cmd4');
26 } // die 'please install Inline::C or Socket::MsgHdr';
30 my $flags = scalar(@_) ? POSIX::WNOHANG() : 0;
31 for my $pid (keys %pids) {
32 delete($pids{$pid}) if waitpid($pid, $flags) == $pid;
37 my ($fds, $argc, @argv) = @_;
40 my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO});
43 open(my $newfh, '+<&=', $fd) or die "open +<&=$fd: $!";
44 push @rdr, shift(@old), $newfh;
47 my @non_std; # ex. $op_p from lei_edit_search
48 while (my ($io, $newfh) = splice(@rdr, 0, 2)) {
50 open $io, '+<&', $newfh or die "open +<&=: $!";
51 push @non_std, $io unless $old_io;
55 fcntl($_, Fcntl::F_SETFD(), 0) for @non_std;
57 my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
58 @ENV{keys %env} = values %env;
61 warn "exec: @argv: $!\n";
64 $SIG{CHLD} = $sigchld;
65 my $pid = fork // die "fork: $!";
67 $do_exec->() if $fds->[1]; # git-credential, pager
69 # parent backgrounds on MUA
70 POSIX::setsid() > 0 or die "setsid: $!";
72 return; # continue $recv_cmd in background
77 $do_exec->(); # MUA reuses stdout
81 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
82 if ($runtime_dir eq '/lei') {
84 $runtime_dir = File::Spec->tmpdir."/lei-$<";
86 unless (-d $runtime_dir) {
88 File::Path::make_path($runtime_dir, { mode => 0700 });
90 my $path = "$runtime_dir/$narg.seq.sock";
91 my $addr = pack_sockaddr_un($path);
92 socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
93 unless (connect($sock, $addr)) { # start the daemon if not started
94 local $ENV{PERL5LIB} = join(':', @INC);
95 open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
96 -E PublicInbox::LEI::lazy_start(@ARGV)],
97 $path, $! + 0, $narg) or die "popen: $!";
98 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
99 close($daemon) or warn <<"";
100 lei-daemon could not start, exited with \$?=$?
102 # try connecting again anyways, unlink+bind may be racy
103 connect($sock, $addr) or die <<"";
104 connect($path): $! (after attempted daemon start)
107 # (Socket::MsgHdr|Inline::C), $sock are all available:
108 open my $dh, '<', '.' or die "open(.) $!";
109 my $buf = join("\0", scalar(@ARGV), @ARGV);
110 while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
112 $send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR) or die "sendmsg: $!";
113 $SIG{TSTP} = sub { send($sock, 'STOP', MSG_EOR); kill 'STOP', $$ };
114 $SIG{CONT} = sub { send($sock, 'CONT', MSG_EOR) };
118 my (@fds) = $recv_cmd->($sock, my $buf, 4096 * 33);
119 if (scalar(@fds) == 1 && !defined($fds[0])) {
124 if ($buf =~ /\Aexec (.+)\z/) {
125 $exec_cmd->(\@fds, split(/\0/, $1));
126 } elsif ($buf eq '-WINCH') {
127 kill($buf, @parent); # for MUA
128 } elsif ($buf eq 'umask') {
129 send($sock, 'u'.pack('V', umask), MSG_EOR) or die "send: $!"
130 } elsif ($buf =~ /\Ax_it ([0-9]+)\z/) {
131 $x_it_code ||= $1 + 0;
133 } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
134 $x_it_code ||= $1 + 0;
135 } elsif ($buf eq 'wait') {
143 if (my $sig = ($x_it_code & 127)) {
145 sleep(1) while 1; # no self-pipe/signalfd, here, so we loop
147 exit($x_it_code >> 8);