-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp_state.cpp
374 lines (306 loc) · 12.3 KB
/
app_state.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright (c) 2022, Eugene Gershnik
// SPDX-License-Identifier: BSD-3-Clause
#include "app_state.h"
AppState::AppState(int argc, char ** argv, std::set<int> untouchedSignals):
m_untouchedSignals(std::move(untouchedSignals)),
m_mainPid(getpid()) {
m_origCommandLine.parse(argc, argv);
m_currentCommandLine = m_origCommandLine;
#if HAVE_SYSTEMD
if (m_currentCommandLine.daemonType && *m_currentCommandLine.daemonType == DaemonType::Systemd) {
auto * systemd = dlopen(LIBSYSTEMD_SO, RTLD_NOW | RTLD_LOCAL);
if (!systemd) {
WSDLOG_CRITICAL("systemd mode requested but cannot load libsystemd.so: {}", dlerror());
exit(EXIT_FAILURE);
}
m_sdNotify = (decltype(m_sdNotify))dlsym(systemd, "sd_notify");
if (!m_sdNotify) {
WSDLOG_CRITICAL("systemd mode requested but cannot find _sd_notify in libsystemd.so: {}", dlerror());
exit(EXIT_FAILURE);
}
}
#endif
}
void AppState::reload() {
if (m_origCommandLine.configFile) {
m_currentCommandLine = m_origCommandLine;
m_currentCommandLine.mergeConfigFile(*m_origCommandLine.configFile);
m_isInitialized ? refresh() : init();
} else if (!m_isInitialized) {
m_currentCommandLine = m_origCommandLine;
init();
}
m_config = Config::make(m_currentCommandLine);
}
void AppState::init() {
setLogLevel();
auto devNull = ptl::FileDescriptor::open("/dev/null", O_RDWR);
redirectStdFile(stdin, devNull);
if (m_currentCommandLine.daemonType && *m_currentCommandLine.daemonType == DaemonType::Unix) {
m_savedStdOut = ptl::duplicate(devNull);
m_savedStdErr = ptl::duplicate(devNull);
setLogOutput(false); //false to make it replace stdxxx if log file isn't set
devNull.close();
daemonize();
} else {
m_savedStdOut = ptl::duplicate(stdout);
m_savedStdErr = ptl::duplicate(stderr);
setLogOutput(true);
}
setPidFile();
XmlParserInit initXml;
if (getuid() == 0) {
if (!m_currentCommandLine.runAs) {
#if CAN_CREATE_USERS
WSDLOG_DEBUG("Running as root but no account to run under is specified in configuration. Using {}", WSDDN_DEFAULT_USER_NAME);
auto pwd = ptl::Passwd::getByName(WSDDN_DEFAULT_USER_NAME);
if (pwd) {
m_currentCommandLine.runAs = Identity(pwd->pw_uid, pwd->pw_gid);
} else {
WSDLOG_INFO("User {} does not exist, creating", WSDDN_DEFAULT_USER_NAME);
m_currentCommandLine.runAs = Identity::createDaemonUser(WSDDN_DEFAULT_USER_NAME);
}
#else
WSDLOG_CRITICAL("Running network service as a root is extremely insecure and is not allowed.\n"
"Please use one of the following approaches: \n"
" * pass `--user username` command line option to specify which account to run network code under\n"
" * start " WSDDN_PROGNAME " under a non-root account (if using systemd consider DynamicUser approach)"
);
exit(EXIT_FAILURE);
#endif
}
if (!m_currentCommandLine.chrootDir) {
WSDLOG_DEBUG("Running as root but no chroot specified in configuration. Using {}", WSDDN_DEFAULT_CHROOT_DIR);
createMissingDirs(WSDDN_DEFAULT_CHROOT_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP, Identity::admin());
m_currentCommandLine.chrootDir = WSDDN_DEFAULT_CHROOT_DIR;
}
}
m_mainPid = getpid();
m_isInitialized = true;
}
void AppState::refresh() {
if (m_currentCommandLine.logLevel != m_logLevel)
setLogLevel();
if ( m_currentCommandLine.logFile != m_logFilePath
#if HAVE_OS_LOG
|| m_logToOsLog != m_currentCommandLine.logToOsLog
#endif
)
setLogOutput(false);
if ( m_currentCommandLine.pidFile != m_pidFilePath)
setPidFile();
}
void AppState::preFork() {
spdlog::default_logger()->flush();
fflush(stdout);
fflush(stderr);
}
void AppState::postForkInServerProcess() noexcept {
m_savedStdOut.close();
m_savedStdErr.close();
m_pidFile = PidFile();
if (m_currentCommandLine.chrootDir) {
WSDLOG_DEBUG("Changing root directory");
ptl::changeRoot(*m_currentCommandLine.chrootDir);
ptl::changeDirectory("/");
}
if (m_currentCommandLine.runAs) {
WSDLOG_DEBUG("Changing identity");
m_currentCommandLine.runAs->setMyIdentity();
}
}
void AppState::notify([[maybe_unused]] DaemonStatus status) {
#if HAVE_SYSTEMD
if (m_sdNotify) {
std::string env;
switch(status) {
case DaemonStatus::Ready: env += "READY=1"; break;
case DaemonStatus::Reloading: env += "RELOADING=1"; break;
case DaemonStatus::Stopping: env += "STOPPING=1"; break;
}
env += "\nMAINPID=";
env += std::to_string(m_mainPid);
m_sdNotify(0, env.c_str());
}
#endif
}
void AppState::setLogLevel() {
if (m_currentCommandLine.logLevel) {
spdlog::set_level(*m_currentCommandLine.logLevel);
} else {
spdlog::set_level(spdlog::level::info);
}
m_logLevel = m_currentCommandLine.logLevel;
}
void AppState::setLogOutput(bool firstTime) {
#if HAVE_OS_LOG
if (m_currentCommandLine.logToOsLog && *m_currentCommandLine.logToOsLog) {
using Sink = OsLogSink<spdlog::details::null_mutex>;
auto sink = std::make_shared<Sink>();
auto logger = std::make_shared<spdlog::logger>("os_log", std::move(sink));
spdlog::set_default_logger(logger);
spdlog::set_pattern("%v");
} else
#endif
if (m_currentCommandLine.logFile) {
auto fileFd = openLogFile(*m_currentCommandLine.logFile);
redirectStdFile(stdout, fileFd);
redirectStdFile(stderr, fileFd);
auto logger = spdlog::stdout_logger_st("file");
spdlog::set_default_logger(logger);
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] %L -- %v");
} else {
if (!firstTime) {
redirectStdFile(stdout, m_savedStdOut);
redirectStdFile(stderr, m_savedStdErr);
}
auto logger = isatty(fileno(stdout)) ? spdlog::stdout_color_st("console") : spdlog::stdout_logger_st("console");
spdlog::set_default_logger(logger);
#if HAVE_SYSTEMD
if (m_currentCommandLine.daemonType && *m_currentCommandLine.daemonType == DaemonType::Systemd) {
auto formatter = std::make_unique<spdlog::pattern_formatter>();
formatter->add_flag<SystemdLevelFormatter>('l').set_pattern("%l%v");
spdlog::set_formatter(std::move(formatter));
} else {
spdlog::set_pattern("[%l] %v");
}
#else
spdlog::set_pattern("[%l] %v");
#endif
}
m_logFilePath = m_currentCommandLine.logFile;
#if HAVE_OS_LOG
m_logToOsLog = m_currentCommandLine.logToOsLog;
#endif
}
void AppState::setPidFile() {
if (m_currentCommandLine.pidFile) {
auto maybePidFile = PidFile::open(*m_currentCommandLine.pidFile);
if (!maybePidFile)
throw std::runtime_error("another copy of this application is already running");
m_pidFile = std::move(*maybePidFile);
} else {
m_pidFile = PidFile();
}
m_pidFilePath = m_currentCommandLine.pidFile;
}
auto AppState::openLogFile(const std::filesystem::path & filename) -> ptl::FileDescriptor {
std::optional<Identity> owner;
mode_t mode = S_IRUSR | S_IWUSR;
mode_t dirMode = mode | S_IXUSR;
if (getuid() == 0) {
//make root own it and let "standard" permissions apply
owner = Identity::admin();
mode |= S_IRGRP;
dirMode |= S_IRGRP | S_IXGRP;
}
createMissingDirs(filename.parent_path(), dirMode, owner);
auto fd = ptl::FileDescriptor::open(filename, O_WRONLY | O_CREAT | O_APPEND, mode);
if (owner) {
auto admin = Identity::admin();
ptl::changeOwner(fd, admin.uid(), admin.gid());
ptl::changeMode(fd, S_IRUSR | S_IWUSR | S_IRGRP);
}
return fd;
}
void AppState::redirectStdFile(FILE * from, const ptl::FileDescriptor & to) {
fflush(from);
duplicateTo(to, from); //yes, reversed! we make the descriptor "get into" FILE
}
//void AppState::closeAllExcept(const int * first, const int * last) {
//
// std::filesystem::path fdDir = "/proc/self/fd";
// std::error_code ec = std::make_error_code(std::errc::no_such_file_or_directory);
// for (const auto & fdEntry : std::filesystem::directory_iterator(fdDir, ec)) {
// auto filename = fdEntry.path().filename().native();
// auto first = filename.c_str();
// auto last = first + filename.size();
// unsigned fd = 0;
// auto res = std::from_chars(first, last, fd, 10);
// if (res.ec != std::errc() || res.ptr != last)
// continue;
// if (std::find(first, last, fd) != last)
// continue;
// (void)close(fd);
// }
// if (ec) {
// rlimit lim = {};
// if (getrlimit(RLIMIT_NOFILE, &lim) == 0) {
// for(unsigned fd = 3; fd < unsigned(lim.rlim_cur); ++fd) {
// if (std::find(first, last, fd) != last)
// continue;
// (void)close(fd);
// }
// }
// }
//}
void AppState::daemonize() {
auto [reportPipeRead, reportPipeWrite] = ptl::Pipe::create();
char reportBuf[1];
// auto preserve = std::array{
// fileno(stdin),
// fileno(stdout),
// fileno(stderr),
// reportPipe.first.get(),
// reportPipe.second.get(),
// m_savedStdOut.get(),
// m_savedStdErr.get()
// };
fflush(stdout);
fflush(stderr);
// 1. Close all open file descriptors except standard input, output, and error
// This is only meaningfull if you exec - we don't
//closeAllExcept(preserve.begin(), preserve.end());
// 2. Reset all signal handlers to their default
for(int sig = 1; sig < NSIG; ++sig) {
if (!m_untouchedSignals.contains(sig))
(void)signal(sig, SIG_DFL);
}
// 3. Reset the signal mask
auto allSigs = ptl::SignalSet::all();
for(auto sig: m_untouchedSignals)
allSigs.del(sig);
std::error_code ec;
ptl::setSignalProcessMask(SIG_UNBLOCK, allSigs.get());
// 4. Sanitize the environment block. Eh...
// 5. Call fork(), to create a background process.
preFork();
auto childProcess = ptl::forkProcess();
if (childProcess) {
reportPipeWrite.close();
int ret = EXIT_FAILURE;
ssize_t res = readFile(reportPipeRead, reportBuf, 1, ec);
if (res == 1) {
WSDLOG_INFO("Daemon successfully started");
ret = EXIT_SUCCESS;
} else {
WSDLOG_INFO("Daemon failed to start");
}
// 15. Call exit() in the original process.
exit(ret);
}
reportPipeRead.close();
// 6. In the child, call setsid() to detach from any terminal and create an independent session.
ptl::setSessionId();
// 7. Call fork() again, to ensure that the daemon can never re-acquire a terminal again.
preFork();
childProcess = ptl::forkProcess();
// 8. Call exit() in the first child, so that only the second child
// (the actual daemon process) stays around. This ensures that
// the daemon process is re-parented to init/PID 1, as all daemons should be.
if (childProcess)
exit(EXIT_SUCCESS);
// 8a. Start a new process group. This prevents the initial invoker from killing
// the daemon if it (or somebody) kills the process group
ptl::setProcessGroupId(0, 0);
// 9. Connect /dev/null to standard input, output, and error. ... We are handling output outside of this
// 10. Reset the umask to 0... We handle it differently
// 11. Change the current directory to the root directory (/), in order to
// avoid that the daemon involuntarily blocks mount points from being unmounted.
ptl::changeDirectory("/");
// 12. Write the daemon PID to a PID file... We handle it later
// 13. Drop privileges, if possible and applicable... We handle it later
// 14. From the daemon process, notify the original process started that initialization is complete.
reportBuf[0] = 0;
writeFile(reportPipeWrite, reportBuf, 1);
}