forked from acassen/keepalived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignals.c
448 lines (383 loc) · 9.91 KB
/
signals.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
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
/*
* 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: Signals framework.
*
* Author: Kevin Lindsay, <[email protected]>
* Alexandre Cassen, <[email protected]>
*
* 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) 2001-2017 Alexandre Cassen, <[email protected]>
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <errno.h>
#include <sys/signalfd.h>
#ifdef _INCLUDE_UNUSED_CODE_
#include <sys/epoll.h>
#endif
#include <inttypes.h>
#include "signals.h"
#include "utils.h"
#include "logger.h"
#include "scheduler.h"
#include "assert_debug.h"
#ifdef _WITH_JSON_
#include "../keepalived/include/vrrp_json.h"
#endif
/* We need to include the realtime signals, but
* unfortunately SIGRTMIN/SIGRTMAX are not constants.
* I'm not clear if _NSIG is always defined, so play safe.
* Although we are not meant to use __SIGRTMAX, we are
* using it here as an upper bound, which is rather different. */
#ifdef _NSIG
#define SIG_MAX _NSIG
#elif defined __SIGRTMAX
#define SIG_MAX __SIGRTMAX
#else
#define SIG_MAX 64
#endif
/* Local Vars */
#ifndef USE_SIGNAL_THREADS
static void (*signal_handler_func[SIG_MAX]) (void *, int sig);
static void *signal_v[SIG_MAX];
#endif
static int signal_fd = -1;
static sigset_t signal_fd_set;
/* Remember signal disposition for not default disposition */
static sigset_t dfl_sig;
/* Signal handlers set in parent */
static sigset_t parent_sig;
/* Signal handling thread */
static thread_ref_t signal_thread;
int __attribute__((pure))
get_signum(const char *sigfunc)
{
if (!strcmp(sigfunc, "STOP"))
return SIGTERM;
else if (!strcmp(sigfunc, "RELOAD"))
return SIGHUP;
else if (!strcmp(sigfunc, "DATA"))
return SIGUSR1;
else if (!strcmp(sigfunc, "STATS"))
return SIGUSR2;
else if (!strcmp(sigfunc, "STATS_CLEAR"))
return SIGSTATS_CLEAR;
#ifdef _WITH_JSON_
else if (!strcmp(sigfunc, "JSON"))
return SIGJSON;
#endif
#ifdef THREAD_DUMP
else if (!strcmp(sigfunc, "TDUMP"))
return SIGTDUMP;
#endif
/* Not found */
return -1;
}
static void
log_sigxcpu(__attribute__((unused)) void * ptr, __attribute__((unused)) int signum)
{
log_message(LOG_INFO, "%s process has used too much CPU time, %s_rlimit_rttime may need to be increased",
#ifdef _ONE_PROCESS_DEBUG_
"Main debug",
#else
#ifdef _WITH_VRRP_
prog_type == PROG_TYPE_VRRP ? "VRRP" :
#endif
#ifdef _WITH_LVS_
prog_type == PROG_TYPE_CHECKER ? "Checker" :
#endif
#ifdef _WITH_BFD_
prog_type == PROG_TYPE_BFD ? "BFD" :
#endif
"Unknown",
#endif
#ifdef _ONE_PROCESS_DEBUG_
"UNDEFINED"
#else
#ifdef _WITH_VRRP_
prog_type == PROG_TYPE_VRRP ? "vrrp" :
#endif
#ifdef _WITH_LVS_
prog_type == PROG_TYPE_CHECKER ? "checker" :
#endif
#ifdef _WITH_BFD_
prog_type == PROG_TYPE_BFD ? "bfd" :
#endif
"Unknown"
#endif
);
}
#ifdef _INCLUDE_UNUSED_CODE_
/* Local signal test */
bool
signal_pending(void)
{
int rc;
int efd;
struct epoll_event ev = { .events = EPOLLIN };
efd = epoll_create(1);
epoll_ctl(efd, EPOLL_CTL_ADD, signal_fd, &ev);
rc = epoll_wait(efd, &ev, 1, 0);
close(efd);
return rc > 0;
}
#endif
/* Signal wrapper */
void
signal_set(int signo, void (*func) (void *, int), void *v)
{
int ret;
sigset_t sset;
struct sigaction sig;
#ifdef _SIGNAL_DEBUG_
static int max_signo = SIG_MAX;
static int min_signo = 1;
if ((signo < min_signo) || (signo > max_signo)) {
log_message(LOG_ERR, "BUG - signal %d out of range (1..%d)", signo, SIG_MAX);
if (signo > max_signo)
max_signo = signo;
else
min_signo = signo;
return;
}
#endif
if (func == (void *)SIG_DFL)
sigaddset(&dfl_sig, signo);
else
sigdelset(&dfl_sig, signo);
if (func == (void *)SIG_IGN || func == (void *)SIG_DFL) {
/* We are no longer handling the signal, so
* clear our handlers */
func = NULL;
v = NULL;
}
sigemptyset(&sset);
sigaddset(&sset, signo);
sigemptyset(&sig.sa_mask);
sig.sa_flags = 0;
if (!func) {
sigdelset(&signal_fd_set, signo);
sig.sa_handler = SIG_IGN;
}
else {
sigaddset(&signal_fd_set, signo);
sigmask_func(SIG_BLOCK, &sset, NULL);
sig.sa_handler = SIG_DFL;
}
/* Don't open signal_fd if clearing the handler */
if (func || signal_fd != -1) {
ret = signalfd(signal_fd, &signal_fd_set, 0);
if (ret == -1)
log_message(LOG_INFO, "BUG - signal_fd update failed - %d (%s), please report", errno, strerror(errno));
}
if (sigaction(signo, &sig, NULL))
log_message(LOG_INFO, "sigaction failed for signalfd");
if (!func)
sigmask_func(SIG_UNBLOCK, &sset, NULL);
signal_handler_func[signo-1] = func;
signal_v[signo-1] = v;
}
/* Signal Ignore */
void
signal_ignore(int signo)
{
signal_set(signo, (void *)SIG_IGN, NULL);
sigdelset(&parent_sig, signo);
}
/* Handlers callback */
static void
signal_run_callback(thread_ref_t thread)
{
uint32_t sig;
struct signalfd_siginfo siginfo;
while (read(signal_fd, &siginfo, sizeof(struct signalfd_siginfo)) == sizeof(struct signalfd_siginfo)) {
sig = siginfo.ssi_signo;
#ifdef _EPOLL_DEBUG_
if (do_epoll_debug) {
if (sig >= 1 && sig < sizeof(signal_handler_func) / sizeof(signal_handler_func[0]))
log_message(LOG_INFO, "Signal %" PRIu32 ", func %s()", sig, get_signal_function_name(signal_handler_func[sig-1]));
else
log_message(LOG_INFO, "Signal %" PRIu32 ", unknown function", sig);
}
#endif
#ifdef USE_SIGNAL_THREADS
/* This is instead of signal_handler_func[] array if signals are
* handled by threads. The thread handling function would have to
* do a thread_add_signal() to reinstate itself. */
list_for_each_entry_safe(t, t_tmp, &m->signal, next) {
if (t->u.val == sig) {
list_del_init(&t->next);
list_add_tail(&t->next, &m->ready);
t->type = THREAD_READY;
}
}
#else
if (sig >= 1 && sig <= SIG_MAX && signal_handler_func[sig-1])
signal_handler_func[sig-1](signal_v[sig-1], sig);
#endif
}
signal_thread = thread_add_read(master, signal_run_callback, NULL, thread->u.f.fd, TIMER_NEVER, 0);
}
static void
clear_signal_handler_addresses(void)
{
int i;
for (i = 0; i < SIG_MAX; i++)
signal_handler_func[i] = NULL;
}
/* Handlers intialization */
void
add_signal_read_thread(thread_master_t *thread_master)
{
signal_thread = thread_add_read(thread_master, signal_run_callback, NULL, thread_master->signal_fd, TIMER_NEVER, 0);
}
void
cancel_signal_read_thread(void)
{
if (signal_thread) {
thread_cancel(signal_thread);
signal_thread = NULL;
}
}
static int
open_signal_fd(void)
{
sigemptyset(&signal_fd_set);
signal_fd = signalfd(signal_fd, &signal_fd_set, SFD_NONBLOCK | SFD_CLOEXEC);
if (signal_fd == -1)
log_message(LOG_INFO, "BUG - signal_fd init failed - %d (%s), please report", errno, strerror(errno));
return signal_fd;
}
static void
signal_handler_parent_init(void)
{
sigset_t sset;
int sig;
struct sigaction act;
/* Ignore all signals except essential ones */
sigemptyset(&sset);
sigaddset(&sset, SIGILL);
sigaddset(&sset, SIGFPE);
sigaddset(&sset, SIGSEGV);
sigaddset(&sset, SIGBUS);
sigaddset(&sset, SIGKILL);
sigaddset(&sset, SIGSTOP);
dfl_sig = sset;
act.sa_handler = SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
for (sig = 1; sig <= SIGRTMAX; sig++) {
if (!sigismember(&sset, sig))
sigaction(sig, &act, NULL);
}
sigemptyset(&sset);
sigmask_func(SIG_SETMASK, &sset, NULL);
}
#ifndef _ONE_PROCESS_DEBUG_
static void
signal_handler_child_init(void)
{
struct sigaction act;
int sig;
act.sa_handler = SIG_IGN;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
for (sig = 1; sig <= SIGRTMAX; sig++) {
if (sigismember(&parent_sig, sig))
sigaction(sig, &act, NULL);
}
}
#endif
int
signal_handler_init(void)
{
int fd;
#ifdef _ONE_PROCESS_DEBUG_
signal_handler_parent_init();
#else
if (prog_type == PROG_TYPE_PARENT)
signal_handler_parent_init();
else
signal_handler_child_init();
#endif
sigemptyset(&parent_sig);
fd = open_signal_fd();
clear_signal_handler_addresses();
return fd;
}
static void
signal_handlers_clear(void *state)
{
/* Ensure no more pending signals */
signal_set(SIGHUP, state, NULL);
signal_set(SIGINT, state, NULL);
signal_set(SIGTERM, state, NULL);
signal_set(SIGCHLD, state, NULL);
signal_set(SIGUSR1, state, NULL);
signal_set(SIGUSR2, state, NULL);
signal_set(SIGXCPU, state, NULL);
#ifdef _WITH_JSON_
signal_set(SIGJSON, state, NULL);
#endif
}
void
signal_handler_destroy(void)
{
if (signal_thread) {
thread_cancel(signal_thread);
signal_thread = NULL;
}
if (signal_fd != -1) {
close(signal_fd);
signal_fd = -1;
}
sigemptyset(&signal_fd_set);
signal_handlers_clear(SIG_IGN);
}
/* Called prior to exec'ing a script. The script can reasonably
* expect to have the standard signal disposition */
void
signal_handler_script(void)
{
struct sigaction dfl;
int sig;
sigset_t sset;
dfl.sa_handler = SIG_DFL;
dfl.sa_flags = 0;
sigemptyset(&dfl.sa_mask);
for (sig = 1; sig <= SIGRTMAX; sig++) {
if (!sigismember(&dfl_sig, sig))
sigaction(sig, &dfl, NULL);
}
sigemptyset(&sset);
sigmask_func(SIG_SETMASK, &sset, NULL);
}
void
set_sigxcpu_handler(void)
{
signal_set(SIGXCPU, log_sigxcpu, NULL);
}
#ifdef THREAD_DUMP
void
register_signal_thread_addresses(void)
{
register_thread_address("signal_run_callback", signal_run_callback);
register_signal_handler_address("log_sigxcpu", log_sigxcpu);
}
#endif