]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/bind9/lib/isc/result.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / bind9 / lib / isc / result.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001, 2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: result.c,v 1.71 2008/09/25 04:02:39 tbox Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <stddef.h>
25 #include <stdlib.h>
26
27 #include <isc/lib.h>
28 #include <isc/msgs.h>
29 #include <isc/mutex.h>
30 #include <isc/once.h>
31 #include <isc/resultclass.h>
32 #include <isc/util.h>
33
34 typedef struct resulttable {
35         unsigned int                            base;
36         unsigned int                            last;
37         const char **                           text;
38         isc_msgcat_t *                          msgcat;
39         int                                     set;
40         ISC_LINK(struct resulttable)            link;
41 } resulttable;
42
43 static const char *text[ISC_R_NRESULTS] = {
44         "success",                              /*%< 0 */
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 */
60         "reload",                               /*%< 16 */
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 */
69         "failure",                              /*%< 25 */
70         "I/O error",                            /*%< 26 */
71         "not implemented",                      /*%< 27 */
72         "unbalanced parentheses",               /*%< 28 */
73         "no more",                              /*%< 29 */
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 */
80         "ignore",                               /*%< 36 */
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 */
104         "bad base32 encoding",                  /*%< 60 */
105 };
106
107 #define ISC_RESULT_RESULTSET                    2
108 #define ISC_RESULT_UNAVAILABLESET               3
109
110 static isc_once_t                               once = ISC_ONCE_INIT;
111 static ISC_LIST(resulttable)                    tables;
112 static isc_mutex_t                              lock;
113
114 static isc_result_t
115 register_table(unsigned int base, unsigned int nresults, const char **text,
116                isc_msgcat_t *msgcat, int set)
117 {
118         resulttable *table;
119
120         REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
121         REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
122         REQUIRE(text != NULL);
123
124         /*
125          * We use malloc() here because we we want to be able to use
126          * isc_result_totext() even if there is no memory context.
127          */
128         table = malloc(sizeof(*table));
129         if (table == NULL)
130                 return (ISC_R_NOMEMORY);
131         table->base = base;
132         table->last = base + nresults - 1;
133         table->text = text;
134         table->msgcat = msgcat;
135         table->set = set;
136         ISC_LINK_INIT(table, link);
137
138         LOCK(&lock);
139
140         ISC_LIST_APPEND(tables, table, link);
141
142         UNLOCK(&lock);
143
144         return (ISC_R_SUCCESS);
145 }
146
147 static void
148 initialize_action(void) {
149         isc_result_t result;
150
151         RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
152         ISC_LIST_INIT(tables);
153
154         result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
155                                 isc_msgcat, ISC_RESULT_RESULTSET);
156         if (result != ISC_R_SUCCESS)
157                 UNEXPECTED_ERROR(__FILE__, __LINE__,
158                                  "register_table() %s: %u",
159                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
160                                                 ISC_MSG_FAILED, "failed"),
161                                  result);
162 }
163
164 static void
165 initialize(void) {
166         isc_lib_initmsgcat();
167         RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
168 }
169
170 const char *
171 isc_result_totext(isc_result_t result) {
172         resulttable *table;
173         const char *text, *default_text;
174         int index;
175
176         initialize();
177
178         LOCK(&lock);
179
180         text = NULL;
181         for (table = ISC_LIST_HEAD(tables);
182              table != NULL;
183              table = ISC_LIST_NEXT(table, link)) {
184                 if (result >= table->base && result <= table->last) {
185                         index = (int)(result - table->base);
186                         default_text = table->text[index];
187                         /*
188                          * Note: we use 'index + 1' as the message number
189                          * instead of index because isc_msgcat_get() requires
190                          * the message number to be > 0.
191                          */
192                         text = isc_msgcat_get(table->msgcat, table->set,
193                                               index + 1, default_text);
194                         break;
195                 }
196         }
197         if (text == NULL)
198                 text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
199                                       1, "(result code text not available)");
200
201         UNLOCK(&lock);
202
203         return (text);
204 }
205
206 isc_result_t
207 isc_result_register(unsigned int base, unsigned int nresults,
208                     const char **text, isc_msgcat_t *msgcat, int set)
209 {
210         initialize();
211
212         return (register_table(base, nresults, text, msgcat, set));
213 }