summaryrefslogtreecommitdiff
path: root/src/daemon/daemon-config.c
blob: f6de2282c87bb1bf5ac933270f47dcae181c7436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* PipeWire
 * Copyright © 2016 Axis Communications <dev-gstreamer@axis.com>
 *	@author Linus Svensson <linus.svensson@axis.com>
 * Copyright © 2018 Wim Taymans
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdio.h>
#include <errno.h>

#include <pipewire/pipewire.h>
#include <pipewire/command.h>
#include <pipewire/private.h>

#include "daemon/daemon-config.h"

#define DEFAULT_CONFIG_FILE PIPEWIRE_CONFIG_DIR "/pipewire.conf"

static int
parse_line(struct pw_daemon_config *config,
	   const char *filename, char *line, unsigned int lineno, char **err)
{
	struct pw_command *command = NULL;
	char *p;
	int ret = 0;
	char *local_err = NULL;

	/* search for comments */
	if ((p = strchr(line, '#')))
		*p = '\0';

	/* remove whitespaces */
	pw_strip(line, "\n\r \t");

	if (*line == '\0')	/* empty line */
		return 0;

	if ((command = pw_command_parse(line, &local_err)) == NULL) {
		asprintf(err, "%s:%u: %s", filename, lineno, local_err);
		free(local_err);
		ret = -EINVAL;
	} else {
		spa_list_append(&config->commands, &command->link);
	}

	return ret;
}

/**
 * pw_daemon_config_new:
 *
 * Returns a new empty #struct pw_daemon_config.
 */
struct pw_daemon_config *pw_daemon_config_new(void)
{
	struct pw_daemon_config *config;

	config = calloc(1, sizeof(struct pw_daemon_config));
	spa_list_init(&config->commands);

	return config;
}

/**
 * pw_daemon_config_free:
 * @config: A #struct pw_daemon_config
 *
 * Free all resources associated to @config.
 */
void pw_daemon_config_free(struct pw_daemon_config *config)
{
	struct pw_command *cmd, *tmp;

	spa_list_for_each_safe(cmd, tmp, &config->commands, link)
	    pw_command_free(cmd);

	free(config);
}

/**
 * pw_daemon_config_load_file:
 * @config: A #struct pw_daemon_config
 * @filename: A filename
 * @err: Return location for an error string
 *
 * Loads PipeWire config from @filename.
 *
 * Returns: 0 on success, otherwise < 0 and @err is set.
 */
int pw_daemon_config_load_file(struct pw_daemon_config *config, const char *filename, char **err)
{
	unsigned int line;
	FILE *f;
	char buf[4096];

	pw_log_debug("deamon-config %p: loading configuration file '%s'", config, filename);

	if ((f = fopen(filename, "r")) == NULL) {
		asprintf(err, "failed to open configuration file '%s': %s", filename,
			 strerror(errno));
		goto open_error;
	}

	line = 0;

	while (!feof(f)) {
		if (!fgets(buf, sizeof(buf), f)) {
			if (feof(f))
				break;

			asprintf(err, "failed to read configuration file '%s': %s",
				 filename, strerror(errno));
			goto read_error;
		}

		line++;

		if (parse_line(config, filename, buf, line, err) != 0)
			goto parse_failed;
	}
	fclose(f);

	return 0;

      parse_failed:
      read_error:
	fclose(f);
      open_error:
	return -EINVAL;
}

/**
 * pw_daemon_config_load:
 * @config: A #struct pw_daemon_config
 * @err: Return location for a #GError, or %NULL
 *
 * Loads the default config file for PipeWire. The filename can be overridden with
 * an evironment variable PIPEWIRE_CONFIG_FILE.
 *
 * Return: 0 on success, otherwise < 0 and @err is set.
 */
int pw_daemon_config_load(struct pw_daemon_config *config, char **err)
{
	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);
}

/**
 * pw_daemon_config_run_commands:
 * @config: A #struct pw_daemon_config
 * @core: A #struct pw_core
 *
 * Run all commands that have been parsed. The list of commands will be cleared
 * when this function has been called.
 *
 * Returns: 0 if all commands where executed with success, otherwise < 0.
 */
int pw_daemon_config_run_commands(struct pw_daemon_config *config, struct pw_core *core)
{
	char *err = NULL;
	int ret = 0;
	struct pw_command *command, *tmp;

	spa_list_for_each(command, &config->commands, link) {
		if ((ret = pw_command_run(command, core, &err)) < 0) {
			pw_log_warn("could not run command %s: %s", command->args[0], err);
			free(err);
			break;
		}
	}

	spa_list_for_each_safe(command, tmp, &config->commands, link)
		pw_command_free(command);

	return ret;
}