[Libreoffice-commits] online.git: net/Ssl.cpp net/Ssl.hpp wsd/LOOLWSD.cpp wsd/LOOLWSD.hpp

Michael Meeks michael.meeks at collabora.com
Mon Mar 27 19:53:41 UTC 2017


 net/Ssl.cpp     |    8 +++++++-
 net/Ssl.hpp     |   16 ++++------------
 wsd/LOOLWSD.cpp |   23 +++++++++++++++++++++--
 wsd/LOOLWSD.hpp |    4 ++++
 4 files changed, 36 insertions(+), 15 deletions(-)

New commits:
commit e26079eecb78947f2394fcad8092483890946773
Author: Michael Meeks <michael.meeks at collabora.com>
Date:   Mon Mar 27 20:14:16 2017 +0100

    tdf#106797 - avoid locking up / crashing on exceptions from main.

diff --git a/net/Ssl.cpp b/net/Ssl.cpp
index 6e266c70..74f606cf 100644
--- a/net/Ssl.cpp
+++ b/net/Ssl.cpp
@@ -9,6 +9,7 @@
 
 #include "config.h"
 
+#include <assert.h>
 #include "Ssl.hpp"
 
 #include <sys/syscall.h>
@@ -25,7 +26,6 @@ extern "C"
     };
 }
 
-std::atomic<int> SslContext::RefCount(0);
 std::unique_ptr<SslContext> SslContext::Instance;
 std::vector<std::unique_ptr<std::mutex>> SslContext::Mutexes;
 
@@ -132,6 +132,12 @@ SslContext::~SslContext()
     CONF_modules_free();
 }
 
+void SslContext::uninitialize()
+{
+    assert (Instance);
+    Instance.reset();
+}
+
 void SslContext::lock(int mode, int n, const char* /*file*/, int /*line*/)
 {
     if (mode & CRYPTO_LOCK)
diff --git a/net/Ssl.hpp b/net/Ssl.hpp
index 2c1cabd7..7c13474a 100644
--- a/net/Ssl.hpp
+++ b/net/Ssl.hpp
@@ -31,19 +31,11 @@ public:
                            const std::string& keyFilePath,
                            const std::string& caFilePath)
     {
-        if (++RefCount == 1)
-        {
-            Instance.reset(new SslContext(certFilePath, keyFilePath, caFilePath));
-        }
+        assert (!Instance);
+        Instance.reset(new SslContext(certFilePath, keyFilePath, caFilePath));
     }
 
-    static void uninitialize()
-    {
-        if (--RefCount == 0)
-        {
-            Instance.reset();
-        }
-    }
+    static void uninitialize();
 
     static SSL* newSsl()
     {
@@ -59,6 +51,7 @@ private:
 
     void initDH();
     void initECDH();
+    void shutdown();
 
     std::string getLastErrorMsg();
 
@@ -71,7 +64,6 @@ private:
     static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line);
 
 private:
-    static std::atomic<int> RefCount;
     static std::unique_ptr<SslContext> Instance;
     static std::vector<std::unique_ptr<std::mutex>> Mutexes;
 
diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp
index bdad1aaf..4e2c923a 100644
--- a/wsd/LOOLWSD.cpp
+++ b/wsd/LOOLWSD.cpp
@@ -2343,7 +2343,7 @@ bool LOOLWSD::handleShutdownRequest()
     return false;
 }
 
-int LOOLWSD::main(const std::vector<std::string>& /*args*/)
+int LOOLWSD::innerMain()
 {
 #ifndef FUZZER
     SigUtil::setUserSignals();
@@ -2425,6 +2425,7 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
         {
             const auto msg = "Failed to fork child processes.";
             LOG_FTL(msg);
+            std::cerr << msg << std::endl;
             throw std::runtime_error(msg);
         }
 
@@ -2519,7 +2520,11 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
         LOG_INF("Removing jail [" << path << "].");
         FileUtil::removeFile(path, true);
     }
+    return Application::EXIT_OK;
+}
 
+void LOOLWSD::cleanup()
+{
     // Finally, we no longer need SSL.
     if (LOOLWSD::isSSLEnabled())
     {
@@ -2529,14 +2534,28 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
         SslContext::uninitialize();
 #endif
     }
+}
+
+int LOOLWSD::main(const std::vector<std::string>& /*args*/)
+{
+    int returnValue;
+
+    try {
+        returnValue = innerMain();
+    } catch (...) {
+        cleanup();
+        throw;
+    }
+
+    cleanup();
 
-    int returnValue = Application::EXIT_OK;
     UnitWSD::get().returnValue(returnValue);
 
     LOG_INF("Process [loolwsd] finished.");
     return returnValue;
 }
 
+
 void UnitWSD::testHandleRequest(TestRequest type, UnitHTTPServerRequest& /* request */, UnitHTTPServerResponse& /* response */)
 {
     switch (type)
diff --git a/wsd/LOOLWSD.hpp b/wsd/LOOLWSD.hpp
index 94ff1475..2461a0d5 100644
--- a/wsd/LOOLWSD.hpp
+++ b/wsd/LOOLWSD.hpp
@@ -103,8 +103,12 @@ protected:
     void initialize(Poco::Util::Application& self) override;
     void defineOptions(Poco::Util::OptionSet& options) override;
     void handleOption(const std::string& name, const std::string& value) override;
+    int innerMain();
     int main(const std::vector<std::string>& args) override;
 
+    /// Handle various global static destructors.
+    void cleanup();
+
 private:
     static Util::RuntimeConstant<bool> SSLEnabled;
     static Util::RuntimeConstant<bool> SSLTermination;


More information about the Libreoffice-commits mailing list