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

2016-10-16 Thread Ashod Nakashian
 loolwsd/LOOLKit.cpp |7 +++
 loolwsd/Util.cpp|3 +++
 loolwsd/Util.hpp|5 +
 3 files changed, 11 insertions(+), 4 deletions(-)

New commits:
commit 57c5e55a4dd589216493cd4d88a71fb24e784d02
Author: Ashod Nakashian 
Date:   Fri Oct 14 23:10:03 2016 -0400

loolwsd: trap signal handler to prevent premature exit

When upon cleanup we segfault we want to avoid
forcible exit using _Exit(). This was done by
an unconditional wait (sleep).

This patch changes that mechanism into a latch
(mutex) that traps the exit when the sig handler
is invoked, therby preventing exit as long as
necessary for the sighandler to finish.

Change-Id: Ifc8e972be12645a1b310d4cb4e3a4172afc31327
Reviewed-on: https://gerrit.libreoffice.org/29945
Reviewed-by: Ashod Nakashian 
Tested-by: Ashod Nakashian 

diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp
index 94e5a5a..0ddcac5 100644
--- a/loolwsd/LOOLKit.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -1561,11 +1561,10 @@ void lokit_main(const std::string& childRoot,
 Log::error(std::string("Exception: ") + exc.what());
 }
 
-// Sleep a second here in case we get a fatal signal just when about to 
finish up, which sadly
-// seems to happen often, so that handleFatalSignal() in Util.cpp has time 
to produce a
-// backtrace.
-sleep(1);
+// Trap the signal handler, if invoked,
+// to prevent exiting.
 Log::info("Process finished.");
+std::unique_lock lock(SigHandlerTrap);
 std::_Exit(Application::EXIT_OK);
 }
 #endif
diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp
index c367630..1f53d7e 100644
--- a/loolwsd/Util.cpp
+++ b/loolwsd/Util.cpp
@@ -50,6 +50,7 @@
 #include "Util.hpp"
 
 std::atomic TerminationFlag(false);
