From: pcarana Date: Mon, 25 Mar 2019 23:10:06 +0000 (-0600) Subject: Return success when the VRPs location doesn't exists (useful for cron) X-Git-Tag: v0.0.2~52^2~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=df36ac7f2efbba51be14b476305788082bc15464;p=thirdparty%2FFORT-validator.git Return success when the VRPs location doesn't exists (useful for cron) --- diff --git a/src/configuration.c b/src/configuration.c index aa821442..ad4fc2f3 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -1,6 +1,7 @@ #include "configuration.h" #include +#include #include #include #include @@ -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); } diff --git a/src/main.c b/src/main.c index 7715cfb8..bf1be53f 100644 --- a/src/main.c +++ b/src/main.c @@ -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)