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