loolwsd/LOOLBroker.cpp | 38 ++++++++++++++------ loolwsd/LOOLKit.cpp | 89 +++++++++++++++++++++++++++++-------------------- 2 files changed, 79 insertions(+), 48 deletions(-)
New commits: commit d855d3b63614631b9fe2d65dfd0b4234c6d80850 Author: Michael Meeks <michael.me...@collabora.com> Date: Mon Aug 17 17:12:45 2015 +0100 Make LOOLKit re-usable more easily. diff --git a/loolwsd/LOOLBroker.cpp b/loolwsd/LOOLBroker.cpp index b245376..70ac266 100644 --- a/loolwsd/LOOLBroker.cpp +++ b/loolwsd/LOOLBroker.cpp @@ -34,6 +34,13 @@ #include "Util.hpp" +// First include the grist of the helper process - ideally +// we can avoid execve and share lots of memory here. We +// can't link to a non-PIC translation unit though, so +// include to share. +#define LOOKIT_NO_MAIN 1 +#include "LOOLKit.cpp" + #define INTERVAL_PROBES 10 #define MAINTENANCE_INTERVAL 1 @@ -215,13 +222,18 @@ static std::map<Poco::Process::PID, Poco::UInt64> _childProcesses; static int prefixcmp(const char *str, const char *prefix) { - for (; ; str++, prefix++) - if (!*prefix) - return 0; - else if (*str != *prefix) - return (unsigned char)*prefix - (unsigned char)*str; + for (; ; str++, prefix++) + if (!*prefix) + return 0; + else if (*str != *prefix) + return (unsigned char)*prefix - (unsigned char)*str; } +/// Initializes LibreOfficeKit for cross-fork re-use. +static bool globalPreinit() +{ + return false; +} static int createLibreOfficeKit(std::string loSubPath, Poco::UInt64 childID) { @@ -266,12 +278,11 @@ int main(int argc, char** argv) while (argc > 0) { - char *cmd = argv[0]; - char *eq = NULL; - if (strstr(cmd, "loolbroker")) + char *cmd = argv[0]; + char *eq = NULL; + if (strstr(cmd, "loolbroker")) { - - } + } if (!prefixcmp(cmd, "--losubpath=")) { eq = strchrnul(cmd, '='); @@ -303,8 +314,8 @@ int main(int argc, char** argv) _numPreSpawnedChildren = std::stoi(std::string(++eq)); } - argv++; - argc--; + argv++; + argc--; } if (loSubPath.empty()) @@ -337,6 +348,9 @@ int main(int argc, char** argv) exit(1); } + + globalPreinit(); + std::unique_lock<std::mutex> rngLock(_rngMutex); Poco::UInt64 _childId = (((Poco::UInt64)_rng.next()) << 32) | _rng.next() | 1; rngLock.unlock(); diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp index e47ca4d..8315440 100644 --- a/loolwsd/LOOLKit.cpp +++ b/loolwsd/LOOLKit.cpp @@ -7,12 +7,15 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +/* + * NB. this file is compiled both standalone, and as part of the LOOLBroker. + */ + #include <sys/prctl.h> #include <memory> #include <iostream> -#include <Poco/NamedMutex.h> #include <Poco/Util/Application.h> #include <Poco/Net/WebSocket.h> #include <Poco/Net/HTTPClientSession.h> @@ -89,42 +92,10 @@ static int prefixcmp(const char *str, const char *prefix) const int MASTER_PORT_NUMBER = 9981; const std::string CHILD_URI = "/loolws/child/"; -int main(int argc, char** argv) +void run_lok_main(const std::string &loSubPath, Poco::UInt64 _childId) { - std::string loSubPath; - Poco::UInt64 _childId = 0; - - while (argc > 0) - { - char *cmd = argv[0]; - char *eq = NULL; - if (!prefixcmp(cmd, "--losubpath=")) - { - eq = strchrnul(cmd, '='); - if (*eq) - loSubPath = std::string(++eq); - } - else if (!prefixcmp(cmd, "--child=")) - { - eq = strchrnul(cmd, '='); - if (*eq) - _childId = std::stoll(std::string(++eq)); - } - argv++; - argc--; - } - - if (loSubPath.empty()) - { - std::cout << Util::logPrefix() << "--losubpath is empty" << std::endl; - exit(1); - } - - if ( !_childId ) - { - std::cout << Util::logPrefix() << "--child is 0" << std::endl; - exit(1); - } + assert (_childId != 0); + assert (!loSubPath.empty()); try { @@ -211,7 +182,53 @@ int main(int argc, char** argv) } std::cout << Util::logPrefix() << "loolkit finished OK!" << std::endl; +} + +#ifdef LOOLKIT_NO_MAIN + +/// Simple argument parsing wrapper / helper for the above. +int main(int argc, char** argv) +{ + std::string loSubPath; + Poco::UInt64 _childId = 0; + + while (argc > 0) + { + char *cmd = argv[0]; + char *eq = NULL; + if (!prefixcmp(cmd, "--losubpath=")) + { + eq = strchrnul(cmd, '='); + if (*eq) + loSubPath = std::string(++eq); + } + else if (!prefixcmp(cmd, "--child=")) + { + eq = strchrnul(cmd, '='); + if (*eq) + _childId = std::stoll(std::string(++eq)); + } + argv++; + argc--; + } + + if (loSubPath.empty()) + { + std::cout << Util::logPrefix() << "--losubpath is empty" << std::endl; + exit(1); + } + + if ( !_childId ) + { + std::cout << Util::logPrefix() << "--child is 0" << std::endl; + exit(1); + } + + run_lok_main(loSubPath, _childId); + return 0; } +#endif + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits