failtest.c 24 KB

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