/
bfd_daemon.c
338 lines (276 loc) · 7.47 KB
/
bfd_daemon.c
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
/*
* Soft: Keepalived is a failover program for the LVS project
* <www.linuxvirtualserver.org>. It monitor & manipulate
* a loadbalanced server pool using multi-layer checks.
*
* Part: BFD child process handling
*
* Author: Ilya Voronin, <ivoronin@gmail.com>
*
* 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 GNU General Public License for more details.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Copyright (C) 2015-2017 Alexandre Cassen, <acassen@gmail.com>
*/
#include "config.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/prctl.h>
#include <fcntl.h>
#include "bfd.h"
#include "bfd_daemon.h"
#include "bfd_data.h"
#include "bfd_parser.h"
#include "bfd_scheduler.h"
#include "bfd_event.h"
#include "pidfile.h"
#include "logger.h"
#include "signals.h"
#include "list.h"
#include "main.h"
#include "parser.h"
#include "time.h"
#include "global_data.h"
#include "bitops.h"
#include "utils.h"
#include "scheduler.h"
#include "process.h"
#include "utils.h"
/* Global variables */
int bfd_event_pipe[2] = { -1, -1};
/* Local variables */
static char *bfd_syslog_ident;
static int reload_bfd_thread(thread_t *);
/* Daemon stop sequence */
static void
stop_bfd(int status)
{
signal_handler_destroy();
/* Stop daemon */
pidfile_rm(bfd_pidfile);
/* Clean data */
free_global_data(global_data);
bfd_dispatcher_release(bfd_data);
free_bfd_data(bfd_data);
free_bfd_buffer();
thread_destroy_master(master);
#ifdef _DEBUG_
keepalived_free_final("BFD Child process");
#endif
/*
* Reached when terminate signal catched.
* finally return to parent process.
*/
log_message(LOG_INFO, "Stopped");
if (log_file_name)
close_log_file();
closelog();
#ifndef _MEM_CHECK_LOG_
FREE_PTR(bfd_syslog_ident);
#else
if (bfd_syslog_ident)
free(bfd_syslog_ident);
#endif
close_std_fd();
exit(status);
}
/* Daemon init sequence */
void
open_bfd_pipe(void)
{
/* Open BFD control pipe */
if (open_pipe(bfd_event_pipe) == -1) {
log_message(LOG_ERR, "Unable to create BFD event pipe: %m");
stop_keepalived();
}
}
/* Daemon init sequence */
static void
start_bfd(void)
{
srand(time(NULL));
global_data = alloc_global_data();
if (!(bfd_data = alloc_bfd_data())) {
stop_bfd(KEEPALIVED_EXIT_FATAL);
return;
}
alloc_bfd_buffer();
init_data(conf_file, bfd_init_keywords);
/* Set the process priority and non swappable if configured */
// TODO - measure max stack usage
set_process_priorities(global_data->bfd_realtime_priority, global_data->bfd_rlimit_rt,
global_data->bfd_process_priority, global_data->bfd_no_swap ? 4096 : 0);
bfd_complete_init();
if (__test_bit(DUMP_CONF_BIT, &debug))
dump_bfd_data(bfd_data);
thread_add_event(master, bfd_dispatcher_init, bfd_data, 0);
}
/* Reload handler */
static void
sighup_bfd(__attribute__ ((unused)) void *v,
__attribute__ ((unused)) int sig)
{
thread_add_event(master, reload_bfd_thread, NULL, 0);
}
/* Terminate handler */
static void
sigend_bfd(__attribute__ ((unused)) void *v,
__attribute__ ((unused)) int sig)
{
if (master)
thread_add_terminate_event(master);
}
/* BFD Child signal handling */
static void
bfd_signal_init(void)
{
signal_handler_init();
signal_set(SIGHUP, sighup_bfd, NULL);
signal_set(SIGINT, sigend_bfd, NULL);
signal_set(SIGTERM, sigend_bfd, NULL);
signal_ignore(SIGPIPE);
}
/* Reload thread */
static int
reload_bfd_thread(__attribute__((unused)) thread_t * thread)
{
timeval_t timer;
timer = timer_now();
/* set the reloading flag */
SET_RELOAD;
/* Signal handling */
signal_handler_destroy();
/* Destroy master thread */
bfd_dispatcher_release(bfd_data);
thread_destroy_master(master);
master = thread_make_master();
free_global_data(global_data);
free_bfd_buffer();
old_bfd_data = bfd_data;
bfd_data = NULL;
/* Reload the conf */
signal_set(SIGCHLD, thread_child_handler, master);
start_bfd();
free_bfd_data(old_bfd_data);
UNSET_RELOAD;
log_message(LOG_INFO, "Reload finished in %li usec",
timer_tol(timer_sub_now(timer)));
return 0;
}
/* BFD Child respawning thread */
static int
bfd_respawn_thread(thread_t * thread)
{
pid_t pid;
/* Fetch thread args */
pid = THREAD_CHILD_PID(thread);
/* Restart respawning thread */
if (thread->type == THREAD_CHILD_TIMEOUT) {
thread_add_child(master, bfd_respawn_thread, NULL,
pid, RESPAWN_TIMER);
return 0;
}
/* We catch a SIGCHLD, handle it */
if (!__test_bit(DONT_RESPAWN_BIT, &debug)) {
log_message(LOG_ALERT, "BFD child process(%d) died: Respawning",
pid);
start_bfd_child();
} else {
log_message(LOG_ALERT, "BFD child process(%d) died: Exiting",
pid);
raise(SIGTERM);
}
return 0;
}
int
start_bfd_child(void)
{
#ifndef _DEBUG_
pid_t pid;
int ret;
char *syslog_ident;
/* Initialize child process */
if (log_file_name)
flush_log_file();
pid = fork();
if (pid < 0) {
log_message(LOG_INFO, "BFD child process: fork error(%m)");
return -1;
} else if (pid) {
bfd_child = pid;
log_message(LOG_INFO, "Starting BFD child process, pid=%d",
pid);
/* Start respawning thread */
thread_add_child(master, bfd_respawn_thread, NULL,
pid, RESPAWN_TIMER);
return 0;
}
prctl(PR_SET_PDEATHSIG, SIGTERM);
/* Clear any child finder functions set in parent */
set_child_finder_name(NULL);
set_child_finder(NULL, NULL, NULL, NULL, NULL, 0); /* Currently these won't be set */
prog_type = PROG_TYPE_BFD;
/* Close the read end of the event notification pipe */
close(bfd_event_pipe[0]);
if ((instance_name
#if HAVE_DECL_CLONE_NEWNET
|| network_namespace
#endif
) &&
(bfd_syslog_ident = make_syslog_ident(PROG_BFD)))
syslog_ident = bfd_syslog_ident;
else
syslog_ident = PROG_BFD;
/* Opening local BFD syslog channel */
if (!__test_bit(NO_SYSLOG_BIT, &debug))
openlog(syslog_ident, LOG_PID | ((__test_bit(LOG_CONSOLE_BIT, &debug)) ? LOG_CONS : 0)
, (log_facility==LOG_DAEMON) ? LOG_LOCAL2 : log_facility);
if (log_file_name)
open_log_file(log_file_name, "bfd", network_namespace, instance_name);
signal_handler_destroy();
#ifdef _MEM_CHECK_
mem_log_init(PROG_CHECK, "Healthcheck child process");
#endif
free_parent_mallocs_startup(true);
/* Child process part, write pidfile */
if (!pidfile_write(bfd_pidfile, getpid())) {
/* Fatal error */
log_message(LOG_INFO,
"BFD child process: cannot write pidfile");
exit(0);
}
/* Create the new master thread */
signal_handler_destroy();
thread_destroy_master(master);
master = thread_make_master();
/* change to / dir */
ret = chdir("/");
if (ret < 0) {
log_message(LOG_INFO, "BFD child process: error chdir");
}
/* Set mask */
umask(0);
#endif
/* If last process died during a reload, we can get there and we
* don't want to loop again, because we're not reloading anymore.
*/
UNSET_RELOAD;
/* Signal handling initialization */
bfd_signal_init();
/* Start BFD daemon */
start_bfd();
/* Launch the scheduling I/O multiplexer */
launch_scheduler();
/* Finish BFD daemon process */
stop_bfd(EXIT_SUCCESS);
/* unreachable */
exit(EXIT_SUCCESS);
}