2 # Copyright (C) 2020 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_STREAM pack_sockaddr_un);
8 if (eval { require IO::FDPass; 1 }) { # use daemon to reduce load time
10 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
11 if ($runtime_dir eq '/lei') {
13 $runtime_dir = File::Spec->tmpdir."/lei-$<";
15 unless (-d $runtime_dir) {
17 File::Path::mkpath($runtime_dir, 0, 0700);
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
25 my $env = { PERL5LIB => join(':', @INC) };
26 my $cmd = [ $^X, qw[-MPublicInbox::LEI
27 -E PublicInbox::LEI::lazy_start(@ARGV)],
29 require PublicInbox::Spawn;
30 waitpid(PublicInbox::Spawn::spawn($cmd, $env), 0);
31 warn "lei-daemon exited with \$?=$?\n" if $?;
33 # try connecting again anyways, unlink+bind may be racy
34 connect($sock, $addr) or die
35 "connect($path): $! (after attempted daemon start)";
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]);
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" }
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);
62 } else { # for systems lacking IO::FDPass
63 require PublicInbox::LEI;
64 PublicInbox::LEI::oneshot(__PACKAGE__);