]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Introduce util-signal.[ch]. Move our signal setup functions here
authorAnoop Saldanha <poonaatsoc@gmail.com>
Fri, 22 Jun 2012 18:03:11 +0000 (23:33 +0530)
committerVictor Julien <victor@inliniac.net>
Tue, 26 Jun 2012 07:36:11 +0000 (09:36 +0200)
src/Makefile.am
src/counters.c
src/detect-engine.c
src/flow-manager.c
src/suricata.c
src/suricata.h
src/tm-threads.c
src/util-misc.c
src/util-misc.h
src/util-signal.c [new file with mode: 0644]
src/util-signal.h [new file with mode: 0644]

index 3508af10ccef2fdf0ebe52e0ea56403d96731c20..1b8401dec908b16193809fff5d2e890fa573ecc5 100644 (file)
@@ -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 \
index 53e9c9caceb976b63db83bab13839cae1b0e73e9..dd0c98796ffeba35c66a9602ac7de41d5f0cf198 100644 (file)
@@ -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;
index 815343b5c415179cc38f938d6e43b44810fbdad7..db1c0697dd6f7e509390182d3fd7a9d1333b3dc7 100644 (file)
@@ -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);
 
index 36dd467db4004b1599a12df7cb1e71cf9a4a4fd9..cc4329a4504d774a6f83e56918fad21fc8301255 100644 (file)
@@ -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;
index 5a9ed6654a589b9e90580a18583f991c20aa783d..818cb9e6a413c18943629ba12eed59148163f249 100644 (file)
 #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);
index c100a6fc47c517894f524cd09c7c43034f0724ad..d5aed9968f989a7d756e955ec00115a308e8f16a 100644 (file)
@@ -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);
 
index 3864e88d5ddd12251c888b06fc69ddac21660ea5..1b167ddd049d5ce8d09e9dadbe5d6d50c57aec23 100644 (file)
@@ -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;
index 076b5ce78ef6873e3e3fde1de61a790b383e31ce..9a993274cc96166fc860e09c94731166c37ccbb8 100644 (file)
@@ -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*$"
index aed68bf2fbfd477277c7b5addcea503e64046d73..3c139db5d4ce46feae9cbe108d76fc9c113f7931 100644 (file)
@@ -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 (file)
index 0000000..753cb9b
--- /dev/null
@@ -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 <anoopsaldanha@gmail.com>
+ */
+
+#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 (file)
index 0000000..6bbd855
--- /dev/null
@@ -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 <anoopsaldanha@gmail.com>
+ */
+
+#ifndef __UTIL_STRING_H__
+#define __UTIL_STRING_H__
+
+int UtilSignalBlock(int);
+void UtilSignalHandlerSetup(int, void (*handler)());;
+
+#endif /* __UTIL_STRING_H__ */