Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lsof.8
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
23 changes: 21 additions & 2 deletions src/arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1160,12 +1160,12 @@ 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.
*/
for (i = id = x = 0; *cp && *cp != ','; cp++) {
for (i = id = n = x = 0; *cp && *cp != ','; cp++) {
if (!i) {
i = 1;
if (*cp == '^') {
Expand All @@ -1187,9 +1187,19 @@ int enter_id(struct lsof_context *ctx, /* context */
return (1);
}
id = (id * 10) + *cp - '0';
n++;
}
if (*cp)
cp++;
if (!n) {

/*
* Ignore an empty entry (e.g. from ",123" or "^"); it would
* otherwise be taken as ID 0.
*/
continue;
}
ni++;
if (ty == PGID) {
if (lsof_select_pgid(ctx, id, x)) {
err = 1;
Expand All @@ -1200,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);
}

Expand Down
97 changes: 97 additions & 0 deletions tests/case-20-id-empty-entry.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env bash
# 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
trap 'rm -f "$errors"' EXIT

check_error() {
local output status
output=$("$lsof" "$@" -F p 2>"$errors")
status=$?
if [ "$status" -ne 1 ] || [ -n "$output" ] ||
! 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 $* reports 'no process ID specified'"
}

check_valid() {
local output status pids
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 only this process and its PGID, status 0"
echo "status: $status; stdout: $output"
cat "$errors"
exit 1
fi
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"
}

{
# 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 ;;
# 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

# -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 ""

# An empty entry is ignored, and the IDs next to it are selected.
for spec in "$$" "$$,$$" ",$$" "$$,,$$" "$$," "^,$$" "$$,^"; do
check_valid -p "$spec"
done
# 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,
# 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