10 #include <sys/types.h>
14 #include <ccan/read_write_all/read_write_all.h>
15 #include <ccan/failtest/failtest_proto.h>
16 #include <ccan/failtest/failtest.h>
17 #include <ccan/build_assert/build_assert.h>
19 bool (*failtest_hook)(struct failtest_call *history, unsigned num)
20 = failtest_default_hook;
22 unsigned int failtest_timeout_ms = 20000;
34 struct write_info_hdr {
48 struct write_info_hdr hdr;
56 /* end is inclusive: you can't have a 0-byte lock. */
61 bool (*failtest_exit_check)(struct failtest_call *history, unsigned num);
63 static struct failtest_call *history = NULL;
64 static unsigned int history_num = 0;
65 static int control_fd = -1;
67 static struct write_info *writes = NULL;
68 static unsigned int writes_num = 0;
70 static struct write_info *child_writes = NULL;
71 static unsigned int child_writes_num = 0;
73 static struct fd_orig *fd_orig = NULL;
74 static unsigned int fd_orig_num = 0;
76 static pid_t lock_owner;
77 static struct lock_info *locks = NULL;
78 static unsigned int lock_num = 0;
80 static const char info_to_arg[] = "mceoprwf";
82 /* Dummy call used for failtest_undo wrappers. */
83 static struct failtest_call unrecorded_call;
85 static struct failtest_call *add_history_(enum failtest_call_type type,
91 /* NULL file is how we suppress failure. */
93 return &unrecorded_call;
95 history = realloc(history, (history_num + 1) * sizeof(*history));
96 history[history_num].type = type;
97 history[history_num].file = file;
98 history[history_num].line = line;
99 memcpy(&history[history_num].u, elem, elem_size);
100 return &history[history_num++];
103 #define add_history(type, file, line, elem) \
104 add_history_((type), (file), (line), (elem), sizeof(*(elem)))
106 static void save_fd_orig(int fd)
110 for (i = 0; i < fd_orig_num; i++)
111 if (fd_orig[i].fd == fd)
114 fd_orig = realloc(fd_orig, (fd_orig_num + 1) * sizeof(*fd_orig));
115 fd_orig[fd_orig_num].fd = fd;
116 fd_orig[fd_orig_num].dupped = false;
117 fd_orig[fd_orig_num].offset = lseek(fd, 0, SEEK_CUR);
118 fd_orig[fd_orig_num].size = lseek(fd, 0, SEEK_END);
119 lseek(fd, fd_orig[fd_orig_num].offset, SEEK_SET);
123 bool failtest_default_hook(struct failtest_call *history, unsigned num)
128 static bool read_write_info(int fd)
130 struct write_info_hdr hdr;
132 if (!read_all(fd, &hdr, sizeof(hdr)))
135 child_writes = realloc(child_writes,
136 (child_writes_num+1) * sizeof(child_writes[0]));
137 child_writes[child_writes_num].hdr = hdr;
138 child_writes[child_writes_num].data = malloc(hdr.len);
139 if (!read_all(fd, child_writes[child_writes_num].data, hdr.len))
146 static void print_reproduce(void)
150 printf("To reproduce: --failpath=");
151 for (i = 0; i < history_num; i++) {
153 printf("%c", toupper(info_to_arg[history[i].type]));
155 printf("%c", info_to_arg[history[i].type]);
160 static void tell_parent(enum info_type type)
162 if (control_fd != -1)
163 write_all(control_fd, &type, sizeof(type));
166 static void child_fail(const char *out, size_t outlen, const char *fmt, ...)
171 vfprintf(stderr, fmt, ap);
174 fprintf(stderr, "%.*s", (int)outlen, out);
176 tell_parent(FAILURE);
182 static void hand_down(int signal)
187 static void release_locks(void)
189 /* Locks were never acquired/reacquired? */
193 /* We own them? Release them all. */
194 if (lock_owner == getpid()) {
198 fl.l_whence = SEEK_SET;
202 for (i = 0; i < lock_num; i++)
203 fcntl(locks[i].fd, F_SETLK, &fl);
205 /* Our parent must have them; pass request up. */
206 enum info_type type = RELEASE_LOCKS;
207 assert(control_fd != -1);
208 write_all(control_fd, &type, sizeof(type));
213 /* off_t is a signed type. Getting its max is non-trivial. */
214 static off_t off_max(void)
216 BUILD_ASSERT(sizeof(off_t) == 4 || sizeof(off_t) == 8);
217 if (sizeof(off_t) == 4)
218 return (off_t)0x7FFFFFF;
220 return (off_t)0x7FFFFFFFFFFFFFFULL;
223 static void get_locks(void)
228 if (lock_owner == getpid())
231 if (lock_owner != 0) {
232 enum info_type type = RELEASE_LOCKS;
233 assert(control_fd != -1);
234 write_all(control_fd, &type, sizeof(type));
237 fl.l_whence = SEEK_SET;
239 for (i = 0; i < lock_num; i++) {
240 fl.l_type = locks[i].type;
241 fl.l_start = locks[i].start;
242 if (locks[i].end == off_max())
245 fl.l_len = locks[i].end - locks[i].start + 1;
247 if (fcntl(locks[i].fd, F_SETLKW, &fl) != 0)
250 lock_owner = getpid();
253 static bool should_fail(struct failtest_call *call)
256 int control[2], output[2];
257 enum info_type type = UNEXPECTED;
261 if (call == &unrecorded_call)
265 if (tolower(*failpath) != info_to_arg[call->type])
266 errx(1, "Failpath expected '%c' got '%c'\n",
267 info_to_arg[call->type], *failpath);
268 return isupper(*(failpath++));
271 if (!failtest_hook(history, history_num)) {
276 /* We're going to fail in the child. */
278 if (pipe(control) != 0 || pipe(output) != 0)
279 err(1, "opening pipe");
281 /* Prevent double-printing (in child and parent) */
285 err(1, "forking failed");
290 dup2(output[1], STDOUT_FILENO);
291 dup2(output[1], STDERR_FILENO);
292 if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
294 control_fd = control[1];
298 signal(SIGUSR1, hand_down);
303 /* We grab output so we can display it; we grab writes so we
306 struct pollfd pfd[2];
309 pfd[0].fd = output[0];
310 pfd[0].events = POLLIN|POLLHUP;
311 pfd[1].fd = control[0];
312 pfd[1].events = POLLIN|POLLHUP;
315 ret = poll(pfd, 1, failtest_timeout_ms);
317 ret = poll(pfd, 2, failtest_timeout_ms);
322 if (pfd[0].revents & POLLIN) {
325 out = realloc(out, outlen + 8192);
326 len = read(output[0], out + outlen, 8192);
328 } else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
329 if (read_all(control[0], &type, sizeof(type))) {
331 if (!read_write_info(control[0]))
333 } else if (type == RELEASE_LOCKS) {
335 /* FIXME: Tell them we're done... */
338 } else if (pfd[0].revents & POLLHUP) {
341 } while (type != FAILURE);
345 waitpid(child, &status, 0);
346 if (!WIFEXITED(status))
347 child_fail(out, outlen, "Killed by signal %u: ",
349 /* Child printed failure already, just pass up exit code. */
350 if (type == FAILURE) {
351 fprintf(stderr, "%.*s", (int)outlen, out);
353 exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
355 if (WEXITSTATUS(status) != 0)
356 child_fail(out, outlen, "Exited with status %i: ",
357 WEXITSTATUS(status));
360 signal(SIGUSR1, SIG_DFL);
362 /* We continue onwards without failing. */
367 void *failtest_calloc(size_t nmemb, size_t size,
368 const char *file, unsigned line)
370 struct failtest_call *p;
371 struct calloc_call call;
374 p = add_history(FAILTEST_CALLOC, file, line, &call);
376 if (should_fail(p)) {
377 p->u.calloc.ret = NULL;
380 p->u.calloc.ret = calloc(nmemb, size);
383 return p->u.calloc.ret;
386 void *failtest_malloc(size_t size, const char *file, unsigned line)
388 struct failtest_call *p;
389 struct malloc_call call;
392 p = add_history(FAILTEST_MALLOC, file, line, &call);
393 if (should_fail(p)) {
394 p->u.calloc.ret = NULL;
397 p->u.calloc.ret = malloc(size);
400 return p->u.calloc.ret;
403 void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
405 struct failtest_call *p;
406 struct realloc_call call;
408 p = add_history(FAILTEST_REALLOC, file, line, &call);
410 /* FIXME: Try one child moving allocation, one not. */
411 if (should_fail(p)) {
412 p->u.realloc.ret = NULL;
415 p->u.realloc.ret = realloc(ptr, size);
418 return p->u.realloc.ret;
421 int failtest_open(const char *pathname, int flags,
422 const char *file, unsigned line, ...)
424 struct failtest_call *p;
425 struct open_call call;
427 call.pathname = strdup(pathname);
429 if (flags & O_CREAT) {
432 call.mode = va_arg(ap, mode_t);
435 p = add_history(FAILTEST_OPEN, file, line, &call);
436 /* Avoid memory leak! */
437 if (p == &unrecorded_call)
438 free((char *)call.pathname);
439 if (should_fail(p)) {
441 /* FIXME: Play with error codes? */
444 p->u.open.ret = open(pathname, flags, call.mode);
447 return p->u.open.ret;
450 int failtest_pipe(int pipefd[2], const char *file, unsigned line)
452 struct failtest_call *p;
453 struct pipe_call call;
455 p = add_history(FAILTEST_PIPE, file, line, &call);
456 if (should_fail(p)) {
458 /* FIXME: Play with error codes? */
461 p->u.pipe.ret = pipe(p->u.pipe.fds);
463 /* This causes valgrind to notice if they use pipefd[] after failure */
464 memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
466 return p->u.pipe.ret;
469 ssize_t failtest_read(int fd, void *buf, size_t count,
470 const char *file, unsigned line)
472 struct failtest_call *p;
473 struct read_call call;
477 p = add_history(FAILTEST_READ, file, line, &call);
479 /* This is going to change seek offset, so save it. */
480 if (control_fd != -1)
483 /* FIXME: Try partial read returns. */
484 if (should_fail(p)) {
488 p->u.read.ret = read(fd, buf, count);
491 return p->u.read.ret;
494 static struct write_info *new_write(void)
496 writes = realloc(writes, (writes_num + 1) * sizeof(*writes));
497 return &writes[writes_num++];
500 ssize_t failtest_write(int fd, const void *buf, size_t count,
501 const char *file, unsigned line)
503 struct failtest_call *p;
504 struct write_call call;
510 p = add_history(FAILTEST_WRITE, file, line, &call);
512 offset = lseek(fd, 0, SEEK_CUR);
514 /* If we're a child, save contents and tell parent about write. */
515 if (control_fd != -1) {
516 struct write_info *winfo = new_write();
517 enum info_type type = WRITE;
521 winfo->hdr.len = count;
523 winfo->data = malloc(count);
524 memcpy(winfo->data, buf, count);
525 winfo->hdr.offset = offset;
526 if (winfo->hdr.offset != (off_t)-1) {
527 lseek(fd, offset, SEEK_SET);
528 winfo->olddata = malloc(count);
529 winfo->oldlen = read(fd, winfo->olddata, count);
530 if (winfo->oldlen == -1)
533 write_all(control_fd, &type, sizeof(type));
534 write_all(control_fd, &winfo->hdr, sizeof(winfo->hdr));
535 write_all(control_fd, winfo->data, count);
538 /* FIXME: Try partial write returns. */
539 if (should_fail(p)) {
543 /* FIXME: We assume same write order in parent and child */
544 if (child_writes_num != 0) {
545 if (child_writes[0].hdr.fd != fd)
546 errx(1, "Child wrote to fd %u, not %u?",
547 child_writes[0].hdr.fd, fd);
548 if (child_writes[0].hdr.offset != offset)
549 errx(1, "Child wrote to offset %zu, not %zu?",
550 (size_t)child_writes[0].hdr.offset,
552 if (child_writes[0].hdr.len != count)
553 errx(1, "Child wrote length %zu, not %zu?",
554 child_writes[0].hdr.len, count);
555 if (memcmp(child_writes[0].data, buf, count)) {
557 "Child wrote differently to"
558 " fd %u than we did!\n", fd);
560 free(child_writes[0].data);
562 memmove(&child_writes[0], &child_writes[1],
563 sizeof(child_writes[0]) * child_writes_num);
565 /* Is this is a socket or pipe, child wrote it
567 if (offset == (off_t)-1) {
568 p->u.write.ret = count;
570 return p->u.write.ret;
573 p->u.write.ret = write(fd, buf, count);
576 return p->u.write.ret;
579 static struct lock_info *WARN_UNUSED_RESULT
580 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
585 for (i = 0; i < lock_num; i++) {
590 /* Four cases we care about:
604 if (start > l->start && end < l->end) {
605 /* Mid overlap: trim entry, add new one. */
606 off_t new_start, new_end;
610 locks = add_lock(locks,
611 fd, new_start, new_end, l->type);
613 } else if (start <= l->start && end >= l->end) {
614 /* Total overlap: eliminate entry. */
617 } else if (end >= l->start && end < l->end) {
618 /* Start overlap: trim entry. */
620 } else if (start > l->start && start <= l->end) {
621 /* End overlap: trim entry. */
624 /* Nothing left? Remove it. */
625 if (l->end < l->start) {
626 memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
631 if (type != F_UNLCK) {
632 locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
633 l = &locks[lock_num++];
642 /* We only trap this so we can dup fds in case we need to restore. */
643 int failtest_close(int fd)
648 for (i = 0; i < fd_orig_num; i++) {
649 if (fd_orig[i].fd == fd) {
650 fd_orig[i].fd = newfd = dup(fd);
651 fd_orig[i].dupped = true;
655 for (i = 0; i < writes_num; i++) {
656 if (writes[i].hdr.fd == fd)
657 writes[i].hdr.fd = newfd;
660 locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
664 /* Zero length means "to end of file" */
665 static off_t end_of(off_t start, off_t len)
669 return start + len - 1;
672 /* FIXME: This only handles locks, really. */
673 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
675 struct failtest_call *p;
676 struct fcntl_call call;
682 /* Argument extraction. */
687 call.arg.l = va_arg(ap, long);
689 return fcntl(fd, cmd, call.arg.l);
692 return fcntl(fd, cmd);
696 call.arg.fl = *va_arg(ap, struct flock *);
698 return fcntl(fd, cmd, &call.arg.fl);
702 call.arg.fl = *va_arg(ap, struct flock *);
706 /* This means you need to implement it here. */
707 err(1, "failtest: unknown fcntl %u", cmd);
710 p = add_history(FAILTEST_FCNTL, file, line, &call);
713 if (should_fail(p)) {
715 if (p->u.fcntl.cmd == F_SETLK)
720 p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
722 if (p->u.fcntl.ret == -1)
725 /* We don't handle anything else yet. */
726 assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
727 locks = add_lock(locks,
729 p->u.fcntl.arg.fl.l_start,
730 end_of(p->u.fcntl.arg.fl.l_start,
731 p->u.fcntl.arg.fl.l_len),
732 p->u.fcntl.arg.fl.l_type);
736 return p->u.fcntl.ret;
739 void failtest_init(int argc, char *argv[])
742 && strncmp(argv[1], "--failpath=", strlen("--failpath=")) == 0) {
743 failpath = argv[1] + strlen("--failpath=");
747 /* Free up memory, so valgrind doesn't report leaks. */
748 static void free_everything(void)
752 for (i = 0; i < writes_num; i++) {
753 free(writes[i].data);
754 if (writes[i].hdr.offset != (off_t)-1)
755 free(writes[i].olddata);
759 for (i = 0; i < history_num; i++) {
760 if (history[i].type == FAILTEST_OPEN)
761 free((char *)history[i].u.open.pathname);
766 void failtest_exit(int status)
770 if (control_fd == -1) {
775 if (failtest_exit_check) {
776 if (!failtest_exit_check(history, history_num))
777 child_fail(NULL, 0, "failtest_exit_check failed\n");
780 /* Restore any stuff we overwrote. */
781 for (i = 0; i < writes_num; i++) {
782 if (writes[i].hdr.offset == (off_t)-1)
784 if (writes[i].oldlen != 0) {
785 lseek(writes[i].hdr.fd, writes[i].hdr.offset,
787 write(writes[i].hdr.fd, writes[i].olddata,
792 /* Fix up fd offsets, restore sizes. */
793 for (i = 0; i < fd_orig_num; i++) {
794 lseek(fd_orig[i].fd, fd_orig[i].offset, SEEK_SET);
795 ftruncate(fd_orig[i].fd, fd_orig[i].size);
796 /* Free up any file descriptors we dup'ed. */
797 if (fd_orig[i].dupped)
798 close(fd_orig[i].fd);
802 tell_parent(SUCCESS);