~noskcaj/ubuntu/vivid/gdm/3.14.1

« back to all changes in this revision

Viewing changes to .pc/00git_logind_check.patch/gui/libgdm/gdm-user-switching.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha, Tim Lunn, Jeremy Bicha, Robert Ancell
  • Date: 2013-05-31 22:36:08 UTC
  • mfrom: (1.4.55)
  • Revision ID: package-import@ubuntu.com-20130531223608-33uo85niksee5460
Tags: 3.8.1.1-0ubuntu1
[ Tim Lunn ]
* New upstream release
* debian/patches/ubuntu_dont_catch_sigsegv.patch:
  - Dropped, obsolete
* debian/rules:
  - Don't rename gdm binary since that's already been
    done in the new version

[ Jeremy Bicha ]
* debian/control.in: Bump minimum glib
* debian/watch: Watch for unstable releases
* debian/patches/00git_logind_check.patch:
  - Dropped, applied in new release
* debian/patches/08_frequent-users_greeter.patch: Refreshed

[ Robert Ancell ]
* New upstream release
* debian/patches/ubuntu_daemon_autologin_tracking.patch:
* debian/patches/ubuntu_ensure_dirs.patch:
* debian/patches/ubuntu_slave-only-set-up-autologin.patch:
  - Applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2
 
 *
3
 
 * Copyright (C) 2012 Red Hat, Inc.
4
 
 * Copyright (C) 2012 Giovanni Campagna <scampa.giovanni@gmail.com>
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 
 *
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <stdlib.h>
25
 
#include <stdio.h>
26
 
#include <unistd.h>
27
 
#include <string.h>
28
 
 
29
 
#include <glib.h>
30
 
#include <glib/gi18n.h>
31
 
#include <glib-object.h>
32
 
#include <gio/gio.h>
33
 
 
34
 
#ifdef WITH_SYSTEMD
35
 
#include <systemd/sd-daemon.h>
36
 
#include <systemd/sd-login.h>
37
 
#endif
38
 
 
39
 
#include "gdm-user-switching.h"
40
 
#include "gdm-client.h"
41
 
 
42
 
#ifdef WITH_CONSOLE_KIT
43
 
#define CK_NAME      "org.freedesktop.ConsoleKit"
44
 
#define CK_PATH      "/org/freedesktop/ConsoleKit"
45
 
#define CK_INTERFACE "org.freedesktop.ConsoleKit"
46
 
 
47
 
#define CK_MANAGER_PATH      "/org/freedesktop/ConsoleKit/Manager"
48
 
#define CK_MANAGER_INTERFACE "org.freedesktop.ConsoleKit.Manager"
49
 
#define CK_SEAT_INTERFACE    "org.freedesktop.ConsoleKit.Seat"
50
 
#define CK_SESSION_INTERFACE "org.freedesktop.ConsoleKit.Session"
51
 
#endif
52
 
 
53
 
static gboolean
54
 
create_transient_display (GDBusConnection *connection,
55
 
                          GCancellable    *cancellable,
56
 
                          GError         **error)
57
 
{
58
 
        GVariant *reply;
59
 
        const char     *value;
60
 
 
61
 
        reply = g_dbus_connection_call_sync (connection,
62
 
                                             "org.gnome.DisplayManager",
63
 
                                             "/org/gnome/DisplayManager/LocalDisplayFactory",
64
 
                                             "org.gnome.DisplayManager.LocalDisplayFactory",
65
 
                                             "CreateTransientDisplay",
66
 
                                             NULL, /* parameters */
67
 
                                             G_VARIANT_TYPE ("(o)"),
68
 
                                             G_DBUS_CALL_FLAGS_NONE,
69
 
                                             -1,
70
 
                                             cancellable, error);
71
 
        if (reply == NULL) {
72
 
                g_prefix_error (error, _("Unable to create transient display: "));
73
 
                return FALSE;
74
 
        }
75
 
 
76
 
        g_variant_get (reply, "(&o)", &value);
77
 
        g_debug ("Started %s", value);
78
 
 
79
 
        g_variant_unref (reply);
80
 
        return TRUE;
81
 
}
82
 
 
83
 
#ifdef WITH_CONSOLE_KIT
84
 
 
85
 
static gboolean
86
 
get_current_session_id (GDBusConnection  *connection,
87
 
                        char            **session_id)
88
 
