app.c revision e8336c458cca9289f34dc5cb58fc0b5769502649
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User * Copyright (C) 1999, 2000 Internet Software Consortium.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * copyright notice and this permission notice appear in all copies.
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
c2bc56dc65b4b103a5600565680eb5f33fa4c90bMark Andrewsstatic isc_boolean_t shutdown_requested = ISC_FALSE;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * We assume that 'want_shutdown' can be read and written atomically.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonstatic isc_boolean_t want_shutdown = ISC_FALSE;
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * We assume that 'want_reload' can be read and written atomically.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Linux has sigwait(), but it appears to prevent signal handlers from
3dfc806ddf433d0569340d9c4ccc5af8ec41a27bBrian Wellington * running, even if they're not in the set being waited for. This makes
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * it impossible to get the default actions for SIGILL, SIGSEGV, etc.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Instead of messing with it, we just use sigsuspend() instead.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * We need to remember which thread is the main thread...
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonhandle_signal(int sig, void (*handler)(int)) {
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * Start an ISC library application.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * BSDI 3.1 seg faults in pthread_sigmask() if we don't do this.
b32e75f3e1822f14681c538d7657bdbccd7fcd45Brian Wellington "isc_app_start() pthread_init: %s",
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Install do-nothing handlers for SIGINT and SIGTERM.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * We install them now because BSDI 3.1 won't block
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * the default actions, regardless of what we do with
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * pthread_sigmask().
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington result = handle_signal(SIGINT, exit_action);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington result = handle_signal(SIGTERM, exit_action);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Always ignore SIGPIPE.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Block SIGHUP, SIGINT, SIGTERM.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * If isc_app_start() is called from the main thread before any other
3a0da183bb40bd120698102b20b61ef12665c09bMark Andrews * threads have been created, then the pthread_sigmask() call below
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * will result in all threads having SIGHUP, SIGINT and SIGTERM
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * blocked by default, ensuring that only the thread that calls
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * sigwait() for them will get those signals.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington "isc_app_start() sigsetops: %s",
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington "isc_app_start() pthread_sigmask: %s",
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellingtonisc_app_onrun(isc_mem_t *mctx, isc_task_t *task, isc_taskaction_t action,
32d248107a5bc92b4bf9fc77deaa55b3da969ba2Andreas Gustafsson * Request delivery of an event when the application is run.
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington * Note that we store the task to which we're going to send the event
4423c99613db1399dbb5c51e86ef0d351a1418c2Mark Andrews * in the event's "sender" field.
ed3418751ebdf7de397df76753dae97851d2bdf9Brian Wellington event = isc_event_allocate(mctx, cloned_task, ISC_APPEVENT_SHUTDOWN,
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Run an ISC library application.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Post any on-run events (in FIFO order).
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington next_event = ISC_LIST_NEXT(event, ev_link);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Catch SIGHUP.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * We do this here to ensure that the signal handler is installed
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * (i.e. that it wasn't a "one-shot" handler).
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington result = handle_signal(SIGHUP, reload_action);
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * There is no danger if isc_app_shutdown() is called before we wait
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * for signals. Signals are blocked, so any such signal will simply
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington * be made pending and we will get it when we call sigwait().
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Wait for SIGHUP, SIGINT, or SIGTERM.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington "isc_app_run() sigsetops: %s",
091329e690b20755aa80b86cc7389d25c5d32c9bBrian Wellington#else /* Using UnixWare sigwait semantics. */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#endif /* HAVE_UNIXWARE_SIGWAIT */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#else /* Don't have sigwait(). */
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Listen for all signals.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington "isc_app_run() sigsetops: %s",
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington#endif /* HAVE_SIGWAIT */
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * Request application shutdown.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington result = pthread_kill(main_thread, SIGTERM);
c2da4f9d8a153ffeb2b659541130abef2d586789Brian Wellington "isc_app_shutdown() pthread_kill: %s",
c2da4f9d8a153ffeb2b659541130abef2d586789Brian Wellington "isc_app_shutdown() kill: %s",
c2da4f9d8a153ffeb2b659541130abef2d586789Brian Wellington * Request application reload.
1b4e6163bed546ca7f8ad186f3eabfebacc36bc1Brian Wellington * Don't send the reload signal if we're shutting down.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews "isc_app_shutdown() pthread_kill: %s",
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews "isc_app_shutdown() kill: %s",
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * Finish an ISC library application.