result.c revision 242bba8991b030b7764f0bdca3922d75c34ea51e
5785N/A/*
5785N/A * Copyright (C) 1998-2001 Internet Software Consortium.
5785N/A *
5785N/A * Permission to use, copy, modify, and distribute this software for any
5785N/A * purpose with or without fee is hereby granted, provided that the above
5785N/A * copyright notice and this permission notice appear in all copies.
5785N/A *
5785N/A * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
5785N/A * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
5785N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
5785N/A * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
5785N/A * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
5785N/A * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
5785N/A * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
5785N/A * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5785N/A */
5785N/A
5785N/A/* $Id: result.c,v 1.59 2001/11/30 01:02:14 gson Exp $ */
5785N/A
5785N/A#include <config.h>
5785N/A
5785N/A#include <stdlib.h>
5785N/A
5785N/A#include <isc/lib.h>
5785N/A#include <isc/msgs.h>
5785N/A#include <isc/mutex.h>
5785N/A#include <isc/once.h>
5785N/A#include <isc/resultclass.h>
5785N/A#include <isc/util.h>
5785N/A
5785N/Atypedef struct resulttable {
5785N/A unsigned int base;
5785N/A unsigned int last;
5785N/A const char ** text;
5785N/A isc_msgcat_t * msgcat;
5785N/A int set;
5785N/A ISC_LINK(struct resulttable) link;
5785N/A} resulttable;
5785N/A
5785N/Astatic const char *text[ISC_R_NRESULTS] = {
5785N/A "success", /* 0 */
5785N/A "out of memory", /* 1 */
5785N/A "timed out", /* 2 */
5785N/A "no available threads", /* 3 */
5785N/A "address not available", /* 4 */
5785N/A "address in use", /* 5 */
5785N/A "permission denied", /* 6 */
5785N/A "no pending connections", /* 7 */
5785N/A "network unreachable", /* 8 */
5785N/A "host unreachable", /* 9 */
5785N/A "network down", /* 10 */
5785N/A "host down", /* 11 */
5785N/A "connection refused", /* 12 */
5785N/A "not enough free resources", /* 13 */
5785N/A "end of file", /* 14 */
5785N/A "socket already bound", /* 15 */
5785N/A "reload", /* 16 */
5785N/A "lock busy", /* 17 */
5785N/A "already exists", /* 18 */
5785N/A "ran out of space", /* 19 */
5785N/A "operation canceled", /* 20 */
5785N/A "socket is not bound", /* 21 */
5785N/A "shutting down", /* 22 */
5785N/A "not found", /* 23 */
5785N/A "unexpected end of input", /* 24 */
5785N/A "failure", /* 25 */
5785N/A "I/O error", /* 26 */
5785N/A "not implemented", /* 27 */
5785N/A "unbalanced parentheses", /* 28 */
5785N/A "no more", /* 29 */
5785N/A "invalid file", /* 30 */
5785N/A "bad base64 encoding", /* 31 */
5785N/A "unexpected token", /* 32 */
5785N/A "quota reached", /* 33 */
5785N/A "unexpected error", /* 34 */
5785N/A "already running", /* 35 */
5785N/A "ignore", /* 36 */
5785N/A "address mask not contiguous", /* 37 */
5785N/A "file not found", /* 38 */
5785N/A "file already exists", /* 39 */
5785N/A "socket is not connected", /* 40 */
5785N/A "out of range", /* 41 */
5785N/A "out of entropy", /* 42 */
5785N/A "invalid use of multicast address", /* 43 */
5785N/A "not a file", /* 44 */
5785N/A "not a directory", /* 45 */
5785N/A "queue is full", /* 46 */
5785N/A "address family mismatch", /* 47 */
5785N/A "address family not supported", /* 48 */
5785N/A "bad hex encoding", /* 49 */
5785N/A "too many open files", /* 50 */
5785N/A "not blocking", /* 51 */
5785N/A "unbalanced quotes", /* 52 */
5785N/A "operation in progress", /* 53 */
5785N/A "connection reset", /* 54 */
5785N/A "soft quota reached", /* 55 */
5785N/A "not a valid number" /* 56 */
5785N/A};
5785N/A
5785N/A#define ISC_RESULT_RESULTSET 2
5785N/A#define ISC_RESULT_UNAVAILABLESET 3
5785N/A
5785N/Astatic isc_once_t once = ISC_ONCE_INIT;
5785N/Astatic ISC_LIST(resulttable) tables;
5785N/Astatic isc_mutex_t lock;
5785N/A
5785N/Astatic isc_result_t
5785N/Aregister_table(unsigned int base, unsigned int nresults, const char **text,
5785N/A isc_msgcat_t *msgcat, int set)
5785N/A{
5785N/A resulttable *table;
5785N/A
5785N/A REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
5785N/A REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
5785N/A REQUIRE(text != NULL);
5785N/A
5785N/A /*
5785N/A * We use malloc() here because we we want to be able to use
5785N/A * isc_result_totext() even if there is no memory context.
5785N/A */
5785N/A table = malloc(sizeof(*table));
5785N/A if (table == NULL)
5785N/A return (ISC_R_NOMEMORY);
5785N/A table->base = base;
5785N/A table->last = base + nresults;
5785N/A table->text = text;
5785N/A table->msgcat = msgcat;
5785N/A table->set = set;
5785N/A ISC_LINK_INIT(table, link);
5785N/A
5785N/A LOCK(&lock);
5785N/A
5785N/A ISC_LIST_APPEND(tables, table, link);
5785N/A
5785N/A UNLOCK(&lock);
5785N/A
5785N/A return (ISC_R_SUCCESS);
5785N/A}
5785N/A
5785N/Astatic void
5785N/Ainitialize_action(void) {
5785N/A isc_result_t result;
5785N/A
5785N/A RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
5785N/A ISC_LIST_INIT(tables);
5785N/A
5785N/A result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
5785N/A isc_msgcat, ISC_RESULT_RESULTSET);
5785N/A if (result != ISC_R_SUCCESS)
5785N/A UNEXPECTED_ERROR(__FILE__, __LINE__,
5785N/A "register_table() %s: %u",
5785N/A isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
5785N/A ISC_MSG_FAILED, "failed"),
5785N/A result);
5785N/A}
static void
initialize(void) {
isc_lib_initmsgcat();
RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
}
const char *
isc_result_totext(isc_result_t result) {
resulttable *table;
const char *text, *default_text;
int index;
initialize();
LOCK(&lock);
text = NULL;
for (table = ISC_LIST_HEAD(tables);
table != NULL;
table = ISC_LIST_NEXT(table, link)) {
if (result >= table->base && result <= table->last) {
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);
break;
}
}
if (text == NULL)
text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
1, "(result code text not available)");
UNLOCK(&lock);
return (text);
}
isc_result_t
isc_result_register(unsigned int base, unsigned int nresults,
const char **text, isc_msgcat_t *msgcat, int set)
{
initialize();
return (register_table(base, nresults, text, msgcat, set));
}