summaryrefslogtreecommitdiff
path: root/usr.sbin/bind/lib/dns/lib.c
blob: 55dd08d1b04b87461ada743889e091f8121a1140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

/* $Id: lib.c,v 1.9 2020/01/21 23:59:20 tedu Exp $ */

/*! \file */

#include <config.h>

#include <stddef.h>

#include <isc/hash.h>


#include <isc/once.h>
#include <isc/util.h>



#include <dns/lib.h>
#include <dns/result.h>

#include <dst/dst.h>


/***
 *** Globals
 ***/

unsigned int			dns_pps = 0U;

/***
 *** Functions
 ***/

static isc_once_t init_once = ISC_ONCE_INIT;
static isc_boolean_t initialize_done = ISC_FALSE;
static unsigned int references = 0;

static void
initialize(void) {
	isc_result_t result;

	REQUIRE(initialize_done == ISC_FALSE);

	dns_result_register();
	result = isc_hash_create(DNS_NAME_MAXWIRE);
	if (result != ISC_R_SUCCESS)
		return;

	result = dst_lib_init();
	if (result != ISC_R_SUCCESS)
		goto cleanup_hash;

	initialize_done = ISC_TRUE;
	return;

  cleanup_hash:
	isc_hash_destroy();
}

isc_result_t
dns_lib_init(void) {
	isc_result_t result;

	/*
	 * Since this routine is expected to be used by a normal application,
	 * it should be better to return an error, instead of an emergency
	 * abort, on any failure.
	 */
	result = isc_once_do(&init_once, initialize);
	if (result != ISC_R_SUCCESS)
		return (result);

	if (!initialize_done)
		return (ISC_R_FAILURE);

	references++;

	return (ISC_R_SUCCESS);
}

void
dns_lib_shutdown(void) {
	isc_boolean_t cleanup_ok = ISC_FALSE;

	if (--references == 0)
		cleanup_ok = ISC_TRUE;

	if (!cleanup_ok)
		return;

	dst_lib_destroy();
	isc_hash_destroy();
}