]> git.ipfire.org Git - thirdparty/FORT-validator.git/commitdiff
Return success when the VRPs location doesn't exists (useful for cron)
authorpcarana <pc.moreno2099@gmail.com>
Mon, 25 Mar 2019 23:10:06 +0000 (17:10 -0600)
committerpcarana <pc.moreno2099@gmail.com>
Mon, 25 Mar 2019 23:10:06 +0000 (17:10 -0600)
src/configuration.c
src/main.c

index aa82144225b38b41f47a85bf43c994d4282b5a6c..ad4fc2f359d598fbe96c463629b3851449a58798 100644 (file)
@@ -1,6 +1,7 @@
 #include "configuration.h"
 
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <err.h>
 #include <errno.h>
 #include <jansson.h>
@@ -124,57 +125,14 @@ load_range(json_t *parent, char const *name, int default_value,
 }
 
 static int
-handle_json(json_t *root)
+load_vrps(json_t *root)
 {
-       json_t *listen;
+       struct stat attr;
        json_t *vrps;
-       json_t *interval;
-       char const *address;
-       char const *port;
        char const *vrps_location;
-       int queue;
        int vrps_check_interval;
-       int refresh_interval;
-       int retry_interval;
-       int expire_interval;
        int error;
 
-       if (!json_is_object(root)) {
-               warnx("The root of the JSON file is not a JSON object.");
-               return -EINVAL;
-       }
-
-       listen = json_object_get(root, OPTNAME_LISTEN);
-       if (listen != NULL) {
-               if (!json_is_object(listen)) {
-                       warnx("The '%s' element is not a JSON object.",
-                           OPTNAME_LISTEN);
-                       return -EINVAL;
-               }
-
-               error = json_get_string(listen, OPTNAME_LISTEN_ADDRESS,
-                   DEFAULT_ADDR, &address);
-               if (error)
-                       return error;
-
-               error = json_get_string(listen, OPTNAME_LISTEN_PORT,
-                   DEFAULT_PORT, &port);
-               if (error)
-                       return error;
-
-               error = load_range(listen, OPTNAME_LISTEN_QUEUE,
-                   DEFAULT_QUEUE, &queue,
-                   MIN_LISTEN_QUEUE, MAX_LISTEN_QUEUE);
-               if (error)
-                       return error;
-               config.queue = queue;
-
-       } else {
-               address = DEFAULT_ADDR;
-               port = DEFAULT_PORT;
-               config.queue = DEFAULT_QUEUE;
-       }
-
        vrps = json_object_get(root, OPTNAME_VRPS);
        if (vrps != NULL) {
                if (!json_is_object(vrps)) {
@@ -192,7 +150,7 @@ handle_json(json_t *root)
                if (config.vrps_location == NULL) {
                        warn("'%s' couldn't be allocated.",
                            OPTNAME_VRPS_LOCATION);
-                       return errno;
+                       return -errno;
                }
 
                /*
@@ -211,10 +169,33 @@ handle_json(json_t *root)
                config.vrps_check_interval = DEFAULT_VRPS_CHECK_INTERVAL;
        }
 
-       /*
-        * Exclusively for RTR v1, so this are optional values to configure
-        * since RTR v1 isn't fully supported yet
-        */
+       /* Validate required data */
+       error = stat(config.vrps_location, &attr) < 0;
+       if (error) {
+               warn("VRPs location '%s' isn't a valid path",
+                   config.vrps_location);
+               return -errno;
+       }
+       if (S_ISDIR(attr.st_mode) != 0) {
+               warnx("VRPs location '%s' isn't a file", config.vrps_location);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+/*
+ * Exclusively for RTR v1, so this are optional values to configure
+ * since RTR v1 isn't fully supported yet
+ */
+static int
+load_intervals(json_t *root)
+{
+       json_t *interval;
+       int refresh_interval;
+       int retry_interval;
+       int expire_interval;
+       int error;
        interval = json_object_get(root, OPTNAME_RTR_INTERVAL);
        if (interval != NULL) {
                if (!json_is_object(interval)) {
@@ -250,6 +231,62 @@ handle_json(json_t *root)
                config.expire_interval = DEFAULT_EXPIRE_INTERVAL;
        }
 
+       return 0;
+}
+
+static int
+handle_json(json_t *root)
+{
+       json_t *listen;
+       char const *address;
+       char const *port;
+       int queue;
+       int error;
+
+       if (!json_is_object(root)) {
+               warnx("The root of the JSON file is not a JSON object.");
+               return -EINVAL;
+       }
+
+       listen = json_object_get(root, OPTNAME_LISTEN);
+       if (listen != NULL) {
+               if (!json_is_object(listen)) {
+                       warnx("The '%s' element is not a JSON object.",
+                           OPTNAME_LISTEN);
+                       return -EINVAL;
+               }
+
+               error = json_get_string(listen, OPTNAME_LISTEN_ADDRESS,
+                   DEFAULT_ADDR, &address);
+               if (error)
+                       return error;
+
+               error = json_get_string(listen, OPTNAME_LISTEN_PORT,
+                   DEFAULT_PORT, &port);
+               if (error)
+                       return error;
+
+               error = load_range(listen, OPTNAME_LISTEN_QUEUE,
+                   DEFAULT_QUEUE, &queue,
+                   MIN_LISTEN_QUEUE, MAX_LISTEN_QUEUE);
+               if (error)
+                       return error;
+               config.queue = queue;
+
+       } else {
+               address = DEFAULT_ADDR;
+               port = DEFAULT_PORT;
+               config.queue = DEFAULT_QUEUE;
+       }
+
+       error = load_vrps(root);
+       if (error)
+               return error;
+
+       error = load_intervals(root);
+       if (error)
+               return error;
+
        return init_addrinfo(address, port);
 }
 
index 7715cfb8e7323bbd2539eb61905200194476be28..bf1be53f3f4f14ee4b83b561a74327f9ec993f78 100644 (file)
@@ -42,8 +42,16 @@ main(int argc, char *argv[])
        }
 
        err = config_init(json_file);
-       if (err)
+       if (err) {
+               /*
+                * TODO Special scenario, if the VRPs location doesn't exists
+                * just send a warning (logged by the config_init function).
+                *
+                * This should be fixed later.
+                */
+               err = (err == -ENOENT ? 0 : err);
                goto end1;
+       }
 
        err = deltas_db_init();
        if (err)