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 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
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;
}
......
......@@ -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 <ccan/tal/str/str.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