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-04-02 10:29:08 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
# include <ltdl.h>
|
|
|
|
#endif
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
Application::Ptr Application::m_Instance;
|
|
|
|
bool Application::m_ShuttingDown = false;
|
|
|
|
bool Application::m_Debugging = false;
|
|
|
|
boost::thread::id Application::m_MainThreadID;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the Application class.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
Application::Application(void)
|
2012-07-13 09:59:26 +02:00
|
|
|
: m_PidFile(NULL)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsaData;
|
2012-05-13 20:39:51 +02:00
|
|
|
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
|
|
|
|
throw Win32Exception("WSAStartup failed", WSAGetLastError());
|
2012-04-01 15:20:13 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
lt_dlinit();
|
|
|
|
#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-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-05-25 22:04:03 +02:00
|
|
|
m_ShuttingDown = true;
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/* stop all components */
|
2012-07-17 19:19:03 +02:00
|
|
|
Component::Ptr component;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, component), m_Components) {
|
2012-07-16 22:00:50 +02:00
|
|
|
component->Stop();
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
2012-04-01 15:20:13 +02:00
|
|
|
|
2012-04-03 19:10:51 +02:00
|
|
|
m_Components.clear();
|
|
|
|
|
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-07-12 15:50:31 +02:00
|
|
|
return m_Instance;
|
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)
|
|
|
|
{
|
|
|
|
while (!m_ShuttingDown) {
|
2012-06-14 15:16:41 +02:00
|
|
|
Object::ClearHeldObjects();
|
|
|
|
|
2012-06-22 11:19:58 +02:00
|
|
|
long sleep = Timer::ProcessTimers();
|
|
|
|
|
|
|
|
if (m_ShuttingDown)
|
|
|
|
break;
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-07-13 23:33:30 +02:00
|
|
|
Event::ProcessEvents(boost::get_system_time() + boost::posix_time::seconds(sleep));
|
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-03-28 13:24:49 +02:00
|
|
|
void Application::Shutdown(void)
|
|
|
|
{
|
|
|
|
m_ShuttingDown = true;
|
|
|
|
}
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
2012-05-14 19:14:23 +02:00
|
|
|
* Loads a component from a shared library.
|
2012-04-24 14:02:15 +02:00
|
|
|
*
|
|
|
|
* @param path The path of the component library.
|
|
|
|
* @param componentConfig The configuration for the component.
|
|
|
|
* @returns The component.
|
|
|
|
*/
|
2012-04-22 16:45:31 +02:00
|
|
|
Component::Ptr Application::LoadComponent(const string& path,
|
|
|
|
const ConfigObject::Ptr& componentConfig)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
Component::Ptr component;
|
2012-03-31 15:18:09 +02:00
|
|
|
Component *(*pCreateComponent)();
|
|
|
|
|
2012-07-10 13:00:53 +02:00
|
|
|
assert(Application::IsMainThread());
|
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogInformation, "base", "Loading component '" + path + "'");
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-04-02 10:29:08 +02:00
|
|
|
#ifdef _WIN32
|
2012-03-31 15:18:09 +02:00
|
|
|
HMODULE hModule = LoadLibrary(path.c_str());
|
2012-07-10 13:31:17 +02:00
|
|
|
|
|
|
|
if (hModule == NULL)
|
|
|
|
throw Win32Exception("LoadLibrary('" + path + "') failed", GetLastError());
|
2012-04-02 10:29:08 +02:00
|
|
|
#else /* _WIN32 */
|
2012-04-13 11:08:33 +02:00
|
|
|
lt_dlhandle hModule = lt_dlopen(path.c_str());
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-07-10 13:31:17 +02:00
|
|
|
if (hModule == NULL) {
|
|
|
|
throw runtime_error("Could not load module '" + path + "': " + lt_dlerror());
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-04-02 10:29:08 +02:00
|
|
|
#ifdef _WIN32
|
2012-05-25 16:56:47 +02:00
|
|
|
pCreateComponent = (CreateComponentFunction)GetProcAddress(hModule,
|
|
|
|
"CreateComponent");
|
2012-04-02 10:29:08 +02:00
|
|
|
#else /* _WIN32 */
|
2012-05-10 13:46:04 +02:00
|
|
|
# ifdef __GNUC__
|
|
|
|
/* suppress compiler warning for void * cast */
|
|
|
|
__extension__
|
|
|
|
# endif
|
2012-05-25 16:56:47 +02:00
|
|
|
pCreateComponent = (CreateComponentFunction)lt_dlsym(hModule,
|
|
|
|
"CreateComponent");
|
2012-04-02 10:29:08 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-03-31 15:18:09 +02:00
|
|
|
|
|
|
|
if (pCreateComponent == NULL)
|
2012-05-26 21:30:04 +02:00
|
|
|
throw runtime_error("Loadable module does not contain "
|
|
|
|
"CreateComponent function");
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
component = Component::Ptr(pCreateComponent());
|
2012-03-31 16:03:42 +02:00
|
|
|
component->SetConfig(componentConfig);
|
2012-04-18 15:22:25 +02:00
|
|
|
RegisterComponent(component);
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Registers a component object and starts it.
|
|
|
|
*
|
|
|
|
* @param component The component.
|
|
|
|
*/
|
2012-06-16 20:44:24 +02:00
|
|
|
void Application::RegisterComponent(const Component::Ptr& component)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-03-31 15:18:09 +02:00
|
|
|
m_Components[component->GetName()] = component;
|
|
|
|
|
2012-03-31 16:03:42 +02:00
|
|
|
component->Start();
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Unregisters a component object and stops it.
|
|
|
|
*
|
|
|
|
* @param component The component.
|
|
|
|
*/
|
2012-06-16 20:44:24 +02:00
|
|
|
void Application::UnregisterComponent(const Component::Ptr& component)
|
2012-03-31 16:03:42 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
string name = component->GetName();
|
2012-03-31 16:03:42 +02:00
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogInformation, "base", "Unloading component '" + name + "'");
|
2012-04-18 15:22:25 +02:00
|
|
|
map<string, Component::Ptr>::iterator i = m_Components.find(name);
|
2012-04-24 14:02:15 +02:00
|
|
|
if (i != m_Components.end())
|
2012-04-18 15:22:25 +02:00
|
|
|
m_Components.erase(i);
|
2012-04-24 14:02:15 +02:00
|
|
|
|
|
|
|
component->Stop();
|
2012-03-31 16:03:42 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Finds a loaded component by name.
|
|
|
|
*
|
|
|
|
* @param name The name of the component.
|
|
|
|
* @returns The component or a null pointer if the component could not be found.
|
|
|
|
*/
|
2012-05-18 22:53:35 +02:00
|
|
|
Component::Ptr Application::GetComponent(const string& name) const
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-05-18 22:53:35 +02:00
|
|
|
map<string, Component::Ptr>::const_iterator i = m_Components.find(name);
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-05-18 22:53:35 +02:00
|
|
|
if (i == m_Components.end())
|
2012-04-18 15:22:25 +02:00
|
|
|
return Component::Ptr();
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-05-18 22:53:35 +02:00
|
|
|
return i->second;
|
2012-03-31 15:18:09 +02:00
|
|
|
}
|
2012-04-01 09:30:08 +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-07-10 13:31:17 +02:00
|
|
|
* @returns The path.
|
2012-04-24 14:02:15 +02:00
|
|
|
*/
|
2012-07-10 13:31:17 +02:00
|
|
|
string Application::GetExePath(void) const
|
2012-04-02 13:09:33 +02:00
|
|
|
{
|
2012-06-15 22:26:25 +02:00
|
|
|
static string result;
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-06-15 22:26:25 +02:00
|
|
|
if (!result.empty())
|
|
|
|
return result;
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-07-08 21:18:35 +02:00
|
|
|
string executablePath;
|
|
|
|
|
2012-04-02 13:09:33 +02:00
|
|
|
#ifndef _WIN32
|
2012-06-15 22:26:25 +02:00
|
|
|
string argv0 = m_Arguments[0];
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-06-15 22:26:25 +02:00
|
|
|
char buffer[MAXPATHLEN];
|
|
|
|
if (getcwd(buffer, sizeof(buffer)) == NULL)
|
2012-04-23 09:48:20 +02:00
|
|
|
throw PosixException("getcwd failed", errno);
|
2012-06-15 22:26:25 +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-06-15 22:26:25 +02:00
|
|
|
if (argv0.find_first_of('/') == string::npos) {
|
|
|
|
const char *pathEnv = getenv("PATH");
|
|
|
|
if (pathEnv != NULL) {
|
|
|
|
vector<string> paths;
|
|
|
|
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-07-16 22:00:50 +02:00
|
|
|
BOOST_FOREACH(string& path, paths) {
|
|
|
|
string pathTest = path + "/" + argv0;
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-06-15 22:26:25 +02:00
|
|
|
if (access(pathTest.c_str(), 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-07-08 21:18:35 +02:00
|
|
|
executablePath.clear();
|
2012-05-26 21:30:04 +02:00
|
|
|
throw runtime_error("Could not determine executable path.");
|
2012-06-15 22:26:25 +02:00
|
|
|
}
|
2012-04-02 13:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-08 21:18:35 +02:00
|
|
|
if (realpath(executablePath.c_str(), buffer) == NULL)
|
2012-04-23 09:48:20 +02:00
|
|
|
throw PosixException("realpath failed", errno);
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-07-10 13:31:17 +02:00
|
|
|
result = 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)))
|
|
|
|
throw Win32Exception("GetModuleFileName() failed", GetLastError());
|
2012-04-02 13:09:33 +02:00
|
|
|
|
2012-07-10 13:31:17 +02:00
|
|
|
result = FullExePath;
|
2012-04-02 13:09:33 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2012-06-15 22:26:25 +02:00
|
|
|
return result;
|
2012-04-02 13:09:33 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Adds a directory to the component search path.
|
|
|
|
*
|
|
|
|
* @param componentDirectory The directory.
|
|
|
|
*/
|
2012-04-03 11:39:26 +02:00
|
|
|
void Application::AddComponentSearchDir(const string& componentDirectory)
|
2012-04-02 13:09:33 +02:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetDllDirectory(componentDirectory.c_str());
|
|
|
|
#else /* _WIN32 */
|
|
|
|
lt_dladdsearchdir(componentDirectory.c_str());
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
2012-04-03 19:10:51 +02:00
|
|
|
|
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-07-10 12:21:19 +02:00
|
|
|
bool Application::IsMainThread(void)
|
|
|
|
{
|
|
|
|
return (boost::this_thread::get_id() == m_MainThreadID);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
instance->Shutdown();
|
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
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
instance->GetInstance()->Shutdown();
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
|
2012-05-21 12:53:38 +02:00
|
|
|
assert(!Application::m_Instance);
|
2012-07-10 12:21:19 +02:00
|
|
|
|
|
|
|
m_MainThreadID = boost::this_thread::get_id();
|
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
Application::m_Instance = GetSelf();
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
#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);
|
2012-05-21 12:53:38 +02:00
|
|
|
#else
|
|
|
|
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-05-15 16:24:04 +02:00
|
|
|
m_Arguments.push_back(string(argv[i]));
|
2012-05-15 10:58:14 +02:00
|
|
|
|
|
|
|
if (IsDebugging()) {
|
2012-05-15 16:24:04 +02:00
|
|
|
result = Main(m_Arguments);
|
2012-04-06 08:56:52 +02:00
|
|
|
|
2012-05-21 12:53:38 +02:00
|
|
|
Application::m_Instance.reset();
|
2012-04-06 08:56:52 +02:00
|
|
|
} else {
|
|
|
|
try {
|
2012-05-15 16:24:04 +02:00
|
|
|
result = Main(m_Arguments);
|
2012-07-10 15:14:45 +02:00
|
|
|
} catch (const exception& ex) {
|
2012-05-21 12:53:38 +02:00
|
|
|
Application::m_Instance.reset();
|
2012-05-15 10:58:14 +02:00
|
|
|
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogCritical, "base", "---");
|
2012-07-11 20:55:46 +02:00
|
|
|
Logger::Write(LogCritical, "base", "Exception: " + Utility::GetTypeName(typeid(ex)));
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogCritical, "base", "Message: " + string(ex.what()));
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
2012-07-12 17:03:34 +02:00
|
|
|
|
|
|
|
void Application::UpdatePidFile(const string& filename)
|
|
|
|
{
|
|
|
|
ClosePidFile();
|
|
|
|
|
|
|
|
/* There's just no sane way of getting a file descriptor for a
|
|
|
|
* C++ ofstream which is why we're using FILEs here. */
|
|
|
|
m_PidFile = fopen(filename.c_str(), "w");
|
|
|
|
|
|
|
|
if (m_PidFile == NULL)
|
|
|
|
throw runtime_error("Could not open PID file '" + filename + "'");
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
throw runtime_error("Another instance of the application is "
|
|
|
|
"already running. Remove the '" + filename + "' file if "
|
|
|
|
"you're certain that this is not the case.");
|
|
|
|
}
|
2012-07-13 15:29:39 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-07-12 17:03:34 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
pid_t pid = getpid();
|
|
|
|
#else /* _WIN32 */
|
|
|
|
DWORD pid = GetCurrentProcessId();
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
fprintf(m_PidFile, "%d", pid);
|
|
|
|
fflush(m_PidFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::ClosePidFile(void)
|
|
|
|
{
|
|
|
|
if (m_PidFile != NULL)
|
|
|
|
fclose(m_PidFile);
|
|
|
|
|
|
|
|
m_PidFile = NULL;
|
|
|
|
}
|
|
|
|
|