[Libreoffice-commits] online.git: loolwsd/ChildProcessSession.cpp loolwsd/ChildProcessSession.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
Wed Jan 6 21:56:19 PST 2016


 loolwsd/ChildProcessSession.cpp |    8 ++---
 loolwsd/ChildProcessSession.hpp |    7 ++---
 loolwsd/LOOLBroker.cpp          |   26 +++++++++++--------
 loolwsd/LOOLKit.cpp             |   54 ++++++++++++++++++++--------------------
 loolwsd/LOOLWSD.cpp             |    6 ++--
 loolwsd/LOOLWSD.hpp             |    2 -
 loolwsd/Util.cpp                |   28 ++++++++++++++++++++
 loolwsd/Util.hpp                |    5 +++
 8 files changed, 88 insertions(+), 48 deletions(-)

New commits:
commit 0b5bc5f244dbdc73df708d9e92e00c1297a5bc71
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Wed Jan 6 22:47:01 2016 -0500

    loolwsd: random jailId dir created by WSD and renamed childId -> jailId
    
    Change-Id: Ib5ce65c578c0c7fd3dbf81628906cd17728fb3ff
    Reviewed-on: https://gerrit.libreoffice.org/21182
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/ChildProcessSession.cpp b/loolwsd/ChildProcessSession.cpp
index 3267570..8434b61 100644
--- a/loolwsd/ChildProcessSession.cpp
+++ b/loolwsd/ChildProcessSession.cpp
@@ -45,13 +45,13 @@ ChildProcessSession::ChildProcessSession(const std::string& id,
                                          std::shared_ptr<Poco::Net::WebSocket> ws,
                                          LibreOfficeKit *loKit,
                                          LibreOfficeKitDocument * loKitDocument,
-                                         const std::string& childId,
+                                         const std::string& jailId,
                                          std::function<void(LibreOfficeKitDocument*, int)> onLoad,
                                          std::function<void(int)> onUnload) :
     LOOLSession(id, Kind::ToMaster, ws),
     _loKitDocument(loKitDocument),
     _loKit(loKit),
-    _childId(childId),
+    _jailId(jailId),
     _viewId(0),
     _clientPart(0),
     _onLoad(onLoad),
@@ -528,14 +528,14 @@ bool ChildProcessSession::downloadAs(const char* /*buffer*/, int /*length*/, Str
             format.size() == 0 ? nullptr :format.c_str(),
             filterOptions.size() == 0 ? nullptr : filterOptions.c_str());
 
-    sendTextFrame("downloadas: jail=" + _childId + " dir=" + tmpDir + " name=" + name +
+    sendTextFrame("downloadas: jail=" + _jailId + " dir=" + tmpDir + " name=" + name +
             " port=" + std::to_string(ClientPortNumber) + " id=" + id);
     return true;
 }
 
 bool ChildProcessSession::getChildId()
 {
-    sendTextFrame("getchildid: id=" + _childId);
+    sendTextFrame("getchildid: id=" + _jailId);
     return true;
 }
 
diff --git a/loolwsd/ChildProcessSession.hpp b/loolwsd/ChildProcessSession.hpp
index dd8bd20..a24096e 100644
--- a/loolwsd/ChildProcessSession.hpp
+++ b/loolwsd/ChildProcessSession.hpp
@@ -27,12 +27,13 @@ public:
     /// loKit The LOKit instance.
     /// loKitDocument The instance to an existing document (when opening
     ///                 a new view) or nullptr (when first view).
-    /// childId The id of the lokit instance, used by downloadas to construct jailed path.
+    /// jailId The JailID of the jail root directory,
+    //         used by downloadas to construct jailed path.
     ChildProcessSession(const std::string& id,
                         std::shared_ptr<Poco::Net::WebSocket> ws,
                         LibreOfficeKit *loKit,
                         LibreOfficeKitDocument * loKitDocument,
-                        const std::string& childId,
+                        const std::string& jailId,
                         std::function<void(LibreOfficeKitDocument*, int)> onLoad,
                         std::function<void(int)> onUnload);
     virtual ~ChildProcessSession();
