summaryrefslogtreecommitdiff
path: root/lib/ccan/failtest/failtest.c
blob: 7915a9222d9f37ddeb1ac183c2e84e02611e6bc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
/* Licensed under LGPL - see LICENSE file for details */
#include <ccan/failtest/failtest.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <unistd.h>
#include <poll.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <signal.h>
#include <assert.h>
#include <ccan/err/err.h>
#include <ccan/time/time.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/failtest/failtest_proto.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/hash/hash.h>
#include <ccan/htable/htable_type.h>
#include <ccan/str/str.h>
#include <ccan/compiler/compiler.h>

enum failtest_result (*failtest_hook)(struct tlist_calls *);

static FILE *tracef = NULL, *warnf;
static int traceindent = 0;

unsigned int failtest_timeout_ms = 20000;

const char *failpath;
const char *debugpath;

enum info_type {
	WRITE,
	RELEASE_LOCKS,
	FAILURE,
	SUCCESS,
	UNEXPECTED
};

struct lock_info {
	int fd;
	/* end is inclusive: you can't have a 0-byte lock. */
	off_t start, end;
	int type;
};

/* We hash the call location together with its backtrace. */
static size_t hash_call(const struct failtest_call *call)
{
	return hash(call->file, strlen(call->file),
		    hash(&call->line, 1,
			 hash(call->backtrace, call->backtrace_num,
			      call->type)));
}

static bool call_eq(const struct failtest_call *call1,
		    const struct failtest_call *call2)
{
	unsigned int i;

	if (strcmp(call1->file, call2->file) != 0
	    || call1->line != call2->line
	    || call1->type != call2->type
	    || call1->backtrace_num != call2->backtrace_num)
		return false;

	for (i = 0; i < call1->backtrace_num; i++)
		if (call1->backtrace[i] != call2->backtrace[i])
			return false;

	return true;
}

/* Defines struct failtable. */
HTABLE_DEFINE_TYPE(struct failtest_call, (struct failtest_call *), hash_call,
		   call_eq, failtable);

bool (*failtest_exit_check)(struct tlist_calls *history);

/* The entire history of all calls. */
static struct tlist_calls history = TLIST_INIT(history);
/* If we're a child, the fd two write control info to the parent. */
static int control_fd = -1;
/* If we're a child, this is the first call we did ourselves. */
static struct failtest_call *our_history_start = NULL;
/* For printing runtime with --trace. */
static struct timeval start;
/* Set when failtest_hook returns FAIL_PROBE */
static bool probing = false;
/* Table to track duplicates. */
static struct failtable failtable;

/* Array of writes which our child did.  We report them on failure. */
static struct write_call *child_writes = NULL;
static unsigned int child_writes_num = 0;

/* fcntl locking info. */
static pid_t lock_owner;
static struct lock_info *locks = NULL;
static unsigned int lock_num = 0;

/* Our original pid, which we return to anyone who asks. */
static pid_t orig_pid;

/* Mapping from failtest_type to char. */
static const char info_to_arg[] = "mceoxprwfal";

/* Dummy call used for failtest_undo wrappers. */
static struct failtest_call unrecorded_call;

struct contents_saved {
	size_t count;
	off_t off;
	off_t old_len;
	char contents[1];
};

/* File contents, saved in this child only. */
struct saved_mmapped_file {
	struct saved_mmapped_file *next;
	struct failtest_call *opener;
	struct contents_saved *s;
};

static struct saved_mmapped_file *saved_mmapped_files;

#if HAVE_BACKTRACE
#include <execinfo.h>

static void **get_backtrace(unsigned int *num)
{
	static unsigned int max_back = 100;
	void **ret;

again:
	ret = malloc(max_back * sizeof(void *));
	*num = backtrace(ret, max_back);
	if (*num == max_back) {
		free(ret);
		max_back *= 2;
		goto again;
	}
	return ret;
}
#else
/* This will test slightly less, since will consider all of the same
 * calls as identical.  But, it's slightly faster! */
static void **get_backtrace(unsigned int *num)
{
	*num = 0;
	return NULL;
}
#endif /* HAVE_BACKTRACE */

static struct failtest_call *add_history_(enum failtest_call_type type,
					  bool can_leak,
					  const char *file,
					  unsigned int line,
					  const void *elem,
					  size_t elem_size)
{
	struct failtest_call *call;

	/* NULL file is how we suppress failure. */
	if (!file)
		return &unrecorded_call;

	call = malloc(sizeof *call);
	call->type = type;
	call->can_leak = can_leak;
	call->file = file;
	call->line = line;
	call->cleanup = NULL;
	call->backtrace = get_backtrace(&call->backtrace_num);
	memcpy(&call->u, elem, elem_size);
	tlist_add_tail(&history, call, list);
	return call;
}

#define add_history(type, can_leak, file, line, elem)		\
	add_history_((type), (can_leak), (file), (line), (elem), sizeof(*(elem)))

/* We do a fake call inside a sizeof(), to check types. */
#define set_cleanup(call, clean, type)			\
	(call)->cleanup = (void *)((void)sizeof(clean((type *)NULL, false),1), (clean))

/* Dup the fd to a high value (out of the way I hope!), and close the old fd. */
static int move_fd_to_high(int fd)
{
	int i;
	struct rlimit lim;
	int max;

	if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
		max = lim.rlim_cur;
	} else
		max = FD_SETSIZE;

	for (i = max - 1; i > fd; i--) {
		if (fcntl(i, F_GETFL) == -1 && errno == EBADF) {
			if (dup2(fd, i) == -1) {
				warn("Failed to dup fd %i to %i", fd, i);
				continue;
			}
			close(fd);
			return i;
		}
	}
	/* Nothing?  Really?  Er... ok? */
	return fd;
}

static bool read_write_info(int fd)
{
	struct write_call *w;
	char *buf;

	/* We don't need all of this, but it's simple. */
	child_writes = realloc(child_writes,
			       (child_writes_num+1) * sizeof(child_writes[0]));
	w = &child_writes[child_writes_num];
	if (!read_all(fd, w, sizeof(*w)))
		return false;

	w->buf = buf = malloc(w->count);
	if (!read_all(fd, buf, w->count))
		return false;

	child_writes_num++;
	return true;
}

static char *failpath_string(void)
{
	struct failtest_call *i;
	char *ret = strdup("");
	unsigned len = 0;

	/* Inefficient, but who cares? */
	tlist_for_each(&history, i, list) {
		ret = realloc(ret, len + 2);
		ret[len] = info_to_arg[i->type];
		if (i->fail)
			ret[len] = toupper(ret[len]);
		ret[++len] = '\0';
	}
	return ret;
}

static void do_warn(int e, const char *fmt, va_list ap)
{
	char *p = failpath_string();

	vfprintf(warnf, fmt, ap);
	if (e != -1)
		fprintf(warnf, ": %s", strerror(e));
	fprintf(warnf, " [%s]\n", p);
	free(p);
}

static void fwarn(const char *fmt, ...)
{
	va_list ap;
	int e = errno;

	va_start(ap, fmt);
	do_warn(e, fmt, ap);
	va_end(ap);
}


static void fwarnx(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	do_warn(-1, fmt, ap);
	va_end(ap);
}

static void tell_parent(enum info_type type)
{
	if (control_fd != -1)
		write_all(control_fd, &type, sizeof(type));
}

static void child_fail(const char *out, size_t outlen, const char *fmt, ...)
{
	va_list ap;
	char *path = failpath_string();

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	fprintf(stderr, "%.*s", (int)outlen, out);
	printf("To reproduce: --failpath=%s\n", path);
	free(path);
	tell_parent(FAILURE);
	exit(1);
}

static void PRINTF_FMT(1, 2) trace(const char *fmt, ...)
{
	va_list ap;
	unsigned int i;
	char *p;
	static int idx;

	if (!tracef)
		return;

	for (i = 0; i < traceindent; i++)
		fprintf(tracef, "  ");

	p = failpath_string();
	fprintf(tracef, "%i: %u: %s ", idx++, getpid(), p);
	va_start(ap, fmt);
	vfprintf(tracef, fmt, ap);
	va_end(ap);
	free(p);
}

static pid_t child;

static void hand_down(int signum)
{
	kill(child, signum);
}

static void release_locks(void)
{
	/* Locks were never acquired/reacquired? */
	if (lock_owner == 0)
		return;

	/* We own them?  Release them all. */
	if (lock_owner == getpid()) {
		unsigned int i;
		struct flock fl;
		fl.l_type = F_UNLCK;
		fl.l_whence = SEEK_SET;
		fl.l_start = 0;
		fl.l_len = 0;

		trace("Releasing %u locks\n", lock_num);
		for (i = 0; i < lock_num; i++)
			fcntl(locks[i].fd, F_SETLK, &fl);
	} else {
		/* Our parent must have them; pass request up. */
		enum info_type type = RELEASE_LOCKS;
		assert(control_fd != -1);
		write_all(control_fd, &type, sizeof(type));
	}
	lock_owner = 0;
}

/* off_t is a signed type.  Getting its max is non-trivial. */
static off_t off_max(void)
{
	BUILD_ASSERT(sizeof(off_t) == 4 || sizeof(off_t) == 8);
	if (sizeof(off_t) == 4)
		return (off_t)0x7FFFFFF;
	else
		return (off_t)0x7FFFFFFFFFFFFFFULL;
}

static void get_locks(void)
{
	unsigned int i;
	struct flock fl;

	if (lock_owner == getpid())
		return;

	if (lock_owner != 0) {
		enum info_type type = RELEASE_LOCKS;
		assert(control_fd != -1);
		trace("Asking parent to release locks\n");
		write_all(control_fd, &type, sizeof(type));
	}

	fl.l_whence = SEEK_SET;

	for (i = 0; i < lock_num; i++) {
		fl.l_type = locks[i].type;
		fl.l_start = locks[i].start;
		if (locks[i].end == off_max())
			fl.l_len = 0;
		else
			fl.l_len = locks[i].end - locks[i].start + 1;

		if (fcntl(locks[i].fd, F_SETLKW, &fl) != 0)
			abort();
	}
	trace("Acquired %u locks\n", lock_num);
	lock_owner = getpid();
}


static struct contents_saved *save_contents(const char *filename,
					    int fd, size_t count, off_t off,
					    const char *why)
{
	struct contents_saved *s = malloc(sizeof(*s) + count);
	ssize_t ret;

	s->off = off;

	ret = pread(fd, s->contents, count, off);
	if (ret < 0) {
		fwarn("failtest_write: failed to save old contents!");
		s->count = 0;
	} else
		s->count = ret;

	/* Use lseek to get the size of file, but we have to restore
	 * file offset */
	off = lseek(fd, 0, SEEK_CUR);
	s->old_len = lseek(fd, 0, SEEK_END);
	lseek(fd, off, SEEK_SET);

	trace("Saving %p %s %zu@%llu after %s (filelength %llu) via fd %i\n",
	      s, filename, s->count, (long long)s->off, why,
	      (long long)s->old_len, fd);
	return s;
}

static void restore_contents(struct failtest_call *opener,
			     struct contents_saved *s,
			     bool restore_offset,
			     const char *caller)
{
	int fd;

	/* The top parent doesn't need to restore. */
	if (control_fd == -1)
		return;

	/* Has the fd been closed? */
	if (opener->u.open.closed) {
		/* Reopen, replace fd, close silently as we clean up. */
		fd = open(opener->u.open.pathname, O_RDWR);
		if (fd < 0) {
			fwarn("failtest: could not reopen %s to clean up %s!",
			      opener->u.open.pathname, caller);
			return;
		}
		/* Make it clearly distinguisable from a "normal" fd. */
		fd = move_fd_to_high(fd);
		trace("Reopening %s to restore it (was fd %i, now %i)\n",
		      opener->u.open.pathname, opener->u.open.ret, fd);
		opener->u.open.ret = fd;
		opener->u.open.closed = false;
	}
	fd = opener->u.open.ret;

	trace("Restoring %p %s %zu@%llu after %s (filelength %llu) via fd %i\n",
	      s, opener->u.open.pathname, s->count, (long long)s->off, caller,
	      (long long)s->old_len, fd);
	if (pwrite(fd, s->contents, s->count, s->off) != s->count) {
		fwarn("failtest: write failed cleaning up %s for %s!",
		      opener->u.open.pathname, caller);
	}

	if (ftruncate(fd, s->old_len) != 0) {
		fwarn("failtest_write: truncate failed cleaning up %s for %s!",
		      opener->u.open.pathname, caller);
	}

	if (restore_offset) {
		trace("Restoring offset of fd %i to %llu\n",
		      fd, (long long)s->off);
		lseek(fd, s->off, SEEK_SET);
	}
}

