1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2012 Red Hat, Inc.
4
* Copyright (C) 2012 Giovanni Campagna <scampa.giovanni@gmail.com>
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.
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.
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.
30
#include <glib/gi18n.h>
31
#include <glib-object.h>
35
#include <systemd/sd-daemon.h>
36
#include <systemd/sd-login.h>
39
#include "gdm-user-switching.h"
40
#include "gdm-client.h"
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"
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"
54
create_transient_display (GDBusConnection *connection,
55
GCancellable *cancellable,
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,
72
g_prefix_error (error, _("Unable to create transient display: "));
76
g_variant_get (reply, "(&o)", &value);
77
g_debug ("Started %s", value);
79
g_variant_unref (reply);
83
#ifdef WITH_CONSOLE_KIT
86
get_current_session_id (GDBusConnection *connection,
89
GError *local_error = NULL;
92
reply = g_dbus_connection_call_sync (connection,
97
NULL, /* parameters */
98
G_VARIANT_TYPE ("(o)"),
99
G_DBUS_CALL_FLAGS_NONE,
103
g_warning ("Unable to determine session: %s", local_error->message);
104
g_error_free (local_error);
108
g_variant_get (reply, "(o)", session_id);
109
g_variant_unref (reply);
115
get_seat_id_for_session (GDBusConnection *connection,
116
const char *session_id,
119
GError *local_error = NULL;
122
reply = g_dbus_connection_call_sync (connection,
125
CK_SESSION_INTERFACE,
127
NULL, /* parameters */
128
G_VARIANT_TYPE ("(o)"),
129
G_DBUS_CALL_FLAGS_NONE,
133
g_warning ("Unable to determine seat: %s", local_error->message);
134
g_error_free (local_error);
138
g_variant_get (reply, "(o)", seat_id);
139
g_variant_unref (reply);
145
get_current_seat_id (GDBusConnection *connection)
154
res = get_current_session_id (connection, &session_id);
156
res = get_seat_id_for_session (connection, session_id, &seat_id);
164
activate_session_id_for_ck (GDBusConnection *connection,
165
GCancellable *cancellable,
167
const char *session_id,
172
reply = g_dbus_connection_call_sync (connection,
177
g_variant_new ("(o)", session_id),
179
G_DBUS_CALL_FLAGS_NONE,
183
g_prefix_error (error, _("Unable to activate session: "));
187
g_variant_unref (reply);
193
session_is_login_window (GDBusConnection *connection,
194
const char *session_id)
196
GError *local_error = NULL;
201
reply = g_dbus_connection_call_sync (connection,
204
CK_SESSION_INTERFACE,
207
G_VARIANT_TYPE ("(s)"),
208
G_DBUS_CALL_FLAGS_NONE,
212
g_warning ("Unable to determine session type: %s", local_error->message);
213
g_error_free (local_error);
217
g_variant_get (reply, "(&s)", &value);
219
if (value == NULL || value[0] == '\0' || strcmp (value, "LoginWindow") != 0) {
225
g_variant_unref (reply);
231
seat_can_activate_sessions (GDBusConnection *connection,
234
GError *local_error = NULL;
238
reply = g_dbus_connection_call_sync (connection,
242
"CanActivateSessions",
244
G_VARIANT_TYPE ("(b)"),
245
G_DBUS_CALL_FLAGS_NONE,
249
g_warning ("Unable to determine if can activate sessions: %s", local_error->message);
250
g_error_free (local_error);
254
g_variant_get (reply, "(b)", &ret);
255
g_variant_unref (reply);
261
seat_get_sessions (GDBusConnection *connection,
264
GError *local_error = NULL;
268
reply = g_dbus_connection_call_sync (connection,
274
G_VARIANT_TYPE ("(ao)"),
275
G_DBUS_CALL_FLAGS_NONE,
279
g_warning ("Unable to list sessions: %s", local_error->message);
280
g_error_free (local_error);
284
g_variant_get (reply, "(^ao)", &value);
285
g_variant_unref (reply);
291
get_login_window_session_id_for_ck (GDBusConnection *connection,
295
gboolean can_activate_sessions;
296
const char **sessions;
302
g_debug ("checking if seat can activate sessions");
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");
310
sessions = seat_get_sessions (connection, seat_id);
311
for (i = 0; sessions [i] != NULL; i++) {
316
if (session_is_login_window (connection, ssid)) {
317
*session_id = g_strdup (ssid);
327
goto_login_session_for_ck (GDBusConnection *connection,
328
GCancellable *cancellable,
338
/* First look for any existing LoginWindow sessions on the seat.
339
If none are found, create a new one. */
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."));
349
res = get_login_window_session_id_for_ck (connection, seat_id, &session_id);
351
g_set_error (error, GDM_CLIENT_ERROR, 0, _("User unable to switch sessions."));
355
if (session_id != NULL) {
356
res = activate_session_id_for_ck (connection, cancellable, seat_id, session_id, error);
362
if (! ret && g_strcmp0 (seat_id, "/org/freedesktop/ConsoleKit/Seat1") == 0) {
363
res = create_transient_display (connection, cancellable, error);
376
activate_session_id_for_systemd (GDBusConnection *connection,
377
GCancellable *cancellable,
379
const char *session_id,
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),
391
G_DBUS_CALL_FLAGS_NONE,
395
g_prefix_error (error, _("Unable to activate session: "));
399
g_variant_unref (reply);
405
get_login_window_session_id_for_systemd (const char *seat_id,
415
res = sd_seat_get_sessions (seat_id, &sessions, NULL, NULL);
417
g_debug ("Failed to determine sessions: %s", strerror (-res));
421
if (sessions == NULL || sessions[0] == NULL) {
427
for (i = 0; sessions[i]; i ++) {
429
res = sd_session_get_class (sessions[i], &service_class);
431
g_debug ("failed to determine class of session %s: %s", sessions[i], strerror (-res));
436
if (strcmp (service_class, "greeter") != 0) {
437
free (service_class);
441
free (service_class);
443
ret = sd_session_get_state (sessions[i], &state);
445
g_debug ("failed to determine state of session %s: %s", sessions[i], strerror (-res));
450
if (g_strcmp0 (state, "closing") == 0) {
456
res = sd_session_get_service (sessions[i], &service_id);
458
g_debug ("failed to determine service of session %s: %s", sessions[i], strerror (-res));
463
if (strcmp (service_id, "gdm-launch-environment") == 0) {
464
*session_id = g_strdup (sessions[i]);
478
for (i = 0; sessions[i]; i ++) {
488
goto_login_session_for_systemd (GDBusConnection *connection,
489
GCancellable *cancellable,
502
/* First look for any existing LoginWindow sessions on the seat.
503
If none are found, create a new one. */
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 (). */
509
res = sd_pid_get_session (0, &our_session);
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."));
517
res = sd_session_get_seat (our_session, &seat_id);
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."));
526
res = sd_seat_can_multi_session (seat_id);
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."));
539
g_set_error (error, GDM_CLIENT_ERROR, 0, _("The system is unable to start up a new login screen."));
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);
553
if (! ret && g_strcmp0 (seat_id, "seat0") == 0) {
554
res = create_transient_display (connection, cancellable, error);
568
gdm_goto_login_session_sync (GCancellable *cancellable,
571
GDBusConnection *connection;
574
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error);
579
if (sd_booted () > 0) {
580
retval = goto_login_session_for_systemd (connection,
584
g_object_unref (connection);
589
#ifdef WITH_CONSOLE_KIT
590
retval = goto_login_session_for_ck (connection, cancellable, error);
592
g_object_unref (connection);