1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://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 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include "base/application.thpp"
25 #include "base/threadpool.hpp"
26 #include "base/utility.hpp"
27 #include "base/logger.hpp"
34 * Abstract base class for applications.
38 class I2_BASE_API Application : public ObjectImpl<Application> {
40 DECLARE_OBJECT(Application);
42 static boost::signals2::signal<void (void)> OnReopenLogs;
46 static void InitializeBase(void);
47 static void UninitializeBase(void);
49 static Application::Ptr GetInstance(void);
51 static void Exit(int rc);
56 * Starts the application.
58 * @returns The exit code of the application.
60 virtual int Main(void) = 0;
62 static void SetResourceLimits(void);
64 static int GetArgC(void);
65 static void SetArgC(int argc);
67 static char **GetArgV(void);
68 static void SetArgV(char **argv);
70 static void InstallExceptionHandlers(void);
72 static void RequestShutdown(void);
73 static void RequestRestart(void);
74 static void RequestReopenLogs(void);
76 static void SetDebuggingSeverity(LogSeverity severity);
77 static LogSeverity GetDebuggingSeverity(void);
79 void UpdatePidFile(const String& filename, pid_t pid = Utility::GetPid());
80 void ClosePidFile(bool unlink);
81 static pid_t ReadPidFile(const String& filename);
83 static String GetExePath(const String& argv0);
85 static String GetPrefixDir(void);
86 static void DeclarePrefixDir(const String& path);
88 static String GetSysconfDir(void);
89 static void DeclareSysconfDir(const String& path);
91 static String GetZonesDir(void);
92 static void DeclareZonesDir(const String& path);
94 static String GetRunDir(void);
95 static void DeclareRunDir(const String& path);
97 static String GetLocalStateDir(void);
98 static void DeclareLocalStateDir(const String& path);
100 static String GetPkgDataDir(void);
101 static void DeclarePkgDataDir(const String& path);
103 static String GetIncludeConfDir(void);
104 static void DeclareIncludeConfDir(const String& path);
106 static String GetStatePath(void);
107 static void DeclareStatePath(const String& path);
109 static String GetModAttrPath(void);
110 static void DeclareModAttrPath(const String& path);
112 static String GetObjectsPath(void);
113 static void DeclareObjectsPath(const String& path);
115 static String GetVarsPath(void);
116 static void DeclareVarsPath(const String& path);
118 static String GetPidPath(void);
119 static void DeclarePidPath(const String& path);
121 static String GetRunAsUser(void);
122 static void DeclareRunAsUser(const String& user);
124 static String GetRunAsGroup(void);
125 static void DeclareRunAsGroup(const String& group);
127 static int GetConcurrency(void);
128 static void DeclareConcurrency(int ncpus);
130 static ThreadPool& GetTP(void);
132 static String GetAppVersion(void);
134 static double GetStartTime(void);
135 static void SetStartTime(double ts);
137 static bool GetScriptDebuggerEnabled(void);
138 static void SetScriptDebuggerEnabled(bool enabled);
140 static void DisplayInfoMessage(std::ostream& os, bool skipVersion = false);
143 virtual void OnConfigLoaded(void) override;
144 virtual void Stop(bool runtimeRemoved) override;
146 void RunEventLoop(void);
148 pid_t StartReloadProcess(void);
150 virtual void OnShutdown(void);
152 virtual void ValidateName(const String& value, const ValidationUtils& utils) override;
155 static Application::Ptr m_Instance; /**< The application instance. */
157 static bool m_ShuttingDown; /**< Whether the application is in the process of
159 static bool m_RequestRestart; /**< A restart was requested through SIGHUP */
160 static pid_t m_ReloadProcess; /**< The PID of a subprocess doing a reload,
161 only valid when l_Restarting==true */
162 static bool m_RequestReopenLogs; /**< Whether we should re-open log files. */
164 static int m_ArgC; /**< The number of command-line arguments. */
165 static char **m_ArgV; /**< Command-line arguments. */
166 FILE *m_PidFile; /**< The PID file */
167 static bool m_Debugging; /**< Whether debugging is enabled. */
168 static LogSeverity m_DebuggingSeverity; /**< Whether debugging severity is set. */
169 static double m_StartTime;
170 static bool m_ScriptDebuggerEnabled;
173 static void SigIntTermHandler(int signum);
175 static BOOL WINAPI CtrlHandler(DWORD type);
176 static LONG WINAPI SEHUnhandledExceptionFilter(PEXCEPTION_POINTERS exi);
179 static void DisplayBugMessage(std::ostream& os);
181 static void SigAbrtHandler(int signum);
182 static void SigUsr1Handler(int signum);
183 static void ExceptionHandler(void);
185 static String GetCrashReportFilename(void);
187 static void AttachDebugger(const String& filename, bool interactive);
192 #endif /* APPLICATION_H */