{
89
 
        GError *local_error = NULL;
90
 
        GVariant *reply;
91
 
 
92
 
        reply = g_dbus_connection_call_sync (connection,
93
 
                                             CK_NAME,
94
 
                                             CK_MANAGER_PATH,
95
 
                                             CK_MANAGER_INTERFACE,
96
 
                                             "GetCurrentSession",
97
 
                                             NULL, /* parameters */
98
 
                                             G_VARIANT_TYPE ("(o)"),
99
 
                                             G_DBUS_CALL_FLAGS_NONE,
100
 
                                             -1,
101
 
                                             NULL, &local_error);
102
 
        if (reply == NULL) {
103
 
                g_warning ("Unable to determine session: %s", local_error->message);
104
 
                g_error_free (local_error);
105
 
                return FALSE;
106
 
        }
107
 
 
108
 
        g_variant_get (reply, "(o)", session_id);
109
 
        g_variant_unref (reply);
110
 
 
111
 
        return TRUE;
112
 
}
113
 
 
114
 
static gboolean
115
 
get_seat_id_for_session (GDBusConnection  *connection,
116
 
                         const char       *session_id,
117
 
                         char            **seat_id)
118
 
{
119
 
        GError *local_error = NULL;
120
 
        GVariant *reply;
121
 
 
122
 
        reply = g_dbus_connection_call_sync (connection,
123
 
                                             CK_NAME,
124
 
                                             session_id,
125
 
                                             CK_SESSION_INTERFACE,
126
 
                                             "GetSeatId",
127
 
                                             NULL, /* parameters */
128
 
                                             G_VARIANT_TYPE ("(o)"),
129
 
                                             G_DBUS_CALL_FLAGS_NONE,
130
 
                                             -1,
131
 
                                             NULL, &local_error);
132
 
        if (reply == NULL) {
133
 
                g_warning ("Unable to determine seat: %s", local_error->message);
134
 
                g_error_free (local_error);
135
 
                return FALSE;
136
 
        }
137
 
 
138
 
        g_variant_get (reply, "(o)", seat_id);
139
 
        g_variant_unref (reply);
140
 
 
141
 
        return TRUE;
142
 
}
143
 
 
144
 
static char *
145
 
get_current_seat_id (GDBusConnection *connection)
146
 
{
147
 
        gboolean res;
148
 
        char    *session_id;
149
 
        char    *seat_id;
150
 
 
151
 
        session_id = NULL;
152
 
        seat_id = NULL;
153
 
 
154
 
        res = get_current_session_id (connection, &session_id);
155
 
        if (res) {
156
 
                res = get_seat_id_for_session (connection, session_id, &seat_id);
157
 
        }
158
 
        g_free (session_id);
159
 
 
160
 
        return seat_id;
161
 
}
162
 
 
163
 
static gboolean
164
 
activate_session_id_for_ck (GDBusConnection *connection,
165
 
                            GCancellable    *cancellable,
166
 
                            const char      *seat_id,
167
 
                            const char      *session_id,
168
 
                            GError         **error)
169
 
{
170
 
        GVariant *reply;
171
 
 
172
 
        reply = g_dbus_connection_call_sync (connection,
173
 
                                             CK_NAME,
174
 
                                             seat_id,
175
 
                                             CK_SEAT_INTERFACE,
176
 
                                             "ActivateSession",
177
 
                                             g_variant_new ("(o)", session_id),
178
 
                                             NULL,
179
 
                                             G_DBUS_CALL_FLAGS_NONE,
180
 
                                             -1,
181
 
                                             NULL, error);
182
 
        if (reply == NULL) {
183
 
                g_prefix_error (error, _("Unable to activate session: "));
184
 
                return FALSE;
185
 
        }
186
 
 
187
 
        g_variant_unref (reply);
188
 
 
189
 
        return TRUE;
190
 
}
191
 
 
192
 
static gboolean
193
 
session_is_login_window (GDBusConnection *connection,
194
 
                         const char      *session_id)
195
 
{
196
 
        GError *local_error = NULL;
197
 
        GVariant *reply;
198
 
        const char *value;
199
 
        gboolean ret;
200
 
 
201
 
        reply = g_dbus_connection_call_sync (connection,
202
 
                                             CK_NAME,
203
 
                                             session_id,
204
 
                                             CK_SESSION_INTERFACE,
205
 
                                             "GetSessionType",
206
 
                                             NULL,
207
 
                                             G_VARIANT_TYPE ("(s)"),
208
 
                                             G_DBUS_CALL_FLAGS_NONE,
209
 
                                             -1,
210
 
                                             NULL, &local_error);
211
 
        if (reply == NULL) {
212
 
                g_warning ("Unable to determine session type: %s", local_error->message);
213
 
                g_error_free (local_error);
214
 
                return FALSE;
215
 
        }
216
 
 
217
 
        g_variant_get (reply, "(&s)", &value);
218
 
 
219
 
        if (value == NULL || value[0] == '\0' || strcmp (value, "LoginWindow") != 0) {
220
 
                ret = FALSE;
221
 
        } else {
222
 
                ret = TRUE;
223
 
        }
224
 
 
225
 
        g_variant_unref (reply);
226
 
 
227
 
        return ret;
228
 
}
229
 
 
230
 
