gdm-user-switching.c 9.03 KB
Newer Older
1 2
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
3 4
 * Copyright (C) 2012 Red Hat, Inc.
 * Copyright (C) 2012 Giovanni Campagna <scampa.giovanni@gmail.com>
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * 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 23 24 25 26
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
27
#include <string.h>
28

29
#include <glib.h>
30
#include <glib/gi18n.h>
31 32
#include <glib-object.h>
#include <gio/gio.h>
33

34 35
#include <systemd/sd-login.h>

Martin Pitt's avatar
Martin Pitt committed
36
#include "common/gdm-common.h"
37 38
#include "gdm-user-switching.h"
#include "gdm-client.h"
39 40

static gboolean
41
create_transient_display (GDBusConnection *connection,
42
                          GCancellable    *cancellable,
43
                          GError         **error)
44
{
45
        GVariant *reply;
46
        const char     *value;
47

48
        reply = g_dbus_connection_call_sync (connection,
49 50 51
                                             "org.gnome.DisplayManager",
                                             "/org/gnome/DisplayManager/LocalDisplayFactory",
                                             "org.gnome.DisplayManager.LocalDisplayFactory",
52 53 54 55 56
                                             "CreateTransientDisplay",
                                             NULL, /* parameters */
                                             G_VARIANT_TYPE ("(o)"),
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1,
57
                                             cancellable, error);
58
        if (reply == NULL) {
59
                g_prefix_error (error, _("Unable to create transient display: "));
60
                return FALSE;
61 62
        }

63
        g_variant_get (reply, "(&o)", &value);
64 65
        g_debug ("Started %s", value);

66 67
        g_variant_unref (reply);
        return TRUE;
68 69
}

70
static gboolean
Ray Strode's avatar
Ray Strode committed
71 72 73 74 75
activate_session_id (GDBusConnection  *connection,
                     GCancellable     *cancellable,
                     const char       *seat_id,
                     const char       *session_id,
                     GError          **error)
76
{
77 78 79 80 81 82 83 84 85 86 87
        GVariant *reply;

        reply = g_dbus_connection_call_sync (connection,
                                             "org.freedesktop.login1",
                                             "/org/freedesktop/login1",
                                             "org.freedesktop.login1.Manager",
                                             "ActivateSessionOnSeat",
                                             g_variant_new ("(ss)", session_id, seat_id),
                                             NULL,
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1,
88
                                             cancellable, error);
89
        if (reply == NULL) {
90
                g_prefix_error (error, _("Unable to activate session: "));
91
                return FALSE;
92 93
        }

94
        g_variant_unref (reply);
95

96
        return TRUE;
97 98 99
}

static gboolean
Ray Strode's avatar
Ray Strode committed
100 101
get_login_window_session_id (const char  *seat_id,
                             char       **session_id)
102 103 104 105 106
{
        gboolean   ret;
        int        res, i;
        char     **sessions;
        char      *service_id;
107 108
        char      *service_class;
        char      *state;
109 110 111 112

        res = sd_seat_get_sessions (seat_id, &sessions, NULL, NULL);
        if (res < 0) {
                g_debug ("Failed to determine sessions: %s", strerror (-res));
113 114 115
                return FALSE;
        }

116 117 118 119 120 121 122 123
        if (sessions == NULL || sessions[0] == NULL) {
                *session_id = NULL;
                ret = TRUE;
                goto out;
        }

        for (i = 0; sessions[i]; i ++) {

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                res = sd_session_get_class (sessions[i], &service_class);
                if (res < 0) {
                        g_debug ("failed to determine class of session %s: %s", sessions[i], strerror (-res));
                        ret = FALSE;
                        goto out;
                }

                if (strcmp (service_class, "greeter") != 0) {
                        free (service_class);
                        continue;
                }

                free (service_class);

                ret = sd_session_get_state (sessions[i], &state);
                if (ret < 0) {
                        g_debug ("failed to determine state of session %s: %s", sessions[i], strerror (-res));
                        ret = FALSE;
                        goto out;
                }

                if (g_strcmp0 (state, "closing") == 0) {
                        free (state);
                        continue;
                }
                free (state);

151 152 153 154 155 156 157
                res = sd_session_get_service (sessions[i], &service_id);
                if (res < 0) {
                        g_debug ("failed to determine service of session %s: %s", sessions[i], strerror (-res));
                        ret = FALSE;
                        goto out;
                }

158
                if (strcmp (service_id, "gdm-launch-environment") == 0) {
159 160 161 162 163 164 165 166 167 168 169 170 171 172
                        *session_id = g_strdup (sessions[i]);
                        ret = TRUE;

                        free (service_id);
                        goto out;
                }

                free (service_id);
        }

        *session_id = NULL;
        ret = TRUE;

out:
173 174 175 176
        if (sessions) {
                for (i = 0; sessions[i]; i ++) {
                        free (sessions[i]);
                }
177

178 179
                free (sessions);
        }
180 181 182 183 184

        return ret;
}

