use File::Path qw(mkpath);
use File::Spec;
our $quit = \&CORE::exit;
+my $recv_3fds;
my $GLP = Getopt::Long::Parser->new;
$GLP->configure(qw(gnu_getopt no_ignore_case auto_abbrev));
my $GLP_PASS = Getopt::Long::Parser->new;
sub x_it ($$) { # pronounced "exit"
my ($self, $code) = @_;
+ $self->{1}->autoflush(1); # make sure client sees stdout before exit
if (my $sig = ($code & 127)) {
kill($sig, $self->{pid} // $$);
} else {
my $self = bless { sock => $sock }, __PACKAGE__;
vec(my $rin = '', fileno($sock), 1) = 1;
# `say $sock' triggers "die" in lei(1)
- for my $i (0..2) {
- if (select(my $rout = $rin, undef, undef, 1)) {
- my $fd = IO::FDPass::recv(fileno($sock));
- if ($fd >= 0) {
- my $rdr = ($fd == 0 ? '<&=' : '>&=');
+ if (select(my $rout = $rin, undef, undef, 1)) {
+ my @fds = $recv_3fds->(fileno($sock));
+ if (scalar(@fds) == 3) {
+ my $i = 0;
+ for my $rdr (qw(<&= >&= >&=)) {
+ my $fd = shift(@fds);
if (open(my $fh, $rdr, $fd)) {
- $self->{$i} = $fh;
- } else {
+ $self->{$i++} = $fh;
+ } else {
say $sock "open($rdr$fd) (FD=$i): $!";
return;
}
- } else {
- say $sock "recv FD=$i: $!";
- return;
}
} else {
- say $sock "timed out waiting to recv FD=$i";
+ say $sock "recv_3fds failed: $!";
return;
}
+ } else {
+ say $sock "timed out waiting to recv FDs";
+ return;
}
+ $self->{2}->autoflush(1); # keep stdout buffered until x_it|DESTROY
# $ARGV_STR = join("]\0[", @ARGV);
# $ENV_STR = join('', map { "$_=$ENV{$_}\0" } keys %ENV);
# $line = "$$\0\0>$ARGV_STR\0\0>$ENV_STR\0\0";
# lei(1) calls this when it can't connect
sub lazy_start {
- my ($path, $errno) = @_;
+ my ($path, $errno, $nfd) = @_;
if ($errno == ECONNREFUSED) {
unlink($path) or die "unlink($path): $!";
} elsif ($errno != ENOENT) {
my $dev_ino_expect = pack('dd', $st[0], $st[1]); # dev+ino
pipe(my ($eof_r, $eof_w)) or die "pipe: $!";
my $oldset = PublicInbox::Sigfd::block_signals();
- require IO::FDPass;
+ if ($nfd == 1) {
+ require IO::FDPass;
+ $recv_3fds = sub { map { IO::FDPass::recv($_[0]) } (0..2) };
+ } elsif ($nfd == 3) {
+ $recv_3fds = PublicInbox::Spawn->can('recv_3fds');
+ }
+ $recv_3fds or die
+ "IO::FDPass missing or Inline::C not installed/configured\n";
require PublicInbox::Listener;
require PublicInbox::EOFpipe;
(-p STDOUT) or die "E: stdout must be a pipe\n";
}, __PACKAGE__), @ARGV);
}
+# ensures stdout hits the FS before sock disconnects so a client
+# can immediately reread it
+sub DESTROY { $_[0]->{1}->autoflush(1) }
+
1;