+std::mutex SigHandlerTrap;
 
 namespace Util
 {
@@ -393,6 +394,8 @@ namespace Util
 static
 void handleFatalSignal(const int signal)
 {
+std::unique_lock lock(SigHandlerTrap);
+
 Log::signalLogPrefix();
 Log::signalLog(" Fatal signal received: ");
 Log::signalLog(signalName(signal));
diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp
index 7d73ecc..a55c70e 100644
--- a/loolwsd/Util.hpp
+++ b/loolwsd/Util.hpp
@@ -31,6 +31,11 @@
 /// Flag to stop pump loops.
 extern std::atomic TerminationFlag;
 
+/// Mutex to trap signal handler, if any,
+/// and prevent _Exit while collecting backtrace.
+extern std::mutex SigHandlerTrap;
+
+
 namespace Util
 {
 namespace rng
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2016-03-13 Thread Ashod Nakashian
 loolwsd/LOOLKit.cpp |5 +++--
 loolwsd/Util.cpp|   17 -
 loolwsd/Util.hpp|3 ++-
 3 files changed, 21 insertions(+), 4 deletions(-)

New commits:
commit 019aed134e17b69e615a5990eab8c471184973e9
Author: Ashod Nakashian 
Date:   Sat Mar 12 10:15:45 2016 -0500

loolwsd: using random jailId

Change-Id: Ie9501e4346a124994270ca1fac61869ed04b9b72
Reviewed-on: https://gerrit.libreoffice.org/23213
Reviewed-by: Ashod Nakashian 
Tested-by: Ashod Nakashian 

diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp
index 5414a26..4fb6141 100644
--- a/loolwsd/LOOLKit.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -414,7 +414,7 @@ public:
[this](const std::string& id, const std::string& uri, 
const std::string& docPassword, bool isDocPasswordProvided) { return onLoad(id, 
uri, docPassword, isDocPasswordProvided); },
[this](const std::string& id) { onUnload(id); });
 // child -> 0,  sessionId -> 1, PID -> 2
-const std::string hello("child " + sessionId + " " + 
std::to_string(Process::id()));
+const std::string hello("child " + sessionId + " " + _jailId);
 session->sendTextFrame(hello);
 
 auto thread = std::make_shared(session, ws);
@@ -764,6 +764,7 @@ void lokit_main(const std::string& childRoot,
 #ifdef LOOLKIT_NO_MAIN
 // Reinitialize logging when forked.
 Log::initialize("kit");
+Util::rng::reseed();
 #endif
 
 struct pollfd pollPipeBroker;
@@ -782,7 +783,7 @@ void lokit_main(const std::string& childRoot,
 
 std::map _documents;
 
-static const std::string jailId = std::to_string(Process::id());
+static const std::string jailId = Util::encodeId(Util::rng::getNext());
 static const std::string process_name = "loolkit";
 
 if (prctl(PR_SET_NAME, reinterpret_cast(process_name.c_str()), 0, 0, 0) != 0)
diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp
index 7485a9e..5fc9a2a 100644
--- a/loolwsd/Util.cpp
+++ b/loolwsd/Util.cpp
@@ -65,7 +65,22 @@ namespace rng
 {
 static std::random_device _rd;
 static std::mutex _rngMutex;
-static std::mt19937_64 _rng = std::mt19937_64(_rd());
+
+// Create the prng with a random-device for seed.
+// If we don't have a hardware random-device, we will get the same seed.
+// In that case we are better off with an arbitrary, but changing, seed.
+static std::mt19937_64 _rng = std::mt19937_64(_rd.entropy()
+? _rd()
+: (clock() + getpid()));
+
+// A new seed is used to shuffle the sequence.
+// N.B. Always reseed after getting forked!
+void reseed()
+{
+_rng.seed(_rd.entropy() ? _rd() : (clock() + getpid()));
+}
+
+// Returns a new random number.
 unsigned getNext()
 {
 std::unique_lock lock(_rngMutex);
diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp
index 1e6e515..2e83ea8 100644
--- a/loolwsd/Util.hpp
+++ b/loolwsd/Util.hpp
@@ -50,7 +50,8 @@ namespace Util
 {
 namespace rng
 {
-   unsigned getNext();
+void reseed();
+unsigned getNext();
 }
 
 /// Encode an integral ID into a string, with padding support.
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] online.git: loolwsd/LOOLKit.cpp loolwsd/Util.cpp

2016-02-14 Thread Henry Castro
 loolwsd/LOOLKit.cpp |2 --
 loolwsd/Util.cpp|2 --
 2 files changed, 4 deletions(-)

New commits:
commit 45754cc36c7b087700c238d6e9d7d05b5cc04f2f
Author: Henry Castro 
Date:   Sun Feb 14 23:07:40 2016 -0400

loolwsd: remove unused TerminationState

diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp
index 80a763b..8089bd2 100644
--- a/loolwsd/LOOLKit.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -835,12 +835,10 @@ void lokit_main(const std::string& childRoot,
 Log::error() << exc.name() << ": " << exc.displayText()
  << (exc.nested() ? " (" + exc.nested()->displayText() + 
")" : "")
  << Log::end;
-TerminationState = LOOLState::LOOL_ABNORMAL;
 }
 catch (const std::exception& exc)
 {
 Log::error(std::string("Exception: ") + exc.what());
-TerminationState = LOOLState::LOOL_ABNORMAL;
 }
 
 Log::debug("Destroying documents.");
diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp
index e4ae391..1685d38 100644
--- a/loolwsd/Util.cpp
+++ b/loolwsd/Util.cpp
@@ -60,7 +60,6 @@ extern "C"
 }
 }
 
-volatile LOOLState TerminationState = LOOLState::LOOL_RUNNING;
 volatile bool TerminationFlag = false;
 
 namespace Util
@@ -424,7 +423,6 @@ namespace Util
 // If we are signaled while having that lock,
 // logging again will deadlock on it.
 TerminationFlag = true;
-TerminationState = ( aSignal == SIGTERM ? LOOLState::LOOL_ABNORMAL 
: LOOLState::LOOL_STOPPING );
 
 Log::info() << "Termination signal received: "
 << Util::signalName(aSignal) << " "
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits