xref: /minix/external/bsd/bind/dist/lib/isc/app_api.c (revision 00b67f09)
1 /*	$NetBSD: app_api.c,v 1.7 2014/12/10 04:37:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2009, 2013, 2014  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Id: app_api.c,v 1.5 2009/09/02 23:48:02 tbox Exp  */
20 
21 #include <config.h>
22 
23 #include <unistd.h>
24 
25 #include <isc/app.h>
26 #include <isc/magic.h>
27 #include <isc/mutex.h>
28 #include <isc/once.h>
29 #include <isc/util.h>
30 
31 static isc_mutex_t createlock;
32 static isc_once_t once = ISC_ONCE_INIT;
33 static isc_appctxcreatefunc_t appctx_createfunc = NULL;
34 
35 #define ISCAPI_APPMETHODS_VALID(m) ISC_MAGIC_VALID(m, ISCAPI_APPMETHODS_MAGIC)
36 
37 static void
initialize(void)38 initialize(void) {
39 	RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
40 }
41 
42 isc_result_t
isc_app_register(isc_appctxcreatefunc_t createfunc)43 isc_app_register(isc_appctxcreatefunc_t createfunc) {
44 	isc_result_t result = ISC_R_SUCCESS;
45 
46 	RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
47 
48 	LOCK(&createlock);
49 	if (appctx_createfunc == NULL)
50 		appctx_createfunc = createfunc;
51 	else
52 		result = ISC_R_EXISTS;
53 	UNLOCK(&createlock);
54 
55 	return (result);
56 }
57 
58 isc_result_t
isc_appctx_create(isc_mem_t * mctx,isc_appctx_t ** ctxp)59 isc_appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
60 	isc_result_t result;
61 
62 	if (isc_bind9)
63 		return (isc__appctx_create(mctx, ctxp));
64 
65 	LOCK(&createlock);
66 
67 	REQUIRE(appctx_createfunc != NULL);
68 	result = (*appctx_createfunc)(mctx, ctxp);
69 
70 	UNLOCK(&createlock);
71 
72 	return (result);
73 }
74 
75 void
isc_appctx_destroy(isc_appctx_t ** ctxp)76 isc_appctx_destroy(isc_appctx_t **ctxp) {
77 	REQUIRE(ctxp != NULL && ISCAPI_APPCTX_VALID(*ctxp));
78 
79 	if (isc_bind9)
80 		isc__appctx_destroy(ctxp);
81 	else
82 		(*ctxp)->methods->ctxdestroy(ctxp);
83 
84 	ENSURE(*ctxp == NULL);
85 }
86 
87 isc_result_t
isc_app_ctxstart(isc_appctx_t * ctx)88 isc_app_ctxstart(isc_appctx_t *ctx) {
89 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
90 
91 	if (isc_bind9)
92 		return (isc__app_ctxstart(ctx));
93 
94 	return (ctx->methods->ctxstart(ctx));
95 }
96 
97 isc_result_t
isc_app_ctxrun(isc_appctx_t * ctx)98 isc_app_ctxrun(isc_appctx_t *ctx) {
99 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
100 
101 	if (isc_bind9)
102 		return (isc__app_ctxrun(ctx));
103 
104 	return (ctx->methods->ctxrun(ctx));
105 }
106 
107 isc_result_t
isc_app_ctxonrun(isc_appctx_t * ctx,isc_mem_t * mctx,isc_task_t * task,isc_taskaction_t action,void * arg)108 isc_app_ctxonrun(isc_appctx_t *ctx, isc_mem_t *mctx,
109 		 isc_task_t *task, isc_taskaction_t action,
110 		 void *arg)
111 {
112 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
113 
114 	if (isc_bind9)
115 		return (isc__app_ctxonrun(ctx, mctx, task, action, arg));
116 
117 	return (ctx->methods->ctxonrun(ctx, mctx, task, action, arg));
118 }
119 
120 isc_result_t
isc_app_ctxsuspend(isc_appctx_t * ctx)121 isc_app_ctxsuspend(isc_appctx_t *ctx) {
122 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
123 
124 	if (isc_bind9)
125 		return (isc__app_ctxsuspend(ctx));
126 
127 	return (ctx->methods->ctxsuspend(ctx));
128 }
129 
130 isc_result_t
isc_app_ctxshutdown(isc_appctx_t * ctx)131 isc_app_ctxshutdown(isc_appctx_t *ctx) {
132 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
133 
134 	if (isc_bind9)
135 		return (isc__app_ctxshutdown(ctx));
136 
137 	return (ctx->methods->ctxshutdown(ctx));
138 }
139 
140 void
isc_app_ctxfinish(isc_appctx_t * ctx)141 isc_app_ctxfinish(isc_appctx_t *ctx) {
142 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
143 
144 	if (isc_bind9)
145 		isc__app_ctxfinish(ctx);
146 
147 	ctx->methods->ctxfinish(ctx);
148 }
149 
150 void
isc_appctx_settaskmgr(isc_appctx_t * ctx,isc_taskmgr_t * taskmgr)151 isc_appctx_settaskmgr(isc_appctx_t *ctx, isc_taskmgr_t *taskmgr) {
152 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
153 	REQUIRE(taskmgr != NULL);
154 
155 	if (isc_bind9)
156 		isc__appctx_settaskmgr(ctx, taskmgr);
157 
158 	ctx->methods->settaskmgr(ctx, taskmgr);
159 }
160 
161 void
isc_appctx_setsocketmgr(isc_appctx_t * ctx,isc_socketmgr_t * socketmgr)162 isc_appctx_setsocketmgr(isc_appctx_t *ctx, isc_socketmgr_t *socketmgr) {
163 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
164 	REQUIRE(socketmgr != NULL);
165 
166 	if (isc_bind9)
167 		isc__appctx_setsocketmgr(ctx, socketmgr);
168 
169 	ctx->methods->setsocketmgr(ctx, socketmgr);
170 }
171 
172 void
isc_appctx_settimermgr(isc_appctx_t * ctx,isc_timermgr_t * timermgr)173 isc_appctx_settimermgr(isc_appctx_t *ctx, isc_timermgr_t *timermgr) {
174 	REQUIRE(ISCAPI_APPCTX_VALID(ctx));
175 	REQUIRE(timermgr != NULL);
176 
177 	if (isc_bind9)
178 		isc__appctx_settimermgr(ctx, timermgr);
179 
180 	ctx->methods->settimermgr(ctx, timermgr);
181 }
182 
183 isc_result_t
isc_app_start(void)184 isc_app_start(void) {
185 	if (isc_bind9)
186 		return (isc__app_start());
187 
188 	return (ISC_R_NOTIMPLEMENTED);
189 }
190 
191 isc_result_t
isc_app_onrun(isc_mem_t * mctx,isc_task_t * task,isc_taskaction_t action,void * arg)192 isc_app_onrun(isc_mem_t *mctx, isc_task_t *task,
193 	       isc_taskaction_t action, void *arg)
194 {
195 	if (isc_bind9)
196 		return (isc__app_onrun(mctx, task, action, arg));
197 
198 	return (ISC_R_NOTIMPLEMENTED);
199 }
200 
201 isc_result_t
isc_app_run()202 isc_app_run() {
203 	if (isc_bind9)
204 		return (isc__app_run());
205 
206 	return (ISC_R_NOTIMPLEMENTED);
207 }
208 
209 isc_result_t
isc_app_shutdown(void)210 isc_app_shutdown(void) {
211 	if (isc_bind9)
212 		return (isc__app_shutdown());
213 
214 	return (ISC_R_NOTIMPLEMENTED);
215 }
216 
217 isc_result_t
isc_app_reload(void)218 isc_app_reload(void) {
219 	if (isc_bind9)
220 		return (isc__app_reload());
221 
222 	return (ISC_R_NOTIMPLEMENTED);
223 }
224 
225 void
isc_app_finish(void)226 isc_app_finish(void) {
227 	if (!isc_bind9)
228 		return;
229 
230 	isc__app_finish();
231 }
232 
233 void
isc_app_block(void)234 isc_app_block(void) {
235 	if (!isc_bind9)
236 		return;
237 
238 	isc__app_block();
239 }
240 
241 void
isc_app_unblock(void)242 isc_app_unblock(void) {
243 	if (!isc_bind9)
244 		return;
245 
246 	isc__app_unblock();
247 }
248