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/wait.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <atomic>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <Poco/Path.h>
|
|
|
|
#include <Poco/Process.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
#include <Poco/Thread.h>
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
|
|
|
|
#include "Common.hpp"
|
|
|
|
#include "IoUtil.hpp"
|
|
|
|
#include "LOOLKit.hpp"
|
|
|
|
#include "Util.hpp"
|
2016-04-09 11:30:48 -05:00
|
|
|
#include "Unit.hpp"
|
2016-04-05 08:32:10 -05:00
|
|
|
#include "ChildProcessSession.hpp"
|
|
|
|
|
2016-04-12 04:00:33 -05:00
|
|
|
#include "security.h"
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
using Poco::Path;
|
|
|
|
using Poco::Process;
|
|
|
|
using Poco::StringTokenizer;
|
|
|
|
using Poco::Thread;
|
|
|
|
using Poco::Timestamp;
|
|
|
|
using Poco::Util::Application;
|
|
|
|
|
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-07 03:27:43 -05:00
|
|
|
static int pipeFd = -1;
|
2016-04-05 08:32:10 -05:00
|
|
|
|
|
|
|
class ChildDispatcher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChildDispatcher() :
|
2016-04-07 03:27:43 -05:00
|
|
|
_wsdPipeReader("wsd_pipe_rd", pipeFd)
|
2016-04-05 08:32:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Polls WSD commands and dispatches them to the appropriate child.
|
|
|
|
bool pollAndDispatch()
|
|
|
|
{
|
|
|
|
return _wsdPipeReader.processOnce([this](std::string& message) { handleInput(message); return true; },
|
|
|
|
[]() { return TerminationFlag; });
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handleInput(const std::string& message)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IoUtil::PipeReader _wsdPipeReader;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int createLibreOfficeKit(const std::string& childRoot,
|
|
|
|
const std::string& sysTemplate,
|
|
|
|
const std::string& loTemplate,
|
|
|
|
const std::string& loSubPath)
|
|
|
|
{
|
|
|
|
Log::debug("Forking a loolkit process.");
|
|
|
|
|
|
|
|
Process::PID pid;
|
|
|
|
if (!(pid = fork()))
|
|
|
|
{
|
2016-04-05 08:51:22 -05:00
|
|
|
// quicker than a generic socket closing approach.
|
2016-04-08 05:12:21 -05:00
|
|
|
// (but pipeFd is a pipe, not a socket...?)
|
2016-04-07 03:27:43 -05:00
|
|
|
close(pipeFd);
|
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
|
|
|
// child
|
|
|
|
if (std::getenv("SLEEPKITFORDEBUGGER"))
|
|
|
|
{
|
|
|
|
std::cerr << "Sleeping " << std::getenv("SLEEPKITFORDEBUGGER")
|
|
|
|
<< " seconds to give you time to attach debugger to process "
|
|
|
|
<< Process::id() << std::endl;
|
|
|
|
Thread::sleep(std::stoul(std::getenv("SLEEPKITFORDEBUGGER")) * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
lokit_main(childRoot, sysTemplate, loTemplate, loSubPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// parent
|
2016-04-07 02:36:38 -05:00
|
|
|
if (pid < 0)
|
|
|
|
Log::syserror("Fork failed.");
|
|
|
|
else
|
2016-04-08 05:09:06 -05:00
|
|
|
Log::info("Forked kit [" + std::to_string(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"))
|
|
|
|
return 1;
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
if (std::getenv("SLEEPFORDEBUGGER"))
|
|
|
|
{
|
|
|
|
std::cerr << "Sleeping " << std::getenv("SLEEPFORDEBUGGER")
|
|
|
|
<< " seconds to give you time to attach debugger to process "
|
|
|
|
<< Process::id() << std::endl;
|
|
|
|
Thread::sleep(std::stoul(std::getenv("SLEEPFORDEBUGGER")) * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialization
|
2016-04-12 06:22:51 -05:00
|
|
|
Log::initialize("frk");
|
2016-04-05 08:32:10 -05:00
|
|
|
|
|
|
|
Util::setTerminationSignals();
|
|
|
|
Util::setFatalSignals();
|
|
|
|
|
2016-04-06 07:16:56 -05:00
|
|
|
// Auto-reap zombies.
|
|
|
|
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
|
|
|
|
{
|
2016-04-07 02:36:38 -05:00
|
|
|
Log::syserror("Failed to set SIGCHLD to SIG_IGN.");
|
2016-04-06 07:16:56 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-05 08:32:10 -05:00
|
|
|
std::string childRoot;
|
|
|
|
std::string loSubPath;
|
|
|
|
std::string sysTemplate;
|
|
|
|
std::string loTemplate;
|
|
|
|
|
|
|
|
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-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-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();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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.");
|
|
|
|
|
|
|
|
if (!std::getenv("LOK_VIEW_CALLBACK"))
|
|
|
|
Log::info("Note: LOK_VIEW_CALLBACK is not set.");
|
|
|
|
|
|
|
|
const Path pipePath = Path::forDirectory(childRoot + Path::separator() + FIFO_PATH);
|
|
|
|
const std::string pipeLoolwsd = Path(pipePath, FIFO_LOOLWSD).toString();
|
2016-04-07 03:27:43 -05:00
|
|
|
if ( (pipeFd = open(pipeLoolwsd.c_str(), O_RDONLY) ) < 0 )
|
2016-04-05 08:32:10 -05:00
|
|
|
{
|
2016-04-07 03:04:05 -05:00
|
|
|
Log::syserror("Failed to open pipe [" + pipeLoolwsd + "] for reading. Exiting.");
|
2016-04-05 08:32:10 -05:00
|
|
|
std::exit(Application::EXIT_SOFTWARE);
|
|
|
|
}
|
2016-04-12 09:49:20 -05:00
|
|
|
Log::debug("open(" + pipeLoolwsd + ", RDONLY) = " + std::to_string(pipeFd));
|
2016-04-05 08:32:10 -05:00
|
|
|
|
|
|
|
// Initialize LoKit
|
|
|
|
if (!globalPreinit(loTemplate))
|
2016-04-08 03:43:30 -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.
|
|
|
|
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
|
|
|
std::exit(Application::EXIT_SOFTWARE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ChildDispatcher childDispatcher;
|
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-04-05 08:32:10 -05:00
|
|
|
if (!childDispatcher.pollAndDispatch())
|
|
|
|
{
|
|
|
|
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-04-07 03:27:43 -05:00
|
|
|
close(pipeFd);
|
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-09 11:30:48 -05:00
|
|
|
return returnValue;
|
2016-04-05 08:32:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|