]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
More concise API for setting config values that
authorJason Ish <jason.ish@emulex.com>
Thu, 21 Nov 2013 19:36:45 +0000 (13:36 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 4 Dec 2013 10:21:52 +0000 (11:21 +0100)
can be overrided or not (final values).

src/conf-yaml-loader.c
src/conf.c
src/conf.h
src/defrag.c
src/runmode-unix-socket.c
src/suricata.c
src/util-conf.c

index c97ec0550fa9c47f309230e2c96ae83e2998f081..9442caeac618fc9543b3991d6497f44250d437ae 100644 (file)
@@ -271,7 +271,7 @@ ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq)
                         if (ConfYamlHandleInclude(node, value) != 0)
                             goto fail;
                     }
-                    else if (node->allow_override) {
+                    else if (!node->final) {
                         if (node->val != NULL)
                             SCFree(node->val);
                         node->val = SCStrdup(value);
index 82e1879f18f3aa0b02ce76a9bd65b7a66e2bab76..61fd29488661fe27af1a5b8e627174763cb0844b 100644 (file)
@@ -131,8 +131,6 @@ ConfNodeNew(void)
     if (unlikely(new == NULL)) {
         return NULL;
     }
-    /* By default we allow an override. */
-    new->allow_override = 1;
     TAILQ_INIT(&new->head);
 
     return new;
@@ -215,16 +213,29 @@ ConfGetRootNode(void)
  * \retval 1 if the value was set otherwise 0.
  */
 int
-ConfSet(char *name, char *val, int allow_override)
+ConfSet(char *name, char *val)
 {
     ConfNode *node = ConfGetNodeOrCreate(name);
-    if (node == NULL || !node->allow_override) {
+    if (node == NULL || node->final) {
         return 0;
     }
     if (node->val != NULL)
         SCFree(node->val);
     node->val = SCStrdup(val);
-    node->allow_override = allow_override;
+    return 1;
+}
+
+int
+ConfSetFinal(char *name, char *val)
+{
+    ConfNode *node = ConfGetNodeOrCreate(name);
+    if (node == NULL) {
+        return 0;
+    }
+    if (node->val != NULL)
+        SCFree(node->val);
+    node->val = SCStrdup(val);
+    node->final = 1;
     return 1;
 }
 
@@ -786,7 +797,7 @@ ConfTestSetAndGet(void)
     char value[] = "some-value";
     char *value0;
 
-    if (ConfSet(name, value, 1) != 1)
+    if (ConfSet(name, value) != 1)
         return 0;
     if (ConfGet(name, &value0) != 1)
         return 0;
@@ -812,9 +823,9 @@ ConfTestOverrideValue1(void)
     char *val;
     int rc;
 
-    if (ConfSet(name, value0, 1) != 1)
+    if (ConfSet(name, value0) != 1)
         return 0;
-    if (ConfSet(name, value1, 1) != 1)
+    if (ConfSet(name, value1) != 1)
         return 0;
     if (ConfGet(name, &val) != 1)
         return 0;
@@ -828,8 +839,7 @@ ConfTestOverrideValue1(void)
 }
 
 /**
- * Test that overriding a value is not allowed provided that
- * allow_override is false and make sure the value was not overrided.
+ * Test that a final value will not be overrided by a ConfSet.
  */
 static int
 ConfTestOverrideValue2(void)
@@ -840,9 +850,9 @@ ConfTestOverrideValue2(void)
     char *val;
     int rc;
 
-    if (ConfSet(name, value0, 0) != 1)
+    if (ConfSetFinal(name, value0) != 1)
         return 0;
-    if (ConfSet(name, value1, 1) != 0)
+    if (ConfSet(name, value1) != 0)
         return 0;
     if (ConfGet(name, &val) != 1)
         return 0;
@@ -864,7 +874,7 @@ ConfTestGetInt(void)
     char name[] = "some-int.x";
     intmax_t val;
 
-    if (ConfSet(name, "0", 1) != 1)
+    if (ConfSet(name, "0") != 1)
         return 0;
     if (ConfGetInt(name, &val) != 1)
         return 0;
@@ -872,21 +882,21 @@ ConfTestGetInt(void)
     if (val != 0)
         return 0;
 
-    if (ConfSet(name, "-1", 1) != 1)
+    if (ConfSet(name, "-1") != 1)
         return 0;
     if (ConfGetInt(name, &val) != 1)
         return 0;
     if (val != -1)
         return 0;
 
-    if (ConfSet(name, "0xffff", 1) != 1)
+    if (ConfSet(name, "0xffff") != 1)
         return 0;
     if (ConfGetInt(name, &val) != 1)
         return 0;
     if (val != 0xffff)
         return 0;
 
-    if (ConfSet(name, "not-an-int", 1) != 1)
+    if (ConfSet(name, "not-an-int") != 1)
         return 0;
     if (ConfGetInt(name, &val) != 0)
         return 0;
@@ -918,7 +928,7 @@ ConfTestGetBool(void)
     size_t u;
 
     for (u = 0; u < sizeof(trues) / sizeof(trues[0]); u++) {
-        if (ConfSet(name, trues[u], 1) != 1)
+        if (ConfSet(name, trues[u]) != 1)
             return 0;
         if (ConfGetBool(name, &val) != 1)
             return 0;
@@ -927,7 +937,7 @@ ConfTestGetBool(void)
     }
 
     for (u = 0; u < sizeof(falses) / sizeof(falses[0]); u++) {
-        if (ConfSet(name, falses[u], 1) != 1)
+        if (ConfSet(name, falses[u]) != 1)
             return 0;
         if (ConfGetBool(name, &val) != 1)
             return 0;
@@ -1037,9 +1047,9 @@ static int ConfGetChildValueWithDefaultTest(void)
     int ret = 1;
     ConfCreateContextBackup();
     ConfInit();
-    ConfSet("af-packet.0.interface", "eth0", 1);
-    ConfSet("af-packet.1.interface", "default", 1);
-    ConfSet("af-packet.1.cluster-type", "cluster_cpu", 1);
+    ConfSet("af-packet.0.interface", "eth0");
+    ConfSet("af-packet.1.interface", "default");
+    ConfSet("af-packet.1.cluster-type", "cluster_cpu");
 
     ConfNode *root = ConfGetNode("af-packet.0");
     ConfNode *dflt = ConfGetNode("af-packet.1");
@@ -1050,7 +1060,7 @@ static int ConfGetChildValueWithDefaultTest(void)
         return 0;
     }
 
-    ConfSet("af-packet.0.cluster-type", "cluster_flow", 1);
+    ConfSet("af-packet.0.cluster-type", "cluster_flow");
     ConfGetChildValueWithDefault(root, dflt, "cluster-type", &val);
 
     if (strcmp(val, "cluster_flow")) {
@@ -1066,9 +1076,9 @@ static int ConfGetChildValueIntWithDefaultTest(void)
     intmax_t val;
     ConfCreateContextBackup();
     ConfInit();
-    ConfSet("af-packet.0.interface", "eth0", 1);
-    ConfSet("af-packet.1.interface", "default", 1);
-    ConfSet("af-packet.1.threads", "2", 1);
+    ConfSet("af-packet.0.interface", "eth0");
+    ConfSet("af-packet.1.interface", "default");
+    ConfSet("af-packet.1.threads", "2");
 
     ConfNode *root = ConfGetNode("af-packet.0");
     ConfNode *dflt = ConfGetNode("af-packet.1");
@@ -1079,7 +1089,7 @@ static int ConfGetChildValueIntWithDefaultTest(void)
         return 0;
     }
 
-    ConfSet("af-packet.0.threads", "1", 1);
+    ConfSet("af-packet.0.threads", "1");
     ConfGetChildValueIntWithDefault(root, dflt, "threads", &val);
 
     ConfDeInit();
@@ -1095,9 +1105,9 @@ static int ConfGetChildValueBoolWithDefaultTest(void)
     int val;
     ConfCreateContextBackup();
     ConfInit();
-    ConfSet("af-packet.0.interface", "eth0", 1);
-    ConfSet("af-packet.1.interface", "default", 1);
-    ConfSet("af-packet.1.use-mmap", "yes", 1);
+    ConfSet("af-packet.0.interface", "eth0");
+    ConfSet("af-packet.1.interface", "default");
+    ConfSet("af-packet.1.use-mmap", "yes");
 
     ConfNode *root = ConfGetNode("af-packet.0");
     ConfNode *dflt = ConfGetNode("af-packet.1");
@@ -1108,7 +1118,7 @@ static int ConfGetChildValueBoolWithDefaultTest(void)
         return 0;
     }
 
-    ConfSet("af-packet.0.use-mmap", "no", 1);
+    ConfSet("af-packet.0.use-mmap", "no");
     ConfGetChildValueBoolWithDefault(root, dflt, "use-mmap", &val);
 
     ConfDeInit();
@@ -1128,7 +1138,7 @@ ConfNodeRemoveTest(void)
     ConfCreateContextBackup();
     ConfInit();
 
-    if (ConfSet("some.nested.parameter", "blah", 1) != 1)
+    if (ConfSet("some.nested.parameter", "blah") != 1)
         return 0;
 
     ConfNode *node = ConfGetNode("some.nested.parameter");
@@ -1153,7 +1163,7 @@ ConfSetTest(void)
     ConfInit();
 
     /* Set some value with 2 levels. */
-    if (ConfSet("one.two", "three", 1) != 1)
+    if (ConfSet("one.two", "three") != 1)
         return 0;
     ConfNode *n = ConfGetNode("one.two");
     if (n == NULL)
@@ -1162,7 +1172,7 @@ ConfSetTest(void)
     /* Set another 2 level parameter with the same first level, this
      * used to trigger a bug that caused the second level of the name
      * to become a first level node. */
-    if (ConfSet("one.three", "four", 1) != 1)
+    if (ConfSet("one.three", "four") != 1)
         return 0;
 
     n = ConfGetNode("one.three");
index c9a089f771bca184b4e5ad3c83339eedcd902891..86caeaa8f4f06d78754ec6212f84c927747c394c 100644 (file)
@@ -34,7 +34,9 @@ typedef struct ConfNode_ {
     char *val;
 
     int is_seq;
-    int allow_override;
+
+    /**< Flag that sets this nodes value as final. */
+    int final;
 
     struct ConfNode_ *parent;
     TAILQ_HEAD(, ConfNode_) head;
@@ -59,7 +61,8 @@ int ConfGetInt(char *name, intmax_t *val);
 int ConfGetBool(char *name, int *val);
 int ConfGetDouble(char *name, double *val);
 int ConfGetFloat(char *name, float *val);
-int ConfSet(char *name, char *val, int allow_override);
+int ConfSet(char *name, char *val);
+int ConfSetFinal(char *name, char *val);
 void ConfDump(void);
 void ConfNodeDump(ConfNode *node, const char *prefix);
 ConfNode *ConfNodeNew(void);
index 01d8c6c907c6880a4bd3f9c5a7f548bc1a0d2487..bd2c6e854b80bd57b6c1a6f5f2cf5316289ee712 100644 (file)
@@ -2040,7 +2040,7 @@ DefragTimeoutTest(void)
     int ret = 0;
 
     /* Setup a small numberr of trackers. */
-    if (ConfSet("defrag.trackers", "16", 1) != 1) {
+    if (ConfSet("defrag.trackers", "16") != 1) {
         printf("ConfSet failed: ");
         goto end;
     }
index f1e2b7e825099bdb613eb5a82f1a429df0317b6c..fd198c84ff4291147bbc2a477f86fc37de091cff 100644 (file)
@@ -307,13 +307,13 @@ TmEcode UnixSocketPcapFilesCheck(void *data)
         SCLogInfo("Starting run for '%s'", cfile->filename);
         unix_manager_file_task_running = 1;
         this->running = 1;
-        if (ConfSet("pcap-file.file", cfile->filename, 1) != 1) {
+        if (ConfSet("pcap-file.file", cfile->filename) != 1) {
             SCLogInfo("Can not set working file to '%s'", cfile->filename);
             PcapFilesFree(cfile);
             return TM_ECODE_FAILED;
         }
         if (cfile->output_dir) {
-            if (ConfSet("default-log-dir", cfile->output_dir, 1) != 1) {
+            if (ConfSet("default-log-dir", cfile->output_dir) != 1) {
                 SCLogInfo("Can not set output dir to '%s'", cfile->output_dir);
                 PcapFilesFree(cfile);
                 return TM_ECODE_FAILED;
index a569bafa94ba20c0c4125cd01da6028be32fe7fe..32c7157cd9784f30da8f7d8db67061069fa1ba41 100644 (file)
@@ -389,7 +389,7 @@ static int SetBpfString(int optind, char *argv[]) {
     }
 
     if(strlen(bpf_filter) > 0) {
-        if (ConfSet("bpf-filter", bpf_filter, 0) != 1) {
+        if (ConfSetFinal("bpf-filter", bpf_filter) != 1) {
             SCLogError(SC_ERR_FATAL, "Failed to set bpf filter.");
             return TM_ECODE_FAILED;
         }
@@ -459,7 +459,7 @@ static void SetBpfStringFromFile(char *filename) {
         while((bpf_comment_tmp = strchr(bpf_filter, '\n')) != NULL) {
             *bpf_comment_tmp = ' ';
         }
-        if(ConfSet("bpf-filter", bpf_filter, 0) != 1) {
+        if(ConfSetFinal("bpf-filter", bpf_filter) != 1) {
             SCLogError(SC_ERR_FOPEN, "ERROR: Failed to set bpf filter!");
             SCFree(bpf_filter);
             exit(EXIT_FAILURE);
@@ -873,7 +873,7 @@ static TmEcode ParseInterfacesList(int run_mode, char *pcap_dev)
 #ifdef HAVE_MPIPE
     } else if (run_mode == RUNMODE_TILERA_MPIPE) {
         if (strlen(pcap_dev)) {
-            if (ConfSet("mpipe.single_mpipe_dev", pcap_dev, 0) != 1) {
+            if (ConfSetFinal("mpipe.single_mpipe_dev", pcap_dev) != 1) {
                 fprintf(stderr, "ERROR: Failed to set mpipe.single_mpipe_dev\n");
                 SCReturnInt(TM_ECODE_FAILED);
             }
@@ -889,7 +889,7 @@ static TmEcode ParseInterfacesList(int run_mode, char *pcap_dev)
         /* FIXME add backward compat support */
         /* iface has been set on command line */
         if (strlen(pcap_dev)) {
-            if (ConfSet("pfring.live-interface", pcap_dev, 0) != 1) {
+            if (ConfSetFinal("pfring.live-interface", pcap_dev) != 1) {
                 SCLogError(SC_ERR_INITIALIZATION, "Failed to set pfring.live-interface");
                 SCReturnInt(TM_ECODE_FAILED);
             }
@@ -900,7 +900,7 @@ static TmEcode ParseInterfacesList(int run_mode, char *pcap_dev)
     } else if (run_mode == RUNMODE_AFP_DEV) {
         /* iface has been set on command line */
         if (strlen(pcap_dev)) {
-            if (ConfSet("af-packet.live-interface", pcap_dev, 0) != 1) {
+            if (ConfSetFinal("af-packet.live-interface", pcap_dev) != 1) {
                 SCLogError(SC_ERR_INITIALIZATION, "Failed to set af-packet.live-interface");
                 SCReturnInt(TM_ECODE_FAILED);
             }
@@ -1073,7 +1073,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
             }
             else if(strcmp((long_opts[option_index]).name , "pfring-cluster-id") == 0){
 #ifdef HAVE_PFRING
-                if (ConfSet("pfring.cluster-id", optarg, 0) != 1) {
+                if (ConfSetFinal("pfring.cluster-id", optarg) != 1) {
                     fprintf(stderr, "ERROR: Failed to set pfring.cluster-id.\n");
                     return TM_ECODE_FAILED;
                 }
@@ -1085,7 +1085,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
             }
             else if(strcmp((long_opts[option_index]).name , "pfring-cluster-type") == 0){
 #ifdef HAVE_PFRING
-                if (ConfSet("pfring.cluster-type", optarg, 0) != 1) {
+                if (ConfSetFinal("pfring.cluster-type", optarg) != 1) {
                     fprintf(stderr, "ERROR: Failed to set pfring.cluster-type.\n");
                     return TM_ECODE_FAILED;
                 }
@@ -1154,7 +1154,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
                     return TM_ECODE_FAILED;
                 }
             } else if(strcmp((long_opts[option_index]).name, "init-errors-fatal") == 0) {
-                if (ConfSet("engine.init-failure-fatal", "1", 0) != 1) {
+                if (ConfSetFinal("engine.init-failure-fatal", "1") != 1) {
                     fprintf(stderr, "ERROR: Failed to set engine init-failure-fatal.\n");
                     return TM_ECODE_FAILED;
                 }
@@ -1163,7 +1163,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
                 if (suri->run_mode == RUNMODE_UNKNOWN) {
                     suri->run_mode = RUNMODE_UNIX_SOCKET;
                     if (optarg) {
-                        if (ConfSet("unix-command.filename", optarg, 0) != 1) {
+                        if (ConfSetFinal("unix-command.filename", optarg) != 1) {
                             fprintf(stderr, "ERROR: Failed to set unix-command.filename.\n");
                             return TM_ECODE_FAILED;
                         }
@@ -1226,7 +1226,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
             }
             else if(strcmp((long_opts[option_index]).name, "fatal-unittests") == 0) {
 #ifdef UNITTESTS
-                if (ConfSet("unittests.failure-fatal", "1", 0) != 1) {
+                if (ConfSetFinal("unittests.failure-fatal", "1") != 1) {
                     fprintf(stderr, "ERROR: Failed to set unittests failure-fatal.\n");
                     return TM_ECODE_FAILED;
                 }
@@ -1257,7 +1257,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
             }
             else if (strcmp((long_opts[option_index]).name, "erf-in") == 0) {
                 suri->run_mode = RUNMODE_ERF_FILE;
-                if (ConfSet("erf-file.file", optarg, 0) != 1) {
+                if (ConfSetFinal("erf-file.file", optarg) != 1) {
                     fprintf(stderr, "ERROR: Failed to set erf-file.file\n");
                     return TM_ECODE_FAILED;
                 }
@@ -1291,7 +1291,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
                        }
             else if(strcmp((long_opts[option_index]).name, "pcap-buffer-size") == 0) {
 #ifdef HAVE_PCAP_SET_BUFF
-                if (ConfSet("pcap.buffer-size", optarg, 0) != 1) {
+                if (ConfSetFinal("pcap.buffer-size", optarg) != 1) {
                     fprintf(stderr, "ERROR: Failed to set pcap-buffer-size.\n");
                     return TM_ECODE_FAILED;
                 }
@@ -1330,7 +1330,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
         case 'T':
             SCLogInfo("Running suricata under test mode");
             conf_test = 1;
-            if (ConfSet("engine.init-failure-fatal", "1", 0) != 1) {
+            if (ConfSetFinal("engine.init-failure-fatal", "1") != 1) {
                 fprintf(stderr, "ERROR: Failed to set engine init-failure-fatal.\n");
                 return TM_ECODE_FAILED;
             }
@@ -1455,7 +1455,7 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
                 usage(argv[0]);
                 return TM_ECODE_FAILED;
             }
-            if (ConfSet("pcap-file.file", optarg, 0) != 1) {
+            if (ConfSetFinal("pcap-file.file", optarg) != 1) {
                 fprintf(stderr, "ERROR: Failed to set pcap-file.file\n");
                 return TM_ECODE_FAILED;
             }
index e74b1ca18df4551bcc8d33e028623873383482a1..e0f25d1d6395f3ee7b9f06eac48f2e36cd58dd82 100644 (file)
@@ -28,7 +28,7 @@
 
 TmEcode ConfigSetLogDirectory(char *name)
 {
-    return ConfSet("default-log-dir", name, 0) ? TM_ECODE_OK : TM_ECODE_FAILED;
+    return ConfSetFinal("default-log-dir", name) ? TM_ECODE_OK : TM_ECODE_FAILED;
 }
 
 char *ConfigGetLogDirectory()