about summary refs log tree commit homepage
path: root/lib/PublicInbox/TestCommon.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-12-13 00:50:09 +0000
committerEric Wong <e@80x24.org>2023-12-13 09:01:48 +0000
commit77ad8a38e589a51cc8204a7c71c7e734db7220d5 (patch)
treedc242814cb2872a9ae360fe7deb365edfd340ae1 /lib/PublicInbox/TestCommon.pm
parent0f833a25eb64607386bc93c9d7b004c84acb54e0 (diff)
downloadpublic-inbox-77ad8a38e589a51cc8204a7c71c7e734db7220d5.tar.gz
BusyBox lsof(1) ignores the `-p PID' argument and shows
the open files for every process it knows about.  BusyBox
lsof also lacks the `NODE' column of the non-BusyBox
implementation, so we'll rely on /proc/PID/fd/ in those
cases since the deleted file checks are Linux-only and
it's common to have procfs is mounted on /proc on Linux.
Diffstat (limited to 'lib/PublicInbox/TestCommon.pm')
-rw-r--r--lib/PublicInbox/TestCommon.pm23
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index 27a758e4..9c413f43 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -29,7 +29,7 @@ BEGIN {
                 tcp_host_port test_lei lei lei_ok $lei_out $lei_err $lei_opt
                 test_httpd xbail require_cmd is_xdeeply tail_f
                 ignore_inline_c_missing no_pollerfd no_coredump cfg_new
-                strace strace_inject);
+                strace strace_inject lsof_pid);
         require Test::More;
         my @methods = grep(!/\W/, @Test::More::EXPORT);
         eval(join('', map { "*$_=\\&Test::More::$_;" } @methods));
@@ -951,6 +951,26 @@ sub test_httpd ($$;$$) {
         }
 };
 
+# TODO: support fstat(1) on OpenBSD, lsof already works on FreeBSD + Linux
+# don't use this for deleted file checks, we only check that on Linux atm
+# and we can readlink /proc/PID/fd/* directly
+sub lsof_pid ($;$) {
+        my ($pid, $rdr) = @_;
+        state $lsof = require_cmd('lsof', 1);
+        $lsof or skip 'lsof missing/broken', 1;
+        my @out = xqx([$lsof, '-p', $pid], undef, $rdr);
+        if ($?) {
+                undef $lsof;
+                skip "lsof -p PID broken \$?=$?", 1;
+        }
+        my @cols = split ' ', $out[0];
+        if (($cols[7] // '') eq 'NODE') { # normal lsof
+                @out;
+        } else { # busybox lsof ignores -p, so we DIY it
+                grep /\b$pid\b/, @out;
+        }
+}
+
 sub no_pollerfd ($) {
         my ($pid) = @_;
         my ($re, @cmd);
@@ -966,6 +986,7 @@ sub no_pollerfd ($) {
                 my @of = xqx(\@cmd, {}, {2 => \(my $e)});
                 my $err = $?;
                 skip "$bin broken? (\$?=$err) ($e)", 1 if $err;
+                @of = grep /\b$pid\b/, @of; # busybox lsof ignores -p
                 is(grep(/$re/, @of), 0, "no $re FDs") or diag explain(\@of);
         }
 }