Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / lib.c
blob1c059f74e6ec988031188aeb72386cd44d5e107b
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: lib.c,v 1.19 2009/09/03 00:12:23 each Exp */
22 /*! \file */
24 #include <config.h>
26 #include <stddef.h>
28 #include <isc/hash.h>
29 #include <isc/mem.h>
30 #include <isc/msgcat.h>
31 #include <isc/mutex.h>
32 #include <isc/once.h>
33 #include <isc/util.h>
35 #include <dns/db.h>
36 #include <dns/ecdb.h>
37 #include <dns/lib.h>
38 #include <dns/result.h>
40 #include <dst/dst.h>
43 /***
44 *** Globals
45 ***/
47 LIBDNS_EXTERNAL_DATA unsigned int dns_pps = 0U;
48 LIBDNS_EXTERNAL_DATA isc_msgcat_t * dns_msgcat = NULL;
51 /***
52 *** Private
53 ***/
55 static isc_once_t msgcat_once = ISC_ONCE_INIT;
58 /***
59 *** Functions
60 ***/
62 static void
63 open_msgcat(void) {
64 isc_msgcat_open("libdns.cat", &dns_msgcat);
67 void
68 dns_lib_initmsgcat(void) {
71 * Initialize the DNS library's message catalog, dns_msgcat, if it
72 * has not already been initialized.
75 RUNTIME_CHECK(isc_once_do(&msgcat_once, open_msgcat) == ISC_R_SUCCESS);
78 static isc_once_t init_once = ISC_ONCE_INIT;
79 static isc_mem_t *dns_g_mctx = NULL;
80 #ifndef BIND9
81 static dns_dbimplementation_t *dbimp = NULL;
82 #endif
83 static isc_boolean_t initialize_done = ISC_FALSE;
84 static isc_mutex_t reflock;
85 static unsigned int references = 0;
87 static void
88 initialize(void) {
89 isc_result_t result;
91 REQUIRE(initialize_done == ISC_FALSE);
93 result = isc_mem_create(0, 0, &dns_g_mctx);
94 if (result != ISC_R_SUCCESS)
95 return;
96 dns_result_register();
97 #ifndef BIND9
98 result = dns_ecdb_register(dns_g_mctx, &dbimp);
99 if (result != ISC_R_SUCCESS)
100 goto cleanup_mctx;
101 #endif
102 result = isc_hash_create(dns_g_mctx, NULL, DNS_NAME_MAXWIRE);
103 if (result != ISC_R_SUCCESS)
104 goto cleanup_db;
106 result = dst_lib_init(dns_g_mctx, NULL, 0);
107 if (result != ISC_R_SUCCESS)
108 goto cleanup_hash;
110 result = isc_mutex_init(&reflock);
111 if (result != ISC_R_SUCCESS)
112 goto cleanup_dst;
114 initialize_done = ISC_TRUE;
115 return;
117 cleanup_dst:
118 dst_lib_destroy();
119 cleanup_hash:
120 isc_hash_destroy();
121 cleanup_db:
122 #ifndef BIND9
123 dns_ecdb_unregister(&dbimp);
124 cleanup_mctx:
125 #endif
126 isc_mem_detach(&dns_g_mctx);
129 isc_result_t
130 dns_lib_init(void) {
131 isc_result_t result;
134 * Since this routine is expected to be used by a normal application,
135 * it should be better to return an error, instead of an emergency
136 * abort, on any failure.
138 result = isc_once_do(&init_once, initialize);
139 if (result != ISC_R_SUCCESS)
140 return (result);
142 if (!initialize_done)
143 return (ISC_R_FAILURE);
145 LOCK(&reflock);
146 references++;
147 UNLOCK(&reflock);
149 return (ISC_R_SUCCESS);
152 void
153 dns_lib_shutdown(void) {
154 isc_boolean_t cleanup_ok = ISC_FALSE;
156 LOCK(&reflock);
157 if (--references == 0)
158 cleanup_ok = ISC_TRUE;
159 UNLOCK(&reflock);
161 if (!cleanup_ok)
162 return;
164 dst_lib_destroy();
165 isc_hash_destroy();
166 #ifndef BIND9
167 dns_ecdb_unregister(&dbimp);
168 #endif
169 isc_mem_detach(&dns_g_mctx);