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 /* + means continue after end, like normal. */
284 if (*failpath == '+')
287 if (tolower(*failpath) != info_to_arg[call->type])
288 errx(1, "Failpath expected '%c' got '%c'\n",
289 info_to_arg[call->type], *failpath);
290 call->fail = isupper(*(failpath++));
295 if (!failtest_hook(history, history_num)) {
300 /* We're going to fail in the child. */
302 if (pipe(control) != 0 || pipe(output) != 0)
303 err(1, "opening pipe");
305 /* Prevent double-printing (in child and parent) */
309 err(1, "forking failed");
315 gettimeofday(&now, NULL);
316 if (now.tv_usec < start.tv_usec) {
318 now.tv_usec += 1000000;
320 now.tv_usec -= start.tv_usec;
321 now.tv_sec -= start.tv_sec;
322 sprintf(str, "%u (%u.%02u): ", getpid(),
323 (int)now.tv_sec, (int)now.tv_usec / 10000);
325 p = failpath_string();
329 p = strchr(history[history_num-1].file, '/');
333 trace_str(history[history_num-1].file);
334 sprintf(str, ":%u)\n", history[history_num-1].line);
339 dup2(output[1], STDOUT_FILENO);
340 dup2(output[1], STDERR_FILENO);
341 if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
343 control_fd = control[1];
347 signal(SIGUSR1, hand_down);
352 /* We grab output so we can display it; we grab writes so we
355 struct pollfd pfd[2];
358 pfd[0].fd = output[0];
359 pfd[0].events = POLLIN|POLLHUP;
360 pfd[1].fd = control[0];
361 pfd[1].events = POLLIN|POLLHUP;
364 ret = poll(pfd, 1, failtest_timeout_ms);
366 ret = poll(pfd, 2, failtest_timeout_ms);
371 if (pfd[0].revents & POLLIN) {
374 out = realloc(out, outlen + 8192);
375 len = read(output[0], out + outlen, 8192);
377 } else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
378 if (read_all(control[0], &type, sizeof(type))) {
380 if (!read_write_info(control[0]))
382 } else if (type == RELEASE_LOCKS) {
384 /* FIXME: Tell them we're done... */
387 } else if (pfd[0].revents & POLLHUP) {
390 } while (type != FAILURE);
394 waitpid(child, &status, 0);
395 if (!WIFEXITED(status))
396 child_fail(out, outlen, "Killed by signal %u: ",
398 /* Child printed failure already, just pass up exit code. */
399 if (type == FAILURE) {
400 fprintf(stderr, "%.*s", (int)outlen, out);
402 exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
404 if (WEXITSTATUS(status) != 0)
405 child_fail(out, outlen, "Exited with status %i: ",
406 WEXITSTATUS(status));
409 signal(SIGUSR1, SIG_DFL);
411 /* We continue onwards without failing. */
416 void *failtest_calloc(size_t nmemb, size_t size,
417 const char *file, unsigned line)
419 struct failtest_call *p;
420 struct calloc_call call;
423 p = add_history(FAILTEST_CALLOC, file, line, &call);
425 if (should_fail(p)) {
426 p->u.calloc.ret = NULL;
429 p->u.calloc.ret = calloc(nmemb, size);
432 return p->u.calloc.ret;
435 void *failtest_malloc(size_t size, const char *file, unsigned line)
437 struct failtest_call *p;
438 struct malloc_call call;
441 p = add_history(FAILTEST_MALLOC, file, line, &call);
442 if (should_fail(p)) {
443 p->u.calloc.ret = NULL;
446 p->u.calloc.ret = malloc(size);
449 return p->u.calloc.ret;
452 void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
454 struct failtest_call *p;
455 struct realloc_call call;
457 p = add_history(FAILTEST_REALLOC, file, line, &call);
459 /* FIXME: Try one child moving allocation, one not. */
460 if (should_fail(p)) {
461 p->u.realloc.ret = NULL;
464 p->u.realloc.ret = realloc(ptr, size);
467 return p->u.realloc.ret;
470 int failtest_open(const char *pathname,
471 const char *file, unsigned line, ...)
473 struct failtest_call *p;
474 struct open_call call;
477 call.pathname = strdup(pathname);
479 call.flags = va_arg(ap, int);
480 if (call.flags & O_CREAT) {
481 call.mode = va_arg(ap, mode_t);
484 p = add_history(FAILTEST_OPEN, file, line, &call);
485 /* Avoid memory leak! */
486 if (p == &unrecorded_call)
487 free((char *)call.pathname);
488 if (should_fail(p)) {
490 /* FIXME: Play with error codes? */
493 p->u.open.ret = open(pathname, call.flags, call.mode);
496 return p->u.open.ret;
499 int failtest_pipe(int pipefd[2], const char *file, unsigned line)
501 struct failtest_call *p;
502 struct pipe_call call;
504 p = add_history(FAILTEST_PIPE, file, line, &call);
505 if (should_fail(p)) {
507 /* FIXME: Play with error codes? */
510 p->u.pipe.ret = pipe(p->u.pipe.fds);
512 /* This causes valgrind to notice if they use pipefd[] after failure */
513 memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
515 return p->u.pipe.ret;
518 ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
519 const char *file, unsigned line)
521 struct failtest_call *p;
522 struct read_call call;
527 p = add_history(FAILTEST_READ, file, line, &call);
529 /* This is going to change seek offset, so save it. */
530 if (control_fd != -1)
533 /* FIXME: Try partial read returns. */
534 if (should_fail(p)) {
538 p->u.read.ret = pread(fd, buf, count, off);
541 return p->u.read.ret;
544 static struct write_info *new_write(void)
546 writes = realloc(writes, (writes_num + 1) * sizeof(*writes));
547 return &writes[writes_num++];
550 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t off,
551 const char *file, unsigned line)
553 struct failtest_call *p;
554 struct write_call call;
561 p = add_history(FAILTEST_WRITE, file, line, &call);
563 offset = lseek(fd, 0, SEEK_CUR);
565 /* If we're a child, save contents and tell parent about write. */
566 if (control_fd != -1) {
567 struct write_info *winfo = new_write();
568 enum info_type type = WRITE;
572 winfo->hdr.len = count;
574 winfo->data = malloc(count);
575 memcpy(winfo->data, buf, count);
576 winfo->hdr.offset = offset;
577 if (winfo->hdr.offset != (off_t)-1) {
578 lseek(fd, offset, SEEK_SET);
579 winfo->olddata = malloc(count);
580 winfo->oldlen = read(fd, winfo->olddata, count);
581 if (winfo->oldlen == -1)
584 write_all(control_fd, &type, sizeof(type));
585 write_all(control_fd, &winfo->hdr, sizeof(winfo->hdr));
586 write_all(control_fd, winfo->data, count);
589 /* FIXME: Try partial write returns. */
590 if (should_fail(p)) {
594 /* FIXME: We assume same write order in parent and child */
595 if (child_writes_num != 0) {
596 if (child_writes[0].hdr.fd != fd)
597 errx(1, "Child wrote to fd %u, not %u?",
598 child_writes[0].hdr.fd, fd);
599 if (child_writes[0].hdr.offset != offset)
600 errx(1, "Child wrote to offset %zu, not %zu?",
601 (size_t)child_writes[0].hdr.offset,
603 if (child_writes[0].hdr.len != count)
604 errx(1, "Child wrote length %zu, not %zu?",
605 child_writes[0].hdr.len, count);
606 if (memcmp(child_writes[0].data, buf, count)) {
608 "Child wrote differently to"
609 " fd %u than we did!\n", fd);
611 free(child_writes[0].data);
613 memmove(&child_writes[0], &child_writes[1],
614 sizeof(child_writes[0]) * child_writes_num);
616 /* Is this is a socket or pipe, child wrote it
618 if (offset == (off_t)-1) {
619 p->u.write.ret = count;
621 return p->u.write.ret;
624 p->u.write.ret = pwrite(fd, buf, count, off);
627 return p->u.write.ret;
630 ssize_t failtest_read(int fd, void *buf, size_t count,
631 const char *file, unsigned line)
633 return failtest_pread(fd, buf, count, lseek(fd, 0, SEEK_CUR),
637 ssize_t failtest_write(int fd, const void *buf, size_t count,
638 const char *file, unsigned line)
640 return failtest_pwrite(fd, buf, count, lseek(fd, 0, SEEK_CUR),
644 static struct lock_info *WARN_UNUSED_RESULT
645 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
650 for (i = 0; i < lock_num; i++) {
655 /* Four cases we care about:
669 if (start > l->start && end < l->end) {
670 /* Mid overlap: trim entry, add new one. */
671 off_t new_start, new_end;
675 locks = add_lock(locks,
676 fd, new_start, new_end, l->type);
678 } else if (start <= l->start && end >= l->end) {
679 /* Total overlap: eliminate entry. */
682 } else if (end >= l->start && end < l->end) {
683 /* Start overlap: trim entry. */
685 } else if (start > l->start && start <= l->end) {
686 /* End overlap: trim entry. */
689 /* Nothing left? Remove it. */
690 if (l->end < l->start) {
691 memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
696 if (type != F_UNLCK) {
697 locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
698 l = &locks[lock_num++];
707 /* We only trap this so we can dup fds in case we need to restore. */
708 int failtest_close(int fd)
713 for (i = 0; i < fd_orig_num; i++) {
714 if (fd_orig[i].fd == fd) {
715 fd_orig[i].fd = newfd = dup(fd);
716 fd_orig[i].dupped = true;
720 for (i = 0; i < writes_num; i++) {
721 if (writes[i].hdr.fd == fd)
722 writes[i].hdr.fd = newfd;
725 locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
729 /* Zero length means "to end of file" */
730 static off_t end_of(off_t start, off_t len)
734 return start + len - 1;
737 /* FIXME: This only handles locks, really. */
738 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
740 struct failtest_call *p;
741 struct fcntl_call call;
747 /* Argument extraction. */
752 call.arg.l = va_arg(ap, long);
754 return fcntl(fd, cmd, call.arg.l);
757 return fcntl(fd, cmd);
761 call.arg.fl = *va_arg(ap, struct flock *);
763 return fcntl(fd, cmd, &call.arg.fl);
767 call.arg.fl = *va_arg(ap, struct flock *);
771 /* This means you need to implement it here. */
772 err(1, "failtest: unknown fcntl %u", cmd);
775 p = add_history(FAILTEST_FCNTL, file, line, &call);
778 if (should_fail(p)) {
780 if (p->u.fcntl.cmd == F_SETLK)
785 p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
787 if (p->u.fcntl.ret == -1)
790 /* We don't handle anything else yet. */
791 assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
792 locks = add_lock(locks,
794 p->u.fcntl.arg.fl.l_start,
795 end_of(p->u.fcntl.arg.fl.l_start,
796 p->u.fcntl.arg.fl.l_len),
797 p->u.fcntl.arg.fl.l_type);
801 return p->u.fcntl.ret;
804 void failtest_init(int argc, char *argv[])
808 for (i = 1; i < argc; i++) {
809 if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) {
810 failpath = argv[i] + strlen("--failpath=");
811 } else if (strcmp(argv[i], "--tracepath") == 0) {
812 tracefd = dup(STDERR_FILENO);
813 failtest_timeout_ms = -1;
816 gettimeofday(&start, NULL);
819 /* Free up memory, so valgrind doesn't report leaks. */
820 static void free_everything(void)
824 for (i = 0; i < writes_num; i++) {
825 free(writes[i].data);
826 if (writes[i].hdr.offset != (off_t)-1)
827 free(writes[i].olddata);
831 for (i = 0; i < history_num; i++) {
832 if (history[i].type == FAILTEST_OPEN)
833 free((char *)history[i].u.open.pathname);
838 void failtest_exit(int status)
842 if (control_fd == -1) {
847 if (failtest_exit_check) {
848 if (!failtest_exit_check(history, history_num))
849 child_fail(NULL, 0, "failtest_exit_check failed\n");
852 /* Restore any stuff we overwrote. */
853 for (i = 0; i < writes_num; i++) {
854 if (writes[i].hdr.offset == (off_t)-1)
856 if (writes[i].oldlen != 0) {
857 lseek(writes[i].hdr.fd, writes[i].hdr.offset,
859 write(writes[i].hdr.fd, writes[i].olddata,
864 /* Fix up fd offsets, restore sizes. */
865 for (i = 0; i < fd_orig_num; i++) {
866 lseek(fd_orig[i].fd, fd_orig[i].offset, SEEK_SET);
867 ftruncate(fd_orig[i].fd, fd_orig[i].size);
868 /* Free up any file descriptors we dup'ed. */
869 if (fd_orig[i].dupped)
870 close(fd_orig[i].fd);
874 tell_parent(SUCCESS);