]> granicus.if.org Git - icinga2/blob - lib/base/application.hpp
Update copyright headers for 2016
[icinga2] / lib / base / application.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef APPLICATION_H
21 #define APPLICATION_H
22
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"
28 #include <ostream>
29
30 namespace icinga
31 {
32
33 /**
34  * Abstract base class for applications.
35  *
36  * @ingroup base
37  */
38 class I2_BASE_API Application : public ObjectImpl<Application> {
39 public:
40         DECLARE_OBJECT(Application);
41
42         static boost::signals2::signal<void (void)> OnReopenLogs;
43
44         ~Application(void);
45
46         static void InitializeBase(void);
47         static void UninitializeBase(void);
48
49         static Application::Ptr GetInstance(void);
50
51         static void Exit(int rc);
52
53         int Run(void);
54
55         /**
56          * Starts the application.
57          *
58          * @returns The exit code of the application.
59          */
60         virtual int Main(void) = 0;
61
62         static void SetResourceLimits(void);
63
64         static int GetArgC(void);
65         static void SetArgC(int argc);
66
67         static char **GetArgV(void);
68         static void SetArgV(char **argv);
69
70         static void InstallExceptionHandlers(void);
71
72         static void RequestShutdown(void);
73         static void RequestRestart(void);
74         static void RequestReopenLogs(void);
75
76         static void SetDebuggingSeverity(LogSeverity severity);
77         static LogSeverity GetDebuggingSeverity(void);
78
79         void UpdatePidFile(const String& filename, pid_t pid = Utility::GetPid());
80         void ClosePidFile(bool unlink);
81         static pid_t ReadPidFile(const String& filename);
82
83         static String GetExePath(const String& argv0);
84
85         static String GetPrefixDir(void);
86         static void DeclarePrefixDir(const String& path);
87
88         static String GetSysconfDir(void);
89         static void DeclareSysconfDir(const String& path);
90
91         static String GetZonesDir(void);
92         static void DeclareZonesDir(const String& path);
93
94         static String GetRunDir(void);
95         static void DeclareRunDir(const String& path);
96
97         static String GetLocalStateDir(void);
98         static void DeclareLocalStateDir(const String& path);
99
100         static String GetPkgDataDir(void);
101         static void DeclarePkgDataDir(const String& path);
102
103         static String GetIncludeConfDir(void);
104         static void DeclareIncludeConfDir(const String& path);
105
106         static String GetStatePath(void);
107         static void DeclareStatePath(const String& path);
108
109         static String GetModAttrPath(void);
110         static void DeclareModAttrPath(const String& path);
111
112         static String GetObjectsPath(void);
113         static void DeclareObjectsPath(const String& path);
114
115         static String GetVarsPath(void);
116         static void DeclareVarsPath(const String& path);
117
118         static String GetPidPath(void);
119         static void DeclarePidPath(const String& path);
120
121         static String GetRunAsUser(void);
122         static void DeclareRunAsUser(const String& user);
123
124         static String GetRunAsGroup(void);
125         static void DeclareRunAsGroup(const String& group);
126
127         static int GetConcurrency(void);
128         static void DeclareConcurrency(int ncpus);
129
130         static ThreadPool& GetTP(void);
131
132         static String GetAppVersion(void);
133
134         static double GetStartTime(void);
135         static void SetStartTime(double ts);
136
137         static bool GetScriptDebuggerEnabled(void);
138         static void SetScriptDebuggerEnabled(bool enabled);
139
140         static void DisplayInfoMessage(std::ostream& os, bool skipVersion = false);
141
142 protected:
143         virtual void OnConfigLoaded(void) override;
144         virtual void Stop(bool runtimeRemoved) override;
145
146         void RunEventLoop(void);
147
148         pid_t StartReloadProcess(void);
149
150         virtual void OnShutdown(void);
151
152         virtual void ValidateName(const String& value, const ValidationUtils& utils) override;
153
154 private:
155         static Application::Ptr m_Instance; /**< The application instance. */
156
157         static bool m_ShuttingDown; /**< Whether the application is in the process of
158                                   shutting down. */
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. */
163
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;
171
172 #ifndef _WIN32
173         static void SigIntTermHandler(int signum);
174 #else /* _WIN32 */
175         static BOOL WINAPI CtrlHandler(DWORD type);
176         static LONG WINAPI SEHUnhandledExceptionFilter(PEXCEPTION_POINTERS exi);
177 #endif /* _WIN32 */
178
179         static void DisplayBugMessage(std::ostream& os);
180
181         static void SigAbrtHandler(int signum);
182         static void SigUsr1Handler(int signum);
183         static void ExceptionHandler(void);
184
185         static String GetCrashReportFilename(void);
186
187         static void AttachDebugger(const String& filename, bool interactive);
188 };
189
190 }
191
192 #endif /* APPLICATION_H */