2 * Copyright (c) 2012 Sean Bartell
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /** @addtogroup bithenge
35 * Fake system call errors for testing.
43 #include <sys/types.h>
45 #define BITHENGE_FAILURE_DECLS_ONLY 1
49 /* This file raises fake errors from system calls, to test that Bithenge
50 * handles the errors correctly. It has two primary modes of operation,
51 * depending on an environment variable:
53 * BITHENGE_FAILURE_INDEX not set: when a system call is made, a child process
54 * returns a fake error from that call. If the child process handles the error
55 * correctly (exit code is 1), the main process continues without errors. If
56 * the child process has a problem, the main process raises the fake error
57 * again and shows all stdout and stderr output. For speed, errors are only
58 * raised when part of the backtrace has not been seen before.
60 * BITHENGE_FAILURE_INDEX set: the program runs normally until system call
61 * number BITHENGE_FAILURE_INDEX is made; a fake error is returned from this
64 static int g_initialized
= 0;
65 static int g_failure_index
= -1;
66 static int g_failure_index_selected
= -1;
68 typedef struct backtrace_item
{
69 struct backtrace_item
*next
;
73 static backtrace_item_t
*g_backtrace_items
= NULL
;
75 static void atexit_handler(void)
77 while (g_backtrace_items
) {
78 backtrace_item_t
*first
= g_backtrace_items
;
79 g_backtrace_items
= first
->next
;
84 static inline void initialize(void)
87 int rc
= atexit(atexit_handler
);
91 char *sel_str
= getenv("BITHENGE_FAILURE_INDEX");
93 g_failure_index_selected
= strtol(sel_str
, NULL
, 10);
96 /* Record a hit for a backtrace address and return whether this is the first
98 static inline int backtrace_item_hit(void *addr
)
100 backtrace_item_t
**bip
;
101 for (bip
= &g_backtrace_items
; *bip
; bip
= &(*bip
)->next
) {
102 backtrace_item_t
*bi
= *bip
;
103 if (bi
->backtrace_item
== addr
) {
104 /* Keep frequently accessed items near the front. */
106 bi
->next
= g_backtrace_items
;
107 g_backtrace_items
= bi
;
112 /* No item found; create one. */
113 backtrace_item_t
*i
= malloc(sizeof(*i
));
116 i
->next
= g_backtrace_items
;
117 i
->backtrace_item
= addr
;
118 g_backtrace_items
= i
;
122 int bithenge_should_fail(void)
129 if (g_failure_index_selected
!= -1) {
130 if (g_failure_index
== g_failure_index_selected
)
131 return 1; /* breakpoint here */
135 /* If all backtrace items have been seen already, there's no need to
136 * try raising an error. */
138 int size
= backtrace(trace
, 256);
140 for (int i
= 0; i
< size
; i
++) {
141 if (backtrace_item_hit(trace
[i
]))
148 /* Child silently fails. */
149 int null
= open("/dev/null", O_WRONLY
);
152 vfs_clone(null
, STDOUT_FILENO
, false);
153 vfs_clone(null
, STDERR_FILENO
, false);
158 /* Parent checks whether child failed correctly. */
161 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 1)
164 /* The child had an error! We couldn't easily debug it because it was
165 * in a separate process with redirected stdout and stderr. Do it again
166 * without redirecting or forking. */
167 fprintf(stderr
, "** Fake error raised here (BITHENGE_FAILURE_INDEX=%d)\n",
172 void *bithenge_failure_malloc(size_t size
)
174 if (bithenge_should_fail())
179 void *bithenge_failure_realloc(void *ptr
, size_t size
)
181 if (bithenge_should_fail())
183 return realloc(ptr
, size
);
186 ssize_t
bithenge_failure_read(int fd
, void *buf
, size_t count
)
188 if (bithenge_should_fail()) {
192 return read(fd
, buf
, count
);
195 off_t
bithenge_failure_lseek(int fd
, off_t offset
, int whither
)
197 if (bithenge_should_fail()) {
201 return lseek(fd
, offset
, whither
);
204 int bithenge_failure_ferror(FILE *stream
)
206 if (bithenge_should_fail())
208 return ferror(stream
);
211 char *bithenge_failure_str_ndup(const char *s
, size_t max_len
)
213 if (bithenge_should_fail())
215 return str_ndup(s
, max_len
);
218 int bithenge_failure_open(const char *pathname
, int flags
)
220 if (bithenge_should_fail()) {
224 return open(pathname
, flags
);
227 int bithenge_failure_fstat(int fd
, struct stat
*buf
)
229 if (bithenge_should_fail()) {
233 return fstat(fd
, buf
);