From 37ab187408799ba3f3f9107bdc5a72fea0b4b608 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 17 Jul 2009 15:52:27 +0200 Subject: rework unsafe handling of SIGINT/SIGTERM Signals are asynchronous. It is unsafe to perform such complex operations in a signal handler. I changed signal handler to just set a flag, and added an event source which returns an event when this variable is set. The action is to stop event loop. Execution then falls through to program exit. Signed-off-by: Denys Vlasenko --- src/Daemon/Daemon.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/Daemon/Daemon.cpp') diff --git a/src/Daemon/Daemon.cpp b/src/Daemon/Daemon.cpp index ea769c5..c28331f 100644 --- a/src/Daemon/Daemon.cpp +++ b/src/Daemon/Daemon.cpp @@ -25,15 +25,15 @@ #include #include -CCrashWatcher *g_pCrashWatcher = NULL; +uint8_t sig_caught; -void terminate(int signal) +static void handle_fatal_signal(int signal) { - fprintf(stderr, "Got SIGINT/SIGTERM, cleaning up..\n"); - delete g_pCrashWatcher; - exit(0); + sig_caught = signal; } +CCrashWatcher *g_pCrashWatcher = NULL; + void print_help() { @@ -43,8 +43,8 @@ int main(int argc, char** argv) { int daemonize = 1; /*signal handlers */ - signal(SIGTERM, terminate); - signal(SIGINT, terminate); + signal(SIGTERM, handle_fatal_signal); + signal(SIGINT, handle_fatal_signal); try { @@ -97,7 +97,7 @@ int main(int argc, char** argv) { std::cerr << "Cannot create daemon: " << e.what() << std::endl; } - //do we need this? delete g_pCrashWatcher; + delete g_pCrashWatcher; return 1; /* Any exit is a failure. Normally we don't exit at all */ } -- cgit