From 1fbf0e27f594105816cc374fc587575cbc1066c8 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 3 Oct 2018 16:55:25 -0400 Subject: [PATCH] make error reporting more robust --- util.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/util.c b/util.c index 16a6e15..4d4360f 100644 --- a/util.c +++ b/util.c @@ -1,3 +1,4 @@ +#include #include #include @@ -5,10 +6,26 @@ #include "util.h" +static int write_full(int fd, const char *buf, size_t length) { + do { + ssize_t bytes_written = write(fd, buf, length); + if (bytes_written == -1) { + if (errno == EINTR) { + continue; + } + return -1; + } + buf += bytes_written; + length -= bytes_written; + } while (length); + + return 0; +} + COLD noreturn void fatal_error(const char *s) { const char *prefix = "fatal allocator error: "; - write(STDERR_FILENO, prefix, strlen(prefix)); - write(STDERR_FILENO, s, strlen(s)); - write(STDERR_FILENO, "\n", 1); + (void)(write_full(STDERR_FILENO, prefix, strlen(prefix)) != -1 && + write_full(STDERR_FILENO, s, strlen(s)) != -1 && + write_full(STDERR_FILENO, "\n", 1)); abort(); }