PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/bind9/lib/dns/lib.c

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