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
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ check_PROGRAMS =
# Linux
LINUX_TESTS = lib/dialects/linux/tests/case-10-mqueue.bash \
lib/dialects/linux/tests/case-10-ux-socket-state.bash \
lib/dialects/linux/tests/case-20-avoid-blocking.bash \
lib/dialects/linux/tests/case-20-epoll.bash \
lib/dialects/linux/tests/case-20-eventfd-endpoint.bash \
lib/dialects/linux/tests/case-20-inet6-ffffffff-handling.bash \
Expand Down
26 changes: 20 additions & 6 deletions lib/dialects/linux/dproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ static int process_id(struct lsof_context *ctx, /* context */
pn = 0;
} else {
ss = SB_ALL;
if (HasNFS) {
if (HasNFS || Fblock) {
if ((sv = statsafely(ctx, Path, &sb)))
sv = statEx(ctx, pbuf, &sb, &ss);
} else
Expand Down Expand Up @@ -1012,7 +1012,7 @@ static int process_id(struct lsof_context *ctx, /* context */
pn = 0;
else {
ss = SB_ALL;
if (HasNFS) {
if (HasNFS || Fblock) {
if ((sv = statsafely(ctx, Path, &sb)))
sv = statEx(ctx, pbuf, &sb, &ss);
} else
Expand Down Expand Up @@ -1061,7 +1061,7 @@ static int process_id(struct lsof_context *ctx, /* context */
pn = 0;
else {
ss = SB_ALL;
if (HasNFS) {
if (HasNFS || Fblock) {
if ((sv = statsafely(ctx, Path, &sb))) {
sv = statEx(ctx, pbuf, &sb, &ss);
if (!sv && (ss & SB_DEV) && (ss & SB_INO))
Expand Down Expand Up @@ -1191,7 +1191,7 @@ static int process_id(struct lsof_context *ctx, /* context */
efs = 1;
pn = 0;
} else {
if (HasNFS) {
if (HasNFS || Fblock) {
if (lstatsafely(ctx, Path, &lsb)) {
enls_fd = errno;
(void)statEx(ctx, pbuf, &lsb, &ls);
Expand Down Expand Up @@ -1226,6 +1226,20 @@ static int process_id(struct lsof_context *ctx, /* context */
nmabuf[sizeof(nmabuf) - 1] = '\0';
(void)add_nma(ctx, nmabuf, strlen(nmabuf));
}
/* Proc socket links already identify the inode; keep network
* selection usable with -b without following the link. */
if (Fblock && !strcmp(pbuf, "socket") && rest && *rest == '[') {
char *ep;
unsigned long long ino = strtoull(rest + 1, &ep, 10);

if (ino != ULLONG_MAX && ep > rest + 1 && *ep == ']' &&
!ep[1]) {
zeromem((char *)&sb, sizeof(sb));
sb.st_mode = S_IFSOCK;
sb.st_ino = (ino_t)ino;
ss = SB_MODE | SB_INO;
}
}
if (Ckscko) {
if ((ss & SB_MODE) && ((sb.st_mode & S_IFMT) == S_IFSOCK)) {
pn = 1;
Expand Down Expand Up @@ -1555,15 +1569,15 @@ process_proc_map(struct lsof_context *ctx, /* context */
goto stat_directly;

if (!efs) {
if (HasNFS)
if (HasNFS || Fblock)
sv = statsafely(ctx, path, &sb);
else
sv = stat(path, &sb);
}
} else {
stat_directly:
if (!efs) {
if (HasNFS)
if (HasNFS || Fblock)
sv = statsafely(ctx, fp[6], &sb);
else
sv = stat(fp[6], &sb);
Expand Down
52 changes: 52 additions & 0 deletions lib/dialects/linux/tests/case-20-avoid-blocking.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
source tests/common.bash

for tool in python3 strace; do
if ! command -v "$tool" >/dev/null 2>&1; then
echo "SKIP: $tool not available" >> "$report"
exit 77
fi
done

python3 - "$lsof" >> "$report" 2>&1 <<'PY'
import os
import pathlib
import re
import socket
import subprocess
import sys
import tempfile

with tempfile.TemporaryDirectory() as directory:
trace = pathlib.Path(directory) / "trace"
with tempfile.TemporaryFile() as file, socket.socket() as listener:
listener.bind(("127.0.0.1", 0))
listener.listen()
pid = str(os.getpid())
base = [sys.argv[1], "-b", "-w", "-nP", "-p", pid]
result = subprocess.run(
["strace", "-f", "-e", "trace=%file", "-o", str(trace)] + base,
capture_output=True, text=True, timeout=20,
)
if "ptrace" in result.stderr and "Operation not permitted" in result.stderr:
print("SKIP: tracing is not permitted")
sys.exit(77)
assert result.returncode == 0, result.stderr
target = re.compile(
r'"/proc/' + pid + r'/(?:cwd|root|exe|fd/\d+|map_files/[^"]+)"'
)
stat_call = re.compile(r'\b(?:l?stat(?:64)?|newfstatat|fstatat64|statx)\(')
calls = [line for line in trace.read_text().splitlines()
if stat_call.search(line) and target.search(line)]
assert not calls, "-b still stats process targets:\n" + "\n".join(calls)

port = str(listener.getsockname()[1])
result = subprocess.run(
base + ["-a", "-iTCP:" + port, "-Ffn"],
capture_output=True, text=True, timeout=20,
)
assert result.returncode == 0, result.stderr
assert "f" + str(listener.fileno()) in result.stdout.splitlines(), result.stdout
assert ":" + port in result.stdout, result.stdout
print("PASS: -b avoids process-target stat calls and still finds sockets")
PY
Loading