]> granicus.if.org Git - icinga2/blob - lib/base/application.hpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / application.hpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #ifndef APPLICATION_H
4 #define APPLICATION_H
5
6 #include "base/i2-base.hpp"
7 #include "base/application-ti.hpp"
8 #include "base/logger.hpp"
9 #include "base/configuration.hpp"
10 #include <iosfwd>
11
12 namespace icinga
13 {
14
15 class ThreadPool;
16
17 /**
18  * Abstract base class for applications.
19  *
20  * @ingroup base
21  */
22 class Application : public ObjectImpl<Application> {
23 public:
24         DECLARE_OBJECT(Application);
25
26         static boost::signals2::signal<void ()> OnReopenLogs;
27
28         ~Application() override;
29
30         static void InitializeBase();
31         static void UninitializeBase();
32
33         static Application::Ptr GetInstance();
34
35         static void Exit(int rc);
36
37         int Run();
38
39         /**
40          * Starts the application.
41          *
42          * @returns The exit code of the application.
43          */
44         virtual int Main() = 0;
45
46         static void SetResourceLimits();
47
48         static int GetArgC();
49         static void SetArgC(int argc);
50
51         static char **GetArgV();
52         static void SetArgV(char **argv);
53
54         static void InstallExceptionHandlers();
55
56         static void RequestShutdown();
57         static void RequestRestart();
58         static void RequestReopenLogs();
59
60         static bool IsShuttingDown();
61         static bool IsRestarting();
62
63         static void SetDebuggingSeverity(LogSeverity severity);
64         static LogSeverity GetDebuggingSeverity();
65
66         void UpdatePidFile(const String& filename);
67         void UpdatePidFile(const String& filename, pid_t pid);
68         void ClosePidFile(bool unlink);
69         static pid_t ReadPidFile(const String& filename);
70
71         static String GetExePath(const String& argv0);
72
73 #ifdef _WIN32
74         static bool IsProcessElevated();
75 #endif /* _WIN32 */
76
77         static int GetDefaultRLimitFiles();
78         static int GetDefaultRLimitProcesses();
79         static int GetDefaultRLimitStack();
80
81         static double GetReloadTimeout();
82
83         static ThreadPool& GetTP();
84
85         static String GetAppVersion();
86         static String GetAppSpecVersion();
87
88         static String GetAppEnvironment();
89         static void SetAppEnvironment(const String& name);
90
91         static double GetStartTime();
92         static void SetStartTime(double ts);
93
94         static double GetMainTime();
95         static void SetMainTime(double ts);
96
97         static bool GetScriptDebuggerEnabled();
98         static void SetScriptDebuggerEnabled(bool enabled);
99
100         static double GetLastReloadFailed();
101         static void SetLastReloadFailed(double ts);
102
103         static void DisplayInfoMessage(std::ostream& os, bool skipVersion = false);
104
105 protected:
106         void OnConfigLoaded() override;
107         void Stop(bool runtimeRemoved) override;
108
109         void RunEventLoop();
110
111         pid_t StartReloadProcess();
112
113         virtual void OnShutdown();
114
115         void ValidateName(const Lazy<String>& lvalue, const ValidationUtils& utils) final;
116
117 private:
118         static Application::Ptr m_Instance; /**< The application instance. */
119
120         static bool m_ShuttingDown; /**< Whether the application is in the process of shutting down. */
121         static bool m_RequestRestart; /**< A restart was requested through SIGHUP */
122         static pid_t m_ReloadProcess; /**< The PID of a subprocess doing a reload, only valid when l_Restarting==true */
123         static bool m_RequestReopenLogs; /**< Whether we should re-open log files. */
124
125         static int m_ArgC; /**< The number of command-line arguments. */
126         static char **m_ArgV; /**< Command-line arguments. */
127         FILE *m_PidFile; /**< The PID file */
128         static bool m_Debugging; /**< Whether debugging is enabled. */
129         static LogSeverity m_DebuggingSeverity; /**< Whether debugging severity is set. */
130         static double m_StartTime;
131         static double m_MainTime;
132         static bool m_ScriptDebuggerEnabled;
133         static double m_LastReloadFailed;
134
135 #ifndef _WIN32
136         static void SigIntTermHandler(int signum);
137 #else /* _WIN32 */
138         static BOOL WINAPI CtrlHandler(DWORD type);
139         static LONG WINAPI SEHUnhandledExceptionFilter(PEXCEPTION_POINTERS exi);
140 #endif /* _WIN32 */
141
142         static void DisplayBugMessage(std::ostream& os);
143
144         static void SigAbrtHandler(int signum);
145         static void SigUsr1Handler(int signum);
146         static void SigUsr2Handler(int signum);
147         static void ExceptionHandler();
148
149         static String GetCrashReportFilename();
150
151         static void AttachDebugger(const String& filename, bool interactive);
152 };
153
154 }
155
156 #endif /* APPLICATION_H */