2016-04-05 11:41: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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <ftw.h>
|
|
|
|
#include <cassert>
|
|
|
|
#include "Util.hpp"
|
|
|
|
#include "Unit.hpp"
|
|
|
|
|
2016-04-08 11:36:08 -05:00
|
|
|
#include <Poco/Thread.h>
|
2016-04-06 13:50:55 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitBase *UnitBase::_global = nullptr;
|
2016-04-05 11:41:10 -05:00
|
|
|
|
2016-04-08 11:36:08 -05:00
|
|
|
static Poco::Thread TimeoutThread("unit timeout");
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitBase *UnitBase::linkAndCreateUnit(UnitType type, const std::string &unitLibPath)
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
|
|
|
void *dlHandle = dlopen(unitLibPath.c_str(), RTLD_GLOBAL|RTLD_NOW);
|
|
|
|
if (!dlHandle)
|
|
|
|
{
|
|
|
|
Log::error("Failed to load " + unitLibPath + ": " + std::string(dlerror()));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
const char *symbol = NULL;
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TYPE_WSD:
|
|
|
|
symbol = "unit_create_wsd";
|
|
|
|
break;
|
|
|
|
case TYPE_KIT:
|
|
|
|
symbol = "unit_create_kit";
|
|
|
|
break;
|
|
|
|
}
|
2016-04-05 11:41:10 -05:00
|
|
|
CreateUnitHooksFunction* createHooks;
|
2016-04-09 11:30:48 -05:00
|
|
|
createHooks = reinterpret_cast<CreateUnitHooksFunction *>(dlsym(dlHandle, symbol));
|
2016-04-05 11:41:10 -05:00
|
|
|
if (!createHooks)
|
|
|
|
{
|
2016-04-09 11:30:48 -05:00
|
|
|
Log::error("No " + std::string(symbol) + " symbol in " + unitLibPath);
|
2016-04-05 11:41:10 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitBase *pHooks = createHooks();
|
2016-04-05 11:41:10 -05:00
|
|
|
|
|
|
|
if (pHooks)
|
|
|
|
pHooks->setHandle(dlHandle);
|
|
|
|
|
|
|
|
return pHooks;
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
bool UnitBase::init(UnitType type, const std::string &unitLibPath)
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
2016-04-09 11:30:48 -05:00
|
|
|
assert(!_global);
|
2016-04-05 11:41:10 -05:00
|
|
|
if (!unitLibPath.empty())
|
2016-04-08 11:36:08 -05:00
|
|
|
{
|
2016-04-09 11:30:48 -05:00
|
|
|
_global = linkAndCreateUnit(type, unitLibPath);
|
|
|
|
if (_global)
|
|
|
|
{
|
|
|
|
TimeoutThread.startFunc([](){
|
|
|
|
TimeoutThread.trySleep(_global->_timeoutMilliSeconds);
|
|
|
|
if (!_global->_timeoutShutdown)
|
|
|
|
{
|
|
|
|
Log::error("Timeout");
|
|
|
|
_global->timeout();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-04-08 11:36:08 -05:00
|
|
|
}
|
2016-04-05 11:41:10 -05:00
|
|
|
else
|
2016-04-09 11:30:48 -05:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TYPE_WSD:
|
|
|
|
_global = new UnitWSD();
|
|
|
|
break;
|
|
|
|
case TYPE_KIT:
|
|
|
|
_global = new UnitKit();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_global)
|
|
|
|
_global->_type = type;
|
2016-04-05 11:41:10 -05:00
|
|
|
|
|
|
|
return _global != NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
void UnitBase::setTimeout(int timeoutMilliSeconds)
|
2016-04-08 11:36:08 -05:00
|
|
|
{
|
|
|
|
assert(!TimeoutThread.isRunning());
|
|
|
|
_timeoutMilliSeconds = timeoutMilliSeconds;
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitBase::UnitBase()
|
2016-04-08 11:36:08 -05:00
|
|
|
: _dlHandle(NULL),
|
2016-04-09 11:30:48 -05:00
|
|
|
_setRetValue(false),
|
|
|
|
_retValue(0),
|
2016-04-08 11:36:08 -05:00
|
|
|
_timeoutMilliSeconds(30 * 1000),
|
|
|
|
_timeoutShutdown(false)
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitBase::~UnitBase()
|
2016-04-05 11:41:10 -05:00
|
|
|
{
|
2016-04-06 13:50:55 -05:00
|
|
|
// FIXME: we should really clean-up properly.
|
|
|
|
// if (_dlHandle)
|
|
|
|
// dlclose(_dlHandle);
|
2016-04-05 11:41:10 -05:00
|
|
|
_dlHandle = NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
UnitWSD::UnitWSD()
|
|
|
|
: _hasKitHooks(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitWSD::~UnitWSD()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitKit::UnitKit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UnitKit::~UnitKit()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitBase::exitTest(TestResult result)
|
2016-04-06 13:50:55 -05:00
|
|
|
{
|
|
|
|
_setRetValue = true;
|
|
|
|
_retValue = result == TestResult::TEST_OK ?
|
2016-04-08 11:36:08 -05:00
|
|
|
Poco::Util::Application::EXIT_OK :
|
|
|
|
Poco::Util::Application::EXIT_SOFTWARE;
|
2016-04-06 13:50:55 -05:00
|
|
|
TerminationFlag = true;
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
void UnitBase::timeout()
|
2016-04-08 11:36:08 -05:00
|
|
|
{
|
|
|
|
exitTest(TestResult::TEST_TIMED_OUT);
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:30:48 -05:00
|
|
|
void UnitBase::returnValue(int &retValue)
|
2016-04-06 13:50:55 -05:00
|
|
|
{
|
|
|
|
if (_setRetValue)
|
|
|
|
retValue = _retValue;
|
|
|
|
|
2016-04-08 11:36:08 -05:00
|
|
|
_timeoutShutdown = true;
|
|
|
|
TimeoutThread.wakeUp();
|
|
|
|
TimeoutThread.join();
|
|
|
|
|
|
|
|
delete _global;
|
|
|
|
_global = nullptr;
|
2016-04-06 13:50:55 -05:00
|
|
|
}
|
|
|
|
|
2016-04-05 11:41:10 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|