From b4080fb52b70406b1ebb3c0ffa09c836fbf2bd7f Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Tue, 1 Feb 2022 13:49:04 +0100 Subject: [PATCH] Return error from nvme_read_config() nvme_read_config() can fail, so we should be returning an error code to inform the caller about it. Signed-off-by: Hannes Reinecke --- src/nvme/json.c | 15 ++++++++++----- src/nvme/private.h | 2 +- src/nvme/tree.c | 11 ++++++++--- src/nvme/tree.h | 4 +++- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/nvme/json.c b/src/nvme/json.c index ebc1ae4f..c4375d9f 100644 --- a/src/nvme/json.c +++ b/src/nvme/json.c @@ -126,7 +126,8 @@ static void json_parse_subsys(nvme_host_t h, struct json_object *subsys_obj) struct json_object *port_obj; port_obj = json_object_array_get_idx(port_array, p); - json_parse_port(s, port_obj); + if (port_obj) + json_parse_port(s, port_obj); } } @@ -153,11 +154,12 @@ static void json_parse_host(nvme_root_t r, struct json_object *host_obj) return; for (s = 0; s < json_object_array_length(subsys_array); s++) { subsys_obj = json_object_array_get_idx(subsys_array, s); - json_parse_subsys(h, subsys_obj); + if (subsys_obj) + json_parse_subsys(h, subsys_obj); } } -void json_read_config(nvme_root_t r, const char *config_file) +int json_read_config(nvme_root_t r, const char *config_file) { struct json_object *json_root, *host_obj; int h; @@ -166,13 +168,16 @@ void json_read_config(nvme_root_t r, const char *config_file) if (!json_root) { nvme_msg(r, LOG_DEBUG, "Failed to read %s, %s\n", config_file, json_util_get_last_err()); - return; + errno = EAGAIN; + return -1; } for (h = 0; h < json_object_array_length(json_root); h++) { host_obj = json_object_array_get_idx(json_root, h); - json_parse_host(r, host_obj); + if (host_obj) + json_parse_host(r, host_obj); } json_object_put(json_root); + return 0; } #define JSON_STRING_OPTION(c, p, o) \ diff --git a/src/nvme/private.h b/src/nvme/private.h index 945b453a..356cae30 100644 --- a/src/nvme/private.h +++ b/src/nvme/private.h @@ -129,7 +129,7 @@ struct nvme_root { int nvme_set_attr(const char *dir, const char *attr, const char *value); -void json_read_config(nvme_root_t r, const char *config_file); +int json_read_config(nvme_root_t r, const char *config_file); int json_update_config(nvme_root_t r, const char *config_file); diff --git a/src/nvme/tree.c b/src/nvme/tree.c index a2fe5335..0acc045e 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -137,14 +137,19 @@ nvme_root_t nvme_create_root(FILE *fp, int log_level) return r; } -void nvme_read_config(nvme_root_t r, const char *config_file) +int nvme_read_config(nvme_root_t r, const char *config_file) { + int err = -1; #ifdef CONFIG_JSONC if (r && config_file) { - json_read_config(r, config_file); - r->config_file = strdup(config_file); + err = json_read_config(r, config_file); + if (!err) + r->config_file = strdup(config_file); } +#else + errno = ENOTSUP; #endif + return err; } nvme_root_t nvme_scan(const char *config_file) diff --git a/src/nvme/tree.h b/src/nvme/tree.h index 00b75b00..1f9703fb 100644 --- a/src/nvme/tree.h +++ b/src/nvme/tree.h @@ -1040,8 +1040,10 @@ nvme_root_t nvme_scan(const char *config_file); * * Read in the contents of @config_file and merge them with * the elements in @r. + * + * Returns: 0 on success, -1 on failure with errno set. */ -void nvme_read_config(nvme_root_t r, const char *config_file); +int nvme_read_config(nvme_root_t r, const char *config_file); /** * nvme_refresh_topology() - -- 2.50.1