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 <jaume.pujantell@collabora.com>
Change-Id: I6b6ad6dccfedaf28ebd0d68851205c540b9a0317
This commit is contained in:
Jaume Pujantell 2023-11-29 15:22:13 +01:00 committed by Caolán McNamara
parent 8f178ef6cd
commit eca990920a
3 changed files with 68 additions and 36 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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;
}