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-04-06 08:56:52 +02:00
|
|
|
Application::Ptr I2_EXPORT Application::Instance;
|
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)
|
|
|
|
{
|
|
|
|
#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 */
|
2012-05-12 16:12:26 +02:00
|
|
|
LTDL_SET_PRELOADED_SYMBOLS();
|
|
|
|
|
2012-04-01 15:20:13 +02:00
|
|
|
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
|
|
|
m_ShuttingDown = false;
|
2012-04-03 15:16:11 +02:00
|
|
|
m_ConfigHive = make_shared<ConfigHive>();
|
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-04-24 14:02:15 +02:00
|
|
|
/* stop all components */
|
2012-04-22 16:45:31 +02:00
|
|
|
for (map<string, Component::Ptr>::iterator i = m_Components.begin();
|
|
|
|
i != m_Components.end(); i++) {
|
2012-03-31 15:18:09 +02:00
|
|
|
i->second->Stop();
|
|
|
|
}
|
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();
|
|
|
|
#else /* _WIN32 */
|
2012-04-04 10:08:31 +02:00
|
|
|
//lt_dlexit();
|
2012-04-01 15:20:13 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Processes events (e.g. sockets and timers).
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void Application::RunEventLoop(void)
|
|
|
|
{
|
|
|
|
while (!m_ShuttingDown) {
|
|
|
|
fd_set readfds, writefds, exceptfds;
|
|
|
|
int nfds = -1;
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Timer::CallExpiredTimers();
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&exceptfds);
|
|
|
|
|
2012-04-20 16:21:43 +02:00
|
|
|
Socket::CollectionType::iterator prev, i;
|
2012-04-22 16:45:31 +02:00
|
|
|
for (i = Socket::Sockets.begin();
|
|
|
|
i != Socket::Sockets.end(); ) {
|
2012-04-02 20:50:35 +02:00
|
|
|
Socket::Ptr socket = i->lock();
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-20 16:21:43 +02:00
|
|
|
prev = i;
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (!socket) {
|
|
|
|
Socket::Sockets.erase(prev);
|
2012-03-28 13:24:49 +02:00
|
|
|
continue;
|
2012-04-20 16:21:43 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
int fd = socket->GetFD();
|
|
|
|
|
|
|
|
if (socket->WantsToWrite())
|
|
|
|
FD_SET(fd, &writefds);
|
|
|
|
|
2012-03-29 20:03:29 +02:00
|
|
|
if (socket->WantsToRead())
|
|
|
|
FD_SET(fd, &readfds);
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
FD_SET(fd, &exceptfds);
|
|
|
|
|
|
|
|
if (fd > nfds)
|
|
|
|
nfds = fd;
|
|
|
|
}
|
|
|
|
|
2012-04-19 09:41:12 +02:00
|
|
|
time_t now = time(NULL);
|
|
|
|
time_t next = Timer::GetNextCall();
|
2012-04-19 11:29:36 +02:00
|
|
|
time_t sleep = (next < now) ? 0 : (next - now);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (m_ShuttingDown)
|
|
|
|
break;
|
|
|
|
|
|
|
|
timeval tv;
|
2012-04-19 11:29:36 +02:00
|
|
|
tv.tv_sec = (sleep < 0) ? 0 : (long)sleep;
|
2012-03-28 13:24:49 +02:00
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
int ready;
|
|
|
|
|
|
|
|
if (nfds == -1) {
|
|
|
|
Sleep(tv.tv_sec * 1000 + tv.tv_usec);
|
|
|
|
ready = 0;
|
|
|
|
} else
|
2012-04-22 16:45:31 +02:00
|
|
|
ready = select(nfds + 1, &readfds, &writefds,
|
|
|
|
&exceptfds, &tv);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (ready < 0)
|
|
|
|
break;
|
|
|
|
else if (ready == 0)
|
|
|
|
continue;
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
EventArgs ea;
|
|
|
|
ea.Source = shared_from_this();
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
for (i = Socket::Sockets.begin();
|
|
|
|
i != Socket::Sockets.end(); ) {
|
2012-04-20 16:21:43 +02:00
|
|
|
Socket::Ptr socket = i->lock();
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
prev = i;
|
|
|
|
i++;
|
|
|
|
|
2012-04-20 16:21:43 +02:00
|
|
|
if (!socket) {
|
|
|
|
Socket::Sockets.erase(prev);
|
2012-03-28 13:24:49 +02:00
|
|
|
continue;
|
2012-04-20 16:21:43 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
int fd;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
fd = socket->GetFD();
|
|
|
|
if (fd != INVALID_SOCKET && FD_ISSET(fd, &writefds))
|
2012-03-28 13:24:49 +02:00
|
|
|
socket->OnWritable(ea);
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
fd = socket->GetFD();
|
|
|
|
if (fd != INVALID_SOCKET && FD_ISSET(fd, &readfds))
|
2012-03-29 20:03:29 +02:00
|
|
|
socket->OnReadable(ea);
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
fd = socket->GetFD();
|
|
|
|
if (fd != INVALID_SOCKET && FD_ISSET(fd, &exceptfds))
|
2012-03-28 13:24:49 +02:00
|
|
|
socket->OnException(ea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns the application's configuration hive.
|
|
|
|
*
|
|
|
|
* @returns The config hive.
|
|
|
|
*/
|
2012-04-22 16:45:31 +02:00
|
|
|
ConfigHive::Ptr Application::GetConfigHive(void) const
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
|
|
|
return m_ConfigHive;
|
|
|
|
}
|
|
|
|
|
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-04-26 16:45:00 +02:00
|
|
|
Log("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-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-04-02 10:29:08 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-04-02 10:29:08 +02:00
|
|
|
if (hModule == NULL)
|
2012-04-03 13:01:00 +02:00
|
|
|
throw ComponentLoadException("Could not load module");
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-04-02 10:29:08 +02:00
|
|
|
#ifdef _WIN32
|
2012-05-10 13:46:04 +02:00
|
|
|
pCreateComponent = reinterpret_cast<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
|
|
|
|
pCreateComponent = reinterpret_cast<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-04-22 16:45:31 +02:00
|
|
|
throw ComponentLoadException("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-04-18 15:22:25 +02:00
|
|
|
void Application::RegisterComponent(Component::Ptr component)
|
|
|
|
{
|
|
|
|
component->SetApplication(static_pointer_cast<Application>(shared_from_this()));
|
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-04-18 15:22:25 +02:00
|
|
|
void Application::UnregisterComponent(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-04-26 16:45:00 +02:00
|
|
|
Log("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-04-18 15:22:25 +02:00
|
|
|
Component::Ptr Application::GetComponent(const string& name)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
map<string, Component::Ptr>::iterator ci = m_Components.find(name);
|
2012-03-31 15:18:09 +02:00
|
|
|
|
|
|
|
if (ci == m_Components.end())
|
2012-04-18 15:22:25 +02:00
|
|
|
return Component::Ptr();
|
2012-03-31 15:18:09 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
return ci->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
|
|
|
/**
|
|
|
|
* Logs a message.
|
|
|
|
*
|
2012-04-26 16:45:00 +02:00
|
|
|
* @param message The message.
|
2012-04-24 14:02:15 +02:00
|
|
|
*/
|
2012-04-26 16:45:00 +02:00
|
|
|
void Application::Log(string message)
|
2012-04-01 09:30:08 +02:00
|
|
|
{
|
2012-04-26 16:45:00 +02:00
|
|
|
char timestamp[100];
|
2012-04-01 09:30:08 +02:00
|
|
|
|
2012-04-26 16:45:00 +02:00
|
|
|
time_t now;
|
|
|
|
time(&now);
|
|
|
|
tm tmnow = *localtime(&now);
|
2012-04-01 09:30:08 +02:00
|
|
|
|
2012-04-26 16:45:00 +02:00
|
|
|
strftime(timestamp, sizeof(timestamp), "%a %B %d %Y %H:%M:%S", &tmnow);
|
|
|
|
|
|
|
|
cout << "[" << timestamp << "]: " << message << endl;
|
2012-04-01 15:20:13 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the directory the application's binary is contained in.
|
|
|
|
*
|
|
|
|
* @returns The directory.
|
|
|
|
*/
|
2012-04-22 16:45:31 +02:00
|
|
|
string Application::GetExeDirectory(void) const
|
2012-04-02 13:09:33 +02:00
|
|
|
{
|
|
|
|
static string ExePath;
|
|
|
|
|
|
|
|
if (ExePath.length() != 0)
|
|
|
|
return ExePath;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2012-04-02 19:38:04 +02:00
|
|
|
char Cwd[MAXPATHLEN];
|
|
|
|
char *Buf, *PathEnv, *Directory, PathTest[MAXPATHLEN], FullExePath[MAXPATHLEN];
|
2012-04-02 13:09:33 +02:00
|
|
|
bool FoundPath;
|
|
|
|
|
|
|
|
const char *argv0 = m_Arguments[0].c_str();
|
|
|
|
|
|
|
|
if (getcwd(Cwd, sizeof(Cwd)) == NULL)
|
2012-04-23 09:48:20 +02:00
|
|
|
throw PosixException("getcwd failed", errno);
|
2012-04-02 13:09:33 +02:00
|
|
|
|
|
|
|
if (argv0[0] != '/')
|
|
|
|
snprintf(FullExePath, sizeof(FullExePath), "%s/%s", Cwd, argv0);
|
|
|
|
else
|
|
|
|
strncpy(FullExePath, argv0, sizeof(FullExePath));
|
|
|
|
|
|
|
|
if (strchr(argv0, '/') == NULL) {
|
|
|
|
PathEnv = getenv("PATH");
|
|
|
|
|
|
|
|
if (PathEnv != NULL) {
|
2012-04-03 13:01:00 +02:00
|
|
|
PathEnv = Memory::StrDup(PathEnv);
|
2012-04-02 13:09:33 +02:00
|
|
|
|
|
|
|
FoundPath = false;
|
|
|
|
|
|
|
|
for (Directory = strtok(PathEnv, ":"); Directory != NULL; Directory = strtok(NULL, ":")) {
|
|
|
|
if (snprintf(PathTest, sizeof(PathTest), "%s/%s", Directory, argv0) < 0)
|
2012-04-23 09:48:20 +02:00
|
|
|
throw PosixException("snprintf failed", errno);
|
2012-04-02 13:09:33 +02:00
|
|
|
|
|
|
|
if (access(PathTest, X_OK) == 0) {
|
|
|
|
strncpy(FullExePath, PathTest, sizeof(FullExePath));
|
|
|
|
|
|
|
|
FoundPath = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(PathEnv);
|
|
|
|
|
|
|
|
if (!FoundPath)
|
2012-04-23 09:48:20 +02:00
|
|
|
throw Exception("Could not determine executable path.");
|
2012-04-02 13:09:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-02 19:38:04 +02:00
|
|
|
if ((Buf = realpath(FullExePath, NULL)) == NULL)
|
2012-04-23 09:48:20 +02:00
|
|
|
throw PosixException("realpath failed", errno);
|
2012-04-02 13:09:33 +02:00
|
|
|
|
|
|
|
// remove filename
|
|
|
|
char *LastSlash = strrchr(Buf, '/');
|
|
|
|
|
|
|
|
if (LastSlash != NULL)
|
|
|
|
*LastSlash = '\0';
|
|
|
|
|
|
|
|
ExePath = string(Buf);
|
2012-04-02 19:38:04 +02:00
|
|
|
|
|
|
|
free(Buf);
|
2012-04-02 13:09:33 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
char FullExePath[MAXPATHLEN];
|
|
|
|
|
2012-05-16 11:30:54 +02:00
|
|
|
GetModuleFileName(NULL, FullExePath, sizeof(FullExePath));
|
2012-04-02 13:09:33 +02:00
|
|
|
|
|
|
|
PathRemoveFileSpec(FullExePath);
|
|
|
|
|
|
|
|
ExePath = string(FullExePath);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
return ExePath;
|
|
|
|
}
|
|
|
|
|
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-04-03 19:10:51 +02:00
|
|
|
bool Application::IsDebugging(void) const
|
|
|
|
{
|
|
|
|
return m_Debugging;
|
|
|
|
}
|
2012-04-03 19:49:56 +02:00
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
#ifndef _WIN32
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Signal handler for SIGINT.
|
|
|
|
*
|
|
|
|
* @param signum The signal number.
|
|
|
|
*/
|
2012-04-22 16:45:31 +02:00
|
|
|
static void ApplicationSigIntHandler(int signum)
|
2012-04-03 19:49:56 +02:00
|
|
|
{
|
2012-04-24 14:02:15 +02:00
|
|
|
assert(signum == SIGINT);
|
|
|
|
|
2012-04-03 19:49:56 +02:00
|
|
|
Application::Instance->Shutdown();
|
|
|
|
|
|
|
|
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-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-14 19:14:23 +02:00
|
|
|
assert(!Application::Instance);
|
2012-05-15 10:58:14 +02:00
|
|
|
Application::Instance = static_pointer_cast<Application>(shared_from_this());
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
2012-04-22 16:45:31 +02:00
|
|
|
sa.sa_handler = ApplicationSigIntHandler;
|
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-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-15 10:58:14 +02:00
|
|
|
Application::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-04-06 08:56:52 +02:00
|
|
|
} catch (const Exception& ex) {
|
2012-05-15 10:58:14 +02:00
|
|
|
Application::Instance.reset();
|
|
|
|
|
2012-04-26 16:45:00 +02:00
|
|
|
Application::Log("---");
|
|
|
|
Application::Log("Exception: " + Utility::GetTypeName(ex));
|
|
|
|
Application::Log("Message: " + ex.GetMessage());
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|