]> Sergey Matveev's repositories - public-inbox.git/blob - script/lei
rename LeiDaemon package to PublicInbox::LEI
[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 Cwd qw(cwd);
7 use IO::Socket::UNIX;
8
9 if (eval { require IO::FDPass; 1 }) { # use daemon to reduce load time
10         my $path = do {
11                 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
12                 if ($runtime_dir eq '/lei') {
13                         require File::Spec;
14                         $runtime_dir = File::Spec->tmpdir."/lei-$<";
15                 }
16                 unless (-d $runtime_dir && -w _) {
17                         require File::Path;
18                         File::Path::mkpath($runtime_dir, 0, 0700);
19                 }
20                 "$runtime_dir/sock";
21         };
22         my $sock = IO::Socket::UNIX->new(Peer => $path, Type => SOCK_STREAM);
23         unless ($sock) { # 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                 $sock = IO::Socket::UNIX->new(Peer => $path,
35                                                 Type => SOCK_STREAM) // die
36                         "connect($path): $! (after attempted daemon start)";
37         }
38         my $pwd = $ENV{PWD};
39         my $cwd = cwd();
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]);
46         } else {
47                 $pwd = $cwd;
48         }
49         local $ENV{PWD} = $pwd;
50         $sock->autoflush(1);
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" }
54         $buf .= "\0\0";
55         print $sock $buf or die "print(sock, buf): $!";
56         local $/ = "\n";
57         while (my $line = <$sock>) {
58                 $line =~ /\Aexit=([0-9]+)\n\z/ and exit($1 + 0);
59                 die $line;
60         }
61 } else { # for systems lacking IO::FDPass
62         require PublicInbox::LEI;
63         PublicInbox::LEI::oneshot(__PACKAGE__);
64 }