From eca990920af7dc034ca1a263416aea8f3f9251d0 Mon Sep 17 00:00:00 2001 From: Jaume Pujantell Date: Wed, 29 Nov 2023 15:22:13 +0100 Subject: [PATCH] add logic to sapwn threads intead of processes Added code that spawns forkit and lokit in threads instead of new processes. Signed-off-by: Jaume Pujantell Change-Id: I6b6ad6dccfedaf28ebd0d68851205c540b9a0317 --- common/Log.cpp | 5 +-- kit/ForKit.cpp | 82 ++++++++++++++++++++++++------------------ wsd/coolwsd-inproc.cpp | 17 +++++++++ 3 files changed, 68 insertions(+), 36 deletions(-) diff --git a/common/Log.cpp b/common/Log.cpp index e730aa891..d1847ee4d 100644 --- a/common/Log.cpp +++ b/common/Log.cpp @@ -611,8 +611,9 @@ namespace Log void shutdown() { #if !MOBILEAPP - assert(ThreadLocalBufferCount <= 1 && - "Unstopped threads may have unflushed buffered log entries"); + if (!Util::isKitInProcess()) + assert(ThreadLocalBufferCount <= 1 && + "Unstopped threads may have unflushed buffered log entries"); IsShutdown = true; diff --git a/kit/ForKit.cpp b/kit/ForKit.cpp index 7a26cc9fb..8368ff732 100644 --- a/kit/ForKit.cpp +++ b/kit/ForKit.cpp @@ -385,6 +385,21 @@ static void cleanupChildren() } } +void sleepForDebugger() +{ + if (std::getenv("SLEEPKITFORDEBUGGER")) + { + const size_t delaySecs = std::stoul(std::getenv("SLEEPKITFORDEBUGGER")); + if (delaySecs > 0) + { + std::cerr << "Kit: Sleeping " << delaySecs + << " seconds to give you time to attach debugger to process " << getpid() + << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(delaySecs)); + } + } +} + static int createLibreOfficeKit(const std::string& childRoot, const std::string& sysTemplate, const std::string& loTemplate, @@ -403,49 +418,48 @@ static int createLibreOfficeKit(const std::string& childRoot, << spareKitId << '.'); const auto startForkingTime = std::chrono::steady_clock::now(); - const pid_t pid = fork(); - if (!pid) + pid_t pid = 0; + if (Util::isKitInProcess()) { - // Child - - // Close the pipe from coolwsd - close(0); - -#ifndef KIT_IN_PROCESS - UnitKit::get().postFork(); -#endif - - if (std::getenv("SLEEPKITFORDEBUGGER")) - { - const size_t delaySecs = std::stoul(std::getenv("SLEEPKITFORDEBUGGER")); - if (delaySecs > 0) - { - std::cerr << "Kit: Sleeping " << delaySecs - << " seconds to give you time to attach debugger to process " - << getpid() << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(delaySecs)); - } - } - - lokit_main(childRoot, jailId, sysTemplate, loTemplate, NoCapsForKit, NoSeccomp, - queryVersion, DisplayVersion, spareKitId); + std::thread([childRoot, jailId, sysTemplate, loTemplate, queryVersion] { + sleepForDebugger(); + lokit_main(childRoot, jailId, sysTemplate, loTemplate, true, true, queryVersion, + DisplayVersion, spareKitId); + }) + .detach(); } else { - // Parent - if (pid < 0) + pid = fork(); + if (!pid) { - LOG_SYS("Fork failed"); + // Child + + // Close the pipe from coolwsd + close(0); + + UnitKit::get().postFork(); + + sleepForDebugger(); + + lokit_main(childRoot, jailId, sysTemplate, loTemplate, NoCapsForKit, NoSeccomp, + queryVersion, DisplayVersion, spareKitId); } else { - LOG_INF("Forked kit [" << pid << ']'); - childJails[pid] = childRoot + jailId; - } + // Parent + if (pid < 0) + { + LOG_SYS("Fork failed"); + } + else + { + LOG_INF("Forked kit [" << pid << ']'); + childJails[pid] = childRoot + jailId; + } -#ifndef KIT_IN_PROCESS - UnitKit::get().launchedKit(pid); -#endif + UnitKit::get().launchedKit(pid); + } } const auto duration = (std::chrono::steady_clock::now() - startForkingTime); diff --git a/wsd/coolwsd-inproc.cpp b/wsd/coolwsd-inproc.cpp index 25f06b906..9dc6fa80f 100644 --- a/wsd/coolwsd-inproc.cpp +++ b/wsd/coolwsd-inproc.cpp @@ -15,5 +15,22 @@ int createForkit(const std::string& forKitPath, const StringVector& args) { // create forkit in a thread + int argc = args.size() + 1; + char** argv = new char*[argc]; + + argv[0] = new char[forKitPath.size() + 1]; + std::strcpy(argv[0], forKitPath.c_str()); + for (size_t i = 0; i < args.size(); ++i) + { + argv[i + 1] = new char[args[i].size() + 1]; + std::strcpy(argv[i + 1], args[i].c_str()); + } + + std::thread([argc, argv] { + Util::setThreadName("forkit"); + forkit_main(argc, argv); + }) + .detach(); + return 0; }