1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/application.hpp"
21 #include "base/application.tcpp"
22 #include "base/stacktrace.hpp"
23 #include "base/timer.hpp"
24 #include "base/logger.hpp"
25 #include "base/exception.hpp"
26 #include "base/objectlock.hpp"
27 #include "base/utility.hpp"
28 #include "base/loader.hpp"
29 #include "base/debug.hpp"
30 #include "base/type.hpp"
31 #include "base/convert.hpp"
32 #include "base/scriptglobal.hpp"
33 #include "base/process.hpp"
34 #include <boost/algorithm/string/classification.hpp>
35 #include <boost/foreach.hpp>
36 #include <boost/algorithm/string/split.hpp>
37 #include <boost/algorithm/string/trim.hpp>
38 #include <boost/exception/errinfo_api_function.hpp>
39 #include <boost/exception/errinfo_errno.hpp>
40 #include <boost/exception/errinfo_file_name.hpp>
45 #include <sys/prctl.h>
46 #endif /* __linux__ */
48 using namespace icinga;
50 REGISTER_TYPE(Application);
52 boost::signals2::signal<void (void)> Application::OnReopenLogs;
53 Application::Ptr Application::m_Instance = NULL;
54 bool Application::m_ShuttingDown = false;
55 bool Application::m_RequestRestart = false;
56 bool Application::m_RequestReopenLogs = false;
57 pid_t Application::m_ReloadProcess = 0;
58 static bool l_Restarting = false;
59 static bool l_InExceptionHandler = false;
60 int Application::m_ArgC;
61 char **Application::m_ArgV;
62 double Application::m_StartTime;
63 bool Application::m_ScriptDebuggerEnabled = false;
66 * Constructor for the Application class.
68 void Application::OnConfigLoaded(void)
72 ASSERT(m_Instance == NULL);
77 * Destructor for the application class.
79 void Application::Stop(bool runtimeRemoved)
81 m_ShuttingDown = true;
87 // Getting a shutdown-signal when a restart is in progress usually
88 // means that the restart succeeded and the new process wants to take
89 // over. Write the PID of the new process to the pidfile before this
90 // process exits to keep systemd happy.
93 UpdatePidFile(GetPidPath(), m_ReloadProcess);
94 } catch (const std::exception&) {
96 Log(LogCritical, "Application", "Cannot update PID file. Aborting restart operation.");
103 ObjectImpl<Application>::Stop(runtimeRemoved);
106 Application::~Application(void)
111 void Application::Exit(int rc)
116 BOOST_FOREACH(const Logger::Ptr& logger, Logger::GetLoggers()) {
124 _exit(rc); // Yay, our static destructors are pretty much beyond repair at this point.
125 #endif /* I2_DEBUG */
128 void Application::InitializeBase(void)
131 /* disable GUI-based error messages for LoadLibrary() */
132 SetErrorMode(SEM_FAILCRITICALERRORS);
135 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
136 BOOST_THROW_EXCEPTION(win32_error()
137 << boost::errinfo_api_function("WSAStartup")
138 << errinfo_win32_error(WSAGetLastError()));
142 Loader::ExecuteDeferredInitializers();
144 /* make sure the thread pool gets initialized */
150 void Application::UninitializeBase(void)
152 Timer::Uninitialize();
158 * Retrieves a pointer to the application singleton object.
160 * @returns The application object.
162 Application::Ptr Application::GetInstance(void)
167 void Application::SetResourceLimits(void)
172 # ifdef RLIMIT_NOFILE
173 rl.rlim_cur = 16 * 1024;
174 rl.rlim_max = rl.rlim_cur;
176 if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
177 Log(LogNotice, "Application", "Could not adjust resource limit for open file handles (RLIMIT_NOFILE)");
178 # else /* RLIMIT_NOFILE */
179 Log(LogNotice, "Application", "System does not support adjusting the resource limit for open file handles (RLIMIT_NOFILE)");
180 # endif /* RLIMIT_NOFILE */
183 rl.rlim_cur = 16 * 1024;
184 rl.rlim_max = rl.rlim_cur;
186 if (setrlimit(RLIMIT_NPROC, &rl) < 0)
187 Log(LogNotice, "Application", "Could not adjust resource limit for number of processes (RLIMIT_NPROC)");
188 # else /* RLIMIT_NPROC */
189 Log(LogNotice, "Application", "System does not support adjusting the resource limit for number of processes (RLIMIT_NPROC)");
190 # endif /* RLIMIT_NPROC */
193 int argc = Application::GetArgC();
194 char **argv = Application::GetArgV();
195 bool set_stack_rlimit = true;
197 for (int i = 0; i < argc; i++) {
198 if (strcmp(argv[i], "--no-stack-rlimit") == 0) {
199 set_stack_rlimit = false;
204 if (getrlimit(RLIMIT_STACK, &rl) < 0) {
205 Log(LogWarning, "Application", "Could not determine resource limit for stack size (RLIMIT_STACK)");
206 rl.rlim_max = RLIM_INFINITY;
209 if (set_stack_rlimit)
210 rl.rlim_cur = 256 * 1024;
212 rl.rlim_cur = rl.rlim_max;
214 if (setrlimit(RLIMIT_STACK, &rl) < 0)
215 Log(LogNotice, "Application", "Could not adjust resource limit for stack size (RLIMIT_STACK)");
216 else if (set_stack_rlimit) {
217 char **new_argv = static_cast<char **>(malloc(sizeof(char *) * (argc + 2)));
224 new_argv[0] = argv[0];
225 new_argv[1] = strdup("--no-stack-rlimit");
232 for (int i = 1; i < argc; i++)
233 new_argv[i + 1] = argv[i];
235 new_argv[argc + 1] = NULL;
237 (void) execvp(new_argv[0], new_argv);
241 # else /* RLIMIT_STACK */
242 Log(LogNotice, "Application", "System does not support adjusting the resource limit for stack size (RLIMIT_STACK)");
243 # endif /* RLIMIT_STACK */
244 #endif /* __linux__ */
247 int Application::GetArgC(void)
252 void Application::SetArgC(int argc)
257 char **Application::GetArgV(void)
262 void Application::SetArgV(char **argv)
268 * Processes events for registered sockets and timers and calls whatever
269 * handlers have been set up for these events.
271 void Application::RunEventLoop(void)
275 double lastLoop = Utility::GetTime();
278 while (!m_ShuttingDown && !m_RequestRestart) {
279 /* Watches for changes to the system time. Adjusts timers if necessary. */
282 if (m_RequestReopenLogs) {
283 Log(LogNotice, "Application", "Reopening log files");
284 m_RequestReopenLogs = false;
288 double now = Utility::GetTime();
289 double timeDiff = lastLoop - now;
291 if (std::fabs(timeDiff) > 15) {
292 /* We made a significant jump in time. */
293 Log(LogInformation, "Application")
295 << (timeDiff < 0 ? "forward" : "backward")
296 << " in time: " << std::fabs(timeDiff) << " seconds";
298 Timer::AdjustTimers(-timeDiff);
304 if (m_RequestRestart) {
305 m_RequestRestart = false; // we are now handling the request, once is enough
307 // are we already restarting? ignore request if we already are
312 m_ReloadProcess = StartReloadProcess();
317 Log(LogInformation, "Application", "Shutting down...");
319 ConfigObject::StopObjects();
320 Application::GetInstance()->OnShutdown();
325 void Application::OnShutdown(void)
327 /* Nothing to do here. */
330 static void ReloadProcessCallbackInternal(const ProcessResult& pr)
332 if (pr.ExitStatus != 0)
333 Log(LogCritical, "Application", "Found error in config: reloading aborted");
336 Application::Exit(7); /* keep this exit code in sync with icinga-app */
340 static void ReloadProcessCallback(const ProcessResult& pr)
342 l_Restarting = false;
344 boost::thread t(boost::bind(&ReloadProcessCallbackInternal, pr));
348 pid_t Application::StartReloadProcess(void)
350 Log(LogInformation, "Application", "Got reload command: Starting new instance.");
353 Array::Ptr args = new Array();
354 args->Add(GetExePath(m_ArgV[0]));
356 for (int i=1; i < Application::GetArgC(); i++) {
357 if (std::string(Application::GetArgV()[i]) != "--reload-internal")
358 args->Add(Application::GetArgV()[i]);
360 i++; // the next parameter after --reload-internal is the pid, remove that too
364 args->Add("--reload-internal");
365 args->Add(Convert::ToString(Utility::GetPid()));
367 args->Add("--validate");
370 Process::Ptr process = new Process(Process::PrepareCommand(args));
371 process->SetTimeout(300);
372 process->Run(&ReloadProcessCallback);
374 return process->GetPID();
378 * Signals the application to shut down during the next
379 * execution of the event loop.
381 void Application::RequestShutdown(void)
383 Log(LogInformation, "Application", "Received request to shut down.");
385 m_ShuttingDown = true;
389 * Signals the application to restart during the next
390 * execution of the event loop.
392 void Application::RequestRestart(void)
394 m_RequestRestart = true;
398 * Signals the application to reopen log files during the
399 * next execution of the event loop.
401 void Application::RequestReopenLogs(void)
403 m_RequestReopenLogs = true;
407 * Retrieves the full path of the executable.
409 * @param argv0 The first command-line argument.
412 String Application::GetExePath(const String& argv0)
414 String executablePath;
417 char buffer[MAXPATHLEN];
418 if (getcwd(buffer, sizeof(buffer)) == NULL) {
419 BOOST_THROW_EXCEPTION(posix_error()
420 << boost::errinfo_api_function("getcwd")
421 << boost::errinfo_errno(errno));
424 String workingDirectory = buffer;
427 executablePath = workingDirectory + "/" + argv0;
429 executablePath = argv0;
431 bool foundSlash = false;
432 for (size_t i = 0; i < argv0.GetLength(); i++) {
433 if (argv0[i] == '/') {
440 const char *pathEnv = getenv("PATH");
441 if (pathEnv != NULL) {
442 std::vector<String> paths;
443 boost::algorithm::split(paths, pathEnv, boost::is_any_of(":"));
445 bool foundPath = false;
446 BOOST_FOREACH(String& path, paths) {
447 String pathTest = path + "/" + argv0;
449 if (access(pathTest.CStr(), X_OK) == 0) {
450 executablePath = pathTest;
457 executablePath.Clear();
458 BOOST_THROW_EXCEPTION(std::runtime_error("Could not determine executable path."));
463 if (realpath(executablePath.CStr(), buffer) == NULL) {
464 BOOST_THROW_EXCEPTION(posix_error()
465 << boost::errinfo_api_function("realpath")
466 << boost::errinfo_errno(errno)
467 << boost::errinfo_file_name(executablePath));
472 char FullExePath[MAXPATHLEN];
474 if (!GetModuleFileName(NULL, FullExePath, sizeof(FullExePath)))
475 BOOST_THROW_EXCEPTION(win32_error()
476 << boost::errinfo_api_function("GetModuleFileName")
477 << errinfo_win32_error(GetLastError()));
484 * Display version and path information.
486 void Application::DisplayInfoMessage(std::ostream& os, bool skipVersion)
488 os << "Application information:" << "\n";
491 os << " Application version: " << GetAppVersion() << "\n";
493 os << " Installation root: " << GetPrefixDir() << "\n"
494 << " Sysconf directory: " << GetSysconfDir() << "\n"
495 << " Run directory: " << GetRunDir() << "\n"
496 << " Local state directory: " << GetLocalStateDir() << "\n"
497 << " Package data directory: " << GetPkgDataDir() << "\n"
498 << " State path: " << GetStatePath() << "\n"
499 << " Modified attributes path: " << GetModAttrPath() << "\n"
500 << " Objects path: " << GetObjectsPath() << "\n"
501 << " Vars path: " << GetVarsPath() << "\n"
502 << " PID path: " << GetPidPath() << "\n";
505 << "System information:" << "\n"
506 << " Platform: " << Utility::GetPlatformName() << "\n"
507 << " Platform version: " << Utility::GetPlatformVersion() << "\n"
508 << " Kernel: " << Utility::GetPlatformKernel() << "\n"
509 << " Kernel version: " << Utility::GetPlatformKernelVersion() << "\n"
510 << " Architecture: " << Utility::GetPlatformArchitecture() << "\n";
514 * Displays a message that tells users what to do when they encounter a bug.
516 void Application::DisplayBugMessage(std::ostream& os)
519 << "* This would indicate a runtime problem or configuration error. If you believe this is a bug in Icinga 2" << "\n"
520 << "* please submit a bug report at https://dev.icinga.org/ and include this stack trace as well as any other" << "\n"
521 << "* information that might be useful in order to reproduce this problem." << "\n"
525 String Application::GetCrashReportFilename(void)
527 return GetLocalStateDir() + "/log/icinga2/crash/report." + Convert::ToString(Utility::GetTime());
531 void Application::AttachDebugger(const String& filename, bool interactive)
535 prctl(PR_SET_DUMPABLE, 1);
536 #endif /* __linux __ */
538 String my_pid = Convert::ToString(Utility::GetPid());
543 BOOST_THROW_EXCEPTION(posix_error()
544 << boost::errinfo_api_function("fork")
545 << boost::errinfo_errno(errno));
550 int fd = open(filename.CStr(), O_CREAT | O_RDWR | O_APPEND, 0600);
553 BOOST_THROW_EXCEPTION(posix_error()
554 << boost::errinfo_api_function("open")
555 << boost::errinfo_errno(errno)
556 << boost::errinfo_file_name(filename));
560 /* redirect stdout to the file */
565 /* redirect stderr to stdout */
573 char *my_pid_str = strdup(my_pid.CStr());
576 const char *uargv[] = {
582 argv = const_cast<char **>(uargv);
584 const char *uargv[] = {
590 "thread apply all bt full",
597 argv = const_cast<char **>(uargv);
600 (void)execvp(argv[0], argv);
601 perror("Failed to launch GDB");
607 if (waitpid(pid, &status, 0) < 0) {
608 BOOST_THROW_EXCEPTION(posix_error()
609 << boost::errinfo_api_function("waitpid")
610 << boost::errinfo_errno(errno));
614 prctl(PR_SET_DUMPABLE, 0);
615 #endif /* __linux __ */
623 * Signal handler for SIGINT and SIGTERM. Prepares the application for cleanly
624 * shutting down during the next execution of the event loop.
626 * @param - The signal number.
628 void Application::SigIntTermHandler(int signum)
631 memset(&sa, 0, sizeof(sa));
632 sa.sa_handler = SIG_DFL;
633 sigaction(signum, &sa, NULL);
635 Application::Ptr instance = Application::GetInstance();
640 instance->RequestShutdown();
645 * Signal handler for SIGUSR1. This signal causes Icinga to re-open
646 * its log files and is mainly for use by logrotate.
648 * @param - The signal number.
650 void Application::SigUsr1Handler(int)
656 * Signal handler for SIGABRT. Helps with debugging ASSERT()s.
658 * @param - The signal number.
660 void Application::SigAbrtHandler(int)
664 memset(&sa, 0, sizeof(sa));
665 sa.sa_handler = SIG_DFL;
666 sigaction(SIGABRT, &sa, NULL);
669 std::cerr << "Caught SIGABRT." << std::endl
670 << "Current time: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime()) << std::endl
673 String fname = GetCrashReportFilename();
674 Utility::MkDir(Utility::DirName(fname), 0750);
676 bool interactive_debugger = Convert::ToBool(ScriptGlobal::Get("AttachDebugger"));
678 if (!interactive_debugger) {
680 ofs.open(fname.CStr());
682 Log(LogCritical, "Application")
683 << "Icinga 2 has terminated unexpectedly. Additional information can be found in '" << fname << "'" << "\n";
685 DisplayInfoMessage(ofs);
688 ofs << "Stacktrace:" << "\n";
691 DisplayBugMessage(ofs);
696 Log(LogCritical, "Application", "Icinga 2 has terminated unexpeectedly. Attaching debugger...");
699 AttachDebugger(fname, interactive_debugger);
704 * Console control handler. Prepares the application for cleanly
705 * shutting down during the next execution of the event loop.
707 BOOL WINAPI Application::CtrlHandler(DWORD type)
709 Application::Ptr instance = Application::GetInstance();
714 instance->RequestShutdown();
716 SetConsoleCtrlHandler(NULL, FALSE);
722 * Handler for unhandled exceptions.
724 void Application::ExceptionHandler(void)
726 if (l_InExceptionHandler)
730 l_InExceptionHandler = true;
734 memset(&sa, 0, sizeof(sa));
735 sa.sa_handler = SIG_DFL;
736 sigaction(SIGABRT, &sa, NULL);
739 String fname = GetCrashReportFilename();
740 Utility::MkDir(Utility::DirName(fname), 0750);
742 bool interactive_debugger = Convert::ToBool(ScriptGlobal::Get("AttachDebugger"));
744 if (interactive_debugger) {
746 ofs.open(fname.CStr());
748 ofs << "Caught unhandled exception." << "\n"
749 << "Current time: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime()) << "\n"
752 DisplayInfoMessage(ofs);
755 RethrowUncaughtException();
756 } catch (const std::exception& ex) {
757 Log(LogCritical, "Application")
758 << DiagnosticInformation(ex, false) << "\n"
760 << "Additional information is available in '" << fname << "'" << "\n";
763 << DiagnosticInformation(ex)
767 DisplayBugMessage(ofs);
772 AttachDebugger(fname, interactive_debugger);
778 LONG CALLBACK Application::SEHUnhandledExceptionFilter(PEXCEPTION_POINTERS exi)
780 if (l_InExceptionHandler)
781 return EXCEPTION_CONTINUE_SEARCH;
783 l_InExceptionHandler = true;
785 String fname = GetCrashReportFilename();
786 Utility::MkDir(Utility::DirName(fname), 0750);
789 ofs.open(fname.CStr());
791 Log(LogCritical, "Application")
792 << "Icinga 2 has terminated unexpectedly. Additional information can be found in '" << fname << "'";
794 DisplayInfoMessage(ofs);
796 ofs << "Caught unhandled SEH exception." << "\n"
797 << "Current time: " << Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", Utility::GetTime()) << "\n"
800 StackTrace trace(exi);
801 ofs << "Stacktrace:" << "\n";
804 DisplayBugMessage(ofs);
806 return EXCEPTION_CONTINUE_SEARCH;
811 * Installs the exception handlers.
813 void Application::InstallExceptionHandlers(void)
815 std::set_terminate(&Application::ExceptionHandler);
819 memset(&sa, 0, sizeof(sa));
820 sa.sa_handler = &Application::SigAbrtHandler;
821 sigaction(SIGABRT, &sa, NULL);
823 SetUnhandledExceptionFilter(&Application::SEHUnhandledExceptionFilter);
828 * Runs the application.
830 * @returns The application's exit code.
832 int Application::Run(void)
836 memset(&sa, 0, sizeof(sa));
837 sa.sa_handler = &Application::SigIntTermHandler;
838 sigaction(SIGINT, &sa, NULL);
839 sigaction(SIGTERM, &sa, NULL);
841 sa.sa_handler = SIG_IGN;
842 sigaction(SIGPIPE, &sa, NULL);
844 sa.sa_handler = &Application::SigUsr1Handler;
845 sigaction(SIGUSR1, &sa, NULL);
847 SetConsoleCtrlHandler(&Application::CtrlHandler, TRUE);
851 UpdatePidFile(GetPidPath());
852 } catch (const std::exception&) {
853 Log(LogCritical, "Application")
854 << "Cannot update PID file '" << GetPidPath() << "'. Aborting.";
862 * Grabs the PID file lock and updates the PID. Terminates the application
863 * if the PID file is already locked by another instance of the application.
865 * @param filename The name of the PID file.
866 * @param pid The PID to write; default is the current PID
868 void Application::UpdatePidFile(const String& filename, pid_t pid)
870 ObjectLock olock(this);
872 if (m_PidFile != NULL)
875 /* There's just no sane way of getting a file descriptor for a
876 * C++ ofstream which is why we're using FILEs here. */
877 m_PidFile = fopen(filename.CStr(), "r+");
879 if (m_PidFile == NULL)
880 m_PidFile = fopen(filename.CStr(), "w");
882 if (m_PidFile == NULL) {
883 Log(LogCritical, "Application")
884 << "Could not open PID file '" << filename << "'.";
885 BOOST_THROW_EXCEPTION(std::runtime_error("Could not open PID file '" + filename + "'"));
889 int fd = fileno(m_PidFile);
891 Utility::SetCloExec(fd);
897 lock.l_type = F_WRLCK;
898 lock.l_whence = SEEK_SET;
900 if (fcntl(fd, F_SETLK, &lock) < 0) {
901 Log(LogCritical, "Application", "Could not lock PID file. Make sure that only one instance of the application is running.");
903 Application::Exit(EXIT_FAILURE);
906 if (ftruncate(fd, 0) < 0) {
907 Log(LogCritical, "Application")
908 << "ftruncate() failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\"";
910 BOOST_THROW_EXCEPTION(posix_error()
911 << boost::errinfo_api_function("ftruncate")
912 << boost::errinfo_errno(errno));
916 fprintf(m_PidFile, "%lu\n", (unsigned long)pid);
921 * Closes the PID file. Does nothing if the PID file is not currently open.
923 void Application::ClosePidFile(bool unlink)
925 ObjectLock olock(this);
927 if (m_PidFile != NULL)
930 String pidpath = GetPidPath();
931 ::unlink(pidpath.CStr());
941 * Checks if another process currently owns the pidfile and read it
943 * @param filename The name of the PID file.
944 * @returns 0: no process owning the pidfile, pid of the process otherwise
946 pid_t Application::ReadPidFile(const String& filename)
948 FILE *pidfile = fopen(filename.CStr(), "r");
954 int fd = fileno(pidfile);
960 lock.l_type = F_WRLCK;
961 lock.l_whence = SEEK_SET;
963 if (fcntl(fd, F_GETLK, &lock) < 0) {
966 BOOST_THROW_EXCEPTION(posix_error()
967 << boost::errinfo_api_function("fcntl")
968 << boost::errinfo_errno(error));
971 if (lock.l_type == F_UNLCK) {
972 // nobody has locked the file: no icinga running
979 int res = fscanf(pidfile, "%d", &runningpid);
987 HANDLE hProcess = OpenProcess(0, FALSE, runningpid);
992 CloseHandle(hProcess);
1000 * Retrieves the path of the installation prefix.
1002 * @returns The path.
1004 String Application::GetPrefixDir(void)
1006 return ScriptGlobal::Get("PrefixDir");
1010 * Sets the path for the installation prefix.
1012 * @param path The new path.
1014 void Application::DeclarePrefixDir(const String& path)
1016 if (!ScriptGlobal::Exists("PrefixDir"))
1017 ScriptGlobal::Set("PrefixDir", path);
1021 * Retrives the path of the sysconf dir.
1023 * @returns The path.
1025 String Application::GetSysconfDir(void)
1027 return ScriptGlobal::Get("SysconfDir");
1031 * Sets the path of the sysconf dir.
1033 * @param path The new path.
1035 void Application::DeclareSysconfDir(const String& path)
1037 if (!ScriptGlobal::Exists("SysconfDir"))
1038 ScriptGlobal::Set("SysconfDir", path);
1042 * Retrieves the path for the run dir.
1044 * @returns The path.
1046 String Application::GetRunDir(void)
1048 return ScriptGlobal::Get("RunDir");
1052 * Sets the path of the run dir.
1054 * @param path The new path.
1056 void Application::DeclareRunDir(const String& path)
1058 if (!ScriptGlobal::Exists("RunDir"))
1059 ScriptGlobal::Set("RunDir", path);
1063 * Retrieves the path for the local state dir.
1065 * @returns The path.
1067 String Application::GetLocalStateDir(void)
1069 return ScriptGlobal::Get("LocalStateDir");
1073 * Sets the path for the local state dir.
1075 * @param path The new path.
1077 void Application::DeclareLocalStateDir(const String& path)
1079 if (!ScriptGlobal::Exists("LocalStateDir"))
1080 ScriptGlobal::Set("LocalStateDir", path);
1084 * Retrieves the path for the local state dir.
1086 * @returns The path.
1088 String Application::GetZonesDir(void)
1090 return ScriptGlobal::Get("ZonesDir", &Empty);
1094 * Sets the path of the zones dir.
1096 * @param path The new path.
1098 void Application::DeclareZonesDir(const String& path)
1100 if (!ScriptGlobal::Exists("ZonesDir"))
1101 ScriptGlobal::Set("ZonesDir", path);
1105 * Retrieves the path for the package data dir.
1107 * @returns The path.
1109 String Application::GetPkgDataDir(void)
1111 String defaultValue = "";
1112 return ScriptGlobal::Get("PkgDataDir", &Empty);
1116 * Sets the path for the package data dir.
1118 * @param path The new path.
1120 void Application::DeclarePkgDataDir(const String& path)
1122 if (!ScriptGlobal::Exists("PkgDataDir"))
1123 ScriptGlobal::Set("PkgDataDir", path);
1127 * Retrieves the path for the include conf dir.
1129 * @returns The path.
1131 String Application::GetIncludeConfDir(void)
1133 return ScriptGlobal::Get("IncludeConfDir", &Empty);
1137 * Sets the path for the package data dir.
1139 * @param path The new path.
1141 void Application::DeclareIncludeConfDir(const String& path)
1143 if (!ScriptGlobal::Exists("IncludeConfDir"))
1144 ScriptGlobal::Set("IncludeConfDir", path);
1148 * Retrieves the path for the state file.
1150 * @returns The path.
1152 String Application::GetStatePath(void)
1154 return ScriptGlobal::Get("StatePath", &Empty);
1158 * Sets the path for the state file.
1160 * @param path The new path.
1162 void Application::DeclareStatePath(const String& path)
1164 if (!ScriptGlobal::Exists("StatePath"))
1165 ScriptGlobal::Set("StatePath", path);
1169 * Retrieves the path for the modified attributes file.
1171 * @returns The path.
1173 String Application::GetModAttrPath(void)
1175 return ScriptGlobal::Get("ModAttrPath", &Empty);
1179 * Sets the path for the modified attributes file.
1181 * @param path The new path.
1183 void Application::DeclareModAttrPath(const String& path)
1185 if (!ScriptGlobal::Exists("ModAttrPath"))
1186 ScriptGlobal::Set("ModAttrPath", path);
1190 * Retrieves the path for the objects file.
1192 * @returns The path.
1194 String Application::GetObjectsPath(void)
1196 return ScriptGlobal::Get("ObjectsPath", &Empty);
1200 * Sets the path for the objects file.
1202 * @param path The new path.
1204 void Application::DeclareObjectsPath(const String& path)
1206 if (!ScriptGlobal::Exists("ObjectsPath"))
1207 ScriptGlobal::Set("ObjectsPath", path);
1211 * Retrieves the path for the vars file.
1213 * @returns The path.
1215 String Application::GetVarsPath(void)
1217 return ScriptGlobal::Get("VarsPath", &Empty);
1221 * Sets the path for the vars file.
1223 * @param path The new path.
1225 void Application::DeclareVarsPath(const String& path)
1227 if (!ScriptGlobal::Exists("VarsPath"))
1228 ScriptGlobal::Set("VarsPath", path);
1232 * Retrieves the path for the PID file.
1234 * @returns The path.
1236 String Application::GetPidPath(void)
1238 return ScriptGlobal::Get("PidPath", &Empty);
1242 * Sets the path for the PID file.
1244 * @param path The new path.
1246 void Application::DeclarePidPath(const String& path)
1248 if (!ScriptGlobal::Exists("PidPath"))
1249 ScriptGlobal::Set("PidPath", path);
1253 * Retrieves the name of the user.
1255 * @returns The name.
1257 String Application::GetRunAsUser(void)
1259 return ScriptGlobal::Get("RunAsUser");
1263 * Sets the name of the user.
1265 * @param path The new user name.
1267 void Application::DeclareRunAsUser(const String& user)
1269 if (!ScriptGlobal::Exists("RunAsUser"))
1270 ScriptGlobal::Set("RunAsUser", user);
1274 * Retrieves the name of the group.
1276 * @returns The name.
1278 String Application::GetRunAsGroup(void)
1280 return ScriptGlobal::Get("RunAsGroup");
1284 * Sets the concurrency level.
1286 * @param path The new concurrency level.
1288 void Application::DeclareConcurrency(int ncpus)
1290 if (!ScriptGlobal::Exists("Concurrency"))
1291 ScriptGlobal::Set("Concurrency", ncpus);
1295 * Retrieves the concurrency level.
1297 * @returns The concurrency level.
1299 int Application::GetConcurrency(void)
1301 Value defaultConcurrency = boost::thread::hardware_concurrency();
1302 return ScriptGlobal::Get("Concurrency", &defaultConcurrency);
1306 * Sets the name of the group.
1308 * @param path The new group name.
1310 void Application::DeclareRunAsGroup(const String& group)
1312 if (!ScriptGlobal::Exists("RunAsGroup"))
1313 ScriptGlobal::Set("RunAsGroup", group);
1317 * Returns the global thread pool.
1319 * @returns The global thread pool.
1321 ThreadPool& Application::GetTP(void)
1323 static ThreadPool tp;
1327 double Application::GetStartTime(void)
1332 void Application::SetStartTime(double ts)
1337 bool Application::GetScriptDebuggerEnabled(void)
1339 return m_ScriptDebuggerEnabled;
1342 void Application::SetScriptDebuggerEnabled(bool enabled)
1344 m_ScriptDebuggerEnabled = enabled;
1347 void Application::ValidateName(const String& value, const ValidationUtils& utils)
1349 ObjectImpl<Application>::ValidateName(value, utils);
1352 BOOST_THROW_EXCEPTION(ValidationError(this, boost::assign::list_of("name"), "Application object must be named 'app'."));