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