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