Skip to content

Commit

Permalink
Some more WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Jaume Pujantell <[email protected]>
Change-Id: Ib08067e83ab08ce5bc525f069357218b3d3614db
  • Loading branch information
Jaume Pujantell committed Nov 3, 2023
1 parent 6e3a541 commit bf01942
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 74 deletions.
31 changes: 19 additions & 12 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 += \
Expand Down Expand Up @@ -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:
Expand All @@ -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 \
Expand Down
5 changes: 5 additions & 0 deletions common/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions common/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion common/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions common/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -104,6 +103,5 @@ inline int hasAnyCapability()
return 0;
#endif
}
#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
51 changes: 19 additions & 32 deletions kit/ForKit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -196,7 +197,6 @@ class ServerWSHandler final : public WebSocketHandler
}
};

#ifndef KIT_IN_PROCESS
#ifndef __FreeBSD__
static bool haveCapability(cap_value_t capability)
{
Expand Down Expand Up @@ -275,7 +275,6 @@ static bool haveCorrectCapabilities()
return getuid() == 0;
}
#endif // __FreeBSD__
#endif

/// Check if some previously forked kids have died.
static void cleanupChildren()
Expand Down Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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"))
{
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand All @@ -486,7 +477,6 @@ void forkLibreOfficeKit(const std::string& childRoot,
}
}

#ifndef KIT_IN_PROCESS
static void printArgumentHelp()
{
std::cout << "Usage: coolforkit [OPTION]..." << std::endl;
Expand All @@ -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 */

Expand Down Expand Up @@ -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: */
4 changes: 0 additions & 4 deletions kit/Kit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 12 additions & 10 deletions wsd/COOLWSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -872,10 +876,8 @@ void sendLoadResult(std::shared_ptr<ClientSession> clientSession, bool success,
std::atomic<uint64_t> COOLWSD::NextConnectionId(1);

#if !MOBILEAPP
#ifndef KIT_IN_PROCESS
std::atomic<int> COOLWSD::ForKitProcId(-1);
std::shared_ptr<ForKitProcess> COOLWSD::ForKitProc;
#endif
bool COOLWSD::NoCapsForKit = false;
bool COOLWSD::NoSeccomp = false;
bool COOLWSD::AdminEnabled = true;
Expand Down Expand Up @@ -6293,7 +6295,7 @@ std::set<pid_t> COOLWSD::getKitPids()
return pids;
}

#if !defined(BUILDING_TESTS) && !defined(KIT_IN_PROCESS)
#if !defined(BUILDING_TESTS)
namespace Util
{

Expand Down
13 changes: 3 additions & 10 deletions wsd/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion wsd/debug-main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit bf01942

Please sign in to comment.