run-mode: remove duplicate var; add setter function

Remove the global "run_mode" var as it was a duplicate of the runmode on
the "instance" struct. For direct access outside of suricata.c, use the
getter function.

Also expose a setter function for unit tests that need to change it.
pull/10952/head
Jason Ish 1 year ago committed by Victor Julien
parent a2502c9fca
commit d2537361f4

@ -304,7 +304,7 @@ int SigLoadSignatures(DetectEngineCtx *de_ctx, char *sig_file, bool sig_file_exc
de_ctx->config_prefix);
}
if (RunmodeGetCurrent() == RUNMODE_ENGINE_ANALYSIS) {
if (SCRunmodeGet() == RUNMODE_ENGINE_ANALYSIS) {
SetupEngineAnalysis(de_ctx, &fp_engine_analysis_set, &rule_engine_analysis_set);
}
@ -405,7 +405,7 @@ int SigLoadSignatures(DetectEngineCtx *de_ctx, char *sig_file, bool sig_file_exc
end:
gettimeofday(&de_ctx->last_reload, NULL);
if (RunmodeGetCurrent() == RUNMODE_ENGINE_ANALYSIS) {
if (SCRunmodeGet() == RUNMODE_ENGINE_ANALYSIS) {
CleanupEngineAnalysis(de_ctx);
}

@ -2457,7 +2457,7 @@ static DetectEngineCtx *DetectEngineCtxInitReal(
SCClassConfInit(de_ctx);
if (!SCClassConfLoadClassificationConfigFile(de_ctx, NULL)) {
if (RunmodeGetCurrent() == RUNMODE_CONF_TEST)
if (SCRunmodeGet() == RUNMODE_CONF_TEST)
goto error;
}
@ -2466,7 +2466,7 @@ static DetectEngineCtx *DetectEngineCtxInitReal(
}
SCReferenceConfInit(de_ctx);
if (SCRConfLoadReferenceConfigFile(de_ctx, NULL) < 0) {
if (RunmodeGetCurrent() == RUNMODE_CONF_TEST)
if (SCRunmodeGet() == RUNMODE_CONF_TEST)
goto error;
}
@ -2698,7 +2698,7 @@ static int DetectEngineCtxLoadConf(DetectEngineCtx *de_ctx)
}
}
if (run_mode == RUNMODE_UNITTEST) {
if (RunmodeIsUnittests()) {
de_ctx->sgh_mpm_ctx_cnf = ENGINE_SGH_MPM_FACTORY_CONTEXT_FULL;
}

@ -30,8 +30,6 @@
#include "runmodes.h"
extern int run_mode;
#include "decode.h"
#include "detect.h"
@ -100,7 +98,7 @@ int DetectReplaceSetup(DetectEngineCtx *de_ctx, Signature *s, const char *replac
return -1;
}
switch (run_mode) {
switch (SCRunmodeGet()) {
case RUNMODE_NFQ:
case RUNMODE_IPFW:
break;
@ -355,15 +353,15 @@ static int DetectReplaceLongPatternMatchTestWrp(const char *sig, uint32_t sid, c
uint16_t psize = sizeof(raw_eth_pkt);
/* would be unittest */
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
ret = DetectReplaceLongPatternMatchTest(raw_eth_pkt, (uint16_t)sizeof(raw_eth_pkt),
sig, sid, p, &psize);
if (ret == 1) {
SCLogDebug("replace: test1 phase1");
ret = DetectReplaceLongPatternMatchTest(p, psize, sig_rep, sid_rep, NULL, NULL);
}
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
return ret;
}
@ -390,15 +388,15 @@ static int DetectReplaceLongPatternMatchTestUDPWrp(const char *sig, uint32_t sid
uint8_t p[sizeof(raw_eth_pkt)];
uint16_t psize = sizeof(raw_eth_pkt);
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
ret = DetectReplaceLongPatternMatchTest(raw_eth_pkt, (uint16_t)sizeof(raw_eth_pkt),
sig, sid, p, &psize);
if (ret == 1) {
SCLogDebug("replace: test1 phase1 ok: %" PRIuMAX" vs %d",(uintmax_t)sizeof(raw_eth_pkt),psize);
ret = DetectReplaceLongPatternMatchTest(p, psize, sig_rep, sid_rep, NULL, NULL);
}
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
return ret;
}
@ -605,8 +603,8 @@ static int DetectReplaceMatchTest15(void)
*/
static int DetectReplaceParseTest01(void)
{
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
@ -616,7 +614,7 @@ static int DetectReplaceParseTest01(void)
"alert udp any any -> any any "
"(msg:\"test\"; content:\"doh\"; replace:\"; sid:238012;)"));
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
DetectEngineCtxFree(de_ctx);
PASS;
}
@ -626,8 +624,8 @@ static int DetectReplaceParseTest01(void)
*/
static int DetectReplaceParseTest02(void)
{
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
@ -637,7 +635,7 @@ static int DetectReplaceParseTest02(void)
"alert http any any -> any any "
"(msg:\"test\"; content:\"doh\"; replace:\"bon\"; sid:238012;)"));
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
DetectEngineCtxFree(de_ctx);
PASS;
}
@ -648,8 +646,8 @@ static int DetectReplaceParseTest02(void)
*/
static int DetectReplaceParseTest03(void)
{
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
@ -660,7 +658,7 @@ static int DetectReplaceParseTest03(void)
"alert tcp any any -> any any "
"(msg:\"test\"; content:\"doh\"; replace:\"don\"; http_header; sid:238012;)"));
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
DetectEngineCtxFree(de_ctx);
PASS;
}
@ -670,8 +668,8 @@ static int DetectReplaceParseTest03(void)
*/
static int DetectReplaceParseTest04(void)
{
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
@ -680,7 +678,7 @@ static int DetectReplaceParseTest04(void)
FAIL_IF_NOT_NULL(DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
"(msg:\"test\"; replace:\"don\"; sid:238012;)"));
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
DetectEngineCtxFree(de_ctx);
PASS;
}
@ -690,8 +688,8 @@ static int DetectReplaceParseTest04(void)
*/
static int DetectReplaceParseTest05(void)
{
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
@ -701,7 +699,7 @@ static int DetectReplaceParseTest05(void)
"alert tcp any any -> any any "
"(msg:\"test\"; replace:\"don\"; content:\"doh\"; sid:238012;)"));
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
DetectEngineCtxFree(de_ctx);
PASS;
}
@ -711,8 +709,8 @@ static int DetectReplaceParseTest05(void)
*/
static int DetectReplaceParseTest06(void)
{
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
@ -722,7 +720,7 @@ static int DetectReplaceParseTest06(void)
"alert tcp any any -> any any "
"(msg:\"test\"; content:\"don\"; replace:\"donut\"; sid:238012;)"));
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
DetectEngineCtxFree(de_ctx);
PASS;
}
@ -732,8 +730,8 @@ static int DetectReplaceParseTest06(void)
*/
static int DetectReplaceParseTest07(void)
{
int run_mode_backup = run_mode;
run_mode = RUNMODE_NFQ;
int run_mode_backup = SCRunmodeGet();
SCRunmodeSet(RUNMODE_NFQ);
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
@ -744,7 +742,7 @@ static int DetectReplaceParseTest07(void)
"(msg:\"test\"; content:\"don\"; replace:\"dou\"; "
"content:\"jpg\"; http_header; sid:238012;)"));
run_mode = run_mode_backup;
SCRunmodeSet(run_mode_backup);
DetectEngineCtxFree(de_ctx);
PASS;
}

