result.c revision 1255d388f034dc556d235a002527101781dbeb29
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff/*
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox User * Copyright (C) 1998-2001 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * copyright notice and this permission notice appear in all copies.
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff */
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
26696386938e70e93d7dde509d7bc45ead5870b7Mark Andrews/* $Id: result.c,v 1.57 2001/10/22 07:09:21 marka Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <stdlib.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <isc/lib.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <isc/msgs.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <isc/mutex.h>
db30f4bdcb66afb7eb1ab0c6882cc70be9a53d79Mark Andrews#include <isc/once.h>
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff#include <isc/resultclass.h>
ce24330566b66a5ca8522fa948fb36b94a4d6981Mark Andrews#include <isc/util.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
ce24330566b66a5ca8522fa948fb36b94a4d6981Mark Andrewstypedef struct resulttable {
6028d1ce0380d0ba7f6c6ecd1ad20b31ddd1becbDavid Lawrence unsigned int base;
364a82f7c25b62967678027043425201a5e5171aBob Halley unsigned int last;
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff const char ** text;
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence isc_msgcat_t * msgcat;
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence int set;
7d823f705d9d3a8cb4d43fcf11249515e2845364Andreas Gustafsson ISC_LINK(struct resulttable) link;
531eafa3026663020f4a2ac5587cce44341e3442Andreas Gustafsson} resulttable;
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
5bd76af084edfdcd1cb4db9453ac781d32dde6f7Mark Andrewsstatic const char *text[ISC_R_NRESULTS] = {
f9df80f4348ef68043903efa08299480324f4823Michael Graff "success", /* 0 */
f9df80f4348ef68043903efa08299480324f4823Michael Graff "out of memory", /* 1 */
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence "timed out", /* 2 */
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington "no available threads", /* 3 */
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence "address not available", /* 4 */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "address in use", /* 5 */
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington "permission denied", /* 6 */
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff "no pending connections", /* 7 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "network unreachable", /* 8 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "host unreachable", /* 9 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "network down", /* 10 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "host down", /* 11 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "connection refused", /* 12 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "not enough free resources", /* 13 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "end of file", /* 14 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "socket already bound", /* 15 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "reload", /* 16 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "lock busy", /* 17 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "already exists", /* 18 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "ran out of space", /* 19 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "operation canceled", /* 20 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "socket is not bound", /* 21 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "shutting down", /* 22 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "not found", /* 23 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "unexpected end of input", /* 24 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "failure", /* 25 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "I/O error", /* 26 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "not implemented", /* 27 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "unbalanced parentheses", /* 28 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "no more", /* 29 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "invalid file", /* 30 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "bad base64 encoding", /* 31 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "unexpected token", /* 32 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "quota reached", /* 33 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "unexpected error", /* 34 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "already running", /* 35 */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews "ignore", /* 36 */
75ec9bc9c7b4f2485647414330122e7b8e188097Andreas Gustafsson "address mask not contiguous", /* 37 */
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halley "file not found", /* 38 */
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff "file already exists", /* 39 */
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff "socket is not connected", /* 40 */
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halley "out of range", /* 41 */
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halley "out of entropy", /* 42 */
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halley "invalid use of multicast address", /* 43 */
ac77fece9a62537a9e0e5852498ebeda7b2978c3Bob Halley "not a file", /* 44 */
f9df80f4348ef68043903efa08299480324f4823Michael Graff "not a directory", /* 45 */
f9df80f4348ef68043903efa08299480324f4823Michael Graff "queue is full", /* 46 */
f9df80f4348ef68043903efa08299480324f4823Michael Graff "address family mismatch", /* 47 */
f9df80f4348ef68043903efa08299480324f4823Michael Graff "address family not supported", /* 48 */
f9df80f4348ef68043903efa08299480324f4823Michael Graff "bad hex encoding", /* 49 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "too many open files", /* 50 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "not blocking", /* 51 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "unbalanced quotes", /* 52 */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington "operation in progress", /* 53 */
1ed4ba5a1fcb6aecd1c92fdcc75c6b4bbb7cc60fMichael Sawyer "connection reset", /* 54 */
1ed4ba5a1fcb6aecd1c92fdcc75c6b4bbb7cc60fMichael Sawyer "soft quota reached" /* 55 */
f9df80f4348ef68043903efa08299480324f4823Michael Graff};
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews#define ISC_RESULT_RESULTSET 2
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define ISC_RESULT_UNAVAILABLESET 3
f9df80f4348ef68043903efa08299480324f4823Michael Graff
f9df80f4348ef68043903efa08299480324f4823Michael Graffstatic isc_once_t once = ISC_ONCE_INIT;
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graffstatic ISC_LIST(resulttable) tables;
f9df80f4348ef68043903efa08299480324f4823Michael Graffstatic isc_mutex_t lock;
e223094b2248afa2697c531f75e6f84855638becMichael Graff
b02262cbcd550c63f85df76edc6fff556ea5e95dMichael Graffstatic isc_result_t
be066f0629a12e11bc17f27671036b3f451bd5eaBrian Wellingtonregister_table(unsigned int base, unsigned int nresults, const char **text,
b02262cbcd550c63f85df76edc6fff556ea5e95dMichael Graff isc_msgcat_t *msgcat, int set)
b02262cbcd550c63f85df76edc6fff556ea5e95dMichael Graff{
b02262cbcd550c63f85df76edc6fff556ea5e95dMichael Graff resulttable *table;
f9df80f4348ef68043903efa08299480324f4823Michael Graff
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer REQUIRE(text != NULL);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence /*
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer * We use malloc() here because we we want to be able to use
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer * isc_result_totext() even if there is no memory context.
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer */
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer table = malloc(sizeof *table);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer if (table == NULL)
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer return (ISC_R_NOMEMORY);
58c40ca8bda08458804d7f15cf97942dea2a17acMichael Sawyer table->base = base;
58c40ca8bda08458804d7f15cf97942dea2a17acMichael Sawyer table->last = base + nresults;
58c40ca8bda08458804d7f15cf97942dea2a17acMichael Sawyer table->text = text;
58c40ca8bda08458804d7f15cf97942dea2a17acMichael Sawyer table->msgcat = msgcat;
58c40ca8bda08458804d7f15cf97942dea2a17acMichael Sawyer table->set = set;
58c40ca8bda08458804d7f15cf97942dea2a17acMichael Sawyer ISC_LINK_INIT(table, link);
58c40ca8bda08458804d7f15cf97942dea2a17acMichael Sawyer
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence LOCK(&lock);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer ISC_LIST_APPEND(tables, table, link);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer UNLOCK(&lock);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer return (ISC_R_SUCCESS);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer}
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyerstatic void
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyerinitialize_action(void) {
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer isc_result_t result;
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer ISC_LIST_INIT(tables);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer isc_msgcat, ISC_RESULT_RESULTSET);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer if (result != ISC_R_SUCCESS)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer "register_table() %s: %u",
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer ISC_MSG_FAILED, "failed"),
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer result);
da5d1cf1b1aa29ae53a0427be49291b04bd60549Mark Andrews}
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyerstatic void
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyerinitialize(void) {
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer isc_lib_initmsgcat();
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer}
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyerconst char *
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyerisc_result_totext(isc_result_t result) {
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer resulttable *table;
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer const char *text, *default_text;
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer int index;
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer initialize();
c95a89b433e42ecf9108b6c263f405fecc0d8a65Michael Sawyer
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein LOCK(&lock);
f9df80f4348ef68043903efa08299480324f4823Michael Graff
f9df80f4348ef68043903efa08299480324f4823Michael Graff text = NULL;
47b7dfffe5d806c6a5e99ef17f07bcde812c2132Francis Dupont for (table = ISC_LIST_HEAD(tables);
f9df80f4348ef68043903efa08299480324f4823Michael Graff table != NULL;
f9df80f4348ef68043903efa08299480324f4823Michael Graff table = ISC_LIST_NEXT(table, link)) {
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff if (result >= table->base && result <= table->last) {
f9df80f4348ef68043903efa08299480324f4823Michael Graff index = (int)(result - table->base);
f9df80f4348ef68043903efa08299480324f4823Michael Graff default_text = table->text[index];
f9df80f4348ef68043903efa08299480324f4823Michael Graff /*
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff * Note: we use 'index + 1' as the message number
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff * instead of index because isc_msgcat_get() requires
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff * the message number to be > 0.
fccf7905e8a06067d49ec00c53d4d57a38a71e52Michael Graff */
f9df80f4348ef68043903efa08299480324f4823Michael Graff text = isc_msgcat_get(table->msgcat, table->set,
d68838693666ba930ec4143f848c18bff2bfc244Michael Graff index + 1, default_text);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff break;
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff }
f9df80f4348ef68043903efa08299480324f4823Michael Graff }
f9df80f4348ef68043903efa08299480324f4823Michael Graff if (text == NULL)
f9df80f4348ef68043903efa08299480324f4823Michael Graff text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff 1, "(result code text not available)");
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff UNLOCK(&lock);
97e7d389d54a9e3a1ba8313ed140b04afabc7081Michael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff return (text);
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff}
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graffisc_result_t
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graffisc_result_register(unsigned int base, unsigned int nresults,
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff const char **text, isc_msgcat_t *msgcat, int set)
f9df80f4348ef68043903efa08299480324f4823Michael Graff{
f9df80f4348ef68043903efa08299480324f4823Michael Graff initialize();
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff return (register_table(base, nresults, text, msgcat, set));
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff}
9178881e1bf6a4b01db886b355406c8bed61cc2aMichael Graff