]> git.ozlabs.org Git - ccan/commitdiff
tal/grab_file: be robust against EINTR.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 6 Apr 2018 07:29:39 +0000 (16:59 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 6 Apr 2018 07:31:00 +0000 (17:01 +0930)
Exracted (and slightly modified) from a MacOS PR for lightning.

Based-on-patch-by: https://github.com/conanoc
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/tal/grab_file/grab_file.c
ccan/tal/grab_file/grab_file.h

index 88e7c22533ad2b8d1a1b07253e5ea613e99baf14..6528780a67813aae78f43b318ce72e7eaca3b6c1 100644 (file)
@@ -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;
 }
index 03bf32a73c265975ae58bc36a55264340e000f12..2eb1a0082ef7c7efe2610c420199a3677df6a918 100644 (file)
@@ -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>