/* We save/restore most things on demand, but always do mmaped files. */
static void save_mmapped_files(void)
{
	struct failtest_call *i;
	trace("Saving mmapped files in child\n");

	tlist_for_each_rev(&history, i, list) {
		struct mmap_call *m = &i->u.mmap;
		struct saved_mmapped_file *s;

		if (i->type != FAILTEST_MMAP)
			continue;

		/* FIXME: We only handle mmapped files where fd is still open. */
		if (m->opener->u.open.closed)
			continue;

		s = malloc(sizeof *s);
		s->s = save_contents(m->opener->u.open.pathname,
				     m->fd, m->length, m->offset,
				     "mmapped file before fork");
		s->opener = m->opener;
		s->next = saved_mmapped_files;
		saved_mmapped_files = s;
	}
}

static void free_mmapped_files(bool restore)
{
	trace("%s mmapped files in child\n",
	      restore ? "Restoring" : "Discarding");
	while (saved_mmapped_files) {
		struct saved_mmapped_file *next = saved_mmapped_files->next;
		if (restore)
			restore_contents(saved_mmapped_files->opener,
					 saved_mmapped_files->s, false,
					 "saved mmap");
		free(saved_mmapped_files->s);
		free(saved_mmapped_files);
		saved_mmapped_files = next;
	}
}

/* Returns a FAILTEST_OPEN, FAILTEST_PIPE or NULL. */
static struct failtest_call *opener_of(int fd)
{
	struct failtest_call *i;

	/* Don't get confused and match genuinely failed opens. */
	if (fd < 0)
		return NULL;

	/* Figure out the set of live fds. */
	tlist_for_each_rev(&history, i, list) {
		if (i->fail)
			continue;
		switch (i->type) {
		case FAILTEST_CLOSE:
			if (i->u.close.fd == fd) {
				return NULL;
			}
			break;
		case FAILTEST_OPEN:
			if (i->u.open.ret == fd) {
				if (i->u.open.closed)
					return NULL;
				return i;
			}
			break;
		case FAILTEST_PIPE:
			if (i->u.pipe.fds[0] == fd || i->u.pipe.fds[1] == fd) {
				return i;
			}
			break;
		default:
			break;
		}
	}

	/* FIXME: socket, dup, etc are untracked! */
	return NULL;
}

static void free_call(struct failtest_call *call)
{
	/* We don't do this in cleanup: needed even for failed opens. */
	if (call->type == FAILTEST_OPEN)
		free((char *)call->u.open.pathname);
	free(call->backtrace);
	tlist_del_from(&history, call, list);
	free(call);
}

/* Free up memory, so valgrind doesn't report leaks. */
static void free_everything(void)
{
	struct failtest_call *i;

	while ((i = tlist_top(&history, list)) != NULL)
		free_call(i);

	failtable_clear(&failtable);
}

static NORETURN void failtest_cleanup(bool forced_cleanup, int status)
{
	struct failtest_call *i;
	bool restore = true;

	/* For children, we don't care if they "failed" the testing. */
	if (control_fd != -1)
		status = 0;
	else
		/* We don't restore contents for original parent. */
		restore = false;

	/* Cleanup everything, in reverse order. */
	tlist_for_each_rev(&history, i, list) {
		/* Don't restore things our parent did. */
		if (i == our_history_start)
			restore = false;

		if (i->fail)
			continue;

		if (i->cleanup)
			i->cleanup(&i->u, restore);

		/* But their program shouldn't leak, even on failure. */
		if (!forced_cleanup && i->can_leak) {
			printf("Leak at %s:%u: --failpath=%s\n",
			       i->file, i->line, failpath_string());
			status = 1;
		}
	}

	/* Put back mmaped files the way our parent (if any) expects. */
	free_mmapped_files(true);

	free_everything();
	if (status == 0)
		tell_parent(SUCCESS);
	else
		tell_parent(FAILURE);
	exit(status);
}

static bool following_path(void)
{
	if (!failpath)
		return false;
	/* + means continue after end, like normal. */
	if (*failpath == '+') {
		failpath = NULL;
		return false;
	}
	return true;
}

static bool follow_path(struct failtest_call *call)
{
	if (*failpath == '\0') {
		/* Continue, but don't inject errors. */
		return call->fail = false;
	}

	if (tolower((unsigned char)*failpath) != info_to_arg[call->type])
		errx(1, "Failpath expected '%s' got '%c'\n",
		     failpath, info_to_arg[call->type]);
	call->fail = cisupper(*(failpath++));
			if (call->fail)
				call->can_leak = false;
	return call->fail;
}

