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=\"LockSession\">\n" \
86 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
88 " <method name=\"UnlockSession\">\n" \
89 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
91 " <method name=\"KillSession\">\n" \
92 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
93 " <arg name=\"who\" type=\"s\"/>\n" \
94 " <arg name=\"signal\" type=\"s\"/>\n" \
96 " <method name=\"KillUser\">\n" \
97 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
98 " <arg name=\"signal\" type=\"s\"/>\n" \
100 " <method name=\"TerminateSession\">\n" \
101 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
103 " <method name=\"TerminateUser\">\n" \
104 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
106 " <method name=\"TerminateSeat\">\n" \
107 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
109 " <method name=\"SetUserLinger\">\n" \
110 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
111 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
112 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
114 " <method name=\"AttachDevice\">\n" \
115 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
116 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
117 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
119 " <method name=\"FlushDevices\">\n" \
120 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
122 " <method name=\"PowerOff\">\n" \
123 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
125 " <method name=\"Reboot\">\n" \
126 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
128 " <signal name=\"SessionNew\">\n" \
129 " <arg name=\"id\" type=\"s\"/>\n" \
130 " <arg name=\"path\" type=\"o\"/>\n" \
132 " <signal name=\"SessionRemoved\">\n" \
133 " <arg name=\"id\" type=\"s\"/>\n" \
134 " <arg name=\"path\" type=\"o\"/>\n" \
136 " <signal name=\"UserNew\">\n" \
137 " <arg name=\"uid\" type=\"u\"/>\n" \
138 " <arg name=\"path\" type=\"o\"/>\n" \
140 " <signal name=\"UserRemoved\">\n" \
141 " <arg name=\"uid\" type=\"u\"/>\n" \
142 " <arg name=\"path\" type=\"o\"/>\n" \
144 " <signal name=\"SeatNew\">\n" \
145 " <arg name=\"id\" type=\"s\"/>\n" \
146 " <arg name=\"path\" type=\"o\"/>\n" \
148 " <signal name=\"SeatRemoved\">\n" \
149 " <arg name=\"id\" type=\"s\"/>\n" \
150 " <arg name=\"path\" type=\"o\"/>\n" \
152 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
153 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
154 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
155 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
156 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
157 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
158 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
159 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
160 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
161 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
164 #define INTROSPECTION_BEGIN \
165 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
167 BUS_MANAGER_INTERFACE \
168 BUS_PROPERTIES_INTERFACE \
170 BUS_INTROSPECTABLE_INTERFACE
172 #define INTROSPECTION_END \
175 #define INTERFACES_LIST \
176 BUS_GENERIC_INTERFACES_LIST \
177 "org.freedesktop.login1.Manager\0"
179 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
187 b = manager_get_idle_hint(m, NULL) > 0;
188 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
194 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
203 manager_get_idle_hint(m, &t);
204 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
206 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
212 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
213 Session *session = NULL;
215 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
216 uint32_t uid, leader, audit_id = 0;
217 dbus_bool_t remote, kill_processes;
218 char **controllers = NULL, **reset_controllers = NULL;
221 DBusMessageIter iter;
226 DBusMessage *reply = NULL;
233 if (!dbus_message_iter_init(message, &iter) ||
234 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
237 dbus_message_iter_get_basic(&iter, &uid);
239 if (!dbus_message_iter_next(&iter) ||
240 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
243 dbus_message_iter_get_basic(&iter, &leader);
246 !dbus_message_iter_next(&iter) ||
247 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
250 dbus_message_iter_get_basic(&iter, &service);
252 if (!dbus_message_iter_next(&iter) ||
253 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
256 dbus_message_iter_get_basic(&iter, &type);
257 t = session_type_from_string(type);
260 !dbus_message_iter_next(&iter) ||
261 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
264 dbus_message_iter_get_basic(&iter, &seat);
269 s = hashmap_get(m->seats, seat);
274 if (!dbus_message_iter_next(&iter) ||
275 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
278 dbus_message_iter_get_basic(&iter, &vtnr);
280 if (!dbus_message_iter_next(&iter) ||
281 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
284 dbus_message_iter_get_basic(&iter, &tty);
286 if (tty_is_vc(tty)) {
291 else if (s != m->vtconsole)
294 v = vtnr_from_tty(tty);
297 return v < 0 ? v : -EINVAL;
301 else if (vtnr != (uint32_t) v)
304 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
308 if (seat_can_multi_session(s)) {
309 if (vtnr <= 0 || vtnr > 63)
317 if (!dbus_message_iter_next(&iter) ||
318 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
321 dbus_message_iter_get_basic(&iter, &display);
323 if (!dbus_message_iter_next(&iter) ||
324 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
327 dbus_message_iter_get_basic(&iter, &remote);
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_user);
335 if (!dbus_message_iter_next(&iter) ||
336 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
339 dbus_message_iter_get_basic(&iter, &remote_host);
341 if (!dbus_message_iter_next(&iter) ||
342 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
343 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
346 r = bus_parse_strv_iter(&iter, &controllers);
350 if (strv_contains(controllers, "systemd") ||
351 !dbus_message_iter_next(&iter) ||
352 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
353 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
358 r = bus_parse_strv_iter(&iter, &reset_controllers);
362 if (strv_contains(reset_controllers, "systemd") ||
363 !dbus_message_iter_next(&iter) ||
364 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
369 dbus_message_iter_get_basic(&iter, &kill_processes);
371 r = manager_add_user_by_uid(m, uid, &user);
375 audit_session_from_pid(leader, &audit_id);
378 asprintf(&id, "%lu", (unsigned long) audit_id);
385 session = hashmap_get(m->sessions, id);
390 fifo_fd = session_create_fifo(session);
396 /* Session already exists, client is probably
397 * something like "su" which changes uid but
398 * is still the same audit session */
400 reply = dbus_message_new_method_return(message);
406 p = session_bus_path(session);
412 seat = session->seat ? session->seat->id : "";
413 vtnr = session->vtnr;
414 b = dbus_message_append_args(
416 DBUS_TYPE_STRING, &session->id,
417 DBUS_TYPE_OBJECT_PATH, &p,
418 DBUS_TYPE_STRING, &session->user->runtime_path,
419 DBUS_TYPE_UNIX_FD, &fifo_fd,
420 DBUS_TYPE_STRING, &seat,
421 DBUS_TYPE_UINT32, &vtnr,
430 close_nointr_nofail(fifo_fd);
433 strv_free(controllers);
434 strv_free(reset_controllers);
442 asprintf(&id, "c%lu", ++m->session_counter);
449 } while (hashmap_get(m->sessions, id));
452 r = manager_add_session(m, user, id, &session);
457 session->leader = leader;
458 session->audit_id = audit_id;
460 session->remote = remote;
461 session->controllers = controllers;
462 session->reset_controllers = reset_controllers;
463 session->kill_processes = kill_processes;
464 session->vtnr = vtnr;
466 controllers = reset_controllers = NULL;
469 session->tty = strdup(tty);
476 if (!isempty(display)) {
477 session->display = strdup(display);
478 if (!session->display) {
484 if (!isempty(remote_user)) {
485 session->remote_user = strdup(remote_user);
486 if (!session->remote_user) {
492 if (!isempty(remote_host)) {
493 session->remote_host = strdup(remote_host);
494 if (!session->remote_host) {
500 if (!isempty(service)) {
501 session->service = strdup(service);
502 if (!session->service) {
508 fifo_fd = session_create_fifo(session);
515 r = seat_attach_session(s, session);
520 r = session_start(session);
524 reply = dbus_message_new_method_return(message);
530 p = session_bus_path(session);
536 seat = s ? s->id : "";
537 b = dbus_message_append_args(
539 DBUS_TYPE_STRING, &session->id,
540 DBUS_TYPE_OBJECT_PATH, &p,
541 DBUS_TYPE_STRING, &session->user->runtime_path,
542 DBUS_TYPE_UNIX_FD, &fifo_fd,
543 DBUS_TYPE_STRING, &seat,
544 DBUS_TYPE_UINT32, &vtnr,
553 close_nointr_nofail(fifo_fd);
559 strv_free(controllers);
560 strv_free(reset_controllers);
563 session_add_to_gc_queue(session);
566 user_add_to_gc_queue(user);
569 close_nointr_nofail(fifo_fd);
572 dbus_message_unref(reply);
577 static int trigger_device(Manager *m, struct udev_device *d) {
578 struct udev_enumerate *e;
579 struct udev_list_entry *first, *item;
584 e = udev_enumerate_new(m->udev);
591 if (udev_enumerate_add_match_parent(e, d) < 0) {
597 if (udev_enumerate_scan_devices(e) < 0) {
602 first = udev_enumerate_get_list_entry(e);
603 udev_list_entry_foreach(item, first) {
607 p = udev_list_entry_get_name(item);
609 t = strappend(p, "/uevent");
615 write_one_line_file(t, "change");
623 udev_enumerate_unref(e);
628 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
629 struct udev_device *d;
630 char *rule = NULL, *file = NULL;
631 const char *id_for_seat;
638 d = udev_device_new_from_syspath(m->udev, sysfs);
642 if (!udev_device_has_tag(d, "seat")) {
647 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
653 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
658 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
663 mkdir_p("/etc/udev/rules.d", 0755);
664 r = write_one_line_file_atomic(file, rule);
668 r = trigger_device(m, d);
675 udev_device_unref(d);
680 static int flush_devices(Manager *m) {
685 d = opendir("/etc/udev/rules.d");
688 log_warning("Failed to open /etc/udev/rules.d: %m");
692 while ((de = readdir(d))) {
694 if (!dirent_is_file(de))
697 if (!startswith(de->d_name, "72-seat-"))
700 if (!endswith(de->d_name, ".rules"))
703 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
704 log_warning("Failed to unlink %s: %m", de->d_name);
710 return trigger_device(m, NULL);
713 static const BusProperty bus_login_manager_properties[] = {
714 { "ControlGroupHierarchy", bus_property_append_string, "s", offsetof(Manager, cgroup_path), true },
715 { "Controllers", bus_property_append_strv, "as", offsetof(Manager, controllers), true },
716 { "ResetControllers", bus_property_append_strv, "as", offsetof(Manager, reset_controllers), true },
717 { "NAutoVTs", bus_property_append_unsigned, "u", offsetof(Manager, n_autovts) },
718 { "KillOnlyUsers", bus_property_append_strv, "as", offsetof(Manager, kill_only_users), true },
719 { "KillExcludeUsers", bus_property_append_strv, "as", offsetof(Manager, kill_exclude_users), true },
720 { "KillUserProcesses", bus_property_append_bool, "b", offsetof(Manager, kill_user_processes) },
721 { "IdleHint", bus_manager_append_idle_hint, "b", 0 },
722 { "IdleSinceHint", bus_manager_append_idle_hint_since, "t", 0 },
723 { "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", 0 },
727 static DBusHandlerResult manager_message_handler(
728 DBusConnection *connection,
729 DBusMessage *message,
732 Manager *m = userdata;
735 DBusMessage *reply = NULL;
742 dbus_error_init(&error);
744 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
750 if (!dbus_message_get_args(
753 DBUS_TYPE_STRING, &name,
755 return bus_send_error_reply(connection, message, &error, -EINVAL);
757 session = hashmap_get(m->sessions, name);
759 return bus_send_error_reply(connection, message, &error, -ENOENT);
761 reply = dbus_message_new_method_return(message);
765 p = session_bus_path(session);
769 b = dbus_message_append_args(
771 DBUS_TYPE_OBJECT_PATH, &p,
778 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSessionByPID")) {
784 if (!dbus_message_get_args(
787 DBUS_TYPE_UINT32, &pid,
789 return bus_send_error_reply(connection, message, &error, -EINVAL);
791 r = manager_get_session_by_pid(m, pid, &session);
793 return bus_send_error_reply(connection, message, NULL, r < 0 ? r : -ENOENT);
795 reply = dbus_message_new_method_return(message);
799 p = session_bus_path(session);
803 b = dbus_message_append_args(
805 DBUS_TYPE_OBJECT_PATH, &p,
812 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
818 if (!dbus_message_get_args(
821 DBUS_TYPE_UINT32, &uid,
823 return bus_send_error_reply(connection, message, &error, -EINVAL);
825 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
827 return bus_send_error_reply(connection, message, &error, -ENOENT);
829 reply = dbus_message_new_method_return(message);
833 p = user_bus_path(user);
837 b = dbus_message_append_args(
839 DBUS_TYPE_OBJECT_PATH, &p,
846 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
852 if (!dbus_message_get_args(
855 DBUS_TYPE_STRING, &name,
857 return bus_send_error_reply(connection, message, &error, -EINVAL);
859 seat = hashmap_get(m->seats, name);
861 return bus_send_error_reply(connection, message, &error, -ENOENT);
863 reply = dbus_message_new_method_return(message);
867 p = seat_bus_path(seat);
871 b = dbus_message_append_args(
873 DBUS_TYPE_OBJECT_PATH, &p,
880 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
884 DBusMessageIter iter, sub;
885 const char *empty = "";
887 reply = dbus_message_new_method_return(message);
891 dbus_message_iter_init_append(reply, &iter);
893 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
896 HASHMAP_FOREACH(session, m->sessions, i) {
897 DBusMessageIter sub2;
900 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
903 uid = session->user->uid;
905 p = session_bus_path(session);
909 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
910 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
911 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
912 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
913 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
920 if (!dbus_message_iter_close_container(&sub, &sub2))
924 if (!dbus_message_iter_close_container(&iter, &sub))
927 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
931 DBusMessageIter iter, sub;
933 reply = dbus_message_new_method_return(message);
937 dbus_message_iter_init_append(reply, &iter);
939 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
942 HASHMAP_FOREACH(user, m->users, i) {
943 DBusMessageIter sub2;
946 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
951 p = user_bus_path(user);
955 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
956 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
957 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
964 if (!dbus_message_iter_close_container(&sub, &sub2))
968 if (!dbus_message_iter_close_container(&iter, &sub))
971 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
975 DBusMessageIter iter, sub;
977 reply = dbus_message_new_method_return(message);
981 dbus_message_iter_init_append(reply, &iter);
983 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
986 HASHMAP_FOREACH(seat, m->seats, i) {
987 DBusMessageIter sub2;
989 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
992 p = seat_bus_path(seat);
996 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
997 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
1004 if (!dbus_message_iter_close_container(&sub, &sub2))
1008 if (!dbus_message_iter_close_container(&iter, &sub))
1011 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
1013 r = bus_manager_create_session(m, message, &reply);
1015 /* Don't delay the work on OOM here, since it might be
1016 * triggered by a low RLIMIT_NOFILE here (since we
1017 * send a dupped fd to the client), and we'd rather
1018 * see this fail quickly then be retried later */
1021 return bus_send_error_reply(connection, message, &error, r);
1023 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
1027 if (!dbus_message_get_args(
1030 DBUS_TYPE_STRING, &name,
1032 return bus_send_error_reply(connection, message, &error, -EINVAL);
1034 session = hashmap_get(m->sessions, name);
1036 return bus_send_error_reply(connection, message, &error, -ENOENT);
1038 r = session_activate(session);
1040 return bus_send_error_reply(connection, message, NULL, r);
1042 reply = dbus_message_new_method_return(message);
1046 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
1047 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
1051 if (!dbus_message_get_args(
1054 DBUS_TYPE_STRING, &name,
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 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1065 reply = dbus_message_new_method_return(message);
1069 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1076 if (!dbus_message_get_args(
1079 DBUS_TYPE_STRING, &name,
1080 DBUS_TYPE_STRING, &swho,
1081 DBUS_TYPE_INT32, &signo,
1083 return bus_send_error_reply(connection, message, &error, -EINVAL);
1088 who = kill_who_from_string(swho);
1090 return bus_send_error_reply(connection, message, &error, -EINVAL);
1093 if (signo <= 0 || signo >= _NSIG)
1094 return bus_send_error_reply(connection, message, &error, -EINVAL);
1096 session = hashmap_get(m->sessions, name);
1098 return bus_send_error_reply(connection, message, &error, -ENOENT);
1100 r = session_kill(session, who, signo);
1102 return bus_send_error_reply(connection, message, NULL, r);
1104 reply = dbus_message_new_method_return(message);
1108 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1113 if (!dbus_message_get_args(
1116 DBUS_TYPE_UINT32, &uid,
1117 DBUS_TYPE_INT32, &signo,
1119 return bus_send_error_reply(connection, message, &error, -EINVAL);
1121 if (signo <= 0 || signo >= _NSIG)
1122 return bus_send_error_reply(connection, message, &error, -EINVAL);
1124 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1126 return bus_send_error_reply(connection, message, &error, -ENOENT);
1128 r = user_kill(user, signo);
1130 return bus_send_error_reply(connection, message, NULL, r);
1132 reply = dbus_message_new_method_return(message);
1136 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1140 if (!dbus_message_get_args(
1143 DBUS_TYPE_STRING, &name,
1145 return bus_send_error_reply(connection, message, &error, -EINVAL);
1147 session = hashmap_get(m->sessions, name);
1149 return bus_send_error_reply(connection, message, &error, -ENOENT);
1151 r = session_stop(session);
1153 return bus_send_error_reply(connection, message, NULL, r);
1155 reply = dbus_message_new_method_return(message);
1159 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1163 if (!dbus_message_get_args(
1166 DBUS_TYPE_UINT32, &uid,
1168 return bus_send_error_reply(connection, message, &error, -EINVAL);
1170 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1172 return bus_send_error_reply(connection, message, &error, -ENOENT);
1174 r = user_stop(user);
1176 return bus_send_error_reply(connection, message, NULL, r);
1178 reply = dbus_message_new_method_return(message);
1182 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1186 if (!dbus_message_get_args(
1189 DBUS_TYPE_STRING, &name,
1191 return bus_send_error_reply(connection, message, &error, -EINVAL);
1193 seat = hashmap_get(m->seats, name);
1195 return bus_send_error_reply(connection, message, &error, -ENOENT);
1197 r = seat_stop_sessions(seat);
1199 return bus_send_error_reply(connection, message, NULL, r);
1201 reply = dbus_message_new_method_return(message);
1205 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1208 dbus_bool_t b, interactive;
1211 if (!dbus_message_get_args(
1214 DBUS_TYPE_UINT32, &uid,
1215 DBUS_TYPE_BOOLEAN, &b,
1216 DBUS_TYPE_BOOLEAN, &interactive,
1218 return bus_send_error_reply(connection, message, &error, -EINVAL);
1223 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1225 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1227 return bus_send_error_reply(connection, message, &error, r);
1229 mkdir_p("/var/lib/systemd", 0755);
1231 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1233 return bus_send_error_reply(connection, message, &error, r);
1235 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1246 return bus_send_error_reply(connection, message, &error, r);
1248 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1257 if (r < 0 && errno != ENOENT)
1258 return bus_send_error_reply(connection, message, &error, -errno);
1260 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1262 user_add_to_gc_queue(u);
1265 reply = dbus_message_new_method_return(message);
1269 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1270 const char *sysfs, *seat;
1271 dbus_bool_t interactive;
1273 if (!dbus_message_get_args(
1276 DBUS_TYPE_STRING, &seat,
1277 DBUS_TYPE_STRING, &sysfs,
1278 DBUS_TYPE_BOOLEAN, &interactive,
1280 return bus_send_error_reply(connection, message, &error, -EINVAL);
1282 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1283 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1285 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1287 return bus_send_error_reply(connection, message, &error, r);
1289 r = attach_device(m, seat, sysfs);
1291 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1293 reply = dbus_message_new_method_return(message);
1298 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1299 dbus_bool_t interactive;
1301 if (!dbus_message_get_args(
1304 DBUS_TYPE_BOOLEAN, &interactive,
1306 return bus_send_error_reply(connection, message, &error, -EINVAL);
1308 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1310 return bus_send_error_reply(connection, message, &error, r);
1312 r = flush_devices(m);
1314 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1316 reply = dbus_message_new_method_return(message);
1320 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "PowerOff") ||
1321 dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Reboot")) {
1322 dbus_bool_t interactive;
1323 bool multiple_sessions;
1324 DBusMessage *forward, *freply;
1326 const char *mode = "replace";
1329 if (!dbus_message_get_args(
1332 DBUS_TYPE_BOOLEAN, &interactive,
1334 return bus_send_error_reply(connection, message, &error, -EINVAL);
1336 multiple_sessions = hashmap_size(m->sessions) > 1;
1338 if (!multiple_sessions) {
1341 /* Hmm, there's only one session, but let's
1342 * make sure it actually belongs to the user
1343 * who is asking. If not, better be safe than
1346 s = hashmap_first(m->sessions);
1350 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
1351 if (ul == (unsigned long) -1)
1352 return bus_send_error_reply(connection, message, &error, -EIO);
1354 multiple_sessions = s->user->uid != ul;
1358 if (streq(dbus_message_get_member(message), "PowerOff")) {
1359 if (multiple_sessions)
1360 action = "org.freedesktop.login1.power-off-multiple-sessions";
1362 action = "org.freedesktop.login1.power-off";
1364 name = SPECIAL_POWEROFF_TARGET;
1366 if (multiple_sessions)
1367 action = "org.freedesktop.login1.reboot-multiple-sessions";
1369 action = "org.freedesktop.login1.reboot";
1371 name = SPECIAL_REBOOT_TARGET;
1374 r = verify_polkit(connection, message, action, interactive, &error);
1376 return bus_send_error_reply(connection, message, &error, r);
1378 forward = dbus_message_new_method_call(
1379 "org.freedesktop.systemd1",
1380 "/org/freedesktop/systemd1",
1381 "org.freedesktop.systemd1.Manager",
1384 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1386 if (!dbus_message_append_args(forward,
1387 DBUS_TYPE_STRING, &name,
1388 DBUS_TYPE_STRING, &mode,
1389 DBUS_TYPE_INVALID)) {
1390 dbus_message_unref(forward);
1391 return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1394 freply = dbus_connection_send_with_reply_and_block(connection, forward, -1, &error);
1395 dbus_message_unref(forward);
1398 return bus_send_error_reply(connection, message, &error, -EIO);
1400 dbus_message_unref(freply);
1402 reply = dbus_message_new_method_return(message);
1406 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1407 char *introspection = NULL;
1416 if (!(reply = dbus_message_new_method_return(message)))
1419 /* We roll our own introspection code here, instead of
1420 * relying on bus_default_message_handler() because we
1421 * need to generate our introspection string
1424 if (!(f = open_memstream(&introspection, &size)))
1427 fputs(INTROSPECTION_BEGIN, f);
1429 HASHMAP_FOREACH(seat, m->seats, i) {
1430 p = bus_path_escape(seat->id);
1433 fprintf(f, "<node name=\"seat/%s\"/>", p);
1438 HASHMAP_FOREACH(user, m->users, i)
1439 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1441 HASHMAP_FOREACH(session, m->sessions, i) {
1442 p = bus_path_escape(session->id);
1445 fprintf(f, "<node name=\"session/%s\"/>", p);
1450 fputs(INTROSPECTION_END, f);
1454 free(introspection);
1463 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1464 free(introspection);
1468 free(introspection);
1470 const BusBoundProperties bps[] = {
1471 { "org.freedesktop.login1.Manager", bus_login_manager_properties, m },
1474 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, bps);
1478 if (!dbus_connection_send(connection, reply, NULL))
1481 dbus_message_unref(reply);
1484 return DBUS_HANDLER_RESULT_HANDLED;
1488 dbus_message_unref(reply);
1490 dbus_error_free(&error);
1492 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1495 const DBusObjectPathVTable bus_manager_vtable = {
1496 .message_function = manager_message_handler
1499 DBusHandlerResult bus_message_filter(
1500 DBusConnection *connection,
1501 DBusMessage *message,
1504 Manager *m = userdata;
1511 dbus_error_init(&error);
1513 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1516 if (!dbus_message_get_args(message, &error,
1517 DBUS_TYPE_STRING, &cgroup,
1519 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1521 manager_cgroup_notify_empty(m, cgroup);
1524 dbus_error_free(&error);
1526 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1529 int manager_send_changed(Manager *manager, const char *properties) {
1535 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1539 if (!dbus_connection_send(manager->bus, m, NULL))
1546 dbus_message_unref(m);