]> git.ozlabs.org Git - ccan/blobdiff - ccan/failtest/failtest.c
failtest: fix failpath on open.
[ccan] / ccan / failtest / failtest.c
index be5234f7a90453ac8ec313a287b3dd4594667431..24c394aa993099024a804a8a1b71161ac9e015ee 100644 (file)
@@ -13,6 +13,7 @@
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/mman.h>
 #include <signal.h>
 #include <assert.h>
 #include <ccan/time/time.h>
@@ -96,7 +97,7 @@ static unsigned int lock_num = 0;
 
 static pid_t orig_pid;
 
-static const char info_to_arg[] = "mceoxprwf";
+static const char info_to_arg[] = "mceoxprwfa";
 
 /* Dummy call used for failtest_undo wrappers. */
 static struct failtest_call unrecorded_call;
@@ -487,10 +488,39 @@ static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
        }
 
        free_everything();
-       tell_parent(SUCCESS);
+       if (status == 0)
+               tell_parent(SUCCESS);
+       else
+               tell_parent(FAILURE);
        exit(status);
 }
 
+static bool following_path(void)
+{
+       if (!failpath)
+               return false;
+       /* + means continue after end, like normal. */
+       if (*failpath == '+') {
+               failpath = NULL;
+               return false;
+       }
+       return true;
+}
+
+static bool follow_path(struct failtest_call *call)
+{
+       if (*failpath == '\0') {
+               /* Continue, but don't inject errors. */
+               return call->fail = false;
+       }
+
+       if (tolower((unsigned char)*failpath) != info_to_arg[call->type])
+               errx(1, "Failpath expected '%s' got '%c'\n",
+                    failpath, info_to_arg[call->type]);
+       call->fail = cisupper(*(failpath++));
+       return call->fail;
+}
+
 static bool should_fail(struct failtest_call *call)
 {
        int status;
@@ -504,22 +534,8 @@ static bool should_fail(struct failtest_call *call)
        if (call == &unrecorded_call)
                return false;
 
-       if (failpath) {
-               /* + means continue after end, like normal. */
-               if (*failpath == '+')
-                       failpath = NULL;
-               else if (*failpath == '\0') {
-                       /* Continue, but don't inject errors. */
-                       return call->fail = false;
-               } else {
-                       if (tolower((unsigned char)*failpath)
-                           != info_to_arg[call->type])
-                               errx(1, "Failpath expected '%c' got '%c'\n",
-                                    info_to_arg[call->type], *failpath);
-                       call->fail = cisupper(*(failpath++));
-                       return call->fail;
-               }
-       }
+       if (following_path())
+               return follow_path(call);
 
        /* Attach debugger if they asked for it. */
        if (debugpath) {
@@ -846,6 +862,8 @@ int failtest_open(const char *pathname,
        p->u.open.ret = open(pathname, call.flags, call.mode);
 
        if (p->u.open.ret == -1) {
+               if (following_path())
+                       follow_path(p);
                p->fail = false;
                p->error = errno;
        } else if (should_fail(p)) {
@@ -860,6 +878,30 @@ int failtest_open(const char *pathname,
        return p->u.open.ret;
 }
 
+void *failtest_mmap(void *addr, size_t length, int prot, int flags,
+                   int fd, off_t offset, const char *file, unsigned line)
+{
+       struct failtest_call *p;
+       struct mmap_call call;
+
+       call.addr = addr;
+       call.length = length;
+       call.prot = prot;
+       call.flags = flags;
+       call.offset = offset;
+       call.fd = fd;
+
+       p = add_history(FAILTEST_MMAP, file, line, &call);
+       if (should_fail(p)) {
+               p->u.mmap.ret = MAP_FAILED;
+               p->error = ENOMEM;
+       } else {
+               p->u.mmap.ret = mmap(addr, length, prot, flags, fd, offset);
+       }
+       errno = p->error;
+       return p->u.mmap.ret;
+}
+
 static void cleanup_pipe(struct pipe_call *call)
 {
        if (!call->closed[0])
@@ -1062,10 +1104,11 @@ int failtest_close(int fd, const char *file, unsigned line)
        p = add_history(FAILTEST_CLOSE, file, line, &call);
        p->fail = false;
 
-       /* Consume close from failpath. */
-       if (failpath)
-               if (should_fail(p))
+       /* Consume close from failpath (shouldn't tell us to fail). */
+       if (following_path()) {
+               if (follow_path(p))
                        abort();
+       }
 
        if (fd < 0)
                return close(fd);