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 call->fail = isupper(*(failpath++));
272 if (!failtest_hook(history, history_num)) {
277 /* We're going to fail in the child. */
279 if (pipe(control) != 0 || pipe(output) != 0)
280 err(1, "opening pipe");
282 /* Prevent double-printing (in child and parent) */
286 err(1, "forking failed");
291 dup2(output[1], STDOUT_FILENO);
292 dup2(output[1], STDERR_FILENO);
293 if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
295 control_fd = control[1];
299 signal(SIGUSR1, hand_down);
304 /* We grab output so we can display it; we grab writes so we
307 struct pollfd pfd[2];
310 pfd[0].fd = output[0];
311 pfd[0].events = POLLIN|POLLHUP;
312 pfd[1].fd = control[0];
313 pfd[1].events = POLLIN|POLLHUP;
316 ret = poll(pfd, 1, failtest_timeout_ms);
318 ret = poll(pfd, 2, failtest_timeout_ms);
323 if (pfd[0].revents & POLLIN) {
326 out = realloc(out, outlen + 8192);
327 len = read(output[0], out + outlen, 8192);
329 } else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
330 if (read_all(control[0], &type, sizeof(type))) {
332 if (!read_write_info(control[0]))
334 } else if (type == RELEASE_LOCKS) {
336 /* FIXME: Tell them we're done... */
339 } else if (pfd[0].revents & POLLHUP) {
342 } while (type != FAILURE);
346 waitpid(child, &status, 0);
347 if (!WIFEXITED(status))
348 child_fail(out, outlen, "Killed by signal %u: ",
350 /* Child printed failure already, just pass up exit code. */
351 if (type == FAILURE) {
352 fprintf(stderr, "%.*s", (int)outlen, out);
354 exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
356 if (WEXITSTATUS(status) != 0)
357 child_fail(out, outlen, "Exited with status %i: ",
358 WEXITSTATUS(status));
361 signal(SIGUSR1, SIG_DFL);
363 /* We continue onwards without failing. */
368 void *failtest_calloc(size_t nmemb, size_t size,
369 const char *file, unsigned line)
371 struct failtest_call *p;
372 struct calloc_call call;
375 p = add_history(FAILTEST_CALLOC, file, line, &call);
377 if (should_fail(p)) {
378 p->u.calloc.ret = NULL;
381 p->u.calloc.ret = calloc(nmemb, size);
384 return p->u.calloc.ret;
387 void *failtest_malloc(size_t size, const char *file, unsigned line)
389 struct failtest_call *p;
390 struct malloc_call call;
393 p = add_history(FAILTEST_MALLOC, file, line, &call);
394 if (should_fail(p)) {
395 p->u.calloc.ret = NULL;
398 p->u.calloc.ret = malloc(size);
401 return p->u.calloc.ret;
404 void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
406 struct failtest_call *p;
407 struct realloc_call call;
409 p = add_history(FAILTEST_REALLOC, file, line, &call);
411 /* FIXME: Try one child moving allocation, one not. */
412 if (should_fail(p)) {
413 p->u.realloc.ret = NULL;
416 p->u.realloc.ret = realloc(ptr, size);
419 return p->u.realloc.ret;
422 int failtest_open(const char *pathname,
423 const char *file, unsigned line, ...)
425 struct failtest_call *p;
426 struct open_call call;
429 call.pathname = strdup(pathname);
431 call.flags = va_arg(ap, int);
432 if (call.flags & O_CREAT) {
433 call.mode = va_arg(ap, mode_t);
436 p = add_history(FAILTEST_OPEN, file, line, &call);
437 /* Avoid memory leak! */
438 if (p == &unrecorded_call)
439 free((char *)call.pathname);
440 if (should_fail(p)) {
442 /* FIXME: Play with error codes? */
445 p->u.open.ret = open(pathname, call.flags, call.mode);
448 return p->u.open.ret;
451 int failtest_pipe(int pipefd[2], const char *file, unsigned line)
453 struct failtest_call *p;
454 struct pipe_call call;
456 p = add_history(FAILTEST_PIPE, file, line, &call);
457 if (should_fail(p)) {
459 /* FIXME: Play with error codes? */
462 p->u.pipe.ret = pipe(p->u.pipe.fds);
464 /* This causes valgrind to notice if they use pipefd[] after failure */
465 memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
467 return p->u.pipe.ret;
470 ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
471 const char *file, unsigned line)
473 struct failtest_call *p;
474 struct read_call call;
479 p = add_history(FAILTEST_READ, file, line, &call);
481 /* This is going to change seek offset, so save it. */
482 if (control_fd != -1)
485 /* FIXME: Try partial read returns. */
486 if (should_fail(p)) {
490 p->u.read.ret = pread(fd, buf, count, off);
493 return p->u.read.ret;
496 static struct write_info *new_write(void)
498 writes = realloc(writes, (writes_num + 1) * sizeof(*writes));
499 return &writes[writes_num++];
502 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t off,
503 const char *file, unsigned line)
505 struct failtest_call *p;
506 struct write_call call;
513 p = add_history(FAILTEST_WRITE, file, line, &call);
515 offset = lseek(fd, 0, SEEK_CUR);
517 /* If we're a child, save contents and tell parent about write. */
518 if (control_fd != -1) {
519 struct write_info *winfo = new_write();
520 enum info_type type = WRITE;
524 winfo->hdr.len = count;
526 winfo->data = malloc(count);
527 memcpy(winfo->data, buf, count);
528 winfo->hdr.offset = offset;
529 if (winfo->hdr.offset != (off_t)-1) {
530 lseek(fd, offset, SEEK_SET);
531 winfo->olddata = malloc(count);
532 winfo->oldlen = read(fd, winfo->olddata, count);
533 if (winfo->oldlen == -1)
536 write_all(control_fd, &type, sizeof(type));
537 write_all(control_fd, &winfo->hdr, sizeof(winfo->hdr));
538 write_all(control_fd, winfo->data, count);
541 /* FIXME: Try partial write returns. */
542 if (should_fail(p)) {
546 /* FIXME: We assume same write order in parent and child */
547 if (child_writes_num != 0) {
548 if (child_writes[0].hdr.fd != fd)
549 errx(1, "Child wrote to fd %u, not %u?",
550 child_writes[0].hdr.fd, fd);
551 if (child_writes[0].hdr.offset != offset)
552 errx(1, "Child wrote to offset %zu, not %zu?",
553 (size_t)child_writes[0].hdr.offset,
555 if (child_writes[0].hdr.len != count)
556 errx(1, "Child wrote length %zu, not %zu?",
557 child_writes[0].hdr.len, count);
558 if (memcmp(child_writes[0].data, buf, count)) {
560 "Child wrote differently to"
561 " fd %u than we did!\n", fd);
563 free(child_writes[0].data);
565 memmove(&child_writes[0], &child_writes[1],
566 sizeof(child_writes[0]) * child_writes_num);
568 /* Is this is a socket or pipe, child wrote it
570 if (offset == (off_t)-1) {
571 p->u.write.ret = count;
573 return p->u.write.ret;
576 p->u.write.ret = pwrite(fd, buf, count, off);
579 return p->u.write.ret;
582 ssize_t failtest_read(int fd, void *buf, size_t count,
583 const char *file, unsigned line)
585 return failtest_pread(fd, buf, count, lseek(fd, 0, SEEK_CUR),
589 ssize_t failtest_write(int fd, const void *buf, size_t count,
590 const char *file, unsigned line)
592 return failtest_pwrite(fd, buf, count, lseek(fd, 0, SEEK_CUR),
596 static struct lock_info *WARN_UNUSED_RESULT
597 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
602 for (i = 0; i < lock_num; i++) {
607 /* Four cases we care about:
621 if (start > l->start && end < l->end) {
622 /* Mid overlap: trim entry, add new one. */
623 off_t new_start, new_end;
627 locks = add_lock(locks,
628 fd, new_start, new_end, l->type);
630 } else if (start <= l->start && end >= l->end) {
631 /* Total overlap: eliminate entry. */
634 } else if (end >= l->start && end < l->end) {
635 /* Start overlap: trim entry. */
637 } else if (start > l->start && start <= l->end) {
638 /* End overlap: trim entry. */
641 /* Nothing left? Remove it. */
642 if (l->end < l->start) {
643 memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
648 if (type != F_UNLCK) {
649 locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
650 l = &locks[lock_num++];
659 /* We only trap this so we can dup fds in case we need to restore. */
660 int failtest_close(int fd)
665 for (i = 0; i < fd_orig_num; i++) {
666 if (fd_orig[i].fd == fd) {
667 fd_orig[i].fd = newfd = dup(fd);
668 fd_orig[i].dupped = true;
672 for (i = 0; i < writes_num; i++) {
673 if (writes[i].hdr.fd == fd)
674 writes[i].hdr.fd = newfd;
677 locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
681 /* Zero length means "to end of file" */
682 static off_t end_of(off_t start, off_t len)
686 return start + len - 1;
689 /* FIXME: This only handles locks, really. */
690 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
692 struct failtest_call *p;
693 struct fcntl_call call;
699 /* Argument extraction. */
704 call.arg.l = va_arg(ap, long);
706 return fcntl(fd, cmd, call.arg.l);
709 return fcntl(fd, cmd);
713 call.arg.fl = *va_arg(ap, struct flock *);
715 return fcntl(fd, cmd, &call.arg.fl);
719 call.arg.fl = *va_arg(ap, struct flock *);
723 /* This means you need to implement it here. */
724 err(1, "failtest: unknown fcntl %u", cmd);
727 p = add_history(FAILTEST_FCNTL, file, line, &call);
730 if (should_fail(p)) {
732 if (p->u.fcntl.cmd == F_SETLK)
737 p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
739 if (p->u.fcntl.ret == -1)
742 /* We don't handle anything else yet. */
743 assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
744 locks = add_lock(locks,
746 p->u.fcntl.arg.fl.l_start,
747 end_of(p->u.fcntl.arg.fl.l_start,
748 p->u.fcntl.arg.fl.l_len),
749 p->u.fcntl.arg.fl.l_type);
753 return p->u.fcntl.ret;
756 void failtest_init(int argc, char *argv[])
759 && strncmp(argv[1], "--failpath=", strlen("--failpath=")) == 0) {
760 failpath = argv[1] + strlen("--failpath=");
764 /* Free up memory, so valgrind doesn't report leaks. */
765 static void free_everything(void)
769 for (i = 0; i < writes_num; i++) {
770 free(writes[i].data);
771 if (writes[i].hdr.offset != (off_t)-1)
772 free(writes[i].olddata);
776 for (i = 0; i < history_num; i++) {
777 if (history[i].type == FAILTEST_OPEN)
778 free((char *)history[i].u.open.pathname);
783 void failtest_exit(int status)
787 if (control_fd == -1) {
792 if (failtest_exit_check) {
793 if (!failtest_exit_check(history, history_num))
794 child_fail(NULL, 0, "failtest_exit_check failed\n");
797 /* Restore any stuff we overwrote. */
798 for (i = 0; i < writes_num; i++) {
799 if (writes[i].hdr.offset == (off_t)-1)
801 if (writes[i].oldlen != 0) {
802 lseek(writes[i].hdr.fd, writes[i].hdr.offset,
804 write(writes[i].hdr.fd, writes[i].olddata,
809 /* Fix up fd offsets, restore sizes. */
810 for (i = 0; i < fd_orig_num; i++) {
811 lseek(fd_orig[i].fd, fd_orig[i].offset, SEEK_SET);
812 ftruncate(fd_orig[i].fd, fd_orig[i].size);
813 /* Free up any file descriptors we dup'ed. */
814 if (fd_orig[i].dupped)
815 close(fd_orig[i].fd);
819 tell_parent(SUCCESS);