diff mbox series

lib: handle ENOENT errors when processes exit during PID scanning

Message ID 20240918073257.13930-1-liwang@redhat.com
State Accepted
Headers show
Series lib: handle ENOENT errors when processes exit during PID scanning | expand

Commit Message

Li Wang Sept. 18, 2024, 7:32 a.m. UTC
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.

Reported-by: Paul Bunyan <pbunyan@redhat.com>
Analysed-by: Charles Mirabile <cmirabil@redhat.com>
Signed-off-by: Li Wang <liwang@redhat.com>
---
 lib/safe_file_ops.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Cyril Hrubis Sept. 20, 2024, 2:26 p.m. UTC | #1
Hi!
I've spend some time looking at the code that uses the
FILE_LINES_SCANF() and it looks like this change shouldn't break
anything.

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Li Wang Sept. 21, 2024, 2:35 a.m. UTC | #2
Cyril Hrubis <chrubis@suse.cz> wrote:

Hi!
> I've spend some time looking at the code that uses the
> FILE_LINES_SCANF() and it looks like this change shouldn't break
> anything.
>
> Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
>

Patch merged, thank you.
diff mbox series

Patch

diff --git a/lib/safe_file_ops.c b/lib/safe_file_ops.c
index 63ae2dbbe..4cf0d6e0f 100644
--- a/lib/safe_file_ops.c
+++ b/lib/safe_file_ops.c
@@ -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;