add new module-console-kit which tracks ck sessions to avoid termination when there...
[platform/upstream/pulseaudio.git] / src / modules / module-console-kit.c
1 /* $Id$ */
2
3 /***
4     This file is part of PulseAudio.
5
6     Copyright 2008 Lennart Poettering
7
8     PulseAudio is free software; you can redistribute it and/or modify
9     it under the terms of the GNU Lesser General Public License as published
10     by the Free Software Foundation; either version 2 of the License,
11     or (at your option) any later version.
12
13     PulseAudio is distributed in the hope that it will be useful, but
14     WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     General Public License for more details.
17
18     You should have received a copy of the GNU Lesser General Public License
19     along with PulseAudio; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21     USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <pulse/xmalloc.h>
38 #include <pulse/timeval.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/hashmap.h>
44 #include <pulsecore/idxset.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/namereg.h>
47 #include <pulsecore/core-scache.h>
48 #include <pulsecore/modargs.h>
49
50 #include <hal/libhal.h>
51
52 #include "dbus-util.h"
53 #include "module-console-kit-symdef.h"
54
55 PA_MODULE_AUTHOR("Lennart Poettering");
56 PA_MODULE_DESCRIPTION("Create a client for each ConsoleKit session of this user");
57 PA_MODULE_VERSION(PACKAGE_VERSION);
58 PA_MODULE_LOAD_ONCE(TRUE);
59
60 static const char* const valid_modargs[] = {
61     NULL
62 };
63
64 struct session {
65     char *id;
66     pa_client *client;
67 };
68
69 struct userdata {
70     pa_core *core;
71     pa_dbus_connection *connection;
72     pa_hashmap *sessions;
73 };
74
75 static void add_session(struct userdata *u, const char *id) {
76     DBusError error;
77     DBusMessage *m = NULL, *reply = NULL;
78     int32_t uid;
79     struct session *session;
80     char *t;
81
82     dbus_error_init (&error);
83
84     if (pa_hashmap_get(u->sessions, id)) {
85         pa_log_warn("Duplicate session %s, ignoring.", id);
86         return;
87     }
88
89     if (!(m = dbus_message_new_method_call("org.freedesktop.ConsoleKit", id, "org.freedesktop.ConsoleKit.Session", "GetUnixUser"))) {
90         pa_log("Failed to allocate GetUnixUser() method call.");
91         goto fail;
92     }
93
94     if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
95         pa_log("GetUnixUser() call failed: %s: %s", error.name, error.message);
96         goto fail;
97     }
98
99     /* FIXME: Why is this in int32? and not an uint32? */
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     /* 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     t = pa_sprintf_malloc("ConsoleKit Session %s", id);
113     session->client = pa_client_new(u->core, __FILE__, t);
114     pa_xfree(t);
115
116     pa_proplist_sets(session->client->proplist, "console-kit.session", id);
117
118     pa_hashmap_put(u->sessions, session->id, session);
119
120     pa_log_debug("Added new session %s", id);
121
122 fail:
123
124     if (m)
125         dbus_message_unref(m);
126
127     if (reply)
128         dbus_message_unref(reply);
129
130     dbus_error_free(&error);
131 }
132
133 static void free_session(struct session *session) {
134     pa_assert(session);
135
136     pa_log_debug("Removing session %s", session->id);
137
138     pa_client_free(session->client);
139     pa_xfree(session->id);
140     pa_xfree(session);
141 }
142
143 static void remove_session(struct userdata *u, const char *id) {
144     struct session *session;
145
146     if (!(session = pa_hashmap_remove(u->sessions, id)))
147         return;
148
149     free_session(session);
150 }
151
152 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata) {
153     struct userdata *u = userdata;
154     DBusError error;
155     const char *path;
156
157     pa_assert(bus);
158     pa_assert(message);
159     pa_assert(u);
160
161     dbus_error_init(&error);
162
163     pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
164                  dbus_message_get_interface(message),
165                  dbus_message_get_path(message),
166                  dbus_message_get_member(message));
167
168     if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionAdded")) {
169
170         if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
171             pa_log_error("Failed to parse SessionAdded message: %s: %s", error.name, error.message);
172             goto finish;
173         }
174
175         add_session(u, path);
176
177     } else if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionRemoved")) {
178
179         if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
180             pa_log_error("Failed to parse SessionRemoved message: %s: %s", error.name, error.message);
181             goto finish;
182         }
183
184         remove_session(u, path);
185     }
186
187 finish:
188     dbus_error_free(&error);
189
190     return DBUS_HANDLER_RESULT_HANDLED;
191 }
192
193 static int get_session_list(struct userdata *u) {
194     DBusError error;
195     DBusMessage *m = NULL, *reply = NULL;
196     uint32_t uid;
197     DBusMessageIter iter, sub;
198     int ret = -1;
199
200     pa_assert(u);
201
202     dbus_error_init(&error);
203
204     if (!(m = dbus_message_new_method_call("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "GetSessionsForUnixUser"))) {
205         pa_log("Failed to allocate GetSessionsForUnixUser() method call.");
206         goto fail;
207     }
208
209     uid = (uint32_t) getuid();
210     if (!(dbus_message_append_args(m, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID))) {
211         pa_log("Failed to append arguments to GetSessionsForUnixUser() method call.");
212         goto fail;
213     }
214
215     if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
216         pa_log("GetSessionsForUnixUser() call failed: %s: %s", error.name, error.message);
217         goto fail;
218     }
219
220     dbus_message_iter_init(reply, &iter);
221
222     if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
223         dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_OBJECT_PATH) {
224         pa_log("Failed to parse GetSessionsForUnixUser() result.");
225         goto fail;
226     }
227
228     dbus_message_iter_recurse(&iter, &sub);
229
230     for (;;) {
231         int at;
232         const char *id;
233
234         if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
235             break;
236
237         assert(at == DBUS_TYPE_OBJECT_PATH);
238         dbus_message_iter_get_basic(&sub, &id);
239
240         add_session(u, id);
241
242         dbus_message_iter_next(&sub);
243     }
244
245     ret = 0;
246
247 fail:
248
249     if (m)
250         dbus_message_unref(m);
251
252     if (reply)
253         dbus_message_unref(reply);
254
255     dbus_error_free(&error);
256
257     return ret;
258 }
259
260 int pa__init(pa_module*m) {
261     DBusError error;
262     pa_dbus_connection *connection;
263     struct userdata *u = NULL;
264     pa_modargs *ma;
265
266     pa_assert(m);
267
268     dbus_error_init(&error);
269
270     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
271         pa_log("Failed to parse module arguments");
272         goto fail;
273     }
274
275     if (!(connection = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
276
277         if (connection)
278             pa_dbus_connection_unref(connection);
279
280         pa_log_error("Unable to contact D-Bus system bus: %s: %s", error.name, error.message);
281         goto fail;
282     }
283
284     m->userdata = u = pa_xnew(struct userdata, 1);
285     u->core = m->core;
286     u->connection = connection;
287     u->sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
288
289     if (!dbus_connection_add_filter(pa_dbus_connection_get(connection), filter_cb, u, NULL)) {
290         pa_log_error("Failed to add filter function");
291         goto fail;
292     }
293
294     dbus_bus_add_match(pa_dbus_connection_get(connection), "type='signal',sender='org.freedesktop.ConsoleKit', interface='org.freedesktop.ConsoleKit.Seat'", &error);
295     if (dbus_error_is_set(&error)) {
296         pa_log_error("Unable to subscribe to ConsoleKit signals: %s: %s", error.name, error.message);
297         goto fail;
298     }
299
300     if (get_session_list(u) < 0)
301         goto fail;
302
303     pa_modargs_free(ma);
304
305     return 0;
306
307 fail:
308     if (ma)
309         pa_modargs_free(ma);
310
311     dbus_error_free(&error);
312     pa__done(m);
313
314     return -1;
315 }
316
317
318 void pa__done(pa_module *m) {
319     struct userdata *u;
320     struct session *session;
321
322     pa_assert(m);
323
324     if (!(u = m->userdata))
325         return;
326
327     while ((session = pa_hashmap_steal_first(u->sessions)))
328         free_session(session);
329
330     if (u->connection)
331         pa_dbus_connection_unref(u->connection);
332
333     pa_xfree(u);
334 }