1 /* Copyright (c) 2008 The NetBSD Foundation, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
26 #include "atf-c/error.h"
33 #include "atf-c/detail/sanity.h"
35 /* Theoretically, there can only be a single error intance at any given
36 * point in time, because errors are raised at one point and must be
37 * handled immediately. If another error has to be raised during the
38 * handling process, something else has to be done with the previous
41 * This is per-thread information and will break threaded tests, but we
42 * currently do not have any threading support; therefore, this is fine. */
43 static bool error_on_flight = false;
45 /* ---------------------------------------------------------------------
46 * Auxiliary functions.
47 * --------------------------------------------------------------------- */
51 error_format(const atf_error_t err, char *buf, size_t buflen)
54 snprintf(buf, buflen, "Error '%s'", err->m_type);
59 error_init(atf_error_t err, const char *type, void *data, size_t datalen,
60 void (*format)(const atf_error_t, char *, size_t))
64 PRE(data != NULL || datalen == 0);
65 PRE(datalen != 0 || data == NULL);
69 err->m_format = (format == NULL) ? error_format : format;
75 err->m_data = malloc(datalen);
76 if (err->m_data == NULL) {
79 memcpy(err->m_data, data, datalen);
85 /* ---------------------------------------------------------------------
86 * The "atf_error" type.
87 * --------------------------------------------------------------------- */
90 atf_error_new(const char *type, void *data, size_t datalen,
91 void (*format)(const atf_error_t, char *, size_t))
95 PRE(!error_on_flight);
96 PRE(data != NULL || datalen == 0);
97 PRE(datalen != 0 || data == NULL);
99 err = malloc(sizeof(*err));
101 err = atf_no_memory_error();
103 if (!error_init(err, type, data, datalen, format)) {
105 err = atf_no_memory_error();
108 error_on_flight = true;
113 POST(error_on_flight);
118 atf_error_free(atf_error_t err)
122 PRE(error_on_flight);
125 freeit = err->m_free;
127 if (err->m_data != NULL)
133 error_on_flight = false;
143 atf_is_error(const atf_error_t err)
149 atf_error_is(const atf_error_t err, const char *type)
153 return strcmp(err->m_type, type) == 0;
157 atf_error_data(const atf_error_t err)
165 atf_error_format(const atf_error_t err, char *buf, size_t buflen)
168 err->m_format(err, buf, buflen);
171 /* ---------------------------------------------------------------------
172 * Common error types.
173 * --------------------------------------------------------------------- */
179 struct atf_libc_error_data {
183 typedef struct atf_libc_error_data atf_libc_error_data_t;
187 libc_format(const atf_error_t err, char *buf, size_t buflen)
189 const atf_libc_error_data_t *data;
191 PRE(atf_error_is(err, "libc"));
193 data = atf_error_data(err);
194 snprintf(buf, buflen, "%s: %s", data->m_what, strerror(data->m_errno));
198 atf_libc_error(int syserrno, const char *fmt, ...)
201 atf_libc_error_data_t data;
204 data.m_errno = syserrno;
206 vsnprintf(data.m_what, sizeof(data.m_what), fmt, ap);
209 err = atf_error_new("libc", &data, sizeof(data), libc_format);
215 atf_libc_error_code(const atf_error_t err)
217 const struct atf_libc_error_data *data;
219 PRE(atf_error_is(err, "libc"));
221 data = atf_error_data(err);
223 return data->m_errno;
227 atf_libc_error_msg(const atf_error_t err)
229 const struct atf_libc_error_data *data;
231 PRE(atf_error_is(err, "libc"));
233 data = atf_error_data(err);
239 * The "no_memory" error.
242 static struct atf_error no_memory_error;
246 no_memory_format(const atf_error_t err, char *buf, size_t buflen)
248 PRE(atf_error_is(err, "no_memory"));
250 snprintf(buf, buflen, "Not enough memory");
254 atf_no_memory_error(void)
256 PRE(!error_on_flight);
258 error_init(&no_memory_error, "no_memory", NULL, 0,
261 error_on_flight = true;
262 return &no_memory_error;