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. */
528 /* Pretend this last call matches whatever path wanted:
529 * keeps valgrind happy. */
530 call->fail = cisupper(debugpath[strlen(debugpath)-1]);
531 path = failpath_string();
533 if (streq(path, debugpath)) {
537 signal(SIGUSR1, SIG_IGN);
538 sprintf(str, "xterm -e gdb /proc/%d/exe %d &",
540 if (system(str) == 0)
543 /* Ignore last character: could be upper or lower. */
544 path[strlen(path)-1] = '\0';
545 if (!strstarts(debugpath, path)) {
547 "--debugpath not followed: %s\n", path);
554 /* Are we probing? If so, we never fail twice. */
556 return call->fail = false;
558 /* Don't more than once in the same place. */
559 dup = failtable_get(&failtable, call);
561 return call->fail = false;
564 switch (failtest_hook(&history)) {
578 /* Add it to our table of calls. */
579 failtable_add(&failtable, call);
581 files = save_files();
583 /* We're going to fail in the child. */
585 if (pipe(control) != 0 || pipe(output) != 0)
586 err(1, "opening pipe");
588 /* Prevent double-printing (in child and parent) */
592 err(1, "forking failed");
599 struct failtest_call *c;
601 c = tlist_tail(&history, struct failtest_call, list);
602 diff = time_sub(time_now(), start);
603 failpath = failpath_string();
604 trace("%u->%u (%u.%02u): %s (", getppid(), getpid(),
605 (int)diff.tv_sec, (int)diff.tv_usec / 10000,
608 p = strrchr(c->file, '/');
612 trace("%s", c->file);
613 trace(":%u)\n", c->line);
617 dup2(output[1], STDOUT_FILENO);
618 dup2(output[1], STDERR_FILENO);
619 if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
621 control_fd = move_fd_to_high(control[1]);
622 /* Valgrind spots the leak if we don't free these. */
627 signal(SIGUSR1, hand_down);
632 /* We grab output so we can display it; we grab writes so we
635 struct pollfd pfd[2];
638 pfd[0].fd = output[0];
639 pfd[0].events = POLLIN|POLLHUP;
640 pfd[1].fd = control[0];
641 pfd[1].events = POLLIN|POLLHUP;
644 ret = poll(pfd, 1, failtest_timeout_ms);
646 ret = poll(pfd, 2, failtest_timeout_ms);
653 err(1, "Poll returned %i", ret);
656 if (pfd[0].revents & POLLIN) {
659 out = realloc(out, outlen + 8192);
660 len = read(output[0], out + outlen, 8192);
662 } else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
663 if (read_all(control[0], &type, sizeof(type))) {
665 if (!read_write_info(control[0]))
667 } else if (type == RELEASE_LOCKS) {
669 /* FIXME: Tell them we're done... */
672 } else if (pfd[0].revents & POLLHUP) {
675 } while (type != FAILURE);
679 waitpid(child, &status, 0);
680 if (!WIFEXITED(status)) {
681 if (WTERMSIG(status) == SIGUSR1)
682 child_fail(out, outlen, "Timed out");
684 child_fail(out, outlen, "Killed by signal %u: ",
687 /* Child printed failure already, just pass up exit code. */
688 if (type == FAILURE) {
689 fprintf(stderr, "%.*s", (int)outlen, out);
691 exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
693 if (WEXITSTATUS(status) != 0)
694 child_fail(out, outlen, "Exited with status %i: ",
695 WEXITSTATUS(status));
698 signal(SIGUSR1, SIG_DFL);
700 restore_files(files);
702 /* Only child does probe. */
705 /* We continue onwards without failing. */
710 static void cleanup_calloc(struct calloc_call *call)
715 void *failtest_calloc(size_t nmemb, size_t size,
716 const char *file, unsigned line)
718 struct failtest_call *p;
719 struct calloc_call call;
722 p = add_history(FAILTEST_CALLOC, file, line, &call);
724 if (should_fail(p)) {
725 p->u.calloc.ret = NULL;
728 p->u.calloc.ret = calloc(nmemb, size);
729 set_cleanup(p, cleanup_calloc, struct calloc_call);
732 return p->u.calloc.ret;
735 static void cleanup_malloc(struct malloc_call *call)
740 void *failtest_malloc(size_t size, const char *file, unsigned line)
742 struct failtest_call *p;
743 struct malloc_call call;
746 p = add_history(FAILTEST_MALLOC, file, line, &call);
747 if (should_fail(p)) {
748 p->u.malloc.ret = NULL;
751 p->u.malloc.ret = malloc(size);
752 set_cleanup(p, cleanup_malloc, struct malloc_call);
755 return p->u.malloc.ret;
758 static void cleanup_realloc(struct realloc_call *call)
763 /* Walk back and find out if we got this ptr from a previous routine. */
764 static void fixup_ptr_history(void *ptr)
766 struct failtest_call *i;
768 /* Start at end of history, work back. */
769 tlist_for_each_rev(&history, i, list) {
771 case FAILTEST_REALLOC:
772 if (i->u.realloc.ret == ptr) {
777 case FAILTEST_MALLOC:
778 if (i->u.malloc.ret == ptr) {
783 case FAILTEST_CALLOC:
784 if (i->u.calloc.ret == ptr) {
795 void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
797 struct failtest_call *p;
798 struct realloc_call call;
800 p = add_history(FAILTEST_REALLOC, file, line, &call);
802 /* FIXME: Try one child moving allocation, one not. */
803 if (should_fail(p)) {
804 p->u.realloc.ret = NULL;
807 /* Don't catch this one in the history fixup... */
808 p->u.realloc.ret = NULL;
809 fixup_ptr_history(ptr);
810 p->u.realloc.ret = realloc(ptr, size);
811 set_cleanup(p, cleanup_realloc, struct realloc_call);
814 return p->u.realloc.ret;
817 void failtest_free(void *ptr)
819 fixup_ptr_history(ptr);
823 static void cleanup_open(struct open_call *call)
828 int failtest_open(const char *pathname,
829 const char *file, unsigned line, ...)
831 struct failtest_call *p;
832 struct open_call call;
835 call.pathname = strdup(pathname);
837 call.flags = va_arg(ap, int);
838 if (call.flags & O_CREAT) {
839 call.mode = va_arg(ap, int);
842 p = add_history(FAILTEST_OPEN, file, line, &call);
843 /* Avoid memory leak! */
844 if (p == &unrecorded_call)
845 free((char *)call.pathname);
846 p->u.open.ret = open(pathname, call.flags, call.mode);
848 if (p->u.open.ret == -1) {
851 } else if (should_fail(p)) {
852 close(p->u.open.ret);
854 /* FIXME: Play with error codes? */
857 set_cleanup(p, cleanup_open, struct open_call);
860 return p->u.open.ret;
863 static void cleanup_pipe(struct pipe_call *call)
865 if (!call->closed[0])
867 if (!call->closed[1])
871 int failtest_pipe(int pipefd[2], const char *file, unsigned line)
873 struct failtest_call *p;
874 struct pipe_call call;
876 p = add_history(FAILTEST_PIPE, file, line, &call);
877 if (should_fail(p)) {
879 /* FIXME: Play with error codes? */
882 p->u.pipe.ret = pipe(p->u.pipe.fds);
883 p->u.pipe.closed[0] = p->u.pipe.closed[1] = false;
884 set_cleanup(p, cleanup_pipe, struct pipe_call);
886 /* This causes valgrind to notice if they use pipefd[] after failure */
887 memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
889 return p->u.pipe.ret;
892 ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
893 const char *file, unsigned line)
895 struct failtest_call *p;
896 struct read_call call;
901 p = add_history(FAILTEST_READ, file, line, &call);
903 /* FIXME: Try partial read returns. */
904 if (should_fail(p)) {
908 p->u.read.ret = pread(fd, buf, count, off);
911 return p->u.read.ret;
914 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t off,
915 const char *file, unsigned line)
917 struct failtest_call *p;
918 struct write_call call;
924 p = add_history(FAILTEST_WRITE, file, line, &call);
926 /* If we're a child, we need to make sure we write the same thing
927 * to non-files as the parent does, so tell it. */
928 if (control_fd != -1 && off == (off_t)-1) {
929 enum info_type type = WRITE;
931 write_all(control_fd, &type, sizeof(type));
932 write_all(control_fd, &p->u.write, sizeof(p->u.write));
933 write_all(control_fd, buf, count);
936 /* FIXME: Try partial write returns. */
937 if (should_fail(p)) {
941 /* FIXME: We assume same write order in parent and child */
942 if (off == (off_t)-1 && child_writes_num != 0) {
943 if (child_writes[0].fd != fd)
944 errx(1, "Child wrote to fd %u, not %u?",
945 child_writes[0].fd, fd);
946 if (child_writes[0].off != p->u.write.off)
947 errx(1, "Child wrote to offset %zu, not %zu?",
948 (size_t)child_writes[0].off,
949 (size_t)p->u.write.off);
950 if (child_writes[0].count != count)
951 errx(1, "Child wrote length %zu, not %zu?",
952 child_writes[0].count, count);
953 if (memcmp(child_writes[0].buf, buf, count)) {
955 "Child wrote differently to"
956 " fd %u than we did!\n", fd);
958 free((char *)child_writes[0].buf);
960 memmove(&child_writes[0], &child_writes[1],
961 sizeof(child_writes[0]) * child_writes_num);
963 /* Is this is a socket or pipe, child wrote it
965 if (p->u.write.off == (off_t)-1) {
966 p->u.write.ret = count;
968 return p->u.write.ret;
971 p->u.write.ret = pwrite(fd, buf, count, off);
974 return p->u.write.ret;
977 ssize_t failtest_read(int fd, void *buf, size_t count,
978 const char *file, unsigned line)
980 return failtest_pread(fd, buf, count, lseek(fd, 0, SEEK_CUR),
984 ssize_t failtest_write(int fd, const void *buf, size_t count,
985 const char *file, unsigned line)
987 return failtest_pwrite(fd, buf, count, lseek(fd, 0, SEEK_CUR),
991 static struct lock_info *WARN_UNUSED_RESULT
992 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
997 for (i = 0; i < lock_num; i++) {
1002 /* Four cases we care about:
1016 if (start > l->start && end < l->end) {
1017 /* Mid overlap: trim entry, add new one. */
1018 off_t new_start, new_end;
1019 new_start = end + 1;
1022 locks = add_lock(locks,
1023 fd, new_start, new_end, l->type);
1025 } else if (start <= l->start && end >= l->end) {
1026 /* Total overlap: eliminate entry. */
1029 } else if (end >= l->start && end < l->end) {
1030 /* Start overlap: trim entry. */
1032 } else if (start > l->start && start <= l->end) {
1033 /* End overlap: trim entry. */
1036 /* Nothing left? Remove it. */
1037 if (l->end < l->start) {
1038 memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
1043 if (type != F_UNLCK) {
1044 locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
1045 l = &locks[lock_num++];
1054 /* We trap this so we can record it: we don't fail it. */
1055 int failtest_close(int fd, const char *file, unsigned line)
1057 struct failtest_call *i;
1058 struct close_call call;
1059 struct failtest_call *p;
1062 p = add_history(FAILTEST_CLOSE, file, line, &call);
1065 /* Consume close from failpath. */
1073 /* Trace history to find source of fd. */
1074 tlist_for_each_rev(&history, i, list) {
1078 if (i->u.pipe.fds[0] == fd) {
1079 assert(!i->u.pipe.closed[0]);
1080 i->u.pipe.closed[0] = true;
1081 if (i->u.pipe.closed[1])
1085 if (i->u.pipe.fds[1] == fd) {
1086 assert(!i->u.pipe.closed[1]);
1087 i->u.pipe.closed[1] = true;
1088 if (i->u.pipe.closed[0])
1094 if (i->u.open.ret == fd) {
1095 assert((void *)i->cleanup
1096 == (void *)cleanup_open);
1107 locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
1111 /* Zero length means "to end of file" */
1112 static off_t end_of(off_t start, off_t len)
1116 return start + len - 1;
1119 /* FIXME: This only handles locks, really. */
1120 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
1122 struct failtest_call *p;
1123 struct fcntl_call call;
1129 /* Argument extraction. */
1134 call.arg.l = va_arg(ap, long);
1136 return fcntl(fd, cmd, call.arg.l);
1139 return fcntl(fd, cmd);
1143 call.arg.fl = *va_arg(ap, struct flock *);
1145 return fcntl(fd, cmd, &call.arg.fl);
1149 call.arg.fl = *va_arg(ap, struct flock *);
1153 /* This means you need to implement it here. */
1154 err(1, "failtest: unknown fcntl %u", cmd);
1157 p = add_history(FAILTEST_FCNTL, file, line, &call);
1159 if (should_fail(p)) {
1160 p->u.fcntl.ret = -1;
1161 if (p->u.fcntl.cmd == F_SETLK)
1167 p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
1168 &p->u.fcntl.arg.fl);
1169 if (p->u.fcntl.ret == -1)
1172 /* We don't handle anything else yet. */
1173 assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
1174 locks = add_lock(locks,
1176 p->u.fcntl.arg.fl.l_start,
1177 end_of(p->u.fcntl.arg.fl.l_start,
1178 p->u.fcntl.arg.fl.l_len),
1179 p->u.fcntl.arg.fl.l_type);
1183 return p->u.fcntl.ret;
1186 pid_t failtest_getpid(const char *file, unsigned line)
1188 /* You must call failtest_init first! */
1193 void failtest_init(int argc, char *argv[])
1197 orig_pid = getpid();
1199 warnfd = move_fd_to_high(dup(STDERR_FILENO));
1200 for (i = 1; i < argc; i++) {
1201 if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) {
1202 failpath = argv[i] + strlen("--failpath=");
1203 } else if (strcmp(argv[i], "--tracepath") == 0) {
1205 failtest_timeout_ms = -1;
1206 } else if (!strncmp(argv[i], "--debugpath=",
1207 strlen("--debugpath="))) {
1208 debugpath = argv[i] + strlen("--debugpath=");
1211 failtable_init(&failtable);
1215 bool failtest_has_failed(void)
1217 return control_fd != -1;
1220 void failtest_exit(int status)
1222 if (failtest_exit_check) {
1223 if (!failtest_exit_check(&history))
1224 child_fail(NULL, 0, "failtest_exit_check failed\n");
1227 failtest_cleanup(false, status);