1 /* $NetBSD: result.c,v 1.5 2015/07/08 17:28:59 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2005, 2007, 2008, 2012, 2014, 2015 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 36 typedef 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 45 static const char *description[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 "multiple", /*%< 62 */ 109 }; 110 111 #define ISC_RESULT_RESULTSET 2 112 #define ISC_RESULT_UNAVAILABLESET 3 113 114 static isc_once_t once = ISC_ONCE_INIT; 115 static ISC_LIST(resulttable) tables; 116 static isc_mutex_t lock; 117 118 static isc_result_t 119 register_table(unsigned int base, unsigned int nresults, const char **text, 120 isc_msgcat_t *msgcat, int set) 121 { 122 resulttable *table; 123 124 REQUIRE(base % ISC_RESULTCLASS_SIZE == 0); 125 REQUIRE(nresults <= ISC_RESULTCLASS_SIZE); 126 REQUIRE(text != NULL); 127 128 /* 129 * We use malloc() here because we we want to be able to use 130 * isc_result_totext() even if there is no memory context. 131 */ 132 table = malloc(sizeof(*table)); 133 if (table == NULL) 134 return (ISC_R_NOMEMORY); 135 table->base = base; 136 table->last = base + nresults - 1; 137 table->text = text; 138 table->msgcat = msgcat; 139 table->set = set; 140 ISC_LINK_INIT(table, link); 141 142 LOCK(&lock); 143 144 ISC_LIST_APPEND(tables, table, link); 145 146 UNLOCK(&lock); 147 148 return (ISC_R_SUCCESS); 149 } 150 151 static void 152 initialize_action(void) { 153 isc_result_t result; 154 155 RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS); 156 ISC_LIST_INIT(tables); 157 158 result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, 159 description, isc_msgcat, ISC_RESULT_RESULTSET); 160 if (result != ISC_R_SUCCESS) 161 UNEXPECTED_ERROR(__FILE__, __LINE__, 162 "register_table() %s: %u", 163 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL, 164 ISC_MSG_FAILED, "failed"), 165 result); 166 } 167 168 static void 169 initialize(void) { 170 isc_lib_initmsgcat(); 171 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS); 172 } 173 174 const char * 175 isc_result_totext(isc_result_t result) { 176 resulttable *table; 177 const char *text, *default_text; 178 int index; 179 180 initialize(); 181 182 LOCK(&lock); 183 184 text = NULL; 185 for (table = ISC_LIST_HEAD(tables); 186 table != NULL; 187 table = ISC_LIST_NEXT(table, link)) { 188 if (result >= table->base && result <= table->last) { 189 index = (int)(result - table->base); 190 default_text = table->text[index]; 191 /* 192 * Note: we use 'index + 1' as the message number 193 * instead of index because isc_msgcat_get() requires 194 * the message number to be > 0. 195 */ 196 text = isc_msgcat_get(table->msgcat, table->set, 197 index + 1, default_text); 198 break; 199 } 200 } 201 if (text == NULL) 202 text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET, 203 1, "(result code text not available)"); 204 205 UNLOCK(&lock); 206 207 return (text); 208 } 209 210 isc_result_t 211 isc_result_register(unsigned int base, unsigned int nresults, 212 const char **text, isc_msgcat_t *msgcat, int set) 213 { 214 initialize(); 215 216 return (register_table(base, nresults, text, msgcat, set)); 217 } 218