blob: ce26bdbc7bd156360cd68b40b28035433e21c929 (
plain)
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
|
/* ISC license. */
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include <skalibs/strerr2.h>
#include <skalibs/sig.h>
#include <skalibs/selfpipe.h>
#include <skalibs/tai.h>
#include "clientrules.h"
#include "transition.h"
#include "main.h"
#include "signals.h"
static inline void activate_lameduck (void)
{
if (!flags.lameduck)
{
flags.lameduck = 1 ;
tain_add_g(&lameduckdeadline, &lameduckdeadline) ;
}
}
static inline void reload_config (void)
{
clientrules_reload() ;
}
static inline void wait_children (void)
{
for (;;)
{
unsigned int j = 0 ;
int wstat ;
pid_t r = wait_nohang(&wstat) ;
if (r < 0)
if (errno == ECHILD) break ;
else strerr_diefu1sys(111, "wait for children") ;
else if (!r) break ;
for (; j < ntransitions ; j++) if (transitions[j].pid == r) break ;
if (r < ntransitions)
{
transition_t tr = transitions[j] ;
transitions[j] = transitions[--ntransitions] ;
if (!WIFSIGNALED(wstat) && !WEXITSTATUS(wstat))
transition_success(&tr) ;
else
transition_failure(&tr, !!WIFSIGNALED(wstat), WIFSIGNALED(wstat) ? WTERMSIG(wstat) : WEXITSTATUS(wstat)) ;
}
}
}
void signals_handle ()
{
for (;;)
{
switch (selfpipe_read())
{
case -1 : strerr_diefu1sys(111, "selfpipe_read") ;
case 0 : return ;
case SIGTERM :
activate_lameduck() ;
break ;
case SIGHUP :
reload_config() ;
break ;
case SIGCHLD :
wait_children() ;
break ;
}
}
}
int signals_init ()
{
int fd = selfpipe_init() ;
if (fd < 0) strerr_diefu1sys(111, "selfpipe_init") ;
if (sig_ignore(SIGPIPE) < 0) strerr_diefu1sys(111, "ignore SIGPIPE") ;
{
sigset_t set ;
sigemptyset(&set) ;
sigaddset(&set, SIGTERM) ;
sigaddset(&set, SIGHUP) ;
if (selfpipe_trapset(&set) < 0) strerr_diefu1sys(111, "trap signals") ;
}
return fd ;
}
|