2 This file is part of PulseAudio.
4 Copyright 2008 Lennart Poettering
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.
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.
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
32 #include <sys/types.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/timeval.h>
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>
49 #include "module-console-kit-symdef.h"
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);
56 static const char* const valid_modargs[] = {
68 pa_dbus_connection *connection;
72 static void add_session(struct userdata *u, const char *id) {
74 DBusMessage *m = NULL, *reply = NULL;
76 struct session *session;
77 pa_client_new_data data;
79 dbus_error_init (&error);
81 if (pa_hashmap_get(u->sessions, id)) {
82 pa_log_warn("Duplicate session %s, ignoring.", id);
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.");
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);
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);
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);
106 /* We only care about our own sessions */
107 if ((uid_t) uid != getuid())
110 session = pa_xnew(struct session, 1);
111 session->id = pa_xstrdup(id);
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);
121 if (!session->client) {
122 pa_xfree(session->id);
127 pa_hashmap_put(u->sessions, session->id, session);
129 pa_log_debug("Added new session %s", id);
134 dbus_message_unref(m);
137 dbus_message_unref(reply);
139 dbus_error_free(&error);
142 static void free_session(struct session *session) {
145 pa_log_debug("Removing session %s", session->id);
147 pa_client_free(session->client);
148 pa_xfree(session->id);
152 static void remove_session(struct userdata *u, const char *id) {
153 struct session *session;
155 if (!(session = pa_hashmap_remove(u->sessions, id)))
158 free_session(session);
161 static DBusHandlerResult filter_cb(DBusConnection *bus, DBusMessage *message, void *userdata) {
162 struct userdata *u = userdata;
170 dbus_error_init(&error);
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));
177 if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionAdded")) {
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);
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);
189 add_session(u, path);
190 return DBUS_HANDLER_RESULT_HANDLED;
192 } else if (dbus_message_is_signal(message, "org.freedesktop.ConsoleKit.Seat", "SessionRemoved")) {
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);
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);
204 remove_session(u, path);
205 return DBUS_HANDLER_RESULT_HANDLED;
209 dbus_error_free(&error);
211 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
214 static int get_session_list(struct userdata *u) {
216 DBusMessage *m = NULL, *reply = NULL;
218 DBusMessageIter iter, sub;
223 dbus_error_init(&error);
225 if (!(m = dbus_message_new_method_call("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "GetSessionsForUnixUser"))) {
226 pa_log("Failed to allocate GetSessionsForUnixUser() method call.");
230 uid = (uint32_t) getuid();
231 if (!(dbus_message_append_args(m, DBUS_TYPE_UINT32, &uid, DBUS_TYPE_INVALID))) {
232 pa_log("Failed to append arguments to GetSessionsForUnixUser() method call.");
236 if (!(reply = dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u->connection), m, -1, &error))) {
237 pa_log("GetSessionsForUnixUser() call failed: %s: %s", error.name, error.message);
241 dbus_message_iter_init(reply, &iter);
243 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
244 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_OBJECT_PATH) {
245 pa_log("Failed to parse GetSessionsForUnixUser() result.");
249 dbus_message_iter_recurse(&iter, &sub);
255 if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
258 assert(at == DBUS_TYPE_OBJECT_PATH);
259 dbus_message_iter_get_basic(&sub, &id);
263 dbus_message_iter_next(&sub);
271 dbus_message_unref(m);
274 dbus_message_unref(reply);
276 dbus_error_free(&error);
281 int pa__init(pa_module*m) {
283 pa_dbus_connection *connection;
284 struct userdata *u = NULL;
289 dbus_error_init(&error);
291 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
292 pa_log("Failed to parse module arguments");
296 if (!(connection = pa_dbus_bus_get(m->core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
299 pa_dbus_connection_unref(connection);
301 pa_log_error("Unable to contact D-Bus system bus: %s: %s", error.name, error.message);
305 m->userdata = u = pa_xnew(struct userdata, 1);
308 u->connection = connection;
309 u->sessions = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
311 if (!dbus_connection_add_filter(pa_dbus_connection_get(connection), filter_cb, u, NULL)) {
312 pa_log_error("Failed to add filter function");
316 if (pa_dbus_add_matches(
317 pa_dbus_connection_get(connection), &error,
318 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionAdded'",
319 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionRemoved'", NULL) < 0) {
320 pa_log_error("Unable to subscribe to ConsoleKit signals: %s: %s", error.name, error.message);
324 if (get_session_list(u) < 0)
335 dbus_error_free(&error);
342 void pa__done(pa_module *m) {
344 struct session *session;
348 if (!(u = m->userdata))
352 while ((session = pa_hashmap_steal_first(u->sessions)))
353 free_session(session);
355 pa_hashmap_free(u->sessions, NULL, NULL);
359 pa_dbus_remove_matches(
360 pa_dbus_connection_get(u->connection),
361 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionAdded'",
362 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionRemoved'", NULL);
364 dbus_connection_remove_filter(pa_dbus_connection_get(u->connection), filter_cb, u);
365 pa_dbus_connection_unref(u->connection);