public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/3] lib: introduce safe_write_fully()
@ 2022-10-03 12:48 Jan Stancek
  2022-10-03 12:48 ` [LTP] [PATCH 2/3] syscalls/preadv203: don't treat short write() as failure Jan Stancek
                   ` (3 more replies)
  0 siblings, 4 replies; 13+ messages in thread
From: Jan Stancek @ 2022-10-03 12:48 UTC (permalink / raw)
  To: ltp

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


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

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-10-17 14:16 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-03 12:48 [LTP] [PATCH 1/3] lib: introduce safe_write_fully() Jan Stancek
2022-10-03 12:48 ` [LTP] [PATCH 2/3] syscalls/preadv203: don't treat short write() as failure Jan Stancek
2022-10-03 12:48 ` [LTP] [PATCH 3/3] mmapstress04: use SAFE_WRITE_FULLY from LTP library Jan Stancek
2022-10-03 13:16 ` [LTP] [PATCH 1/3] lib: introduce safe_write_fully() Cyril Hrubis
2022-10-03 13:56   ` Jan Stancek
2022-10-03 14:00     ` Cyril Hrubis
2022-10-04  8:31 ` [LTP] [PATCH v2] lib: introduce safe_write() retry Jan Stancek
2022-10-04 12:20   ` Petr Vorel
2022-10-04 12:34     ` Jan Stancek
2022-10-04 18:45       ` Petr Vorel
2022-10-05  9:32   ` Cyril Hrubis
2022-10-05 12:02     ` Jan Stancek
2022-10-17 14:15       ` Richard Palethorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox