From 8ab818626d8ef8cd303e0c16765252c6fb22ef17 Mon Sep 17 00:00:00 2001 From: "Anthony G. Basile" Date: Wed, 27 May 2015 08:16:14 -0400 Subject: src/shared/util.h: import loop_write() from upstream Signed-off-by: Anthony G. Basile --- src/shared/util.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/shared/util.c') diff --git a/src/shared/util.c b/src/shared/util.c index f726e0ee2b..88defdc4f2 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -906,6 +906,44 @@ int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) { return 0; } +int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) { + const uint8_t *p = buf; + + assert(fd >= 0); + assert(buf); + + errno = 0; + + do { + ssize_t k; + + k = write(fd, p, nbytes); + if (k < 0) { + if (errno == EINTR) + continue; + + if (errno == EAGAIN && do_poll) { + /* We knowingly ignore any return value here, + * and expect that any error/EOF is reported + * via write() */ + + fd_wait_for_event(fd, POLLOUT, USEC_INFINITY); + continue; + } + + return -errno; + } + + if (nbytes > 0 && k == 0) /* Can't really happen */ + return -EIO; + + p += k; + nbytes -= k; + } while (nbytes > 0); + + return 0; +} + char* dirname_malloc(const char *path) { char *d, *dir, *dir2; -- cgit v1.2.3-54-g00ecf