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
@@ -130,7 +132,8 @@ static void json_parse_subsys(nvme_host_t h, struct json_object *subsys_obj)
130132 struct json_object * port_obj ;
131133
132134 port_obj = json_object_array_get_idx (port_array , p );
133- json_parse_port (s , port_obj );
135+ if (port_obj )
136+ json_parse_port (s , port_obj );
134137 }
135138}
136139
@@ -157,26 +160,38 @@ static void json_parse_host(nvme_root_t r, struct json_object *host_obj)
157160 return ;
158161 for (s = 0 ; s < json_object_array_length (subsys_array ); s ++ ) {
159162 subsys_obj = json_object_array_get_idx (subsys_array , s );
160- json_parse_subsys (h , subsys_obj );
163+ if (subsys_obj )
164+ json_parse_subsys (h , subsys_obj );
161165 }
162166}
163167
164- void json_read_config (nvme_root_t r , const char * config_file )
168+ int json_read_config (nvme_root_t r , const char * config_file )
165169{
166170 struct json_object * json_root , * host_obj ;
167- int h ;
171+ int fd , h ;
168172
169- json_root = json_object_from_file (config_file );
173+ fd = open (config_file , O_RDONLY );
174+ if (fd < 0 ) {
175+ nvme_msg (r , LOG_DEBUG , "Error opening %s, %s\n" ,
176+ config_file , strerror (errno ));
177+ return fd ;
178+ }
179+ json_root = json_object_from_fd (fd );
170180 if (!json_root ) {
171181 nvme_msg (r , LOG_DEBUG , "Failed to read %s, %s\n" ,
172182 config_file , json_util_get_last_err ());
173- return ;
183+ errno = EPROTO ;
184+ close (fd );
185+ return -1 ;
174186 }
175187 for (h = 0 ; h < json_object_array_length (json_root ); h ++ ) {
176188 host_obj = json_object_array_get_idx (json_root , h );
177- json_parse_host (r , host_obj );
189+ if (host_obj )
190+ json_parse_host (r , host_obj );
178191 }
179192 json_object_put (json_root );
193+ close (fd );
194+ return 0 ;
180195}
181196
182197#define JSON_STRING_OPTION (c , p , o ) \
0 commit comments