2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* 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
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/stacktrace.hpp"
|
|
|
|
#include "base/timer.hpp"
|
2014-08-07 08:34:38 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/debug.hpp"
|
|
|
|
#include "base/type.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/scriptvariable.hpp"
|
|
|
|
#include "base/process.hpp"
|
2013-11-03 13:45:26 +01:00
|
|
|
#include "icinga-version.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>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.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;
|
|
|
|
|
2013-11-08 16:07:21 +01:00
|
|
|
REGISTER_TYPE(Application);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2014-05-22 11:22:30 +02:00
|
|
|
boost::signals2::signal<void (void)> Application::OnReopenLogs;
|
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;
|
2014-04-27 21:47:25 +02:00
|
|
|
bool Application::m_RequestRestart = false;
|
2014-05-22 11:22:30 +02:00
|
|
|
bool Application::m_RequestReopenLogs = false;
|
2014-05-17 23:07:10 +02:00
|
|
|
pid_t Application::m_ReloadProcess = 0;
|
2014-05-11 17:14:35 +02:00
|
|
|
static bool l_Restarting = false;
|
2014-09-09 14:49:21 +02:00
|
|
|
static bool l_InExceptionHandler = false;
|
2013-02-14 16:18:58 +01:00
|
|
|
int Application::m_ArgC;
|
|
|
|
char **Application::m_ArgV;
|
2013-10-26 09:41:45 +02:00
|
|
|
double Application::m_StartTime;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the Application class.
|
|
|
|
*/
|
2013-09-09 14:09:33 +02:00
|
|
|
void Application::OnConfigLoaded(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
m_PidFile = NULL;
|
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
|
|
|
|
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.
|
|
|
|
*/
|
2013-08-20 11:06:04 +02:00
|
|
|
void Application::Stop(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
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
|
|
|
|
2014-05-17 23:07:10 +02:00
|
|
|
// Getting a shutdown-signal when a restart is in progress usually
|
|
|
|
// means that the restart succeeded and the new process wants to take
|
|
|
|
// over. Write the PID of the new process to the pidfile before this
|
|
|
|
// process exits to keep systemd happy.
|
2014-05-18 16:23:16 +02:00
|
|
|
if (l_Restarting) {
|
2014-06-05 17:44:41 +02:00
|
|
|
try {
|
|
|
|
UpdatePidFile(GetPidPath(), m_ReloadProcess);
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {
|
2014-06-05 17:44:41 +02:00
|
|
|
/* abort restart */
|
|
|
|
Log(LogCritical, "Application", "Cannot update PID file. Aborting restart operation.");
|
|
|
|
return;
|
|
|
|
}
|
2014-05-18 16:23:16 +02:00
|
|
|
ClosePidFile(false);
|
|
|
|
} else
|
|
|
|
ClosePidFile(true);
|
2014-04-16 15:01:31 +02:00
|
|
|
|
|
|
|
DynamicObject::Stop();
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Application::~Application(void)
|
|
|
|
{
|
|
|
|
m_Instance = NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:43:34 +02:00
|
|
|
void Application::Exit(int rc)
|
2014-08-04 16:26:17 +02:00
|
|
|
{
|
2014-08-05 13:52:24 +02:00
|
|
|
std::cout.flush();
|
2014-08-07 08:34:38 +02:00
|
|
|
|
|
|
|
BOOST_FOREACH(const Logger::Ptr& logger, Logger::GetLoggers()) {
|
|
|
|
logger->Flush();
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:43:34 +02:00
|
|
|
_exit(rc); // Yay, our static destructors are pretty much beyond repair at this point.
|
2014-08-04 16:26:17 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
void Application::InitializeBase(void)
|
|
|
|
{
|
2014-05-23 06:31:52 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
rlimit rl;
|
2014-05-26 07:35:07 +02:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) {
|
|
|
|
rlim_t maxfds = rl.rlim_max;
|
|
|
|
|
|
|
|
if (maxfds == RLIM_INFINITY)
|
|
|
|
maxfds = 65536;
|
|
|
|
|
|
|
|
for (rlim_t i = 3; i < maxfds; i++) {
|
2014-10-06 14:21:18 +02:00
|
|
|
#ifdef _DEBUG
|
2014-05-23 06:31:52 +02:00
|
|
|
if (close(i) >= 0)
|
|
|
|
std::cerr << "Closed FD " << i << " which we inherited from our parent process." << std::endl;
|
2014-10-06 14:21:18 +02:00
|
|
|
#endif /* _DEBUG */
|
2014-05-23 06:31:52 +02:00
|
|
|
}
|
2014-05-26 07:35:07 +02:00
|
|
|
}
|
2014-05-23 06:31:52 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
Utility::ExecuteDeferredInitializers();
|
|
|
|
}
|
|
|
|
|
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-04-08 10:02:06 +02:00
|
|
|
if (!m_Instance)
|
|
|
|
return Application::Ptr();
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
return m_Instance->GetSelf();
|
2012-05-21 12:53:38 +02:00
|
|
|
}
|
|
|
|
|
2013-12-05 11:04:47 +01:00
|
|
|
void Application::SetResourceLimits(void)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
rlimit rl;
|
2014-04-22 19:47:19 +02:00
|
|
|
|
|
|
|
# ifdef RLIMIT_NOFILE
|
2013-12-05 11:04:47 +01:00
|
|
|
rl.rlim_cur = 16 * 1024;
|
|
|
|
rl.rlim_max = rl.rlim_cur;
|
|
|
|
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogNotice, "Application", "Could not adjust resource limit for open file handles (RLIMIT_NOFILE)");
|
2013-12-05 11:04:47 +01:00
|
|
|
# else /* RLIMIT_NOFILE */
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogNotice, "Application", "System does not support adjusting the resource limit for open file handles (RLIMIT_NOFILE)");
|
2013-12-05 11:04:47 +01:00
|
|
|
# endif /* RLIMIT_NOFILE */
|
|
|
|
|
|
|
|
# ifdef RLIMIT_NPROC
|
|
|
|
rl.rlim_cur = 16 * 1024;
|
|
|
|
rl.rlim_max = rl.rlim_cur;
|
|
|
|
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &rl) < 0)
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogNotice, "Application", "Could not adjust resource limit for number of processes (RLIMIT_NPROC)");
|
2013-12-05 11:04:47 +01:00
|
|
|
# else /* RLIMIT_NPROC */
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogNotice, "Application", "System does not support adjusting the resource limit for number of processes (RLIMIT_NPROC)");
|
2013-12-05 11:04:47 +01:00
|
|
|
# endif /* RLIMIT_NPROC */
|
2014-04-22 19:47:19 +02:00
|
|
|
|
|
|
|
# ifdef RLIMIT_STACK
|
|
|
|
int argc = Application::GetArgC();
|
|
|
|
char **argv = Application::GetArgV();
|
|
|
|
bool set_stack_rlimit = true;
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "--no-stack-rlimit") == 0) {
|
|
|
|
set_stack_rlimit = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 13:33:01 +02:00
|
|
|
if (getrlimit(RLIMIT_STACK, &rl) < 0) {
|
|
|
|
Log(LogWarning, "Application", "Could not determine resource limit for stack size (RLIMIT_STACK)");
|
|
|
|
rl.rlim_max = RLIM_INFINITY;
|
|
|
|
}
|
2014-04-22 19:47:19 +02:00
|
|
|
|
2014-07-21 13:33:01 +02:00
|
|
|
if (set_stack_rlimit)
|
|
|
|
rl.rlim_cur = 256 * 1024;
|
|
|
|
else
|
|
|
|
rl.rlim_cur = rl.rlim_max;
|
2014-04-22 19:47:19 +02:00
|
|
|
|
2014-07-21 13:33:01 +02:00
|
|
|
if (setrlimit(RLIMIT_STACK, &rl) < 0)
|
|
|
|
Log(LogNotice, "Application", "Could not adjust resource limit for stack size (RLIMIT_STACK)");
|
|
|
|
else if (set_stack_rlimit) {
|
|
|
|
char **new_argv = static_cast<char **>(malloc(sizeof(char *) * (argc + 2)));
|
2014-04-22 19:47:19 +02:00
|
|
|
|
2014-07-21 13:33:01 +02:00
|
|
|
if (!new_argv) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
2014-04-22 19:47:19 +02:00
|
|
|
|
2014-08-06 10:35:27 +02:00
|
|
|
new_argv[0] = argv[0];
|
|
|
|
new_argv[1] = strdup("--no-stack-rlimit");
|
2014-04-22 19:47:19 +02:00
|
|
|
|
2014-08-06 10:35:27 +02:00
|
|
|
if (!new_argv[1]) {
|
2014-07-21 13:33:01 +02:00
|
|
|
perror("strdup");
|
2014-04-22 19:47:19 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2014-07-21 13:33:01 +02:00
|
|
|
|
2014-08-06 10:35:27 +02:00
|
|
|
for (int i = 1; i < argc; i++)
|
|
|
|
new_argv[i + 1] = argv[i];
|
|
|
|
|
2014-07-21 13:33:01 +02:00
|
|
|
new_argv[argc + 1] = NULL;
|
|
|
|
|
|
|
|
if (execvp(new_argv[0], new_argv) < 0)
|
|
|
|
perror("execvp");
|
|
|
|
|
|
|
|
exit(1);
|
2014-04-22 19:47:19 +02:00
|
|
|
}
|
|
|
|
# else /* RLIMIT_STACK */
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogNotice, "Application", "System does not support adjusting the resource limit for stack size (RLIMIT_STACK)");
|
2014-04-22 19:47:19 +02:00
|
|
|
# endif /* RLIMIT_STACK */
|
2013-12-05 11:04:47 +01:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2014-05-17 23:07:10 +02:00
|
|
|
void Application::RunEventLoop(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2013-02-19 23:02:08 +01:00
|
|
|
Timer::Initialize();
|
|
|
|
|
2014-04-22 19:47:19 +02:00
|
|
|
double lastLoop = Utility::GetTime();
|
|
|
|
|
2014-04-27 21:47:25 +02:00
|
|
|
mainloop:
|
|
|
|
while (!m_ShuttingDown && !m_RequestRestart) {
|
2014-04-22 19:47:19 +02:00
|
|
|
/* Watches for changes to the system time. Adjusts timers if necessary. */
|
|
|
|
Utility::Sleep(2.5);
|
|
|
|
|
2014-05-22 11:22:30 +02:00
|
|
|
if (m_RequestReopenLogs) {
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogNotice, "Application", "Reopening log files");
|
2014-05-22 11:22:30 +02:00
|
|
|
m_RequestReopenLogs = false;
|
|
|
|
OnReopenLogs();
|
|
|
|
}
|
|
|
|
|
2014-04-22 19:47:19 +02:00
|
|
|
double now = Utility::GetTime();
|
|
|
|
double timeDiff = lastLoop - now;
|
|
|
|
|
|
|
|
if (abs(timeDiff) > 15) {
|
|
|
|
/* We made a significant jump in time. */
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "We jumped "
|
|
|
|
<< (timeDiff < 0 ? "forward" : "backward")
|
|
|
|
<< " in time: " << abs(timeDiff) << " seconds";
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogInformation, "Application", msgbuf.str());
|
2014-04-22 19:47:19 +02:00
|
|
|
|
|
|
|
Timer::AdjustTimers(-timeDiff);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastLoop = now;
|
2014-04-28 23:30:56 +02:00
|
|
|
}
|
2014-08-04 14:03:37 +02:00
|
|
|
|
2014-04-28 23:30:56 +02:00
|
|
|
if (m_RequestRestart) {
|
|
|
|
m_RequestRestart = false; // we are now handling the request, once is enough
|
2014-04-27 21:47:25 +02:00
|
|
|
|
2014-04-28 23:30:56 +02:00
|
|
|
// are we already restarting? ignore request if we already are
|
2014-05-11 17:14:35 +02:00
|
|
|
if (l_Restarting)
|
2014-04-27 21:47:25 +02:00
|
|
|
goto mainloop;
|
2014-04-28 23:30:56 +02:00
|
|
|
|
2014-05-11 17:14:35 +02:00
|
|
|
l_Restarting = true;
|
2014-05-17 23:07:10 +02:00
|
|
|
m_ReloadProcess = StartReloadProcess();
|
2014-04-28 23:30:56 +02:00
|
|
|
|
|
|
|
goto mainloop;
|
2014-04-27 21:47:25 +02:00
|
|
|
}
|
2014-08-04 14:03:37 +02:00
|
|
|
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogInformation, "Application", "Shutting down Icinga...");
|
2014-03-17 09:04:19 +01:00
|
|
|
DynamicObject::StopObjects();
|
2013-10-17 10:56:42 +02:00
|
|
|
Application::GetInstance()->OnShutdown();
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
GetTP().Stop();
|
|
|
|
m_ShuttingDown = false;
|
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
GetTP().Join(true);
|
2013-02-19 23:02:08 +01:00
|
|
|
|
|
|
|
Timer::Uninitialize();
|
2013-10-17 10:56:42 +02:00
|
|
|
#endif /* _DEBUG */
|
2012-09-25 15:24:14 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 15:57:46 +01:00
|
|
|
void Application::OnShutdown(void)
|
|
|
|
{
|
|
|
|
/* Nothing to do here. */
|
|
|
|
}
|
|
|
|
|
2014-05-11 17:14:35 +02:00
|
|
|
static void ReloadProcessCallback(const ProcessResult& pr)
|
|
|
|
{
|
|
|
|
if (pr.ExitStatus != 0)
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogCritical, "Application", "Found error in config: reloading aborted");
|
2014-05-11 17:14:35 +02:00
|
|
|
l_Restarting = false;
|
|
|
|
}
|
|
|
|
|
2014-05-17 23:07:10 +02:00
|
|
|
pid_t Application::StartReloadProcess(void)
|
2014-04-27 21:47:25 +02:00
|
|
|
{
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogInformation, "Application", "Got reload command: Starting new instance.");
|
2014-04-27 21:47:25 +02:00
|
|
|
|
|
|
|
// prepare arguments
|
2014-05-01 11:27:43 +02:00
|
|
|
Array::Ptr args = make_shared<Array>();
|
|
|
|
args->Add(GetExePath(m_ArgV[0]));
|
2014-04-27 21:47:25 +02:00
|
|
|
|
2014-04-29 00:05:39 +02:00
|
|
|
for (int i=1; i < Application::GetArgC(); i++) {
|
2014-04-27 21:47:25 +02:00
|
|
|
if (std::string(Application::GetArgV()[i]) != "--reload-internal")
|
2014-05-01 11:27:43 +02:00
|
|
|
args->Add(Application::GetArgV()[i]);
|
2014-04-29 00:05:39 +02:00
|
|
|
else
|
|
|
|
i++; // the next parameter after --reload-internal is the pid, remove that too
|
|
|
|
}
|
2014-05-01 11:27:43 +02:00
|
|
|
args->Add("--reload-internal");
|
|
|
|
args->Add(Convert::ToString(Utility::GetPid()));
|
2014-04-27 21:47:25 +02:00
|
|
|
|
2014-05-01 11:27:43 +02:00
|
|
|
Process::Ptr process = make_shared<Process>(Process::PrepareCommand(args));
|
2014-04-28 23:30:56 +02:00
|
|
|
process->SetTimeout(300);
|
2014-05-11 17:14:35 +02:00
|
|
|
process->Run(&ReloadProcessCallback);
|
2014-08-04 14:03:37 +02:00
|
|
|
|
2014-05-18 18:04:34 +02:00
|
|
|
return process->GetPID();
|
2014-04-27 21:47:25 +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
|
|
|
|
2013-08-30 14:27:24 +02:00
|
|
|
/**
|
|
|
|
* Signals the application to restart during the next
|
|
|
|
* execution of the event loop.
|
|
|
|
*/
|
|
|
|
void Application::RequestRestart(void)
|
|
|
|
{
|
2014-04-27 21:47:25 +02:00
|
|
|
m_RequestRestart = true;
|
2013-08-30 14:27:24 +02:00
|
|
|
}
|
|
|
|
|
2014-05-22 11:22:30 +02:00
|
|
|
/**
|
|
|
|
* Signals the application to reopen log files during the
|
|
|
|
* next execution of the event loop.
|
|
|
|
*/
|
|
|
|
void Application::RequestReopenLogs(void)
|
|
|
|
{
|
|
|
|
m_RequestReopenLogs = true;
|
|
|
|
}
|
|
|
|
|
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-11-15 09:04:39 +01:00
|
|
|
/**
|
2014-07-23 09:30:56 +02:00
|
|
|
* Display version and path information.
|
2013-11-15 09:04:39 +01:00
|
|
|
*/
|
2014-07-23 09:30:56 +02:00
|
|
|
void Application::DisplayInfoMessage(bool skipVersion)
|
2013-11-15 09:04:39 +01:00
|
|
|
{
|
2014-07-23 09:30:56 +02:00
|
|
|
std::cerr << "Application information:" << std::endl;
|
|
|
|
|
|
|
|
if (!skipVersion)
|
2014-07-23 13:05:20 +02:00
|
|
|
std::cerr << " Application version: " << GetVersion() << std::endl;
|
2014-07-23 09:30:56 +02:00
|
|
|
|
|
|
|
std::cerr << " Installation root: " << GetPrefixDir() << std::endl
|
|
|
|
<< " Sysconf directory: " << GetSysconfDir() << std::endl
|
|
|
|
<< " Run directory: " << GetRunDir() << std::endl
|
|
|
|
<< " Local state directory: " << GetLocalStateDir() << std::endl
|
|
|
|
<< " Package data directory: " << GetPkgDataDir() << std::endl
|
|
|
|
<< " State path: " << GetStatePath() << std::endl
|
2014-08-12 13:46:54 +02:00
|
|
|
<< " Objects path: " << GetObjectsPath() << std::endl
|
2014-07-23 09:30:56 +02:00
|
|
|
<< " PID path: " << GetPidPath() << std::endl
|
|
|
|
<< " Application type: " << GetApplicationType() << std::endl;
|
2013-11-15 09:04:39 +01: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
|
2013-11-15 09:04:39 +01:00
|
|
|
<< "* This would indicate a runtime problem or configuration error. If you believe this is a bug in Icinga 2" << std::endl
|
|
|
|
<< "* please submit a bug report at https://dev.icinga.org/ and include this stack trace as well as any other" << std::endl
|
|
|
|
<< "* information that might be useful in order to reproduce this problem." << std::endl
|
|
|
|
<< "***" << std::endl;
|
2013-02-01 19:36:47 +01:00
|
|
|
}
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
#ifndef _WIN32
|
2012-04-24 14:02:15 +02:00
|
|
|
/**
|
2013-11-17 19:58:32 +01:00
|
|
|
* Signal handler for SIGINT and SIGTERM. Prepares the application for cleanly
|
2012-05-18 22:53:35 +02:00
|
|
|
* shutting down during the next execution of the event loop.
|
2012-04-24 14:02:15 +02:00
|
|
|
*
|
2013-08-20 11:06:04 +02:00
|
|
|
* @param - The signal number.
|
2012-04-24 14:02:15 +02:00
|
|
|
*/
|
2013-11-17 19:58:32 +01:00
|
|
|
void Application::SigIntTermHandler(int signum)
|
2012-04-03 19:49:56 +02:00
|
|
|
{
|
2013-03-06 11:03:50 +01:00
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = SIG_DFL;
|
2013-11-17 19:58:32 +01:00
|
|
|
sigaction(signum, &sa, NULL);
|
2013-03-06 11:03:50 +01:00
|
|
|
|
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
|
|
|
|
2014-05-22 11:22:30 +02:00
|
|
|
/**
|
|
|
|
* Signal handler for SIGUSR1. This signal causes Icinga to re-open
|
|
|
|
* its log files and is mainly for use by logrotate.
|
|
|
|
*
|
|
|
|
* @param - The signal number.
|
|
|
|
*/
|
|
|
|
void Application::SigUsr1Handler(int)
|
|
|
|
{
|
|
|
|
RequestReopenLogs();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2013-08-20 11:06:04 +02:00
|
|
|
* @param - The signal number.
|
2013-01-30 11:51:15 +01:00
|
|
|
*/
|
2013-08-20 11:06:04 +02:00
|
|
|
void Application::SigAbrtHandler(int)
|
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-11-15 09:04:39 +01:00
|
|
|
std::cerr << "Caught SIGABRT." << std::endl
|
2013-12-12 14:38:34 +01:00
|
|
|
<< "Current time: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime()) << std::endl
|
2013-11-15 09:04:39 +01:00
|
|
|
<< std::endl;
|
|
|
|
|
2014-07-23 09:30:56 +02:00
|
|
|
DisplayInfoMessage();
|
2013-01-30 11:51:15 +01:00
|
|
|
|
2013-03-07 15:16:01 +01:00
|
|
|
StackTrace trace;
|
2013-11-19 07:49:41 +01:00
|
|
|
std::cerr << "Stacktrace:" << std::endl;
|
2013-03-07 15:16:01 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-09-09 14:49:21 +02:00
|
|
|
if (l_InExceptionHandler)
|
|
|
|
for (;;)
|
|
|
|
Utility::Sleep(5);
|
|
|
|
|
|
|
|
l_InExceptionHandler = true;
|
|
|
|
|
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-11-15 09:04:39 +01:00
|
|
|
std::cerr << "Caught unhandled exception." << std::endl
|
2013-12-12 14:38:34 +01:00
|
|
|
<< "Current time: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime()) << std::endl
|
2013-11-15 09:04:39 +01:00
|
|
|
<< std::endl;
|
|
|
|
|
2014-07-23 09:30:56 +02:00
|
|
|
DisplayInfoMessage();
|
2013-11-15 09:04:39 +01:00
|
|
|
|
2013-01-30 11:51:15 +01:00
|
|
|
try {
|
2014-04-14 03:02:33 +02:00
|
|
|
RethrowUncaughtException();
|
2013-01-30 11:51:15 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2013-02-06 12:51:12 +01:00
|
|
|
std::cerr << std::endl
|
2013-11-20 21:55:14 +01:00
|
|
|
<< DiagnosticInformation(ex)
|
2013-01-30 11:51:15 +01:00
|
|
|
<< std::endl;
|
2013-03-07 19:44:39 +01:00
|
|
|
}
|
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)
|
|
|
|
{
|
2014-09-09 14:49:21 +02:00
|
|
|
if (l_InExceptionHandler)
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
|
|
|
|
l_InExceptionHandler = true;
|
|
|
|
|
2014-07-23 09:30:56 +02:00
|
|
|
DisplayInfoMessage();
|
2013-11-15 09:04:39 +01:00
|
|
|
|
|
|
|
std::cerr << "Caught unhandled SEH exception." << std::endl
|
2013-12-12 14:38:34 +01:00
|
|
|
<< "Current time: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime()) << std::endl
|
2013-11-15 09:04:39 +01:00
|
|
|
<< std::endl;
|
2013-03-07 15:00:26 +01:00
|
|
|
|
|
|
|
StackTrace trace(exi);
|
2013-11-19 07:49:41 +01:00
|
|
|
std::cerr << "Stacktrace:" << std::endl;
|
2013-03-07 15:00:26 +01:00
|
|
|
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));
|
2013-11-17 19:58:32 +01:00
|
|
|
sa.sa_handler = &Application::SigIntTermHandler;
|
2012-04-06 08:56:52 +02:00
|
|
|
sigaction(SIGINT, &sa, NULL);
|
2013-11-17 19:58:32 +01:00
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
2012-04-24 19:55:18 +02:00
|
|
|
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGPIPE, &sa, NULL);
|
2014-05-22 11:22:30 +02:00
|
|
|
|
|
|
|
sa.sa_handler = &Application::SigUsr1Handler;
|
|
|
|
sigaction(SIGUSR1, &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 */
|
|
|
|
|
2014-06-05 17:44:41 +02:00
|
|
|
try {
|
|
|
|
UpdatePidFile(GetPidPath());
|
2014-08-25 08:35:35 +02:00
|
|
|
} catch (const std::exception&) {
|
2014-06-05 17:44:41 +02:00
|
|
|
Log(LogCritical, "Application", "Cannot update PID file '" + GetPidPath() + "'. Aborting.");
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-10 16:03:36 +02:00
|
|
|
|
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.
|
2014-05-17 23:07:10 +02:00
|
|
|
* @param pid The PID to write; default is the current PID
|
2012-09-14 14:41:17 +02:00
|
|
|
*/
|
2014-05-17 23:07:10 +02:00
|
|
|
void Application::UpdatePidFile(const String& filename, pid_t pid)
|
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
|
|
|
|
2014-06-05 17:44:41 +02:00
|
|
|
if (m_PidFile == NULL) {
|
|
|
|
Log(LogCritical, "Application", "Could not open PID file '" + filename + "'.");
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open PID file '" + filename + "'"));
|
2014-06-05 17:44:41 +02:00
|
|
|
}
|
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);
|
|
|
|
|
2013-11-24 00:27:10 +01:00
|
|
|
struct flock lock;
|
|
|
|
|
|
|
|
lock.l_start = 0;
|
|
|
|
lock.l_len = 0;
|
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETLK, &lock) < 0) {
|
2014-05-28 13:17:02 +02:00
|
|
|
Log(LogCritical, "Application", "Could not lock PID file. Make sure that only one instance of the application is running.");
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2014-08-04 16:43:34 +02:00
|
|
|
Application::Exit(EXIT_FAILURE);
|
2013-03-02 09:07:47 +01:00
|
|
|
}
|
2013-03-12 14:06:59 +01:00
|
|
|
|
2013-08-28 10:48:19 +02:00
|
|
|
if (ftruncate(fd, 0) < 0) {
|
2014-06-05 17:44:41 +02:00
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "ftruncate() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
|
|
|
|
Log(LogCritical, "Application", msgbuf.str());
|
|
|
|
|
2013-08-28 10:48:19 +02:00
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("ftruncate")
|
|
|
|
<< boost::errinfo_errno(errno));
|
|
|
|
}
|
2012-07-13 15:29:39 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-07-12 17:03:34 +02:00
|
|
|
|
2014-05-17 23:07:10 +02:00
|
|
|
fprintf(m_PidFile, "%d\n", pid);
|
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.
|
|
|
|
*/
|
2014-05-18 16:23:16 +02:00
|
|
|
void Application::ClosePidFile(bool unlink)
|
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);
|
|
|
|
|
2012-07-12 17:03:34 +02:00
|
|
|
if (m_PidFile != NULL)
|
2014-05-18 16:23:16 +02:00
|
|
|
{
|
|
|
|
if (unlink) {
|
|
|
|
String pidpath = GetPidPath();
|
|
|
|
::unlink(pidpath.CStr());
|
|
|
|
}
|
|
|
|
|
2012-07-12 17:03:34 +02:00
|
|
|
fclose(m_PidFile);
|
2014-05-18 16:23:16 +02:00
|
|
|
}
|
2012-07-12 17:03:34 +02:00
|
|
|
|
|
|
|
m_PidFile = NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-14 00:16:48 +02:00
|
|
|
/**
|
|
|
|
* Checks if another process currently owns the pidfile and read it
|
|
|
|
*
|
|
|
|
* @param filename The name of the PID file.
|
2014-05-01 18:42:36 +02:00
|
|
|
* @returns 0: no process owning the pidfile, pid of the process otherwise
|
2014-04-14 00:16:48 +02:00
|
|
|
*/
|
|
|
|
pid_t Application::ReadPidFile(const String& filename)
|
|
|
|
{
|
|
|
|
FILE *pidfile = fopen(filename.CStr(), "r");
|
|
|
|
|
|
|
|
if (pidfile == NULL)
|
2014-05-01 18:42:36 +02:00
|
|
|
return 0;
|
2014-04-14 00:16:48 +02:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
int fd = fileno(pidfile);
|
|
|
|
|
|
|
|
struct flock lock;
|
|
|
|
|
|
|
|
lock.l_start = 0;
|
|
|
|
lock.l_len = 0;
|
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
|
|
|
|
if (fcntl(fd, F_GETLK, &lock) < 0) {
|
|
|
|
int error = errno;
|
|
|
|
fclose(pidfile);
|
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("fcntl")
|
|
|
|
<< boost::errinfo_errno(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lock.l_type == F_UNLCK) {
|
|
|
|
// nobody has locked the file: no icinga running
|
|
|
|
fclose(pidfile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
pid_t runningpid;
|
|
|
|
int res = fscanf(pidfile, "%d", &runningpid);
|
|
|
|
fclose(pidfile);
|
|
|
|
|
|
|
|
// bogus result?
|
|
|
|
if (res != 1)
|
2014-05-01 18:42:36 +02:00
|
|
|
return 0;
|
2014-04-14 00:16:48 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2014-05-01 18:42:36 +02:00
|
|
|
HANDLE hProcess = OpenProcess(0, FALSE, runningpid);
|
|
|
|
|
|
|
|
if (!hProcess)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
CloseHandle(hProcess);
|
2014-04-14 00:16:48 +02:00
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
return runningpid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-28 13:16:08 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path of the installation prefix.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetPrefixDir(void)
|
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
return ScriptVariable::Get("PrefixDir");
|
2012-09-28 13:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the installation prefix.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
2013-11-03 13:45:26 +01:00
|
|
|
void Application::DeclarePrefixDir(const String& path)
|
2012-09-28 13:16:08 +02:00
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
ScriptVariable::Set("PrefixDir", path, false);
|
2012-09-28 13:16:08 +02:00
|
|
|
}
|
|
|
|
|
2013-11-27 09:23:30 +01:00
|
|
|
/**
|
|
|
|
* Retrives the path of the sysconf dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetSysconfDir(void)
|
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
return ScriptVariable::Get("SysconfDir");
|
2013-11-27 09:23:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path of the sysconf dir.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
|
|
|
void Application::DeclareSysconfDir(const String& path)
|
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
ScriptVariable::Set("SysconfDir", path, false);
|
2013-11-27 09:23:30 +01:00
|
|
|
}
|
|
|
|
|
2014-07-22 13:18:41 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path for the run dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetRunDir(void)
|
|
|
|
{
|
|
|
|
return ScriptVariable::Get("RunDir");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path of the run dir.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
|
|
|
void Application::DeclareRunDir(const String& path)
|
|
|
|
{
|
|
|
|
ScriptVariable::Set("RunDir", path, false);
|
|
|
|
}
|
|
|
|
|
2012-09-28 13:16:08 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path for the local state dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetLocalStateDir(void)
|
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
return ScriptVariable::Get("LocalStateDir");
|
2012-09-28 13:16:08 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 13:18:27 +02:00
|
|
|
/**
|
2014-07-22 13:18:41 +02:00
|
|
|
* Sets the path for the local state dir.
|
2014-05-13 13:18:27 +02:00
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
2014-07-22 13:18:41 +02:00
|
|
|
void Application::DeclareLocalStateDir(const String& path)
|
2014-05-13 13:18:27 +02:00
|
|
|
{
|
2014-07-22 13:18:41 +02:00
|
|
|
ScriptVariable::Set("LocalStateDir", path, false);
|
2014-05-13 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the path for the local state dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetZonesDir(void)
|
|
|
|
{
|
2014-10-16 12:27:09 +02:00
|
|
|
return ScriptVariable::Get("ZonesDir", &Empty);
|
2014-05-13 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
2012-09-28 13:16:08 +02:00
|
|
|
/**
|
2014-07-22 13:18:41 +02:00
|
|
|
* Sets the path of the zones dir.
|
2012-09-28 13:16:08 +02:00
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
2014-07-22 13:18:41 +02:00
|
|
|
void Application::DeclareZonesDir(const String& path)
|
2012-10-02 09:26:17 +02:00
|
|
|
{
|
2014-07-22 13:18:41 +02:00
|
|
|
ScriptVariable::Set("ZonesDir", path, false);
|
2012-10-02 09:26:17 +02:00
|
|
|
}
|
2013-02-01 22:44:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the path for the package data dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetPkgDataDir(void)
|
|
|
|
{
|
2014-10-16 12:27:09 +02:00
|
|
|
String defaultValue = "";
|
|
|
|
return ScriptVariable::Get("PkgDataDir", &Empty);
|
2013-02-01 22:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the package data dir.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
2013-11-03 13:45:26 +01:00
|
|
|
void Application::DeclarePkgDataDir(const String& path)
|
2013-02-01 22:44:58 +01:00
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
ScriptVariable::Set("PkgDataDir", path, false);
|
2013-02-01 22:44:58 +01:00
|
|
|
}
|
2013-02-14 10:55:47 +01:00
|
|
|
|
2014-05-10 18:23:08 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path for the include conf dir.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetIncludeConfDir(void)
|
|
|
|
{
|
2014-10-16 12:27:09 +02:00
|
|
|
return ScriptVariable::Get("IncludeConfDir", &Empty);
|
2014-05-10 18:23:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the package data dir.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
|
|
|
void Application::DeclareIncludeConfDir(const String& path)
|
|
|
|
{
|
|
|
|
ScriptVariable::Set("IncludeConfDir", path, false);
|
|
|
|
}
|
|
|
|
|
2013-08-29 10:17:12 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path for the state file.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetStatePath(void)
|
|
|
|
{
|
2014-10-16 12:27:09 +02:00
|
|
|
return ScriptVariable::Get("StatePath", &Empty);
|
2013-08-30 12:04:24 +02:00
|
|
|
}
|
2013-08-29 10:17:12 +02:00
|
|
|
|
2013-08-30 12:04:24 +02:00
|
|
|
/**
|
2013-09-10 16:03:36 +02:00
|
|
|
* Sets the path for the state file.
|
2013-08-30 12:04:24 +02:00
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
2013-11-03 13:45:26 +01:00
|
|
|
void Application::DeclareStatePath(const String& path)
|
2013-08-30 12:04:24 +02:00
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
ScriptVariable::Set("StatePath", path, false);
|
2013-08-29 10:17:12 +02:00
|
|
|
}
|
|
|
|
|
2014-08-12 13:46:54 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path for the objects file.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetObjectsPath(void)
|
|
|
|
{
|
2014-10-16 12:27:09 +02:00
|
|
|
return ScriptVariable::Get("ObjectsPath", &Empty);
|
2014-08-12 13:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the objects file.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
|
|
|
void Application::DeclareObjectsPath(const String& path)
|
|
|
|
{
|
|
|
|
ScriptVariable::Set("ObjectsPath", path, false);
|
|
|
|
}
|
|
|
|
|
2013-09-10 16:03:36 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path for the PID file.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
|
|
|
String Application::GetPidPath(void)
|
|
|
|
{
|
2014-10-16 12:27:09 +02:00
|
|
|
return ScriptVariable::Get("PidPath", &Empty);
|
2013-09-10 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the path for the PID file.
|
|
|
|
*
|
|
|
|
* @param path The new path.
|
|
|
|
*/
|
2013-11-03 13:45:26 +01:00
|
|
|
void Application::DeclarePidPath(const String& path)
|
2013-09-10 16:03:36 +02:00
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
ScriptVariable::Set("PidPath", path, false);
|
2013-09-10 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the name of the Application type.
|
|
|
|
*
|
|
|
|
* @returns The name.
|
|
|
|
*/
|
|
|
|
String Application::GetApplicationType(void)
|
|
|
|
{
|
|
|
|
return ScriptVariable::Get("ApplicationType");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the name of the Application type.
|
|
|
|
*
|
|
|
|
* @param path The new type name.
|
|
|
|
*/
|
2013-11-03 13:45:26 +01:00
|
|
|
void Application::DeclareApplicationType(const String& type)
|
2013-09-10 16:03:36 +02:00
|
|
|
{
|
2013-12-04 10:41:26 +01:00
|
|
|
ScriptVariable::Set("ApplicationType", type, false);
|
2013-09-10 16:03:36 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 13:43:05 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the name of the user.
|
|
|
|
*
|
|
|
|
* @returns The name.
|
|
|
|
*/
|
|
|
|
String Application::GetRunAsUser(void)
|
|
|
|
{
|
|
|
|
return ScriptVariable::Get("RunAsUser");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the name of the user.
|
|
|
|
*
|
|
|
|
* @param path The new user name.
|
|
|
|
*/
|
|
|
|
void Application::DeclareRunAsUser(const String& user)
|
|
|
|
{
|
|
|
|
ScriptVariable::Set("RunAsUser", user, false);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Retrieves the name of the group.
|
|
|
|
*
|
|
|
|
* @returns The name.
|
|
|
|
*/
|
|
|
|
String Application::GetRunAsGroup(void)
|
|
|
|
{
|
|
|
|
return ScriptVariable::Get("RunAsGroup");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the name of the group.
|
|
|
|
*
|
|
|
|
* @param path The new group name.
|
|
|
|
*/
|
|
|
|
void Application::DeclareRunAsGroup(const String& group)
|
|
|
|
{
|
|
|
|
ScriptVariable::Set("RunAsGroup", group, false);
|
|
|
|
}
|
|
|
|
|
2013-12-04 11:04:36 +01:00
|
|
|
void Application::MakeVariablesConstant(void)
|
|
|
|
{
|
2014-04-07 21:30:27 +02:00
|
|
|
ScriptVariable::GetByName("PrefixDir")->SetConstant(true);
|
|
|
|
ScriptVariable::GetByName("SysconfDir")->SetConstant(true);
|
|
|
|
ScriptVariable::GetByName("LocalStateDir")->SetConstant(true);
|
2014-08-04 14:03:37 +02:00
|
|
|
ScriptVariable::GetByName("RunDir")->SetConstant(true);
|
2014-04-07 21:30:27 +02:00
|
|
|
ScriptVariable::GetByName("PkgDataDir")->SetConstant(true);
|
2014-08-12 13:46:54 +02:00
|
|
|
ScriptVariable::GetByName("StatePath")->SetConstant(true);
|
|
|
|
ScriptVariable::GetByName("ObjectsPath")->SetConstant(true);
|
|
|
|
ScriptVariable::GetByName("PidPath")->SetConstant(true);
|
2013-12-04 11:04:36 +01:00
|
|
|
ScriptVariable::GetByName("ApplicationType")->SetConstant(true);
|
2014-10-13 13:43:05 +02:00
|
|
|
ScriptVariable::GetByName("RunAsUser")->SetConstant(true);
|
|
|
|
ScriptVariable::GetByName("RunAsGroup")->SetConstant(true);
|
2013-12-04 11:04:36 +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
|
|
|
}
|
2013-10-09 08:46:10 +02:00
|
|
|
|
|
|
|
String Application::GetVersion(void)
|
|
|
|
{
|
2013-11-03 13:45:26 +01:00
|
|
|
return VERSION;
|
2013-10-09 08:46:10 +02:00
|
|
|
}
|
2013-10-26 09:41:45 +02:00
|
|
|
|
|
|
|
double Application::GetStartTime(void)
|
|
|
|
{
|
|
|
|
return m_StartTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Application::SetStartTime(double ts)
|
|
|
|
{
|
|
|
|
m_StartTime = ts;
|
|
|
|
}
|