Skip to content

ignore an empty entry in the -p and -g lists - #393

Open
sagaragas wants to merge 3 commits into
lsof-org:masterfrom
sagaragas:empty-id-entry
Open

sagaragas wants to merge 3 commits into
lsof-org:masterfrom
sagaragas:empty-id-entry

Conversation

@sagaragas

@sagaragas sagaragas commented Nov 14, 2026 •

Copy link
Copy Markdown

An empty entry in a -p or -g list gets entered as ID 0, and -p "" selects every process:

lsof -t -p ,1309331
before: 1309331, exit status 1 (-V adds "lsof: process ID not located: 0")
after:  1309331, exit status 0

lsof -t -p ""
before: the PIDs of all processes, exit status 0
after:  lsof: no process ID specified, exit status 1

lsof -t -p ^
before: the PIDs of all processes, exit status 0
after:  lsof: no process ID specified, exit status 1

enter_id() doesn't check that an entry has a digit, so ,123, 123,,456 and a lone ^ end up as PID or PGID 0. It's not new, 4.99.7 and Debian's 4.99.4 do the same. Empty entries are skipped now, so a list with a stray comma selects the IDs that are in it. -p still needs at least one ID: -p "", -p , and -p ^ are an error like -p with no argument, which keeps lsof -t -p "$pids" with an empty variable from printing every PID. That's per option, so -p 123 -p "", which used to work, is an error too.

-g with no ID left (-g "", -g ,, -g ^) only turns on the PGID display, which is what Lsof.8 says for -g without a PGID set.

Lsof.8 has a line about empty entries now. Added tests/case-20-id-empty-entry.bash, it fails before and passes after, and make check is clean on Linux.

An empty entry in a -p or -g list, like the first one in "-p ,123", or
a lone "^", was entered as ID 0. On Linux, "lsof -p ,123" listed
process 123 and exited with 1 because PID 0 was not located, and
"lsof -p ^" listed every process. An empty -p list entered no PID at
all, so "lsof -p ''" listed every process and exited with 0.

Report "empty process ID" or "empty process group ID" for an empty
entry, and "no process ID specified" for an empty -p list, like the
checks that enter_uid() has for -u.

"-g ''" keeps its meaning: Lsof.8 says that -g without a PGID set only
enables the PGID display. A trailing comma is still accepted; it never
entered an ID.
Comment thread tests/case-20-id-empty-entry.bash Outdated
Comment on lines +6 to +28
check() {
output=$($lsof "$1" "$2" -F p 2>/dev/null)
status=$?
if echo "$output" | grep -q '^p'; then
echo "lsof $1 '$2' listed a process"
exit 1
fi
if [ $status -eq 0 ]; then
echo "lsof $1 '$2' should fail"
exit 1
fi
}

{
pgid=$(ps -o pgid= -p $$ | tr -d ' ')
for spec in "" ",$$" "$$,,$$" "^"; do
check -p "$spec"
done
for spec in ",$pgid" "$pgid,,$pgid" "^"; do
check -g "$spec"
done
exit 0
} > $report 2>&1

@hja-oss hja-oss Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parser fix looks good. As a non-blocking improvement, could we check the expected diagnostic as well as exit status, and cover the valid inputs this PR preserves: empty -g, explicit zero, exclusions, and trailing commas?

Suggested update as follow:

