From 77ad8a38e589a51cc8204a7c71c7e734db7220d5 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 13 Dec 2023 00:50:09 +0000 Subject: tests: attempt compatibility w/ busybox lsof 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. --- lib/PublicInbox/TestCommon.pm | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'lib/PublicInbox/TestCommon.pm') 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); } } -- cgit v1.2.3-24-ge0c7