]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/login1.c
24b4d6daf39cd2034a84b1daf9fb0cd7537e1e75
[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 gboolean
18 login1_is_running (void)
19 {
20     return access ("/run/systemd/seats/", F_OK) >= 0;
21 }
22
23 gchar *
24 login1_get_session_id (void)
25 {
26     GDBusConnection *bus;
27     GVariant *result;
28     gchar *session_path;
29     GError *error = NULL;
30
31     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
32     if (error)
33         g_warning ("Failed to get system bus: %s", error->message);
34     g_clear_error (&error);
35     if (!bus)
36         return NULL;
37     result = g_dbus_connection_call_sync (bus,
38                                           "org.freedesktop.login1",
39                                           "/org/freedesktop/login1",
40                                           "org.freedesktop.login1.Manager",
41                                           "GetSessionByPID",
42                                           g_variant_new ("(u)", getpid()),
43                                           G_VARIANT_TYPE ("(o)"),
44                                           G_DBUS_CALL_FLAGS_NONE,
45                                           -1,
46                                           NULL,
47                                           &error);
48     g_object_unref (bus);
49
50     if (error)
51         g_warning ("Failed to open login1 session: %s", error->message);
52     g_clear_error (&error);
53     if (!result)
54         return NULL;
55
56     g_variant_get (result, "(o)", &session_path);
57     g_variant_unref (result);
58     g_debug ("Got login1 session id: %s", session_path);
59
60     return session_path;
61 }
62
63 void
64 login1_lock_session (const gchar *session_path)
65 {
66     GDBusConnection *bus;
67     GError *error = NULL;
68
69     g_return_if_fail (session_path != NULL);
70
71     g_debug ("Locking login1 session %s", session_path);
72
73     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
74     if (error)
75         g_warning ("Failed to get system bus: %s", error->message);
76     g_clear_error (&error);
77     if (!bus)
78         return;
79
80     if (session_path)
81     {
82         GVariant *result;
83
84         result = g_dbus_connection_call_sync (bus,
85                                               "org.freedesktop.login1",
86                                               session_path,
87                                               "org.freedesktop.login1.Session",
88                                               "Lock",
89                                               g_variant_new ("()"),
90                                               G_VARIANT_TYPE ("()"),
91                                               G_DBUS_CALL_FLAGS_NONE,
92                                               -1,
93                                               NULL,
94                                               &error);
95         if (error)
96             g_warning ("Error locking login1 session: %s", error->message);
97         g_clear_error (&error);
98         if (result)
99             g_variant_unref (result);
100     }
101     g_object_unref (bus);
102 }
103
104 void
105 login1_unlock_session (const gchar *session_path)
106 {
107     GDBusConnection *bus;
108     GError *error = NULL;
109
110     g_return_if_fail (session_path != NULL);
111
112     g_debug ("Unlocking login1 session %s", session_path);
113
114     bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
115     if (error)
116         g_warning ("Failed to get system bus: %s", error->message);
117     g_clear_error (&error);
118     if (!bus)
119         return;
120
121     if (session_path)
122     {
123         GVariant *result;
124
125         result = g_dbus_connection_call_sync (bus,
126                                               "org.freedesktop.login1",
127                                               session_path,
128                                               "org.freedesktop.login1.Session",
129                                               "Unlock",
130                                               g_variant_new ("()"),
131                                               G_VARIANT_TYPE ("()"),
132                                               G_DBUS_CALL_FLAGS_NONE,
133                                               -1,
134                                               NULL,
135                                               &error);
136         if (error)
137             g_warning ("Error unlocking login1 session: %s", error->message);
138         g_clear_error (&error);
139         if (result)
140             g_variant_unref (result);
141     }
142     g_object_unref (bus);
143 }