10 #include <sys/types.h>
15 #include <ccan/read_write_all/read_write_all.h>
16 #include <ccan/failtest/failtest_proto.h>
17 #include <ccan/failtest/failtest.h>
18 #include <ccan/build_assert/build_assert.h>
20 bool (*failtest_hook)(struct failtest_call *history, unsigned num)
21 = failtest_default_hook;
23 static int tracefd = -1;
25 unsigned int failtest_timeout_ms = 20000;
37 struct write_info_hdr {
51 struct write_info_hdr hdr;
59 /* end is inclusive: you can't have a 0-byte lock. */
64 bool (*failtest_exit_check)(struct failtest_call *history, unsigned num);
66 static struct failtest_call *history = NULL;
67 static unsigned int history_num = 0;
68 static int control_fd = -1;
69 static struct timeval start;
71 static struct write_info *writes = NULL;
72 static unsigned int writes_num = 0;
74 static struct write_info *child_writes = NULL;
75 static unsigned int child_writes_num = 0;
77 static struct fd_orig *fd_orig = NULL;
78 static unsigned int fd_orig_num = 0;
80 static pid_t lock_owner;
81 static struct lock_info *locks = NULL;
82 static unsigned int lock_num = 0;
84 static const char info_to_arg[] = "mceoprwf";
86 /* Dummy call used for failtest_undo wrappers. */
87 static struct failtest_call unrecorded_call;
89 static struct failtest_call *add_history_(enum failtest_call_type type,
95 /* NULL file is how we suppress failure. */
97 return &unrecorded_call;
99 history = realloc(history, (history_num + 1) * sizeof(*history));
100 history[history_num].type = type;
101 history[history_num].file = file;
102 history[history_num].line = line;
103 memcpy(&history[history_num].u, elem, elem_size);
104 return &history[history_num++];
107 #define add_history(type, file, line, elem) \
108 add_history_((type), (file), (line), (elem), sizeof(*(elem)))
110 static void save_fd_orig(int fd)
114 for (i = 0; i < fd_orig_num; i++)
115 if (fd_orig[i].fd == fd)
118 fd_orig = realloc(fd_orig, (fd_orig_num + 1) * sizeof(*fd_orig));
119 fd_orig[fd_orig_num].fd = fd;
120 fd_orig[fd_orig_num].dupped = false;
121 fd_orig[fd_orig_num].offset = lseek(fd, 0, SEEK_CUR);
122 fd_orig[fd_orig_num].size = lseek(fd, 0, SEEK_END);
123 lseek(fd, fd_orig[fd_orig_num].offset, SEEK_SET);
127 bool failtest_default_hook(struct failtest_call *history, unsigned num)
132 static bool read_write_info(int fd)
134 struct write_info_hdr hdr;
136 if (!read_all(fd, &hdr, sizeof(hdr)))
139 child_writes = realloc(child_writes,
140 (child_writes_num+1) * sizeof(child_writes[0]));
141 child_writes[child_writes_num].hdr = hdr;
142 child_writes[child_writes_num].data = malloc(hdr.len);
143 if (!read_all(fd, child_writes[child_writes_num].data, hdr.len))
150 static char *failpath_string(void)
153 char *ret = malloc(history_num + 1);
155 for (i = 0; i < history_num; i++) {
156 ret[i] = info_to_arg[history[i].type];
158 ret[i] = toupper(ret[i]);
164 static void tell_parent(enum info_type type)
166 if (control_fd != -1)
167 write_all(control_fd, &type, sizeof(type));
170 static void child_fail(const char *out, size_t outlen, const char *fmt, ...)
173 char *path = failpath_string();
176 vfprintf(stderr, fmt, ap);
179 fprintf(stderr, "%.*s", (int)outlen, out);
180 printf("To reproduce: --failpath=%s\n", path);
182 tell_parent(FAILURE);
188 static void hand_down(int signal)
193 static void release_locks(void)
195 /* Locks were never acquired/reacquired? */
199 /* We own them? Release them all. */
200 if (lock_owner == getpid()) {
204 fl.l_whence = SEEK_SET;
208 for (i = 0; i < lock_num; i++)
209 fcntl(locks[i].fd, F_SETLK, &fl);
211 /* Our parent must have them; pass request up. */
212 enum info_type type = RELEASE_LOCKS;
213 assert(control_fd != -1);
214 write_all(control_fd, &type, sizeof(type));
219 /* off_t is a signed type. Getting its max is non-trivial. */
220 static off_t off_max(void)
222 BUILD_ASSERT(sizeof(off_t) == 4 || sizeof(off_t) == 8);
223 if (sizeof(off_t) == 4)
224 return (off_t)0x7FFFFFF;
226 return (off_t)0x7FFFFFFFFFFFFFFULL;
229 static void get_locks(void)
234 if (lock_owner == getpid())
237 if (lock_owner != 0) {
238 enum info_type type = RELEASE_LOCKS;
239 assert(control_fd != -1);
240 write_all(control_fd, &type, sizeof(type));
243 fl.l_whence = SEEK_SET;
245 for (i = 0; i < lock_num; i++) {
246 fl.l_type = locks[i].type;
247 fl.l_start = locks[i].start;
248 if (locks[i].end == off_max())
251 fl.l_len = locks[i].end - locks[i].start + 1;
253 if (fcntl(locks[i].fd, F_SETLKW, &fl) != 0)
256 lock_owner = getpid();
259 static void trace_str(const char *str)
263 while ((ret = write(tracefd, str, strlen(str))) <= 0) {
268 err(1, "Writing trace.");
271 static bool should_fail(struct failtest_call *call)
274 int control[2], output[2];
275 enum info_type type = UNEXPECTED;
279 if (call == &unrecorded_call)
283 if (tolower(*failpath) != info_to_arg[call->type])
284 errx(1, "Failpath expected '%c' got '%c'\n",
285 info_to_arg[call->type], *failpath);
286 call->fail = isupper(*(failpath++));
290 if (!failtest_hook(history, history_num)) {
295 /* We're going to fail in the child. */
297 if (pipe(control) != 0 || pipe(output) != 0)
298 err(1, "opening pipe");
300 /* Prevent double-printing (in child and parent) */
304 err(1, "forking failed");
310 gettimeofday(&now, NULL);
311 if (now.tv_usec < start.tv_usec) {
313 now.tv_usec += 1000000;
315 now.tv_usec -= start.tv_usec;
316 now.tv_sec -= start.tv_sec;
317 sprintf(str, "%u (%u.%02u): ", getpid(),
318 (int)now.tv_sec, (int)now.tv_usec / 10000);
320 p = failpath_string();
324 p = strchr(history[history_num-1].file, '/');
328 trace_str(history[history_num-1].file);
329 sprintf(str, ":%u)\n", history[history_num-1].line);
334 dup2(output[1], STDOUT_FILENO);
335 dup2(output[1], STDERR_FILENO);
336 if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
338 control_fd = control[1];
342 signal(SIGUSR1, hand_down);
347 /* We grab output so we can display it; we grab writes so we
350 struct pollfd pfd[2];
353 pfd[0].fd = output[0];
354 pfd[0].events = POLLIN|POLLHUP;
355 pfd[1].fd = control[0];
356 pfd[1].events = POLLIN|POLLHUP;
359 ret = poll(pfd, 1, failtest_timeout_ms);
361 ret = poll(pfd, 2, failtest_timeout_ms);
366 if (pfd[0].revents & POLLIN) {
369 out = realloc(out, outlen + 8192);
370 len = read(output[0], out + outlen, 8192);
372 } else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
373 if (read_all(control[0], &type, sizeof(type))) {
375 if (!read_write_info(control[0]))
377 } else if (type == RELEASE_LOCKS) {
379 /* FIXME: Tell them we're done... */
382 } else if (pfd[0].revents & POLLHUP) {
385 } while (type != FAILURE);
389 waitpid(child, &status, 0);
390 if (!WIFEXITED(status))
391 child_fail(out, outlen, "Killed by signal %u: ",
393 /* Child printed failure already, just pass up exit code. */
394 if (type == FAILURE) {
395 fprintf(stderr, "%.*s", (int)outlen, out);
397 exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
399 if (WEXITSTATUS(status) != 0)
400 child_fail(out, outlen, "Exited with status %i: ",
401 WEXITSTATUS(status));
404 signal(SIGUSR1, SIG_DFL);
406 /* We continue onwards without failing. */
411 void *failtest_calloc(size_t nmemb, size_t size,
412 const char *file, unsigned line)
414 struct failtest_call *p;
415 struct calloc_call call;
418 p = add_history(FAILTEST_CALLOC, file, line, &call);
420 if (should_fail(p)) {
421 p->u.calloc.ret = NULL;
424 p->u.calloc.ret = calloc(nmemb, size);
427 return p->u.calloc.ret;
430 void *failtest_malloc(size_t size, const char *file, unsigned line)
432 struct failtest_call *p;
433 struct malloc_call call;
436 p = add_history(FAILTEST_MALLOC, file, line, &call);
437 if (should_fail(p)) {
438 p->u.calloc.ret = NULL;
441 p->u.calloc.ret = malloc(size);
444 return p->u.calloc.ret;
447 void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
449 struct failtest_call *p;
450 struct realloc_call call;
452 p = add_history(FAILTEST_REALLOC, file, line, &call);
454 /* FIXME: Try one child moving allocation, one not. */
455 if (should_fail(p)) {
456 p->u.realloc.ret = NULL;
459 p->u.realloc.ret = realloc(ptr, size);
462 return p->u.realloc.ret;
465 int failtest_open(const char *pathname,
466 const char *file, unsigned line, ...)
468 struct failtest_call *p;
469 struct open_call call;
472 call.pathname = strdup(pathname);
474 call.flags = va_arg(ap, int);
475 if (call.flags & O_CREAT) {
476 call.mode = va_arg(ap, mode_t);
479 p = add_history(FAILTEST_OPEN, file, line, &call);
480 /* Avoid memory leak! */
481 if (p == &unrecorded_call)
482 free((char *)call.pathname);
483 if (should_fail(p)) {
485 /* FIXME: Play with error codes? */
488 p->u.open.ret = open(pathname, call.flags, call.mode);
491 return p->u.open.ret;
494 int failtest_pipe(int pipefd[2], const char *file, unsigned line)
496 struct failtest_call *p;
497 struct pipe_call call;
499 p = add_history(FAILTEST_PIPE, file, line, &call);
500 if (should_fail(p)) {
502 /* FIXME: Play with error codes? */
505 p->u.pipe.ret = pipe(p->u.pipe.fds);
507 /* This causes valgrind to notice if they use pipefd[] after failure */
508 memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
510 return p->u.pipe.ret;
513 ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
514 const char *file, unsigned line)
516 struct failtest_call *p;
517 struct read_call call;
522 p = add_history(FAILTEST_READ, file, line, &call);
524 /* This is going to change seek offset, so save it. */
525 if (control_fd != -1)
528 /* FIXME: Try partial read returns. */
529 if (should_fail(p)) {
533 p->u.read.ret = pread(fd, buf, count, off);
536 return p->u.read.ret;
539 static struct write_info *new_write(void)
541 writes = realloc(writes, (writes_num + 1) * sizeof(*writes));
542 return &writes[writes_num++];
545 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t off,
546 const char *file, unsigned line)
548 struct failtest_call *p;
549 struct write_call call;
556 p = add_history(FAILTEST_WRITE, file, line, &call);
558 offset = lseek(fd, 0, SEEK_CUR);
560 /* If we're a child, save contents and tell parent about write. */
561 if (control_fd != -1) {
562 struct write_info *winfo = new_write();
563 enum info_type type = WRITE;
567 winfo->hdr.len = count;
569 winfo->data = malloc(count);
570 memcpy(winfo->data, buf, count);
571 winfo->hdr.offset = offset;
572 if (winfo->hdr.offset != (off_t)-1) {
573 lseek(fd, offset, SEEK_SET);
574 winfo->olddata = malloc(count);
575 winfo->oldlen = read(fd, winfo->olddata, count);
576 if (winfo->oldlen == -1)
579 write_all(control_fd, &type, sizeof(type));
580 write_all(control_fd, &winfo->hdr, sizeof(winfo->hdr));
581 write_all(control_fd, winfo->data, count);
584 /* FIXME: Try partial write returns. */
585 if (should_fail(p)) {
589 /* FIXME: We assume same write order in parent and child */
590 if (child_writes_num != 0) {
591 if (child_writes[0].hdr.fd != fd)
592 errx(1, "Child wrote to fd %u, not %u?",
593 child_writes[0].hdr.fd, fd);
594 if (child_writes[0].hdr.offset != offset)
595 errx(1, "Child wrote to offset %zu, not %zu?",
596 (size_t)child_writes[0].hdr.offset,
598 if (child_writes[0].hdr.len != count)
599 errx(1, "Child wrote length %zu, not %zu?",
600 child_writes[0].hdr.len, count);
601 if (memcmp(child_writes[0].data, buf, count)) {
603 "Child wrote differently to"
604 " fd %u than we did!\n", fd);
606 free(child_writes[0].data);
608 memmove(&child_writes[0], &child_writes[1],
609 sizeof(child_writes[0]) * child_writes_num);
611 /* Is this is a socket or pipe, child wrote it
613 if (offset == (off_t)-1) {
614 p->u.write.ret = count;
616 return p->u.write.ret;
619 p->u.write.ret = pwrite(fd, buf, count, off);
622 return p->u.write.ret;
625 ssize_t failtest_read(int fd, void *buf, size_t count,
626 const char *file, unsigned line)
628 return failtest_pread(fd, buf, count, lseek(fd, 0, SEEK_CUR),
632 ssize_t failtest_write(int fd, const void *buf, size_t count,
633 const char *file, unsigned line)
635 return failtest_pwrite(fd, buf, count, lseek(fd, 0, SEEK_CUR),
639 static struct lock_info *WARN_UNUSED_RESULT
640 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
645 for (i = 0; i < lock_num; i++) {
650 /* Four cases we care about:
664 if (start > l->start && end < l->end) {
665 /* Mid overlap: trim entry, add new one. */
666 off_t new_start, new_end;
670 locks = add_lock(locks,
671 fd, new_start, new_end, l->type);
673 } else if (start <= l->start && end >= l->end) {
674 /* Total overlap: eliminate entry. */
677 } else if (end >= l->start && end < l->end) {
678 /* Start overlap: trim entry. */
680 } else if (start > l->start && start <= l->end) {
681 /* End overlap: trim entry. */
684 /* Nothing left? Remove it. */
685 if (l->end < l->start) {
686 memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
691 if (type != F_UNLCK) {
692 locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
693 l = &locks[lock_num++];
702 /* We only trap this so we can dup fds in case we need to restore. */
703 int failtest_close(int fd)
708 for (i = 0; i < fd_orig_num; i++) {
709 if (fd_orig[i].fd == fd) {
710 fd_orig[i].fd = newfd = dup(fd);
711 fd_orig[i].dupped = true;
715 for (i = 0; i < writes_num; i++) {
716 if (writes[i].hdr.fd == fd)
717 writes[i].hdr.fd = newfd;
720 locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
724 /* Zero length means "to end of file" */
725 static off_t end_of(off_t start, off_t len)
729 return start + len - 1;
732 /* FIXME: This only handles locks, really. */
733 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
735 struct failtest_call *p;
736 struct fcntl_call call;
742 /* Argument extraction. */
747 call.arg.l = va_arg(ap, long);
749 return fcntl(fd, cmd, call.arg.l);
752 return fcntl(fd, cmd);
756 call.arg.fl = *va_arg(ap, struct flock *);
758 return fcntl(fd, cmd, &call.arg.fl);
762 call.arg.fl = *va_arg(ap, struct flock *);
766 /* This means you need to implement it here. */
767 err(1, "failtest: unknown fcntl %u", cmd);
770 p = add_history(FAILTEST_FCNTL, file, line, &call);
773 if (should_fail(p)) {
775 if (p->u.fcntl.cmd == F_SETLK)
780 p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
782 if (p->u.fcntl.ret == -1)
785 /* We don't handle anything else yet. */
786 assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
787 locks = add_lock(locks,
789 p->u.fcntl.arg.fl.l_start,
790 end_of(p->u.fcntl.arg.fl.l_start,
791 p->u.fcntl.arg.fl.l_len),
792 p->u.fcntl.arg.fl.l_type);
796 return p->u.fcntl.ret;
799 void failtest_init(int argc, char *argv[])
803 for (i = 1; i < argc; i++) {
804 if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) {
805 failpath = argv[i] + strlen("--failpath=");
806 } else if (strcmp(argv[i], "--tracepath") == 0) {
807 tracefd = dup(STDERR_FILENO);
808 failtest_timeout_ms = -1;
811 gettimeofday(&start, NULL);
814 /* Free up memory, so valgrind doesn't report leaks. */
815 static void free_everything(void)
819 for (i = 0; i < writes_num; i++) {
820 free(writes[i].data);
821 if (writes[i].hdr.offset != (off_t)-1)
822 free(writes[i].olddata);
826 for (i = 0; i < history_num; i++) {
827 if (history[i].type == FAILTEST_OPEN)
828 free((char *)history[i].u.open.pathname);
833 void failtest_exit(int status)
837 if (control_fd == -1) {
842 if (failtest_exit_check) {
843 if (!failtest_exit_check(history, history_num))
844 child_fail(NULL, 0, "failtest_exit_check failed\n");
847 /* Restore any stuff we overwrote. */
848 for (i = 0; i < writes_num; i++) {
849 if (writes[i].hdr.offset == (off_t)-1)
851 if (writes[i].oldlen != 0) {
852 lseek(writes[i].hdr.fd, writes[i].hdr.offset,
854 write(writes[i].hdr.fd, writes[i].olddata,
859 /* Fix up fd offsets, restore sizes. */
860 for (i = 0; i < fd_orig_num; i++) {
861 lseek(fd_orig[i].fd, fd_orig[i].offset, SEEK_SET);
862 ftruncate(fd_orig[i].fd, fd_orig[i].size);
863 /* Free up any file descriptors we dup'ed. */
864 if (fd_orig[i].dupped)
865 close(fd_orig[i].fd);
869 tell_parent(SUCCESS);