From ad8e8dd0e8b658b4e2e25afc3aac32be109a74fa Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 26 Aug 2024 14:18:22 -0600 Subject: [PATCH] runmodes: typedef runmode enum and use as type Also remove function to set the library mode. This is easy enough to do with SCRunmodeSet, and we don't want to add a specific setter for each and every runmode. Ticket: #7240 --- examples/lib/custom/main.c | 7 ++++--- src/runmodes.c | 10 +++++----- src/runmodes.h | 10 +++++----- src/suricata.c | 9 ++------- src/suricata.h | 11 ++++------- src/util-privs.c | 2 ++ 6 files changed, 22 insertions(+), 27 deletions(-) diff --git a/examples/lib/custom/main.c b/examples/lib/custom/main.c index 85926ce314..45b0bb5046 100644 --- a/examples/lib/custom/main.c +++ b/examples/lib/custom/main.c @@ -16,6 +16,7 @@ */ #include "suricata.h" +#include "runmodes.h" #include "conf.h" #include "pcap.h" #include "runmode-lib.h" @@ -94,9 +95,9 @@ int main(int argc, char **argv) return 1; } - /* Set lib runmode. There is currently no way to set it via the - * Conf API. */ - SuricataSetLibRunmode(); + /* Set the runmode to library mode. Perhaps in the future this + * should be done in some library bootstrap function. */ + SCRunmodeSet(RUNMODE_LIB); /* Validate/finalize the runmode. */ if (SCFinalizeRunMode() != TM_ECODE_OK) { diff --git a/src/runmodes.c b/src/runmodes.c index f5434f4cc8..897aba6d37 100644 --- a/src/runmodes.c +++ b/src/runmodes.c @@ -81,7 +81,7 @@ const char *thread_name_heartbeat = "HB"; */ typedef struct RunMode_ { /* the runmode type */ - enum RunModes runmode; + enum SCRunModes runmode; const char *name; const char *description; /* runmode function */ @@ -177,7 +177,7 @@ static const char *RunModeTranslateModeToName(int runmode) * \param runmode The runmode type. * \param runmode_custom_id The runmode custom id. */ -static RunMode *RunModeGetCustomMode(enum RunModes runmode, const char *custom_mode) +static RunMode *RunModeGetCustomMode(enum SCRunModes runmode, const char *custom_mode) { if (runmode < RUNMODE_USER_MAX) { for (int i = 0; i < runmodes[runmode].cnt; i++) { @@ -473,7 +473,7 @@ int RunModeNeedsBypassManager(void) * \param description Description for this runmode. * \param RunModeFunc The function to be run for this runmode. */ -void RunModeRegisterNewRunMode(enum RunModes runmode, const char *name, const char *description, +void RunModeRegisterNewRunMode(enum SCRunModes runmode, const char *name, const char *description, int (*RunModeFunc)(void), int (*RunModeIsIPSEnabled)(void)) { if (RunModeGetCustomMode(runmode, name) != NULL) { @@ -535,7 +535,7 @@ int RunModeOutputFiledataEnabled(void) return filedata_logger_count > 0; } -bool IsRunModeSystem(enum RunModes run_mode_to_check) +bool IsRunModeSystem(enum SCRunModes run_mode_to_check) { switch (run_mode_to_check) { case RUNMODE_PCAP_FILE: @@ -548,7 +548,7 @@ bool IsRunModeSystem(enum RunModes run_mode_to_check) } } -bool IsRunModeOffline(enum RunModes run_mode_to_check) +bool IsRunModeOffline(enum SCRunModes run_mode_to_check) { switch(run_mode_to_check) { case RUNMODE_CONF_TEST: diff --git a/src/runmodes.h b/src/runmodes.h index 4466d69b9e..748733e4f4 100644 --- a/src/runmodes.h +++ b/src/runmodes.h @@ -24,7 +24,7 @@ #define SURICATA_RUNMODES_H /* Run mode */ -enum RunModes { +typedef enum SCRunModes { RUNMODE_UNKNOWN = 0, RUNMODE_PCAP_DEV, RUNMODE_PCAP_FILE, @@ -60,7 +60,7 @@ enum RunModes { #endif RUNMODE_DUMP_FEATURES, RUNMODE_MAX, -}; +} SCRunMode; /* Run Mode Global Thread Names */ extern const char *thread_name_autofp; @@ -83,7 +83,7 @@ void RunModeListRunmodes(void); int RunModeEngineIsIPS(int capture_mode, const char *runmode, const char *capture_plugin_name); void RunModeDispatch(int, const char *, const char *capture_plugin_name, const char *capture_plugin_args); void RunModeRegisterRunModes(void); -void RunModeRegisterNewRunMode(enum RunModes, const char *, const char *, int (*RunModeFunc)(void), +void RunModeRegisterNewRunMode(SCRunMode, const char *, const char *, int (*RunModeFunc)(void), int (*RunModeIsIPSEnabled)(void)); void RunModeInitializeThreadSettings(void); void RunModeInitializeOutputs(void); @@ -92,8 +92,8 @@ void RunModeShutDown(void); /* bool indicating if filedata logger is enabled */ int RunModeOutputFiledataEnabled(void); /** bool indicating if run mode is offline */ -bool IsRunModeOffline(enum RunModes run_mode_to_check); -bool IsRunModeSystem(enum RunModes run_mode_to_check); +bool IsRunModeOffline(SCRunMode run_mode_to_check); +bool IsRunModeSystem(SCRunMode run_mode_to_check); void RunModeEnablesBypassManager(void); int RunModeNeedsBypassManager(void); diff --git a/src/suricata.c b/src/suricata.c index 7c3b05f3f8..ebd411af27 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -262,12 +262,12 @@ int RunmodeIsUnittests(void) } #endif -int SCRunmodeGet(void) +SCRunMode SCRunmodeGet(void) { return suricata.run_mode; } -void SCRunmodeSet(int run_mode) +void SCRunmodeSet(SCRunMode run_mode) { suricata.run_mode = run_mode; } @@ -3067,8 +3067,3 @@ void SuricataPostInit(void) } SCPledge(); } - -void SuricataSetLibRunmode(void) -{ - suricata.run_mode = RUNMODE_LIB; -} diff --git a/src/suricata.h b/src/suricata.h index 311671cb79..ba83c16b16 100644 --- a/src/suricata.h +++ b/src/suricata.h @@ -121,8 +121,8 @@ enum { #include "runmodes.h" typedef struct SCInstance_ { - enum RunModes run_mode; - enum RunModes aux_run_mode; + enum SCRunModes run_mode; + enum SCRunModes aux_run_mode; char pcap_dev[128]; char *sig_file; @@ -191,14 +191,14 @@ int RunmodeIsUnittests(void); /** * \brief Get the current run mode. */ -int SCRunmodeGet(void); +SCRunMode SCRunmodeGet(void); /** * \brief Set the current run mode. * * Mainly exposed outside of suricata.c as a unit-test helper. */ -void SCRunmodeSet(int run_mode); +void SCRunmodeSet(SCRunMode run_mode); int SuriHasSigFile(void); @@ -227,7 +227,4 @@ int WindowsInitService(int argc, char **argv); const char *GetProgramVersion(void); -/* Library only methods. */ -void SuricataSetLibRunmode(void); - #endif /* SURICATA_SURICATA_H */ diff --git a/src/util-privs.c b/src/util-privs.c index 835247090d..da82234ba4 100644 --- a/src/util-privs.c +++ b/src/util-privs.c @@ -72,6 +72,8 @@ void SCDropMainThreadCaps(uint32_t userid, uint32_t groupid) CAP_SYS_NICE, -1); break; + default: + break; } if (capng_change_id(userid, groupid, CAPNG_DROP_SUPP_GRP | -- 2.47.2