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 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>
48 #include "dbus-util.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
[] = {
67 pa_dbus_connection
*connection
;
71 static void add_session(struct userdata
*u
, const char *id
) {
73 DBusMessage
*m
= NULL
, *reply
= NULL
;
75 struct session
*session
;
78 dbus_error_init (&error
);
80 if (pa_hashmap_get(u
->sessions
, id
)) {
81 pa_log_warn("Duplicate session %s, ignoring.", id
);
85 if (!(m
= dbus_message_new_method_call("org.freedesktop.ConsoleKit", id
, "org.freedesktop.ConsoleKit.Session", "GetUnixUser"))) {
86 pa_log("Failed to allocate GetUnixUser() method call.");
90 if (!(reply
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->connection
), m
, -1, &error
))) {
91 pa_log("GetUnixUser() call failed: %s: %s", error
.name
, error
.message
);
95 /* FIXME: Why is this in int32? and not an uint32? */
96 if (!dbus_message_get_args(reply
, &error
, DBUS_TYPE_INT32
, &uid
, DBUS_TYPE_INVALID
)) {
97 pa_log("Failed to parse GetUnixUser() result: %s: %s", error
.name
, error
.message
);
101 /* We only care about our own sessions */
102 if ((uid_t
) uid
!= getuid())
105 session
= pa_xnew(struct session
, 1);
106 session
->id
= pa_xstrdup(id
);
108 t
= pa_sprintf_malloc("ConsoleKit Session %s", id
);
109 session
->client
= pa_client_new(u
->core
, __FILE__
, t
);
112 pa_proplist_sets(session
->client
->proplist
, "console-kit.session", id
);
114 pa_hashmap_put(u
->sessions
, session
->id
, session
);
116 pa_log_debug("Added new session %s", id
);
121 dbus_message_unref(m
);
124 dbus_message_unref(reply
);
126 dbus_error_free(&error
);
129 static void free_session(struct session
*session
) {
132 pa_log_debug("Removing session %s", session
->id
);
134 pa_client_free(session
->client
);
135 pa_xfree(session
->id
);
139 static void remove_session(struct userdata
*u
, const char *id
) {
140 struct session
*session
;
142 if (!(session
= pa_hashmap_remove(u
->sessions
, id
)))
145 free_session(session
);
148 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*message
, void *userdata
) {
149 struct userdata
*u
= userdata
;
157 dbus_error_init(&error
);
159 pa_log_debug("dbus: interface=%s, path=%s, member=%s\n",
160 dbus_message_get_interface(message
),
161 dbus_message_get_path(message
),
162 dbus_message_get_member(message
));
164 if (dbus_message_is_signal(message
, "org.freedesktop.ConsoleKit.Seat", "SessionAdded")) {
166 if (!dbus_message_get_args(message
, &error
, DBUS_TYPE_STRING
, &path
, DBUS_TYPE_INVALID
) || dbus_error_is_set(&error
)) {
167 pa_log_error("Failed to parse SessionAdded message: %s: %s", error
.name
, error
.message
);
171 add_session(u
, path
);
173 } else if (dbus_message_is_signal(message
, "org.freedesktop.ConsoleKit.Seat", "SessionRemoved")) {
175 if (!dbus_message_get_args(message
, &error
, DBUS_TYPE_STRING
, &path
, DBUS_TYPE_INVALID
) || dbus_error_is_set(&error
)) {
176 pa_log_error("Failed to parse SessionRemoved message: %s: %s", error
.name
, error
.message
);
180 remove_session(u
, path
);
184 dbus_error_free(&error
);
186 return DBUS_HANDLER_RESULT_HANDLED
;
189 static int get_session_list(struct userdata
*u
) {
191 DBusMessage
*m
= NULL
, *reply
= NULL
;
193 DBusMessageIter iter
, sub
;
198 dbus_error_init(&error
);
200 if (!(m
= dbus_message_new_method_call("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "GetSessionsForUnixUser"))) {
201 pa_log("Failed to allocate GetSessionsForUnixUser() method call.");
205 uid
= (uint32_t) getuid();
206 if (!(dbus_message_append_args(m
, DBUS_TYPE_UINT32
, &uid
, DBUS_TYPE_INVALID
))) {
207 pa_log("Failed to append arguments to GetSessionsForUnixUser() method call.");
211 if (!(reply
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->connection
), m
, -1, &error
))) {
212 pa_log("GetSessionsForUnixUser() call failed: %s: %s", error
.name
, error
.message
);
216 dbus_message_iter_init(reply
, &iter
);
218 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
219 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_OBJECT_PATH
) {
220 pa_log("Failed to parse GetSessionsForUnixUser() result.");
224 dbus_message_iter_recurse(&iter
, &sub
);
230 if ((at
= dbus_message_iter_get_arg_type(&sub
)) == DBUS_TYPE_INVALID
)
233 assert(at
== DBUS_TYPE_OBJECT_PATH
);
234 dbus_message_iter_get_basic(&sub
, &id
);
238 dbus_message_iter_next(&sub
);
246 dbus_message_unref(m
);
249 dbus_message_unref(reply
);
251 dbus_error_free(&error
);
256 int pa__init(pa_module
*m
) {
258 pa_dbus_connection
*connection
;
259 struct userdata
*u
= NULL
;
264 dbus_error_init(&error
);
266 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
267 pa_log("Failed to parse module arguments");
271 if (!(connection
= pa_dbus_bus_get(m
->core
, DBUS_BUS_SYSTEM
, &error
)) || dbus_error_is_set(&error
)) {
274 pa_dbus_connection_unref(connection
);
276 pa_log_error("Unable to contact D-Bus system bus: %s: %s", error
.name
, error
.message
);
280 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
282 u
->connection
= connection
;
283 u
->sessions
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
285 if (!dbus_connection_add_filter(pa_dbus_connection_get(connection
), filter_cb
, u
, NULL
)) {
286 pa_log_error("Failed to add filter function");
290 dbus_bus_add_match(pa_dbus_connection_get(connection
), "type='signal',sender='org.freedesktop.ConsoleKit', interface='org.freedesktop.ConsoleKit.Seat'", &error
);
291 if (dbus_error_is_set(&error
)) {
292 pa_log_error("Unable to subscribe to ConsoleKit signals: %s: %s", error
.name
, error
.message
);
296 if (get_session_list(u
) < 0)
307 dbus_error_free(&error
);
314 void pa__done(pa_module
*m
) {
316 struct session
*session
;
320 if (!(u
= m
->userdata
))
324 while ((session
= pa_hashmap_steal_first(u
->sessions
)))
325 free_session(session
);
327 pa_hashmap_free(u
->sessions
, NULL
, NULL
);
331 pa_dbus_connection_unref(u
->connection
);