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 1/7] tests: map CLOFORK->FD_CLOEXEC temporarily for `tail -f'
Date: Mon, 11 Sep 2023 09:41:26 +0000	[thread overview]
Message-ID: <20230911094132.75792-2-e@80x24.org> (raw)
In-Reply-To: <20230911094132.75792-1-e@80x24.org>

This fixes `TAIL="tail -F" prove -bvw t/lei-refresh-mail-sync.t'
since that test relies on lacking FD_CLOEXEC to detect dead
lei-daemons, but we still want FD_CLOEXEC when when relying
on tail(1) to check -imapd output.
---
 lib/PublicInbox/TestCommon.pm | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index ec300b3f..b7f1eb57 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -6,7 +6,7 @@ package PublicInbox::TestCommon;
 use strict;
 use parent qw(Exporter);
 use v5.10.1;
-use Fcntl qw(F_SETFD :seek);
+use Fcntl qw(F_SETFD F_GETFD FD_CLOEXEC :seek);
 use POSIX qw(dup2);
 use IO::Socket::INET;
 use File::Spec;
@@ -326,7 +326,7 @@ sub run_script ($;$$) {
 			die "unable to deal with $ref $redir";
 		}
 	}
-	my $tail = @tail_paths ? tail_f(@tail_paths) : undef;
+	my $tail = @tail_paths ? tail_f(@tail_paths, $opt) : undef;
 	if ($key =~ /-(index|cindex|extindex|convert|xcpdb)\z/) {
 		unshift @argv, '--no-fsync';
 	}
@@ -442,11 +442,23 @@ sub xqx {
 }
 
 sub tail_f (@) {
+	my @f = grep(defined, @_);
 	$tail_cmd or return; # "tail -F" or "tail -f"
-	for (@_) { open(my $fh, '>>', $_) or die $! };
-	my $cmd = [ split(/ /, $tail_cmd), @_ ];
+	my $opt = (ref($f[-1]) eq 'HASH') ? pop(@f) : {};
+	my $clofork = $opt->{-CLOFORK} // [];
+	use autodie qw(fcntl open);
+	my @cfmap = map {
+		my $fl = fcntl($_, F_GETFD, 0);
+		fcntl($_, F_SETFD, $fl | FD_CLOEXEC) unless $fl & FD_CLOEXEC;
+		($_, $fl);
+	} @$clofork;
+	for (@f) { open(my $fh, '>>', $_) };
+	my $cmd = [ split(/ /, $tail_cmd), @f ];
 	require PublicInbox::Spawn;
 	my $pid = PublicInbox::Spawn::spawn($cmd, undef, { 1 => 2 });
+	while (my ($io, $fl) = splice(@cfmap, 0, 2)) {
+		fcntl($io, F_SETFD, $fl);
+	}
 	wait_for_tail($pid, scalar @_);
 	require PublicInbox::AutoReap;
 	PublicInbox::AutoReap->new($pid, \&wait_for_tail);
@@ -476,7 +488,7 @@ sub start_script {
 				}
 			}
 		}
-		$tail = tail_f(@paths);
+		$tail = tail_f(@paths, $opt);
 	}
 	my $oset = PublicInbox::DS::block_signals();
 	require PublicInbox::OnDestroy;

  reply	other threads:[~2023-09-11  9:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-11  9:41 [PATCH 0/7] system-related updates and cleanups Eric Wong
2023-09-11  9:41 ` Eric Wong [this message]
2023-09-11  9:41 ` [PATCH 2/7] daemon: depend on DS event_loop in master process, too Eric Wong
2023-09-11  9:41 ` [PATCH 3/7] ds: use object-oriented API for epoll Eric Wong
2023-09-11  9:41 ` [PATCH 4/7] favor poll(2) for most daemons Eric Wong
2023-09-11  9:41 ` [PATCH 5/7] dspoll: switch to the documented IO::Poll API Eric Wong
2023-09-12  2:34   ` Eric Wong
2023-09-12  6:13     ` [PATCH 8/7] provide select(2) backend for PublicInbox::DS Eric Wong
2023-09-11  9:41 ` [PATCH 6/7] ds: use constants for @UNBLOCKABLE list Eric Wong
2023-09-11  9:41 ` [PATCH 7/7] spawn: do not block ABRT/BUS/ILL/SEGV signals 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=20230911094132.75792-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).