source: mainline/uspace/lib/bithenge/failure.c@ 0784869

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0784869 was 0784869, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: improve Doxygen documentation

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Copyright (c) 2012 Sean Bartell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @cond internal */
30/** @addtogroup bithenge
31 * @{
32 */
33/**
34 * @file
35 * Fake system call errors for testing.
36 */
37
38#include <errno.h>
39#include <execinfo.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <sys/stat.h>
44#include <sys/types.h>
45#include <sys/wait.h>
46#include <unistd.h>
47#define BITHENGE_FAILURE_DECLS_ONLY 1
48#include "failure.h"
49#include "os.h"
50
51/* This file raises fake errors from system calls, to test that Bithenge
52 * handles the errors correctly. It has two primary modes of operation,
53 * depending on an environment variable:
54 *
55 * BITHENGE_FAILURE_INDEX not set: when a system call is made, a child process
56 * returns a fake error from that call. If the child process handles the error
57 * correctly (exit code is 1), the main process continues without errors. If
58 * the child process has a problem, the main process raises the fake error
59 * again and shows all stdout and stderr output. For speed, errors are only
60 * raised when part of the backtrace has not been seen before.
61 *
62 * BITHENGE_FAILURE_INDEX set: the program runs normally until system call
63 * number BITHENGE_FAILURE_INDEX is made; a fake error is returned from this
64 * call. */
65
66static int g_initialized = 0;
67static int g_failure_index = -1;
68static int g_failure_index_selected = -1;
69
70typedef struct backtrace_item {
71 struct backtrace_item *next;
72 void *backtrace_item;
73} backtrace_item_t;
74
75static backtrace_item_t *g_backtrace_items = NULL;
76
77static void atexit_handler(void)
78{
79 while (g_backtrace_items) {
80 backtrace_item_t *first = g_backtrace_items;
81 g_backtrace_items = first->next;
82 free(first);
83 }
84}
85
86static inline void initialize(void)
87{
88 g_initialized = 1;
89 int rc = atexit(atexit_handler);
90 if (rc)
91 exit(127);
92
93 char *sel_str = getenv("BITHENGE_FAILURE_INDEX");
94 if (sel_str)
95 g_failure_index_selected = strtol(sel_str, NULL, 10);
96}
97
98/* Record a hit for a backtrace address and return whether this is the first
99 * hit. */
100static inline int backtrace_item_hit(void *addr)
101{
102 backtrace_item_t **bip;
103 for (bip = &g_backtrace_items; *bip; bip = &(*bip)->next) {
104 backtrace_item_t *bi = *bip;
105 if (bi->backtrace_item == addr) {
106 /* Keep frequently accessed items near the front. */
107 *bip = bi->next;
108 bi->next = g_backtrace_items;
109 g_backtrace_items = bi;
110 return 0;
111 }
112 }
113
114 /* No item found; create one. */
115 backtrace_item_t *i = malloc(sizeof(*i));
116 if (!i)
117 exit(127);
118 i->next = g_backtrace_items;
119 i->backtrace_item = addr;
120 g_backtrace_items = i;
121 return 1;
122}
123
124int bithenge_should_fail(void)
125{
126 g_failure_index++;
127
128 if (!g_initialized)
129 initialize();
130
131 if (g_failure_index_selected != -1) {
132 if (g_failure_index == g_failure_index_selected)
133 return 1; /* breakpoint here */
134 return 0;
135 }
136
137 /* If all backtrace items have been seen already, there's no need to
138 * try raising an error. */
139 void *trace[256];
140 int size = backtrace(trace, 256);
141 int raise_error = 0;
142 for (int i = 0; i < size; i++) {
143 if (backtrace_item_hit(trace[i]))
144 raise_error = 1;
145 }
146 if (!raise_error)
147 return 0;
148
149 if (!fork()) {
150 /* Child silently fails. */
151 int null = open("/dev/null", O_WRONLY);
152 if (null == -1)
153 exit(127);
154 dup2(null, STDOUT_FILENO);
155 dup2(null, STDERR_FILENO);
156 close(null);
157 return 1;
158 }
159
160 /* Parent checks whether child failed correctly. */
161 int status;
162 wait(&status);
163 if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
164 return 0;
165
166 /* The child had an error! We couldn't easily debug it because it was
167 * in a separate process with redirected stdout and stderr. Do it again
168 * without redirecting or forking. */
169 fprintf(stderr, "** Fake error raised here (BITHENGE_FAILURE_INDEX=%d)\n",
170 g_failure_index);
171 return 1;
172}
173
174void *bithenge_failure_malloc(size_t size)
175{
176 if (bithenge_should_fail())
177 return NULL;
178 return malloc(size);
179}
180
181void *bithenge_failure_realloc(void *ptr, size_t size)
182{
183 if (bithenge_should_fail())
184 return NULL;
185 return realloc(ptr, size);
186}
187
188ssize_t bithenge_failure_read(int fd, void *buf, size_t count)
189{
190 if (bithenge_should_fail()) {
191 errno = EIO;
192 return -1;
193 }
194 return read(fd, buf, count);
195}
196
197off_t bithenge_failure_lseek(int fd, off_t offset, int whither)
198{
199 if (bithenge_should_fail()) {
200 errno = EINVAL;
201 return (off_t) -1;
202 }
203 return lseek(fd, offset, whither);
204}
205
206int bithenge_failure_ferror(FILE *stream)
207{
208 if (bithenge_should_fail())
209 return 1;
210 return ferror(stream);
211}
212
213char *bithenge_failure_str_ndup(const char *s, size_t max_len)
214{
215 if (bithenge_should_fail())
216 return NULL;
217 return str_ndup(s, max_len);
218}
219
220int bithenge_failure_open(const char *pathname, int flags)
221{
222 if (bithenge_should_fail()) {
223 errno = EACCES;
224 return -1;
225 }
226 return open(pathname, flags);
227}
228
229int bithenge_failure_fstat(int fd, struct stat *buf)
230{
231 if (bithenge_should_fail()) {
232 errno = EIO;
233 return -1;
234 }
235 return fstat(fd, buf);
236}
237
238/** @}
239 */
240/** @endcond */
Note: See TracBrowser for help on using the repository browser.