2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
2012-08-14 12:51:51 +02:00
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2012-08-14 12:51:51 +02:00
|
|
|
Application *Application::m_Instance = NULL;
|
2012-07-10 12:21:19 +02:00
|
|
|
bool Application::m_ShuttingDown = false;
|
|
|
|
bool Application::m_Debugging = false;
|
|
|
|
boost::thread::id Application::m_MainThreadID;
|
2012-09-28 13:16:08 +02:00
|
|
|
String Application::m_PrefixDir;
|
|
|
|
String Application::m_LocalStateDir;
|
2012-10-02 09:26:17 +02:00
|
|
|
String Application::m_PkgLibDir;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the Application class.
|
|
|
|
*/
|
2012-08-14 12:51:51 +02:00
|
|
|
Application::Application(const Dictionary::Ptr& serializedUpdate)
|
|
|
|
: DynamicObject(serializedUpdate), m_PidFile(NULL)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-08-14 12:51:51 +02:00
|
|
|
if (!IsLocal())
|
|
|
|
throw_exception(runtime_error("Application objects must be local."));
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
#ifdef _WIN32
|
2012-07-27 16:05:02 +02:00
|
|
|
/* disable GUI-based error messages for LoadLibrary() */
|
|
|
|
SetErrorMode(SEM_FAILCRITICALERRORS);
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
WSADATA wsaData;
|
2012-05-13 20:39:51 +02:00
|
|
|
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(Win32Exception("WSAStartup failed", WSAGetLastError()));
|
2012-04-01 15:20:13 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-03 19:10:51 +02:00
|
|
|
char *debugging = getenv("_DEBUG");
|
|
|
|
m_Debugging = (debugging && strtol(debugging, NULL, 10) != 0);
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (IsDebuggerPresent())
|
|
|
|
m_Debugging = true;
|
|
|
|
#endif /* _WIN32 */
|
2012-08-14 12:51:51 +02:00
|
|
|
|
|
|
|
assert(m_Instance == NULL);
|
|
|
|
m_Instance = this;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Destructor for the application class.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
Application::~Application(void)
|
|
|
|
{
|
2012-08-14 12:51:51 +02:00
|
|
|
m_Instance = NULL;
|
|
|
|
|
2012-05-25 22:04:03 +02:00
|
|
|
m_ShuttingDown = true;
|
|
|
|
|
2012-04-01 15:20:13 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#endif /* _WIN32 */
|
2012-07-12 17:03:34 +02:00
|
|
|
|
|
|
|
ClosePidFile();
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 12:53:38 +02:00
|
|
|
/**
|
|
|
|
* Retrieves a pointer to the application singleton object.
|
|
|
|
*
|
|
|
|
* @returns The application object.
|
|
|
|
*/
|
|
|
|
Application::Ptr Application::GetInstance(void)
|
|
|
|
{
|
2012-08-14 12:51:51 +02:00
|
|
|
if (m_Instance)
|
|
|
|
return m_Instance->GetSelf();
|
|
|
|
else
|
|
|
|
return Application::Ptr();
|
2012-05-21 12:53:38 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
2012-05-18 22:53:35 +02:00
|
|
|
* Processes events for registered sockets and timers and calls whatever
|
|
|
|
* handlers have been set up for these events.
|
2012-04-24 14:02:15 +02:00
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Application::RunEventLoop(void)
|
|
|
|
{
|
2012-08-04 09:58:31 +02:00
|
|
|
#ifdef _DEBUG
|
2012-08-03 23:03:58 +02:00
|
|
|
double nextProfile = 0;
|
2012-08-04 09:58:31 +02:00
|
|
|
#endif /* _DEBUG */
|
2012-08-03 23:03:58 +02:00
|
|
|
|
2012-09-25 15:24:14 +02:00
|
|
|
/* Start the system time watch thread. */
|
|
|
|
thread t(&Application::TimeWatchThreadProc);
|
|
|
|
t.detach();
|
2012-09-25 14:03:41 +02:00
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
while (!m_ShuttingDown) {
|
2012-06-14 15:16:41 +02:00
|
|
|
Object::ClearHeldObjects();
|
|
|
|
|
2012-07-25 12:59:17 +02:00
|
|
|
double sleep = Timer::ProcessTimers();
|
2012-06-22 11:19:58 +02:00
|
|
|
|
|
|
|
if (m_ShuttingDown)
|
|
|
|
break;
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-09-25 15:24:14 +02:00
|
|
|
Event::ProcessEvents(boost::posix_time::milliseconds(sleep * 1000));
|
2012-08-02 09:38:08 +02:00
|
|
|
|
|
|
|
DynamicObject::FinishTx();
|
|
|
|
DynamicObject::BeginTx();
|
2012-08-03 13:19:55 +02:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2012-08-03 23:03:58 +02:00
|
|
|
if (nextProfile < Utility::GetTime()) {
|
|
|
|
stringstream msgbuf;
|
2012-12-04 08:42:24 +01:00
|
|
|
msgbuf << "Active objects: " << Object::GetAliveObjectsCount();
|
2012-08-03 23:03:58 +02:00
|
|
|
Logger::Write(LogInformation, "base", msgbuf.str());
|
|
|
|
|
|
|
|
Object::PrintMemoryProfile();
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2012-08-03 23:03:58 +02:00
|
|
|
nextProfile = Utility::GetTime() + 15.0;
|
|
|
|
}
|
2012-08-03 13:19:55 +02:00
|
|
|
#endif /* _DEBUG */
|
2012-09-25 15:24:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Watches for changes to the system time. Adjusts timers if necessary.
|
|
|
|
*/
|
|
|
|
void Application::TimeWatchThreadProc(void)
|
|
|
|
{
|
|
|
|
double lastLoop = Utility::GetTime();
|
|
|
|
|
|
|
|
for (;;) {
|
2012-09-25 15:41:43 +02:00
|
|
|
Utility::Sleep(5);
|
2012-09-25 14:03:41 +02:00
|
|
|
|
|
|
|
double now = Utility::GetTime();
|
2012-09-25 15:24:14 +02:00
|
|
|
double timeDiff = lastLoop - now;
|
2012-09-25 14:03:41 +02:00
|
|
|
|
2012-09-25 15:24:14 +02:00
|
|
|
if (abs(timeDiff) > 15) {
|
|
|
|
/* We made a significant jump in time. */
|
2012-09-25 14:03:41 +02:00
|
|
|
stringstream msgbuf;
|
2012-09-25 15:24:14 +02:00
|
|
|
msgbuf << "We jumped "
|
2012-09-25 15:33:51 +02:00
|
|
|
<< (timeDiff < 0 ? "forward" : "backward")
|
2012-09-25 15:24:14 +02:00
|
|
|
<< " in time: " << abs(timeDiff) << " seconds";
|
2012-09-25 14:03:41 +02:00
|
|
|
Logger::Write(LogInformation, "base", msgbuf.str());
|
2012-09-25 15:24:14 +02:00
|
|
|
|
|
|
|
/* in addition to rescheduling the timers this
|
|
|
|
* causes the event loop to wake up thereby
|
|
|
|
* solving the problem that timed_wait()
|
|
|
|
* uses an absolute timestamp for the timeout */
|
|
|
|
Event::Post(boost::bind(&Timer::AdjustTimers,
|
|
|
|
-timeDiff));
|
2012-09-25 14:03:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lastLoop = now;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Signals the application to shut down during the next
|
|
|
|
* execution of the event loop.
|
|
|
|
*/
|
2012-09-27 09:58:16 +02:00
|
|
|
void Application::RequestShutdown(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
m_ShuttingDown = true;
|
|
|
|
}
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-09-27 09:58:16 +02:00
|
|
|
/**
|
|
|
|
* Terminates the application.
|
|
|
|
*/
|
|
|
|
void Application::Terminate(int exitCode)
|
|
|
|
{
|
|
|
|
_exit(exitCode);
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
2012-07-10 13:31:17 +02:00
|
|
|
* Retrieves the full path of the executable.
|
2012-04-24 14:02:15 +02:00
|
|
|
*
|
2012-08-14 12:51:51 +02:00
|
|
|
* @param argv0 The first command-line argument.
|
2012-07-10 13:31:17 +02:00
|
|
|
* @returns The path.
|
2012-04-24 14:02:15 +02:00
|
|
|
*/
|
2012-08-14 12:51:51 +02:00
|
|
|
String Application::GetExePath(const String& argv0)
|
2012-04-02 13:09:33 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
String executablePath;
|
2012-07-08 21:18:35 +02:00
|
|
|
|
2012-04-02 13:09:33 +02:00
|
|
|
#ifndef _WIN32
|
2012-06-15 22:26:25 +02:00
|
|
|
char buffer[MAXPATHLEN];
|
|
|
|
if (getcwd(buffer, sizeof(buffer)) == NULL)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(PosixException("getcwd failed", errno));
|
2012-08-02 09:38:08 +02:00
|
|
|
String workingDirectory = buffer;
|
2012-04-02 13:09:33 +02:00
|
|
|
|
|
|
|
if (argv0[0] != '/')
|
2012-07-08 21:18:35 +02:00
|
|
|
executablePath = workingDirectory + "/" + argv0;
|
2012-04-02 13:09:33 +02:00
|
|
|
else
|
2012-07-08 21:18:35 +02:00
|
|
|
executablePath = argv0;
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
bool foundSlash = false;
|
2012-08-04 09:58:31 +02:00
|
|
|
for (size_t i = 0; i < argv0.GetLength(); i++) {
|
2012-08-02 09:38:08 +02:00
|
|
|
if (argv0[i] == '/') {
|
|
|
|
foundSlash = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foundSlash) {
|
2012-06-15 22:26:25 +02:00
|
|
|
const char *pathEnv = getenv("PATH");
|
|
|
|
if (pathEnv != NULL) {
|
2012-08-02 09:38:08 +02:00
|
|
|
vector<String> paths;
|
2012-06-15 22:26:25 +02:00
|
|
|
boost::algorithm::split(paths, pathEnv, boost::is_any_of(":"));
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-06-15 22:26:25 +02:00
|
|
|
bool foundPath = false;
|
2012-08-02 09:38:08 +02:00
|
|
|
BOOST_FOREACH(String& path, paths) {
|
|
|
|
String pathTest = path + "/" + argv0;
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (access(pathTest.CStr(), X_OK) == 0) {
|
2012-07-08 21:18:35 +02:00
|
|
|
executablePath = pathTest;
|
2012-06-15 22:26:25 +02:00
|
|
|
foundPath = true;
|
2012-04-02 13:09:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 22:26:25 +02:00
|
|
|
if (!foundPath) {
|
2012-08-02 09:38:08 +02:00
|
|
|
executablePath.Clear();
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(runtime_error("Could not determine executable path."));
|
2012-06-15 22:26:25 +02:00
|
|
|
}
|
2012-04-02 13:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (realpath(executablePath.CStr(), buffer) == NULL)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(PosixException("realpath failed", errno));
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-08-14 12:51:51 +02:00
|
|
|
return buffer;
|
2012-04-02 13:09:33 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
char FullExePath[MAXPATHLEN];
|
|
|
|
|
2012-07-08 21:18:35 +02:00
|
|
|
if (!GetModuleFileName(NULL, FullExePath, sizeof(FullExePath)))
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(Win32Exception("GetModuleFileName() failed", GetLastError()));
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-08-14 12:51:51 +02:00
|
|
|
return FullExePath;
|
2012-04-02 13:09:33 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the debugging mode of the application.
|
|
|
|
*
|
|
|
|
* @returns true if the application is being debugged, false otherwise
|
|
|
|
*/
|
2012-06-05 15:05:15 +02:00
|
|
|
bool Application::IsDebugging(void)
|
2012-04-03 19:10:51 +02:00
|
|
|
{
|
|
|
|
return m_Debugging;
|
|
|
|
}
|
2012-04-03 19:49:56 +02:00
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
|
|
|
* Checks whether we're currently on the main thread.
|
|
|
|
*
|
|
|
|
* @returns true if this is the main thread, false otherwise
|
|
|
|
*/
|
2012-07-10 12:21:19 +02:00
|
|
|
bool Application::IsMainThread(void)
|
|
|
|
{
|
|
|
|
return (boost::this_thread::get_id() == m_MainThreadID);
|
|
|
|
}
|
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
|
|
|
* Sets the main thread to the currently running thread.
|
|
|
|
*/
|
2012-08-14 12:51:51 +02:00
|
|
|
void Application::SetMainThread(void)
|
|
|
|
{
|
|
|
|
m_MainThreadID = boost::this_thread::get_id();
|
|
|
|
}
|
|
|
|
|
2013-02-01 19:36:47 +01:00
|
|
|
/**
|
|
|
|
* Displays a message that tells users what to do when they encounter a bug.
|
|
|
|
*/
|
|
|
|
void Application::DisplayBugMessage(void)
|
|
|
|
{
|
|
|
|
std::cerr << "***" << std::endl
|
|
|
|
<< "*** This would indicate a bug in Icinga 2. Please submit a bug report at https://dev.icinga.org/ and include" << std::endl
|
|
|
|
<< "*** this stack trace as well as any other information that might be useful in order to reproduce this problem." << std::endl
|
|
|
|
<< "***" << std::endl
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
#ifndef _WIN32
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
2012-05-18 22:53:35 +02:00
|
|
|
* Signal handler for SIGINT. Prepares the application for cleanly
|
|
|
|
* shutting down during the next execution of the event loop.
|
2012-04-24 14:02:15 +02:00
|
|
|
*
|
|
|
|
* @param signum The signal number.
|
|
|
|
*/
|
2012-05-18 22:53:35 +02:00
|
|
|
void Application::SigIntHandler(int signum)
|
2012-04-03 19:49:56 +02:00
|
|
|
{
|
2012-04-24 14:02:15 +02:00
|
|
|
assert(signum == SIGINT);
|
|
|
|
|
2012-06-20 15:23:10 +02:00
|
|
|
Application::Ptr instance = Application::GetInstance();
|
|
|
|
|
|
|
|
if (!instance)
|
|
|
|
return;
|
|
|
|
|
2012-09-27 09:58:16 +02:00
|
|
|
instance->RequestShutdown();
|
2012-04-03 19:49:56 +02:00
|
|
|
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
2013-01-30 11:51:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal handler for SIGABRT. Helps with debugging assert()s.
|
|
|
|
*
|
|
|
|
* @param signum The signal number.
|
|
|
|
*/
|
|
|
|
void Application::SigAbrtHandler(int signum)
|
|
|
|
{
|
|
|
|
assert(signum == SIGABRT);
|
|
|
|
|
|
|
|
std::cerr << "Caught SIGABRT." << std::endl;
|
|
|
|
|
|
|
|
Utility::PrintStacktrace(std::cerr, 1);
|
2013-02-01 14:46:06 +01:00
|
|
|
|
2013-02-01 19:11:15 +01:00
|
|
|
DisplayBugMessage();
|
2013-01-30 11:51:15 +01:00
|
|
|
}
|
2012-05-21 12:53:38 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
/**
|
|
|
|
* Console control handler. Prepares the application for cleanly
|
|
|
|
* shutting down during the next execution of the event loop.
|
|
|
|
*/
|
|
|
|
BOOL WINAPI Application::CtrlHandler(DWORD type)
|
|
|
|
{
|
2012-06-20 15:23:10 +02:00
|
|
|
Application::Ptr instance = Application::GetInstance();
|
|
|
|
|
|
|
|
if (!instance)
|
|
|
|
return TRUE;
|
|
|
|
|
2012-09-27 09:58:16 +02:00
|
|
|
instance->GetInstance()->RequestShutdown();
|
2012-06-20 15:23:10 +02:00
|
|
|
|
2012-05-21 12:53:38 +02:00
|
|
|
SetConsoleCtrlHandler(NULL, FALSE);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2012-04-22 16:45:31 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-04-06 08:56:52 +02:00
|
|
|
|
2013-01-30 11:51:15 +01:00
|
|
|
/**
|
|
|
|
* Handler for unhandled exceptions.
|
|
|
|
*/
|
|
|
|
void Application::ExceptionHandler(void)
|
|
|
|
{
|
|
|
|
static bool rethrow = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
rethrow = false;
|
|
|
|
throw;
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "Unhandled exception of type "
|
|
|
|
<< Utility::GetTypeName(typeid(ex))
|
|
|
|
<< std::endl;
|
|
|
|
std::cerr << "Diagnostic Information: "
|
|
|
|
<< ex.what()
|
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
Utility::PrintStacktrace(std::cerr, 1);
|
|
|
|
|
2013-02-01 19:11:15 +01:00
|
|
|
DisplayBugMessage();
|
|
|
|
|
2013-01-30 11:51:15 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
struct sigaction sa;
|
2013-01-30 20:49:25 +01:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2013-01-30 11:51:15 +01:00
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGABRT, &sa, NULL);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Installs the exception handlers.
|
|
|
|
*/
|
|
|
|
void Application::InstallExceptionHandlers(void)
|
|
|
|
{
|
|
|
|
std::set_terminate(&Application::ExceptionHandler);
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = &Application::SigAbrtHandler;
|
|
|
|
sigaction(SIGABRT, &sa, NULL);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
2012-05-15 10:58:14 +02:00
|
|
|
* Runs the application.
|
2012-04-24 14:02:15 +02:00
|
|
|
*
|
|
|
|
* @param argc The number of arguments.
|
|
|
|
* @param argv The arguments that should be passed to the application.
|
|
|
|
* @returns The application's exit code.
|
|
|
|
*/
|
2012-05-15 10:58:14 +02:00
|
|
|
int Application::Run(int argc, char **argv)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
2012-05-21 12:53:38 +02:00
|
|
|
sa.sa_handler = &Application::SigIntHandler;
|
2012-04-06 08:56:52 +02:00
|
|
|
sigaction(SIGINT, &sa, NULL);
|
2012-04-24 19:55:18 +02:00
|
|
|
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &sa, NULL);
|
2013-01-30 11:51:15 +01:00
|
|
|
#else /* _WIN32 */
|
2012-05-21 12:53:38 +02:00
|
|
|
SetConsoleCtrlHandler(&Application::CtrlHandler, TRUE);
|
2012-04-06 08:56:52 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
m_Arguments.clear();
|
2012-04-06 08:56:52 +02:00
|
|
|
for (int i = 0; i < argc; i++)
|
2012-08-02 09:38:08 +02:00
|
|
|
m_Arguments.push_back(String(argv[i]));
|
|
|
|
|
|
|
|
DynamicObject::BeginTx();
|
2012-05-15 10:58:14 +02:00
|
|
|
|
2012-09-27 09:38:28 +02:00
|
|
|
result = Main(m_Arguments);
|
2012-04-06 08:56:52 +02:00
|
|
|
|
2012-08-14 14:23:16 +02:00
|
|
|
DynamicObject::FinishTx();
|
|
|
|
DynamicObject::DeactivateObjects();
|
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
return result;
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
2012-07-12 17:03:34 +02:00
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
2012-09-27 09:58:16 +02:00
|
|
|
* Grabs the PID file lock and updates the PID. Terminates the application
|
|
|
|
* if the PID file is already locked by another instance of the application.
|
2012-09-14 14:41:17 +02:00
|
|
|
*
|
|
|
|
* @param filename The name of the PID file.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
void Application::UpdatePidFile(const String& filename)
|
2012-07-12 17:03:34 +02:00
|
|
|
{
|
|
|
|
ClosePidFile();
|
|
|
|
|
|
|
|
/* There's just no sane way of getting a file descriptor for a
|
|
|
|
* C++ ofstream which is why we're using FILEs here. */
|
2012-08-02 09:38:08 +02:00
|
|
|
m_PidFile = fopen(filename.CStr(), "w");
|
2012-07-12 17:03:34 +02:00
|
|
|
|
|
|
|
if (m_PidFile == NULL)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(runtime_error("Could not open PID file '" + filename + "'"));
|
2012-07-12 17:03:34 +02:00
|
|
|
|
2012-07-13 15:24:19 +02:00
|
|
|
#ifndef _WIN32
|
2012-07-12 17:03:34 +02:00
|
|
|
if (flock(fileno(m_PidFile), LOCK_EX | LOCK_NB) < 0) {
|
|
|
|
ClosePidFile();
|
|
|
|
|
2012-09-27 09:58:16 +02:00
|
|
|
Logger::Write(LogCritical, "base",
|
|
|
|
"Another instance of the application is "
|
2012-07-12 17:03:34 +02:00
|
|
|
"already running. Remove the '" + filename + "' file if "
|
2012-09-27 09:58:16 +02:00
|
|
|
"you're certain that this is not the case.");
|
|
|
|
Terminate(EXIT_FAILURE);
|
2012-07-12 17:03:34 +02:00
|
|
|
}
|
2012-07-13 15:29:39 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-07-12 17:03:34 +02:00
|
|
|
|
2012-09-19 13:00:48 +02:00
|
|
|
fprintf(m_PidFile, "%d", Utility::GetPid());
|
2012-07-12 17:03:34 +02:00
|
|
|
fflush(m_PidFile);
|
|
|
|
}
|
|
|
|
|
2012-09-14 14:41:17 +02:00
|
|
|
/**
|
|
|
|
* Closes the PID file. Does nothing if the PID file is not currently open.
|
|
|
|
*/
|
2012-07-12 17:03:34 +02:00
|
|
|
void Application::ClosePidFile(void)
|
|
|
|
{
|
|
|
|
if (m_PidFile != NULL)
|
|
|
|
fclose(m_PidFile);
|
|
|
|
|
|
|
|
m_PidFile = NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-28 13:16:08 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path of the installation prefix.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetPrefixDir(void)
|
|
|
|
{
|
|
|
|
if (m_PrefixDir.IsEmpty())
|
2012-10-02 09:26:17 +02:00
|
|
|
return ".";
|
2012-09-28 13:16:08 +02:00
|
|
|
else
|
|
|
|
return m_PrefixDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the installation prefix.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
|
|
|
void Application::SetPrefixDir(const String& path)
|
|
|
|
{
|
|
|
|
m_PrefixDir = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the path for the local state dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetLocalStateDir(void)
|
|
|
|
{
|
|
|
|
if (m_LocalStateDir.IsEmpty())
|
2012-10-02 09:26:17 +02:00
|
|
|
return "./var";
|
2012-09-28 13:16:08 +02:00
|
|
|
else
|
|
|
|
return m_LocalStateDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the local state dir.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
|
|
|
void Application::SetLocalStateDir(const String& path)
|
|
|
|
{
|
|
|
|
m_LocalStateDir = path;
|
|
|
|
}
|
|
|
|
|
2012-10-02 09:26:17 +02:00
|
|
|
/**
|
|
|
|
* Retrives the path for the package lib dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetPkgLibDir(void)
|
|
|
|
{
|
|
|
|
if (m_PkgLibDir.IsEmpty())
|
|
|
|
return ".";
|
|
|
|
else
|
|
|
|
return m_PkgLibDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the package lib dir.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
|
|
|
void Application::SetPkgLibDir(const String& path)
|
|
|
|
{
|
|
|
|
m_PkgLibDir = path;
|
|
|
|
}
|