Repository navigation
Conversation
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.
| 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 |
There was a problem hiding this comment.
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:
| 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 |
There was a problem hiding this comment.
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.
|
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:
The same would apply to 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.
The remaining question is what to do when no valid ID remains at all, for example
For The main reason I’m raising this is consistency with #377. #377 currently rejects empty entries in
I don’t want to silently diverge from #377 without deciding this explicitly. Explicit |
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.
|
Agreed, ignoring them is friendlier for scripts. 933a4f2 skips empty entries in For the empty list I went with option 1, mostly for the I left |
An empty entry in a
-por-glist gets entered as ID 0, and-p ""selects every process:enter_id()doesn't check that an entry has a digit, so,123,123,,456and 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.-pstill needs at least one ID:-p "",-p ,and-p ^are an error like-pwith no argument, which keepslsof -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.-gwith no ID left (-g "",-g ,,-g ^) only turns on the PGID display, which is what Lsof.8 says for-gwithout a PGID set.Lsof.8has a line about empty entries now. Addedtests/case-20-id-empty-entry.bash, it fails before and passes after, andmake checkis clean on Linux.