@ -66,9 +66,6 @@
#include "runmode-unix-socket.h"
/* Run mode selected at suricata.c */
extern int run_mode;
/** queue to pass flows to cleanup/log thread(s) */
FlowQueue flow_recycle_q;

@ -108,9 +108,6 @@ void FlowRegisterTests(void);
void FlowInitFlowProto(void);
int FlowSetProtoFreeFunc(uint8_t, void (*Free)(void *));
/* Run mode selected at suricata.c */
extern int run_mode;
/**
* \brief Update memcap value
*

@ -1091,7 +1091,7 @@ static TmEcode PcapLogDataInit(ThreadVars *t, const void *initdata, void **data)
/* Don't early initialize output files if in a PCAP file (offline)
* mode. */
if (!IsRunModeOffline(RunmodeGetCurrent())) {
if (!IsRunModeOffline(SCRunmodeGet())) {
if (pl->mode == LOGMODE_MULTI) {
PcapLogOpenFileCtx(td->pcap_log);
} else {

@ -1178,8 +1178,7 @@ OutputInitResult OutputJsonInitCtx(ConfNode *conf)
const char *pcapfile_s = ConfNodeLookupChildValue(conf, "pcap-file");
if (pcapfile_s != NULL && ConfValIsTrue(pcapfile_s)) {
json_ctx->file_ctx->is_pcap_offline =
(RunmodeGetCurrent() == RUNMODE_PCAP_FILE ||
RunmodeGetCurrent() == RUNMODE_UNIX_SOCKET);
(SCRunmodeGet() == RUNMODE_PCAP_FILE || SCRunmodeGet() == RUNMODE_UNIX_SOCKET);
}
json_ctx->file_ctx->type = log_filetype;
}

@ -229,7 +229,7 @@ char *RunmodeGetActive(void)
*/
const char *RunModeGetMainMode(void)
{
int mainmode = RunmodeGetCurrent();
int mainmode = SCRunmodeGet();
return RunModeTranslateModeToName(mainmode);
}

@ -171,9 +171,6 @@ SC_ATOMIC_DECLARE(unsigned int, engine_stage);
/** suricata engine control flags */
volatile uint8_t suricata_ctl_flags = 0;
/** Run mode selected */
int run_mode = RUNMODE_UNKNOWN;
/** Engine mode: inline (ENGINE_MODE_IPS) or just
* detection mode (ENGINE_MODE_IDS by default) */
static enum EngineMode g_engine_mode = ENGINE_MODE_UNKNOWN;
@ -256,16 +253,21 @@ void EngineModeSetIDS(void)
#ifdef UNITTESTS
int RunmodeIsUnittests(void)
{
if (run_mode == RUNMODE_UNITTEST)
if (suricata.run_mode == RUNMODE_UNITTEST)
return 1;
return 0;
}
#endif
int RunmodeGetCurrent(void)
int SCRunmodeGet(void)
{
return suricata.run_mode;
}
void SCRunmodeSet(int run_mode)
{
return run_mode;
suricata.run_mode = run_mode;
}
/** signal handlers
@ -2385,8 +2387,6 @@ int SCFinalizeRunMode(void)
default:
break;
}
/* Set the global run mode and offline flag. */
run_mode = suri->run_mode;
if (!CheckValidDaemonModes(suri->daemon, suri->run_mode)) {
return TM_ECODE_FAILED;
@ -2984,7 +2984,7 @@ void SuricataInit(void)
goto out;
}
if (run_mode == RUNMODE_DPDK)
if (suricata.run_mode == RUNMODE_DPDK)
prerun_snap = SystemHugepageSnapshotCreate();
SCSetStartTime(&suricata);
@ -3066,7 +3066,7 @@ void SuricataPostInit(void)
OnNotifyRunning();
PostRunStartedDetectSetup(&suricata);
if (run_mode == RUNMODE_DPDK) { // only DPDK uses hpages at the moment
if (suricata.run_mode == RUNMODE_DPDK) { // only DPDK uses hpages at the moment
SystemHugepageSnapshot *postrun_snap = SystemHugepageSnapshotCreate();
SystemHugepageEvaluateHugepages(prerun_snap, postrun_snap);
SystemHugepageSnapshotDestroy(prerun_snap);

@ -185,11 +185,21 @@ int RunmodeIsUnittests(void);
#else
#define RunmodeIsUnittests() 0
#endif
int RunmodeGetCurrent(void);
int SCRunmodeGet(void);
int SuriHasSigFile(void);
/**
* \brief Get the current run mode.
*/
int SCRunmodeGet(void);
/**
* \brief Set the current run mode.
*
* Mainly exposed outside of suricata.c as a unit-test helper.
*/
void SCRunmodeSet(int run_mode);
extern int run_mode;
int SuriHasSigFile(void);
void SuricataPreInit(const char *progname);
void SuricataInit(void);

@ -86,7 +86,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
setenv("SC_LOG_FILE", "/dev/null", 0);
InitGlobal();
run_mode = RUNMODE_PCAP_FILE;
SCRunmodeSet(RUNMODE_PCAP_FILE);
GlobalsInitPreConfig();
//redirect logs to /tmp

@ -36,7 +36,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
if (alpd_tctx == NULL) {
//global init
InitGlobal();
run_mode = RUNMODE_UNITTEST;
SCRunmodeSet(RUNMODE_UNITTEST);
if (ConfYamlLoadString(configNoChecksum, strlen(configNoChecksum)) != 0) {
abort();
}

@ -21,7 +21,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
setenv("SC_LOG_FILE", "/dev/null", 0);
//global init
InitGlobal();
run_mode = RUNMODE_UNITTEST;
SCRunmodeSet(RUNMODE_UNITTEST);
initialized = 1;
}

@ -43,7 +43,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
setenv("SC_LOG_FILE", "/dev/null", 0);
InitGlobal();
run_mode = RUNMODE_PCAP_FILE;
SCRunmodeSet(RUNMODE_PCAP_FILE);
//redirect logs to /tmp
ConfigSetLogDirectory("/tmp/");

@ -32,7 +32,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
setenv("SC_LOG_FILE", "/dev/null", 0);
//global init
InitGlobal();
run_mode = RUNMODE_UNITTEST;
SCRunmodeSet(RUNMODE_UNITTEST);
initialized = 1;
}

@ -69,7 +69,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
InitGlobal();
GlobalsInitPreConfig();
run_mode = RUNMODE_PCAP_FILE;
SCRunmodeSet(RUNMODE_PCAP_FILE);
// redirect logs to /tmp
ConfigSetLogDirectory("/tmp/");
// disables checksums validation for fuzzing
@ -85,7 +85,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
surifuzz.delayed_detect = 1;
PostConfLoadedSetup(&surifuzz);
PreRunPostPrivsDropInit(run_mode);
PreRunPostPrivsDropInit(SCRunmodeGet());
PostConfLoadedDetectSetup(&surifuzz);
memset(&tv, 0, sizeof(tv));

@ -23,7 +23,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
setenv("SC_LOG_FILE", "/dev/null", 0);
//global init
InitGlobal();
run_mode = RUNMODE_UNITTEST;
SCRunmodeSet(RUNMODE_UNITTEST);
MpmTableSetup();
SpmTableSetup();
EngineModeSetIDS();

@ -63,7 +63,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
InitGlobal();
GlobalsInitPreConfig();
run_mode = RUNMODE_PCAP_FILE;
SCRunmodeSet(RUNMODE_PCAP_FILE);
//redirect logs to /tmp
ConfigSetLogDirectory("/tmp/");
//disables checksums validation for fuzzing
@ -78,7 +78,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
surifuzz.delayed_detect = 1;
PostConfLoadedSetup(&surifuzz);
PreRunPostPrivsDropInit(run_mode);
PreRunPostPrivsDropInit(SCRunmodeGet());
PostConfLoadedDetectSetup(&surifuzz);
memset(&tv, 0, sizeof(tv));

@ -89,7 +89,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
InitGlobal();
GlobalsInitPreConfig();
run_mode = RUNMODE_PCAP_FILE;
SCRunmodeSet(RUNMODE_PCAP_FILE);
// redirect logs to /tmp
ConfigSetLogDirectory("/tmp/");
// disables checksums validation for fuzzing
@ -104,7 +104,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
surifuzz.delayed_detect = 1;
PostConfLoadedSetup(&surifuzz);
PreRunPostPrivsDropInit(run_mode);
PreRunPostPrivsDropInit(SCRunmodeGet());
PostConfLoadedDetectSetup(&surifuzz);
memset(&tv, 0, sizeof(tv));

@ -150,7 +150,7 @@ int ConfUnixSocketIsEnable(void)
#ifdef OS_WIN32
return 0;
#else
if (!IsRunModeOffline(RunmodeGetCurrent())) {
if (!IsRunModeOffline(SCRunmodeGet())) {
SCLogInfo("Running in live mode, activating unix socket");
return 1;
} else {

@ -29,7 +29,7 @@
void DPDKCleanupEAL(void)
{
#ifdef HAVE_DPDK
if (run_mode == RUNMODE_DPDK) {
if (SCRunmodeGet() == RUNMODE_DPDK) {
int retval = rte_eal_cleanup();
if (retval != 0)
SCLogError("EAL cleanup failed: %s", strerror(-retval));
@ -41,7 +41,7 @@ void DPDKCloseDevice(LiveDevice *ldev)
{
(void)ldev; // avoid warnings of unused variable
#ifdef HAVE_DPDK
if (run_mode == RUNMODE_DPDK) {
if (SCRunmodeGet() == RUNMODE_DPDK) {
uint16_t port_id;
int retval = rte_eth_dev_get_port_by_name(ldev->dev, &port_id);
if (retval < 0) {
@ -59,7 +59,7 @@ void DPDKFreeDevice(LiveDevice *ldev)
{
(void)ldev; // avoid warnings of unused variable
#ifdef HAVE_DPDK
if (run_mode == RUNMODE_DPDK) {
if (SCRunmodeGet() == RUNMODE_DPDK) {
SCLogDebug("%s: releasing packet mempool", ldev->dev);
rte_mempool_free(ldev->dpdk_vars.pkt_mp);
}

@ -608,7 +608,7 @@ SCConfLogOpenGeneric(ConfNode *conf,
#ifdef BUILD_WITH_UNIXSOCKET
/* If a socket and running live, do non-blocking writes. */
if (log_ctx->is_sock && !IsRunModeOffline(RunmodeGetCurrent())) {
if (log_ctx->is_sock && !IsRunModeOffline(SCRunmodeGet())) {
SCLogInfo("Setting logging socket of non-blocking in live mode.");
log_ctx->send_flags |= MSG_DONTWAIT;
}

@ -121,7 +121,7 @@ void SCPluginsLoad(const char *capture_plugin_name, const char *capture_plugin_a
}
}
if (run_mode == RUNMODE_PLUGIN) {
if (SCRunmodeGet() == RUNMODE_PLUGIN) {
SCCapturePlugin *capture = SCPluginFindCaptureByName(capture_plugin_name);
if (capture == NULL) {
FatalError("No capture plugin found with name %s", capture_plugin_name);

@ -46,9 +46,6 @@
/** flag indicating if we'll be using caps */
extern bool sc_set_caps;
/** our current runmode */
extern int run_mode;
/**
* \brief Drop the privileges of the main thread
*/
@ -59,7 +56,7 @@ void SCDropMainThreadCaps(uint32_t userid, uint32_t groupid)
capng_clear(CAPNG_SELECT_BOTH);
switch (run_mode) {
switch (SCRunmodeGet()) {
case RUNMODE_PCAP_DEV:
case RUNMODE_AFP_DEV:
case RUNMODE_AFXDP_DEV:

@ -321,7 +321,7 @@ static int SCRConfIsLineBlankOrComment(char *line)
static bool SCRConfParseFile(DetectEngineCtx *de_ctx, FILE *fd)
{
char line[1024];
int runmode = RunmodeGetCurrent();
int runmode = SCRunmodeGet();
bool is_conf_test_mode = runmode == RUNMODE_CONF_TEST;
while (fgets(line, sizeof(line), fd) != NULL) {
if (SCRConfIsLineBlankOrComment(line))

@ -186,7 +186,7 @@ int SCThresholdConfInitContext(DetectEngineCtx *de_ctx)
SCLogWarning("Error loading threshold configuration from %s", filename);
SCThresholdConfDeInitContext(de_ctx, fd);
/* maintain legacy behavior so no errors unless config testing */
if (RunmodeGetCurrent() == RUNMODE_CONF_TEST) {
if (SCRunmodeGet() == RUNMODE_CONF_TEST) {
ret = -1;
}
return ret;
@ -996,7 +996,7 @@ int SCThresholdConfParseFile(DetectEngineCtx *de_ctx, FILE *fp)
esc_pos = SCThresholdConfLineIsMultiline(line);
if (esc_pos == 0) {
if (SCThresholdConfAddThresholdtype(line, de_ctx) < 0) {
if (RunmodeGetCurrent() == RUNMODE_CONF_TEST)
if (SCRunmodeGet() == RUNMODE_CONF_TEST)
return -1;
} else {
SCLogDebug("Adding threshold.config rule num %" PRIu32 "( %s )", rule_num, line);

Loading…
Cancel
Save