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" \
76 " <method name=\"ActivateSession\">\n" \
77 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
79 " <method name=\"LockSession\">\n" \
80 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
82 " <method name=\"UnlockSession\">\n" \
83 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
85 " <method name=\"KillSession\">\n" \
86 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
87 " <arg name=\"who\" type=\"s\"/>\n" \
88 " <arg name=\"signal\" type=\"s\"/>\n" \
90 " <method name=\"KillUser\">\n" \
91 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
92 " <arg name=\"signal\" type=\"s\"/>\n" \
94 " <method name=\"TerminateSession\">\n" \
95 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
97 " <method name=\"TerminateUser\">\n" \
98 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
100 " <method name=\"TerminateSeat\">\n" \
101 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
103 " <method name=\"SetUserLinger\">\n" \
104 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
105 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
106 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
108 " <method name=\"AttachDevice\">\n" \
109 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
110 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
111 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
113 " <method name=\"FlushDevices\">\n" \
114 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
116 " <method name=\"PowerOff\">\n" \
117 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
119 " <method name=\"Reboot\">\n" \
120 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
122 " <signal name=\"SessionNew\">\n" \
123 " <arg name=\"id\" type=\"s\"/>\n" \
124 " <arg name=\"path\" type=\"o\"/>\n" \
126 " <signal name=\"SessionRemoved\">\n" \
127 " <arg name=\"id\" type=\"s\"/>\n" \
128 " <arg name=\"path\" type=\"o\"/>\n" \
130 " <signal name=\"UserNew\">\n" \
131 " <arg name=\"uid\" type=\"u\"/>\n" \
132 " <arg name=\"path\" type=\"o\"/>\n" \
134 " <signal name=\"UserRemoved\">\n" \
135 " <arg name=\"uid\" type=\"u\"/>\n" \
136 " <arg name=\"path\" type=\"o\"/>\n" \
138 " <signal name=\"SeatNew\">\n" \
139 " <arg name=\"id\" type=\"s\"/>\n" \
140 " <arg name=\"path\" type=\"o\"/>\n" \
142 " <signal name=\"SeatRemoved\">\n" \
143 " <arg name=\"id\" type=\"s\"/>\n" \
144 " <arg name=\"path\" type=\"o\"/>\n" \
146 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
147 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
148 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
149 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
150 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
151 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
152 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
153 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
154 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
155 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
158 #define INTROSPECTION_BEGIN \
159 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
161 BUS_MANAGER_INTERFACE \
162 BUS_PROPERTIES_INTERFACE \
164 BUS_INTROSPECTABLE_INTERFACE
166 #define INTROSPECTION_END \
169 #define INTERFACES_LIST \
170 BUS_GENERIC_INTERFACES_LIST \
171 "org.freedesktop.login1.Manager\0"
173 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
181 b = manager_get_idle_hint(m, NULL) > 0;
182 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
188 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
197 manager_get_idle_hint(m, &t);
198 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
200 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
206 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
207 Session *session = NULL;
209 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
210 uint32_t uid, leader, audit_id = 0;
211 dbus_bool_t remote, kill_processes;
212 char **controllers = NULL, **reset_controllers = NULL;
215 DBusMessageIter iter;
220 DBusMessage *reply = NULL;
227 if (!dbus_message_iter_init(message, &iter) ||
228 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
231 dbus_message_iter_get_basic(&iter, &uid);
233 if (!dbus_message_iter_next(&iter) ||
234 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
237 dbus_message_iter_get_basic(&iter, &leader);
240 !dbus_message_iter_next(&iter) ||
241 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
244 dbus_message_iter_get_basic(&iter, &service);
246 if (!dbus_message_iter_next(&iter) ||
247 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
250 dbus_message_iter_get_basic(&iter, &type);
251 t = session_type_from_string(type);
254 !dbus_message_iter_next(&iter) ||
255 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
258 dbus_message_iter_get_basic(&iter, &seat);
263 s = hashmap_get(m->seats, seat);
268 if (!dbus_message_iter_next(&iter) ||
269 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
272 dbus_message_iter_get_basic(&iter, &vtnr);
274 if (!dbus_message_iter_next(&iter) ||
275 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
278 dbus_message_iter_get_basic(&iter, &tty);
280 if (tty_is_vc(tty)) {
285 else if (s != m->vtconsole)
288 v = vtnr_from_tty(tty);
291 return v < 0 ? v : -EINVAL;
295 else if (vtnr != (uint32_t) v)
298 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
302 if (seat_is_vtconsole(s)) {
303 if (vtnr <= 0 || vtnr > 63)
311 if (!dbus_message_iter_next(&iter) ||
312 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
315 dbus_message_iter_get_basic(&iter, &display);
317 if (!dbus_message_iter_next(&iter) ||
318 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
321 dbus_message_iter_get_basic(&iter, &remote);
323 if (!dbus_message_iter_next(&iter) ||
324 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
327 dbus_message_iter_get_basic(&iter, &remote_user);
329 if (!dbus_message_iter_next(&iter) ||
330 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
333 dbus_message_iter_get_basic(&iter, &remote_host);
335 if (!dbus_message_iter_next(&iter) ||
336 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
337 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
340 r = bus_parse_strv_iter(&iter, &controllers);
344 if (strv_contains(controllers, "systemd") ||
345 !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) {
352 r = bus_parse_strv_iter(&iter, &reset_controllers);
356 if (strv_contains(reset_controllers, "systemd") ||
357 !dbus_message_iter_next(&iter) ||
358 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
363 dbus_message_iter_get_basic(&iter, &kill_processes);
365 r = manager_add_user_by_uid(m, uid, &user);
369 audit_session_from_pid(leader, &audit_id);
372 asprintf(&id, "%lu", (unsigned long) audit_id);
379 session = hashmap_get(m->sessions, id);
383 fifo_fd = session_create_fifo(session);
389 /* Session already exists, client is probably
390 * something like "su" which changes uid but
391 * is still the same audit session */
393 reply = dbus_message_new_method_return(message);
399 p = session_bus_path(session);
405 b = dbus_message_append_args(
407 DBUS_TYPE_STRING, &session->id,
408 DBUS_TYPE_OBJECT_PATH, &p,
409 DBUS_TYPE_STRING, &session->user->runtime_path,
410 DBUS_TYPE_UNIX_FD, &fifo_fd,
419 close_nointr_nofail(fifo_fd);
428 asprintf(&id, "c%lu", ++m->session_counter);
435 } while (hashmap_get(m->sessions, id));
438 r = manager_add_session(m, user, id, &session);
443 session->leader = leader;
444 session->audit_id = audit_id;
446 session->remote = remote;
447 session->controllers = controllers;
448 session->reset_controllers = reset_controllers;
449 session->kill_processes = kill_processes;
450 session->vtnr = vtnr;
452 controllers = reset_controllers = NULL;
455 session->tty = strdup(tty);
462 if (!isempty(display)) {
463 session->display = strdup(display);
464 if (!session->display) {
470 if (!isempty(remote_user)) {
471 session->remote_user = strdup(remote_user);
472 if (!session->remote_user) {
478 if (!isempty(remote_host)) {
479 session->remote_host = strdup(remote_host);
480 if (!session->remote_host) {
486 if (!isempty(service)) {
487 session->service = strdup(service);
488 if (!session->service) {
494 fifo_fd = session_create_fifo(session);
501 r = seat_attach_session(s, session);
506 r = session_start(session);
510 reply = dbus_message_new_method_return(message);
516 p = session_bus_path(session);
522 b = dbus_message_append_args(
524 DBUS_TYPE_STRING, &session->id,
525 DBUS_TYPE_OBJECT_PATH, &p,
526 DBUS_TYPE_STRING, &session->user->runtime_path,
527 DBUS_TYPE_UNIX_FD, &fifo_fd,
536 close_nointr_nofail(fifo_fd);
542 strv_free(controllers);
543 strv_free(reset_controllers);
546 session_add_to_gc_queue(session);
549 user_add_to_gc_queue(user);
552 close_nointr_nofail(fifo_fd);
555 dbus_message_unref(reply);
560 static int trigger_device(Manager *m, struct udev_device *d) {
561 struct udev_enumerate *e;
562 struct udev_list_entry *first, *item;
567 e = udev_enumerate_new(m->udev);
574 if (udev_enumerate_add_match_parent(e, d) < 0) {
581 if (udev_enumerate_scan_devices(e) < 0) {
586 first = udev_enumerate_get_list_entry(e);
587 udev_list_entry_foreach(item, first) {
591 p = udev_list_entry_get_name(item);
593 t = strappend(p, "/uevent");
599 write_one_line_file(t, "change");
607 udev_enumerate_unref(e);
612 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
613 struct udev_device *d;
614 char *rule = NULL, *file = NULL;
615 const char *id_for_seat;
622 d = udev_device_new_from_syspath(m->udev, sysfs);
626 if (!udev_device_has_tag(d, "seat")) {
631 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
637 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
642 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
647 mkdir_p("/etc/udev/rules.d", 0755);
648 r = write_one_line_file_atomic(file, rule);
652 r = trigger_device(m, d);
659 udev_device_unref(d);
664 static int flush_devices(Manager *m) {
669 d = opendir("/etc/udev/rules.d");
672 log_warning("Failed to open /etc/udev/rules.d: %m");
676 while ((de = readdir(d))) {
678 if (!dirent_is_file(de))
681 if (!startswith(de->d_name, "72-seat-"))
684 if (!endswith(de->d_name, ".rules"))
687 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
688 log_warning("Failed to unlink %s: %m", de->d_name);
694 return trigger_device(m, NULL);
697 static DBusHandlerResult manager_message_handler(
698 DBusConnection *connection,
699 DBusMessage *message,
702 Manager *m = userdata;
704 const BusProperty properties[] = {
705 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
706 { "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
707 { "org.freedesktop.login1.Manager", "ResetControllers", bus_property_append_strv, "as", m->reset_controllers },
708 { "org.freedesktop.login1.Manager", "NAutoVTs", bus_property_append_unsigned, "u", &m->n_autovts },
709 { "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
710 { "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
711 { "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
712 { "org.freedesktop.login1.Manager", "IdleHint", bus_manager_append_idle_hint, "b", m },
713 { "org.freedesktop.login1.Manager", "IdleSinceHint", bus_manager_append_idle_hint_since, "t", m },
714 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
715 { NULL, NULL, NULL, NULL, NULL }
719 DBusMessage *reply = NULL;
726 dbus_error_init(&error);
728 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
734 if (!dbus_message_get_args(
737 DBUS_TYPE_STRING, &name,
739 return bus_send_error_reply(connection, message, &error, -EINVAL);
741 session = hashmap_get(m->sessions, name);
743 return bus_send_error_reply(connection, message, &error, -ENOENT);
745 reply = dbus_message_new_method_return(message);
749 p = session_bus_path(session);
753 b = dbus_message_append_args(
755 DBUS_TYPE_OBJECT_PATH, &p,
762 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
768 if (!dbus_message_get_args(
771 DBUS_TYPE_UINT32, &uid,
773 return bus_send_error_reply(connection, message, &error, -EINVAL);
775 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
777 return bus_send_error_reply(connection, message, &error, -ENOENT);
779 reply = dbus_message_new_method_return(message);
783 p = user_bus_path(user);
787 b = dbus_message_append_args(
789 DBUS_TYPE_OBJECT_PATH, &p,
796 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
802 if (!dbus_message_get_args(
805 DBUS_TYPE_STRING, &name,
807 return bus_send_error_reply(connection, message, &error, -EINVAL);
809 seat = hashmap_get(m->seats, name);
811 return bus_send_error_reply(connection, message, &error, -ENOENT);
813 reply = dbus_message_new_method_return(message);
817 p = seat_bus_path(seat);
821 b = dbus_message_append_args(
823 DBUS_TYPE_OBJECT_PATH, &p,
830 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
834 DBusMessageIter iter, sub;
835 const char *empty = "";
837 reply = dbus_message_new_method_return(message);
841 dbus_message_iter_init_append(reply, &iter);
843 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
846 HASHMAP_FOREACH(session, m->sessions, i) {
847 DBusMessageIter sub2;
850 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
853 uid = session->user->uid;
855 p = session_bus_path(session);
859 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
860 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
861 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
862 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
863 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
870 if (!dbus_message_iter_close_container(&sub, &sub2))
874 if (!dbus_message_iter_close_container(&iter, &sub))
877 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
881 DBusMessageIter iter, sub;
883 reply = dbus_message_new_method_return(message);
887 dbus_message_iter_init_append(reply, &iter);
889 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
892 HASHMAP_FOREACH(user, m->users, i) {
893 DBusMessageIter sub2;
896 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
901 p = user_bus_path(user);
905 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
906 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
907 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
914 if (!dbus_message_iter_close_container(&sub, &sub2))
918 if (!dbus_message_iter_close_container(&iter, &sub))
921 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
925 DBusMessageIter iter, sub;
927 reply = dbus_message_new_method_return(message);
931 dbus_message_iter_init_append(reply, &iter);
933 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
936 HASHMAP_FOREACH(seat, m->seats, i) {
937 DBusMessageIter sub2;
939 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
942 p = seat_bus_path(seat);
946 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
947 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
954 if (!dbus_message_iter_close_container(&sub, &sub2))
958 if (!dbus_message_iter_close_container(&iter, &sub))
961 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
963 r = bus_manager_create_session(m, message, &reply);
968 return bus_send_error_reply(connection, message, &error, r);
970 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
974 if (!dbus_message_get_args(
977 DBUS_TYPE_STRING, &name,
979 return bus_send_error_reply(connection, message, &error, -EINVAL);
981 session = hashmap_get(m->sessions, name);
983 return bus_send_error_reply(connection, message, &error, -ENOENT);
985 r = session_activate(session);
987 return bus_send_error_reply(connection, message, NULL, r);
989 reply = dbus_message_new_method_return(message);
993 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
994 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
998 if (!dbus_message_get_args(
1001 DBUS_TYPE_STRING, &name,
1003 return bus_send_error_reply(connection, message, &error, -EINVAL);
1005 session = hashmap_get(m->sessions, name);
1007 return bus_send_error_reply(connection, message, &error, -ENOENT);
1009 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1012 reply = dbus_message_new_method_return(message);
1016 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1023 if (!dbus_message_get_args(
1026 DBUS_TYPE_STRING, &name,
1027 DBUS_TYPE_STRING, &swho,
1028 DBUS_TYPE_INT32, &signo,
1030 return bus_send_error_reply(connection, message, &error, -EINVAL);
1035 who = kill_who_from_string(swho);
1037 return bus_send_error_reply(connection, message, &error, -EINVAL);
1040 if (signo <= 0 || signo >= _NSIG)
1041 return bus_send_error_reply(connection, message, &error, -EINVAL);
1043 session = hashmap_get(m->sessions, name);
1045 return bus_send_error_reply(connection, message, &error, -ENOENT);
1047 r = session_kill(session, who, signo);
1049 return bus_send_error_reply(connection, message, NULL, r);
1051 reply = dbus_message_new_method_return(message);
1055 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1060 if (!dbus_message_get_args(
1063 DBUS_TYPE_UINT32, &uid,
1064 DBUS_TYPE_INT32, &signo,
1066 return bus_send_error_reply(connection, message, &error, -EINVAL);
1068 if (signo <= 0 || signo >= _NSIG)
1069 return bus_send_error_reply(connection, message, &error, -EINVAL);
1071 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1073 return bus_send_error_reply(connection, message, &error, -ENOENT);
1075 r = user_kill(user, signo);
1077 return bus_send_error_reply(connection, message, NULL, r);
1079 reply = dbus_message_new_method_return(message);
1083 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1087 if (!dbus_message_get_args(
1090 DBUS_TYPE_STRING, &name,
1092 return bus_send_error_reply(connection, message, &error, -EINVAL);
1094 session = hashmap_get(m->sessions, name);
1096 return bus_send_error_reply(connection, message, &error, -ENOENT);
1098 r = session_stop(session);
1100 return bus_send_error_reply(connection, message, NULL, r);
1102 reply = dbus_message_new_method_return(message);
1106 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1110 if (!dbus_message_get_args(
1113 DBUS_TYPE_UINT32, &uid,
1115 return bus_send_error_reply(connection, message, &error, -EINVAL);
1117 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1119 return bus_send_error_reply(connection, message, &error, -ENOENT);
1121 r = user_stop(user);
1123 return bus_send_error_reply(connection, message, NULL, r);
1125 reply = dbus_message_new_method_return(message);
1129 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1133 if (!dbus_message_get_args(
1136 DBUS_TYPE_STRING, &name,
1138 return bus_send_error_reply(connection, message, &error, -EINVAL);
1140 seat = hashmap_get(m->seats, name);
1142 return bus_send_error_reply(connection, message, &error, -ENOENT);
1144 r = seat_stop_sessions(seat);
1146 return bus_send_error_reply(connection, message, NULL, r);
1148 reply = dbus_message_new_method_return(message);
1152 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1155 dbus_bool_t b, interactive;
1158 if (!dbus_message_get_args(
1161 DBUS_TYPE_UINT32, &uid,
1162 DBUS_TYPE_BOOLEAN, &b,
1163 DBUS_TYPE_BOOLEAN, &interactive,
1165 return bus_send_error_reply(connection, message, &error, -EINVAL);
1170 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1172 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1174 return bus_send_error_reply(connection, message, &error, r);
1176 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1178 return bus_send_error_reply(connection, message, &error, r);
1180 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1191 return bus_send_error_reply(connection, message, &error, r);
1193 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1202 if (r < 0 && errno != ENOENT)
1203 return bus_send_error_reply(connection, message, &error, -errno);
1205 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1207 user_add_to_gc_queue(u);
1210 reply = dbus_message_new_method_return(message);
1214 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1215 const char *sysfs, *seat;
1216 dbus_bool_t interactive;
1218 if (!dbus_message_get_args(
1221 DBUS_TYPE_STRING, &seat,
1222 DBUS_TYPE_STRING, &sysfs,
1223 DBUS_TYPE_BOOLEAN, &interactive,
1225 return bus_send_error_reply(connection, message, &error, -EINVAL);
1227 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1228 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1230 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1232 return bus_send_error_reply(connection, message, &error, r);
1234 r = attach_device(m, seat, sysfs);
1236 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1238 reply = dbus_message_new_method_return(message);
1243 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1244 dbus_bool_t interactive;
1246 if (!dbus_message_get_args(
1249 DBUS_TYPE_BOOLEAN, &interactive,
1251 return bus_send_error_reply(connection, message, &error, -EINVAL);
1253 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1255 return bus_send_error_reply(connection, message, &error, r);
1257 r = flush_devices(m);
1259 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1261 reply = dbus_message_new_method_return(message);
1265 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "PowerOff") ||
1266 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Reboot")) {
1267 dbus_bool_t interactive;
1268 bool multiple_sessions;
1269 DBusMessage *forward, *freply;
1271 const char *mode = "replace";
1274 if (!dbus_message_get_args(
1277 DBUS_TYPE_BOOLEAN, &interactive,
1279 return bus_send_error_reply(connection, message, &error, -EINVAL);
1281 multiple_sessions = hashmap_size(m->sessions) > 1;
1283 if (!multiple_sessions) {
1286 /* Hmm, there's only one session, but let's
1287 * make sure it actually belongs to the user
1288 * who is asking. If not, better be safe than
1291 s = hashmap_first(m->sessions);
1295 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
1296 if (ul == (unsigned long) -1)
1297 return bus_send_error_reply(connection, message, &error, -EIO);
1299 multiple_sessions = s->user->uid != ul;
1303 if (streq(dbus_message_get_member(message), "PowerOff")) {
1304 if (multiple_sessions)
1305 action = "org.freedesktop.login1.power-off-multiple-sessions";
1307 action = "org.freedesktop.login1.power-off";
1309 name = SPECIAL_POWEROFF_TARGET;
1311 if (multiple_sessions)
1312 action = "org.freedesktop.login1.reboot-multiple-sessions";
1314 action = "org.freedesktop.login1.reboot";
1316 name = SPECIAL_REBOOT_TARGET;
1319 r = verify_polkit(connection, message, action, interactive, &error);
1321 return bus_send_error_reply(connection, message, &error, r);
1323 forward = dbus_message_new_method_call(
1324 "org.freedesktop.systemd1",
1325 "/org/freedesktop/systemd1",
1326 "org.freedesktop.systemd1.Manager",
1329 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1331 if (!dbus_message_append_args(forward,
1332 DBUS_TYPE_STRING, &name,
1333 DBUS_TYPE_STRING, &mode,
1334 DBUS_TYPE_INVALID)) {
1335 dbus_message_unref(forward);
1336 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1339 freply = dbus_connection_send_with_reply_and_block(connection, forward, -1, &error);
1340 dbus_message_unref(forward);
1343 return bus_send_error_reply(connection, message, &error, -EIO);
1345 dbus_message_unref(freply);
1347 reply = dbus_message_new_method_return(message);
1351 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1352 char *introspection = NULL;
1361 if (!(reply = dbus_message_new_method_return(message)))
1364 /* We roll our own introspection code here, instead of
1365 * relying on bus_default_message_handler() because we
1366 * need to generate our introspection string
1369 if (!(f = open_memstream(&introspection, &size)))
1372 fputs(INTROSPECTION_BEGIN, f);
1374 HASHMAP_FOREACH(seat, m->seats, i) {
1375 p = bus_path_escape(seat->id);
1378 fprintf(f, "<node name=\"seat/%s\"/>", p);
1383 HASHMAP_FOREACH(user, m->users, i)
1384 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1386 HASHMAP_FOREACH(session, m->sessions, i) {
1387 p = bus_path_escape(session->id);
1390 fprintf(f, "<node name=\"session/%s\"/>", p);
1395 fputs(INTROSPECTION_END, f);
1399 free(introspection);
1408 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1409 free(introspection);
1413 free(introspection);
1415 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1418 if (!dbus_connection_send(connection, reply, NULL))
1421 dbus_message_unref(reply);
1424 return DBUS_HANDLER_RESULT_HANDLED;
1428 dbus_message_unref(reply);
1430 dbus_error_free(&error);
1432 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1435 const DBusObjectPathVTable bus_manager_vtable = {
1436 .message_function = manager_message_handler
1439 DBusHandlerResult bus_message_filter(
1440 DBusConnection *connection,
1441 DBusMessage *message,
1444 Manager *m = userdata;
1451 dbus_error_init(&error);
1453 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1456 if (!dbus_message_get_args(message, &error,
1457 DBUS_TYPE_STRING, &cgroup,
1459 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1461 manager_cgroup_notify_empty(m, cgroup);
1464 dbus_error_free(&error);
1466 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1469 int manager_send_changed(Manager *manager, const char *properties) {
1475 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1479 if (!dbus_connection_send(manager->bus, m, NULL))
1486 dbus_message_unref(m);