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
|
|
|
|
|
|
|
Application::Application(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsaData;
|
|
|
|
WSAStartup(MAKEWORD(1, 1), &wsaData);
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
Application::~Application(void)
|
|
|
|
{
|
|
|
|
Timer::StopAllTimers();
|
|
|
|
Socket::CloseAllSockets();
|
|
|
|
|
2012-04-02 20:50:35 +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
|
|
|
}
|
|
|
|
|
|
|
|
void Application::RunEventLoop(void)
|
|
|
|
{
|
|
|
|
while (!m_ShuttingDown) {
|
|
|
|
fd_set readfds, writefds, exceptfds;
|
|
|
|
int nfds = -1;
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_ZERO(&writefds);
|
|
|
|
FD_ZERO(&exceptfds);
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
for (list<Socket::WeakPtr>::iterator i = Socket::Sockets.begin(); i != Socket::Sockets.end(); i++) {
|
|
|
|
Socket::Ptr socket = i->lock();
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (socket == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
long sleep;
|
|
|
|
|
|
|
|
do {
|
|
|
|
Timer::CallExpiredTimers();
|
|
|
|
sleep = (long)(Timer::GetNextCall() - time(NULL));
|
|
|
|
} while (sleep <= 0);
|
|
|
|
|
|
|
|
if (m_ShuttingDown)
|
|
|
|
break;
|
|
|
|
|
|
|
|
timeval tv;
|
|
|
|
tv.tv_sec = sleep;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
int ready;
|
|
|
|
|
|
|
|
if (nfds == -1) {
|
|
|
|
Sleep(tv.tv_sec * 1000 + tv.tv_usec);
|
|
|
|
ready = 0;
|
|
|
|
} else
|
|
|
|
ready = select(nfds + 1, &readfds, &writefds, &exceptfds, &tv);
|
|
|
|
|
|
|
|
if (ready < 0)
|
|
|
|
break;
|
|
|
|
else if (ready == 0)
|
|
|
|
continue;
|
|
|
|
|
2012-04-03 15:16:11 +02:00
|
|
|
EventArgs::Ptr ea = make_shared<EventArgs>();
|
2012-03-28 13:24:49 +02:00
|
|
|
ea->Source = shared_from_this();
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
list<Socket::WeakPtr>::iterator prev, i;
|
2012-03-28 13:24:49 +02:00
|
|
|
for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) {
|
|
|
|
prev = i;
|
|
|
|
i++;
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
Socket::Ptr socket = prev->lock();
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (socket == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int fd = socket->GetFD();
|
|
|
|
|
|
|
|
if (FD_ISSET(fd, &writefds))
|
|
|
|
socket->OnWritable(ea);
|
|
|
|
|
2012-03-29 20:03:29 +02:00
|
|
|
if (FD_ISSET(fd, &readfds))
|
|
|
|
socket->OnReadable(ea);
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
if (FD_ISSET(fd, &exceptfds))
|
|
|
|
socket->OnException(ea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-30 10:24:42 +02:00
|
|
|
bool Application::Daemonize(void) {
|
|
|
|
#ifndef _WIN32
|
|
|
|
pid_t pid;
|
|
|
|
pid_t sid;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid) {
|
|
|
|
fprintf(stdout, "DONE\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
if (fd) {
|
|
|
|
if (fd != 0) {
|
|
|
|
dup2(fd, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd != 1) {
|
|
|
|
dup2(fd, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd != 2) {
|
|
|
|
dup2(fd, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fd > 2) {
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sid = setsid();
|
|
|
|
if (sid == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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-02 20:50:35 +02:00
|
|
|
ConfigHive::Ptr Application::GetConfigHive(void)
|
2012-03-31 15:18:09 +02:00
|
|
|
{
|
|
|
|
return m_ConfigHive;
|
|
|
|
}
|
|
|
|
|
2012-04-03 11:39:26 +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-01 19:32:41 +02:00
|
|
|
Log("Loading component '%s'", path.c_str());
|
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 */
|
|
|
|
lt_dlhandle hModule = 0;
|
|
|
|
lt_dladvise advise;
|
|
|
|
|
2012-04-02 13:09:33 +02:00
|
|
|
if (!lt_dladvise_init(&advise) && !lt_dladvise_local(&advise)) {
|
2012-04-02 10:29:08 +02:00
|
|
|
hModule = lt_dlopenadvise(path.c_str(), advise);
|
|
|
|
}
|
|
|
|
|
|
|
|
lt_dladvise_destroy(&advise);
|
|
|
|
#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-04-02 13:09:33 +02:00
|
|
|
pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
|
2012-04-02 10:29:08 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
pCreateComponent = (Component *(*)())lt_dlsym(hModule, "CreateComponent");
|
|
|
|
#endif /* _WIN32 */
|
2012-03-31 15:18:09 +02:00
|
|
|
|
|
|
|
if (pCreateComponent == NULL)
|
2012-04-03 13:01:00 +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 15:18:09 +02:00
|
|
|
component->SetApplication(static_pointer_cast<Application>(shared_from_this()));
|
2012-03-31 16:03:42 +02:00
|
|
|
component->SetConfig(componentConfig);
|
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
|
|
|
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
2012-04-03 11:39:26 +02:00
|
|
|
Component::Ptr Application::GetComponent(const string& name)
|
2012-03-31 16:03:42 +02:00
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
map<string, Component::Ptr>::iterator ci = m_Components.find(name);
|
2012-03-31 16:03:42 +02:00
|
|
|
|
|
|
|
if (ci == m_Components.end())
|
2012-04-02 20:50:35 +02:00
|
|
|
return Component::Ptr();
|
2012-03-31 16:03:42 +02:00
|
|
|
|
|
|
|
return ci->second;
|
|
|
|
}
|
|
|
|
|
2012-04-03 11:39:26 +02:00
|
|
|
void Application::UnloadComponent(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())
|
|
|
|
return;
|
|
|
|
|
2012-04-01 09:30:08 +02:00
|
|
|
Log("Unloading component '%s'", name.c_str());
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
Component::Ptr component = ci->second;
|
2012-03-31 15:18:09 +02:00
|
|
|
component->Stop();
|
|
|
|
m_Components.erase(ci);
|
|
|
|
|
|
|
|
// TODO: unload DLL
|
|
|
|
}
|
2012-04-01 09:30:08 +02:00
|
|
|
|
|
|
|
void Application::Log(const char *format, ...)
|
|
|
|
{
|
|
|
|
char message[512];
|
|
|
|
va_list marker;
|
|
|
|
|
|
|
|
va_start(marker, format);
|
|
|
|
vsnprintf(message, sizeof(message), format, marker);
|
|
|
|
va_end(marker);
|
|
|
|
|
|
|
|
// TODO: log to file
|
|
|
|
fprintf(stderr, "%s\n", message);
|
2012-04-01 15:20:13 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 13:16:22 +02:00
|
|
|
void Application::SetArguments(const vector<string>& arguments)
|
|
|
|
{
|
|
|
|
m_Arguments = arguments;
|
|
|
|
}
|
|
|
|
|
2012-04-03 11:39:26 +02:00
|
|
|
const vector<string>& Application::GetArguments(void)
|
2012-04-02 13:09:33 +02:00
|
|
|
{
|
|
|
|
return m_Arguments;
|
|
|
|
}
|
|
|
|
|
2012-04-03 11:39:26 +02:00
|
|
|
const string& Application::GetExeDirectory(void)
|
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)
|
|
|
|
throw exception(/*"getcwd() failed"*/);
|
|
|
|
|
|
|
|
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)
|
|
|
|
throw exception(/*"snprintf() failed"*/);
|
|
|
|
|
|
|
|
if (access(PathTest, X_OK) == 0) {
|
|
|
|
strncpy(FullExePath, PathTest, sizeof(FullExePath));
|
|
|
|
|
|
|
|
FoundPath = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(PathEnv);
|
|
|
|
|
|
|
|
if (!FoundPath)
|
|
|
|
throw exception(/*"Could not determine executable path."*/);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-02 19:38:04 +02:00
|
|
|
if ((Buf = realpath(FullExePath, NULL)) == NULL)
|
2012-04-02 13:09:33 +02:00
|
|
|
throw exception(/*"realpath() failed"*/);
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
|
|
|
|
GetModuleFileName(NULL, FullExePath, MAXPATHLEN);
|
|
|
|
|
|
|
|
PathRemoveFileSpec(FullExePath);
|
|
|
|
|
|
|
|
ExePath = string(FullExePath);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
return ExePath;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
bool Application::IsDebugging(void) const
|
|
|
|
{
|
|
|
|
return m_Debugging;
|
|
|
|
}
|
2012-04-03 19:49:56 +02:00
|
|
|
|
|
|
|
void Application::SigIntHandler(int signum)
|
|
|
|
{
|
|
|
|
Application::Instance->Shutdown();
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = SIG_DFL;
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
static void application_sigint_handler(int signum)
|
|
|
|
{
|
|
|
|
Application::Instance->SigIntHandler(signum);
|
|
|
|
}
|
|
|
|
|
|
|
|
int application_main(int argc, char **argv, Application::Ptr instance)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
Application::Instance = instance;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = sigint_handler;
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
vector<string> args;
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++)
|
|
|
|
args.push_back(string(argv[i]));
|
|
|
|
|
|
|
|
Application::Instance->SetArguments(args);
|
|
|
|
|
|
|
|
if (Application::Instance->IsDebugging()) {
|
|
|
|
result = Application::Instance->Main(args);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
result = Application::Instance->Main(args);
|
|
|
|
} catch (const Exception& ex) {
|
|
|
|
cout << "---" << endl;
|
|
|
|
|
|
|
|
string klass = typeid(ex).name();
|
|
|
|
|
|
|
|
#ifdef HAVE_GCC_ABI_DEMANGLE
|
|
|
|
int status;
|
|
|
|
char *realname = abi::__cxa_demangle(klass.c_str(), 0, 0, &status);
|
|
|
|
|
|
|
|
if (realname != NULL) {
|
|
|
|
klass = string(realname);
|
|
|
|
free(realname);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_GCC_ABI_DEMANGLE */
|
|
|
|
|
|
|
|
cout << "Exception: " << klass << endl;
|
|
|
|
cout << "Message: " << ex.GetMessage() << endl;
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Application::Instance.reset();
|
|
|
|
|
|
|
|
assert(Object::ActiveObjects == 0);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|