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=\"GetUser\">\n" \
40 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
41 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
43 " <method name=\"GetSeat\">\n" \
44 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
45 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
47 " <method name=\"ListSessions\">\n" \
48 " <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
50 " <method name=\"ListUsers\">\n" \
51 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
53 " <method name=\"ListSeats\">\n" \
54 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
56 " <method name=\"CreateSession\">\n" \
57 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
58 " <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
59 " <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n" \
60 " <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" \
61 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
62 " <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n" \
63 " <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
65 " <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
66 " <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
68 " <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
69 " <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
70 " <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
71 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
72 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
73 " <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
74 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
75 " <arg name=\"seat\" type=\"s\" direction=\"out\"/>\n" \
76 " <arg name=\"vtnr\" type=\"u\" direction=\"out\"/>\n" \
78 " <method name=\"ActivateSession\">\n" \
79 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
81 " <method name=\"LockSession\">\n" \
82 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
84 " <method name=\"UnlockSession\">\n" \
85 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
87 " <method name=\"KillSession\">\n" \
88 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
89 " <arg name=\"who\" type=\"s\"/>\n" \
90 " <arg name=\"signal\" type=\"s\"/>\n" \
92 " <method name=\"KillUser\">\n" \
93 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
94 " <arg name=\"signal\" type=\"s\"/>\n" \
96 " <method name=\"TerminateSession\">\n" \
97 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
99 " <method name=\"TerminateUser\">\n" \
100 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
102 " <method name=\"TerminateSeat\">\n" \
103 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
105 " <method name=\"SetUserLinger\">\n" \
106 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
107 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
108 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
110 " <method name=\"AttachDevice\">\n" \
111 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
112 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
113 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
115 " <method name=\"FlushDevices\">\n" \
116 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
118 " <method name=\"PowerOff\">\n" \
119 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
121 " <method name=\"Reboot\">\n" \
122 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
124 " <signal name=\"SessionNew\">\n" \
125 " <arg name=\"id\" type=\"s\"/>\n" \
126 " <arg name=\"path\" type=\"o\"/>\n" \
128 " <signal name=\"SessionRemoved\">\n" \
129 " <arg name=\"id\" type=\"s\"/>\n" \
130 " <arg name=\"path\" type=\"o\"/>\n" \
132 " <signal name=\"UserNew\">\n" \
133 " <arg name=\"uid\" type=\"u\"/>\n" \
134 " <arg name=\"path\" type=\"o\"/>\n" \
136 " <signal name=\"UserRemoved\">\n" \
137 " <arg name=\"uid\" type=\"u\"/>\n" \
138 " <arg name=\"path\" type=\"o\"/>\n" \
140 " <signal name=\"SeatNew\">\n" \
141 " <arg name=\"id\" type=\"s\"/>\n" \
142 " <arg name=\"path\" type=\"o\"/>\n" \
144 " <signal name=\"SeatRemoved\">\n" \
145 " <arg name=\"id\" type=\"s\"/>\n" \
146 " <arg name=\"path\" type=\"o\"/>\n" \
148 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
149 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
150 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
151 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
152 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
153 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
154 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
155 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
156 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
157 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
160 #define INTROSPECTION_BEGIN \
161 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
163 BUS_MANAGER_INTERFACE \
164 BUS_PROPERTIES_INTERFACE \
166 BUS_INTROSPECTABLE_INTERFACE
168 #define INTROSPECTION_END \
171 #define INTERFACES_LIST \
172 BUS_GENERIC_INTERFACES_LIST \
173 "org.freedesktop.login1.Manager\0"
175 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
183 b = manager_get_idle_hint(m, NULL) > 0;
184 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
190 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
199 manager_get_idle_hint(m, &t);
200 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
202 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
208 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
209 Session *session = NULL;
211 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
212 uint32_t uid, leader, audit_id = 0;
213 dbus_bool_t remote, kill_processes;
214 char **controllers = NULL, **reset_controllers = NULL;
217 DBusMessageIter iter;
222 DBusMessage *reply = NULL;
229 if (!dbus_message_iter_init(message, &iter) ||
230 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
233 dbus_message_iter_get_basic(&iter, &uid);
235 if (!dbus_message_iter_next(&iter) ||
236 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
239 dbus_message_iter_get_basic(&iter, &leader);
242 !dbus_message_iter_next(&iter) ||
243 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
246 dbus_message_iter_get_basic(&iter, &service);
248 if (!dbus_message_iter_next(&iter) ||
249 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
252 dbus_message_iter_get_basic(&iter, &type);
253 t = session_type_from_string(type);
256 !dbus_message_iter_next(&iter) ||
257 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
260 dbus_message_iter_get_basic(&iter, &seat);
265 s = hashmap_get(m->seats, seat);
270 if (!dbus_message_iter_next(&iter) ||
271 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
274 dbus_message_iter_get_basic(&iter, &vtnr);
276 if (!dbus_message_iter_next(&iter) ||
277 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
280 dbus_message_iter_get_basic(&iter, &tty);
282 if (tty_is_vc(tty)) {
287 else if (s != m->vtconsole)
290 v = vtnr_from_tty(tty);
293 return v < 0 ? v : -EINVAL;
297 else if (vtnr != (uint32_t) v)
300 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
304 if (seat_is_vtconsole(s)) {
305 if (vtnr <= 0 || vtnr > 63)
313 if (!dbus_message_iter_next(&iter) ||
314 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
317 dbus_message_iter_get_basic(&iter, &display);
319 if (!dbus_message_iter_next(&iter) ||
320 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
323 dbus_message_iter_get_basic(&iter, &remote);
325 if (!dbus_message_iter_next(&iter) ||
326 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
329 dbus_message_iter_get_basic(&iter, &remote_user);
331 if (!dbus_message_iter_next(&iter) ||
332 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
335 dbus_message_iter_get_basic(&iter, &remote_host);
337 if (!dbus_message_iter_next(&iter) ||
338 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
339 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
342 r = bus_parse_strv_iter(&iter, &controllers);
346 if (strv_contains(controllers, "systemd") ||
347 !dbus_message_iter_next(&iter) ||
348 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
349 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
354 r = bus_parse_strv_iter(&iter, &reset_controllers);
358 if (strv_contains(reset_controllers, "systemd") ||
359 !dbus_message_iter_next(&iter) ||
360 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
365 dbus_message_iter_get_basic(&iter, &kill_processes);
367 r = manager_add_user_by_uid(m, uid, &user);
371 audit_session_from_pid(leader, &audit_id);
374 asprintf(&id, "%lu", (unsigned long) audit_id);
381 session = hashmap_get(m->sessions, id);
385 fifo_fd = session_create_fifo(session);
391 /* Session already exists, client is probably
392 * something like "su" which changes uid but
393 * is still the same audit session */
395 reply = dbus_message_new_method_return(message);
401 p = session_bus_path(session);
407 b = dbus_message_append_args(
409 DBUS_TYPE_STRING, &session->id,
410 DBUS_TYPE_OBJECT_PATH, &p,
411 DBUS_TYPE_STRING, &session->user->runtime_path,
412 DBUS_TYPE_UNIX_FD, &fifo_fd,
421 close_nointr_nofail(fifo_fd);
430 asprintf(&id, "c%lu", ++m->session_counter);
437 } while (hashmap_get(m->sessions, id));
440 r = manager_add_session(m, user, id, &session);
445 session->leader = leader;
446 session->audit_id = audit_id;
448 session->remote = remote;
449 session->controllers = controllers;
450 session->reset_controllers = reset_controllers;
451 session->kill_processes = kill_processes;
452 session->vtnr = vtnr;
454 controllers = reset_controllers = NULL;
457 session->tty = strdup(tty);
464 if (!isempty(display)) {
465 session->display = strdup(display);
466 if (!session->display) {
472 if (!isempty(remote_user)) {
473 session->remote_user = strdup(remote_user);
474 if (!session->remote_user) {
480 if (!isempty(remote_host)) {
481 session->remote_host = strdup(remote_host);
482 if (!session->remote_host) {
488 if (!isempty(service)) {
489 session->service = strdup(service);
490 if (!session->service) {
496 fifo_fd = session_create_fifo(session);
503 r = seat_attach_session(s, session);
508 r = session_start(session);
512 reply = dbus_message_new_method_return(message);
518 p = session_bus_path(session);
524 seat = s ? s->id : "";
525 b = dbus_message_append_args(
527 DBUS_TYPE_STRING, &session->id,
528 DBUS_TYPE_OBJECT_PATH, &p,
529 DBUS_TYPE_STRING, &session->user->runtime_path,
530 DBUS_TYPE_UNIX_FD, &fifo_fd,
531 DBUS_TYPE_STRING, &seat,
532 DBUS_TYPE_UINT32, &vtnr,
541 close_nointr_nofail(fifo_fd);
547 strv_free(controllers);
548 strv_free(reset_controllers);
551 session_add_to_gc_queue(session);
554 user_add_to_gc_queue(user);
557 close_nointr_nofail(fifo_fd);
560 dbus_message_unref(reply);
565 static int trigger_device(Manager *m, struct udev_device *d) {
566 struct udev_enumerate *e;
567 struct udev_list_entry *first, *item;
572 e = udev_enumerate_new(m->udev);
579 if (udev_enumerate_add_match_parent(e, d) < 0) {
585 if (udev_enumerate_scan_devices(e) < 0) {
590 first = udev_enumerate_get_list_entry(e);
591 udev_list_entry_foreach(item, first) {
595 p = udev_list_entry_get_name(item);
597 t = strappend(p, "/uevent");
603 write_one_line_file(t, "change");
611 udev_enumerate_unref(e);
616 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
617 struct udev_device *d;
618 char *rule = NULL, *file = NULL;
619 const char *id_for_seat;
626 d = udev_device_new_from_syspath(m->udev, sysfs);
630 if (!udev_device_has_tag(d, "seat")) {
635 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
641 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
646 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
651 mkdir_p("/etc/udev/rules.d", 0755);
652 r = write_one_line_file_atomic(file, rule);
656 r = trigger_device(m, d);
663 udev_device_unref(d);
668 static int flush_devices(Manager *m) {
673 d = opendir("/etc/udev/rules.d");
676 log_warning("Failed to open /etc/udev/rules.d: %m");
680 while ((de = readdir(d))) {
682 if (!dirent_is_file(de))
685 if (!startswith(de->d_name, "72-seat-"))
688 if (!endswith(de->d_name, ".rules"))
691 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
692 log_warning("Failed to unlink %s: %m", de->d_name);
698 return trigger_device(m, NULL);
701 static DBusHandlerResult manager_message_handler(
702 DBusConnection *connection,
703 DBusMessage *message,
706 Manager *m = userdata;
708 const BusProperty properties[] = {
709 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
710 { "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
711 { "org.freedesktop.login1.Manager", "ResetControllers", bus_property_append_strv, "as", m->reset_controllers },
712 { "org.freedesktop.login1.Manager", "NAutoVTs", bus_property_append_unsigned, "u", &m->n_autovts },
713 { "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
714 { "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
715 { "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
716 { "org.freedesktop.login1.Manager", "IdleHint", bus_manager_append_idle_hint, "b", m },
717 { "org.freedesktop.login1.Manager", "IdleSinceHint", bus_manager_append_idle_hint_since, "t", m },
718 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
719 { NULL, NULL, NULL, NULL, NULL }
723 DBusMessage *reply = NULL;
730 dbus_error_init(&error);
732 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
738 if (!dbus_message_get_args(
741 DBUS_TYPE_STRING, &name,
743 return bus_send_error_reply(connection, message, &error, -EINVAL);
745 session = hashmap_get(m->sessions, name);
747 return bus_send_error_reply(connection, message, &error, -ENOENT);
749 reply = dbus_message_new_method_return(message);
753 p = session_bus_path(session);
757 b = dbus_message_append_args(
759 DBUS_TYPE_OBJECT_PATH, &p,
766 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
772 if (!dbus_message_get_args(
775 DBUS_TYPE_UINT32, &uid,
777 return bus_send_error_reply(connection, message, &error, -EINVAL);
779 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
781 return bus_send_error_reply(connection, message, &error, -ENOENT);
783 reply = dbus_message_new_method_return(message);
787 p = user_bus_path(user);
791 b = dbus_message_append_args(
793 DBUS_TYPE_OBJECT_PATH, &p,
800 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
806 if (!dbus_message_get_args(
809 DBUS_TYPE_STRING, &name,
811 return bus_send_error_reply(connection, message, &error, -EINVAL);
813 seat = hashmap_get(m->seats, name);
815 return bus_send_error_reply(connection, message, &error, -ENOENT);
817 reply = dbus_message_new_method_return(message);
821 p = seat_bus_path(seat);
825 b = dbus_message_append_args(
827 DBUS_TYPE_OBJECT_PATH, &p,
834 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
838 DBusMessageIter iter, sub;
839 const char *empty = "";
841 reply = dbus_message_new_method_return(message);
845 dbus_message_iter_init_append(reply, &iter);
847 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
850 HASHMAP_FOREACH(session, m->sessions, i) {
851 DBusMessageIter sub2;
854 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
857 uid = session->user->uid;
859 p = session_bus_path(session);
863 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
864 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
865 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
866 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
867 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
874 if (!dbus_message_iter_close_container(&sub, &sub2))
878 if (!dbus_message_iter_close_container(&iter, &sub))
881 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
885 DBusMessageIter iter, sub;
887 reply = dbus_message_new_method_return(message);
891 dbus_message_iter_init_append(reply, &iter);
893 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
896 HASHMAP_FOREACH(user, m->users, i) {
897 DBusMessageIter sub2;
900 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
905 p = user_bus_path(user);
909 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
910 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
911 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
918 if (!dbus_message_iter_close_container(&sub, &sub2))
922 if (!dbus_message_iter_close_container(&iter, &sub))
925 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
929 DBusMessageIter iter, sub;
931 reply = dbus_message_new_method_return(message);
935 dbus_message_iter_init_append(reply, &iter);
937 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
940 HASHMAP_FOREACH(seat, m->seats, i) {
941 DBusMessageIter sub2;
943 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
946 p = seat_bus_path(seat);
950 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
951 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
958 if (!dbus_message_iter_close_container(&sub, &sub2))
962 if (!dbus_message_iter_close_container(&iter, &sub))
965 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
967 r = bus_manager_create_session(m, message, &reply);
972 return bus_send_error_reply(connection, message, &error, r);
974 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
978 if (!dbus_message_get_args(
981 DBUS_TYPE_STRING, &name,
983 return bus_send_error_reply(connection, message, &error, -EINVAL);
985 session = hashmap_get(m->sessions, name);
987 return bus_send_error_reply(connection, message, &error, -ENOENT);
989 r = session_activate(session);
991 return bus_send_error_reply(connection, message, NULL, r);
993 reply = dbus_message_new_method_return(message);
997 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
998 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
1002 if (!dbus_message_get_args(
1005 DBUS_TYPE_STRING, &name,
1007 return bus_send_error_reply(connection, message, &error, -EINVAL);
1009 session = hashmap_get(m->sessions, name);
1011 return bus_send_error_reply(connection, message, &error, -ENOENT);
1013 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1016 reply = dbus_message_new_method_return(message);
1020 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1027 if (!dbus_message_get_args(
1030 DBUS_TYPE_STRING, &name,
1031 DBUS_TYPE_STRING, &swho,
1032 DBUS_TYPE_INT32, &signo,
1034 return bus_send_error_reply(connection, message, &error, -EINVAL);
1039 who = kill_who_from_string(swho);
1041 return bus_send_error_reply(connection, message, &error, -EINVAL);
1044 if (signo <= 0 || signo >= _NSIG)
1045 return bus_send_error_reply(connection, message, &error, -EINVAL);
1047 session = hashmap_get(m->sessions, name);
1049 return bus_send_error_reply(connection, message, &error, -ENOENT);
1051 r = session_kill(session, who, signo);
1053 return bus_send_error_reply(connection, message, NULL, r);
1055 reply = dbus_message_new_method_return(message);
1059 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1064 if (!dbus_message_get_args(
1067 DBUS_TYPE_UINT32, &uid,
1068 DBUS_TYPE_INT32, &signo,
1070 return bus_send_error_reply(connection, message, &error, -EINVAL);
1072 if (signo <= 0 || signo >= _NSIG)
1073 return bus_send_error_reply(connection, message, &error, -EINVAL);
1075 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1077 return bus_send_error_reply(connection, message, &error, -ENOENT);
1079 r = user_kill(user, signo);
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", "TerminateSession")) {
1091 if (!dbus_message_get_args(
1094 DBUS_TYPE_STRING, &name,
1096 return bus_send_error_reply(connection, message, &error, -EINVAL);
1098 session = hashmap_get(m->sessions, name);
1100 return bus_send_error_reply(connection, message, &error, -ENOENT);
1102 r = session_stop(session);
1104 return bus_send_error_reply(connection, message, NULL, r);
1106 reply = dbus_message_new_method_return(message);
1110 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1114 if (!dbus_message_get_args(
1117 DBUS_TYPE_UINT32, &uid,
1119 return bus_send_error_reply(connection, message, &error, -EINVAL);
1121 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1123 return bus_send_error_reply(connection, message, &error, -ENOENT);
1125 r = user_stop(user);
1127 return bus_send_error_reply(connection, message, NULL, r);
1129 reply = dbus_message_new_method_return(message);
1133 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1137 if (!dbus_message_get_args(
1140 DBUS_TYPE_STRING, &name,
1142 return bus_send_error_reply(connection, message, &error, -EINVAL);
1144 seat = hashmap_get(m->seats, name);
1146 return bus_send_error_reply(connection, message, &error, -ENOENT);
1148 r = seat_stop_sessions(seat);
1150 return bus_send_error_reply(connection, message, NULL, r);
1152 reply = dbus_message_new_method_return(message);
1156 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1159 dbus_bool_t b, interactive;
1162 if (!dbus_message_get_args(
1165 DBUS_TYPE_UINT32, &uid,
1166 DBUS_TYPE_BOOLEAN, &b,
1167 DBUS_TYPE_BOOLEAN, &interactive,
1169 return bus_send_error_reply(connection, message, &error, -EINVAL);
1174 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1176 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1178 return bus_send_error_reply(connection, message, &error, r);
1180 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1182 return bus_send_error_reply(connection, message, &error, r);
1184 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1195 return bus_send_error_reply(connection, message, &error, r);
1197 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1206 if (r < 0 && errno != ENOENT)
1207 return bus_send_error_reply(connection, message, &error, -errno);
1209 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1211 user_add_to_gc_queue(u);
1214 reply = dbus_message_new_method_return(message);
1218 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1219 const char *sysfs, *seat;
1220 dbus_bool_t interactive;
1222 if (!dbus_message_get_args(
1225 DBUS_TYPE_STRING, &seat,
1226 DBUS_TYPE_STRING, &sysfs,
1227 DBUS_TYPE_BOOLEAN, &interactive,
1229 return bus_send_error_reply(connection, message, &error, -EINVAL);
1231 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1232 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1234 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1236 return bus_send_error_reply(connection, message, &error, r);
1238 r = attach_device(m, seat, sysfs);
1240 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1242 reply = dbus_message_new_method_return(message);
1247 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1248 dbus_bool_t interactive;
1250 if (!dbus_message_get_args(
1253 DBUS_TYPE_BOOLEAN, &interactive,
1255 return bus_send_error_reply(connection, message, &error, -EINVAL);
1257 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1259 return bus_send_error_reply(connection, message, &error, r);
1261 r = flush_devices(m);
1263 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1265 reply = dbus_message_new_method_return(message);
1269 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "PowerOff") ||
1270 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Reboot")) {
1271 dbus_bool_t interactive;
1272 bool multiple_sessions;
1273 DBusMessage *forward, *freply;
1275 const char *mode = "replace";
1278 if (!dbus_message_get_args(
1281 DBUS_TYPE_BOOLEAN, &interactive,
1283 return bus_send_error_reply(connection, message, &error, -EINVAL);
1285 multiple_sessions = hashmap_size(m->sessions) > 1;
1287 if (!multiple_sessions) {
1290 /* Hmm, there's only one session, but let's
1291 * make sure it actually belongs to the user
1292 * who is asking. If not, better be safe than
1295 s = hashmap_first(m->sessions);
1299 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
1300 if (ul == (unsigned long) -1)
1301 return bus_send_error_reply(connection, message, &error, -EIO);
1303 multiple_sessions = s->user->uid != ul;
1307 if (streq(dbus_message_get_member(message), "PowerOff")) {
1308 if (multiple_sessions)
1309 action = "org.freedesktop.login1.power-off-multiple-sessions";
1311 action = "org.freedesktop.login1.power-off";
1313 name = SPECIAL_POWEROFF_TARGET;
1315 if (multiple_sessions)
1316 action = "org.freedesktop.login1.reboot-multiple-sessions";
1318 action = "org.freedesktop.login1.reboot";
1320 name = SPECIAL_REBOOT_TARGET;
1323 r = verify_polkit(connection, message, action, interactive, &error);
1325 return bus_send_error_reply(connection, message, &error, r);
1327 forward = dbus_message_new_method_call(
1328 "org.freedesktop.systemd1",
1329 "/org/freedesktop/systemd1",
1330 "org.freedesktop.systemd1.Manager",
1333 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1335 if (!dbus_message_append_args(forward,
1336 DBUS_TYPE_STRING, &name,
1337 DBUS_TYPE_STRING, &mode,
1338 DBUS_TYPE_INVALID)) {
1339 dbus_message_unref(forward);
1340 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1343 freply = dbus_connection_send_with_reply_and_block(connection, forward, -1, &error);
1344 dbus_message_unref(forward);
1347 return bus_send_error_reply(connection, message, &error, -EIO);
1349 dbus_message_unref(freply);
1351 reply = dbus_message_new_method_return(message);
1355 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1356 char *introspection = NULL;
1365 if (!(reply = dbus_message_new_method_return(message)))
1368 /* We roll our own introspection code here, instead of
1369 * relying on bus_default_message_handler() because we
1370 * need to generate our introspection string
1373 if (!(f = open_memstream(&introspection, &size)))
1376 fputs(INTROSPECTION_BEGIN, f);
1378 HASHMAP_FOREACH(seat, m->seats, i) {
1379 p = bus_path_escape(seat->id);
1382 fprintf(f, "<node name=\"seat/%s\"/>", p);
1387 HASHMAP_FOREACH(user, m->users, i)
1388 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1390 HASHMAP_FOREACH(session, m->sessions, i) {
1391 p = bus_path_escape(session->id);
1394 fprintf(f, "<node name=\"session/%s\"/>", p);
1399 fputs(INTROSPECTION_END, f);
1403 free(introspection);
1412 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1413 free(introspection);
1417 free(introspection);
1419 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1422 if (!dbus_connection_send(connection, reply, NULL))
1425 dbus_message_unref(reply);
1428 return DBUS_HANDLER_RESULT_HANDLED;
1432 dbus_message_unref(reply);
1434 dbus_error_free(&error);
1436 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1439 const DBusObjectPathVTable bus_manager_vtable = {
1440 .message_function = manager_message_handler
1443 DBusHandlerResult bus_message_filter(
1444 DBusConnection *connection,
1445 DBusMessage *message,
1448 Manager *m = userdata;
1455 dbus_error_init(&error);
1457 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1460 if (!dbus_message_get_args(message, &error,
1461 DBUS_TYPE_STRING, &cgroup,
1463 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1465 manager_cgroup_notify_empty(m, cgroup);
1468 dbus_error_free(&error);
1470 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1473 int manager_send_changed(Manager *manager, const char *properties) {
1479 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1483 if (!dbus_connection_send(manager->bus, m, NULL))
1490 dbus_message_unref(m);