static bool should_fail(struct failtest_call *call)
{
	int status;
	int control[2], output[2];
	enum info_type type = UNEXPECTED;
	char *out = NULL;
	size_t outlen = 0;
	struct failtest_call *dup;

	if (call == &unrecorded_call)
		return false;

	if (following_path())
		return follow_path(call);

	/* Attach debugger if they asked for it. */
	if (debugpath) {
		char *path;

		/* Pretend this last call matches whatever path wanted:
		 * keeps valgrind happy. */
		call->fail = cisupper(debugpath[strlen(debugpath)-1]);
		path = failpath_string();

		if (streq(path, debugpath)) {
			char str[80];

			/* Don't timeout. */
			signal(SIGUSR1, SIG_IGN);
			sprintf(str, "xterm -e gdb /proc/%d/exe %d &",
				getpid(), getpid());
			if (system(str) == 0)
				sleep(5);
		} else {
			/* Ignore last character: could be upper or lower. */
			path[strlen(path)-1] = '\0';
			if (!strstarts(debugpath, path)) {
				fprintf(stderr,
					"--debugpath not followed: %s\n", path);
				debugpath = NULL;
			}
		}
		free(path);
	}

	/* Are we probing?  If so, we never fail twice. */
	if (probing) {
		trace("Not failing %c due to FAIL_PROBE return\n",
		      info_to_arg[call->type]);
		return call->fail = false;
	}

	/* Don't fail more than once in the same place. */
	dup = failtable_get(&failtable, call);
	if (dup) {
		trace("Not failing %c due to duplicate\n",
		      info_to_arg[call->type]);
		return call->fail = false;
	}

	if (failtest_hook) {
		switch (failtest_hook(&history)) {
		case FAIL_OK:
			break;
		case FAIL_PROBE:
			probing = true;
			break;
		case FAIL_DONT_FAIL:
			trace("Not failing %c due to failhook return\n",
			      info_to_arg[call->type]);
			call->fail = false;
			return false;
		default:
			abort();
		}
	}

	/* Add it to our table of calls. */
	failtable_add(&failtable, call);

	/* We're going to fail in the child. */
	call->fail = true;
	if (pipe(control) != 0 || pipe(output) != 0)
		err(1, "opening pipe");

	/* Move out the way, to high fds. */
	control[0] = move_fd_to_high(control[0]);
	control[1] = move_fd_to_high(control[1]);
	output[0] = move_fd_to_high(output[0]);
	output[1] = move_fd_to_high(output[1]);

	/* Prevent double-printing (in child and parent) */
	fflush(stdout);
	fflush(warnf);
	if (tracef)
		fflush(tracef);
	child = fork();
	if (child == -1)
		err(1, "forking failed");

	if (child == 0) {
		traceindent++;
		if (tracef) {
			struct timeval diff;
			const char *p;
			char *failpath;
			struct failtest_call *c;

			c = tlist_tail(&history, list);
			diff = time_sub(time_now(), start);
			failpath = failpath_string();
			p = strrchr(c->file, '/');
			if (p)
				p++;
			else
				p = c->file;
			trace("%u->%u (%u.%02u): %s (%s:%u)\n",
			      getppid(), getpid(),
			      (int)diff.tv_sec, (int)diff.tv_usec / 10000,
			      failpath, p, c->line);
			free(failpath);
		}
		/* From here on, we have to clean up! */
		our_history_start = tlist_tail(&history, list);
		close(control[0]);
		close(output[0]);
		/* Don't swallow stderr if we're tracing. */
		if (!tracef) {
			dup2(output[1], STDOUT_FILENO);
			dup2(output[1], STDERR_FILENO);
			if (output[1] != STDOUT_FILENO
			    && output[1] != STDERR_FILENO)
				close(output[1]);
		}
		control_fd = move_fd_to_high(control[1]);

		/* Forget any of our parent's saved files. */
		free_mmapped_files(false);

		/* Now, save any files we need to. */
		save_mmapped_files();

		/* Failed calls can't leak. */
		call->can_leak = false;

		return true;
	}

	signal(SIGUSR1, hand_down);

	close(control[1]);
	close(output[1]);

	/* We grab output so we can display it; we grab writes so we
	 * can compare. */
	do {
		struct pollfd pfd[2];
		int ret;

		pfd[0].fd = output[0];
		pfd[0].events = POLLIN|POLLHUP;
		pfd[1].fd = control[0];
		pfd[1].events = POLLIN|POLLHUP;

		if (type == SUCCESS)
			ret = poll(pfd, 1, failtest_timeout_ms);
		else
			ret = poll(pfd, 2, failtest_timeout_ms);

		if (ret == 0)
			hand_down(SIGUSR1);
		if (ret < 0) {
			if (errno == EINTR)
				continue;
			err(1, "Poll returned %i", ret);
		}

		if (pfd[0].revents & POLLIN) {
			ssize_t len;

			out = realloc(out, outlen + 8192);
			len = read(output[0], out + outlen, 8192);
			outlen += len;
		} else if (type != SUCCESS && (pfd[1].revents & POLLIN)) {
			if (read_all(control[0], &type, sizeof(type))) {
				if (type == WRITE) {
					if (!read_write_info(control[0]))
						break;
				} else if (type == RELEASE_LOCKS) {
					release_locks();
					/* FIXME: Tell them we're done... */
				}
			}
		} else if (pfd[0].revents & POLLHUP) {
			break;
		}
	} while (type != FAILURE);

	close(output[0]);
	close(control[0]);
	waitpid(child, &status, 0);
	if (!WIFEXITED(status)) {
		if (WTERMSIG(status) == SIGUSR1)
			child_fail(out, outlen, "Timed out");
		else
			child_fail(out, outlen, "Killed by signal %u: ",
				   WTERMSIG(status));
	}
	/* Child printed failure already, just pass up exit code. */
	if (type == FAILURE) {
		fprintf(stderr, "%.*s", (int)outlen, out);
		tell_parent(type);
		exit(WEXITSTATUS(status) ? WEXITSTATUS(status) : 1);
	}
	if (WEXITSTATUS(status) != 0)
		child_fail(out, outlen, "Exited with status %i: ",
			   WEXITSTATUS(status));

	free(out);
	signal(SIGUSR1, SIG_DFL);

	/* Only child does probe. */
	probing = false;

	/* We continue onwards without failing. */
	call->fail = false;
	return false;
}

static void cleanup_calloc(struct calloc_call *call, bool restore)
{
	trace("undoing calloc %p\n", call->ret);
	free(call->ret);
}

void *failtest_calloc(size_t nmemb, size_t size,
		      const char *file, unsigned line)
{
	struct failtest_call *p;
	struct calloc_call call;
	call.nmemb = nmemb;
	call.size = size;
	p = add_history(FAILTEST_CALLOC, true, file, line, &call);

	if (should_fail(p)) {
		p->u.calloc.ret = NULL;
		p->error = ENOMEM;
	} else {
		p->u.calloc.ret = calloc(nmemb, size);
		set_cleanup(p, cleanup_calloc, struct calloc_call);
	}
	trace("calloc %zu x %zu %s:%u -> %p\n",
	      nmemb, size, file, line, p->u.calloc.ret);
	errno = p->error;
	return p->u.calloc.ret;
}

static void cleanup_malloc(struct malloc_call *call, bool restore)
{
	trace("undoing malloc %p\n", call->ret);
	free(call->ret);
}

void *failtest_malloc(size_t size, const char *file, unsigned line)
{
	struct failtest_call *p;
	struct malloc_call call;
	call.size = size;

	p = add_history(FAILTEST_MALLOC, true, file, line, &call);
	if (should_fail(p)) {
		p->u.malloc.ret = NULL;
		p->error = ENOMEM;
	} else {
		p->u.malloc.ret = malloc(size);
		set_cleanup(p, cleanup_malloc, struct malloc_call);
	}
	trace("malloc %zu %s:%u -> %p\n",
	      size, file, line, p->u.malloc.ret);
	errno = p->error;
	return p->u.malloc.ret;
}

static void cleanup_realloc(struct realloc_call *call, bool restore)
{
	trace("undoing realloc %p\n", call->ret);
	free(call->ret);
}

