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"
32 #define BUS_MANAGER_INTERFACE \
33 " <interface name=\"org.freedesktop.login1.Manager\">\n" \
34 " <method name=\"GetSession\">\n" \
35 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
36 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
38 " <method name=\"GetUser\">\n" \
39 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
40 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
42 " <method name=\"GetSeat\">\n" \
43 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
44 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
46 " <method name=\"ListSessions\">\n" \
47 " <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
49 " <method name=\"ListUsers\">\n" \
50 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
52 " <method name=\"ListSeats\">\n" \
53 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
55 " <method name=\"CreateSession\">\n" \
56 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
57 " <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
58 " <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n" \
59 " <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" \
60 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
61 " <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n" \
62 " <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
63 " <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
65 " <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
68 " <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
69 " <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
70 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
71 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
72 " <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
73 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
75 " <method name=\"ActivateSession\">\n" \
76 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
78 " <method name=\"TerminateSession\">\n" \
79 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
81 " <method name=\"TerminateUser\">\n" \
82 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
84 " <method name=\"TerminateSeat\">\n" \
85 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
87 " <method name=\"SetUserLinger\">\n" \
88 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
89 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
90 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
92 " <method name=\"AttachDevice\">\n" \
93 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
94 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
95 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
97 " <method name=\"FlushDevices\">\n" \
98 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
100 " <signal name=\"SessionNew\">\n" \
101 " <arg name=\"id\" type=\"s\"/>\n" \
102 " <arg name=\"path\" type=\"o\"/>\n" \
104 " <signal name=\"SessionRemoved\">\n" \
105 " <arg name=\"id\" type=\"s\"/>\n" \
106 " <arg name=\"path\" type=\"o\"/>\n" \
108 " <signal name=\"UserNew\">\n" \
109 " <arg name=\"uid\" type=\"u\"/>\n" \
110 " <arg name=\"path\" type=\"o\"/>\n" \
112 " <signal name=\"UserRemoved\">\n" \
113 " <arg name=\"uid\" type=\"u\"/>\n" \
114 " <arg name=\"path\" type=\"o\"/>\n" \
116 " <signal name=\"SeatNew\">\n" \
117 " <arg name=\"id\" type=\"s\"/>\n" \
118 " <arg name=\"path\" type=\"o\"/>\n" \
120 " <signal name=\"SeatRemoved\">\n" \
121 " <arg name=\"id\" type=\"s\"/>\n" \
122 " <arg name=\"path\" type=\"o\"/>\n" \
124 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
125 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
126 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
127 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
128 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
129 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
130 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
131 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
132 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
133 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
136 #define INTROSPECTION_BEGIN \
137 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
139 BUS_MANAGER_INTERFACE \
140 BUS_PROPERTIES_INTERFACE \
142 BUS_INTROSPECTABLE_INTERFACE
144 #define INTROSPECTION_END \
147 #define INTERFACES_LIST \
148 BUS_GENERIC_INTERFACES_LIST \
149 "org.freedesktop.login1.Manager\0"
151 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
159 b = manager_get_idle_hint(m, NULL) > 0;
160 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
166 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
175 manager_get_idle_hint(m, &t);
176 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
178 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
184 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
185 Session *session = NULL;
187 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
188 uint32_t uid, leader, audit_id = 0;
189 dbus_bool_t remote, kill_processes;
190 char **controllers = NULL, **reset_controllers = NULL;
193 DBusMessageIter iter;
198 DBusMessage *reply = NULL;
205 if (!dbus_message_iter_init(message, &iter) ||
206 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
209 dbus_message_iter_get_basic(&iter, &uid);
211 if (!dbus_message_iter_next(&iter) ||
212 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
215 dbus_message_iter_get_basic(&iter, &leader);
218 !dbus_message_iter_next(&iter) ||
219 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
222 dbus_message_iter_get_basic(&iter, &service);
224 if (!dbus_message_iter_next(&iter) ||
225 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
228 dbus_message_iter_get_basic(&iter, &type);
229 t = session_type_from_string(type);
232 !dbus_message_iter_next(&iter) ||
233 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
236 dbus_message_iter_get_basic(&iter, &seat);
241 s = hashmap_get(m->seats, seat);
246 if (!dbus_message_iter_next(&iter) ||
247 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
250 dbus_message_iter_get_basic(&iter, &vtnr);
252 if (!dbus_message_iter_next(&iter) ||
253 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
256 dbus_message_iter_get_basic(&iter, &tty);
258 if (tty_is_vc(tty)) {
263 else if (s != m->vtconsole)
266 v = vtnr_from_tty(tty);
269 return v < 0 ? v : -EINVAL;
273 else if (vtnr != (uint32_t) v)
276 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
280 if (seat_is_vtconsole(s)) {
281 if (vtnr <= 0 || vtnr > 63)
289 if (!dbus_message_iter_next(&iter) ||
290 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
293 dbus_message_iter_get_basic(&iter, &display);
295 if (!dbus_message_iter_next(&iter) ||
296 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
299 dbus_message_iter_get_basic(&iter, &remote);
301 if (!dbus_message_iter_next(&iter) ||
302 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
305 dbus_message_iter_get_basic(&iter, &remote_user);
307 if (!dbus_message_iter_next(&iter) ||
308 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
311 dbus_message_iter_get_basic(&iter, &remote_host);
313 if (!dbus_message_iter_next(&iter) ||
314 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
315 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
318 r = bus_parse_strv_iter(&iter, &controllers);
322 if (strv_contains(controllers, "systemd") ||
323 !dbus_message_iter_next(&iter) ||
324 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
325 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
330 r = bus_parse_strv_iter(&iter, &reset_controllers);
334 if (strv_contains(reset_controllers, "systemd") ||
335 !dbus_message_iter_next(&iter) ||
336 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
341 dbus_message_iter_get_basic(&iter, &kill_processes);
343 r = manager_add_user_by_uid(m, uid, &user);
347 audit_session_from_pid(leader, &audit_id);
350 asprintf(&id, "%lu", (unsigned long) audit_id);
357 session = hashmap_get(m->sessions, id);
361 fifo_fd = session_create_fifo(session);
367 /* Session already exists, client is probably
368 * something like "su" which changes uid but
369 * is still the same audit session */
371 reply = dbus_message_new_method_return(message);
377 p = session_bus_path(session);
383 b = dbus_message_append_args(
385 DBUS_TYPE_STRING, &session->id,
386 DBUS_TYPE_OBJECT_PATH, &p,
387 DBUS_TYPE_STRING, &session->user->runtime_path,
388 DBUS_TYPE_UNIX_FD, &fifo_fd,
397 close_nointr_nofail(fifo_fd);
406 asprintf(&id, "c%lu", ++m->session_counter);
413 } while (hashmap_get(m->sessions, id));
416 r = manager_add_session(m, user, id, &session);
421 session->leader = leader;
422 session->audit_id = audit_id;
424 session->remote = remote;
425 session->controllers = controllers;
426 session->reset_controllers = reset_controllers;
427 session->kill_processes = kill_processes;
428 session->vtnr = vtnr;
430 controllers = reset_controllers = NULL;
433 session->tty = strdup(tty);
440 if (!isempty(display)) {
441 session->display = strdup(display);
442 if (!session->display) {
448 if (!isempty(remote_user)) {
449 session->remote_user = strdup(remote_user);
450 if (!session->remote_user) {
456 if (!isempty(remote_host)) {
457 session->remote_host = strdup(remote_host);
458 if (!session->remote_host) {
464 if (!isempty(service)) {
465 session->service = strdup(service);
466 if (!session->service) {
472 fifo_fd = session_create_fifo(session);
479 r = seat_attach_session(s, session);
484 r = session_start(session);
488 reply = dbus_message_new_method_return(message);
494 p = session_bus_path(session);
500 b = dbus_message_append_args(
502 DBUS_TYPE_STRING, &session->id,
503 DBUS_TYPE_OBJECT_PATH, &p,
504 DBUS_TYPE_STRING, &session->user->runtime_path,
505 DBUS_TYPE_UNIX_FD, &fifo_fd,
514 close_nointr_nofail(fifo_fd);
520 strv_free(controllers);
521 strv_free(reset_controllers);
524 session_add_to_gc_queue(session);
527 user_add_to_gc_queue(user);
530 close_nointr_nofail(fifo_fd);
533 dbus_message_unref(reply);
538 static bool device_has_tag(struct udev_device *d, const char *tag) {
539 struct udev_list_entry *first, *item;
544 udev_device_get_is_initialized(d);
546 first = udev_device_get_tags_list_entry(d);
547 udev_list_entry_foreach(item, first)
548 if (streq(udev_list_entry_get_name(item), tag))
554 static int trigger_device(Manager *m, const char *prefix) {
555 struct udev_enumerate *e;
556 struct udev_list_entry *first, *item;
561 e = udev_enumerate_new(m->udev);
567 if (udev_enumerate_scan_devices(e) < 0) {
572 first = udev_enumerate_get_list_entry(e);
573 udev_list_entry_foreach(item, first) {
577 p = udev_list_entry_get_name(item);
579 if (prefix && !path_startswith(p, prefix))
582 t = strappend(p, "/uevent");
588 write_one_line_file(t, "change");
596 udev_enumerate_unref(e);
601 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
602 struct udev_device *d;
603 char *rule = NULL, *file = NULL;
604 const char *id_for_seat;
611 d = udev_device_new_from_syspath(m->udev, sysfs);
615 if (!device_has_tag(d, "seat")) {
620 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
626 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
631 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
636 mkdir_p("/etc/udev/rules.d", 0755);
637 r = write_one_line_file(file, rule);
641 r = trigger_device(m, sysfs);
648 udev_device_unref(d);
653 static int flush_devices(Manager *m) {
658 d = opendir("/etc/udev/rules.d");
661 log_warning("Failed to open /etc/udev/rules.d: %m");
665 while ((de = readdir(d))) {
667 if (!dirent_is_file(de))
670 if (!startswith(de->d_name, "72-seat-"))
673 if (!endswith(de->d_name, ".rules"))
676 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
677 log_warning("Failed to unlink %s: %m", de->d_name);
683 return trigger_device(m, NULL);
686 static DBusHandlerResult manager_message_handler(
687 DBusConnection *connection,
688 DBusMessage *message,
691 Manager *m = userdata;
693 const BusProperty properties[] = {
694 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
695 { "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
696 { "org.freedesktop.login1.Manager", "ResetControllers", bus_property_append_strv, "as", m->reset_controllers },
697 { "org.freedesktop.login1.Manager", "NAutoVTs", bus_property_append_unsigned, "u", &m->n_autovts },
698 { "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
699 { "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
700 { "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
701 { "org.freedesktop.login1.Manager", "IdleHint", bus_manager_append_idle_hint, "b", m },
702 { "org.freedesktop.login1.Manager", "IdleSinceHint", bus_manager_append_idle_hint_since, "t", m },
703 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
704 { NULL, NULL, NULL, NULL, NULL }
708 DBusMessage *reply = NULL;
715 dbus_error_init(&error);
717 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
723 if (!dbus_message_get_args(
726 DBUS_TYPE_STRING, &name,
728 return bus_send_error_reply(connection, message, &error, -EINVAL);
730 session = hashmap_get(m->sessions, name);
732 return bus_send_error_reply(connection, message, &error, -ENOENT);
734 reply = dbus_message_new_method_return(message);
738 p = session_bus_path(session);
742 b = dbus_message_append_args(
744 DBUS_TYPE_OBJECT_PATH, &p,
751 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
757 if (!dbus_message_get_args(
760 DBUS_TYPE_UINT32, &uid,
762 return bus_send_error_reply(connection, message, &error, -EINVAL);
764 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
766 return bus_send_error_reply(connection, message, &error, -ENOENT);
768 reply = dbus_message_new_method_return(message);
772 p = user_bus_path(user);
776 b = dbus_message_append_args(
778 DBUS_TYPE_OBJECT_PATH, &p,
785 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
791 if (!dbus_message_get_args(
794 DBUS_TYPE_STRING, &name,
796 return bus_send_error_reply(connection, message, &error, -EINVAL);
798 seat = hashmap_get(m->seats, name);
800 return bus_send_error_reply(connection, message, &error, -ENOENT);
802 reply = dbus_message_new_method_return(message);
806 p = seat_bus_path(seat);
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", "ListSessions")) {
823 DBusMessageIter iter, sub;
824 const char *empty = "";
826 reply = dbus_message_new_method_return(message);
830 dbus_message_iter_init_append(reply, &iter);
832 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
835 HASHMAP_FOREACH(session, m->sessions, i) {
836 DBusMessageIter sub2;
839 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
842 uid = session->user->uid;
844 p = session_bus_path(session);
848 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
849 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
850 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
851 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
852 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
859 if (!dbus_message_iter_close_container(&sub, &sub2))
863 if (!dbus_message_iter_close_container(&iter, &sub))
866 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
870 DBusMessageIter iter, sub;
872 reply = dbus_message_new_method_return(message);
876 dbus_message_iter_init_append(reply, &iter);
878 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
881 HASHMAP_FOREACH(user, m->users, i) {
882 DBusMessageIter sub2;
885 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
890 p = user_bus_path(user);
894 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
895 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
896 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
903 if (!dbus_message_iter_close_container(&sub, &sub2))
907 if (!dbus_message_iter_close_container(&iter, &sub))
910 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
914 DBusMessageIter iter, sub;
916 reply = dbus_message_new_method_return(message);
920 dbus_message_iter_init_append(reply, &iter);
922 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
925 HASHMAP_FOREACH(seat, m->seats, i) {
926 DBusMessageIter sub2;
928 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
931 p = seat_bus_path(seat);
935 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
936 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
943 if (!dbus_message_iter_close_container(&sub, &sub2))
947 if (!dbus_message_iter_close_container(&iter, &sub))
950 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
952 r = bus_manager_create_session(m, message, &reply);
957 return bus_send_error_reply(connection, message, &error, r);
959 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
963 if (!dbus_message_get_args(
966 DBUS_TYPE_STRING, &name,
968 return bus_send_error_reply(connection, message, &error, -EINVAL);
970 session = hashmap_get(m->sessions, name);
972 return bus_send_error_reply(connection, message, &error, -ENOENT);
974 r = session_activate(session);
976 return bus_send_error_reply(connection, message, NULL, r);
978 reply = dbus_message_new_method_return(message);
982 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
986 if (!dbus_message_get_args(
989 DBUS_TYPE_STRING, &name,
991 return bus_send_error_reply(connection, message, &error, -EINVAL);
993 session = hashmap_get(m->sessions, name);
995 return bus_send_error_reply(connection, message, &error, -ENOENT);
997 r = session_stop(session);
999 return bus_send_error_reply(connection, message, NULL, r);
1001 reply = dbus_message_new_method_return(message);
1005 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1009 if (!dbus_message_get_args(
1012 DBUS_TYPE_UINT32, &uid,
1014 return bus_send_error_reply(connection, message, &error, -EINVAL);
1016 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1018 return bus_send_error_reply(connection, message, &error, -ENOENT);
1020 r = user_stop(user);
1022 return bus_send_error_reply(connection, message, NULL, r);
1024 reply = dbus_message_new_method_return(message);
1028 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1032 if (!dbus_message_get_args(
1035 DBUS_TYPE_STRING, &name,
1037 return bus_send_error_reply(connection, message, &error, -EINVAL);
1039 seat = hashmap_get(m->seats, name);
1041 return bus_send_error_reply(connection, message, &error, -ENOENT);
1043 r = seat_stop_sessions(seat);
1045 return bus_send_error_reply(connection, message, NULL, r);
1047 reply = dbus_message_new_method_return(message);
1051 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1054 dbus_bool_t b, interactive;
1057 if (!dbus_message_get_args(
1060 DBUS_TYPE_UINT32, &uid,
1061 DBUS_TYPE_BOOLEAN, &b,
1062 DBUS_TYPE_BOOLEAN, &interactive,
1064 return bus_send_error_reply(connection, message, &error, -EINVAL);
1069 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1071 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1073 return bus_send_error_reply(connection, message, &error, r);
1075 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1077 return bus_send_error_reply(connection, message, &error, r);
1079 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1090 return bus_send_error_reply(connection, message, &error, r);
1092 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1101 if (r < 0 && errno != ENOENT)
1102 return bus_send_error_reply(connection, message, &error, -errno);
1104 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1106 user_add_to_gc_queue(u);
1109 reply = dbus_message_new_method_return(message);
1113 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1114 const char *sysfs, *seat;
1115 dbus_bool_t interactive;
1117 if (!dbus_message_get_args(
1120 DBUS_TYPE_STRING, &seat,
1121 DBUS_TYPE_STRING, &sysfs,
1122 DBUS_TYPE_BOOLEAN, &interactive,
1124 return bus_send_error_reply(connection, message, &error, -EINVAL);
1126 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1127 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1129 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1131 return bus_send_error_reply(connection, message, &error, r);
1133 r = attach_device(m, seat, sysfs);
1135 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1137 reply = dbus_message_new_method_return(message);
1142 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1143 dbus_bool_t interactive;
1145 if (!dbus_message_get_args(
1148 DBUS_TYPE_BOOLEAN, &interactive,
1150 return bus_send_error_reply(connection, message, &error, -EINVAL);
1152 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1154 return bus_send_error_reply(connection, message, &error, r);
1156 r = flush_devices(m);
1158 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1160 reply = dbus_message_new_method_return(message);
1164 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1165 char *introspection = NULL;
1174 if (!(reply = dbus_message_new_method_return(message)))
1177 /* We roll our own introspection code here, instead of
1178 * relying on bus_default_message_handler() because we
1179 * need to generate our introspection string
1182 if (!(f = open_memstream(&introspection, &size)))
1185 fputs(INTROSPECTION_BEGIN, f);
1187 HASHMAP_FOREACH(seat, m->seats, i) {
1188 p = bus_path_escape(seat->id);
1191 fprintf(f, "<node name=\"seat/%s\"/>", p);
1196 HASHMAP_FOREACH(user, m->users, i)
1197 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1199 HASHMAP_FOREACH(session, m->sessions, i) {
1200 p = bus_path_escape(session->id);
1203 fprintf(f, "<node name=\"session/%s\"/>", p);
1208 fputs(INTROSPECTION_END, f);
1212 free(introspection);
1221 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1222 free(introspection);
1226 free(introspection);
1228 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1231 if (!dbus_connection_send(connection, reply, NULL))
1234 dbus_message_unref(reply);
1237 return DBUS_HANDLER_RESULT_HANDLED;
1241 dbus_message_unref(reply);
1243 dbus_error_free(&error);
1245 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1248 const DBusObjectPathVTable bus_manager_vtable = {
1249 .message_function = manager_message_handler
1252 DBusHandlerResult bus_message_filter(
1253 DBusConnection *connection,
1254 DBusMessage *message,
1257 Manager *m = userdata;
1264 dbus_error_init(&error);
1266 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1269 if (!dbus_message_get_args(message, &error,
1270 DBUS_TYPE_STRING, &cgroup,
1272 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1274 manager_cgroup_notify_empty(m, cgroup);
1277 dbus_error_free(&error);
1279 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1282 int manager_send_changed(Manager *manager, const char *properties) {
1288 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1292 if (!dbus_connection_send(manager->bus, m, NULL))
1299 dbus_message_unref(m);