-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathfdbmonitor.cpp
686 lines (596 loc) · 19.4 KB
/
fdbmonitor.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* fdbmonitor.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <csignal>
#include <memory>
#include <sys/wait.h>
#include <cinttypes>
#include <sys/stat.h>
#include <fcntl.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <sys/event.h>
#endif
#include "fdbmonitor.h"
#include "SimpleOpt/SimpleOpt.h"
#include "fdbclient/versions.h"
namespace fdbmonitor {
enum { OPT_CONFFILE, OPT_LOCKFILE, OPT_LOGGROUP, OPT_DAEMONIZE, OPT_HELP };
CSimpleOpt::SOption g_rgOptions[] = { { OPT_CONFFILE, "--conffile", SO_REQ_SEP },
{ OPT_LOCKFILE, "--lockfile", SO_REQ_SEP },
{ OPT_LOGGROUP, "--loggroup", SO_REQ_SEP },
{ OPT_DAEMONIZE, "--daemonize", SO_NONE },
{ OPT_HELP, "-?", SO_NONE },
{ OPT_HELP, "-h", SO_NONE },
{ OPT_HELP, "--help", SO_NONE },
SO_END_OF_OPTIONS };
volatile int exit_signal = 0;
#ifdef __linux__
void signal_handler(int sig) {
if (sig > exit_signal)
exit_signal = sig;
}
#endif
volatile bool child_exited = false;
#ifdef __linux__
void child_handler(int sig) {
child_exited = true;
}
#endif
extern std::unordered_map<ProcessID, std::unique_ptr<Command>> id_command;
extern std::unordered_map<pid_t, ProcessID> pid_id;
extern std::unordered_map<ProcessID, pid_t> id_pid;
} // namespace fdbmonitor
int main(int argc, char** argv) {
using namespace fdbmonitor;
std::string lockfile = "/var/run/fdbmonitor.pid";
#ifdef __FreeBSD__
std::string _confpath = "/usr/local/etc/foundationdb/foundationdb.conf";
#else
std::string _confpath = "/etc/foundationdb/foundationdb.conf";
#endif
std::vector<const char*> additional_watch_paths;
CSimpleOpt args(argc, argv, g_rgOptions, SO_O_NOERR | SO_O_HYPHEN_TO_UNDERSCORE);
while (args.Next()) {
if (args.LastError() == SO_SUCCESS) {
switch (args.OptionId()) {
case OPT_CONFFILE:
_confpath = args.OptionArg();
break;
case OPT_LOCKFILE:
lockfile = args.OptionArg();
break;
case OPT_LOGGROUP:
if (strchr(args.OptionArg(), '"') != nullptr) {
log_msg(SevError, "Invalid log group '%s', cannot contain '\"'\n", args.OptionArg());
exit(1);
}
logGroup = args.OptionArg();
break;
case OPT_DAEMONIZE:
daemonize = true;
break;
case OPT_HELP:
print_usage(argv[0]);
exit(0);
}
} else {
print_usage(argv[0]);
exit(1);
}
}
log_msg(SevInfo, "Started FoundationDB Process Monitor " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n");
// Modify _confpath to be absolute for further traversals
if (!_confpath.empty() && _confpath[0] != '/') {
char buf[PATH_MAX];
if (!getcwd(buf, PATH_MAX)) {
log_err("getcwd", errno, "Unable to get cwd");
exit(1);
}
_confpath = joinPath(buf, _confpath);
}
// Guaranteed (if non-nullptr) to be an absolute path with no
// symbolic link, /./ or /../ components
char* p = realpath(_confpath.c_str(), nullptr);
if (!p) {
log_msg(SevError, "No configuration file at %s\n", _confpath.c_str());
exit(1);
}
std::string confpath = p;
free(p);
// Will always succeed given an absolute path
std::string confdir = parentDirectory(confpath, false);
std::string conffile = confpath.substr(confdir.size());
#ifdef __linux__
// Setup inotify
int ifd = inotify_init();
if (ifd < 0) {
log_err("inotify_init", errno, "Unable to initialize inotify");
exit(1);
}
int conffile_wd = -1;
int confdir_wd = -1;
std::unordered_map<int, std::unordered_set<std::string>> additional_watch_wds;
bool reload_additional_watches = true;
#endif
/* fds we're blocking on via pselect or kevent */
fdb_fd_set watched_fds;
/* only linux needs this, but... */
int maxfd = 0;
#ifdef __linux__
fd_set rfds;
watched_fds = &rfds;
FD_ZERO(&rfds);
FD_SET(ifd, &rfds);
maxfd = ifd;
int nfds = 0;
fd_set srfds;
#endif
if (daemonize) {
#if defined(__APPLE__) || defined(__FreeBSD__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
if (daemon(0, 0)) {
#if defined(__APPLE__) || defined(__FreeBSD__)
#pragma GCC diagnostic pop
#endif
log_err("daemon", errno, "Unable to daemonize");
exit(1);
}
/* open syslog connection immediately, to be inherited by
forked children */
openlog("fdbmonitor", LOG_PID | LOG_NDELAY, LOG_DAEMON);
signal(SIGTSTP, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
/* new process group, no controlling terminal */
/* unchecked since the only failure indicates we're already a
process group leader */
setsid();
}
/* open and lock our lockfile for mutual exclusion */
std::string lockfileDir = parentDirectory(lockfile, true);
if (lockfileDir.size() == 0) {
log_msg(SevError, "Unable to determine parent directory of lockfile %s\n", lockfile.c_str());
exit(1);
}
if (mkdir(lockfileDir) < 0) {
log_err("mkdir", errno, "Unable to create parent directory for lockfile %s", lockfile.c_str());
exit(1);
}
int lockfile_fd = open(lockfile.c_str(), O_RDWR | O_CREAT, 0640);
if (lockfile_fd < 0) {
log_err("open", errno, "Unable to open fdbmonitor lockfile %s", lockfile.c_str());
exit(1);
}
if (lockf(lockfile_fd, F_LOCK, 0) < 0) {
log_err(
"lockf", errno, "Unable to lock fdbmonitor lockfile %s (is fdbmonitor already running?)", lockfile.c_str());
exit(0);
}
if (chdir("/") < 0) {
log_err("chdir", errno, "Unable to change working directory");
exit(1);
}
/* write our pid to the lockfile for convenience */
char pid_buf[16];
snprintf(pid_buf, sizeof(pid_buf), "%d\n", getpid());
auto ign = write(lockfile_fd, pid_buf, strlen(pid_buf));
(void)ign;
#ifdef __linux__
/* attempt to do clean shutdown and remove lockfile when killed */
signal(SIGHUP, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
#elif defined(__APPLE__) || defined(__FreeBSD__)
int kq = kqueue();
if (kq < 0) {
log_err("kqueue", errno, "Unable to create kqueue");
exit(1);
}
watched_fds = kq;
signal(SIGHUP, SIG_IGN);
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
struct kevent ev;
EV_SET(&ev, SIGHUP, EVFILT_SIGNAL, EV_ADD, 0, 0, nullptr);
kevent(kq, &ev, 1, nullptr, 0, nullptr);
EV_SET(&ev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, nullptr);
kevent(kq, &ev, 1, nullptr, 0, nullptr);
EV_SET(&ev, SIGTERM, EVFILT_SIGNAL, EV_ADD, 0, 0, nullptr);
kevent(kq, &ev, 1, nullptr, 0, nullptr);
EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, nullptr);
kevent(kq, &ev, 1, nullptr, 0, nullptr);
int confd_fd = -1;
int conff_fd = -1;
// Watch the directory holding the configuration file
watch_conf_dir(kq, &confd_fd, confdir);
#endif
#ifdef __linux__
signal(SIGCHLD, child_handler);
#endif
uid_t uid = 0;
gid_t gid = 0;
sigset_t normal_mask, full_mask;
sigfillset(&full_mask);
#ifdef __linux__
/* normal will be restored in our main loop in the call to
pselect, but none blocks all signals while processing events */
sigprocmask(SIG_SETMASK, &full_mask, &normal_mask);
#elif defined(__APPLE__) || defined(__FreeBSD__)
sigprocmask(0, nullptr, &normal_mask);
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
struct stat st_buf;
struct timespec mtimespec;
if (stat(confpath.c_str(), &st_buf) < 0) {
log_err("stat", errno, "Unable to stat configuration file %s", confpath.c_str());
}
memcpy(&mtimespec, &(st_buf.st_mtimespec), sizeof(struct timespec));
#endif
bool reload = true;
double last_rss_check = timer();
while (1) {
if (reload) {
reload = false;
#ifdef __linux__
/* Remove existing watches on conf file and directory */
if (confdir_wd >= 0 && inotify_rm_watch(ifd, confdir_wd) < 0) {
log_msg(SevInfo, "Could not remove inotify conf dir watch, continuing...\n");
}
if (conffile_wd >= 0 && inotify_rm_watch(ifd, conffile_wd) < 0) {
log_msg(SevInfo, "Could not remove inotify conf file watch, continuing...\n");
}
/* Create new watches */
conffile_wd = inotify_add_watch(ifd, confpath.c_str(), IN_CLOSE_WRITE);
if (conffile_wd < 0) {
if (errno != ENOENT) {
log_err(
"inotify_add_watch", errno, "Unable to set watch on configuration file %s", confpath.c_str());
exit(1);
} else {
log_msg(SevInfo, "Conf file has been deleted %s\n", confpath.c_str());
}
} else {
log_msg(SevInfo, "Watching conf file %s\n", confpath.c_str());
}
confdir_wd = inotify_add_watch(ifd, confdir.c_str(), IN_CLOSE_WRITE | IN_MOVED_TO);
if (confdir_wd < 0) {
if (errno != ENOENT) {
log_err("inotify_add_watch",
errno,
"Unable to set watch on configuration file parent directory %s",
confdir.c_str());
exit(1);
} else {
reload_additional_watches = true;
log_msg(SevInfo, "Conf dir has been deleted %s\n", confdir.c_str());
}
} else {
log_msg(SevInfo, "Watching conf dir %s (%d)\n", confdir.c_str(), confdir_wd);
}
/* Reload watches on symlinks and/or the oldest existing ancestor */
if (reload_additional_watches) {
additional_watch_wds = set_watches(_confpath, ifd);
}
load_conf(confpath.c_str(), uid, gid, &normal_mask, &rfds, &maxfd);
reload_additional_watches = false;
#elif defined(__APPLE__) || defined(__FreeBSD__)
load_conf(confpath.c_str(), uid, gid, &normal_mask, watched_fds, &maxfd);
watch_conf_file(kq, &conff_fd, confpath.c_str());
watch_conf_dir(kq, &confd_fd, confdir);
#endif
}
double end_time = std::numeric_limits<double>::max();
double now = timer();
// True if any process has a resident memory limit
bool need_rss_check = false;
for (auto& i : id_command) {
if (i.second->fork_retry_time >= 0) {
end_time = std::min(i.second->fork_retry_time, end_time);
}
// If process has a resident memory limit and is currently running
if (i.second->memory_rss > 0 && id_pid.count(i.first) > 0) {
need_rss_check = true;
}
}
bool timeout_for_rss_check = false;
if (need_rss_check && end_time > last_rss_check + MEMORY_CHECK_INTERVAL) {
end_time = last_rss_check + MEMORY_CHECK_INTERVAL;
timeout_for_rss_check = true;
}
struct timespec tv;
double timeout = -1;
if (end_time < std::numeric_limits<double>::max()) {
timeout = std::max(0.0, end_time - now);
if (timeout > 0) {
tv.tv_sec = timeout;
tv.tv_nsec = 1e9 * (timeout - tv.tv_sec);
}
}
bool is_timeout = false;
#ifdef __linux__
/* Block until something interesting happens (while atomically
unblocking signals) */
srfds = rfds;
nfds = 0;
if (timeout < 0) {
nfds = pselect(maxfd + 1, &srfds, nullptr, nullptr, nullptr, &normal_mask);
} else if (timeout > 0) {
nfds = pselect(maxfd + 1, &srfds, nullptr, nullptr, &tv, &normal_mask);
}
if (nfds == 0) {
is_timeout = true;
if (!timeout_for_rss_check) {
reload = true;
}
}
#elif defined(__APPLE__) || defined(__FreeBSD__)
int nev = 0;
if (timeout < 0) {
nev = kevent(kq, nullptr, 0, &ev, 1, nullptr);
} else if (timeout > 0) {
nev = kevent(kq, nullptr, 0, &ev, 1, &tv);
}
if (nev == 0) {
is_timeout = true;
if (!timeout_for_rss_check) {
reload = true;
}
}
if (nev > 0) {
switch (ev.filter) {
case EVFILT_VNODE:
struct kevent timeout;
// This could be the conf dir or conf file
if (ev.ident == confd_fd) {
/* Changes in the directory holding the conf file; schedule a future timeout to reset watches and
* reload the conf */
EV_SET(&timeout, 1, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 200, nullptr);
kevent(kq, &timeout, 1, nullptr, 0, nullptr);
} else {
/* Direct writes to the conf file; reload! */
reload = true;
}
break;
case EVFILT_TIMER:
reload = true;
break;
case EVFILT_SIGNAL:
switch (ev.ident) {
case SIGHUP:
case SIGINT:
case SIGTERM:
exit_signal = ev.ident;
break;
case SIGCHLD:
child_exited = true;
break;
default:
break;
}
break;
case EVFILT_READ:
Command* cmd = (Command*)ev.udata;
for (int i = 0; i < 2; i++) {
if (ev.ident == cmd->pipes[i][0]) {
read_child_output(cmd, i, watched_fds);
}
}
break;
}
} else {
reload = true;
}
#endif
if (is_timeout && timeout_for_rss_check) {
last_rss_check = timer();
std::vector<ProcessID> oom_ids;
for (auto& i : id_command) {
if (id_pid.count(i.first) == 0) {
// process is not running
continue;
}
uint64_t rss_limit = i.second->memory_rss;
if (rss_limit == 0) {
continue;
}
uint64_t current_rss = getRss(i.first);
if (current_rss > rss_limit) {
log_process_msg(SevWarn,
i.second->ssection.c_str(),
"Process %d being killed for exceeding resident memory limit, current %" PRIu64
" , limit %" PRIu64 "\n",
id_pid[i.first],
current_rss,
i.second->memory_rss);
oom_ids.push_back(i.first);
}
}
// kill process without waiting, and rely on the SIGCHLD handling logic below to restart the process.
for (auto& id : oom_ids) {
kill_process(id, false /*wait*/, false /*cleanup*/);
}
if (oom_ids.size() > 0) {
child_exited = true;
}
}
/* select() could have returned because received an exit signal */
if (exit_signal > 0) {
switch (exit_signal) {
case SIGHUP:
for (auto& i : id_command) {
i.second->current_restart_delay = i.second->initial_restart_delay;
i.second->fork_retry_time = -1;
}
reload = true;
log_msg(SevInfo,
"Received signal %d (%s), resetting timeouts and reloading configuration\n",
exit_signal,
strsignal(exit_signal));
break;
case SIGINT:
case SIGTERM:
log_msg(SevWarn, "Received signal %d (%s), shutting down\n", exit_signal, strsignal(exit_signal));
/* Unblock signals */
signal(SIGCHLD, SIG_IGN);
sigprocmask(SIG_SETMASK, &normal_mask, nullptr);
/* If daemonized, setsid() was called earlier so we can just kill our entire new process group */
if (daemonize) {
kill(0, SIGHUP);
} else {
/* Otherwise kill each process individually but don't wait on them yet */
auto i = id_pid.begin();
auto iEnd = id_pid.end();
while (i != iEnd) {
// Must advance i before calling kill_process() which erases the entry at i
kill_process((i++)->first, false);
}
}
/* Wait for all child processes (says POSIX.1-2001) */
/* POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN, then children that
terminate do not become zombies and a call to wait() will block until all children have terminated,
and then fail with errno set to ECHILD */
wait(nullptr);
unlink(lockfile.c_str());
exit(0);
default:
break;
}
exit_signal = 0;
}
#ifdef __linux__
/* select() could have returned because we have a fd ready to
read (child output or inotify on conf file) */
if (nfds > 0) {
int len, i = 0;
char buf[4096];
for (auto& itr : id_command) {
for (int i = 0; i < 2; i++) {
if (FD_ISSET((itr.second)->pipes[i][0], &srfds)) {
read_child_output(itr.second.get(), i, watched_fds);
}
}
}
if (FD_ISSET(ifd, &srfds)) {
len = read(ifd, buf, 4096);
if (len < 0)
log_err("read", errno, "Error reading inotify message");
while (i < len) {
struct inotify_event* event = (struct inotify_event*)&buf[i];
auto search = additional_watch_wds.find(event->wd);
if (event->wd != conffile_wd) {
if (search != additional_watch_wds.end() && event->len && search->second.count(event->name)) {
log_msg(SevInfo,
"Changes detected on watched symlink `%s': (%d, %#010x)\n",
event->name,
event->wd,
event->mask);
char* redone_confpath = realpath(_confpath.c_str(), nullptr);
if (!redone_confpath) {
log_msg(SevInfo, "Error calling realpath on `%s', continuing...\n", _confpath.c_str());
// exit(1);
i += sizeof(struct inotify_event) + event->len;
continue;
}
confpath = redone_confpath;
// Will always succeed given an absolute path
confdir = parentDirectory(confpath, false);
conffile = confpath.substr(confdir.size());
// Remove all the old watches
for (auto wd : additional_watch_wds) {
if (inotify_rm_watch(ifd, wd.first) < 0) {
// log_err("inotify_rm_watch", errno, "Unable to remove symlink watch %d",
// wd.first); exit(1);
log_msg(SevInfo, "Could not remove inotify watch %d, continuing...\n", wd.first);
}
}
reload = true;
reload_additional_watches = true;
break;
} else if (event->wd == confdir_wd && event->len && conffile == event->name) {
reload = true;
}
}
else if (event->wd == conffile_wd) {
reload = true;
}
i += sizeof(struct inotify_event) + event->len;
}
}
}
#endif
/* select() could have returned because of one or more
SIGCHLDs */
if (child_exited) {
pid_t pid;
int child_status;
while ((pid = waitpid(-1, &child_status, WNOHANG))) {
if (pid < 0) {
if (errno != ECHILD)
log_err("waitpid", errno, "Error while waiting for child process");
break;
}
ProcessID id = pid_id[pid];
Command* cmd = id_command[id].get();
pid_id.erase(pid);
id_pid.erase(id);
if (cmd->deconfigured) {
id_command.erase(id);
} else {
int delay = cmd->get_and_update_current_restart_delay();
if (!cmd->quiet) {
if (WIFEXITED(child_status)) {
Severity priority;
// If the process exited successfully, e.g. because someone restarted the process
// with fdbcli kill we don't want to delay the restart. We only want to delay
// the restart if the process was exited unsuccessfully (exit code different from 0).
if (WEXITSTATUS(child_status) == 0) {
priority = SevWarn;
delay = 0;
} else {
priority = SevError;
}
log_process_msg(priority,
cmd->ssection.c_str(),
"Process %d exited %d, restarting in %d seconds\n",
pid,
WEXITSTATUS(child_status),
delay);
} else if (WIFSIGNALED(child_status))
log_process_msg(SevWarn,
cmd->ssection.c_str(),
"Process %d terminated by signal %d, restarting in %d seconds\n",
pid,
WTERMSIG(child_status),
delay);
else
log_process_msg(SevWarnAlways,
cmd->ssection.c_str(),
"Process %d exited for unknown reason, restarting in %d seconds\n",
pid,
delay);
}
start_process(cmd, id, uid, gid, delay, &normal_mask);
}
}
child_exited = false;
}
}
}