]> git.ozlabs.org Git - ccan/blob - ccan/failtest/failtest.c
failtest: simplify FAIL_PROBE.
[ccan] / ccan / failtest / failtest.c
1 /* Licensed under LGPL - see LICENSE file for details */
2 #include <ccan/failtest/failtest.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <ctype.h>
8 #include <err.h>
9 #include <unistd.h>
10 #include <poll.h>
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #include <signal.h>
17 #include <assert.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/str/str.h>
23
24 enum failtest_result (*failtest_hook)(struct tlist_calls *);
25
26 static int tracefd = -1;
27 static int warnfd;
28
29 unsigned int failtest_timeout_ms = 20000;
30
31 const char *failpath;
32 const char *debugpath;
33
34 enum info_type {
35         WRITE,
36         RELEASE_LOCKS,
37         FAILURE,
38         SUCCESS,
39         UNEXPECTED
40 };
41
42 struct lock_info {
43         int fd;
44         /* end is inclusive: you can't have a 0-byte lock. */
45         off_t start, end;
46         int type;
47 };
48
49 bool (*failtest_exit_check)(struct tlist_calls *history);
50
51 static struct tlist_calls history = TLIST_INIT(history);
52 static int control_fd = -1;
53 static struct timeval start;
54 static bool probing = false;
55
56 static struct write_call *child_writes = NULL;
57 static unsigned int child_writes_num = 0;
58
59 static pid_t lock_owner;
60 static struct lock_info *locks = NULL;
61 static unsigned int lock_num = 0;
62
63 static pid_t orig_pid;
64
65 static const char info_to_arg[] = "mceoxprwf";
66
67 /* Dummy call used for failtest_undo wrappers. */
68 static struct failtest_call unrecorded_call;
69
70 static struct failtest_call *add_history_(enum failtest_call_type type,
71                                           const char *file,
72                                           unsigned int line,
73                                           const void *elem,
74                                           size_t elem_size)
75 {
76         struct failtest_call *call;
77
78         /* NULL file is how we suppress failure. */
79         if (!file)
80                 return &unrecorded_call;
81
82         call = malloc(sizeof *call);
83         call->type = type;
84         call->file = file;
85         call->line = line;
86         call->cleanup = NULL;
87         memcpy(&call->u, elem, elem_size);
88         tlist_add_tail(&history, call, list);
89         return call;
90 }
91
92 #define add_history(type, file, line, elem) \
93         add_history_((type), (file), (line), (elem), sizeof(*(elem)))
94
95 /* We do a fake call inside a sizeof(), to check types. */
96 #define set_cleanup(call, clean, type)                  \
97         (call)->cleanup = (void *)((void)sizeof(clean((type *)NULL),1), (clean))
98
99
100 /* Dup the fd to a high value (out of the way I hope!), and close the old fd. */
101 static int move_fd_to_high(int fd)
102 {
103         int i;
104
105         for (i = FD_SETSIZE - 1; i >= 0; i--) {
106                 if (fcntl(i, F_GETFL) == -1 && errno == EBADF) {
107                         if (dup2(fd, i) == -1)
108                                 err(1, "Failed to dup fd %i to %i", fd, i);
109                         close(fd);
110                         return i;
111                 }
112         }
113         /* Nothing?  Really?  Er... ok? */
114         return fd;
115 }
116
117 static bool read_write_info(int fd)
118 {
119         struct write_call *w;
120         char *buf;
121
122         /* We don't need all of this, but it's simple. */
123         child_writes = realloc(child_writes,
124                                (child_writes_num+1) * sizeof(child_writes[0]));
125         w = &child_writes[child_writes_num];
126         if (!read_all(fd, w, sizeof(*w)))
127                 return false;
128
129         w->buf = buf = malloc(w->count);
130         if (!read_all(fd, buf, w->count))
131                 return false;
132
133         child_writes_num++;
134         return true;
135 }
136
137 static char *failpath_string(void)
138 {
139         struct failtest_call *i;
140         char *ret = strdup("");
141         unsigned len = 0;
142
143         /* Inefficient, but who cares? */
144         tlist_for_each(&history, i, list) {
145                 ret = realloc(ret, len + 2);
146                 ret[len] = info_to_arg[i->type];
147                 if (i->fail)
148                         ret[len] = toupper(ret[len]);
149                 ret[++len] = '\0';
150         }
151         return ret;
152 }
153
154 static void warn_via_fd(int e, const char *fmt, va_list ap)
155 {
156         char *p = failpath_string();
157
158         vdprintf(warnfd, fmt, ap);
159         if (e != -1)
160                 dprintf(warnfd, ": %s", strerror(e));
161         dprintf(warnfd, " [%s]\n", p);
162         free(p);
163 }
164
165 static void fwarn(const char *fmt, ...)
166 {
167         va_list ap;
168         int e = errno;
169
170         va_start(ap, fmt);
171         warn_via_fd(e, fmt, ap);
172         va_end(ap);
173 }
174
175
176 static void fwarnx(const char *fmt, ...)
177 {
178         va_list ap;
179
180         va_start(ap, fmt);
181         warn_via_fd(-1, fmt, ap);
182         va_end(ap);
183 }
184
185 static void tell_parent(enum info_type type)
186 {
187         if (control_fd != -1)
188                 write_all(control_fd, &type, sizeof(type));
189 }
190
191 static void child_fail(const char *out, size_t outlen, const char *fmt, ...)
192 {
193         va_list ap;
194         char *path = failpath_string();
195
196         va_start(ap, fmt);
197         vfprintf(stderr, fmt, ap);
198         va_end(ap);
199
200         fprintf(stderr, "%.*s", (int)outlen, out);
201         printf("To reproduce: --failpath=%s\n", path);
202         free(path);
203         tell_parent(FAILURE);
204         exit(1);
205 }
206
207 static void trace(const char *fmt, ...)
208 {
209         va_list ap;
210
211         if (tracefd == -1)
212                 return;
213
214         va_start(ap, fmt);
215         vdprintf(tracefd, fmt, ap);
216         va_end(ap);
217 }
218
219 static pid_t child;
220
221 static void hand_down(int signum)
222 {
223         kill(child, signum);
224 }
225
226 static void release_locks(void)
227 {
228         /* Locks were never acquired/reacquired? */
229         if (lock_owner == 0)
230                 return;
231
232         /* We own them?  Release them all. */
233         if (lock_owner == getpid()) {
234                 unsigned int i;
235                 struct flock fl;
236                 fl.l_type = F_UNLCK;
237                 fl.l_whence = SEEK_SET;
238                 fl.l_start = 0;
239                 fl.l_len = 0;
240
241                 for (i = 0; i < lock_num; i++)
242                         fcntl(locks[i].fd, F_SETLK, &fl);
243         } else {
244                 /* Our parent must have them; pass request up. */
245                 enum info_type type = RELEASE_LOCKS;
246                 assert(control_fd != -1);
247                 write_all(control_fd, &type, sizeof(type));
248         }
249         lock_owner = 0;
250 }
251
252 /* off_t is a signed type.  Getting its max is non-trivial. */
253 static off_t off_max(void)
254 {
255         BUILD_ASSERT(sizeof(off_t) == 4 || sizeof(off_t) == 8);
256         if (sizeof(off_t) == 4)
257                 return (off_t)0x7FFFFFF;
258         else
259                 return (off_t)0x7FFFFFFFFFFFFFFULL;
260 }
261
262 static void get_locks(void)
263 {
264         unsigned int i;
265         struct flock fl;
266
267         if (lock_owner == getpid())
268                 return;
269
270         if (lock_owner != 0) {
271                 enum info_type type = RELEASE_LOCKS;
272                 assert(control_fd != -1);
273                 write_all(control_fd, &type, sizeof(type));
274         }
275
276         fl.l_whence = SEEK_SET;
277
278         for (i = 0; i < lock_num; i++) {
279                 fl.l_type = locks[i].type;
280                 fl.l_start = locks[i].start;
281                 if (locks[i].end == off_max())
282                         fl.l_len = 0;
283                 else
284                         fl.l_len = locks[i].end - locks[i].start + 1;
285
286                 if (fcntl(locks[i].fd, F_SETLKW, &fl) != 0)
287                         abort();
288         }
289         lock_owner = getpid();
290 }
291
292 struct saved_file {
293         struct saved_file *next;
294         int fd;
295         void *contents;
296         off_t off, len;
297 };
298
299 static struct saved_file *save_file(struct saved_file *next, int fd)
300 {
301         struct saved_file *s = malloc(sizeof(*s));
302
303         s->next = next;
304         s->fd = fd;
305         s->off = lseek(fd, 0, SEEK_CUR);
306         /* Special file?  Erk... */
307         assert(s->off != -1);
308         s->len = lseek(fd, 0, SEEK_END);
309         lseek(fd, 0, SEEK_SET);
310         s->contents = malloc(s->len);
311         if (read(fd, s->contents, s->len) != s->len)
312                 err(1, "Failed to save %zu bytes", (size_t)s->len);
313         lseek(fd, s->off, SEEK_SET);
314         return s;
315 }
316         
317 /* We have little choice but to save and restore open files: mmap means we
318  * can really intercept changes in the child.
319  *
320  * We could do non-mmap'ed files on demand, however. */
321 static struct saved_file *save_files(void)
322 {
323         struct saved_file *files = NULL;
324         struct failtest_call *i;
325
326         /* Figure out the set of live fds. */
327         tlist_for_each_rev(&history, i, list) {
328                 if (i->type == FAILTEST_OPEN) {
329                         int fd = i->u.open.ret;
330                         /* Only do successful, writable fds. */
331                         if (fd < 0)
332                                 continue;
333
334                         /* If it was closed, cleanup == NULL. */
335                         if (!i->cleanup)
336                                 continue;
337
338                         if ((i->u.open.flags & O_RDWR) == O_RDWR) {
339                                 files = save_file(files, fd);
340                         } else if ((i->u.open.flags & O_WRONLY)
341                                    == O_WRONLY) {
342                                 /* FIXME: Handle O_WRONLY.  Open with O_RDWR? */
343                                 abort();
344                         }
345                 }
346         }
347
348         return files;
349 }
350
351 static void restore_files(struct saved_file *s)
352 {
353         while (s) {
354                 struct saved_file *next = s->next;
355
356                 lseek(s->fd, 0, SEEK_SET);
357                 if (write(s->fd, s->contents, s->len) != s->len)
358                         err(1, "Failed to restore %zu bytes", (size_t)s->len);
359                 if (ftruncate(s->fd, s->len) != 0)
360                         err(1, "Failed to trim file to length %zu",
361                             (size_t)s->len);
362                 free(s->contents);
363                 lseek(s->fd, s->off, SEEK_SET);
364                 free(s);
365                 s = next;
366         }
367 }
368
369 static void free_files(struct saved_file *s)
370 {
371         while (s) {
372                 struct saved_file *next = s->next;
373                 free(s->contents);
374                 free(s);
375                 s = next;
376         }
377 }
378
379 static void free_call(struct failtest_call *call)
380 {
381         /* We don't do this in cleanup: needed even for failed opens. */
382         if (call->type == FAILTEST_OPEN)
383                 free((char *)call->u.open.pathname);
384         tlist_del_from(&history, call, list);
385         free(call);
386 }
387
388 /* Free up memory, so valgrind doesn't report leaks. */
389 static void free_everything(void)
390 {
391         struct failtest_call *i;
392
393         while ((i = tlist_top(&history, struct failtest_call, list)) != NULL)
394                 free_call(i);
395 }
396
397 static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
398 {
399         struct failtest_call *i;
400
401         /* For children, we don't care if they "failed" the testing. */
402         if (control_fd != -1)
403                 status = 0;
404
405         if (forced_cleanup) {
406                 /* We didn't actually do final operation: remove it. */
407                 i = tlist_tail(&history, struct failtest_call, list);
408                 free_call(i);
409         }
410
411         /* Cleanup everything, in reverse order. */
412         tlist_for_each_rev(&history, i, list) {
413                 if (!i->cleanup)
414                         continue;
415                 if (!forced_cleanup) {
416                         printf("Leak at %s:%u: --failpath=%s\n",
417                                i->file, i->line, failpath_string());
418                         status = 1;
419                 }
420                 i->cleanup(&i->u);
421         }
422
423         free_everything();
424         tell_parent(SUCCESS);
425         exit(status);
426 }
427
428 static bool should_fail(struct failtest_call *call)
429 {
430         int status;
431         int control[2], output[2];
432         enum info_type type = UNEXPECTED;
433         char *out = NULL;
434         size_t outlen = 0;
435         struct saved_file *files;
436
437         if (call == &unrecorded_call)
438                 return false;
439
440         if (failpath) {
441                 /* + means continue after end, like normal. */
442                 if (*failpath == '+')
443                         failpath = NULL;
444                 else if (*failpath == '\0') {
445                         /* Continue, but don't inject errors. */
446                         return call->fail = false;
447                 } else {
448                         if (tolower((unsigned char)*failpath)
449                             != info_to_arg[call->type])
450                                 errx(1, "Failpath expected '%c' got '%c'\n",
451                                      info_to_arg[call->type], *failpath);
452                         call->fail = cisupper(*(failpath++));
453                         return call->fail;
454                 }
455         }
456
457         /* Attach debugger if they asked for it. */
458         if (debugpath) {
459                 char *path = failpath_string();
460
461                 if (streq(path, debugpath)) {
462                         char str[80];
463
464                         /* Don't timeout. */
465                         signal(SIGUSR1, SIG_IGN);
466                         sprintf(str, "xterm -e gdb /proc/%d/exe %d &",
467                                 getpid(), getpid());
468                         if (system(str) == 0)
469                                 sleep(5);
470                 } else if (!strstarts(path, debugpath)) {
471                         fprintf(stderr, "--debugpath not followed: %s\n", path);
472                         debugpath = NULL;
473                 }
474                 free(path);
475         }
476
477         /* Are we probing?  If so, we never fail twice. */
478         if (probing)
479                 return call->fail = false;
480
481         if (failtest_hook) {
482                 switch (failtest_hook(&history)) {
483                 case FAIL_OK:
484                         break;
485                 case FAIL_PROBE:
486                         probing = true;
487                         break;
488                 case FAIL_DONT_FAIL:
489                         call->fail = false;
490                         return false;
491                 default:
492                         abort();
493                 }
494         }
495
496         files = save_files();
497
498         /* We're going to fail in the child. */
499         call->fail = true;
500         if (pipe(control) != 0 || pipe(output) != 0)
501                 err(1, "opening pipe");
502
503         /* Prevent double-printing (in child and parent) */
504         fflush(stdout);
505         child = fork();
506         if (child == -1)
507                 err(1, "forking failed");
508
509         if (child == 0) {
510                 if (tracefd != -1) {
511                         struct timeval diff;
512                         const char *p;
513                         char *failpath;
514                         struct failtest_call *c;
515
516                         c = tlist_tail(&history, struct failtest_call, list);
517                         diff = time_sub(time_now(), start);
518                         failpath = failpath_string();
519                         trace("%u->%u (%u.%02u): %s (", getppid(), getpid(),
520                               (int)diff.tv_sec, (int)diff.tv_usec / 10000,
521                               failpath);
522                         free(failpath);
523                         p = strrchr(c->file, '/');
524                         if (p)
525                                 trace("%s", p+1);
526                         else
527                                 trace("%s", c->file);
528                         trace(":%u)\n", c->line);
529                 }
530                 close(control[0]);
531                 close(output[0]);
532                 dup2(output[1], STDOUT_FILENO);
533                 dup2(output[1], STDERR_FILENO);
534                 if (output[1] != STDOUT_FILENO && output[1] != STDERR_FILENO)
535                         close(output[1]);
536                 control_fd = move_fd_to_high(control[1]);
537                 /* Valgrind spots the leak if we don't free these. */
538                 free_files(files);
539                 return true;
540         }
541
542         signal(SIGUSR1, hand_down);
543
544         close(control[1]);
545         close(output[1]);
546
547         /* We grab output so we can display it; we grab writes so we
548          * can compare. */
549         do {
550                 struct pollfd pfd[2];
551                 int ret;
552
553                 pfd[0].fd = output[0];
554                 pfd[0].events = POLLIN|POLLHUP;
555                 pfd[1].fd = control[0];
556                 pfd[1].events = POLLIN|POLLHUP;
557
558                 if (type == SUCCESS)
559                         ret = poll(pfd, 1, failtest_timeout_ms);
560                 else
561                         ret = poll(pfd, 2, failtest_timeout_ms);
562
563                 if (ret == 0)
564                         hand_down(SIGUSR1);
565                 if (ret < 0) {
566                         if (errno == EINTR)
567                                 continue;
568                         err(1, "Poll returned %i", ret);
569                 }
570
571                 if (pfd[0].revents & POLLIN) {
572                         ssize_t len;
573
574                         out = realloc(out, outlen + 8192);
575                         len = read(output[0], out + outlen, 8192);
576                         outlen += len;
577                 } else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
578                         if (read_all(control[0], &type, sizeof(type))) {
579                                 if (type == WRITE) {
580                                         if (!read_write_info(control[0]))
581                                                 break;
582                                 } else if (type == RELEASE_LOCKS) {
583                                         release_locks();
584                                         /* FIXME: Tell them we're done... */
585                                 }
586                         }
587                 } else if (pfd[0].revents & POLLHUP) {
588                         break;
589                 }
590         } while (type != FAILURE);
591
592         close(output[0]);
593         close(control[0]);
594         waitpid(child, &status, 0);
595         if (!WIFEXITED(status)) {
596                 if (WTERMSIG(status) == SIGUSR1)
597                         child_fail(out, outlen, "Timed out");
598                 else
599                         child_fail(out, outlen, "Killed by signal %u: ",
600                                    WTERMSIG(status));
601         }
602         /* Child printed failure already, just pass up exit code. */
603         if (type == FAILURE) {
604                 fprintf(stderr, "%.*s", (int)outlen, out);
605                 tell_parent(type);
606                 exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
607         }
608         if (WEXITSTATUS(status) != 0)
609                 child_fail(out, outlen, "Exited with status %i: ",
610                            WEXITSTATUS(status));
611
612         free(out);
613         signal(SIGUSR1, SIG_DFL);
614
615         restore_files(files);
616
617         /* Only child does probe. */
618         probing = false;
619
620         /* We continue onwards without failing. */
621         call->fail = false;
622         return false;
623 }
624
625 static void cleanup_calloc(struct calloc_call *call)
626 {
627         free(call->ret);
628 }
629
630 void *failtest_calloc(size_t nmemb, size_t size,
631                       const char *file, unsigned line)
632 {
633         struct failtest_call *p;
634         struct calloc_call call;
635         call.nmemb = nmemb;
636         call.size = size;
637         p = add_history(FAILTEST_CALLOC, file, line, &call);
638
639         if (should_fail(p)) {
640                 p->u.calloc.ret = NULL;
641                 p->error = ENOMEM;
642         } else {
643                 p->u.calloc.ret = calloc(nmemb, size);
644                 set_cleanup(p, cleanup_calloc, struct calloc_call);
645         }
646         errno = p->error;
647         return p->u.calloc.ret;
648 }
649
650 static void cleanup_malloc(struct malloc_call *call)
651 {
652         free(call->ret);
653 }
654
655 void *failtest_malloc(size_t size, const char *file, unsigned line)
656 {
657         struct failtest_call *p;
658         struct malloc_call call;
659         call.size = size;
660
661         p = add_history(FAILTEST_MALLOC, file, line, &call);
662         if (should_fail(p)) {
663                 p->u.malloc.ret = NULL;
664                 p->error = ENOMEM;
665         } else {
666                 p->u.malloc.ret = malloc(size);
667                 set_cleanup(p, cleanup_malloc, struct malloc_call);
668         }
669         errno = p->error;
670         return p->u.malloc.ret;
671 }
672
673 static void cleanup_realloc(struct realloc_call *call)
674 {
675         free(call->ret);
676 }
677
678 /* Walk back and find out if we got this ptr from a previous routine. */
679 static void fixup_ptr_history(void *ptr)
680 {
681         struct failtest_call *i;
682
683         /* Start at end of history, work back. */
684         tlist_for_each_rev(&history, i, list) {
685                 switch (i->type) {
686                 case FAILTEST_REALLOC:
687                         if (i->u.realloc.ret == ptr) {
688                                 i->cleanup = NULL;
689                                 return;
690                         }
691                         break;
692                 case FAILTEST_MALLOC:
693                         if (i->u.malloc.ret == ptr) {
694                                 i->cleanup = NULL;
695                                 return;
696                         }
697                         break;
698                 case FAILTEST_CALLOC:
699                         if (i->u.calloc.ret == ptr) {
700                                 i->cleanup = NULL;
701                                 return;
702                         }
703                         break;
704                 default:
705                         break;
706                 }
707         }
708 }
709
710 void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
711 {
712         struct failtest_call *p;
713         struct realloc_call call;
714         call.size = size;
715         p = add_history(FAILTEST_REALLOC, file, line, &call);
716
717         /* FIXME: Try one child moving allocation, one not. */
718         if (should_fail(p)) {
719                 p->u.realloc.ret = NULL;
720                 p->error = ENOMEM;
721         } else {
722                 /* Don't catch this one in the history fixup... */
723                 p->u.realloc.ret = NULL;
724                 fixup_ptr_history(ptr);
725                 p->u.realloc.ret = realloc(ptr, size);
726                 set_cleanup(p, cleanup_realloc, struct realloc_call);
727         }
728         errno = p->error;
729         return p->u.realloc.ret;
730 }
731
732 void failtest_free(void *ptr)
733 {
734         fixup_ptr_history(ptr);
735         free(ptr);
736 }
737
738 static void cleanup_open(struct open_call *call)
739 {
740         close(call->ret);
741 }
742
743 int failtest_open(const char *pathname,
744                   const char *file, unsigned line, ...)
745 {
746         struct failtest_call *p;
747         struct open_call call;
748         va_list ap;
749
750         call.pathname = strdup(pathname);
751         va_start(ap, line);
752         call.flags = va_arg(ap, int);
753         if (call.flags & O_CREAT) {
754                 call.mode = va_arg(ap, int);
755                 va_end(ap);
756         }
757         p = add_history(FAILTEST_OPEN, file, line, &call);
758         /* Avoid memory leak! */
759         if (p == &unrecorded_call)
760                 free((char *)call.pathname);
761         p->u.open.ret = open(pathname, call.flags, call.mode);
762
763         if (p->u.open.ret == -1) {
764                 p->fail = false;
765                 p->error = errno;
766         } else if (should_fail(p)) {
767                 close(p->u.open.ret);
768                 p->u.open.ret = -1;
769                 /* FIXME: Play with error codes? */
770                 p->error = EACCES;
771         } else {
772                 set_cleanup(p, cleanup_open, struct open_call);
773         }
774         errno = p->error;
775         return p->u.open.ret;
776 }
777
778 static void cleanup_pipe(struct pipe_call *call)
779 {
780         if (!call->closed[0])
781                 close(call->fds[0]);
782         if (!call->closed[1])
783                 close(call->fds[1]);
784 }
785
786 int failtest_pipe(int pipefd[2], const char *file, unsigned line)
787 {
788         struct failtest_call *p;
789         struct pipe_call call;
790
791         p = add_history(FAILTEST_PIPE, file, line, &call);
792         if (should_fail(p)) {
793                 p->u.open.ret = -1;
794                 /* FIXME: Play with error codes? */
795                 p->error = EMFILE;
796         } else {
797                 p->u.pipe.ret = pipe(p->u.pipe.fds);
798                 p->u.pipe.closed[0] = p->u.pipe.closed[1] = false;
799                 set_cleanup(p, cleanup_pipe, struct pipe_call);
800         }
801         /* This causes valgrind to notice if they use pipefd[] after failure */
802         memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
803         errno = p->error;
804         return p->u.pipe.ret;
805 }
806
807 ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
808                        const char *file, unsigned line)
809 {
810         struct failtest_call *p;
811         struct read_call call;
812         call.fd = fd;
813         call.buf = buf;
814         call.count = count;
815         call.off = off;
816         p = add_history(FAILTEST_READ, file, line, &call);
817
818         /* FIXME: Try partial read returns. */
819         if (should_fail(p)) {
820                 p->u.read.ret = -1;
821                 p->error = EIO;
822         } else {
823                 p->u.read.ret = pread(fd, buf, count, off);
824         }
825         errno = p->error;
826         return p->u.read.ret;
827 }
828
829 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t off,
830                         const char *file, unsigned line)
831 {
832         struct failtest_call *p;
833         struct write_call call;
834
835         call.fd = fd;
836         call.buf = buf;
837         call.count = count;
838         call.off = off;
839         p = add_history(FAILTEST_WRITE, file, line, &call);
840
841         /* If we're a child, we need to make sure we write the same thing
842          * to non-files as the parent does, so tell it. */
843         if (control_fd != -1 && off == (off_t)-1) {
844                 enum info_type type = WRITE;
845
846                 write_all(control_fd, &type, sizeof(type));
847                 write_all(control_fd, &p->u.write, sizeof(p->u.write));
848                 write_all(control_fd, buf, count);
849         }
850
851         /* FIXME: Try partial write returns. */
852         if (should_fail(p)) {
853                 p->u.write.ret = -1;
854                 p->error = EIO;
855         } else {
856                 /* FIXME: We assume same write order in parent and child */
857                 if (off == (off_t)-1 && child_writes_num != 0) {
858                         if (child_writes[0].fd != fd)
859                                 errx(1, "Child wrote to fd %u, not %u?",
860                                      child_writes[0].fd, fd);
861                         if (child_writes[0].off != p->u.write.off)
862                                 errx(1, "Child wrote to offset %zu, not %zu?",
863                                      (size_t)child_writes[0].off,
864                                      (size_t)p->u.write.off);
865                         if (child_writes[0].count != count)
866                                 errx(1, "Child wrote length %zu, not %zu?",
867                                      child_writes[0].count, count);
868                         if (memcmp(child_writes[0].buf, buf, count)) {
869                                 child_fail(NULL, 0,
870                                            "Child wrote differently to"
871                                            " fd %u than we did!\n", fd);
872                         }
873                         free((char *)child_writes[0].buf);
874                         child_writes_num--;
875                         memmove(&child_writes[0], &child_writes[1],
876                                 sizeof(child_writes[0]) * child_writes_num);
877
878                         /* Is this is a socket or pipe, child wrote it
879                            already. */
880                         if (p->u.write.off == (off_t)-1) {
881                                 p->u.write.ret = count;
882                                 errno = p->error;
883                                 return p->u.write.ret;
884                         }
885                 }
886                 p->u.write.ret = pwrite(fd, buf, count, off);
887         }
888         errno = p->error;
889         return p->u.write.ret;
890 }
891
892 ssize_t failtest_read(int fd, void *buf, size_t count,
893                       const char *file, unsigned line)
894 {
895         return failtest_pread(fd, buf, count, lseek(fd, 0, SEEK_CUR),
896                               file, line);
897 }
898
899 ssize_t failtest_write(int fd, const void *buf, size_t count,
900                        const char *file, unsigned line)
901 {
902         return failtest_pwrite(fd, buf, count, lseek(fd, 0, SEEK_CUR),
903                                file, line);
904 }
905
906 static struct lock_info *WARN_UNUSED_RESULT
907 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
908 {
909         unsigned int i;
910         struct lock_info *l;
911
912         for (i = 0; i < lock_num; i++) {
913                 l = &locks[i];
914
915                 if (l->fd != fd)
916                         continue;
917                 /* Four cases we care about:
918                  * Start overlap:
919                  *      l =    |      |
920                  *      new = |   |
921                  * Mid overlap:
922                  *      l =    |      |
923                  *      new =    |  |
924                  * End overlap:
925                  *      l =    |      |
926                  *      new =      |    |
927                  * Total overlap:
928                  *      l =    |      |
929                  *      new = |         |
930                  */
931                 if (start > l->start && end < l->end) {
932                         /* Mid overlap: trim entry, add new one. */
933                         off_t new_start, new_end;
934                         new_start = end + 1;
935                         new_end = l->end;
936                         l->end = start - 1;
937                         locks = add_lock(locks,
938                                          fd, new_start, new_end, l->type);
939                         l = &locks[i];
940                 } else if (start <= l->start && end >= l->end) {
941                         /* Total overlap: eliminate entry. */
942                         l->end = 0;
943                         l->start = 1;
944                 } else if (end >= l->start && end < l->end) {
945                         /* Start overlap: trim entry. */
946                         l->start = end + 1;
947                 } else if (start > l->start && start <= l->end) {
948                         /* End overlap: trim entry. */
949                         l->end = start-1;
950                 }
951                 /* Nothing left?  Remove it. */
952                 if (l->end < l->start) {
953                         memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
954                         i--;
955                 }
956         }
957
958         if (type != F_UNLCK) {
959                 locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
960                 l = &locks[lock_num++];
961                 l->fd = fd;
962                 l->start = start;
963                 l->end = end;
964                 l->type = type;
965         }
966         return locks;
967 }
968
969 /* We trap this so we can record it: we don't fail it. */
970 int failtest_close(int fd, const char *file, unsigned line)
971 {
972         struct failtest_call *i;
973         struct close_call call;
974         struct failtest_call *p;
975
976         call.fd = fd;
977         p = add_history(FAILTEST_CLOSE, file, line, &call);
978         p->fail = false;
979
980         /* Consume close from failpath. */
981         if (failpath)
982                 if (should_fail(p))
983                         abort();
984
985         if (fd < 0)
986                 return close(fd);
987
988         /* Trace history to find source of fd. */
989         tlist_for_each_rev(&history, i, list) {
990                 switch (i->type) {
991                 case FAILTEST_PIPE:
992                         /* From a pipe? */
993                         if (i->u.pipe.fds[0] == fd) {
994                                 assert(!i->u.pipe.closed[0]);
995                                 i->u.pipe.closed[0] = true;
996                                 if (i->u.pipe.closed[1])
997                                         i->cleanup = NULL;
998                                 goto out;
999                         }
1000                         if (i->u.pipe.fds[1] == fd) {
1001                                 assert(!i->u.pipe.closed[1]);
1002                                 i->u.pipe.closed[1] = true;
1003                                 if (i->u.pipe.closed[0])
1004                                         i->cleanup = NULL;
1005                                 goto out;
1006                         }
1007                         break;
1008                 case FAILTEST_OPEN:
1009                         if (i->u.open.ret == fd) {
1010                                 assert((void *)i->cleanup
1011                                        == (void *)cleanup_open);
1012                                 i->cleanup = NULL;
1013                                 goto out;
1014                         }
1015                         break;
1016                 default:
1017                         break;
1018                 }
1019         }
1020
1021 out:
1022         locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
1023         return close(fd);
1024 }
1025
1026 /* Zero length means "to end of file" */
1027 static off_t end_of(off_t start, off_t len)
1028 {
1029         if (len == 0)
1030                 return off_max();
1031         return start + len - 1;
1032 }
1033
1034 /* FIXME: This only handles locks, really. */
1035 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
1036 {
1037         struct failtest_call *p;
1038         struct fcntl_call call;
1039         va_list ap;
1040
1041         call.fd = fd;
1042         call.cmd = cmd;
1043
1044         /* Argument extraction. */
1045         switch (cmd) {
1046         case F_SETFL:
1047         case F_SETFD:
1048                 va_start(ap, cmd);
1049                 call.arg.l = va_arg(ap, long);
1050                 va_end(ap);
1051                 return fcntl(fd, cmd, call.arg.l);
1052         case F_GETFD:
1053         case F_GETFL:
1054                 return fcntl(fd, cmd);
1055         case F_GETLK:
1056                 get_locks();
1057                 va_start(ap, cmd);
1058                 call.arg.fl = *va_arg(ap, struct flock *);
1059                 va_end(ap);
1060                 return fcntl(fd, cmd, &call.arg.fl);
1061         case F_SETLK:
1062         case F_SETLKW:
1063                 va_start(ap, cmd);
1064                 call.arg.fl = *va_arg(ap, struct flock *);
1065                 va_end(ap);
1066                 break;
1067         default:
1068                 /* This means you need to implement it here. */
1069                 err(1, "failtest: unknown fcntl %u", cmd);
1070         }
1071
1072         p = add_history(FAILTEST_FCNTL, file, line, &call);
1073
1074         if (should_fail(p)) {
1075                 p->u.fcntl.ret = -1;
1076                 if (p->u.fcntl.cmd == F_SETLK)
1077                         p->error = EAGAIN;
1078                 else
1079                         p->error = EDEADLK;
1080         } else {
1081                 get_locks();
1082                 p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
1083                                        &p->u.fcntl.arg.fl);
1084                 if (p->u.fcntl.ret == -1)
1085                         p->error = errno;
1086                 else {
1087                         /* We don't handle anything else yet. */
1088                         assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
1089                         locks = add_lock(locks,
1090                                          p->u.fcntl.fd,
1091                                          p->u.fcntl.arg.fl.l_start,
1092                                          end_of(p->u.fcntl.arg.fl.l_start,
1093                                                 p->u.fcntl.arg.fl.l_len),
1094                                          p->u.fcntl.arg.fl.l_type);
1095                 }
1096         }
1097         errno = p->error;
1098         return p->u.fcntl.ret;
1099 }
1100
1101 pid_t failtest_getpid(const char *file, unsigned line)
1102 {
1103         /* You must call failtest_init first! */
1104         assert(orig_pid);
1105         return orig_pid;
1106 }
1107         
1108 void failtest_init(int argc, char *argv[])
1109 {
1110         unsigned int i;
1111
1112         orig_pid = getpid();
1113
1114         warnfd = move_fd_to_high(dup(STDERR_FILENO));
1115         for (i = 1; i < argc; i++) {
1116                 if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) {
1117                         failpath = argv[i] + strlen("--failpath=");
1118                 } else if (strcmp(argv[i], "--tracepath") == 0) {
1119                         tracefd = warnfd;
1120                         failtest_timeout_ms = -1;
1121                 } else if (!strncmp(argv[i], "--debugpath=",
1122                                     strlen("--debugpath="))) {
1123                         debugpath = argv[i] + strlen("--debugpath=");
1124                 }
1125         }
1126         start = time_now();
1127 }
1128
1129 bool failtest_has_failed(void)
1130 {
1131         return control_fd != -1;
1132 }
1133
1134 void failtest_exit(int status)
1135 {
1136         if (failtest_exit_check) {
1137                 if (!failtest_exit_check(&history))
1138                         child_fail(NULL, 0, "failtest_exit_check failed\n");
1139         }
1140
1141         failtest_cleanup(false, status);
1142 }