1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /* $Id: result.c,v 1.5 2020/09/14 08:40:44 florian Exp $ */
18
19 /*! \file */
20
21 #include <stddef.h>
22 #include <stdlib.h>
23
24 #include <isc/resultclass.h>
25 #include <isc/util.h>
26
27 typedef struct resulttable {
28 unsigned int base;
29 unsigned int last;
30 const char ** text;
31 int set;
32 ISC_LINK(struct resulttable) link;
33 } resulttable;
34
35 static const char *description[ISC_R_NRESULTS] = {
36 "success", /*%< 0 */
37 "out of memory", /*%< 1 */
38 "timed out", /*%< 2 */
39 "no available threads", /*%< 3 */
40 "address not available", /*%< 4 */
41 "address in use", /*%< 5 */
42 "permission denied", /*%< 6 */
43 "no pending connections", /*%< 7 */
44 "network unreachable", /*%< 8 */
45 "host unreachable", /*%< 9 */
46 "network down", /*%< 10 */
47 "host down", /*%< 11 */
48 "connection refused", /*%< 12 */
49 "not enough free resources", /*%< 13 */
50 "end of file", /*%< 14 */
51 "socket already bound", /*%< 15 */
52 "reload", /*%< 16 */
53 "lock busy", /*%< 17 */
54 "already exists", /*%< 18 */
55 "ran out of space", /*%< 19 */
56 "operation canceled", /*%< 20 */
57 "socket is not bound", /*%< 21 */
58 "shutting down", /*%< 22 */
59 "not found", /*%< 23 */
60 "unexpected end of input", /*%< 24 */
61 "failure", /*%< 25 */
62 "I/O error", /*%< 26 */
63 "not implemented", /*%< 27 */
64 "unbalanced parentheses", /*%< 28 */
65 "no more", /*%< 29 */
66 "invalid file", /*%< 30 */
67 "bad base64 encoding", /*%< 31 */
68 "unexpected token", /*%< 32 */
69 "quota reached", /*%< 33 */
70 "unexpected error", /*%< 34 */
71 "already running", /*%< 35 */
72 "ignore", /*%< 36 */
73 "address mask not contiguous", /*%< 37 */
74 "file not found", /*%< 38 */
75 "file already exists", /*%< 39 */
76 "socket is not connected", /*%< 40 */
77 "out of range", /*%< 41 */
78 "out of entropy", /*%< 42 */
79 "invalid use of multicast address", /*%< 43 */
80 "not a file", /*%< 44 */
81 "not a directory", /*%< 45 */
82 "queue is full", /*%< 46 */
83 "address family mismatch", /*%< 47 */
84 "address family not supported", /*%< 48 */
85 "bad hex encoding", /*%< 49 */
86 "too many open files", /*%< 50 */
87 "not blocking", /*%< 51 */
88 "unbalanced quotes", /*%< 52 */
89 "operation in progress", /*%< 53 */
90 "connection reset", /*%< 54 */
91 "soft quota reached", /*%< 55 */
92 "not a valid number", /*%< 56 */
93 "disabled", /*%< 57 */
94 "max size", /*%< 58 */
95 "invalid address format", /*%< 59 */
96 "bad base32 encoding", /*%< 60 */
97 "unset", /*%< 61 */
98 "multiple", /*%< 62 */
99 "would block", /*%< 63 */
100 };
101
102 #define ISC_RESULT_RESULTSET 2
103
104 static int once = 0;
105 static ISC_LIST(resulttable) tables;
106
107 static isc_result_t
register_table(unsigned int base,unsigned int nresults,const char ** text,int set)108 register_table(unsigned int base, unsigned int nresults, const char **text,
109 int set)
110 {
111 resulttable *table;
112
113 REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
114 REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
115 REQUIRE(text != NULL);
116
117 /*
118 * We use malloc() here because we we want to be able to use
119 * isc_result_totext() even if there is no memory context.
120 */
121 table = malloc(sizeof(*table));
122 if (table == NULL)
123 return (ISC_R_NOMEMORY);
124 table->base = base;
125 table->last = base + nresults - 1;
126 table->text = text;
127 table->set = set;
128 ISC_LINK_INIT(table, link);
129
130 ISC_LIST_APPEND(tables, table, link);
131
132 return (ISC_R_SUCCESS);
133 }
134
135 static void
initialize_action(void)136 initialize_action(void) {
137 isc_result_t result;
138
139 ISC_LIST_INIT(tables);
140
141 result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS,
142 description, ISC_RESULT_RESULTSET);
143 if (result != ISC_R_SUCCESS)
144 UNEXPECTED_ERROR(__FILE__, __LINE__,
145 "register_table() %s: %u", "failed",
146 result);
147 }
148
149 static void
initialize(void)150 initialize(void) {
151 if (!once) {
152 once = 1;
153 initialize_action();
154 }
155 }
156
157 const char *
isc_result_totext(isc_result_t result)158 isc_result_totext(isc_result_t result) {
159 resulttable *table;
160 const char *text;
161 int index;
162
163 initialize();
164
165 text = NULL;
166 for (table = ISC_LIST_HEAD(tables);
167 table != NULL;
168 table = ISC_LIST_NEXT(table, link)) {
169 if (result >= table->base && result <= table->last) {
170 index = (int)(result - table->base);
171 text = table->text[index];
172 break;
173 }
174 }
175 if (text == NULL)
176 text = "(result code text not available)";
177
178 return (text);
179 }
180
181 isc_result_t
isc_result_register(unsigned int base,unsigned int nresults,const char ** text,int set)182 isc_result_register(unsigned int base, unsigned int nresults,
183 const char **text, int set)
184 {
185 initialize();
186
187 return (register_table(base, nresults, text, set));
188 }
189