]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
suricata: move SuricataMain into main()
authorJason Ish <jason.ish@oisf.net>
Mon, 26 Feb 2024 22:45:07 +0000 (16:45 -0600)
committerVictor Julien <victor@inliniac.net>
Mon, 25 Mar 2024 16:36:29 +0000 (17:36 +0100)
Move the contents of SuricataMain into the `main()` function found in
main.c. This forces the Suricata application to bootstrap and run
Suricata through the same interfaces as a library user might do.

Required exposing StartInternalRunMode as SCStartInternalRunmode. Its
arguable whether those "actions" belong in the library or just the
application, but I think that is separation we can look at later.

For now the lib example and Suricata's own main are the same, however
the example will probably extend more into programmatically
configuring Suricata or dynamically registering a runmode, which
doesn't really belong the main Suricata application.

examples/lib/simple/main.c
src/main.c
src/suricata.c
src/suricata.h

index bae590afdda818d5817247a2cbd8c9d5c1a96e6e..0e728e9732e2e2877d9748ca40834d27716a6319 100644 (file)
@@ -30,6 +30,16 @@ int main(int argc, char **argv)
         exit(EXIT_FAILURE);
     }
 
+    /* Handle internal runmodes. Typically you wouldn't do this as a
+     * library user, however this example is showing how to replicate
+     * the Suricata application with the library. */
+    switch (SCStartInternalRunMode(argc, argv)) {
+        case TM_ECODE_DONE:
+            exit(EXIT_SUCCESS);
+        case TM_ECODE_FAILED:
+            exit(EXIT_FAILURE);
+    }
+
     SuricataInit();
     SuricataPostInit();
 
index 05c9276b4b8dc4830c53cd17bc3eb3854a46f59f..714efda118850bf125912ca03e0cce69d0333fa0 100644 (file)
 
 int main(int argc, char **argv)
 {
-    return SuricataMain(argc, argv);
+    /* Pre-initialization tasks: initialize global context and variables. */
+    SuricataPreInit(argv[0]);
+
+#ifdef OS_WIN32
+    /* service initialization */
+    if (WindowsInitService(argc, argv) != 0) {
+        exit(EXIT_FAILURE);
+    }
+#endif /* OS_WIN32 */
+
+    if (SCParseCommandLine(argc, argv) != TM_ECODE_OK) {
+        exit(EXIT_FAILURE);
+    }
+
+    if (SCFinalizeRunMode() != TM_ECODE_OK) {
+        exit(EXIT_FAILURE);
+    }
+
+    switch (SCStartInternalRunMode(argc, argv)) {
+        case TM_ECODE_DONE:
+            exit(EXIT_SUCCESS);
+        case TM_ECODE_FAILED:
+            exit(EXIT_FAILURE);
+    }
+
+    /* Initialization tasks: Loading config, setup logging */
+    SuricataInit();
+
+    /* Post-initialization tasks: wait on thread start/running and get ready for the main loop. */
+    SuricataPostInit();
+
+    SuricataMainLoop();
+
+    /* Shutdown engine. */
+    SuricataShutdown();
+    GlobalsDestroy();
+
+    exit(EXIT_SUCCESS);
 }
index 33a3fe6c523840de8e3c1d894f27bf3437499427..3bc86f446ac1bc0b2fdf561c671fde3d716b9956 100644 (file)
@@ -2078,7 +2078,7 @@ TmEcode SCParseCommandLine(int argc, char **argv)
 }
 
 #ifdef OS_WIN32
-static int WindowsInitService(int argc, char **argv)
+int WindowsInitService(int argc, char **argv)
 {
     if (SCRunningAsService()) {
         char path[MAX_PATH];
@@ -2312,11 +2312,11 @@ void PostRunDeinit(const int runmode, struct timeval *start_time)
 #endif
 }
 
-
-static int StartInternalRunMode(SCInstance *suri, int argc, char **argv)
+int SCStartInternalRunMode(int argc, char **argv)
 {
+    SCInstance *suri = &suricata;
     /* Treat internal running mode */
-    switch(suri->run_mode) {
+    switch (suri->run_mode) {
         case RUNMODE_LIST_KEYWORDS:
             return ListKeywords(suri->keyword_info);
         case RUNMODE_LIST_APP_LAYERS:
@@ -3060,45 +3060,3 @@ void SuricataPostInit(void)
     }
     SCPledge();
 }
-
-int SuricataMain(int argc, char **argv)
-{
-    /* Pre-initialization tasks: initialize global context and variables. */
-    SuricataPreInit(argv[0]);
-
-#ifdef OS_WIN32
-    /* service initialization */
-    if (WindowsInitService(argc, argv) != 0) {
-        exit(EXIT_FAILURE);
-    }
-#endif /* OS_WIN32 */
-
-    if (SCParseCommandLine(argc, argv) != TM_ECODE_OK) {
-        exit(EXIT_FAILURE);
-    }
-
-    if (SCFinalizeRunMode() != TM_ECODE_OK) {
-        exit(EXIT_FAILURE);
-    }
-
-    switch (StartInternalRunMode(&suricata, argc, argv)) {
-        case TM_ECODE_DONE:
-            exit(EXIT_SUCCESS);
-        case TM_ECODE_FAILED:
-            exit(EXIT_FAILURE);
-    }
-
-    /* Initialization tasks: Loading config, setup logging */
-    SuricataInit();
-
-    /* Post-initialization tasks: wait on thread start/running and get ready for the main loop. */
-    SuricataPostInit();
-
-    SuricataMainLoop();
-
-    /* Shutdown engine. */
-    SuricataShutdown();
-    GlobalsDestroy();
-
-    exit(EXIT_SUCCESS);
-}
index 059eebf67568a5c9ace6741b44242b7fa12ac436..9ec80ddc832360af18dc095b4cbf54143e851cb7 100644 (file)
@@ -194,7 +194,6 @@ extern int run_mode;
 void SuricataPreInit(const char *progname);
 void SuricataInit(void);
 void SuricataPostInit(void);
-int SuricataMain(int argc, char **argv);
 void SuricataMainLoop(void);
 void SuricataShutdown(void);
 int InitGlobal(void);
@@ -203,12 +202,17 @@ int PostConfLoadedSetup(SCInstance *suri);
 void PostConfLoadedDetectSetup(SCInstance *suri);
 int SCFinalizeRunMode(void);
 TmEcode SCParseCommandLine(int argc, char **argv);
+int SCStartInternalRunMode(int argc, char **argv);
 
 void PreRunInit(const int runmode);
 void PreRunPostPrivsDropInit(const int runmode);
 void PostRunDeinit(const int runmode, struct timeval *start_time);
 void RegisterAllModules(void);
 
+#ifdef OS_WIN32
+int WindowsInitService(int argc, char **argv);
+#endif
+
 const char *GetProgramVersion(void);
 
 #endif /* SURICATA_SURICATA_H */