Commit 9bdb4be8 authored by Rusty Russell's avatar Rusty Russell

tal/grab_file: be robust against EINTR.

Exracted (and slightly modified) from a MacOS PR for lightning.

Based-on-patch-by: https://github.com/conanocSigned-off-by: default avatarRusty Russell <rusty@rustcorp.com.au>
parent 0dc64675
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
void *grab_fd(const void *ctx, int fd) void *grab_fd(const void *ctx, int fd)
...@@ -22,7 +23,12 @@ void *grab_fd(const void *ctx, int fd) ...@@ -22,7 +23,12 @@ void *grab_fd(const void *ctx, int fd)
max = 16384; max = 16384;
buffer = tal_arr(ctx, char, max+1); 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; size += ret;
if (size == max) { if (size == max) {
size_t extra = max; size_t extra = max;
...@@ -35,12 +41,8 @@ void *grab_fd(const void *ctx, int fd) ...@@ -35,12 +41,8 @@ void *grab_fd(const void *ctx, int fd)
max += extra; max += extra;
} }
} }
if (ret < 0) buffer[size] = '\0';
buffer = tal_free(buffer); tal_resize(&buffer, size+1);
else {
buffer[size] = '\0';
tal_resize(&buffer, size+1);
}
return buffer; return buffer;
} }
......
...@@ -13,6 +13,9 @@ ...@@ -13,6 +13,9 @@
* tal_count() is the size in bytes plus one: for convenience, the * tal_count() is the size in bytes plus one: for convenience, the
* byte after the end of the content will always be NUL. * byte after the end of the content will always be NUL.
* *
* Note that this does *not* currently exit on EINTR, but continues
* reading.
*
* Example: * Example:
* #include <ccan/tal/str/str.h> * #include <ccan/tal/str/str.h>
* #include <ccan/tal/tal.h> * #include <ccan/tal/tal.h>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment