2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.
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.
18 /* $Id: result.c,v 1.56.2.2.8.9 2005/06/09 23:54:30 marka Exp $ */
27 #include <isc/mutex.h>
29 #include <isc/resultclass.h>
32 typedef struct resulttable {
36 isc_msgcat_t * msgcat;
38 ISC_LINK(struct resulttable) link;
41 static const char *text[ISC_R_NRESULTS] = {
43 "out of memory", /* 1 */
45 "no available threads", /* 3 */
46 "address not available", /* 4 */
47 "address in use", /* 5 */
48 "permission denied", /* 6 */
49 "no pending connections", /* 7 */
50 "network unreachable", /* 8 */
51 "host unreachable", /* 9 */
52 "network down", /* 10 */
54 "connection refused", /* 12 */
55 "not enough free resources", /* 13 */
56 "end of file", /* 14 */
57 "socket already bound", /* 15 */
60 "already exists", /* 18 */
61 "ran out of space", /* 19 */
62 "operation canceled", /* 20 */
63 "socket is not bound", /* 21 */
64 "shutting down", /* 22 */
66 "unexpected end of input", /* 24 */
69 "not implemented", /* 27 */
70 "unbalanced parentheses", /* 28 */
72 "invalid file", /* 30 */
73 "bad base64 encoding", /* 31 */
74 "unexpected token", /* 32 */
75 "quota reached", /* 33 */
76 "unexpected error", /* 34 */
77 "already running", /* 35 */
79 "address mask not contiguous", /* 37 */
80 "file not found", /* 38 */
81 "file already exists", /* 39 */
82 "socket is not connected", /* 40 */
83 "out of range", /* 41 */
84 "out of entropy", /* 42 */
85 "invalid use of multicast address", /* 43 */
86 "not a file", /* 44 */
87 "not a directory", /* 45 */
88 "queue is full", /* 46 */
89 "address family mismatch", /* 47 */
90 "address family not supported", /* 48 */
91 "bad hex encoding", /* 49 */
92 "too many open files", /* 50 */
93 "not blocking", /* 51 */
94 "unbalanced quotes", /* 52 */
95 "operation in progress", /* 53 */
96 "connection reset", /* 54 */
97 "soft quota reached", /* 55 */
98 "not a valid number", /* 56 */
101 "invalid address format" /* 59 */
104 #define ISC_RESULT_RESULTSET 2
105 #define ISC_RESULT_UNAVAILABLESET 3
107 static isc_once_t once = ISC_ONCE_INIT;
108 static ISC_LIST(resulttable) tables;
109 static isc_mutex_t lock;
112 register_table(unsigned int base, unsigned int nresults, const char **text,
113 isc_msgcat_t *msgcat, int set)
117 REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
118 REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
119 REQUIRE(text != NULL);
122 * We use malloc() here because we we want to be able to use
123 * isc_result_totext() even if there is no memory context.
125 table = malloc(sizeof(*table));
127 return (ISC_R_NOMEMORY);
129 table->last = base + nresults - 1;
131 table->msgcat = msgcat;
133 ISC_LINK_INIT(table, link);
137 ISC_LIST_APPEND(tables, table, link);
141 return (ISC_R_SUCCESS);
145 initialize_action(void) {
148 RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
149 ISC_LIST_INIT(tables);
151 result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
152 isc_msgcat, ISC_RESULT_RESULTSET);
153 if (result != ISC_R_SUCCESS)
154 UNEXPECTED_ERROR(__FILE__, __LINE__,
155 "register_table() %s: %u",
156 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
157 ISC_MSG_FAILED, "failed"),
163 isc_lib_initmsgcat();
164 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
168 isc_result_totext(isc_result_t result) {
170 const char *text, *default_text;
178 for (table = ISC_LIST_HEAD(tables);
180 table = ISC_LIST_NEXT(table, link)) {
181 if (result >= table->base && result <= table->last) {
182 index = (int)(result - table->base);
183 default_text = table->text[index];
185 * Note: we use 'index + 1' as the message number
186 * instead of index because isc_msgcat_get() requires
187 * the message number to be > 0.
189 text = isc_msgcat_get(table->msgcat, table->set,
190 index + 1, default_text);
195 text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
196 1, "(result code text not available)");
204 isc_result_register(unsigned int base, unsigned int nresults,
205 const char **text, isc_msgcat_t *msgcat, int set)
209 return (register_table(base, nresults, text, msgcat, set));