]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/login1.c
Just create proxy and ask for owner rather than manually calling StartServiceByName...
[sojka/lightdm.git] / src / login1.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 "login1.h"
16
17 static gboolean
18 check_login1 (void)
19 {
20     GDBusProxy *proxy;
21     gchar *owner;
22     gboolean success;
23
24     proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
25                                            G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
26                                            NULL,
27                                            "org.freedesktop.login1",
28                                            "/org/freedesktop/login1",
29                                            "org.freedesktop.login1.Manager",
30                                            NULL,
31                                            NULL);
32     if (!proxy)
33         return FALSE;
34
35     owner = g_dbus_proxy_get_name_owner (proxy);
36     g_object_unref (proxy);
37
38     success = (owner != NULL);
39     g_free (owner);
40
41     return success;
42 }
43
44 gboolean
45 login1_is_running (void)
46 {
47     static gboolean have_checked = FALSE;
48     static gboolean is_running = FALSE;
49
50     if (!have_checked)
51     {
52         have_checked = TRUE;
53         is_running = check_login1();
54     }
55
56     return is_running;
57 }
58
59 gchar *
60 login1_get_session_id (void)
61 {
62     GDBusConnection *bus;
63     GVariant *result;
64     gchar *session_path;
65     GError *error = NULL;
66
67     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
68     if (error)
69         g_warning ("Failed to get system bus: %s", error->message);
70     g_clear_error (&error);
71     if (!bus)
72         return NULL;
73     result = g_dbus_connection_call_sync (bus,
74                                           "org.freedesktop.login1",
75                                           "/org/freedesktop/login1",
76                                           "org.freedesktop.login1.Manager",
77                                           "GetSessionByPID",
78                                           g_variant_new ("(u)", getpid()),
79                                           G_VARIANT_TYPE ("(o)"),
80                                           G_DBUS_CALL_FLAGS_NONE,
81                                           -1,
82                                           NULL,
83                                           &error);
84     g_object_unref (bus);
85
86     if (error)
87         g_warning ("Failed to open login1 session: %s", error->message);
88     g_clear_error (&error);
89     if (!result)
90         return NULL;
91
92     g_variant_get (result, "(o)", &session_path);
93     g_variant_unref (result);
94     g_debug ("Got login1 session id: %s", session_path);
95
96     return session_path;
97 }
98
99 void
100 login1_lock_session (const gchar *session_path)
101 {
102     GDBusConnection *bus;
103     GError *error = NULL;
104
105     g_return_if_fail (session_path != NULL);
106
107     g_debug ("Locking login1 session %s", session_path);
108
109     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
110     if (error)
111         g_warning ("Failed to get system bus: %s", error->message);
112     g_clear_error (&error);
113     if (!bus)
114         return;
115
116     if (session_path)
117     {
118         GVariant *result;
119
120         result = g_dbus_connection_call_sync (bus,
121                                               "org.freedesktop.login1",
122                                               session_path,
123                                               "org.freedesktop.login1.Session",
124                                               "Lock",
125                                               g_variant_new ("()"),
126                                               G_VARIANT_TYPE ("()"),
127                                               G_DBUS_CALL_FLAGS_NONE,
128                                               -1,
129                                               NULL,
130                                               &error);
131         if (error)
132             g_warning ("Error locking login1 session: %s", error->message);
133         g_clear_error (&error);
134         if (result)
135             g_variant_unref (result);
136     }
137     g_object_unref (bus);
138 }
139
140 void
141 login1_unlock_session (const gchar *session_path)
142 {
143     GDBusConnection *bus;
144     GError *error = NULL;
145
146     g_return_if_fail (session_path != NULL);
147
148     g_debug ("Unlocking login1 session %s", session_path);
149
150     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
151     if (error)
152         g_warning ("Failed to get system bus: %s", error->message);
153     g_clear_error (&error);
154     if (!bus)
155         return;
156
157     if (session_path)
158     {
159         GVariant *result;
160
161         result = g_dbus_connection_call_sync (bus,
162                                               "org.freedesktop.login1",
163                                               session_path,
164                                               "org.freedesktop.login1.Session",
165                                               "Unlock",
166                                               g_variant_new ("()"),
167                                               G_VARIANT_TYPE ("()"),
168                                               G_DBUS_CALL_FLAGS_NONE,
169                                               -1,
170                                               NULL,
171                                               &error);
172         if (error)
173             g_warning ("Error unlocking login1 session: %s", error->message);
174         g_clear_error (&error);
175         if (result)
176             g_variant_unref (result);
177     }
178     g_object_unref (bus);
179 }
180
181 void
182 login1_activate_session (const gchar *session_path)
183 {
184     GDBusConnection *bus;
185     GError *error = NULL;
186
187     g_return_if_fail (session_path != NULL);
188
189     g_debug ("Activating login1 session %s", session_path);
190
191     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
192     if (error)
193         g_warning ("Failed to get system bus: %s", error->message);
194     g_clear_error (&error);
195     if (!bus)
196         return;
197
198     if (session_path)
199     {
200         GVariant *result;
201
202         result = g_dbus_connection_call_sync (bus,
203                                               "org.freedesktop.login1",
204                                               session_path,
205                                               "org.freedesktop.login1.Session",
206                                               "Activate",
207                                               g_variant_new ("()"),
208                                               G_VARIANT_TYPE ("()"),
209                                               G_DBUS_CALL_FLAGS_NONE,
210                                               -1,
211                                               NULL,
212                                               &error);
213         if (error)
214             g_warning ("Error activating login1 session: %s", error->message);
215         g_clear_error (&error);
216         if (result)
217             g_variant_unref (result);
218     }
219     g_object_unref (bus);
220 }