2
* Soft: Keepalived is a failover program for the LVS project
3
* <www.linuxvirtualserver.org>. It monitor & manipulate
4
* a loadbalanced server pool using multi-layer checks.
6
* Part: Signals framework.
8
* Version: $Id: signals.c,v 1.1.11 2005/03/01 01:22:13 acassen Exp $
10
* Author: Kevin Lindsay, <kevinl@netnation.com>
11
* Alexandre Cassen, <acassen@linux-vs.org>
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
* See the GNU General Public License for more details.
18
* This program is free software; you can redistribute it and/or
19
* modify it under the terms of the GNU General Public License
20
* as published by the Free Software Foundation; either version
21
* 2 of the License, or (at your option) any later version.
23
* Copyright (C) 2001-2005 Alexandre Cassen, <acassen@linux-vs.org>
32
static int signal_mask;
33
void (*signal_SIGHUP_handler) (int sig);
34
void (*signal_SIGINT_handler) (int sig);
35
void (*signal_SIGTERM_handler) (int sig);
36
void (*signal_SIGCHLD_handler) (int sig);
38
/* Local signal test */
42
return (signal_mask) ? 1 : 0;
47
signal_handler(int sig)
51
signal_mask |= SIGNAL_SIGHUP;
54
signal_mask |= SIGNAL_SIGINT;
57
signal_mask |= SIGNAL_SIGTERM;
60
signal_mask |= SIGNAL_SIGCHLD;
67
signal_set(int signo, void (*func) (int))
71
struct sigaction osig;
73
sig.sa_handler = signal_handler;
74
sigemptyset(&sig.sa_mask);
77
sig.sa_flags |= SA_RESTART;
78
#endif /* SA_RESTART */
80
ret = sigaction(signo, &sig, &osig);
84
signal_SIGHUP_handler = func;
87
signal_SIGINT_handler = func;
90
signal_SIGTERM_handler = func;
93
signal_SIGCHLD_handler = func;
100
return (osig.sa_handler);
105
signal_ignore(int signo)
107
return signal_set(signo, SIG_IGN);
111
* SIGCHLD handler. Reap all zombie child.
112
* WNOHANG prevent against parent process get
113
* stuck waiting child termination.
116
dummy_handler(int sig)
122
signal_noignore_sigchld(void)
127
/* Need to remove the NOCHLD flag */
129
sa.sa_handler = dummy_handler;
133
sigaction(SIGCHLD, &sa, NULL);
135
/* Block SIGCHLD so that we only receive it
136
* when required (ie when its unblocked in the
139
sigaddset(&mask, SIGCHLD);
140
sigprocmask(SIG_BLOCK, &mask, NULL);
143
/* Handlers intialization */
145
signal_handler_init(void)
148
signal_SIGHUP_handler = NULL;
149
signal_SIGINT_handler = NULL;
150
signal_SIGTERM_handler = NULL;
151
signal_SIGCHLD_handler = NULL;
154
/* Handlers callback according to global signal mask */
156
signal_run_callback(void)
158
if (SIGNAL_SIGHUP & signal_mask) {
159
signal_mask &= ~SIGNAL_SIGHUP;
160
if (signal_SIGHUP_handler)
161
signal_SIGHUP_handler(SIGHUP);
164
if (SIGNAL_SIGINT & signal_mask) {
165
signal_mask &= ~SIGNAL_SIGINT;
166
if (signal_SIGINT_handler)
167
signal_SIGINT_handler(SIGINT);
170
if (SIGNAL_SIGTERM & signal_mask) {
171
signal_mask &= ~SIGNAL_SIGTERM;
172
if (signal_SIGTERM_handler)
173
signal_SIGTERM_handler(SIGTERM);
176
if (SIGNAL_SIGCHLD & signal_mask) {
177
signal_mask &= ~SIGNAL_SIGCHLD;
178
if (signal_SIGCHLD_handler)
179
signal_SIGCHLD_handler(SIGCHLD);