[LTP] [PATCH 1/3] lib: introduce safe_write_fully()

Jan Stancek jstancek@redhat.com
Mon Oct 3 14:48:46 CEST 2022


In case there is a short (but otherwise successful) write(),
safe_write_fully() repeats write() and attempts to resume
with the remainder of the buffer.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 include/tst_safe_macros.h |  5 +++++
 lib/tst_safe_macros.c     | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/tst_safe_macros.h b/include/tst_safe_macros.h
index 81c4b0844267..caee0e9cf842 100644
--- a/include/tst_safe_macros.h
+++ b/include/tst_safe_macros.h
@@ -645,4 +645,9 @@ int safe_sysinfo(const char *file, const int lineno, struct sysinfo *info);
 #define SAFE_SYSINFO(info) \
 	safe_sysinfo(__FILE__, __LINE__, (info))
 
+ssize_t safe_write_fully(const char *file, const int lineno,
+	int fildes, const void *buf, size_t nbyte);
+#define SAFE_WRITE_FULLY(fildes, buf, nbyte) \
+	safe_write_fully(__FILE__, __LINE__, (fildes), (buf), (nbyte))
+
 #endif /* SAFE_MACROS_H__ */
diff --git a/lib/tst_safe_macros.c b/lib/tst_safe_macros.c
index c4cdc87e7346..e4d4ef4269a4 100644
--- a/lib/tst_safe_macros.c
+++ b/lib/tst_safe_macros.c
@@ -591,3 +591,22 @@ void safe_cmd(const char *file, const int lineno, const char *const argv[],
 		tst_brk_(file, lineno, TBROK, "%s failed (%d)", argv[0], rval);
 	}
 }
+
+ssize_t safe_write_fully(const char *file, const int lineno,
+	int fildes, const void *buf, size_t nbyte)
+{
+	ssize_t rval;
+	size_t len = nbyte;
+
+	do {
+		rval = write(fildes, buf, len);
+		if (rval == -1) {
+			tst_brk_(file, lineno, TBROK | TERRNO,
+				"write(%d,%p,%zu) failed", fildes, buf, len);
+		}
+		buf += rval;
+		len -= rval;
+	} while (len > 0);
+
+	return nbyte;
+}
-- 
2.27.0



More information about the ltp mailing list