From 2f72156e05f812b4dcc41111ca8e4f89d71c9211 Mon Sep 17 00:00:00 2001 From: Colin Patrick McCabe Date: Wed, 19 Jan 2011 09:15:02 -0800 Subject: [PATCH] common: move signal handler stuff into signal.cc Signed-off-by: Colin McCabe --- src/Makefile.am | 2 + src/common/BackTrace.h | 1 + src/common/common_init.cc | 2 + src/common/signal.cc | 94 +++++++++++++++++++++++++++++++++++++++ src/common/signal.h | 20 +++++++++ src/config.cc | 62 -------------------------- 6 files changed, 119 insertions(+), 62 deletions(-) create mode 100644 src/common/signal.cc create mode 100644 src/common/signal.h diff --git a/src/Makefile.am b/src/Makefile.am index 470f57ba5c58e..c8fb07bc292b5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -486,6 +486,7 @@ libcommon_files = \ common/common_init.cc \ common/buffer.cc \ common/debug.cc \ + common/signal.cc \ include/ceph_fs.cc \ include/ceph_hash.cc \ include/ceph_strings.cc \ @@ -633,6 +634,7 @@ noinst_HEADERS = \ common/arch.h\ common/armor.h\ common/common_init.h\ + common/signal.h\ common/dyn_snprintf.h\ common/tls.h\ config.h\ diff --git a/src/common/BackTrace.h b/src/common/BackTrace.h index 9e0c020807a03..a7a3b6e33c751 100644 --- a/src/common/BackTrace.h +++ b/src/common/BackTrace.h @@ -1,6 +1,7 @@ #ifndef CEPH_BACKTRACE_H #define CEPH_BACKTRACE_H +#include #include #include diff --git a/src/common/common_init.cc b/src/common/common_init.cc index 71455089baf0f..fc4736b61f320 100644 --- a/src/common/common_init.cc +++ b/src/common/common_init.cc @@ -16,6 +16,7 @@ #include "auth/KeyRing.h" #include "config.h" #include "common/errno.h" +#include "common/signal.h" #include "include/color.h" #include "tls.h" @@ -119,6 +120,7 @@ void common_init(std::vector& args, const char *module_type, bool i parse_startup_config_options(args, module_type); parse_config_options(args); + install_standard_sighandlers(); #ifdef HAVE_LIBTCMALLOC if (g_conf.tcmalloc_profiler_run && g_conf.tcmalloc_have) { diff --git a/src/common/signal.cc b/src/common/signal.cc new file mode 100644 index 0000000000000..973e3f1a2e6ad --- /dev/null +++ b/src/common/signal.cc @@ -0,0 +1,94 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "common/BackTrace.h" +#include "common/DoutStreambuf.h" +#include "common/Logger.h" +#include "common/debug.h" + +#include +#include +#include +#include +#include + +typedef void (*signal_handler_t)(int); + +static void install_sighandler(int signum, signal_handler_t handler, int flags) +{ + int ret; + struct sigaction oldact; + struct sigaction act; + memset(&act, 0, sizeof(act)); + + act.sa_handler = handler; + sigemptyset(&act.sa_mask); + act.sa_flags = flags; + + ret = sigaction(signum, &act, &oldact); + if (ret != 0) { + *_dout << "install_sighandler: sigaction returned " << ret << " when " + << "trying to install a signal handler for " + << sys_siglist[signum] << "\n"; + exit(1); + } +} + +static void sighup_handler(int signum) +{ + // All this does is set a few bits telling us to re-open our logfiles and + // restart our central logging service. + _dout_need_open = true; + logger_reopen_all(); +} + +static void handle_fatal_signal(int signum) +{ + // This code may itself trigger a SIGSEGV if the heap is corrupt. In that + // case, SA_RESETHAND specifies that the default signal handler-- + // presumably dump core-- will handle it. + *_dout << "*** Caught signal (" << sys_siglist[signum] << ") ***" + << std::endl; + *_dout << "in thread " << std::hex << pthread_self() << std::endl; + BackTrace bt(0); + bt.print(*_dout); + _dout->flush(); + + // Use default handler to dump core + int ret = raise(signum); + + // Normally, we won't get here. If we do, something is very weird. + if (ret) { + *_dout << "handle_fatal_signal: failed to re-raise signal " << signum + << std::endl; + } + else { + *_dout << "handle_fatal_signal: default handler for signal " << signum + << " didn't terminate the process?" << std::endl; + } + exit(1); +} + +void install_standard_sighandlers(void) +{ + install_sighandler(SIGHUP, sighup_handler, SA_RESTART); + install_sighandler(SIGSEGV, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); + install_sighandler(SIGABRT, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); + install_sighandler(SIGBUS, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); + install_sighandler(SIGILL, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); + install_sighandler(SIGFPE, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); + install_sighandler(SIGXCPU, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); + install_sighandler(SIGXFSZ, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); + install_sighandler(SIGSYS, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); +} diff --git a/src/common/signal.h b/src/common/signal.h new file mode 100644 index 0000000000000..a58fcd7a1d618 --- /dev/null +++ b/src/common/signal.h @@ -0,0 +1,20 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef CEPH_COMMON_SIGNAL_H +#define CEPH_COMMON_SIGNAL_H + +void install_standard_sighandlers(void); + +#endif diff --git a/src/config.cc b/src/config.cc index 4ef50af7806cc..0cec10266b715 100644 --- a/src/config.cc +++ b/src/config.cc @@ -224,56 +224,6 @@ void parse_config_option_string(string& s) parse_config_options(nargs); } -typedef void (*signal_handler_t)(int); - -static void install_sighandler(int signum, signal_handler_t handler, int flags) -{ - int ret; - struct sigaction oldact; - struct sigaction act; - memset(&act, 0, sizeof(act)); - - act.sa_handler = handler; - sigemptyset(&act.sa_mask); - act.sa_flags = flags; - - ret = sigaction(signum, &act, &oldact); - if (ret != 0) { - fprintf(stderr, "failed to install signal handler for signal %d\n", signum); - exit(1); - } -} - -void sighup_handler(int signum) -{ - _dout_need_open = true; - logger_reopen_all(); -} - -void handle_fatal_signal(int signum) -{ - *_dout << "*** Caught signal (" << sys_siglist[signum] << ") ***" - << std::endl; - *_dout << "in thread " << std::hex << pthread_self() << std::endl; - BackTrace bt(0); - bt.print(*_dout); - _dout->flush(); - - // Use default handler to dump core - int ret = raise(signum); - - // Normally, we won't get here. If we do, something is very weird. - if (ret) { - *_dout << "handle_fatal_signal: failed to re-raise signal " << signum - << std::endl; - } - else { - *_dout << "handle_fatal_signal: default handler for signal " << signum - << " didn't terminate the process?" << std::endl; - } - exit(1); -} - #define _STR(x) #x #define STRINGIFY(x) _STR(x) @@ -1331,19 +1281,7 @@ void parse_config_options(std::vector& args) nargs.push_back(args[i]); } - env_override(&g_conf.keyring, "CEPH_KEYRING"); - - install_sighandler(SIGHUP, sighup_handler, SA_RESTART); - install_sighandler(SIGSEGV, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); - install_sighandler(SIGABRT, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); - install_sighandler(SIGBUS, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); - install_sighandler(SIGILL, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); - install_sighandler(SIGFPE, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); - install_sighandler(SIGXCPU, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); - install_sighandler(SIGXFSZ, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); - install_sighandler(SIGSYS, handle_fatal_signal, SA_RESETHAND | SA_NODEFER); - args = nargs; } -- 2.39.5