failtest.c 37 KB

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