]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/logind-dbus.c
logind: make idle hint logic work
[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" \
bef422ae 30 " <method name=\"GetSession\">\n" \
3f49d45a 31 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
bef422ae 32 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
3f49d45a
LP
33 " </method>\n" \
34 " <method name=\"GetUser\">\n" \
bef422ae 35 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
3f49d45a
LP
36 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
37 " </method>\n" \
bef422ae 38 " <method name=\"GetSeat\">\n" \
3f49d45a 39 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
bef422ae 40 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
3f49d45a 41 " </method>\n" \
bef422ae 42 " <method name=\"ListSessions\">\n" \
e1c9c2d5 43 " <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
3f49d45a
LP
44 " </method>\n" \
45 " <method name=\"ListUsers\">\n" \
46 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
47 " </method>\n" \
bef422ae
LP
48 " <method name=\"ListSeats\">\n" \
49 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
3f49d45a
LP
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" \
bef422ae 75 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
3f49d45a
LP
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;
77527da0 133 dbus_bool_t b;
a185c5aa
LP
134
135 assert(i);
136 assert(property);
137 assert(m);
138
77527da0 139 b = manager_get_idle_hint(m, NULL) > 0;
a185c5aa
LP
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;
bef422ae 187 int r;
3f49d45a
LP
188
189 assert(connection);
190 assert(message);
191 assert(m);
192
193 dbus_error_init(&error);
194
bef422ae
LP
195 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
196 const char *name;
197 char *p;
198 Session *session;
199 bool b;
200
201 if (!dbus_message_get_args(
202 message,
203 &error,
204 DBUS_TYPE_STRING, &name,
205 DBUS_TYPE_INVALID))
206 return bus_send_error_reply(connection, message, &error, -EINVAL);
207
208 session = hashmap_get(m->sessions, name);
209 if (!session)
210 return bus_send_error_reply(connection, message, &error, -ENOENT);
211
212 reply = dbus_message_new_method_return(message);
213 if (!reply)
214 goto oom;
215
216 p = session_bus_path(session);
217 if (!p)
218 goto oom;
219
220 b = dbus_message_append_args(
221 reply,
222 DBUS_TYPE_OBJECT_PATH, &p,
223 DBUS_TYPE_INVALID);
224 free(p);
225
226 if (!b)
227 goto oom;
228
229 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
230 uint32_t uid;
231 char *p;
232 User *user;
233 bool b;
234
235 if (!dbus_message_get_args(
236 message,
237 &error,
238 DBUS_TYPE_UINT32, &uid,
239 DBUS_TYPE_INVALID))
240 return bus_send_error_reply(connection, message, &error, -EINVAL);
241
242 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
243 if (!user)
244 return bus_send_error_reply(connection, message, &error, -ENOENT);
245
246 reply = dbus_message_new_method_return(message);
247 if (!reply)
248 goto oom;
249
250 p = user_bus_path(user);
251 if (!p)
252 goto oom;
253
254 b = dbus_message_append_args(
255 reply,
256 DBUS_TYPE_OBJECT_PATH, &p,
257 DBUS_TYPE_INVALID);
258 free(p);
259
260 if (!b)
261 goto oom;
262
263 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
264 const char *name;
265 char *p;
266 Seat *seat;
267 bool b;
268
269 if (!dbus_message_get_args(
270 message,
271 &error,
272 DBUS_TYPE_STRING, &name,
273 DBUS_TYPE_INVALID))
274 return bus_send_error_reply(connection, message, &error, -EINVAL);
275
276 seat = hashmap_get(m->seats, name);
277 if (!seat)
278 return bus_send_error_reply(connection, message, &error, -ENOENT);
279
280 reply = dbus_message_new_method_return(message);
281 if (!reply)
282 goto oom;
283
284 p = seat_bus_path(seat);
285 if (!p)
286 goto oom;
287
288 b = dbus_message_append_args(
289 reply,
290 DBUS_TYPE_OBJECT_PATH, &p,
291 DBUS_TYPE_INVALID);
292 free(p);
293
294 if (!b)
295 goto oom;
296
e1c9c2d5
LP
297 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
298 char *p;
299 Session *session;
300 Iterator i;
301 DBusMessageIter iter, sub;
302 const char *empty = "";
303
304 reply = dbus_message_new_method_return(message);
305 if (!reply)
306 goto oom;
307
308 dbus_message_iter_init_append(reply, &iter);
309
310 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "susso", &sub))
311 goto oom;
312
313 HASHMAP_FOREACH(session, m->sessions, i) {
314 DBusMessageIter sub2;
315 uint32_t uid;
316
317 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
318 goto oom;
319
320 uid = session->user->uid;
321
322 p = session_bus_path(session);
323 if (!p)
324 goto oom;
325
326 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
327 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
328 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
329 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
330 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
331 free(p);
332 goto oom;
333 }
334
335 free(p);
336
337 if (!dbus_message_iter_close_container(&sub, &sub2))
338 goto oom;
339 }
340
341 if (!dbus_message_iter_close_container(&iter, &sub))
342 goto oom;
343
344 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
345 char *p;
346 User *user;
347 Iterator i;
348 DBusMessageIter iter, sub;
349
350 reply = dbus_message_new_method_return(message);
351 if (!reply)
352 goto oom;
353
354 dbus_message_iter_init_append(reply, &iter);
355
356 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "uso", &sub))
357 goto oom;
358
359 HASHMAP_FOREACH(user, m->users, i) {
360 DBusMessageIter sub2;
361 uint32_t uid;
362
363 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
364 goto oom;
365
366 uid = user->uid;
367
368 p = user_bus_path(user);
369 if (!p)
370 goto oom;
371
372 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
373 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
374 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
375 free(p);
376 goto oom;
377 }
378
379 free(p);
380
381 if (!dbus_message_iter_close_container(&sub, &sub2))
382 goto oom;
383 }
384
385 if (!dbus_message_iter_close_container(&iter, &sub))
386 goto oom;
387
388 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
389 char *p;
390 Seat *seat;
391 Iterator i;
392 DBusMessageIter iter, sub;
393
394 reply = dbus_message_new_method_return(message);
395 if (!reply)
396 goto oom;
397
398 dbus_message_iter_init_append(reply, &iter);
399
400 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "so", &sub))
401 goto oom;
402
403 HASHMAP_FOREACH(seat, m->seats, i) {
404 DBusMessageIter sub2;
405
406 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
407 goto oom;
408
409 p = seat_bus_path(seat);
410 if (!p)
411 goto oom;
412
413 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
414 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
415 free(p);
416 goto oom;
417 }
418
419 free(p);
420
421 if (!dbus_message_iter_close_container(&sub, &sub2))
422 goto oom;
423 }
424
425 if (!dbus_message_iter_close_container(&iter, &sub))
426 goto oom;
427
bef422ae
LP
428 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
429 const char *name;
430 Session *session;
431
432 if (!dbus_message_get_args(
433 message,
434 &error,
435 DBUS_TYPE_STRING, &name,
436 DBUS_TYPE_INVALID))
437 return bus_send_error_reply(connection, message, &error, -EINVAL);
438
439 session = hashmap_get(m->sessions, name);
440 if (!session)
441 return bus_send_error_reply(connection, message, &error, -ENOENT);
442
443 r = session_activate(session);
444 if (r < 0)
445 return bus_send_error_reply(connection, message, NULL, r);
446
447 reply = dbus_message_new_method_return(message);
448 if (!reply)
449 goto oom;
450
451 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
452 const char *name;
453 Session *session;
454
455 if (!dbus_message_get_args(
456 message,
457 &error,
458 DBUS_TYPE_STRING, &name,
459 DBUS_TYPE_INVALID))
460 return bus_send_error_reply(connection, message, &error, -EINVAL);
461
462 session = hashmap_get(m->sessions, name);
463 if (!session)
464 return bus_send_error_reply(connection, message, &error, -ENOENT);
465
466 r = session_stop(session);
467 if (r < 0)
468 return bus_send_error_reply(connection, message, NULL, r);
469
470 reply = dbus_message_new_method_return(message);
471 if (!reply)
472 goto oom;
473
474 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
475 uint32_t uid;
476 User *user;
477
478 if (!dbus_message_get_args(
479 message,
480 &error,
481 DBUS_TYPE_UINT32, &uid,
482 DBUS_TYPE_INVALID))
483 return bus_send_error_reply(connection, message, &error, -EINVAL);
484
485 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
486 if (!user)
487 return bus_send_error_reply(connection, message, &error, -ENOENT);
488
489 r = user_stop(user);
490 if (r < 0)
491 return bus_send_error_reply(connection, message, NULL, r);
492
493 reply = dbus_message_new_method_return(message);
494 if (!reply)
495 goto oom;
496
497 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
498 const char *name;
499 Seat *seat;
500
501 if (!dbus_message_get_args(
502 message,
503 &error,
504 DBUS_TYPE_STRING, &name,
505 DBUS_TYPE_INVALID))
506 return bus_send_error_reply(connection, message, &error, -EINVAL);
507
508 seat = hashmap_get(m->seats, name);
509 if (!seat)
510 return bus_send_error_reply(connection, message, &error, -ENOENT);
511
512 r = seat_stop_sessions(seat);
513 if (r < 0)
514 return bus_send_error_reply(connection, message, NULL, r);
515
516 reply = dbus_message_new_method_return(message);
517 if (!reply)
518 goto oom;
519
520 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
3f49d45a
LP
521 char *introspection = NULL;
522 FILE *f;
523 Iterator i;
524 Session *session;
525 Seat *seat;
526 User *user;
527 size_t size;
528 char *p;
529
530 if (!(reply = dbus_message_new_method_return(message)))
531 goto oom;
532
533 /* We roll our own introspection code here, instead of
534 * relying on bus_default_message_handler() because we
535 * need to generate our introspection string
536 * dynamically. */
537
538 if (!(f = open_memstream(&introspection, &size)))
539 goto oom;
540
541 fputs(INTROSPECTION_BEGIN, f);
542
543 HASHMAP_FOREACH(seat, m->seats, i) {
544 p = bus_path_escape(seat->id);
545
546 if (p) {
547 fprintf(f, "<node name=\"seat/%s\"/>", p);
548 free(p);
549 }
550 }
551
552 HASHMAP_FOREACH(user, m->users, i)
553 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
554
555 HASHMAP_FOREACH(session, m->sessions, i) {
556 p = bus_path_escape(session->id);
557
558 if (p) {
559 fprintf(f, "<node name=\"session/%s\"/>", p);
560 free(p);
561 }
562 }
563
564 fputs(INTROSPECTION_END, f);
565
566 if (ferror(f)) {
567 fclose(f);
568 free(introspection);
569 goto oom;
570 }
571
572 fclose(f);
573
574 if (!introspection)
575 goto oom;
576
577 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
578 free(introspection);
579 goto oom;
580 }
581
582 free(introspection);
583 } else
584 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
585
586 if (reply) {
587 if (!dbus_connection_send(connection, reply, NULL))
588 goto oom;
589
590 dbus_message_unref(reply);
591 }
592
593 return DBUS_HANDLER_RESULT_HANDLED;
594
595oom:
596 if (reply)
597 dbus_message_unref(reply);
598
599 dbus_error_free(&error);
600
601 return DBUS_HANDLER_RESULT_NEED_MEMORY;
602}
603
604const DBusObjectPathVTable bus_manager_vtable = {
605 .message_function = manager_message_handler
606};
9418f147
LP
607
608int manager_send_changed(Manager *manager, const char *properties) {
609 DBusMessage *m;
610 int r = -ENOMEM;
611
612 assert(manager);
613
614 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
615 if (!m)
616 goto finish;
617
618 if (!dbus_connection_send(manager->bus, m, NULL))
619 goto finish;
620
621 r = 0;
622
623finish:
624 if (m)
625 dbus_message_unref(m);
626
627 return r;
628}