]> git.ozlabs.org Git - ccan/blobdiff - ccan/failtest/failtest.c
failtest: internally eliminate duplicate calls.
[ccan] / ccan / failtest / failtest.c
index f59ed03acf4abc70f48c8779fabf6684ab724444..ec41e92b46e4cc514bdecb54598686b0a5e9b716 100644 (file)
 #include <ccan/read_write_all/read_write_all.h>
 #include <ccan/failtest/failtest_proto.h>
 #include <ccan/build_assert/build_assert.h>
+#include <ccan/hash/hash.h>
+#include <ccan/htable/htable_type.h>
 #include <ccan/str/str.h>
 
 enum failtest_result (*failtest_hook)(struct tlist_calls *);
 
 static int tracefd = -1;
+static int warnfd;
 
 unsigned int failtest_timeout_ms = 20000;
 
@@ -45,12 +48,44 @@ struct lock_info {
        int type;
 };
 
+/* We hash the call location together with its backtrace. */
+static size_t hash_call(const struct failtest_call *call)
+{
+       return hash(call->file, strlen(call->file),
+                   hash(&call->line, 1,
+                        hash(call->backtrace, call->backtrace_num,
+                             call->type)));
+}
+
+static bool call_eq(const struct failtest_call *call1,
+                   const struct failtest_call *call2)
+{
+       unsigned int i;
+
+       if (strcmp(call1->file, call2->file) != 0
+           || call1->line != call2->line
+           || call1->type != call2->type
+           || call1->backtrace_num != call2->backtrace_num)
+               return false;
+
+       for (i = 0; i < call1->backtrace_num; i++)
+               if (call1->backtrace[i] != call2->backtrace[i])
+                       return false;
+
+       return true;
+}
+
+/* Defines struct failtable. */
+HTABLE_DEFINE_TYPE(struct failtest_call, (struct failtest_call *), hash_call,
+                  call_eq, failtable);
+
 bool (*failtest_exit_check)(struct tlist_calls *history);
 
 static struct tlist_calls history = TLIST_INIT(history);
 static int control_fd = -1;
 static struct timeval start;
-static unsigned int probe_count = 0;
+static bool probing = false;
+static struct failtable failtable;
 
 static struct write_call *child_writes = NULL;
 static unsigned int child_writes_num = 0;
@@ -66,6 +101,34 @@ static const char info_to_arg[] = "mceoxprwf";
 /* Dummy call used for failtest_undo wrappers. */
 static struct failtest_call unrecorded_call;
 
+#if HAVE_BACKTRACE
+#include <execinfo.h>
+
+static void **get_backtrace(unsigned int *num)
+{
+       static unsigned int max_back = 100;
+       void **ret;
+
+again:
+       ret = malloc(max_back * sizeof(void *));
+       *num = backtrace(ret, max_back);
+       if (*num == max_back) {
+               free(ret);
+               max_back *= 2;
+               goto again;
+       }
+       return ret;
+}
+#else
+/* This will test slightly less, since will consider all of the same
+ * calls as identical.  But, it's slightly faster! */
+static void **get_backtrace(unsigned int *num)
+{
+       *num = 0;
+       return NULL;
+}
+#endif /* HAVE_BACKTRACE */
+
 static struct failtest_call *add_history_(enum failtest_call_type type,
                                          const char *file,
                                          unsigned int line,
@@ -83,6 +146,7 @@ static struct failtest_call *add_history_(enum failtest_call_type type,
        call->file = file;
        call->line = line;
        call->cleanup = NULL;
+       call->backtrace = get_backtrace(&call->backtrace_num);
        memcpy(&call->u, elem, elem_size);
        tlist_add_tail(&history, call, list);
        return call;
@@ -95,6 +159,24 @@ static struct failtest_call *add_history_(enum failtest_call_type type,
 #define set_cleanup(call, clean, type)                 \
        (call)->cleanup = (void *)((void)sizeof(clean((type *)NULL),1), (clean))
 
+
+/* Dup the fd to a high value (out of the way I hope!), and close the old fd. */
+static int move_fd_to_high(int fd)
+{
+       int i;
+
+       for (i = FD_SETSIZE - 1; i >= 0; 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);
+                       close(fd);
+                       return i;
+               }
+       }
+       /* Nothing?  Really?  Er... ok? */
+       return fd;
+}
+
 static bool read_write_info(int fd)
 {
        struct write_call *w;
@@ -132,6 +214,37 @@ static char *failpath_string(void)
        return ret;
 }
 
+static void warn_via_fd(int e, const char *fmt, va_list ap)
+{
+       char *p = failpath_string();
+
+       vdprintf(warnfd, fmt, ap);
+       if (e != -1)
+               dprintf(warnfd, ": %s", strerror(e));
+       dprintf(warnfd, " [%s]\n", p);
+       free(p);
+}
+
+static void fwarn(const char *fmt, ...)
+{
+       va_list ap;
+       int e = errno;
+
+       va_start(ap, fmt);
+       warn_via_fd(e, fmt, ap);
+       va_end(ap);
+}
+
+
+static void fwarnx(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       warn_via_fd(-1, fmt, ap);
+       va_end(ap);
+}
+
 static void tell_parent(enum info_type type)
 {
        if (control_fd != -1)
@@ -331,6 +444,7 @@ static void free_call(struct failtest_call *call)
        /* We don't do this in cleanup: needed even for failed opens. */
        if (call->type == FAILTEST_OPEN)
                free((char *)call->u.open.pathname);
+       free(call->backtrace);
        tlist_del_from(&history, call, list);
        free(call);
 }
@@ -342,6 +456,8 @@ static void free_everything(void)
 
        while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
                free_call(i);
+
+       failtable_clear(&failtable);
 }
 
 static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
