From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id A553D1F55F for ; Mon, 11 Sep 2023 03:29:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1694402999; bh=34GsuMtKhjW1a3fjHx5ujD699vM15hyZXcfmDIZSohQ=; h=From:To:Subject:Date:From; b=JFQIp61yxuZxPv3hjngeqUvTXcJ48n7EA+bYr+xykYQ8U2F9yfc1yg2TcwUVtwLdu gHT7LI5+bu7Zb5ucV/ptQHDlHWI6v01TE7o57jXCtV32kfVbbBoT+B92r52k4EFuHY O2zS2ovRJECq9kCGUxHd28asrYb0UvonrFCBzz88= From: Eric Wong To: spew@80x24.org Subject: [PATCH] tests: map CLOFORK->FD_CLOEXEC temporarily for `tail -f' Date: Mon, 11 Sep 2023 03:29:59 +0000 Message-ID: <20230911032959.1264058-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 b2774f58..157b1f5a 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;