Suggested change
check() {
output=$($lsof "$1" "$2" -F p 2>/dev/null)
status=$?
if echo "$output" | grep -q '^p'; then
echo "lsof $1 '$2' listed a process"
exit 1
fi
if [ $status -eq 0 ]; then
echo "lsof $1 '$2' should fail"
exit 1
fi
}
{
pgid=$(ps -o pgid= -p $$ | tr -d ' ')
for spec in "" ",$$" "$$,,$$" "^"; do
check -p "$spec"
done
for spec in ",$pgid" "$pgid,,$pgid" "^"; do
check -g "$spec"
done
exit 0
} > $report 2>&1
errors=$(mktemp "${TMPDIR:-/tmp}/lsof-id-errors.XXXXXX") || exit 1
trap 'rm -f "$errors"' EXIT
check_error() {
local output status
output=$("$lsof" "$1" "$2" -F p 2>"$errors")
status=$?
if [ "$status" -ne 1 ] || [ -n "$output" ] ||
! grep -Fq ": $3" "$errors"; then
echo "FAIL: lsof $1 '$2': expected '$3', status 1, and no stdout"
echo "status: $status; stdout: $output"
cat "$errors"
exit 1
fi
echo "PASS: lsof $1 '$2' reports '$3'"
}
check_valid() {
local output status pids
# Limit successful searches to this process and its cwd, not the whole system.
output=$("$lsof" -w -a -p "$$" -d cwd -Fpg "$@" 2>"$errors")
status=$?
pids=$(echo "$output" | grep '^p')
if [ "$status" -ne 0 ] || [ "$pids" != "p$$" ] ||
! echo "$output" | grep -qx "g$pgid"; then
echo "FAIL: lsof $*: expected this process and its PGID, status 0"
echo "status: $status; stdout: $output"
cat "$errors"
exit 1
fi
echo "PASS: lsof $* remains valid"
}
{
pgid=$(ps -o pgid= -p $$ | tr -d ' ')
case "$pgid" in
''|*[!0-9]*) echo "FAIL: cannot determine process group ID"; exit 1 ;;
esac
check_error -p "" "no process ID specified"
for spec in "," ",$$" "$$,,$$" "^" "^,$$" "$$,^"; do
check_error -p "$spec" "empty process ID: $spec"
done
for spec in "," ",$pgid" "$pgid,,$pgid" "^" "^,$pgid" "$pgid,^"; do
check_error -g "$spec" "empty process group ID: $spec"
done
# Preserve valid IDs, exclusions, and the accepted trailing comma.
for spec in "$$" "$$,$$" "$$," "^$PPID" "^0"; do
check_valid -p "$spec"
done
check_valid -g
check_valid -g ""
for spec in "$pgid" "$pgid,$pgid" "$pgid," "^$((pgid + 1))" "^0"; do
check_valid -g "$spec"
done
# Explicit zero is an ID, not an empty entry. -Q permits an unmatched ID 0.
check_valid -Q -p "$$,0"
check_valid -Q -g "$pgid,0"
exit 0
} > "$report" 2>&1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cf64be0 takes the suggested test with two changes.

The PGID comes from lsof -Fg and not from ps -o pgid= -p $$, because BusyBox ps has no -p option and the linux-alpine317 job installs no procps. With BusyBox 1.37.0 ps, the suggested test stops at "FAIL: cannot determine process group ID".

check_valid -Q -p "$$,0" is not in the test. PID 0 is the kernel process on FreeBSD, and gather_proc_info() in lib/dialects/freebsd/dproc.c does not skip it, so a second p line is possible there. This is from the source only, not tested on FreeBSD. -p ^0, -g ^0 and -Q -g "$pgid,0" stay for the explicit zero.

The test fails on master. With the change, make check on Debian 13 gives 38 PASS, 10 SKIP, 0 FAIL as an unprivileged user. Not tested on macOS, FreeBSD, and OpenBSD.

The test checked only that lsof fails and lists no process. Check
also the exit status 1, the empty standard output, and the message
of each rejected list. Add the forms that must stay accepted: a
plain list, a trailing comma, an exclusion, an explicit ID 0, -g
with an empty list, and -g with no list.

Take the PGID from lsof and not from ps. BusyBox ps has no -p
option, so the -g lists of the test had no PGID there.

The new checks follow the test suggested in the review of lsof-org#393.
@jiegec

jiegec commented Nov 15, 2026

Copy link
Copy Markdown
Member

Thanks for the patch. I agree with the core bug fix: an empty entry must not become ID 0. However, I’d like to suggest a slightly different treatment: instead of rejecting empty entries, ignore them.

