From: Anoop Saldanha Date: Fri, 22 Jun 2012 18:03:11 +0000 (+0530) Subject: Introduce util-signal.[ch]. Move our signal setup functions here X-Git-Tag: suricata-1.3rc1~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31eb5fa2f69d99689705cb6431235effc77e0d3f;p=thirdparty%2Fsuricata.git Introduce util-signal.[ch]. Move our signal setup functions here --- diff --git a/src/Makefile.am b/src/Makefile.am index 3508af10cc..1b8401dec9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -181,6 +181,7 @@ detect-replace.c detect-replace.h \ util-magic.c util-magic.h \ util-misc.c util-misc.h \ util-atomic.c util-atomic.h \ +util-signal.c util-signal.h \ util-print.c util-print.h \ util-fmemopen.c util-fmemopen.h \ util-cpu.c util-cpu.h \ diff --git a/src/counters.c b/src/counters.c index 53e9c9cace..dd0c98796f 100644 --- a/src/counters.c +++ b/src/counters.c @@ -33,6 +33,7 @@ #include "util-unittest.h" #include "util-debug.h" #include "util-privs.h" +#include "util-signal.h" /** \todo Get the default log directory from some global resource. */ #define SC_PERF_DEFAULT_LOG_FILENAME "stats.log" @@ -434,10 +435,7 @@ static void SCPerfReleaseOPCtx() static void *SCPerfMgmtThread(void *arg) { /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *tv_local = (ThreadVars *)arg; uint8_t run = 1; @@ -494,10 +492,7 @@ static void *SCPerfMgmtThread(void *arg) static void *SCPerfWakeupThread(void *arg) { /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *tv_local = (ThreadVars *)arg; uint8_t run = 1; diff --git a/src/detect-engine.c b/src/detect-engine.c index 815343b5c4..db1c0697dd 100644 --- a/src/detect-engine.c +++ b/src/detect-engine.c @@ -49,7 +49,6 @@ #include "detect-uricontent.h" #include "detect-engine-threshold.h" -//#include "util-mpm.h" #include "util-classification-config.h" #include "util-reference-config.h" #include "util-threshold-config.h" @@ -60,6 +59,7 @@ #include "util-unittest.h" #include "util-action.h" #include "util-magic.h" +#include "util-signal.h" #include "util-var-name.h" @@ -82,10 +82,7 @@ static void *DetectEngineLiveRuleSwap(void *arg) ThreadVars *tv_local = (ThreadVars *)arg; /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ConfDeInit(); ConfInit(); @@ -125,9 +122,6 @@ static void *DetectEngineLiveRuleSwap(void *arg) exit(EXIT_FAILURE); } - //if (MagicInit() != 0) - // exit(EXIT_FAILURE); - uint8_t local_need_htp_request_body = need_htp_request_body; uint8_t local_need_htp_request_multipart_hdr = need_htp_request_multipart_hdr; uint8_t local_need_htp_request_file = need_htp_request_file; @@ -147,7 +141,7 @@ static void *DetectEngineLiveRuleSwap(void *arg) "can't be enabled at runtime. You will have to restart " "engine to load the new ruleset ====="); DetectEngineCtxFree(de_ctx); - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2); TmThreadsSetFlag(tv_local, THV_CLOSED); @@ -174,7 +168,7 @@ static void *DetectEngineLiveRuleSwap(void *arg) SCLogInfo("===== Live rule swap premature exit, since " "suricta_ctl_flags != 0 ====="); - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); pthread_exit(NULL); } @@ -227,7 +221,7 @@ static void *DetectEngineLiveRuleSwap(void *arg) "swapping det_ctxs, since " "suricta_ctl_flags != 0 ====="); - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); pthread_exit(NULL); } @@ -257,7 +251,7 @@ static void *DetectEngineLiveRuleSwap(void *arg) SCLogInfo("===== Live rule swap done, but premature exit at " "de-init phase, since suricta_ctl_flags != 0 ====="); - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); pthread_exit(NULL); } @@ -276,7 +270,7 @@ static void *DetectEngineLiveRuleSwap(void *arg) SCLogInfo("===== Live rule swap done, but premature exit at " "de-init phase, since suricta_ctl_flags != 0 ====="); - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); pthread_exit(NULL); } @@ -286,7 +280,7 @@ static void *DetectEngineLiveRuleSwap(void *arg) } DetectEngineCtxFree(old_de_ctx); - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2); TmThreadsSetFlag(tv_local, THV_CLOSED); diff --git a/src/flow-manager.c b/src/flow-manager.c index 36dd467db4..cc4329a450 100644 --- a/src/flow-manager.c +++ b/src/flow-manager.c @@ -52,6 +52,7 @@ #include "util-debug.h" #include "util-privs.h" +#include "util-signal.h" #include "threads.h" #include "detect.h" @@ -372,10 +373,7 @@ next: void *FlowManagerThread(void *td) { /* block usr1. usr1 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *th_v = (ThreadVars *)td; struct timeval ts; diff --git a/src/suricata.c b/src/suricata.c index 5a9ed6654a..818cb9e6a4 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -154,6 +154,7 @@ #include "util-reference-config.h" #include "util-profiling.h" #include "util-magic.h" +#include "util-signal.h" #include "util-coredump-config.h" @@ -267,7 +268,7 @@ void SignalHandlerSigusr2(int sig) return; } - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2Idle); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2Idle); DetectEngineSpawnLiveRuleSwapMgmtThread(); @@ -295,22 +296,6 @@ uint8_t print_mem_flag = 1; #endif #endif -void SignalHandlerSetup(int sig, void (*handler)()) -{ -#if defined (OS_WIN32) - signal(sig, handler); -#else - struct sigaction action; - memset(&action, 0x00, sizeof(struct sigaction)); - - action.sa_handler = handler; - sigemptyset(&(action.sa_mask)); - sigaddset(&(action.sa_mask),sig); - action.sa_flags = 0; - sigaction(sig, &action, 0); -#endif /* OS_WIN32 */ -} - void GlobalInits() { memset(trans_q, 0, sizeof(trans_q)); @@ -1470,7 +1455,7 @@ int main(int argc, char **argv) AppLayerHtpNeedFileInspection(); - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2Idle); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2Idle); #ifdef UNITTESTS @@ -1630,14 +1615,14 @@ int main(int argc, char **argv) #endif /* registering signals we use */ - SignalHandlerSetup(SIGINT, SignalHandlerSigint); - SignalHandlerSetup(SIGTERM, SignalHandlerSigterm); - SignalHandlerSetup(SIGPIPE, SIG_IGN); - SignalHandlerSetup(SIGSYS, SIG_IGN); + UtilSignalHandlerSetup(SIGINT, SignalHandlerSigint); + UtilSignalHandlerSetup(SIGTERM, SignalHandlerSigterm); + UtilSignalHandlerSetup(SIGPIPE, SIG_IGN); + UtilSignalHandlerSetup(SIGSYS, SIG_IGN); #ifndef OS_WIN32 /* SIGHUP is not implemnetd on WIN32 */ - //SignalHandlerSetup(SIGHUP, SignalHandlerSighup); + //UtilSignalHandlerSetup(SIGHUP, SignalHandlerSighup); /* Get the suricata user ID to given user ID */ if (do_setuid == TRUE) { @@ -1711,7 +1696,7 @@ int main(int argc, char **argv) /* registering singal handlers we use. We register usr2 here, so that one * can't call it during the first sig load phase */ - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2); #ifdef PROFILING SCProfilingInitRuleCounters(de_ctx); @@ -1880,7 +1865,7 @@ int main(int argc, char **argv) usleep(10* 1000); } - SignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); + UtilSignalHandlerSetup(SIGUSR2, SignalHandlerSigusr2EngineShutdown); /* Update the engine stage/status flag */ SC_ATOMIC_CAS(&engine_stage, SURICATA_RUNTIME, SURICATA_DEINIT); diff --git a/src/suricata.h b/src/suricata.h index c100a6fc47..d5aed9968f 100644 --- a/src/suricata.h +++ b/src/suricata.h @@ -141,7 +141,6 @@ void EngineStop(void); void EngineKill(void); /* live rule swap required this to be made static */ -void SignalHandlerSetup(int, void (*handler)()); void SignalHandlerSigusr2(int); void SignalHandlerSigusr2EngineShutdown(int); diff --git a/src/tm-threads.c b/src/tm-threads.c index 3864e88d5d..1b167ddd04 100644 --- a/src/tm-threads.c +++ b/src/tm-threads.c @@ -42,6 +42,7 @@ #include "util-cpu.h" #include "util-optimize.h" #include "util-profiling.h" +#include "util-signal.h" #ifdef PROFILE_LOCKING __thread uint64_t mutex_lock_contention; @@ -122,10 +123,7 @@ void TmThreadsUnsetFlag(ThreadVars *tv, uint8_t flag) void *TmThreadsSlot1NoIn(void *td) { /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *tv = (ThreadVars *)td; TmSlot *s = (TmSlot *)tv->tm_slots; @@ -220,10 +218,7 @@ void *TmThreadsSlot1NoIn(void *td) void *TmThreadsSlot1NoOut(void *td) { /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *tv = (ThreadVars *)td; TmSlot *s = (TmSlot *)tv->tm_slots; @@ -301,10 +296,7 @@ void *TmThreadsSlot1NoOut(void *td) void *TmThreadsSlot1NoInOut(void *td) { /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *tv = (ThreadVars *)td; TmSlot *s = (TmSlot *)tv->tm_slots; @@ -377,10 +369,7 @@ void *TmThreadsSlot1NoInOut(void *td) void *TmThreadsSlot1(void *td) { /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *tv = (ThreadVars *)td; TmSlot *s = (TmSlot *)tv->tm_slots; @@ -583,10 +572,7 @@ TmEcode TmThreadsSlotVarRun(ThreadVars *tv, Packet *p, void *TmThreadsSlotPktAcqLoop(void *td) { /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *tv = (ThreadVars *)td; TmSlot *s = tv->tm_slots; @@ -676,10 +662,7 @@ void *TmThreadsSlotPktAcqLoop(void *td) { void *TmThreadsSlotVar(void *td) { /* block usr2. usr2 to be handled by the main thread only */ - sigset_t x; - sigemptyset(&x); - sigaddset(&x, SIGUSR2); - sigprocmask(SIG_BLOCK, &x, NULL); + UtilSignalBlock(SIGUSR2); ThreadVars *tv = (ThreadVars *)td; TmSlot *s = (TmSlot *)tv->tm_slots; diff --git a/src/util-misc.c b/src/util-misc.c index 076b5ce78e..9a993274cc 100644 --- a/src/util-misc.c +++ b/src/util-misc.c @@ -28,6 +28,8 @@ #include "util-debug.h" #include "util-unittest.h" +/* size string parsing API */ + static int ParseSizeString(const char *size, double *res) { #define PARSE_REGEX "^\\s*(\\d+(?:.\\d+)?)\\s*([a-zA-Z]{2})?\\s*$" diff --git a/src/util-misc.h b/src/util-misc.h index aed68bf2fb..3c139db5d4 100644 --- a/src/util-misc.h +++ b/src/util-misc.h @@ -24,6 +24,8 @@ #ifndef __UTIL_MISC_H__ #define __UTIL_MISC_H__ +/* size string parsing API */ + int ParseSizeStringU8(const char *, uint8_t *); int ParseSizeStringU16(const char *, uint16_t *); int ParseSizeStringU32(const char *, uint32_t *); diff --git a/src/util-signal.c b/src/util-signal.c new file mode 100644 index 0000000000..753cb9b15d --- /dev/null +++ b/src/util-signal.c @@ -0,0 +1,57 @@ +/* Copyright (C) 2007-2012 Open Information Security Foundation + * + * You can copy, redistribute or modify this Program under the terms of + * the GNU General Public License version 2 as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +/** + * \file + * + * \author Anoop Saldanha + */ + +#include "suricata-common.h" +#include "suricata.h" +#include "util-debug.h" + +int UtilSignalBlock(int signum) +{ + sigset_t x; + if (sigemptyset(&x) < 0) + return -1; + if (sigaddset(&x, signum) < 0) + return -1; + if (sigprocmask(SIG_BLOCK, &x, NULL) < 0) + return -1; + + return 0; +} + +void UtilSignalHandlerSetup(int sig, void (*handler)()) +{ +#if defined (OS_WIN32) + signal(sig, handler); +#else + struct sigaction action; + memset(&action, 0x00, sizeof(struct sigaction)); + + action.sa_handler = handler; + sigemptyset(&(action.sa_mask)); + sigaddset(&(action.sa_mask),sig); + action.sa_flags = 0; + sigaction(sig, &action, 0); +#endif /* OS_WIN32 */ + + return; +} diff --git a/src/util-signal.h b/src/util-signal.h new file mode 100644 index 0000000000..6bbd8553da --- /dev/null +++ b/src/util-signal.h @@ -0,0 +1,30 @@ +/* Copyright (C) 2007-2012 Open Information Security Foundation + * + * You can copy, redistribute or modify this Program under the terms of + * the GNU General Public License version 2 as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +/** + * \file + * + * \author Anoop Saldanha + */ + +#ifndef __UTIL_STRING_H__ +#define __UTIL_STRING_H__ + +int UtilSignalBlock(int); +void UtilSignalHandlerSetup(int, void (*handler)());; + +#endif /* __UTIL_STRING_H__ */