From: pcarana Date: Thu, 11 Apr 2019 20:26:48 +0000 (-0500) Subject: Update SLURM loading: X-Git-Tag: v0.0.2~49^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1c6db55dc9b53a97306bcb5741342f2fd3de46ea;p=thirdparty%2FFORT-validator.git Update SLURM loading: -Remove slurm_check_interval configuration parameter since is unnecessary. -Load multiple SLURM files from a configured location (must be a directory). --- diff --git a/src/Makefile.am b/src/Makefile.am index 3375adda..7041508c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,6 +13,7 @@ rtr_server_SOURCES += csv.c csv.h rtr_server_SOURCES += json_parser.c json_parser.h rtr_server_SOURCES += line_file.c line_file.h rtr_server_SOURCES += notify.c notify.h +rtr_server_SOURCES += slurm_loader.c slurm_loader.h rtr_server_SOURCES += slurm_parser.c slurm_parser.h rtr_server_SOURCES += updates_daemon.c updates_daemon.h rtr_server_SOURCES += vrps.c vrps.h diff --git a/src/configuration.c b/src/configuration.c index 422d433c..bc19c0cf 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -24,7 +24,6 @@ #define OPTNAME_RTR_INTERVAL_EXPIRE "expire" #define OPTNAME_SLURM "slurm" #define OPTNAME_SLURM_LOCATION "location" -#define OPTNAME_SLURM_CHECK_INTERVAL "checkInterval" #define DEFAULT_ADDR NULL #define DEFAULT_PORT "323" @@ -35,7 +34,6 @@ #define DEFAULT_RETRY_INTERVAL 600 #define DEFAULT_EXPIRE_INTERVAL 7200 #define DEFAULT_SLURM_LOCATION NULL -#define DEFAULT_SLURM_CHECK_INTERVAL 60 /* Protocol timing parameters ranges in secs */ #define MIN_VRPS_CHECK_INTERVAL 60 @@ -46,8 +44,6 @@ #define MAX_RETRY_INTERVAL 7200 #define MIN_EXPIRE_INTERVAL 600 #define MAX_EXPIRE_INTERVAL 172800 -#define MIN_SLURM_CHECK_INTERVAL 60 -#define MAX_SLURM_CHECK_INTERVAL 7200 /* Range values for other params */ #define MIN_LISTEN_QUEUE 1 @@ -70,8 +66,6 @@ struct rtr_config { int expire_interval; /** SLURM location */ char *slurm_location; - /** Interval used to look for updates at SLURM location */ - int slurm_check_interval; } config; static int handle_json(json_t *); @@ -253,7 +247,6 @@ load_slurm(json_t *root) struct stat attr; json_t *slurm; char const *slurm_location; - int slurm_check_interval; int error; slurm = json_object_get(root, OPTNAME_SLURM); @@ -275,17 +268,8 @@ load_slurm(json_t *root) OPTNAME_SLURM_LOCATION); return -errno; } - - error = load_range(slurm, OPTNAME_SLURM_CHECK_INTERVAL, - DEFAULT_SLURM_CHECK_INTERVAL, &slurm_check_interval, - MIN_SLURM_CHECK_INTERVAL, MAX_SLURM_CHECK_INTERVAL); - if (error) - return error; - config.slurm_check_interval = slurm_check_interval; - } else { + } else config.slurm_location = DEFAULT_SLURM_LOCATION; - config.slurm_check_interval = DEFAULT_SLURM_CHECK_INTERVAL; - } /* Validate data (only if a value was set */ if (config.slurm_location == NULL) @@ -297,8 +281,8 @@ load_slurm(json_t *root) config.slurm_location); return -errno; } - if (S_ISDIR(attr.st_mode) != 0) { - warnx("SLURM location '%s' isn't a file", + if (S_ISDIR(attr.st_mode) == 0) { + warnx("SLURM location '%s' isn't a directory", config.slurm_location); return -EINVAL; } @@ -489,9 +473,3 @@ config_get_slurm_location(void) { return config.slurm_location; } - -int -config_get_slurm_check_interval(void) -{ - return config.slurm_check_interval; -} diff --git a/src/configuration.h b/src/configuration.h index 196a032d..1fe7f1d0 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -16,6 +16,5 @@ int config_get_retry_interval(void); int config_get_expire_interval(void); char const *config_get_slurm_location(void); -int config_get_slurm_check_interval(void); #endif /* _SRC_CONFIGURATION_H_ */ diff --git a/src/main.c b/src/main.c index 1f13a885..8d02dfff 100644 --- a/src/main.c +++ b/src/main.c @@ -4,7 +4,7 @@ #include #include "rtr/rtr.h" -#include "slurm_parser.h" +#include "slurm_loader.h" #include "clients.h" #include "configuration.h" #include "csv.h" diff --git a/src/slurm_loader.c b/src/slurm_loader.c new file mode 100644 index 00000000..6a56fac6 --- /dev/null +++ b/src/slurm_loader.c @@ -0,0 +1,93 @@ +#include "slurm_loader.h" + +#include +#include +#include +#include +#include + +#include "configuration.h" +#include "slurm_parser.h" + +#define SLURM_FILE_EXTENSION ".slurm" + +static int +single_slurm_load(const char *dir_name, const char *file_name) +{ + char *ext, *fullpath, *tmp; + int error; + + ext = strrchr(file_name, '.'); + /* Ignore file if extension isn't the expected */ + if (ext == NULL || strcmp(ext, SLURM_FILE_EXTENSION) != 0) + return 0; + + /* Get the full file path */ + tmp = strdup(dir_name); + if (tmp == NULL) { + warn("Couldn't create temporal char for SLURM"); + return -errno; + } + tmp = realloc(tmp, strlen(tmp) + 1 + strlen(file_name) + 1); + if (tmp == NULL) { + warn("Couldn't reallocate temporal char for SLURM"); + return -errno; + } + + strcat(tmp, "/"); + strcat(tmp, file_name); + fullpath = realpath(tmp, NULL); + if (fullpath == NULL) { + warn("Error getting real path for file '%s' at dir '%s'", + dir_name, file_name); + free(tmp); + return -errno; + } + + error = slurm_parse(fullpath); + free(tmp); + free(fullpath); + return error; +} + +int +slurm_load(void) +{ + DIR *dir_loc; + struct dirent *dir_ent; + char const *slurm_dir; + int error; + + /* Optional configuration */ + slurm_dir = config_get_slurm_location(); + if (slurm_dir == NULL) + return 0; + + dir_loc = opendir(slurm_dir); + if (dir_loc == NULL) { + warn("Couldn't open dir %s", slurm_dir); + return -errno; + } + + error = 0; + errno = 0; + while ((dir_ent = readdir(dir_loc)) != NULL) { + error = single_slurm_load(slurm_dir, dir_ent->d_name); + if (error) + goto end; + errno = 0; + } + if (errno) { + warn("Error reading dir %s", slurm_dir); + error = -errno; + } +end: + closedir(dir_loc); + return error; +} + +void +slurm_cleanup(void) +{ + /* TODO Nothing for now */ +} diff --git a/src/slurm_loader.h b/src/slurm_loader.h new file mode 100644 index 00000000..fdef9ca1 --- /dev/null +++ b/src/slurm_loader.h @@ -0,0 +1,7 @@ +#ifndef SRC_SLURM_LOADER_H_ +#define SRC_SLURM_LOADER_H_ + +int slurm_load(void); +void slurm_cleanup(void); + +#endif /* SRC_SLURM_LOADER_H_ */ diff --git a/src/slurm_parser.c b/src/slurm_parser.c index 190f52f9..85665f95 100644 --- a/src/slurm_parser.c +++ b/src/slurm_parser.c @@ -8,7 +8,6 @@ #include #include "address.h" -#include "configuration.h" #include "crypto/base64.h" #include "json_parser.h" @@ -61,18 +60,14 @@ struct slurm_bgpsec { static int handle_json(json_t *); int -slurm_load(void) +slurm_parse(char const *location) { json_t *json_root; json_error_t json_error; int error; - /* Optional configuration */ - if (config_get_slurm_location() == NULL) - return 0; - - json_root = json_load_file(config_get_slurm_location(), - JSON_REJECT_DUPLICATES, &json_error); + json_root = json_load_file(location, JSON_REJECT_DUPLICATES, + &json_error); if (json_root == NULL) { warnx("SLURM JSON error on line %d, column %d: %s", json_error.line, json_error.column, json_error.text); @@ -85,12 +80,6 @@ slurm_load(void) return error; } -void -slurm_cleanup(void) -{ - /* TODO Nothing for now */ -} - /* * TODO Maybe some of the parsing functions can be on a common place, since * csv.c also does a similar parsing diff --git a/src/slurm_parser.h b/src/slurm_parser.h index 208ef4c6..290d5543 100644 --- a/src/slurm_parser.h +++ b/src/slurm_parser.h @@ -13,7 +13,7 @@ #define SLURM_BGPS_FLAG_ROUTER_KEY 0x08 -int slurm_load(void); -void slurm_cleanup(void); +int slurm_parse(char const *); + #endif /* SRC_SLURM_PARSER_H_ */