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.
pull/10720/head
Jason Ish 2 years ago committed by Victor Julien
parent c476fcc85b
commit 2c71c7fe6a

@ -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();

@ -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);
}

@ -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);
}

@ -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 */

Loading…
Cancel
Save