etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / krb5 / warn.c
bloba8fbdeb56c5dda275c80c8ba8283566fd82bbb0a
1 /* $NetBSD: warn.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
3 /*
4 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
33 * SUCH DAMAGE.
36 #include "krb5_locl.h"
37 #include <err.h>
39 static krb5_error_code _warnerr(krb5_context context, int do_errtext,
40 krb5_error_code code, int level, const char *fmt, va_list ap)
41 __attribute__((__format__(__printf__, 5, 0)));
43 static krb5_error_code
44 _warnerr(krb5_context context, int do_errtext,
45 krb5_error_code code, int level, const char *fmt, va_list ap)
47 char xfmt[7] = "";
48 const char *args[2], **arg;
49 char *msg = NULL;
50 const char *err_str = NULL;
51 krb5_error_code ret;
53 args[0] = args[1] = NULL;
54 arg = args;
55 if(fmt){
56 strlcat(xfmt, "%s", sizeof(xfmt));
57 if(do_errtext)
58 strlcat(xfmt, ": ", sizeof(xfmt));
59 ret = vasprintf(&msg, fmt, ap);
60 if(ret < 0 || msg == NULL)
61 return ENOMEM;
62 *arg++ = msg;
64 if(context && do_errtext){
65 strlcat(xfmt, "%s", sizeof(xfmt));
67 err_str = krb5_get_error_message(context, code);
68 if (err_str != NULL) {
69 *arg = err_str;
70 } else {
71 *arg= "<unknown error>";
75 if(context && context->warn_dest)
76 krb5_log(context, context->warn_dest, level, xfmt, args[0], args[1]);
77 else
78 warnx(xfmt, args[0], args[1]);
79 free(msg);
80 krb5_free_error_message(context, err_str);
81 return 0;
84 #define FUNC(ETEXT, CODE, LEVEL) \
85 krb5_error_code ret; \
86 va_list ap; \
87 va_start(ap, fmt); \
88 ret = _warnerr(context, ETEXT, CODE, LEVEL, fmt, ap); \
89 va_end(ap);
91 #undef __attribute__
92 #define __attribute__(X)
94 /**
95 * Log a warning to the log, default stderr, include the error from
96 * the last failure.
98 * @param context A Kerberos 5 context.
99 * @param code error code of the last error
100 * @param fmt message to print
101 * @param ap arguments
103 * @ingroup krb5_error
106 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
107 krb5_vwarn(krb5_context context, krb5_error_code code,
108 const char *fmt, va_list ap)
109 __attribute__ ((format (printf, 3, 0)))
111 return _warnerr(context, 1, code, 1, fmt, ap);
115 * Log a warning to the log, default stderr, include the error from
116 * the last failure.
118 * @param context A Kerberos 5 context.
119 * @param code error code of the last error
120 * @param fmt message to print
122 * @ingroup krb5_error
125 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
126 krb5_warn(krb5_context context, krb5_error_code code, const char *fmt, ...)
127 __attribute__ ((format (printf, 3, 4)))
129 FUNC(1, code, 1);
130 return ret;
134 * Log a warning to the log, default stderr.
136 * @param context A Kerberos 5 context.
137 * @param fmt message to print
138 * @param ap arguments
140 * @ingroup krb5_error
143 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
144 krb5_vwarnx(krb5_context context, const char *fmt, va_list ap)
145 __attribute__ ((format (printf, 2, 0)))
147 return _warnerr(context, 0, 0, 1, fmt, ap);
151 * Log a warning to the log, default stderr.
153 * @param context A Kerberos 5 context.
154 * @param fmt message to print
156 * @ingroup krb5_error
159 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
160 krb5_warnx(krb5_context context, const char *fmt, ...)
161 __attribute__ ((format (printf, 2, 3)))
163 FUNC(0, 0, 1);
164 return ret;
168 * Log a warning to the log, default stderr, include bthe error from
169 * the last failure and then exit.
171 * @param context A Kerberos 5 context
172 * @param eval the exit code to exit with
173 * @param code error code of the last error
174 * @param fmt message to print
175 * @param ap arguments
177 * @ingroup krb5_error
180 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
181 krb5_verr(krb5_context context, int eval, krb5_error_code code,
182 const char *fmt, va_list ap)
183 __attribute__ ((noreturn, format (printf, 4, 0)))
185 _warnerr(context, 1, code, 0, fmt, ap);
186 exit(eval);
187 UNREACHABLE(return 0);
191 * Log a warning to the log, default stderr, include bthe error from
192 * the last failure and then exit.
194 * @param context A Kerberos 5 context
195 * @param eval the exit code to exit with
196 * @param code error code of the last error
197 * @param fmt message to print
199 * @ingroup krb5_error
202 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
203 krb5_err(krb5_context context, int eval, krb5_error_code code,
204 const char *fmt, ...)
205 __attribute__ ((noreturn, format (printf, 4, 5)))
207 FUNC(1, code, 0);
208 exit(eval);
209 UNREACHABLE(return 0);
213 * Log a warning to the log, default stderr, and then exit.
215 * @param context A Kerberos 5 context
216 * @param eval the exit code to exit with
217 * @param fmt message to print
218 * @param ap arguments
220 * @ingroup krb5_error
223 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
224 krb5_verrx(krb5_context context, int eval, const char *fmt, va_list ap)
225 __attribute__ ((noreturn, format (printf, 3, 0)))
227 _warnerr(context, 0, 0, 0, fmt, ap);
228 exit(eval);
229 UNREACHABLE(return 0);
233 * Log a warning to the log, default stderr, and then exit.
235 * @param context A Kerberos 5 context
236 * @param eval the exit code to exit with
237 * @param fmt message to print
239 * @ingroup krb5_error
242 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
243 krb5_errx(krb5_context context, int eval, const char *fmt, ...)
244 __attribute__ ((noreturn, format (printf, 3, 4)))
246 FUNC(0, 0, 0);
247 exit(eval);
248 UNREACHABLE(return 0);
252 * Log a warning to the log, default stderr, include bthe error from
253 * the last failure and then abort.
255 * @param context A Kerberos 5 context
256 * @param code error code of the last error
257 * @param fmt message to print
258 * @param ap arguments
260 * @ingroup krb5_error
263 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
264 krb5_vabort(krb5_context context, krb5_error_code code,
265 const char *fmt, va_list ap)
266 __attribute__ ((noreturn, format (printf, 3, 0)))
268 _warnerr(context, 1, code, 0, fmt, ap);
269 abort();
270 UNREACHABLE(return 0);
274 * Log a warning to the log, default stderr, include the error from
275 * the last failure and then abort.
277 * @param context A Kerberos 5 context
278 * @param code error code of the last error
279 * @param fmt message to print
281 * @ingroup krb5_error
284 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
285 krb5_abort(krb5_context context, krb5_error_code code, const char *fmt, ...)
286 __attribute__ ((noreturn, format (printf, 3, 4)))
288 FUNC(1, code, 0);
289 abort();
290 UNREACHABLE(return 0);
293 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
294 krb5_vabortx(krb5_context context, const char *fmt, va_list ap)
295 __attribute__ ((noreturn, format (printf, 2, 0)))
297 _warnerr(context, 0, 0, 0, fmt, ap);
298 abort();
299 UNREACHABLE(return 0);
303 * Log a warning to the log, default stderr, and then abort.
305 * @param context A Kerberos 5 context
306 * @param code error code of the last error
307 * @param fmt message to print
309 * @ingroup krb5_error
312 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
313 krb5_abortx(krb5_context context, const char *fmt, ...)
314 __attribute__ ((noreturn, format (printf, 2, 3)))
316 FUNC(0, 0, 0);
317 abort();
318 UNREACHABLE(return 0);
322 * Set the default logging facility.
324 * @param context A Kerberos 5 context
325 * @param fac Facility to use for logging.
327 * @ingroup krb5_error
330 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
331 krb5_set_warn_dest(krb5_context context, krb5_log_facility *fac)
333 context->warn_dest = fac;
334 return 0;
338 * Get the default logging facility.
340 * @param context A Kerberos 5 context
342 * @ingroup krb5_error
345 KRB5_LIB_FUNCTION krb5_log_facility * KRB5_LIB_CALL
346 krb5_get_warn_dest(krb5_context context)
348 return context->warn_dest;