/* Walk back and find out if we got this ptr from a previous routine. */
static void fixup_ptr_history(void *ptr, const char *why)
{
	struct failtest_call *i;

	/* Start at end of history, work back. */
	tlist_for_each_rev(&history, i, list) {
		switch (i->type) {
		case FAILTEST_REALLOC:
			if (i->u.realloc.ret == ptr) {
				trace("found realloc %p %s:%u matching %s\n",
				      ptr, i->file, i->line, why);
				i->cleanup = NULL;
				i->can_leak = false;
				return;
			}
			break;
		case FAILTEST_MALLOC:
			if (i->u.malloc.ret == ptr) {
				trace("found malloc %p %s:%u matching %s\n",
				      ptr, i->file, i->line, why);
				i->cleanup = NULL;
				i->can_leak = false;
				return;
			}
			break;
		case FAILTEST_CALLOC:
			if (i->u.calloc.ret == ptr) {
				trace("found calloc %p %s:%u matching %s\n",
				      ptr, i->file, i->line, why);
				i->cleanup = NULL;
				i->can_leak = false;
				return;
			}
			break;
		default:
			break;
		}
	}
	trace("Did not find %p matching %s\n", ptr, why);
}

void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
{
	struct failtest_call *p;
	struct realloc_call call;
	call.size = size;
	p = add_history(FAILTEST_REALLOC, true, file, line, &call);

	/* FIXME: Try one child moving allocation, one not. */
	if (should_fail(p)) {
		p->u.realloc.ret = NULL;
		p->error = ENOMEM;
	} else {
		/* Don't catch this one in the history fixup... */
		p->u.realloc.ret = NULL;
		fixup_ptr_history(ptr, "realloc");
		p->u.realloc.ret = realloc(ptr, size);
		set_cleanup(p, cleanup_realloc, struct realloc_call);
	}
	trace("realloc %p %s:%u -> %p\n",
	      ptr, file, line, p->u.realloc.ret);
	errno = p->error;
	return p->u.realloc.ret;
}

/* FIXME: Record free, so we can terminate fixup_ptr_history correctly.
 * If there's an alloc we don't see, it could get confusing if it matches
 * a previous allocation we did see. */
void failtest_free(void *ptr)
{
	fixup_ptr_history(ptr, "free");
	trace("free %p\n", ptr);
	free(ptr);
}


static struct contents_saved *save_file(const char *pathname)
{
	int fd;
	struct contents_saved *s;

	fd = open(pathname, O_RDONLY);
	if (fd < 0)
		return NULL;

	s = save_contents(pathname, fd, lseek(fd, 0, SEEK_END), 0,
			  "open with O_TRUNC");
	close(fd);
	return s;
}

/* Optimization: don't create a child for an open which *we know*
 * would fail anyway. */
static bool open_would_fail(const char *pathname, int flags)
{
	if ((flags & O_ACCMODE) == O_RDONLY)
		return access(pathname, R_OK) != 0;
	if (!(flags & O_CREAT)) {
		if ((flags & O_ACCMODE) == O_WRONLY)
			return access(pathname, W_OK) != 0;
		if ((flags & O_ACCMODE) == O_RDWR)
			return access(pathname, W_OK) != 0
				|| access(pathname, R_OK) != 0;
	}
	/* FIXME: We could check if it exists, for O_CREAT|O_EXCL */
	return false;
}

static void cleanup_open(struct open_call *call, bool restore)
{
	if (restore && call->saved)
		restore_contents(container_of(call, struct failtest_call,
					      u.open),
				 call->saved, false, "open with O_TRUNC");
	if (!call->closed) {
		trace("Cleaning up open %s by closing fd %i\n",
		      call->pathname, call->ret);
		close(call->ret);
		call->closed = true;
	}
	free(call->saved);
}

int failtest_open(const char *pathname,
		  const char *file, unsigned line, ...)
{
	struct failtest_call *p;
	struct open_call call;
	va_list ap;

	call.pathname = strdup(pathname);
	va_start(ap, line);
	call.flags = va_arg(ap, int);
	call.always_save = false;
	call.closed = false;
	if (call.flags & O_CREAT) {
		call.mode = va_arg(ap, int);
		va_end(ap);
	}
	p = add_history(FAILTEST_OPEN, true, file, line, &call);
	/* Avoid memory leak! */
	if (p == &unrecorded_call)
		free((char *)call.pathname);

	if (should_fail(p)) {
		/* Don't bother inserting failures that would happen anyway. */
		if (open_would_fail(pathname, call.flags)) {
			trace("Open would have failed anyway: stopping\n");
			failtest_cleanup(true, 0);
		}
		p->u.open.ret = -1;
		/* FIXME: Play with error codes? */
		p->error = EACCES;
	} else {
		/* Save the old version if they're truncating it. */
		if (call.flags & O_TRUNC)
			p->u.open.saved = save_file(pathname);
		else
			p->u.open.saved = NULL;
		p->u.open.ret = open(pathname, call.flags, call.mode);
		if (p->u.open.ret == -1) {
			p->u.open.closed = true;
			p->can_leak = false;
		} else {
			set_cleanup(p, cleanup_open, struct open_call);
		}
	}
	trace("open %s %s:%u -> %i (opener %p)\n",
	      pathname, file, line, p->u.open.ret, &p->u.open);
	errno = p->error;
	return p->u.open.ret;
}

static void cleanup_mmap(struct mmap_call *mmap, bool restore)
{
	trace("cleaning up mmap @%p (opener %p)\n",
	      mmap->ret, mmap->opener);
	if (restore)
		restore_contents(mmap->opener, mmap->saved, false, "mmap");
	free(mmap->saved);
}

