mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-06 13:30:01 -05:00
daemon: rework config parsing
Replace config parsing for something more flexible based on json.
This commit is contained in:
parent
1bd90dc666
commit
49d11acde0
7 changed files with 458 additions and 905 deletions
|
|
@ -24,39 +24,341 @@
|
|||
|
||||
#include <signal.h>
|
||||
#include <getopt.h>
|
||||
#include <limits.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <spa/utils/result.h>
|
||||
#include <spa/utils/json.h>
|
||||
|
||||
#include <pipewire/impl.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "daemon-config.h"
|
||||
|
||||
static const char *daemon_name;
|
||||
#define NAME "daemon"
|
||||
|
||||
#define DEFAULT_CONFIG_FILE "pipewire.conf"
|
||||
|
||||
struct data {
|
||||
struct pw_context *context;
|
||||
struct pw_main_loop *loop;
|
||||
|
||||
const char *daemon_name;
|
||||
struct pw_properties *conf;
|
||||
};
|
||||
|
||||
static int load_config(struct data *d)
|
||||
{
|
||||
const char *path;
|
||||
char filename[PATH_MAX], *data;
|
||||
struct stat sbuf;
|
||||
int fd;
|
||||
|
||||
path = getenv("PIPEWIRE_CONFIG_FILE");
|
||||
if (path == NULL) {
|
||||
const char *dir;
|
||||
dir = getenv("PIPEWIRE_CONFIG_DIR");
|
||||
if (dir == NULL)
|
||||
dir = PIPEWIRE_CONFIG_DIR;
|
||||
if (dir == NULL)
|
||||
return -ENOENT;
|
||||
|
||||
snprintf(filename, sizeof(filename), "%s/%s",
|
||||
dir, DEFAULT_CONFIG_FILE);
|
||||
path = filename;
|
||||
}
|
||||
|
||||
if ((fd = open(path, O_CLOEXEC | O_RDONLY)) < 0) {
|
||||
pw_log_warn(NAME" %p: error loading config '%s': %m", d, path);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
pw_log_info(NAME" %p: loading config '%s'", d, path);
|
||||
if (fstat(fd, &sbuf) < 0)
|
||||
goto error_close;
|
||||
if ((data = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
|
||||
goto error_close;
|
||||
close(fd);
|
||||
|
||||
pw_properties_update_string(d->conf, data, sbuf.st_size);
|
||||
munmap(data, sbuf.st_size);
|
||||
|
||||
return 0;
|
||||
|
||||
error_close:
|
||||
close(fd);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
static int parse_spa_libs(struct data *d, const char *str)
|
||||
{
|
||||
struct spa_json it[2];
|
||||
char key[512], value[512];
|
||||
|
||||
spa_json_init(&it[0], str, strlen(str));
|
||||
if (spa_json_enter_object(&it[0], &it[1]) < 0)
|
||||
return -EINVAL;
|
||||
|
||||
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
|
||||
const char *val;
|
||||
if (key[0] == '#') {
|
||||
if (spa_json_next(&it[1], &val) <= 0)
|
||||
break;
|
||||
}
|
||||
else if (spa_json_get_string(&it[1], value, sizeof(value)-1) > 0) {
|
||||
pw_context_add_spa_lib(d->context, key, value);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int load_module(struct data *d, const char *key, const char *args, const char *flags)
|
||||
{
|
||||
if (pw_context_load_module(d->context, key, args, NULL) == NULL) {
|
||||
if (errno == ENOENT && flags && strstr(flags, "ifexists") != NULL) {
|
||||
pw_log_debug(NAME" %p: skipping unavailable module %s",
|
||||
d, key);
|
||||
} else {
|
||||
pw_log_error(NAME" %p: could not load module \"%s\": %m",
|
||||
d, key);
|
||||
return -errno;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_modules(struct data *d, const char *str)
|
||||
{
|
||||
struct spa_json it[3];
|
||||
char key[512];
|
||||
int res = 0;
|
||||
|
||||
spa_json_init(&it[0], str, strlen(str));
|
||||
if (spa_json_enter_object(&it[0], &it[1]) < 0)
|
||||
return -EINVAL;
|
||||
|
||||
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
|
||||
const char *val;
|
||||
char *args = NULL, *flags = NULL;
|
||||
int len;
|
||||
|
||||
if ((len = spa_json_next(&it[1], &val)) <= 0)
|
||||
break;
|
||||
|
||||
if (key[0] == '#')
|
||||
continue;
|
||||
|
||||
if (spa_json_is_object(val, len)) {
|
||||
char arg[512], aval[1024];
|
||||
|
||||
spa_json_enter(&it[1], &it[2]);
|
||||
|
||||
while (spa_json_get_string(&it[2], arg, sizeof(arg)-1) > 0) {
|
||||
if (spa_json_get_string(&it[2], aval, sizeof(aval)-1) <= 0)
|
||||
break;
|
||||
|
||||
if (strcmp(arg, "args") == 0) {
|
||||
args = strdup(aval);
|
||||
} else if (strcmp(arg, "flags") == 0) {
|
||||
flags = strdup(aval);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!spa_json_is_null(val, len))
|
||||
break;
|
||||
|
||||
res = load_module(d, key, args, flags);
|
||||
|
||||
free(args);
|
||||
free(flags);
|
||||
|
||||
if (res < 0)
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int create_object(struct data *d, const char *key, const char *args, const char *flags)
|
||||
{
|
||||
struct pw_impl_factory *factory;
|
||||
void *obj;
|
||||
|
||||
pw_log_debug("find factory %s", key);
|
||||
factory = pw_context_find_factory(d->context, key);
|
||||
if (factory == NULL) {
|
||||
if (flags && strstr(flags, "nofail") != NULL)
|
||||
return 0;
|
||||
pw_log_error("can't find factory %s", key);
|
||||
return -ENOENT;
|
||||
}
|
||||
pw_log_debug("create object with args %s", args);
|
||||
obj = pw_impl_factory_create_object(factory,
|
||||
NULL, NULL, 0,
|
||||
args ? pw_properties_new_string(args) : NULL,
|
||||
SPA_ID_INVALID);
|
||||
if (obj == NULL) {
|
||||
if (flags && strstr(flags, "nofail") != NULL)
|
||||
return 0;
|
||||
pw_log_error("can't create object from factory %s: %m", key);
|
||||
return -errno;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_objects(struct data *d, const char *str)
|
||||
{
|
||||
struct spa_json it[3];
|
||||
char key[512];
|
||||
int res = 0;
|
||||
|
||||
spa_json_init(&it[0], str, strlen(str));
|
||||
if (spa_json_enter_object(&it[0], &it[1]) < 0)
|
||||
return -EINVAL;
|
||||
|
||||
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
|
||||
const char *val;
|
||||
char *args = NULL, *flags = NULL;
|
||||
int len;
|
||||
|
||||
if ((len = spa_json_next(&it[1], &val)) <= 0)
|
||||
break;
|
||||
|
||||
if (key[0] == '#')
|
||||
continue;
|
||||
|
||||
if (spa_json_is_object(val, len)) {
|
||||
char arg[512], aval[1024];
|
||||
|
||||
spa_json_enter(&it[1], &it[2]);
|
||||
|
||||
while (spa_json_get_string(&it[2], arg, sizeof(arg)-1) > 0) {
|
||||
if (spa_json_get_string(&it[2], aval, sizeof(aval)-1) <= 0)
|
||||
break;
|
||||
|
||||
if (strcmp(arg, "args") == 0) {
|
||||
args = strdup(aval);
|
||||
} else if (strcmp(arg, "flags") == 0) {
|
||||
flags = strdup(aval);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (!spa_json_is_null(val, len))
|
||||
break;
|
||||
|
||||
res = create_object(d, key, args, flags);
|
||||
|
||||
free(args);
|
||||
free(flags);
|
||||
|
||||
if (res < 0)
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int do_exec(struct data *d, const char *key, const char *args)
|
||||
{
|
||||
int pid, res, n_args;
|
||||
|
||||
pid = fork();
|
||||
|
||||
if (pid == 0) {
|
||||
char *cmd, **argv;
|
||||
|
||||
cmd = spa_aprintf("%s %s", key, args);
|
||||
argv = pw_split_strv(cmd, " \t", INT_MAX, &n_args);
|
||||
free(cmd);
|
||||
|
||||
pw_log_info("exec %s '%s'", key, args);
|
||||
res = execvp(key, argv);
|
||||
pw_free_strv(argv);
|
||||
|
||||
if (res == -1) {
|
||||
res = -errno;
|
||||
pw_log_error("execvp error '%s': %m", key);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int status;
|
||||
res = waitpid(pid, &status, WNOHANG);
|
||||
pw_log_info("exec got pid %d res:%d status:%d", pid, res, status);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int parse_exec(struct data *d, const char *str)
|
||||
{
|
||||
struct spa_json it[3];
|
||||
char key[512];
|
||||
int res = 0;
|
||||
|
||||
spa_json_init(&it[0], str, strlen(str));
|
||||
if (spa_json_enter_object(&it[0], &it[1]) < 0)
|
||||
return -EINVAL;
|
||||
|
||||
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
|
||||
const char *val;
|
||||
char *args = NULL;
|
||||
int len;
|
||||
|
||||
if ((len = spa_json_next(&it[1], &val)) <= 0)
|
||||
break;
|
||||
|
||||
if (key[0] == '#')
|
||||
continue;
|
||||
|
||||
if (spa_json_is_object(val, len)) {
|
||||
char arg[512], aval[1024];
|
||||
|
||||
spa_json_enter(&it[1], &it[2]);
|
||||
|
||||
while (spa_json_get_string(&it[2], arg, sizeof(arg)-1) > 0) {
|
||||
if (spa_json_get_string(&it[2], aval, sizeof(aval)-1) <= 0)
|
||||
break;
|
||||
|
||||
if (strcmp(arg, "args") == 0)
|
||||
args = strdup(aval);
|
||||
}
|
||||
}
|
||||
else if (!spa_json_is_null(val, len))
|
||||
break;
|
||||
|
||||
res = do_exec(d, key, args);
|
||||
|
||||
free(args);
|
||||
|
||||
if (res < 0)
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static void do_quit(void *data, int signal_number)
|
||||
{
|
||||
struct pw_main_loop *loop = data;
|
||||
pw_main_loop_quit(loop);
|
||||
struct data *d = data;
|
||||
pw_main_loop_quit(d->loop);
|
||||
}
|
||||
|
||||
static void show_help(const char *name)
|
||||
static void show_help(struct data *d, const char *name)
|
||||
{
|
||||
fprintf(stdout, "%s [options]\n"
|
||||
" -h, --help Show this help\n"
|
||||
" --version Show version\n"
|
||||
" -n, --name Daemon name (Default %s)\n",
|
||||
name,
|
||||
daemon_name);
|
||||
d->daemon_name);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct pw_context *context;
|
||||
struct pw_main_loop *loop;
|
||||
struct pw_daemon_config *config;
|
||||
struct data d;
|
||||
struct pw_properties *properties;
|
||||
char *err = NULL;
|
||||
const char *str;
|
||||
static const struct option long_options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "version", no_argument, NULL, 'V' },
|
||||
|
|
@ -66,19 +368,30 @@ int main(int argc, char *argv[])
|
|||
};
|
||||
int c, res;
|
||||
|
||||
if (setenv("PIPEWIRE_INTERNAL", "1", 1) < 0)
|
||||
fprintf(stderr, "can't set PIPEWIRE_INTERNAL env: %m");
|
||||
|
||||
spa_zero(d);
|
||||
pw_init(&argc, &argv);
|
||||
|
||||
if (setenv("PIPEWIRE_INTERNAL", "1", 1) < 0)
|
||||
fprintf(stderr, "can't PIPEWIRE_INTERNAL env: %m");
|
||||
if ((d.conf = pw_properties_new(NULL, NULL)) == NULL) {
|
||||
pw_log_error("failed to create config: %m");
|
||||
return -1;
|
||||
}
|
||||
|
||||
daemon_name = getenv("PIPEWIRE_CORE");
|
||||
if (daemon_name == NULL)
|
||||
daemon_name = PW_DEFAULT_REMOTE;
|
||||
if ((res = load_config(&d)) < 0) {
|
||||
pw_log_error("failed to load config: %s", spa_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
|
||||
d.daemon_name = getenv("PIPEWIRE_CORE");
|
||||
if (d.daemon_name == NULL)
|
||||
d.daemon_name = PW_DEFAULT_REMOTE;
|
||||
|
||||
while ((c = getopt_long(argc, argv, "hVn:", long_options, NULL)) != -1) {
|
||||
switch (c) {
|
||||
case 'h' :
|
||||
show_help(argv[0]);
|
||||
show_help(&d, argv[0]);
|
||||
return 0;
|
||||
case 'V' :
|
||||
fprintf(stdout, "%s\n"
|
||||
|
|
@ -89,8 +402,8 @@ int main(int argc, char *argv[])
|
|||
pw_get_library_version());
|
||||
return 0;
|
||||
case 'n' :
|
||||
daemon_name = optarg;
|
||||
fprintf(stdout, "set name %s\n", daemon_name);
|
||||
d.daemon_name = optarg;
|
||||
fprintf(stdout, "set name %s\n", d.daemon_name);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
|
|
@ -98,47 +411,57 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
|
||||
properties = pw_properties_new(
|
||||
PW_KEY_CORE_NAME, daemon_name,
|
||||
PW_KEY_CORE_NAME, d.daemon_name,
|
||||
PW_KEY_CONTEXT_PROFILE_MODULES, "none",
|
||||
PW_KEY_CORE_DAEMON, "true", NULL);
|
||||
|
||||
/* parse configuration */
|
||||
config = pw_daemon_config_new(properties);
|
||||
if (pw_daemon_config_load(config, &err) < 0) {
|
||||
pw_log_error("failed to parse config: %s", err);
|
||||
free(err);
|
||||
return -1;
|
||||
}
|
||||
if ((str = pw_properties_get(d.conf, "properties")) != NULL)
|
||||
pw_properties_update_string(properties, str, strlen(str));
|
||||
|
||||
|
||||
loop = pw_main_loop_new(&properties->dict);
|
||||
if (loop == NULL) {
|
||||
d.loop = pw_main_loop_new(&properties->dict);
|
||||
if (d.loop == NULL) {
|
||||
pw_log_error("failed to create main-loop: %m");
|
||||
return -1;
|
||||
}
|
||||
|
||||
pw_loop_add_signal(pw_main_loop_get_loop(loop), SIGINT, do_quit, loop);
|
||||
pw_loop_add_signal(pw_main_loop_get_loop(loop), SIGTERM, do_quit, loop);
|
||||
pw_loop_add_signal(pw_main_loop_get_loop(d.loop), SIGINT, do_quit, &d);
|
||||
pw_loop_add_signal(pw_main_loop_get_loop(d.loop), SIGTERM, do_quit, &d);
|
||||
|
||||
context = pw_context_new(pw_main_loop_get_loop(loop), properties, 0);
|
||||
if (context == NULL) {
|
||||
d.context = pw_context_new(pw_main_loop_get_loop(d.loop), properties, 0);
|
||||
if (d.context == NULL) {
|
||||
pw_log_error("failed to create context: %m");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((res = pw_daemon_config_run_commands(config, context)) < 0) {
|
||||
pw_log_error("failed to run config commands: %s", spa_strerror(res));
|
||||
pw_main_loop_quit(loop);
|
||||
return -1;
|
||||
if ((str = pw_properties_get(d.conf, "spa-libs")) != NULL)
|
||||
parse_spa_libs(&d, str);
|
||||
if ((str = pw_properties_get(d.conf, "modules")) != NULL) {
|
||||
if ((res = parse_modules(&d, str)) < 0) {
|
||||
pw_log_error("failed to load modules: %s", spa_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if ((str = pw_properties_get(d.conf, "objects")) != NULL) {
|
||||
if ((res = parse_objects(&d, str)) < 0) {
|
||||
pw_log_error("failed to load objects: %s", spa_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if ((str = pw_properties_get(d.conf, "exec")) != NULL) {
|
||||
if ((res = parse_exec(&d, str)) < 0) {
|
||||
pw_log_error("failed to exec: %s", spa_strerror(res));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
pw_log_info("start main loop");
|
||||
pw_main_loop_run(loop);
|
||||
pw_main_loop_run(d.loop);
|
||||
pw_log_info("leave main loop");
|
||||
|
||||
pw_daemon_config_free(config);
|
||||
pw_context_destroy(context);
|
||||
pw_main_loop_destroy(loop);
|
||||
pw_properties_free(d.conf);
|
||||
pw_context_destroy(d.context);
|
||||
pw_main_loop_destroy(d.loop);
|
||||
pw_deinit();
|
||||
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue