1/***
2  This file is part of PulseAudio.
3
4  Copyright 2006 Lennart Poettering
5
6  PulseAudio is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation; either version 2.1 of the License,
9  or (at your option) any later version.
10
11  PulseAudio is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <sys/types.h>
25#include <sys/wait.h>
26
27#include <pulsecore/core-error.h>
28#include <pulsecore/core-util.h>
29#include <pulsecore/start-child.h>
30
31#include "../stdin-util.h"
32
33PA_MODULE_AUTHOR("Sylvain Baubeau");
34PA_MODULE_DESCRIPTION("GSettings Adapter");
35PA_MODULE_VERSION(PACKAGE_VERSION);
36PA_MODULE_LOAD_ONCE(true);
37
38int pa__init(pa_module*m) {
39    struct userdata *u;
40    int r;
41
42    u = pa_xnew(struct userdata, 1);
43    u->core = m->core;
44    u->module = m;
45    m->userdata = u;
46    u->module_infos = pa_hashmap_new_full(pa_idxset_string_hash_func, pa_idxset_string_compare_func, NULL, (pa_free_cb_t) module_info_free);
47    u->pid = (pid_t) -1;
48    u->fd = -1;
49    u->fd_type = 0;
50    u->io_event = NULL;
51    u->buf_fill = 0;
52
53    if ((u->fd = pa_start_child_for_read(
54#if defined(__linux__) && defined(HAVE_RUNNING_FROM_BUILD_TREE)
55#ifdef MESON_BUILD
56                              pa_run_from_build_tree() ? PA_BUILDDIR PA_PATH_SEP "src" PA_PATH_SEP "modules" PA_PATH_SEP "gsettings" PA_PATH_SEP "gsettings-helper" :
57#else
58                              pa_run_from_build_tree() ? PA_BUILDDIR "/gsettings-helper" :
59#endif
60#endif
61                 PA_GSETTINGS_HELPER, NULL, &u->pid)) < 0)
62        goto fail;
63
64    u->io_event = m->core->mainloop->io_new(
65            m->core->mainloop,
66            u->fd,
67            PA_IO_EVENT_INPUT | PA_IO_EVENT_HANGUP | PA_IO_EVENT_ERROR,
68            io_event_cb,
69            u);
70
71    do {
72        if ((r = handle_event(u)) < 0)
73            goto fail;
74
75        /* Read until the client signalled us that it is ready with
76         * initialization */
77    } while (r != 1);
78
79    return 0;
80
81fail:
82    pa__done(m);
83    return -1;
84}
85
86void pa__done(pa_module*m) {
87    struct userdata *u;
88
89    pa_assert(m);
90
91    if (!(u = m->userdata))
92        return;
93
94    if (u->pid != (pid_t) -1) {
95        kill(u->pid, SIGTERM);
96
97        for (;;) {
98            if (waitpid(u->pid, NULL, 0) >= 0)
99                break;
100
101            if (errno != EINTR) {
102                pa_log("waitpid() failed: %s", pa_cstrerror(errno));
103                break;
104            }
105        }
106    }
107
108    if (u->io_event)
109        m->core->mainloop->io_free(u->io_event);
110
111    if (u->fd >= 0)
112        pa_close(u->fd);
113
114    if (u->module_infos)
115        pa_hashmap_free(u->module_infos);
116
117    pa_xfree(u);
118}
119