]> code.delx.au - pulseaudio/blob - src/modules/module-console-kit.c
hashmap: Use pa_free_cb_t instead of pa_free2_cb_t
[pulseaudio] / src / modules / module-console-kit.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 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 #ifdef HAVE_SYSTEMD
34 #include <systemd/sd-login.h>
35 #include <systemd/sd-daemon.h>
36 #endif
37
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/module.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/hashmap.h>
43 #include <pulsecore/idxset.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/dbus-shared.h>
46
47 #include "module-console-kit-symdef.h"
48
49 PA_MODULE_AUTHOR("Lennart Poettering");
50 PA_MODULE_DESCRIPTION("Create a client for each ConsoleKit session of this user");
51 PA_MODULE_VERSION(PACKAGE_VERSION);
52 PA_MODULE_LOAD_ONCE(TRUE);
53
54 static const char* const valid_modargs[] = {
55 NULL
56 };
57
58 struct session {
59 char *id;
60 pa_client *client;
61 };
62
63 struct userdata {
64 pa_module *module;
65 pa_core *core;
66 pa_dbus_connection *connection;
67 pa_hashmap *sessions;
68 pa_bool_t filter_added;
69 };
70
71 static void add_session(struct userdata *u, const char *id) {
72 DBusError error;
73 DBusMessage *m = NULL, *reply = NULL;
74 uint32_t uid;
75 struct session *session;
76 pa_client_new_data data;
77
78 dbus_error_init(&error);
79
80 if (pa_hashmap_get(u->sessions, id)) {
81 pa_log_warn("Duplicate session %s, ignoring.", id);
82 return;
83 }
84
85 if (!(m = dbus_message_new_method_call("org.freedesktop.ConsoleKit", id, "org.freedesktop.ConsoleKit.Session", "GetUnixUser"))) {
86 pa_log("Failed to allocate GetUnixUser() method call.");
87 goto fail;
88 }
89
90 if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
91 pa_log("GetUnixUser() call failed: %s: %s", error.name, error.message);
92 goto fail;
93 }
94
95 /* CK 0.3 this changed from int32 to uint32 */
96 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID)) {
97 dbus_error_free(&error);
98
99 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
100 pa_log("Failed to parse GetUnixUser() result: %s: %s", error.name, error.message);
101 goto fail;
102 }
103 }
104
105 /* We only care about our own sessions */
106 if ((uid_t) uid != getuid())
107 goto fail;
108
109 session = pa_xnew(struct session, 1);
110 session->id = pa_xstrdup(id);
111
112 pa_client_new_data_init(&data);
113 data.module = u->module;
114 data.driver = __FILE__;
115 pa_proplist_setf(data.proplist, PA_PROP_APPLICATION_NAME, "ConsoleKit Session %s", id);
116 pa_proplist_sets(data.proplist, "console-kit.session", id);
117 session->client = pa_client_new(u->core, &data);
118 pa_client_new_data_done(&data);
119
120 if (!session->client) {
121 pa_xfree(session->id);
122 pa_xfree(session);
123 goto fail;
124 }
125
126 pa_hashmap_put(u->sessions, session->id, session);
127
128 pa_log_debug("Added new session %s", id);
129
130 fail:
131
132 if (m)
133 dbus_message_unref(m);
134
135 if (reply)
136 dbus_message_unref(reply);
137
138 dbus_error_free(&error);
139 }
140
141 static void free_session(struct session *session) {
142 pa_assert(session);
143
144 pa_log_debug("Removing session %s", session->id);
145
146 pa_client_free(session->client);
147 pa_xfree(session->id);
148 pa_xfree(session);
149 }
150
151 static void remove_session(struct userdata *u, const char *id) {
152 struct session *session;
153
154 if (!(session = pa_hashmap_remove(u->sessions, id)))
155 return;
156
157 free_session(session);
158 }
159
160 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata) {
161 struct userdata *u = userdata;
162 DBusError error;
163 const char *path;
164
165 pa_assert(bus);
166 pa_assert(message);
167 pa_assert(u);
168
169 dbus_error_init(&error);
170
171 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
172 dbus_message_get_interface(message),
173 dbus_message_get_path(message),
174 dbus_message_get_member(message));
175
176 if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionAdded")) {
177
178 /* CK API changed to match spec in 0.3 */
179 if (!dbus_message_get_args(message, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
180 dbus_error_free(&error);
181
182 if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID)) {
183 pa_log_error("Failed to parse SessionAdded message: %s: %s", error.name, error.message);
184 goto finish;
185 }
186 }
187
188 add_session(u, path);
189
190 } else if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionRemoved")) {
191
192 /* CK API changed to match spec in 0.3 */
193 if (!dbus_message_get_args(message, &error, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID)) {
194 dbus_error_free(&error);
195
196 if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID)) {
197 pa_log_error("Failed to parse SessionRemoved message: %s: %s", error.name, error.message);
198 goto finish;
199 }
200 }
201
202 remove_session(u, path);
203 }
204
205 finish:
206 dbus_error_free(&error);
207
208 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
209 }
210
211 static int get_session_list(struct userdata *u) {
212 DBusError error;
213 DBusMessage *m = NULL, *reply = NULL;
214 uint32_t uid;
215 DBusMessageIter iter, sub;
216 int ret = -1;
217
218 pa_assert(u);
219
220 dbus_error_init(&error);
221
222 if (!(m = dbus_message_new_method_call("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "GetSessionsForUnixUser"))) {
223 pa_log("Failed to allocate GetSessionsForUnixUser() method call.");
224 goto fail;
225 }
226
227 uid = (uint32_t) getuid();
228 if (!(dbus_message_append_args(m, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID))) {
229 pa_log("Failed to append arguments to GetSessionsForUnixUser() method call.");
230 goto fail;
231 }
232
233 if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
234 pa_log("GetSessionsForUnixUser() call failed: %s: %s", error.name, error.message);
235 goto fail;
236 }
237
238 dbus_message_iter_init(reply, &iter);
239
240 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
241 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_OBJECT_PATH) {
242 pa_log("Failed to parse GetSessionsForUnixUser() result.");
243 goto fail;
244 }
245
246 dbus_message_iter_recurse(&iter, &sub);
247
248 for (;;) {
249 int at;
250 const char *id;
251
252 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
253 break;
254
255 assert(at == DBUS_TYPE_OBJECT_PATH);
256 dbus_message_iter_get_basic(&sub, &id);
257
258 add_session(u, id);
259
260 dbus_message_iter_next(&sub);
261 }
262
263 ret = 0;
264
265 fail:
266
267 if (m)
268 dbus_message_unref(m);
269
270 if (reply)
271 dbus_message_unref(reply);
272
273 dbus_error_free(&error);
274
275 return ret;
276 }
277
278 int pa__init(pa_module*m) {
279 DBusError error;
280 pa_dbus_connection *connection;
281 struct userdata *u = NULL;
282 pa_modargs *ma;
283
284 pa_assert(m);
285
286 dbus_error_init(&error);
287
288 #ifdef HAVE_SYSTEMD
289 /* If systemd support is enabled and we boot on systemd we
290 shouldn't watch ConsoleKit but systemd's logind service. */
291 if (sd_booted() > 0)
292 return 0;
293 #endif
294
295 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
296 pa_log("Failed to parse module arguments");
297 goto fail;
298 }
299
300 if (!(connection = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
301
302 if (connection)
303 pa_dbus_connection_unref(connection);
304
305 pa_log_error("Unable to contact D-Bus system bus: %s: %s", error.name, error.message);
306 goto fail;
307 }
308
309 m->userdata = u = pa_xnew0(struct userdata, 1);
310 u->core = m->core;
311 u->module = m;
312 u->connection = connection;
313 u->sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
314
315 if (!dbus_connection_add_filter(pa_dbus_connection_get(connection), filter_cb, u, NULL)) {
316 pa_log_error("Failed to add filter function");
317 goto fail;
318 }
319
320 u->filter_added = TRUE;
321
322 if (pa_dbus_add_matches(
323 pa_dbus_connection_get(connection), &error,
324 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionAdded'",
325 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionRemoved'", NULL) < 0) {
326 pa_log_error("Unable to subscribe to ConsoleKit signals: %s: %s", error.name, error.message);
327 goto fail;
328 }
329
330 if (get_session_list(u) < 0)
331 goto fail;
332
333 pa_modargs_free(ma);
334
335 return 0;
336
337 fail:
338 if (ma)
339 pa_modargs_free(ma);
340
341 dbus_error_free(&error);
342 pa__done(m);
343
344 return -1;
345 }
346
347
348 void pa__done(pa_module *m) {
349 struct userdata *u;
350
351 pa_assert(m);
352
353 if (!(u = m->userdata))
354 return;
355
356 if (u->sessions)
357 pa_hashmap_free(u->sessions, (pa_free_cb_t) free_session);
358
359 if (u->connection) {
360 pa_dbus_remove_matches(
361 pa_dbus_connection_get(u->connection),
362 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionAdded'",
363 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionRemoved'", NULL);
364
365 if (u->filter_added)
366 dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u);
367
368 pa_dbus_connection_unref(u->connection);
369 }
370
371 pa_xfree(u);
372 }