@@ -383,10 +499,7 @@ static bool should_fail(struct failtest_call *call)
        char *out = NULL;
        size_t outlen = 0;
        struct saved_file *files;
-
-       /* Are we probing? */
-       if (probe_count && --probe_count == 0 && control_fd != -1)
-               failtest_cleanup(true, 0);
+       struct failtest_call *dup;
 
        if (call == &unrecorded_call)
                return false;
@@ -428,23 +541,22 @@ static bool should_fail(struct failtest_call *call)
                free(path);
        }
 
+       /* Are we probing?  If so, we never fail twice. */
+       if (probing)
+               return call->fail = false;
+
+       /* Don't more than once in the same place. */
+       dup = failtable_get(&failtable, call);
+       if (dup)
+               return call->fail = false;
+
        if (failtest_hook) {
                switch (failtest_hook(&history)) {
                case FAIL_OK:
                        break;
                case FAIL_PROBE:
-                       /* Already down probe path?  Stop now. */
-                       if (!probe_count) {
-                               /* FIXME: We should run *parent* and
-                                * run probe until calls match up again. */
-                               probe_count = 3;
-                               break;
-                       } else {
-                               /* Child should give up now. */
-                               if (control_fd != -1)
-                                       failtest_cleanup(true, 0);
-                               /* Parent, don't fail again. */
-                       }
+                       probing = true;
+                       break;
                case FAIL_DONT_FAIL:
                        call->fail = false;
                        return false;
@@ -453,6 +565,9 @@ static bool should_fail(struct failtest_call *call)
                }
        }
 
+       /* Add it to our table of calls. */
+       failtable_add(&failtable, call);
+
        files = save_files();
 
        /* We're going to fail in the child. */
@@ -493,7 +608,7 @@ static bool should_fail(struct failtest_call *call)
                dup2(output[1], STDERR_FILENO);
                if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
                        close(output[1]);
-               control_fd = control[1];
+               control_fd = move_fd_to_high(control[1]);
                /* Valgrind spots the leak if we don't free these. */
                free_files(files);
                return true;
@@ -574,6 +689,9 @@ static bool should_fail(struct failtest_call *call)
 
        restore_files(files);
 
+       /* Only child does probe. */
+       probing = false;
+
        /* We continue onwards without failing. */
        call->fail = false;
        return false;
@@ -1067,18 +1185,20 @@ void failtest_init(int argc, char *argv[])
        unsigned int i;
 
        orig_pid = getpid();
-               
+
+       warnfd = move_fd_to_high(dup(STDERR_FILENO));
        for (i = 1; i < argc; i++) {
                if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) {
                        failpath = argv[i] + strlen("--failpath=");
                } else if (strcmp(argv[i], "--tracepath") == 0) {
-                       tracefd = dup(STDERR_FILENO);
+                       tracefd = warnfd;
                        failtest_timeout_ms = -1;
                } else if (!strncmp(argv[i], "--debugpath=",
                                    strlen("--debugpath="))) {
                        debugpath = argv[i] + strlen("--debugpath=");
                }
        }
+       failtable_init(&failtable);
        start = time_now();
 }