]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Catch most exceptions in main() to report exceptions uncaught by Squid. This
authorAlex Rousskov <rousskov@measurement-factory.com>
Tue, 30 Sep 2008 17:28:53 +0000 (11:28 -0600)
committerAlex Rousskov <rousskov@measurement-factory.com>
Tue, 30 Sep 2008 17:28:53 +0000 (11:28 -0600)
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.

src/main.cc

index 6eaea0acded0a3300a63323fd7d65e022ea3e480..6d6f4f2e54661b8dc7c71f090cf82890b162c843 100644 (file)
@@ -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_