From 2c71c7fe6ae7be1bafb8eac5e8513b90f026f785 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 26 Feb 2024 16:45:07 -0600 Subject: [PATCH] suricata: move SuricataMain into main() 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 | 10 ++++++++ src/main.c | 39 ++++++++++++++++++++++++++++- src/suricata.c | 50 +++----------------------------------- src/suricata.h | 6 ++++- 4 files changed, 57 insertions(+), 48 deletions(-) diff --git a/examples/lib/simple/main.c b/examples/lib/simple/main.c index bae590afdd..0e728e9732 100644 --- a/examples/lib/simple/main.c +++ b/examples/lib/simple/main.c @@ -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(); diff --git a/src/main.c b/src/main.c index 05c9276b4b..714efda118 100644 --- a/src/main.c +++ b/src/main.c @@ -19,5 +19,42 @@ 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); } diff --git a/src/suricata.c b/src/suricata.c index 33a3fe6c52..3bc86f446a 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -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); -} diff --git a/src/suricata.h b/src/suricata.h index 059eebf675..9ec80ddc83 100644 --- a/src/suricata.h +++ b/src/suricata.h @@ -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 */ -- 2.47.2