From: Rusty Russell Date: Fri, 6 Apr 2018 07:29:39 +0000 (+0930) Subject: tal/grab_file: be robust against EINTR. X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=9bdb4be82879348fc3867a922445a4aa06ff8ffd tal/grab_file: be robust against EINTR. Exracted (and slightly modified) from a MacOS PR for lightning. Based-on-patch-by: https://github.com/conanoc Signed-off-by: Rusty Russell --- diff --git a/ccan/tal/grab_file/grab_file.c b/ccan/tal/grab_file/grab_file.c index 88e7c225..6528780a 100644 --- a/ccan/tal/grab_file/grab_file.c +++ b/ccan/tal/grab_file/grab_file.c @@ -5,6 +5,7 @@ #include #include #include +#include #include void *grab_fd(const void *ctx, int fd) @@ -22,7 +23,12 @@ void *grab_fd(const void *ctx, int fd) max = 16384; buffer = tal_arr(ctx, char, max+1); - while ((ret = read(fd, buffer + size, max - size)) > 0) { + while ((ret = read(fd, buffer + size, max - size)) != 0) { + if (ret < 0) { + if (errno == EINTR) + continue; + return tal_free(buffer); + } size += ret; if (size == max) { size_t extra = max; @@ -35,12 +41,8 @@ void *grab_fd(const void *ctx, int fd) max += extra; } } - if (ret < 0) - buffer = tal_free(buffer); - else { - buffer[size] = '\0'; - tal_resize(&buffer, size+1); - } + buffer[size] = '\0'; + tal_resize(&buffer, size+1); return buffer; } diff --git a/ccan/tal/grab_file/grab_file.h b/ccan/tal/grab_file/grab_file.h index 03bf32a7..2eb1a008 100644 --- a/ccan/tal/grab_file/grab_file.h +++ b/ccan/tal/grab_file/grab_file.h @@ -13,6 +13,9 @@ * tal_count() is the size in bytes plus one: for convenience, the * byte after the end of the content will always be NUL. * + * Note that this does *not* currently exit on EINTR, but continues + * reading. + * * Example: * #include * #include