echo-cancel-test: Enable debug log level
[platform/upstream/pulseaudio.git] / src / modules / module-systemd-login.c
1 /***
2     This file is part of PulseAudio.
3
4     Copyright 2012 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, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19     USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32
33 #include <systemd/sd-login.h>
34 #include <systemd/sd-daemon.h>
35
36 #include <pulse/xmalloc.h>
37
38 #include <pulsecore/module.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/hashmap.h>
41 #include <pulsecore/idxset.h>
42 #include <pulsecore/modargs.h>
43
44 #include "module-systemd-login-symdef.h"
45
46 PA_MODULE_AUTHOR("Lennart Poettering");
47 PA_MODULE_DESCRIPTION("Create a client for each login session of this user");
48 PA_MODULE_VERSION(PACKAGE_VERSION);
49 PA_MODULE_LOAD_ONCE(TRUE);
50
51 static const char* const valid_modargs[] = {
52     NULL
53 };
54
55 struct session {
56     char *id;
57     pa_client *client;
58 };
59
60 struct userdata {
61     pa_module *module;
62     pa_core *core;
63     pa_hashmap *sessions, *previous_sessions;
64     sd_login_monitor *monitor;
65     pa_io_event *io;
66 };
67
68 static int add_session(struct userdata *u, const char *id) {
69     struct session *session;
70     pa_client_new_data data;
71
72     session = pa_xnew(struct session, 1);
73     session->id = pa_xstrdup(id);
74
75     pa_client_new_data_init(&data);
76     data.module = u->module;
77     data.driver = __FILE__;
78     pa_proplist_setf(data.proplist, PA_PROP_APPLICATION_NAME, "Login Session %s", id);
79     pa_proplist_sets(data.proplist, "systemd-login.session", id);
80     session->client = pa_client_new(u->core, &data);
81     pa_client_new_data_done(&data);
82
83     if (!session->client) {
84         pa_xfree(session->id);
85         pa_xfree(session);
86         return -1;
87     }
88
89     pa_hashmap_put(u->sessions, session->id, session);
90
91     pa_log_debug("Added new session %s", id);
92     return 0;
93 }
94
95 static void free_session(struct session *session) {
96     pa_assert(session);
97
98     pa_log_debug("Removing session %s", session->id);
99
100     pa_client_free(session->client);
101     pa_xfree(session->id);
102     pa_xfree(session);
103 }
104
105 static int get_session_list(struct userdata *u) {
106     int r;
107     char **sessions;
108     pa_hashmap *h;
109     struct session *o;
110
111     pa_assert(u);
112
113     r = sd_uid_get_sessions(getuid(), 0, &sessions);
114     if (r < 0)
115         return -1;
116
117     /* We copy all sessions that still exist from one hashmap to the
118      * other and then flush the remaining ones */
119
120     h = u->previous_sessions;
121     u->previous_sessions = u->sessions;
122     u->sessions = h;
123
124     if (sessions) {
125         char **s;
126
127         /* Note that the sessions array is allocated with libc's
128          * malloc()/free() calls, hence do not use pa_xfree() to free
129          * this here. */
130
131         for (s = sessions; *s; s++) {
132             o = pa_hashmap_remove(u->previous_sessions, *s);
133             if (o)
134                 pa_hashmap_put(u->sessions, o->id, o);
135             else
136                 add_session(u, *s);
137
138             free(*s);
139         }
140
141         free(sessions);
142     }
143
144     pa_hashmap_remove_all(u->previous_sessions, (pa_free_cb_t) free_session);
145
146     return 0;
147 }
148
149 static void monitor_cb(
150         pa_mainloop_api*a,
151         pa_io_event* e,
152         int fd,
153         pa_io_event_flags_t events,
154         void *userdata) {
155
156     struct userdata *u = userdata;
157
158     pa_assert(u);
159
160     sd_login_monitor_flush(u->monitor);
161     get_session_list(u);
162 }
163
164 int pa__init(pa_module *m) {
165     struct userdata *u = NULL;
166     pa_modargs *ma;
167     sd_login_monitor *monitor = NULL;
168     int r;
169
170     pa_assert(m);
171
172     /* If we are not actually booting with systemd become a NOP */
173     if (sd_booted() <= 0)
174         return 0;
175
176     ma = pa_modargs_new(m->argument, valid_modargs);
177     if (!ma) {
178         pa_log("Failed to parse module arguments");
179         goto fail;
180     }
181
182     r = sd_login_monitor_new("session", &monitor);
183     if (r < 0) {
184         pa_log("Failed to create session monitor: %s", strerror(-r));
185         goto fail;
186     }
187
188     m->userdata = u = pa_xnew0(struct userdata, 1);
189     u->core = m->core;
190     u->module = m;
191     u->sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
192     u->previous_sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
193     u->monitor = monitor;
194
195     u->io = u->core->mainloop->io_new(u->core->mainloop, sd_login_monitor_get_fd(monitor), PA_IO_EVENT_INPUT, monitor_cb, u);
196
197     if (get_session_list(u) < 0)
198         goto fail;
199
200     pa_modargs_free(ma);
201
202     return 0;
203
204 fail:
205     if (ma)
206         pa_modargs_free(ma);
207
208     pa__done(m);
209
210     return -1;
211 }
212
213 void pa__done(pa_module *m) {
214     struct userdata *u;
215     struct session *session;
216
217     pa_assert(m);
218
219     u = m->userdata;
220     if (!u)
221         return;
222
223     if (u->sessions) {
224         pa_hashmap_free(u->sessions, (pa_free_cb_t) free_session);
225         pa_hashmap_free(u->previous_sessions, (pa_free_cb_t) free_session);
226     }
227
228     if (u->io)
229         m->core->mainloop->io_free(u->io);
230
231     if (u->monitor)
232         sd_login_monitor_unref(u->monitor);
233
234     pa_xfree(u);
235 }