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
375
376
377
378
379
380
381
382
383
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kControl
#include "mongo/platform/basic.h"
#include "mongo/db/initialize_server_global_state.h"
#include "mongo/db/initialize_server_global_state_gen.h"
#include <boost/filesystem/operations.hpp>
#include <iostream>
#include <memory>
#include <signal.h>
#ifndef _WIN32
#include <sys/stat.h>
#include <sys/wait.h>
#include <syslog.h>
#endif
#include "mongo/base/init.h"
#include "mongo/config.h"
#include "mongo/db/server_options.h"
#include "mongo/logger/console_appender.h"
#include "mongo/logger/logger.h"
#include "mongo/logger/message_event.h"
#include "mongo/logger/message_event_utf8_encoder.h"
#include "mongo/logger/ramlog.h"
#include "mongo/logger/rotatable_file_appender.h"
#include "mongo/logger/rotatable_file_manager.h"
#include "mongo/logger/rotatable_file_writer.h"
#include "mongo/logger/syslog_appender.h"
#include "mongo/platform/process_id.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/processinfo.h"
#include "mongo/util/quick_exit.h"
#include "mongo/util/signal_handlers_synchronous.h"
#if defined(__APPLE__)
#include <TargetConditionals.h>
#endif
namespace fs = boost::filesystem;
namespace mongo {
using std::cerr;
using std::cout;
using std::endl;
#ifndef _WIN32
// support for exit value propagation with fork
void launchSignal(int sig) {
if (sig == SIGUSR2) {
ProcessId cur = ProcessId::getCurrent();
if (cur == serverGlobalParams.parentProc || cur == serverGlobalParams.leaderProc) {
// signal indicates successful start allowing us to exit
quickExit(0);
}
}
}
void signalForkSuccess() {
if (serverGlobalParams.doFork) {
// killing leader will propagate to parent
verify(kill(serverGlobalParams.leaderProc.toNative(), SIGUSR2) == 0);
}
}
#endif
static bool forkServer() {
#if !defined(_WIN32) && !(defined(__APPLE__) && TARGET_OS_TV)
if (serverGlobalParams.doFork) {
fassert(16447, !serverGlobalParams.logpath.empty() || serverGlobalParams.logWithSyslog);
cout.flush();
cerr.flush();
serverGlobalParams.parentProc = ProcessId::getCurrent();
// clear signal mask so that SIGUSR2 will always be caught and we can clean up the original
// parent process
clearSignalMask();
// facilitate clean exit when child starts successfully
verify(signal(SIGUSR2, launchSignal) != SIG_ERR);
cout << "about to fork child process, waiting until server is ready for connections."
<< endl;
pid_t child1 = fork();
if (child1 == -1) {
cout << "ERROR: stage 1 fork() failed: " << errnoWithDescription();
quickExit(EXIT_ABRUPT);
} else if (child1) {
// this is run in the original parent process
int pstat;
if (waitpid(child1, &pstat, 0) == pid_t{-1}) {
perror("waitpid");
quickExit(-1);
}
if (WIFEXITED(pstat)) {
if (WEXITSTATUS(pstat)) {
cout << "ERROR: child process failed, exited with error number "
<< WEXITSTATUS(pstat) << endl
<< "To see additional information in this output, start without "
<< "the \"--fork\" option." << endl;
} else {
cout << "child process started successfully, parent exiting" << endl;
}
quickExit(WEXITSTATUS(pstat));
}
quickExit(50);
}
if (chdir("/") < 0) {
cout << "Cant chdir() while forking server process: " << strerror(errno) << endl;
quickExit(-1);
}
setsid();
serverGlobalParams.leaderProc = ProcessId::getCurrent();
pid_t child2 = fork();
if (child2 == -1) {
cout << "ERROR: stage 2 fork() failed: " << errnoWithDescription();
quickExit(EXIT_ABRUPT);
} else if (child2) {
// this is run in the middle process
int pstat;
cout << "forked process: " << child2 << endl;
if (waitpid(child2, &pstat, 0) == pid_t{-1}) {
perror("waitpid");
quickExit(-1);
}
if (WIFEXITED(pstat)) {
quickExit(WEXITSTATUS(pstat));
}
quickExit(51);
}
// this is run in the final child process (the server)
FILE* f = freopen("/dev/null", "w", stdout);
if (f == NULL) {
cout << "Cant reassign stdout while forking server process: " << strerror(errno)
<< endl;
return false;
}
f = freopen("/dev/null", "w", stderr);
if (f == NULL) {
cout << "Cant reassign stderr while forking server process: " << strerror(errno)
<< endl;
return false;
}
f = freopen("/dev/null", "r", stdin);
if (f == NULL) {
cout << "Cant reassign stdin while forking server process: " << strerror(errno) << endl;
return false;
}
}
#endif // !defined(_WIN32)
return true;
}
void forkServerOrDie() {
if (!forkServer())
quickExit(EXIT_FAILURE);
}
// On POSIX platforms we need to set our umask before opening any log files, so this
// should depend on MungeUmask above, but not on Windows.
MONGO_INITIALIZER_GENERAL(
ServerLogRedirection,
("GlobalLogManager", "EndStartupOptionHandling", "ForkServer", "MungeUmask"),
("default"))
(InitializerContext*) {
using logger::LogManager;
using logger::MessageEventEphemeral;
using logger::MessageEventDetailsEncoder;
using logger::MessageEventWithContextEncoder;
using logger::MessageLogDomain;
using logger::RotatableFileAppender;
using logger::StatusWithRotatableFileWriter;
// Hook up this global into our logging encoder
MessageEventDetailsEncoder::setMaxLogSizeKBSource(gMaxLogSizeKB);
if (serverGlobalParams.logWithSyslog) {
#ifdef _WIN32
return Status(ErrorCodes::InternalError,
"Syslog requested in Windows build; command line processor logic error");
#else
using logger::SyslogAppender;
StringBuilder sb;
sb << serverGlobalParams.binaryName << "." << serverGlobalParams.port;
openlog(strdup(sb.str().c_str()), LOG_PID | LOG_CONS, serverGlobalParams.syslogFacility);
LogManager* manager = logger::globalLogManager();
manager->getGlobalDomain()->clearAppenders();
manager->getGlobalDomain()->attachAppender(
std::make_unique<SyslogAppender<MessageEventEphemeral>>(
std::make_unique<logger::MessageEventDetailsEncoder>()));
manager->getNamedDomain("javascriptOutput")
->attachAppender(std::make_unique<SyslogAppender<MessageEventEphemeral>>(
std::make_unique<logger::MessageEventDetailsEncoder>()));
#endif // defined(_WIN32)
} else if (!serverGlobalParams.logpath.empty()) {
fassert(16448, !serverGlobalParams.logWithSyslog);
std::string absoluteLogpath =
boost::filesystem::absolute(serverGlobalParams.logpath, serverGlobalParams.cwd)
.string();
bool exists;
try {
exists = boost::filesystem::exists(absoluteLogpath);
} catch (boost::filesystem::filesystem_error& e) {
return Status(ErrorCodes::FileNotOpen,
mongoutils::str::stream() << "Failed probe for \"" << absoluteLogpath
<< "\": "
<< e.code().message());
}
if (exists) {
if (boost::filesystem::is_directory(absoluteLogpath)) {
return Status(
ErrorCodes::FileNotOpen,
mongoutils::str::stream() << "logpath \"" << absoluteLogpath
<< "\" should name a file, not a directory.");
}
if (!serverGlobalParams.logAppend && boost::filesystem::is_regular(absoluteLogpath)) {
std::string renameTarget = absoluteLogpath + "." + terseCurrentTime(false);
boost::system::error_code ec;
boost::filesystem::rename(absoluteLogpath, renameTarget, ec);
if (!ec) {
log() << "log file \"" << absoluteLogpath << "\" exists; moved to \""
<< renameTarget << "\".";
} else {
return Status(ErrorCodes::FileRenameFailed,
mongoutils::str::stream()
<< "Could not rename preexisting log file \""
<< absoluteLogpath
<< "\" to \""
<< renameTarget
<< "\"; run with --logappend or manually remove file: "
<< ec.message());
}
}
}
StatusWithRotatableFileWriter writer = logger::globalRotatableFileManager()->openFile(
absoluteLogpath, serverGlobalParams.logAppend);
if (!writer.isOK()) {
return writer.getStatus();
}
LogManager* manager = logger::globalLogManager();
manager->getGlobalDomain()->clearAppenders();
manager->getGlobalDomain()->attachAppender(
std::make_unique<RotatableFileAppender<MessageEventEphemeral>>(
std::make_unique<MessageEventDetailsEncoder>(), writer.getValue()));
manager->getNamedDomain("javascriptOutput")
->attachAppender(std::make_unique<RotatableFileAppender<MessageEventEphemeral>>(
std::make_unique<MessageEventDetailsEncoder>(), writer.getValue()));
if (serverGlobalParams.logAppend && exists) {
log() << "***** SERVER RESTARTED *****";
Status status = logger::RotatableFileWriter::Use(writer.getValue()).status();
if (!status.isOK())
return status;
}
} else {
logger::globalLogManager()
->getNamedDomain("javascriptOutput")
->attachAppender(std::make_unique<logger::ConsoleAppender<MessageEventEphemeral>>(
std::make_unique<MessageEventDetailsEncoder>()));
}
logger::globalLogDomain()->attachAppender(
std::make_unique<RamLogAppender>(RamLog::get("global")));
return Status::OK();
}
/**
* atexit handler to terminate the process before static destructors run.
*
* Mongo server processes cannot safely call ::exit() or std::exit(), but
* some third-party libraries may call one of those functions. In that
* case, to avoid static-destructor problems in the server, this exits the
* process immediately with code EXIT_FAILURE.
*
* TODO: Remove once exit() executes safely in mongo server processes.
*/
static void shortCircuitExit() {
quickExit(EXIT_FAILURE);
}
MONGO_INITIALIZER(RegisterShortCircuitExitHandler)(InitializerContext*) {
if (std::atexit(&shortCircuitExit) != 0)
return Status(ErrorCodes::InternalError, "Failed setting short-circuit exit handler.");
return Status::OK();
}
// On non-windows platforms, drop rwx for group and other unless the
// user has opted into using the system umask. To do so, we first read
// out the current umask (by temporarily setting it to
// no-permissions), and then or the returned umask with the
// restrictions we want to apply and set it back. The overall effect
// is to set the bits for 'other' and 'group', but leave umask bits
// bits for 'user' unaltered.
namespace {
MONGO_INITIALIZER_WITH_PREREQUISITES(MungeUmask, ("EndStartupOptionHandling"))
(InitializerContext*) {
#ifndef _WIN32
if (!gHonorSystemUmask) {
umask(umask(S_IRWXU | S_IRWXG | S_IRWXO) | S_IRWXG | S_IRWXO);
}
#endif
return Status::OK();
}
} // namespace
bool initializeServerGlobalState(ServiceContext* service) {
#ifndef _WIN32
if (!serverGlobalParams.noUnixSocket && !fs::is_directory(serverGlobalParams.socket)) {
cout << serverGlobalParams.socket << " must be a directory" << endl;
return false;
}
#endif
if (!serverGlobalParams.pidFile.empty()) {
if (!writePidFile(serverGlobalParams.pidFile)) {
// error message logged in writePidFile
return false;
}
}
return true;
}
} // namespace mongo
|