For example:

  • -p ,123 → 123
  • -p 123,,456 → 123 and 456
  • -p 123, → 123
  • -p ^,123 → 123
  • -p 123,^ → 123

The same would apply to -g and -u.

This still fixes the dangerous ID 0 behavior, but avoids turning lists that are assembled mechanically from comma-separated values into hard errors. A leading, trailing, or doubled comma is a very common artifact of shell scripts; ignoring the empty field seems friendlier than aborting.

Invocation Before Current PR If empty entries are ignored
-p ,123 ID 0 plus 123; often exits 1 because PID 0 isn’t found error 123
-p 123,,456 ID 0 plus 123 and 456 error 123 and 456
-p 123, accepted as 123 accepted 123
-p ^,123 empty exclusion plus 123 error 123
-p 123,^ 123 plus an empty exclusion error 123
-p ^ excludes ID 0; effectively lists everything error no valid IDs; see the empty-list policy below
-p 0 / -p ^0 explicit PID 0 valid valid
-g ,123 PGID 0 plus 123 error 123
-g "" only enables PGID display unchanged unchanged
-g "," / -g "^" empty entry error no valid IDs; no PGID set, so display-only
-u ,123 PR #377 currently rejects this n/a 123
-u "" PR #377 reports “no UIDs specified” n/a error under the empty-list policy

The remaining question is what to do when no valid ID remains at all, for example -p "", -p ",", or -p "^". There are two reasonable choices:

  1. Treat it like a missing required argument and report an error.

  2. Treat it as “no PID selection” and list everything.

    • Backward-compatible with the current -p "" behavior.
    • But it preserves a footgun, and it is inconsistent with -u "" unless we change reject an empty entry in the -u list #377 too.
    • Also makes -p "^" list everything, which is surprising.

For -g, I think the answer is clearer: no valid PGID means no PGID set, and Lsof.8 explicitly says that -g without a PGID set only enables PGID display. So -g "", -g ",", and -g "^" should all remain display-only, not errors.

The main reason I’m raising this is consistency with #377. #377 currently rejects empty entries in -u (-u ,99999 is an error). If we ignore empty entries for -p and -g, we either:

  • leave -u stricter than -p/-g, which is hard to explain, or
  • relax -u as a follow-up so all three options ignore empty entries.

I don’t want to silently diverge from #377 without deciding this explicitly.

Explicit 0 must remain valid in all cases: -p 0, -p ^0, -g 0, and -g ^0 are real IDs, not empty entries. And -Q should not suppress syntax/argument errors; it should only ignore “search result not found” failures.

Rejecting an empty entry turns a list that a script joined with a
leading or doubled comma into a hard error. Skip the entry instead,
as suggested in the review of lsof-org#393: "-p ,123" and "-p 123,,456" now
select the IDs that are there, and still never ID 0. Lsof.8 says so.

-p needs at least one ID, so "-p ''", "-p ," and "-p ^" report "no
process ID specified" like -p with no argument. That keeps
'lsof -p "$pids"' with an empty variable from listing every process.
-g with no ID keeps its documented meaning and only enables the PGID
display.
@sagaragas sagaragas changed the title reject an empty entry in the -p and -g lists ignore an empty entry in the -p and -g lists Nov 18, 2026
@sagaragas

Copy link
Copy Markdown
Author

Agreed, ignoring them is friendlier for scripts. 933a4f2 skips empty entries in -p and -g, so the -p and -g rows of your table give the last column now. An explicit 0 is still an ID.

For the empty list I went with option 1, mostly for the lsof -p "$pids" case. -p "", -p , and -p ^ report "no process ID specified" like -p with no argument, and -Q doesn't hide that. -g with no ID left stays display-only.

I left -u as it is here. I'd relax it the same way so all three match, since skipping an empty entry in enter_uid() doesn't make anything ambiguous. That's a follow-up to #377 though, so your call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants