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