result.c 5.8 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1998-2001, 2003  Internet Software Consortium.
4
 *
Bob Halley's avatar
Bob Halley committed
5 6 7
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
Bob Halley's avatar
Bob Halley committed
16
 */
Bob Halley's avatar
Bob Halley committed
17

18
/* $Id: result.c,v 1.67 2005/06/08 02:06:59 marka Exp $ */
19 20

/*! \file */
David Lawrence's avatar
David Lawrence committed
21

Bob Halley's avatar
Bob Halley committed
22 23
#include <config.h>

24
#include <stddef.h>
Bob Halley's avatar
Bob Halley committed
25 26
#include <stdlib.h>

27
#include <isc/lib.h>
28
#include <isc/msgs.h>
Bob Halley's avatar
Bob Halley committed
29 30
#include <isc/mutex.h>
#include <isc/once.h>
31
#include <isc/resultclass.h>
Michael Graff's avatar
Michael Graff committed
32
#include <isc/util.h>
Bob Halley's avatar
Bob Halley committed
33

Bob Halley's avatar
Bob Halley committed
34 35 36
typedef struct resulttable {
	unsigned int				base;
	unsigned int				last;
David Lawrence's avatar
David Lawrence committed
37
	const char **				text;
Bob Halley's avatar
Bob Halley committed
38 39
	isc_msgcat_t *				msgcat;
	int					set;
Bob Halley's avatar
Bob Halley committed
40 41 42
	ISC_LINK(struct resulttable)		link;
} resulttable;

David Lawrence's avatar
David Lawrence committed
43
static const char *text[ISC_R_NRESULTS] = {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	"success",				/*%< 0 */
	"out of memory",			/*%< 1 */
	"timed out",				/*%< 2 */
	"no available threads",			/*%< 3 */
	"address not available",		/*%< 4 */
	"address in use",			/*%< 5 */
	"permission denied",			/*%< 6 */
	"no pending connections",		/*%< 7 */
	"network unreachable",			/*%< 8 */
	"host unreachable",			/*%< 9 */
	"network down",				/*%< 10 */
	"host down",				/*%< 11 */
	"connection refused",			/*%< 12 */
	"not enough free resources",		/*%< 13 */
	"end of file",				/*%< 14 */
	"socket already bound",			/*%< 15 */
	"reload",				/*%< 16 */
	"lock busy",				/*%< 17 */
	"already exists",			/*%< 18 */
	"ran out of space",			/*%< 19 */
	"operation canceled",			/*%< 20 */
	"socket is not bound",			/*%< 21 */
	"shutting down",			/*%< 22 */
	"not found",				/*%< 23 */
	"unexpected end of input",		/*%< 24 */
	"failure",				/*%< 25 */
	"I/O error",				/*%< 26 */
	"not implemented",			/*%< 27 */
	"unbalanced parentheses",		/*%< 28 */
	"no more",				/*%< 29 */
	"invalid file",				/*%< 30 */
	"bad base64 encoding",			/*%< 31 */
	"unexpected token",			/*%< 32 */
	"quota reached",			/*%< 33 */
	"unexpected error",			/*%< 34 */
	"already running",			/*%< 35 */
	"ignore",				/*%< 36 */
	"address mask not contiguous",		/*%< 37 */
	"file not found",			/*%< 38 */
	"file already exists",			/*%< 39 */
	"socket is not connected",		/*%< 40 */
	"out of range",				/*%< 41 */
	"out of entropy",			/*%< 42 */
	"invalid use of multicast address",	/*%< 43 */
	"not a file",				/*%< 44 */
	"not a directory",			/*%< 45 */
	"queue is full",			/*%< 46 */
	"address family mismatch",		/*%< 47 */
	"address family not supported",		/*%< 48 */
	"bad hex encoding",			/*%< 49 */
	"too many open files",			/*%< 50 */
	"not blocking",				/*%< 51 */
	"unbalanced quotes",			/*%< 52 */
	"operation in progress",		/*%< 53 */
	"connection reset",			/*%< 54 */
	"soft quota reached",			/*%< 55 */
	"not a valid number",			/*%< 56 */
	"disabled",				/*%< 57 */
	"max size",				/*%< 58 */
	"invalid address format"		/*%< 59 */
Bob Halley's avatar
Bob Halley committed
104 105
};

106 107 108
#define ISC_RESULT_RESULTSET			2
#define ISC_RESULT_UNAVAILABLESET		3

