]> Sergey Matveev's repositories - public-inbox.git/blob - script/lei
No ext_urls
[public-inbox.git] / script / lei
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use v5.12;
5 use Socket qw(AF_UNIX SOCK_SEQPACKET MSG_EOR pack_sockaddr_un);
6 use PublicInbox::CmdIPC4;
7 my $narg = 5;
8 my $sock;
9 my $recv_cmd = PublicInbox::CmdIPC4->can('recv_cmd4');
10 my $send_cmd = PublicInbox::CmdIPC4->can('send_cmd4') // do {
11         require PublicInbox::Syscall;
12         $recv_cmd = PublicInbox::Syscall->can('recv_cmd4');
13         PublicInbox::Syscall->can('send_cmd4');
14 } // do {
15         my $inline_dir = $ENV{PERL_INLINE_DIRECTORY} //= (
16                         $ENV{XDG_CACHE_HOME} //
17                         ( ($ENV{HOME} // '/nonexistent').'/.cache' )
18                         ).'/public-inbox/inline-c';
19         if (!-d $inline_dir) {
20                 require File::Path;
21                 File::Path::make_path($inline_dir);
22         }
23         require PublicInbox::Spawn; # takes ~50ms even if built *sigh*
24         $recv_cmd = PublicInbox::Spawn->can('recv_cmd4');
25         PublicInbox::Spawn->can('send_cmd4');
26 } // die 'please install Inline::C or Socket::MsgHdr';
27
28 my %pids;
29 my $sigchld = sub {
30         my $flags = scalar(@_) ? POSIX::WNOHANG() : 0;
31         for my $pid (keys %pids) {
32                 delete($pids{$pid}) if waitpid($pid, $flags) == $pid;
33         }
34 };
35 my @parent;
36 my $exec_cmd = sub {
37         my ($fds, $argc, @argv) = @_;
38         my $parent = $$;
39         require POSIX;
40         my @old = (*STDIN{IO}, *STDOUT{IO}, *STDERR{IO});
41         my @rdr;
42         for my $fd (@$fds) {
43                 open(my $newfh, '+<&=', $fd) or die "open +<&=$fd: $!";
44                 push @rdr, shift(@old), $newfh;
45         }
46         my $do_exec = sub {
47                 my @non_std; # ex. $op_p from lei_edit_search
48                 while (my ($io, $newfh) = splice(@rdr, 0, 2)) {
49                         my $old_io = !!$io;
50                         open $io, '+<&', $newfh or die "open +<&=: $!";
51                         push @non_std, $io unless $old_io;
52                 }
53                 if (@non_std) {
54                         require Fcntl;
55                         fcntl($_, Fcntl::F_SETFD(), 0) for @non_std;
56                 }
57                 my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
58                 @ENV{keys %env} = values %env;
59                 umask 077;
60                 exec(@argv);
61                 warn "exec: @argv: $!\n";
62                 POSIX::_exit(1);
63         };
64         $SIG{CHLD} = $sigchld;
65         my $pid = fork // die "fork: $!";
66         if ($pid == 0) {
67                 $do_exec->() if $fds->[1]; # git-credential, pager
68
69                 # parent backgrounds on MUA
70                 POSIX::setsid() > 0 or die "setsid: $!";
71                 @parent = ($parent);
72                 return; # continue $recv_cmd in background
73         }
74         if ($fds->[1]) {
75                 $pids{$pid} = undef;
76         } else {
77                 $do_exec->(); # MUA reuses stdout
78         }
79 };
80
81 my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
82 if ($runtime_dir eq '/lei') {
83         require File::Spec;
84         $runtime_dir = File::Spec->tmpdir."/lei-$<";
85 }
86 unless (-d $runtime_dir) {
87         require File::Path;
88         File::Path::make_path($runtime_dir, { mode => 0700 });
89 }
90 my $path = "$runtime_dir/$narg.seq.sock";
91 my $addr = pack_sockaddr_un($path);
92 socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
93 unless (connect($sock, $addr)) { # start the daemon if not started
94         local $ENV{PERL5LIB} = join(':', @INC);
95         open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
96                 -E PublicInbox::LEI::lazy_start(@ARGV)],
97                 $path, $! + 0, $narg) or die "popen: $!";
98         while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
99         close($daemon) or warn <<"";
100 lei-daemon could not start, exited with \$?=$?
101
102         # try connecting again anyways, unlink+bind may be racy
103         connect($sock, $addr) or die <<"";
104 connect($path): $! (after attempted daemon start)
105
106 }
107 # (Socket::MsgHdr|Inline::C), $sock are all available:
108 open my $dh, '<', '.' or die "open(.) $!";
109 my $buf = join("\0", scalar(@ARGV), @ARGV);
110 while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
111 $buf .= "\0\0";
112 $send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR) or die "sendmsg: $!";
113 $SIG{TSTP} = sub { send($sock, 'STOP', MSG_EOR); kill 'STOP', $$ };
114 $SIG{CONT} = sub { send($sock, 'CONT', MSG_EOR) };
115
116 my $x_it_code = 0;
117 while (1) {
118         my (@fds) = $recv_cmd->($sock, my $buf, 4096 * 33);
119         if (scalar(@fds) == 1 && !defined($fds[0])) {
120                 next if $!{EINTR};
121                 die "recvmsg: $!";
122         }
123         last if $buf eq '';
124         if ($buf =~ /\Aexec (.+)\z/) {
125                 $exec_cmd->(\@fds, split(/\0/, $1));
126         } elsif ($buf eq '-WINCH') {
127                 kill($buf, @parent); # for MUA
128         } elsif ($buf eq 'umask') {
129                 send($sock, 'u'.pack('V', umask), MSG_EOR) or die "send: $!"
130         } elsif ($buf =~ /\Ax_it ([0-9]+)\z/) {
131                 $x_it_code ||= $1 + 0;
132                 last;
133         } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
134                 $x_it_code ||= $1 + 0;
135         } elsif ($buf eq 'wait') {
136                 $sigchld->();
137         } else {
138                 $sigchld->();
139                 die $buf;
140         }
141 }
142 $sigchld->();
143 if (my $sig = ($x_it_code & 127)) {
144         kill $sig, $$;
145         sleep(1) while 1; # no self-pipe/signalfd, here, so we loop
146 }
147 exit($x_it_code >> 8);