2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
9 if (eval { require IO::FDPass; 1 }) { # use daemon to reduce load time
11 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
12 if ($runtime_dir eq '/lei') {
14 $runtime_dir = File::Spec->tmpdir."/lei-$<";
16 unless (-d $runtime_dir && -w _) {
18 File::Path::mkpath($runtime_dir, 0, 0700);
22 my $sock = IO::Socket::UNIX->new(Peer => $path, Type => SOCK_STREAM);
23 unless ($sock) { # start the daemon if not started
25 my $env = { PERL5LIB => join(':', @INC) };
26 my $cmd = [ $^X, qw[-MPublicInbox::LeiDaemon
27 -E PublicInbox::LeiDaemon::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 $sock = IO::Socket::UNIX->new(Peer => $path,
35 Type => SOCK_STREAM) // die
36 "connect($path): $! (after attempted daemon start)";
40 if ($pwd) { # prefer ENV{PWD} if it's a symlink to real cwd
41 my @st_cwd = stat($cwd) or die "stat(cwd=$cwd): $!\n";
42 my @st_pwd = stat($pwd);
43 # make sure st_dev/st_ino match for {PWD} to be valid
44 $pwd = $cwd if (!@st_pwd || $st_pwd[1] != $st_cwd[1] ||
45 $st_pwd[0] != $st_cwd[0]);
49 local $ENV{PWD} = $pwd;
51 IO::FDPass::send(fileno($sock), $_) for (0..2);
52 my $buf = "$$\0\0>" . join("]\0[", @ARGV) . "\0\0>";
53 while (my ($k, $v) = each %ENV) { $buf .= "$k=$v\0" }
55 print $sock $buf or die "print(sock, buf): $!";
57 while (my $line = <$sock>) {
58 $line =~ /\Aexit=([0-9]+)\n\z/ and exit($1 + 0);
61 } else { # for systems lacking IO::FDPass
62 require PublicInbox::LeiDaemon;
63 PublicInbox::LeiDaemon::oneshot();