1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
28 #include "dbus-common.h"
33 #define BUS_MANAGER_INTERFACE \
34 " <interface name=\"org.freedesktop.login1.Manager\">\n" \
35 " <method name=\"GetSession\">\n" \
36 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
37 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
39 " <method name=\"GetSessionByPID\">\n" \
40 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
41 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
43 " <method name=\"GetUser\">\n" \
44 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
45 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
47 " <method name=\"GetSeat\">\n" \
48 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
51 " <method name=\"ListSessions\">\n" \
52 " <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
54 " <method name=\"ListUsers\">\n" \
55 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
57 " <method name=\"ListSeats\">\n" \
58 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
60 " <method name=\"CreateSession\">\n" \
61 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
62 " <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
63 " <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" \
65 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n" \
67 " <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
68 " <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
69 " <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
70 " <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
71 " <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
72 " <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
73 " <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
74 " <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
75 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
76 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
77 " <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
78 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
79 " <arg name=\"seat\" type=\"s\" direction=\"out\"/>\n" \
80 " <arg name=\"vtnr\" type=\"u\" direction=\"out\"/>\n" \
82 " <method name=\"ActivateSession\">\n" \
83 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
85 " <method name=\"ActivateSessionOnSeat\">\n" \
86 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
87 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
89 " <method name=\"LockSession\">\n" \
90 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
92 " <method name=\"UnlockSession\">\n" \
93 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
95 " <method name=\"KillSession\">\n" \
96 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
97 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
98 " <arg name=\"signal\" type=\"s\" direction=\"in\"/>\n" \
100 " <method name=\"KillUser\">\n" \
101 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
102 " <arg name=\"signal\" type=\"s\" direction=\"in\"/>\n" \
104 " <method name=\"TerminateSession\">\n" \
105 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
107 " <method name=\"TerminateUser\">\n" \
108 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
110 " <method name=\"TerminateSeat\">\n" \
111 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
113 " <method name=\"SetUserLinger\">\n" \
114 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
115 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
116 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
118 " <method name=\"AttachDevice\">\n" \
119 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
120 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
121 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
123 " <method name=\"FlushDevices\">\n" \
124 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
126 " <method name=\"PowerOff\">\n" \
127 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
129 " <method name=\"Reboot\">\n" \
130 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
132 " <method name=\"CanPowerOff\">\n" \
133 " <arg name=\"result\" type=\"s\" direction=\"out\"/>\n" \
135 " <method name=\"CanReboot\">\n" \
136 " <arg name=\"result\" type=\"s\" direction=\"out\"/>\n" \
138 " <signal name=\"SessionNew\">\n" \
139 " <arg name=\"id\" type=\"s\"/>\n" \
140 " <arg name=\"path\" type=\"o\"/>\n" \
142 " <signal name=\"SessionRemoved\">\n" \
143 " <arg name=\"id\" type=\"s\"/>\n" \
144 " <arg name=\"path\" type=\"o\"/>\n" \
146 " <signal name=\"UserNew\">\n" \
147 " <arg name=\"uid\" type=\"u\"/>\n" \
148 " <arg name=\"path\" type=\"o\"/>\n" \
150 " <signal name=\"UserRemoved\">\n" \
151 " <arg name=\"uid\" type=\"u\"/>\n" \
152 " <arg name=\"path\" type=\"o\"/>\n" \
154 " <signal name=\"SeatNew\">\n" \
155 " <arg name=\"id\" type=\"s\"/>\n" \
156 " <arg name=\"path\" type=\"o\"/>\n" \
158 " <signal name=\"SeatRemoved\">\n" \
159 " <arg name=\"id\" type=\"s\"/>\n" \
160 " <arg name=\"path\" type=\"o\"/>\n" \
162 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
163 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
164 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
165 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
166 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
167 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
168 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
169 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
170 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
171 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
174 #define INTROSPECTION_BEGIN \
175 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
177 BUS_MANAGER_INTERFACE \
178 BUS_PROPERTIES_INTERFACE \
180 BUS_INTROSPECTABLE_INTERFACE
182 #define INTROSPECTION_END \
185 #define INTERFACES_LIST \
186 BUS_GENERIC_INTERFACES_LIST \
187 "org.freedesktop.login1.Manager\0"
189 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
197 b = manager_get_idle_hint(m, NULL) > 0;
198 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
204 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
213 manager_get_idle_hint(m, &t);
214 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
216 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
222 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
223 Session *session = NULL;
225 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
226 uint32_t uid, leader, audit_id = 0;
227 dbus_bool_t remote, kill_processes;
228 char **controllers = NULL, **reset_controllers = NULL;
231 DBusMessageIter iter;
236 DBusMessage *reply = NULL;
243 if (!dbus_message_iter_init(message, &iter) ||
244 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
247 dbus_message_iter_get_basic(&iter, &uid);
249 if (!dbus_message_iter_next(&iter) ||
250 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
253 dbus_message_iter_get_basic(&iter, &leader);
256 !dbus_message_iter_next(&iter) ||
257 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
260 dbus_message_iter_get_basic(&iter, &service);
262 if (!dbus_message_iter_next(&iter) ||
263 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
266 dbus_message_iter_get_basic(&iter, &type);
267 t = session_type_from_string(type);
270 !dbus_message_iter_next(&iter) ||
271 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
274 dbus_message_iter_get_basic(&iter, &seat);
279 s = hashmap_get(m->seats, seat);
284 if (!dbus_message_iter_next(&iter) ||
285 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
288 dbus_message_iter_get_basic(&iter, &vtnr);
290 if (!dbus_message_iter_next(&iter) ||
291 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
294 dbus_message_iter_get_basic(&iter, &tty);
296 if (tty_is_vc(tty)) {
301 else if (s != m->vtconsole)
304 v = vtnr_from_tty(tty);
307 return v < 0 ? v : -EINVAL;
311 else if (vtnr != (uint32_t) v)
314 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
318 if (seat_can_multi_session(s)) {
319 if (vtnr <= 0 || vtnr > 63)
327 if (!dbus_message_iter_next(&iter) ||
328 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
331 dbus_message_iter_get_basic(&iter, &display);
333 if (!dbus_message_iter_next(&iter) ||
334 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
337 dbus_message_iter_get_basic(&iter, &remote);
339 if (!dbus_message_iter_next(&iter) ||
340 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
343 dbus_message_iter_get_basic(&iter, &remote_user);
345 if (!dbus_message_iter_next(&iter) ||
346 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
349 dbus_message_iter_get_basic(&iter, &remote_host);
351 if (!dbus_message_iter_next(&iter) ||
352 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
353 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
356 r = bus_parse_strv_iter(&iter, &controllers);
360 if (strv_contains(controllers, "systemd") ||
361 !dbus_message_iter_next(&iter) ||
362 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
363 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
368 r = bus_parse_strv_iter(&iter, &reset_controllers);
372 if (strv_contains(reset_controllers, "systemd") ||
373 !dbus_message_iter_next(&iter) ||
374 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
379 dbus_message_iter_get_basic(&iter, &kill_processes);
381 r = manager_add_user_by_uid(m, uid, &user);
385 audit_session_from_pid(leader, &audit_id);
388 asprintf(&id, "%lu", (unsigned long) audit_id);
395 session = hashmap_get(m->sessions, id);
400 fifo_fd = session_create_fifo(session);
406 /* Session already exists, client is probably
407 * something like "su" which changes uid but
408 * is still the same audit session */
410 reply = dbus_message_new_method_return(message);
416 p = session_bus_path(session);
422 seat = session->seat ? session->seat->id : "";
423 vtnr = session->vtnr;
424 b = dbus_message_append_args(
426 DBUS_TYPE_STRING, &session->id,
427 DBUS_TYPE_OBJECT_PATH, &p,
428 DBUS_TYPE_STRING, &session->user->runtime_path,
429 DBUS_TYPE_UNIX_FD, &fifo_fd,
430 DBUS_TYPE_STRING, &seat,
431 DBUS_TYPE_UINT32, &vtnr,
440 close_nointr_nofail(fifo_fd);
443 strv_free(controllers);
444 strv_free(reset_controllers);
452 asprintf(&id, "c%lu", ++m->session_counter);
459 } while (hashmap_get(m->sessions, id));
462 r = manager_add_session(m, user, id, &session);
467 session->leader = leader;
468 session->audit_id = audit_id;
470 session->remote = remote;
471 session->controllers = controllers;
472 session->reset_controllers = reset_controllers;
473 session->kill_processes = kill_processes;
474 session->vtnr = vtnr;
476 controllers = reset_controllers = NULL;
479 session->tty = strdup(tty);
486 if (!isempty(display)) {
487 session->display = strdup(display);
488 if (!session->display) {
494 if (!isempty(remote_user)) {
495 session->remote_user = strdup(remote_user);
496 if (!session->remote_user) {
502 if (!isempty(remote_host)) {
503 session->remote_host = strdup(remote_host);
504 if (!session->remote_host) {
510 if (!isempty(service)) {
511 session->service = strdup(service);
512 if (!session->service) {
518 fifo_fd = session_create_fifo(session);
525 r = seat_attach_session(s, session);
530 r = session_start(session);
534 reply = dbus_message_new_method_return(message);
540 p = session_bus_path(session);
546 seat = s ? s->id : "";
547 b = dbus_message_append_args(
549 DBUS_TYPE_STRING, &session->id,
550 DBUS_TYPE_OBJECT_PATH, &p,
551 DBUS_TYPE_STRING, &session->user->runtime_path,
552 DBUS_TYPE_UNIX_FD, &fifo_fd,
553 DBUS_TYPE_STRING, &seat,
554 DBUS_TYPE_UINT32, &vtnr,
563 close_nointr_nofail(fifo_fd);
569 strv_free(controllers);
570 strv_free(reset_controllers);
573 session_add_to_gc_queue(session);
576 user_add_to_gc_queue(user);
579 close_nointr_nofail(fifo_fd);
582 dbus_message_unref(reply);
587 static int trigger_device(Manager *m, struct udev_device *d) {
588 struct udev_enumerate *e;
589 struct udev_list_entry *first, *item;
594 e = udev_enumerate_new(m->udev);
601 if (udev_enumerate_add_match_parent(e, d) < 0) {
607 if (udev_enumerate_scan_devices(e) < 0) {
612 first = udev_enumerate_get_list_entry(e);
613 udev_list_entry_foreach(item, first) {
617 p = udev_list_entry_get_name(item);
619 t = strappend(p, "/uevent");
625 write_one_line_file(t, "change");
633 udev_enumerate_unref(e);
638 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
639 struct udev_device *d;
640 char *rule = NULL, *file = NULL;
641 const char *id_for_seat;
648 d = udev_device_new_from_syspath(m->udev, sysfs);
652 if (!udev_device_has_tag(d, "seat")) {
657 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
663 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
668 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
673 mkdir_p("/etc/udev/rules.d", 0755);
674 r = write_one_line_file_atomic(file, rule);
678 r = trigger_device(m, d);
685 udev_device_unref(d);
690 static int flush_devices(Manager *m) {
695 d = opendir("/etc/udev/rules.d");
698 log_warning("Failed to open /etc/udev/rules.d: %m");
702 while ((de = readdir(d))) {
704 if (!dirent_is_file(de))
707 if (!startswith(de->d_name, "72-seat-"))
710 if (!endswith(de->d_name, ".rules"))
713 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
714 log_warning("Failed to unlink %s: %m", de->d_name);
720 return trigger_device(m, NULL);
723 static int have_multiple_sessions(
724 DBusConnection *connection,
726 DBusMessage *message,
733 if (hashmap_size(m->sessions) > 1)
736 /* Hmm, there's only one session, but let's make sure it
737 * actually belongs to the user who is asking. If not, better
738 * be safe than sorry. */
740 s = hashmap_first(m->sessions);
744 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
745 if (ul == (unsigned long) -1)
748 return s->user->uid != ul;
754 static const BusProperty bus_login_manager_properties[] = {
755 { "ControlGroupHierarchy", bus_property_append_string, "s", offsetof(Manager, cgroup_path), true },
756 { "Controllers", bus_property_append_strv, "as", offsetof(Manager, controllers), true },
757 { "ResetControllers", bus_property_append_strv, "as", offsetof(Manager, reset_controllers), true },
758 { "NAutoVTs", bus_property_append_unsigned, "u", offsetof(Manager, n_autovts) },
759 { "KillOnlyUsers", bus_property_append_strv, "as", offsetof(Manager, kill_only_users), true },
760 { "KillExcludeUsers", bus_property_append_strv, "as", offsetof(Manager, kill_exclude_users), true },
761 { "KillUserProcesses", bus_property_append_bool, "b", offsetof(Manager, kill_user_processes) },
762 { "IdleHint", bus_manager_append_idle_hint, "b", 0 },
763 { "IdleSinceHint", bus_manager_append_idle_hint_since, "t", 0 },
764 { "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", 0 },
768 static DBusHandlerResult manager_message_handler(
769 DBusConnection *connection,
770 DBusMessage *message,
773 Manager *m = userdata;
776 DBusMessage *reply = NULL;
783 dbus_error_init(&error);
785 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
791 if (!dbus_message_get_args(
794 DBUS_TYPE_STRING, &name,
796 return bus_send_error_reply(connection, message, &error, -EINVAL);
798 session = hashmap_get(m->sessions, name);
800 return bus_send_error_reply(connection, message, &error, -ENOENT);
802 reply = dbus_message_new_method_return(message);
806 p = session_bus_path(session);
810 b = dbus_message_append_args(
812 DBUS_TYPE_OBJECT_PATH, &p,
819 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSessionByPID")) {
825 if (!dbus_message_get_args(
828 DBUS_TYPE_UINT32, &pid,
830 return bus_send_error_reply(connection, message, &error, -EINVAL);
832 r = manager_get_session_by_pid(m, pid, &session);
834 return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
836 reply = dbus_message_new_method_return(message);
840 p = session_bus_path(session);
844 b = dbus_message_append_args(
846 DBUS_TYPE_OBJECT_PATH, &p,
853 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
859 if (!dbus_message_get_args(
862 DBUS_TYPE_UINT32, &uid,
864 return bus_send_error_reply(connection, message, &error, -EINVAL);
866 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
868 return bus_send_error_reply(connection, message, &error, -ENOENT);
870 reply = dbus_message_new_method_return(message);
874 p = user_bus_path(user);
878 b = dbus_message_append_args(
880 DBUS_TYPE_OBJECT_PATH, &p,
887 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
893 if (!dbus_message_get_args(
896 DBUS_TYPE_STRING, &name,
898 return bus_send_error_reply(connection, message, &error, -EINVAL);
900 seat = hashmap_get(m->seats, name);
902 return bus_send_error_reply(connection, message, &error, -ENOENT);
904 reply = dbus_message_new_method_return(message);
908 p = seat_bus_path(seat);
912 b = dbus_message_append_args(
914 DBUS_TYPE_OBJECT_PATH, &p,
921 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
925 DBusMessageIter iter, sub;
926 const char *empty = "";
928 reply = dbus_message_new_method_return(message);
932 dbus_message_iter_init_append(reply, &iter);
934 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
937 HASHMAP_FOREACH(session, m->sessions, i) {
938 DBusMessageIter sub2;
941 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
944 uid = session->user->uid;
946 p = session_bus_path(session);
950 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
951 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
952 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
953 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
954 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
961 if (!dbus_message_iter_close_container(&sub, &sub2))
965 if (!dbus_message_iter_close_container(&iter, &sub))
968 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
972 DBusMessageIter iter, sub;
974 reply = dbus_message_new_method_return(message);
978 dbus_message_iter_init_append(reply, &iter);
980 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
983 HASHMAP_FOREACH(user, m->users, i) {
984 DBusMessageIter sub2;
987 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
992 p = user_bus_path(user);
996 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
997 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
998 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
1005 if (!dbus_message_iter_close_container(&sub, &sub2))
1009 if (!dbus_message_iter_close_container(&iter, &sub))
1012 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
1016 DBusMessageIter iter, sub;
1018 reply = dbus_message_new_method_return(message);
1022 dbus_message_iter_init_append(reply, &iter);
1024 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
1027 HASHMAP_FOREACH(seat, m->seats, i) {
1028 DBusMessageIter sub2;
1030 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
1033 p = seat_bus_path(seat);
1037 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
1038 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
1045 if (!dbus_message_iter_close_container(&sub, &sub2))
1049 if (!dbus_message_iter_close_container(&iter, &sub))
1052 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
1054 r = bus_manager_create_session(m, message, &reply);
1056 /* Don't delay the work on OOM here, since it might be
1057 * triggered by a low RLIMIT_NOFILE here (since we
1058 * send a dupped fd to the client), and we'd rather
1059 * see this fail quickly then be retried later */
1062 return bus_send_error_reply(connection, message, &error, r);
1064 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
1068 if (!dbus_message_get_args(
1071 DBUS_TYPE_STRING, &name,
1073 return bus_send_error_reply(connection, message, &error, -EINVAL);
1075 session = hashmap_get(m->sessions, name);
1077 return bus_send_error_reply(connection, message, &error, -ENOENT);
1079 r = session_activate(session);
1081 return bus_send_error_reply(connection, message, NULL, r);
1083 reply = dbus_message_new_method_return(message);
1087 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSessionOnSeat")) {
1088 const char *session_name, *seat_name;
1092 /* Same as ActivateSession() but refuses to work if
1093 * the seat doesn't match */
1095 if (!dbus_message_get_args(
1098 DBUS_TYPE_STRING, &session_name,
1099 DBUS_TYPE_STRING, &seat_name,
1101 return bus_send_error_reply(connection, message, &error, -EINVAL);
1103 session = hashmap_get(m->sessions, session_name);
1105 return bus_send_error_reply(connection, message, &error, -ENOENT);
1107 seat = hashmap_get(m->seats, seat_name);
1109 return bus_send_error_reply(connection, message, &error, -ENOENT);
1111 if (session->seat != seat)
1112 return bus_send_error_reply(connection, message, &error, -EINVAL);
1114 r = session_activate(session);
1116 return bus_send_error_reply(connection, message, NULL, r);
1118 reply = dbus_message_new_method_return(message);
1122 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
1123 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
1127 if (!dbus_message_get_args(
1130 DBUS_TYPE_STRING, &name,
1132 return bus_send_error_reply(connection, message, &error, -EINVAL);
1134 session = hashmap_get(m->sessions, name);
1136 return bus_send_error_reply(connection, message, &error, -ENOENT);
1138 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1141 reply = dbus_message_new_method_return(message);
1145 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1152 if (!dbus_message_get_args(
1155 DBUS_TYPE_STRING, &name,
1156 DBUS_TYPE_STRING, &swho,
1157 DBUS_TYPE_INT32, &signo,
1159 return bus_send_error_reply(connection, message, &error, -EINVAL);
1164 who = kill_who_from_string(swho);
1166 return bus_send_error_reply(connection, message, &error, -EINVAL);
1169 if (signo <= 0 || signo >= _NSIG)
1170 return bus_send_error_reply(connection, message, &error, -EINVAL);
1172 session = hashmap_get(m->sessions, name);
1174 return bus_send_error_reply(connection, message, &error, -ENOENT);
1176 r = session_kill(session, who, signo);
1178 return bus_send_error_reply(connection, message, NULL, r);
1180 reply = dbus_message_new_method_return(message);
1184 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1189 if (!dbus_message_get_args(
1192 DBUS_TYPE_UINT32, &uid,
1193 DBUS_TYPE_INT32, &signo,
1195 return bus_send_error_reply(connection, message, &error, -EINVAL);
1197 if (signo <= 0 || signo >= _NSIG)
1198 return bus_send_error_reply(connection, message, &error, -EINVAL);
1200 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1202 return bus_send_error_reply(connection, message, &error, -ENOENT);
1204 r = user_kill(user, signo);
1206 return bus_send_error_reply(connection, message, NULL, r);
1208 reply = dbus_message_new_method_return(message);
1212 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1216 if (!dbus_message_get_args(
1219 DBUS_TYPE_STRING, &name,
1221 return bus_send_error_reply(connection, message, &error, -EINVAL);
1223 session = hashmap_get(m->sessions, name);
1225 return bus_send_error_reply(connection, message, &error, -ENOENT);
1227 r = session_stop(session);
1229 return bus_send_error_reply(connection, message, NULL, r);
1231 reply = dbus_message_new_method_return(message);
1235 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1239 if (!dbus_message_get_args(
1242 DBUS_TYPE_UINT32, &uid,
1244 return bus_send_error_reply(connection, message, &error, -EINVAL);
1246 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1248 return bus_send_error_reply(connection, message, &error, -ENOENT);
1250 r = user_stop(user);
1252 return bus_send_error_reply(connection, message, NULL, r);
1254 reply = dbus_message_new_method_return(message);
1258 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1262 if (!dbus_message_get_args(
1265 DBUS_TYPE_STRING, &name,
1267 return bus_send_error_reply(connection, message, &error, -EINVAL);
1269 seat = hashmap_get(m->seats, name);
1271 return bus_send_error_reply(connection, message, &error, -ENOENT);
1273 r = seat_stop_sessions(seat);
1275 return bus_send_error_reply(connection, message, NULL, r);
1277 reply = dbus_message_new_method_return(message);
1281 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1284 dbus_bool_t b, interactive;
1287 if (!dbus_message_get_args(
1290 DBUS_TYPE_UINT32, &uid,
1291 DBUS_TYPE_BOOLEAN, &b,
1292 DBUS_TYPE_BOOLEAN, &interactive,
1294 return bus_send_error_reply(connection, message, &error, -EINVAL);
1299 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1301 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, NULL, &error);
1303 return bus_send_error_reply(connection, message, &error, r);
1305 mkdir_p("/var/lib/systemd", 0755);
1307 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1309 return bus_send_error_reply(connection, message, &error, r);
1311 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1322 return bus_send_error_reply(connection, message, &error, r);
1324 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1333 if (r < 0 && errno != ENOENT)
1334 return bus_send_error_reply(connection, message, &error, -errno);
1336 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1338 user_add_to_gc_queue(u);
1341 reply = dbus_message_new_method_return(message);
1345 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1346 const char *sysfs, *seat;
1347 dbus_bool_t interactive;
1349 if (!dbus_message_get_args(
1352 DBUS_TYPE_STRING, &seat,
1353 DBUS_TYPE_STRING, &sysfs,
1354 DBUS_TYPE_BOOLEAN, &interactive,
1356 return bus_send_error_reply(connection, message, &error, -EINVAL);
1358 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1359 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1361 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, NULL, &error);
1363 return bus_send_error_reply(connection, message, &error, r);
1365 r = attach_device(m, seat, sysfs);
1367 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1369 reply = dbus_message_new_method_return(message);
1374 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1375 dbus_bool_t interactive;
1377 if (!dbus_message_get_args(
1380 DBUS_TYPE_BOOLEAN, &interactive,
1382 return bus_send_error_reply(connection, message, &error, -EINVAL);
1384 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, NULL, &error);
1386 return bus_send_error_reply(connection, message, &error, r);
1388 r = flush_devices(m);
1390 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1392 reply = dbus_message_new_method_return(message);
1396 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "PowerOff") ||
1397 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Reboot")) {
1398 dbus_bool_t interactive;
1399 bool multiple_sessions;
1400 DBusMessage *forward, *freply;
1402 const char *mode = "replace";
1405 if (!dbus_message_get_args(
1408 DBUS_TYPE_BOOLEAN, &interactive,
1410 return bus_send_error_reply(connection, message, &error, -EINVAL);
1412 r = have_multiple_sessions(connection, m, message, &error);
1414 return bus_send_error_reply(connection, message, &error, r);
1416 multiple_sessions = r > 0;
1418 if (streq(dbus_message_get_member(message), "PowerOff")) {
1419 if (multiple_sessions)
1420 action = "org.freedesktop.login1.power-off-multiple-sessions";
1422 action = "org.freedesktop.login1.power-off";
1424 name = SPECIAL_POWEROFF_TARGET;
1426 if (multiple_sessions)
1427 action = "org.freedesktop.login1.reboot-multiple-sessions";
1429 action = "org.freedesktop.login1.reboot";
1431 name = SPECIAL_REBOOT_TARGET;
1434 r = verify_polkit(connection, message, action, interactive, NULL, &error);
1436 return bus_send_error_reply(connection, message, &error, r);
1438 forward = dbus_message_new_method_call(
1439 "org.freedesktop.systemd1",
1440 "/org/freedesktop/systemd1",
1441 "org.freedesktop.systemd1.Manager",
1444 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1446 if (!dbus_message_append_args(forward,
1447 DBUS_TYPE_STRING, &name,
1448 DBUS_TYPE_STRING, &mode,
1449 DBUS_TYPE_INVALID)) {
1450 dbus_message_unref(forward);
1451 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1454 freply = dbus_connection_send_with_reply_and_block(connection, forward, -1, &error);
1455 dbus_message_unref(forward);
1458 return bus_send_error_reply(connection, message, &error, -EIO);
1460 dbus_message_unref(freply);
1462 reply = dbus_message_new_method_return(message);
1466 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CanPowerOff") ||
1467 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CanReboot")) {
1469 bool multiple_sessions, challenge, b;
1470 const char *t, *action;
1472 r = have_multiple_sessions(connection, m, message, &error);
1474 return bus_send_error_reply(connection, message, &error, r);
1476 multiple_sessions = r > 0;
1478 if (streq(dbus_message_get_member(message), "CanPowerOff")) {
1479 if (multiple_sessions)
1480 action = "org.freedesktop.login1.power-off-multiple-sessions";
1482 action = "org.freedesktop.login1.power-off";
1485 if (multiple_sessions)
1486 action = "org.freedesktop.login1.reboot-multiple-sessions";
1488 action = "org.freedesktop.login1.reboot";
1491 r = verify_polkit(connection, message, action, false, &challenge, &error);
1493 return bus_send_error_reply(connection, message, &error, r);
1495 reply = dbus_message_new_method_return(message);
1500 challenge ? "challenge" :
1503 b = dbus_message_append_args(
1505 DBUS_TYPE_STRING, &t,
1510 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1511 char *introspection = NULL;
1520 if (!(reply = dbus_message_new_method_return(message)))
1523 /* We roll our own introspection code here, instead of
1524 * relying on bus_default_message_handler() because we
1525 * need to generate our introspection string
1528 if (!(f = open_memstream(&introspection, &size)))
1531 fputs(INTROSPECTION_BEGIN, f);
1533 HASHMAP_FOREACH(seat, m->seats, i) {
1534 p = bus_path_escape(seat->id);
1537 fprintf(f, "<node name=\"seat/%s\"/>", p);
1542 HASHMAP_FOREACH(user, m->users, i)
1543 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1545 HASHMAP_FOREACH(session, m->sessions, i) {
1546 p = bus_path_escape(session->id);
1549 fprintf(f, "<node name=\"session/%s\"/>", p);
1554 fputs(INTROSPECTION_END, f);
1558 free(introspection);
1567 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1568 free(introspection);
1572 free(introspection);
1574 const BusBoundProperties bps[] = {
1575 { "org.freedesktop.login1.Manager", bus_login_manager_properties, m },
1578 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, bps);
1582 if (!dbus_connection_send(connection, reply, NULL))
1585 dbus_message_unref(reply);
1588 return DBUS_HANDLER_RESULT_HANDLED;
1592 dbus_message_unref(reply);
1594 dbus_error_free(&error);
1596 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1599 const DBusObjectPathVTable bus_manager_vtable = {
1600 .message_function = manager_message_handler
1603 DBusHandlerResult bus_message_filter(
1604 DBusConnection *connection,
1605 DBusMessage *message,
1608 Manager *m = userdata;
1615 dbus_error_init(&error);
1617 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1620 if (!dbus_message_get_args(message, &error,
1621 DBUS_TYPE_STRING, &cgroup,
1623 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1625 manager_cgroup_notify_empty(m, cgroup);
1628 dbus_error_free(&error);
1630 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1633 int manager_send_changed(Manager *manager, const char *properties) {
1639 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1643 if (!dbus_connection_send(manager->bus, m, NULL))
1650 dbus_message_unref(m);