static gboolean
231
 
seat_can_activate_sessions (GDBusConnection *connection,
232
 
                            const char      *seat_id)
233
 
{
234
 
        GError *local_error = NULL;
235
 
        GVariant *reply;
236
 
        gboolean ret;
237
 
 
238
 
        reply = g_dbus_connection_call_sync (connection,
239
 
                                             CK_NAME,
240
 
                                             seat_id,
241
 
                                             CK_SEAT_INTERFACE,
242
 
                                             "CanActivateSessions",
243
 
                                             NULL,
244
 
                                             G_VARIANT_TYPE ("(b)"),
245
 
                                             G_DBUS_CALL_FLAGS_NONE,
246
 
                                             -1,
247
 
                                             NULL, &local_error);
248
 
        if (reply == NULL) {
249
 
                g_warning ("Unable to determine if can activate sessions: %s", local_error->message);
250
 
                g_error_free (local_error);
251
 
                return FALSE;
252
 
        }
253
 
 
254
 
        g_variant_get (reply, "(b)", &ret);
255
 
        g_variant_unref (reply);
256
 
 
257
 
        return ret;
258
 
}
259
 
 
260
 
static const char **
261
 
seat_get_sessions (GDBusConnection *connection,
262
 
                   const char      *seat_id)
263
 
{
264
 
        GError *local_error = NULL;
265
 
        GVariant *reply;
266
 
        const char **value;
267
 
 
268
 
        reply = g_dbus_connection_call_sync (connection,
269
 
                                             CK_NAME,
270
 
                                             seat_id,
271
 
                                             CK_SEAT_INTERFACE,
272
 
                                             "GetSessions",
273
 
                                             NULL,
274
 
                                             G_VARIANT_TYPE ("(ao)"),
275
 
                                             G_DBUS_CALL_FLAGS_NONE,
276
 
                                             -1,
277
 
                                             NULL, &local_error);
278
 
        if (reply == NULL) {
279
 
                g_warning ("Unable to list sessions: %s", local_error->message);
280
 
                g_error_free (local_error);
281
 
                return FALSE;
282
 
        }
283
 
 
284
 
        g_variant_get (reply, "(^ao)", &value);
285
 
        g_variant_unref (reply);
286
 
 
287
 
        return value;
288
 
}
289
 
 
290
 
static gboolean
291
 
get_login_window_session_id_for_ck (GDBusConnection  *connection,
292
 
                                    const char       *seat_id,
293
 
                                    char            **session_id)
294
 
{
295
 
        gboolean     can_activate_sessions;
296
 
        const char **sessions;
297
 
        int          i;
298
 
 
299
 
        *session_id = NULL;
300
 
        sessions = NULL;
301
 
 
302
 
        g_debug ("checking if seat can activate sessions");
303
 
 
304
 
        can_activate_sessions = seat_can_activate_sessions (connection, seat_id);
305
 
        if (! can_activate_sessions) {
306
 
                g_debug ("seat is unable to activate sessions");
307
 
                return FALSE;
308
 
        }
309
 
 
310
 
        sessions = seat_get_sessions (connection, seat_id);
311
 
        for (i = 0; sessions [i] != NULL; i++) {
312
 
                const char *ssid;
313
 
 
314
 
                ssid = sessions [i];
315
 
 
316
 
                if (session_is_login_window (connection, ssid)) {
317
 
                        *session_id = g_strdup (ssid);
318
 
                        break;
319
 
                }
320
 
        }
321
 
        g_free (sessions);
322
 
 
323
 
        return TRUE;
324
 
}
325
 
 
326
 
static gboolean
327
 
goto_login_session_for_ck (GDBusConnection  *connection,
328
 
                           GCancellable     *cancellable,
329
 
                           GError          **error)
330
 
{
331
 
        gboolean        ret;
332
 
        gboolean        res;
333
 
        char           *session_id;
334
 
        char           *seat_id;
335
 
 
336
 
        ret = FALSE;
337
 
 
338
 
        /* First look for any existing LoginWindow sessions on the seat.
339
 
           If none are found, create a new one. */
340
 
 
341
 
        seat_id = get_current_seat_id (connection);
342
 
        if (seat_id == NULL || seat_id[0] == '\0') {
343
 
                g_debug ("seat id is not set; can't switch sessions");
344
 
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("Could not identify the current session."));
345
 
 
346
 
                return FALSE;
347
 
        }
