about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-05-28 09:45:20 +0000
committerEric Wong <e@80x24.org>2021-05-28 18:28:05 +0000
commit74b5dc0569db5adb4cc931ce3c3b1b50507eb31d (patch)
tree517e3e6e920e73dcdd942e3df34bf958bc104d83
parent6778614fc11224b6b472a3830543c0f6635193f8 (diff)
downloadpublic-inbox-74b5dc0569db5adb4cc931ce3c3b1b50507eb31d.tar.gz
Every tick of the event loop can change the working directory,
so we need to restore it for every client if they operate
in different directories.

This would be easier if we had openat(2) and friends in Perl;
but Inline::C is practically required for lei, now.
-rw-r--r--lib/PublicInbox/LEI.pm6
-rw-r--r--lib/PublicInbox/LeiLcat.pm4
-rw-r--r--lib/PublicInbox/LeiQuery.pm4
-rw-r--r--lib/PublicInbox/LeiXSearch.pm1
4 files changed, 9 insertions, 6 deletions
diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 07378ca7..e5ff9e5d 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -1239,6 +1239,12 @@ sub wq_done_wait { # dwaitpid callback
         $lei->dclose;
 }
 
+sub fchdir {
+        my ($lei) = @_;
+        my $dh = $lei->{3} // die 'BUG: lei->{3} (CWD) gone';
+        chdir($dh) || $lei->fail("fchdir: $!");
+}
+
 sub wq_eof { # EOF callback for main daemon
         my ($lei) = @_;
         my $wq1 = delete $lei->{wq1} // return $lei->fail; # already failed
diff --git a/lib/PublicInbox/LeiLcat.pm b/lib/PublicInbox/LeiLcat.pm
index 0f585ff5..f9d9633a 100644
--- a/lib/PublicInbox/LeiLcat.pm
+++ b/lib/PublicInbox/LeiLcat.pm
@@ -89,9 +89,7 @@ sub _stdin { # PublicInbox::InputPipe::consume callback for --stdin
         my ($lei) = @_; # $_[1] = $rbuf
         if (defined($_[1])) {
                 $_[1] eq '' and return eval {
-                        if (my $dfd = $lei->{3}) {
-                                chdir($dfd) or return $lei->fail("fchdir: $!");
-                        }
+                        $lei->fchdir or return;
                         my @argv = split(/\s+/, $lei->{mset_opt}->{qstr});
                         $lei->{mset_opt}->{qstr} = extract_all($lei, @argv)
                                 or return;
diff --git a/lib/PublicInbox/LeiQuery.pm b/lib/PublicInbox/LeiQuery.pm
index 1999a534..0435a516 100644
--- a/lib/PublicInbox/LeiQuery.pm
+++ b/lib/PublicInbox/LeiQuery.pm
@@ -51,9 +51,7 @@ sub qstr_add { # PublicInbox::InputPipe::consume callback for --stdin
         my ($self) = @_; # $_[1] = $rbuf
         if (defined($_[1])) {
                 $_[1] eq '' and return eval {
-                        if (my $dfd = $self->{3}) {
-                                chdir($dfd) or return $self->fail("fchdir: $!");
-                        }
+                        $self->fchdir or return;
                         $self->{mset_opt}->{q_raw} = $self->{mset_opt}->{qstr};
                         $self->{lse}->query_approxidate($self->{lse}->git,
                                                 $self->{mset_opt}->{qstr});
diff --git a/lib/PublicInbox/LeiXSearch.pm b/lib/PublicInbox/LeiXSearch.pm
index e2a8e8e3..760f9718 100644
--- a/lib/PublicInbox/LeiXSearch.pm
+++ b/lib/PublicInbox/LeiXSearch.pm
@@ -394,6 +394,7 @@ Error closing $lei->{ovv}->{dst}: $!
 sub do_post_augment {
         my ($lei) = @_;
         my $l2m = $lei->{l2m} or return; # client disconnected
+        $lei->fchdir or return;
         my $err;
         eval { $l2m->post_augment($lei) };
         $err = $@;