- local $ENV{PWD} = $pwd;
- my $buf = "$$\0\0>" . join("]\0[", @ARGV) . "\0\0>";
- while (my ($k, $v) = each %ENV) { $buf .= "$k=$v\0" }
- $buf .= "\0\0";
- select $sock;
- $| = 1; # unbuffer selected $sock
- IO::FDPass::send(fileno($sock), $_) for (0..2);
- print $sock $buf or die "print(sock, buf): $!";
- while ($buf = <$sock>) {
- $buf =~ /\Aexit=([0-9]+)\n\z/ and exit($1 + 0);
+};
+
+my $runtime_dir = ($ENV{XDG_RUNTIME_DIR} // '') . '/lei';
+if ($runtime_dir eq '/lei') {
+ require File::Spec;
+ $runtime_dir = File::Spec->tmpdir."/lei-$<";
+}
+unless (-d $runtime_dir) {
+ require File::Path;
+ File::Path::make_path($runtime_dir, { mode => 0700 });
+}
+my $path = "$runtime_dir/$narg.seq.sock";
+my $addr = pack_sockaddr_un($path);
+socket($sock, AF_UNIX, SOCK_SEQPACKET, 0) or die "socket: $!";
+unless (connect($sock, $addr)) { # start the daemon if not started
+ local $ENV{PERL5LIB} = join(':', @INC);
+ open(my $daemon, '-|', $^X, qw[-MPublicInbox::LEI
+ -E PublicInbox::LEI::lazy_start(@ARGV)],
+ $path, $! + 0, $narg) or die "popen: $!";
+ while (<$daemon>) { warn $_ } # EOF when STDERR is redirected
+ close($daemon) or warn <<"";
+lei-daemon could not start, exited with \$?=$?
+
+ # try connecting again anyways, unlink+bind may be racy
+ connect($sock, $addr) or die <<"";
+connect($path): $! (after attempted daemon start)
+
+}
+# (Socket::MsgHdr|Inline::C), $sock are all available:
+open my $dh, '<', '.' or die "open(.) $!";
+my $buf = join("\0", scalar(@ARGV), @ARGV);
+while (my ($k, $v) = each %ENV) { $buf .= "\0$k=$v" }
+$buf .= "\0\0";
+$send_cmd->($sock, [0, 1, 2, fileno($dh)], $buf, MSG_EOR) or die "sendmsg: $!";
+$SIG{TSTP} = sub { send($sock, 'STOP', MSG_EOR); kill 'STOP', $$ };
+$SIG{CONT} = sub { send($sock, 'CONT', MSG_EOR) };
+
+my $x_it_code = 0;
+while (1) {
+ my (@fds) = $recv_cmd->($sock, my $buf, 4096 * 33);
+ if (scalar(@fds) == 1 && !defined($fds[0])) {
+ next if $!{EINTR};
+ die "recvmsg: $!";
+ }
+ last if $buf eq '';
+ if ($buf =~ /\Aexec (.+)\z/) {
+ $exec_cmd->(\@fds, split(/\0/, $1));
+ } elsif ($buf eq '-WINCH') {
+ kill($buf, @parent); # for MUA
+ } elsif ($buf eq 'umask') {
+ send($sock, 'u'.pack('V', umask), MSG_EOR) or die "send: $!"
+ } elsif ($buf =~ /\Ax_it ([0-9]+)\z/) {
+ $x_it_code ||= $1 + 0;
+ last;
+ } elsif ($buf =~ /\Achild_error ([0-9]+)\z/) {
+ $x_it_code ||= $1 + 0;
+ } elsif ($buf eq 'wait') {
+ $sigchld->();
+ } else {
+ $sigchld->();