void *failtest_mmap(void *addr, size_t length, int prot, int flags,
		    int fd, off_t offset, const char *file, unsigned line)
{
	struct failtest_call *p;
	struct mmap_call call;

	call.addr = addr;
	call.length = length;
	call.prot = prot;
	call.flags = flags;
	call.offset = offset;
	call.fd = fd;
	call.opener = opener_of(fd);

	/* If we don't know what file it was, don't fail. */
	if (!call.opener) {
		if (fd != -1) {
			fwarnx("failtest_mmap: couldn't figure out source for"
			       " fd %i at %s:%u", fd, file, line);
		}
		addr = mmap(addr, length, prot, flags, fd, offset);
		trace("mmap of fd %i -> %p (opener = NULL)\n", fd, addr);
		return addr;
	}

	p = add_history(FAILTEST_MMAP, false, file, line, &call);
	if (should_fail(p)) {
		p->u.mmap.ret = MAP_FAILED;
		p->error = ENOMEM;
	} else {
		p->u.mmap.ret = mmap(addr, length, prot, flags, fd, offset);
		/* Save contents if we're writing to a normal file */
		if (p->u.mmap.ret != MAP_FAILED
		    && (prot & PROT_WRITE)
		    && call.opener->type == FAILTEST_OPEN) {
			const char *fname = call.opener->u.open.pathname;
			p->u.mmap.saved = save_contents(fname, fd, length,
							offset, "being mmapped");
			set_cleanup(p, cleanup_mmap, struct mmap_call);
		}
	}
	trace("mmap of fd %i %s:%u -> %p (opener = %p)\n",
	      fd, file, line, addr, call.opener);
	errno = p->error;
	return p->u.mmap.ret;
}

/* Since OpenBSD can't handle adding args, we use this file and line.
 * This will make all mmaps look the same, reducing coverage. */
void *failtest_mmap_noloc(void *addr, size_t length, int prot, int flags,
			  int fd, off_t offset)
{
	return failtest_mmap(addr, length, prot, flags, fd, offset,
			     __FILE__, __LINE__);
}

static void cleanup_pipe(struct pipe_call *call, bool restore)
{
	trace("cleaning up pipe fd=%i%s,%i%s\n",
	      call->fds[0], call->closed[0] ? "(already closed)" : "",
	      call->fds[1], call->closed[1] ? "(already closed)" : "");
	if (!call->closed[0])
		close(call->fds[0]);
	if (!call->closed[1])
		close(call->fds[1]);
}

int failtest_pipe(int pipefd[2], const char *file, unsigned line)
{
	struct failtest_call *p;
	struct pipe_call call;

	p = add_history(FAILTEST_PIPE, true, file, line, &call);
	if (should_fail(p)) {
		p->u.open.ret = -1;
		/* FIXME: Play with error codes? */
		p->error = EMFILE;
	} else {
		p->u.pipe.ret = pipe(p->u.pipe.fds);
		p->u.pipe.closed[0] = p->u.pipe.closed[1] = false;
		set_cleanup(p, cleanup_pipe, struct pipe_call);
	}

	trace("pipe %s:%u -> %i,%i\n", file, line,
	      p->u.pipe.ret ? -1 : p->u.pipe.fds[0],
	      p->u.pipe.ret ? -1 : p->u.pipe.fds[1]);

	/* This causes valgrind to notice if they use pipefd[] after failure */
	memcpy(pipefd, p->u.pipe.fds, sizeof(p->u.pipe.fds));
	errno = p->error;
	return p->u.pipe.ret;
}

static void cleanup_read(struct read_call *call, bool restore)
{
	if (restore) {
		trace("cleaning up read on fd %i: seeking to %llu\n",
		      call->fd, (long long)call->off);

		/* Read (not readv!) moves file offset! */
		if (lseek(call->fd, call->off, SEEK_SET) != call->off) {
			fwarn("Restoring lseek pointer failed (read)");
		}
	}
}

static ssize_t failtest_add_read(int fd, void *buf, size_t count, off_t off,
				 bool is_pread, const char *file, unsigned line)
{
	struct failtest_call *p;
	struct read_call call;
	call.fd = fd;
	call.buf = buf;
	call.count = count;
	call.off = off;
	p = add_history(FAILTEST_READ, false, file, line, &call);

	/* FIXME: Try partial read returns. */
	if (should_fail(p)) {
		p->u.read.ret = -1;
		p->error = EIO;
	} else {
		if (is_pread)
			p->u.read.ret = pread(fd, buf, count, off);
		else {
			p->u.read.ret = read(fd, buf, count);
			if (p->u.read.ret != -1)
				set_cleanup(p, cleanup_read, struct read_call);
		}
	}
	trace("%sread %s:%u fd %i %zu@%llu -> %zd\n",
	      is_pread ? "p" : "", file, line, fd, count, (long long)off,
	      p->u.read.ret);
	errno = p->error;
	return p->u.read.ret;
}

static void cleanup_write(struct write_call *write, bool restore)
{
	trace("cleaning up write on %s\n", write->opener->u.open.pathname);
	if (restore)
		restore_contents(write->opener, write->saved, !write->is_pwrite,
				 "write");
	free(write->saved);
}

static ssize_t failtest_add_write(int fd, const void *buf,
				  size_t count, off_t off,
				  bool is_pwrite,
				  const char *file, unsigned line)
{
	struct failtest_call *p;
	struct write_call call;

	call.fd = fd;
	call.buf = buf;
	call.count = count;
	call.off = off;
	call.is_pwrite = is_pwrite;
	call.opener = opener_of(fd);
	p = add_history(FAILTEST_WRITE, false, file, line, &call);

	/* If we're a child, we need to make sure we write the same thing
	 * to non-files as the parent does, so tell it. */
	if (control_fd != -1 && off == (off_t)-1) {
		enum info_type type = WRITE;

		write_all(control_fd, &type, sizeof(type));
		write_all(control_fd, &p->u.write, sizeof(p->u.write));
		write_all(control_fd, buf, count);
	}

	/* FIXME: Try partial write returns. */
	if (should_fail(p)) {
		p->u.write.ret = -1;
		p->error = EIO;
	} else {
		bool is_file;
		assert(call.opener == p->u.write.opener);

		if (p->u.write.opener) {
			is_file = (p->u.write.opener->type == FAILTEST_OPEN);
		} else {
			/* We can't unwind it, so at least check same
			 * in parent and child. */
			is_file = false;
		}

		/* FIXME: We assume same write order in parent and child */
		if (!is_file && child_writes_num != 0) {
			if (child_writes[0].fd != fd)
				errx(1, "Child wrote to fd %u, not %u?",
				     child_writes[0].fd, fd);
			if (child_writes[0].off != p->u.write.off)
				errx(1, "Child wrote to offset %zu, not %zu?",
				     (size_t)child_writes[0].off,
				     (size_t)p->u.write.off);
			if (child_writes[0].count != count)
				errx(1, "Child wrote length %zu, not %zu?",
				     child_writes[0].count, count);
			if (memcmp(child_writes[0].buf, buf, count)) {
				child_fail(NULL, 0,
					   "Child wrote differently to"
					   " fd %u than we did!\n", fd);
			}
			free((char *)child_writes[0].buf);
			child_writes_num--;
			memmove(&child_writes[0], &child_writes[1],
				sizeof(child_writes[0]) * child_writes_num);

			/* Child wrote it already. */
			trace("write %s:%i on fd %i already done by child\n",
			      file, line, fd);
			p->u.write.ret = count;
			errno = p->error;
			return p->u.write.ret;
		}

		if (is_file) {
			p->u.write.saved = save_contents(call.opener->u.open.pathname,
							 fd, count, off,
							 "being overwritten");
			set_cleanup(p, cleanup_write, struct write_call);
		}

		/* Though off is current seek ptr for write case, we need to
		 * move it.  write() does that for us. */
		if (p->u.write.is_pwrite)
			p->u.write.ret = pwrite(fd, buf, count, off);
		else
			p->u.write.ret = write(fd, buf, count);
	}
	trace("%swrite %s:%i %zu@%llu on fd %i -> %zd\n",
	      p->u.write.is_pwrite ? "p" : "",
	      file, line, count, (long long)off, fd, p->u.write.ret);
	errno = p->error;
	return p->u.write.ret;
}

ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t offset,
			const char *file, unsigned line)
{
	return failtest_add_write(fd, buf, count, offset, true, file, line);
}

