1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "dbus-common.h"
33 #define BUS_MANAGER_INTERFACE \
34 " <interface name=\"org.freedesktop.login1.Manager\">\n" \
35 " <method name=\"GetSession\">\n" \
36 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
37 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
39 " <method name=\"GetUser\">\n" \
40 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
41 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
43 " <method name=\"GetSeat\">\n" \
44 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
45 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
47 " <method name=\"ListSessions\">\n" \
48 " <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
50 " <method name=\"ListUsers\">\n" \
51 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
53 " <method name=\"ListSeats\">\n" \
54 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
56 " <method name=\"CreateSession\">\n" \
57 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
58 " <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
59 " <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n" \
60 " <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" \
61 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
62 " <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n" \
63 " <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
65 " <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
66 " <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
68 " <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
69 " <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
70 " <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
71 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
72 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
73 " <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
74 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
75 " <arg name=\"seat\" type=\"s\" direction=\"out\"/>\n" \
76 " <arg name=\"vtnr\" type=\"u\" direction=\"out\"/>\n" \
78 " <method name=\"ActivateSession\">\n" \
79 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
81 " <method name=\"LockSession\">\n" \
82 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
84 " <method name=\"UnlockSession\">\n" \
85 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
87 " <method name=\"KillSession\">\n" \
88 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
89 " <arg name=\"who\" type=\"s\"/>\n" \
90 " <arg name=\"signal\" type=\"s\"/>\n" \
92 " <method name=\"KillUser\">\n" \
93 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
94 " <arg name=\"signal\" type=\"s\"/>\n" \
96 " <method name=\"TerminateSession\">\n" \
97 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
99 " <method name=\"TerminateUser\">\n" \
100 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
102 " <method name=\"TerminateSeat\">\n" \
103 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
105 " <method name=\"SetUserLinger\">\n" \
106 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
107 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
108 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
110 " <method name=\"AttachDevice\">\n" \
111 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
112 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
113 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
115 " <method name=\"FlushDevices\">\n" \
116 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
118 " <method name=\"PowerOff\">\n" \
119 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
121 " <method name=\"Reboot\">\n" \
122 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
124 " <signal name=\"SessionNew\">\n" \
125 " <arg name=\"id\" type=\"s\"/>\n" \
126 " <arg name=\"path\" type=\"o\"/>\n" \
128 " <signal name=\"SessionRemoved\">\n" \
129 " <arg name=\"id\" type=\"s\"/>\n" \
130 " <arg name=\"path\" type=\"o\"/>\n" \
132 " <signal name=\"UserNew\">\n" \
133 " <arg name=\"uid\" type=\"u\"/>\n" \
134 " <arg name=\"path\" type=\"o\"/>\n" \
136 " <signal name=\"UserRemoved\">\n" \
137 " <arg name=\"uid\" type=\"u\"/>\n" \
138 " <arg name=\"path\" type=\"o\"/>\n" \
140 " <signal name=\"SeatNew\">\n" \
141 " <arg name=\"id\" type=\"s\"/>\n" \
142 " <arg name=\"path\" type=\"o\"/>\n" \
144 " <signal name=\"SeatRemoved\">\n" \
145 " <arg name=\"id\" type=\"s\"/>\n" \
146 " <arg name=\"path\" type=\"o\"/>\n" \
148 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
149 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
150 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
151 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
152 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
153 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
154 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
155 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
156 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
157 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
160 #define INTROSPECTION_BEGIN \
161 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
163 BUS_MANAGER_INTERFACE \
164 BUS_PROPERTIES_INTERFACE \
166 BUS_INTROSPECTABLE_INTERFACE
168 #define INTROSPECTION_END \
171 #define INTERFACES_LIST \
172 BUS_GENERIC_INTERFACES_LIST \
173 "org.freedesktop.login1.Manager\0"
175 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
183 b = manager_get_idle_hint(m, NULL) > 0;
184 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
190 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
199 manager_get_idle_hint(m, &t);
200 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
202 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
208 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
209 Session *session = NULL;
211 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
212 uint32_t uid, leader, audit_id = 0;
213 dbus_bool_t remote, kill_processes;
214 char **controllers = NULL, **reset_controllers = NULL;
217 DBusMessageIter iter;
222 DBusMessage *reply = NULL;
229 if (!dbus_message_iter_init(message, &iter) ||
230 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
233 dbus_message_iter_get_basic(&iter, &uid);
235 if (!dbus_message_iter_next(&iter) ||
236 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
239 dbus_message_iter_get_basic(&iter, &leader);
242 !dbus_message_iter_next(&iter) ||
243 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
246 dbus_message_iter_get_basic(&iter, &service);
248 if (!dbus_message_iter_next(&iter) ||
249 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
252 dbus_message_iter_get_basic(&iter, &type);
253 t = session_type_from_string(type);
256 !dbus_message_iter_next(&iter) ||
257 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
260 dbus_message_iter_get_basic(&iter, &seat);
265 s = hashmap_get(m->seats, seat);
270 if (!dbus_message_iter_next(&iter) ||
271 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
274 dbus_message_iter_get_basic(&iter, &vtnr);
276 if (!dbus_message_iter_next(&iter) ||
277 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
280 dbus_message_iter_get_basic(&iter, &tty);
282 if (tty_is_vc(tty)) {
287 else if (s != m->vtconsole)
290 v = vtnr_from_tty(tty);
293 return v < 0 ? v : -EINVAL;
297 else if (vtnr != (uint32_t) v)
300 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
304 if (seat_is_vtconsole(s)) {
305 if (vtnr <= 0 || vtnr > 63)
313 if (!dbus_message_iter_next(&iter) ||
314 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
317 dbus_message_iter_get_basic(&iter, &display);
319 if (!dbus_message_iter_next(&iter) ||
320 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
323 dbus_message_iter_get_basic(&iter, &remote);
325 if (!dbus_message_iter_next(&iter) ||
326 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
329 dbus_message_iter_get_basic(&iter, &remote_user);
331 if (!dbus_message_iter_next(&iter) ||
332 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
335 dbus_message_iter_get_basic(&iter, &remote_host);
337 if (!dbus_message_iter_next(&iter) ||
338 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
339 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
342 r = bus_parse_strv_iter(&iter, &controllers);
346 if (strv_contains(controllers, "systemd") ||
347 !dbus_message_iter_next(&iter) ||
348 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
349 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
354 r = bus_parse_strv_iter(&iter, &reset_controllers);
358 if (strv_contains(reset_controllers, "systemd") ||
359 !dbus_message_iter_next(&iter) ||
360 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
365 dbus_message_iter_get_basic(&iter, &kill_processes);
367 r = manager_add_user_by_uid(m, uid, &user);
371 audit_session_from_pid(leader, &audit_id);
374 asprintf(&id, "%lu", (unsigned long) audit_id);
381 session = hashmap_get(m->sessions, id);
386 fifo_fd = session_create_fifo(session);
392 /* Session already exists, client is probably
393 * something like "su" which changes uid but
394 * is still the same audit session */
396 reply = dbus_message_new_method_return(message);
402 p = session_bus_path(session);
408 seat = session->seat ? session->seat->id : "";
409 vtnr = session->vtnr;
410 b = dbus_message_append_args(
412 DBUS_TYPE_STRING, &session->id,
413 DBUS_TYPE_OBJECT_PATH, &p,
414 DBUS_TYPE_STRING, &session->user->runtime_path,
415 DBUS_TYPE_UNIX_FD, &fifo_fd,
416 DBUS_TYPE_STRING, &seat,
417 DBUS_TYPE_UINT32, &vtnr,
426 close_nointr_nofail(fifo_fd);
429 strv_free(controllers);
430 strv_free(reset_controllers);
438 asprintf(&id, "c%lu", ++m->session_counter);
445 } while (hashmap_get(m->sessions, id));
448 r = manager_add_session(m, user, id, &session);
453 session->leader = leader;
454 session->audit_id = audit_id;
456 session->remote = remote;
457 session->controllers = controllers;
458 session->reset_controllers = reset_controllers;
459 session->kill_processes = kill_processes;
460 session->vtnr = vtnr;
462 controllers = reset_controllers = NULL;
465 session->tty = strdup(tty);
472 if (!isempty(display)) {
473 session->display = strdup(display);
474 if (!session->display) {
480 if (!isempty(remote_user)) {
481 session->remote_user = strdup(remote_user);
482 if (!session->remote_user) {
488 if (!isempty(remote_host)) {
489 session->remote_host = strdup(remote_host);
490 if (!session->remote_host) {
496 if (!isempty(service)) {
497 session->service = strdup(service);
498 if (!session->service) {
504 fifo_fd = session_create_fifo(session);
511 r = seat_attach_session(s, session);
516 r = session_start(session);
520 reply = dbus_message_new_method_return(message);
526 p = session_bus_path(session);
532 seat = s ? s->id : "";
533 b = dbus_message_append_args(
535 DBUS_TYPE_STRING, &session->id,
536 DBUS_TYPE_OBJECT_PATH, &p,
537 DBUS_TYPE_STRING, &session->user->runtime_path,
538 DBUS_TYPE_UNIX_FD, &fifo_fd,
539 DBUS_TYPE_STRING, &seat,
540 DBUS_TYPE_UINT32, &vtnr,
549 close_nointr_nofail(fifo_fd);
555 strv_free(controllers);
556 strv_free(reset_controllers);
559 session_add_to_gc_queue(session);
562 user_add_to_gc_queue(user);
565 close_nointr_nofail(fifo_fd);
568 dbus_message_unref(reply);
573 static int trigger_device(Manager *m, struct udev_device *d) {
574 struct udev_enumerate *e;
575 struct udev_list_entry *first, *item;
580 e = udev_enumerate_new(m->udev);
587 if (udev_enumerate_add_match_parent(e, d) < 0) {
593 if (udev_enumerate_scan_devices(e) < 0) {
598 first = udev_enumerate_get_list_entry(e);
599 udev_list_entry_foreach(item, first) {
603 p = udev_list_entry_get_name(item);
605 t = strappend(p, "/uevent");
611 write_one_line_file(t, "change");
619 udev_enumerate_unref(e);
624 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
625 struct udev_device *d;
626 char *rule = NULL, *file = NULL;
627 const char *id_for_seat;
634 d = udev_device_new_from_syspath(m->udev, sysfs);
638 if (!udev_device_has_tag(d, "seat")) {
643 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
649 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
654 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
659 mkdir_p("/etc/udev/rules.d", 0755);
660 r = write_one_line_file_atomic(file, rule);
664 r = trigger_device(m, d);
671 udev_device_unref(d);
676 static int flush_devices(Manager *m) {
681 d = opendir("/etc/udev/rules.d");
684 log_warning("Failed to open /etc/udev/rules.d: %m");
688 while ((de = readdir(d))) {
690 if (!dirent_is_file(de))
693 if (!startswith(de->d_name, "72-seat-"))
696 if (!endswith(de->d_name, ".rules"))
699 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
700 log_warning("Failed to unlink %s: %m", de->d_name);
706 return trigger_device(m, NULL);
709 static DBusHandlerResult manager_message_handler(
710 DBusConnection *connection,
711 DBusMessage *message,
714 Manager *m = userdata;
716 const BusProperty properties[] = {
717 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
718 { "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
719 { "org.freedesktop.login1.Manager", "ResetControllers", bus_property_append_strv, "as", m->reset_controllers },
720 { "org.freedesktop.login1.Manager", "NAutoVTs", bus_property_append_unsigned, "u", &m->n_autovts },
721 { "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
722 { "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
723 { "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
724 { "org.freedesktop.login1.Manager", "IdleHint", bus_manager_append_idle_hint, "b", m },
725 { "org.freedesktop.login1.Manager", "IdleSinceHint", bus_manager_append_idle_hint_since, "t", m },
726 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
727 { NULL, NULL, NULL, NULL, NULL }
731 DBusMessage *reply = NULL;
738 dbus_error_init(&error);
740 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
746 if (!dbus_message_get_args(
749 DBUS_TYPE_STRING, &name,
751 return bus_send_error_reply(connection, message, &error, -EINVAL);
753 session = hashmap_get(m->sessions, name);
755 return bus_send_error_reply(connection, message, &error, -ENOENT);
757 reply = dbus_message_new_method_return(message);
761 p = session_bus_path(session);
765 b = dbus_message_append_args(
767 DBUS_TYPE_OBJECT_PATH, &p,
774 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
780 if (!dbus_message_get_args(
783 DBUS_TYPE_UINT32, &uid,
785 return bus_send_error_reply(connection, message, &error, -EINVAL);
787 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
789 return bus_send_error_reply(connection, message, &error, -ENOENT);
791 reply = dbus_message_new_method_return(message);
795 p = user_bus_path(user);
799 b = dbus_message_append_args(
801 DBUS_TYPE_OBJECT_PATH, &p,
808 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
814 if (!dbus_message_get_args(
817 DBUS_TYPE_STRING, &name,
819 return bus_send_error_reply(connection, message, &error, -EINVAL);
821 seat = hashmap_get(m->seats, name);
823 return bus_send_error_reply(connection, message, &error, -ENOENT);
825 reply = dbus_message_new_method_return(message);
829 p = seat_bus_path(seat);
833 b = dbus_message_append_args(
835 DBUS_TYPE_OBJECT_PATH, &p,
842 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
846 DBusMessageIter iter, sub;
847 const char *empty = "";
849 reply = dbus_message_new_method_return(message);
853 dbus_message_iter_init_append(reply, &iter);
855 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
858 HASHMAP_FOREACH(session, m->sessions, i) {
859 DBusMessageIter sub2;
862 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
865 uid = session->user->uid;
867 p = session_bus_path(session);
871 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
872 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
873 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
874 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
875 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
882 if (!dbus_message_iter_close_container(&sub, &sub2))
886 if (!dbus_message_iter_close_container(&iter, &sub))
889 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
893 DBusMessageIter iter, sub;
895 reply = dbus_message_new_method_return(message);
899 dbus_message_iter_init_append(reply, &iter);
901 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
904 HASHMAP_FOREACH(user, m->users, i) {
905 DBusMessageIter sub2;
908 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
913 p = user_bus_path(user);
917 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
918 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
919 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
926 if (!dbus_message_iter_close_container(&sub, &sub2))
930 if (!dbus_message_iter_close_container(&iter, &sub))
933 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
937 DBusMessageIter iter, sub;
939 reply = dbus_message_new_method_return(message);
943 dbus_message_iter_init_append(reply, &iter);
945 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
948 HASHMAP_FOREACH(seat, m->seats, i) {
949 DBusMessageIter sub2;
951 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
954 p = seat_bus_path(seat);
958 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
959 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
966 if (!dbus_message_iter_close_container(&sub, &sub2))
970 if (!dbus_message_iter_close_container(&iter, &sub))
973 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
975 r = bus_manager_create_session(m, message, &reply);
977 /* Don't delay the work on OOM here, since it might be
978 * triggered by a low RLIMIT_NOFILE here (since we
979 * send a dupped fd to the client), and we'd rather
980 * see this fail quickly then be retried later */
983 return bus_send_error_reply(connection, message, &error, r);
985 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
989 if (!dbus_message_get_args(
992 DBUS_TYPE_STRING, &name,
994 return bus_send_error_reply(connection, message, &error, -EINVAL);
996 session = hashmap_get(m->sessions, name);
998 return bus_send_error_reply(connection, message, &error, -ENOENT);
1000 r = session_activate(session);
1002 return bus_send_error_reply(connection, message, NULL, r);
1004 reply = dbus_message_new_method_return(message);
1008 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
1009 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
1013 if (!dbus_message_get_args(
1016 DBUS_TYPE_STRING, &name,
1018 return bus_send_error_reply(connection, message, &error, -EINVAL);
1020 session = hashmap_get(m->sessions, name);
1022 return bus_send_error_reply(connection, message, &error, -ENOENT);
1024 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1027 reply = dbus_message_new_method_return(message);
1031 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1038 if (!dbus_message_get_args(
1041 DBUS_TYPE_STRING, &name,
1042 DBUS_TYPE_STRING, &swho,
1043 DBUS_TYPE_INT32, &signo,
1045 return bus_send_error_reply(connection, message, &error, -EINVAL);
1050 who = kill_who_from_string(swho);
1052 return bus_send_error_reply(connection, message, &error, -EINVAL);
1055 if (signo <= 0 || signo >= _NSIG)
1056 return bus_send_error_reply(connection, message, &error, -EINVAL);
1058 session = hashmap_get(m->sessions, name);
1060 return bus_send_error_reply(connection, message, &error, -ENOENT);
1062 r = session_kill(session, who, signo);
1064 return bus_send_error_reply(connection, message, NULL, r);
1066 reply = dbus_message_new_method_return(message);
1070 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1075 if (!dbus_message_get_args(
1078 DBUS_TYPE_UINT32, &uid,
1079 DBUS_TYPE_INT32, &signo,
1081 return bus_send_error_reply(connection, message, &error, -EINVAL);
1083 if (signo <= 0 || signo >= _NSIG)
1084 return bus_send_error_reply(connection, message, &error, -EINVAL);
1086 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1088 return bus_send_error_reply(connection, message, &error, -ENOENT);
1090 r = user_kill(user, signo);
1092 return bus_send_error_reply(connection, message, NULL, r);
1094 reply = dbus_message_new_method_return(message);
1098 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1102 if (!dbus_message_get_args(
1105 DBUS_TYPE_STRING, &name,
1107 return bus_send_error_reply(connection, message, &error, -EINVAL);
1109 session = hashmap_get(m->sessions, name);
1111 return bus_send_error_reply(connection, message, &error, -ENOENT);
1113 r = session_stop(session);
1115 return bus_send_error_reply(connection, message, NULL, r);
1117 reply = dbus_message_new_method_return(message);
1121 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1125 if (!dbus_message_get_args(
1128 DBUS_TYPE_UINT32, &uid,
1130 return bus_send_error_reply(connection, message, &error, -EINVAL);
1132 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1134 return bus_send_error_reply(connection, message, &error, -ENOENT);
1136 r = user_stop(user);
1138 return bus_send_error_reply(connection, message, NULL, r);
1140 reply = dbus_message_new_method_return(message);
1144 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1148 if (!dbus_message_get_args(
1151 DBUS_TYPE_STRING, &name,
1153 return bus_send_error_reply(connection, message, &error, -EINVAL);
1155 seat = hashmap_get(m->seats, name);
1157 return bus_send_error_reply(connection, message, &error, -ENOENT);
1159 r = seat_stop_sessions(seat);
1161 return bus_send_error_reply(connection, message, NULL, r);
1163 reply = dbus_message_new_method_return(message);
1167 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1170 dbus_bool_t b, interactive;
1173 if (!dbus_message_get_args(
1176 DBUS_TYPE_UINT32, &uid,
1177 DBUS_TYPE_BOOLEAN, &b,
1178 DBUS_TYPE_BOOLEAN, &interactive,
1180 return bus_send_error_reply(connection, message, &error, -EINVAL);
1185 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1187 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1189 return bus_send_error_reply(connection, message, &error, r);
1191 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1193 return bus_send_error_reply(connection, message, &error, r);
1195 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1206 return bus_send_error_reply(connection, message, &error, r);
1208 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1217 if (r < 0 && errno != ENOENT)
1218 return bus_send_error_reply(connection, message, &error, -errno);
1220 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1222 user_add_to_gc_queue(u);
1225 reply = dbus_message_new_method_return(message);
1229 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1230 const char *sysfs, *seat;
1231 dbus_bool_t interactive;
1233 if (!dbus_message_get_args(
1236 DBUS_TYPE_STRING, &seat,
1237 DBUS_TYPE_STRING, &sysfs,
1238 DBUS_TYPE_BOOLEAN, &interactive,
1240 return bus_send_error_reply(connection, message, &error, -EINVAL);
1242 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1243 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1245 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1247 return bus_send_error_reply(connection, message, &error, r);
1249 r = attach_device(m, seat, sysfs);
1251 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1253 reply = dbus_message_new_method_return(message);
1258 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1259 dbus_bool_t interactive;
1261 if (!dbus_message_get_args(
1264 DBUS_TYPE_BOOLEAN, &interactive,
1266 return bus_send_error_reply(connection, message, &error, -EINVAL);
1268 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1270 return bus_send_error_reply(connection, message, &error, r);
1272 r = flush_devices(m);
1274 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1276 reply = dbus_message_new_method_return(message);
1280 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "PowerOff") ||
1281 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Reboot")) {
1282 dbus_bool_t interactive;
1283 bool multiple_sessions;
1284 DBusMessage *forward, *freply;
1286 const char *mode = "replace";
1289 if (!dbus_message_get_args(
1292 DBUS_TYPE_BOOLEAN, &interactive,
1294 return bus_send_error_reply(connection, message, &error, -EINVAL);
1296 multiple_sessions = hashmap_size(m->sessions) > 1;
1298 if (!multiple_sessions) {
1301 /* Hmm, there's only one session, but let's
1302 * make sure it actually belongs to the user
1303 * who is asking. If not, better be safe than
1306 s = hashmap_first(m->sessions);
1310 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
1311 if (ul == (unsigned long) -1)
1312 return bus_send_error_reply(connection, message, &error, -EIO);
1314 multiple_sessions = s->user->uid != ul;
1318 if (streq(dbus_message_get_member(message), "PowerOff")) {
1319 if (multiple_sessions)
1320 action = "org.freedesktop.login1.power-off-multiple-sessions";
1322 action = "org.freedesktop.login1.power-off";
1324 name = SPECIAL_POWEROFF_TARGET;
1326 if (multiple_sessions)
1327 action = "org.freedesktop.login1.reboot-multiple-sessions";
1329 action = "org.freedesktop.login1.reboot";
1331 name = SPECIAL_REBOOT_TARGET;
1334 r = verify_polkit(connection, message, action, interactive, &error);
1336 return bus_send_error_reply(connection, message, &error, r);
1338 forward = dbus_message_new_method_call(
1339 "org.freedesktop.systemd1",
1340 "/org/freedesktop/systemd1",
1341 "org.freedesktop.systemd1.Manager",
1344 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1346 if (!dbus_message_append_args(forward,
1347 DBUS_TYPE_STRING, &name,
1348 DBUS_TYPE_STRING, &mode,
1349 DBUS_TYPE_INVALID)) {
1350 dbus_message_unref(forward);
1351 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1354 freply = dbus_connection_send_with_reply_and_block(connection, forward, -1, &error);
1355 dbus_message_unref(forward);
1358 return bus_send_error_reply(connection, message, &error, -EIO);
1360 dbus_message_unref(freply);
1362 reply = dbus_message_new_method_return(message);
1366 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1367 char *introspection = NULL;
1376 if (!(reply = dbus_message_new_method_return(message)))
1379 /* We roll our own introspection code here, instead of
1380 * relying on bus_default_message_handler() because we
1381 * need to generate our introspection string
1384 if (!(f = open_memstream(&introspection, &size)))
1387 fputs(INTROSPECTION_BEGIN, f);
1389 HASHMAP_FOREACH(seat, m->seats, i) {
1390 p = bus_path_escape(seat->id);
1393 fprintf(f, "<node name=\"seat/%s\"/>", p);
1398 HASHMAP_FOREACH(user, m->users, i)
1399 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1401 HASHMAP_FOREACH(session, m->sessions, i) {
1402 p = bus_path_escape(session->id);
1405 fprintf(f, "<node name=\"session/%s\"/>", p);
1410 fputs(INTROSPECTION_END, f);
1414 free(introspection);
1423 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1424 free(introspection);
1428 free(introspection);
1430 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1433 if (!dbus_connection_send(connection, reply, NULL))
1436 dbus_message_unref(reply);
1439 return DBUS_HANDLER_RESULT_HANDLED;
1443 dbus_message_unref(reply);
1445 dbus_error_free(&error);
1447 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1450 const DBusObjectPathVTable bus_manager_vtable = {
1451 .message_function = manager_message_handler
1454 DBusHandlerResult bus_message_filter(
1455 DBusConnection *connection,
1456 DBusMessage *message,
1459 Manager *m = userdata;
1466 dbus_error_init(&error);
1468 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1471 if (!dbus_message_get_args(message, &error,
1472 DBUS_TYPE_STRING, &cgroup,
1474 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1476 manager_cgroup_notify_empty(m, cgroup);
1479 dbus_error_free(&error);
1481 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1484 int manager_send_changed(Manager *manager, const char *properties) {
1490 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1494 if (!dbus_connection_send(manager->bus, m, NULL))
1501 dbus_message_unref(m);