1 #ifndef CCAN_FAILTEST_H
2 #define CCAN_FAILTEST_H
7 #include <ccan/compiler/compiler.h>
10 * failtest_init - initialize the failtest module
11 * @argc: the number of commandline arguments
12 * @argv: the commandline argument array
14 * This initializes the module, and in particular if argv[1] is "--failpath="
15 * then it ensures that failures follow that pattern. This allows easy
16 * debugging of complex failure paths.
18 void failtest_init(int argc, char *argv[]);
21 * failtest_exit - clean up and exit the test
22 * @status: the status (usually exit_status() from ccan/tap).
24 * This cleans up and changes to files made in this child, and exits the test.
25 * It also calls your failtest_default_hook, if any.
27 * A child which does not exit via failtest_exit() will cause the overall test
30 void NORETURN failtest_exit(int status);
33 * enum failtest_call_type - discriminator for failtest_call.u
35 enum failtest_call_type {
109 * struct failtest_call - description of a call redirected to failtest module
110 * @type: the call type
111 * @file: the filename of the caller
112 * @line: the line number of the caller
113 * @fail: did this call fail
114 * @error: the errno (if any)
115 * @u: the union of call data
117 * This structure is used to represent the ordered history of calls.
120 * failtest_hook, failtest_exit_check
122 struct failtest_call {
123 enum failtest_call_type type;
124 /* Where we were called from. */
129 /* What we set errno to. */
131 /* How do we clean this up? */
132 void (*cleanup)(void *u);
133 /* The actual call data. */
135 struct calloc_call calloc;
136 struct malloc_call malloc;
137 struct realloc_call realloc;
138 struct open_call open;
139 struct close_call close;
140 struct pipe_call pipe;
141 struct read_call read;
142 struct write_call write;
143 struct fcntl_call fcntl;
147 enum failtest_result {
148 /* Yes try failing this call. */
150 /* No, don't try failing this call. */
152 /* Try failing this call but don't go too far down that path. */
157 * failtest_hook - whether a certain call should fail or not.
158 * @history: the ordered history of all failtest calls.
159 * @num: the number of elements in @history (greater than 0)
161 * The default value of this hook is failtest_default_hook(), which returns
162 * FAIL_OK (ie. yes, fail the call).
164 * You can override it, and avoid failing certain calls. The parameters
165 * of the call (but not the return value(s)) will be filled in for the last
169 * static enum failtest_result dont_fail_alloc(struct failtest_call *hist,
172 * if (hist[num-1].type == FAILTEST_MALLOC
173 * || hist[num-1].type == FAILTEST_CALLOC
174 * || hist[num-1].type == FAILTEST_REALLOC)
175 * return FAIL_DONT_FAIL;
179 * failtest_hook = dont_fail_alloc;
181 extern enum failtest_result
182 (*failtest_hook)(struct failtest_call *history, unsigned num);
185 * failtest_exit_check - hook for additional checks on a failed child.
186 * @history: the ordered history of all failtest calls.
187 * @num: the number of elements in @history (greater than 0)
189 * Your program might have additional checks to do on failure, such as
190 * check that a file is not corrupted, or than an error message has been
193 * If this returns false, the path to this failure will be printed and the
194 * overall test will fail.
196 extern bool (*failtest_exit_check)(struct failtest_call *history,
200 * failtest_timeout_ms - how long to wait before killing child.
202 * Default is 20,000 (20 seconds).
204 extern unsigned int failtest_timeout_ms;
205 #endif /* CCAN_FAILTEST_H */