1 #include "failtest_helper.h"
4 #include <ccan/tap/tap.h>
6 bool failtest_suppress = false;
8 /* FIXME: From ccan/str */
9 static inline bool strends(const char *str, const char *postfix)
11 if (strlen(str) < strlen(postfix))
14 return !strcmp(str + strlen(str) - strlen(postfix), postfix);
17 bool failmatch(const struct failtest_call *call,
18 const char *file, int line, enum failtest_call_type type)
20 return call->type == type
22 && ((strcmp(call->file, file) == 0)
23 || (strends(call->file, file)
24 && (call->file[strlen(call->file) - strlen(file) - 1]
28 static const struct failtest_call *
29 find_repeat(const struct failtest_call *start, const struct failtest_call *end,
30 const struct failtest_call *call)
32 const struct failtest_call *i;
34 for (i = start; i < end; i++) {
35 if (failmatch(i, call->file, call->line, call->type))
41 static bool is_nonblocking_lock(const struct failtest_call *call)
43 return call->type == FAILTEST_FCNTL && call->u.fcntl.cmd == F_SETLK;
46 static bool is_unlock(const struct failtest_call *call)
48 return call->type == FAILTEST_FCNTL
49 && call->u.fcntl.arg.fl.l_type == F_UNLCK;
52 bool exit_check_log(struct failtest_call *history, unsigned num)
56 for (i = 0; i < num; i++) {
59 /* Failing the /dev/urandom open doesn't count: we fall back. */
60 if (failmatch(&history[i], URANDOM_OPEN))
63 /* Similarly with read fail. */
64 if (failmatch(&history[i], URANDOM_READ))
67 /* Initial allocation of tdb doesn't log. */
68 if (failmatch(&history[i], INITIAL_TDB_MALLOC))
71 /* We don't block "failures" on non-blocking locks. */
72 if (is_nonblocking_lock(&history[i]))
75 if (!tap_log_messages)
76 diag("We didn't log for %u (%s:%u)",
77 i, history[i].file, history[i].line);
78 return tap_log_messages != 0;
83 /* Some places we soldier on despite errors: only fail them once. */
85 block_repeat_failures(struct failtest_call *history, unsigned num)
87 const struct failtest_call *i, *last = &history[num-1];
89 if (failtest_suppress)
90 return FAIL_DONT_FAIL;
92 if (failmatch(last, INITIAL_TDB_MALLOC)
93 || failmatch(last, URANDOM_OPEN)
94 || failmatch(last, URANDOM_READ)) {
95 if (find_repeat(history, last, last))
96 return FAIL_DONT_FAIL;
100 /* Unlock or non-blocking lock is fail-once. */
101 if (is_unlock(last)) {
102 /* Find a previous unlock at this point? */
103 for (i = find_repeat(history, last, last);
105 i = find_repeat(history, i, last)) {
107 return FAIL_DONT_FAIL;
110 } else if (is_nonblocking_lock(last)) {
111 /* Find a previous non-blocking lock at this point? */
112 for (i = find_repeat(history, last, last);
114 i = find_repeat(history, i, last)) {
115 if (is_nonblocking_lock(i))
116 return FAIL_DONT_FAIL;