]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
unix-socket: implement reload-rules
authorVictor Julien <victor@inliniac.net>
Thu, 5 Mar 2015 15:25:09 +0000 (16:25 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 19 Mar 2015 17:02:19 +0000 (18:02 +0100)
Implement the reload-rules unix socket command. The unix command
thread signals the main thread to do the reload and it waits for
it to complete.

src/detect-engine.c
src/detect-engine.h
src/suricata.c
src/unix-manager.c

index 4e9757af23a5463b1e989d24a25dc53054cc2a56..f643594cf154d445651c489f42c7a1c97cc76ce7 100644 (file)
@@ -446,6 +446,69 @@ void DetectEngineRegisterAppInspectionEngine(uint8_t ipproto,
     return;
 }
 
+/* code to control the main thread to do a reload */
+
+enum DetectEngineSyncState {
+    IDLE,   /**< ready to start a reload */
+    RELOAD, /**< command main thread to do the reload */
+    DONE,   /**< main thread telling us reload is done */
+};
+
+
+typedef struct DetectEngineSyncer_ {
+    SCMutex m;
+    enum DetectEngineSyncState state;
+} DetectEngineSyncer;
+
+static DetectEngineSyncer detect_sync = { SCMUTEX_INITIALIZER, IDLE };
+
+/* tell main to start reloading */
+int DetectEngineReloadStart(void)
+{
+    int r = 0;
+    SCMutexLock(&detect_sync.m);
+    if (detect_sync.state == IDLE) {
+        detect_sync.state = RELOAD;
+    } else {
+        r = -1;
+    }
+    SCMutexUnlock(&detect_sync.m);
+    return r;
+}
+
+/* main thread checks this to see if it should start */
+int DetectEngineReloadIsStart(void)
+{
+    int r = 0;
+    SCMutexLock(&detect_sync.m);
+    if (detect_sync.state == RELOAD) {
+        r = 1;
+    }
+    SCMutexUnlock(&detect_sync.m);
+    return r;
+}
+
+/* main thread sets done when it's done */
+void DetectEngineReloadSetDone(void)
+{
+    SCMutexLock(&detect_sync.m);
+    detect_sync.state = DONE;
+    SCMutexUnlock(&detect_sync.m);
+}
+
+/* caller loops this until it returns 1 */
+int DetectEngineReloadIsDone(void)
+{
+    int r = 0;
+    SCMutexLock(&detect_sync.m);
+    if (detect_sync.state == DONE) {
+        r = 1;
+        detect_sync.state = IDLE;
+    }
+    SCMutexUnlock(&detect_sync.m);
+    return r;
+}
+
 static int DetectEngineReloadThreads(DetectEngineCtx *new_de_ctx)
 {
     SCEnter();
index a7a0734d4d82c4bcbecab21f8367da0699cbfbe2..dfd7bae93a010576a8246879464c9007c026d788 100644 (file)
@@ -78,6 +78,12 @@ void DetectEngineDeReference(DetectEngineCtx **de_ctx);
 int DetectEngineReload(const char *filename);
 int DetectEngineEnabled(void);
 
+int DetectEngineReloadStart(void);
+int DetectEngineReloadIsStart(void);
+void DetectEngineReloadSetDone(void);
+int DetectEngineReloadIsDone(void);
+
+
 /**
  * \brief Registers an app inspection engine.
  *
index 0cf908f15a9fb8742f4660695a55bdb4efe80ee1..c999611b2a0b0e2eb9dd32e88f785a5694d92051 100644 (file)
@@ -2414,9 +2414,13 @@ int main(int argc, char **argv)
             OutputNotifyFileRotation();
             sighup_count--;
         }
+
         if (sigusr2_count > 0) {
             DetectEngineReload(conf_filename);
             sigusr2_count--;
+        } else if (DetectEngineReloadIsStart()) {
+            DetectEngineReload(conf_filename);
+            DetectEngineReloadSetDone();
         }
 
         usleep(10* 1000);
index 6863db3494558b2f6672867feb192f5465c16d71..e86bae81aab94719975b23f497818de906d6ba89 100644 (file)
@@ -647,6 +647,18 @@ TmEcode UnixManagerCaptureModeCommand(json_t *cmd,
     SCReturnInt(TM_ECODE_OK);
 }
 
+TmEcode UnixManagerReloadRules(json_t *cmd, json_t *server_msg, void *data)
+{
+    SCEnter();
+    DetectEngineReloadStart();
+
+    while (DetectEngineReloadIsDone() == 0)
+        usleep(100);
+
+    json_object_set_new(server_msg, "message", json_string("done"));
+    SCReturnInt(TM_ECODE_OK);
+}
+
 TmEcode UnixManagerConfGetCommand(json_t *cmd,
                                   json_t *server_msg, void *data)
 {
@@ -870,9 +882,7 @@ void *UnixManagerThread(void *td)
     UnixManagerRegisterCommand("capture-mode", UnixManagerCaptureModeCommand, &command, 0);
     UnixManagerRegisterCommand("conf-get", UnixManagerConfGetCommand, &command, UNIX_CMD_TAKE_ARGS);
     UnixManagerRegisterCommand("dump-counters", SCPerfOutputCounterSocket, NULL, 0);
-#if 0
     UnixManagerRegisterCommand("reload-rules", UnixManagerReloadRules, NULL, 0);
-#endif
 
     TmThreadsSetFlag(th_v, THV_INIT_DONE);
     while (1) {