ssize_t failtest_write(int fd, const void *buf, size_t count,
		       const char *file, unsigned line)
{
	return failtest_add_write(fd, buf, count, lseek(fd, 0, SEEK_CUR), false,
				  file, line);
}

ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
		       const char *file, unsigned line)
{
	return failtest_add_read(fd, buf, count, off, true, file, line);
}

ssize_t failtest_read(int fd, void *buf, size_t count,
		      const char *file, unsigned line)
{
	return failtest_add_read(fd, buf, count, lseek(fd, 0, SEEK_CUR), false,
				 file, line);
}

static struct lock_info *WARN_UNUSED_RESULT
add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
{
	unsigned int i;
	struct lock_info *l;

	for (i = 0; i < lock_num; i++) {
		l = &locks[i];

		if (l->fd != fd)
			continue;
		/* Four cases we care about:
		 * Start overlap:
		 *	l =    |      |
		 *	new = |   |
		 * Mid overlap:
		 *	l =    |      |
		 *	new =    |  |
		 * End overlap:
		 *	l =    |      |
		 *	new =      |    |
		 * Total overlap:
		 *	l =    |      |
		 *	new = |         |
		 */
		if (start > l->start && end < l->end) {
			/* Mid overlap: trim entry, add new one. */
			off_t new_start, new_end;
			new_start = end + 1;
			new_end = l->end;
			trace("splitting lock on fd %i from %llu-%llu"
			      " to %llu-%llu\n",
			      fd, (long long)l->start, (long long)l->end,
			      (long long)l->start, (long long)start - 1);
			l->end = start - 1;
			locks = add_lock(locks,
					 fd, new_start, new_end, l->type);
			l = &locks[i];
		} else if (start <= l->start && end >= l->end) {
			/* Total overlap: eliminate entry. */
			trace("erasing lock on fd %i %llu-%llu\n",
			      fd, (long long)l->start, (long long)l->end);
			l->end = 0;
			l->start = 1;
		} else if (end >= l->start && end < l->end) {
			trace("trimming lock on fd %i from %llu-%llu"
			      " to %llu-%llu\n",
			      fd, (long long)l->start, (long long)l->end,
			      (long long)end + 1, (long long)l->end);
			/* Start overlap: trim entry. */
			l->start = end + 1;
		} else if (start > l->start && start <= l->end) {
			trace("trimming lock on fd %i from %llu-%llu"
			      " to %llu-%llu\n",
			      fd, (long long)l->start, (long long)l->end,
			      (long long)l->start, (long long)start - 1);
			/* End overlap: trim entry. */
			l->end = start-1;
		}
		/* Nothing left?  Remove it. */
		if (l->end < l->start) {
			trace("forgetting lock on fd %i\n", fd);
			memmove(l, l + 1, (--lock_num - i) * sizeof(l[0]));
			i--;
		}
	}

	if (type != F_UNLCK) {
		locks = realloc(locks, (lock_num + 1) * sizeof(*locks));
		l = &locks[lock_num++];
		l->fd = fd;
		l->start = start;
		l->end = end;
		l->type = type;
		trace("new lock on fd %i %llu-%llu\n",
		      fd, (long long)l->start, (long long)l->end);
	}
	return locks;
}

/* We trap this so we can record it: we don't fail it. */
int failtest_close(int fd, const char *file, unsigned line)
{
	struct close_call call;
	struct failtest_call *p, *opener;

	/* Do this before we add ourselves to history! */
	opener = opener_of(fd);

	call.fd = fd;
	p = add_history(FAILTEST_CLOSE, false, file, line, &call);
	p->fail = false;

	/* Consume close from failpath (shouldn't tell us to fail). */
	if (following_path()) {
		if (follow_path(p))
			abort();
	}

	trace("close on fd %i\n", fd);
	if (fd < 0)
		return close(fd);

	/* Mark opener as not leaking, remove its cleanup function. */
	if (opener) {
		trace("close on fd %i found opener %p\n", fd, opener);
		if (opener->type == FAILTEST_PIPE) {
			/* From a pipe? */
			if (opener->u.pipe.fds[0] == fd) {
				assert(!opener->u.pipe.closed[0]);
				opener->u.pipe.closed[0] = true;
			} else if (opener->u.pipe.fds[1] == fd) {
				assert(!opener->u.pipe.closed[1]);
				opener->u.pipe.closed[1] = true;
			} else
				abort();
			opener->can_leak = (!opener->u.pipe.closed[0]
					    || !opener->u.pipe.closed[1]);
		} else if (opener->type == FAILTEST_OPEN) {
			opener->u.open.closed = true;
			opener->can_leak = false;
		} else
			abort();
	}

	/* Restore offset now, in case parent shared (can't do after close!). */
	if (control_fd != -1) {
		struct failtest_call *i;

		tlist_for_each_rev(&history, i, list) {
			if (i == our_history_start)
				break;
			if (i == opener)
				break;
			if (i->type == FAILTEST_LSEEK && i->u.lseek.fd == fd) {
				trace("close on fd %i undoes lseek\n", fd);
				/* This seeks back. */
				i->cleanup(&i->u, true);
				i->cleanup = NULL;
			} else if (i->type == FAILTEST_WRITE
				   && i->u.write.fd == fd
				   && !i->u.write.is_pwrite) {
				trace("close on fd %i undoes write"
				      " offset change\n", fd);
				/* Write (not pwrite!) moves file offset! */
				if (lseek(fd, i->u.write.off, SEEK_SET)
				    != i->u.write.off) {
					fwarn("Restoring lseek pointer failed (write)");
				}
			} else if (i->type == FAILTEST_READ
				   && i->u.read.fd == fd) {
				/* preads don't *have* cleanups */
				if (i->cleanup) {
					trace("close on fd %i undoes read"
					      " offset change\n", fd);
					/* This seeks back. */
					i->cleanup(&i->u, true);
					i->cleanup = NULL;
				}
			}
		}
	}

	/* Close unlocks everything. */
	locks = add_lock(locks, fd, 0, off_max(), F_UNLCK);
	return close(fd);
}

