]> git.ozlabs.org Git - ccan/blobdiff - ccan/failtest/failtest.c
failtest: don't assume FD_SETSIZE is maximum runtime fd.
[ccan] / ccan / failtest / failtest.c
index 97bcb62cb552d268cc4a6e93a447688715b76ae9..104628352dd580a61e22bb5ff0743b35e0267ad5 100644 (file)
@@ -14,6 +14,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/mman.h>
+#include <sys/resource.h>
 #include <signal.h>
 #include <assert.h>
 #include <ccan/time/time.h>
@@ -194,11 +195,21 @@ static struct failtest_call *add_history_(enum failtest_call_type type,
 static int move_fd_to_high(int fd)
 {
        int i;
+       struct rlimit lim;
+       int max;
 
-       for (i = FD_SETSIZE - 1; i >= 0; i--) {
+       if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
+               max = lim.rlim_cur;
+               printf("Max is %i\n", max);
+       } else
+               max = FD_SETSIZE;
+
+       for (i = max - 1; i > fd; i--) {
                if (fcntl(i, F_GETFL) == -1 && errno == EBADF) {
-                       if (dup2(fd, i) == -1)
-                               err(1, "Failed to dup fd %i to %i", fd, i);
+                       if (dup2(fd, i) == -1) {
+                               warn("Failed to dup fd %i to %i", fd, i);
+                               continue;
+                       }
                        close(fd);
                        return i;
                }
@@ -570,7 +581,7 @@ static void free_everything(void)
 {
        struct failtest_call *i;
 
-       while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
+       while ((i = tlist_top(&history, list)) != NULL)
                free_call(i);
 
        failtable_clear(&failtable);
@@ -755,7 +766,7 @@ static bool should_fail(struct failtest_call *call)
                        char *failpath;
                        struct failtest_call *c;
 
-                       c = tlist_tail(&history, struct failtest_call, list);
+                       c = tlist_tail(&history, list);
                        diff = time_sub(time_now(), start);
                        failpath = failpath_string();
                        p = strrchr(c->file, '/');
@@ -770,8 +781,7 @@ static bool should_fail(struct failtest_call *call)
                        free(failpath);
                }
                /* From here on, we have to clean up! */
-               our_history_start = tlist_tail(&history, struct failtest_call,
-                                              list);
+               our_history_start = tlist_tail(&history, list);
                close(control[0]);
                close(output[0]);
                /* Don't swallow stderr if we're tracing. */