... | ... |
@@ -25,6 +25,8 @@ |
25 | 25 |
#include <stdio.h> |
26 | 26 |
#include <string.h> |
27 | 27 |
#include <stdlib.h> |
28 |
+#include <pthread.h> |
|
29 |
+#include <signal.h> |
|
28 | 30 |
#ifdef HAVE_UNISTD_H |
29 | 31 |
#include <unistd.h> |
30 | 32 |
#endif |
... | ... |
@@ -55,7 +57,42 @@ |
55 | 55 |
pthread_t ddd_pid = 0; |
56 | 56 |
pthread_t scque_pid = 0; |
57 | 57 |
|
58 |
+static void onas_handle_signals(); |
|
58 | 59 |
static int startup_checks(struct onas_context *ctx); |
60 |
+static struct onas_context *g_ctx = NULL; |
|
61 |
+ |
|
62 |
+static void onas_clamonacc_exit(int sig) |
|
63 |
+{ |
|
64 |
+ logg("*Clamonacc: onas_clamonacc_exit(), signal %d\n", sig); |
|
65 |
+ if (sig == 11) { |
|
66 |
+ logg("!Clamonacc: clamonacc has experienced a fatal error, if you continue to see this error, please run clamonacc with --debug and report the issue and crash report to the developpers\n"); |
|
67 |
+ } |
|
68 |
+ |
|
69 |
+ if (g_ctx) { |
|
70 |
+ if(g_ctx->fan_fd) { |
|
71 |
+ close(g_ctx->fan_fd); |
|
72 |
+ } |
|
73 |
+ g_ctx->fan_fd = 0; |
|
74 |
+ } |
|
75 |
+ |
|
76 |
+ logg("*Clamonacc: attempting to stop event consumer thread ...\n"); |
|
77 |
+ if (scque_pid > 0) { |
|
78 |
+ pthread_cancel(scque_pid); |
|
79 |
+ pthread_join(scque_pid, NULL); |
|
80 |
+ } |
|
81 |
+ scque_pid = 0; |
|
82 |
+ |
|
83 |
+ logg("*Clamonacc: attempting to stop ddd thread ... \n"); |
|
84 |
+ if (ddd_pid > 0) { |
|
85 |
+ pthread_cancel(ddd_pid); |
|
86 |
+ pthread_join(ddd_pid, NULL); |
|
87 |
+ } |
|
88 |
+ ddd_pid = 0; |
|
89 |
+ |
|
90 |
+ logg("Clamonacc: stopped\n"); |
|
91 |
+ onas_cleanup(g_ctx); |
|
92 |
+ pthread_exit(NULL); |
|
93 |
+} |
|
59 | 94 |
|
60 | 95 |
int main(int argc, char **argv) |
61 | 96 |
{ |
... | ... |
@@ -174,6 +211,10 @@ int main(int argc, char **argv) |
174 | 174 |
goto clean_up; |
175 | 175 |
#endif |
176 | 176 |
|
177 |
+ /* Setup signal handling */ |
|
178 |
+ g_ctx = ctx; |
|
179 |
+ onas_handle_signals(); |
|
180 |
+ |
|
177 | 181 |
logg("*Clamonacc: beginning event loops\n"); |
178 | 182 |
/* Kick off event loop(s) */ |
179 | 183 |
ret = onas_start_eloop(&ctx); |
... | ... |
@@ -184,6 +225,34 @@ clean_up: |
184 | 184 |
exit(ret); |
185 | 185 |
} |
186 | 186 |
|
187 |
+static void onas_handle_signals() { |
|
188 |
+ sigset_t sigset; |
|
189 |
+ struct sigaction act; |
|
190 |
+ |
|
191 |
+ /* ignore all signals except SIGUSR1 */ |
|
192 |
+ sigfillset(&sigset); |
|
193 |
+ sigdelset(&sigset, SIGUSR1); |
|
194 |
+ sigdelset(&sigset, SIGUSR2); |
|
195 |
+ /* The behavior of a process is undefined after it ignores a |
|
196 |
+ * SIGFPE, SIGILL, SIGSEGV, or SIGBUS signal */ |
|
197 |
+ sigdelset(&sigset, SIGFPE); |
|
198 |
+ sigdelset(&sigset, SIGILL); |
|
199 |
+ sigdelset(&sigset, SIGSEGV); |
|
200 |
+ sigdelset(&sigset, SIGINT); |
|
201 |
+ sigdelset(&sigset, SIGTERM); |
|
202 |
+#ifdef SIGBUS |
|
203 |
+ sigdelset(&sigset, SIGBUS); |
|
204 |
+#endif |
|
205 |
+ pthread_sigmask(SIG_SETMASK, &sigset, NULL); |
|
206 |
+ memset(&act, 0, sizeof(struct sigaction)); |
|
207 |
+ act.sa_handler = onas_clamonacc_exit; |
|
208 |
+ sigfillset(&(act.sa_mask)); |
|
209 |
+ sigaction(SIGUSR2, &act, NULL); |
|
210 |
+ sigaction(SIGTERM, &act, NULL); |
|
211 |
+ sigaction(SIGSEGV, &act, NULL); |
|
212 |
+ sigaction(SIGINT, &act, NULL); |
|
213 |
+} |
|
214 |
+ |
|
187 | 215 |
struct onas_context *onas_init_context(void) { |
188 | 216 |
struct onas_context *ctx = (struct onas_context*) cli_malloc(sizeof(struct onas_context)); |
189 | 217 |
if (NULL == ctx) { |
... | ... |
@@ -29,7 +29,6 @@ |
29 | 29 |
#include <sys/types.h> |
30 | 30 |
#include <sys/stat.h> |
31 | 31 |
#include <fcntl.h> |
32 |
-#include <signal.h> |
|
33 | 32 |
#include <pthread.h> |
34 | 33 |
#include <string.h> |
35 | 34 |
#include <errno.h> |
... | ... |
@@ -59,36 +58,6 @@ extern pthread_t ddd_pid; |
59 | 59 |
extern pthread_t scque_pid; |
60 | 60 |
static int onas_fan_fd; |
61 | 61 |
|
62 |
-static void onas_fan_exit(int sig) |
|
63 |
-{ |
|
64 |
- logg("*ClamFanotif: onas_fan_exit(), signal %d\n", sig); |
|
65 |
- if (sig == 11) { |
|
66 |
- logg("!Clamonacc: clamonacc has experienced a fatal error, if you continue to see this error, please run clamonacc with --debug and report the issue and crash report to the developpers\n"); |
|
67 |
- } |
|
68 |
- |
|
69 |
- if(onas_fan_fd) { |
|
70 |
- close(onas_fan_fd); |
|
71 |
- } |
|
72 |
- onas_fan_fd = 0; |
|
73 |
- |
|
74 |
- logg("*ClamFanotif: attempting to stop event consumer thread ...\n"); |
|
75 |
- if (scque_pid > 0) { |
|
76 |
- pthread_cancel(scque_pid); |
|
77 |
- pthread_join(scque_pid, NULL); |
|
78 |
- } |
|
79 |
- scque_pid = 0; |
|
80 |
- |
|
81 |
- logg("*ClamFanotif: attempting to stop ddd thread ... \n"); |
|
82 |
- if (ddd_pid > 0) { |
|
83 |
- pthread_cancel(ddd_pid); |
|
84 |
- pthread_join(ddd_pid, NULL); |
|
85 |
- } |
|
86 |
- ddd_pid = 0; |
|
87 |
- |
|
88 |
- logg("ClamFanotif: stopped\n"); |
|
89 |
- pthread_exit(NULL); |
|
90 |
-} |
|
91 |
- |
|
92 | 62 |
cl_error_t onas_setup_fanotif(struct onas_context **ctx) { |
93 | 63 |
|
94 | 64 |
const struct optstruct *pt; |
... | ... |
@@ -166,8 +135,6 @@ cl_error_t onas_setup_fanotif(struct onas_context **ctx) { |
166 | 166 |
int onas_fan_eloop(struct onas_context **ctx) { |
167 | 167 |
int ret = 0; |
168 | 168 |
int err_cnt = 0; |
169 |
- sigset_t sigset; |
|
170 |
- struct sigaction act; |
|
171 | 169 |
short int scan; |
172 | 170 |
STATBUF sb; |
173 | 171 |
fd_set rfds; |
... | ... |
@@ -178,29 +145,6 @@ int onas_fan_eloop(struct onas_context **ctx) { |
178 | 178 |
int len, check, fres; |
179 | 179 |
char err[128]; |
180 | 180 |
|
181 |
- /* ignore all signals except SIGUSR1 */ |
|
182 |
- sigfillset(&sigset); |
|
183 |
- sigdelset(&sigset, SIGUSR1); |
|
184 |
- sigdelset(&sigset, SIGUSR2); |
|
185 |
- /* The behavior of a process is undefined after it ignores a |
|
186 |
- * SIGFPE, SIGILL, SIGSEGV, or SIGBUS signal */ |
|
187 |
- sigdelset(&sigset, SIGFPE); |
|
188 |
- sigdelset(&sigset, SIGILL); |
|
189 |
- sigdelset(&sigset, SIGSEGV); |
|
190 |
- sigdelset(&sigset, SIGINT); |
|
191 |
- sigdelset(&sigset, SIGTERM); |
|
192 |
-#ifdef SIGBUS |
|
193 |
- sigdelset(&sigset, SIGBUS); |
|
194 |
-#endif |
|
195 |
- pthread_sigmask(SIG_SETMASK, &sigset, NULL); |
|
196 |
- memset(&act, 0, sizeof(struct sigaction)); |
|
197 |
- act.sa_handler = onas_fan_exit; |
|
198 |
- sigfillset(&(act.sa_mask)); |
|
199 |
- sigaction(SIGUSR2, &act, NULL); |
|
200 |
- sigaction(SIGTERM, &act, NULL); |
|
201 |
- sigaction(SIGSEGV, &act, NULL); |
|
202 |
- sigaction(SIGINT, &act, NULL); |
|
203 |
- |
|
204 | 181 |
FD_ZERO(&rfds); |
205 | 182 |
FD_SET((*ctx)->fan_fd, &rfds); |
206 | 183 |
|