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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "sd-messages.h"
31 #include "path-util.h"
33 #include "sleep-config.h"
34 #include "fileio-label.h"
37 #include "unit-name.h"
41 #include "bus-error.h"
42 #include "bus-common-errors.h"
43 #include "udev-util.h"
44 #include "selinux-util.h"
47 static int property_get_idle_hint(
50 const char *interface,
52 sd_bus_message *reply,
54 sd_bus_error *error) {
56 Manager *m = userdata;
62 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
65 static int property_get_idle_since_hint(
68 const char *interface,
70 sd_bus_message *reply,
72 sd_bus_error *error) {
74 Manager *m = userdata;
81 manager_get_idle_hint(m, &t);
83 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
86 static int property_get_inhibited(
89 const char *interface,
91 sd_bus_message *reply,
93 sd_bus_error *error) {
95 Manager *m = userdata;
102 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
104 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
107 static int property_get_preparing(
110 const char *interface,
111 const char *property,
112 sd_bus_message *reply,
114 sd_bus_error *error) {
116 Manager *m = userdata;
123 if (streq(property, "PreparingForShutdown"))
124 b = !!(m->action_what & INHIBIT_SHUTDOWN);
126 b = !!(m->action_what & INHIBIT_SLEEP);
128 return sd_bus_message_append(reply, "b", b);
131 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
133 static int method_get_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
134 _cleanup_free_ char *p = NULL;
135 Manager *m = userdata;
144 r = sd_bus_message_read(message, "s", &name);
148 session = hashmap_get(m->sessions, name);
150 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
152 p = session_bus_path(session);
156 return sd_bus_reply_method_return(message, "o", p);
159 static int method_get_session_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
160 _cleanup_free_ char *p = NULL;
161 Session *session = NULL;
162 Manager *m = userdata;
170 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
172 r = sd_bus_message_read(message, "u", &pid);
177 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
179 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
183 r = sd_bus_creds_get_pid(creds, &pid);
188 r = manager_get_session_by_pid(m, pid, &session);
192 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
194 p = session_bus_path(session);
198 return sd_bus_reply_method_return(message, "o", p);
201 static int method_get_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
202 _cleanup_free_ char *p = NULL;
203 Manager *m = userdata;
212 r = sd_bus_message_read(message, "u", &uid);
216 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
218 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user "UID_FMT" known or logged in", uid);
220 p = user_bus_path(user);
224 return sd_bus_reply_method_return(message, "o", p);
227 static int method_get_user_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
228 _cleanup_free_ char *p = NULL;
229 Manager *m = userdata;
238 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
240 r = sd_bus_message_read(message, "u", &pid);
245 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
247 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
251 r = sd_bus_creds_get_pid(creds, &pid);
256 r = manager_get_user_by_pid(m, pid, &user);
260 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "PID "PID_FMT" does not belong to any known or logged in user", pid);
262 p = user_bus_path(user);
266 return sd_bus_reply_method_return(message, "o", p);
269 static int method_get_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
270 _cleanup_free_ char *p = NULL;
271 Manager *m = userdata;
280 r = sd_bus_message_read(message, "s", &name);
284 seat = hashmap_get(m->seats, name);
286 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
288 p = seat_bus_path(seat);
292 return sd_bus_reply_method_return(message, "o", p);
295 static int method_list_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
296 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
297 Manager *m = userdata;
306 r = sd_bus_message_new_method_return(message, &reply);
310 r = sd_bus_message_open_container(reply, 'a', "(susso)");
314 HASHMAP_FOREACH(session, m->sessions, i) {
315 _cleanup_free_ char *p = NULL;
317 p = session_bus_path(session);
321 r = sd_bus_message_append(reply, "(susso)",
323 (uint32_t) session->user->uid,
325 session->seat ? session->seat->id : "",
331 r = sd_bus_message_close_container(reply);
335 return sd_bus_send(bus, reply, NULL);
338 static int method_list_users(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
339 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
340 Manager *m = userdata;
349 r = sd_bus_message_new_method_return(message, &reply);
353 r = sd_bus_message_open_container(reply, 'a', "(uso)");
357 HASHMAP_FOREACH(user, m->users, i) {
358 _cleanup_free_ char *p = NULL;
360 p = user_bus_path(user);
364 r = sd_bus_message_append(reply, "(uso)",
365 (uint32_t) user->uid,
372 r = sd_bus_message_close_container(reply);
376 return sd_bus_send(bus, reply, NULL);
379 static int method_list_seats(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
380 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
381 Manager *m = userdata;
390 r = sd_bus_message_new_method_return(message, &reply);
394 r = sd_bus_message_open_container(reply, 'a', "(so)");
398 HASHMAP_FOREACH(seat, m->seats, i) {
399 _cleanup_free_ char *p = NULL;
401 p = seat_bus_path(seat);
405 r = sd_bus_message_append(reply, "(so)", seat->id, p);
410 r = sd_bus_message_close_container(reply);
414 return sd_bus_send(bus, reply, NULL);
417 static int method_list_inhibitors(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
418 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
419 Manager *m = userdata;
420 Inhibitor *inhibitor;
424 r = sd_bus_message_new_method_return(message, &reply);
428 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
432 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
434 r = sd_bus_message_append(reply, "(ssssuu)",
435 strempty(inhibit_what_to_string(inhibitor->what)),
436 strempty(inhibitor->who),
437 strempty(inhibitor->why),
438 strempty(inhibit_mode_to_string(inhibitor->mode)),
439 (uint32_t) inhibitor->uid,
440 (uint32_t) inhibitor->pid);
445 r = sd_bus_message_close_container(reply);
449 return sd_bus_send(bus, reply, NULL);
452 static int method_create_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
453 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
454 uint32_t uid, leader, audit_id = 0;
455 _cleanup_free_ char *id = NULL;
456 Session *session = NULL;
457 Manager *m = userdata;
470 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
475 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
478 t = _SESSION_TYPE_INVALID;
480 t = session_type_from_string(type);
482 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
486 c = _SESSION_CLASS_INVALID;
488 c = session_class_from_string(class);
490 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
493 if (isempty(desktop))
496 if (!string_is_safe(desktop))
497 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
503 seat = hashmap_get(m->seats, cseat);
505 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
508 if (tty_is_vc(tty)) {
513 else if (seat != m->seat0)
514 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TTY %s is virtual console but seat %s is not seat0", tty, seat->id);
516 v = vtnr_from_tty(tty);
518 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
522 else if (vtnr != (uint32_t) v)
523 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
525 } else if (tty_is_console(tty)) {
529 else if (seat != m->seat0)
530 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
533 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
537 if (seat_has_vts(seat)) {
538 if (!vtnr || vtnr > 63)
539 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
542 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
546 r = sd_bus_message_enter_container(message, 'a', "(sv)");
550 if (t == _SESSION_TYPE_INVALID) {
551 if (!isempty(display))
553 else if (!isempty(tty))
556 t = SESSION_UNSPECIFIED;
559 if (c == _SESSION_CLASS_INVALID) {
560 if (t == SESSION_UNSPECIFIED)
561 c = SESSION_BACKGROUND;
567 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
569 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
573 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
575 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
580 manager_get_session_by_pid(m, leader, &session);
582 _cleanup_free_ char *path = NULL;
583 _cleanup_close_ int fifo_fd = -1;
585 /* Session already exists, client is probably
586 * something like "su" which changes uid but is still
587 * the same session */
589 fifo_fd = session_create_fifo(session);
593 path = session_bus_path(session);
597 log_debug("Sending reply about an existing session: "
598 "id=%s object_path=%s uid=%u runtime_path=%s "
599 "session_fd=%d seat=%s vtnr=%u",
602 (uint32_t) session->user->uid,
603 session->user->runtime_path,
605 session->seat ? session->seat->id : "",
606 (uint32_t) session->vtnr);
608 return sd_bus_reply_method_return(
612 session->user->runtime_path,
614 (uint32_t) session->user->uid,
615 session->seat ? session->seat->id : "",
616 (uint32_t) session->vtnr,
620 audit_session_from_pid(leader, &audit_id);
622 /* Keep our session IDs and the audit session IDs in sync */
624 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
627 /* Wut? There's already a session by this name and we
628 * didn't find it above? Weird, then let's not trust
629 * the audit data and let's better register a new
631 if (hashmap_get(m->sessions, id)) {
632 log_warning("Existing logind session ID %s used by new audit session, ignoring", id);
645 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
648 } while (hashmap_get(m->sessions, id));
651 r = manager_add_user_by_uid(m, uid, &user);
655 r = manager_add_session(m, id, &session);
659 session_set_user(session, user);
661 session->leader = leader;
662 session->audit_id = audit_id;
665 session->remote = remote;
666 session->vtnr = vtnr;
669 session->tty = strdup(tty);
676 if (!isempty(display)) {
677 session->display = strdup(display);
678 if (!session->display) {
684 if (!isempty(remote_user)) {
685 session->remote_user = strdup(remote_user);
686 if (!session->remote_user) {
692 if (!isempty(remote_host)) {
693 session->remote_host = strdup(remote_host);
694 if (!session->remote_host) {
700 if (!isempty(service)) {
701 session->service = strdup(service);
702 if (!session->service) {
708 if (!isempty(desktop)) {
709 session->desktop = strdup(desktop);
710 if (!session->desktop) {
717 r = seat_attach_session(seat, session);
722 r = session_start(session);
726 session->create_message = sd_bus_message_ref(message);
728 /* Now, let's wait until the slice unit and stuff got
729 * created. We send the reply back from
730 * session_send_create_reply(). */
736 session_add_to_gc_queue(session);
739 user_add_to_gc_queue(user);
744 static int method_release_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
745 Manager *m = userdata;
754 r = sd_bus_message_read(message, "s", &name);
758 session = hashmap_get(m->sessions, name);
760 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
762 session_release(session);
764 return sd_bus_reply_method_return(message, NULL);
767 static int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
768 Manager *m = userdata;
777 r = sd_bus_message_read(message, "s", &name);
781 session = hashmap_get(m->sessions, name);
783 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
785 r = session_activate(session);
789 return sd_bus_reply_method_return(message, NULL);
792 static int method_activate_session_on_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
793 const char *session_name, *seat_name;
794 Manager *m = userdata;
803 /* Same as ActivateSession() but refuses to work if
804 * the seat doesn't match */
806 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
810 session = hashmap_get(m->sessions, session_name);
812 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", session_name);
814 seat = hashmap_get(m->seats, seat_name);
816 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", seat_name);
818 if (session->seat != seat)
819 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
821 r = session_activate(session);
825 return sd_bus_reply_method_return(message, NULL);
828 static int method_lock_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
829 Manager *m = userdata;
838 r = sd_bus_message_read(message, "s", &name);
842 session = hashmap_get(m->sessions, name);
844 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
846 r = session_send_lock(session, streq(sd_bus_message_get_member(message), "LockSession"));
850 return sd_bus_reply_method_return(message, NULL);
853 static int method_lock_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
854 Manager *m = userdata;
861 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
865 return sd_bus_reply_method_return(message, NULL);
868 static int method_kill_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
869 const char *name, *swho;
870 Manager *m = userdata;
880 r = sd_bus_message_read(message, "ssi", &name, &swho, &signo);
887 who = kill_who_from_string(swho);
889 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
892 if (signo <= 0 || signo >= _NSIG)
893 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
895 session = hashmap_get(m->sessions, name);
897 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
899 r = session_kill(session, who, signo);
903 return sd_bus_reply_method_return(message, NULL);
906 static int method_kill_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
907 Manager *m = userdata;
917 r = sd_bus_message_read(message, "ui", &uid, &signo);
921 if (signo <= 0 || signo >= _NSIG)
922 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
924 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
926 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user "UID_FMT" known or logged in", uid);
928 r = user_kill(user, signo);
932 return sd_bus_reply_method_return(message, NULL);
935 static int method_terminate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
936 Manager *m = userdata;
945 r = sd_bus_message_read(message, "s", &name);
949 session = hashmap_get(m->sessions, name);
951 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
953 r = session_stop(session, true);
957 return sd_bus_reply_method_return(message, NULL);
960 static int method_terminate_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
961 Manager *m = userdata;
970 r = sd_bus_message_read(message, "u", &uid);
974 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
976 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user "UID_FMT" known or logged in", uid);
978 r = user_stop(user, true);
982 return sd_bus_reply_method_return(message, NULL);
985 static int method_terminate_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
986 Manager *m = userdata;
995 r = sd_bus_message_read(message, "s", &name);
999 seat = hashmap_get(m->seats, name);
1001 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
1003 r = seat_stop_sessions(seat, true);
1007 return sd_bus_reply_method_return(message, NULL);
1010 static int method_set_user_linger(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1011 _cleanup_free_ char *cc = NULL;
1012 Manager *m = userdata;
1023 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1030 return errno ? -errno : -ENOENT;
1032 r = bus_verify_polkit_async(
1035 "org.freedesktop.login1.set-user-linger",
1037 &m->polkit_registry,
1042 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1044 mkdir_p_label("/var/lib/systemd", 0755);
1046 r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0);
1050 cc = cescape(pw->pw_name);
1054 path = strappenda("/var/lib/systemd/linger/", cc);
1062 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1069 if (r < 0 && errno != ENOENT)
1072 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1074 user_add_to_gc_queue(u);
1077 return sd_bus_reply_method_return(message, NULL);
1080 static int trigger_device(Manager *m, struct udev_device *d) {
1081 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
1082 struct udev_list_entry *first, *item;
1087 e = udev_enumerate_new(m->udev);
1092 r = udev_enumerate_add_match_parent(e, d);
1097 r = udev_enumerate_scan_devices(e);
1101 first = udev_enumerate_get_list_entry(e);
1102 udev_list_entry_foreach(item, first) {
1103 _cleanup_free_ char *t = NULL;
1106 p = udev_list_entry_get_name(item);
1108 t = strappend(p, "/uevent");
1112 write_string_file(t, "change");
1118 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1119 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
1120 _cleanup_free_ char *rule = NULL, *file = NULL;
1121 const char *id_for_seat;
1128 d = udev_device_new_from_syspath(m->udev, sysfs);
1132 if (!udev_device_has_tag(d, "seat"))
1135 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
1139 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1142 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1145 mkdir_p_label("/etc/udev/rules.d", 0755);
1146 mac_selinux_init("/etc");
1147 r = write_string_file_atomic_label(file, rule);
1151 return trigger_device(m, d);
1154 static int flush_devices(Manager *m) {
1155 _cleanup_closedir_ DIR *d;
1159 d = opendir("/etc/udev/rules.d");
1161 if (errno != ENOENT)
1162 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1166 while ((de = readdir(d))) {
1168 if (!dirent_is_file(de))
1171 if (!startswith(de->d_name, "72-seat-"))
1174 if (!endswith(de->d_name, ".rules"))
1177 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1178 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1182 return trigger_device(m, NULL);
1185 static int method_attach_device(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1186 const char *sysfs, *seat;
1187 Manager *m = userdata;
1194 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1198 if (!path_startswith(sysfs, "/sys"))
1199 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1201 if (!seat_name_is_valid(seat))
1202 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1204 r = bus_verify_polkit_async(
1207 "org.freedesktop.login1.attach-device",
1209 &m->polkit_registry,
1214 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1216 r = attach_device(m, seat, sysfs);
1220 return sd_bus_reply_method_return(message, NULL);
1223 static int method_flush_devices(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1224 Manager *m = userdata;
1231 r = sd_bus_message_read(message, "b", &interactive);
1235 r = bus_verify_polkit_async(
1238 "org.freedesktop.login1.flush-devices",
1240 &m->polkit_registry,
1245 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1247 r = flush_devices(m);
1251 return sd_bus_reply_method_return(message, NULL);
1254 static int have_multiple_sessions(
1263 /* Check for other users' sessions. Greeter sessions do not
1264 * count, and non-login sessions do not count either. */
1265 HASHMAP_FOREACH(session, m->sessions, i)
1266 if (session->class == SESSION_USER &&
1267 session->user->uid != uid)
1273 static int bus_manager_log_shutdown(
1276 const char *unit_name) {
1283 if (w != INHIBIT_SHUTDOWN)
1286 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1287 p = "MESSAGE=System is powering down.";
1288 q = "SHUTDOWN=power-off";
1289 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1290 p = "MESSAGE=System is halting.";
1291 q = "SHUTDOWN=halt";
1292 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1293 p = "MESSAGE=System is rebooting.";
1294 q = "SHUTDOWN=reboot";
1295 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1296 p = "MESSAGE=System is rebooting with kexec.";
1297 q = "SHUTDOWN=kexec";
1299 p = "MESSAGE=System is shutting down.";
1303 return log_struct(LOG_NOTICE,
1304 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
1310 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1311 Manager *m = userdata;
1316 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1320 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1325 if (until <= now(CLOCK_MONOTONIC))
1328 /* We want to ignore the lid switch for a while after each
1329 * suspend, and after boot-up. Hence let's install a timer for
1330 * this. As long as the event source exists we ignore the lid
1333 if (m->lid_switch_ignore_event_source) {
1336 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1343 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1345 r = sd_event_add_time(
1347 &m->lid_switch_ignore_event_source,
1350 lid_switch_ignore_handler, m);
1355 static int execute_shutdown_or_sleep(
1358 const char *unit_name,
1359 sd_bus_error *error) {
1361 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1368 assert(w < _INHIBIT_WHAT_MAX);
1371 bus_manager_log_shutdown(m, w, unit_name);
1373 r = sd_bus_call_method(
1375 "org.freedesktop.systemd1",
1376 "/org/freedesktop/systemd1",
1377 "org.freedesktop.systemd1.Manager",
1381 "ss", unit_name, "replace-irreversibly");
1385 r = sd_bus_message_read(reply, "o", &p);
1393 m->action_unit = unit_name;
1394 free(m->action_job);
1398 /* Make sure the lid switch is ignored for a while */
1399 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + IGNORE_LID_SWITCH_SUSPEND_USEC);
1404 static int delay_shutdown_or_sleep(
1407 const char *unit_name) {
1411 assert(w < _INHIBIT_WHAT_MAX);
1414 m->action_timestamp = now(CLOCK_MONOTONIC);
1415 m->action_unit = unit_name;
1421 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1423 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1424 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1425 [INHIBIT_SLEEP] = "PrepareForSleep"
1428 int active = _active;
1432 assert(w < _INHIBIT_WHAT_MAX);
1433 assert(signal_name[w]);
1435 return sd_bus_emit_signal(m->bus,
1436 "/org/freedesktop/login1",
1437 "org.freedesktop.login1.Manager",
1443 int bus_manager_shutdown_or_sleep_now_or_later(
1445 const char *unit_name,
1447 sd_bus_error *error) {
1455 assert(w <= _INHIBIT_WHAT_MAX);
1456 assert(!m->action_job);
1458 /* Tell everybody to prepare for shutdown/sleep */
1459 send_prepare_for(m, w, true);
1462 m->inhibit_delay_max > 0 &&
1463 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1466 /* Shutdown is delayed, keep in mind what we
1467 * want to do, and start a timeout */
1468 r = delay_shutdown_or_sleep(m, w, unit_name);
1470 /* Shutdown is not delayed, execute it
1472 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1477 static int method_do_shutdown_or_sleep(
1479 sd_bus_message *message,
1480 const char *unit_name,
1483 const char *action_multiple_sessions,
1484 const char *action_ignore_inhibit,
1485 const char *sleep_verb,
1486 sd_bus_message_handler_t method,
1487 sd_bus_error *error) {
1489 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
1490 bool multiple_sessions, blocked;
1498 assert(w <= _INHIBIT_WHAT_MAX);
1500 assert(action_multiple_sessions);
1501 assert(action_ignore_inhibit);
1504 r = sd_bus_message_read(message, "b", &interactive);
1508 /* Don't allow multiple jobs being executed at the same time */
1510 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1513 r = can_sleep(sleep_verb);
1518 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
1521 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_UID, &creds);
1525 r = sd_bus_creds_get_uid(creds, &uid);
1529 r = have_multiple_sessions(m, uid);
1533 multiple_sessions = r > 0;
1534 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1536 if (multiple_sessions) {
1537 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, interactive, &m->polkit_registry, error);
1541 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1545 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, interactive, &m->polkit_registry, error);
1549 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1552 if (!multiple_sessions && !blocked) {
1553 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, interactive, &m->polkit_registry, error);
1557 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1560 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1564 return sd_bus_reply_method_return(message, NULL);
1567 static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1568 Manager *m = userdata;
1570 return method_do_shutdown_or_sleep(
1572 SPECIAL_POWEROFF_TARGET,
1574 "org.freedesktop.login1.power-off",
1575 "org.freedesktop.login1.power-off-multiple-sessions",
1576 "org.freedesktop.login1.power-off-ignore-inhibit",
1582 static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1583 Manager *m = userdata;
1585 return method_do_shutdown_or_sleep(
1587 SPECIAL_REBOOT_TARGET,
1589 "org.freedesktop.login1.reboot",
1590 "org.freedesktop.login1.reboot-multiple-sessions",
1591 "org.freedesktop.login1.reboot-ignore-inhibit",
1597 static int method_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1598 Manager *m = userdata;
1600 return method_do_shutdown_or_sleep(
1602 SPECIAL_SUSPEND_TARGET,
1604 "org.freedesktop.login1.suspend",
1605 "org.freedesktop.login1.suspend-multiple-sessions",
1606 "org.freedesktop.login1.suspend-ignore-inhibit",
1612 static int method_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1613 Manager *m = userdata;
1615 return method_do_shutdown_or_sleep(
1617 SPECIAL_HIBERNATE_TARGET,
1619 "org.freedesktop.login1.hibernate",
1620 "org.freedesktop.login1.hibernate-multiple-sessions",
1621 "org.freedesktop.login1.hibernate-ignore-inhibit",
1627 static int method_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1628 Manager *m = userdata;
1630 return method_do_shutdown_or_sleep(
1632 SPECIAL_HYBRID_SLEEP_TARGET,
1634 "org.freedesktop.login1.hibernate",
1635 "org.freedesktop.login1.hibernate-multiple-sessions",
1636 "org.freedesktop.login1.hibernate-ignore-inhibit",
1638 method_hybrid_sleep,
1642 static int method_can_shutdown_or_sleep(
1644 sd_bus_message *message,
1647 const char *action_multiple_sessions,
1648 const char *action_ignore_inhibit,
1649 const char *sleep_verb,
1650 sd_bus_error *error) {
1652 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
1653 bool multiple_sessions, challenge, blocked;
1654 const char *result = NULL;
1661 assert(w <= _INHIBIT_WHAT_MAX);
1663 assert(action_multiple_sessions);
1664 assert(action_ignore_inhibit);
1667 r = can_sleep(sleep_verb);
1671 return sd_bus_reply_method_return(message, "s", "na");
1674 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_UID, &creds);
1678 r = sd_bus_creds_get_uid(creds, &uid);
1682 r = have_multiple_sessions(m, uid);
1686 multiple_sessions = r > 0;
1687 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1689 if (multiple_sessions) {
1690 r = bus_verify_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, false, &challenge, error);
1697 result = "challenge";
1703 r = bus_verify_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, false, &challenge, error);
1707 if (r > 0 && !result)
1709 else if (challenge && (!result || streq(result, "yes")))
1710 result = "challenge";
1715 if (!multiple_sessions && !blocked) {
1716 /* If neither inhibit nor multiple sessions
1717 * apply then just check the normal policy */
1719 r = bus_verify_polkit(message, CAP_SYS_BOOT, action, false, &challenge, error);
1726 result = "challenge";
1731 return sd_bus_reply_method_return(message, "s", result);
1734 static int method_can_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1735 Manager *m = userdata;
1737 return method_can_shutdown_or_sleep(
1740 "org.freedesktop.login1.power-off",
1741 "org.freedesktop.login1.power-off-multiple-sessions",
1742 "org.freedesktop.login1.power-off-ignore-inhibit",
1747 static int method_can_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1748 Manager *m = userdata;
1750 return method_can_shutdown_or_sleep(
1753 "org.freedesktop.login1.reboot",
1754 "org.freedesktop.login1.reboot-multiple-sessions",
1755 "org.freedesktop.login1.reboot-ignore-inhibit",
1760 static int method_can_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1761 Manager *m = userdata;
1763 return method_can_shutdown_or_sleep(
1766 "org.freedesktop.login1.suspend",
1767 "org.freedesktop.login1.suspend-multiple-sessions",
1768 "org.freedesktop.login1.suspend-ignore-inhibit",
1773 static int method_can_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1774 Manager *m = userdata;
1776 return method_can_shutdown_or_sleep(
1779 "org.freedesktop.login1.hibernate",
1780 "org.freedesktop.login1.hibernate-multiple-sessions",
1781 "org.freedesktop.login1.hibernate-ignore-inhibit",
1786 static int method_can_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1787 Manager *m = userdata;
1789 return method_can_shutdown_or_sleep(
1792 "org.freedesktop.login1.hibernate",
1793 "org.freedesktop.login1.hibernate-multiple-sessions",
1794 "org.freedesktop.login1.hibernate-ignore-inhibit",
1799 static int method_inhibit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1800 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
1801 const char *who, *why, *what, *mode;
1802 _cleanup_free_ char *id = NULL;
1803 _cleanup_close_ int fifo_fd = -1;
1804 Manager *m = userdata;
1805 Inhibitor *i = NULL;
1816 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
1820 w = inhibit_what_from_string(what);
1822 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
1824 mm = inhibit_mode_from_string(mode);
1826 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
1828 /* Delay is only supported for shutdown/sleep */
1829 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
1830 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
1832 /* Don't allow taking delay locks while we are already
1833 * executing the operation. We shouldn't create the impression
1834 * that the lock was successful if the machine is about to go
1835 * down/suspend any moment. */
1836 if (m->action_what & w)
1837 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
1839 r = bus_verify_polkit_async(message, CAP_SYS_BOOT,
1840 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
1841 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
1842 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
1843 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
1844 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
1845 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
1846 "org.freedesktop.login1.inhibit-handle-lid-switch",
1847 false, &m->polkit_registry, error);
1851 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1853 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID, &creds);
1857 r = sd_bus_creds_get_uid(creds, &uid);
1861 r = sd_bus_creds_get_pid(creds, &pid);
1869 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
1872 } while (hashmap_get(m->inhibitors, id));
1874 r = manager_add_inhibitor(m, id, &i);
1882 i->why = strdup(why);
1883 i->who = strdup(who);
1885 if (!i->why || !i->who) {
1890 fifo_fd = inhibitor_create_fifo(i);
1898 return sd_bus_reply_method_return(message, "h", fifo_fd);
1907 const sd_bus_vtable manager_vtable[] = {
1908 SD_BUS_VTABLE_START(0),
1910 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
1911 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
1912 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
1913 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
1914 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1915 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1916 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1917 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1918 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1919 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
1920 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
1921 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
1922 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
1923 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
1924 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
1925 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
1926 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
1927 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
1928 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
1930 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
1931 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
1932 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
1933 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
1934 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
1935 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
1936 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
1937 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
1938 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
1939 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
1940 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
1941 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
1942 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
1943 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, 0),
1944 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, 0),
1945 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, 0),
1946 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, 0),
1947 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
1948 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
1949 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
1950 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
1951 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
1952 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
1953 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
1954 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
1955 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
1956 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
1957 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
1958 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
1959 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
1960 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
1961 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
1962 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
1963 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
1964 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
1965 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
1967 SD_BUS_SIGNAL("SessionNew", "so", 0),
1968 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
1969 SD_BUS_SIGNAL("UserNew", "uo", 0),
1970 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
1971 SD_BUS_SIGNAL("SeatNew", "so", 0),
1972 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
1973 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
1974 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
1979 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
1988 if (streq(result, "done"))
1989 r = session_send_create_reply(s, NULL);
1991 _cleanup_bus_error_free_ sd_bus_error e = SD_BUS_ERROR_NULL;
1993 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
1994 r = session_send_create_reply(s, &e);
2000 int match_job_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2001 const char *path, *result, *unit;
2002 Manager *m = userdata;
2012 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2014 bus_log_parse_error(r);
2018 if (m->action_job && streq(m->action_job, path)) {
2019 log_info("Operation finished.");
2021 /* Tell people that they now may take a lock again */
2022 send_prepare_for(m, m->action_what, false);
2024 free(m->action_job);
2025 m->action_job = NULL;
2026 m->action_unit = NULL;
2031 session = hashmap_get(m->session_units, unit);
2034 if (streq_ptr(path, session->scope_job)) {
2035 free(session->scope_job);
2036 session->scope_job = NULL;
2039 session_jobs_reply(session, unit, result);
2041 session_save(session);
2042 session_add_to_gc_queue(session);
2045 user = hashmap_get(m->user_units, unit);
2048 if (streq_ptr(path, user->service_job)) {
2049 free(user->service_job);
2050 user->service_job = NULL;
2053 if (streq_ptr(path, user->slice_job)) {
2054 free(user->slice_job);
2055 user->slice_job = NULL;
2058 LIST_FOREACH(sessions_by_user, session, user->sessions) {
2059 session_jobs_reply(session, unit, result);
2063 user_add_to_gc_queue(user);
2069 int match_unit_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2070 const char *path, *unit;
2071 Manager *m = userdata;
2080 r = sd_bus_message_read(message, "so", &unit, &path);
2082 bus_log_parse_error(r);
2086 session = hashmap_get(m->session_units, unit);
2088 session_add_to_gc_queue(session);
2090 user = hashmap_get(m->user_units, unit);
2092 user_add_to_gc_queue(user);
2097 int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2098 _cleanup_free_ char *unit = NULL;
2099 Manager *m = userdata;
2109 path = sd_bus_message_get_path(message);
2113 r = unit_name_from_dbus_path(path, &unit);
2114 if (r == -EINVAL) /* not a unit */
2119 session = hashmap_get(m->session_units, unit);
2121 session_add_to_gc_queue(session);
2123 user = hashmap_get(m->user_units, unit);
2125 user_add_to_gc_queue(user);
2130 int match_reloading(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2131 Manager *m = userdata;
2138 r = sd_bus_message_read(message, "b", &b);
2140 bus_log_parse_error(r);
2147 /* systemd finished reloading, let's recheck all our sessions */
2148 log_debug("System manager has been reloaded, rechecking sessions...");
2150 HASHMAP_FOREACH(session, m->sessions, i)
2151 session_add_to_gc_queue(session);
2156 int match_name_owner_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
2157 const char *name, *old, *new;
2158 Manager *m = userdata;
2166 r = sd_bus_message_read(message, "sss", &name, &old, &new);
2168 bus_log_parse_error(r);
2172 if (isempty(old) || !isempty(new))
2175 key = set_remove(m->busnames, (char*) old);
2179 /* Drop all controllers owned by this name */
2183 HASHMAP_FOREACH(session, m->sessions, i)
2184 if (session_is_controller(session, old))
2185 session_drop_controller(session);
2190 int manager_send_changed(Manager *manager, const char *property, ...) {
2195 l = strv_from_stdarg_alloca(property);
2197 return sd_bus_emit_properties_changed_strv(
2199 "/org/freedesktop/login1",
2200 "org.freedesktop.login1.Manager",
2204 int manager_dispatch_delayed(Manager *manager) {
2205 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2206 Inhibitor *offending = NULL;
2211 if (manager->action_what == 0 || manager->action_job)
2214 /* Continue delay? */
2215 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
2216 _cleanup_free_ char *comm = NULL, *u = NULL;
2218 get_process_comm(offending->pid, &comm);
2219 u = uid_to_name(offending->uid);
2221 if (manager->action_timestamp + manager->inhibit_delay_max > now(CLOCK_MONOTONIC))
2224 log_info("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
2225 offending->uid, strna(u),
2226 offending->pid, strna(comm));
2229 /* Actually do the operation */
2230 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
2232 log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
2234 manager->action_unit = NULL;
2235 manager->action_what = 0;
2242 int manager_start_scope(
2247 const char *description,
2248 const char *after, const char *after2,
2249 sd_bus_error *error,
2252 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
2259 r = sd_bus_message_new_method_call(
2262 "org.freedesktop.systemd1",
2263 "/org/freedesktop/systemd1",
2264 "org.freedesktop.systemd1.Manager",
2265 "StartTransientUnit");
2269 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
2273 r = sd_bus_message_open_container(m, 'a', "(sv)");
2277 if (!isempty(slice)) {
2278 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
2283 if (!isempty(description)) {
2284 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
2289 if (!isempty(after)) {
2290 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
2295 if (!isempty(after2)) {
2296 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
2301 /* cgroup empty notification is not available in containers
2302 * currently. To make this less problematic, let's shorten the
2303 * stop timeout for sessions, so that we don't wait
2306 /* Make sure that the session shells are terminated with
2307 * SIGHUP since bash and friends tend to ignore SIGTERM */
2308 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
2312 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
2316 r = sd_bus_message_close_container(m);
2320 r = sd_bus_message_append(m, "a(sa(sv))", 0);
2324 r = sd_bus_call(manager->bus, m, 0, error, &reply);
2332 r = sd_bus_message_read(reply, "o", &j);
2346 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
2347 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2353 r = sd_bus_call_method(
2355 "org.freedesktop.systemd1",
2356 "/org/freedesktop/systemd1",
2357 "org.freedesktop.systemd1.Manager",
2361 "ss", unit, "fail");
2369 r = sd_bus_message_read(reply, "o", &j);
2383 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
2384 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2390 r = sd_bus_call_method(
2392 "org.freedesktop.systemd1",
2393 "/org/freedesktop/systemd1",
2394 "org.freedesktop.systemd1.Manager",
2398 "ss", unit, "fail");
2400 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
2401 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
2406 sd_bus_error_free(error);
2417 r = sd_bus_message_read(reply, "o", &j);
2431 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
2432 _cleanup_free_ char *path = NULL;
2438 path = unit_dbus_path_from_name(scope);
2442 r = sd_bus_call_method(
2444 "org.freedesktop.systemd1",
2446 "org.freedesktop.systemd1.Scope",
2452 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
2453 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
2454 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
2455 sd_bus_error_free(error);
2465 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
2469 return sd_bus_call_method(
2471 "org.freedesktop.systemd1",
2472 "/org/freedesktop/systemd1",
2473 "org.freedesktop.systemd1.Manager",
2477 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
2480 int manager_unit_is_active(Manager *manager, const char *unit) {
2481 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2482 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2483 _cleanup_free_ char *path = NULL;
2490 path = unit_dbus_path_from_name(unit);
2494 r = sd_bus_get_property(
2496 "org.freedesktop.systemd1",
2498 "org.freedesktop.systemd1.Unit",
2504 /* systemd might have droppped off momentarily, let's
2505 * not make this an error */
2506 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
2507 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
2510 /* If the unit is already unloaded then it's not
2512 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
2513 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
2519 r = sd_bus_message_read(reply, "s", &state);
2523 return !streq(state, "inactive") && !streq(state, "failed");
2526 int manager_job_is_active(Manager *manager, const char *path) {
2527 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
2528 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
2534 r = sd_bus_get_property(
2536 "org.freedesktop.systemd1",
2538 "org.freedesktop.systemd1.Job",
2544 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
2545 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
2548 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
2554 /* We don't actually care about the state really. The fact
2555 * that we could read the job state is enough for us */