4 * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
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.
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.
20 /* Id: result.c,v 1.71 2008/09/25 04:02:39 tbox Exp */
31 #include <isc/mutex.h>
33 #include <isc/resultclass.h>
36 typedef struct resulttable
{
40 isc_msgcat_t
* msgcat
;
42 ISC_LINK(struct resulttable
) link
;
45 static const char *text
[ISC_R_NRESULTS
] = {
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 */
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 */
72 "I/O error", /*%< 26 */
73 "not implemented", /*%< 27 */
74 "unbalanced parentheses", /*%< 28 */
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 */
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 */
109 #define ISC_RESULT_RESULTSET 2
110 #define ISC_RESULT_UNAVAILABLESET 3
112 static isc_once_t once
= ISC_ONCE_INIT
;
113 static ISC_LIST(resulttable
) tables
;
114 static isc_mutex_t lock
;
117 register_table(unsigned int base
, unsigned int nresults
, const char **text
,
118 isc_msgcat_t
*msgcat
, int set
)
122 REQUIRE(base
% ISC_RESULTCLASS_SIZE
== 0);
123 REQUIRE(nresults
<= ISC_RESULTCLASS_SIZE
);
124 REQUIRE(text
!= NULL
);
127 * We use malloc() here because we we want to be able to use
128 * isc_result_totext() even if there is no memory context.
130 table
= malloc(sizeof(*table
));
132 return (ISC_R_NOMEMORY
);
134 table
->last
= base
+ nresults
- 1;
136 table
->msgcat
= msgcat
;
138 ISC_LINK_INIT(table
, link
);
142 ISC_LIST_APPEND(tables
, table
, link
);
146 return (ISC_R_SUCCESS
);
150 initialize_action(void) {
153 RUNTIME_CHECK(isc_mutex_init(&lock
) == ISC_R_SUCCESS
);
154 ISC_LIST_INIT(tables
);
156 result
= register_table(ISC_RESULTCLASS_ISC
, ISC_R_NRESULTS
, text
,
157 isc_msgcat
, ISC_RESULT_RESULTSET
);
158 if (result
!= ISC_R_SUCCESS
)
159 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
160 "register_table() %s: %u",
161 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
162 ISC_MSG_FAILED
, "failed"),
168 isc_lib_initmsgcat();
169 RUNTIME_CHECK(isc_once_do(&once
, initialize_action
) == ISC_R_SUCCESS
);
173 isc_result_totext(isc_result_t result
) {
175 const char *text
, *default_text
;
183 for (table
= ISC_LIST_HEAD(tables
);
185 table
= ISC_LIST_NEXT(table
, link
)) {
186 if (result
>= table
->base
&& result
<= table
->last
) {
187 index
= (int)(result
- table
->base
);
188 default_text
= table
->text
[index
];
190 * Note: we use 'index + 1' as the message number
191 * instead of index because isc_msgcat_get() requires
192 * the message number to be > 0.
194 text
= isc_msgcat_get(table
->msgcat
, table
->set
,
195 index
+ 1, default_text
);
200 text
= isc_msgcat_get(isc_msgcat
, ISC_RESULT_UNAVAILABLESET
,
201 1, "(result code text not available)");
209 isc_result_register(unsigned int base
, unsigned int nresults
,
210 const char **text
, isc_msgcat_t
*msgcat
, int set
)
214 return (register_table(base
, nresults
, text
, msgcat
, set
));