failtest.c 42 KB

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