]> code.delx.au - pulseaudio/blob - src/modules/module-console-kit.c
drop hal inclusion from module-console-kit.c
[pulseaudio] / 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 "dbus-util.h"
51 #include "module-console-kit-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("Create a client for each ConsoleKit session of this user");
55 PA_MODULE_VERSION(PACKAGE_VERSION);
56 PA_MODULE_LOAD_ONCE(TRUE);
57
58 static const char* const valid_modargs[] = {
59 NULL
60 };
61
62 struct session {
63 char *id;
64 pa_client *client;
65 };
66
67 struct userdata {
68 pa_core *core;
69 pa_dbus_connection *connection;
70 pa_hashmap *sessions;
71 };
72
73 static void add_session(struct userdata *u, const char *id) {
74 DBusError error;
75 DBusMessage *m = NULL, *reply = NULL;
76 int32_t uid;
77 struct session *session;
78 char *t;
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 /* FIXME: Why is this in int32? and not an uint32? */
98 if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INT32, &uid, DBUS_TYPE_INVALID)) {
99 pa_log("Failed to parse GetUnixUser() result: %s: %s", error.name, error.message);
100 goto fail;
101 }
102
103 /* We only care about our own sessions */
104 if ((uid_t) uid != getuid())
105 goto fail;
106
107 session = pa_xnew(struct session, 1);
108 session->id = pa_xstrdup(id);
109
110 t = pa_sprintf_malloc("ConsoleKit Session %s", id);
111 session->client = pa_client_new(u->core, __FILE__, t);
112 pa_xfree(t);
113
114 pa_proplist_sets(session->client->proplist, "console-kit.session", id);
115
116 pa_hashmap_put(u->sessions, session->id, session);
117
118 pa_log_debug("Added new session %s", id);
119
120 fail:
121
122 if (m)
123 dbus_message_unref(m);
124
125 if (reply)
126 dbus_message_unref(reply);
127
128 dbus_error_free(&error);
129 }
130
131 static void free_session(struct session *session) {
132 pa_assert(session);
133
134 pa_log_debug("Removing session %s", session->id);
135
136 pa_client_free(session->client);
137 pa_xfree(session->id);
138 pa_xfree(session);
139 }
140
141 static void remove_session(struct userdata *u, const char *id) {
142 struct session *session;
143
144 if (!(session = pa_hashmap_remove(u->sessions, id)))
145 return;
146
147 free_session(session);
148 }
149
150 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata) {
151 struct userdata *u = userdata;
152 DBusError error;
153 const char *path;
154
155 pa_assert(bus);
156 pa_assert(message);
157 pa_assert(u);
158
159 dbus_error_init(&error);
160
161 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
162 dbus_message_get_interface(message),
163 dbus_message_get_path(message),
164 dbus_message_get_member(message));
165
166 if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionAdded")) {
167
168 if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
169 pa_log_error("Failed to parse SessionAdded message: %s: %s", error.name, error.message);
170 goto finish;
171 }
172
173 add_session(u, path);
174
175 } else if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionRemoved")) {
176
177 if (!dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
178 pa_log_error("Failed to parse SessionRemoved message: %s: %s", error.name, error.message);
179 goto finish;
180 }
181
182 remove_session(u, path);
183 }
184
185 finish:
186 dbus_error_free(&error);
187
188 return DBUS_HANDLER_RESULT_HANDLED;
189 }
190
191 static int get_session_list(struct userdata *u) {
192 DBusError error;
193 DBusMessage *m = NULL, *reply = NULL;
194 uint32_t uid;
195 DBusMessageIter iter, sub;
196 int ret = -1;
197
198 pa_assert(u);
199
200 dbus_error_init(&error);
201
202 if (!(m = dbus_message_new_method_call("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "GetSessionsForUnixUser"))) {
203 pa_log("Failed to allocate GetSessionsForUnixUser() method call.");
204 goto fail;
205 }
206
207 uid = (uint32_t) getuid();
208 if (!(dbus_message_append_args(m, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID))) {
209 pa_log("Failed to append arguments to GetSessionsForUnixUser() method call.");
210 goto fail;
211 }
212
213 if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
214 pa_log("GetSessionsForUnixUser() call failed: %s: %s", error.name, error.message);
215 goto fail;
216 }
217
218 dbus_message_iter_init(reply, &iter);
219
220 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
221 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_OBJECT_PATH) {
222 pa_log("Failed to parse GetSessionsForUnixUser() result.");
223 goto fail;
224 }
225
226 dbus_message_iter_recurse(&iter, &sub);
227
228 for (;;) {
229 int at;
230 const char *id;
231
232 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
233 break;
234
235 assert(at == DBUS_TYPE_OBJECT_PATH);
236 dbus_message_iter_get_basic(&sub, &id);
237
238 add_session(u, id);
239
240 dbus_message_iter_next(&sub);
241 }
242
243 ret = 0;
244
245 fail:
246
247 if (m)
248 dbus_message_unref(m);
249
250 if (reply)
251 dbus_message_unref(reply);
252
253 dbus_error_free(&error);
254
255 return ret;
256 }
257
258 int pa__init(pa_module*m) {
259 DBusError error;
260 pa_dbus_connection *connection;
261 struct userdata *u = NULL;
262 pa_modargs *ma;
263
264 pa_assert(m);
265
266 dbus_error_init(&error);
267
268 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
269 pa_log("Failed to parse module arguments");
270 goto fail;
271 }
272
273 if (!(connection = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
274
275 if (connection)
276 pa_dbus_connection_unref(connection);
277
278 pa_log_error("Unable to contact D-Bus system bus: %s: %s", error.name, error.message);
279 goto fail;
280 }
281
282 m->userdata = u = pa_xnew(struct userdata, 1);
283 u->core = m->core;
284 u->connection = connection;
285 u->sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
286
287 if (!dbus_connection_add_filter(pa_dbus_connection_get(connection), filter_cb, u, NULL)) {
288 pa_log_error("Failed to add filter function");
289 goto fail;
290 }
291
292 dbus_bus_add_match(pa_dbus_connection_get(connection), "type='signal',sender='org.freedesktop.ConsoleKit', interface='org.freedesktop.ConsoleKit.Seat'", &error);
293 if (dbus_error_is_set(&error)) {
294 pa_log_error("Unable to subscribe to ConsoleKit signals: %s: %s", error.name, error.message);
295 goto fail;
296 }
297
298 if (get_session_list(u) < 0)
299 goto fail;
300
301 pa_modargs_free(ma);
302
303 return 0;
304
305 fail:
306 if (ma)
307 pa_modargs_free(ma);
308
309 dbus_error_free(&error);
310 pa__done(m);
311
312 return -1;
313 }
314
315
316 void pa__done(pa_module *m) {
317 struct userdata *u;
318 struct session *session;
319
320 pa_assert(m);
321
322 if (!(u = m->userdata))
323 return;
324
325 while ((session = pa_hashmap_steal_first(u->sessions)))
326 free_session(session);
327
328 if (u->connection)
329 pa_dbus_connection_unref(u->connection);
330
331 pa_xfree(u);
332 }