~binli/ubuntu/vivid/pulseaudio/load-extcon-module

« back to all changes in this revision

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

  • Committer: Bin Li
  • Date: 2016-01-23 15:04:48 UTC
  • Revision ID: bin.li@canonical.com-20160123150448-5ockvw4p5xxntda4
init the 1:6.0-0ubuntu9.15 from silo 12

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