diff --git a/Makefile.am b/Makefile.am index 00798573..99f563bf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/lib/dialects/linux/dproc.c b/lib/dialects/linux/dproc.c index 62d1d6e2..0de41a6d 100644 --- a/lib/dialects/linux/dproc.c +++ b/lib/dialects/linux/dproc.c @@ -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 @@ -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 @@ -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)) @@ -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); @@ -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; @@ -1555,7 +1569,7 @@ 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); @@ -1563,7 +1577,7 @@ process_proc_map(struct lsof_context *ctx, /* context */ } else { stat_directly: if (!efs) { - if (HasNFS) + if (HasNFS || Fblock) sv = statsafely(ctx, fp[6], &sb); else sv = stat(fp[6], &sb); diff --git a/lib/dialects/linux/tests/case-20-avoid-blocking.bash b/lib/dialects/linux/tests/case-20-avoid-blocking.bash new file mode 100755 index 00000000..c8ad3eee --- /dev/null +++ b/lib/dialects/linux/tests/case-20-avoid-blocking.bash @@ -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