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=\"class\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n" \
68 " <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
69 " <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
70 " <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
71 " <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
72 " <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
73 " <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
74 " <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
75 " <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
76 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
77 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
78 " <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
79 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
80 " <arg name=\"seat\" type=\"s\" direction=\"out\"/>\n" \
81 " <arg name=\"vtnr\" type=\"u\" direction=\"out\"/>\n" \
83 " <method name=\"ActivateSession\">\n" \
84 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
86 " <method name=\"ActivateSessionOnSeat\">\n" \
87 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
88 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
90 " <method name=\"LockSession\">\n" \
91 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
93 " <method name=\"UnlockSession\">\n" \
94 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
96 " <method name=\"KillSession\">\n" \
97 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
98 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
99 " <arg name=\"signal\" type=\"s\" direction=\"in\"/>\n" \
101 " <method name=\"KillUser\">\n" \
102 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
103 " <arg name=\"signal\" type=\"s\" direction=\"in\"/>\n" \
105 " <method name=\"TerminateSession\">\n" \
106 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
108 " <method name=\"TerminateUser\">\n" \
109 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
111 " <method name=\"TerminateSeat\">\n" \
112 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
114 " <method name=\"SetUserLinger\">\n" \
115 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
116 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
117 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
119 " <method name=\"AttachDevice\">\n" \
120 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
121 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
122 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
124 " <method name=\"FlushDevices\">\n" \
125 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
127 " <method name=\"PowerOff\">\n" \
128 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
130 " <method name=\"Reboot\">\n" \
131 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
133 " <method name=\"CanPowerOff\">\n" \
134 " <arg name=\"result\" type=\"s\" direction=\"out\"/>\n" \
136 " <method name=\"CanReboot\">\n" \
137 " <arg name=\"result\" type=\"s\" direction=\"out\"/>\n" \
139 " <signal name=\"SessionNew\">\n" \
140 " <arg name=\"id\" type=\"s\"/>\n" \
141 " <arg name=\"path\" type=\"o\"/>\n" \
143 " <signal name=\"SessionRemoved\">\n" \
144 " <arg name=\"id\" type=\"s\"/>\n" \
145 " <arg name=\"path\" type=\"o\"/>\n" \
147 " <signal name=\"UserNew\">\n" \
148 " <arg name=\"uid\" type=\"u\"/>\n" \
149 " <arg name=\"path\" type=\"o\"/>\n" \
151 " <signal name=\"UserRemoved\">\n" \
152 " <arg name=\"uid\" type=\"u\"/>\n" \
153 " <arg name=\"path\" type=\"o\"/>\n" \
155 " <signal name=\"SeatNew\">\n" \
156 " <arg name=\"id\" type=\"s\"/>\n" \
157 " <arg name=\"path\" type=\"o\"/>\n" \
159 " <signal name=\"SeatRemoved\">\n" \
160 " <arg name=\"id\" type=\"s\"/>\n" \
161 " <arg name=\"path\" type=\"o\"/>\n" \
163 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
164 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
165 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
166 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
167 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
168 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
169 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
170 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
171 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
172 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
175 #define INTROSPECTION_BEGIN \
176 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
178 BUS_MANAGER_INTERFACE \
179 BUS_PROPERTIES_INTERFACE \
181 BUS_INTROSPECTABLE_INTERFACE
183 #define INTROSPECTION_END \
186 #define INTERFACES_LIST \
187 BUS_GENERIC_INTERFACES_LIST \
188 "org.freedesktop.login1.Manager\0"
190 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
198 b = manager_get_idle_hint(m, NULL) > 0;
199 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
205 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
214 manager_get_idle_hint(m, &t);
215 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
217 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
223 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
224 Session *session = NULL;
226 const char *type, *class, *seat, *tty, *display, *remote_user, *remote_host, *service;
227 uint32_t uid, leader, audit_id = 0;
228 dbus_bool_t remote, kill_processes;
229 char **controllers = NULL, **reset_controllers = NULL;
233 DBusMessageIter iter;
238 DBusMessage *reply = NULL;
245 if (!dbus_message_iter_init(message, &iter) ||
246 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
249 dbus_message_iter_get_basic(&iter, &uid);
251 if (!dbus_message_iter_next(&iter) ||
252 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
255 dbus_message_iter_get_basic(&iter, &leader);
258 !dbus_message_iter_next(&iter) ||
259 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
262 dbus_message_iter_get_basic(&iter, &service);
264 if (!dbus_message_iter_next(&iter) ||
265 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
268 dbus_message_iter_get_basic(&iter, &type);
269 t = session_type_from_string(type);
272 !dbus_message_iter_next(&iter) ||
273 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
276 dbus_message_iter_get_basic(&iter, &class);
280 c = session_class_from_string(class);
283 !dbus_message_iter_next(&iter) ||
284 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
287 dbus_message_iter_get_basic(&iter, &seat);
292 s = hashmap_get(m->seats, seat);
297 if (!dbus_message_iter_next(&iter) ||
298 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
301 dbus_message_iter_get_basic(&iter, &vtnr);
303 if (!dbus_message_iter_next(&iter) ||
304 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
307 dbus_message_iter_get_basic(&iter, &tty);
309 if (tty_is_vc(tty)) {
314 else if (s != m->vtconsole)
317 v = vtnr_from_tty(tty);
320 return v < 0 ? v : -EINVAL;
324 else if (vtnr != (uint32_t) v)
327 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
331 if (seat_can_multi_session(s)) {
332 if (vtnr <= 0 || vtnr > 63)
340 if (!dbus_message_iter_next(&iter) ||
341 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
344 dbus_message_iter_get_basic(&iter, &display);
346 if (!dbus_message_iter_next(&iter) ||
347 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
350 dbus_message_iter_get_basic(&iter, &remote);
352 if (!dbus_message_iter_next(&iter) ||
353 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
356 dbus_message_iter_get_basic(&iter, &remote_user);
358 if (!dbus_message_iter_next(&iter) ||
359 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
362 dbus_message_iter_get_basic(&iter, &remote_host);
364 if (!dbus_message_iter_next(&iter) ||
365 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
366 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
369 r = bus_parse_strv_iter(&iter, &controllers);
373 if (strv_contains(controllers, "systemd") ||
374 !dbus_message_iter_next(&iter) ||
375 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
376 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
381 r = bus_parse_strv_iter(&iter, &reset_controllers);
385 if (strv_contains(reset_controllers, "systemd") ||
386 !dbus_message_iter_next(&iter) ||
387 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
392 dbus_message_iter_get_basic(&iter, &kill_processes);
394 r = manager_add_user_by_uid(m, uid, &user);
398 audit_session_from_pid(leader, &audit_id);
401 asprintf(&id, "%lu", (unsigned long) audit_id);
408 session = hashmap_get(m->sessions, id);
413 fifo_fd = session_create_fifo(session);
419 /* Session already exists, client is probably
420 * something like "su" which changes uid but
421 * is still the same audit session */
423 reply = dbus_message_new_method_return(message);
429 p = session_bus_path(session);
435 seat = session->seat ? session->seat->id : "";
436 vtnr = session->vtnr;
437 b = dbus_message_append_args(
439 DBUS_TYPE_STRING, &session->id,
440 DBUS_TYPE_OBJECT_PATH, &p,
441 DBUS_TYPE_STRING, &session->user->runtime_path,
442 DBUS_TYPE_UNIX_FD, &fifo_fd,
443 DBUS_TYPE_STRING, &seat,
444 DBUS_TYPE_UINT32, &vtnr,
453 close_nointr_nofail(fifo_fd);
456 strv_free(controllers);
457 strv_free(reset_controllers);
465 asprintf(&id, "c%lu", ++m->session_counter);
472 } while (hashmap_get(m->sessions, id));
475 r = manager_add_session(m, user, id, &session);
480 session->leader = leader;
481 session->audit_id = audit_id;
484 session->remote = remote;
485 session->controllers = controllers;
486 session->reset_controllers = reset_controllers;
487 session->kill_processes = kill_processes;
488 session->vtnr = vtnr;
490 controllers = reset_controllers = NULL;
493 session->tty = strdup(tty);
500 if (!isempty(display)) {
501 session->display = strdup(display);
502 if (!session->display) {
508 if (!isempty(remote_user)) {
509 session->remote_user = strdup(remote_user);
510 if (!session->remote_user) {
516 if (!isempty(remote_host)) {
517 session->remote_host = strdup(remote_host);
518 if (!session->remote_host) {
524 if (!isempty(service)) {
525 session->service = strdup(service);
526 if (!session->service) {
532 fifo_fd = session_create_fifo(session);
539 r = seat_attach_session(s, session);
544 r = session_start(session);
548 reply = dbus_message_new_method_return(message);
554 p = session_bus_path(session);
560 seat = s ? s->id : "";
561 b = dbus_message_append_args(
563 DBUS_TYPE_STRING, &session->id,
564 DBUS_TYPE_OBJECT_PATH, &p,
565 DBUS_TYPE_STRING, &session->user->runtime_path,
566 DBUS_TYPE_UNIX_FD, &fifo_fd,
567 DBUS_TYPE_STRING, &seat,
568 DBUS_TYPE_UINT32, &vtnr,
577 close_nointr_nofail(fifo_fd);
583 strv_free(controllers);
584 strv_free(reset_controllers);
587 session_add_to_gc_queue(session);
590 user_add_to_gc_queue(user);
593 close_nointr_nofail(fifo_fd);
596 dbus_message_unref(reply);
601 static int trigger_device(Manager *m, struct udev_device *d) {
602 struct udev_enumerate *e;
603 struct udev_list_entry *first, *item;
608 e = udev_enumerate_new(m->udev);
615 if (udev_enumerate_add_match_parent(e, d) < 0) {
621 if (udev_enumerate_scan_devices(e) < 0) {
626 first = udev_enumerate_get_list_entry(e);
627 udev_list_entry_foreach(item, first) {
631 p = udev_list_entry_get_name(item);
633 t = strappend(p, "/uevent");
639 write_one_line_file(t, "change");
647 udev_enumerate_unref(e);
652 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
653 struct udev_device *d;
654 char *rule = NULL, *file = NULL;
655 const char *id_for_seat;
662 d = udev_device_new_from_syspath(m->udev, sysfs);
666 if (!udev_device_has_tag(d, "seat")) {
671 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
677 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
682 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
687 mkdir_p("/etc/udev/rules.d", 0755);
688 r = write_one_line_file_atomic(file, rule);
692 r = trigger_device(m, d);
699 udev_device_unref(d);
704 static int flush_devices(Manager *m) {
709 d = opendir("/etc/udev/rules.d");
712 log_warning("Failed to open /etc/udev/rules.d: %m");
716 while ((de = readdir(d))) {
718 if (!dirent_is_file(de))
721 if (!startswith(de->d_name, "72-seat-"))
724 if (!endswith(de->d_name, ".rules"))
727 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
728 log_warning("Failed to unlink %s: %m", de->d_name);
734 return trigger_device(m, NULL);
737 static int have_multiple_sessions(
738 DBusConnection *connection,
740 DBusMessage *message,
747 if (hashmap_size(m->sessions) > 1)
750 /* Hmm, there's only one session, but let's make sure it
751 * actually belongs to the user who is asking. If not, better
752 * be safe than sorry. */
754 s = hashmap_first(m->sessions);
758 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), error);
759 if (ul == (unsigned long) -1)
762 return s->user->uid != ul;
768 static const BusProperty bus_login_manager_properties[] = {
769 { "ControlGroupHierarchy", bus_property_append_string, "s", offsetof(Manager, cgroup_path), true },
770 { "Controllers", bus_property_append_strv, "as", offsetof(Manager, controllers), true },
771 { "ResetControllers", bus_property_append_strv, "as", offsetof(Manager, reset_controllers), true },
772 { "NAutoVTs", bus_property_append_unsigned, "u", offsetof(Manager, n_autovts) },
773 { "KillOnlyUsers", bus_property_append_strv, "as", offsetof(Manager, kill_only_users), true },
774 { "KillExcludeUsers", bus_property_append_strv, "as", offsetof(Manager, kill_exclude_users), true },
775 { "KillUserProcesses", bus_property_append_bool, "b", offsetof(Manager, kill_user_processes) },
776 { "IdleHint", bus_manager_append_idle_hint, "b", 0 },
777 { "IdleSinceHint", bus_manager_append_idle_hint_since, "t", 0 },
778 { "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", 0 },
782 static DBusHandlerResult manager_message_handler(
783 DBusConnection *connection,
784 DBusMessage *message,
787 Manager *m = userdata;
790 DBusMessage *reply = NULL;
797 dbus_error_init(&error);
799 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
805 if (!dbus_message_get_args(
808 DBUS_TYPE_STRING, &name,
810 return bus_send_error_reply(connection, message, &error, -EINVAL);
812 session = hashmap_get(m->sessions, name);
814 return bus_send_error_reply(connection, message, &error, -ENOENT);
816 reply = dbus_message_new_method_return(message);
820 p = session_bus_path(session);
824 b = dbus_message_append_args(
826 DBUS_TYPE_OBJECT_PATH, &p,
833 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSessionByPID")) {
839 if (!dbus_message_get_args(
842 DBUS_TYPE_UINT32, &pid,
844 return bus_send_error_reply(connection, message, &error, -EINVAL);
846 r = manager_get_session_by_pid(m, pid, &session);
848 return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
850 reply = dbus_message_new_method_return(message);
854 p = session_bus_path(session);
858 b = dbus_message_append_args(
860 DBUS_TYPE_OBJECT_PATH, &p,
867 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
873 if (!dbus_message_get_args(
876 DBUS_TYPE_UINT32, &uid,
878 return bus_send_error_reply(connection, message, &error, -EINVAL);
880 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
882 return bus_send_error_reply(connection, message, &error, -ENOENT);
884 reply = dbus_message_new_method_return(message);
888 p = user_bus_path(user);
892 b = dbus_message_append_args(
894 DBUS_TYPE_OBJECT_PATH, &p,
901 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
907 if (!dbus_message_get_args(
910 DBUS_TYPE_STRING, &name,
912 return bus_send_error_reply(connection, message, &error, -EINVAL);
914 seat = hashmap_get(m->seats, name);
916 return bus_send_error_reply(connection, message, &error, -ENOENT);
918 reply = dbus_message_new_method_return(message);
922 p = seat_bus_path(seat);
926 b = dbus_message_append_args(
928 DBUS_TYPE_OBJECT_PATH, &p,
935 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
939 DBusMessageIter iter, sub;
940 const char *empty = "";
942 reply = dbus_message_new_method_return(message);
946 dbus_message_iter_init_append(reply, &iter);
948 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
951 HASHMAP_FOREACH(session, m->sessions, i) {
952 DBusMessageIter sub2;
955 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
958 uid = session->user->uid;
960 p = session_bus_path(session);
964 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
965 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
966 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
967 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
968 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
975 if (!dbus_message_iter_close_container(&sub, &sub2))
979 if (!dbus_message_iter_close_container(&iter, &sub))
982 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
986 DBusMessageIter iter, sub;
988 reply = dbus_message_new_method_return(message);
992 dbus_message_iter_init_append(reply, &iter);
994 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
997 HASHMAP_FOREACH(user, m->users, i) {
998 DBusMessageIter sub2;
1001 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
1006 p = user_bus_path(user);
1010 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
1011 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
1012 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
1019 if (!dbus_message_iter_close_container(&sub, &sub2))
1023 if (!dbus_message_iter_close_container(&iter, &sub))
1026 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
1030 DBusMessageIter iter, sub;
1032 reply = dbus_message_new_method_return(message);
1036 dbus_message_iter_init_append(reply, &iter);
1038 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
1041 HASHMAP_FOREACH(seat, m->seats, i) {
1042 DBusMessageIter sub2;
1044 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
1047 p = seat_bus_path(seat);
1051 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
1052 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
1059 if (!dbus_message_iter_close_container(&sub, &sub2))
1063 if (!dbus_message_iter_close_container(&iter, &sub))
1066 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
1068 r = bus_manager_create_session(m, message, &reply);
1070 /* Don't delay the work on OOM here, since it might be
1071 * triggered by a low RLIMIT_NOFILE here (since we
1072 * send a dupped fd to the client), and we'd rather
1073 * see this fail quickly then be retried later */
1076 return bus_send_error_reply(connection, message, &error, r);
1078 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
1082 if (!dbus_message_get_args(
1085 DBUS_TYPE_STRING, &name,
1087 return bus_send_error_reply(connection, message, &error, -EINVAL);
1089 session = hashmap_get(m->sessions, name);
1091 return bus_send_error_reply(connection, message, &error, -ENOENT);
1093 r = session_activate(session);
1095 return bus_send_error_reply(connection, message, NULL, r);
1097 reply = dbus_message_new_method_return(message);
1101 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSessionOnSeat")) {
1102 const char *session_name, *seat_name;
1106 /* Same as ActivateSession() but refuses to work if
1107 * the seat doesn't match */
1109 if (!dbus_message_get_args(
1112 DBUS_TYPE_STRING, &session_name,
1113 DBUS_TYPE_STRING, &seat_name,
1115 return bus_send_error_reply(connection, message, &error, -EINVAL);
1117 session = hashmap_get(m->sessions, session_name);
1119 return bus_send_error_reply(connection, message, &error, -ENOENT);
1121 seat = hashmap_get(m->seats, seat_name);
1123 return bus_send_error_reply(connection, message, &error, -ENOENT);
1125 if (session->seat != seat)
1126 return bus_send_error_reply(connection, message, &error, -EINVAL);
1128 r = session_activate(session);
1130 return bus_send_error_reply(connection, message, NULL, r);
1132 reply = dbus_message_new_method_return(message);
1136 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
1137 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
1141 if (!dbus_message_get_args(
1144 DBUS_TYPE_STRING, &name,
1146 return bus_send_error_reply(connection, message, &error, -EINVAL);
1148 session = hashmap_get(m->sessions, name);
1150 return bus_send_error_reply(connection, message, &error, -ENOENT);
1152 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1155 reply = dbus_message_new_method_return(message);
1159 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1166 if (!dbus_message_get_args(
1169 DBUS_TYPE_STRING, &name,
1170 DBUS_TYPE_STRING, &swho,
1171 DBUS_TYPE_INT32, &signo,
1173 return bus_send_error_reply(connection, message, &error, -EINVAL);
1178 who = kill_who_from_string(swho);
1180 return bus_send_error_reply(connection, message, &error, -EINVAL);
1183 if (signo <= 0 || signo >= _NSIG)
1184 return bus_send_error_reply(connection, message, &error, -EINVAL);
1186 session = hashmap_get(m->sessions, name);
1188 return bus_send_error_reply(connection, message, &error, -ENOENT);
1190 r = session_kill(session, who, signo);
1192 return bus_send_error_reply(connection, message, NULL, r);
1194 reply = dbus_message_new_method_return(message);
1198 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1203 if (!dbus_message_get_args(
1206 DBUS_TYPE_UINT32, &uid,
1207 DBUS_TYPE_INT32, &signo,
1209 return bus_send_error_reply(connection, message, &error, -EINVAL);
1211 if (signo <= 0 || signo >= _NSIG)
1212 return bus_send_error_reply(connection, message, &error, -EINVAL);
1214 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1216 return bus_send_error_reply(connection, message, &error, -ENOENT);
1218 r = user_kill(user, signo);
1220 return bus_send_error_reply(connection, message, NULL, r);
1222 reply = dbus_message_new_method_return(message);
1226 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1230 if (!dbus_message_get_args(
1233 DBUS_TYPE_STRING, &name,
1235 return bus_send_error_reply(connection, message, &error, -EINVAL);
1237 session = hashmap_get(m->sessions, name);
1239 return bus_send_error_reply(connection, message, &error, -ENOENT);
1241 r = session_stop(session);
1243 return bus_send_error_reply(connection, message, NULL, r);
1245 reply = dbus_message_new_method_return(message);
1249 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1253 if (!dbus_message_get_args(
1256 DBUS_TYPE_UINT32, &uid,
1258 return bus_send_error_reply(connection, message, &error, -EINVAL);
1260 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1262 return bus_send_error_reply(connection, message, &error, -ENOENT);
1264 r = user_stop(user);
1266 return bus_send_error_reply(connection, message, NULL, r);
1268 reply = dbus_message_new_method_return(message);
1272 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1276 if (!dbus_message_get_args(
1279 DBUS_TYPE_STRING, &name,
1281 return bus_send_error_reply(connection, message, &error, -EINVAL);
1283 seat = hashmap_get(m->seats, name);
1285 return bus_send_error_reply(connection, message, &error, -ENOENT);
1287 r = seat_stop_sessions(seat);
1289 return bus_send_error_reply(connection, message, NULL, r);
1291 reply = dbus_message_new_method_return(message);
1295 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1298 dbus_bool_t b, interactive;
1301 if (!dbus_message_get_args(
1304 DBUS_TYPE_UINT32, &uid,
1305 DBUS_TYPE_BOOLEAN, &b,
1306 DBUS_TYPE_BOOLEAN, &interactive,
1308 return bus_send_error_reply(connection, message, &error, -EINVAL);
1313 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1315 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, NULL, &error);
1317 return bus_send_error_reply(connection, message, &error, r);
1319 mkdir_p("/var/lib/systemd", 0755);
1321 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1323 return bus_send_error_reply(connection, message, &error, r);
1325 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1336 return bus_send_error_reply(connection, message, &error, r);
1338 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1347 if (r < 0 && errno != ENOENT)
1348 return bus_send_error_reply(connection, message, &error, -errno);
1350 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1352 user_add_to_gc_queue(u);
1355 reply = dbus_message_new_method_return(message);
1359 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1360 const char *sysfs, *seat;
1361 dbus_bool_t interactive;
1363 if (!dbus_message_get_args(
1366 DBUS_TYPE_STRING, &seat,
1367 DBUS_TYPE_STRING, &sysfs,
1368 DBUS_TYPE_BOOLEAN, &interactive,
1370 return bus_send_error_reply(connection, message, &error, -EINVAL);
1372 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1373 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1375 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, NULL, &error);
1377 return bus_send_error_reply(connection, message, &error, r);
1379 r = attach_device(m, seat, sysfs);
1381 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1383 reply = dbus_message_new_method_return(message);
1388 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1389 dbus_bool_t interactive;
1391 if (!dbus_message_get_args(
1394 DBUS_TYPE_BOOLEAN, &interactive,
1396 return bus_send_error_reply(connection, message, &error, -EINVAL);
1398 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, NULL, &error);
1400 return bus_send_error_reply(connection, message, &error, r);
1402 r = flush_devices(m);
1404 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1406 reply = dbus_message_new_method_return(message);
1410 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "PowerOff") ||
1411 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Reboot")) {
1412 dbus_bool_t interactive;
1413 bool multiple_sessions;
1414 DBusMessage *forward, *freply;
1416 const char *mode = "replace";
1419 if (!dbus_message_get_args(
1422 DBUS_TYPE_BOOLEAN, &interactive,
1424 return bus_send_error_reply(connection, message, &error, -EINVAL);
1426 r = have_multiple_sessions(connection, m, message, &error);
1428 return bus_send_error_reply(connection, message, &error, r);
1430 multiple_sessions = r > 0;
1432 if (streq(dbus_message_get_member(message), "PowerOff")) {
1433 if (multiple_sessions)
1434 action = "org.freedesktop.login1.power-off-multiple-sessions";
1436 action = "org.freedesktop.login1.power-off";
1438 name = SPECIAL_POWEROFF_TARGET;
1440 if (multiple_sessions)
1441 action = "org.freedesktop.login1.reboot-multiple-sessions";
1443 action = "org.freedesktop.login1.reboot";
1445 name = SPECIAL_REBOOT_TARGET;
1448 r = verify_polkit(connection, message, action, interactive, NULL, &error);
1450 return bus_send_error_reply(connection, message, &error, r);
1452 forward = dbus_message_new_method_call(
1453 "org.freedesktop.systemd1",
1454 "/org/freedesktop/systemd1",
1455 "org.freedesktop.systemd1.Manager",
1458 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1460 if (!dbus_message_append_args(forward,
1461 DBUS_TYPE_STRING, &name,
1462 DBUS_TYPE_STRING, &mode,
1463 DBUS_TYPE_INVALID)) {
1464 dbus_message_unref(forward);
1465 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1468 freply = dbus_connection_send_with_reply_and_block(connection, forward, -1, &error);
1469 dbus_message_unref(forward);
1472 return bus_send_error_reply(connection, message, &error, -EIO);
1474 dbus_message_unref(freply);
1476 reply = dbus_message_new_method_return(message);
1480 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CanPowerOff") ||
1481 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CanReboot")) {
1483 bool multiple_sessions, challenge, b;
1484 const char *t, *action;
1486 r = have_multiple_sessions(connection, m, message, &error);
1488 return bus_send_error_reply(connection, message, &error, r);
1490 multiple_sessions = r > 0;
1492 if (streq(dbus_message_get_member(message), "CanPowerOff")) {
1493 if (multiple_sessions)
1494 action = "org.freedesktop.login1.power-off-multiple-sessions";
1496 action = "org.freedesktop.login1.power-off";
1499 if (multiple_sessions)
1500 action = "org.freedesktop.login1.reboot-multiple-sessions";
1502 action = "org.freedesktop.login1.reboot";
1505 r = verify_polkit(connection, message, action, false, &challenge, &error);
1507 return bus_send_error_reply(connection, message, &error, r);
1509 reply = dbus_message_new_method_return(message);
1514 challenge ? "challenge" :
1517 b = dbus_message_append_args(
1519 DBUS_TYPE_STRING, &t,
1524 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1525 char *introspection = NULL;
1534 if (!(reply = dbus_message_new_method_return(message)))
1537 /* We roll our own introspection code here, instead of
1538 * relying on bus_default_message_handler() because we
1539 * need to generate our introspection string
1542 if (!(f = open_memstream(&introspection, &size)))
1545 fputs(INTROSPECTION_BEGIN, f);
1547 HASHMAP_FOREACH(seat, m->seats, i) {
1548 p = bus_path_escape(seat->id);
1551 fprintf(f, "<node name=\"seat/%s\"/>", p);
1556 HASHMAP_FOREACH(user, m->users, i)
1557 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1559 HASHMAP_FOREACH(session, m->sessions, i) {
1560 p = bus_path_escape(session->id);
1563 fprintf(f, "<node name=\"session/%s\"/>", p);
1568 fputs(INTROSPECTION_END, f);
1572 free(introspection);
1581 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1582 free(introspection);
1586 free(introspection);
1588 const BusBoundProperties bps[] = {
1589 { "org.freedesktop.login1.Manager", bus_login_manager_properties, m },
1592 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, bps);
1596 if (!dbus_connection_send(connection, reply, NULL))
1599 dbus_message_unref(reply);
1602 return DBUS_HANDLER_RESULT_HANDLED;
1606 dbus_message_unref(reply);
1608 dbus_error_free(&error);
1610 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1613 const DBusObjectPathVTable bus_manager_vtable = {
1614 .message_function = manager_message_handler
1617 DBusHandlerResult bus_message_filter(
1618 DBusConnection *connection,
1619 DBusMessage *message,
1622 Manager *m = userdata;
1629 dbus_error_init(&error);
1631 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1634 if (!dbus_message_get_args(message, &error,
1635 DBUS_TYPE_STRING, &cgroup,
1637 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1639 manager_cgroup_notify_empty(m, cgroup);
1642 dbus_error_free(&error);
1644 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1647 int manager_send_changed(Manager *manager, const char *properties) {
1653 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1657 if (!dbus_connection_send(manager->bus, m, NULL))
1664 dbus_message_unref(m);