user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/9] mbox_lock: dotlock: chdir for relative lock paths
Date: Wed, 24 Mar 2021 14:23:28 +0500	[thread overview]
Message-ID: <20210324092335.12345-3-e@80x24.org> (raw)
In-Reply-To: <20210324092335.12345-1-e@80x24.org>

Since lei-daemon will fchdir on every request, we must ensure
we're in the correct directory before unlink(2) is called,
since we can't use unlinkat(2) from pure Perl.
---
 lib/PublicInbox/MboxLock.pm | 14 ++++++++++++++
 t/mbox_lock.t               | 12 ++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/lib/PublicInbox/MboxLock.pm b/lib/PublicInbox/MboxLock.pm
index bea0e325..856b1e21 100644
--- a/lib/PublicInbox/MboxLock.pm
+++ b/lib/PublicInbox/MboxLock.pm
@@ -60,6 +60,11 @@ sub acq_dotlock {
 			if (link($tmp, $dot_lock)) {
 				unlink($tmp) or die "unlink($tmp): $!";
 				$self->{".lock$pid"} = $dot_lock;
+				if (substr($dot_lock, 0, 1) ne '/') {
+					opendir(my $dh, '.') or
+							die "opendir . $!";
+					$self->{dh} = $dh;
+				}
 				return;
 			}
 			unlink($tmp) or die "unlink($tmp): $!";
@@ -111,10 +116,19 @@ sub acq {
 	$self;
 }
 
+sub _fchdir { chdir($_[0]) } # OnDestroy callback
+
 sub DESTROY {
 	my ($self) = @_;
 	if (my $f = $self->{".lock$$"}) {
+		my $x;
+		if (my $dh = delete $self->{dh}) {
+			opendir my $c, '.' or die "opendir . $!";
+			$x = PublicInbox::OnDestroy->new(\&_fchdir, $c);
+			chdir($dh) or die "chdir (for $f): $!";
+		}
 		unlink($f) or die "unlink($f): $! (lock stolen?)";
+		undef $x;
 	}
 }
 
diff --git a/t/mbox_lock.t b/t/mbox_lock.t
index 3dc3b449..c2fee0d4 100644
--- a/t/mbox_lock.t
+++ b/t/mbox_lock.t
@@ -5,6 +5,7 @@ use strict; use v5.10.1; use PublicInbox::TestCommon;
 use POSIX qw(_exit);
 use PublicInbox::DS qw(now);
 use Errno qw(EAGAIN);
+use PublicInbox::OnDestroy;
 use_ok 'PublicInbox::MboxLock';
 my ($tmpdir, $for_destroy) = tmpdir();
 my $f = "$tmpdir/f";
@@ -15,6 +16,17 @@ ok(!-f "$f.lock", 'dotlock gone');
 $mbl = PublicInbox::MboxLock->acq($f, 1, ['none']);
 ok(!-f "$f.lock", 'no dotlock with none');
 undef $mbl;
+{
+	opendir my $cur, '.' or BAIL_OUT $!;
+	my $od = PublicInbox::OnDestroy->new(sub { chdir $cur });
+	chdir $tmpdir or BAIL_OUT;
+	my $abs = "$tmpdir/rel.lock";
+	my $rel = PublicInbox::MboxLock->acq('rel', 1, ['dotlock']);
+	chdir '/' or BAIL_OUT;
+	ok(-f $abs, 'lock with abs path created');
+	undef $rel;
+	ok(!-f $abs, 'lock gone despite being in the wrong dir');
+}
 
 eval {
 	PublicInbox::MboxLock->acq($f, 1, ['bogus']);

  parent reply	other threads:[~2021-03-24  9:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-24  9:23 [PATCH 0/9] lei: various corner case leak fixes Eric Wong
2021-03-24  9:23 ` [PATCH 1/9] ds: improve DS->Reset fork-safety Eric Wong
2021-03-24 23:01   ` [SQUASH] " Eric Wong
2021-03-24  9:23 ` Eric Wong [this message]
2021-03-24  9:23 ` [PATCH 3/9] lei: drop circular reference in lei_store process Eric Wong
2021-03-24  9:23 ` [PATCH 4/9] lei: update {3} after -C chdirs Eric Wong
2021-03-24  9:23 ` [PATCH 5/9] lei: clean up pkt_op consumer on exception, too Eric Wong
2021-03-24  9:23 ` [PATCH 6/9] lei_store: give process a better name Eric Wong
2021-03-24  9:23 ` [PATCH 7/9] v2writable: cleanup SQLite handles on --xapian-only Eric Wong
2021-03-24  9:23 ` [PATCH 8/9] lei_mirror: fix circular reference Eric Wong
2021-03-24  9:23 ` [PATCH 9/9] lei-daemon: do not leak FDs on bogus requests Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210324092335.12345-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH 2/9] mbox_lock: dotlock: chdir for relative lock paths' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).