From 1f610e4bbcc5d454c90d54ea97ab450fa9536bf9 Mon Sep 17 00:00:00 2001 From: Sagar Narayan <113566055+sagaragas@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:08:58 -0800 Subject: [PATCH 1/3] reject an empty entry in the -p and -g lists 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. --- Makefile.am | 1 + src/arg.c | 20 ++++++++++++++++++-- tests/case-20-id-empty-entry.bash | 28 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100755 tests/case-20-id-empty-entry.bash diff --git a/Makefile.am b/Makefile.am index 00798573..5ffe2f40 100644 --- a/Makefile.am +++ b/Makefile.am @@ -130,6 +130,7 @@ DIALECT_NEUTRAL_TESTS = tests/case-00-hello.bash \ tests/case-20-exit-status.bash \ tests/case-20-fd-only-inclusion.bash \ tests/case-20-handle-missing-files.bash \ + tests/case-20-id-empty-entry.bash \ tests/case-20-soft-errors.bash \ tests/case-20-offset-field.bash \ tests/case-20-repeat-count.bash \ diff --git a/src/arg.c b/src/arg.c index b0573615..8276bff2 100644 --- a/src/arg.c +++ b/src/arg.c @@ -1152,7 +1152,11 @@ int enter_id(struct lsof_context *ctx, /* context */ int err, i, id, j, mx, n, ni, nx, x; struct int_lst *s; - if (!p) { + /* + * An empty PGID list is the same as -g with no list, which only enables + * the PGID display. + */ + if (!p || (ty == PID && !*p)) { (void)fprintf(stderr, "%s: no process%s ID specified\n", Pn, (ty == PGID) ? " group" : ""); return (1); @@ -1165,7 +1169,7 @@ int enter_id(struct lsof_context *ctx, /* context */ /* * Assemble ID. */ - for (i = id = x = 0; *cp && *cp != ','; cp++) { + for (i = id = n = x = 0; *cp && *cp != ','; cp++) { if (!i) { i = 1; if (*cp == '^') { @@ -1187,6 +1191,18 @@ int enter_id(struct lsof_context *ctx, /* context */ return (1); } id = (id * 10) + *cp - '0'; + n++; + } + if (!n) { + + /* + * An empty entry (e.g. from ",123" or "^") would otherwise be + * taken as ID 0. + */ + (void)fprintf(stderr, "%s: empty process%s ID: ", Pn, + (ty == PGID) ? " group" : ""); + safestrprt(p, stderr, 1); + return (1); } if (*cp) cp++; diff --git a/tests/case-20-id-empty-entry.bash b/tests/case-20-id-empty-entry.bash new file mode 100755 index 00000000..656f1f55 --- /dev/null +++ b/tests/case-20-id-empty-entry.bash @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# An empty entry in a -p or -g list must not be taken as ID 0, and an empty +# -p list must not select every process. +source tests/common.bash + +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 From cf64be05a38a20451bdc172e42799dfb52f1cd5c Mon Sep 17 00:00:00 2001 From: Sagar Narayan <113566055+sagaragas@users.noreply.github.com> Date: Sat, 14 Nov 2026 16:09:04 -0800 Subject: [PATCH 2/3] check the messages and the accepted forms in the -p and -g test 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 #393. --- tests/case-20-id-empty-entry.bash | 62 +++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/tests/case-20-id-empty-entry.bash b/tests/case-20-id-empty-entry.bash index 656f1f55..81024533 100755 --- a/tests/case-20-id-empty-entry.bash +++ b/tests/case-20-id-empty-entry.bash @@ -3,26 +3,64 @@ # -p list must not select every process. source tests/common.bash -check() { - output=$($lsof "$1" "$2" -F p 2>/dev/null) +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 echo "$output" | grep -q '^p'; then - echo "lsof $1 '$2' listed a process" + 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 - if [ $status -eq 0 ]; then - echo "lsof $1 '$2' should fail" + 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 ' ') - for spec in "" ",$$" "$$,,$$" "^"; do - check -p "$spec" + # Not from ps: BusyBox ps has no -p option. + pgid=$("$lsof" -w -a -p "$$" -d cwd -Fg | sed -n 's/^g//p') + 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 - for spec in ",$pgid" "$pgid,,$pgid" "^"; do - check -g "$spec" + 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 -g "$pgid,0" exit 0 -} > $report 2>&1 +} > "$report" 2>&1 From 933a4f2f5f1e5a58bcfe4826ba9626a270cc7607 Mon Sep 17 00:00:00 2001 From: Sagar Narayan <113566055+sagaragas@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:51:30 -0800 Subject: [PATCH 3/3] ignore an empty entry in the -p and -g lists 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 #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. --- Lsof.8 | 2 + src/arg.c | 31 ++++++------ tests/case-20-id-empty-entry.bash | 79 +++++++++++++++++++++---------- 3 files changed, 74 insertions(+), 38 deletions(-) diff --git a/Lsof.8 b/Lsof.8 index cc81314c..9e5ad20c 100644 --- a/Lsof.8 +++ b/Lsof.8 @@ -884,6 +884,7 @@ comma\-separated set .I s \&\- e.g., ``123'' or ``123,^456''. (There should be no spaces in the set.) +An empty entry in the set, as in ``,123'', is ignored. .IP PGID numbers that begin with `^' (negation) represent exclusions. .IP @@ -1456,6 +1457,7 @@ comma\-separated set .I s \&\- e.g., ``123'' or ``123,^456''. (There should be no spaces in the set.) +An empty entry in the set, as in ``,123'', is ignored. .IP PID numbers that begin with `^' (negation) represent exclusions. .IP diff --git a/src/arg.c b/src/arg.c index 8276bff2..850e5b2c 100644 --- a/src/arg.c +++ b/src/arg.c @@ -1152,11 +1152,7 @@ int enter_id(struct lsof_context *ctx, /* context */ int err, i, id, j, mx, n, ni, nx, x; struct int_lst *s; - /* - * An empty PGID list is the same as -g with no list, which only enables - * the PGID display. - */ - if (!p || (ty == PID && !*p)) { + if (!p) { (void)fprintf(stderr, "%s: no process%s ID specified\n", Pn, (ty == PGID) ? " group" : ""); return (1); @@ -1164,7 +1160,7 @@ int enter_id(struct lsof_context *ctx, /* context */ /* * Convert and store the ID. */ - for (cp = p, err = 0; *cp;) { + for (cp = p, err = ni = 0; *cp;) { /* * Assemble ID. @@ -1193,19 +1189,17 @@ int enter_id(struct lsof_context *ctx, /* context */ id = (id * 10) + *cp - '0'; n++; } + if (*cp) + cp++; if (!n) { /* - * An empty entry (e.g. from ",123" or "^") would otherwise be - * taken as ID 0. + * Ignore an empty entry (e.g. from ",123" or "^"); it would + * otherwise be taken as ID 0. */ - (void)fprintf(stderr, "%s: empty process%s ID: ", Pn, - (ty == PGID) ? " group" : ""); - safestrprt(p, stderr, 1); - return (1); + continue; } - if (*cp) - cp++; + ni++; if (ty == PGID) { if (lsof_select_pgid(ctx, id, x)) { err = 1; @@ -1216,6 +1210,15 @@ int enter_id(struct lsof_context *ctx, /* context */ } } } + if (ty == PID && !ni) { + + /* + * -p needs at least one ID. A PGID list with no ID is the same as -g + * with no list, which only enables the PGID display. + */ + (void)fprintf(stderr, "%s: no process ID specified\n", Pn); + return (1); + } return (err); } diff --git a/tests/case-20-id-empty-entry.bash b/tests/case-20-id-empty-entry.bash index 81024533..23f8eac2 100755 --- a/tests/case-20-id-empty-entry.bash +++ b/tests/case-20-id-empty-entry.bash @@ -1,6 +1,7 @@ #!/usr/bin/env bash -# An empty entry in a -p or -g list must not be taken as ID 0, and an empty -# -p list must not select every process. +# An empty entry in a -p or -g list is ignored and must not be taken as ID 0. +# -p with no ID left is an error; -g with no ID left only enables the PGID +# display. source tests/common.bash errors=$(mktemp "${TMPDIR:-/tmp}/lsof-id-errors.XXXXXX") || exit 1 @@ -8,32 +9,46 @@ trap 'rm -f "$errors"' EXIT check_error() { local output status - output=$("$lsof" "$1" "$2" -F p 2>"$errors") + output=$("$lsof" "$@" -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" + ! grep -Fq ": no process ID specified" "$errors"; then + echo "FAIL: lsof $*: expected 'no process ID specified', status 1, and no stdout" echo "status: $status; stdout: $output" cat "$errors" exit 1 fi - echo "PASS: lsof $1 '$2' reports '$3'" + echo "PASS: lsof $* reports 'no process ID specified'" } 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") + output=$("$lsof" -w -a -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 "FAIL: lsof $*: expected only this process and its PGID, status 0" echo "status: $status; stdout: $output" cat "$errors" exit 1 fi - echo "PASS: lsof $* remains valid" + echo "PASS: lsof $* lists only this process" +} + +# This process is not in the selected process group, so nothing is listed. +check_none() { + local output status + # -Q: a PGID that matches no process is not an error. + output=$("$lsof" -w -Q -a -p "$$" -d cwd -Fp "$@" 2>"$errors") + status=$? + if [ "$status" -ne 0 ] || [ -n "$output" ]; then + echo "FAIL: lsof $*: expected no process, status 0" + echo "status: $status; stdout: $output" + cat "$errors" + exit 1 + fi + echo "PASS: lsof $* does not list this process" } { @@ -41,26 +56,42 @@ check_valid() { pgid=$("$lsof" -w -a -p "$$" -d cwd -Fg | sed -n 's/^g//p') case "$pgid" in ''|*[!0-9]*) echo "FAIL: cannot determine process group ID"; exit 1 ;; + # e.g. in a PID namespace that the process group leader is not in + 0) echo "this shell has PGID 0, skipping"; exit 77 ;; 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" + # -p needs at least one ID, and -Q does not hide that. + for spec in "" "," "^"; do + check_error -p "$spec" done + check_error -Q -p "" - # Preserve valid IDs, exclusions, and the accepted trailing comma. - for spec in "$$" "$$,$$" "$$," "^$PPID" "^0"; do + # An empty entry is ignored, and the IDs next to it are selected. + for spec in "$$" "$$,$$" ",$$" "$$,,$$" "$$," "^,$$" "$$,^"; 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" + # From here on, -p "$$" limits the search to this process. In ",^0" an + # empty entry that is taken as ID 0 is both included and excluded. + for spec in "^$PPID" "^0" ",^0"; do + check_valid -p "$$" -p "$spec" + done + for spec in "$pgid" "$pgid,$pgid" "^$((pgid + 1))" "^0" ",^0" \ + ",$pgid" "$pgid,,$pgid" "$pgid," "^,$pgid" "$pgid,^"; do + check_valid -p "$$" -g "$spec" + done + # -g with no ID only enables the PGID display. + check_valid -p "$$" -g + for spec in "" "," "^"; do + check_valid -p "$$" -g "$spec" + done + # A PGID next to an empty entry is selected, and so is an explicit 0. In + # "^,0" an empty entry that is taken as ID 0 is both excluded and included. + for spec in ",$((pgid + 1))" 0 "^,0"; do + check_none -g "$spec" done - # Explicit zero is an ID, not an empty entry. -Q permits an unmatched ID 0. - check_valid -Q -g "$pgid,0" + # Explicit zero is an ID, not an empty entry. -Q permits an unmatched ID 0, + # and -g keeps a PID 0 that exists out of the output. + check_valid -Q -p "$$" -g "$pgid,0" + check_valid -Q -g "$pgid" -p "$$" -p "^,0" exit 0 } > "$report" 2>&1