From: Alex Rousskov Date: Tue, 30 Sep 2008 17:28:53 +0000 (-0600) Subject: Catch most exceptions in main() to report exceptions uncaught by Squid. This X-Git-Tag: SQUID_3_1_0_1~45^2~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c842329da28b9188b46053babfb2cf6cc3ceba59;p=thirdparty%2Fsquid.git Catch most exceptions in main() to report exceptions uncaught by Squid. This is for last resort reporting only -- the program would exit anyway (usually with less information) if we did not catch these. The code re-throws caught exceptions to reduce side effects of catching it, just in case. May need more work depending on how compilers handle rethrowing. These changes were inspired by eCAP. --- diff --git a/src/main.cc b/src/main.cc index 6eaea0acde..6d6f4f2e54 100644 --- a/src/main.cc +++ b/src/main.cc @@ -69,6 +69,7 @@ #include "forward.h" #include "MemPool.h" #include "ICMPSquid.h" +#include "TextException.h" #if USE_LOADABLE_MODULES #include "LoadableModules.h" @@ -1082,20 +1083,42 @@ mainInitialize(void) configured_once = 1; } +/// unsafe main routine -- may throw +static int SquidMain(int argc, char **argv); +/// unsafe main routine wrapper to catch exceptions +static int SquidMainSafe(int argc, char **argv); + #if USE_WIN32_SERVICE /* When USE_WIN32_SERVICE is defined, the main function is placed in win32.cc */ extern "C" void WINAPI SquidWinSvcMain(int argc, char **argv) -{ - SquidMain(argc, argv); -} - -int -SquidMain(int argc, char **argv) #else int main(int argc, char **argv) #endif +{ + SquidMainSafe(argc, argv); +} + +static int +SquidMainSafe(int argc, char **argv) +{ + try { + return SquidMain(argc, argv); + } + catch (const std::exception &e) { + std::cerr << "dying from an unhandled exception: " << e.what() << std::endl; + throw; + } + catch (...) { + std::cerr << "dying from an unhandled exception." << std::endl; + throw; + } + return -1; // not reached +} + +static int +SquidMain(int argc, char **argv) { #ifdef _SQUID_WIN32_