pulse-server: add an option to check module arguments

Add a list of valid keys to the module info. When set, check if the
module arguments only contain the allowed keys and give an error
otherwise.
This commit is contained in:
Wim Taymans 2023-09-13 12:26:57 +02:00
parent b094057b0b
commit 3c812f672b
4 changed files with 61 additions and 9 deletions

View file

@ -164,6 +164,28 @@ void module_args_add_props(struct pw_properties *props, const char *str)
} }
} }
static bool find_key(const char * const keys[], const char *key)
{
for (int i = 0; keys[i] != NULL; i++)
if (spa_streq(keys[i], key))
return true;
return false;
}
static int module_args_check(struct pw_properties *props, const char * const valid_args[])
{
if (valid_args != NULL) {
const struct spa_dict_item *it;
spa_dict_for_each(it, &props->dict) {
if (!find_key(valid_args, it->key)) {
pw_log_warn("'%s' is not a valid module argument key", it->key);
return -EINVAL;
}
}
}
return 0;
}
int module_args_to_audioinfo_keys(struct impl *impl, struct pw_properties *props, int module_args_to_audioinfo_keys(struct impl *impl, struct pw_properties *props,
const char *key_format, const char *key_rate, const char *key_format, const char *key_rate,
const char *key_channels, const char *key_channel_map, const char *key_channels, const char *key_channel_map,
@ -299,6 +321,7 @@ struct module *module_create(struct impl *impl, const char *name, const char *ar
{ {
const struct module_info *info; const struct module_info *info;
struct module *module; struct module *module;
int res;
info = find_module_info(name); info = find_module_info(name);
if (info == NULL) { if (info == NULL) {
@ -321,19 +344,19 @@ struct module *module_create(struct impl *impl, const char *name, const char *ar
return NULL; return NULL;
module->props = pw_properties_new(NULL, NULL); module->props = pw_properties_new(NULL, NULL);
if (module->props == NULL) { if (module->props == NULL)
module_free(module); goto error_free;
return NULL;
}
if (args) if (args)
module_args_add_props(module->props, args); module_args_add_props(module->props, args);
if ((res = module_args_check(module->props, info->valid_args)) < 0) {
int res = module->info->prepare(module);
if (res < 0) {
module_free(module);
errno = -res; errno = -res;
return NULL; goto error_free;
}
if ((res = module->info->prepare(module)) < 0) {
errno = -res;
goto error_free;
} }
module->index = pw_map_insert_new(&impl->modules, module); module->index = pw_map_insert_new(&impl->modules, module);
@ -346,4 +369,9 @@ struct module *module_create(struct impl *impl, const char *name, const char *ar
module->index |= MODULE_FLAG; module->index |= MODULE_FLAG;
return module; return module;
error_free:
module_free(module);
return NULL;
} }

View file

@ -23,6 +23,7 @@ struct module_info {
int (*load) (struct module *module); int (*load) (struct module *module);
int (*unload) (struct module *module); int (*unload) (struct module *module);
const char* const *valid_args;
const struct spa_dict *properties; const struct spa_dict *properties;
size_t data_size; size_t data_size;
}; };

View file

@ -89,6 +89,15 @@ static int module_roc_sink_unload(struct module *module)
return 0; return 0;
} }
static const char* const valid_args[] = {
"sink_name",
"sink_properties",
"fec_code",
"remote_ip",
"remote_source_port",
"remote_repair_port",
NULL
};
static const struct spa_dict_item module_roc_sink_info[] = { static const struct spa_dict_item module_roc_sink_info[] = {
{ PW_KEY_MODULE_AUTHOR, "Sanchayan Maity <sanchayan@asymptotic.io>" }, { PW_KEY_MODULE_AUTHOR, "Sanchayan Maity <sanchayan@asymptotic.io>" },
{ PW_KEY_MODULE_DESCRIPTION, "roc sink" }, { PW_KEY_MODULE_DESCRIPTION, "roc sink" },
@ -169,6 +178,7 @@ out:
DEFINE_MODULE_INFO(module_roc_sink) = { DEFINE_MODULE_INFO(module_roc_sink) = {
.name = "module-roc-sink", .name = "module-roc-sink",
.valid_args = valid_args,
.prepare = module_roc_sink_prepare, .prepare = module_roc_sink_prepare,
.load = module_roc_sink_load, .load = module_roc_sink_load,
.unload = module_roc_sink_unload, .unload = module_roc_sink_unload,

View file

@ -89,6 +89,18 @@ static int module_roc_source_unload(struct module *module)
return 0; return 0;
} }
static const char* const valid_args[] = {
"source_name",
"source_properties",
"resampler_profile",
"fec_code",
"sess_latency_msec",
"local_ip",
"local_source_port",
"local_repair_port",
NULL
};
static const struct spa_dict_item module_roc_source_info[] = { static const struct spa_dict_item module_roc_source_info[] = {
{ PW_KEY_MODULE_AUTHOR, "Sanchayan Maity <sanchayan@asymptotic.io>" }, { PW_KEY_MODULE_AUTHOR, "Sanchayan Maity <sanchayan@asymptotic.io>" },
{ PW_KEY_MODULE_DESCRIPTION, "roc source" }, { PW_KEY_MODULE_DESCRIPTION, "roc source" },
@ -178,6 +190,7 @@ out:
DEFINE_MODULE_INFO(module_roc_source) = { DEFINE_MODULE_INFO(module_roc_source) = {
.name = "module-roc-source", .name = "module-roc-source",
.valid_args = valid_args,
.prepare = module_roc_source_prepare, .prepare = module_roc_source_prepare,
.load = module_roc_source_load, .load = module_roc_source_load,
.unload = module_roc_source_unload, .unload = module_roc_source_unload,