@@ -75,7 +76,7 @@ public:
 
     std::string _loSubPath;
     LibreOfficeKit *_loKit;
-    const std::string _childId;
+    const std::string _jailId;
 
 private:
 
diff --git a/loolwsd/LOOLBroker.cpp b/loolwsd/LOOLBroker.cpp
index 12bde23..2ebbda8 100644
--- a/loolwsd/LOOLBroker.cpp
+++ b/loolwsd/LOOLBroker.cpp
@@ -431,7 +431,7 @@ static bool globalPreinit(const std::string &loSubPath)
 }
 
 static int createLibreOfficeKit(const bool sharePages, const std::string& loSubPath,
-                                const std::string& childId)
+                                const std::string& jailId)
 {
     Poco::UInt64 child;
     int nFIFOWriter = -1;
@@ -452,7 +452,7 @@ static int createLibreOfficeKit(const bool sharePages, const std::string& loSubP
         if (!(pid = fork()))
         {
             // child
-            lokit_main(loSubPath, childId, pipe);
+            lokit_main(loSubPath, jailId, pipe);
             _exit(0);
         }
         else
@@ -466,7 +466,7 @@ static int createLibreOfficeKit(const bool sharePages, const std::string& loSubP
     {
         Process::Args args;
         args.push_back("--losubpath=" + loSubPath);
-        args.push_back("--child=" + childId);
+        args.push_back("--jailid=" + jailId);
         args.push_back("--pipe=" + pipe);
         args.push_back("--clientport=" + std::to_string(ClientPortNumber));
 
@@ -501,14 +501,14 @@ static int createLibreOfficeKit(const bool sharePages, const std::string& loSubP
 }
 
 static int startupLibreOfficeKit(const bool sharePages, const int nLOKits,
-                                 const std::string& loSubPath, const std::string& childId)
+                                 const std::string& loSubPath, const std::string& jailId)
 {
     Process::PID pId = -1;
 
     Log::info() << "Starting " << nLOKits << " LoKit instaces." << Log::end;
     for (int nCntr = nLOKits; nCntr; nCntr--)
     {
-        if ((pId = createLibreOfficeKit(sharePages, loSubPath, childId)) < 0)
+        if ((pId = createLibreOfficeKit(sharePages, loSubPath, jailId)) < 0)
         {
             Log::error("Error: failed to create LibreOfficeKit.");
             break;
@@ -537,6 +537,7 @@ int main(int argc, char** argv)
 #endif
 
     std::string childRoot;
+    std::string jailId;
     std::string loSubPath;
     std::string sysTemplate;
     std::string loTemplate;
@@ -569,6 +570,12 @@ int main(int argc, char** argv)
             if (*eq)
                 childRoot = std::string(++eq);
         }
+        else if (strstr(cmd, "--jailid=") == cmd)
+        {
+            eq = strchrnul(cmd, '=');
+            if (*eq)
+                jailId = std::string(++eq);
+        }
         else if (strstr(cmd, "--numprespawns=") == cmd)
         {
             eq = strchrnul(cmd, '=');
@@ -645,10 +652,7 @@ int main(int argc, char** argv)
         exit(-1);
     }
 
-    //TODO: Why not use our PID?
-    const std::string childId = std::to_string(Util::rng::getNext());
-
-    const Path jailPath = Path::forDirectory(childRoot + Path::separator() + childId);
+    const Path jailPath = Path::forDirectory(childRoot + Path::separator() + jailId);
     Log::info("Jail path: " + jailPath.toString());
 
     File(jailPath).createDirectories();
@@ -722,7 +726,7 @@ int main(int argc, char** argv)
 
     const bool sharePages = globalPreinit(loSubPath);
 
-    if ( startupLibreOfficeKit(sharePages, numPreSpawnedChildren, loSubPath, childId) < 0 )
+    if ( startupLibreOfficeKit(sharePages, numPreSpawnedChildren, loSubPath, jailId) < 0 )
     {
         Log::error("Error: failed to create children.");
         exit(-1);
@@ -785,7 +789,7 @@ int main(int argc, char** argv)
             forkMutex.lock();
             --forkCounter;
 
-            if (createLibreOfficeKit(sharePages, loSubPath, childId) < 0)
+            if (createLibreOfficeKit(sharePages, loSubPath, jailId) < 0)
                 Log::error("Error: fork failed.");
 
             forkMutex.unlock();
diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp
index 6c765ab..3f67652 100644
--- a/loolwsd/LOOLKit.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -320,23 +320,23 @@ class Connection: public Runnable
 {
 public:
     Connection(LibreOfficeKit *loKit, LibreOfficeKitDocument *loKitDocument,
-               const std::string& childId, const std::string& sessionId,
+               const std::string& jailId, const std::string& sessionId,
                std::function<void(LibreOfficeKitDocument*, int)> onLoad,
                std::function<void(int)> onUnload) :
         _loKit(loKit),
         _loKitDocument(loKitDocument),
-        _childId(childId),
+        _jailId(jailId),
         _sessionId(sessionId),
         _stop(false),
         _onLoad(onLoad),
         _onUnload(onUnload)
     {
-        Log::info("Connection ctor in child: " + childId + ", thread: " + _sessionId);
+        Log::info("Connection ctor in child: " + jailId + ", thread: " + _sessionId);
     }
 
     ~Connection()
     {
-        Log::info("~Connection dtor in child: " + _childId + ", thread: " + _sessionId);
+        Log::info("~Connection dtor in child: " + _jailId + ", thread: " + _sessionId);
         stop();
     }
 
@@ -387,11 +387,11 @@ public:
             HTTPResponse response;
             auto ws = std::make_shared<WebSocket>(cs, request, response);
 
-            _session.reset(new ChildProcessSession(_sessionId, ws, _loKit, _loKitDocument, _childId, _onLoad, _onUnload));
+            _session.reset(new ChildProcessSession(_sessionId, ws, _loKit, _loKitDocument, _jailId, _onLoad, _onUnload));
             ws->setReceiveTimeout(0);
 
             // child Jail TID PID
-            std::string hello("child " + _childId + " " +
+            std::string hello("child " + _jailId + " " +
                               _sessionId + " " + std::to_string(Process::id()));
             _session->sendTextFrame(hello);
 
@@ -451,7 +451,7 @@ public:
 private:
     LibreOfficeKit *_loKit;
     LibreOfficeKitDocument *_loKitDocument;
-    const std::string _childId;
+    const std::string _jailId;
     const std::string _sessionId;
     Thread _thread;
     std::shared_ptr<ChildProcessSession> _session;
@@ -470,14 +470,14 @@ private:
 class Document
 {
 public:
-    Document(LibreOfficeKit *loKit, const std::string& childId,
+    Document(LibreOfficeKit *loKit, const std::string& jailId,
              const std::string& url)
       : _loKit(loKit),
-        _childId(childId),
+        _jailId(jailId),
         _url(url),
         _loKitDocument(nullptr)
     {
-        Log::info("Document ctor for url [" + url + "] on child [" + childId + "].");
+        Log::info("Document ctor for url [" + url + "] on child [" + jailId + "].");
     }
 
     ~Document()
@@ -521,7 +521,7 @@ public:
                 Log::warn("Thread [" + sessionId + "] is not running. Restoring.");
                 _connections.erase(sessionId);
 
-                auto thread = std::make_shared<Connection>(_loKit, aItem->second->getLOKitDocument(), _childId, sessionId,
+                auto thread = std::make_shared<Connection>(_loKit, aItem->second->getLOKitDocument(), _jailId, sessionId,
                                                            [this](LibreOfficeKitDocument *loKitDocument, const int viewId) { onLoad(loKitDocument, viewId); },
                                                            [this](const int viewId) { onUnload(viewId); });
                 _connections.emplace(sessionId, thread);
@@ -535,16 +535,16 @@ public:
             std::shared_ptr<Connection> thread;
             if ( _connections.empty() )
             {
-                Log::info("Creating main thread for child: " + _childId + ", thread: " + sessionId);
-                thread = std::make_shared<Connection>(_loKit, nullptr, _childId, sessionId,
+                Log::info("Creating main thread for child: " + _jailId + ", thread: " + sessionId);
+                thread = std::make_shared<Connection>(_loKit, nullptr, _jailId, sessionId,
                                                       [this](LibreOfficeKitDocument *loKitDocument, const int viewId) { onLoad(loKitDocument, viewId); },
                                                       [this](const int viewId) { onUnload(viewId); });
             }
             else
             {
-                Log::info("Creating view thread for child: " + _childId + ", thread: " + sessionId);
+                Log::info("Creating view thread for child: " + _jailId + ", thread: " + sessionId);
                 auto aConnection = _connections.begin();
-                thread = std::make_shared<Connection>(_loKit, aConnection->second->getLOKitDocument(), _childId, sessionId,
+                thread = std::make_shared<Connection>(_loKit, aConnection->second->getLOKitDocument(), _jailId, sessionId,
                                                       [this](LibreOfficeKitDocument *loKitDocument, const int viewId) { onLoad(loKitDocument, viewId); },
                                                       [this](const int viewId) { onUnload(viewId); });
             }
@@ -554,7 +554,7 @@ public:
             if ( aInserted.second )
                 thread->start();
             else
-                Log::error("Connection already exists for child: " + _childId + ", thread: " + sessionId);
+                Log::error("Connection already exists for child: " + _jailId + ", thread: " + sessionId);
 
             Log::debug("Connections: " + std::to_string(_connections.size()));
         }
@@ -578,7 +578,7 @@ private:
 private:
 
     LibreOfficeKit *_loKit;
-    const std::string _childId;
+    const std::string _jailId;
     const std::string _url;
 
     LibreOfficeKitDocument *_loKitDocument;
@@ -586,7 +586,7 @@ private:
     std::map<std::string, std::shared_ptr<Connection>> _connections;
 };
 
-void lokit_main(const std::string &loSubPath, const std::string& childId, const std::string& pipe)
+void lokit_main(const std::string &loSubPath, const std::string& jailId, const std::string& pipe)
 {
 #ifdef LOOLKIT_NO_MAIN
     // Reinitialize logging when forked.
@@ -601,7 +601,7 @@ void lokit_main(const std::string &loSubPath, const std::string& childId, const
 
     std::map<std::string, std::shared_ptr<Document>> _documents;
 
-    assert(!childId.empty());
+    assert(!jailId.empty());
     assert(!loSubPath.empty());
 
     static const std::string process_name = "loolkit";
@@ -647,7 +647,7 @@ void lokit_main(const std::string &loSubPath, const std::string& childId, const
         CallBackWorker callbackWorker(ChildProcessSession::_callbackQueue);
         Poco::ThreadPool::defaultPool().start(callbackWorker);
 
-        Log::info("Child [" + childId + "] is ready.");
+        Log::info("Child [" + jailId + "] is ready.");
 
         std::string aResponse;
         std::string aMessage;
@@ -723,7 +723,7 @@ void lokit_main(const std::string &loSubPath, const std::string& childId, const
 
                         auto it = _documents.lower_bound(url);
                         if (it == _documents.end())
-                            it = _documents.emplace_hint(it, url, std::make_shared<Document>(loKit.get(), childId, url));
+                            it = _documents.emplace_hint(it, url, std::make_shared<Document>(loKit.get(), jailId, url));
 
                         it->second->createSession(sessionId);
                     }
@@ -780,7 +780,7 @@ int main(int argc, char** argv)
     Log::initialize("kit");
 
     std::string loSubPath;
-    std::string childId;
+    std::string jailId;
     std::string pipe;
 
     for (int i = 1; i < argc; ++i)
@@ -793,11 +793,11 @@ int main(int argc, char** argv)
             if (*eq)
                 loSubPath = std::string(++eq);
         }
-        else if (strstr(cmd, "--child=") == cmd)
+        else if (strstr(cmd, "--jailid=") == cmd)
         {
             eq = strchrnul(cmd, '=');
             if (*eq)
-                childId = std::string(++eq);
+                jailId = std::string(++eq);
         }
         else if (strstr(cmd, "--pipe=") == cmd)
         {
@@ -819,9 +819,9 @@ int main(int argc, char** argv)
         exit(1);
     }
 
-    if ( childId.empty() )
+    if (jailId.empty())
     {
-        Log::error("Error: --child is 0");
+        Log::error("Error: --jailid is empty");
         exit(1);
     }
 
@@ -849,7 +849,7 @@ int main(int argc, char** argv)
         Log::error(std::string("Exception: ") + exc.what());
     }
 
-    lokit_main(loSubPath, childId, pipe);
+    lokit_main(loSubPath, jailId, pipe);
 
     return 0;
 }
diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp
index 81d1712..09b5a06 100644
--- a/loolwsd/LOOLWSD.cpp
+++ b/loolwsd/LOOLWSD.cpp
@@ -786,7 +786,7 @@ void LOOLWSD::displayHelp()
     helpFormatter.format(std::cout);
 }
 
-bool LOOLWSD::createBroker()
+bool LOOLWSD::createBroker(const std::string& jailId)
 {
     Process::Args args;
 
@@ -794,6 +794,7 @@ bool LOOLWSD::createBroker()
     args.push_back("--systemplate=" + sysTemplate);
     args.push_back("--lotemplate=" + loTemplate);
     args.push_back("--childroot=" + childRoot);
+    args.push_back("--jailid=" + jailId);
     args.push_back("--numprespawns=" + std::to_string(NumPreSpawnedChildren));
     args.push_back("--clientport=" + std::to_string(ClientPortNumber));
 
@@ -869,7 +870,8 @@ int LOOLWSD::main(const std::vector<std::string>& /*args*/)
 
     NamedMutexLOOL.lock();
 
-    if (!createBroker())
+    const auto jailId = Util::createRandomDir(childRoot);
+    if (!createBroker(jailId))
     {
         Log::error("Failed to spawn loolBroker.");
         return Application::EXIT_UNAVAILABLE;
diff --git a/loolwsd/LOOLWSD.hpp b/loolwsd/LOOLWSD.hpp
index f03faed..653659d 100644
--- a/loolwsd/LOOLWSD.hpp
+++ b/loolwsd/LOOLWSD.hpp
@@ -74,7 +74,7 @@ private:
     int  createComponent();
     int  createDesktop();
 
-    bool createBroker();
+    bool createBroker(const std::string& jailId);
 };
 
 #endif
diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp
index 81e3b7d..80f2f23 100644
--- a/loolwsd/Util.cpp
+++ b/loolwsd/Util.cpp
@@ -176,6 +176,34 @@ namespace Util
         return oss.str();
     }
 
+    std::string createRandomDir(const std::string& path)
+    {
+        Poco::File(path).createDirectories();
+        for (;;)
+        {
+            const auto name = Util::encodeId(rng::getNext());
+            Poco::File dir(Poco::Path(path, name));
+            if (dir.createDirectory())
+            {
+                return name;
+            }
+        }
+    }
+
+    std::string createRandomFile(const std::string& path)
+    {
+        Poco::File(path).createDirectories();
+        for (;;)
+        {
+            const auto name = Util::encodeId(rng::getNext());
+            Poco::File file(Poco::Path(path, name));
+            if (file.createFile())
+            {
+                return name;
+            }
+        }
+    }
+
     bool windowingAvailable()
     {
 #ifdef __linux
diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp
index ac3d091..4eec373 100644
--- a/loolwsd/Util.hpp
+++ b/loolwsd/Util.hpp
@@ -32,6 +32,11 @@ namespace Util
     /// Encode an integral ID into a string, with padding support.
     std::string encodeId(const unsigned number, const int padding = 5);
 
+    /// Creates a randomly name directory within path and returns the name.
+    std::string createRandomDir(const std::string& path);
+    /// Creates a randomly name file within path and returns the name.
+    std::string createRandomFile(const std::string& path);
+
     bool windowingAvailable();
 
     // Sadly, older libpng headers don't use const for the pixmap pointer parameter to


More information about the Libreoffice-commits mailing list