Properly handle unlimited IRC reconnection retries
[quassel.git] / src / common / quassel.cpp
index e804bcc..a43a707 100644 (file)
@@ -22,6 +22,9 @@
 
 #include <iostream>
 #include <signal.h>
+#if !defined Q_OS_WIN32 && !defined Q_OS_MAC
+#  include <sys/resource.h>
+#endif
 
 #include <QCoreApplication>
 #include <QDateTime>
@@ -46,8 +49,12 @@ QStringList Quassel::_dataDirPaths;
 bool Quassel::_initialized = false;
 bool Quassel::DEBUG = false;
 QString Quassel::_coreDumpFileName;
+Quassel *Quassel::_instance = 0;
 
 Quassel::Quassel() {
+  Q_ASSERT(!_instance);
+  _instance = this;
+
   // We catch SIGTERM and SIGINT (caused by Ctrl+C) to graceful shutdown Quassel.
   signal(SIGTERM, handleSignal);
   signal(SIGINT, handleSignal);
@@ -55,11 +62,22 @@ Quassel::Quassel() {
   // we have crashhandler for win32 and unix (based on execinfo).
   // on mac os we use it's integrated backtrace generator
 #if defined(Q_OS_WIN32) || (defined(HAVE_EXECINFO) && !defined(Q_OS_MAC))
-  signal(SIGABRT, handleSignal);
-  signal(SIGSEGV, handleSignal);
-#  ifndef Q_OS_WIN32
-  signal(SIGBUS, handleSignal);
-#  endif
+
+# ifndef Q_OS_WIN32
+  // we only handle crashes ourselves if coredumps are disabled
+  struct rlimit *limit = (rlimit *) malloc(sizeof(struct rlimit));
+  int rc = getrlimit(RLIMIT_CORE, limit);
+
+  if(rc == -1 || !((long)limit->rlim_cur > 0 || limit->rlim_cur == RLIM_INFINITY)) {
+# endif
+    signal(SIGABRT, handleSignal);
+    signal(SIGSEGV, handleSignal);
+#   ifndef Q_OS_WIN32
+    signal(SIGBUS, handleSignal);
+  }
+  free(limit);
+#   endif
+
 #endif
 }
 
@@ -86,7 +104,7 @@ bool Quassel::init() {
   }
 
   if(isOptionSet("version")) {
-    std::cerr << qPrintable("Quassel IRC: " + Quassel::buildInfo().plainVersionString) << std::endl;
+    std::cout << qPrintable("Quassel IRC: " + Quassel::buildInfo().plainVersionString) << std::endl;
     return false;
   }
 
@@ -94,6 +112,10 @@ bool Quassel::init() {
   return true;
 }
 
+void Quassel::quit() {
+  QCoreApplication::quit();
+}
+
 //! Register our custom types with Qt's Meta Object System.
 /**  This makes them available for QVariant and in signals/slots, among other things.
 *
@@ -193,7 +215,10 @@ void Quassel::handleSignal(int sig) {
   case SIGTERM:
   case SIGINT:
     qWarning("%s", qPrintable(QString("Caught signal %1 - exiting.").arg(sig)));
-    QCoreApplication::quit();
+    if(_instance)
+      _instance->quit();
+    else
+      QCoreApplication::quit();
     break;
   case SIGABRT:
   case SIGSEGV: