-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp_state.h
73 lines (57 loc) · 1.77 KB
/
app_state.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2022, Eugene Gershnik
// SPDX-License-Identifier: BSD-3-Clause
#ifndef HEADER_APP_STATE_H_INCLUDED
#define HEADER_APP_STATE_H_INCLUDED
#include "xml_wrapper.h"
#include "command_line.h"
#include "pid_file.h"
#include "config.h"
class AppState {
public:
AppState(int argc, char ** argv, std::set<int> untouchedSignals);
void reload();
auto config() const -> const refcnt_ptr<Config> {
return m_config;
}
auto shouldFork() const -> bool {
return m_currentCommandLine.chrootDir || m_currentCommandLine.runAs;
}
void preFork();
void postForkInServerProcess() noexcept;
enum class DaemonStatus {
Ready,
Reloading,
Stopping
};
void notify(DaemonStatus status);
private:
void init();
void refresh();
void daemonize();
void setLogLevel();
void setLogOutput(bool firstTime);
void setPidFile();
static auto openLogFile(const std::filesystem::path & filename) -> ptl::FileDescriptor;
static void redirectStdFile(FILE * from, const ptl::FileDescriptor & to);
static void closeAllExcept(const int * first, const int * last);
private:
std::set<int> m_untouchedSignals;
CommandLine m_origCommandLine;
CommandLine m_currentCommandLine;
pid_t m_mainPid;
bool m_isInitialized = false;
std::optional<spdlog::level::level_enum> m_logLevel;
std::optional<std::filesystem::path> m_logFilePath;
std::optional<std::filesystem::path> m_pidFilePath;
#if HAVE_OS_LOG
std::optional<bool> m_logToOsLog;
#endif
#if HAVE_SYSTEMD
decltype(sd_notify) * m_sdNotify = nullptr;
#endif
PidFile m_pidFile;
ptl::FileDescriptor m_savedStdOut;
ptl::FileDescriptor m_savedStdErr;
refcnt_ptr<Config> m_config;
};
#endif