~diwic/ubuntu/lucid/pulseaudio/bugfixes

« back to all changes in this revision

Viewing changes to src/modules/module-console-kit.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich
  • Date: 2008-11-04 15:46:00 UTC
  • mfrom: (1.2.1 upstream) (1.1.6 lenny)
  • Revision ID: james.westby@ubuntu.com-20081104154600-hlzknpcazaam0nxm
Tags: 0.9.13-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Don't build against, and create jack package. Jack is not in main.
  - Remove --disable-per-user-esound-socket from configure flags, as we still
    want per user esound sockets.
  - Remove stop links from rc0 and rc6.
  - Change default resample algorithm and bubffer size.
  - Add alsa configuration files to route alsa applications via pulseaudio.
  - Move libasound2-plugins from Recommends to Depends.
* debian/pulseaudio.preinst: When upgrading from intrepid, remove
  /etc/X11/Xsession.d/70pulseaudio, as this was used to minimize a race
  condition when starting GNOME in intrepid. This race should not exist in
  jaunty once libcanberra is built to use pulseaudio as a backend.
* Do not spawn a pulseaudio server if clients fail to find a running server.
* Remove explicit version dependency for libspeex-dev to allow the package
  to be built for now.
* Regenerate autotools files to work with Ubuntu's newer libtool/libltdl.
* debian/control: libpulsecore5 -> libpulsecore8 to match the library
  soname.

Show diffs side-by-side

added added

removed removed

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