]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/isc/error.c
Update BIND to 9.9.8
[FreeBSD/stable/9.git] / contrib / bind9 / lib / isc / error.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: error.c,v 1.21 2007/06/19 23:47:17 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <isc/error.h>
28 #include <isc/msgs.h>
29 #include <isc/print.h>
30
31 /*% Default unexpected callback. */
32 static void
33 default_unexpected_callback(const char *, int, const char *, va_list)
34      ISC_FORMAT_PRINTF(3, 0);
35
36 /*% Default fatal callback. */
37 static void
38 default_fatal_callback(const char *, int, const char *, va_list)
39      ISC_FORMAT_PRINTF(3, 0);
40
41 /*% unexpected_callback */
42 static isc_errorcallback_t unexpected_callback = default_unexpected_callback;
43 static isc_errorcallback_t fatal_callback = default_fatal_callback;
44
45 void
46 isc_error_setunexpected(isc_errorcallback_t cb) {
47         if (cb == NULL)
48                 unexpected_callback = default_unexpected_callback;
49         else
50                 unexpected_callback = cb;
51 }
52
53 void
54 isc_error_setfatal(isc_errorcallback_t cb) {
55         if (cb == NULL)
56                 fatal_callback = default_fatal_callback;
57         else
58                 fatal_callback = cb;
59 }
60
61 void
62 isc_error_unexpected(const char *file, int line, const char *format, ...) {
63         va_list args;
64
65         va_start(args, format);
66         (unexpected_callback)(file, line, format, args);
67         va_end(args);
68 }
69
70 void
71 isc_error_fatal(const char *file, int line, const char *format, ...) {
72         va_list args;
73
74         va_start(args, format);
75         (fatal_callback)(file, line, format, args);
76         va_end(args);
77         abort();
78 }
79
80 void
81 isc_error_runtimecheck(const char *file, int line, const char *expression) {
82         isc_error_fatal(file, line, "RUNTIME_CHECK(%s) %s", expression,
83                         isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
84                                        ISC_MSG_FAILED, "failed"));
85 }
86
87 static void
88 default_unexpected_callback(const char *file, int line, const char *format,
89                             va_list args)
90 {
91         fprintf(stderr, "%s:%d: ", file, line);
92         vfprintf(stderr, format, args);
93         fprintf(stderr, "\n");
94         fflush(stderr);
95 }
96
97 static void
98 default_fatal_callback(const char *file, int line, const char *format,
99                        va_list args)
100 {
101         fprintf(stderr, "%s:%d: %s: ", file, line,
102                 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
103                                ISC_MSG_FATALERROR, "fatal error"));
104         vfprintf(stderr, format, args);
105         fprintf(stderr, "\n");
106         fflush(stderr);
107 }