forked from twitter/twemproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnc_signal.c
131 lines (108 loc) · 3.01 KB
/
nc_signal.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
/*
* twemproxy - A fast and lightweight proxy for memcached protocol.
* Copyright (C) 2011 Twitter, Inc.
*
* 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 <stdlib.h>
#include <signal.h>
#include <nc_core.h>
#include <nc_signal.h>
static struct signal signals[] = {
{ SIGUSR1, "SIGUSR1", 0, signal_handler },
{ SIGUSR2, "SIGUSR2", 0, signal_handler },
{ SIGTTIN, "SIGTTIN", 0, signal_handler },
{ SIGTTOU, "SIGTTOU", 0, signal_handler },
{ SIGHUP, "SIGHUP", 0, signal_handler },
{ SIGINT, "SIGINT", 0, signal_handler },
{ SIGSEGV, "SIGSEGV", (int)SA_RESETHAND, signal_handler },
{ SIGPIPE, "SIGPIPE", 0, SIG_IGN },
{ 0, NULL, 0, NULL }
};
rstatus_t
signal_init(void)
{
struct signal *sig;
for (sig = signals; sig->signo != 0; sig++) {
rstatus_t status;
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig->handler;
sa.sa_flags = sig->flags;
sigemptyset(&sa.sa_mask);
status = sigaction(sig->signo, &sa, NULL);
if (status < 0) {
log_error("sigaction(%s) failed: %s", sig->signame,
strerror(errno));
return NC_ERROR;
}
}
return NC_OK;
}
void
signal_deinit(void)
{
}
void
signal_handler(int signo)
{
struct signal *sig;
void (*action)(void);
char *actionstr;
bool done;
for (sig = signals; sig->signo != 0; sig++) {
if (sig->signo == signo) {
break;
}
}
ASSERT(sig->signo != 0);
actionstr = "";
action = NULL;
done = false;
switch (signo) {
case SIGUSR1:
break;
case SIGUSR2:
break;
case SIGTTIN:
actionstr = ", up logging level";
action = log_level_up;
break;
case SIGTTOU:
actionstr = ", down logging level";
action = log_level_down;
break;
case SIGHUP:
actionstr = ", reopening log file";
action = log_reopen;
break;
case SIGINT:
done = true;
actionstr = ", exiting";
break;
case SIGSEGV:
log_stacktrace();
actionstr = ", core dumping";
raise(SIGSEGV);
break;
default:
NOT_REACHED();
}
log_safe("signal %d (%s) received%s", signo, sig->signame, actionstr);
if (action != NULL) {
action();
}
if (done) {
exit(1);
}
}