Bob Halley's avatar
Bob Halley committed
109 110 111 112 113
static isc_once_t 				once = ISC_ONCE_INIT;
static ISC_LIST(resulttable)			tables;
static isc_mutex_t				lock;

static isc_result_t
David Lawrence's avatar
David Lawrence committed
114
register_table(unsigned int base, unsigned int nresults, const char **text,
Bob Halley's avatar
Bob Halley committed
115 116
	       isc_msgcat_t *msgcat, int set)
{
Bob Halley's avatar
Bob Halley committed
117 118 119 120 121 122 123 124 125 126
	resulttable *table;

	REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
	REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
	REQUIRE(text != NULL);

	/*
	 * We use malloc() here because we we want to be able to use
	 * isc_result_totext() even if there is no memory context.
	 */
Andreas Gustafsson's avatar
Andreas Gustafsson committed
127
	table = malloc(sizeof(*table));
Bob Halley's avatar
Bob Halley committed
128 129 130
	if (table == NULL)
		return (ISC_R_NOMEMORY);
	table->base = base;
131
	table->last = base + nresults - 1;
Bob Halley's avatar
Bob Halley committed
132
	table->text = text;
Bob Halley's avatar
Bob Halley committed
133 134
	table->msgcat = msgcat;
	table->set = set;
Bob Halley's avatar
Bob Halley committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	ISC_LINK_INIT(table, link);

	LOCK(&lock);

	ISC_LIST_APPEND(tables, table, link);

	UNLOCK(&lock);

	return (ISC_R_SUCCESS);
}

static void
initialize_action(void) {
	isc_result_t result;

	RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
	ISC_LIST_INIT(tables);

Bob Halley's avatar
Bob Halley committed
153
	result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
154
				isc_msgcat, ISC_RESULT_RESULTSET);
Bob Halley's avatar
Bob Halley committed
155 156
	if (result != ISC_R_SUCCESS)
		UNEXPECTED_ERROR(__FILE__, __LINE__,
157 158 159 160
				 "register_table() %s: %u",
				 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
						ISC_MSG_FAILED, "failed"),
				 result);
Bob Halley's avatar
Bob Halley committed
161 162 163 164
}

static void
initialize(void) {
Bob Halley's avatar
Bob Halley committed
165
	isc_lib_initmsgcat();
Bob Halley's avatar
Bob Halley committed
166 167 168
	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
}

David Lawrence's avatar
David Lawrence committed
169
const char *
Bob Halley's avatar
Bob Halley committed
170
isc_result_totext(isc_result_t result) {
Bob Halley's avatar
Bob Halley committed
171
	resulttable *table;
David Lawrence's avatar
David Lawrence committed
172
	const char *text, *default_text;
Bob Halley's avatar
Bob Halley committed
173
	int index;
Bob Halley's avatar
Bob Halley committed
174 175 176 177 178

	initialize();

	LOCK(&lock);

179
	text = NULL;
Bob Halley's avatar
Bob Halley committed
180 181 182 183
	for (table = ISC_LIST_HEAD(tables);
	     table != NULL;
	     table = ISC_LIST_NEXT(table, link)) {
		if (result >= table->base && result <= table->last) {
Bob Halley's avatar
Bob Halley committed
184 185 186 187 188 189 190 191 192
			index = (int)(result - table->base);
			default_text = table->text[index];
			/*
			 * Note: we use 'index + 1' as the message number
			 * instead of index because isc_msgcat_get() requires
			 * the message number to be > 0.
			 */
			text = isc_msgcat_get(table->msgcat, table->set,
					      index + 1, default_text);
Bob Halley's avatar
Bob Halley committed
193 194 195
			break;
		}
	}
196 197 198
	if (text == NULL)
		text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
				      1, "(result code text not available)");
Bob Halley's avatar
Bob Halley committed
199 200 201 202 203 204 205

	UNLOCK(&lock);

	return (text);
}

isc_result_t
David Lawrence's avatar
David Lawrence committed
206 207
isc_result_register(unsigned int base, unsigned int nresults,
		    const char **text, isc_msgcat_t *msgcat, int set)
Bob Halley's avatar
Bob Halley committed
208
{
Bob Halley's avatar
Bob Halley committed
209 210
	initialize();

Bob Halley's avatar
Bob Halley committed
211
	return (register_table(base, nresults, text, msgcat, set));
Bob Halley's avatar
Bob Halley committed
212
}