]> Sergey Matveev's repositories - public-inbox.git/blob - script/lei
lei: prefer IO::FDPass over our Inline::C recv_3fds
[public-inbox.git] / script / lei
1 #!perl -w
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>
4 use strict;
5 use v5.10.1;
6 use Socket qw(AF_UNIX SOCK_STREAM pack_sockaddr_un);
7 my ($send_3fds, $nfd);
8 if (my ($sock, $pwd) = eval {
9         $send_3fds = eval {
10                 require IO::FDPass;
11                 $nfd = 1; # 1 FD per-sendmsg
12                 sub { IO::FDPass::send($_[0], $_[$_]) for (1..3) }
13         } // do {
14                 require PublicInbox::Spawn; # takes ~50ms even if built *sigh*
15                 $nfd = 3; # 3 FDs per-sendmsg(2)
16                 PublicInbox::Spawn->can('send_3fds');
17         } // die "IO::FDPass missing or Inline::C not installed/configured\n";
18         my $path = do {
19                 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
20                 if ($runtime_dir eq '/lei') {
21                         require File::Spec;
22                         $runtime_dir = File::Spec->tmpdir."/lei-$<";
23                 }
24                 unless (-d $runtime_dir) {
25                         require File::Path;
26                         File::Path::mkpath($runtime_dir, 0, 0700);
27                 }
28                 "$runtime_dir/$nfd.sock";
29         };
30         my $addr = pack_sockaddr_un($path);
31         socket(my $sock, AF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
32         unless (connect($sock, $addr)) { # start the daemon if not started
33                 local $ENV{PERL5LIB} = join(':', @INC);
34                 open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
35                         -E PublicInbox::LEI::lazy_start(@ARGV)],
36                         $path, $! + 0, $nfd) or die "popen: $!";
37                 while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
38                 close($daemon) or warn <<"";
39 lei-daemon could not start, exited with \$?=$?
40
41                 # try connecting again anyways, unlink+bind may be racy
42                 unless (connect($sock, $addr)) {
43                         die <<"";
44 connect($path): $! (after attempted daemon start)
45 Falling back to (slow) one-shot mode
46
47                 }
48         }
49         require Cwd;
50         my $pwd = $ENV{PWD} // '';
51         my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=$pwd): $!";
52         if ($pwd ne $cwd) { # prefer ENV{PWD} if it's a symlink to real cwd
53                 my @st_cwd = stat($cwd) or die "stat(cwd=$cwd): $!";
54                 my @st_pwd = stat($pwd); # PWD invalid, use cwd
55                 # make sure st_dev/st_ino match for {PWD} to be valid
56                 $pwd = $cwd if (!@st_pwd || $st_pwd[1] != $st_cwd[1] ||
57                                         $st_pwd[0] != $st_cwd[0]);
58         } else {
59                 $pwd = $cwd;
60         }
61         ($sock, $pwd);
62 }) { # IO::FDPass, $sock, $pwd are all available:
63         local $ENV{PWD} = $pwd;
64         my $buf = "$$\0\0>" . join("]\0[", @ARGV) . "\0\0>";
65         while (my ($k, $v) = each %ENV) { $buf .= "$k=$v\0" }
66         $buf .= "\0\0";
67         select $sock;
68         $| = 1; # unbuffer selected $sock
69         $send_3fds->(fileno($sock), 0, 1, 2);
70         print $sock $buf or die "print(sock, buf): $!";
71         while ($buf = <$sock>) {
72                 $buf =~ /\Aexit=([0-9]+)\n\z/ and exit($1 + 0);
73                 die $buf;
74         }
75 } else { # for systems lacking IO::FDPass
76         # don't warn about IO::FDPass since it's not commonly installed
77         warn $@ if $@ && index($@, 'IO::FDPass') < 0;
78         require PublicInbox::LEI;
79         PublicInbox::LEI::oneshot(__PACKAGE__);
80 }