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
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/application.h"
|
|
|
|
#include "base/stacktrace.h"
|
|
|
|
#include "base/timer.h"
|
|
|
|
#include "base/logger_fwd.h"
|
|
|
|
#include "base/exception.h"
|
|
|
|
#include "base/objectlock.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/utility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/exception/diagnostic_information.hpp>
|
2013-03-18 11:02:18 +01:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2013-03-18 17:04:22 +01:00
|
|
|
#include <boost/exception/errinfo_api_function.hpp>
|
|
|
|
#include <boost/exception/errinfo_errno.hpp>
|
|
|
|
#include <boost/exception/errinfo_file_name.hpp>
|
2013-03-17 22:14:40 +01:00
|
|
|
#include <iostream>
|
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;
|
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;
|
2013-02-01 22:44:58 +01:00
|
|
|
String Application::m_PkgDataDir;
|
2013-02-14 16:18:58 +01:00
|
|
|
int Application::m_ArgC;
|
|
|
|
char **Application::m_ArgV;
|
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())
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Application objects must be local."));
|
2012-08-14 12:51:51 +02:00
|
|
|
|
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;
|
2013-03-11 14:03:01 +01:00
|
|
|
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
|
|
|
|
BOOST_THROW_EXCEPTION(win32_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("WSAStartup")
|
2013-03-18 22:40:40 +01:00
|
|
|
<< errinfo_win32_error(WSAGetLastError()));
|
2013-03-11 14:03:01 +01:00
|
|
|
}
|
2012-04-01 15:20:13 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-03-28 13:24:49 +02:00
|
|
|
|
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
|
|
|
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(m_Instance == NULL);
|
2012-08-14 12:51:51 +02:00
|
|
|
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.
|
|
|
|
*/
|
2013-03-02 09:07:47 +01:00
|
|
|
Application::Ptr Application::GetInstance(void)
|
2012-05-21 12:53:38 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
return m_Instance->GetSelf();
|
2012-05-21 12:53:38 +02:00
|
|
|
}
|
|
|
|
|
2013-02-14 16:18:58 +01:00
|
|
|
int Application::GetArgC(void)
|
|
|
|
{
|
|
|
|
return m_ArgC;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::SetArgC(int argc)
|
|
|
|
{
|
|
|
|
m_ArgC = argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
char **Application::GetArgV(void)
|
|
|
|
{
|
|
|
|
return m_ArgV;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::SetArgV(char **argv)
|
|
|
|
{
|
|
|
|
m_ArgV = argv;
|
|
|
|
}
|
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
void Application::ShutdownTimerHandler(void)
|
|
|
|
{
|
2013-02-24 01:10:34 +01:00
|
|
|
if (m_ShuttingDown) {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "base", "Shutting down Icinga...");
|
2013-02-24 01:10:34 +01:00
|
|
|
Application::GetInstance()->OnShutdown();
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
DynamicObject::DeactivateObjects();
|
2013-03-25 18:36:15 +01:00
|
|
|
GetTP().Stop();
|
2013-02-24 01:10:34 +01:00
|
|
|
m_ShuttingDown = false;
|
|
|
|
}
|
2013-02-06 10:53:28 +01: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
|
|
|
*/
|
2013-02-06 10:53:28 +01:00
|
|
|
void Application::RunEventLoop(void) const
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-09-25 15:24:14 +02:00
|
|
|
/* Start the system time watch thread. */
|
2013-03-15 18:21:29 +01:00
|
|
|
boost::thread t(&Application::TimeWatchThreadProc);
|
2012-09-25 15:24:14 +02:00
|
|
|
t.detach();
|
2012-09-25 14:03:41 +02:00
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
/* Set up a timer that watches the m_Shutdown flag. */
|
|
|
|
Timer::Ptr shutdownTimer = boost::make_shared<Timer>();
|
|
|
|
shutdownTimer->OnTimerExpired.connect(boost::bind(&Application::ShutdownTimerHandler));
|
|
|
|
shutdownTimer->SetInterval(0.5);
|
|
|
|
shutdownTimer->Start();
|
2012-08-03 13:19:55 +02:00
|
|
|
|
2013-02-19 23:02:08 +01:00
|
|
|
Timer::Initialize();
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
GetTP().Join();
|
2013-02-19 23:02:08 +01:00
|
|
|
|
|
|
|
Timer::Uninitialize();
|
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. */
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream 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";
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "base", msgbuf.str());
|
2012-09-25 15:24:14 +02:00
|
|
|
|
2013-02-17 19:14:34 +01:00
|
|
|
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-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];
|
2013-03-11 13:45:08 +01:00
|
|
|
if (getcwd(buffer, sizeof(buffer)) == NULL) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("getcwd")
|
|
|
|
<< boost::errinfo_errno(errno));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::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();
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not determine executable path."));
|
2012-06-15 22:26:25 +02:00
|
|
|
}
|
2012-04-02 13:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-11 13:45:08 +01:00
|
|
|
if (realpath(executablePath.CStr(), buffer) == NULL) {
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("realpath")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(executablePath));
|
2013-03-11 13:45:08 +01:00
|
|
|
}
|
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)))
|
2013-03-11 14:03:01 +01:00
|
|
|
BOOST_THROW_EXCEPTION(win32_error()
|
2013-03-18 17:04:22 +01:00
|
|
|
<< boost::errinfo_api_function("GetModuleFileName")
|
2013-03-18 22:40:40 +01:00
|
|
|
<< errinfo_win32_error(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 */
|
|
|
|
}
|
|
|
|
|
2013-02-02 23:22:27 +01:00
|
|
|
/**
|
|
|
|
* Sets whether debugging is enabled.
|
|
|
|
*
|
|
|
|
* @param debug Whether to enable debugging.
|
|
|
|
*/
|
|
|
|
void Application::SetDebugging(bool debug)
|
|
|
|
{
|
|
|
|
m_Debugging = debug;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(signum == SIGINT);
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Application::Ptr instance = Application::GetInstance();
|
2012-06-20 15:23:10 +02:00
|
|
|
|
|
|
|
if (!instance)
|
|
|
|
return;
|
|
|
|
|
2012-09-27 09:58:16 +02:00
|
|
|
instance->RequestShutdown();
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
2013-01-30 11:51:15 +01:00
|
|
|
|
|
|
|
/**
|
2013-03-07 16:00:10 +01:00
|
|
|
* Signal handler for SIGABRT. Helps with debugging ASSERT()s.
|
2013-01-30 11:51:15 +01:00
|
|
|
*
|
|
|
|
* @param signum The signal number.
|
|
|
|
*/
|
|
|
|
void Application::SigAbrtHandler(int signum)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(signum == SIGABRT);
|
2013-01-30 11:51:15 +01:00
|
|
|
|
2013-03-07 15:00:26 +01:00
|
|
|
#ifndef _WIN32
|
2013-03-06 11:03:50 +01:00
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGABRT, &sa, NULL);
|
2013-03-07 15:00:26 +01:00
|
|
|
#endif /* _WIN32 */
|
2013-03-06 11:03:50 +01:00
|
|
|
|
2013-01-30 11:51:15 +01:00
|
|
|
std::cerr << "Caught SIGABRT." << std::endl;
|
|
|
|
|
2013-03-07 15:16:01 +01:00
|
|
|
StackTrace trace;
|
|
|
|
trace.Print(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)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
Application::Ptr instance = Application::GetInstance();
|
2012-06-20 15:23:10 +02:00
|
|
|
|
|
|
|
if (!instance)
|
|
|
|
return TRUE;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
instance->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)
|
|
|
|
{
|
2013-03-06 11:03:50 +01:00
|
|
|
#ifndef _WIN32
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGABRT, &sa, NULL);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2013-03-07 19:44:39 +01:00
|
|
|
bool has_trace = false;
|
|
|
|
|
2013-01-30 11:51:15 +01:00
|
|
|
try {
|
|
|
|
throw;
|
|
|
|
} catch (const std::exception& ex) {
|
2013-02-06 12:51:12 +01:00
|
|
|
std::cerr << std::endl
|
2013-03-16 21:18:53 +01:00
|
|
|
<< boost::diagnostic_information(ex)
|
2013-01-30 11:51:15 +01:00
|
|
|
<< std::endl;
|
2013-03-07 19:44:39 +01:00
|
|
|
|
|
|
|
has_trace = (boost::get_error_info<StackTraceErrorInfo>(ex) != NULL);
|
|
|
|
} catch (...) {
|
|
|
|
std::cerr << "Exception of unknown type." << std::endl;
|
2013-01-30 11:51:15 +01:00
|
|
|
}
|
|
|
|
|
2013-03-07 19:44:39 +01:00
|
|
|
if (!has_trace) {
|
|
|
|
StackTrace trace;
|
|
|
|
trace.Print(std::cerr, 1);
|
|
|
|
}
|
2013-01-30 11:51:15 +01:00
|
|
|
|
2013-02-01 19:11:15 +01:00
|
|
|
DisplayBugMessage();
|
|
|
|
|
2013-01-30 11:51:15 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2013-03-07 15:00:26 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
LONG CALLBACK Application::SEHUnhandledExceptionFilter(PEXCEPTION_POINTERS exi)
|
|
|
|
{
|
|
|
|
std::cerr << "Unhandled SEH exception." << std::endl;
|
|
|
|
|
|
|
|
StackTrace trace(exi);
|
|
|
|
trace.Print(std::cerr, 1);
|
|
|
|
|
|
|
|
DisplayBugMessage();
|
|
|
|
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
2013-03-09 12:56:40 +01:00
|
|
|
#endif /* _WIN32 */
|
2013-01-30 11:51:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2013-03-07 15:00:26 +01:00
|
|
|
#else /* _WIN32 */
|
|
|
|
SetUnhandledExceptionFilter(&Application::SEHUnhandledExceptionFilter);
|
2013-01-30 11:51:15 +01:00
|
|
|
#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
|
|
|
*
|
|
|
|
* @returns The application's exit code.
|
|
|
|
*/
|
2013-02-02 23:22:27 +01:00
|
|
|
int Application::Run(void)
|
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 */
|
|
|
|
|
2013-02-02 23:22:27 +01:00
|
|
|
result = Main();
|
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
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
if (m_PidFile != NULL)
|
|
|
|
fclose(m_PidFile);
|
2012-07-12 17:03:34 +02:00
|
|
|
|
|
|
|
/* There's just no sane way of getting a file descriptor for a
|
|
|
|
* C++ ofstream which is why we're using FILEs here. */
|
2013-03-12 16:02:35 +01:00
|
|
|
m_PidFile = fopen(filename.CStr(), "r+");
|
|
|
|
|
|
|
|
if (m_PidFile == NULL)
|
|
|
|
m_PidFile = fopen(filename.CStr(), "w");
|
2012-07-12 17:03:34 +02:00
|
|
|
|
|
|
|
if (m_PidFile == NULL)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open PID file '" + filename + "'"));
|
2012-07-12 17:03:34 +02:00
|
|
|
|
2013-02-13 12:47:51 +01:00
|
|
|
#ifndef _WIN32
|
2013-03-02 09:07:47 +01:00
|
|
|
int fd = fileno(m_PidFile);
|
|
|
|
|
|
|
|
Utility::SetCloExec(fd);
|
|
|
|
|
|
|
|
if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogCritical, "base", "Could not lock PID file. Make sure that only one instance of the application is running.");
|
2013-03-02 09:07:47 +01:00
|
|
|
|
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-03-12 14:06:59 +01:00
|
|
|
|
|
|
|
(void) ftruncate(fd, 0);
|
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)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-07-12 17:03:34 +02:00
|
|
|
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
|
|
|
/**
|
2013-02-01 22:44:58 +01:00
|
|
|
* Retrieves the path for the package lib dir.
|
2012-10-02 09:26:17 +02:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2013-02-01 22:44:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the path for the package data dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetPkgDataDir(void)
|
|
|
|
{
|
|
|
|
if (m_PkgDataDir.IsEmpty())
|
|
|
|
return ".";
|
|
|
|
else
|
|
|
|
return m_PkgDataDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the package data dir.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
|
|
|
void Application::SetPkgDataDir(const String& path)
|
|
|
|
{
|
|
|
|
m_PkgDataDir = path;
|
|
|
|
}
|
2013-02-14 10:55:47 +01:00
|
|
|
|
2013-02-15 06:47:26 +01:00
|
|
|
/**
|
2013-03-25 18:36:15 +01:00
|
|
|
* Returns the global thread pool.
|
2013-02-15 06:47:26 +01:00
|
|
|
*
|
2013-03-25 18:36:15 +01:00
|
|
|
* @returns The global thread pool.
|
2013-02-15 06:47:26 +01:00
|
|
|
*/
|
2013-03-25 18:36:15 +01:00
|
|
|
ThreadPool& Application::GetTP(void)
|
2013-02-15 06:47:26 +01:00
|
|
|
{
|
2013-03-25 18:36:15 +01:00
|
|
|
static ThreadPool tp;
|
|
|
|
return tp;
|
2013-02-15 06:47:26 +01:00
|
|
|
}
|