Skip to content

Commit b4080fb

Browse files
committed
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 <[email protected]>
1 parent 5b397aa commit b4080fb

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/nvme/json.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ static void json_parse_subsys(nvme_host_t h, struct json_object *subsys_obj)
126126
struct json_object *port_obj;
127127

128128
port_obj = json_object_array_get_idx(port_array, p);
129-
json_parse_port(s, port_obj);
129+
if (port_obj)
130+
json_parse_port(s, port_obj);
130131
}
131132
}
132133

@@ -153,11 +154,12 @@ static void json_parse_host(nvme_root_t r, struct json_object *host_obj)
153154
return;
154155
for (s = 0; s < json_object_array_length(subsys_array); s++) {
155156
subsys_obj = json_object_array_get_idx(subsys_array, s);
156-
json_parse_subsys(h, subsys_obj);
157+
if (subsys_obj)
158+
json_parse_subsys(h, subsys_obj);
157159
}
158160
}
159161

160-
void json_read_config(nvme_root_t r, const char *config_file)
162+
int json_read_config(nvme_root_t r, const char *config_file)
161163
{
162164
struct json_object *json_root, *host_obj;
163165
int h;
@@ -166,13 +168,16 @@ void json_read_config(nvme_root_t r, const char *config_file)
166168
if (!json_root) {
167169
nvme_msg(r, LOG_DEBUG, "Failed to read %s, %s\n",
168170
config_file, json_util_get_last_err());
169-
return;
171+
errno = EAGAIN;
172+
return -1;
170173
}
171174
for (h = 0; h < json_object_array_length(json_root); h++) {
172175
host_obj = json_object_array_get_idx(json_root, h);
173-
json_parse_host(r, host_obj);
176+
if (host_obj)
177+
json_parse_host(r, host_obj);
174178
}
175179
json_object_put(json_root);
180+
return 0;
176181
}
177182

178183
#define JSON_STRING_OPTION(c, p, o) \

src/nvme/private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ struct nvme_root {
129129

130130
int nvme_set_attr(const char *dir, const char *attr, const char *value);
131131

132-
void json_read_config(nvme_root_t r, const char *config_file);
132+
int json_read_config(nvme_root_t r, const char *config_file);
133133

134134
int json_update_config(nvme_root_t r, const char *config_file);
135135

src/nvme/tree.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,19 @@ nvme_root_t nvme_create_root(FILE *fp, int log_level)
137137
return r;
138138
}
139139

140-
void nvme_read_config(nvme_root_t r, const char *config_file)
140+
int nvme_read_config(nvme_root_t r, const char *config_file)
141141
{
142+
int err = -1;
142143
#ifdef CONFIG_JSONC
143144
if (r && config_file) {
144-
json_read_config(r, config_file);
145-
r->config_file = strdup(config_file);
145+
err = json_read_config(r, config_file);
146+
if (!err)
147+
r->config_file = strdup(config_file);
146148
}
149+
#else
150+
errno = ENOTSUP;
147151
#endif
152+
return err;
148153
}
149154

150155
nvme_root_t nvme_scan(const char *config_file)

src/nvme/tree.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,8 +1040,10 @@ nvme_root_t nvme_scan(const char *config_file);
10401040
*
10411041
* Read in the contents of @config_file and merge them with
10421042
* the elements in @r.
1043+
*
1044+
* Returns: 0 on success, -1 on failure with errno set.
10431045
*/
1044-
void nvme_read_config(nvme_root_t r, const char *config_file);
1046+
int nvme_read_config(nvme_root_t r, const char *config_file);
10451047

10461048
/**
10471049
* nvme_refresh_topology() -

0 commit comments

Comments
 (0)