2017-05-23 19:15:33 +02:00
|
|
|
/* PipeWire
|
2018-11-05 17:48:52 +01:00
|
|
|
* Copyright © 2016 Axis Communications <dev-gstreamer@axis.com>
|
|
|
|
|
* @author Linus Svensson <linus.svensson@axis.com>
|
|
|
|
|
* Copyright © 2018 Wim Taymans
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* 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:
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* 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.
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
2017-07-12 18:04:00 +02:00
|
|
|
#include <stdio.h>
|
2016-11-16 09:43:51 +01:00
|
|
|
#include <errno.h>
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-07-11 15:57:20 +02:00
|
|
|
#include <pipewire/pipewire.h>
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
#include "daemon/command.h"
|
2017-07-11 15:57:20 +02:00
|
|
|
#include "daemon/daemon-config.h"
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
#define DEFAULT_CONFIG_FILE PIPEWIRE_CONFIG_DIR "/pipewire.conf"
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
static int
|
2017-05-26 08:05:01 +02:00
|
|
|
parse_line(struct pw_daemon_config *config,
|
|
|
|
|
const char *filename, char *line, unsigned int lineno, char **err)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
struct pw_command *command = NULL;
|
|
|
|
|
char *p;
|
|
|
|
|
char *local_err = NULL;
|
|
|
|
|
|
|
|
|
|
/* search for comments */
|
|
|
|
|
if ((p = strchr(line, '#')))
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
|
|
/* remove whitespaces */
|
2019-05-31 13:21:17 +02:00
|
|
|
line = pw_strip(line, "\n\r \t");
|
2017-05-26 08:05:01 +02:00
|
|
|
if (*line == '\0') /* empty line */
|
2019-06-20 15:19:28 +02:00
|
|
|
goto out;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
if ((command = pw_command_parse(config->properties, line, &local_err)) == NULL)
|
|
|
|
|
goto error_parse;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
spa_list_append(&config->commands, &command->link);
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
error_parse:
|
|
|
|
|
asprintf(err, "%s:%u: %s", filename, lineno, local_err);
|
|
|
|
|
free(local_err);
|
|
|
|
|
return -EINVAL;
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-23 19:15:33 +02:00
|
|
|
* pw_daemon_config_new:
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-05-23 19:15:33 +02:00
|
|
|
* Returns a new empty #struct pw_daemon_config.
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
2019-06-20 15:19:28 +02:00
|
|
|
struct pw_daemon_config *pw_daemon_config_new(struct pw_properties *properties)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
struct pw_daemon_config *config;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
config = calloc(1, sizeof(struct pw_daemon_config));
|
2019-06-20 15:19:28 +02:00
|
|
|
if (config == NULL)
|
|
|
|
|
goto error_exit;
|
|
|
|
|
|
|
|
|
|
config->properties = properties;
|
2017-05-26 08:05:01 +02:00
|
|
|
spa_list_init(&config->commands);
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
return config;
|
2019-06-20 15:19:28 +02:00
|
|
|
|
|
|
|
|
error_exit:
|
|
|
|
|
return NULL;
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-23 19:15:33 +02:00
|
|
|
* pw_daemon_config_free:
|
|
|
|
|
* @config: A #struct pw_daemon_config
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
|
|
|
|
* Free all resources associated to @config.
|
|
|
|
|
*/
|
2017-05-26 08:05:01 +02:00
|
|
|
void pw_daemon_config_free(struct pw_daemon_config *config)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2019-05-23 09:56:02 +02:00
|
|
|
struct pw_command *cmd;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2019-05-23 09:56:02 +02:00
|
|
|
spa_list_consume(cmd, &config->commands, link)
|
2018-12-19 17:47:25 +01:00
|
|
|
pw_command_free(cmd);
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
free(config);
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-23 19:15:33 +02:00
|
|
|
* pw_daemon_config_load_file:
|
|
|
|
|
* @config: A #struct pw_daemon_config
|
2016-08-29 18:29:07 +02:00
|
|
|
* @filename: A filename
|
2016-11-14 12:42:00 +01:00
|
|
|
* @err: Return location for an error string
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-05-23 19:15:33 +02:00
|
|
|
* Loads PipeWire config from @filename.
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-12-18 11:38:30 +01:00
|
|
|
* Returns: 0 on success, otherwise < 0 and @err is set.
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
2017-12-18 11:38:30 +01:00
|
|
|
int pw_daemon_config_load_file(struct pw_daemon_config *config, const char *filename, char **err)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
unsigned int line;
|
|
|
|
|
FILE *f;
|
|
|
|
|
char buf[4096];
|
2016-11-16 09:43:51 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_log_debug("deamon-config %p: loading configuration file '%s'", config, filename);
|
2016-11-16 09:43:51 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
if ((f = fopen(filename, "r")) == NULL) {
|
|
|
|
|
asprintf(err, "failed to open configuration file '%s': %s", filename,
|
|
|
|
|
strerror(errno));
|
|
|
|
|
goto open_error;
|
|
|
|
|
}
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
line = 0;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
while (!feof(f)) {
|
|
|
|
|
if (!fgets(buf, sizeof(buf), f)) {
|
|
|
|
|
if (feof(f))
|
|
|
|
|
break;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
asprintf(err, "failed to read configuration file '%s': %s",
|
|
|
|
|
filename, strerror(errno));
|
|
|
|
|
goto read_error;
|
|
|
|
|
}
|
2016-11-16 09:43:51 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
line++;
|
2016-11-16 09:43:51 +01:00
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
if (parse_line(config, filename, buf, line, err) != 0)
|
2017-05-26 08:05:01 +02:00
|
|
|
goto parse_failed;
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
return 0;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
parse_failed:
|
|
|
|
|
read_error:
|
|
|
|
|
fclose(f);
|
|
|
|
|
open_error:
|
2017-12-18 11:38:30 +01:00
|
|
|
return -EINVAL;
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-23 19:15:33 +02:00
|
|
|
* pw_daemon_config_load:
|
|
|
|
|
* @config: A #struct pw_daemon_config
|
2016-08-29 18:29:07 +02:00
|
|
|
* @err: Return location for a #GError, or %NULL
|
|
|
|
|
*
|
2017-05-23 19:15:33 +02:00
|
|
|
* Loads the default config file for PipeWire. The filename can be overridden with
|
|
|
|
|
* an evironment variable PIPEWIRE_CONFIG_FILE.
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-12-18 11:38:30 +01:00
|
|
|
* Return: 0 on success, otherwise < 0 and @err is set.
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
2017-12-18 11:38:30 +01:00
|
|
|
int pw_daemon_config_load(struct pw_daemon_config *config, char **err)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
const char *filename;
|
|
|
|
|
|
|
|
|
|
filename = getenv("PIPEWIRE_CONFIG_FILE");
|
|
|
|
|
if (filename != NULL && *filename != '\0') {
|
|
|
|
|
pw_log_debug("PIPEWIRE_CONFIG_FILE set to: %s", filename);
|
|
|
|
|
} else {
|
|
|
|
|
filename = DEFAULT_CONFIG_FILE;
|
|
|
|
|
}
|
|
|
|
|
return pw_daemon_config_load_file(config, filename, err);
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-23 19:15:33 +02:00
|
|
|
* pw_daemon_config_run_commands:
|
|
|
|
|
* @config: A #struct pw_daemon_config
|
2019-12-10 18:19:56 +01:00
|
|
|
* @context: A #struct pw_context
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
|
|
|
|
* Run all commands that have been parsed. The list of commands will be cleared
|
|
|
|
|
* when this function has been called.
|
|
|
|
|
*
|
2017-12-18 11:38:30 +01:00
|
|
|
* Returns: 0 if all commands where executed with success, otherwise < 0.
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
2019-12-10 18:19:56 +01:00
|
|
|
int pw_daemon_config_run_commands(struct pw_daemon_config *config, struct pw_context *context)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
char *err = NULL;
|
2017-12-18 11:38:30 +01:00
|
|
|
int ret = 0;
|
2019-05-23 09:56:02 +02:00
|
|
|
struct pw_command *command;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
|
|
|
|
spa_list_for_each(command, &config->commands, link) {
|
2019-12-10 18:19:56 +01:00
|
|
|
if ((ret = pw_command_run(command, context, &err)) < 0) {
|
2017-09-15 14:47:54 +02:00
|
|
|
pw_log_warn("could not run command %s: %s", command->args[0], err);
|
2017-05-26 08:05:01 +02:00
|
|
|
free(err);
|
2018-05-14 13:07:55 +02:00
|
|
|
break;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 09:56:02 +02:00
|
|
|
spa_list_consume(command, &config->commands, link)
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_command_free(command);
|
|
|
|
|
|
|
|
|
|
return ret;
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|