[Libreoffice-commits] online.git: 2 commits - loolwsd/ChildProcessSession.cpp loolwsd/ChildProcessSession.hpp loolwsd/LOOLBroker.cpp loolwsd/LOOLForKit.cpp loolwsd/LOOLKit.cpp loolwsd/LOOLKit.hpp loolwsd/LOOLWSD.cpp loolwsd/loolwsd.spec.in loolwsd/Makefile.am

Michael Meeks michael.meeks at collabora.com
Tue Apr 5 13:50:03 UTC 2016


 loolwsd/ChildProcessSession.cpp |    2 
 loolwsd/ChildProcessSession.hpp |    4 
 loolwsd/LOOLBroker.cpp          | 1410 ----------------------------------------
 loolwsd/LOOLForKit.cpp          |  265 +++++++
 loolwsd/LOOLKit.cpp             | 1182 +++++++++++++++++++++++++++++++++
 loolwsd/LOOLKit.hpp             |   20 
 loolwsd/LOOLWSD.cpp             |    4 
 loolwsd/Makefile.am             |   21 
 loolwsd/loolwsd.spec.in         |    2 
 9 files changed, 1483 insertions(+), 1427 deletions(-)

New commits:
commit 36d734ab815fb3ef3d8611f4ad0474aabf6fe95a
Author: Michael Meeks <michael.meeks at collabora.com>
Date:   Tue Apr 5 14:37:10 2016 +0100

    Rename loolbroker to loolforkit.
    
    Now we have just one broker - the DocumentBroker inside WSD.

diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp
index a2f8911..5928ad7 100644
--- a/loolwsd/LOOLWSD.cpp
+++ b/loolwsd/LOOLWSD.cpp
@@ -1135,9 +1135,9 @@ Process::PID LOOLWSD::createBroker()
     args.push_back("--childroot=" + ChildRoot);
     args.push_back("--clientport=" + std::to_string(ClientPortNumber));
 
-    const std::string brokerPath = Path(Application::instance().commandPath()).parent().toString() + "loolbroker";
+    const std::string brokerPath = Path(Application::instance().commandPath()).parent().toString() + "loolforkit";
 
