]> Sergey Matveev's repositories - public-inbox.git/blobdiff - script/lei
ds: block signals when reaping
[public-inbox.git] / script / lei
index ceaf1e004666474308c37c34d59e2719941e3c0c..2ea98da4d0ac4ec586d9ecb82bb7c69da89c3247 100755 (executable)
@@ -1,11 +1,20 @@
 #!perl -w
-# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
 use v5.10.1;
 use Socket qw(AF_UNIX SOCK_STREAM pack_sockaddr_un);
+my ($send_3fds, $nfd);
 if (my ($sock, $pwd) = eval {
-       require IO::FDPass; # will try to use a daemon to reduce load time
+       $send_3fds = eval {
+               require IO::FDPass;
+               $nfd = 1; # 1 FD per-sendmsg
+               sub { IO::FDPass::send($_[0], $_[$_]) for (1..3) }
+       } // do {
+               require PublicInbox::Spawn; # takes ~50ms even if built *sigh*
+               $nfd = 3; # 3 FDs per-sendmsg(2)
+               PublicInbox::Spawn->can('send_3fds');
+       } // die "IO::FDPass missing or Inline::C not installed/configured\n";
        my $path = do {
                my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
                if ($runtime_dir eq '/lei') {
@@ -16,23 +25,18 @@ if (my ($sock, $pwd) = eval {
                        require File::Path;
                        File::Path::mkpath($runtime_dir, 0, 0700);
                }
-               "$runtime_dir/sock";
+               "$runtime_dir/$nfd.sock";
        };
        my $addr = pack_sockaddr_un($path);
        socket(my $sock, AF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
        unless (connect($sock, $addr)) { # start the daemon if not started
-               my $cmd = [ $^X, qw[-MPublicInbox::LEI
+               local $ENV{PERL5LIB} = join(':', @INC);
+               open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
                        -E PublicInbox::LEI::lazy_start(@ARGV)],
-                       $path, $! + 0 ];
-               my $env = { PERL5LIB => join(':', @INC) };
-               pipe(my ($daemon, $w)) or die "pipe: $!";
-               my $opt = { 1 => $w, 2 => $w };
-               require PublicInbox::Spawn;
-               my $pid = PublicInbox::Spawn::spawn($cmd, $env, $opt);
-               $opt = $w = undef;
+                       $path, $! + 0, $nfd) or die "popen: $!";
                while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
-               waitpid($pid, 0) or warn <<"";
-lei-daemon could not start, PID:$pid exited with \$?=$?
+               close($daemon) or warn <<"";
+lei-daemon could not start, exited with \$?=$?
 
                # try connecting again anyways, unlink+bind may be racy
                unless (connect($sock, $addr)) {
@@ -43,8 +47,8 @@ Falling back to (slow) one-shot mode
                }
        }
        require Cwd;
-       my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=".($ENV{PWD}//'').": $!";
        my $pwd = $ENV{PWD} // '';
+       my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=$pwd): $!";
        if ($pwd ne $cwd) { # prefer ENV{PWD} if it's a symlink to real cwd
                my @st_cwd = stat($cwd) or die "stat(cwd=$cwd): $!";
                my @st_pwd = stat($pwd); # PWD invalid, use cwd
@@ -62,7 +66,7 @@ Falling back to (slow) one-shot mode
        $buf .= "\0\0";
        select $sock;
        $| = 1; # unbuffer selected $sock
-       IO::FDPass::send(fileno($sock), $_) for (0..2);
+       $send_3fds->(fileno($sock), 0, 1, 2);
        print $sock $buf or die "print(sock, buf): $!";
        while ($buf = <$sock>) {
                $buf =~ /\Aexit=([0-9]+)\n\z/ and exit($1 + 0);