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 = $sig ? POSIX::WNOHANG() : 0;
20 while (waitpid(-1, $flags) > 0) {}
24 my ($fds, $argc, @argv) = @_;
25 my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO});
28 open(my $tmpfh, '+<&=', $fd) or die "open +<&=$fd: $!";
29 push @rdr, shift(@old), $tmpfh;
31 require POSIX; # WNOHANG
32 $SIG{CHLD} = \&sigchld;
33 my $pid = fork // die "fork: $!";
35 my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
36 while (my ($old_io, $tmpfh) = splice(@rdr, 0, 2)) {
37 open $old_io, '+<&', $tmpfh or die "open +<&=: $!";
41 die "exec: @argv: $!";
45 if ($send_cmd && eval {
47 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
48 if ($runtime_dir eq '/lei') {
50 $runtime_dir = File::Spec->tmpdir."/lei-$<";
52 unless (-d $runtime_dir) {
54 File::Path::mkpath($runtime_dir, 0, 0700);
56 "$runtime_dir/$narg.seq.sock";
58 my $addr = pack_sockaddr_un($path);
59 socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
60 unless (connect($sock, $addr)) { # start the daemon if not started
61 local $ENV{PERL5LIB} = join(':', @INC);
62 open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
63 -E PublicInbox::LEI::lazy_start(@ARGV)],
64 $path, $! + 0, $narg) or die "popen: $!";
65 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
66 close($daemon) or warn <<"";
67 lei-daemon could not start, exited with \$?=$?
69 # try connecting again anyways, unlink+bind may be racy
70 connect($sock, $addr) or die <<"";
71 connect($path): $! (after attempted daemon start)
72 Falling back to (slow) one-shot mode
75 # (Socket::MsgHdr|Inline::C), $sock are all available:
76 open my $dh, '<', '.' or die "open(.) $!";
77 my $buf = join("\0", scalar(@ARGV), @ARGV);
78 while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
80 my $n = $send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR);
81 if (!$n && $!{ETOOMANYREFS} && eval { require BSD::Resource }) {
82 my $NOFILE = BSD::Resource::RLIMIT_NOFILE();
83 my ($s, $h) = BSD::Resource::getrlimit($NOFILE);
84 if ($s < $h && BSD::Resource::setrlimit($NOFILE, $h, $h)) {
85 $n = $send_cmd->($sock, [0, 1, 2, fileno($dh)],
90 die "sendmsg: $! (check RLIMIT_NOFILE)\n" if $!{ETOOMANYREFS};
94 }) { # connected and request sent to lei-daemon, wait for responses or EOF
97 my (@fds) = $recv_cmd->($sock, my $buf, 4096 * 33);
98 if (scalar(@fds) == 1 && !defined($fds[0])) {
100 last if $!{ECONNRESET};
104 if ($buf =~ /\Ax_it ([0-9]+)\z/) {
107 } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
109 } elsif ($buf =~ /\Aexec (.+)\z/) {
110 exec_cmd(\@fds, split(/\0/, $1));
117 if (my $sig = ($x_it_code & 127)) {
121 exit($x_it_code >> 8);
122 } else { # for systems lacking Socket::MsgHdr or Inline::C
124 require PublicInbox::LEI;
125 PublicInbox::LEI::oneshot(__PACKAGE__);