diff --git a/Makefile.am b/Makefile.am index 191a9b407f9c2..b41db4b710766 100644 --- a/Makefile.am +++ b/Makefile.am @@ -146,7 +146,7 @@ coolwsd_sources = common/Crypto.cpp \ coolwsd_SOURCES = $(coolwsd_sources) \ $(shared_sources) \ - wsd/COOLWSD-main.cpp + wsd/COOLWSD-main.cpp EXTRA_coolwsd_DEPENDENCIES = browser/node_modules @@ -189,14 +189,17 @@ coolforkit_sources = kit/ChildSession.cpp \ coolforkit_SOURCES = $(coolforkit_sources) \ $(shared_sources) \ - kit/ForKitMain.cpp + kit/ForKitMain.cpp coolforkit_LDADD = libsimd.a +coolwsddebug_CPPFLAGS = -DKIT_IN_PROCESS=1 \ + $(AM_CPPFLAGS) + coolwsddebug_SOURCES = $(coolwsd_sources) \ - $(shared_sources) \ - $(coolforkit_sources) \ - wsd/debug-main.cpp + $(shared_sources) \ + $(coolforkit_sources) \ + wsd/debug-main.cpp if ENABLE_LIBFUZZER coolforkit_SOURCES += \ @@ -500,6 +503,17 @@ run-one: setup-wsd --o:security.capabilities="$(CAPABILITIES)" \ --o:logging.file[@enable]=true --o:logging.level=trace \ --singlekit + +run-kitinprocess: setup-wsd + @echo "Launching coolwsddebug" + ./coolwsddebug $(COMMON_PARAMS) \ + --o:logging.file[@enable]=false --o:logging.level=trace + +run-valgrind-kitinprocess: setup-wsd + @echo "Launching coolwsd under valgrind for single KIT IN PROCESS" + valgrind --tool=memcheck --leak-check=full --trace-children=no -v --read-var-info=yes \ + ./coolwsddebug $(COMMON_PARAMS) \ + --o:logging.file[@enable]=false --o:logging.level=trace endif sync-writer: @@ -517,13 +531,6 @@ run-trace: setup-wsd --o:trace[@enable]=true --o:trace.path=${builddir}/trace.txt.gz \ --o:trace.outgoing.record=false -run-valgrind-kitinprocess: setup-wsd - @echo "Launching coolwsd under valgrind for single KIT IN PROCESS" - KIT_IN_PROCESS=true valgrind --tool=memcheck --leak-check=full --trace-children=no -v --read-var-info=yes \ - ./coolwsddebug $(COMMON_PARAMS) \ - --o:logging.file[@enable]=false --o:logging.level=trace - - run-valgrind: setup-wsd @echo "Launching coolwsd under valgrind (but not forkit/coolkit, yet)" valgrind --tool=memcheck --trace-children=no -v --read-var-info=yes \ diff --git a/common/Unit.cpp b/common/Unit.cpp index c3a4306d6c2eb..d95be72105c49 100644 --- a/common/Unit.cpp +++ b/common/Unit.cpp @@ -685,6 +685,11 @@ UnitKit& UnitKit::get() if (!GlobalKit) GlobalKit = new UnitKit("UnitKit"); #endif + if (Util::isKitInProcess()) + { + if (!GlobalKit) + GlobalKit = new UnitKit("UnitKit"); + } assert(GlobalKit); return *GlobalKit; diff --git a/common/Util.cpp b/common/Util.cpp index b9bbce528d5f9..24b0e51175fe2 100644 --- a/common/Util.cpp +++ b/common/Util.cpp @@ -328,8 +328,11 @@ namespace Util bool isKitInProcess() { - const char* kitInProcess = std::getenv("KIT_IN_PROCESS"); - return (kitInProcess != nullptr && strcmp(kitInProcess, "true") == 0); +#ifdef KIT_IN_PROCESS + return true; +#else + return false; +#endif } #if !MOBILEAPP diff --git a/common/Util.hpp b/common/Util.hpp index 0e0798c3247fa..d2c3787c2ca18 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -190,7 +190,7 @@ namespace Util bool windowingAvailable(); -#if !defined(BUILDING_TESTS) && !defined(KIT_IN_PROCESS) && !MOBILEAPP +#if !defined(BUILDING_TESTS) && !MOBILEAPP /// Send a message to all clients. void alertAllUsers(const std::string& msg); diff --git a/common/security.h b/common/security.h index f7fff2c61ead2..bf249e831d5a5 100644 --- a/common/security.h +++ b/common/security.h @@ -24,7 +24,6 @@ # error "include config.h for user id"; #endif -#ifndef KIT_IN_PROCESS inline int hasUID(const char *userId) { struct passwd *pw = getpwuid(getuid()); @@ -104,6 +103,5 @@ inline int hasAnyCapability() return 0; #endif } -#endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/kit/ForKit.cpp b/kit/ForKit.cpp index d67221f5699ca..b18936ace0c88 100644 --- a/kit/ForKit.cpp +++ b/kit/ForKit.cpp @@ -56,8 +56,9 @@ static bool NoSeccomp = false; static bool SingleKit = false; #endif #else -static const bool NoCapsForKit = true; // NoCaps for in-process kit. -static const bool NoSeccomp = true; // NoSeccomp for in-process kit. +static bool NoCapsForKit = true; // NoCaps for in-process kit. +static bool NoSeccomp = true; // NoSeccomp for in-process kit. +static bool SingleKit = true; #endif static std::string UserInterface; @@ -196,7 +197,6 @@ class ServerWSHandler final : public WebSocketHandler } }; -#ifndef KIT_IN_PROCESS #ifndef __FreeBSD__ static bool haveCapability(cap_value_t capability) { @@ -275,7 +275,6 @@ static bool haveCorrectCapabilities() return getuid() == 0; } #endif // __FreeBSD__ -#endif /// Check if some previously forked kids have died. static void cleanupChildren() @@ -360,6 +359,7 @@ static void cleanupChildren() cleanupJailPaths.erase(cleanupJailPaths.begin() + i); } } + // lokit_main should only call once, there maybe chances createLibreOfficeKit called more then once // and that leads to execute multiple threads static bool lokitProcessIsRunning = false; @@ -382,17 +382,19 @@ static int createLibreOfficeKit(const std::string& childRoot, << spareKitId << '.'); const auto startForkingTime = std::chrono::steady_clock::now(); pid_t pid = 0; - if (Util::isKitInProcess() && !lokitProcessIsRunning) + if (Util::isKitInProcess()) { - std::thread( - [childRoot, jailId, sysTemplate, loTemplate, queryVersion] - { - Util::setThreadName("kit_in_process"); - lokit_main(childRoot, jailId, sysTemplate, loTemplate, true, true, - queryVersion, DisplayVersion, spareKitId); - }) - .detach(); - lokitProcessIsRunning = true; + if (!lokitProcessIsRunning) + { + lokitProcessIsRunning = true; + std::thread( + [childRoot, jailId, sysTemplate, loTemplate, queryVersion] + { + lokit_main(childRoot, jailId, sysTemplate, loTemplate, true, true, + queryVersion, DisplayVersion, spareKitId); + }) + .detach(); + } } else { @@ -403,10 +405,6 @@ static int createLibreOfficeKit(const std::string& childRoot, // Close the pipe from coolwsd close(0); - if (!Util::isKitInProcess()) - { - UnitKit::get().postFork(); - } if (std::getenv("SLEEPKITFORDEBUGGER")) { @@ -435,10 +433,7 @@ static int createLibreOfficeKit(const std::string& childRoot, LOG_INF("Forked kit [" << pid << ']'); childJails[pid] = childRoot + jailId; } - if (!Util::isKitInProcess()) - { - UnitKit::get().launchedKit(pid); - } + UnitKit::get().launchedKit(pid); } } @@ -458,11 +453,7 @@ void forkLibreOfficeKit(const std::string& childRoot, // Cleanup first, to reduce disk load. cleanupChildren(); - if (!Util::isKitInProcess()) - { - (void)limit; - } - else + if (Util::isKitInProcess()) { if (limit > 0) ForkCounter = limit; @@ -486,7 +477,6 @@ void forkLibreOfficeKit(const std::string& childRoot, } } -#ifndef KIT_IN_PROCESS static void printArgumentHelp() { std::cout << "Usage: coolforkit [OPTION]..." << std::endl; @@ -496,9 +486,7 @@ static void printArgumentHelp() std::cout << "" << std::endl; } -extern "C" int forkit_main(int argc, char **argv); - -int forkit_main(int argc, char** argv) +extern "C" int forkit_main(int argc, char** argv) { /*WARNING: PRIVILEGED CODE CHECKING START */ @@ -810,6 +798,5 @@ int forkit_main(int argc, char** argv) LOG_INF("ForKit process finished."); Util::forcedExit(returnValue); } -#endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/kit/Kit.cpp b/kit/Kit.cpp index ca96900cd682b..57ccab7eb963d 100644 --- a/kit/Kit.cpp +++ b/kit/Kit.cpp @@ -3043,11 +3043,7 @@ void lokit_main( { const char *instdir = instdir_path.c_str(); const char *userdir = userdir_url.c_str(); -#ifndef KIT_IN_PROCESS kit = UnitKit::get().lok_init(instdir, userdir); -#else - kit = nullptr; -#endif if (!kit) { kit = (initFunction ? initFunction(instdir, userdir) diff --git a/wsd/COOLWSD.cpp b/wsd/COOLWSD.cpp index 9bb27839df3ee..66df70c6bdd00 100644 --- a/wsd/COOLWSD.cpp +++ b/wsd/COOLWSD.cpp @@ -438,13 +438,17 @@ static int forkChildren(const int number) { COOLWSD::checkDiskSpaceAndWarnClients(false); -#ifndef KIT_IN_PROCESS - createLibreOfficeKit(COOLWSD::ChildRoot, COOLWSD::SysTemplate, COOLWSD::LoTemplate, number); -#else - const std::string aMessage = "spawn " + std::to_string(number) + '\n'; - LOG_DBG("MasterToForKit: " << aMessage.substr(0, aMessage.length() - 1)); - COOLWSD::sendMessageToForKit(aMessage); -#endif + if (Util::isKitInProcess()) + { + createLibreOfficeKit(COOLWSD::ChildRoot, COOLWSD::SysTemplate, COOLWSD::LoTemplate, number); + } + else + { + const std::string aMessage = "spawn " + std::to_string(number) + '\n'; + LOG_DBG("MasterToForKit: " << aMessage.substr(0, aMessage.length() - 1)); + COOLWSD::sendMessageToForKit(aMessage); + } + OutstandingForks += number; LastForkRequestTime = std::chrono::steady_clock::now(); return number; @@ -872,10 +876,8 @@ void sendLoadResult(std::shared_ptr clientSession, bool success, std::atomic COOLWSD::NextConnectionId(1); #if !MOBILEAPP -#ifndef KIT_IN_PROCESS std::atomic COOLWSD::ForKitProcId(-1); std::shared_ptr COOLWSD::ForKitProc; -#endif bool COOLWSD::NoCapsForKit = false; bool COOLWSD::NoSeccomp = false; bool COOLWSD::AdminEnabled = true; @@ -6293,7 +6295,7 @@ std::set COOLWSD::getKitPids() return pids; } -#if !defined(BUILDING_TESTS) && !defined(KIT_IN_PROCESS) +#if !defined(BUILDING_TESTS) namespace Util { diff --git a/wsd/Storage.cpp b/wsd/Storage.cpp index db99a22cf4cb3..127dafbe06ef4 100644 --- a/wsd/Storage.cpp +++ b/wsd/Storage.cpp @@ -412,17 +412,10 @@ std::string LocalStorage::downloadStorageFileToLocal(const Authorization& /*auth setDownloaded(true); // Now return the jailed path. - if (!Util::isKitInProcess()) - { - if (COOLWSD::NoCapsForKit) - return getRootFilePath(); - else - return Poco::Path(getJailPath(), filename).toString(); - } - else - { + if (COOLWSD::NoCapsForKit) return getRootFilePath(); - } + else + return Poco::Path(getJailPath(), filename).toString(); #else // MOBILEAPP // In the mobile app we use no jail diff --git a/wsd/debug-main.cpp b/wsd/debug-main.cpp index 466a09b50c1e6..3850e3eaeb5e3 100644 --- a/wsd/debug-main.cpp +++ b/wsd/debug-main.cpp @@ -28,7 +28,6 @@ char** convertStringVectorToCharArray(StringVector stringVector) extern "C" int createForkit(const std::string forKitPath, const StringVector args) { - LOG_INF(forKitPath); char** argv = convertStringVectorToCharArray(args);