summaryrefslogtreecommitdiffstats
path: root/lib/dns/dynamic_db.c
blob: 662fc065bdc47175900ec75363c00c546616299a (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright (C) 2008-2009  Red Hat, Inc.
 *
 * 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 Red Hat DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL Red Hat 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.
 */


#include <config.h>

#include <isc/mem.h>
#include <isc/mutex.h>
#include <isc/once.h>
#include <isc/result.h>
#include <isc/types.h>
#include <isc/util.h>

#include <dns/dynamic_db.h>
#include <dns/log.h>
#include <dns/types.h>


#if HAVE_DLFCN_H
#include <dlfcn.h>
#endif

#define CHECK(op)						\
	do { result = (op);					\
		if (result != ISC_R_SUCCESS) goto cleanup;	\
	} while (0)


typedef isc_result_t (*register_func_t)(isc_mem_t *mctx, const char *name,
		const char * const *argv, dns_view_t *view,
		dns_zonemgr_t *zmgr);
typedef void (*destroy_func_t)(void);

typedef struct dyndb_implementation dyndb_implementation_t;

struct dyndb_implementation {
	isc_mem_t			*mctx;
	void				*handle;
	register_func_t			register_function;
	destroy_func_t			destroy_function;
	LINK(dyndb_implementation_t)	link;
};

/* List of implementations. Locked by dyndb_lock. */
static LIST(dyndb_implementation_t) dyndb_implementations;
/* Locks dyndb_implementations. */
static isc_mutex_t dyndb_lock;
static isc_once_t once = ISC_ONCE_INIT;

static void
dyndb_initialize(void) {
	RUNTIME_CHECK(isc_mutex_init(&dyndb_lock) == ISC_R_SUCCESS);
	INIT_LIST(dyndb_implementations);
}


#if HAVE_DLFCN_H
static isc_result_t
load_symbol(void *handle, const char *symbol_name, void **symbolp)
{
	const char *errmsg;
	void *symbol;

	REQUIRE(handle != NULL);
	REQUIRE(symbolp != NULL && *symbolp == NULL);

	symbol = dlsym(handle, symbol_name);
	if (symbol == NULL) {
		errmsg = dlerror();
		if (errmsg == NULL)
			errmsg = "returned function pointer is NULL";
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR,
			      "failed to lookup symbol %s: %s",
			      symbol_name, errmsg);
		return ISC_R_FAILURE;
	}
	dlerror();

	*symbolp = symbol;

	return ISC_R_SUCCESS;
}

static isc_result_t
load_library(isc_mem_t *mctx, const char *filename, dyndb_implementation_t **impp)
{
	isc_result_t result;
	void *handle;
	dyndb_implementation_t *imp;
	register_func_t register_function = NULL;
	destroy_func_t destroy_function = NULL;

	REQUIRE(impp != NULL && *impp == NULL);

	handle = dlopen(filename, RTLD_LAZY);
	if (handle == NULL) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DYNDB, ISC_LOG_ERROR,
			      "failed to dynamically load driver '%s': %s",
			      filename, dlerror());
		result = ISC_R_FAILURE;
		goto cleanup;
	}
	dlerror();

	CHECK(load_symbol(handle, "dynamic_driver_init",
			  (void **)&register_function));
	CHECK(load_symbol(handle, "dynamic_driver_destroy",
			  (void **)&destroy_function));

	imp = isc_mem_get(mctx, sizeof(dyndb_implementation_t));
	if (imp == NULL) {
		result = ISC_R_NOMEMORY;
		goto cleanup;
	}

	imp->mctx = NULL;
	isc_mem_attach(mctx, &imp->mctx);
	imp->handle = handle;
	imp->register_function = register_function;
	imp->destroy_function = destroy_function;
	INIT_LINK(imp, link);

	*impp = imp;

	return ISC_R_SUCCESS;

cleanup:
	if (handle != NULL)
		dlclose(handle);

	return result;
}

static void
unload_library(dyndb_implementation_t **impp)
{
	dyndb_implementation_t *imp;

	REQUIRE(impp != NULL && *impp != NULL);

	imp = *impp;

	isc_mem_putanddetach(&imp->mctx, imp, sizeof(dyndb_implementation_t));

	*impp = NULL;
}

#else	/* HAVE_DLFCN_H */
static isc_result_t
load_library(isc_mem_t *mctx, const char *filename, dyndb_implementation_t **impp)
{
	UNUSED(mctx);
	UNUSED(filename);
	UNUSED(impp);

	isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DYNDB,
		      ISC_LOG_ERROR,
		      "dynamic database support is not implemented")

	return ISC_R_NOTIMPLEMENTED;
}

static void
unload_library(dyndb_implementation_t **impp)
{
	dyndb_implementation_t *imp;

	REQUIRE(impp != NULL && *impp != NULL);

	imp = *impp;

	isc_mem_putanddetach(&imp->mctx, imp, sizeof(dyndb_implementation_t));

	*impp = NULL;
}
#endif	/* HAVE_DLFCN_H */

isc_result_t
dns_dynamic_db_load(const char *libname, const char *name, isc_mem_t *mctx,
		    const char * const *argv, dns_view_t *view,
		    dns_zonemgr_t *zmgr)
{
	isc_result_t result;
	dyndb_implementation_t *implementation = NULL;

	RUNTIME_CHECK(isc_once_do(&once, dyndb_initialize) == ISC_R_SUCCESS);

	CHECK(load_library(mctx, libname, &implementation));
	CHECK(implementation->register_function(mctx, name, argv, view, zmgr));

	LOCK(&dyndb_lock);
	APPEND(dyndb_implementations, implementation, link);
	UNLOCK(&dyndb_lock);

	return ISC_R_SUCCESS;

cleanup:
	if (implementation != NULL)
		unload_library(&implementation);

	return result;
}

void
dns_dynamic_db_cleanup(void)
{
	dyndb_implementation_t *elem;
	dyndb_implementation_t *next;

	RUNTIME_CHECK(isc_once_do(&once, dyndb_initialize) == ISC_R_SUCCESS);

	LOCK(&dyndb_lock);
	elem = HEAD(dyndb_implementations);
	while (elem != NULL) {
		next = NEXT(elem, link);
		UNLINK(dyndb_implementations, elem, link);
		elem->destroy_function();
		unload_library(&elem);
		elem = next;
	}
	UNLOCK(&dyndb_lock);

	isc_mutex_destroy(&dyndb_lock);
}