From 3431b2cee2ad71c0046bcef239ddd30539c202ae Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 15 Jul 2009 15:12:16 +0200 Subject: Restore /proc/sys/kernel/core_pattern on error exit. The bug was observed when dbus-abrt.conf is missing. Signed-off-by: Denys Vlasenko --- src/Daemon/Daemon.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/Daemon/Daemon.cpp') diff --git a/src/Daemon/Daemon.cpp b/src/Daemon/Daemon.cpp index 94f5e66..ea769c5 100644 --- a/src/Daemon/Daemon.cpp +++ b/src/Daemon/Daemon.cpp @@ -21,6 +21,9 @@ #include "ABRTException.h" #include #include +#include +#include +#include CCrashWatcher *g_pCrashWatcher = NULL; @@ -54,7 +57,15 @@ int main(int argc, char** argv) } if(daemonize) { - // forking to background + /* Open stdin to /dev/null. We do it before forking + * in order to emit useful exitcode to the parent + * if open fails */ + close(STDIN_FILENO); + if (open("/dev/null", O_RDWR)) + { + throw CABRTException(EXCEP_FATAL, "Can't open /dev/null"); + } + /* forking to background */ pid_t pid = fork(); if (pid < 0) { @@ -68,9 +79,12 @@ int main(int argc, char** argv) { throw CABRTException(EXCEP_FATAL, "CCrashWatcher::Daemonize(): setsid failed"); } - close(STDIN_FILENO); + /* We must not leave fds 0,1,2 closed. + * Otherwise fprintf(stderr) dumps messages into random fds, etc. */ close(STDOUT_FILENO); close(STDERR_FILENO); + dup(0); + dup(0); } g_pCrashWatcher = new CCrashWatcher(DEBUG_DUMPS_DIR); g_pCrashWatcher->Run(); @@ -83,5 +97,7 @@ int main(int argc, char** argv) { std::cerr << "Cannot create daemon: " << e.what() << std::endl; } + //do we need this? delete g_pCrashWatcher; + return 1; /* Any exit is a failure. Normally we don't exit at all */ } -- cgit 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 From db9ffc6dafce568e0b70d064411db81933d61eb4 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 17 Jul 2009 15:58:51 +0200 Subject: On exit, take care to emit consistent exit status. If we were Ctrl-C'ed, then we should exit by killing ourself, not exit(N). Signed-off-by: Denys Vlasenko --- src/Daemon/Daemon.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'src/Daemon/Daemon.cpp') diff --git a/src/Daemon/Daemon.cpp b/src/Daemon/Daemon.cpp index c28331f..dff8de6 100644 --- a/src/Daemon/Daemon.cpp +++ b/src/Daemon/Daemon.cpp @@ -42,13 +42,14 @@ void print_help() int main(int argc, char** argv) { int daemonize = 1; - /*signal handlers */ + + /* signal handlers */ signal(SIGTERM, handle_fatal_signal); signal(SIGINT, handle_fatal_signal); + try { - - if (argc > 1) + if (argv[1]) { if (strcmp(argv[1], "-d") == 0) { @@ -97,7 +98,14 @@ int main(int argc, char** argv) { std::cerr << "Cannot create daemon: " << e.what() << std::endl; } + delete g_pCrashWatcher; - return 1; /* Any exit is a failure. Normally we don't exit at all */ -} + /* Take care to emit correct exit status */ + if (sig_caught) { + signal(sig_caught, SIG_DFL); + raise(sig_caught); + } + /* I think we never end up here */ + return 0; +} -- cgit