2 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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.69 2007/06/19 23:47:17 tbox Exp $ */
29 #include <isc/mutex.h>
31 #include <isc/resultclass.h>
34 typedef struct resulttable
{
38 isc_msgcat_t
* msgcat
;
40 ISC_LINK(struct resulttable
) link
;
43 static const char *text
[ISC_R_NRESULTS
] = {
45 "out of memory", /*%< 1 */
46 "timed out", /*%< 2 */
47 "no available threads", /*%< 3 */
48 "address not available", /*%< 4 */
49 "address in use", /*%< 5 */
50 "permission denied", /*%< 6 */
51 "no pending connections", /*%< 7 */
52 "network unreachable", /*%< 8 */
53 "host unreachable", /*%< 9 */
54 "network down", /*%< 10 */
55 "host down", /*%< 11 */
56 "connection refused", /*%< 12 */
57 "not enough free resources", /*%< 13 */
58 "end of file", /*%< 14 */
59 "socket already bound", /*%< 15 */
61 "lock busy", /*%< 17 */
62 "already exists", /*%< 18 */
63 "ran out of space", /*%< 19 */
64 "operation canceled", /*%< 20 */
65 "socket is not bound", /*%< 21 */
66 "shutting down", /*%< 22 */
67 "not found", /*%< 23 */
68 "unexpected end of input", /*%< 24 */
70 "I/O error", /*%< 26 */
71 "not implemented", /*%< 27 */
72 "unbalanced parentheses", /*%< 28 */
74 "invalid file", /*%< 30 */
75 "bad base64 encoding", /*%< 31 */
76 "unexpected token", /*%< 32 */
77 "quota reached", /*%< 33 */
78 "unexpected error", /*%< 34 */
79 "already running", /*%< 35 */
81 "address mask not contiguous", /*%< 37 */
82 "file not found", /*%< 38 */
83 "file already exists", /*%< 39 */
84 "socket is not connected", /*%< 40 */
85 "out of range", /*%< 41 */
86 "out of entropy", /*%< 42 */
87 "invalid use of multicast address", /*%< 43 */
88 "not a file", /*%< 44 */
89 "not a directory", /*%< 45 */
90 "queue is full", /*%< 46 */
91 "address family mismatch", /*%< 47 */
92 "address family not supported", /*%< 48 */
93 "bad hex encoding", /*%< 49 */
94 "too many open files", /*%< 50 */
95 "not blocking", /*%< 51 */
96 "unbalanced quotes", /*%< 52 */
97 "operation in progress", /*%< 53 */
98 "connection reset", /*%< 54 */
99 "soft quota reached", /*%< 55 */
100 "not a valid number", /*%< 56 */
101 "disabled", /*%< 57 */
102 "max size", /*%< 58 */
103 "invalid address format" /*%< 59 */
106 #define ISC_RESULT_RESULTSET 2
107 #define ISC_RESULT_UNAVAILABLESET 3
109 static isc_once_t once
= ISC_ONCE_INIT
;
110 static ISC_LIST(resulttable
) tables
;
111 static isc_mutex_t lock
;
114 register_table(unsigned int base
, unsigned int nresults
, const char **text
,
115 isc_msgcat_t
*msgcat
, int set
)
119 REQUIRE(base
% ISC_RESULTCLASS_SIZE
== 0);
120 REQUIRE(nresults
<= ISC_RESULTCLASS_SIZE
);
121 REQUIRE(text
!= NULL
);
124 * We use malloc() here because we we want to be able to use
125 * isc_result_totext() even if there is no memory context.
127 table
= malloc(sizeof(*table
));
129 return (ISC_R_NOMEMORY
);
131 table
->last
= base
+ nresults
- 1;
133 table
->msgcat
= msgcat
;
135 ISC_LINK_INIT(table
, link
);
139 ISC_LIST_APPEND(tables
, table
, link
);
143 return (ISC_R_SUCCESS
);
147 initialize_action(void) {
150 RUNTIME_CHECK(isc_mutex_init(&lock
) == ISC_R_SUCCESS
);
151 ISC_LIST_INIT(tables
);
153 result
= register_table(ISC_RESULTCLASS_ISC
, ISC_R_NRESULTS
, text
,
154 isc_msgcat
, ISC_RESULT_RESULTSET
);
155 if (result
!= ISC_R_SUCCESS
)
156 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
157 "register_table() %s: %u",
158 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
159 ISC_MSG_FAILED
, "failed"),
165 isc_lib_initmsgcat();
166 RUNTIME_CHECK(isc_once_do(&once
, initialize_action
) == ISC_R_SUCCESS
);
170 isc_result_totext(isc_result_t result
) {
172 const char *text
, *default_text
;
180 for (table
= ISC_LIST_HEAD(tables
);
182 table
= ISC_LIST_NEXT(table
, link
)) {
183 if (result
>= table
->base
&& result
<= table
->last
) {
184 index
= (int)(result
- table
->base
);
185 default_text
= table
->text
[index
];
187 * Note: we use 'index + 1' as the message number
188 * instead of index because isc_msgcat_get() requires
189 * the message number to be > 0.
191 text
= isc_msgcat_get(table
->msgcat
, table
->set
,
192 index
+ 1, default_text
);
197 text
= isc_msgcat_get(isc_msgcat
, ISC_RESULT_UNAVAILABLESET
,
198 1, "(result code text not available)");
206 isc_result_register(unsigned int base
, unsigned int nresults
,
207 const char **text
, isc_msgcat_t
*msgcat
, int set
)
211 return (register_table(base
, nresults
, text
, msgcat
, set
));