1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Backend for `lei' (local email interface). Unlike the C10K-oriented
5 # PublicInbox::Daemon, this is designed exclusively to handle trusted
6 # local clients with read/write access to the FS and use as many
7 # system resources as the local user has access to.
8 package PublicInbox::LeiDaemon;
11 use parent qw(PublicInbox::DS);
13 use Errno qw(EAGAIN ECONNREFUSED ENOENT);
17 use Sys::Syslog qw(syslog openlog);
18 use PublicInbox::Syscall qw($SFD_NONBLOCK EPOLLIN EPOLLONESHOT);
19 use PublicInbox::Sigfd;
20 use PublicInbox::DS qw(now);
21 use PublicInbox::Spawn qw(spawn);
22 our $quit = sub { exit(shift // 0) };
23 my $glp = Getopt::Long::Parser->new;
24 $glp->configure(qw(gnu_getopt no_ignore_case auto_abbrev));
26 sub x_it ($$) { # pronounced "exit"
27 my ($client, $code) = @_;
28 if (my $sig = ($code & 127)) {
29 kill($sig, $client->{pid} // $$);
32 if (my $sock = $client->{sock}) {
33 say $sock "exit=$code";
34 } else { # for oneshot
41 my ($client, $channel, $buf) = @_;
42 print { $client->{$channel} } $buf or warn "print FD[$channel]: $!";
46 my ($client, $buf, $exit_code) = @_;
47 $buf .= "\n" unless $buf =~ /\n\z/s;
48 emit($client, 2, $buf);
49 x_it($client, ($exit_code // 1) << 8);
54 my ($client, $channel) = @_;
55 emit($client, $channel //= 1, <<EOF);
56 usage: lei COMMAND [OPTIONS]
60 x_it($client, $channel == 2 ? 1 << 8 : 0); # stderr => failure
63 sub assert_args ($$$;$@) {
64 my ($client, $argv, $proto, $opt, @spec) = @_;
66 push @spec, qw(help|h);
67 $glp->getoptionsfromarray($argv, $opt, @spec) or
68 return fail($client, 'bad arguments or options');
73 my ($nreq, $rest) = split(/;/, $proto);
74 $nreq = (($nreq // '') =~ tr/$/$/);
75 my $argc = scalar(@$argv);
76 my $tot = ($rest // '') eq '@' ? $argc : ($proto =~ tr/$/$/);
77 return 1 if $argc <= $tot && $argc >= $nreq;
84 my ($client, $cmd, @argv) = @_;
85 local $SIG{__WARN__} = sub { emit($client, 2, "@_") };
86 local $SIG{__DIE__} = 'DEFAULT';
88 my $func = "lei_$cmd";
90 if (my $cb = __PACKAGE__->can($func)) {
91 $client->{cmd} = $cmd;
92 $cb->($client, \@argv);
93 } elsif (grep(/\A-/, $cmd, @argv)) {
94 assert_args($client, [ $cmd, @argv ], '');
96 fail($client, "`$cmd' is not an lei command");
104 my ($client, $argv) = @_;
105 assert_args($client, $argv, '') and emit($client, 1, "$$\n");
109 my ($client, $argv) = @_;
110 assert_args($client, $argv, '') and
111 emit($client, 1, "$client->{env}->{PWD}\n");
115 my ($client, $argv) = @_;
117 assert_args($client, $argv, '') and emit($client, 1, Cwd::cwd()."\n");
120 sub lei_DBG_false { x_it($_[0], 1 << 8) }
122 sub lei_daemon_stop {
123 my ($client, $argv) = @_;
124 assert_args($client, $argv, '') and $quit->(0);
127 sub lei_help { _help($_[0]) }
129 sub reap_exec { # dwaitpid callback
130 my ($client, $pid) = @_;
134 sub lei_git { # support passing through random git commands
135 my ($client, $argv) = @_;
136 my %opt = map { $_ => $client->{$_} } (0..2);
137 my $pid = spawn(['git', @$argv], $client->{env}, \%opt);
138 PublicInbox::DS::dwaitpid($pid, \&reap_exec, $client);
141 sub accept_dispatch { # Listener {post_accept} callback
142 my ($sock) = @_; # ignore other
145 my $client = { sock => $sock };
146 vec(my $rin = '', fileno($sock), 1) = 1;
147 # `say $sock' triggers "die" in lei(1)
149 if (select(my $rout = $rin, undef, undef, 1)) {
150 my $fd = IO::FDPass::recv(fileno($sock));
152 my $rdr = ($fd == 0 ? '<&=' : '>&=');
153 if (open(my $fh, $rdr, $fd)) {
156 say $sock "open($rdr$fd) (FD=$i): $!";
160 say $sock "recv FD=$i: $!";
164 say $sock "timed out waiting to recv FD=$i";
168 # $ARGV_STR = join("]\0[", @ARGV);
169 # $ENV_STR = join('', map { "$_=$ENV{$_}\0" } keys %ENV);
170 # $line = "$$\0\0>$ARGV_STR\0\0>$ENV_STR\0\0";
171 my ($client_pid, $argv, $env) = do {
172 local $/ = "\0\0\0"; # yes, 3 NULs at EOL, not 2
173 chomp(my $line = <$sock>);
174 split(/\0\0>/, $line, 3);
176 my %env = map { split(/=/, $_, 2) } split(/\0/, $env);
177 if (chdir($env{PWD})) {
178 $client->{env} = \%env;
179 $client->{pid} = $client_pid;
180 eval { dispatch($client, split(/\]\0\[/, $argv)) };
183 say $sock "chdir($env{PWD}): $!"; # implicit close
189 # lei(1) calls this when it can't connect
190 sub lazy_start ($$) {
191 my ($path, $err) = @_;
192 if ($err == ECONNREFUSED) {
193 unlink($path) or die "unlink($path): $!";
194 } elsif ($err != ENOENT) {
195 die "connect($path): $!";
197 my $umask = umask(077) // die("umask(077): $!");
198 my $l = IO::Socket::UNIX->new(Local => $path,
200 Type => SOCK_STREAM) or
202 umask($umask) or die("umask(restore): $!");
204 my @st = stat($path) or die "stat($path): $!";
205 my $dev_ino_expect = pack('dd', $st[0], $st[1]); # dev+ino
206 pipe(my ($eof_r, $eof_w)) or die "pipe: $!";
207 my $oldset = PublicInbox::Sigfd::block_signals();
208 my $pid = fork // die "fork: $!";
210 PublicInbox::Sigfd::sig_setmask($oldset);
211 return; # client will connect to $path
213 openlog($path, 'pid', 'user');
214 local $SIG{__DIE__} = sub {
215 syslog('crit', "@_");
217 exit $? >> 8 if $? >> 8;
220 local $SIG{__WARN__} = sub { syslog('warning', "@_") };
221 open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!\n";
222 open STDOUT, '>&STDIN' or die "redirect stdout failed: $!\n";
223 open STDERR, '>&STDIN' or die "redirect stderr failed: $!\n";
225 $pid = fork // die "fork: $!";
227 $0 = "lei-daemon $path";
228 require PublicInbox::Listener;
229 require PublicInbox::EOFpipe;
233 my $listener = PublicInbox::Listener->new($l, \&accept_dispatch, $l);
236 $exit_code //= shift;
237 my $tmp = $listener or exit($exit_code);
238 unlink($path) if defined($path);
239 syswrite($eof_w, '.');
240 $l = $listener = $path = undef;
241 $tmp->close if $tmp; # DS::close
242 PublicInbox::DS->SetLoopTimeout(1000);
244 PublicInbox::EOFpipe->new($eof_r, sub {}, undef);
246 CHLD => \&PublicInbox::DS::enqueue_reap,
254 my $sigfd = PublicInbox::Sigfd->new($sig, $SFD_NONBLOCK);
255 local %SIG = (%SIG, %$sig) if !$sigfd;
256 if ($sigfd) { # TODO: use inotify/kqueue to detect unlinked sockets
257 PublicInbox::DS->SetLoopTimeout(5000);
259 # wake up every second to accept signals if we don't
260 # have signalfd or IO::KQueue:
261 PublicInbox::Sigfd::sig_setmask($oldset);
262 PublicInbox::DS->SetLoopTimeout(1000);
264 PublicInbox::DS->SetPostLoopCallback(sub {
265 my ($dmap, undef) = @_;
266 if (@st = defined($path) ? stat($path) : ()) {
267 if ($dev_ino_expect ne pack('dd', $st[0], $st[1])) {
268 warn "$path dev/ino changed, quitting\n";
271 } elsif (defined($path)) {
272 warn "stat($path): $!, quitting ...\n";
273 undef $path; # don't unlink
276 return 1 if defined($path);
279 for my $s (values %$dmap) {
280 $s->can('busy') or next;
281 if ($s->busy($now)) {
287 $n; # true: continue, false: stop
289 PublicInbox::DS->EventLoop;
290 exit($exit_code // 0);
293 # for users w/o IO::FDPass