/* Zero length means "to end of file" */
static off_t end_of(off_t start, off_t len)
{
	if (len == 0)
		return off_max();
	return start + len - 1;
}

/* FIXME: This only handles locks, really. */
int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...)
{
	struct failtest_call *p;
	struct fcntl_call call;
	va_list ap;

	call.fd = fd;
	call.cmd = cmd;

	/* Argument extraction. */
	switch (cmd) {
	case F_SETFL:
	case F_SETFD:
		va_start(ap, cmd);
		call.arg.l = va_arg(ap, long);
		va_end(ap);
		trace("fcntl on fd %i F_SETFL/F_SETFD\n", fd);
		return fcntl(fd, cmd, call.arg.l);
	case F_GETFD:
	case F_GETFL:
		trace("fcntl on fd %i F_GETFL/F_GETFD\n", fd);
		return fcntl(fd, cmd);
	case F_GETLK:
		trace("fcntl on fd %i F_GETLK\n", fd);
		get_locks();
		va_start(ap, cmd);
		call.arg.fl = *va_arg(ap, struct flock *);
		va_end(ap);
		return fcntl(fd, cmd, &call.arg.fl);
	case F_SETLK:
	case F_SETLKW:
		trace("fcntl on fd %i F_SETLK%s\n",
		      fd, cmd == F_SETLKW ? "W" : "");
		va_start(ap, cmd);
		call.arg.fl = *va_arg(ap, struct flock *);
		va_end(ap);
		break;
	default:
		/* This means you need to implement it here. */
		err(1, "failtest: unknown fcntl %u", cmd);
	}

	p = add_history(FAILTEST_FCNTL, false, file, line, &call);

	if (should_fail(p)) {
		p->u.fcntl.ret = -1;
		if (p->u.fcntl.cmd == F_SETLK)
			p->error = EAGAIN;
		else
			p->error = EDEADLK;
	} else {
		get_locks();
		p->u.fcntl.ret = fcntl(p->u.fcntl.fd, p->u.fcntl.cmd,
				       &p->u.fcntl.arg.fl);
		if (p->u.fcntl.ret == -1)
			p->error = errno;
		else {
			/* We don't handle anything else yet. */
			assert(p->u.fcntl.arg.fl.l_whence == SEEK_SET);
			locks = add_lock(locks,
					 p->u.fcntl.fd,
					 p->u.fcntl.arg.fl.l_start,
					 end_of(p->u.fcntl.arg.fl.l_start,
						p->u.fcntl.arg.fl.l_len),
					 p->u.fcntl.arg.fl.l_type);
		}
	}
	trace("fcntl on fd %i -> %i\n", fd, p->u.fcntl.ret);
	errno = p->error;
	return p->u.fcntl.ret;
}

static void cleanup_lseek(struct lseek_call *call, bool restore)
{
	if (restore) {
		trace("cleaning up lseek on fd %i -> %llu\n",
		      call->fd, (long long)call->old_off);
		if (lseek(call->fd, call->old_off, SEEK_SET) != call->old_off)
			fwarn("Restoring lseek pointer failed");
	}
}

/* We trap this so we can undo it: we don't fail it. */
off_t failtest_lseek(int fd, off_t offset, int whence, const char *file,
		     unsigned int line)
{
	struct failtest_call *p;
	struct lseek_call call;
	call.fd = fd;
	call.offset = offset;
	call.whence = whence;
	call.old_off = lseek(fd, 0, SEEK_CUR);

	p = add_history(FAILTEST_LSEEK, false, file, line, &call);
	p->fail = false;

	/* Consume lseek from failpath. */
	if (failpath)
		if (should_fail(p))
			abort();

	p->u.lseek.ret = lseek(fd, offset, whence);

	if (p->u.lseek.ret != (off_t)-1)
		set_cleanup(p, cleanup_lseek, struct lseek_call);

	trace("lseek %s:%u on fd %i from %llu to %llu%s\n",
	      file, line, fd, (long long)call.old_off, (long long)offset,
	      whence == SEEK_CUR ? " (from current off)" :
	      whence == SEEK_END ? " (from end)" :
	      whence == SEEK_SET ? "" : " (invalid whence)");
	return p->u.lseek.ret;
}


pid_t failtest_getpid(const char *file, unsigned line)
{
	/* You must call failtest_init first! */
	assert(orig_pid);
	return orig_pid;
}

void failtest_init(int argc, char *argv[])
{
	unsigned int i;

	orig_pid = getpid();

	warnf = fdopen(move_fd_to_high(dup(STDERR_FILENO)), "w");
	for (i = 1; i < argc; i++) {
		if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) {
			failpath = argv[i] + strlen("--failpath=");
		} else if (strcmp(argv[i], "--trace") == 0) {
			tracef = warnf;
			failtest_timeout_ms = -1;
		} else if (!strncmp(argv[i], "--debugpath=",
				    strlen("--debugpath="))) {
			debugpath = argv[i] + strlen("--debugpath=");
		}
	}
	failtable_init(&failtable);
	start = time_now();
}

bool failtest_has_failed(void)
{
	return control_fd != -1;
}

void failtest_exit(int status)
{
	trace("failtest_exit with status %i\n", status);
	if (failtest_exit_check) {
		if (!failtest_exit_check(&history))
			child_fail(NULL, 0, "failtest_exit_check failed\n");
	}

	failtest_cleanup(false, status);
}