Skip to content

Commit bee678d

Browse files
committed
Ignore errors when opening JSON configuration file
The JSON configuration file is optional, so any errors during opening can be ignored. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent b4080fb commit bee678d

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/nvme/json.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <stdio.h>
1010
#include <errno.h>
1111
#include <string.h>
12+
#include <unistd.h>
13+
#include <fcntl.h>
1214

1315
#include <json.h>
1416

@@ -162,13 +164,20 @@ static void json_parse_host(nvme_root_t r, struct json_object *host_obj)
162164
int json_read_config(nvme_root_t r, const char *config_file)
163165
{
164166
struct json_object *json_root, *host_obj;
165-
int h;
167+
int fd, h;
166168

167-
json_root = json_object_from_file(config_file);
169+
fd = open(config_file, O_RDONLY);
170+
if (fd < 0) {
171+
nvme_msg(r, LOG_DEBUG, "Error opening %s, %s\n",
172+
config_file, strerror(errno));
173+
return fd;
174+
}
175+
json_root = json_object_from_fd(fd);
168176
if (!json_root) {
169177
nvme_msg(r, LOG_DEBUG, "Failed to read %s, %s\n",
170178
config_file, json_util_get_last_err());
171-
errno = EAGAIN;
179+
errno = EPROTO;
180+
close(fd);
172181
return -1;
173182
}
174183
for (h = 0; h < json_object_array_length(json_root); h++) {
@@ -177,6 +186,7 @@ int json_read_config(nvme_root_t r, const char *config_file)
177186
json_parse_host(r, host_obj);
178187
}
179188
json_object_put(json_root);
189+
close(fd);
180190
return 0;
181191
}
182192

src/nvme/tree.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ int nvme_read_config(nvme_root_t r, const char *config_file)
145145
err = json_read_config(r, config_file);
146146
if (!err)
147147
r->config_file = strdup(config_file);
148+
/*
149+
* The json configuration file is optional,
150+
* so ignore errors when opening the file.
151+
*/
152+
if (err < 0 && errno != EPROTO)
153+
err = 0;
148154
}
149155
#else
150156
errno = ENOTSUP;

0 commit comments

Comments
 (0)