From fb2d35895ea2b5424c2cecffe58f0a0396692355 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 6 Jul 2021 11:04:44 +1000 Subject: [PATCH] conf: ignore the prefix if the config file name is an absolute path Fixes: $ export PIPEWIRE_CONFIG_PREFIX=/usr/share/pipewire $ pipewire -c /etc/pipewire/bar.conf [W][11925.530591][ conf.c: 253 conf_load()] config 0x560039ac6510: error loading config '/usr/share/pipewire//etc/pipewire/pipewire.conf': No such file or directory [W][11925.530721][ context.c: 178 try_load_conf()] context 0x560039ac6190: can't load config /usr/share/pipewire//etc/pipewire/pipewire.conf: No such file or directory --- src/pipewire/conf.c | 21 ++++++++--- src/pipewire/context.c | 5 ++- test/meson.build | 1 + test/test-config.c | 83 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 test/test-config.c diff --git a/src/pipewire/conf.c b/src/pipewire/conf.c index 063f665e2..74a29f908 100644 --- a/src/pipewire/conf.c +++ b/src/pipewire/conf.c @@ -69,13 +69,27 @@ static int get_read_path(char *path, size_t size, const char *prefix, const char const char *dir; char buffer[4096]; - if (prefix[0] == '/') { + if (name[0] == '/') { + const char *paths[] = { name, NULL }; + if (make_path(path, size, paths) == 0 && + access(path, R_OK) == 0) + return 1; + return -ENOENT; + } + + if (prefix && prefix[0] == '/') { const char *paths[] = { prefix, name, NULL }; if (make_path(path, size, paths) == 0 && access(path, R_OK) == 0) return 1; return -ENOENT; } + + if (prefix == NULL) { + prefix = name; + name = NULL; + } + if (pw_check_option("no-config", "true")) goto no_config; @@ -240,11 +254,6 @@ static int conf_load(const char *prefix, const char *name, struct pw_properties struct stat sbuf; int fd; - if (prefix == NULL) { - prefix = name; - name = NULL; - } - if (get_read_path(path, sizeof(path), prefix, name) == 0) { pw_log_debug(NAME" %p: can't load config '%s': %m", conf, path); return -ENOENT; diff --git a/src/pipewire/context.c b/src/pipewire/context.c index ee295bb95..aed06bbd5 100644 --- a/src/pipewire/context.c +++ b/src/pipewire/context.c @@ -175,10 +175,11 @@ static int try_load_conf(struct pw_context *this, const char *conf_prefix, if (spa_streq(conf_name, "null")) return 0; if ((res = pw_conf_load_conf(conf_prefix, conf_name, conf)) < 0) { + bool skip_prefix = conf_prefix == NULL || conf_name[0] == '/'; pw_log_warn(NAME" %p: can't load config %s%s%s: %s", this, - conf_prefix ? conf_prefix : "", - conf_prefix ? "/" : "", + skip_prefix ? "" : conf_prefix, + skip_prefix ? "" : "/", conf_name, spa_strerror(res)); } return res; diff --git a/test/meson.build b/test/meson.build index 2e162ceee..ebae2fac6 100644 --- a/test/meson.build +++ b/test/meson.build @@ -63,6 +63,7 @@ test('test lib', test('test context', executable('test-context', 'test-context.c', + 'test-config.c', include_directories: pwtest_inc, link_with: pwtest_lib) ) diff --git a/test/test-config.c b/test/test-config.c new file mode 100644 index 000000000..99e9f2f02 --- /dev/null +++ b/test/test-config.c @@ -0,0 +1,83 @@ +/* PipeWire + * + * Copyright © 2021 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include "pwtest.h" + +#include + +PWTEST(config_load_abspath) +{ + char path[PATH_MAX]; + int r; + FILE *fp; + struct pw_properties *props; + char *basename; + + pwtest_mkstemp(path); + fp = fopen(path, "w"); + fputs("data = x", fp); + fclose(fp); + + /* Load with NULL prefix and abs path */ + props = pw_properties_new("ignore", "me", NULL); + r = pw_conf_load_conf(NULL, path, props); + pwtest_neg_errno_ok(r); + pwtest_str_eq(pw_properties_get(props, "data"), "x"); + pw_properties_free(props); + + /* Load with non-NULL abs prefix and abs path */ + props = pw_properties_new("ignore", "me", NULL); + r = pw_conf_load_conf("/dummy", path, props); + pwtest_neg_errno_ok(r); + pwtest_str_eq(pw_properties_get(props, "data"), "x"); + pw_properties_free(props); + + /* Load with non-NULL relative prefix and abs path */ + props = pw_properties_new("ignore", "me", NULL); + r = pw_conf_load_conf("dummy", path, props); + pwtest_neg_errno_ok(r); + pwtest_str_eq(pw_properties_get(props, "data"), "x"); + pw_properties_free(props); + + /* Load with non-NULL abs prefix and relative path */ + basename = rindex(path, '/'); /* basename(3) and dirname(3) are terrible */ + pwtest_ptr_notnull(basename); + *basename = '\0'; + basename++; + + props = pw_properties_new("ignore", "me", NULL); + r = pw_conf_load_conf(path, basename, props); + pwtest_neg_errno_ok(r); + pwtest_str_eq(pw_properties_get(props, "data"), "x"); + pw_properties_free(props); + + return PWTEST_PASS; +} + +PWTEST_SUITE(context) +{ + pwtest_add(config_load_abspath, PWTEST_NOARG); + + return PWTEST_PASS; +}