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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d8023313 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • 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 <stddef.h>
44#include <sys/wait.h>
45#define BITHENGE_FAILURE_DECLS_ONLY 1
46#include "failure.h"
47#include "common.h"
48
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:
52 *
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.
59 *
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. */
63
64static int g_initialized = 0;
65static int g_failure_index = -1;
66static int g_failure_index_selected = -1;
67
68typedef struct backtrace_item {
69 struct backtrace_item *next;
70 void *backtrace_item;
71} backtrace_item_t;
72
73static backtrace_item_t *g_backtrace_items = NULL;
74
75static void atexit_handler(void)
76{
77 while (g_backtrace_items) {
78 backtrace_item_t *first = g_backtrace_items;
79 g_backtrace_items = first->next;
80 free(first);
81 }
82}
83
84static inline void initialize(void)
85{
86 g_initialized = 1;
87 errno_t rc = atexit(atexit_handler);
88 if (rc)
89 exit(127);
90
91 char *sel_str = getenv("BITHENGE_FAILURE_INDEX");
92 if (sel_str)
93 g_failure_index_selected = strtol(sel_str, NULL, 10);
94}
95
96/* Record a hit for a backtrace address and return whether this is the first
97 * hit. */
98static inline errno_t backtrace_item_hit(void *addr)
99{
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;
109 }
110 }
111
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;
120}
121
122errno_t bithenge_should_fail(void)
123{
124 g_failure_index++;
125
126 if (!g_initialized)
127 initialize();
128
129 if (g_failure_index_selected != -1) {
130 if (g_failure_index == g_failure_index_selected)
131 return 1; /* breakpoint here */
132 return 0;
133 }
134
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;
143 }
144 if (!raise_error)
145 return 0;
146
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;
156 }
157
158 /* Parent checks whether child failed correctly. */
159 int status;
160 wait(&status);
161 if (WIFEXITED(status) && WEXITSTATUS(status) == 1)
162 return 0;
163
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;
170}
171
172void *bithenge_failure_malloc(size_t size)
173{
174 if (bithenge_should_fail())
175 return NULL;
176 return malloc(size);
177}
178
179void *bithenge_failure_realloc(void *ptr, size_t size)
180{
181 if (bithenge_should_fail())
182 return NULL;
183 return realloc(ptr, size);
184}
185
186ssize_t bithenge_failure_read(int fd, void *buf, size_t count)
187{
188 if (bithenge_should_fail()) {
189 errno = EIO;
190 return -1;
191 }
192 return read(fd, buf, count);
193}
194
195off_t bithenge_failure_lseek(int fd, off_t offset, int whither)
196{
197 if (bithenge_should_fail()) {
198 errno = EINVAL;
199 return (off_t) -1;
200 }
201 return lseek(fd, offset, whither);
202}
203
204errno_t bithenge_failure_ferror(FILE *stream)
205{
206 if (bithenge_should_fail())
207 return 1;
208 return ferror(stream);
209}
210
211char *bithenge_failure_str_ndup(const char *s, size_t max_len)
212{
213 if (bithenge_should_fail())
214 return NULL;
215 return str_ndup(s, max_len);
216}
217
218errno_t bithenge_failure_open(const char *pathname, int flags)
219{
220 if (bithenge_should_fail()) {
221 errno = EACCES;
222 return -1;
223 }
224 return open(pathname, flags);
225}
226
227errno_t bithenge_failure_fstat(int fd, struct stat *buf)
228{
229 if (bithenge_should_fail()) {
230 errno = EIO;
231 return -1;
232 }
233 return fstat(fd, buf);
234}
235
236/** @}
237 */
238/** @endcond */
Note: See TracBrowser for help on using the repository browser.