[Libreoffice-commits] online.git: loolwsd/Common.hpp loolwsd/LOOLBroker.cpp loolwsd/LOOLKit.cpp loolwsd/LOOLWSD.cpp loolwsd/LOOLWSD.hpp loolwsd/Util.cpp loolwsd/Util.hpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Sun Jan 10 20:07:14 PST 2016


 loolwsd/Common.hpp     |    4 ----
 loolwsd/LOOLBroker.cpp |    4 +---
 loolwsd/LOOLKit.cpp    |   29 +----------------------------
 loolwsd/LOOLWSD.cpp    |   26 ++------------------------
 loolwsd/LOOLWSD.hpp    |    3 ---
 loolwsd/Util.cpp       |   39 +++++++++++++++++++++++++++++++++++++++
 loolwsd/Util.hpp       |   10 ++++++++++
 7 files changed, 53 insertions(+), 62 deletions(-)

New commits:
commit 5bf75f56ca88f96d9d8a47c788207cb2a742a556
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sat Jan 9 13:51:55 2016 -0500

    loolwsd: centralized signal handling and eased debugging
    
    Change-Id: Ifdb3e2bca540bec3c6dea1ab24173c8ea0267706
    Reviewed-on: https://gerrit.libreoffice.org/21325
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/Common.hpp b/loolwsd/Common.hpp
index 1be1a89..10c3d7e 100644
--- a/loolwsd/Common.hpp
+++ b/loolwsd/Common.hpp
@@ -13,7 +13,6 @@
 
 #include <string>
 
-enum class LOOLState { LOOL_STARTING, LOOL_STOPPING, LOOL_ABNORMAL };
 constexpr int DEFAULT_CLIENT_PORT_NUMBER = 9980;
 constexpr int MASTER_PORT_NUMBER = 9981;
 constexpr int INTERVAL_PROBES = 10;
@@ -25,8 +24,5 @@ constexpr int PIPE_BUFFER = 1024;
 
 static const std::string JailedDocumentRoot = "/user/docs/";
 
-// Flag to stop pump loops.
-static volatile bool TerminationFlag = false;
-
 #endif
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/LOOLBroker.cpp b/loolwsd/LOOLBroker.cpp
index a63899b..515aa90 100644
--- a/loolwsd/LOOLBroker.cpp
+++ b/loolwsd/LOOLBroker.cpp
@@ -612,9 +612,7 @@ int main(int argc, char** argv)
     // Initialization
     Log::initialize("brk");
 
-#ifdef __linux
-    setSignals(false);
-#endif
+    Util::setSignals(false);
 
     std::string childRoot;
     std::string jailId;
diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp
index 7c9f86e..0d806eb 100644
--- a/loolwsd/LOOLKit.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -60,33 +60,6 @@ using Poco::FastMutex;
 
 const std::string CHILD_URI = "/loolws/child/";
 const std::string LOKIT_BROKER = "/tmp/loolbroker.fifo";
-static LOOLState TerminationState;
-
-namespace
-{
-    void handleSignal(int aSignal)
-    {
-        Log::info() << "Signal received: " << strsignal(aSignal) << Log::end;
-        TerminationState = ( aSignal == SIGTERM ? LOOLState::LOOL_ABNORMAL : LOOLState::LOOL_STOPPING );
-        TerminationFlag = true;
-    }
-
-    void setSignals(bool isIgnored)
-    {
-#ifdef __linux
-        struct sigaction aSigAction;
-
-        sigemptyset(&aSigAction.sa_mask);
-        aSigAction.sa_flags = 0;
-        aSigAction.sa_handler = (isIgnored ? SIG_IGN : handleSignal);
-
-        sigaction(SIGTERM, &aSigAction, nullptr);
-        sigaction(SIGINT, &aSigAction, nullptr);
-        sigaction(SIGQUIT, &aSigAction, nullptr);
-        sigaction(SIGHUP, &aSigAction, nullptr);
-#endif
-    }
-}
 
 // This thread handles callbacks from the
 // lokit instance.
@@ -714,7 +687,7 @@ void lokit_main(const std::string &loSubPath, const std::string& jailId, const s
 #ifdef __linux
     if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(process_name.c_str()), 0, 0, 0) != 0)
         Log::error("Cannot set process name to " + process_name + ".");
-    setSignals(false);
+    Util::setSignals(false);
 #endif
     Log::debug("Process [" + process_name + "] started.");
 
diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp
index 1d1a17b..71e9915 100644
--- a/loolwsd/LOOLWSD.cpp
+++ b/loolwsd/LOOLWSD.cpp
@@ -663,28 +663,6 @@ LOOLWSD::~LOOLWSD()
 {
 }
 
