]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/console-kit.c
Load all users only when really needed
[sojka/lightdm.git] / src / console-kit.c
1 /* -*- Mode: C; indent-tabs-mode: nil; tab-width: 4 -*-
2  *
3  * Copyright (C) 2010-2011 Robert Ancell.
4  * Author: Robert Ancell <robert.ancell@canonical.com>
5  *
6  * This program is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
10  * license.
11  */
12
13 #include <gio/gio.h>
14
15 #include "console-kit.h"
16
17 gchar *
18 ck_open_session (GVariantBuilder *parameters)
19 {
20     GDBusConnection *bus;
21     GVariant *result;
22     gchar *cookie;
23     GError *error = NULL;
24
25     g_return_val_if_fail (parameters != NULL, NULL);
26
27     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
28     if (error)
29         g_warning ("Failed to get system bus: %s", error->message);
30     g_clear_error (&error);
31     if (!bus)
32         return NULL;
33     result = g_dbus_connection_call_sync (bus,
34                                           "org.freedesktop.ConsoleKit",
35                                           "/org/freedesktop/ConsoleKit/Manager",
36                                           "org.freedesktop.ConsoleKit.Manager",
37                                           "OpenSessionWithParameters",
38                                           g_variant_new ("(a(sv))", parameters),
39                                           G_VARIANT_TYPE ("(s)"),
40                                           G_DBUS_CALL_FLAGS_NONE,
41                                           -1,
42                                           NULL,
43                                           &error);
44     g_object_unref (bus);
45
46     if (error)
47         g_warning ("Failed to open CK session: %s", error->message);
48     g_clear_error (&error);
49     if (!result)
50         return NULL;
51
52     g_variant_get (result, "(s)", &cookie);
53     g_variant_unref (result);
54     g_debug ("Opened ConsoleKit session %s", cookie);
55
56     return cookie;
57 }
58
59 static gchar *
60 get_ck_session (GDBusConnection *bus, const gchar *cookie)
61 {
62     GVariant *result;
63     gchar *session_path;
64     GError *error = NULL;
65
66     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
67     result = g_dbus_connection_call_sync (bus,
68                                           "org.freedesktop.ConsoleKit",
69                                           "/org/freedesktop/ConsoleKit/Manager",
70                                           "org.freedesktop.ConsoleKit.Manager",
71                                           "GetSessionForCookie",
72                                           g_variant_new ("(s)", cookie),
73                                           G_VARIANT_TYPE ("(o)"),
74                                           G_DBUS_CALL_FLAGS_NONE,
75                                           -1,
76                                           NULL,
77                                           &error);
78     g_object_unref (bus);
79     if (error)
80         g_warning ("Error getting ConsoleKit session: %s", error->message);
81     g_clear_error (&error);
82     if (!result)
83         return NULL;
84
85     g_variant_get (result, "(o)", &session_path);
86     g_variant_unref (result);
87
88     return session_path;
89 }
90
91 void
92 ck_lock_session (const gchar *cookie)
93 {
94     GDBusConnection *bus;
95     gchar *session_path;
96     GError *error = NULL;
97
98     g_return_if_fail (cookie != NULL);
99
100     g_debug ("Locking ConsoleKit session %s", cookie);
101
102     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
103     if (error)
104         g_warning ("Failed to get system bus: %s", error->message);
105     g_clear_error (&error);
106     if (!bus)
107         return;
108
109     session_path = get_ck_session (bus, cookie);
110     if (session_path)
111     {
112         GVariant *result;
113
114         result = g_dbus_connection_call_sync (bus,
115                                               "org.freedesktop.ConsoleKit",
116                                               session_path,
117                                               "org.freedesktop.ConsoleKit.Session",
118                                               "Lock",
119                                               g_variant_new ("()"),
120                                               G_VARIANT_TYPE ("()"),
121                                               G_DBUS_CALL_FLAGS_NONE,
122                                               -1,
123                                               NULL,
124                                               &error);
125         if (error)
126             g_warning ("Error locking ConsoleKit session: %s", error->message);
127         g_clear_error (&error);
128         if (result)
129             g_variant_unref (result);
130     }
131     g_object_unref (bus);
132 }
133
134 void
135 ck_unlock_session (const gchar *cookie)
136 {
137     GDBusConnection *bus;
138     gchar *session_path;
139     GError *error = NULL;
140
141     g_return_if_fail (cookie != NULL);
142
143     g_debug ("Unlocking ConsoleKit session %s", cookie);
144
145     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
146     if (error)
147         g_warning ("Failed to get system bus: %s", error->message);
148     g_clear_error (&error);
149     if (!bus)
150         return;
151
152     session_path = get_ck_session (bus, cookie);
153     if (session_path)
154     {
155         GVariant *result;
156
157         result = g_dbus_connection_call_sync (bus,
158                                               "org.freedesktop.ConsoleKit",
159                                               session_path,
160                                               "org.freedesktop.ConsoleKit.Session",
161                                               "Unlock",
162                                               g_variant_new ("()"),
163                                               G_VARIANT_TYPE ("()"),
164                                               G_DBUS_CALL_FLAGS_NONE,
165                                               -1,
166                                               NULL,
167                                               &error);
168         if (error)
169             g_warning ("Error unlocking ConsoleKit session: %s", error->message);
170         g_clear_error (&error);
171         if (result)
172             g_variant_unref (result);
173     }
174     g_object_unref (bus);
175 }
176
177 void
178 ck_activate_session (const gchar *cookie)
179 {
180     GDBusConnection *bus;
181     gchar *session_path;
182     GError *error = NULL;
183
184     g_return_if_fail (cookie != NULL);
185
186     g_debug ("Activating ConsoleKit session %s", cookie);
187
188     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
189     if (error)
190         g_warning ("Failed to get system bus: %s", error->message);
191     g_clear_error (&error);
192     if (!bus)
193         return;
194
195     session_path = get_ck_session (bus, cookie);
196     if (session_path)
197     {
198         GVariant *result;
199
200         result = g_dbus_connection_call_sync (bus,
201                                               "org.freedesktop.ConsoleKit",
202                                               session_path,
203                                               "org.freedesktop.ConsoleKit.Session",
204                                               "Activate",
205                                               g_variant_new ("()"),
206                                               G_VARIANT_TYPE ("()"),
207                                               G_DBUS_CALL_FLAGS_NONE,
208                                               -1,
209                                               NULL,
210                                               &error);
211         if (error)
212             g_warning ("Error activating ConsoleKit session: %s", error->message);
213         g_clear_error (&error);
214         if (result)
215             g_variant_unref (result);
216     }
217     g_object_unref (bus);
218 }
219
220 void
221 ck_close_session (const gchar *cookie)
222 {
223     GDBusConnection *bus;
224     GVariant *result;
225     gboolean is_closed;
226     GError *error = NULL;
227
228     g_return_if_fail (cookie != NULL);
229
230     g_debug ("Ending ConsoleKit session %s", cookie);
231
232     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
233     if (error)
234         g_warning ("Failed to get system bus: %s", error->message);
235     g_clear_error (&error);
236     if (!bus)
237         return;
238     result = g_dbus_connection_call_sync (bus,
239                                           "org.freedesktop.ConsoleKit",
240                                           "/org/freedesktop/ConsoleKit/Manager",
241                                           "org.freedesktop.ConsoleKit.Manager",
242                                           "CloseSession",
243                                           g_variant_new ("(s)", cookie),
244                                           G_VARIANT_TYPE ("(b)"),
245                                           G_DBUS_CALL_FLAGS_NONE,
246                                           -1,
247                                           NULL,
248                                           &error);
249     g_object_unref (bus);
250
251     if (error)
252         g_warning ("Error ending ConsoleKit session: %s", error->message);
253     g_clear_error (&error);
254     if (!result)
255         return;
256
257     g_variant_get (result, "(b)", &is_closed);
258     g_variant_unref (result);
259
260     if (!is_closed)
261         g_warning ("ConsoleKit.Manager.CloseSession() returned false");
262 }
263
264 gchar *
265 ck_get_xdg_runtime_dir (const gchar *cookie)
266 {
267     GDBusConnection *bus;
268     gchar *session_path;
269     gchar *runtime_dir = NULL;
270     GError *error = NULL;
271
272     g_return_val_if_fail (cookie != NULL, NULL);
273
274     g_debug ("Getting XDG_RUNTIME_DIR from ConsoleKit for session %s", cookie);
275
276     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
277     if (error)
278         g_warning ("Failed to get system bus: %s", error->message);
279     g_clear_error (&error);
280     if (!bus)
281         return NULL;
282
283     session_path = get_ck_session (bus, cookie);
284     if (session_path)
285     {
286         GVariant *result;
287
288         result = g_dbus_connection_call_sync (bus,
289                                               "org.freedesktop.ConsoleKit",
290                                               session_path,
291                                               "org.freedesktop.ConsoleKit.Session",
292                                               "GetXDGRuntimeDir",
293                                               g_variant_new ("()"),
294                                               G_VARIANT_TYPE ("(s)"),
295                                               G_DBUS_CALL_FLAGS_NONE,
296                                               -1,
297                                               NULL,
298                                               &error);
299         if (error)
300             g_warning ("Error getting XDG_RUNTIME_DIR from ConsoleKit: %s", error->message);
301         g_clear_error (&error);
302         if (!result)
303             return NULL;
304
305         g_variant_get (result, "(s)", &runtime_dir);
306         g_variant_unref (result);
307         g_debug ("ConsoleKit XDG_RUNTIME_DIR is %s", runtime_dir);
308     }
309
310     g_object_unref (bus);
311
312     return runtime_dir;
313 }