2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "atf-c/error.h"
37 #include "detail/sanity.h"
39 /* Theoretically, there can only be a single error intance at any given
40 * point in time, because errors are raised at one point and must be
41 * handled immediately. If another error has to be raised during the
42 * handling process, something else has to be done with the previous
45 * This is per-thread information and will break threaded tests, but we
46 * currently do not have any threading support; therefore, this is fine. */
47 static bool error_on_flight
= false;
49 /* ---------------------------------------------------------------------
50 * Auxiliary functions.
51 * --------------------------------------------------------------------- */
55 error_format(const atf_error_t err
, char *buf
, size_t buflen
)
58 snprintf(buf
, buflen
, "Error '%s'", err
->m_type
);
63 error_init(atf_error_t err
, const char *type
, void *data
, size_t datalen
,
64 void (*format
)(const atf_error_t
, char *, size_t))
68 PRE(data
!= NULL
|| datalen
== 0);
69 PRE(datalen
!= 0 || data
== NULL
);
73 err
->m_format
= (format
== NULL
) ? error_format
: format
;
79 err
->m_data
= malloc(datalen
);
80 if (err
->m_data
== NULL
) {
83 memcpy(err
->m_data
, data
, datalen
);
89 /* ---------------------------------------------------------------------
90 * The "atf_error" type.
91 * --------------------------------------------------------------------- */
94 atf_error_new(const char *type
, void *data
, size_t datalen
,
95 void (*format
)(const atf_error_t
, char *, size_t))
99 PRE(!error_on_flight
);
100 PRE(data
!= NULL
|| datalen
== 0);
101 PRE(datalen
!= 0 || data
== NULL
);
103 err
= malloc(sizeof(*err
));
105 err
= atf_no_memory_error();
107 if (!error_init(err
, type
, data
, datalen
, format
)) {
109 err
= atf_no_memory_error();
112 error_on_flight
= true;
117 POST(error_on_flight
);
122 atf_error_free(atf_error_t err
)
126 PRE(error_on_flight
);
129 freeit
= err
->m_free
;
131 if (err
->m_data
!= NULL
)
137 error_on_flight
= false;
147 atf_is_error(const atf_error_t err
)
153 atf_error_is(const atf_error_t err
, const char *type
)
157 return strcmp(err
->m_type
, type
) == 0;
161 atf_error_data(const atf_error_t err
)
169 atf_error_format(const atf_error_t err
, char *buf
, size_t buflen
)
172 err
->m_format(err
, buf
, buflen
);
175 /* ---------------------------------------------------------------------
176 * Common error types.
177 * --------------------------------------------------------------------- */
183 struct atf_libc_error_data
{
187 typedef struct atf_libc_error_data atf_libc_error_data_t
;
191 libc_format(const atf_error_t err
, char *buf
, size_t buflen
)
193 const atf_libc_error_data_t
*data
;
195 PRE(atf_error_is(err
, "libc"));
197 data
= atf_error_data(err
);
198 snprintf(buf
, buflen
, "%s: %s", data
->m_what
, strerror(data
->m_errno
));
202 atf_libc_error(int syserrno
, const char *fmt
, ...)
205 atf_libc_error_data_t data
;
208 data
.m_errno
= syserrno
;
210 vsnprintf(data
.m_what
, sizeof(data
.m_what
), fmt
, ap
);
213 err
= atf_error_new("libc", &data
, sizeof(data
), libc_format
);
219 atf_libc_error_code(const atf_error_t err
)
221 const struct atf_libc_error_data
*data
;
223 PRE(atf_error_is(err
, "libc"));
225 data
= atf_error_data(err
);
227 return data
->m_errno
;
231 atf_libc_error_msg(const atf_error_t err
)
233 const struct atf_libc_error_data
*data
;
235 PRE(atf_error_is(err
, "libc"));
237 data
= atf_error_data(err
);
243 * The "no_memory" error.
246 static struct atf_error no_memory_error
;
250 no_memory_format(const atf_error_t err
, char *buf
, size_t buflen
)
252 PRE(atf_error_is(err
, "no_memory"));
254 snprintf(buf
, buflen
, "Not enough memory");
258 atf_no_memory_error(void)
260 PRE(!error_on_flight
);
262 error_init(&no_memory_error
, "no_memory", NULL
, 0,
265 error_on_flight
= true;
266 return &no_memory_error
;