1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "dbus-common.h"
33 #define BUS_MANAGER_INTERFACE \
34 " <interface name=\"org.freedesktop.login1.Manager\">\n" \
35 " <method name=\"GetSession\">\n" \
36 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
37 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
39 " <method name=\"GetSessionByPID\">\n" \
40 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
41 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
43 " <method name=\"GetUser\">\n" \
44 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
45 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
47 " <method name=\"GetSeat\">\n" \
48 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
51 " <method name=\"ListSessions\">\n" \
52 " <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
54 " <method name=\"ListUsers\">\n" \
55 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
57 " <method name=\"ListSeats\">\n" \
58 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
60 " <method name=\"CreateSession\">\n" \
61 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
62 " <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
63 " <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" \
65 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n" \
67 " <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
68 " <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
69 " <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
70 " <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
71 " <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
72 " <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
73 " <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
74 " <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
75 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
76 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
77 " <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
78 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
79 " <arg name=\"seat\" type=\"s\" direction=\"out\"/>\n" \
80 " <arg name=\"vtnr\" type=\"u\" direction=\"out\"/>\n" \
82 " <method name=\"ActivateSession\">\n" \
83 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
85 " <method name=\"ActivateSessionOnSeat\">\n" \
86 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
87 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
89 " <method name=\"LockSession\">\n" \
90 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
92 " <method name=\"UnlockSession\">\n" \
93 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
95 " <method name=\"KillSession\">\n" \
96 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
97 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
98 " <arg name=\"signal\" type=\"s\" direction=\"in\"/>\n" \
100 " <method name=\"KillUser\">\n" \
101 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
102 " <arg name=\"signal\" type=\"s\" direction=\"in\"/>\n" \
104 " <method name=\"TerminateSession\">\n" \
105 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
107 " <method name=\"TerminateUser\">\n" \
108 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
110 " <method name=\"TerminateSeat\">\n" \
111 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
113 " <method name=\"SetUserLinger\">\n" \
114 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
115 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
116 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
118 " <method name=\"AttachDevice\">\n" \
119 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
120 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
121 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
123 " <method name=\"FlushDevices\">\n" \
124 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
126 " <method name=\"PowerOff\">\n" \
127 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
129 " <method name=\"Reboot\">\n" \
130 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
132 " <signal name=\"SessionNew\">\n" \
133 " <arg name=\"id\" type=\"s\"/>\n" \
134 " <arg name=\"path\" type=\"o\"/>\n" \
136 " <signal name=\"SessionRemoved\">\n" \
137 " <arg name=\"id\" type=\"s\"/>\n" \
138 " <arg name=\"path\" type=\"o\"/>\n" \
140 " <signal name=\"UserNew\">\n" \
141 " <arg name=\"uid\" type=\"u\"/>\n" \
142 " <arg name=\"path\" type=\"o\"/>\n" \
144 " <signal name=\"UserRemoved\">\n" \
145 " <arg name=\"uid\" type=\"u\"/>\n" \
146 " <arg name=\"path\" type=\"o\"/>\n" \
148 " <signal name=\"SeatNew\">\n" \
149 " <arg name=\"id\" type=\"s\"/>\n" \
150 " <arg name=\"path\" type=\"o\"/>\n" \
152 " <signal name=\"SeatRemoved\">\n" \
153 " <arg name=\"id\" type=\"s\"/>\n" \
154 " <arg name=\"path\" type=\"o\"/>\n" \
156 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
157 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
158 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
159 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
160 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
161 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
162 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
163 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
164 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
165 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
168 #define INTROSPECTION_BEGIN \
169 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
171 BUS_MANAGER_INTERFACE \
172 BUS_PROPERTIES_INTERFACE \
174 BUS_INTROSPECTABLE_INTERFACE
176 #define INTROSPECTION_END \
179 #define INTERFACES_LIST \
180 BUS_GENERIC_INTERFACES_LIST \
181 "org.freedesktop.login1.Manager\0"
183 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
191 b = manager_get_idle_hint(m, NULL) > 0;
192 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
198 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
207 manager_get_idle_hint(m, &t);
208 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
210 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
216 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
217 Session *session = NULL;
219 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
220 uint32_t uid, leader, audit_id = 0;
221 dbus_bool_t remote, kill_processes;
222 char **controllers = NULL, **reset_controllers = NULL;
225 DBusMessageIter iter;
230 DBusMessage *reply = NULL;
237 if (!dbus_message_iter_init(message, &iter) ||
238 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
241 dbus_message_iter_get_basic(&iter, &uid);
243 if (!dbus_message_iter_next(&iter) ||
244 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
247 dbus_message_iter_get_basic(&iter, &leader);
250 !dbus_message_iter_next(&iter) ||
251 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
254 dbus_message_iter_get_basic(&iter, &service);
256 if (!dbus_message_iter_next(&iter) ||
257 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
260 dbus_message_iter_get_basic(&iter, &type);
261 t = session_type_from_string(type);
264 !dbus_message_iter_next(&iter) ||
265 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
268 dbus_message_iter_get_basic(&iter, &seat);
273 s = hashmap_get(m->seats, seat);
278 if (!dbus_message_iter_next(&iter) ||
279 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
282 dbus_message_iter_get_basic(&iter, &vtnr);
284 if (!dbus_message_iter_next(&iter) ||
285 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
288 dbus_message_iter_get_basic(&iter, &tty);
290 if (tty_is_vc(tty)) {
295 else if (s != m->vtconsole)
298 v = vtnr_from_tty(tty);
301 return v < 0 ? v : -EINVAL;
305 else if (vtnr != (uint32_t) v)
308 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
312 if (seat_can_multi_session(s)) {
313 if (vtnr <= 0 || vtnr > 63)
321 if (!dbus_message_iter_next(&iter) ||
322 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
325 dbus_message_iter_get_basic(&iter, &display);
327 if (!dbus_message_iter_next(&iter) ||
328 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
331 dbus_message_iter_get_basic(&iter, &remote);
333 if (!dbus_message_iter_next(&iter) ||
334 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
337 dbus_message_iter_get_basic(&iter, &remote_user);
339 if (!dbus_message_iter_next(&iter) ||
340 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
343 dbus_message_iter_get_basic(&iter, &remote_host);
345 if (!dbus_message_iter_next(&iter) ||
346 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
347 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
350 r = bus_parse_strv_iter(&iter, &controllers);
354 if (strv_contains(controllers, "systemd") ||
355 !dbus_message_iter_next(&iter) ||
356 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
357 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
362 r = bus_parse_strv_iter(&iter, &reset_controllers);
366 if (strv_contains(reset_controllers, "systemd") ||
367 !dbus_message_iter_next(&iter) ||
368 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
373 dbus_message_iter_get_basic(&iter, &kill_processes);
375 r = manager_add_user_by_uid(m, uid, &user);
379 audit_session_from_pid(leader, &audit_id);
382 asprintf(&id, "%lu", (unsigned long) audit_id);
389 session = hashmap_get(m->sessions, id);
394 fifo_fd = session_create_fifo(session);
400 /* Session already exists, client is probably
401 * something like "su" which changes uid but
402 * is still the same audit session */
404 reply = dbus_message_new_method_return(message);
410 p = session_bus_path(session);
416 seat = session->seat ? session->seat->id : "";
417 vtnr = session->vtnr;
418 b = dbus_message_append_args(
420 DBUS_TYPE_STRING, &session->id,
421 DBUS_TYPE_OBJECT_PATH, &p,
422 DBUS_TYPE_STRING, &session->user->runtime_path,
423 DBUS_TYPE_UNIX_FD, &fifo_fd,
424 DBUS_TYPE_STRING, &seat,
425 DBUS_TYPE_UINT32, &vtnr,
434 close_nointr_nofail(fifo_fd);
437 strv_free(controllers);
438 strv_free(reset_controllers);
446 asprintf(&id, "c%lu", ++m->session_counter);
453 } while (hashmap_get(m->sessions, id));
456 r = manager_add_session(m, user, id, &session);
461 session->leader = leader;
462 session->audit_id = audit_id;
464 session->remote = remote;
465 session->controllers = controllers;
466 session->reset_controllers = reset_controllers;
467 session->kill_processes = kill_processes;
468 session->vtnr = vtnr;
470 controllers = reset_controllers = NULL;
473 session->tty = strdup(tty);
480 if (!isempty(display)) {
481 session->display = strdup(display);
482 if (!session->display) {
488 if (!isempty(remote_user)) {
489 session->remote_user = strdup(remote_user);
490 if (!session->remote_user) {
496 if (!isempty(remote_host)) {
497 session->remote_host = strdup(remote_host);
498 if (!session->remote_host) {
504 if (!isempty(service)) {
505 session->service = strdup(service);
506 if (!session->service) {
512 fifo_fd = session_create_fifo(session);
519 r = seat_attach_session(s, session);
524 r = session_start(session);
528 reply = dbus_message_new_method_return(message);
534 p = session_bus_path(session);
540 seat = s ? s->id : "";
541 b = dbus_message_append_args(
543 DBUS_TYPE_STRING, &session->id,
544 DBUS_TYPE_OBJECT_PATH, &p,
545 DBUS_TYPE_STRING, &session->user->runtime_path,
546 DBUS_TYPE_UNIX_FD, &fifo_fd,
547 DBUS_TYPE_STRING, &seat,
548 DBUS_TYPE_UINT32, &vtnr,
557 close_nointr_nofail(fifo_fd);
563 strv_free(controllers);
564 strv_free(reset_controllers);
567 session_add_to_gc_queue(session);
570 user_add_to_gc_queue(user);
573 close_nointr_nofail(fifo_fd);
576 dbus_message_unref(reply);
581 static int trigger_device(Manager *m, struct udev_device *d) {
582 struct udev_enumerate *e;
583 struct udev_list_entry *first, *item;
588 e = udev_enumerate_new(m->udev);
595 if (udev_enumerate_add_match_parent(e, d) < 0) {
601 if (udev_enumerate_scan_devices(e) < 0) {
606 first = udev_enumerate_get_list_entry(e);
607 udev_list_entry_foreach(item, first) {
611 p = udev_list_entry_get_name(item);
613 t = strappend(p, "/uevent");
619 write_one_line_file(t, "change");
627 udev_enumerate_unref(e);
632 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
633 struct udev_device *d;
634 char *rule = NULL, *file = NULL;
635 const char *id_for_seat;
642 d = udev_device_new_from_syspath(m->udev, sysfs);
646 if (!udev_device_has_tag(d, "seat")) {
651 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
657 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
662 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
667 mkdir_p("/etc/udev/rules.d", 0755);
668 r = write_one_line_file_atomic(file, rule);
672 r = trigger_device(m, d);
679 udev_device_unref(d);
684 static int flush_devices(Manager *m) {
689 d = opendir("/etc/udev/rules.d");
692 log_warning("Failed to open /etc/udev/rules.d: %m");
696 while ((de = readdir(d))) {
698 if (!dirent_is_file(de))
701 if (!startswith(de->d_name, "72-seat-"))
704 if (!endswith(de->d_name, ".rules"))
707 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
708 log_warning("Failed to unlink %s: %m", de->d_name);
714 return trigger_device(m, NULL);
717 static const BusProperty bus_login_manager_properties[] = {
718 { "ControlGroupHierarchy", bus_property_append_string, "s", offsetof(Manager, cgroup_path), true },
719 { "Controllers", bus_property_append_strv, "as", offsetof(Manager, controllers), true },
720 { "ResetControllers", bus_property_append_strv, "as", offsetof(Manager, reset_controllers), true },
721 { "NAutoVTs", bus_property_append_unsigned, "u", offsetof(Manager, n_autovts) },
722 { "KillOnlyUsers", bus_property_append_strv, "as", offsetof(Manager, kill_only_users), true },
723 { "KillExcludeUsers", bus_property_append_strv, "as", offsetof(Manager, kill_exclude_users), true },
724 { "KillUserProcesses", bus_property_append_bool, "b", offsetof(Manager, kill_user_processes) },
725 { "IdleHint", bus_manager_append_idle_hint, "b", 0 },
726 { "IdleSinceHint", bus_manager_append_idle_hint_since, "t", 0 },
727 { "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", 0 },
731 static DBusHandlerResult manager_message_handler(
732 DBusConnection *connection,
733 DBusMessage *message,
736 Manager *m = userdata;
739 DBusMessage *reply = NULL;
746 dbus_error_init(&error);
748 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
754 if (!dbus_message_get_args(
757 DBUS_TYPE_STRING, &name,
759 return bus_send_error_reply(connection, message, &error, -EINVAL);
761 session = hashmap_get(m->sessions, name);
763 return bus_send_error_reply(connection, message, &error, -ENOENT);
765 reply = dbus_message_new_method_return(message);
769 p = session_bus_path(session);
773 b = dbus_message_append_args(
775 DBUS_TYPE_OBJECT_PATH, &p,
782 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSessionByPID")) {
788 if (!dbus_message_get_args(
791 DBUS_TYPE_UINT32, &pid,
793 return bus_send_error_reply(connection, message, &error, -EINVAL);
795 r = manager_get_session_by_pid(m, pid, &session);
797 return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
799 reply = dbus_message_new_method_return(message);
803 p = session_bus_path(session);
807 b = dbus_message_append_args(
809 DBUS_TYPE_OBJECT_PATH, &p,
816 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
822 if (!dbus_message_get_args(
825 DBUS_TYPE_UINT32, &uid,
827 return bus_send_error_reply(connection, message, &error, -EINVAL);
829 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
831 return bus_send_error_reply(connection, message, &error, -ENOENT);
833 reply = dbus_message_new_method_return(message);
837 p = user_bus_path(user);
841 b = dbus_message_append_args(
843 DBUS_TYPE_OBJECT_PATH, &p,
850 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
856 if (!dbus_message_get_args(
859 DBUS_TYPE_STRING, &name,
861 return bus_send_error_reply(connection, message, &error, -EINVAL);
863 seat = hashmap_get(m->seats, name);
865 return bus_send_error_reply(connection, message, &error, -ENOENT);
867 reply = dbus_message_new_method_return(message);
871 p = seat_bus_path(seat);
875 b = dbus_message_append_args(
877 DBUS_TYPE_OBJECT_PATH, &p,
884 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
888 DBusMessageIter iter, sub;
889 const char *empty = "";
891 reply = dbus_message_new_method_return(message);
895 dbus_message_iter_init_append(reply, &iter);
897 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
900 HASHMAP_FOREACH(session, m->sessions, i) {
901 DBusMessageIter sub2;
904 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
907 uid = session->user->uid;
909 p = session_bus_path(session);
913 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
914 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
915 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
916 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
917 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
924 if (!dbus_message_iter_close_container(&sub, &sub2))
928 if (!dbus_message_iter_close_container(&iter, &sub))
931 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
935 DBusMessageIter iter, sub;
937 reply = dbus_message_new_method_return(message);
941 dbus_message_iter_init_append(reply, &iter);
943 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
946 HASHMAP_FOREACH(user, m->users, i) {
947 DBusMessageIter sub2;
950 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
955 p = user_bus_path(user);
959 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
960 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
961 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
968 if (!dbus_message_iter_close_container(&sub, &sub2))
972 if (!dbus_message_iter_close_container(&iter, &sub))
975 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
979 DBusMessageIter iter, sub;
981 reply = dbus_message_new_method_return(message);
985 dbus_message_iter_init_append(reply, &iter);
987 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
990 HASHMAP_FOREACH(seat, m->seats, i) {
991 DBusMessageIter sub2;
993 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
996 p = seat_bus_path(seat);
1000 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
1001 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
1008 if (!dbus_message_iter_close_container(&sub, &sub2))
1012 if (!dbus_message_iter_close_container(&iter, &sub))
1015 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
1017 r = bus_manager_create_session(m, message, &reply);
1019 /* Don't delay the work on OOM here, since it might be
1020 * triggered by a low RLIMIT_NOFILE here (since we
1021 * send a dupped fd to the client), and we'd rather
1022 * see this fail quickly then be retried later */
1025 return bus_send_error_reply(connection, message, &error, r);
1027 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
1031 if (!dbus_message_get_args(
1034 DBUS_TYPE_STRING, &name,
1036 return bus_send_error_reply(connection, message, &error, -EINVAL);
1038 session = hashmap_get(m->sessions, name);
1040 return bus_send_error_reply(connection, message, &error, -ENOENT);
1042 r = session_activate(session);
1044 return bus_send_error_reply(connection, message, NULL, r);
1046 reply = dbus_message_new_method_return(message);
1050 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSessionOnSeat")) {
1051 const char *session_name, *seat_name;
1055 /* Same as ActivateSession() but refuses to work if
1056 * the seat doesn't match */
1058 if (!dbus_message_get_args(
1061 DBUS_TYPE_STRING, &session_name,
1062 DBUS_TYPE_STRING, &seat_name,
1064 return bus_send_error_reply(connection, message, &error, -EINVAL);
1066 session = hashmap_get(m->sessions, session_name);
1068 return bus_send_error_reply(connection, message, &error, -ENOENT);
1070 seat = hashmap_get(m->seats, seat_name);
1072 return bus_send_error_reply(connection, message, &error, -ENOENT);
1074 if (session->seat != seat)
1075 return bus_send_error_reply(connection, message, &error, -EINVAL);
1077 r = session_activate(session);
1079 return bus_send_error_reply(connection, message, NULL, r);
1081 reply = dbus_message_new_method_return(message);
1085 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
1086 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
1090 if (!dbus_message_get_args(
1093 DBUS_TYPE_STRING, &name,
1095 return bus_send_error_reply(connection, message, &error, -EINVAL);
1097 session = hashmap_get(m->sessions, name);
1099 return bus_send_error_reply(connection, message, &error, -ENOENT);
1101 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1104 reply = dbus_message_new_method_return(message);
1108 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1115 if (!dbus_message_get_args(
1118 DBUS_TYPE_STRING, &name,
1119 DBUS_TYPE_STRING, &swho,
1120 DBUS_TYPE_INT32, &signo,
1122 return bus_send_error_reply(connection, message, &error, -EINVAL);
1127 who = kill_who_from_string(swho);
1129 return bus_send_error_reply(connection, message, &error, -EINVAL);
1132 if (signo <= 0 || signo >= _NSIG)
1133 return bus_send_error_reply(connection, message, &error, -EINVAL);
1135 session = hashmap_get(m->sessions, name);
1137 return bus_send_error_reply(connection, message, &error, -ENOENT);
1139 r = session_kill(session, who, signo);
1141 return bus_send_error_reply(connection, message, NULL, r);
1143 reply = dbus_message_new_method_return(message);
1147 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1152 if (!dbus_message_get_args(
1155 DBUS_TYPE_UINT32, &uid,
1156 DBUS_TYPE_INT32, &signo,
1158 return bus_send_error_reply(connection, message, &error, -EINVAL);
1160 if (signo <= 0 || signo >= _NSIG)
1161 return bus_send_error_reply(connection, message, &error, -EINVAL);
1163 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1165 return bus_send_error_reply(connection, message, &error, -ENOENT);
1167 r = user_kill(user, signo);
1169 return bus_send_error_reply(connection, message, NULL, r);
1171 reply = dbus_message_new_method_return(message);
1175 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1179 if (!dbus_message_get_args(
1182 DBUS_TYPE_STRING, &name,
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_stop(session);
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", "TerminateUser")) {
1202 if (!dbus_message_get_args(
1205 DBUS_TYPE_UINT32, &uid,
1207 return bus_send_error_reply(connection, message, &error, -EINVAL);
1209 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1211 return bus_send_error_reply(connection, message, &error, -ENOENT);
1213 r = user_stop(user);
1215 return bus_send_error_reply(connection, message, NULL, r);
1217 reply = dbus_message_new_method_return(message);
1221 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1225 if (!dbus_message_get_args(
1228 DBUS_TYPE_STRING, &name,
1230 return bus_send_error_reply(connection, message, &error, -EINVAL);
1232 seat = hashmap_get(m->seats, name);
1234 return bus_send_error_reply(connection, message, &error, -ENOENT);
1236 r = seat_stop_sessions(seat);
1238 return bus_send_error_reply(connection, message, NULL, r);
1240 reply = dbus_message_new_method_return(message);
1244 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1247 dbus_bool_t b, interactive;
1250 if (!dbus_message_get_args(
1253 DBUS_TYPE_UINT32, &uid,
1254 DBUS_TYPE_BOOLEAN, &b,
1255 DBUS_TYPE_BOOLEAN, &interactive,
1257 return bus_send_error_reply(connection, message, &error, -EINVAL);
1262 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1264 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1266 return bus_send_error_reply(connection, message, &error, r);
1268 mkdir_p("/var/lib/systemd", 0755);
1270 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1272 return bus_send_error_reply(connection, message, &error, r);
1274 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1285 return bus_send_error_reply(connection, message, &error, r);
1287 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1296 if (r < 0 && errno != ENOENT)
1297 return bus_send_error_reply(connection, message, &error, -errno);
1299 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1301 user_add_to_gc_queue(u);
1304 reply = dbus_message_new_method_return(message);
1308 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1309 const char *sysfs, *seat;
1310 dbus_bool_t interactive;
1312 if (!dbus_message_get_args(
1315 DBUS_TYPE_STRING, &seat,
1316 DBUS_TYPE_STRING, &sysfs,
1317 DBUS_TYPE_BOOLEAN, &interactive,
1319 return bus_send_error_reply(connection, message, &error, -EINVAL);
1321 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1322 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1324 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1326 return bus_send_error_reply(connection, message, &error, r);
1328 r = attach_device(m, seat, sysfs);
1330 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1332 reply = dbus_message_new_method_return(message);
1337 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1338 dbus_bool_t interactive;
1340 if (!dbus_message_get_args(
1343 DBUS_TYPE_BOOLEAN, &interactive,
1345 return bus_send_error_reply(connection, message, &error, -EINVAL);
1347 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1349 return bus_send_error_reply(connection, message, &error, r);
1351 r = flush_devices(m);
1353 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1355 reply = dbus_message_new_method_return(message);
1359 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "PowerOff") ||
1360 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Reboot")) {
1361 dbus_bool_t interactive;
1362 bool multiple_sessions;
1363 DBusMessage *forward, *freply;
1365 const char *mode = "replace";
1368 if (!dbus_message_get_args(
1371 DBUS_TYPE_BOOLEAN, &interactive,
1373 return bus_send_error_reply(connection, message, &error, -EINVAL);
1375 multiple_sessions = hashmap_size(m->sessions) > 1;
1377 if (!multiple_sessions) {
1380 /* Hmm, there's only one session, but let's
1381 * make sure it actually belongs to the user
1382 * who is asking. If not, better be safe than
1385 s = hashmap_first(m->sessions);
1389 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
1390 if (ul == (unsigned long) -1)
1391 return bus_send_error_reply(connection, message, &error, -EIO);
1393 multiple_sessions = s->user->uid != ul;
1397 if (streq(dbus_message_get_member(message), "PowerOff")) {
1398 if (multiple_sessions)
1399 action = "org.freedesktop.login1.power-off-multiple-sessions";
1401 action = "org.freedesktop.login1.power-off";
1403 name = SPECIAL_POWEROFF_TARGET;
1405 if (multiple_sessions)
1406 action = "org.freedesktop.login1.reboot-multiple-sessions";
1408 action = "org.freedesktop.login1.reboot";
1410 name = SPECIAL_REBOOT_TARGET;
1413 r = verify_polkit(connection, message, action, interactive, &error);
1415 return bus_send_error_reply(connection, message, &error, r);
1417 forward = dbus_message_new_method_call(
1418 "org.freedesktop.systemd1",
1419 "/org/freedesktop/systemd1",
1420 "org.freedesktop.systemd1.Manager",
1423 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1425 if (!dbus_message_append_args(forward,
1426 DBUS_TYPE_STRING, &name,
1427 DBUS_TYPE_STRING, &mode,
1428 DBUS_TYPE_INVALID)) {
1429 dbus_message_unref(forward);
1430 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1433 freply = dbus_connection_send_with_reply_and_block(connection, forward, -1, &error);
1434 dbus_message_unref(forward);
1437 return bus_send_error_reply(connection, message, &error, -EIO);
1439 dbus_message_unref(freply);
1441 reply = dbus_message_new_method_return(message);
1445 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1446 char *introspection = NULL;
1455 if (!(reply = dbus_message_new_method_return(message)))
1458 /* We roll our own introspection code here, instead of
1459 * relying on bus_default_message_handler() because we
1460 * need to generate our introspection string
1463 if (!(f = open_memstream(&introspection, &size)))
1466 fputs(INTROSPECTION_BEGIN, f);
1468 HASHMAP_FOREACH(seat, m->seats, i) {
1469 p = bus_path_escape(seat->id);
1472 fprintf(f, "<node name=\"seat/%s\"/>", p);
1477 HASHMAP_FOREACH(user, m->users, i)
1478 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1480 HASHMAP_FOREACH(session, m->sessions, i) {
1481 p = bus_path_escape(session->id);
1484 fprintf(f, "<node name=\"session/%s\"/>", p);
1489 fputs(INTROSPECTION_END, f);
1493 free(introspection);
1502 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1503 free(introspection);
1507 free(introspection);
1509 const BusBoundProperties bps[] = {
1510 { "org.freedesktop.login1.Manager", bus_login_manager_properties, m },
1513 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, bps);
1517 if (!dbus_connection_send(connection, reply, NULL))
1520 dbus_message_unref(reply);
1523 return DBUS_HANDLER_RESULT_HANDLED;
1527 dbus_message_unref(reply);
1529 dbus_error_free(&error);
1531 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1534 const DBusObjectPathVTable bus_manager_vtable = {
1535 .message_function = manager_message_handler
1538 DBusHandlerResult bus_message_filter(
1539 DBusConnection *connection,
1540 DBusMessage *message,
1543 Manager *m = userdata;
1550 dbus_error_init(&error);
1552 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1555 if (!dbus_message_get_args(message, &error,
1556 DBUS_TYPE_STRING, &cgroup,
1558 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1560 manager_cgroup_notify_empty(m, cgroup);
1563 dbus_error_free(&error);
1565 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1568 int manager_send_changed(Manager *manager, const char *properties) {
1574 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1578 if (!dbus_connection_send(manager->bus, m, NULL))
1585 dbus_message_unref(m);