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

Ashod Nakashian ashod.nakashian at collabora.co.uk
Sat Jan 30 07:41:53 PST 2016


 loolwsd/LOOLBroker.cpp |   69 ++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 54 insertions(+), 15 deletions(-)

New commits:
commit 4861bb75f2ba3231f1fb7f2293a25369e17ae026
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Mon Jan 25 21:57:04 2016 -0500

    loolwsd: child process termination and cleanup
    
    Change-Id: Ib43202dbde92c0223096bad1b9b4d491484b88e0
    Reviewed-on: https://gerrit.libreoffice.org/21931
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/LOOLBroker.cpp b/loolwsd/LOOLBroker.cpp
index c6e66ed..6462f05 100644
--- a/loolwsd/LOOLBroker.cpp
+++ b/loolwsd/LOOLBroker.cpp
@@ -94,6 +94,55 @@ namespace
         {
         }
 
+        ChildProcess(ChildProcess&& other) :
+            _pid(other._pid),
+            _readPipe(other._readPipe),
+            _writePipe(other._writePipe)
+        {
+            other._pid = -1;
+            other._readPipe = -1;
+            other._writePipe = -1;
+        }
+
+        const ChildProcess& operator=(ChildProcess&& other)
+        {
+            _pid = other._pid;
+            other._pid = -1;
+            _readPipe = other._readPipe;
+            other._readPipe = -1;
+            _writePipe = other._writePipe;
+            other._writePipe = -1;
+
+            return *this;
+        }
+
+        ~ChildProcess()
+        {
+            close();
+        }
+
+        void close()
+        {
+            if (_pid != -1)
+            {
+                if (kill(_pid, SIGTERM) != 0 && kill(_pid, 0) != 0)
+                    Log::warn("Cannot terminate lokit [" + std::to_string(_pid) + "]. Abandoning.");
+               _pid = -1;
+            }
+
+            if (_readPipe != -1)
+            {
+                ::close(_readPipe);
+                _readPipe = -1;
+            }
+
+            if (_writePipe != -1)
+            {
+                ::close(_writePipe);
+                _writePipe = -1;
+            }
+        }
+
         void setUrl(const std::string& url) { _url = url; }
         const std::string& getUrl() const { return _url; }
 
@@ -120,14 +169,6 @@ namespace
         return (it != _childProcesses.end() ? it->second.getWritePipe() : -1);
     }
 
-    void requestAbnormalTermination(const Process::PID aPID)
-    {
-        if (kill(aPID, SIGTERM) != 0 && kill(aPID, 0) != 0)
-        {
-            Log::info("Cannot terminate lokit [" + std::to_string(aPID) + "].");
-        }
-    }
-
     /// Safely removes a child process and
     /// invalidates the URL cache.
     void removeChild(const Process::PID pid)
@@ -136,9 +177,8 @@ namespace
         const auto it = _childProcesses.find(pid);
         if (it != _childProcesses.end())
         {
-            // Close the write pipe.
-            requestAbnormalTermination(pid);
-            close(it->second.getWritePipe());
+            // Close the child.
+            it->second.close();
             _childProcesses.erase(it);
             _cacheURL.clear();
             ++forkCounter;
@@ -627,7 +667,7 @@ static int createLibreOfficeKit(const bool sharePages,
     if ( (nFIFOWriter = open(pipe.c_str(), O_WRONLY)) < 0 )
     {
         Log::error("Error: failed to open write pipe [" + pipe + "] with kit. Abandoning child.");
-        requestAbnormalTermination(childPID);
+        ChildProcess(childPID, -1, -1);
         return -1;
     }
 
@@ -977,11 +1017,10 @@ int main(int argc, char** argv)
             Log::info("Forcing child process " + std::to_string(it.first) + " to terminate.");
             Process::kill(it.first);
         }
-
-        // Close the write pipe.
-        close(it.second.getWritePipe());
     }
 
+    _childProcesses.clear();
+
     aPipe.join();
     close(readerChild);
     close(readerBroker);


More information about the Libreoffice-commits mailing list