Back to index

lightdm  1.3.2
console-kit.c
Go to the documentation of this file.
00001 /* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*-
00002  *
00003  * Copyright (C) 2010-2011 Robert Ancell.
00004  * Author: Robert Ancell <robert.ancell@canonical.com>
00005  *
00006  * This program is free software: you can redistribute it and/or modify it under
00007  * the terms of the GNU General Public License as published by the Free Software
00008  * Foundation, either version 3 of the License, or (at your option) any later
00009  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
00010  * license.
00011  */
00012 
00013 #include <gio/gio.h>
00014 
00015 #include "console-kit.h"
00016 
00017 gchar *
00018 ck_open_session (GVariantBuilder *parameters)
00019 {
00020     GDBusConnection *bus;
00021     GVariant *result;
00022     gchar *cookie;
00023     GError *error = NULL;
00024 
00025     g_return_val_if_fail (parameters != NULL, NULL);
00026 
00027     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
00028     if (error)
00029         g_warning ("Failed to get system bus: %s", error->message);
00030     g_clear_error (&error);
00031     if (!bus)
00032         return NULL;
00033     result = g_dbus_connection_call_sync (bus,
00034                                           "org.freedesktop.ConsoleKit",
00035                                           "/org/freedesktop/ConsoleKit/Manager",
00036                                           "org.freedesktop.ConsoleKit.Manager",
00037                                           "OpenSessionWithParameters",
00038                                           g_variant_new ("(a(sv))", parameters),
00039                                           G_VARIANT_TYPE ("(s)"),
00040                                           G_DBUS_CALL_FLAGS_NONE,
00041                                           -1,
00042                                           NULL,
00043                                           &error);
00044     g_object_unref (bus);
00045 
00046     if (error)
00047         g_warning ("Failed to open CK session: %s", error->message);
00048     g_clear_error (&error);
00049     if (!result)
00050         return NULL;
00051 
00052     g_variant_get (result, "(s)", &cookie);
00053     g_variant_unref (result);
00054     g_debug ("Opened ConsoleKit session %s", cookie);
00055 
00056     return cookie;
00057 }
00058 
00059 static gchar *
00060 get_ck_session (GDBusConnection *bus, const gchar *cookie)
00061 {
00062     GVariant *result;
00063     gchar *session_path;
00064     GError *error = NULL;
00065 
00066     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
00067     result = g_dbus_connection_call_sync (bus,
00068                                           "org.freedesktop.ConsoleKit",
00069                                           "/org/freedesktop/ConsoleKit/Manager",
00070                                           "org.freedesktop.ConsoleKit.Manager",
00071                                           "GetSessionForCookie",
00072                                           g_variant_new ("(s)", cookie),
00073                                           G_VARIANT_TYPE ("(o)"),
00074                                           G_DBUS_CALL_FLAGS_NONE,
00075                                           -1,
00076                                           NULL,
00077                                           &error);
00078     g_object_unref (bus);
00079     if (error)
00080         g_warning ("Error getting ConsoleKit session: %s", error->message);
00081     g_clear_error (&error);
00082     if (!result)
00083         return NULL;
00084 
00085     g_variant_get (result, "(o)", &session_path);
00086     g_variant_unref (result);
00087 
00088     return session_path;
00089 }
00090 
00091 void
00092 ck_lock_session (const gchar *cookie)
00093 {
00094     GDBusConnection *bus;
00095     gchar *session_path;
00096     GError *error = NULL;
00097 
00098     g_return_if_fail (cookie != NULL);
00099 
00100     g_debug ("Locking ConsoleKit session %s", cookie);
00101 
00102     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
00103     if (error)
00104         g_warning ("Failed to get system bus: %s", error->message);
00105     g_clear_error (&error);
00106     if (!bus)
00107         return;
00108 
00109     session_path = get_ck_session (bus, cookie);
00110     if (session_path)
00111     {
00112         GVariant *result;
00113 
00114         result = g_dbus_connection_call_sync (bus,
00115                                               "org.freedesktop.ConsoleKit",
00116                                               session_path,
00117                                               "org.freedesktop.ConsoleKit.Session",
00118                                               "Lock",
00119                                               g_variant_new ("()"),
00120                                               G_VARIANT_TYPE ("()"),
00121                                               G_DBUS_CALL_FLAGS_NONE,
00122                                               -1,
00123                                               NULL,
00124                                               &error);
00125         if (error)
00126             g_warning ("Error locking ConsoleKit session: %s", error->message);
00127         g_clear_error (&error);
00128         if (result)
00129             g_variant_unref (result);
00130     }
00131     g_object_unref (bus);
00132 }
00133 
00134 void
00135 ck_unlock_session (const gchar *cookie)
00136 {
00137     GDBusConnection *bus;
00138     gchar *session_path;
00139     GError *error = NULL;
00140 
00141     g_return_if_fail (cookie != NULL);
00142 
00143     g_debug ("Unlocking ConsoleKit session %s", cookie);
00144 
00145     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
00146     if (error)
00147         g_warning ("Failed to get system bus: %s", error->message);
00148     g_clear_error (&error);
00149     if (!bus)
00150         return;
00151 
00152     session_path = get_ck_session (bus, cookie);
00153     if (session_path)
00154     {
00155         GVariant *result;
00156 
00157         result = g_dbus_connection_call_sync (bus,
00158                                               "org.freedesktop.ConsoleKit",
00159                                               session_path,
00160                                               "org.freedesktop.ConsoleKit.Session",
00161                                               "Unlock",
00162                                               g_variant_new ("()"),
00163                                               G_VARIANT_TYPE ("()"),
00164                                               G_DBUS_CALL_FLAGS_NONE,
00165                                               -1,
00166                                               NULL,
00167                                               &error);
00168         if (error)
00169             g_warning ("Error unlocking ConsoleKit session: %s", error->message);
00170         g_clear_error (&error);
00171         if (result)
00172             g_variant_unref (result);
00173     }
00174     g_object_unref (bus);
00175 }
00176 
00177 void
00178 ck_close_session (const gchar *cookie)
00179 {
00180     GDBusConnection *bus;
00181     GVariant *result;
00182     gboolean is_closed;
00183     GError *error = NULL;
00184 
00185     g_return_if_fail (cookie != NULL);
00186 
00187     g_debug ("Ending ConsoleKit session %s", cookie);
00188 
00189     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
00190     if (error)
00191         g_warning ("Failed to get system bus: %s", error->message);
00192     g_clear_error (&error);
00193     if (!bus)
00194         return;
00195     result = g_dbus_connection_call_sync (bus,
00196                                           "org.freedesktop.ConsoleKit",
00197                                           "/org/freedesktop/ConsoleKit/Manager",
00198                                           "org.freedesktop.ConsoleKit.Manager",
00199                                           "CloseSession",
00200                                           g_variant_new ("(s)", cookie),
00201                                           G_VARIANT_TYPE ("(b)"),
00202                                           G_DBUS_CALL_FLAGS_NONE,
00203                                           -1,
00204                                           NULL,
00205                                           &error);
00206     g_object_unref (bus);
00207 
00208     if (error)
00209         g_warning ("Error ending ConsoleKit session: %s", error->message);
00210     g_clear_error (&error);
00211     if (!result)
00212         return;
00213 
00214     g_variant_get (result, "(b)", &is_closed);
00215     g_variant_unref (result);
00216 
00217     if (!is_closed)
00218         g_warning ("ConsoleKit.Manager.CloseSession() returned false");
00219 }