348
 
 
349
 
        res = get_login_window_session_id_for_ck (connection, seat_id, &session_id);
350
 
        if (! res) {
351
 
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("User unable to switch sessions."));
352
 
                return FALSE;
353
 
        }
354
 
 
355
 
        if (session_id != NULL) {
356
 
                res = activate_session_id_for_ck (connection, cancellable, seat_id, session_id, error);
357
 
                if (res) {
358
 
                        ret = TRUE;
359
 
                }
360
 
        }
361
 
 
362
 
        if (! ret && g_strcmp0 (seat_id, "/org/freedesktop/ConsoleKit/Seat1") == 0) {
363
 
                res = create_transient_display (connection, cancellable, error);
364
 
                if (res) {
365
 
                        ret = TRUE;
366
 
                }
367
 
        }
368
 
 
369
 
        return ret;
370
 
}
371
 
#endif
372
 
 
373
 
#ifdef WITH_SYSTEMD
374
 
 
375
 
static gboolean
376
 
activate_session_id_for_systemd (GDBusConnection  *connection,
377
 
                                 GCancellable     *cancellable,
378
 
                                 const char       *seat_id,
379
 
                                 const char       *session_id,
380
 
                                 GError          **error)
381
 
{
382
 
        GVariant *reply;
383
 
 
384
 
        reply = g_dbus_connection_call_sync (connection,
385
 
                                             "org.freedesktop.login1",
386
 
                                             "/org/freedesktop/login1",
387
 
                                             "org.freedesktop.login1.Manager",
388
 
                                             "ActivateSessionOnSeat",
389
 
                                             g_variant_new ("(ss)", session_id, seat_id),
390
 
                                             NULL,
391
 
                                             G_DBUS_CALL_FLAGS_NONE,
392
 
                                             -1,
393
 
                                             cancellable, error);
394
 
        if (reply == NULL) {
395
 
                g_prefix_error (error, _("Unable to activate session: "));
396
 
                return FALSE;
397
 
        }
398
 
 
399
 
        g_variant_unref (reply);
400
 
 
401
 
        return TRUE;
402
 
}
403
 
 
404
 
static gboolean
405
 
get_login_window_session_id_for_systemd (const char  *seat_id,
406
 
                                         char       **session_id)
407
 
{
408
 
        gboolean   ret;
409
 
        int        res, i;
410
 
        char     **sessions;
411
 
        char      *service_id;
412
 
        char      *service_class;
413
 
        char      *state;
414
 
 
415
 
        res = sd_seat_get_sessions (seat_id, &sessions, NULL, NULL);
416
 
        if (res < 0) {
417
 
                g_debug ("Failed to determine sessions: %s", strerror (-res));
418
 
                return FALSE;
419
 
        }
420
 
 
421
 
        if (sessions == NULL || sessions[0] == NULL) {
422
 
                *session_id = NULL;
423
 
                ret = TRUE;
424
 
                goto out;
425
 
        }
426
 
 
427
 
        for (i = 0; sessions[i]; i ++) {
428
 
 
429
 
                res = sd_session_get_class (sessions[i], &service_class);
430
 
                if (res < 0) {
431
 
                        g_debug ("failed to determine class of session %s: %s", sessions[i], strerror (-res));
432
 
                        ret = FALSE;
433
 
                        goto out;
434
 
                }
435
 
 
436
 
                if (strcmp (service_class, "greeter") != 0) {
437
 
                        free (service_class);
438
 
                        continue;
439
 
                }
440
 
 
441
 
                free (service_class);
442
 
 
443
 
                ret = sd_session_get_state (sessions[i], &state);
444
 
                if (ret < 0) {
445
 
                        g_debug ("failed to determine state of session %s: %s", sessions[i], strerror (-res));
446
 
                        ret = FALSE;
447
 
                        goto out;
448
 
                }
449
 
 
450
 
                if (g_strcmp0 (state, "closing") == 0) {
451
 
                        free (state);
452
 
                        continue;
453
 
                }
454
 
                free (state);
455
 
 
456
 
                res = sd_session_get_service (sessions[i], &service_id);
457
 
                if (res < 0) {
458
 
                        g_debug ("failed to determine service of session %s: %s", sessions[i], strerror (-res));
459
 
                        ret = FALSE;
460
 
                        goto out;
461
 
                }
462
 
 
463
 
                if (strcmp (service_id, "gdm-launch-environment") == 0) {
464
 
                        *session_id = g_strdup (sessions[i]);
465
 
                        ret = TRUE;
466
 
 
467
 
                        free (service_id);
468
 
                        goto out;
469
 
                }
470
 
 
471
 
                free (service_id);
472
 
        }
473
 
 
474
 
        *session_id = NULL;
475
 
        ret = TRUE;
476
 
 
477
 
out:
478
 
        for (i = 0; sessions[i]; i ++) {
479
 
                free (sessions[i]);
480
 
        }
481
 
 
482
 
        free (sessions);
483
 
 
484
 
        return ret;
485
 
}
486
 
 
487
 