static gboolean
Ray Strode's avatar
Ray Strode committed
185 186 187
goto_login_session (GDBusConnection  *connection,
                    GCancellable     *cancellable,
                    GError          **error)
188 189 190 191 192 193 194 195
{
        gboolean        ret;
        int             res;
        char           *our_session;
        char           *session_id;
        char           *seat_id;

        ret = FALSE;
196 197
        session_id = NULL;
        seat_id = NULL;
198

199 200 201
        /* First look for any existing LoginWindow sessions on the seat.
           If none are found, create a new one. */

202 203 204 205 206 207 208
        /* Note that we mostly use free () here, instead of g_free ()
         * since the data allocated is from libsystemd-logind, which
         * does not use GLib's g_malloc (). */

        res = sd_pid_get_session (0, &our_session);
        if (res < 0) {
                g_debug ("failed to determine own session: %s", strerror (-res));
209
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("Could not identify the current session."));
210

211 212 213
                return FALSE;
        }

214 215 216 217
        res = sd_session_get_seat (our_session, &seat_id);
        free (our_session);
        if (res < 0) {
                g_debug ("failed to determine own seat: %s", strerror (-res));
218
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("Could not identify the current seat."));
219 220 221 222 223 224 225 226 227

                return FALSE;
        }

        res = sd_seat_can_multi_session (seat_id);
        if (res < 0) {
                free (seat_id);

                g_debug ("failed to determine whether seat can do multi session: %s", strerror (-res));
228
                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."));
229 230 231 232 233 234

                return FALSE;
        }

        if (res == 0) {
                free (seat_id);
235

236
                g_set_error (error, GDM_CLIENT_ERROR, 0, _("The system is unable to start up a new login screen."));
237 238 239 240

                return FALSE;
        }

Ray Strode's avatar
Ray Strode committed
241
        res = get_login_window_session_id (seat_id, &session_id);
242
        if (res && session_id != NULL) {
Ray Strode's avatar
Ray Strode committed
243
                res = activate_session_id (connection, cancellable, seat_id, session_id, error);
244

245 246 247 248 249
                if (res) {
                        ret = TRUE;
                }
        }

250
        if (! ret && g_strcmp0 (seat_id, "seat0") == 0) {
251
                res = create_transient_display (connection, cancellable, error);
252 253 254 255 256
                if (res) {
                        ret = TRUE;
                }
        }

257 258 259
        free (seat_id);
        g_free (session_id);

260 261
        return ret;
}
262

263 264
gboolean
gdm_goto_login_session_sync (GCancellable  *cancellable,
Ray Strode's avatar
Ray Strode committed
265
                             GError       **error)
266
{
267
        GDBusConnection *connection;
268
        gboolean retval;
269

270 271
        connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
        if (!connection)
272 273
                return FALSE;

Ray Strode's avatar
Ray Strode committed
274
        retval = goto_login_session (connection, cancellable, error);
275

Ray Strode's avatar
Ray Strode committed
276 277
        g_object_unref (connection);
        return retval;
278
}