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