static gboolean
488
 
goto_login_session_for_systemd (GDBusConnection  *connection,
489
 
                                GCancellable     *cancellable,
490
 
                                GError          **error)
491
 
{
492
 
        gboolean        ret;
493
 
        int             res;
494
 
        char           *our_session;
495
 
        char           *session_id;
496
 
        char           *seat_id;
497
 
 
498
 
        ret = FALSE;
499
 
        session_id = NULL;
500
 
        seat_id = NULL;
501
 
 
502
 
        /* First look for any existing LoginWindow sessions on the seat.
503
 
           If none are found, create a new one. */
504
 
 
505
 
        /* Note that we mostly use free () here, instead of g_free ()
506
 
         * since the data allocated is from libsystemd-logind, which
507
 
         * does not use GLib's g_malloc (). */
508
 
 
509
 
        res = sd_pid_get_session (0, &our_session);
510
 
        if (res < 0) {
511
 
                g_debug ("failed to determine own session: %s", strerror (-res));
512
 
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("Could not identify the current session."));
513
 
 
514
 
                return FALSE;
515
 
        }
516
 
 
517
 
        res = sd_session_get_seat (our_session, &seat_id);
518
 
        free (our_session);
519
 
        if (res < 0) {
520
 
                g_debug ("failed to determine own seat: %s", strerror (-res));
521
 
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("Could not identify the current seat."));
522
 
 
523
 
                return FALSE;
524
 
        }
525
 
 
526
 
        res = sd_seat_can_multi_session (seat_id);
527
 
        if (res < 0) {
528
 
                free (seat_id);
529
 
 
530
 
                g_debug ("failed to determine whether seat can do multi session: %s", strerror (-res));
531
 
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("The system is unable to determine whether to switch to an existing login screen or start up a new login screen."));
532
 
 
533
 
                return FALSE;
534
 
        }
535
 
 
536
 
        if (res == 0) {
537
 
                free (seat_id);
538
 
 
539
 
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("The system is unable to start up a new login screen."));
540
 
 
541
 
                return FALSE;
542
 
        }
543
 
 
544
 
        res = get_login_window_session_id_for_systemd (seat_id, &session_id);
545
 
        if (res && session_id != NULL) {
546
 
                res = activate_session_id_for_systemd (connection, cancellable, seat_id, session_id, error);
547
 
 
548
 
                if (res) {
549
 
                        ret = TRUE;
550
 
                }
551
 
        }
552
 
 
553
 
        if (! ret && g_strcmp0 (seat_id, "seat0") == 0) {
554
 
                res = create_transient_display (connection, cancellable, error);
555
 
                if (res) {
556
 
                        ret = TRUE;
557
 
                }
558
 
        }
559
 
 
560
 
        free (seat_id);
561
 
        g_free (session_id);
562
 
 
563
 
        return ret;
564
 
}
565
 
#endif
566
 
 
567
 
gboolean
568
 
gdm_goto_login_session_sync (GCancellable  *cancellable,
569
 
                             GError       **error)
570
 
{
571
 
        GDBusConnection *connection;
572
 
        gboolean retval;
573
 
 
574
 
        connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
575
 
        if (!connection)
576
 
                return FALSE;
577
 
 
578
 
#ifdef WITH_SYSTEMD
579
 
        if (sd_booted () > 0) {
580
 
                retval = goto_login_session_for_systemd (connection,
581
 
                                                         cancellable,
582
 
                                                         error);
583
 
 
584
 
                g_object_unref (connection);
585
 
                return retval;
586
 
        }
587
 
#endif
588
 
 
589
 
#ifdef WITH_CONSOLE_KIT
590
 
        retval = goto_login_session_for_ck (connection, cancellable, error);
591
 
 
592
 
        g_object_unref (connection);
593
 
        return retval;
594
 
#endif
595
 
}