]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: pass FD to CWD via cmsg, use fchdir on server
authorEric Wong <e@80x24.org>
Thu, 14 Jan 2021 07:06:27 +0000 (19:06 -1200)
committerEric Wong <e@80x24.org>
Fri, 15 Jan 2021 00:19:12 +0000 (00:19 +0000)
Perl chdir() automatically does fchdir(2) if given a file
or directory handle since 5.8.8/5.10.0, so we can safely
rely on it given our 5.10.1+ requirement.

This means we no longer have to waste several milliseconds
loading the Cwd.so and making stat() calls to ensure
ENV{PWD} is correct and usable in the server.  It also lets
us work in directories that are no longer accessible via
pathname.

lib/PublicInbox/LEI.pm
script/lei
t/lei.t

index 9786e7ac27effa3c0fbe2bac2e5efe9a33a7dc88..1f4a30829a917d734c8ca2cb46abcba2bfa3073b 100644 (file)
@@ -674,9 +674,9 @@ sub accept_dispatch { # Listener {post_accept} callback
        select($rvec, undef, undef, 1) or
                return send($sock, 'timed out waiting to recv FDs', MSG_EOR);
        my @fds = $recv_cmd->($sock, my $buf, 4096 * 33); # >MAX_ARG_STRLEN
-       if (scalar(@fds) == 3) {
+       if (scalar(@fds) == 4) {
                my $i = 0;
-               for my $rdr (qw(<&= >&= >&=)) {
+               for my $rdr (qw(<&= >&= >&= <&=)) {
                        my $fd = shift(@fds);
                        open($self->{$i++}, $rdr, $fd) and next;
                        send($sock, "open($rdr$fd) (FD=$i): $!", MSG_EOR);
@@ -692,13 +692,13 @@ sub accept_dispatch { # Listener {post_accept} callback
        my ($argc, @argv) = split(/\0/, $buf, -1);
        undef $buf;
        my %env = map { split(/=/, $_, 2) } splice(@argv, $argc);
-       if (chdir($env{PWD})) {
+       if (chdir(delete($self->{3}))) {
                local %ENV = %env;
                $self->{env} = \%env;
                eval { dispatch($self, @argv) };
                send($sock, $@, MSG_EOR) if $@;
        } else {
-               send($sock, "chdir($env{PWD}): $!", MSG_EOR); # implicit close
+               send($sock, "fchdir: $!", MSG_EOR); # implicit close
        }
 }
 
@@ -746,7 +746,7 @@ our $oldset; sub oldset { $oldset }
 
 # lei(1) calls this when it can't connect
 sub lazy_start {
-       my ($path, $errno, $nfd) = @_;
+       my ($path, $errno, $narg) = @_;
        if ($errno == ECONNREFUSED) {
                unlink($path) or die "unlink($path): $!";
        } elsif ($errno != ENOENT) {
@@ -761,7 +761,7 @@ sub lazy_start {
        my $dev_ino_expect = pack('dd', $st[0], $st[1]); # dev+ino
        pipe(my ($eof_r, $eof_w)) or die "pipe: $!";
        local $oldset = PublicInbox::DS::block_signals();
-       if ($nfd == 4) {
+       if ($narg == 5) {
                $send_cmd = PublicInbox::Spawn->can('send_cmd4');
                $recv_cmd = PublicInbox::Spawn->can('recv_cmd4') // do {
                        require PublicInbox::CmdIPC4;
@@ -770,7 +770,7 @@ sub lazy_start {
                };
        }
        $recv_cmd or die <<"";
-(Socket::MsgHdr || Inline::C) missing/unconfigured (nfd=$nfd);
+(Socket::MsgHdr || Inline::C) missing/unconfigured (narg=$narg);
 
        require PublicInbox::Listener;
        require PublicInbox::EOFpipe;
index 9610a876b489e7b55726ad5d0f1ea253edec67f0..a4a0217bfa3237ca0f684a012bcbf98740abd7e1 100755 (executable)
@@ -6,7 +6,7 @@ use v5.10.1;
 use Socket qw(AF_UNIX SOCK_SEQPACKET MSG_EOR pack_sockaddr_un);
 use Errno qw(EINTR ECONNRESET);
 use PublicInbox::CmdIPC4;
-my $narg = 4;
+my $narg = 5;
 my ($sock, $pwd);
 my $recv_cmd = PublicInbox::CmdIPC4->can('recv_cmd4');
 my $send_cmd = PublicInbox::CmdIPC4->can('send_cmd4') // do {
@@ -74,25 +74,13 @@ connect($path): $! (after attempted daemon start)
 Falling back to (slow) one-shot mode
 
        }
-       require Cwd;
-       $pwd = $ENV{PWD} // '';
-       my $cwd = Cwd::fastcwd() // die "fastcwd(PWD=$pwd): $!";
-       if ($pwd ne $cwd) { # prefer ENV{PWD} if it's a symlink to real cwd
-               my @st_cwd = stat($cwd) or die "stat(cwd=$cwd): $!";
-               my @st_pwd = stat($pwd); # PWD invalid, use cwd
-               # make sure st_dev/st_ino match for {PWD} to be valid
-               $pwd = $cwd if (!@st_pwd || $st_pwd[1] != $st_cwd[1] ||
-                                       $st_pwd[0] != $st_cwd[0]);
-       } else {
-               $pwd = $cwd;
-       }
        1;
 }) { # (Socket::MsgHdr|Inline::C), $sock, $pwd are all available:
-       $ENV{PWD} = $pwd;
+       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 ], $buf, MSG_EOR);
+       $send_cmd->($sock, [ 0, 1, 2, fileno($dh) ], $buf, MSG_EOR);
        $SIG{TERM} = $SIG{INT} = $SIG{QUIT} = sub {
                my ($sig) = @_; # 'TERM', not an integer :<
                $SIG{$sig} = 'DEFAULT';
diff --git a/t/lei.t b/t/lei.t
index 240735bf7df8b72ff8c04b9e87165937d733c181..2349dca41e3dd304c426110c063b39c38b53d242 100644 (file)
--- a/t/lei.t
+++ b/t/lei.t
@@ -208,9 +208,9 @@ if ($ENV{TEST_LEI_ONESHOT}) {
 
 SKIP: { # real socket
        require_mods(qw(Cwd), my $nr = 105);
-       my $nfd = eval { require Socket::MsgHdr; 4 } // do {
+       my $nfd = eval { require Socket::MsgHdr; 5 } // do {
                require PublicInbox::Spawn;
-               PublicInbox::Spawn->can('send_cmd4') ? 4 : undef;
+               PublicInbox::Spawn->can('send_cmd4') ? 5 : undef;
        } //
        skip 'Socket::MsgHdr or Inline::C missing or unconfigured', $nr;
 
@@ -260,29 +260,6 @@ SKIP: { # real socket
                like($out, qr/^usage: /, 'help output works');
                chmod 0700, $sock or BAIL_OUT "chmod 0700: $!";
        }
-       if ('oneshot on cwd gone') {
-               my $cwd = Cwd::fastcwd() or BAIL_OUT "fastcwd: $!";
-               my $d = "$home/to-be-removed";
-               my $lei_path = 'lei';
-               # we chdir, so we need an abs_path fur run_script
-               if (($ENV{TEST_RUN_MODE}//2) != 2) {
-                       $lei_path = PublicInbox::TestCommon::key2script('lei');
-                       $lei_path = Cwd::abs_path($lei_path);
-               }
-               mkdir $d or BAIL_OUT "mkdir($d) $!";
-               chdir $d or BAIL_OUT "chdir($d) $!";
-               if (rmdir($d)) {
-                       $out = $err = '';
-                       ok(run_script([$lei_path, 'help'], undef, $opt),
-                               'cwd fail, one-shot fallback works');
-               } else {
-                       $err = "rmdir=$!";
-               }
-               chdir $cwd or BAIL_OUT "chdir($cwd) $!";
-               like($err, qr/cwd\(/, 'cwd error noted');
-               like($out, qr/^usage: /, 'help output still works');
-       }
-
        unlink $sock or BAIL_OUT "unlink($sock) $!";
        for (0..100) {
                kill('CHLD', $new_pid) or last;