1 /* $NetBSD: app.c,v 1.6 2014/12/10 04:38:01 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004, 2007, 2009, 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
6 *
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.
10 *
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.
18 */
19
20 /* Id: app.c,v 1.9 2009/09/02 23:48:03 tbox Exp */
21
22 #include <config.h>
23
24 #include <sys/types.h>
25
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <process.h>
31
32 #include <isc/app.h>
33 #include <isc/boolean.h>
34 #include <isc/condition.h>
35 #include <isc/mem.h>
36 #include <isc/msgs.h>
37 #include <isc/mutex.h>
38 #include <isc/event.h>
39 #include <isc/platform.h>
40 #include <isc/string.h>
41 #include <isc/task.h>
42 #include <isc/time.h>
43 #include <isc/util.h>
44 #include <isc/thread.h>
45
46 /*%
47 * For BIND9 internal applications built with threads, we use a single app
48 * context and let multiple worker, I/O, timer threads do actual jobs.
49 */
50
51 static isc_thread_t blockedthread;
52
53 /*%
54 * The following are intended for internal use (indicated by "isc__"
55 * prefix) but are not declared as static, allowing direct access from
56 * unit tests etc.
57 */
58 isc_result_t isc__app_start(void);
59 isc_result_t isc__app_ctxstart(isc_appctx_t *ctx);
60 isc_result_t isc__app_onrun(isc_mem_t *mctx, isc_task_t *task,
61 isc_taskaction_t action, void *arg);
62 isc_result_t isc__app_ctxrun(isc_appctx_t *ctx);
63 isc_result_t isc__app_run(void);
64 isc_result_t isc__app_ctxshutdown(isc_appctx_t *ctx);
65 isc_result_t isc__app_shutdown(void);
66 isc_result_t isc__app_reload(void);
67 isc_result_t isc__app_ctxsuspend(isc_appctx_t *ctx);
68 void isc__app_ctxfinish(isc_appctx_t *ctx);
69 void isc__app_finish(void);
70 void isc__app_block(void);
71 void isc__app_unblock(void);
72 isc_result_t isc__appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp);
73 void isc__appctx_destroy(isc_appctx_t **ctxp);
74 void isc__appctx_settaskmgr(isc_appctx_t *ctx, isc_taskmgr_t *taskmgr);
75 void isc__appctx_setsocketmgr(isc_appctx_t *ctx, isc_socketmgr_t *socketmgr);
76 void isc__appctx_settimermgr(isc_appctx_t *ctx, isc_timermgr_t *timermgr);
77 isc_result_t isc__app_ctxonrun(isc_appctx_t *ctx, isc_mem_t *mctx,
78 isc_task_t *task, isc_taskaction_t action,
79 void *arg);
80
81 /*
82 * The application context of this module. This implementation actually
83 * doesn't use it. (This may change in the future).
84 */
85 #define APPCTX_MAGIC ISC_MAGIC('A', 'p', 'c', 'x')
86 #define VALID_APPCTX(c) ISC_MAGIC_VALID(c, APPCTX_MAGIC)
87
88 /* Events to wait for */
89
90 #define NUM_EVENTS 2
91
92 enum {
93 RELOAD_EVENT,
94 SHUTDOWN_EVENT
95 };
96
97 typedef struct isc__appctx {
98 isc_appctx_t common;
99 isc_mem_t *mctx;
100 isc_eventlist_t on_run;
101 isc_mutex_t lock;
102 isc_boolean_t shutdown_requested;
103 isc_boolean_t running;
104 /*
105 * We assume that 'want_shutdown' can be read and written atomically.
106 */
107 isc_boolean_t want_shutdown;
108 /*
109 * We assume that 'want_reload' can be read and written atomically.
110 */
111 isc_boolean_t want_reload;
112
113 isc_boolean_t blocked;
114
115 HANDLE hEvents[NUM_EVENTS];
116
117 isc_taskmgr_t *taskmgr;
118 isc_socketmgr_t *socketmgr;
119 isc_timermgr_t *timermgr;
120 } isc__appctx_t;
121
122 static isc__appctx_t isc_g_appctx;
123
124 static struct {
125 isc_appmethods_t methods;
126
127 /*%
128 * The following are defined just for avoiding unused static functions.
129 */
130 void *run, *shutdown, *start, *reload, *finish, *block, *unblock;
131 } appmethods = {
132 {
133 isc__appctx_destroy,
134 isc__app_ctxstart,
135 isc__app_ctxrun,
136 isc__app_ctxsuspend,
137 isc__app_ctxshutdown,
138 isc__app_ctxfinish,
139 isc__appctx_settaskmgr,
140 isc__appctx_setsocketmgr,
141 isc__appctx_settimermgr,
142 isc__app_ctxonrun
143 },
144 (void *)isc__app_run,
145 (void *)isc__app_shutdown,
146 (void *)isc__app_start,
147 (void *)isc__app_reload,
148 (void *)isc__app_finish,
149 (void *)isc__app_block,
150 (void *)isc__app_unblock
151 };
152
153 /*
154 * We need to remember which thread is the main thread...
155 */
156 static isc_thread_t main_thread;
157
158 isc_result_t
isc__app_ctxstart(isc_appctx_t * ctx0)159 isc__app_ctxstart(isc_appctx_t *ctx0) {
160 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
161 isc_result_t result;
162
163 REQUIRE(VALID_APPCTX(ctx));
164
165 /*
166 * Start an ISC library application.
167 */
168
169 main_thread = GetCurrentThread();
170
171 result = isc_mutex_init(&ctx->lock);
172 if (result != ISC_R_SUCCESS)
173 return (result);
174
175 ctx->shutdown_requested = ISC_FALSE;
176 ctx->running = ISC_FALSE;
177 ctx->want_shutdown = ISC_FALSE;
178 ctx->want_reload = ISC_FALSE;
179 ctx->blocked = ISC_FALSE;
180
181 /* Create the reload event in a non-signaled state */
182 ctx->hEvents[RELOAD_EVENT] = CreateEvent(NULL, FALSE, FALSE, NULL);
183
184 /* Create the shutdown event in a non-signaled state */
185 ctx->hEvents[SHUTDOWN_EVENT] = CreateEvent(NULL, FALSE, FALSE, NULL);
186
187 ISC_LIST_INIT(ctx->on_run);
188 return (ISC_R_SUCCESS);
189 }
190
191 isc_result_t
isc__app_start(void)192 isc__app_start(void) {
193 isc_g_appctx.common.impmagic = APPCTX_MAGIC;
194 isc_g_appctx.common.magic = ISCAPI_APPCTX_MAGIC;
195 isc_g_appctx.common.methods = &appmethods.methods;
196 isc_g_appctx.mctx = NULL;
197 /* The remaining members will be initialized in ctxstart() */
198
199 return (isc__app_ctxstart((isc_appctx_t *)&isc_g_appctx));
200 }
201
202 isc_result_t
isc__app_onrun(isc_mem_t * mctx,isc_task_t * task,isc_taskaction_t action,void * arg)203 isc__app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
204 void *arg)
205 {
206 return (isc__app_ctxonrun((isc_appctx_t *)&isc_g_appctx, mctx,
207 task, action, arg));
208 }
209
210 isc_result_t
isc__app_ctxonrun(isc_appctx_t * ctx0,isc_mem_t * mctx,isc_task_t * task,isc_taskaction_t action,void * arg)211 isc__app_ctxonrun(isc_appctx_t *ctx0, isc_mem_t *mctx, isc_task_t *task,
212 isc_taskaction_t action, void *arg)
213 {
214 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
215 isc_event_t *event;
216 isc_task_t *cloned_task = NULL;
217 isc_result_t result;
218
219 LOCK(&ctx->lock);
220
221 if (ctx->running) {
222 result = ISC_R_ALREADYRUNNING;
223 goto unlock;
224 }
225
226 /*
227 * Note that we store the task to which we're going to send the event
228 * in the event's "sender" field.
229 */
230 isc_task_attach(task, &cloned_task);
231 event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
232 action, arg, sizeof(*event));
233 if (event == NULL) {
234 result = ISC_R_NOMEMORY;
235 goto unlock;
236 }
237
238 ISC_LIST_APPEND(ctx->on_run, event, ev_link);
239
240 result = ISC_R_SUCCESS;
241
242 unlock:
243 UNLOCK(&ctx->lock);
244
245 return (result);
246 }
247
248 isc_result_t
isc__app_ctxrun(isc_appctx_t * ctx0)249 isc__app_ctxrun(isc_appctx_t *ctx0) {
250 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
251 isc_event_t *event, *next_event;
252 isc_task_t *task;
253 HANDLE *pHandles = NULL;
254 DWORD dwWaitResult;
255
256 REQUIRE(VALID_APPCTX(ctx));
257
258 REQUIRE(main_thread == GetCurrentThread());
259
260 LOCK(&ctx->lock);
261
262 if (!ctx->running) {
263 ctx->running = ISC_TRUE;
264
265 /*
266 * Post any on-run events (in FIFO order).
267 */
268 for (event = ISC_LIST_HEAD(ctx->on_run);
269 event != NULL;
270 event = next_event) {
271 next_event = ISC_LIST_NEXT(event, ev_link);
272 ISC_LIST_UNLINK(ctx->on_run, event, ev_link);
273 task = event->ev_sender;
274 event->ev_sender = NULL;
275 isc_task_sendanddetach(&task, &event);
276 }
277
278 }
279
280 UNLOCK(&ctx->lock);
281
282 /*
283 * There is no danger if isc_app_shutdown() is called before we wait
284 * for events.
285 */
286
287 while (!ctx->want_shutdown) {
288 dwWaitResult = WaitForMultipleObjects(NUM_EVENTS, ctx->hEvents,
289 FALSE, INFINITE);
290
291 /* See why we returned */
292
293 if (WaitSucceeded(dwWaitResult, NUM_EVENTS)) {
294 /*
295 * The return was due to one of the events
296 * being signaled
297 */
298 switch (WaitSucceededIndex(dwWaitResult)) {
299 case RELOAD_EVENT:
300 ctx->want_reload = ISC_TRUE;
301 break;
302
303 case SHUTDOWN_EVENT:
304 ctx->want_shutdown = ISC_TRUE;
305 break;
306 }
307 }
308
309 if (ctx->want_reload) {
310 ctx->want_reload = ISC_FALSE;
311 return (ISC_R_RELOAD);
312 }
313
314 if (ctx->want_shutdown && ctx->blocked)
315 exit(-1);
316 }
317
318 return (ISC_R_SUCCESS);
319 }
320
321 isc_result_t
isc__app_run(void)322 isc__app_run(void) {
323 return (isc__app_ctxrun((isc_appctx_t *)&isc_g_appctx));
324 }
325
326 isc_result_t
isc__app_ctxshutdown(isc_appctx_t * ctx0)327 isc__app_ctxshutdown(isc_appctx_t *ctx0) {
328 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
329 isc_boolean_t want_kill = ISC_TRUE;
330
331 REQUIRE(VALID_APPCTX(ctx));
332
333 LOCK(&ctx->lock);
334
335 REQUIRE(ctx->running);
336
337 if (ctx->shutdown_requested)
338 want_kill = ISC_FALSE; /* We're only signaling once */
339 else
340 ctx->shutdown_requested = ISC_TRUE;
341
342 UNLOCK(&ctx->lock);
343
344 if (want_kill)
345 SetEvent(ctx->hEvents[SHUTDOWN_EVENT]);
346
347 return (ISC_R_SUCCESS);
348 }
349
350 isc_result_t
isc__app_shutdown(void)351 isc__app_shutdown(void) {
352 return (isc__app_ctxshutdown((isc_appctx_t *)&isc_g_appctx));
353 }
354
355 isc_result_t
isc__app_ctxsuspend(isc_appctx_t * ctx0)356 isc__app_ctxsuspend(isc_appctx_t *ctx0) {
357 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
358 isc_boolean_t want_kill = ISC_TRUE;
359
360 REQUIRE(VALID_APPCTX(ctx));
361
362 LOCK(&ctx->lock);
363
364 REQUIRE(ctx->running);
365
366 /*
367 * Don't send the reload signal if we're shutting down.
368 */
369 if (ctx->shutdown_requested)
370 want_kill = ISC_FALSE;
371
372 UNLOCK(&ctx->lock);
373
374 if (want_kill)
375 SetEvent(ctx->hEvents[RELOAD_EVENT]);
376
377 return (ISC_R_SUCCESS);
378 }
379
380 isc_result_t
isc__app_reload(void)381 isc__app_reload(void) {
382 return (isc__app_ctxsuspend((isc_appctx_t *)&isc_g_appctx));
383 }
384
385 void
isc__app_ctxfinish(isc_appctx_t * ctx0)386 isc__app_ctxfinish(isc_appctx_t *ctx0) {
387 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
388
389 REQUIRE(VALID_APPCTX(ctx));
390
391 DESTROYLOCK(&ctx->lock);
392 }
393
394 void
isc__app_finish(void)395 isc__app_finish(void) {
396 isc__app_ctxfinish((isc_appctx_t *)&isc_g_appctx);
397 }
398
399 void
isc__app_block(void)400 isc__app_block(void) {
401 REQUIRE(isc_g_appctx.running);
402 REQUIRE(!isc_g_appctx.blocked);
403
404 isc_g_appctx.blocked = ISC_TRUE;
405 blockedthread = GetCurrentThread();
406 }
407
408 void
isc__app_unblock(void)409 isc__app_unblock(void) {
410 REQUIRE(isc_g_appctx.running);
411 REQUIRE(isc_g_appctx.blocked);
412
413 isc_g_appctx.blocked = ISC_FALSE;
414 REQUIRE(blockedthread == GetCurrentThread());
415 }
416
417 isc_result_t
isc__appctx_create(isc_mem_t * mctx,isc_appctx_t ** ctxp)418 isc__appctx_create(isc_mem_t *mctx, isc_appctx_t **ctxp) {
419 isc__appctx_t *ctx;
420
421 REQUIRE(mctx != NULL);
422 REQUIRE(ctxp != NULL && *ctxp == NULL);
423
424 ctx = isc_mem_get(mctx, sizeof(*ctx));
425 if (ctx == NULL)
426 return (ISC_R_NOMEMORY);
427
428 ctx->common.impmagic = APPCTX_MAGIC;
429 ctx->common.magic = ISCAPI_APPCTX_MAGIC;
430 ctx->common.methods = &appmethods.methods;
431
432 ctx->mctx = NULL;
433 isc_mem_attach(mctx, &ctx->mctx);
434
435 ctx->taskmgr = NULL;
436 ctx->socketmgr = NULL;
437 ctx->timermgr = NULL;
438
439 *ctxp = (isc_appctx_t *)ctx;
440
441 return (ISC_R_SUCCESS);
442 }
443
444 void
isc__appctx_destroy(isc_appctx_t ** ctxp)445 isc__appctx_destroy(isc_appctx_t **ctxp) {
446 isc__appctx_t *ctx;
447
448 REQUIRE(ctxp != NULL);
449 ctx = (isc__appctx_t *)*ctxp;
450 REQUIRE(VALID_APPCTX(ctx));
451
452 isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx));
453
454 *ctxp = NULL;
455 }
456
457 void
isc__appctx_settaskmgr(isc_appctx_t * ctx0,isc_taskmgr_t * taskmgr)458 isc__appctx_settaskmgr(isc_appctx_t *ctx0, isc_taskmgr_t *taskmgr) {
459 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
460
461 REQUIRE(VALID_APPCTX(ctx));
462
463 ctx->taskmgr = taskmgr;
464 }
465
466 void
isc__appctx_setsocketmgr(isc_appctx_t * ctx0,isc_socketmgr_t * socketmgr)467 isc__appctx_setsocketmgr(isc_appctx_t *ctx0, isc_socketmgr_t *socketmgr) {
468 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
469
470 REQUIRE(VALID_APPCTX(ctx));
471
472 ctx->socketmgr = socketmgr;
473 }
474
475 void
isc__appctx_settimermgr(isc_appctx_t * ctx0,isc_timermgr_t * timermgr)476 isc__appctx_settimermgr(isc_appctx_t *ctx0, isc_timermgr_t *timermgr) {
477 isc__appctx_t *ctx = (isc__appctx_t *)ctx0;
478
479 REQUIRE(VALID_APPCTX(ctx));
480
481 ctx->timermgr = timermgr;
482 }
483
484 isc_result_t
isc__app_register(void)485 isc__app_register(void) {
486 return (isc_app_register(isc__appctx_create));
487 }
488
489 #include "../app_api.c"
490