]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/logind-dbus.c
logind: implement idle hint logic
[thirdparty/systemd.git] / src / logind-dbus.c
CommitLineData
3f49d45a
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
a185c5aa
LP
22#include <errno.h>
23#include <string.h>
24
3f49d45a
LP
25#include "logind.h"
26#include "dbus-common.h"
27
28#define BUS_MANAGER_INTERFACE \
29 " <interface name=\"org.freedesktop.login1.Manager\">\n" \
30 " <method name=\"GetSeat\">\n" \
31 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
32 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
33 " </method>\n" \
34 " <method name=\"GetUser\">\n" \
35 " <arg name=\"uid\" type=\"t\" direction=\"in\"/>\n" \
36 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
37 " </method>\n" \
38 " <method name=\"GetSession\">\n" \
39 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
40 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
41 " </method>\n" \
42 " <method name=\"ListSeats\">\n" \
43 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
44 " </method>\n" \
45 " <method name=\"ListUsers\">\n" \
46 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
47 " </method>\n" \
48 " <method name=\"ListSessions\">\n" \
49 " <arg name=\"users\" type=\"a(sussso)\" direction=\"out\"/>\n" \
50 " </method>\n" \
51 " <method name=\"CreateSession\">\n" \
52 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
53 " <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
54 " <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
58 " <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
59 " <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
60 " <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
61 " <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
62 " <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
63 " <arg name=\"kill_processes\" type=\"as\" direction=\"in\"/>\n" \
64 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
65 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
66 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
67 " </method>\n" \
a185c5aa
LP
68 " <method name=\"ActivateSession\">\n" \
69 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
70 " </method>\n" \
3f49d45a
LP
71 " <method name=\"TerminateSession\">\n" \
72 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
73 " </method>\n" \
74 " <method name=\"TerminateUser\">\n" \
75 " <arg name=\"uid\" type=\"t\" direction=\"in\"/>\n" \
76 " </method>\n" \
77 " <method name=\"TerminateSeat\">\n" \
78 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
79 " </method>\n" \
80 " <signal name=\"SessionNew\">\n" \
81 " <arg name=\"id\" type=\"s\"/>\n" \
82 " <arg name=\"path\" type=\"o\"/>\n" \
83 " </signal>\n" \
84 " <signal name=\"SessionRemoved\">\n" \
85 " <arg name=\"id\" type=\"s\"/>\n" \
86 " <arg name=\"path\" type=\"o\"/>\n" \
87 " </signal>\n" \
88 " <signal name=\"UserNew\">\n" \
89 " <arg name=\"uid\" type=\"u\"/>\n" \
90 " <arg name=\"path\" type=\"o\"/>\n" \
91 " </signal>\n" \
92 " <signal name=\"UserRemoved\">\n" \
93 " <arg name=\"uid\" type=\"u\"/>\n" \
94 " <arg name=\"path\" type=\"o\"/>\n" \
95 " </signal>\n" \
96 " <signal name=\"SeatNew\">\n" \
97 " <arg name=\"id\" type=\"s\"/>\n" \
98 " <arg name=\"path\" type=\"o\"/>\n" \
99 " </signal>\n" \
100 " <signal name=\"SeatRemoved\">\n" \
101 " <arg name=\"id\" type=\"s\"/>\n" \
102 " <arg name=\"path\" type=\"o\"/>\n" \
103 " </signal>\n" \
104 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
105 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
106 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
107 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
108 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
109 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
110 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
a185c5aa
LP
111 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
112 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
113 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
3f49d45a
LP
114 " </interface>\n"
115
116#define INTROSPECTION_BEGIN \
117 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
118 "<node>\n" \
119 BUS_MANAGER_INTERFACE \
120 BUS_PROPERTIES_INTERFACE \
121 BUS_PEER_INTERFACE \
122 BUS_INTROSPECTABLE_INTERFACE
123
124#define INTROSPECTION_END \
125 "</node>\n"
126
127#define INTERFACES_LIST \
128 BUS_GENERIC_INTERFACES_LIST \
129 "org.freedesktop.login1.Manager\0"
130
a185c5aa
LP
131static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
132 Manager *m = data;
133 bool b;
134
135 assert(i);
136 assert(property);
137 assert(m);
138
139 b = manager_get_idle_hint(m, NULL);
140 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
141 return -ENOMEM;
142
143 return 0;
144}
145
146static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
147 Manager *m = data;
148 dual_timestamp t;
149 uint64_t u;
150
151 assert(i);
152 assert(property);
153 assert(m);
154
155 manager_get_idle_hint(m, &t);
156 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
157
158 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
159 return -ENOMEM;
160
161 return 0;
162}
163
3f49d45a
LP
164static DBusHandlerResult manager_message_handler(
165 DBusConnection *connection,
166 DBusMessage *message,
167 void *userdata) {
168
169 Manager *m = userdata;
170
171 const BusProperty properties[] = {
a185c5aa
LP
172 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
173 { "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
174 { "org.freedesktop.login1.Manager", "ResetControllers", bus_property_append_strv, "as", m->reset_controllers },
175 { "org.freedesktop.login1.Manager", "NAutoVTs", bus_property_append_unsigned, "u", &m->n_autovts },
176 { "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
177 { "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
178 { "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
179 { "org.freedesktop.login1.Manager", "IdleHint", bus_manager_append_idle_hint, "b", m },
180 { "org.freedesktop.login1.Manager", "IdleSinceHint", bus_manager_append_idle_hint_since, "t", m },
181 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
3f49d45a
LP
182 { NULL, NULL, NULL, NULL, NULL }
183 };
184
185 DBusError error;
186 DBusMessage *reply = NULL;
187
188 assert(connection);
189 assert(message);
190 assert(m);
191
192 dbus_error_init(&error);
193
194 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
195 char *introspection = NULL;
196 FILE *f;
197 Iterator i;
198 Session *session;
199 Seat *seat;
200 User *user;
201 size_t size;
202 char *p;
203
204 if (!(reply = dbus_message_new_method_return(message)))
205 goto oom;
206
207 /* We roll our own introspection code here, instead of
208 * relying on bus_default_message_handler() because we
209 * need to generate our introspection string
210 * dynamically. */
211
212 if (!(f = open_memstream(&introspection, &size)))
213 goto oom;
214
215 fputs(INTROSPECTION_BEGIN, f);
216
217 HASHMAP_FOREACH(seat, m->seats, i) {
218 p = bus_path_escape(seat->id);
219
220 if (p) {
221 fprintf(f, "<node name=\"seat/%s\"/>", p);
222 free(p);
223 }
224 }
225
226 HASHMAP_FOREACH(user, m->users, i)
227 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
228
229 HASHMAP_FOREACH(session, m->sessions, i) {
230 p = bus_path_escape(session->id);
231
232 if (p) {
233 fprintf(f, "<node name=\"session/%s\"/>", p);
234 free(p);
235 }
236 }
237
238 fputs(INTROSPECTION_END, f);
239
240 if (ferror(f)) {
241 fclose(f);
242 free(introspection);
243 goto oom;
244 }
245
246 fclose(f);
247
248 if (!introspection)
249 goto oom;
250
251 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
252 free(introspection);
253 goto oom;
254 }
255
256 free(introspection);
257 } else
258 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
259
260 if (reply) {
261 if (!dbus_connection_send(connection, reply, NULL))
262 goto oom;
263
264 dbus_message_unref(reply);
265 }
266
267 return DBUS_HANDLER_RESULT_HANDLED;
268
269oom:
270 if (reply)
271 dbus_message_unref(reply);
272
273 dbus_error_free(&error);
274
275 return DBUS_HANDLER_RESULT_NEED_MEMORY;
276}
277
278const DBusObjectPathVTable bus_manager_vtable = {
279 .message_function = manager_message_handler
280};