1 /* Licensed under LGPL - see LICENSE file for details */
2 #include <ccan/failtest/failtest.h>
12 #include <sys/types.h>
18 #include <ccan/time/time.h>
19 #include <ccan/read_write_all/read_write_all.h>
20 #include <ccan/failtest/failtest_proto.h>
21 #include <ccan/build_assert/build_assert.h>
22 #include <ccan/hash/hash.h>
23 #include <ccan/htable/htable_type.h>
24 #include <ccan/str/str.h>
26 enum failtest_result (*failtest_hook)(struct tlist_calls *);
28 static int tracefd = -1;
31 unsigned int failtest_timeout_ms = 20000;
34 const char *debugpath;
46 /* end is inclusive: you can't have a 0-byte lock. */
51 /* We hash the call location together with its backtrace. */
52 static size_t hash_call(const struct failtest_call *call)
54 return hash(call->file, strlen(call->file),
56 hash(call->backtrace, call->backtrace_num,
60 static bool call_eq(const struct failtest_call *call1,
61 const struct failtest_call *call2)
65 if (strcmp(call1->file, call2->file) != 0
66 || call1->line != call2->line
67 || call1->type != call2->type
68 || call1->backtrace_num != call2->backtrace_num)
71 for (i = 0; i < call1->backtrace_num; i++)
72 if (call1->backtrace[i] != call2->backtrace[i])
78 /* Defines struct failtable. */
79 HTABLE_DEFINE_TYPE(struct failtest_call, (struct failtest_call *), hash_call,
82 bool (*failtest_exit_check)(struct tlist_calls *history);
84 static struct tlist_calls history = TLIST_INIT(history);
85 static int control_fd = -1;
86 static struct timeval start;
87 static bool probing = false;
88 static struct failtable failtable;
90 static struct write_call *child_writes = NULL;
91 static unsigned int child_writes_num = 0;
93 static pid_t lock_owner;
94 static struct lock_info *locks = NULL;
95 static unsigned int lock_num = 0;
97 static pid_t orig_pid;
99 static const char info_to_arg[] = "mceoxprwf";
101 /* Dummy call used for failtest_undo wrappers. */
102 static struct failtest_call unrecorded_call;
105 #include <execinfo.h>
107 static void **get_backtrace(unsigned int *num)
109 static unsigned int max_back = 100;
113 ret = malloc(max_back * sizeof(void *));
114 *num = backtrace(ret, max_back);
115 if (*num == max_back) {
123 /* This will test slightly less, since will consider all of the same
124 * calls as identical. But, it's slightly faster! */
125 static void **get_backtrace(unsigned int *num)
130 #endif /* HAVE_BACKTRACE */
132 static struct failtest_call *add_history_(enum failtest_call_type type,
138 struct failtest_call *call;
140 /* NULL file is how we suppress failure. */
142 return &unrecorded_call;
144 call = malloc(sizeof *call);
148 call->cleanup = NULL;
149 call->backtrace = get_backtrace(&call->backtrace_num);
150 memcpy(&call->u, elem, elem_size);
151 tlist_add_tail(&history, call, list);
155 #define add_history(type, file, line, elem) \
156 add_history_((type), (file), (line), (elem), sizeof(*(elem)))
158 /* We do a fake call inside a sizeof(), to check types. */
159 #define set_cleanup(call, clean, type) \
160 (call)->cleanup = (void *)((void)sizeof(clean((type *)NULL),1), (clean))
163 /* Dup the fd to a high value (out of the way I hope!), and close the old fd. */
164 static int move_fd_to_high(int fd)
168 for (i = FD_SETSIZE - 1; i >= 0; i--) {
169 if (fcntl(i, F_GETFL) == -1 && errno == EBADF) {
170 if (dup2(fd, i) == -1)
171 err(1, "Failed to dup fd %i to %i", fd, i);
176 /* Nothing? Really? Er... ok? */
180 static bool read_write_info(int fd)
182 struct write_call *w;
185 /* We don't need all of this, but it's simple. */
186 child_writes = realloc(child_writes,
187 (child_writes_num+1) * sizeof(child_writes[0]));
188 w = &child_writes[child_writes_num];
189 if (!read_all(fd, w, sizeof(*w)))
192 w->buf = buf = malloc(w->count);
193 if (!read_all(fd, buf, w->count))
200 static char *failpath_string(void)
202 struct failtest_call *i;
203 char *ret = strdup("");
206 /* Inefficient, but who cares? */
207 tlist_for_each(&history, i, list) {
208 ret = realloc(ret, len + 2);
209 ret[len] = info_to_arg[i->type];
211 ret[len] = toupper(ret[len]);
217 static void warn_via_fd(int e, const char *fmt, va_list ap)
219 char *p = failpath_string();
221 vdprintf(warnfd, fmt, ap);
223 dprintf(warnfd, ": %s", strerror(e));
224 dprintf(warnfd, " [%s]\n", p);
228 static void fwarn(const char *fmt, ...)
234 warn_via_fd(e, fmt, ap);
239 static void fwarnx(const char *fmt, ...)
244 warn_via_fd(-1, fmt, ap);
248 static void tell_parent(enum info_type type)
250 if (control_fd != -1)
251 write_all(control_fd, &type, sizeof(type));
254 static void child_fail(const char *out, size_t outlen, const char *fmt, ...)
257 char *path = failpath_string();
260 vfprintf(stderr, fmt, ap);
263 fprintf(stderr, "%.*s", (int)outlen, out);
264 printf("To reproduce: --failpath=%s\n", path);
266 tell_parent(FAILURE);
270 static void trace(const char *fmt, ...)
278 vdprintf(tracefd, fmt, ap);
284 static void hand_down(int signum)
289 static void release_locks(void)
291 /* Locks were never acquired/reacquired? */
295 /* We own them? Release them all. */
296 if (lock_owner == getpid()) {
300 fl.l_whence = SEEK_SET;
304 for (i = 0; i < lock_num; i++)
305 fcntl(locks[i].fd, F_SETLK, &fl);
307 /* Our parent must have them; pass request up. */
308 enum info_type type = RELEASE_LOCKS;
309 assert(control_fd != -1);
310 write_all(control_fd, &type, sizeof(type));
315 /* off_t is a signed type. Getting its max is non-trivial. */
316 static off_t off_max(void)
318 BUILD_ASSERT(sizeof(off_t) == 4 || sizeof(off_t) == 8);
319 if (sizeof(off_t) == 4)
320 return (off_t)0x7FFFFFF;
322 return (off_t)0x7FFFFFFFFFFFFFFULL;
325 static void get_locks(void)
330 if (lock_owner == getpid())
333 if (lock_owner != 0) {
334 enum info_type type = RELEASE_LOCKS;
335 assert(control_fd != -1);
336 write_all(control_fd, &type, sizeof(type));
339 fl.l_whence = SEEK_SET;
341 for (i = 0; i < lock_num; i++) {
342 fl.l_type = locks[i].type;
343 fl.l_start = locks[i].start;
344 if (locks[i].end == off_max())
347 fl.l_len = locks[i].end - locks[i].start + 1;
349 if (fcntl(locks[i].fd, F_SETLKW, &fl) != 0)
352 lock_owner = getpid();
356 struct saved_file *next;
362 static struct saved_file *save_file(struct saved_file *next, int fd)
364 struct saved_file *s = malloc(sizeof(*s));
368 s->off = lseek(fd, 0, SEEK_CUR);
369 /* Special file? Erk... */
370 assert(s->off != -1);
371 s->len = lseek(fd, 0, SEEK_END);
372 lseek(fd, 0, SEEK_SET);
373 s->contents = malloc(s->len);
374 if (read(fd, s->contents, s->len) != s->len)
375 err(1, "Failed to save %zu bytes", (size_t)s->len);
376 lseek(fd, s->off, SEEK_SET);
380 /* We have little choice but to save and restore open files: mmap means we
381 * can really intercept changes in the child.
383 * We could do non-mmap'ed files on demand, however. */
384 static struct saved_file *save_files(void)
386 struct saved_file *files = NULL;
387 struct failtest_call *i;
389 /* Figure out the set of live fds. */
390 tlist_for_each_rev(&history, i, list) {
391 if (i->type == FAILTEST_OPEN) {
392 int fd = i->u.open.ret;
393 /* Only do successful, writable fds. */
397 /* If it was closed, cleanup == NULL. */
401 if ((i->u.open.flags & O_RDWR) == O_RDWR) {
402 files = save_file(files, fd);
403 } else if ((i->u.open.flags & O_WRONLY)
405 /* FIXME: Handle O_WRONLY. Open with O_RDWR? */
414 static void restore_files(struct saved_file *s)
417 struct saved_file *next = s->next;
419 lseek(s->fd, 0, SEEK_SET);
420 if (write(s->fd, s->contents, s->len) != s->len)
421 err(1, "Failed to restore %zu bytes", (size_t)s->len);
422 if (ftruncate(s->fd, s->len) != 0)
423 err(1, "Failed to trim file to length %zu",
426 lseek(s->fd, s->off, SEEK_SET);
432 static void free_files(struct saved_file *s)
435 struct saved_file *next = s->next;
442 static void free_call(struct failtest_call *call)
444 /* We don't do this in cleanup: needed even for failed opens. */
445 if (call->type == FAILTEST_OPEN)
446 free((char *)call->u.open.pathname);
447 free(call->backtrace);
448 tlist_del_from(&history, call, list);
452 /* Free up memory, so valgrind doesn't report leaks. */
453 static void free_everything(void)
455 struct failtest_call *i;
457 while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
460 failtable_clear(&failtable);
463 static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
465 struct failtest_call *i;
467 /* For children, we don't care if they "failed" the testing. */
468 if (control_fd != -1)
471 if (forced_cleanup) {
472 /* We didn't actually do final operation: remove it. */
473 i = tlist_tail(&history, struct failtest_call, list);
477 /* Cleanup everything, in reverse order. */
478 tlist_for_each_rev(&history, i, list) {
481 if (!forced_cleanup) {
482 printf("Leak at %s:%u: --failpath=%s\n",
483 i->file, i->line, failpath_string());
490 tell_parent(SUCCESS);
494 static bool should_fail(struct failtest_call *call)
497 int control[2], output[2];
498 enum info_type type = UNEXPECTED;
501 struct saved_file *files;
502 struct failtest_call *dup;
504 if (call == &unrecorded_call)
508 /* + means continue after end, like normal. */
509 if (*failpath == '+')
511 else if (*failpath == '\0') {
512 /* Continue, but don't inject errors. */
513 return call->fail = false;
515 if (tolower((unsigned char)*failpath)
516 != info_to_arg[call->type])
517 errx(1, "Failpath expected '%c' got '%c'\n",
518 info_to_arg[call->type], *failpath);
519 call->fail = cisupper(*(failpath++));
524 /* Attach debugger if they asked for it. */
526 char *path = failpath_string();
528 if (streq(path, debugpath)) {
532 signal(SIGUSR1, SIG_IGN);
533 sprintf(str, "xterm -e gdb /proc/%d/exe %d &",
535 if (system(str) == 0)
537 } else if (!strstarts(path, debugpath)) {
538 fprintf(stderr, "--debugpath not followed: %s\n", path);
544 /* Are we probing? If so, we never fail twice. */
546 return call->fail = false;
548 /* Don't more than once in the same place. */
549 dup = failtable_get(&failtable, call);
551 return call->fail = false;
554 switch (failtest_hook(&history)) {
568 /* Add it to our table of calls. */
569 failtable_add(&failtable, call);
571 files = save_files();
573 /* We're going to fail in the child. */
575 if (pipe(control) != 0 || pipe(output) != 0)
576 err(1, "opening pipe");
578 /* Prevent double-printing (in child and parent) */
582 err(1, "forking failed");
589 struct failtest_call *c;
591 c = tlist_tail(&history, struct failtest_call, list);
592 diff = time_sub(time_now(), start);
593 failpath = failpath_string();
594 trace("%u->%u (%u.%02u): %s (", getppid(), getpid(),
595 (int)diff.tv_sec, (int)diff.tv_usec / 10000,
598 p = strrchr(c->file, '/');
602 trace("%s", c->file);
603 trace(":%u)\n", c->line);
607 dup2(output[1], STDOUT_FILENO);
608 dup2(output[1], STDERR_FILENO);
609 if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
611 control_fd = move_fd_to_high(control[1]);
612 /* Valgrind spots the leak if we don't free these. */
617 signal(SIGUSR1, hand_down);
622 /* We grab output so we can display it; we grab writes so we
625 struct pollfd pfd[2];
628 pfd[0].fd = output[0];
629 pfd[0].events = POLLIN|POLLHUP;
630 pfd[1].fd = control[0];
631 pfd[1].events = POLLIN|POLLHUP;
634 ret = poll(pfd, 1, failtest_timeout_ms);
636 ret = poll(pfd, 2, failtest_timeout_ms);
643 err(1, "Poll returned %i", ret);
646 if (pfd[0].revents & POLLIN) {
649 out = realloc(out, outlen + 8192);
650 len = read(output[0], out + outlen, 8192);
652 } else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
653 if (read_all(control[0], &type, sizeof(type))) {
655 if (!read_write_info(control[0]))
657 } else if (type == RELEASE_LOCKS) {
659 /* FIXME: Tell them we're done... */
662 } else if (pfd[0].revents & POLLHUP) {
665 } while (type != FAILURE);
669 waitpid(child, &status, 0);
670 if (!WIFEXITED(status)) {
671 if (WTERMSIG(status) == SIGUSR1)
672 child_fail(out, outlen, "Timed out");
674 child_fail(out, outlen, "Killed by signal %u: ",
677 /* Child printed failure already, just pass up exit code. */
678 if (type == FAILURE) {
679 fprintf(stderr, "%.*s", (int)outlen, out);
681 exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
683 if (WEXITSTATUS(status) != 0)
684 child_fail(out, outlen, "Exited with status %i: ",
685 WEXITSTATUS(status));
688 signal(SIGUSR1, SIG_DFL);
690 restore_files(files);
692 /* Only child does probe. */
695 /* We continue onwards without failing. */
700 static void cleanup_calloc(struct calloc_call *call)
705 void *failtest_calloc(size_t nmemb, size_t size,
706 const char *file, unsigned line)
708 struct failtest_call *p;
709 struct calloc_call call;
712 p = add_history(FAILTEST_CALLOC, file, line, &call);
714 if (should_fail(p)) {
715 p->u.calloc.ret = NULL;
718 p->u.calloc.ret = calloc(nmemb, size);
719 set_cleanup(p, cleanup_calloc, struct calloc_call);
722 return p->u.calloc.ret;
725 static void cleanup_malloc(struct malloc_call *call)
730 void *failtest_malloc(size_t size, const char *file, unsigned line)
732 struct failtest_call *p;
733 struct malloc_call call;
736 p = add_history(FAILTEST_MALLOC, file, line, &call);
737 if (should_fail(p)) {
738 p->u.malloc.ret = NULL;
741 p->u.malloc.ret = malloc(size);
742 set_cleanup(p, cleanup_malloc, struct malloc_call);
745 return p->u.malloc.ret;
748 static void cleanup_realloc(struct realloc_call *call)
753 /* Walk back and find out if we got this ptr from a previous routine. */
754 static void fixup_ptr_history(void *ptr)
756 struct failtest_call *i;
758 /* Start at end of history, work back. */
759 tlist_for_each_rev(&history, i, list) {
761 case FAILTEST_REALLOC:
762 if (i->u.realloc.ret == ptr) {
767 case FAILTEST_MALLOC:
768 if (i->u.malloc.ret == ptr) {
773 case FAILTEST_CALLOC:
774 if (i->u.calloc.ret == ptr) {
785 void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
787 struct failtest_call *p;
788 struct realloc_call call;
790 p = add_history(FAILTEST_REALLOC, file, line, &call);
792 /* FIXME: Try one child moving allocation, one not. */
793 if (should_fail(p)) {
794 p->u.realloc.ret = NULL;
797 /* Don't catch this one in the history fixup... */
798 p->u.realloc.ret = NULL;
799 fixup_ptr_history(ptr);
800 p->u.realloc.ret = realloc(ptr, size);
801 set_cleanup(p, cleanup_realloc, struct realloc_call);
804 return p->u.realloc.ret;
807 void failtest_free(void *ptr)
809 fixup_ptr_history(ptr);
813 static void cleanup_open(struct open_call *call)
818 int failtest_open(const char *pathname,
819 const char *file, unsigned line, ...)
821 struct failtest_call *p;
822 struct open_call call;
825 call.pathname = strdup(pathname);
827 call.flags = va_arg(ap, int);
828 if (call.flags & O_CREAT) {
829 call.mode = va_arg(ap, int);
832 p = add_history(FAILTEST_OPEN, file, line, &call);
833 /* Avoid memory leak! */
834 if (p == &unrecorded_call)
835 free((char *)call.pathname);
836 p->u.open.ret = open(pathname, call.flags, call.mode);
838 if (p->u.open.ret == -1) {
841 } else if (should_fail(p)) {
842 close(p->u.open.ret);
844 /* FIXME: Play with error codes? */
847 set_cleanup(p, cleanup_open, struct open_call);
850 return p->u.open.ret;
853 static void cleanup_pipe(struct pipe_call *call)
855 if (!call->closed[0])
857 if (!call->closed[1])
861 int failtest_pipe(int pipefd[2], const char *file, unsigned line)
863 struct failtest_call *p;
864 struct pipe_call call;
866 p = add_history(FAILTEST_PIPE, file, line, &call);
867 if (should_fail(p)) {
869 /* FIXME: Play with error codes? */
872 p->u.pipe.ret = pipe(p->u.pipe.fds);
873 p->u.pipe.closed[0] = p->u.pipe.closed[1] = false;
874 set_cleanup(p, cleanup_pipe, struct pipe_call);
876 /* This causes valgrind to notice if they use pipefd[] after failure */
877 memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
879 return p->u.pipe.ret;
882 ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
883 const char *file, unsigned line)
885 struct failtest_call *p;
886 struct read_call call;
891 p = add_history(FAILTEST_READ, file, line, &call);
893 /* FIXME: Try partial read returns. */
894 if (should_fail(p)) {
898 p->u.read.ret = pread(fd, buf, count, off);
901 return p->u.read.ret;
904 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t off,
905 const char *file, unsigned line)
907 struct failtest_call *p;
908 struct write_call call;
914 p = add_history(FAILTEST_WRITE, file, line, &call);
916 /* If we're a child, we need to make sure we write the same thing
917 * to non-files as the parent does, so tell it. */
918 if (control_fd != -1 && off == (off_t)-1) {
919 enum info_type type = WRITE;
921 write_all(control_fd, &type, sizeof(type));
922 write_all(control_fd, &p->u.write, sizeof(p->u.write));
923 write_all(control_fd, buf, count);
926 /* FIXME: Try partial write returns. */
927 if (should_fail(p)) {
931 /* FIXME: We assume same write order in parent and child */
932 if (off == (off_t)-1 && child_writes_num != 0) {
933 if (child_writes[0].fd != fd)
934 errx(1, "Child wrote to fd %u, not %u?",
935 child_writes[0].fd, fd);
936 if (child_writes[0].off != p->u.write.off)
937 errx(1, "Child wrote to offset %zu, not %zu?",
938 (size_t)child_writes[0].off,
939 (size_t)p->u.write.off);
940 if (child_writes[0].count != count)
941 errx(1, "Child wrote length %zu, not %zu?",
942 child_writes[0].count, count);
943 if (memcmp(child_writes[0].buf, buf, count)) {
945 "Child wrote differently to"
946 " fd %u than we did!\n", fd);
948 free((char *)child_writes[0].buf);
950 memmove(&child_writes[0], &child_writes[1],
951 sizeof(child_writes[0]) * child_writes_num);
953 /* Is this is a socket or pipe, child wrote it
955 if (p->u.write.off == (off_t)-1) {
956 p->u.write.ret = count;
958 return p->u.write.ret;
961 p->u.write.ret = pwrite(fd, buf, count, off);
964 return p->u.write.ret;
967 ssize_t failtest_read(int fd, void *buf, size_t count,
968 const char *file, unsigned line)
970 return failtest_pread(fd, buf, count, lseek(fd, 0, SEEK_CUR),
974 ssize_t failtest_write(int fd, const void *buf, size_t count,
975 const char *file, unsigned line)
977 return failtest_pwrite(fd, buf, count, lseek(fd, 0, SEEK_CUR),
981 static struct lock_info *WARN_UNUSED_RESULT
982 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
987 for (i = 0; i < lock_num; i++) {
992 /* Four cases we care about:
1006 if (start > l->start && end < l->end) {
1007 /* Mid overlap: trim entry, add new one. */
1008 off_t new_start, new_end;
1009 new_start = end + 1;
1012 locks = add_lock(locks,
1013 fd, new_start, new_end, l->type);
1015 } else if (start <= l->start && end >= l->end) {
1016 /* Total overlap: eliminate entry. */
1019 } else if (end >= l->start && end < l->end) {
1020 /* Start overlap: trim entry. */
1022 } else if (start > l->start && start <= l->end) {
1023 /* End overlap: trim entry. */
1026 /* Nothing left? Remove it. */
1027 if (l->end < l->start) {
1028 memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
1033 if (type != F_UNLCK) {
1034 locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
1035 l = &locks[lock_num++];
1044 /* We trap this so we can record it: we don't fail it. */
1045 int failtest_close(int fd, const char *file, unsigned line)
1047 struct failtest_call *i;
1048 struct close_call call;
1049 struct failtest_call *p;
1052 p = add_history(FAILTEST_CLOSE, file, line, &call);
1055 /* Consume close from failpath. */
1063 /* Trace history to find source of fd. */
1064 tlist_for_each_rev(&history, i, list) {
1068 if (i->u.pipe.fds[0] == fd) {
1069 assert(!i->u.pipe.closed[0]);
1070 i->u.pipe.closed[0] = true;
1071 if (i->u.pipe.closed[1])
1075 if (i->u.pipe.fds[1] == fd) {
1076 assert(!i->u.pipe.closed[1]);
1077 i->u.pipe.closed[1] = true;
1078 if (i->u.pipe.closed[0])
1084 if (i->u.open.ret == fd) {
1085 assert((void *)i->cleanup
1086 == (void *)cleanup_open);
1097 locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
1101 /* Zero length means "to end of file" */
1102 static off_t end_of(off_t start, off_t len)
1106 return start + len - 1;
1109 /* FIXME: This only handles locks, really. */
1110 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
1112 struct failtest_call *p;
1113 struct fcntl_call call;
1119 /* Argument extraction. */
1124 call.arg.l = va_arg(ap, long);
1126 return fcntl(fd, cmd, call.arg.l);
1129 return fcntl(fd, cmd);
1133 call.arg.fl = *va_arg(ap, struct flock *);
1135 return fcntl(fd, cmd, &call.arg.fl);
1139 call.arg.fl = *va_arg(ap, struct flock *);
1143 /* This means you need to implement it here. */
1144 err(1, "failtest: unknown fcntl %u", cmd);
1147 p = add_history(FAILTEST_FCNTL, file, line, &call);
1149 if (should_fail(p)) {
1150 p->u.fcntl.ret = -1;
1151 if (p->u.fcntl.cmd == F_SETLK)
1157 p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
1158 &p->u.fcntl.arg.fl);
1159 if (p->u.fcntl.ret == -1)
1162 /* We don't handle anything else yet. */
1163 assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
1164 locks = add_lock(locks,
1166 p->u.fcntl.arg.fl.l_start,
1167 end_of(p->u.fcntl.arg.fl.l_start,
1168 p->u.fcntl.arg.fl.l_len),
1169 p->u.fcntl.arg.fl.l_type);
1173 return p->u.fcntl.ret;
1176 pid_t failtest_getpid(const char *file, unsigned line)
1178 /* You must call failtest_init first! */
1183 void failtest_init(int argc, char *argv[])
1187 orig_pid = getpid();
1189 warnfd = move_fd_to_high(dup(STDERR_FILENO));
1190 for (i = 1; i < argc; i++) {
1191 if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) {
1192 failpath = argv[i] + strlen("--failpath=");
1193 } else if (strcmp(argv[i], "--tracepath") == 0) {
1195 failtest_timeout_ms = -1;
1196 } else if (!strncmp(argv[i], "--debugpath=",
1197 strlen("--debugpath="))) {
1198 debugpath = argv[i] + strlen("--debugpath=");
1201 failtable_init(&failtable);
1205 bool failtest_has_failed(void)
1207 return control_fd != -1;
1210 void failtest_exit(int status)
1212 if (failtest_exit_check) {
1213 if (!failtest_exit_check(&history))
1214 child_fail(NULL, 0, "failtest_exit_check failed\n");
1217 failtest_cleanup(false, status);