source: mainline/uspace/lib/bithenge/src/failure.c@ 151f1cc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 151f1cc was 9c4cf0d, checked in by Jakub Jermar <jakub@…>, 9 years ago

Rename close() to vfs_put()

This is motivated mainly by the fact that a file handle does not
necessarily correspond to an open file and close() was no longer the
the opposite operation to open().

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