2016-04-05 08:32:10 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* A very simple, single threaded helper to efficiently pre-init and
|
|
|
|
* spawn lots of kits as children.
|
|
|
|
*/
|
|
|
|
|
2016-04-12 04:00:33 -05:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
#include <sys/capability.h>
|
|
|
|
#include <sys/stat.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2016-04-05 08:32:10 -05:00
|
|
|
|
2016-04-18 06:02:36 -05:00
|
|
|
#include <atomic>
|
2016-04-05 08:32:10 -05:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <map>
|
2016-04-18 06:02:36 -05:00
|
|
|
#include <set>
|
2016-04-05 08:32:10 -05:00
|
|
|
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
#include <Poco/Thread.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
|
|
|
|
#include "Common.hpp"
|
2016-11-12 15:38:13 -06:00
|
|
|
#include "common/FileUtil.hpp"
|
2016-04-05 08:32:10 -05:00
|
|
|
#include "IoUtil.hpp"
|
|
|
|
#include "LOOLKit.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2016-04-09 11:30:48 -05:00
|
|
|
#include "Unit.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Util.hpp"
|
2016-04-12 04:00:33 -05:00
|
|
|
#include "security.h"
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
using Poco::Process;
|
|
|
|
using Poco::StringTokenizer;
|
|
|
|
using Poco::Thread;
|
|
|
|
using Poco::Util::Application;
|
|
|
|
|
2016-04-16 14:44:53 -05:00
|
|
|
static bool NoCapsForKit = false;
|
2016-10-06 05:17:36 -05:00
|
|
|
static bool DisplayVersion = false;
|
2016-04-09 11:30:48 -05:00
|
|
|
static std::string UnitTestLibrary;
|
2016-04-05 08:32:10 -05:00
|
|
|
static std::atomic<unsigned> ForkCounter( 0 );
|
|
|
|
|
2016-04-18 06:02:36 -05:00
|
|
|
static std::map<Process::PID, std::string> childJails;
|
|
|
|
|
2016-04-14 12:04:19 -05:00
|
|
|
int ClientPortNumber = DEFAULT_CLIENT_PORT_NUMBER;
|
2016-05-04 06:06:34 -05:00
|
|
|
int MasterPortNumber = DEFAULT_MASTER_PORT_NUMBER;
|
2016-04-14 12:04:19 -05:00
|
|
|
|
2016-08-13 22:59:10 -05:00
|
|
|
/// Dispatcher class to demultiplex requests from
|
|
|
|
/// WSD and handles them.
|
|
|
|
class CommandDispatcher : public IoUtil::PipeReader
|
2016-04-05 08:32:10 -05:00
|
|
|
{
|
|
|
|
public:
|
2016-08-13 22:59:10 -05:00
|
|
|
CommandDispatcher(const int pipe) :
|
2016-04-18 01:41:19 -05:00
|
|
|
PipeReader("wsd_pipe_rd", pipe)
|
2016-04-05 08:32:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-08-13 22:59:10 -05:00
|
|
|
/// Polls WSD commands and handles them.
|
2016-04-05 08:32:10 -05:00
|
|
|
bool pollAndDispatch()
|
|
|
|
{
|
2016-08-13 22:59:10 -05:00
|
|
|
std::string message;
|
|
|
|
const auto ready = readLine(message, [](){ return TerminationFlag.load(); });
|
2016-04-17 11:05:56 -05:00
|
|
|
if (ready == 0)
|
|
|
|
{
|
|
|
|
// Timeout.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (ready < 0)
|
|
|
|
{
|
|
|
|
// Termination is done via SIGINT, which breaks the wait.
|
|
|
|
if (!TerminationFlag)
|
|
|
|
{
|
|
|
|
Log::error("Error reading from pipe [" + getName() + "].");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:27:43 -05:00
|
|
|
Log::info("ForKit command: [" + message + "].");
|
2016-04-05 08:32:10 -05:00
|
|
|
StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
if (tokens[0] == "spawn" && tokens.count() == 2)
|
|
|
|
{
|
|
|
|
const auto count = std::stoi(tokens[1]);
|
2016-04-09 15:53:33 -05:00
|
|
|
if (count > 0)
|
|
|
|
{
|
2016-04-12 06:48:26 -05:00
|
|
|
Log::info("Spawning " + tokens[1] + " " + (count == 1 ? "child" : "children") + " per request.");
|
2016-04-09 15:53:33 -05:00
|
|
|
ForkCounter = count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-12 06:48:26 -05:00
|
|
|
Log::warn("Cannot spawn " + tokens[1] + " children as requested.");
|
2016-04-09 15:53:33 -05:00
|
|
|
}
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
2016-08-13 22:59:10 -05:00
|
|
|
|
|
|
|
return true;
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-12 06:19:42 -05:00
|
|
|
static bool haveCapability(cap_value_t capability)
|
|
|
|
{
|
|
|
|
cap_t caps = cap_get_proc();
|
|
|
|
|
|
|
|
if (caps == nullptr)
|
|
|
|
{
|
2016-11-13 10:59:34 -06:00
|
|
|
LOG_SFL("cap_get_proc() failed.");
|
2016-10-12 06:19:42 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *cap_name = cap_to_name(capability);
|
|
|
|
cap_flag_value_t value;
|
|
|
|
|
|
|
|
if (cap_get_flag(caps, capability, CAP_EFFECTIVE, &value) == -1)
|
|
|
|
{
|
|
|
|
if (cap_name)
|
|
|
|
{
|
2016-11-13 10:59:34 -06:00
|
|
|
LOG_SFL("cap_get_flag failed for " << cap_name << ".");
|
2016-10-12 06:19:42 -05:00
|
|
|
cap_free(cap_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-13 10:59:34 -06:00
|
|
|
LOG_SFL("cap_get_flag failed for capability " << capability << ".");
|
2016-10-12 06:19:42 -05:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value != CAP_SET)
|
|
|
|
{
|
|
|
|
if (cap_name)
|
|
|
|
{
|
2016-10-14 04:57:27 -05:00
|
|
|
Log::fatal("Capability " + std::string(cap_name) + " is not set for the loolforkit program.");
|
2016-10-12 06:19:42 -05:00
|
|
|
cap_free(cap_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-12 07:13:26 -05:00
|
|
|
Log::error("Capability " + std::to_string(capability) + " is not set for the loolforkit program.");
|
2016-10-12 06:19:42 -05:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap_name)
|
|
|
|
{
|
|
|
|
Log::info("Have capability " + std::string(cap_name));
|
|
|
|
cap_free(cap_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::info("Have capability " + std::to_string(capability));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool haveCorrectCapabilities()
|
|
|
|
{
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
// Do check them all, don't shortcut with &&
|
|
|
|
if (!haveCapability(CAP_SYS_CHROOT))
|
|
|
|
result = false;
|
|
|
|
if (!haveCapability(CAP_MKNOD))
|
|
|
|
result = false;
|
2016-10-12 07:15:39 -05:00
|
|
|
if (!haveCapability(CAP_FOWNER))
|
2016-10-12 06:19:42 -05:00
|
|
|
result = false;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-19 19:27:24 -05:00
|
|
|
/// Check if some previously forked kids have died.
|
|
|
|
static void cleanupChildren()
|
|
|
|
{
|
|
|
|
Process::PID exitedChildPid;
|
|
|
|
int status;
|
2016-10-15 16:08:55 -05:00
|
|
|
while ((exitedChildPid = waitpid(-1, &status, WUNTRACED | WNOHANG)) > 0)
|
2016-05-19 19:27:24 -05:00
|
|
|
{
|
|
|
|
if (childJails.find(exitedChildPid) != childJails.end())
|
|
|
|
{
|
|
|
|
Log::info("Child " + std::to_string(exitedChildPid) + " has exited, removing its jail '" + childJails[exitedChildPid] + "'");
|
2016-11-12 15:38:13 -06:00
|
|
|
FileUtil::removeFile(childJails[exitedChildPid], true);
|
2016-05-19 19:27:24 -05:00
|
|
|
childJails.erase(exitedChildPid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::error("Unknown child " + std::to_string(exitedChildPid) + " has exited");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
static int createLibreOfficeKit(const std::string& childRoot,
|
|
|
|
const std::string& sysTemplate,
|
|
|
|
const std::string& loTemplate,
|
2016-06-20 13:58:00 -05:00
|
|
|
const std::string& loSubPath,
|
|
|
|
bool queryVersion = false)
|
2016-04-05 08:32:10 -05:00
|
|
|
{
|
|
|
|
Log::debug("Forking a loolkit process.");
|
|
|
|
|
|
|
|
Process::PID pid;
|
|
|
|
if (!(pid = fork()))
|
|
|
|
{
|
2016-10-13 05:10:09 -05:00
|
|
|
// Child
|
|
|
|
|
2016-10-12 10:15:13 -05:00
|
|
|
// Close the pipe from loolwsd
|
|
|
|
close(0);
|
2016-04-05 08:51:22 -05:00
|
|
|
|
2016-04-09 12:26:33 -05:00
|
|
|
UnitKit::get().postFork();
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
if (std::getenv("SLEEPKITFORDEBUGGER"))
|
|
|
|
{
|
2016-10-22 18:04:07 -05:00
|
|
|
const auto delaySecs = std::stoul(std::getenv("SLEEPKITFORDEBUGGER"));
|
|
|
|
if (delaySecs > 0)
|
|
|
|
{
|
|
|
|
std::cerr << "Sleeping " << delaySecs
|
|
|
|
<< " seconds to give you time to attach debugger to process "
|
|
|
|
<< Process::id() << std::endl;
|
|
|
|
Thread::sleep(delaySecs * 1000);
|
|
|
|
}
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
2016-10-06 05:17:36 -05:00
|
|
|
lokit_main(childRoot, sysTemplate, loTemplate, loSubPath, NoCapsForKit, queryVersion, DisplayVersion);
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-18 06:02:36 -05:00
|
|
|
// Parent
|
2016-04-07 02:36:38 -05:00
|
|
|
if (pid < 0)
|
2016-05-19 19:27:24 -05:00
|
|
|
{
|
2016-11-13 10:59:34 -06:00
|
|
|
LOG_SYS("Fork failed.");
|
2016-05-19 19:27:24 -05:00
|
|
|
}
|
2016-04-07 02:36:38 -05:00
|
|
|
else
|
2016-04-18 06:02:36 -05:00
|
|
|
{
|
2016-04-08 05:09:06 -05:00
|
|
|
Log::info("Forked kit [" + std::to_string(pid) + "].");
|
2016-04-18 06:02:36 -05:00
|
|
|
childJails[pid] = childRoot + std::to_string(pid);
|
|
|
|
}
|
2016-04-13 09:10:02 -05:00
|
|
|
|
|
|
|
UnitKit::get().launchedKit(pid);
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-08 05:09:06 -05:00
|
|
|
return pid;
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printArgumentHelp()
|
|
|
|
{
|
|
|
|
std::cout << "Usage: loolforkit [OPTION]..." << std::endl;
|
2016-04-07 03:27:43 -05:00
|
|
|
std::cout << " Single-threaded process that spawns lok instances" << std::endl;
|
|
|
|
std::cout << " Note: Running this standalone is not possible. It is spawned by loolwsd" << std::endl;
|
2016-04-05 08:32:10 -05:00
|
|
|
std::cout << " and is controlled via a pipe." << std::endl;
|
|
|
|
std::cout << "" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2016-04-12 04:00:33 -05:00
|
|
|
if (!hasCorrectUID("loolforkit"))
|
2016-04-17 18:00:55 -05:00
|
|
|
return Application::EXIT_SOFTWARE;
|
2016-04-12 04:00:33 -05:00
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
if (std::getenv("SLEEPFORDEBUGGER"))
|
|
|
|
{
|
2016-10-22 18:04:07 -05:00
|
|
|
const auto delaySecs = std::stoul(std::getenv("SLEEPFORDEBUGGER"));
|
|
|
|
if (delaySecs > 0)
|
|
|
|
{
|
|
|
|
std::cerr << "Sleeping " << delaySecs
|
|
|
|
<< " seconds to give you time to attach debugger to process "
|
|
|
|
<< Process::id() << std::endl;
|
|
|
|
Thread::sleep(delaySecs * 1000);
|
|
|
|
}
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialization
|
2016-10-12 03:47:26 -05:00
|
|
|
const bool logToFile = std::getenv("LOOL_LOGFILE");
|
|
|
|
const char* logFilename = std::getenv("LOOL_LOGFILENAME");
|
|
|
|
const char* logLevel = std::getenv("LOOL_LOGLEVEL");
|
|
|
|
const char* logColor = std::getenv("LOOL_LOGCOLOR");
|
2016-09-27 07:30:45 -05:00
|
|
|
std::map<std::string, std::string> logProperties;
|
|
|
|
if (logToFile && logFilename)
|
|
|
|
{
|
|
|
|
logProperties["path"] = std::string(logFilename);
|
|
|
|
}
|
|
|
|
|
2016-10-07 03:49:18 -05:00
|
|
|
Log::initialize("frk", logLevel ? logLevel : "", logColor != nullptr, logToFile, logProperties);
|
2016-04-05 08:32:10 -05:00
|
|
|
|
|
|
|
Util::setTerminationSignals();
|
|
|
|
Util::setFatalSignals();
|
|
|
|
|
|
|
|
std::string childRoot;
|
|
|
|
std::string loSubPath;
|
|
|
|
std::string sysTemplate;
|
|
|
|
std::string loTemplate;
|
|
|
|
|
2016-05-04 06:06:34 -05:00
|
|
|
#if ENABLE_DEBUG
|
2016-10-12 03:47:26 -05:00
|
|
|
static const char* clientPort = std::getenv("LOOL_TEST_CLIENT_PORT");
|
2016-05-04 06:06:34 -05:00
|
|
|
if (clientPort)
|
|
|
|
ClientPortNumber = std::stoi(clientPort);
|
2016-10-12 03:47:26 -05:00
|
|
|
static const char* masterPort = std::getenv("LOOL_TEST_MASTER_PORT");
|
2016-05-04 06:06:34 -05:00
|
|
|
if (masterPort)
|
|
|
|
MasterPortNumber = std::stoi(masterPort);
|
|
|
|
#endif
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
{
|
|
|
|
char *cmd = argv[i];
|
|
|
|
char *eq;
|
|
|
|
if (std::strstr(cmd, "--losubpath=") == cmd)
|
|
|
|
{
|
|
|
|
eq = std::strchr(cmd, '=');
|
|
|
|
loSubPath = std::string(eq+1);
|
|
|
|
}
|
|
|
|
else if (std::strstr(cmd, "--systemplate=") == cmd)
|
|
|
|
{
|
|
|
|
eq = std::strchr(cmd, '=');
|
|
|
|
sysTemplate = std::string(eq+1);
|
|
|
|
}
|
|
|
|
else if (std::strstr(cmd, "--lotemplate=") == cmd)
|
|
|
|
{
|
|
|
|
eq = std::strchr(cmd, '=');
|
|
|
|
loTemplate = std::string(eq+1);
|
|
|
|
}
|
|
|
|
else if (std::strstr(cmd, "--childroot=") == cmd)
|
|
|
|
{
|
|
|
|
eq = std::strchr(cmd, '=');
|
|
|
|
childRoot = std::string(eq+1);
|
|
|
|
}
|
|
|
|
else if (std::strstr(cmd, "--clientport=") == cmd)
|
|
|
|
{
|
|
|
|
eq = std::strchr(cmd, '=');
|
|
|
|
ClientPortNumber = std::stoll(std::string(eq+1));
|
|
|
|
}
|
2016-10-26 08:40:46 -05:00
|
|
|
else if (std::strstr(cmd, "--masterport=") == cmd)
|
|
|
|
{
|
|
|
|
eq = std::strchr(cmd, '=');
|
|
|
|
MasterPortNumber = std::stoll(std::string(eq+1));
|
|
|
|
}
|
2016-04-15 09:07:24 -05:00
|
|
|
else if (std::strstr(cmd, "--version") == cmd)
|
|
|
|
{
|
2016-06-20 04:51:35 -05:00
|
|
|
std::string version, hash;
|
|
|
|
Util::getVersionInfo(version, hash);
|
2016-10-06 05:17:36 -05:00
|
|
|
std::cout << "loolforkit version details: " << version << " - " << hash << std::endl;
|
|
|
|
DisplayVersion = true;
|
2016-04-15 09:07:24 -05:00
|
|
|
}
|
2016-04-12 04:00:33 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
// this process has various privileges - don't run arbitrary code.
|
2016-04-09 11:30:48 -05:00
|
|
|
else if (std::strstr(cmd, "--unitlib=") == cmd)
|
|
|
|
{
|
|
|
|
eq = std::strchr(cmd, '=');
|
|
|
|
UnitTestLibrary = std::string(eq+1);
|
|
|
|
}
|
2016-04-16 14:44:53 -05:00
|
|
|
// we are running in no-privilege mode - with no chroot etc.
|
|
|
|
else if (std::strstr(cmd, "--nocaps") == cmd)
|
|
|
|
{
|
|
|
|
NoCapsForKit = true;
|
|
|
|
}
|
2016-04-12 04:00:33 -05:00
|
|
|
#endif
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (loSubPath.empty() || sysTemplate.empty() ||
|
|
|
|
loTemplate.empty() || childRoot.empty())
|
|
|
|
{
|
|
|
|
printArgumentHelp();
|
2016-04-17 18:00:55 -05:00
|
|
|
return Application::EXIT_USAGE;
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
if (!UnitBase::init(UnitBase::UnitType::TYPE_KIT,
|
|
|
|
UnitTestLibrary))
|
|
|
|
{
|
|
|
|
Log::error("Failed to load kit unit test library");
|
|
|
|
return Application::EXIT_USAGE;
|
|
|
|
}
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
if (!std::getenv("LD_BIND_NOW"))
|
|
|
|
Log::info("Note: LD_BIND_NOW is not set.");
|
|
|
|
|
2016-10-12 06:49:01 -05:00
|
|
|
if (!haveCorrectCapabilities())
|
|
|
|
return Application::EXIT_SOFTWARE;
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
// Initialize LoKit
|
|
|
|
if (!globalPreinit(loTemplate))
|
2016-04-17 18:00:55 -05:00
|
|
|
std::_Exit(Application::EXIT_SOFTWARE);
|
2016-04-05 08:32:10 -05:00
|
|
|
|
|
|
|
Log::info("Preinit stage OK.");
|
|
|
|
|
|
|
|
// We must have at least one child, more are created dynamically.
|
2016-06-20 13:58:00 -05:00
|
|
|
// Ask this first child to send version information to master process
|
|
|
|
if (createLibreOfficeKit(childRoot, sysTemplate, loTemplate, loSubPath, true) < 0)
|
2016-04-05 08:32:10 -05:00
|
|
|
{
|
2016-10-14 04:57:27 -05:00
|
|
|
Log::fatal("Failed to create a kit process.");
|
2016-04-17 18:00:55 -05:00
|
|
|
std::_Exit(Application::EXIT_SOFTWARE);
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
2016-10-12 10:15:13 -05:00
|
|
|
CommandDispatcher commandDispatcher(0);
|
2016-04-07 03:27:43 -05:00
|
|
|
Log::info("ForKit process is ready.");
|
2016-04-05 08:32:10 -05:00
|
|
|
|
|
|
|
while (!TerminationFlag)
|
|
|
|
{
|
2016-04-09 12:26:33 -05:00
|
|
|
UnitKit::get().invokeForKitTest();
|
|
|
|
|
2016-08-13 22:59:10 -05:00
|
|
|
if (!commandDispatcher.pollAndDispatch())
|
2016-04-05 08:32:10 -05:00
|
|
|
{
|
|
|
|
Log::info("Child dispatcher flagged for termination.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ForkCounter > 0)
|
|
|
|
{
|
2016-04-05 21:37:29 -05:00
|
|
|
// Create as many as requested.
|
2016-04-05 08:32:10 -05:00
|
|
|
int spawn = ForkCounter;
|
|
|
|
Log::info() << "Creating " << spawn << " new child." << Log::end;
|
|
|
|
size_t newInstances = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (createLibreOfficeKit(childRoot, sysTemplate, loTemplate, loSubPath) < 0)
|
|
|
|
{
|
2016-04-07 03:04:05 -05:00
|
|
|
Log::error("Failed to create a kit process.");
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++newInstances;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (--spawn > 0);
|
|
|
|
|
|
|
|
// If we need to spawn more, retry later.
|
2016-04-05 21:37:29 -05:00
|
|
|
ForkCounter = (newInstances >= ForkCounter ? 0 : ForkCounter - newInstances);
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
2016-10-15 16:08:55 -05:00
|
|
|
|
|
|
|
// We virtually always fork when a child exits.
|
|
|
|
cleanupChildren();
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
int returnValue = Application::EXIT_OK;
|
|
|
|
UnitKit::get().returnValue(returnValue);
|
|
|
|
|
2016-04-07 03:27:43 -05:00
|
|
|
Log::info("ForKit process finished.");
|
2016-04-17 11:02:32 -05:00
|
|
|
std::_Exit(returnValue);
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|