[LTP] [PATCH] lib: handle ENOENT errors when processes exit during PID scanning

Li Wang liwang@redhat.com
Wed Sep 18 09:31:00 CEST 2024


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/.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

diff --git a/lib/.tst_pid.c.swo b/lib/.tst_pid.c.swo
new file mode 100644
index 0000000000000000000000000000000000000000..956a03da5090a5af543e0dca7eab124d2a2f1d67
GIT binary patch
literal 16384
zcmeHNYiuJ|75=);CU)$^1%%2&z*#Tp+NtA5+7&I?tnlWsYc(&~PPf}qtcmA3zS{AO
zHTOEZVf&&*lvknU4??9#AS8Yu1O<T*;_)8@Qjwr4YLO6sKvl)BmI^3BXU5~$-rcC}
zFF^B2Q9SpabM86ko^!_cT<;X_DzDQWxgr4E2&88I)V%o5_uu|p0kGsH!`|hI!(j@`
zn(GMnL$}SJoGZ-d+?G~3VejTT;^KVH6=uuOxrT!WeSvvPcla;Y?I`UEqn)?pPF^cr
z??^M(IDD-TAws-WOySn~Vu87jr;gD(-oE`-1%(I^B1DJ~Awq-*5#oOkx^n~%0XWjr
zoj)^f82KORJ?DD=A0C+x5h6s05FtW@2oWMgh!7z{{P)5z;hpKva0B2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@4M>I
zM(ROo_Cv1U+g)1QtgZ}QSMn5Rj@Mt0^*QL4MW4OrA5JL$CdRwQSu1a>RIhQFGjLUz
UrBuzo*8DK}mpY#Cz~34C2N*U}KL7v#

literal 0
HcmV?d00001

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;
-- 
2.46.0



More information about the ltp mailing list