-    Log::info("Launching Broker #1: " + brokerPath + " " +
+    Log::info("Launching kit forker #1: " + brokerPath + " " +
               Poco::cat(std::string(" "), args.begin(), args.end()));
 
     ProcessHandle child = Process::launch(brokerPath, args);
diff --git a/loolwsd/Makefile.am b/loolwsd/Makefile.am
index a4f5093..0731b3e 100644
--- a/loolwsd/Makefile.am
+++ b/loolwsd/Makefile.am
@@ -1,6 +1,6 @@
 SUBDIRS = test
 
-bin_PROGRAMS = loolwsd loolbroker loolmap loolmount
+bin_PROGRAMS = loolwsd loolforkit loolmap loolmount
 
 dist_bin_SCRIPTS = loolwsd-systemplate-setup discovery.xml
 
@@ -44,7 +44,7 @@ lokitclient_SOURCES = IoUtil.cpp \
                       LOOLProtocol.cpp \
                       Util.cpp
 
-loolbroker_SOURCES = LOOLForKit.cpp \
+loolforkit_SOURCES = LOOLForKit.cpp \
 		     LOOLKit.cpp \
                      $(shared_sources)
 
@@ -87,15 +87,15 @@ clean-cache:
 # Intentionally don't use "*" below... Avoid risk of accidentally running rm -rf /*
 	test -n "@LOOLWSD_CACHEDIR@" && rm -rf "@LOOLWSD_CACHEDIR@"/[0-9a-f]
 
-# After building loolbroker, set its capabilities as required. Do it
+# After building loolforkit, set its capabilities as required. Do it
 # already after a plain 'make' to allow for testing without
 # installing. When building for packaging, no need for this, as the
 # capabilities won't survive packaging anyway. Instead, handle it when
 # installing the RPM or Debian package.
 
-all-local: loolbroker certificates
+all-local: loolforkit certificates
 	if test "$$BUILDING_FROM_RPMBUILD" != yes; then \
-	    sudo @SETCAP@ cap_fowner,cap_mknod,cap_sys_chroot=ep loolbroker; \
+	    sudo @SETCAP@ cap_fowner,cap_mknod,cap_sys_chroot=ep loolforkit; \
 	    sudo @SETCAP@ cap_sys_admin=ep loolmount; \
 	    echo "Set required capabilities"; \
 	else \
diff --git a/loolwsd/loolwsd.spec.in b/loolwsd/loolwsd.spec.in
index 9c0e198..bae0835 100644
--- a/loolwsd/loolwsd.spec.in
+++ b/loolwsd/loolwsd.spec.in
@@ -57,7 +57,7 @@ echo "0 0 */1 * * root find /var/cache/loolwsd -name \"*.png\" -a -atime +10 -ex
 /usr/bin/loolwsd
 /usr/bin/loolwsd-systemplate-setup
 /usr/bin/loolmap
-/usr/bin/loolbroker
+/usr/bin/loolforkit
 /usr/bin/discovery.xml
 %{_unitdir}/loolwsd.service
 /var/adm/fillup-templates/sysconfig.loolwsd
commit 6ec2e8ca68a46a249e00a6b174a75309e1cac48f
Author: Michael Meeks <michael.meeks at collabora.com>
Date:   Tue Apr 5 14:32:10 2016 +0100

    Rename loolbroker to loolforkit - and split the child to LOOLKit.
    
    Move ChildProcessSession to shared_source and fix the static
    ClientPortNumber issue.

diff --git a/loolwsd/ChildProcessSession.cpp b/loolwsd/ChildProcessSession.cpp
index 110d06a..385c880 100644
--- a/loolwsd/ChildProcessSession.cpp
+++ b/loolwsd/ChildProcessSession.cpp
@@ -44,6 +44,8 @@ using Poco::StringTokenizer;
 using Poco::Timestamp;
 using Poco::URI;
 
+int ClientPortNumber = DEFAULT_CLIENT_PORT_NUMBER;
+
 class CallbackNotification: public Notification
 {
 public:
diff --git a/loolwsd/ChildProcessSession.hpp b/loolwsd/ChildProcessSession.hpp
index 0519fd8..b3d2a82 100644
--- a/loolwsd/ChildProcessSession.hpp
+++ b/loolwsd/ChildProcessSession.hpp
@@ -23,9 +23,7 @@
 #include "LOOLSession.hpp"
 
 // The client port number, which is changed via loolwsd args.
-// Except that it isn't. This is "static" so it is a *separate* variable
-// in each compilation unit.
-static int ClientPortNumber = DEFAULT_CLIENT_PORT_NUMBER;
+extern int ClientPortNumber;
 
 class CallbackWorker;
 
diff --git a/loolwsd/LOOLForKit.cpp b/loolwsd/LOOLForKit.cpp
new file mode 100644
index 0000000..84ea975
--- /dev/null
+++ b/loolwsd/LOOLForKit.cpp
@@ -0,0 +1,265 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+/*
+ * A very simple, single threaded helper to efficiently pre-init and
+ * spawn lots of kits as children.
+ */
+
+#include <sys/capability.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <cstdlib>
+#include <cstring>
+#include <atomic>
+#include <iostream>
+
+#include <Poco/Path.h>
+#include <Poco/Process.h>
+#include <Poco/StringTokenizer.h>
+#include <Poco/Thread.h>
+#include <Poco/Util/Application.h>
+
+#include "Common.hpp"
+#include "IoUtil.hpp"
+#include "LOOLKit.hpp"
+#include "Util.hpp"
+#include "ChildProcessSession.hpp"
+
+using Poco::Path;
+using Poco::Process;
+using Poco::StringTokenizer;
+using Poco::Thread;
+using Poco::Timestamp;
+using Poco::Util::Application;
+
+static const std::string BROKER_SUFIX = ".fifo";
+static const std::string BROKER_PREFIX = "lokit";
+
+static std::atomic<unsigned> ForkCounter( 0 );
+static unsigned int ChildCounter = 0;
+
+static int ReaderBroker = -1;
+
+class ChildDispatcher
+{
+public:
+    ChildDispatcher() :
+        _wsdPipeReader("wsd_pipe_rd", ReaderBroker)
+    {
+    }
+
+    /// Polls WSD commands and dispatches them to the appropriate child.
+    bool pollAndDispatch()
+    {
+        return _wsdPipeReader.processOnce([this](std::string& message) { handleInput(message); return true; },
+                                          []() { return TerminationFlag; });
+    }
+
+private:
+    void handleInput(const std::string& message)
+    {
+        Log::info("Broker command: [" + message + "].");
+
+        StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
+
+        if (tokens[0] == "spawn" && tokens.count() == 2)
+        {
+            const auto count = std::stoi(tokens[1]);
+            Log::info("Spawning " + tokens[1] + " children on request.");
+            ForkCounter = count;
+        }
+    }
+
+private:
+    IoUtil::PipeReader _wsdPipeReader;
+};
+
+static int createLibreOfficeKit(const std::string& childRoot,
+                                const std::string& sysTemplate,
+                                const std::string& loTemplate,
+                                const std::string& loSubPath)
+{
+    Process::PID childPID = 0;
+
+    Log::debug("Forking a loolkit process.");
+
+    Process::PID pid;
+    if (!(pid = fork()))
+    {
+        // child
+        if (std::getenv("SLEEPKITFORDEBUGGER"))
+        {
+            std::cerr << "Sleeping " << std::getenv("SLEEPKITFORDEBUGGER")
+                      << " seconds to give you time to attach debugger to process "
+                      << Process::id() << std::endl;
+            Thread::sleep(std::stoul(std::getenv("SLEEPKITFORDEBUGGER")) * 1000);
+        }
+
+        lokit_main(childRoot, sysTemplate, loTemplate, loSubPath);
+    }
+    else
+    {
+        // parent
+        childPID = pid; // (somehow - switch the hash to use real pids or ?) ...
+        Log::info("Forked kit [" + std::to_string(childPID) + "].");
+    }
+
+    Log::info() << "Created Kit #" << ChildCounter << ", PID: " << childPID << Log::end;
+    return childPID;
+}
+
+static void printArgumentHelp()
+{
+    std::cout << "Usage: loolforkit [OPTION]..." << std::endl;
+    std::cout << "  Single threaded process broker that spawns lok instances" << std::endl;
+    std::cout << "  note: running this standalone is not possible, it is spawned by the loolwsd" << std::endl;
+    std::cout << "        and is controlled via a pipe." << std::endl;
+    std::cout << "" << std::endl;
+    std::cout << "  Some parameters are required and passed on to the lok instance:" << std::endl;
+    std::cout << "  --losubpath=<path>        path to chroot for child to live inside." << std::endl;
+    std::cout << "  --childroot=<path>        path to chroot for child to live inside." << std::endl;
+    std::cout << "  --systemplate=<path>      path of system template to pre-populate chroot with." << std::endl;
+    std::cout << "  --lotemplate=<path>       path of libreoffice template to pre-populate chroot with." << std::endl;
+    std::cout << "  --losubpath=<path>        path to libreoffice install" << std::endl;
+}
+
+// Broker process
+int main(int argc, char** argv)
+{
+    if (std::getenv("SLEEPFORDEBUGGER"))
+    {
+        std::cerr << "Sleeping " << std::getenv("SLEEPFORDEBUGGER")
+                  << " seconds to give you time to attach debugger to process "
+                  << Process::id() << std::endl;
+        Thread::sleep(std::stoul(std::getenv("SLEEPFORDEBUGGER")) * 1000);
+    }
+
+    // Initialization
+    Log::initialize("brk");
+
+    Util::setTerminationSignals();
+    Util::setFatalSignals();
+
+    std::string childRoot;
+    std::string loSubPath;
+    std::string sysTemplate;
+    std::string loTemplate;
+
+    for (int i = 0; i < argc; ++i)
+    {
+        char *cmd = argv[i];
+        char *eq;
+        if (std::strstr(cmd, "--losubpath=") == cmd)
+        {
+            eq = std::strchr(cmd, '=');
+            loSubPath = std::string(eq+1);
+        }
+        else if (std::strstr(cmd, "--systemplate=") == cmd)
+        {
+            eq = std::strchr(cmd, '=');
+            sysTemplate = std::string(eq+1);
+        }
+        else if (std::strstr(cmd, "--lotemplate=") == cmd)
+        {
+            eq = std::strchr(cmd, '=');
+            loTemplate = std::string(eq+1);
+        }
+        else if (std::strstr(cmd, "--childroot=") == cmd)
+        {
+            eq = std::strchr(cmd, '=');
+            childRoot = std::string(eq+1);
+        }
+        else if (std::strstr(cmd, "--clientport=") == cmd)
+        {
+            eq = std::strchr(cmd, '=');
+            ClientPortNumber = std::stoll(std::string(eq+1));
+        }
+    }
+
+    if (loSubPath.empty() || sysTemplate.empty() ||
+        loTemplate.empty() || childRoot.empty())
+    {
+        printArgumentHelp();
+        return 1;
+    }
+
+    if (!std::getenv("LD_BIND_NOW"))
+        Log::info("Note: LD_BIND_NOW is not set.");
+
+    if (!std::getenv("LOK_VIEW_CALLBACK"))
+        Log::info("Note: LOK_VIEW_CALLBACK is not set.");
+
+    const Path pipePath = Path::forDirectory(childRoot + Path::separator() + FIFO_PATH);
+    const std::string pipeLoolwsd = Path(pipePath, FIFO_LOOLWSD).toString();
+    if ( (ReaderBroker = open(pipeLoolwsd.c_str(), O_RDONLY) ) < 0 )
+    {
+        Log::error("Error: failed to open pipe [" + pipeLoolwsd + "] read only. Exiting.");
+        std::exit(Application::EXIT_SOFTWARE);
+    }
+
+    // Initialize LoKit
+    if (!globalPreinit(loTemplate))
+        _exit(Application::EXIT_SOFTWARE);
+
+    Log::info("Preinit stage OK.");
+
+    // We must have at least one child, more are created dynamically.
+    if (createLibreOfficeKit(childRoot, sysTemplate, loTemplate, loSubPath) < 0)
+    {
+        Log::error("Error: failed to create children.");
+        std::exit(Application::EXIT_SOFTWARE);
+    }
+
+    ChildDispatcher childDispatcher;
+    Log::info("loolbroker is ready.");
+
+    Timestamp startTime;
+
+    while (!TerminationFlag)
+    {
+        if (!childDispatcher.pollAndDispatch())
+        {
+            Log::info("Child dispatcher flagged for termination.");
+            break;
+        }
+
+        if (ForkCounter > 0)
+        {
+            // Figure out how many children we need. Always create at least as many
+            // as configured pre-spawn or one more than requested (whichever is larger).
+            int spawn = ForkCounter;
+            Log::info() << "Creating " << spawn << " new child." << Log::end;
+            size_t newInstances = 0;
+            do
+            {
+                if (createLibreOfficeKit(childRoot, sysTemplate, loTemplate, loSubPath) < 0)
+                {
+                    Log::error("Error: fork failed.");
+                }
+                else
+                {
+                    ++newInstances;
+                }
+            }
+            while (--spawn > 0);
+
+            // If we need to spawn more, retry later.
+            ForkCounter = (newInstances > ForkCounter ? 0 : ForkCounter - newInstances);
+        }
+    }
+
+    close(ReaderBroker);
+
+    Log::info("Process [loolbroker] finished.");
+    return Application::EXIT_OK;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/LOOLBroker.cpp b/loolwsd/LOOLKit.cpp
similarity index 83%
rename from loolwsd/LOOLBroker.cpp
rename to loolwsd/LOOLKit.cpp
index 49da6f7..d89f315 100644
--- a/loolwsd/LOOLBroker.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -6,20 +6,21 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
+/*
+ * The main entry point for the LibreOfficeKit process serving
+ * a document editing session.
+ */
 
 #include <dlfcn.h>
 #include <ftw.h>
 #include <sys/capability.h>
 #include <sys/prctl.h>
-#include <sys/wait.h>
 #include <unistd.h>
 #include <utime.h>
 
 #include <atomic>
 #include <cassert>
 #include <condition_variable>
-#include <cstdlib>
-#include <cstring>
 #include <iostream>
 
 #include <memory>
@@ -54,10 +55,6 @@
 typedef int (LokHookPreInit)  (const char *install_path, const char *user_profile_path);
 
 static int WriterNotify = -1;
-static int ReaderBroker = -1;
-
-static std::atomic<unsigned> ForkCounter( 0 );
-static unsigned int ChildCounter = 0;
 
 using namespace LOOLProtocol;
 
@@ -901,10 +898,10 @@ private:
     std::atomic_size_t _clientViews;
 };
 
-static void lokit_main(const std::string& childRoot,
-                       const std::string& sysTemplate,
-                       const std::string& loTemplate,
-                       const std::string& loSubPath)
+void lokit_main(const std::string& childRoot,
+                const std::string& sysTemplate,
+                const std::string& loTemplate,
+                const std::string& loSubPath)
 {
     // Reinitialize logging when forked.
     Log::initialize("kit");
@@ -1129,42 +1126,8 @@ static void lokit_main(const std::string& childRoot,
     _exit(Application::EXIT_OK);
 }
 
-class ChildDispatcher
-{
-public:
-    ChildDispatcher() :
-        _wsdPipeReader("wsd_pipe_rd", ReaderBroker)
-    {
-    }
-
-    /// Polls WSD commands and dispatches them to the appropriate child.
-    bool pollAndDispatch()
-    {
-        return _wsdPipeReader.processOnce([this](std::string& message) { handleInput(message); return true; },
-                                          []() { return TerminationFlag; });
-    }
-
-private:
-    void handleInput(const std::string& message)
-    {
-        Log::info("Broker command: [" + message + "].");
-
-        StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
-
-        if (tokens[0] == "spawn" && tokens.count() == 2)
-        {
-            const auto count = std::stoi(tokens[1]);
-            Log::info("Spawning " + tokens[1] + " children on request.");
-            ForkCounter = count;
-        }
-    }
-
-private:
-    IoUtil::PipeReader _wsdPipeReader;
-};
-
 /// Initializes LibreOfficeKit for cross-fork re-use.
-static void globalPreinit(const std::string &loTemplate)
+bool globalPreinit(const std::string &loTemplate)
 {
     const std::string libSofficeapp = loTemplate + "/program/" LIB_SOFFICEAPP;
     const std::string libMerged = loTemplate + "/program/" LIB_MERGED;
@@ -1177,7 +1140,7 @@ static void globalPreinit(const std::string &loTemplate)
         if (!handle)
         {
             Log::error("Failed to load " + libMerged + ": " + std::string(dlerror()));
-            _exit(Application::EXIT_SOFTWARE);
+            return false;
         }
         loadedLibrary = libMerged;
     }
@@ -1189,14 +1152,14 @@ static void globalPreinit(const std::string &loTemplate)
             if (!handle)
             {
                 Log::error("Failed to load " + libSofficeapp + ": " + std::string(dlerror()));
-                _exit(Application::EXIT_SOFTWARE);
+                return false;
             }
             loadedLibrary = libSofficeapp;
         }
         else
         {
             Log::error("Neither " + libSofficeapp + " or " + libMerged + " exist.");
-            _exit(Application::EXIT_SOFTWARE);
+            return false;
         }
     }
 
@@ -1204,207 +1167,16 @@ static void globalPreinit(const std::string &loTemplate)
     if (!preInit)
     {
         Log::error("No lok_preinit symbol in " + loadedLibrary);
-        _exit(Application::EXIT_SOFTWARE);
+        return false;
     }
 
     if (preInit((loTemplate + "/program").c_str(), "file:///user") != 0)
     {
         Log::error("lok_preinit() in " + loadedLibrary + " failed");
-        _exit(Application::EXIT_SOFTWARE);
+        return false;
     }
-}
-
-static int createLibreOfficeKit(const std::string& childRoot,
-                                const std::string& sysTemplate,
-                                const std::string& loTemplate,
-                                const std::string& loSubPath)
-{
-    Process::PID childPID = 0;
-
-    Log::debug("Forking a loolkit process.");
-
-    Process::PID pid;
-    if (!(pid = fork()))
-    {
-        // child
-        if (std::getenv("SLEEPKITFORDEBUGGER"))
-        {
-            std::cerr << "Sleeping " << std::getenv("SLEEPKITFORDEBUGGER")
-                      << " seconds to give you time to attach debugger to process "
-                      << Process::id() << std::endl;
-            Thread::sleep(std::stoul(std::getenv("SLEEPKITFORDEBUGGER")) * 1000);
-        }
-
-        lokit_main(childRoot, sysTemplate, loTemplate, loSubPath);
-    }
-    else
-    {
-        // parent
-        childPID = pid; // (somehow - switch the hash to use real pids or ?) ...
-        Log::info("Forked kit [" + std::to_string(childPID) + "].");
-    }
-
-    Log::info() << "Created Kit #" << ChildCounter << ", PID: " << childPID << Log::end;
-    return childPID;
-}
-
-static void printArgumentHelp()
-{
-    std::cout << "Usage: loolbroker [OPTION]..." << std::endl;
-    std::cout << "  Single threaded process broker that spawns lok instances" << std::endl;
-    std::cout << "  note: running this standalone is not possible, it is spawned by the loolwsd" << std::endl;
-    std::cout << "        and is controlled via a pipe." << std::endl;
-    std::cout << "" << std::endl;
-    std::cout << "  Some parameters are required and passed on to the lok instance:" << std::endl;
-    std::cout << "  --losubpath=<path>        path to chroot for child to live inside." << std::endl;
-    std::cout << "  --childroot=<path>        path to chroot for child to live inside." << std::endl;
-    std::cout << "  --systemplate=<path>      path of system template to pre-populate chroot with." << std::endl;
-    std::cout << "  --lotemplate=<path>       path of libreoffice template to pre-populate chroot with." << std::endl;
-    std::cout << "  --losubpath=<path>        path to libreoffice install" << std::endl;
-}
-
-void setupPipes(const std::string &childRoot)
-{
-    const Path pipePath = Path::forDirectory(childRoot + Path::separator() + FIFO_PATH);
-    const std::string pipeLoolwsd = Path(pipePath, FIFO_LOOLWSD).toString();
-    if ( (ReaderBroker = open(pipeLoolwsd.c_str(), O_RDONLY) ) < 0 )
-    {
-        Log::error("Error: failed to open pipe [" + pipeLoolwsd + "] read only. Exiting.");
-        std::exit(Application::EXIT_SOFTWARE);
-    }
-
-    // Open notify pipe
-    const std::string pipeNotify = Path(pipePath, FIFO_ADMIN_NOTIFY).toString();
-    if ((WriterNotify = open(pipeNotify.c_str(), O_WRONLY) ) < 0)
-    {
-        Log::error("Error: failed to open notify pipe [" + std::string(FIFO_ADMIN_NOTIFY) + "] for writing.");
-        exit(Application::EXIT_SOFTWARE);
-    }
-}
-
-// Broker process
-int main(int argc, char** argv)
-{
-    if (std::getenv("SLEEPFORDEBUGGER"))
-    {
-        std::cerr << "Sleeping " << std::getenv("SLEEPFORDEBUGGER")
-                  << " seconds to give you time to attach debugger to process "
-                  << Process::id() << std::endl;
-        Thread::sleep(std::stoul(std::getenv("SLEEPFORDEBUGGER")) * 1000);
-    }
-
-    // Initialization
-    Log::initialize("brk");
-
-    Util::setTerminationSignals();
-    Util::setFatalSignals();
-
-    std::string childRoot;
-    std::string loSubPath;
-    std::string sysTemplate;
-    std::string loTemplate;
-
-    for (int i = 0; i < argc; ++i)
-    {
-        char *cmd = argv[i];
-        char *eq;
-        if (std::strstr(cmd, "--losubpath=") == cmd)
-        {
-            eq = std::strchr(cmd, '=');
-            loSubPath = std::string(eq+1);
-        }
-        else if (std::strstr(cmd, "--systemplate=") == cmd)
-        {
-            eq = std::strchr(cmd, '=');
-            sysTemplate = std::string(eq+1);
-        }
-        else if (std::strstr(cmd, "--lotemplate=") == cmd)
-        {
-            eq = std::strchr(cmd, '=');
-            loTemplate = std::string(eq+1);
-        }
-        else if (std::strstr(cmd, "--childroot=") == cmd)
-        {
-            eq = std::strchr(cmd, '=');
-            childRoot = std::string(eq+1);
-        }
-        else if (std::strstr(cmd, "--clientport=") == cmd)
-        {
-            eq = std::strchr(cmd, '=');
-            ClientPortNumber = std::stoll(std::string(eq+1));
-        }
-    }
-
-    if (loSubPath.empty() || sysTemplate.empty() ||
-        loTemplate.empty() || childRoot.empty())
-    {
-        printArgumentHelp();
-        return 1;
-    }
-
-    if (!std::getenv("LD_BIND_NOW"))
-        Log::info("Note: LD_BIND_NOW is not set.");
-
-    if (!std::getenv("LOK_VIEW_CALLBACK"))
-        Log::info("Note: LOK_VIEW_CALLBACK is not set.");
-
-    setupPipes(childRoot);
-
-    // Initialize LoKit
-    globalPreinit(loTemplate);
-
-    Log::info("Preinit stage OK.");
-
-    // We must have at least one child, more are created dynamically.
-    if (createLibreOfficeKit(childRoot, sysTemplate, loTemplate, loSubPath) < 0)
-    {
-        Log::error("Error: failed to create children.");
-        std::exit(Application::EXIT_SOFTWARE);
-    }
-
-    ChildDispatcher childDispatcher;
-    Log::info("loolbroker is ready.");
-
-    Timestamp startTime;
-
-    while (!TerminationFlag)
-    {
-        if (!childDispatcher.pollAndDispatch())
-        {
-            Log::info("Child dispatcher flagged for termination.");
-            break;
-        }
-
-        if (ForkCounter > 0)
-        {
-            // Figure out how many children we need. Always create at least as many
-            // as configured pre-spawn or one more than requested (whichever is larger).
-            int spawn = ForkCounter;
-            Log::info() << "Creating " << spawn << " new child." << Log::end;
-            size_t newInstances = 0;
-            do
-            {
-                if (createLibreOfficeKit(childRoot, sysTemplate, loTemplate, loSubPath) < 0)
-                {
-                    Log::error("Error: fork failed.");
-                }
-                else
-                {
-                    ++newInstances;
-                }
-            }
-            while (--spawn > 0);
-
-            // If we need to spawn more, retry later.
-            ForkCounter = (newInstances > ForkCounter ? 0 : ForkCounter - newInstances);
-        }
-    }
-
-    close(WriterNotify);
-    close(ReaderBroker);
 
-    Log::info("Process [loolbroker] finished.");
-    return Application::EXIT_OK;
+    return true;
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/LOOLKit.hpp b/loolwsd/LOOLKit.hpp
new file mode 100644
index 0000000..3a4ca67
--- /dev/null
+++ b/loolwsd/LOOLKit.hpp
@@ -0,0 +1,20 @@
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+#ifndef LOOL_KIT_HPP
+#define LOOL_KIT_HPP
+
+void lokit_main(const std::string& childRoot,
+                const std::string& sysTemplate,
+                const std::string& loTemplate,
+                const std::string& loSubPath);
+
+bool globalPreinit(const std::string &loTemplate);
+
+#endif // LOOL_KIT_HPP
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/Makefile.am b/loolwsd/Makefile.am
index c0bf767..a4f5093 100644
--- a/loolwsd/Makefile.am
+++ b/loolwsd/Makefile.am
@@ -10,7 +10,8 @@ AM_LDFLAGS = -pthread
 AM_ETAGSFLAGS = --c++-kinds=+p --fields=+iaS --extra=+q -R --totals=yes *
 AM_CTAGSFLAGS = $(AM_ETAGSFLAGS)
 
-shared_sources = IoUtil.cpp \
+shared_sources = ChildProcessSession.cpp \
+	  	 IoUtil.cpp \
                  LOOLProtocol.cpp \
                  LOOLSession.cpp \
                  MessageQueue.cpp \
@@ -19,7 +20,6 @@ shared_sources = IoUtil.cpp \
 loolwsd_SOURCES = Admin.cpp \
 		  AdminModel.cpp \
                   Auth.cpp \
-                  ChildProcessSession.cpp \
                   DocumentBroker.cpp \
                   LOOLWSD.cpp \
                   MasterProcessSession.cpp \
@@ -44,11 +44,9 @@ lokitclient_SOURCES = IoUtil.cpp \
                       LOOLProtocol.cpp \
                       Util.cpp
 
-broker_shared_sources = ChildProcessSession.cpp \
-                        $(shared_sources)
-
-loolbroker_SOURCES = LOOLBroker.cpp \
-                     $(broker_shared_sources)
+loolbroker_SOURCES = LOOLForKit.cpp \
+		     LOOLKit.cpp \
+                     $(shared_sources)
 
 loolmount_SOURCES = loolmount.c
 
@@ -64,6 +62,7 @@ noinst_HEADERS = Admin.hpp \
                  IoUtil.hpp \
                  LoadTest.hpp \
                  LOKitHelper.hpp \
+		 LOOLKit.hpp \
                  LOOLProtocol.hpp \
                  LOOLSession.hpp \
                  LOOLWSD.hpp \


More information about the Libreoffice-commits mailing list