about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-10-16 01:00:57 +0000
committerEric Wong <e@80x24.org>2021-10-16 01:42:51 +0000
commit13e60633b0a4fd31f224bf172c976e8d921ec609 (patch)
treeec9b816502f35427659ae0cc1aef558588b786b1
parentd63226bf4107f71b25c74abdcc5483d192c505fe (diff)
downloadpublic-inbox-13e60633b0a4fd31f224bf172c976e8d921ec609.tar.gz
lei: always keep cwd fd {3} for ->fchdir
The extra FD shouldn't cause noticeable overhead in short-lived
workers, and it lets us simplify lei->rel2abs.  Get rid of a
2-argument form of open() while we're at it, since it's been
considered for warning+deprecation by Perl for safety reasons.
-rw-r--r--lib/PublicInbox/LEI.pm14
1 files changed, 6 insertions, 8 deletions
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index e7f37efa..0cdcf449 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -77,19 +77,16 @@ sub rel2abs {
                 return $p;
         }
         my $pwd = $self->{env}->{PWD};
-        my $cwd;
         if (defined $pwd) {
-                my $xcwd = $self->{3} //
-                        ($cwd = getcwd() // die "getcwd(PWD=$pwd): $!");
                 if (my @st_pwd = stat($pwd)) {
-                        my @st_cwd = stat($xcwd) or die "stat($xcwd): $!";
+                        my @st_cwd = stat($self->{3}) or die "stat({3}): $!";
                         "@st_pwd[1,0]" eq "@st_cwd[1,0]" or
                                 $self->{env}->{PWD} = $pwd = undef;
                 } else { # PWD was invalid
                         $self->{env}->{PWD} = $pwd = undef;
                 }
         }
-        $pwd //= $self->{env}->{PWD} = $cwd // getcwd() // die "getcwd: $!";
+        $pwd //= $self->{env}->{PWD} = getcwd() // die "getcwd: $!";
         File::Spec->rel2abs($p, $pwd);
 }
 
@@ -558,7 +555,8 @@ sub _lei_atfork_child {
         my ($self, $persist) = @_;
         # we need to explicitly close things which are on stack
         if ($persist) {
-                chdir '/' or die "chdir(/): $!";
+                open $self->{3}, '<', '/' or die "open(/) $!";
+                fchdir($self) or die;
                 close($_) for (grep(defined, delete @$self{qw(0 1 2 sock)}));
                 if (my $cfg = $self->{cfg}) {
                         delete @$cfg{qw(-lei_store -watches -lei_note_event)};
@@ -568,7 +566,7 @@ sub _lei_atfork_child {
                 STDERR->autoflush(1);
                 POSIX::setpgid(0, $$) // die "setpgid(0, $$): $!";
         }
-        close($_) for (grep(defined, delete @$self{qw(3 old_1 au_done)}));
+        close($_) for (grep(defined, delete @$self{qw(old_1 au_done)}));
         delete $self->{-socks};
         if (my $op_c = delete $self->{pkt_op_c}) {
                 close(delete $op_c->{sock});
@@ -1190,7 +1188,7 @@ sub cfg2lei ($) {
         open($lei->{0}, '<&', \*STDIN) or die "dup 0: $!";
         open($lei->{1}, '>>&', \*STDOUT) or die "dup 1: $!";
         open($lei->{2}, '>>&', \*STDERR) or die "dup 2: $!";
-        open($lei->{3}, '/') or die "open /: $!";
+        open($lei->{3}, '<', '/') or die "open /: $!";
         my ($x, $y);
         socketpair($x, $y, AF_UNIX, SOCK_SEQPACKET, 0) or die "socketpair: $!";
         $lei->{sock} = $x;