-void LOOLWSD::handleSignal(int aSignal)
-{
-    Log::info() << "Signal received: " << strsignal(aSignal) << Log::end;
-    TerminationFlag = true;
-}
-
-void LOOLWSD::setSignals(bool isIgnored)
-{
-#ifdef __linux
-    struct sigaction aSigAction;
-
-    sigemptyset(&aSigAction.sa_mask);
-    aSigAction.sa_flags = 0;
-    aSigAction.sa_handler = (isIgnored ? SIG_IGN : handleSignal);
-
-    sigaction(SIGTERM, &aSigAction, nullptr);
-    sigaction(SIGINT, &aSigAction, nullptr);
-    sigaction(SIGQUIT, &aSigAction, nullptr);
-    sigaction(SIGHUP, &aSigAction, nullptr);
-#endif
-}
-
 void LOOLWSD::initialize(Application& self)
 {
     ServerApplication::initialize(self);
@@ -829,10 +807,10 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
     char *locale = setlocale(LC_ALL, nullptr);
     if (locale == nullptr || std::strcmp(locale, "C") == 0)
         setlocale(LC_ALL, "en_US.utf8");
-
-    setSignals(false);
 #endif
 
+    Util::setSignals(false);
+
     if (access(cache.c_str(), R_OK | W_OK | X_OK) != 0)
     {
         Log::error("Unable to access cache [" + cache +
diff --git a/loolwsd/LOOLWSD.hpp b/loolwsd/LOOLWSD.hpp
index 4108775..50bde75 100644
--- a/loolwsd/LOOLWSD.hpp
+++ b/loolwsd/LOOLWSD.hpp
@@ -176,9 +176,6 @@ public:
     }
 
 protected:
-    static void setSignals(bool bIgnore);
-    static void handleSignal(int nSignal);
-
     void initialize(Poco::Util::Application& self) override;
     void uninitialize() override;
     void defineOptions(Poco::Util::OptionSet& options) override;
diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp
index d474e4d..fbe9c2d 100644
--- a/loolwsd/Util.cpp
+++ b/loolwsd/Util.cpp
@@ -35,6 +35,7 @@
 #include <Poco/Environment.h>
 #include <Poco/ConsoleChannel.h>
 
+#include <Common.hpp>
 #include "Util.hpp"
 #include "Png.hpp"
 
@@ -59,6 +60,9 @@ extern "C"
     }
 }
 
+volatile LOOLState TerminationState = LOOLState::LOOL_RUNNING;
+volatile bool TerminationFlag = false;
+
 namespace Util
 {
 namespace rng
@@ -389,6 +393,41 @@ namespace Util
     ErrorPoll:
         return nBytes;
     }
+
+    static
+    void handleSignal(int aSignal)
+    {
+        Log::info() << "Signal received: " << strsignal(aSignal) << Log::end;
+        TerminationFlag = true;
+        TerminationState = ( aSignal == SIGTERM ? LOOLState::LOOL_ABNORMAL : LOOLState::LOOL_STOPPING );
+
+        if (aSignal == SIGSEGV || aSignal == SIGBUS)
+        {
+            Log::error() << "\nSegfault! Stalling for 10 seconds to attach debugger. Use:\n"
+                         << "sudo gdb --pid=" << Poco::Process::id() << "\n or \n"
+                         << "sudo gdb --q --n --ex 'thread apply all backtrace full' --batch --pid="
+                         << Poco::Process::id() << "\n" << Log::end;
+            sleep(10);
+        }
+    }
+
+    void setSignals(bool isIgnored)
+    {
+#ifdef __linux
+        struct sigaction aSigAction;
+
+        sigemptyset(&aSigAction.sa_mask);
+        aSigAction.sa_flags = 0;
+        aSigAction.sa_handler = (isIgnored ? SIG_IGN : handleSignal);
+
+        sigaction(SIGTERM, &aSigAction, nullptr);
+        sigaction(SIGINT, &aSigAction, nullptr);
+        sigaction(SIGQUIT, &aSigAction, nullptr);
+        sigaction(SIGHUP, &aSigAction, nullptr);
+        sigaction(SIGBUS, &aSigAction, nullptr);
+        sigaction(SIGSEGV, &aSigAction, nullptr);
+#endif
+    }
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp
index 95dd5ef..b363505 100644
--- a/loolwsd/Util.hpp
+++ b/loolwsd/Util.hpp
@@ -22,6 +22,13 @@
 #define LOK_USE_UNSTABLE_API
 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
 
+// Possible states of LOOL processes.
+enum class LOOLState { LOOL_RUNNING, LOOL_STOPPING, LOOL_ABNORMAL };
+extern volatile LOOLState TerminationState;
+
+/// Flag to stop pump loops.
+extern volatile bool TerminationFlag;
+
 namespace Util
 {
     namespace rng
@@ -76,6 +83,9 @@ namespace Util
     {
         removeFile(path.toString(), recursive);
     }
+
+    /// Trap signals to cleanup and exit the process gracefully.
+    void setSignals(bool isIgnored);
 };
 
 //TODO: Move to own file.


More information about the Libreoffice-commits mailing list