From 030acb1a8562bc41d84e4ba3639d9f0666007f68 Mon Sep 17 00:00:00 2001 From: Michael Meeks Date: Tue, 16 Apr 2024 15:49:04 +0100 Subject: [PATCH] bgsave: Add SLEEPBACKGROUNDFORDEBUGGER environment variable. Change-Id: I7284d03ddcd79a0848d1cca9b219e2ef96548511 Signed-off-by: Michael Meeks --- common/Util.cpp | 20 +++++++++++++++++++- common/Util.hpp | 3 +++ kit/ForKit.cpp | 24 ++---------------------- kit/Kit.cpp | 2 ++ wsd/README.vars | 8 ++++++++ 5 files changed, 34 insertions(+), 23 deletions(-) diff --git a/common/Util.cpp b/common/Util.cpp index ba8cc92911..f5ee9ae6fb 100644 --- a/common/Util.cpp +++ b/common/Util.cpp @@ -957,6 +957,24 @@ namespace Util assert(sameThread); } -} + + void sleepFromEnvIfSet(const char *domain, const char *envVar) + { + const char *value; + if ((value = std::getenv(envVar))) + { + const size_t delaySecs = std::stoul(value); + if (delaySecs > 0) + { + std::cerr << domain << ": Sleeping " << delaySecs + << " seconds to give you time to attach debugger to process " + << getpid() << std::endl + << "sudo gdb --pid=" << getpid() << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(delaySecs)); + } + } + } + +} // namespace Util /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/common/Util.hpp b/common/Util.hpp index b6b9778327..736dc153f5 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -1509,6 +1509,9 @@ int main(int argc, char**argv) */ int safe_atoi(const char* p, int len); + /// Sleep based on count of seconds in env. var + void sleepFromEnvIfSet(const char *domain, const char *envVar); + /// Close logs and forcefully exit with the given exit code. /// This calls std::_Exit, which terminates the program without cleaning up /// static instances (i.e. anything registered with `atexit' or `on_exit'). diff --git a/kit/ForKit.cpp b/kit/ForKit.cpp index 6da65d4daa..9e2d4c44c2 100644 --- a/kit/ForKit.cpp +++ b/kit/ForKit.cpp @@ -374,17 +374,7 @@ 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)); - } - } + Util::sleepFromEnvIfSet("Kit", "SLEEPKITFORDEBUGGER"); } static int createLibreOfficeKit(const std::string& childRoot, @@ -584,17 +574,7 @@ int forkit_main(int argc, char** argv) // * the user is a non-priviled user, the binary is not privileged // either (no caps set), and --disable-cool-user-checking was provided - if (std::getenv("SLEEPFORDEBUGGER")) - { - const size_t delaySecs = std::stoul(std::getenv("SLEEPFORDEBUGGER")); - if (delaySecs > 0) - { - std::cerr << "Forkit: Sleeping " << delaySecs - << " seconds to give you time to attach debugger to process " - << getpid() << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(delaySecs)); - } - } + Util::sleepFromEnvIfSet("Forkit", "SLEEPFORDEBUGGER"); if (!Util::isKitInProcess()) { diff --git a/kit/Kit.cpp b/kit/Kit.cpp index 8a6bdae47a..ae2ffae5d8 100644 --- a/kit/Kit.cpp +++ b/kit/Kit.cpp @@ -1425,6 +1425,8 @@ bool Document::forkToSave(const std::function &childSave, int viewId) childSocket.reset(); // now we just have a single socket to our parent + Util::sleepFromEnvIfSet("KitBackgroundSave", "SLEEPBACKGROUNDFORDEBUGGER"); + UnitKit::get().postBackgroundSaveFork(); // Hard drop our previous connections to coolwsd and shared wakeups.x diff --git a/wsd/README.vars b/wsd/README.vars index 866eac527a..f73d6f2023 100644 --- a/wsd/README.vars +++ b/wsd/README.vars @@ -25,6 +25,11 @@ SLEEPKITFORDEBUGGER to allow a 'sudo gdb' session to attach and debug that process. +SLEEPBACKGROUNDFORDEBUGGER + sleep seconds in each kit spawned background save process + after forking, to allow a 'sudo gdb' session to attach and debug + that process. + COOL_STORAGE_COOKIE Sets a cookie to all the requests made to storage. This is extremely useful for debugging WOPI implementations. For eg: Using XDebug with OwnCloud/NextCloud. @@ -33,3 +38,6 @@ COOL_SERVE_FROM_FS If mentioned, files are not loaded into the memory on start, instead they are read from the filesystem. This can be helpful in COOL development where you can tweak it without restarting wsd. + +COOL_FORCE_BGSAVE + If set, force all saves to be done in a background kit process.