lib.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2009, 2013, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1999-2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: lib.c,v 1.19 2009/09/03 00:12:23 each Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <stddef.h>
00025 
00026 #include <isc/hash.h>
00027 #include <isc/mem.h>
00028 #include <isc/msgcat.h>
00029 #include <isc/mutex.h>
00030 #include <isc/once.h>
00031 #include <isc/util.h>
00032 
00033 #include <dns/db.h>
00034 #include <dns/ecdb.h>
00035 #include <dns/lib.h>
00036 #include <dns/result.h>
00037 
00038 #include <dst/dst.h>
00039 
00040 
00041 /***
00042  *** Globals
00043  ***/
00044 
00045 LIBDNS_EXTERNAL_DATA unsigned int                       dns_pps = 0U;
00046 LIBDNS_EXTERNAL_DATA isc_msgcat_t *                     dns_msgcat = NULL;
00047 
00048 
00049 /***
00050  *** Private
00051  ***/
00052 
00053 static isc_once_t               msgcat_once = ISC_ONCE_INIT;
00054 
00055 
00056 /***
00057  *** Functions
00058  ***/
00059 
00060 static void
00061 open_msgcat(void) {
00062         isc_msgcat_open("libdns.cat", &dns_msgcat);
00063 }
00064 
00065 void
00066 dns_lib_initmsgcat(void) {
00067 
00068         /*
00069          * Initialize the DNS library's message catalog, dns_msgcat, if it
00070          * has not already been initialized.
00071          */
00072 
00073         RUNTIME_CHECK(isc_once_do(&msgcat_once, open_msgcat) == ISC_R_SUCCESS);
00074 }
00075 
00076 static isc_once_t init_once = ISC_ONCE_INIT;
00077 static isc_mem_t *dns_g_mctx = NULL;
00078 static dns_dbimplementation_t *dbimp = NULL;
00079 static isc_boolean_t initialize_done = ISC_FALSE;
00080 static isc_mutex_t reflock;
00081 static unsigned int references = 0;
00082 
00083 static void
00084 initialize(void) {
00085         isc_result_t result;
00086 
00087         REQUIRE(initialize_done == ISC_FALSE);
00088 
00089         result = isc_mem_create(0, 0, &dns_g_mctx);
00090         if (result != ISC_R_SUCCESS)
00091                 return;
00092         dns_result_register();
00093         result = dns_ecdb_register(dns_g_mctx, &dbimp);
00094         if (result != ISC_R_SUCCESS)
00095                 goto cleanup_mctx;
00096         result = isc_hash_create(dns_g_mctx, NULL, DNS_NAME_MAXWIRE);
00097         if (result != ISC_R_SUCCESS)
00098                 goto cleanup_db;
00099 
00100         result = dst_lib_init(dns_g_mctx, NULL, 0);
00101         if (result != ISC_R_SUCCESS)
00102                 goto cleanup_hash;
00103 
00104         result = isc_mutex_init(&reflock);
00105         if (result != ISC_R_SUCCESS)
00106                 goto cleanup_dst;
00107 
00108         initialize_done = ISC_TRUE;
00109         return;
00110 
00111   cleanup_dst:
00112         dst_lib_destroy();
00113   cleanup_hash:
00114         isc_hash_destroy();
00115   cleanup_db:
00116         if (dbimp != NULL)
00117                 dns_ecdb_unregister(&dbimp);
00118   cleanup_mctx:
00119         if (dns_g_mctx != NULL)
00120                 isc_mem_detach(&dns_g_mctx);
00121 }
00122 
00123 isc_result_t
00124 dns_lib_init(void) {
00125         isc_result_t result;
00126 
00127         /*
00128          * Since this routine is expected to be used by a normal application,
00129          * it should be better to return an error, instead of an emergency
00130          * abort, on any failure.
00131          */
00132         result = isc_once_do(&init_once, initialize);
00133         if (result != ISC_R_SUCCESS)
00134                 return (result);
00135 
00136         if (!initialize_done)
00137                 return (ISC_R_FAILURE);
00138 
00139         LOCK(&reflock);
00140         references++;
00141         UNLOCK(&reflock);
00142 
00143         return (ISC_R_SUCCESS);
00144 }
00145 
00146 void
00147 dns_lib_shutdown(void) {
00148         isc_boolean_t cleanup_ok = ISC_FALSE;
00149 
00150         LOCK(&reflock);
00151         if (--references == 0)
00152                 cleanup_ok = ISC_TRUE;
00153         UNLOCK(&reflock);
00154 
00155         if (!cleanup_ok)
00156                 return;
00157 
00158         dst_lib_destroy();
00159         isc_hash_destroy();
00160         if (dbimp != NULL)
00161                 dns_ecdb_unregister(&dbimp);
00162         if (dns_g_mctx != NULL)
00163                 isc_mem_detach(&dns_g_mctx);
00164 }

Generated on Tue Apr 28 17:41:04 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha