Skip to contentSkip to conversation

lib: handle ENOENT errors when processes exit during PID scanning

PATCH In review Li Wang 1 participant 2 messages Sep 18, 2024 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 lib: handle ENOENT errors when processes exit during PID scanning

There is a race window between readdir() and fopen() in line 125~131 of lib function get_used_pids. A process could terminate after its directory entry is read but before its status file is opened.

When fopen() is called, the status file may no longer exist, resulting in an error like:

tag=msgstress01__with_dmesg_entry stime=1723563200
...
tst_test.c:1617: TINFO: Timeout per run is 0h 03m 30s
tst_pid.c:128: TBROK: Failed to open FILE '/proc/73429/status' for reading: ENOENT (2)

To resolve this, the file_lines_scanf() function is modified to handle ENOENT errors gracefully when strict mode is not enabled. If fopen() fails due to ENOENT, the function now returns 1 to skip the missing file instead of treating it as a critical error.

---
 lib/.tst_pid.c.swo  | Bin 0 -> 16384 bytes
 lib/safe_file_ops.c |   3 +++
 2 files changed, 3 insertions(+)
 create mode 100644 lib/.tst_pid.c.swo
lib/.tst_pid.c.swo new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..956a03da5090a5af543e0dca7eab124d2a2f1d67
GIT binary patch
lib/safe_file_ops.c +3 -0
index 63ae2dbbe..4cf0d6e0f 100644
@@ -182,6 +182,9 @@ int file_lines_scanf(const char *file, const int lineno,
 
 	fp = fopen(path, "r");
 	if (fp == NULL) {
+		if (strict == 0 && errno == ENOENT)
+			return 1;
+
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"Failed to open FILE '%s' for reading", path);
 		return 1;
Signature · 5 lines
2.46.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#1 lore raw

Sorry, this patch contains binary mess code, please ignore it.

I will resend a pure one.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp