-
Notifications
You must be signed in to change notification settings - Fork 2
/
signals.c
249 lines (226 loc) · 4.59 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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include "signals.h"
#include "clients.h"
#include "debug.h"
#include "timer.h"
#include "set.h"
#include "params.h"
sigset_t g_sigset;
void action_normterm();
void action_syncterm();
void action_fastterm();
void action_chld(pid_t pid);
void action_reexec();
void signal_handler(int sig);
void signal_ALRM(int sig);
void signal_CHLD(int sig);
struct str_signal
{
const char* m_str;
int m_signal;
};
struct str_signal sigarr[] = {
{ "SIGHUP", SIGHUP },
{ "SIGINT", SIGINT },
{ "SIGQUIT", SIGQUIT },
{ "SIGILL", SIGILL },
{ "SIGABRT", SIGABRT },
{ "SIGFPE", SIGFPE },
{ "SIGKILL", SIGKILL },
{ "SIGSEGV", SIGSEGV },
{ "SIGPIPE", SIGPIPE },
{ "SIGALRM", SIGALRM },
{ "SIGTERM", SIGTERM },
{ "SIGUSR1", SIGUSR1 },
{ "SIGUSR2", SIGUSR2 },
{ "SIGCHLD", SIGCHLD },
{ "SIGCONT", SIGCONT },
{ "SIGSTOP", SIGSTOP },
{ "SIGTSTP", SIGTSTP },
{ "SIGTTIN", SIGTTIN },
{ "SIGTTOU", SIGTTOU }
};
int signal_str(const char* str)
{
int index = 0;
for (index = 0; index < sizeof(sigarr)/sizeof(struct str_signal); ++index)
{
if ( !strcmp( sigarr[index].m_str, str ) )
return sigarr[index].m_signal;
}
return atoi( str );
}
void signals_init()
{
sigemptyset( &g_sigset );
sigaddset( &g_sigset, SIGINT );
sigaddset( &g_sigset, SIGUSR1 );
sigaddset( &g_sigset, SIGUSR2 );
signals_cansyncterm();
signal( SIGHUP, signal_handler );
signal( SIGINT, signal_handler );
signal( SIGQUIT, signal_handler );
signal( SIGILL, signal_handler );
signal( SIGFPE, signal_handler );
signal( SIGSEGV, signal_handler );
signal( SIGTERM, signal_handler );
signal( SIGUSR1, signal_handler );
signal( SIGUSR2, signal_handler );
#ifdef SIGPWR
signal( SIGPWR, signal_handler );
#endif
signal( SIGALRM, signal_ALRM );
signal( SIGCHLD, signal_CHLD );
//signal( SIGABRT, );
//signal( SIGPIPE, );
//signal( SIGCONT, );
//signal( SIGTSTP, );
//signal( SIGTTIN, );
//signal( SIGTTOU, );
}
void signal_handler(int sig)
{
switch ( sig )
{
case SIGINT:
action_normterm();
break;
case SIGHUP:
case SIGTERM:
case SIGQUIT:
action_syncterm();
break;
case SIGILL:
case SIGABRT:
case SIGFPE:
case SIGSEGV:
case SIGPIPE:
#ifdef SIGPWR
case SIGPWR:
action_fastterm();
break;
#endif
case SIGUSR1:
action_reexec();
break;
case SIGUSR2:
debug_print();
break;
default:
fprintf( stderr, "unknowh signal %08X\n", sig );
break;
};
}
void signals_block()
{
sigprocmask( SIG_BLOCK, &g_sigset, NULL );
}
void signals_unblock()
{
sigprocmask( SIG_UNBLOCK, &g_sigset, NULL );
}
void signals_cansyncterm()
{
sigaddset( &g_sigset, SIGHUP );
sigaddset( &g_sigset, SIGTERM );
sigaddset( &g_sigset, SIGQUIT );
}
void signals_cantsyncterm()
{
sigdelset( &g_sigset, SIGHUP );
sigdelset( &g_sigset, SIGTERM );
sigdelset( &g_sigset, SIGQUIT );
}
void signal_ALRM(int sig)
{
timer_alarm();
}
void signal_CHLD(int sig)
{
int status = 0;
pid_t pid = wait( &status );
if ( WNOHANG == status )
{
fprintf( stderr, "wait(): WNOHANG! WTF?\n" );
}
else if ( WIFEXITED(status) )
{
action_chld( pid );
}
//ignored:
//WUNTRACED(status)
//WCONTINUED(status)
//WEXITSTATUS(status)
//WIFSIGNALED(status)
//WTERMSIG(status)
//WCOREDUMP(status)
//WIFSTOPPED(status)
//WSTOPSIG(status)
//WIFCONTINUED(status)
}
void action_normterm()
{
int index;
for (index = 0; index < g_setcount; ++index)
{
close( g_set[index].fd );
}
exit( EXIT_SUCCESS );
}
void action_syncterm()
{
int index;
for (index = 0; index < g_setcount; ++index)
{
close( g_set[index].fd );
}
exit( EXIT_SUCCESS );
}
void action_fastterm()
{
int index;
for (index = 0; index < g_setcount; ++index)
{
close( g_set[index].fd );
}
exit( EXIT_FAILURE );
}
void action_chld(pid_t pid)
{
int end = 0;
int index = 0;
struct TClient* client = g_clients.m_client;
while ( index < g_clients.m_count )
{
if ( client->m_ipid == pid )
{
client->m_ipid = -1;
//TODO: shutdown( client->m_sock, SHUT_WR );
client_tdisconnect( client );
end = 1;
}
if ( client->m_opid == pid )
{
client->m_opid = -1;
index = client2set( index );
g_set[index].events |= POLLIN; //socket can be closed
//TODO: shutdown( client->m_sock, SHUT_RD );
end = 1;
}
if ( end )
{
return;
}
++client;
}
}
void action_reexec()
{
//TODO: reexec
fprintf( stderr, "reexec() %d\n", p_sync );
}