fdpass: avoid memory leak in example.
[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 <unistd.h>
9 #include <poll.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <sys/stat.h>
14 #include <sys/time.h>
15 #include <sys/mman.h>
16 #include <sys/resource.h>
17 #include <signal.h>
18 #include <assert.h>
19 #include <ccan/err/err.h>
20 #include <ccan/time/time.h>
21 #include <ccan/read_write_all/read_write_all.h>
22 #include <ccan/failtest/failtest_proto.h>
23 #include <ccan/build_assert/build_assert.h>
24 #include <ccan/hash/hash.h>
25 #include <ccan/htable/htable_type.h>
26 #include <ccan/str/str.h>
27 #include <ccan/compiler/compiler.h>
28
29 enum failtest_result (*failtest_hook)(struct tlist_calls *);
30
31 static FILE *tracef = NULL, *warnf;
32 static int traceindent = 0;
33
34 unsigned int failtest_timeout_ms = 20000;
35
36 const char *failpath;
37 const char *debugpath;
38
39 enum info_type {
40         WRITE,
41         RELEASE_LOCKS,
42         FAILURE,
43         SUCCESS,
44         UNEXPECTED
45 };
46
47 struct lock_info {
48         int fd;
49         /* end is inclusive: you can't have a 0-byte lock. */
50         off_t start, end;
51         int type;
52 };
53
54 /* We hash the call location together with its backtrace. */
55 static size_t hash_call(const struct failtest_call *call)
56 {
57         return hash(call->file, strlen(call->file),
58                     hash(&call->line, 1,
59                          hash(call->backtrace, call->backtrace_num,
60                               call->type)));
61 }
62
63 static bool call_eq(const struct failtest_call *call1,
64                     const struct failtest_call *call2)
65 {
66         unsigned int i;
67
68         if (strcmp(call1->file, call2->file) != 0
69             || call1->line != call2->line
70             || call1->type != call2->type
71             || call1->backtrace_num != call2->backtrace_num)
72                 return false;
73
74         for (i = 0; i < call1->backtrace_num; i++)
75                 if (call1->backtrace[i] != call2->backtrace[i])
76                         return false;
77
78         return true;
79 }
80
81 /* Defines struct failtable. */
82 HTABLE_DEFINE_TYPE(struct failtest_call, (struct failtest_call *), hash_call,
83                    call_eq, failtable);
84
85 bool (*failtest_exit_check)(struct tlist_calls *history);
86
87 /* The entire history of all calls. */
88 static struct tlist_calls history = TLIST_INIT(history);
89 /* If we're a child, the fd two write control info to the parent. */
90 static int control_fd = -1;
91 /* If we're a child, this is the first call we did ourselves. */
92 static struct failtest_call *our_history_start = NULL;
93 /* For printing runtime with --trace. */
94 static struct timeabs start;
95 /* Set when failtest_hook returns FAIL_PROBE */
96 static bool probing = false;
97 /* Table to track duplicates. */
98 static struct failtable failtable;
99
100 /* Array of writes which our child did.  We report them on failure. */
101 static struct write_call *child_writes = NULL;
102 static unsigned int child_writes_num = 0;
103
104 /* fcntl locking info. */
105 static pid_t lock_owner;
106 static struct lock_info *locks = NULL;
107 static unsigned int lock_num = 0;
108
109 /* Our original pid, which we return to anyone who asks. */
110 static pid_t orig_pid;
111
112 /* Mapping from failtest_type to char. */
113 static const char info_to_arg[] = "mceoxprwfal";
114
115 /* Dummy call used for failtest_undo wrappers. */
116 static struct failtest_call unrecorded_call;
117
118 struct contents_saved {
119         size_t count;
120         off_t off;
121         off_t old_len;
122         char contents[1];
123 };
124
125 /* File contents, saved in this child only. */
126 struct saved_mmapped_file {
127         struct saved_mmapped_file *next;
128         struct failtest_call *opener;
129         struct contents_saved *s;
130 };
131
132 static struct saved_mmapped_file *saved_mmapped_files;
133
134 #if HAVE_BACKTRACE
135 #include <execinfo.h>
136
137 static void **get_backtrace(unsigned int *num)
138 {
139         static unsigned int max_back = 100;
140         void **ret;
141
142 again:
143         ret = malloc(max_back * sizeof(void *));
144         *num = backtrace(ret, max_back);
145         if (*num == max_back) {
146                 free(ret);
147                 max_back *= 2;
148                 goto again;
149         }
150         return ret;
151 }
152 #else
153 /* This will test slightly less, since will consider all of the same
154  * calls as identical.  But, it's slightly faster! */
155 static void **get_backtrace(unsigned int *num)
156 {
157         *num = 0;
158         return NULL;
159 }
160 #endif /* HAVE_BACKTRACE */
161
162 static struct failtest_call *add_history_(enum failtest_call_type type,
163                                           bool can_leak,
164                                           const char *file,
165                                           unsigned int line,
166                                           const void *elem,
167                                           size_t elem_size)
168 {
169         struct failtest_call *call;
170
171         /* NULL file is how we suppress failure. */
172         if (!file)
173                 return &unrecorded_call;
174
175         call = malloc(sizeof *call);
176         call->type = type;
177         call->can_leak = can_leak;
178         call->file = file;
179         call->line = line;
180         call->cleanup = NULL;
181         call->backtrace = get_backtrace(&call->backtrace_num);
182         if (elem_size != 0)
183                 memcpy(&call->u, elem, elem_size);
184         tlist_add_tail(&history, call, list);
185         return call;
186 }
187
188 #define add_history(type, can_leak, file, line, elem)           \
189         add_history_((type), (can_leak), (file), (line), (elem), sizeof(*(elem)))
190
191 /* We do a fake call inside a sizeof(), to check types. */
192 #define set_cleanup(call, clean, type)                  \
193         (call)->cleanup = (void *)((void)sizeof(clean((type *)NULL, false),1), (clean))
194
195 /* Dup the fd to a high value (out of the way I hope!), and close the old fd. */
196 static int move_fd_to_high(int fd)
197 {
198         int i;
199         struct rlimit lim;
200         int max;
201
202         if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
203                 max = lim.rlim_cur;
204                 printf("Max is %i\n", max);
205         } else
206                 max = FD_SETSIZE;
207
208         for (i = max - 1; i > fd; i--) {
209                 if (fcntl(i, F_GETFL) == -1 && errno == EBADF) {
210                         if (dup2(fd, i) == -1) {
211                                 warn("Failed to dup fd %i to %i", fd, i);
212                                 continue;
213                         }
214                         close(fd);
215                         return i;
216                 }
217         }
218         /* Nothing?  Really?  Er... ok? */
219         return fd;
220 }
221
222 static bool read_write_info(int fd)
223 {
224         struct write_call *w;
225         char *buf;
226
227         /* We don't need all of this, but it's simple. */
228         child_writes = realloc(child_writes,
229                                (child_writes_num+1) * sizeof(child_writes[0]));
230         w = &child_writes[child_writes_num];
231         if (!read_all(fd, w, sizeof(*w)))
232                 return false;
233
234         w->buf = buf = malloc(w->count);
235         if (!read_all(fd, buf, w->count))
236                 return false;
237
238         child_writes_num++;
239         return true;
240 }
241
242 static char *failpath_string(void)
243 {
244         struct failtest_call *i;
245         char *ret = strdup("");
246         unsigned len = 0;
247
248         /* Inefficient, but who cares? */
249         tlist_for_each(&history, i, list) {
250                 ret = realloc(ret, len + 2);
251                 ret[len] = info_to_arg[i->type];
252                 if (i->fail)
253                         ret[len] = toupper(ret[len]);
254                 ret[++len] = '\0';
255         }
256         return ret;
257 }
258
259 static void do_warn(int e, const char *fmt, va_list ap)
260 {
261         char *p = failpath_string();
262
263         vfprintf(warnf, fmt, ap);
264         if (e != -1)
265                 fprintf(warnf, ": %s", strerror(e));
266         fprintf(warnf, " [%s]\n", p);
267         free(p);
268 }
269
270 static void fwarn(const char *fmt, ...)
271 {
272         va_list ap;
273         int e = errno;
274
275         va_start(ap, fmt);
276         do_warn(e, fmt, ap);
277         va_end(ap);
278 }
279
280
281 static void fwarnx(const char *fmt, ...)
282 {
283         va_list ap;
284
285         va_start(ap, fmt);
286         do_warn(-1, fmt, ap);
287         va_end(ap);
288 }
289
290 static void tell_parent(enum info_type type)
291 {
292         if (control_fd != -1)
293                 write_all(control_fd, &type, sizeof(type));
294 }
295
296 static void child_fail(const char *out, size_t outlen, const char *fmt, ...)
297 {
298         va_list ap;
299         char *path = failpath_string();
300
301         va_start(ap, fmt);
302         vfprintf(stderr, fmt, ap);
303         va_end(ap);
304
305         fprintf(stderr, "%.*s", (int)outlen, out);
306         printf("To reproduce: --failpath=%s\n", path);
307         free(path);
308         tell_parent(FAILURE);
309         exit(1);
310 }
311
312 static void PRINTF_FMT(1, 2) trace(const char *fmt, ...)
313 {
314         va_list ap;
315         unsigned int i;
316         char *p;
317         static int idx;
318
319         if (!tracef)
320                 return;
321
322         for (i = 0; i < traceindent; i++)
323                 fprintf(tracef, "  ");
324
325         p = failpath_string();
326         fprintf(tracef, "%i: %u: %s ", idx++, getpid(), p);
327         va_start(ap, fmt);
328         vfprintf(tracef, fmt, ap);
329         va_end(ap);
330         free(p);
331 }
332
333 static pid_t child;
334
335 static void hand_down(int signum)
336 {
337         kill(child, signum);
338 }
339
340 static void release_locks(void)
341 {
342         /* Locks were never acquired/reacquired? */
343         if (lock_owner == 0)
344                 return;
345
346         /* We own them?  Release them all. */
347         if (lock_owner == getpid()) {
348                 unsigned int i;
349                 struct flock fl;
350                 fl.l_type = F_UNLCK;
351                 fl.l_whence = SEEK_SET;
352                 fl.l_start = 0;
353                 fl.l_len = 0;
354
355                 trace("Releasing %u locks\n", lock_num);
356                 for (i = 0; i < lock_num; i++)
357                         fcntl(locks[i].fd, F_SETLK, &fl);
358         } else {
359                 /* Our parent must have them; pass request up. */
360                 enum info_type type = RELEASE_LOCKS;
361                 assert(control_fd != -1);
362                 write_all(control_fd, &type, sizeof(type));
363         }
364         lock_owner = 0;
365 }
366
367 /* off_t is a signed type.  Getting its max is non-trivial. */
368 static off_t off_max(void)
369 {
370         BUILD_ASSERT(sizeof(off_t) == 4 || sizeof(off_t) == 8);
371         if (sizeof(off_t) == 4)
372                 return (off_t)0x7FFFFFF;
373         else
374                 return (off_t)0x7FFFFFFFFFFFFFFULL;
375 }
376
377 static void get_locks(void)
378 {
379         unsigned int i;
380         struct flock fl;
381
382         if (lock_owner == getpid())
383                 return;
384
385         if (lock_owner != 0) {
386                 enum info_type type = RELEASE_LOCKS;
387                 assert(control_fd != -1);
388                 trace("Asking parent to release locks\n");
389                 write_all(control_fd, &type, sizeof(type));
390         }
391
392         fl.l_whence = SEEK_SET;
393
394         for (i = 0; i < lock_num; i++) {
395                 fl.l_type = locks[i].type;
396                 fl.l_start = locks[i].start;
397                 if (locks[i].end == off_max())
398                         fl.l_len = 0;
399                 else
400                         fl.l_len = locks[i].end - locks[i].start + 1;
401
402                 if (fcntl(locks[i].fd, F_SETLKW, &fl) != 0)
403                         abort();
404         }
405         trace("Acquired %u locks\n", lock_num);
406         lock_owner = getpid();
407 }
408
409
410 static struct contents_saved *save_contents(const char *filename,
411                                             int fd, size_t count, off_t off,
412                                             const char *why)
413 {
414         struct contents_saved *s = malloc(sizeof(*s) + count);
415         ssize_t ret;
416
417         s->off = off;
418
419         ret = pread(fd, s->contents, count, off);
420         if (ret < 0) {
421                 fwarn("failtest_write: failed to save old contents!");
422                 s->count = 0;
423         } else
424                 s->count = ret;
425
426         /* Use lseek to get the size of file, but we have to restore
427          * file offset */
428         off = lseek(fd, 0, SEEK_CUR);
429         s->old_len = lseek(fd, 0, SEEK_END);
430         lseek(fd, off, SEEK_SET);
431
432         trace("Saving %p %s %zu@%llu after %s (filelength %llu) via fd %i\n",
433               s, filename, s->count, (long long)s->off, why,
434               (long long)s->old_len, fd);
435         return s;
436 }
437
438 static void restore_contents(struct failtest_call *opener,
439                              struct contents_saved *s,
440                              bool restore_offset,
441                              const char *caller)
442 {
443         int fd;
444
445         /* The top parent doesn't need to restore. */
446         if (control_fd == -1)
447                 return;
448
449         /* Has the fd been closed? */
450         if (opener->u.open.closed) {
451                 /* Reopen, replace fd, close silently as we clean up. */
452                 fd = open(opener->u.open.pathname, O_RDWR);
453                 if (fd < 0) {
454                         fwarn("failtest: could not reopen %s to clean up %s!",
455                               opener->u.open.pathname, caller);
456                         return;
457                 }
458                 /* Make it clearly distinguisable from a "normal" fd. */
459                 fd = move_fd_to_high(fd);
460                 trace("Reopening %s to restore it (was fd %i, now %i)\n",
461                       opener->u.open.pathname, opener->u.open.ret, fd);
462                 opener->u.open.ret = fd;
463                 opener->u.open.closed = false;
464         }
465         fd = opener->u.open.ret;
466
467         trace("Restoring %p %s %zu@%llu after %s (filelength %llu) via fd %i\n",
468               s, opener->u.open.pathname, s->count, (long long)s->off, caller,
469               (long long)s->old_len, fd);
470         if (pwrite(fd, s->contents, s->count, s->off) != s->count) {
471                 fwarn("failtest: write failed cleaning up %s for %s!",
472                       opener->u.open.pathname, caller);
473         }
474
475         if (ftruncate(fd, s->old_len) != 0) {
476                 fwarn("failtest_write: truncate failed cleaning up %s for %s!",
477                       opener->u.open.pathname, caller);
478         }
479
480         if (restore_offset) {
481                 trace("Restoring offset of fd %i to %llu\n",
482                       fd, (long long)s->off);
483                 lseek(fd, s->off, SEEK_SET);
484         }
485 }
486
487 /* We save/restore most things on demand, but always do mmaped files. */
488 static void save_mmapped_files(void)
489 {
490         struct failtest_call *i;
491         trace("Saving mmapped files in child\n");
492
493         tlist_for_each_rev(&history, i, list) {
494                 struct mmap_call *m = &i->u.mmap;
495                 struct saved_mmapped_file *s;
496
497                 if (i->type != FAILTEST_MMAP)
498                         continue;
499
500                 /* FIXME: We only handle mmapped files where fd is still open. */
501                 if (m->opener->u.open.closed)
502                         continue;
503
504                 s = malloc(sizeof *s);
505                 s->s = save_contents(m->opener->u.open.pathname,
506                                      m->fd, m->length, m->offset,
507                                      "mmapped file before fork");
508                 s->opener = m->opener;
509                 s->next = saved_mmapped_files;
510                 saved_mmapped_files = s;
511         }
512 }
513
514 static void free_mmapped_files(bool restore)
515 {
516         trace("%s mmapped files in child\n",
517               restore ? "Restoring" : "Discarding");
518         while (saved_mmapped_files) {
519                 struct saved_mmapped_file *next = saved_mmapped_files->next;
520                 if (restore)
521                         restore_contents(saved_mmapped_files->opener,
522                                          saved_mmapped_files->s, false,
523                                          "saved mmap");
524                 free(saved_mmapped_files->s);
525                 free(saved_mmapped_files);
526                 saved_mmapped_files = next;
527         }
528 }
529
530 /* Returns a FAILTEST_OPEN, FAILTEST_PIPE or NULL. */
531 static struct failtest_call *opener_of(int fd)
532 {
533         struct failtest_call *i;
534
535         /* Don't get confused and match genuinely failed opens. */
536         if (fd < 0)
537                 return NULL;
538
539         /* Figure out the set of live fds. */
540         tlist_for_each_rev(&history, i, list) {
541                 if (i->fail)
542                         continue;
543                 switch (i->type) {
544                 case FAILTEST_CLOSE:
545                         if (i->u.close.fd == fd) {
546                                 return NULL;
547                         }
548                         break;
549                 case FAILTEST_OPEN:
550                         if (i->u.open.ret == fd) {
551                                 if (i->u.open.closed)
552                                         return NULL;
553                                 return i;
554                         }
555                         break;
556                 case FAILTEST_PIPE:
557                         if (i->u.pipe.fds[0] == fd || i->u.pipe.fds[1] == fd) {
558                                 return i;
559                         }
560                         break;
561                 default:
562                         break;
563                 }
564         }
565
566         /* FIXME: socket, dup, etc are untracked! */
567         return NULL;
568 }
569
570 static void free_call(struct failtest_call *call)
571 {
572         /* We don't do this in cleanup: needed even for failed opens. */
573         if (call->type == FAILTEST_OPEN)
574                 free((char *)call->u.open.pathname);
575         free(call->backtrace);
576         tlist_del_from(&history, call, list);
577         free(call);
578 }
579
580 /* Free up memory, so valgrind doesn't report leaks. */
581 static void free_everything(void)
582 {
583         struct failtest_call *i;
584
585         while ((i = tlist_top(&history, list)) != NULL)
586                 free_call(i);
587
588         failtable_clear(&failtable);
589 }
590
591 static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
592 {
593         struct failtest_call *i;
594         bool restore = true;
595
596         /* For children, we don't care if they "failed" the testing. */
597         if (control_fd != -1)
598                 status = 0;
599         else
600                 /* We don't restore contents for original parent. */
601                 restore = false;
602
603         /* Cleanup everything, in reverse order. */
604         tlist_for_each_rev(&history, i, list) {
605                 /* Don't restore things our parent did. */
606                 if (i == our_history_start)
607                         restore = false;
608
609                 if (i->fail)
610                         continue;
611
612                 if (i->cleanup)
613                         i->cleanup(&i->u, restore);
614
615                 /* But their program shouldn't leak, even on failure. */
616                 if (!forced_cleanup && i->can_leak) {
617                         char *p = failpath_string();
618                         printf("Leak at %s:%u: --failpath=%s\n",
619                                i->file, i->line, p);
620                         free(p);
621                         status = 1;
622                 }
623         }
624
625         /* Put back mmaped files the way our parent (if any) expects. */
626         free_mmapped_files(true);
627
628         free_everything();
629         if (status == 0)
630                 tell_parent(SUCCESS);
631         else
632                 tell_parent(FAILURE);
633         exit(status);
634 }
635
636 static bool following_path(void)
637 {
638         if (!failpath)
639                 return false;
640         /* + means continue after end, like normal. */
641         if (*failpath == '+') {
642                 failpath = NULL;
643                 return false;
644         }
645         return true;
646 }
647
648 static bool follow_path(struct failtest_call *call)
649 {
650         if (*failpath == '\0') {
651                 /* Continue, but don't inject errors. */
652                 return call->fail = false;
653         }
654
655         if (tolower((unsigned char)*failpath) != info_to_arg[call->type])
656                 errx(1, "Failpath expected '%s' got '%c'\n",
657                      failpath, info_to_arg[call->type]);
658         call->fail = cisupper(*(failpath++));
659                         if (call->fail)
660                                 call->can_leak = false;
661         return call->fail;
662 }
663
664 static bool should_fail(struct failtest_call *call)
665 {
666         int status;
667         int control[2], output[2];
668         enum info_type type = UNEXPECTED;
669         char *out = NULL;
670         size_t outlen = 0;
671         struct failtest_call *dup;
672
673         if (call == &unrecorded_call)
674                 return false;
675
676         if (following_path())
677                 return follow_path(call);
678
679         /* Attach debugger if they asked for it. */
680         if (debugpath) {
681                 char *path;
682
683                 /* Pretend this last call matches whatever path wanted:
684                  * keeps valgrind happy. */
685                 call->fail = cisupper(debugpath[strlen(debugpath)-1]);
686                 path = failpath_string();
687
688                 if (streq(path, debugpath)) {
689                         char str[80];
690
691                         /* Don't timeout. */
692                         signal(SIGUSR1, SIG_IGN);
693                         sprintf(str, "xterm -e gdb /proc/%d/exe %d &",
694                                 getpid(), getpid());
695                         if (system(str) == 0)
696                                 sleep(5);
697                 } else {
698                         /* Ignore last character: could be upper or lower. */
699                         path[strlen(path)-1] = '\0';
700                         if (!strstarts(debugpath, path)) {
701                                 fprintf(stderr,
702                                         "--debugpath not followed: %s\n", path);
703                                 debugpath = NULL;
704                         }
705                 }
706                 free(path);
707         }
708
709         /* Are we probing?  If so, we never fail twice. */
710         if (probing) {
711                 trace("Not failing %c due to FAIL_PROBE return\n",
712                       info_to_arg[call->type]);
713                 return call->fail = false;
714         }
715
716         /* Don't fail more than once in the same place. */
717         dup = failtable_get(&failtable, call);
718         if (dup) {
719                 trace("Not failing %c due to duplicate\n",
720                       info_to_arg[call->type]);
721                 return call->fail = false;
722         }
723
724         if (failtest_hook) {
725                 switch (failtest_hook(&history)) {
726                 case FAIL_OK:
727                         break;
728                 case FAIL_PROBE:
729                         probing = true;
730                         break;
731                 case FAIL_DONT_FAIL:
732                         trace("Not failing %c due to failhook return\n",
733                               info_to_arg[call->type]);
734                         call->fail = false;
735                         return false;
736                 default:
737                         abort();
738                 }
739         }
740
741         /* Add it to our table of calls. */
742         failtable_add(&failtable, call);
743
744         /* We're going to fail in the child. */
745         call->fail = true;
746         if (pipe(control) != 0 || pipe(output) != 0)
747                 err(1, "opening pipe");
748
749         /* Move out the way, to high fds. */
750         control[0] = move_fd_to_high(control[0]);
751         control[1] = move_fd_to_high(control[1]);
752         output[0] = move_fd_to_high(output[0]);
753         output[1] = move_fd_to_high(output[1]);
754
755         /* Prevent double-printing (in child and parent) */
756         fflush(stdout);
757         fflush(warnf);
758         if (tracef)
759                 fflush(tracef);
760         child = fork();
761         if (child == -1)
762                 err(1, "forking failed");
763
764         if (child == 0) {
765                 traceindent++;
766                 if (tracef) {
767                         struct timerel diff;
768                         const char *p;
769                         char *failpath;
770                         struct failtest_call *c;
771
772                         c = tlist_tail(&history, list);
773                         diff = time_between(time_now(), start);
774                         failpath = failpath_string();
775                         p = strrchr(c->file, '/');
776                         if (p)
777                                 p++;
778                         else
779                                 p = c->file;
780                         trace("%u->%u (%u.%02u): %s (%s:%u)\n",
781                               getppid(), getpid(),
782                               (int)diff.ts.tv_sec, (int)diff.ts.tv_nsec / 10000000,
783                               failpath, p, c->line);
784                         free(failpath);
785                 }
786                 /* From here on, we have to clean up! */
787                 our_history_start = tlist_tail(&history, list);
788                 close(control[0]);
789                 close(output[0]);
790                 /* Don't swallow stderr if we're tracing. */
791                 if (!tracef) {
792                         dup2(output[1], STDOUT_FILENO);
793                         dup2(output[1], STDERR_FILENO);
794                         if (output[1] != STDOUT_FILENO
795                             && output[1] != STDERR_FILENO)
796                                 close(output[1]);
797                 }
798                 control_fd = move_fd_to_high(control[1]);
799
800                 /* Forget any of our parent's saved files. */
801                 free_mmapped_files(false);
802
803                 /* Now, save any files we need to. */
804                 save_mmapped_files();
805
806                 /* Failed calls can't leak. */
807                 call->can_leak = false;
808
809                 return true;
810         }
811
812         signal(SIGUSR1, hand_down);
813
814         close(control[1]);
815         close(output[1]);
816
817         /* We grab output so we can display it; we grab writes so we
818          * can compare. */
819         do {
820                 struct pollfd pfd[2];
821                 int ret;
822
823                 pfd[0].fd = output[0];
824                 pfd[0].events = POLLIN|POLLHUP;
825                 pfd[1].fd = control[0];
826                 pfd[1].events = POLLIN|POLLHUP;
827
828                 if (type == SUCCESS)
829                         ret = poll(pfd, 1, failtest_timeout_ms);
830                 else
831                         ret = poll(pfd, 2, failtest_timeout_ms);
832
833                 if (ret == 0)
834                         hand_down(SIGUSR1);
835                 if (ret < 0) {
836                         if (errno == EINTR)
837                                 continue;
838                         err(1, "Poll returned %i", ret);
839                 }
840
841                 if (pfd[0].revents & POLLIN) {
842                         ssize_t len;
843
844                         out = realloc(out, outlen + 8192);
845                         len = read(output[0], out + outlen, 8192);
846                         outlen += len;
847                 } else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
848                         if (read_all(control[0], &type, sizeof(type))) {
849                                 if (type == WRITE) {
850                                         if (!read_write_info(control[0]))
851                                                 break;
852                                 } else if (type == RELEASE_LOCKS) {
853                                         release_locks();
854                                         /* FIXME: Tell them we're done... */
855                                 }
856                         }
857                 } else if (pfd[0].revents & POLLHUP) {
858                         break;
859                 }
860         } while (type != FAILURE);
861
862         close(output[0]);
863         close(control[0]);
864         waitpid(child, &status, 0);
865         if (!WIFEXITED(status)) {
866                 if (WTERMSIG(status) == SIGUSR1)
867                         child_fail(out, outlen, "Timed out");
868                 else
869                         child_fail(out, outlen, "Killed by signal %u: ",
870                                    WTERMSIG(status));
871         }
872         /* Child printed failure already, just pass up exit code. */
873         if (type == FAILURE) {
874                 fprintf(stderr, "%.*s", (int)outlen, out);
875                 tell_parent(type);
876                 exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
877         }
878         if (WEXITSTATUS(status) != 0)
879                 child_fail(out, outlen, "Exited with status %i: ",
880                            WEXITSTATUS(status));
881
882         free(out);
883         signal(SIGUSR1, SIG_DFL);
884
885         /* Only child does probe. */
886         probing = false;
887
888         /* We continue onwards without failing. */
889         call->fail = false;
890         return false;
891 }
892
893 static void cleanup_calloc(struct calloc_call *call, bool restore)
894 {
895         trace("undoing calloc %p\n", call->ret);
896         free(call->ret);
897 }
898
899 void *failtest_calloc(size_t nmemb, size_t size,
900                       const char *file, unsigned line)
901 {
902         struct failtest_call *p;
903         struct calloc_call call;
904         call.nmemb = nmemb;
905         call.size = size;
906         p = add_history(FAILTEST_CALLOC, true, file, line, &call);
907
908         if (should_fail(p)) {
909                 p->u.calloc.ret = NULL;
910                 p->error = ENOMEM;
911         } else {
912                 p->u.calloc.ret = calloc(nmemb, size);
913                 set_cleanup(p, cleanup_calloc, struct calloc_call);
914         }
915         trace("calloc %zu x %zu %s:%u -> %p\n",
916               nmemb, size, file, line, p->u.calloc.ret);
917         errno = p->error;
918         return p->u.calloc.ret;
919 }
920
921 static void cleanup_malloc(struct malloc_call *call, bool restore)
922 {
923         trace("undoing malloc %p\n", call->ret);
924         free(call->ret);
925 }
926
927 void *failtest_malloc(size_t size, const char *file, unsigned line)
928 {
929         struct failtest_call *p;
930         struct malloc_call call;
931         call.size = size;
932
933         p = add_history(FAILTEST_MALLOC, true, file, line, &call);
934         if (should_fail(p)) {
935                 p->u.malloc.ret = NULL;
936                 p->error = ENOMEM;
937         } else {
938                 p->u.malloc.ret = malloc(size);
939                 set_cleanup(p, cleanup_malloc, struct malloc_call);
940         }
941         trace("malloc %zu %s:%u -> %p\n",
942               size, file, line, p->u.malloc.ret);
943         errno = p->error;
944         return p->u.malloc.ret;
945 }
946
947 static void cleanup_realloc(struct realloc_call *call, bool restore)
948 {
949         trace("undoing realloc %p\n", call->ret);
950         free(call->ret);
951 }
952
953 /* Walk back and find out if we got this ptr from a previous routine. */
954 static void fixup_ptr_history(void *ptr, const char *why)
955 {
956         struct failtest_call *i;
957
958         /* Start at end of history, work back. */
959         tlist_for_each_rev(&history, i, list) {
960                 switch (i->type) {
961                 case FAILTEST_REALLOC:
962                         if (i->u.realloc.ret == ptr) {
963                                 trace("found realloc %p %s:%u matching %s\n",
964                                       ptr, i->file, i->line, why);
965                                 i->cleanup = NULL;
966                                 i->can_leak = false;
967                                 return;
968                         }
969                         break;
970                 case FAILTEST_MALLOC:
971                         if (i->u.malloc.ret == ptr) {
972                                 trace("found malloc %p %s:%u matching %s\n",
973                                       ptr, i->file, i->line, why);
974                                 i->cleanup = NULL;
975                                 i->can_leak = false;
976                                 return;
977                         }
978                         break;
979                 case FAILTEST_CALLOC:
980                         if (i->u.calloc.ret == ptr) {
981                                 trace("found calloc %p %s:%u matching %s\n",
982                                       ptr, i->file, i->line, why);
983                                 i->cleanup = NULL;
984                                 i->can_leak = false;
985                                 return;
986                         }
987                         break;
988                 default:
989                         break;
990                 }
991         }
992         trace("Did not find %p matching %s\n", ptr, why);
993 }
994
995 void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
996 {
997         struct failtest_call *p;
998         struct realloc_call call;
999         call.size = size;
1000         p = add_history(FAILTEST_REALLOC, true, file, line, &call);
1001
1002         /* FIXME: Try one child moving allocation, one not. */
1003         if (should_fail(p)) {
1004                 p->u.realloc.ret = NULL;
1005                 p->error = ENOMEM;
1006         } else {
1007                 /* Don't catch this one in the history fixup... */
1008                 p->u.realloc.ret = NULL;
1009                 fixup_ptr_history(ptr, "realloc");
1010                 p->u.realloc.ret = realloc(ptr, size);
1011                 set_cleanup(p, cleanup_realloc, struct realloc_call);
1012         }
1013         trace("realloc %p %s:%u -> %p\n",
1014               ptr, file, line, p->u.realloc.ret);
1015         errno = p->error;
1016         return p->u.realloc.ret;
1017 }
1018
1019 /* FIXME: Record free, so we can terminate fixup_ptr_history correctly.
1020  * If there's an alloc we don't see, it could get confusing if it matches
1021  * a previous allocation we did see. */
1022 void failtest_free(void *ptr)
1023 {
1024         fixup_ptr_history(ptr, "free");
1025         trace("free %p\n", ptr);
1026         free(ptr);
1027 }
1028
1029
1030 static struct contents_saved *save_file(const char *pathname)
1031 {
1032         int fd;
1033         struct contents_saved *s;
1034
1035         fd = open(pathname, O_RDONLY);
1036         if (fd < 0)
1037                 return NULL;
1038
1039         s = save_contents(pathname, fd, lseek(fd, 0, SEEK_END), 0,
1040                           "open with O_TRUNC");
1041         close(fd);
1042         return s;
1043 }
1044
1045 /* Optimization: don't create a child for an open which *we know*
1046  * would fail anyway. */
1047 static bool open_would_fail(const char *pathname, int flags)
1048 {
1049         if ((flags & O_ACCMODE) == O_RDONLY)
1050                 return access(pathname, R_OK) != 0;
1051         if (!(flags & O_CREAT)) {
1052                 if ((flags & O_ACCMODE) == O_WRONLY)
1053                         return access(pathname, W_OK) != 0;
1054                 if ((flags & O_ACCMODE) == O_RDWR)
1055                         return access(pathname, W_OK) != 0
1056                                 || access(pathname, R_OK) != 0;
1057         }
1058         /* FIXME: We could check if it exists, for O_CREAT|O_EXCL */
1059         return false;
1060 }
1061
1062 static void cleanup_open(struct open_call *call, bool restore)
1063 {
1064         if (restore && call->saved)
1065                 restore_contents(container_of(call, struct failtest_call,
1066                                               u.open),
1067                                  call->saved, false, "open with O_TRUNC");
1068         if (!call->closed) {
1069                 trace("Cleaning up open %s by closing fd %i\n",
1070                       call->pathname, call->ret);
1071                 close(call->ret);
1072                 call->closed = true;
1073         }
1074         free(call->saved);
1075 }
1076
1077 int failtest_open(const char *pathname,
1078                   const char *file, unsigned line, ...)
1079 {
1080         struct failtest_call *p;
1081         struct open_call call;
1082         va_list ap;
1083
1084         call.pathname = strdup(pathname);
1085         va_start(ap, line);
1086         call.flags = va_arg(ap, int);
1087         call.always_save = false;
1088         call.closed = false;
1089         if (call.flags & O_CREAT) {
1090                 call.mode = va_arg(ap, int);
1091         }
1092         va_end(ap);
1093         p = add_history(FAILTEST_OPEN, true, file, line, &call);
1094         /* Avoid memory leak! */
1095         if (p == &unrecorded_call)
1096                 free((char *)call.pathname);
1097
1098         if (should_fail(p)) {
1099                 /* Don't bother inserting failures that would happen anyway. */
1100                 if (open_would_fail(pathname, call.flags)) {
1101                         trace("Open would have failed anyway: stopping\n");
1102                         failtest_cleanup(true, 0);
1103                 }
1104                 p->u.open.ret = -1;
1105                 /* FIXME: Play with error codes? */
1106                 p->error = EACCES;
1107         } else {
1108                 /* Save the old version if they're truncating it. */
1109                 if (call.flags & O_TRUNC)
1110                         p->u.open.saved = save_file(pathname);
1111                 else
1112                         p->u.open.saved = NULL;
1113                 p->u.open.ret = open(pathname, call.flags, call.mode);
1114                 if (p->u.open.ret == -1) {
1115                         p->u.open.closed = true;
1116                         p->can_leak = false;
1117                 } else {
1118                         set_cleanup(p, cleanup_open, struct open_call);
1119                 }
1120         }
1121         trace("open %s %s:%u -> %i (opener %p)\n",
1122               pathname, file, line, p->u.open.ret, &p->u.open);
1123         errno = p->error;
1124         return p->u.open.ret;
1125 }
1126
1127 static void cleanup_mmap(struct mmap_call *mmap, bool restore)
1128 {
1129         trace("cleaning up mmap @%p (opener %p)\n",
1130               mmap->ret, mmap->opener);
1131         if (restore)
1132                 restore_contents(mmap->opener, mmap->saved, false, "mmap");
1133         free(mmap->saved);
1134 }
1135
1136 void *failtest_mmap(void *addr, size_t length, int prot, int flags,
1137                     int fd, off_t offset, const char *file, unsigned line)
1138 {
1139         struct failtest_call *p;
1140         struct mmap_call call;
1141
1142         call.addr = addr;
1143         call.length = length;
1144         call.prot = prot;
1145         call.flags = flags;
1146         call.offset = offset;
1147         call.fd = fd;
1148         call.opener = opener_of(fd);
1149
1150         /* If we don't know what file it was, don't fail. */
1151         if (!call.opener) {
1152                 if (fd != -1) {
1153                         fwarnx("failtest_mmap: couldn't figure out source for"
1154                                " fd %i at %s:%u", fd, file, line);
1155                 }
1156                 addr = mmap(addr, length, prot, flags, fd, offset);
1157                 trace("mmap of fd %i -> %p (opener = NULL)\n", fd, addr);
1158                 return addr;
1159         }
1160
1161         p = add_history(FAILTEST_MMAP, false, file, line, &call);
1162         if (should_fail(p)) {
1163                 p->u.mmap.ret = MAP_FAILED;
1164                 p->error = ENOMEM;
1165         } else {
1166                 p->u.mmap.ret = mmap(addr, length, prot, flags, fd, offset);
1167                 /* Save contents if we're writing to a normal file */
1168                 if (p->u.mmap.ret != MAP_FAILED
1169                     && (prot & PROT_WRITE)
1170                     && call.opener->type == FAILTEST_OPEN) {
1171                         const char *fname = call.opener->u.open.pathname;
1172                         p->u.mmap.saved = save_contents(fname, fd, length,
1173                                                         offset, "being mmapped");
1174                         set_cleanup(p, cleanup_mmap, struct mmap_call);
1175                 }
1176         }
1177         trace("mmap of fd %i %s:%u -> %p (opener = %p)\n",
1178               fd, file, line, addr, call.opener);
1179         errno = p->error;
1180         return p->u.mmap.ret;
1181 }
1182
1183 /* Since OpenBSD can't handle adding args, we use this file and line.
1184  * This will make all mmaps look the same, reducing coverage. */
1185 void *failtest_mmap_noloc(void *addr, size_t length, int prot, int flags,
1186                           int fd, off_t offset)
1187 {
1188         return failtest_mmap(addr, length, prot, flags, fd, offset,
1189                              __FILE__, __LINE__);
1190 }
1191
1192 static void cleanup_pipe(struct pipe_call *call, bool restore)
1193 {
1194         trace("cleaning up pipe fd=%i%s,%i%s\n",
1195               call->fds[0], call->closed[0] ? "(already closed)" : "",
1196               call->fds[1], call->closed[1] ? "(already closed)" : "");
1197         if (!call->closed[0])
1198                 close(call->fds[0]);
1199         if (!call->closed[1])
1200                 close(call->fds[1]);
1201 }
1202
1203 int failtest_pipe(int pipefd[2], const char *file, unsigned line)
1204 {
1205         struct failtest_call *p;
1206
1207         p = add_history_(FAILTEST_PIPE, true, file, line, NULL, 0);
1208         if (should_fail(p)) {
1209                 p->u.open.ret = -1;
1210                 /* FIXME: Play with error codes? */
1211                 p->error = EMFILE;
1212         } else {
1213                 p->u.pipe.ret = pipe(p->u.pipe.fds);
1214                 p->u.pipe.closed[0] = p->u.pipe.closed[1] = false;
1215                 set_cleanup(p, cleanup_pipe, struct pipe_call);
1216         }
1217
1218         trace("pipe %s:%u -> %i,%i\n", file, line,
1219               p->u.pipe.ret ? -1 : p->u.pipe.fds[0],
1220               p->u.pipe.ret ? -1 : p->u.pipe.fds[1]);
1221
1222         /* This causes valgrind to notice if they use pipefd[] after failure */
1223         memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
1224         errno = p->error;
1225         return p->u.pipe.ret;
1226 }
1227
1228 static void cleanup_read(struct read_call *call, bool restore)
1229 {
1230         if (restore) {
1231                 trace("cleaning up read on fd %i: seeking to %llu\n",
1232                       call->fd, (long long)call->off);
1233
1234                 /* Read (not readv!) moves file offset! */
1235                 if (lseek(call->fd, call->off, SEEK_SET) != call->off) {
1236                         fwarn("Restoring lseek pointer failed (read)");
1237                 }
1238         }
1239 }
1240
1241 static ssize_t failtest_add_read(int fd, void *buf, size_t count, off_t off,
1242                                  bool is_pread, const char *file, unsigned line)
1243 {
1244         struct failtest_call *p;
1245         struct read_call call;
1246         call.fd = fd;
1247         call.buf = buf;
1248         call.count = count;
1249         call.off = off;
1250         p = add_history(FAILTEST_READ, false, file, line, &call);
1251
1252         /* FIXME: Try partial read returns. */
1253         if (should_fail(p)) {
1254                 p->u.read.ret = -1;
1255                 p->error = EIO;
1256         } else {
1257                 if (is_pread)
1258                         p->u.read.ret = pread(fd, buf, count, off);
1259                 else {
1260                         p->u.read.ret = read(fd, buf, count);
1261                         if (p->u.read.ret != -1)
1262                                 set_cleanup(p, cleanup_read, struct read_call);
1263                 }
1264         }
1265         trace("%sread %s:%u fd %i %zu@%llu -> %zi\n",
1266               is_pread ? "p" : "", file, line, fd, count, (long long)off,
1267               p->u.read.ret);
1268         errno = p->error;
1269         return p->u.read.ret;
1270 }
1271
1272 static void cleanup_write(struct write_call *write, bool restore)
1273 {
1274         trace("cleaning up write on %s\n", write->opener->u.open.pathname);
1275         if (restore)
1276                 restore_contents(write->opener, write->saved, !write->is_pwrite,
1277                                  "write");
1278         free(write->saved);
1279 }
1280
1281 static ssize_t failtest_add_write(int fd, const void *buf,
1282                                   size_t count, off_t off,
1283                                   bool is_pwrite,
1284                                   const char *file, unsigned line)
1285 {
1286         struct failtest_call *p;
1287         struct write_call call;
1288
1289         call.fd = fd;
1290         call.buf = buf;
1291         call.count = count;
1292         call.off = off;
1293         call.is_pwrite = is_pwrite;
1294         call.opener = opener_of(fd);
1295         p = add_history(FAILTEST_WRITE, false, file, line, &call);
1296
1297         /* If we're a child, we need to make sure we write the same thing
1298          * to non-files as the parent does, so tell it. */
1299         if (control_fd != -1 && off == (off_t)-1) {
1300                 enum info_type type = WRITE;
1301
1302                 write_all(control_fd, &type, sizeof(type));
1303                 write_all(control_fd, &p->u.write, sizeof(p->u.write));
1304                 write_all(control_fd, buf, count);
1305         }
1306
1307         /* FIXME: Try partial write returns. */
1308         if (should_fail(p)) {
1309                 p->u.write.ret = -1;
1310                 p->error = EIO;
1311         } else {
1312                 bool is_file;
1313                 assert(call.opener == p->u.write.opener);
1314
1315                 if (p->u.write.opener) {
1316                         is_file = (p->u.write.opener->type == FAILTEST_OPEN);
1317                 } else {
1318                         /* We can't unwind it, so at least check same
1319                          * in parent and child. */
1320                         is_file = false;
1321                 }
1322
1323                 /* FIXME: We assume same write order in parent and child */
1324                 if (!is_file && child_writes_num != 0) {
1325                         if (child_writes[0].fd != fd)
1326                                 errx(1, "Child wrote to fd %u, not %u?",
1327                                      child_writes[0].fd, fd);
1328                         if (child_writes[0].off != p->u.write.off)
1329                                 errx(1, "Child wrote to offset %zu, not %zu?",
1330                                      (size_t)child_writes[0].off,
1331                                      (size_t)p->u.write.off);
1332                         if (child_writes[0].count != count)
1333                                 errx(1, "Child wrote length %zu, not %zu?",
1334                                      child_writes[0].count, count);
1335                         if (memcmp(child_writes[0].buf, buf, count)) {
1336                                 child_fail(NULL, 0,
1337                                            "Child wrote differently to"
1338                                            " fd %u than we did!\n", fd);
1339                         }
1340                         free((char *)child_writes[0].buf);
1341                         child_writes_num--;
1342                         memmove(&child_writes[0], &child_writes[1],
1343                                 sizeof(child_writes[0]) * child_writes_num);
1344
1345                         /* Child wrote it already. */
1346                         trace("write %s:%i on fd %i already done by child\n",
1347                               file, line, fd);
1348                         p->u.write.ret = count;
1349                         errno = p->error;
1350                         return p->u.write.ret;
1351                 }
1352
1353                 if (is_file) {
1354                         p->u.write.saved = save_contents(call.opener->u.open.pathname,
1355                                                          fd, count, off,
1356                                                          "being overwritten");
1357                         set_cleanup(p, cleanup_write, struct write_call);
1358                 }
1359
1360                 /* Though off is current seek ptr for write case, we need to
1361                  * move it.  write() does that for us. */
1362                 if (p->u.write.is_pwrite)
1363                         p->u.write.ret = pwrite(fd, buf, count, off);
1364                 else
1365                         p->u.write.ret = write(fd, buf, count);
1366         }
1367         trace("%swrite %s:%i %zu@%llu on fd %i -> %zi\n",
1368               p->u.write.is_pwrite ? "p" : "",
1369               file, line, count, (long long)off, fd, p->u.write.ret);
1370         errno = p->error;
1371         return p->u.write.ret;
1372 }
1373
1374 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t offset,
1375                         const char *file, unsigned line)
1376 {
1377         return failtest_add_write(fd, buf, count, offset, true, file, line);
1378 }
1379
1380 ssize_t failtest_write(int fd, const void *buf, size_t count,
1381                        const char *file, unsigned line)
1382 {
1383         return failtest_add_write(fd, buf, count, lseek(fd, 0, SEEK_CUR), false,
1384                                   file, line);
1385 }
1386
1387 ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
1388                        const char *file, unsigned line)
1389 {
1390         return failtest_add_read(fd, buf, count, off, true, file, line);
1391 }
1392
1393 ssize_t failtest_read(int fd, void *buf, size_t count,
1394                       const char *file, unsigned line)
1395 {
1396         return failtest_add_read(fd, buf, count, lseek(fd, 0, SEEK_CUR), false,
1397                                  file, line);
1398 }
1399
1400 static struct lock_info *WARN_UNUSED_RESULT
1401 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
1402 {
1403         unsigned int i;
1404         struct lock_info *l;
1405
1406         for (i = 0; i < lock_num; i++) {
1407                 l = &locks[i];
1408
1409                 if (l->fd != fd)
1410                         continue;
1411                 /* Four cases we care about:
1412                  * Start overlap:
1413                  *      l =    |      |
1414                  *      new = |   |
1415                  * Mid overlap:
1416                  *      l =    |      |
1417                  *      new =    |  |
1418                  * End overlap:
1419                  *      l =    |      |
1420                  *      new =      |    |
1421                  * Total overlap:
1422                  *      l =    |      |
1423                  *      new = |         |
1424                  */
1425                 if (start > l->start && end < l->end) {
1426                         /* Mid overlap: trim entry, add new one. */
1427                         off_t new_start, new_end;
1428                         new_start = end + 1;
1429                         new_end = l->end;
1430                         trace("splitting lock on fd %i from %llu-%llu"
1431                               " to %llu-%llu\n",
1432                               fd, (long long)l->start, (long long)l->end,
1433                               (long long)l->start, (long long)start - 1);
1434                         l->end = start - 1;
1435                         locks = add_lock(locks,
1436                                          fd, new_start, new_end, l->type);
1437                         l = &locks[i];
1438                 } else if (start <= l->start && end >= l->end) {
1439                         /* Total overlap: eliminate entry. */
1440                         trace("erasing lock on fd %i %llu-%llu\n",
1441                               fd, (long long)l->start, (long long)l->end);
1442                         l->end = 0;
1443                         l->start = 1;
1444                 } else if (end >= l->start && end < l->end) {
1445                         trace("trimming lock on fd %i from %llu-%llu"
1446                               " to %llu-%llu\n",
1447                               fd, (long long)l->start, (long long)l->end,
1448                               (long long)end + 1, (long long)l->end);
1449                         /* Start overlap: trim entry. */
1450                         l->start = end + 1;
1451                 } else if (start > l->start && start <= l->end) {
1452                         trace("trimming lock on fd %i from %llu-%llu"
1453                               " to %llu-%llu\n",
1454                               fd, (long long)l->start, (long long)l->end,
1455                               (long long)l->start, (long long)start - 1);
1456                         /* End overlap: trim entry. */
1457                         l->end = start-1;
1458                 }
1459                 /* Nothing left?  Remove it. */
1460                 if (l->end < l->start) {
1461                         trace("forgetting lock on fd %i\n", fd);
1462                         memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
1463                         i--;
1464                 }
1465         }
1466
1467         if (type != F_UNLCK) {
1468                 locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
1469                 l = &locks[lock_num++];
1470                 l->fd = fd;
1471                 l->start = start;
1472                 l->end = end;
1473                 l->type = type;
1474                 trace("new lock on fd %i %llu-%llu\n",
1475                       fd, (long long)l->start, (long long)l->end);
1476         }
1477         return locks;
1478 }
1479
1480 /* We trap this so we can record it: we don't fail it. */
1481 int failtest_close(int fd, const char *file, unsigned line)
1482 {
1483         struct close_call call;
1484         struct failtest_call *p, *opener;
1485
1486         /* Do this before we add ourselves to history! */
1487         opener = opener_of(fd);
1488
1489         call.fd = fd;
1490         p = add_history(FAILTEST_CLOSE, false, file, line, &call);
1491         p->fail = false;
1492
1493         /* Consume close from failpath (shouldn't tell us to fail). */
1494         if (following_path()) {
1495                 if (follow_path(p))
1496                         abort();
1497         }
1498
1499         trace("close on fd %i\n", fd);
1500         if (fd < 0)
1501                 return close(fd);
1502
1503         /* Mark opener as not leaking, remove its cleanup function. */
1504         if (opener) {
1505                 trace("close on fd %i found opener %p\n", fd, opener);
1506                 if (opener->type == FAILTEST_PIPE) {
1507                         /* From a pipe? */
1508                         if (opener->u.pipe.fds[0] == fd) {
1509                                 assert(!opener->u.pipe.closed[0]);
1510                                 opener->u.pipe.closed[0] = true;
1511                         } else if (opener->u.pipe.fds[1] == fd) {
1512                                 assert(!opener->u.pipe.closed[1]);
1513                                 opener->u.pipe.closed[1] = true;
1514                         } else
1515                                 abort();
1516                         opener->can_leak = (!opener->u.pipe.closed[0]
1517                                             || !opener->u.pipe.closed[1]);
1518                 } else if (opener->type == FAILTEST_OPEN) {
1519                         opener->u.open.closed = true;
1520                         opener->can_leak = false;
1521                 } else
1522                         abort();
1523         }
1524
1525         /* Restore offset now, in case parent shared (can't do after close!). */
1526         if (control_fd != -1) {
1527                 struct failtest_call *i;
1528
1529                 tlist_for_each_rev(&history, i, list) {
1530                         if (i == our_history_start)
1531                                 break;
1532                         if (i == opener)
1533                                 break;
1534                         if (i->type == FAILTEST_LSEEK && i->u.lseek.fd == fd) {
1535                                 trace("close on fd %i undoes lseek\n", fd);
1536                                 /* This seeks back. */
1537                                 i->cleanup(&i->u, true);
1538                                 i->cleanup = NULL;
1539                         } else if (i->type == FAILTEST_WRITE
1540                                    && i->u.write.fd == fd
1541                                    && !i->u.write.is_pwrite) {
1542                                 trace("close on fd %i undoes write"
1543                                       " offset change\n", fd);
1544                                 /* Write (not pwrite!) moves file offset! */
1545                                 if (lseek(fd, i->u.write.off, SEEK_SET)
1546                                     != i->u.write.off) {
1547                                         fwarn("Restoring lseek pointer failed (write)");
1548                                 }
1549                         } else if (i->type == FAILTEST_READ
1550                                    && i->u.read.fd == fd) {
1551                                 /* preads don't *have* cleanups */
1552                                 if (i->cleanup) {
1553                                         trace("close on fd %i undoes read"
1554                                               " offset change\n", fd);
1555                                         /* This seeks back. */
1556                                         i->cleanup(&i->u, true);
1557                                         i->cleanup = NULL;
1558                                 }
1559                         }
1560                 }
1561         }
1562
1563         /* Close unlocks everything. */
1564         locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
1565         return close(fd);
1566 }
1567
1568 /* Zero length means "to end of file" */
1569 static off_t end_of(off_t start, off_t len)
1570 {
1571         if (len == 0)
1572                 return off_max();
1573         return start + len - 1;
1574 }
1575
1576 /* FIXME: This only handles locks, really. */
1577 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
1578 {
1579         struct failtest_call *p;
1580         struct fcntl_call call;
1581         va_list ap;
1582
1583         call.fd = fd;
1584         call.cmd = cmd;
1585
1586         /* Argument extraction. */
1587         switch (cmd) {
1588         case F_SETFL:
1589         case F_SETFD:
1590                 va_start(ap, cmd);
1591                 call.arg.l = va_arg(ap, long);
1592                 va_end(ap);
1593                 trace("fcntl on fd %i F_SETFL/F_SETFD\n", fd);
1594                 return fcntl(fd, cmd, call.arg.l);
1595         case F_GETFD:
1596         case F_GETFL:
1597                 trace("fcntl on fd %i F_GETFL/F_GETFD\n", fd);
1598                 return fcntl(fd, cmd);
1599         case F_GETLK:
1600                 trace("fcntl on fd %i F_GETLK\n", fd);
1601                 get_locks();
1602                 va_start(ap, cmd);
1603                 call.arg.fl = *va_arg(ap, struct flock *);
1604                 va_end(ap);
1605                 return fcntl(fd, cmd, &call.arg.fl);
1606         case F_SETLK:
1607         case F_SETLKW:
1608                 trace("fcntl on fd %i F_SETLK%s\n",
1609                       fd, cmd == F_SETLKW ? "W" : "");
1610                 va_start(ap, cmd);
1611                 call.arg.fl = *va_arg(ap, struct flock *);
1612                 va_end(ap);
1613                 break;
1614         default:
1615                 /* This means you need to implement it here. */
1616                 err(1, "failtest: unknown fcntl %u", cmd);
1617         }
1618
1619         p = add_history(FAILTEST_FCNTL, false, file, line, &call);
1620
1621         if (should_fail(p)) {
1622                 p->u.fcntl.ret = -1;
1623                 if (p->u.fcntl.cmd == F_SETLK)
1624                         p->error = EAGAIN;
1625                 else
1626                         p->error = EDEADLK;
1627         } else {
1628                 get_locks();
1629                 p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
1630                                        &p->u.fcntl.arg.fl);
1631                 if (p->u.fcntl.ret == -1)
1632                         p->error = errno;
1633                 else {
1634                         /* We don't handle anything else yet. */
1635                         assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
1636                         locks = add_lock(locks,
1637                                          p->u.fcntl.fd,
1638                                          p->u.fcntl.arg.fl.l_start,
1639                                          end_of(p->u.fcntl.arg.fl.l_start,
1640                                                 p->u.fcntl.arg.fl.l_len),
1641                                          p->u.fcntl.arg.fl.l_type);
1642                 }
1643         }
1644         trace("fcntl on fd %i -> %i\n", fd, p->u.fcntl.ret);
1645         errno = p->error;
1646         return p->u.fcntl.ret;
1647 }
1648
1649 static void cleanup_lseek(struct lseek_call *call, bool restore)
1650 {
1651         if (restore) {
1652                 trace("cleaning up lseek on fd %i -> %llu\n",
1653                       call->fd, (long long)call->old_off);
1654                 if (lseek(call->fd, call->old_off, SEEK_SET) != call->old_off)
1655                         fwarn("Restoring lseek pointer failed");
1656         }
1657 }
1658
1659 /* We trap this so we can undo it: we don't fail it. */
1660 off_t failtest_lseek(int fd, off_t offset, int whence, const char *file,
1661                      unsigned int line)
1662 {
1663         struct failtest_call *p;
1664         struct lseek_call call;
1665         call.fd = fd;
1666         call.offset = offset;
1667         call.whence = whence;
1668         call.old_off = lseek(fd, 0, SEEK_CUR);
1669
1670         p = add_history(FAILTEST_LSEEK, false, file, line, &call);
1671         p->fail = false;
1672
1673         /* Consume lseek from failpath. */
1674         if (failpath)
1675                 if (should_fail(p))
1676                         abort();
1677
1678         p->u.lseek.ret = lseek(fd, offset, whence);
1679
1680         if (p->u.lseek.ret != (off_t)-1)
1681                 set_cleanup(p, cleanup_lseek, struct lseek_call);
1682
1683         trace("lseek %s:%u on fd %i from %llu to %llu%s\n",
1684               file, line, fd, (long long)call.old_off, (long long)offset,
1685               whence == SEEK_CUR ? " (from current off)" :
1686               whence == SEEK_END ? " (from end)" :
1687               whence == SEEK_SET ? "" : " (invalid whence)");
1688         return p->u.lseek.ret;
1689 }
1690
1691
1692 pid_t failtest_getpid(const char *file, unsigned line)
1693 {
1694         /* You must call failtest_init first! */
1695         assert(orig_pid);
1696         return orig_pid;
1697 }
1698         
1699 void failtest_init(int argc, char *argv[])
1700 {
1701         unsigned int i;
1702
1703         orig_pid = getpid();
1704
1705         warnf = fdopen(move_fd_to_high(dup(STDERR_FILENO)), "w");
1706         for (i = 1; i < argc; i++) {
1707                 if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) {
1708                         failpath = argv[i] + strlen("--failpath=");
1709                 } else if (strcmp(argv[i], "--trace") == 0) {
1710                         tracef = warnf;
1711                         failtest_timeout_ms = -1;
1712                 } else if (!strncmp(argv[i], "--debugpath=",
1713                                     strlen("--debugpath="))) {
1714                         debugpath = argv[i] + strlen("--debugpath=");
1715                 }
1716         }
1717         failtable_init(&failtable);
1718         start = time_now();
1719 }
1720
1721 bool failtest_has_failed(void)
1722 {
1723         return control_fd != -1;
1724 }
1725
1726 void failtest_exit(int status)
1727 {
1728         trace("failtest_exit with status %i\n", status);
1729         if (failtest_exit_check) {
1730                 if (!failtest_exit_check(&history))
1731                         child_fail(NULL, 0, "failtest_exit_check failed\n");
1732         }
1733
1734         failtest_cleanup(false, status);
1735 }