]> git.ozlabs.org Git - ccan/blobdiff - ccan/failtest/failtest.c
failtest: fix locking code.
[ccan] / ccan / failtest / failtest.c
index 9972db3cd29fc8ad83a9d3c51bc8fedf65ef4a76..e2e6c64fec64d188fed1b92184fbf64ad9951fd1 100644 (file)
@@ -219,7 +219,7 @@ static void trace_str(const char *str)
 {
        ssize_t ret;
 
-       while ((ret = write(tracefd, str, strlen(str))) <= 0) {
+       while ((ret = write(tracefd, str, strlen(str))) > 0) {
                str += ret;
                if (!*str)
                        return;
@@ -317,21 +317,28 @@ static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
 {
        int i;
 
+       /* For children, we don't care if they "failed" the testing. */
+       if (control_fd != -1)
+               status = 0;
+
        if (forced_cleanup)
                history_num--;
 
        /* Cleanup everything, in reverse order. */
-       for (i = history_num - 1; i >= 0; i--)
-               if (history[i].cleanup)
-                       history[i].cleanup(&history[i].u);
+       for (i = history_num - 1; i >= 0; i--) {
+               if (!history[i].cleanup)
+                       continue;
+               if (!forced_cleanup) {
+                       printf("Leak at %s:%u\n",
+                              history[i].file, history[i].line);
+                       status = 1;
+               }
+               history[i].cleanup(&history[i].u);
+       }
 
        free_everything();
-
-       if (control_fd == -1)
-               exit(status);
-
        tell_parent(SUCCESS);
-       exit(0);
+       exit(status);
 }
 
 static bool should_fail(struct failtest_call *call)
@@ -976,7 +983,6 @@ int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
        }
 
        p = add_history(FAILTEST_FCNTL, file, line, &call);
-       get_locks();
 
        if (should_fail(p)) {
                p->u.fcntl.ret = -1;
@@ -985,6 +991,7 @@ int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
                else
                        p->error = EDEADLK;
        } else {
+               get_locks();
                p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
                                       &p->u.fcntl.arg.fl);
                if (p->u.fcntl.ret == -1)