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=\"LockSession\">\n" \
79 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
81 " <method name=\"UnlockSession\">\n" \
82 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
84 " <method name=\"KillSession\">\n" \
85 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
86 " <arg name=\"who\" type=\"s\"/>\n" \
87 " <arg name=\"signal\" type=\"s\"/>\n" \
89 " <method name=\"KillUser\">\n" \
90 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
91 " <arg name=\"signal\" type=\"s\"/>\n" \
93 " <method name=\"TerminateSession\">\n" \
94 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
96 " <method name=\"TerminateUser\">\n" \
97 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
99 " <method name=\"TerminateSeat\">\n" \
100 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
102 " <method name=\"SetUserLinger\">\n" \
103 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
104 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
105 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
107 " <method name=\"AttachDevice\">\n" \
108 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
109 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
110 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
112 " <method name=\"FlushDevices\">\n" \
113 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
115 " <signal name=\"SessionNew\">\n" \
116 " <arg name=\"id\" type=\"s\"/>\n" \
117 " <arg name=\"path\" type=\"o\"/>\n" \
119 " <signal name=\"SessionRemoved\">\n" \
120 " <arg name=\"id\" type=\"s\"/>\n" \
121 " <arg name=\"path\" type=\"o\"/>\n" \
123 " <signal name=\"UserNew\">\n" \
124 " <arg name=\"uid\" type=\"u\"/>\n" \
125 " <arg name=\"path\" type=\"o\"/>\n" \
127 " <signal name=\"UserRemoved\">\n" \
128 " <arg name=\"uid\" type=\"u\"/>\n" \
129 " <arg name=\"path\" type=\"o\"/>\n" \
131 " <signal name=\"SeatNew\">\n" \
132 " <arg name=\"id\" type=\"s\"/>\n" \
133 " <arg name=\"path\" type=\"o\"/>\n" \
135 " <signal name=\"SeatRemoved\">\n" \
136 " <arg name=\"id\" type=\"s\"/>\n" \
137 " <arg name=\"path\" type=\"o\"/>\n" \
139 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
140 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
141 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
142 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
143 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
144 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
145 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
146 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
147 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
148 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
151 #define INTROSPECTION_BEGIN \
152 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
154 BUS_MANAGER_INTERFACE \
155 BUS_PROPERTIES_INTERFACE \
157 BUS_INTROSPECTABLE_INTERFACE
159 #define INTROSPECTION_END \
162 #define INTERFACES_LIST \
163 BUS_GENERIC_INTERFACES_LIST \
164 "org.freedesktop.login1.Manager\0"
166 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
174 b = manager_get_idle_hint(m, NULL) > 0;
175 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
181 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
190 manager_get_idle_hint(m, &t);
191 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
193 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
199 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
200 Session *session = NULL;
202 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
203 uint32_t uid, leader, audit_id = 0;
204 dbus_bool_t remote, kill_processes;
205 char **controllers = NULL, **reset_controllers = NULL;
208 DBusMessageIter iter;
213 DBusMessage *reply = NULL;
220 if (!dbus_message_iter_init(message, &iter) ||
221 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
224 dbus_message_iter_get_basic(&iter, &uid);
226 if (!dbus_message_iter_next(&iter) ||
227 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
230 dbus_message_iter_get_basic(&iter, &leader);
233 !dbus_message_iter_next(&iter) ||
234 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
237 dbus_message_iter_get_basic(&iter, &service);
239 if (!dbus_message_iter_next(&iter) ||
240 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
243 dbus_message_iter_get_basic(&iter, &type);
244 t = session_type_from_string(type);
247 !dbus_message_iter_next(&iter) ||
248 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
251 dbus_message_iter_get_basic(&iter, &seat);
256 s = hashmap_get(m->seats, seat);
261 if (!dbus_message_iter_next(&iter) ||
262 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
265 dbus_message_iter_get_basic(&iter, &vtnr);
267 if (!dbus_message_iter_next(&iter) ||
268 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
271 dbus_message_iter_get_basic(&iter, &tty);
273 if (tty_is_vc(tty)) {
278 else if (s != m->vtconsole)
281 v = vtnr_from_tty(tty);
284 return v < 0 ? v : -EINVAL;
288 else if (vtnr != (uint32_t) v)
291 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
295 if (seat_is_vtconsole(s)) {
296 if (vtnr <= 0 || vtnr > 63)
304 if (!dbus_message_iter_next(&iter) ||
305 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
308 dbus_message_iter_get_basic(&iter, &display);
310 if (!dbus_message_iter_next(&iter) ||
311 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
314 dbus_message_iter_get_basic(&iter, &remote);
316 if (!dbus_message_iter_next(&iter) ||
317 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
320 dbus_message_iter_get_basic(&iter, &remote_user);
322 if (!dbus_message_iter_next(&iter) ||
323 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
326 dbus_message_iter_get_basic(&iter, &remote_host);
328 if (!dbus_message_iter_next(&iter) ||
329 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
330 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
333 r = bus_parse_strv_iter(&iter, &controllers);
337 if (strv_contains(controllers, "systemd") ||
338 !dbus_message_iter_next(&iter) ||
339 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
340 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
345 r = bus_parse_strv_iter(&iter, &reset_controllers);
349 if (strv_contains(reset_controllers, "systemd") ||
350 !dbus_message_iter_next(&iter) ||
351 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
356 dbus_message_iter_get_basic(&iter, &kill_processes);
358 r = manager_add_user_by_uid(m, uid, &user);
362 audit_session_from_pid(leader, &audit_id);
365 asprintf(&id, "%lu", (unsigned long) audit_id);
372 session = hashmap_get(m->sessions, id);
376 fifo_fd = session_create_fifo(session);
382 /* Session already exists, client is probably
383 * something like "su" which changes uid but
384 * is still the same audit session */
386 reply = dbus_message_new_method_return(message);
392 p = session_bus_path(session);
398 b = dbus_message_append_args(
400 DBUS_TYPE_STRING, &session->id,
401 DBUS_TYPE_OBJECT_PATH, &p,
402 DBUS_TYPE_STRING, &session->user->runtime_path,
403 DBUS_TYPE_UNIX_FD, &fifo_fd,
412 close_nointr_nofail(fifo_fd);
421 asprintf(&id, "c%lu", ++m->session_counter);
428 } while (hashmap_get(m->sessions, id));
431 r = manager_add_session(m, user, id, &session);
436 session->leader = leader;
437 session->audit_id = audit_id;
439 session->remote = remote;
440 session->controllers = controllers;
441 session->reset_controllers = reset_controllers;
442 session->kill_processes = kill_processes;
443 session->vtnr = vtnr;
445 controllers = reset_controllers = NULL;
448 session->tty = strdup(tty);
455 if (!isempty(display)) {
456 session->display = strdup(display);
457 if (!session->display) {
463 if (!isempty(remote_user)) {
464 session->remote_user = strdup(remote_user);
465 if (!session->remote_user) {
471 if (!isempty(remote_host)) {
472 session->remote_host = strdup(remote_host);
473 if (!session->remote_host) {
479 if (!isempty(service)) {
480 session->service = strdup(service);
481 if (!session->service) {
487 fifo_fd = session_create_fifo(session);
494 r = seat_attach_session(s, session);
499 r = session_start(session);
503 reply = dbus_message_new_method_return(message);
509 p = session_bus_path(session);
515 b = dbus_message_append_args(
517 DBUS_TYPE_STRING, &session->id,
518 DBUS_TYPE_OBJECT_PATH, &p,
519 DBUS_TYPE_STRING, &session->user->runtime_path,
520 DBUS_TYPE_UNIX_FD, &fifo_fd,
529 close_nointr_nofail(fifo_fd);
535 strv_free(controllers);
536 strv_free(reset_controllers);
539 session_add_to_gc_queue(session);
542 user_add_to_gc_queue(user);
545 close_nointr_nofail(fifo_fd);
548 dbus_message_unref(reply);
553 static int trigger_device(Manager *m, struct udev_device *d) {
554 struct udev_enumerate *e;
555 struct udev_list_entry *first, *item;
560 e = udev_enumerate_new(m->udev);
567 if (udev_enumerate_add_match_parent(e, d) < 0) {
574 if (udev_enumerate_scan_devices(e) < 0) {
579 first = udev_enumerate_get_list_entry(e);
580 udev_list_entry_foreach(item, first) {
584 p = udev_list_entry_get_name(item);
586 t = strappend(p, "/uevent");
592 write_one_line_file(t, "change");
600 udev_enumerate_unref(e);
605 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
606 struct udev_device *d;
607 char *rule = NULL, *file = NULL;
608 const char *id_for_seat;
615 d = udev_device_new_from_syspath(m->udev, sysfs);
619 if (!udev_device_has_tag(d, "seat")) {
624 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
630 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
635 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
640 mkdir_p("/etc/udev/rules.d", 0755);
641 r = write_one_line_file_atomic(file, rule);
645 r = trigger_device(m, d);
652 udev_device_unref(d);
657 static int flush_devices(Manager *m) {
662 d = opendir("/etc/udev/rules.d");
665 log_warning("Failed to open /etc/udev/rules.d: %m");
669 while ((de = readdir(d))) {
671 if (!dirent_is_file(de))
674 if (!startswith(de->d_name, "72-seat-"))
677 if (!endswith(de->d_name, ".rules"))
680 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
681 log_warning("Failed to unlink %s: %m", de->d_name);
687 return trigger_device(m, NULL);
690 static DBusHandlerResult manager_message_handler(
691 DBusConnection *connection,
692 DBusMessage *message,
695 Manager *m = userdata;
697 const BusProperty properties[] = {
698 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
699 { "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
700 { "org.freedesktop.login1.Manager", "ResetControllers", bus_property_append_strv, "as", m->reset_controllers },
701 { "org.freedesktop.login1.Manager", "NAutoVTs", bus_property_append_unsigned, "u", &m->n_autovts },
702 { "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
703 { "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
704 { "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
705 { "org.freedesktop.login1.Manager", "IdleHint", bus_manager_append_idle_hint, "b", m },
706 { "org.freedesktop.login1.Manager", "IdleSinceHint", bus_manager_append_idle_hint_since, "t", m },
707 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
708 { NULL, NULL, NULL, NULL, NULL }
712 DBusMessage *reply = NULL;
719 dbus_error_init(&error);
721 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
727 if (!dbus_message_get_args(
730 DBUS_TYPE_STRING, &name,
732 return bus_send_error_reply(connection, message, &error, -EINVAL);
734 session = hashmap_get(m->sessions, name);
736 return bus_send_error_reply(connection, message, &error, -ENOENT);
738 reply = dbus_message_new_method_return(message);
742 p = session_bus_path(session);
746 b = dbus_message_append_args(
748 DBUS_TYPE_OBJECT_PATH, &p,
755 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
761 if (!dbus_message_get_args(
764 DBUS_TYPE_UINT32, &uid,
766 return bus_send_error_reply(connection, message, &error, -EINVAL);
768 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
770 return bus_send_error_reply(connection, message, &error, -ENOENT);
772 reply = dbus_message_new_method_return(message);
776 p = user_bus_path(user);
780 b = dbus_message_append_args(
782 DBUS_TYPE_OBJECT_PATH, &p,
789 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
795 if (!dbus_message_get_args(
798 DBUS_TYPE_STRING, &name,
800 return bus_send_error_reply(connection, message, &error, -EINVAL);
802 seat = hashmap_get(m->seats, name);
804 return bus_send_error_reply(connection, message, &error, -ENOENT);
806 reply = dbus_message_new_method_return(message);
810 p = seat_bus_path(seat);
814 b = dbus_message_append_args(
816 DBUS_TYPE_OBJECT_PATH, &p,
823 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
827 DBusMessageIter iter, sub;
828 const char *empty = "";
830 reply = dbus_message_new_method_return(message);
834 dbus_message_iter_init_append(reply, &iter);
836 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
839 HASHMAP_FOREACH(session, m->sessions, i) {
840 DBusMessageIter sub2;
843 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
846 uid = session->user->uid;
848 p = session_bus_path(session);
852 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
853 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
854 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
855 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
856 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
863 if (!dbus_message_iter_close_container(&sub, &sub2))
867 if (!dbus_message_iter_close_container(&iter, &sub))
870 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
874 DBusMessageIter iter, sub;
876 reply = dbus_message_new_method_return(message);
880 dbus_message_iter_init_append(reply, &iter);
882 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
885 HASHMAP_FOREACH(user, m->users, i) {
886 DBusMessageIter sub2;
889 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
894 p = user_bus_path(user);
898 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
899 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
900 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
907 if (!dbus_message_iter_close_container(&sub, &sub2))
911 if (!dbus_message_iter_close_container(&iter, &sub))
914 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
918 DBusMessageIter iter, sub;
920 reply = dbus_message_new_method_return(message);
924 dbus_message_iter_init_append(reply, &iter);
926 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
929 HASHMAP_FOREACH(seat, m->seats, i) {
930 DBusMessageIter sub2;
932 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
935 p = seat_bus_path(seat);
939 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
940 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
947 if (!dbus_message_iter_close_container(&sub, &sub2))
951 if (!dbus_message_iter_close_container(&iter, &sub))
954 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
956 r = bus_manager_create_session(m, message, &reply);
961 return bus_send_error_reply(connection, message, &error, r);
963 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
967 if (!dbus_message_get_args(
970 DBUS_TYPE_STRING, &name,
972 return bus_send_error_reply(connection, message, &error, -EINVAL);
974 session = hashmap_get(m->sessions, name);
976 return bus_send_error_reply(connection, message, &error, -ENOENT);
978 r = session_activate(session);
980 return bus_send_error_reply(connection, message, NULL, r);
982 reply = dbus_message_new_method_return(message);
986 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
987 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
991 if (!dbus_message_get_args(
994 DBUS_TYPE_STRING, &name,
996 return bus_send_error_reply(connection, message, &error, -EINVAL);
998 session = hashmap_get(m->sessions, name);
1000 return bus_send_error_reply(connection, message, &error, -ENOENT);
1002 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1005 reply = dbus_message_new_method_return(message);
1009 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1016 if (!dbus_message_get_args(
1019 DBUS_TYPE_STRING, &name,
1020 DBUS_TYPE_STRING, &swho,
1021 DBUS_TYPE_INT32, &signo,
1023 return bus_send_error_reply(connection, message, &error, -EINVAL);
1028 who = kill_who_from_string(swho);
1030 return bus_send_error_reply(connection, message, &error, -EINVAL);
1033 if (signo <= 0 || signo >= _NSIG)
1034 return bus_send_error_reply(connection, message, &error, -EINVAL);
1036 session = hashmap_get(m->sessions, name);
1038 return bus_send_error_reply(connection, message, &error, -ENOENT);
1040 r = session_kill(session, who, signo);
1042 return bus_send_error_reply(connection, message, NULL, r);
1044 reply = dbus_message_new_method_return(message);
1048 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1053 if (!dbus_message_get_args(
1056 DBUS_TYPE_UINT32, &uid,
1057 DBUS_TYPE_INT32, &signo,
1059 return bus_send_error_reply(connection, message, &error, -EINVAL);
1061 if (signo <= 0 || signo >= _NSIG)
1062 return bus_send_error_reply(connection, message, &error, -EINVAL);
1064 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1066 return bus_send_error_reply(connection, message, &error, -ENOENT);
1068 r = user_kill(user, signo);
1070 return bus_send_error_reply(connection, message, NULL, r);
1072 reply = dbus_message_new_method_return(message);
1076 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1080 if (!dbus_message_get_args(
1083 DBUS_TYPE_STRING, &name,
1085 return bus_send_error_reply(connection, message, &error, -EINVAL);
1087 session = hashmap_get(m->sessions, name);
1089 return bus_send_error_reply(connection, message, &error, -ENOENT);
1091 r = session_stop(session);
1093 return bus_send_error_reply(connection, message, NULL, r);
1095 reply = dbus_message_new_method_return(message);
1099 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1103 if (!dbus_message_get_args(
1106 DBUS_TYPE_UINT32, &uid,
1108 return bus_send_error_reply(connection, message, &error, -EINVAL);
1110 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1112 return bus_send_error_reply(connection, message, &error, -ENOENT);
1114 r = user_stop(user);
1116 return bus_send_error_reply(connection, message, NULL, r);
1118 reply = dbus_message_new_method_return(message);
1122 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1126 if (!dbus_message_get_args(
1129 DBUS_TYPE_STRING, &name,
1131 return bus_send_error_reply(connection, message, &error, -EINVAL);
1133 seat = hashmap_get(m->seats, name);
1135 return bus_send_error_reply(connection, message, &error, -ENOENT);
1137 r = seat_stop_sessions(seat);
1139 return bus_send_error_reply(connection, message, NULL, r);
1141 reply = dbus_message_new_method_return(message);
1145 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1148 dbus_bool_t b, interactive;
1151 if (!dbus_message_get_args(
1154 DBUS_TYPE_UINT32, &uid,
1155 DBUS_TYPE_BOOLEAN, &b,
1156 DBUS_TYPE_BOOLEAN, &interactive,
1158 return bus_send_error_reply(connection, message, &error, -EINVAL);
1163 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1165 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1167 return bus_send_error_reply(connection, message, &error, r);
1169 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1171 return bus_send_error_reply(connection, message, &error, r);
1173 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1184 return bus_send_error_reply(connection, message, &error, r);
1186 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1195 if (r < 0 && errno != ENOENT)
1196 return bus_send_error_reply(connection, message, &error, -errno);
1198 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1200 user_add_to_gc_queue(u);
1203 reply = dbus_message_new_method_return(message);
1207 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1208 const char *sysfs, *seat;
1209 dbus_bool_t interactive;
1211 if (!dbus_message_get_args(
1214 DBUS_TYPE_STRING, &seat,
1215 DBUS_TYPE_STRING, &sysfs,
1216 DBUS_TYPE_BOOLEAN, &interactive,
1218 return bus_send_error_reply(connection, message, &error, -EINVAL);
1220 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1221 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1223 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1225 return bus_send_error_reply(connection, message, &error, r);
1227 r = attach_device(m, seat, sysfs);
1229 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1231 reply = dbus_message_new_method_return(message);
1236 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1237 dbus_bool_t interactive;
1239 if (!dbus_message_get_args(
1242 DBUS_TYPE_BOOLEAN, &interactive,
1244 return bus_send_error_reply(connection, message, &error, -EINVAL);
1246 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1248 return bus_send_error_reply(connection, message, &error, r);
1250 r = flush_devices(m);
1252 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1254 reply = dbus_message_new_method_return(message);
1258 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1259 char *introspection = NULL;
1268 if (!(reply = dbus_message_new_method_return(message)))
1271 /* We roll our own introspection code here, instead of
1272 * relying on bus_default_message_handler() because we
1273 * need to generate our introspection string
1276 if (!(f = open_memstream(&introspection, &size)))
1279 fputs(INTROSPECTION_BEGIN, f);
1281 HASHMAP_FOREACH(seat, m->seats, i) {
1282 p = bus_path_escape(seat->id);
1285 fprintf(f, "<node name=\"seat/%s\"/>", p);
1290 HASHMAP_FOREACH(user, m->users, i)
1291 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1293 HASHMAP_FOREACH(session, m->sessions, i) {
1294 p = bus_path_escape(session->id);
1297 fprintf(f, "<node name=\"session/%s\"/>", p);
1302 fputs(INTROSPECTION_END, f);
1306 free(introspection);
1315 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1316 free(introspection);
1320 free(introspection);
1322 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1325 if (!dbus_connection_send(connection, reply, NULL))
1328 dbus_message_unref(reply);
1331 return DBUS_HANDLER_RESULT_HANDLED;
1335 dbus_message_unref(reply);
1337 dbus_error_free(&error);
1339 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1342 const DBusObjectPathVTable bus_manager_vtable = {
1343 .message_function = manager_message_handler
1346 DBusHandlerResult bus_message_filter(
1347 DBusConnection *connection,
1348 DBusMessage *message,
1351 Manager *m = userdata;
1358 dbus_error_init(&error);
1360 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1363 if (!dbus_message_get_args(message, &error,
1364 DBUS_TYPE_STRING, &cgroup,
1366 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1368 manager_cgroup_notify_empty(m, cgroup);
1371 dbus_error_free(&error);
1373 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1376 int manager_send_changed(Manager *manager, const char *properties) {
1382 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1386 if (!dbus_connection_send(manager->bus, m, NULL))
1393 dbus_message_unref(m);