result.c revision 1bb3831e13a65afd87078c88e0285d23b1e0bcdf
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley/*
313b0ea9f258edd8530f4454c69e6ba194280162Tinderbox User * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley * purpose with or without fee is hereby granted, provided that the above
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley */
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id: result.c,v 1.72 2012/01/27 01:21:41 marka Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley/*! \file */
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#include <config.h>
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#include <stddef.h>
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#include <stdlib.h>
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt#include <isc/lib.h>
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#include <isc/msgs.h>
78576fe0db75332cd956b037d90dad0c7e09b005Tatuya JINMEI 神明達哉#include <isc/mutex.h>
cfb605168d7403d3c967eec1b761519712e11ce3Mark Andrews#include <isc/once.h>
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#include <isc/resultclass.h>
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley#include <isc/util.h>
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
d8c1e4eccf5bf88a3b4f8dc4a24391b5712bb3a3Mark Andrewstypedef struct resulttable {
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley unsigned int base;
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington unsigned int last;
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence const char ** text;
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley isc_msgcat_t * msgcat;
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt int set;
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley ISC_LINK(struct resulttable) link;
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews} resulttable;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
364a82f7c25b62967678027043425201a5e5171aBob Halleystatic const char *text[ISC_R_NRESULTS] = {
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley "success", /*%< 0 */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt "out of memory", /*%< 1 */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt "timed out", /*%< 2 */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt "no available threads", /*%< 3 */
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt "address not available", /*%< 4 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "address in use", /*%< 5 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "permission denied", /*%< 6 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "no pending connections", /*%< 7 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "network unreachable", /*%< 8 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "host unreachable", /*%< 9 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "network down", /*%< 10 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "host down", /*%< 11 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "connection refused", /*%< 12 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "not enough free resources", /*%< 13 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "end of file", /*%< 14 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "socket already bound", /*%< 15 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "reload", /*%< 16 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "lock busy", /*%< 17 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "already exists", /*%< 18 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "ran out of space", /*%< 19 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "operation canceled", /*%< 20 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "socket is not bound", /*%< 21 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "shutting down", /*%< 22 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "not found", /*%< 23 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "unexpected end of input", /*%< 24 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "failure", /*%< 25 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "I/O error", /*%< 26 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "not implemented", /*%< 27 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "unbalanced parentheses", /*%< 28 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "no more", /*%< 29 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "invalid file", /*%< 30 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "bad base64 encoding", /*%< 31 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "unexpected token", /*%< 32 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "quota reached", /*%< 33 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "unexpected error", /*%< 34 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "already running", /*%< 35 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "ignore", /*%< 36 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "address mask not contiguous", /*%< 37 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "file not found", /*%< 38 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "file already exists", /*%< 39 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "socket is not connected", /*%< 40 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "out of range", /*%< 41 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "out of entropy", /*%< 42 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "invalid use of multicast address", /*%< 43 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "not a file", /*%< 44 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "not a directory", /*%< 45 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "queue is full", /*%< 46 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "address family mismatch", /*%< 47 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "address family not supported", /*%< 48 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "bad hex encoding", /*%< 49 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "too many open files", /*%< 50 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "not blocking", /*%< 51 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "unbalanced quotes", /*%< 52 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "operation in progress", /*%< 53 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "connection reset", /*%< 54 */
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "soft quota reached", /*%< 55 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "not a valid number", /*%< 56 */
64ecc88345766e124e1fb053dbf4e36550471f83Evan Hunt "disabled", /*%< 57 */
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt "max size", /*%< 58 */
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt "invalid address format", /*%< 59 */
435532822dc571a904207d8176e063cc8731eef5Bob Halley "bad base32 encoding", /*%< 60 */
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt "unset", /*%< 61 */
785d1541b6ec7c6939ec8ebf409db367943bab5fAndreas Gustafsson};
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt
9d99e3fda11eb8549a2bc7d5af31b7630ed68162Mark Andrews#define ISC_RESULT_RESULTSET 2
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt#define ISC_RESULT_UNAVAILABLESET 3
435532822dc571a904207d8176e063cc8731eef5Bob Halley
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Huntstatic isc_once_t once = ISC_ONCE_INIT;
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrewsstatic ISC_LIST(resulttable) tables;
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Huntstatic isc_mutex_t lock;
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Huntstatic isc_result_t
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrewsregister_table(unsigned int base, unsigned int nresults, const char **text,
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt isc_msgcat_t *msgcat, int set)
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt{
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt resulttable *table;
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley REQUIRE(text != NULL);
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews /*
aa39170da817cae7b4c6c735cc832e05ec3d2351Mark Andrews * We use malloc() here because we we want to be able to use
cc32d38366fa680fc29f9fb17bd3ebe515835445Mark Andrews * isc_result_totext() even if there is no memory context.
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt */
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley table = malloc(sizeof(*table));
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley if (table == NULL)
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley return (ISC_R_NOMEMORY);
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley table->base = base;
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington table->last = base + nresults - 1;
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews table->text = text;
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley table->msgcat = msgcat;
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley table->set = set;
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley ISC_LINK_INIT(table, link);
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley LOCK(&lock);
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley ISC_LIST_APPEND(tables, table, link);
dcf8825fd74f3f83b1882af4793270083b125ee5David Lawrence
dcf8825fd74f3f83b1882af4793270083b125ee5David Lawrence UNLOCK(&lock);
dcf8825fd74f3f83b1882af4793270083b125ee5David Lawrence
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley return (ISC_R_SUCCESS);
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley}
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halleystatic void
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halleyinitialize_action(void) {
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews isc_result_t result;
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence ISC_LIST_INIT(tables);
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence isc_msgcat, ISC_RESULT_RESULTSET);
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews if (result != ISC_R_SUCCESS)
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley "register_table() %s: %u",
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley ISC_MSG_FAILED, "failed"),
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington result);
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington}
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellingtonstatic void
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellingtoninitialize(void) {
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington isc_lib_initmsgcat();
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington}
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellingtonconst char *
70c91849c3a3d808125d690a46af5fd225a4c291Andreas Gustafssonisc_result_totext(isc_result_t result) {
2d67fbd786284ed570031fa82fc9807b0413aa3fBrian Wellington resulttable *table;
2d67fbd786284ed570031fa82fc9807b0413aa3fBrian Wellington const char *text, *default_text;
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington int index;
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington
9efa0d2ae311157ed1cce3c16129579111b1e3b0Andreas Gustafsson initialize();
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington LOCK(&lock);
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington
0ffaee887ff5674b8c3bb0435ae838f641981706Mark Andrews text = NULL;
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington for (table = ISC_LIST_HEAD(tables);
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington table != NULL;
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington table = ISC_LIST_NEXT(table, link)) {
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington if (result >= table->base && result <= table->last) {
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington index = (int)(result - table->base);
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington default_text = table->text[index];
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington /*
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington * Note: we use 'index + 1' as the message number
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington * instead of index because isc_msgcat_get() requires
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington * the message number to be > 0.
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington */
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington text = isc_msgcat_get(table->msgcat, table->set,
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington index + 1, default_text);
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington break;
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington }
0ffaee887ff5674b8c3bb0435ae838f641981706Mark Andrews }
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington if (text == NULL)
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington 1, "(result code text not available)");
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington UNLOCK(&lock);
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington return (text);
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellington}
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellingtonisc_result_t
64a84169d7eed05486b10be90afea58f4af146f9Brian Wellingtonisc_result_register(unsigned int base, unsigned int nresults,
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley const char **text, isc_msgcat_t *msgcat, int set)
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley{
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley initialize();
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley
e08daafffce8fb74ffca8ecf5772e334299ccf76Bob Halley return (register_table(base, nresults, text, msgcat, set));
eb2c85b103e3fccebddf59619216ba26af98bcd1Bob Halley}
f3f22040a8c4835227e1a3abc54454556d4c47abBrian Wellington