]> Sergey Matveev's repositories - public-inbox.git/blob - script/lei
lei: micro-optimize startup time
[public-inbox.git] / script / lei
1 #!perl -w
2 # Copyright (C) 2020 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
8 if (eval { require IO::FDPass; 1 }) { # use daemon to reduce load time
9         my $path = do {
10                 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
11                 if ($runtime_dir eq '/lei') {
12                         require File::Spec;
13                         $runtime_dir = File::Spec->tmpdir."/lei-$<";
14                 }
15                 unless (-d $runtime_dir) {
16                         require File::Path;
17                         File::Path::mkpath($runtime_dir, 0, 0700);
18                 }
19                 "$runtime_dir/sock";
20         };
21         my $addr = pack_sockaddr_un($path);
22         socket(my $sock, AF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
23         unless (connect($sock, $addr)) { # start the daemon if not started
24                 my $err = $! + 0;
25                 my $env = { PERL5LIB => join(':', @INC) };
26                 my $cmd = [ $^X, qw[-MPublicInbox::LEI
27                         -E PublicInbox::LEI::lazy_start(@ARGV)],
28                         $path, $err ];
29                 require PublicInbox::Spawn;
30                 waitpid(PublicInbox::Spawn::spawn($cmd, $env), 0);
31                 warn "lei-daemon exited with \$?=$?\n" if $?;
32
33                 # try connecting again anyways, unlink+bind may be racy
34                 connect($sock, $addr) or die
35                         "connect($path): $! (after attempted daemon start)";
36         }
37         require Cwd;
38         my $cwd = Cwd::fastcwd() // die "fastcwd: $!";
39         my $pwd = $ENV{PWD} // '';
40         if ($pwd eq $cwd) { # likely, all good
41         } elsif ($pwd) { # prefer ENV{PWD} if it's a symlink to real cwd
42                 my @st_cwd = stat($cwd) or die "stat(cwd=$cwd): $!\n";
43                 my @st_pwd = stat($pwd);
44                 # make sure st_dev/st_ino match for {PWD} to be valid
45                 $pwd = $cwd if (!@st_pwd || $st_pwd[1] != $st_cwd[1] ||
46                                         $st_pwd[0] != $st_cwd[0]);
47         } else {
48                 $pwd = $cwd;
49         }
50         local $ENV{PWD} = $pwd;
51         my $buf = "$$\0\0>" . join("]\0[", @ARGV) . "\0\0>";
52         while (my ($k, $v) = each %ENV) { $buf .= "$k=$v\0" }
53         $buf .= "\0\0";
54         select $sock;
55         $| = 1; # unbuffer selected $sock
56         IO::FDPass::send(fileno($sock), $_) for (0..2);
57         print $sock $buf or die "print(sock, buf): $!";
58         while ($buf = <$sock>) {
59                 $buf =~ /\Aexit=([0-9]+)\n\z/ and exit($1 + 0);
60                 die $buf;
61         }
62 } else { # for systems lacking IO::FDPass
63         require PublicInbox::LEI;
64         PublicInbox::LEI::oneshot(__PACKAGE__);
65 }