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