2 * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
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.
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.
18 /* $Id: app.c,v 1.54.128.6 2008/10/15 03:41:17 marka Exp $ */
24 #include <sys/param.h> /* Openserver 5.0.6A and FD_SETSIZE */
25 #include <sys/types.h>
34 #include <sys/epoll.h>
38 #include <isc/boolean.h>
39 #include <isc/condition.h>
41 #include <isc/mutex.h>
42 #include <isc/event.h>
43 #include <isc/platform.h>
44 #include <isc/strerror.h>
45 #include <isc/string.h>
50 #ifdef ISC_PLATFORM_USETHREADS
52 #else /* ISC_PLATFORM_USETHREADS */
53 #include "../timer_p.h"
54 #include "../task_p.h"
56 #endif /* ISC_PLATFORM_USETHREADS */
58 static isc_eventlist_t on_run
;
59 static isc_mutex_t lock
;
60 static isc_boolean_t shutdown_requested
= ISC_FALSE
;
61 static isc_boolean_t running
= ISC_FALSE
;
63 * We assume that 'want_shutdown' can be read and written atomically.
65 static volatile isc_boolean_t want_shutdown
= ISC_FALSE
;
67 * We assume that 'want_reload' can be read and written atomically.
69 static volatile isc_boolean_t want_reload
= ISC_FALSE
;
71 static isc_boolean_t blocked
= ISC_FALSE
;
72 #ifdef ISC_PLATFORM_USETHREADS
73 static pthread_t blockedthread
;
74 #endif /* ISC_PLATFORM_USETHREADS */
76 #ifdef HAVE_LINUXTHREADS
78 * Linux has sigwait(), but it appears to prevent signal handlers from
79 * running, even if they're not in the set being waited for. This makes
80 * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
81 * Instead of messing with it, we just use sigsuspend() instead.
85 * We need to remember which thread is the main thread...
87 static pthread_t main_thread
;
92 exit_action(int arg
) {
94 want_shutdown
= ISC_TRUE
;
98 reload_action(int arg
) {
100 want_reload
= ISC_TRUE
;
105 handle_signal(int sig
, void (*handler
)(int)) {
107 char strbuf
[ISC_STRERRORSIZE
];
109 memset(&sa
, 0, sizeof(sa
));
110 sa
.sa_handler
= handler
;
112 if (sigfillset(&sa
.sa_mask
) != 0 ||
113 sigaction(sig
, &sa
, NULL
) < 0) {
114 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
115 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
116 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_APP
,
118 "handle_signal() %d setup: %s"),
120 return (ISC_R_UNEXPECTED
);
123 return (ISC_R_SUCCESS
);
127 isc_app_start(void) {
131 char strbuf
[ISC_STRERRORSIZE
];
134 * Start an ISC library application.
137 #ifdef NEED_PTHREAD_INIT
139 * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
141 presult
= pthread_init();
143 isc__strerror(presult
, strbuf
, sizeof(strbuf
));
144 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
145 "isc_app_start() pthread_init: %s", strbuf
);
146 return (ISC_R_UNEXPECTED
);
150 #ifdef HAVE_LINUXTHREADS
151 main_thread
= pthread_self();
154 result
= isc_mutex_init(&lock
);
155 if (result
!= ISC_R_SUCCESS
)
160 * Install do-nothing handlers for SIGINT and SIGTERM.
162 * We install them now because BSDI 3.1 won't block
163 * the default actions, regardless of what we do with
166 result
= handle_signal(SIGINT
, exit_action
);
167 if (result
!= ISC_R_SUCCESS
)
169 result
= handle_signal(SIGTERM
, exit_action
);
170 if (result
!= ISC_R_SUCCESS
)
175 * Always ignore SIGPIPE.
177 result
= handle_signal(SIGPIPE
, SIG_IGN
);
178 if (result
!= ISC_R_SUCCESS
)
182 * On Solaris 2, delivery of a signal whose action is SIG_IGN
183 * will not cause sigwait() to return. We may have inherited
184 * unexpected actions for SIGHUP, SIGINT, and SIGTERM from our parent
185 * process (e.g, Solaris cron). Set an action of SIG_DFL to make
186 * sure sigwait() works as expected. Only do this for SIGTERM and
187 * SIGINT if we don't have sigwait(), since a different handler is
190 result
= handle_signal(SIGHUP
, SIG_DFL
);
191 if (result
!= ISC_R_SUCCESS
)
195 result
= handle_signal(SIGTERM
, SIG_DFL
);
196 if (result
!= ISC_R_SUCCESS
)
198 result
= handle_signal(SIGINT
, SIG_DFL
);
199 if (result
!= ISC_R_SUCCESS
)
203 #ifdef ISC_PLATFORM_USETHREADS
205 * Block SIGHUP, SIGINT, SIGTERM.
207 * If isc_app_start() is called from the main thread before any other
208 * threads have been created, then the pthread_sigmask() call below
209 * will result in all threads having SIGHUP, SIGINT and SIGTERM
210 * blocked by default, ensuring that only the thread that calls
211 * sigwait() for them will get those signals.
213 if (sigemptyset(&sset
) != 0 ||
214 sigaddset(&sset
, SIGHUP
) != 0 ||
215 sigaddset(&sset
, SIGINT
) != 0 ||
216 sigaddset(&sset
, SIGTERM
) != 0) {
217 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
218 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
219 "isc_app_start() sigsetops: %s", strbuf
);
220 return (ISC_R_UNEXPECTED
);
222 presult
= pthread_sigmask(SIG_BLOCK
, &sset
, NULL
);
224 isc__strerror(presult
, strbuf
, sizeof(strbuf
));
225 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
226 "isc_app_start() pthread_sigmask: %s",
228 return (ISC_R_UNEXPECTED
);
230 #else /* ISC_PLATFORM_USETHREADS */
232 * Unblock SIGHUP, SIGINT, SIGTERM.
234 * If we're not using threads, we need to make sure that SIGHUP,
235 * SIGINT and SIGTERM are not inherited as blocked from the parent
238 if (sigemptyset(&sset
) != 0 ||
239 sigaddset(&sset
, SIGHUP
) != 0 ||
240 sigaddset(&sset
, SIGINT
) != 0 ||
241 sigaddset(&sset
, SIGTERM
) != 0) {
242 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
243 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
244 "isc_app_start() sigsetops: %s", strbuf
);
245 return (ISC_R_UNEXPECTED
);
247 presult
= sigprocmask(SIG_UNBLOCK
, &sset
, NULL
);
249 isc__strerror(presult
, strbuf
, sizeof(strbuf
));
250 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
251 "isc_app_start() sigprocmask: %s", strbuf
);
252 return (ISC_R_UNEXPECTED
);
254 #endif /* ISC_PLATFORM_USETHREADS */
256 ISC_LIST_INIT(on_run
);
258 return (ISC_R_SUCCESS
);
262 isc_app_onrun(isc_mem_t
*mctx
, isc_task_t
*task
, isc_taskaction_t action
,
266 isc_task_t
*cloned_task
= NULL
;
272 result
= ISC_R_ALREADYRUNNING
;
277 * Note that we store the task to which we're going to send the event
278 * in the event's "sender" field.
280 isc_task_attach(task
, &cloned_task
);
281 event
= isc_event_allocate(mctx
, cloned_task
, ISC_APPEVENT_SHUTDOWN
,
282 action
, arg
, sizeof(*event
));
284 result
= ISC_R_NOMEMORY
;
288 ISC_LIST_APPEND(on_run
, event
, ev_link
);
290 result
= ISC_R_SUCCESS
;
298 #ifndef ISC_PLATFORM_USETHREADS
300 * Event loop for nonthreaded programs.
305 while (!want_shutdown
) {
307 isc_time_t when
, now
;
308 struct timeval tv
, *tvp
;
309 isc_socketwait_t
*swait
;
310 isc_boolean_t readytasks
;
311 isc_boolean_t call_timer_dispatch
= ISC_FALSE
;
313 readytasks
= isc__taskmgr_ready();
318 call_timer_dispatch
= ISC_TRUE
;
320 result
= isc__timermgr_nextevent(&when
);
321 if (result
!= ISC_R_SUCCESS
)
327 us
= isc_time_microdiff(&when
, &now
);
329 call_timer_dispatch
= ISC_TRUE
;
330 tv
.tv_sec
= us
/ 1000000;
331 tv
.tv_usec
= us
% 1000000;
337 n
= isc__socketmgr_waitevents(tvp
, &swait
);
339 if (n
== 0 || call_timer_dispatch
) {
341 * We call isc__timermgr_dispatch() only when
342 * necessary, in order to reduce overhead. If the
343 * select() call indicates a timeout, we need the
344 * dispatch. Even if not, if we set the 0-timeout
345 * for the select() call, we need to check the timer
346 * events. In the 'readytasks' case, there may be no
347 * timeout event actually, but there is no other way
348 * to reduce the overhead.
349 * Note that we do not have to worry about the case
350 * where a new timer is inserted during the select()
351 * call, since this loop only runs in the non-thread
354 isc__timermgr_dispatch();
357 (void)isc__socketmgr_dispatch(swait
);
358 (void)isc__taskmgr_dispatch();
361 want_reload
= ISC_FALSE
;
362 return (ISC_R_RELOAD
);
365 return (ISC_R_SUCCESS
);
369 * This is a gross hack to support waiting for condition
370 * variables in nonthreaded programs in a limited way;
371 * see lib/isc/nothreads/include/isc/condition.h.
372 * We implement isc_condition_wait() by entering the
373 * event loop recursively until the want_shutdown flag
374 * is set by isc_condition_signal().
378 * \brief True if we are currently executing in the recursive
381 static isc_boolean_t in_recursive_evloop
= ISC_FALSE
;
384 * \brief True if we are exiting the event loop as the result of
385 * a call to isc_condition_signal() rather than a shutdown
388 static isc_boolean_t signalled
= ISC_FALSE
;
391 isc__nothread_wait_hack(isc_condition_t
*cp
, isc_mutex_t
*mp
) {
397 INSIST(!in_recursive_evloop
);
398 in_recursive_evloop
= ISC_TRUE
;
400 INSIST(*mp
== 1); /* Mutex must be locked on entry. */
404 if (result
== ISC_R_RELOAD
)
405 want_reload
= ISC_TRUE
;
407 want_shutdown
= ISC_FALSE
;
408 signalled
= ISC_FALSE
;
412 in_recursive_evloop
= ISC_FALSE
;
413 return (ISC_R_SUCCESS
);
417 isc__nothread_signal_hack(isc_condition_t
*cp
) {
421 INSIST(in_recursive_evloop
);
423 want_shutdown
= ISC_TRUE
;
424 signalled
= ISC_TRUE
;
425 return (ISC_R_SUCCESS
);
428 #endif /* ISC_PLATFORM_USETHREADS */
433 isc_event_t
*event
, *next_event
;
435 #ifdef ISC_PLATFORM_USETHREADS
437 char strbuf
[ISC_STRERRORSIZE
];
441 #endif /* ISC_PLATFORM_USETHREADS */
443 #ifdef HAVE_LINUXTHREADS
444 REQUIRE(main_thread
== pthread_self());
453 * Post any on-run events (in FIFO order).
455 for (event
= ISC_LIST_HEAD(on_run
);
457 event
= next_event
) {
458 next_event
= ISC_LIST_NEXT(event
, ev_link
);
459 ISC_LIST_UNLINK(on_run
, event
, ev_link
);
460 task
= event
->ev_sender
;
461 event
->ev_sender
= NULL
;
462 isc_task_sendanddetach(&task
, &event
);
473 * We do this here to ensure that the signal handler is installed
474 * (i.e. that it wasn't a "one-shot" handler).
476 result
= handle_signal(SIGHUP
, reload_action
);
477 if (result
!= ISC_R_SUCCESS
)
478 return (ISC_R_SUCCESS
);
481 #ifdef ISC_PLATFORM_USETHREADS
483 * There is no danger if isc_app_shutdown() is called before we wait
484 * for signals. Signals are blocked, so any such signal will simply
485 * be made pending and we will get it when we call sigwait().
488 while (!want_shutdown
) {
491 * Wait for SIGHUP, SIGINT, or SIGTERM.
493 if (sigemptyset(&sset
) != 0 ||
494 sigaddset(&sset
, SIGHUP
) != 0 ||
495 sigaddset(&sset
, SIGINT
) != 0 ||
496 sigaddset(&sset
, SIGTERM
) != 0) {
497 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
498 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
499 "isc_app_run() sigsetops: %s", strbuf
);
500 return (ISC_R_UNEXPECTED
);
503 #ifndef HAVE_UNIXWARE_SIGWAIT
504 result
= sigwait(&sset
, &sig
);
508 want_shutdown
= ISC_TRUE
;
509 else if (sig
== SIGHUP
)
510 want_reload
= ISC_TRUE
;
513 #else /* Using UnixWare sigwait semantics. */
514 sig
= sigwait(&sset
);
518 want_shutdown
= ISC_TRUE
;
519 else if (sig
== SIGHUP
)
520 want_reload
= ISC_TRUE
;
523 #endif /* HAVE_UNIXWARE_SIGWAIT */
524 #else /* Don't have sigwait(). */
526 * Listen for all signals.
528 if (sigemptyset(&sset
) != 0) {
529 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
530 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
531 "isc_app_run() sigsetops: %s", strbuf
);
532 return (ISC_R_UNEXPECTED
);
534 result
= sigsuspend(&sset
);
535 #endif /* HAVE_SIGWAIT */
538 want_reload
= ISC_FALSE
;
539 return (ISC_R_RELOAD
);
542 if (want_shutdown
&& blocked
)
546 #else /* ISC_PLATFORM_USETHREADS */
548 (void)isc__taskmgr_dispatch();
551 if (result
!= ISC_R_SUCCESS
)
554 #endif /* ISC_PLATFORM_USETHREADS */
556 return (ISC_R_SUCCESS
);
560 isc_app_shutdown(void) {
561 isc_boolean_t want_kill
= ISC_TRUE
;
562 char strbuf
[ISC_STRERRORSIZE
];
568 if (shutdown_requested
)
569 want_kill
= ISC_FALSE
;
571 shutdown_requested
= ISC_TRUE
;
576 #ifdef HAVE_LINUXTHREADS
579 result
= pthread_kill(main_thread
, SIGTERM
);
581 isc__strerror(result
, strbuf
, sizeof(strbuf
));
582 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
583 "isc_app_shutdown() pthread_kill: %s",
585 return (ISC_R_UNEXPECTED
);
588 if (kill(getpid(), SIGTERM
) < 0) {
589 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
590 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
591 "isc_app_shutdown() kill: %s", strbuf
);
592 return (ISC_R_UNEXPECTED
);
597 return (ISC_R_SUCCESS
);
601 isc_app_reload(void) {
602 isc_boolean_t want_kill
= ISC_TRUE
;
603 char strbuf
[ISC_STRERRORSIZE
];
610 * Don't send the reload signal if we're shutting down.
612 if (shutdown_requested
)
613 want_kill
= ISC_FALSE
;
618 #ifdef HAVE_LINUXTHREADS
621 result
= pthread_kill(main_thread
, SIGHUP
);
623 isc__strerror(result
, strbuf
, sizeof(strbuf
));
624 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
625 "isc_app_reload() pthread_kill: %s",
627 return (ISC_R_UNEXPECTED
);
630 if (kill(getpid(), SIGHUP
) < 0) {
631 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
632 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
633 "isc_app_reload() kill: %s", strbuf
);
634 return (ISC_R_UNEXPECTED
);
639 return (ISC_R_SUCCESS
);
643 isc_app_finish(void) {
648 isc_app_block(void) {
649 #ifdef ISC_PLATFORM_USETHREADS
651 #endif /* ISC_PLATFORM_USETHREADS */
656 #ifdef ISC_PLATFORM_USETHREADS
657 blockedthread
= pthread_self();
658 RUNTIME_CHECK(sigemptyset(&sset
) == 0 &&
659 sigaddset(&sset
, SIGINT
) == 0 &&
660 sigaddset(&sset
, SIGTERM
) == 0);
661 RUNTIME_CHECK(pthread_sigmask(SIG_UNBLOCK
, &sset
, NULL
) == 0);
662 #endif /* ISC_PLATFORM_USETHREADS */
666 isc_app_unblock(void) {
667 #ifdef ISC_PLATFORM_USETHREADS
669 #endif /* ISC_PLATFORM_USETHREADS */
676 #ifdef ISC_PLATFORM_USETHREADS
677 REQUIRE(blockedthread
== pthread_self());
679 RUNTIME_CHECK(sigemptyset(&sset
) == 0 &&
680 sigaddset(&sset
, SIGINT
) == 0 &&
681 sigaddset(&sset
, SIGTERM
) == 0);
682 RUNTIME_CHECK(pthread_sigmask(SIG_BLOCK
, &sset
, NULL
) == 0);
683 #endif /* ISC_PLATFORM_USETHREADS */