1*eabc0478Schristos /* $NetBSD: error.c,v 1.2 2024/08/18 20:47:14 christos Exp $ */ 2897be3a4Schristos 3897be3a4Schristos /* 4897be3a4Schristos * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC") 5897be3a4Schristos * Copyright (C) 1998-2001 Internet Software Consortium. 6897be3a4Schristos * 7897be3a4Schristos * Permission to use, copy, modify, and/or distribute this software for any 8897be3a4Schristos * purpose with or without fee is hereby granted, provided that the above 9897be3a4Schristos * copyright notice and this permission notice appear in all copies. 10897be3a4Schristos * 11897be3a4Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12897be3a4Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13897be3a4Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14897be3a4Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15897be3a4Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16897be3a4Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17897be3a4Schristos * PERFORMANCE OF THIS SOFTWARE. 18897be3a4Schristos */ 19897be3a4Schristos 20897be3a4Schristos /* Id: error.c,v 1.21 2007/06/19 23:47:17 tbox Exp */ 21897be3a4Schristos 22897be3a4Schristos /*! \file */ 23897be3a4Schristos 24897be3a4Schristos #include <config.h> 25897be3a4Schristos 26897be3a4Schristos #include <stdio.h> 27897be3a4Schristos #include <stdlib.h> 28897be3a4Schristos 29897be3a4Schristos #include <isc/error.h> 30897be3a4Schristos #include <isc/msgs.h> 31897be3a4Schristos 32897be3a4Schristos /*% Default unexpected callback. */ 33897be3a4Schristos static void 34897be3a4Schristos default_unexpected_callback(const char *, int, const char *, va_list) 35897be3a4Schristos ISC_FORMAT_PRINTF(3, 0); 36897be3a4Schristos 37897be3a4Schristos /*% Default fatal callback. */ 38897be3a4Schristos static void 39897be3a4Schristos default_fatal_callback(const char *, int, const char *, va_list) 40897be3a4Schristos ISC_FORMAT_PRINTF(3, 0); 41897be3a4Schristos 42897be3a4Schristos /*% unexpected_callback */ 43897be3a4Schristos static isc_errorcallback_t unexpected_callback = default_unexpected_callback; 44897be3a4Schristos static isc_errorcallback_t fatal_callback = default_fatal_callback; 45897be3a4Schristos 46897be3a4Schristos void 47897be3a4Schristos isc_error_setunexpected(isc_errorcallback_t cb) { 48897be3a4Schristos if (cb == NULL) 49897be3a4Schristos unexpected_callback = default_unexpected_callback; 50897be3a4Schristos else 51897be3a4Schristos unexpected_callback = cb; 52897be3a4Schristos } 53897be3a4Schristos 54897be3a4Schristos void 55897be3a4Schristos isc_error_setfatal(isc_errorcallback_t cb) { 56897be3a4Schristos if (cb == NULL) 57897be3a4Schristos fatal_callback = default_fatal_callback; 58897be3a4Schristos else 59897be3a4Schristos fatal_callback = cb; 60897be3a4Schristos } 61897be3a4Schristos 62897be3a4Schristos void 63897be3a4Schristos isc_error_unexpected(const char *file, int line, const char *format, ...) { 64897be3a4Schristos va_list args; 65897be3a4Schristos 66897be3a4Schristos va_start(args, format); 67897be3a4Schristos (unexpected_callback)(file, line, format, args); 68897be3a4Schristos va_end(args); 69897be3a4Schristos } 70897be3a4Schristos 71897be3a4Schristos void 72897be3a4Schristos isc_error_fatal(const char *file, int line, const char *format, ...) { 73897be3a4Schristos va_list args; 74897be3a4Schristos 75897be3a4Schristos va_start(args, format); 76897be3a4Schristos (fatal_callback)(file, line, format, args); 77897be3a4Schristos va_end(args); 78897be3a4Schristos abort(); 79897be3a4Schristos } 80897be3a4Schristos 81897be3a4Schristos void 82897be3a4Schristos isc_error_runtimecheck(const char *file, int line, const char *expression) { 83897be3a4Schristos isc_error_fatal(file, line, "RUNTIME_CHECK(%s) %s", expression, 84897be3a4Schristos isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL, 85897be3a4Schristos ISC_MSG_FAILED, "failed")); 86897be3a4Schristos } 87897be3a4Schristos 88897be3a4Schristos static void 89897be3a4Schristos default_unexpected_callback(const char *file, int line, const char *format, 90897be3a4Schristos va_list args) 91897be3a4Schristos { 92897be3a4Schristos fprintf(stderr, "%s:%d: ", file, line); 93897be3a4Schristos vfprintf(stderr, format, args); 94897be3a4Schristos fprintf(stderr, "\n"); 95897be3a4Schristos fflush(stderr); 96897be3a4Schristos } 97897be3a4Schristos 98897be3a4Schristos static void 99897be3a4Schristos default_fatal_callback(const char *file, int line, const char *format, 100897be3a4Schristos va_list args) 101897be3a4Schristos { 102897be3a4Schristos fprintf(stderr, "%s:%d: %s: ", file, line, 103897be3a4Schristos isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL, 104897be3a4Schristos ISC_MSG_FATALERROR, "fatal error")); 105897be3a4Schristos vfprintf(stderr, format, args); 106897be3a4Schristos fprintf(stderr, "\n"); 107897be3a4Schristos fflush(stderr); 108897be3a4Schristos } 109