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