-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsignals.c
279 lines (238 loc) · 7.23 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
/* signals.c
* Copyright (C) 2005-2007 Tillmann Werner <[email protected]>
*
* This file is free software; as a special exception the author gives
* unlimited permission to copy and/or distribute it, with or without
* modifications, as long as this notice is preserved.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <errno.h>
#include <execinfo.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "logging.h"
#include "honeytrap.h"
#include "ctrl.h"
#include "readconf.h"
#include "plugin.h"
#include "plughook.h"
#include "signals.h"
#define MASTER_PROCESS (pid = getpid()) == master_pid
void get_signal(int sig) {
if (signal(sig, SIG_IGN) == SIG_IGN) return;
if (write(sigpipe[1], (char *) &sig, sizeof(int)) == -1) {
if (write(logfile_fd, "Error - Unable to write signal to pipe.\n", 40)) { };
if ((STDOUT_FILENO != logfile_fd) && (daemonize != 1))
if (write(STDOUT_FILENO, "Error - Unable to write signal to pipe.\n", 40)) { };
exit(EXIT_FAILURE);
}
return;
}
void handle_termsig(int sig) {
int nptrs;
pid_t pid;
struct sigaction s_action;
void *buffer[BUFSIZ];
sigemptyset(&s_action.sa_mask);
s_action.sa_flags = 0;
s_action.sa_handler = SIG_IGN;
switch (sig) {
case SIGSEGV:
case SIGILL:
#ifdef HAVE_SIGBUS
case SIGBUS:
#endif
logmsg(LOG_ERR, 1, "Error - Terminating signal (%d) received by process %u.\n", sig, getpid());
logmsg(LOG_ERR, 1, "-- Begin process backtrace --\n");
nptrs = backtrace(buffer, BUFSIZ);
backtrace_symbols_fd(buffer, nptrs, logfile_fd);
logmsg(LOG_ERR, 1, "-- End of process backtrace --\n");
break;
default:
logmsg(LOG_DEBUG, 1, "Terminating signal (%d) received.\n", sig);
}
if (MASTER_PROCESS) {
if (kill(0-getpgrp(), SIGINT) == 0) {
logmsg(LOG_DEBUG, 1, "Signal was successfully forwarded to process group.\n");
/* wait for children */
while ((pid = waitpid(-master_pid, 0, WNOHANG)) > 0) logmsg(LOG_DEBUG, 1, "Process %d terminated.\n", pid);
if (pid == -1 && errno != ECHILD) {
logmsg(LOG_ERR, 1, "Error - Unable to wait for process status changes: %m.\n");
exit(EXIT_FAILURE);
}
} else {
logmsg(LOG_ERR, 1, "Error - Unable to forward signal to process group: %m.\n");
clean_exit(EXIT_FAILURE);
}
clean_exit(EXIT_SUCCESS);
} else exit(EXIT_SUCCESS);
/* suicide... */
if (kill(master_pid, sig) < 0) logmsg(LOG_ERR, 1, "Error - Unable to terminate master process: %m.\n");
return;
}
void handle_sighup(int sig) {
pid_t pid;
struct sigaction s_action;
if (MASTER_PROCESS) {
logmsg(LOG_DEBUG, 1, "SIGHUP received. Reconfiguring honeytrap.\n");
/* unloading plugins */
unload_plugins();
/* configure honeytrap */
logmsg(LOG_NOTICE, 1, "---- Reloading configuration ----\n");
configure(arg_c, arg_v);
logmsg(LOG_NOTICE, 1, "---- honeytrap reconfigured ----\n");
}
/* reinstall original signal handler */
memset(&s_action, 0, sizeof(struct sigaction));
s_action.sa_handler = get_signal;
#ifdef SA_RESTART
s_action.sa_flags |= SA_RESTART;
#endif
if (sigaction(SIGHUP, &s_action, NULL) == -1) {
logmsg(LOG_ERR, 1, "Error - Unable to reinstall signal handler for SIGHUP.\n");
exit(EXIT_FAILURE);
} else logmsg(LOG_DEBUG, 1, "Signal handler for SIGHUP reinstalled.\n");
return;
}
void handle_sigchld(int sig) {
pid_t pid;
int status;
struct sigaction s_action;
logmsg(LOG_DEBUG, 1, "SIGCHILD received.\n");
status = 0;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
logmsg(LOG_DEBUG, 1, "Process %d terminated.\n", pid);
if WIFSIGNALED(status)
logmsg(LOG_WARN, 1, "Warning - Process %d was terminated by signal %d.\n", pid, WTERMSIG(status));
else if (WIFEXITED(status) && (WEXITSTATUS(status) == EXIT_FAILURE))
logmsg(LOG_WARN, 1, "Warning - Process %d exited on failure.\n", pid);
status = 0;
}
if (pid == -1 && errno != ECHILD) {
logmsg(LOG_ERR, 1, "Error - Unable to wait for process status changes: %m.\n");
exit(EXIT_FAILURE);
}
/* reinstall original signal handler */
memset(&s_action, 0, sizeof(struct sigaction));
s_action.sa_handler = get_signal;
#ifdef SA_RESTART
s_action.sa_flags |= SA_RESTART;
#endif
if (sigaction(SIGCHLD, &s_action, NULL) == -1) {
logmsg(LOG_ERR, 1, "Error - Unable to reinstall signal handler for SIGCHLD.\n");
exit(EXIT_FAILURE);
} else logmsg(LOG_DEBUG, 1, "Signal handler for SIGCHLD reinstalled.\n");
return;
}
void install_signal_handlers(void) {
u_char i;
struct sigaction s_action;
static int sigs[] = {
SIGCHLD,
#ifdef HAVE_SIGBUS
SIGBUS,
#endif
SIGHUP,
SIGILL,
SIGINT,
SIGQUIT,
SIGSEGV,
SIGTERM
};
create_sigpipe();
/* install signal handlers */
memset(&s_action, 0, sizeof(struct sigaction));
s_action.sa_handler = get_signal;
#ifdef SA_RESTART
s_action.sa_flags |= SA_RESTART;
#endif
for (i = 0; i < sizeof(sigs)/sizeof(sigs[0]); i++) {
if (sigaction(sigs[i], &s_action, NULL) == -1) {
fprintf(stdout, " Error - Handler for signal %d was not installed for %u: %m.\n", sigs[i], getpid());
exit(EXIT_FAILURE);
} else DEBUG_FPRINTF(stdout, " Handler for signal %d installed.\n", sigs[i]);
}
return;
}
void create_sigpipe(void) {
/* create a pipe for process-internal signal delivery
* this function must be called in every process, e.g. after a fork()
* as pipes are inherited by childs which would break signal delivery */
/* make sure there are no open pipe endpoints */
close(sigpipe[0]);
close(sigpipe[1]);
/* (re)open pipe */
if (pipe(sigpipe) == -1) {
fprintf(stderr, " Error - Unable to create signal pipe: %m.\n");
exit(EXIT_FAILURE);
}
return;
}
/* check_sigpipe() tries to read a signal from the process's signal pipe
* if a signal is available, the signal handling routine is called.
* select() on sigpipe[0] and call this function for safe signal handling */
int check_sigpipe(void) {
int sig, rv;
switch(rv = read(sigpipe[0], &sig, sizeof(int))) {
case sizeof(int):
/* caught a signal */
logmsg(LOG_DEBUG, 1, "Process %d received signal %d on pipe.\n", getpid(), sig);
switch (sig) {
case SIGCHLD:
handle_sigchld(sig);
break;
case SIGHUP:
handle_sighup(sig);
break;
#ifdef HAVE_SIGBUS
case SIGBUS:
#endif
case SIGILL:
case SIGINT:
case SIGQUIT:
case SIGSEGV:
case SIGTERM:
handle_termsig(sig);
break;
default:
break;
}
break;
case 0:
logmsg(LOG_WARN, 1, "Warning - Signal pipe ready to read but not enough data available.\n");
return(0);
case -1:
logmsg(LOG_ERR, 1, "Error - Unable to read signal from pipe: %m.\n");
return(-1);
}
return(0);
}
int sleep_sigaware(struct timeval *tv) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(sigpipe[0], &rfds);
for (;;) {
switch (select(sigpipe[0]+1, &rfds, NULL, NULL, tv)) {
case -1:
if (errno == EINTR) {
if (check_sigpipe() == -1) exit(EXIT_FAILURE);
break;
}
logmsg(LOG_DEBUG, 1, "Error in signal-aware sleep - select() failed: %s.\n", strerror(errno));
return -1;
case 0:
return 0;
default:
if (FD_ISSET(sigpipe[0], &rfds) && (check_sigpipe() == -1)) exit(EXIT_FAILURE);
}
}
}