Debian Codegraph @

lib/wp/conf.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2022 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "conf.h"
10 #include "log.h"
11 #include "json-utils.h"
12 #include "base-dirs.h"
13 #include "error.h"
14
15 #include <pipewire/pipewire.h>
16 #include <spa/utils/result.h>
17
18 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-conf")
19
20 #define OVERRIDE_SECTION_PREFIX "override."
21
22 /*! \defgroup wpconf WpConf */
23 /*!
24 * \struct WpConf
25 *
26 * WpConf allows accessing the different sections of the wireplumber
27 * configuration.
28 */
29
30 typedef struct _WpConfSection WpConfSection;
31 struct _WpConfSection
32 {
33 gchar *name;
34 WpSpaJson *value;
35 gchar *location;
36 };
37
38 static void
39 wp_conf_section_clear (WpConfSection * section)
40 {
41 g_free (section->name);
42 g_clear_pointer (&section->value, wp_spa_json_unref);
43 g_free (section->location);
44 }
45 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (WpConfSection, wp_conf_section_clear)
46
47 struct _WpConf
48 {
49 GObject parent;
50
51 /* Props */
52 gchar *name;
53 WpProperties *properties;
54
55 /* Private */
56 GArray *conf_sections; /* element-type: WpConfSection */
57 GPtrArray *files; /* element-type: GMappedFile* */
58 };
59
60 enum {
61 PROP_0,
62 PROP_NAME,
63 PROP_PROPERTIES,
64 };
65
66 G_DEFINE_TYPE (WpConf, wp_conf, G_TYPE_OBJECT)
67
68 static void
69 wp_conf_init (WpConf * self)
70 {
71 self->conf_sections = g_array_new (FALSE, FALSE, sizeof (WpConfSection));
72 g_array_set_clear_func (self->conf_sections, (GDestroyNotify) wp_conf_section_clear);
73 self->files = g_ptr_array_new_with_free_func ((GDestroyNotify) g_mapped_file_unref);
74 }
75
76 static void
77 wp_conf_set_property (GObject * object, guint property_id,
78 const GValue * value, GParamSpec * pspec)
79 {
80 WpConf *self = WP_CONF (object);
81
82 switch (property_id) {
83 case PROP_NAME:
84 self->name = g_value_dup_string (value);
85 break;
86 case PROP_PROPERTIES:
87 self->properties = g_value_dup_boxed (value);
88 break;
89 default:
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
91 break;
92 }
93 }
94
95 static void
96 wp_conf_get_property (GObject * object, guint property_id,
97 GValue * value, GParamSpec * pspec)
98 {
99 WpConf *self = WP_CONF (object);
100
101 switch (property_id) {
102 case PROP_NAME:
103 g_value_set_string (value, self->name);
104 break;
105 case PROP_PROPERTIES:
106 g_value_take_boxed (value, wp_properties_copy (self->properties));
107 break;
108 default:
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110 break;
111 }
112 }
113
114 static void
115 wp_conf_finalize (GObject * object)
116 {
117 WpConf *self = WP_CONF (object);
118
119 wp_conf_close (self);
120 g_clear_pointer (&self->properties, wp_properties_unref);
121 g_clear_pointer (&self->conf_sections, g_array_unref);
122 g_clear_pointer (&self->files, g_ptr_array_unref);
123 g_clear_pointer (&self->name, g_free);
124
125 G_OBJECT_CLASS (wp_conf_parent_class)->finalize (object);
126 }
127
128 static void
129 wp_conf_class_init (WpConfClass * klass)
130 {
131 GObjectClass * object_class = G_OBJECT_CLASS (klass);
132
133 object_class->finalize = wp_conf_finalize;
134 object_class->set_property = wp_conf_set_property;
135 object_class->get_property = wp_conf_get_property;
136
137 g_object_class_install_property(object_class, PROP_NAME,
138 g_param_spec_string ("name", "name", "The name of the configuration file",
139 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
140
141 g_object_class_install_property(object_class, PROP_PROPERTIES,
142 g_param_spec_boxed ("properties", "properties", "WpProperties",
143 WP_TYPE_PROPERTIES, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
144 }
145
146 /*!
147 * \brief Creates a new WpConf object
148 *
149 * This does not open the files, it only creates the object. For most use cases,
150 * you should use wp_conf_new_open() instead.
151 *
152 * \ingroup wpconf
153 * \param name the name of the configuration file
154 * \param properties (transfer full) (nullable): a WpProperties with keys
155 * specifying how to load the WpConf object
156 * \returns (transfer full): a new WpConf object
157 */
158 WpConf *
159 wp_conf_new (const gchar * name, WpProperties * properties)
160 {
161 g_return_val_if_fail (name, NULL);
162 g_autoptr (WpProperties) props = properties;
163 return g_object_new (WP_TYPE_CONF, "name", name,
164 "properties", props,
165 NULL);
166 }
167
168 /*!
169 * \brief Creates a new WpConf object and opens the configuration file and its
170 * fragments, keeping them mapped in memory for further access.
171 *
172 * \ingroup wpconf
173 * \param name the name of the configuration file
174 * \param properties (transfer full) (nullable): a WpProperties with keys
175 * specifying how to load the WpConf object
176 * \param error (out) (nullable): return location for a GError, or NULL
177 * \returns (transfer full) (nullable): a new WpConf object, or NULL
178 * if an error occurred
179 */
180 WpConf *
181 wp_conf_new_open (const gchar * name, WpProperties * properties, GError ** error)
182 {
183 g_return_val_if_fail (name, NULL);
184
185 g_autoptr (WpConf) self = wp_conf_new (name, properties);
186 if (!wp_conf_open (self, error))
187 return NULL;
188 return g_steal_pointer (&self);
189 }
190
191 static gboolean
192 detect_old_conf_format (WpConf * self, GMappedFile *file)
193 {
194 const gchar *data = g_mapped_file_get_contents (file);
195 gsize size = g_mapped_file_get_length (file);
196
197 /* wireplumber 0.4 used to have components of type = config/lua */
198 return g_strrstr_len (data, size, "config/lua") ? TRUE : FALSE;
199 }
200
201 static gboolean
202 open_and_load_sections (WpConf * self, const gchar *path, GError ** error)
203 {
204 const gchar *as_section = NULL;
205
206 if (self->properties) {
207 /* as-section="some.name" means that the entire file will be stored as a
208 single JSON value that will be accessible through wp_conf_get_section()
209 using "some.name" - no parsing is done; the value is expected to be a
210 container */
211 as_section = wp_properties_get (self->properties, "as-section");
212 wp_debug_object (self, "Reading config file as single section: %s", as_section);
213 }
214
215 g_autoptr (GMappedFile) file = g_mapped_file_new (path, FALSE, error);
216 if (!file)
217 return FALSE;
218
219 if (!g_mapped_file_get_contents (file) || g_mapped_file_get_length (file) == 0) {
220 wp_notice_object (self, "Ignoring empty configuration file at '%s'", path);
221 return TRUE;
222 }
223
224 /* test if the file is a relic from 0.4 */
225 if (detect_old_conf_format (self, file)) {
226 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT,
227 "The configuration file at '%s' is likely an old WirePlumber 0.4 config "
228 "and is not supported anymore. Try removing it.", path);
229 return FALSE;
230 }
231
232 g_autoptr (GArray) sections = g_array_new (FALSE, FALSE, sizeof (WpConfSection));
233 g_array_set_clear_func (sections, (GDestroyNotify) wp_conf_section_clear);
234
235 g_autoptr (WpSpaJson) json = wp_spa_json_new_wrap_stringn (
236 g_mapped_file_get_contents (file), g_mapped_file_get_length (file));
237
238 if (as_section) {
239 WpConfSection section = { 0, };
240
241 if (!wp_spa_json_is_container (json)) {
242 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT,
243 "invalid single-section config file start (expected an object or array): %.*s",
244 (int) wp_spa_json_get_size (json), wp_spa_json_get_data (json));
245 return FALSE;
246 }
247
248 section.name = g_strdup (as_section);
249 section.value = g_steal_pointer (&json);
250 section.location = g_strdup (path);
251 g_array_append_val (sections, section);
252 }
253 else {
254 g_auto (WpConfSection) section = { 0, };
255 g_autoptr (WpSpaJson) tmp = NULL, toplevel = NULL;
256 g_autoptr (WpSpaJsonParser) parser = wp_spa_json_parser_new_undefined (json);
257
258 /* get the very first token */
259 tmp = wp_spa_json_parser_get_json (parser);
260
261 /* if the top-level token is an object, parse that instead */
262 if (tmp && wp_spa_json_is_object (tmp)) {
263 g_clear_pointer (&parser, wp_spa_json_parser_unref);
264 toplevel = g_steal_pointer (&tmp);
265 parser = wp_spa_json_parser_new_object (toplevel);
266 tmp = wp_spa_json_parser_get_json (parser);
267 }
268
269 while (TRUE) {
270 if (!tmp)
271 break;
272
273 /* if !is_string, but we want to support strings without quotes */
274 if (wp_spa_json_is_container (tmp) ||
275 wp_spa_json_is_int (tmp) ||
276 wp_spa_json_is_float (tmp) ||
277 wp_spa_json_is_boolean (tmp) ||
278 wp_spa_json_is_null (tmp))
279 {
280 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT,
281 "invalid section name (not a string): %.*s",
282 (int) wp_spa_json_get_size (tmp), wp_spa_json_get_data (tmp));
283 return FALSE;
284 }
285
286 section.name = wp_spa_json_parse_string (tmp);
287 g_clear_pointer (&tmp, wp_spa_json_unref);
288
289 /* parse the section contents */
290 tmp = wp_spa_json_parser_get_json (parser);
291 if (!tmp) {
292 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_INVALID_ARGUMENT,
293 "section '%s' has no value", section.name);
294 return FALSE;
295 }
296
297 section.value = g_steal_pointer (&tmp);
298 section.location = g_strdup (path);
299 g_array_append_val (sections, section);
300 memset (&section, 0, sizeof (section));
301
302 /* parse the next section name */
303 tmp = wp_spa_json_parser_get_json (parser);
304 }
305 }
306
307 /* store the mapped file and the sections; note that the stored WpSpaJson
308 still point to the data in the GMappedFile, so this is why we keep the
309 GMappedFile alive */
310 g_ptr_array_add (self->files, g_steal_pointer (&file));
311 g_array_append_vals (self->conf_sections, sections->data, sections->len);
312 g_array_set_clear_func (sections, NULL);
313
314 return TRUE;
315 }
316
317 /*!
318 * \brief Opens the configuration file and its fragments and keeps them
319 * mapped in memory for further access.
320 *
321 * \ingroup wpconf
322 * \param self the configuration
323 * \param error (out)(nullable): return location for a GError, or NULL
324 * \returns TRUE on success, FALSE on error
325 */
326 gboolean
327 wp_conf_open (WpConf * self, GError ** error)
328 {
329 const gchar *no_frags = NULL;
330
331 g_return_val_if_fail (WP_IS_CONF (self), FALSE);
332
333 g_autofree gchar *path = NULL;
334 g_autoptr (WpIterator) iterator = NULL;
335 g_auto (GValue) value = G_VALUE_INIT;
336
337 if (self->properties) {
338 no_frags = wp_properties_get (self->properties, "no-fragments");
339 }
340
341 /*
342 * open the config file - if the path supplied is absolute,
343 * wp_base_dirs_find_file will ignore WP_BASE_DIRS_CONFIGURATION
344 */
345 path = wp_base_dirs_find_file (WP_BASE_DIRS_CONFIGURATION, NULL, self->name);
346 if (path) {
347 wp_info_object (self, "opening config file: %s", path);
348 if (!open_and_load_sections (self, path, error))
349 return FALSE;
350 }
351 g_clear_pointer (&path, g_free);
352
353 /* open the .conf.d/ fragments */
354 if (!no_frags) {
355 path = g_strdup_printf ("%s.d", self->name);
356 iterator = wp_base_dirs_new_files_iterator (WP_BASE_DIRS_CONFIGURATION, path,
357 ".conf");
358
359 for (; wp_iterator_next (iterator, &value); g_value_unset (&value)) {
360 const gchar *filename = g_value_get_string (&value);
361
362 wp_info_object (self, "opening fragment file: %s", filename);
363
364 g_autoptr (GError) e = NULL;
365 if (!open_and_load_sections (self, filename, &e)) {
366 wp_warning_object (self, "failed to open '%s': %s", filename, e->message);
367 continue;
368 }
369 }
370 }
371
372 if (self->files->len == 0) {
373 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
374 "Could not locate configuration file '%s'", self->name);
375 return FALSE;
376 }
377
378 return TRUE;
379 }
380
381 /*!
382 * \brief Closes the configuration file and its fragments
383 *
384 * \ingroup wpconf
385 * \param self the configuration
386 */
387 void
388 wp_conf_close (WpConf * self)
389 {
390 g_return_if_fail (WP_IS_CONF (self));
391
392 g_array_set_size (self->conf_sections, 0);
393 g_ptr_array_set_size (self->files, 0);
394 }
395
396 /*!
397 * \brief Tests if the configuration files are open
398 *
399 * \ingroup wpconf
400 * \param self the configuration
401 * \returns TRUE if the configuration files are open, FALSE otherwise
402 */
403 gboolean
404 wp_conf_is_open (WpConf * self)
405 {
406 g_return_val_if_fail (WP_IS_CONF (self), FALSE);
407 return self->files->len > 0;
408 }
409
410 /*!
411 * \brief Gets the name of the configuration file
412 *
413 * \ingroup wpconf
414 * \param self the configuration
415 * \returns the name of the configuration file
416 */
417 const gchar *
418 wp_conf_get_name (WpConf * self)
419 {
420 g_return_val_if_fail (WP_IS_CONF (self), NULL);
421 return self->name;
422 }
423
424 static WpSpaJson *
425 ensure_merged_section (WpConf * self, const gchar *section)
426 {
427 g_autoptr (WpSpaJson) merged = NULL;
428 WpConfSection *merged_section = NULL;
429
430 /* check if the section is already merged */
431 for (guint i = 0; i < self->conf_sections->len; i++) {
432 WpConfSection *s = &g_array_index (self->conf_sections, WpConfSection, i);
433 if (g_str_equal (s->name, section)) {
434 if (!s->location) {
435 wp_debug_object (self, "section %s is already merged", section);
436 return wp_spa_json_ref (s->value);
437 }
438 }
439 }
440
441 /* Iterate over the sections and merge them */
442 for (guint i = 0; i < self->conf_sections->len; i++) {
443 WpConfSection *s = &g_array_index (self->conf_sections, WpConfSection, i);
444 const gchar *s_name = s->name;
445
446 /* skip the "override." prefix and take a note */
447 gboolean override = g_str_has_prefix (s_name, OVERRIDE_SECTION_PREFIX);
448 if (override)
449 s_name += strlen (OVERRIDE_SECTION_PREFIX);
450
451 if (g_str_equal (s_name, section)) {
452 /* Merge sections if a previous value exists and
453 the 'override.' prefix is not present */
454 if (!override && merged) {
455 g_autoptr (WpSpaJson) new_merged =
456 wp_json_utils_merge_containers (merged, s->value);
457 if (!merged) {
458 wp_warning_object (self,
459 "skipping merge of '%s' from '%s' as JSON containers are not compatible",
460 section, s->location);
461 continue;
462 }
463
464 g_clear_pointer (&merged, wp_spa_json_unref);
465 merged = g_steal_pointer (&new_merged);
466 merged_section = NULL;
467 }
468 /* Otherwise always replace */
469 else {
470 g_clear_pointer (&merged, wp_spa_json_unref);
471 merged = wp_spa_json_ref (s->value);
472 merged_section = s;
473 }
474 }
475 }
476
477 /* cache the result */
478 if (merged_section) {
479 /* if the merged json came from a single location, just clear
480 the location from that WpConfSection to mark it as the result */
481 wp_info_object (self, "section '%s' is used as-is from '%s'", section,
482 merged_section->location);
483 g_clear_pointer (&merged_section->location, g_free);
484 } else if (merged) {
485 /* if the merged json came from multiple locations, create a new
486 WpConfSection to store it */
487 WpConfSection s = { g_strdup (section), wp_spa_json_ref (merged), NULL };
488 g_array_append_val (self->conf_sections, s);
489 wp_info_object (self, "section '%s' is merged from multiple locations",
490 section);
491 } else {
492 wp_info_object (self, "section '%s' is not defined", section);
493 }
494
495 return g_steal_pointer (&merged);
496 }
497
498 /*!
499 * This method will get the JSON value of a specific section from the
500 * configuration. If the same section is defined in multiple locations, the
501 * sections with the same name will be either merged in case of arrays and
502 * objects, or overridden in case of boolean, int, double and strings.
503 *
504 * \ingroup wpconf
505 * \param self the configuration
506 * \param section the section name
507 * \returns (transfer full) (nullable): the JSON value of the section or NULL
508 * if the section does not exist
509 */
510 WpSpaJson *
511 wp_conf_get_section (WpConf *self, const gchar *section)
512 {
513 g_return_val_if_fail (WP_IS_CONF (self), NULL);
514 g_return_val_if_fail (section, NULL);
515
516 return ensure_merged_section (self, section);
517 }
518
519 /*!
520 * \brief Updates the given properties with the values of a specific section
521 * from the configuration.
522 *
523 * \ingroup wpconf
524 * \param self the configuration
525 * \param section the section name
526 * \param props the properties to update
527 * \returns the number of properties updated
528 */
529 gint
530 wp_conf_section_update_props (WpConf *self, const gchar *section,
531 WpProperties *props)
532 {
533 g_autoptr (WpSpaJson) json = NULL;
534
535 g_return_val_if_fail (WP_IS_CONF (self), -1);
536 g_return_val_if_fail (section, -1);
537 g_return_val_if_fail (props, -1);
538
539 json = wp_conf_get_section (self, section);
540 if (!json)
541 return 0;
542 return wp_properties_update_from_json (props, json);
543 }
544
545 #include "private/parse-conf-section.c"
546
547 /*!
548 * \brief Parses standard pw_context sections from \a conf
549 *
550 * \ingroup wpconf
551 * \param self the configuration
552 * \param context the associated pw_context
553 */
554 void
555 wp_conf_parse_pw_context_sections (WpConf * self, struct pw_context * context)
556 {
557 gint res;
558 WpProperties *conf_wp;
559 struct pw_properties *conf_pw;
560
561 g_return_if_fail (WP_IS_CONF (self));
562 g_return_if_fail (context);
563
564 /* convert needed sections into a pipewire-style conf dictionary */
565 conf_wp = wp_properties_new ("config.path", "wpconf", NULL);
566 {
567 g_autoptr (WpSpaJson) j = wp_conf_get_section (self, "context.spa-libs");
568 if (j) {
569 g_autofree gchar *js = wp_spa_json_parse_string (j);
570 wp_properties_set (conf_wp, "context.spa-libs", js);
571 }
572 }
573 {
574 g_autoptr (WpSpaJson) j = wp_conf_get_section (self, "context.modules");
575 if (j) {
576 g_autofree gchar *js = wp_spa_json_parse_string (j);
577 wp_properties_set (conf_wp, "context.modules", js);
578 }
579 }
580 conf_pw = wp_properties_unref_and_take_pw_properties (conf_wp);
581
582 /* parse sections */
583 if ((res = _pw_context_parse_conf_section (context, conf_pw, "context.spa-libs")) < 0)
584 goto error;
585 wp_info_object (self, "parsed %d context.spa-libs items", res);
586
587 if ((res = _pw_context_parse_conf_section (context, conf_pw, "context.modules")) < 0)
588 goto error;
589 if (res > 0)
590 wp_info_object (self, "parsed %d context.modules items", res);
591 else
592 wp_warning_object (self, "no modules loaded from context.modules");
593
594 out:
595 pw_properties_free (conf_pw);
596 return;
597
598 error:
599 wp_critical_object (self, "failed to parse pw_context sections: %s",
600 spa_strerror (res));
601 goto out;
602 }