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
31 #include <sys/types.h>
33 #include <pulse/xmalloc.h>
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>
42 #include "module-console-kit-symdef.h"
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);
49 static const char* const valid_modargs
[] = {
61 pa_dbus_connection
*connection
;
66 static void add_session(struct userdata
*u
, const char *id
) {
68 DBusMessage
*m
= NULL
, *reply
= NULL
;
70 struct session
*session
;
71 pa_client_new_data data
;
73 dbus_error_init(&error
);
75 if (pa_hashmap_get(u
->sessions
, id
)) {
76 pa_log_warn("Duplicate session %s, ignoring.", id
);
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.");
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
);
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
);
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
);
100 /* We only care about our own sessions */
101 if ((uid_t
) uid
!= getuid())
104 session
= pa_xnew(struct session
, 1);
105 session
->id
= pa_xstrdup(id
);
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
);
115 if (!session
->client
) {
116 pa_xfree(session
->id
);
121 pa_hashmap_put(u
->sessions
, session
->id
, session
);
123 pa_log_debug("Added new session %s", id
);
128 dbus_message_unref(m
);
131 dbus_message_unref(reply
);
133 dbus_error_free(&error
);
136 static void free_session(struct session
*session
) {
139 pa_log_debug("Removing session %s", session
->id
);
141 pa_client_free(session
->client
);
142 pa_xfree(session
->id
);
146 static void remove_session(struct userdata
*u
, const char *id
) {
150 pa_hashmap_remove_and_free(u
->sessions
, id
);
153 static DBusHandlerResult
filter_cb(DBusConnection
*bus
, DBusMessage
*message
, void *userdata
) {
154 struct userdata
*u
= userdata
;
162 dbus_error_init(&error
);
164 if (dbus_message_is_signal(message
, "org.freedesktop.ConsoleKit.Seat", "SessionAdded")) {
166 /* CK API changed to match spec in 0.3 */
167 if (!dbus_message_get_args(message
, &error
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
168 dbus_error_free(&error
);
170 if (!dbus_message_get_args(message
, &error
, DBUS_TYPE_STRING
, &path
, DBUS_TYPE_INVALID
)) {
171 pa_log_error("Failed to parse SessionAdded message: %s: %s", error
.name
, error
.message
);
176 add_session(u
, path
);
178 } else if (dbus_message_is_signal(message
, "org.freedesktop.ConsoleKit.Seat", "SessionRemoved")) {
180 /* CK API changed to match spec in 0.3 */
181 if (!dbus_message_get_args(message
, &error
, DBUS_TYPE_OBJECT_PATH
, &path
, DBUS_TYPE_INVALID
)) {
182 dbus_error_free(&error
);
184 if (!dbus_message_get_args(message
, &error
, DBUS_TYPE_STRING
, &path
, DBUS_TYPE_INVALID
)) {
185 pa_log_error("Failed to parse SessionRemoved message: %s: %s", error
.name
, error
.message
);
190 remove_session(u
, path
);
194 dbus_error_free(&error
);
196 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
199 static int get_session_list(struct userdata
*u
) {
201 DBusMessage
*m
= NULL
, *reply
= NULL
;
203 DBusMessageIter iter
, sub
;
208 dbus_error_init(&error
);
210 if (!(m
= dbus_message_new_method_call("org.freedesktop.ConsoleKit", "/org/freedesktop/ConsoleKit/Manager", "org.freedesktop.ConsoleKit.Manager", "GetSessionsForUnixUser"))) {
211 pa_log("Failed to allocate GetSessionsForUnixUser() method call.");
215 uid
= (uint32_t) getuid();
216 if (!(dbus_message_append_args(m
, DBUS_TYPE_UINT32
, &uid
, DBUS_TYPE_INVALID
))) {
217 pa_log("Failed to append arguments to GetSessionsForUnixUser() method call.");
221 if (!(reply
= dbus_connection_send_with_reply_and_block(pa_dbus_connection_get(u
->connection
), m
, -1, &error
))) {
222 pa_log("GetSessionsForUnixUser() call failed: %s: %s", error
.name
, error
.message
);
226 dbus_message_iter_init(reply
, &iter
);
228 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
||
229 dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_OBJECT_PATH
) {
230 pa_log("Failed to parse GetSessionsForUnixUser() result.");
234 dbus_message_iter_recurse(&iter
, &sub
);
240 if ((at
= dbus_message_iter_get_arg_type(&sub
)) == DBUS_TYPE_INVALID
)
243 assert(at
== DBUS_TYPE_OBJECT_PATH
);
244 dbus_message_iter_get_basic(&sub
, &id
);
248 dbus_message_iter_next(&sub
);
256 dbus_message_unref(m
);
259 dbus_message_unref(reply
);
261 dbus_error_free(&error
);
266 int pa__init(pa_module
*m
) {
268 pa_dbus_connection
*connection
;
269 struct userdata
*u
= NULL
;
274 dbus_error_init(&error
);
276 /* If systemd's logind service is running, we shouldn't watch ConsoleKit
278 if (access("/run/systemd/seats/", F_OK
) >= 0)
281 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
282 pa_log("Failed to parse module arguments");
286 if (!(connection
= pa_dbus_bus_get(m
->core
, DBUS_BUS_SYSTEM
, &error
)) || dbus_error_is_set(&error
)) {
289 pa_dbus_connection_unref(connection
);
291 pa_log_error("Unable to contact D-Bus system bus: %s: %s", error
.name
, error
.message
);
295 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
298 u
->connection
= connection
;
299 u
->sessions
= pa_hashmap_new_full(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
, NULL
, (pa_free_cb_t
) free_session
);
301 if (!dbus_connection_add_filter(pa_dbus_connection_get(connection
), filter_cb
, u
, NULL
)) {
302 pa_log_error("Failed to add filter function");
306 u
->filter_added
= true;
308 if (pa_dbus_add_matches(
309 pa_dbus_connection_get(connection
), &error
,
310 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionAdded'",
311 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionRemoved'", NULL
) < 0) {
312 pa_log_error("Unable to subscribe to ConsoleKit signals: %s: %s", error
.name
, error
.message
);
316 if (get_session_list(u
) < 0)
327 dbus_error_free(&error
);
333 void pa__done(pa_module
*m
) {
338 if (!(u
= m
->userdata
))
342 pa_hashmap_free(u
->sessions
);
345 pa_dbus_remove_matches(
346 pa_dbus_connection_get(u
->connection
),
347 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionAdded'",
348 "type='signal',sender='org.freedesktop.ConsoleKit',interface='org.freedesktop.ConsoleKit.Seat',member='SessionRemoved'", NULL
);
351 dbus_connection_remove_filter(pa_dbus_connection_get(u
->connection
), filter_cb
, u
);
353 pa_dbus_connection_unref(u
->connection
);