[LTP] [PATCH 1/2] safe_macros: Add SAFE_READ_ANY_EAGAIN

Cyril Hrubis chrubis@suse.cz
Mon May 25 17:14:48 CEST 2026


Similarily to the safe_write() this turns the len_strict into three way
switch with the new value SAFE_READ_ANY_EAGAIN=2 that turns EAGAIN into
a read with a zero lenght.

Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/safe_macros_fn.h | 10 ++++++++++
 lib/safe_macros.c        |  5 ++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/include/safe_macros_fn.h b/include/safe_macros_fn.h
index 9a09c4922..031b1ff98 100644
--- a/include/safe_macros_fn.h
+++ b/include/safe_macros_fn.h
@@ -36,6 +36,16 @@ enum safe_write_opts {
 	SAFE_WRITE_RETRY = 2,
 };
 
+
+enum safe_read_opts {
+	/* no lenght strictness, short reads are ok */
+	SAFE_READ_ANY = 0,
+	/* strict lenght, short reads trigger TBROK */
+	SAFE_READ_ALL = 1,
+	/* converts EAGAIN to read that returns 0 */
+	SAFE_READ_ANY_EAGAIN = 2,
+};
+
 char* safe_basename(const char *file, const int lineno,
                     void (*cleanup_fn)(void), char *path);
 
diff --git a/lib/safe_macros.c b/lib/safe_macros.c
index 68b8747b4..f838ede66 100644
--- a/lib/safe_macros.c
+++ b/lib/safe_macros.c
@@ -294,6 +294,9 @@ ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
 	rval = read(fildes, buf, nbyte);
 
 	if (rval == -1) {
+		if (len_strict == SAFE_READ_ANY_EAGAIN && errno == EAGAIN)
+			return 0;
+
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"read(%d,%p,%zu) failed, returned %zd", fildes, buf,
 			nbyte, rval);
@@ -301,7 +304,7 @@ ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
 			"Invalid read(%d,%p,%zu) return value %zd", fildes,
 			buf, nbyte, rval);
-	} else if (len_strict && (size_t)rval != nbyte) {
+	} else if (len_strict == SAFE_READ_ALL && (size_t)rval != nbyte) {
 		tst_brkm_(file, lineno, TBROK, cleanup_fn,
 			  "Short read(%d,%p,%zu) returned only %zd",
 			  fildes, buf, nbyte, rval);
-- 
2.53.0



More information about the ltp mailing list