"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.16.7/lib/isc/result.c" (4 Sep 2020, 7814 Bytes) of package /linux/misc/dns/bind9/9.16.7/bind-9.16.7.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "result.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /*! \file */
   13 
   14 #include <stddef.h>
   15 #include <stdlib.h>
   16 
   17 #include <isc/lib.h>
   18 #include <isc/once.h>
   19 #include <isc/resultclass.h>
   20 #include <isc/rwlock.h>
   21 #include <isc/util.h>
   22 
   23 typedef struct resulttable {
   24     unsigned int base;
   25     unsigned int last;
   26     const char **text;
   27     int set;
   28     ISC_LINK(struct resulttable) link;
   29 } resulttable;
   30 
   31 typedef ISC_LIST(resulttable) resulttable_list_t;
   32 
   33 static const char *description[ISC_R_NRESULTS] = {
   34     "success",              /*%< 0 */
   35     "out of memory",            /*%< 1 */
   36     "timed out",                /*%< 2 */
   37     "no available threads",         /*%< 3 */
   38     "address not available",        /*%< 4 */
   39     "address in use",           /*%< 5 */
   40     "permission denied",            /*%< 6 */
   41     "no pending connections",       /*%< 7 */
   42     "network unreachable",          /*%< 8 */
   43     "host unreachable",         /*%< 9 */
   44     "network down",             /*%< 10 */
   45     "host down",                /*%< 11 */
   46     "connection refused",           /*%< 12 */
   47     "not enough free resources",        /*%< 13 */
   48     "end of file",              /*%< 14 */
   49     "socket already bound",         /*%< 15 */
   50     "reload",               /*%< 16 */
   51     "lock busy",                /*%< 17 */
   52     "already exists",           /*%< 18 */
   53     "ran out of space",         /*%< 19 */
   54     "operation canceled",           /*%< 20 */
   55     "socket is not bound",          /*%< 21 */
   56     "shutting down",            /*%< 22 */
   57     "not found",                /*%< 23 */
   58     "unexpected end of input",      /*%< 24 */
   59     "failure",              /*%< 25 */
   60     "I/O error",                /*%< 26 */
   61     "not implemented",          /*%< 27 */
   62     "unbalanced parentheses",       /*%< 28 */
   63     "no more",              /*%< 29 */
   64     "invalid file",             /*%< 30 */
   65     "bad base64 encoding",          /*%< 31 */
   66     "unexpected token",         /*%< 32 */
   67     "quota reached",            /*%< 33 */
   68     "unexpected error",         /*%< 34 */
   69     "already running",          /*%< 35 */
   70     "ignore",               /*%< 36 */
   71     "address mask not contiguous",      /*%< 37 */
   72     "file not found",           /*%< 38 */
   73     "file already exists",          /*%< 39 */
   74     "socket is not connected",      /*%< 40 */
   75     "out of range",             /*%< 41 */
   76     "out of entropy",           /*%< 42 */
   77     "invalid use of multicast address", /*%< 43 */
   78     "not a file",               /*%< 44 */
   79     "not a directory",          /*%< 45 */
   80     "queue is full",            /*%< 46 */
   81     "address family mismatch",      /*%< 47 */
   82     "address family not supported",     /*%< 48 */
   83     "bad hex encoding",         /*%< 49 */
   84     "too many open files",          /*%< 50 */
   85     "not blocking",             /*%< 51 */
   86     "unbalanced quotes",            /*%< 52 */
   87     "operation in progress",        /*%< 53 */
   88     "connection reset",         /*%< 54 */
   89     "soft quota reached",           /*%< 55 */
   90     "not a valid number",           /*%< 56 */
   91     "disabled",             /*%< 57 */
   92     "max size",             /*%< 58 */
   93     "invalid address format",       /*%< 59 */
   94     "bad base32 encoding",          /*%< 60 */
   95     "unset",                /*%< 61 */
   96     "multiple",             /*%< 62 */
   97     "would block",              /*%< 63 */
   98     "complete",             /*%< 64 */
   99     "crypto failure",           /*%< 65 */
  100     "disc quota",               /*%< 66 */
  101     "disc full",                /*%< 67 */
  102     "default",              /*%< 68 */
  103     "IPv4 prefix",              /*%< 69 */
  104 };
  105 
  106 static const char *identifier[ISC_R_NRESULTS] = {
  107     "ISC_R_SUCCESS",
  108     "ISC_R_NOMEMORY",
  109     "ISC_R_TIMEDOUT",
  110     "ISC_R_NOTHREADS",
  111     "ISC_R_ADDRNOTAVAIL",
  112     "ISC_R_ADDRINUSE",
  113     "ISC_R_NOPERM",
  114     "ISC_R_NOCONN",
  115     "ISC_R_NETUNREACH",
  116     "ISC_R_HOSTUNREACH",
  117     "ISC_R_NETDOWN",
  118     "ISC_R_HOSTDOWN",
  119     "ISC_R_CONNREFUSED",
  120     "ISC_R_NORESOURCES",
  121     "ISC_R_EOF",
  122     "ISC_R_BOUND",
  123     "ISC_R_RELOAD",
  124     "ISC_R_LOCKBUSY",
  125     "ISC_R_EXISTS",
  126     "ISC_R_NOSPACE",
  127     "ISC_R_CANCELED",
  128     "ISC_R_NOTBOUND",
  129     "ISC_R_SHUTTINGDOWN",
  130     "ISC_R_NOTFOUND",
  131     "ISC_R_UNEXPECTEDEND",
  132     "ISC_R_FAILURE",
  133     "ISC_R_IOERROR",
  134     "ISC_R_NOTIMPLEMENTED",
  135     "ISC_R_UNBALANCED",
  136     "ISC_R_NOMORE",
  137     "ISC_R_INVALIDFILE",
  138     "ISC_R_BADBASE64",
  139     "ISC_R_UNEXPECTEDTOKEN",
  140     "ISC_R_QUOTA",
  141     "ISC_R_UNEXPECTED",
  142     "ISC_R_ALREADYRUNNING",
  143     "ISC_R_IGNORE",
  144     "ISC_R_MASKNONCONTIG",
  145     "ISC_R_FILENOTFOUND",
  146     "ISC_R_FILEEXISTS",
  147     "ISC_R_NOTCONNECTED",
  148     "ISC_R_RANGE",
  149     "ISC_R_NOENTROPY",
  150     "ISC_R_MULTICAST",
  151     "ISC_R_NOTFILE",
  152     "ISC_R_NOTDIRECTORY",
  153     "ISC_R_QUEUEFULL",
  154     "ISC_R_FAMILYMISMATCH",
  155     "ISC_R_FAMILYNOSUPPORT",
  156     "ISC_R_BADHEX",
  157     "ISC_R_TOOMANYOPENFILES",
  158     "ISC_R_NOTBLOCKING",
  159     "ISC_R_UNBALANCEDQUOTES",
  160     "ISC_R_INPROGRESS",
  161     "ISC_R_CONNECTIONRESET",
  162     "ISC_R_SOFTQUOTA",
  163     "ISC_R_BADNUMBER",
  164     "ISC_R_DISABLED",
  165     "ISC_R_MAXSIZE",
  166     "ISC_R_BADADDRESSFORM",
  167     "ISC_R_BADBASE32",
  168     "ISC_R_UNSET",
  169     "ISC_R_MULTIPLE",
  170     "ISC_R_WOULDBLOCK",
  171     "ISC_R_COMPLETE",
  172     "ISC_R_CRYPTOFAILURE",
  173     "ISC_R_DISCQUOTA",
  174     "ISC_R_DISCFULL",
  175     "ISC_R_DEFAULT",
  176     "ISC_R_IPV4PREFIX",
  177 };
  178 
  179 #define ISC_RESULT_RESULTSET      2
  180 #define ISC_RESULT_UNAVAILABLESET 3
  181 
  182 static isc_once_t once = ISC_ONCE_INIT;
  183 static resulttable_list_t description_tables;
  184 static resulttable_list_t identifier_tables;
  185 static isc_rwlock_t lock;
  186 
  187 static isc_result_t
  188 register_table(resulttable_list_t *tables, unsigned int base,
  189            unsigned int nresults, const char **text, int set) {
  190     resulttable *table;
  191 
  192     REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
  193     REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
  194     REQUIRE(text != NULL);
  195 
  196     /*
  197      * We use malloc() here because we we want to be able to use
  198      * isc_result_totext() even if there is no memory context.
  199      */
  200     table = malloc(sizeof(*table));
  201     if (table == NULL) {
  202         return (ISC_R_NOMEMORY);
  203     }
  204     table->base = base;
  205     table->last = base + nresults - 1;
  206     table->text = text;
  207     table->set = set;
  208     ISC_LINK_INIT(table, link);
  209 
  210     RWLOCK(&lock, isc_rwlocktype_write);
  211 
  212     ISC_LIST_APPEND(*tables, table, link);
  213 
  214     RWUNLOCK(&lock, isc_rwlocktype_write);
  215 
  216     return (ISC_R_SUCCESS);
  217 }
  218 
  219 static void
  220 initialize_action(void) {
  221     isc_result_t result;
  222 
  223     isc_rwlock_init(&lock, 0, 0);
  224     ISC_LIST_INIT(description_tables);
  225     ISC_LIST_INIT(identifier_tables);
  226 
  227     result = register_table(&description_tables, ISC_RESULTCLASS_ISC,
  228                 ISC_R_NRESULTS, description,
  229                 ISC_RESULT_RESULTSET);
  230     if (result != ISC_R_SUCCESS) {
  231         UNEXPECTED_ERROR(__FILE__, __LINE__,
  232                  "register_table() failed: %u", result);
  233     }
  234 
  235     result = register_table(&identifier_tables, ISC_RESULTCLASS_ISC,
  236                 ISC_R_NRESULTS, identifier,
  237                 ISC_RESULT_RESULTSET);
  238     if (result != ISC_R_SUCCESS) {
  239         UNEXPECTED_ERROR(__FILE__, __LINE__,
  240                  "register_table() failed: %u", result);
  241     }
  242 }
  243 
  244 static void
  245 initialize(void) {
  246     RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
  247 }
  248 
  249 static const char *
  250 isc_result_tomany_helper(resulttable_list_t *tables, isc_result_t result) {
  251     resulttable *table;
  252     const char *text;
  253     int index;
  254 
  255     initialize();
  256 
  257     RWLOCK(&lock, isc_rwlocktype_read);
  258 
  259     text = NULL;
  260     for (table = ISC_LIST_HEAD(*tables); table != NULL;
  261          table = ISC_LIST_NEXT(table, link))
  262     {
  263         if (result >= table->base && result <= table->last) {
  264             index = (int)(result - table->base);
  265             text = table->text[index];
  266             break;
  267         }
  268     }
  269     if (text == NULL) {
  270         text = "(result code text not available)";
  271     }
  272 
  273     RWUNLOCK(&lock, isc_rwlocktype_read);
  274 
  275     return (text);
  276 }
  277 
  278 const char *
  279 isc_result_totext(isc_result_t result) {
  280     return (isc_result_tomany_helper(&description_tables, result));
  281 }
  282 
  283 const char *
  284 isc_result_toid(isc_result_t result) {
  285     return (isc_result_tomany_helper(&identifier_tables, result));
  286 }
  287 
  288 isc_result_t
  289 isc_result_register(unsigned int base, unsigned int nresults, const char **text,
  290             int set) {
  291     initialize();
  292 
  293     return (register_table(&description_tables, base, nresults, text, set));
  294 }
  295 
  296 isc_result_t
  297 isc_result_registerids(unsigned int base, unsigned int nresults,
  298                const char **ids, int set) {
  299     initialize();
  300 
  301     return (register_table(&identifier_tables, base, nresults, ids, set));
  302 }