1/* $NetBSD: result.c,v 1.8 2020/05/25 20:47:20 christos Exp $ */ 2 3/* 4 * Copyright (C) 2004, 2005, 2007, 2008, 2012 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 1998-2001, 2003 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 */ 21 22/*! \file */ 23 24#include <config.h> 25 26#include <stddef.h> 27#include <stdlib.h> 28 29#include <isc/lib.h> 30#include <isc/msgs.h> 31#include <isc/mutex.h> 32#include <isc/once.h> 33#include <isc/resultclass.h> 34#include <isc/util.h> 35 36typedef struct resulttable { 37 unsigned int base; 38 unsigned int last; 39 const char ** text; 40 isc_msgcat_t * msgcat; 41 int set; 42 ISC_LINK(struct resulttable) link; 43} resulttable; 44 45static const char *text[ISC_R_NRESULTS] = { 46 "success", /*%< 0 */ 47 "out of memory", /*%< 1 */ 48 "timed out", /*%< 2 */ 49 "no available threads", /*%< 3 */ 50 "address not available", /*%< 4 */ 51 "address in use", /*%< 5 */ 52 "permission denied", /*%< 6 */ 53 "no pending connections", /*%< 7 */ 54 "network unreachable", /*%< 8 */ 55 "host unreachable", /*%< 9 */ 56 "network down", /*%< 10 */ 57 "host down", /*%< 11 */ 58 "connection refused", /*%< 12 */ 59 "not enough free resources", /*%< 13 */ 60 "end of file", /*%< 14 */ 61 "socket already bound", /*%< 15 */ 62 "reload", /*%< 16 */ 63 "lock busy", /*%< 17 */ 64 "already exists", /*%< 18 */ 65 "ran out of space", /*%< 19 */ 66 "operation canceled", /*%< 20 */ 67 "socket is not bound", /*%< 21 */ 68 "shutting down", /*%< 22 */ 69 "not found", /*%< 23 */ 70 "unexpected end of input", /*%< 24 */ 71 "failure", /*%< 25 */ 72 "I/O error", /*%< 26 */ 73 "not implemented", /*%< 27 */ 74 "unbalanced parentheses", /*%< 28 */ 75 "no more", /*%< 29 */ 76 "invalid file", /*%< 30 */ 77 "bad base64 encoding", /*%< 31 */ 78 "unexpected token", /*%< 32 */ 79 "quota reached", /*%< 33 */ 80 "unexpected error", /*%< 34 */ 81 "already running", /*%< 35 */ 82 "ignore", /*%< 36 */ 83 "address mask not contiguous", /*%< 37 */ 84 "file not found", /*%< 38 */ 85 "file already exists", /*%< 39 */ 86 "socket is not connected", /*%< 40 */ 87 "out of range", /*%< 41 */ 88 "out of entropy", /*%< 42 */ 89 "invalid use of multicast address", /*%< 43 */ 90 "not a file", /*%< 44 */ 91 "not a directory", /*%< 45 */ 92 "queue is full", /*%< 46 */ 93 "address family mismatch", /*%< 47 */ 94 "address family not supported", /*%< 48 */ 95 "bad hex encoding", /*%< 49 */ 96 "too many open files", /*%< 50 */ 97 "not blocking", /*%< 51 */ 98 "unbalanced quotes", /*%< 52 */ 99 "operation in progress", /*%< 53 */ 100 "connection reset", /*%< 54 */ 101 "soft quota reached", /*%< 55 */ 102 "not a valid number", /*%< 56 */ 103 "disabled", /*%< 57 */ 104 "max size", /*%< 58 */ 105 "invalid address format", /*%< 59 */ 106 "bad base32 encoding", /*%< 60 */ 107 "unset", /*%< 61 */ 108}; 109 110#define ISC_RESULT_RESULTSET 2 111#define ISC_RESULT_UNAVAILABLESET 3 112 113static isc_once_t once = ISC_ONCE_INIT; 114static ISC_LIST(resulttable) tables; 115static isc_mutex_t lock; 116 117static isc_result_t 118register_table(unsigned int base, unsigned int nresults, const char **txt, 119 isc_msgcat_t *msgcat, int set) 120{ 121 resulttable *table; 122 123 REQUIRE(base % ISC_RESULTCLASS_SIZE == 0); 124 REQUIRE(nresults <= ISC_RESULTCLASS_SIZE); 125 REQUIRE(txt != NULL); 126 127 /* 128 * We use malloc() here because we we want to be able to use 129 * isc_result_totext() even if there is no memory context. 130 */ 131 table = malloc(sizeof(*table)); 132 if (table == NULL) 133 return (ISC_R_NOMEMORY); 134 table->base = base; 135 table->last = base + nresults - 1; 136 table->text = txt; 137 table->msgcat = msgcat; 138 table->set = set; 139 ISC_LINK_INIT(table, link); 140 141 LOCK(&lock); 142 143 ISC_LIST_APPEND(tables, table, link); 144 145 UNLOCK(&lock); 146 147 return (ISC_R_SUCCESS); 148} 149 150static void 151initialize_action(void) { 152 isc_result_t result; 153 154 RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS); 155 ISC_LIST_INIT(tables); 156 157 result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text, 158 isc_msgcat, ISC_RESULT_RESULTSET); 159 if (result != ISC_R_SUCCESS) 160 UNEXPECTED_ERROR(__FILE__, __LINE__, 161 "register_table() %s: %u", 162 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL, 163 ISC_MSG_FAILED, "failed"), 164 result); 165} 166 167static void 168initialize(void) { 169 isc_lib_initmsgcat(); 170 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS); 171} 172 173const char * 174isc_result_totext(isc_result_t result) { 175 resulttable *table; 176 const char *txt, *default_text; 177 int idx; 178 179 initialize(); 180 181 LOCK(&lock); 182 183 txt = NULL; 184 for (table = ISC_LIST_HEAD(tables); 185 table != NULL; 186 table = ISC_LIST_NEXT(table, link)) { 187 if (result >= table->base && result <= table->last) { 188 idx = (int)(result - table->base); 189 default_text = table->text[idx]; 190 /* 191 * Note: we use 'idx + 1' as the message number 192 * instead of idx because isc_msgcat_get() requires 193 * the message number to be > 0. 194 */ 195 txt = isc_msgcat_get(table->msgcat, table->set, 196 idx + 1, default_text); 197 break; 198 } 199 } 200 if (txt == NULL) 201 txt = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET, 202 1, "(result code text not available)"); 203 204 UNLOCK(&lock); 205 206 return (txt); 207} 208 209isc_result_t 210isc_result_register(unsigned int base, unsigned int nresults, 211 const char **txt, isc_msgcat_t *msgcat, int set) 212{ 213 initialize(); 214 215 return (register_table(base, nresults, txt, msgcat, set)); 216} 217