conf: improve config error handling

Make the config parser return the amount of parsed items.
Fail to create a context when parsing the config fails.
Log the number of items parsed from the config.
This commit is contained in:
Wim Taymans 2021-05-13 15:09:57 +02:00
parent 09cba1e4de
commit 577ad0c73a
2 changed files with 27 additions and 14 deletions

View file

@ -281,6 +281,7 @@ static int parse_spa_libs(struct pw_context *context, char *str)
{ {
struct spa_json it[2]; struct spa_json it[2];
char key[512], value[512]; char key[512], value[512];
int count = 0;
spa_json_init(&it[0], str, strlen(str)); spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_object(&it[0], &it[1]) < 0) if (spa_json_enter_object(&it[0], &it[1]) < 0)
@ -294,16 +295,17 @@ static int parse_spa_libs(struct pw_context *context, char *str)
} }
else if (spa_json_get_string(&it[1], value, sizeof(value)-1) > 0) { else if (spa_json_get_string(&it[1], value, sizeof(value)-1) > 0) {
pw_context_add_spa_lib(context, key, value); pw_context_add_spa_lib(context, key, value);
count++;
} }
} }
return 0; return count;
} }
static int load_module(struct pw_context *context, const char *key, const char *args, const char *flags) static int load_module(struct pw_context *context, const char *key, const char *args, const char *flags)
{ {
if (pw_context_load_module(context, key, args, NULL) == NULL) { if (pw_context_load_module(context, key, args, NULL) == NULL) {
if (errno == ENOENT && flags && strstr(flags, "ifexists") != NULL) { if (errno == ENOENT && flags && strstr(flags, "ifexists") != NULL) {
pw_log_debug(NAME" %p: skipping unavailable module %s", pw_log_info(NAME" %p: skipping unavailable module %s",
context, key); context, key);
} else if (flags == NULL || strstr(flags, "nofail") == NULL) { } else if (flags == NULL || strstr(flags, "nofail") == NULL) {
pw_log_error(NAME" %p: could not load mandatory module \"%s\": %m", pw_log_error(NAME" %p: could not load mandatory module \"%s\": %m",
@ -313,6 +315,8 @@ static int load_module(struct pw_context *context, const char *key, const char *
pw_log_info(NAME" %p: could not load optional module \"%s\": %m", pw_log_info(NAME" %p: could not load optional module \"%s\": %m",
context, key); context, key);
} }
} else {
pw_log_info(NAME" %p: loaded module %s", context, key);
} }
return 0; return 0;
} }
@ -329,7 +333,7 @@ static int parse_modules(struct pw_context *context, char *str)
{ {
struct spa_json it[3]; struct spa_json it[3];
char key[512]; char key[512];
int res = 0; int res = 0, count = 0;
spa_json_init(&it[0], str, strlen(str)); spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_array(&it[0], &it[1]) < 0) if (spa_json_enter_array(&it[0], &it[1]) < 0)
@ -366,6 +370,8 @@ static int parse_modules(struct pw_context *context, char *str)
if (res < 0) if (res < 0)
break; break;
res = ++count;
} }
return res; return res;
} }
@ -409,7 +415,7 @@ static int parse_objects(struct pw_context *context, char *str)
{ {
struct spa_json it[3]; struct spa_json it[3];
char key[512]; char key[512];
int res = 0; int res = 0, count = 0;
spa_json_init(&it[0], str, strlen(str)); spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_array(&it[0], &it[1]) < 0) if (spa_json_enter_array(&it[0], &it[1]) < 0)
@ -447,6 +453,7 @@ static int parse_objects(struct pw_context *context, char *str)
if (res < 0) if (res < 0)
break; break;
res = ++count;
} }
return res; return res;
} }
@ -493,7 +500,7 @@ static int parse_exec(struct pw_context *context, char *str)
{ {
struct spa_json it[3]; struct spa_json it[3];
char key[512]; char key[512];
int res = 0; int res = 0, count = 0;
spa_json_init(&it[0], str, strlen(str)); spa_json_init(&it[0], str, strlen(str));
if (spa_json_enter_array(&it[0], &it[1]) < 0) if (spa_json_enter_array(&it[0], &it[1]) < 0)
@ -522,6 +529,8 @@ static int parse_exec(struct pw_context *context, char *str)
if (res < 0) if (res < 0)
break; break;
res = ++count;
} }
return res; return res;
} }
@ -535,7 +544,7 @@ int pw_context_parse_conf_section(struct pw_context *context,
int res; int res;
if ((str = pw_properties_get(conf, section)) == NULL) if ((str = pw_properties_get(conf, section)) == NULL)
return -ENOENT; return 0;
s = strdup(str); s = strdup(str);

View file

@ -361,14 +361,18 @@ struct pw_context *pw_context_new(struct pw_loop *main_loop,
this->sc_pagesize = sysconf(_SC_PAGESIZE); this->sc_pagesize = sysconf(_SC_PAGESIZE);
if ((res = pw_context_parse_conf_section(this, conf, "context.spa-libs")) >= 0) if ((res = pw_context_parse_conf_section(this, conf, "context.spa-libs")) < 0)
pw_log_info(NAME" %p: parsed context.spa-libs section", this); goto error_free_loop;
if ((res = pw_context_parse_conf_section(this, conf, "context.modules")) >= 0) pw_log_info(NAME" %p: parsed %d context.spa-libs items", this, res);
pw_log_info(NAME" %p: parsed context.modules section", this); if ((res = pw_context_parse_conf_section(this, conf, "context.modules")) < 0)
if ((res = pw_context_parse_conf_section(this, conf, "context.objects")) >= 0) goto error_free_loop;
pw_log_info(NAME" %p: parsed context.objects section", this); pw_log_info(NAME" %p: parsed %d context.modules items", this, res);
if ((res = pw_context_parse_conf_section(this, conf, "context.exec")) >= 0) if ((res = pw_context_parse_conf_section(this, conf, "context.objects")) < 0)
pw_log_info(NAME" %p: parsed context.exec section", this); goto error_free_loop;
pw_log_info(NAME" %p: parsed %d context.objects items", this, res);
if ((res = pw_context_parse_conf_section(this, conf, "context.exec")) < 0)
goto error_free_loop;
pw_log_info(NAME" %p: parsed %d context.exec items", this, res);
pw_log_debug(NAME" %p: created", this); pw_log_debug(NAME" %p: created", this);