Remove unistd.h
[helenos.git] / uspace / lib / bithenge / src / failure.c
blobd82a11b810828b65bf77e11244c5bba50cf54737
1 /*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
29 /** @cond internal */
30 /** @addtogroup bithenge
31 * @{
33 /**
34 * @file
35 * Fake system call errors for testing.
38 #include <errno.h>
39 #include <execinfo.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #define BITHENGE_FAILURE_DECLS_ONLY 1
46 #include "failure.h"
47 #include "common.h"
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
62 * call. */
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;
70 void *backtrace_item;
71 } backtrace_item_t;
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;
80 free(first);
84 static inline void initialize(void)
86 g_initialized = 1;
87 int rc = atexit(atexit_handler);
88 if (rc)
89 exit(127);
91 char *sel_str = getenv("BITHENGE_FAILURE_INDEX");
92 if (sel_str)
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
97 * hit. */
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. */
105 *bip = bi->next;
106 bi->next = g_backtrace_items;
107 g_backtrace_items = bi;
108 return 0;
112 /* No item found; create one. */
113 backtrace_item_t *i = malloc(sizeof(*i));
114 if (!i)
115 exit(127);
116 i->next = g_backtrace_items;
117 i->backtrace_item = addr;
118 g_backtrace_items = i;
119 return 1;
122 int bithenge_should_fail(void)
124 g_failure_index++;
126 if (!g_initialized)
127 initialize();
129 if (g_failure_index_selected != -1) {
130 if (g_failure_index == g_failure_index_selected)
131 return 1; /* breakpoint here */
132 return 0;
135 /* If all backtrace items have been seen already, there's no need to
136 * try raising an error. */
137 void *trace[256];
138 int size = backtrace(trace, 256);
139 int raise_error = 0;
140 for (int i = 0; i < size; i++) {
141 if (backtrace_item_hit(trace[i]))
142 raise_error = 1;
144 if (!raise_error)
145 return 0;
147 if (!fork()) {
148 /* Child silently fails. */
149 int null = open("/dev/null", O_WRONLY);
150 if (null == -1)
151 exit(127);
152 vfs_clone(null, STDOUT_FILENO, false);
153 vfs_clone(null, STDERR_FILENO, false);
154 vfs_put(null);
155 return 1;
158 /* Parent checks whether child failed correctly. */
159 int status;
160 wait(&status);
161 if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
162 return 0;
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",
168 g_failure_index);
169 return 1;
172 void *bithenge_failure_malloc(size_t size)
174 if (bithenge_should_fail())
175 return NULL;
176 return malloc(size);
179 void *bithenge_failure_realloc(void *ptr, size_t size)
181 if (bithenge_should_fail())
182 return NULL;
183 return realloc(ptr, size);
186 ssize_t bithenge_failure_read(int fd, void *buf, size_t count)
188 if (bithenge_should_fail()) {
189 errno = EIO;
190 return -1;
192 return read(fd, buf, count);
195 off_t bithenge_failure_lseek(int fd, off_t offset, int whither)
197 if (bithenge_should_fail()) {
198 errno = EINVAL;
199 return (off_t) -1;
201 return lseek(fd, offset, whither);
204 int bithenge_failure_ferror(FILE *stream)
206 if (bithenge_should_fail())
207 return 1;
208 return ferror(stream);
211 char *bithenge_failure_str_ndup(const char *s, size_t max_len)
213 if (bithenge_should_fail())
214 return NULL;
215 return str_ndup(s, max_len);
218 int bithenge_failure_open(const char *pathname, int flags)
220 if (bithenge_should_fail()) {
221 errno = EACCES;
222 return -1;
224 return open(pathname, flags);
227 int bithenge_failure_fstat(int fd, struct stat *buf)
229 if (bithenge_should_fail()) {
230 errno = EIO;
231 return -1;
233 return fstat(fd, buf);
236 /** @}
238 /** @endcond */