1 /* $NetBSD: error.c,v 1.1.1.1 2011/04/13 18:15:10 elric Exp $ */
4 * Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @page page_error Hx509 error reporting functions
41 * See the library functions here: @ref hx509_error
44 struct hx509_error_data
{
51 free_error_string(hx509_error msg
)
54 hx509_error m2
= msg
->next
;
62 * Resets the error strings the hx509 context.
64 * @param context A hx509 context.
66 * @ingroup hx509_error
70 hx509_clear_error_string(hx509_context context
)
73 free_error_string(context
->error
);
74 context
->error
= NULL
;
79 * Add an error message to the hx509 context.
81 * @param context A hx509 context.
83 * - HX509_ERROR_APPEND appends the error string to the old messages
85 * @param code error code related to error message
86 * @param fmt error message format
87 * @param ap arguments to error message format
89 * @ingroup hx509_error
93 hx509_set_error_stringv(hx509_context context
, int flags
, int code
,
94 const char *fmt
, va_list ap
)
101 msg
= calloc(1, sizeof(*msg
));
103 hx509_clear_error_string(context
);
107 if (vasprintf(&msg
->msg
, fmt
, ap
) == -1) {
108 hx509_clear_error_string(context
);
114 if (flags
& HX509_ERROR_APPEND
) {
115 msg
->next
= context
->error
;
116 context
->error
= msg
;
118 free_error_string(context
->error
);
119 context
->error
= msg
;
124 * See hx509_set_error_stringv().
126 * @param context A hx509 context.
128 * - HX509_ERROR_APPEND appends the error string to the old messages
130 * @param code error code related to error message
131 * @param fmt error message format
132 * @param ... arguments to error message format
134 * @ingroup hx509_error
138 hx509_set_error_string(hx509_context context
, int flags
, int code
,
139 const char *fmt
, ...)
144 hx509_set_error_stringv(context
, flags
, code
, fmt
, ap
);
149 * Get an error string from context associated with error_code.
151 * @param context A hx509 context.
152 * @param error_code Get error message for this error code.
154 * @return error string, free with hx509_free_error_string().
156 * @ingroup hx509_error
160 hx509_get_error_string(hx509_context context
, int error_code
)
162 struct rk_strpool
*p
= NULL
;
163 hx509_error msg
= context
->error
;
165 if (msg
== NULL
|| msg
->code
!= error_code
) {
169 cstr
= com_right(context
->et_list
, error_code
);
172 cstr
= strerror(error_code
);
175 if (asprintf(&str
, "<unknown error: %d>", error_code
) == -1)
180 for (msg
= context
->error
; msg
; msg
= msg
->next
)
181 p
= rk_strpoolprintf(p
, "%s%s", msg
->msg
,
182 msg
->next
!= NULL
? "; " : "");
184 return rk_strpoolcollect(p
);
188 * Free error string returned by hx509_get_error_string().
190 * @param str error string to free.
192 * @ingroup hx509_error
196 hx509_free_error_string(char *str
)
202 * Print error message and fatally exit from error code
204 * @param context A hx509 context.
205 * @param exit_code exit() code from process.
206 * @param error_code Error code for the reason to exit.
207 * @param fmt format string with the exit message.
208 * @param ... argument to format string.
210 * @ingroup hx509_error
214 hx509_err(hx509_context context
, int exit_code
,
215 int error_code
, const char *fmt
, ...)
222 vasprintf(&str
, fmt
, ap
);
224 msg
= hx509_get_error_string(context
, error_code
);
228 errx(exit_code
, "%s: %s", str
, msg
);