1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
13 #include "sd-messages.h"
15 #include "alloc-util.h"
16 #include "audit-util.h"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
19 //#include "bus-unit-util.h"
21 #include "dirent-util.h"
22 //#include "efivars.h"
25 #include "fileio-label.h"
26 #include "format-util.h"
30 #include "path-util.h"
31 #include "process-util.h"
32 //#include "cgroup-util.h"
33 #include "selinux-util.h"
34 #include "sleep-config.h"
35 //#include "special.h"
37 #include "terminal-util.h"
38 #include "udev-util.h"
39 #include "unit-name.h"
40 #include "user-util.h"
41 #include "utmp-wtmp.h"
43 /// Additional includes needed by elogind
44 #include "elogind-dbus.h"
46 static int get_sender_session(Manager *m, sd_bus_message *message, sd_bus_error *error, Session **ret) {
48 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
53 /* Get client login session. This is not what you are looking for these days,
54 * as apps may instead belong to a user service unit. This includes terminal
55 * emulators and hence command-line apps. */
56 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
60 r = sd_bus_creds_get_session(creds, &name);
66 session = hashmap_get(m->sessions, name);
74 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID,
75 "Caller does not belong to any known session");
78 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
86 return get_sender_session(m, message, error, ret);
88 session = hashmap_get(m->sessions, name);
90 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
96 static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) {
98 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
103 /* Note that we get the owner UID of the session, not the actual client UID here! */
104 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
108 r = sd_bus_creds_get_owner_uid(creds, &uid);
114 user = hashmap_get(m->users, UID_TO_PTR(uid));
122 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "Caller does not belong to any logged in user or lingering user");
125 int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
132 if (!uid_is_valid(uid))
133 return get_sender_user(m, message, error, ret);
135 user = hashmap_get(m->users, UID_TO_PTR(uid));
137 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "User ID "UID_FMT" is not logged in or lingering", uid);
143 int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) {
154 r = manager_get_session_from_creds(m, message, NULL, error, &session);
158 seat = session->seat;
160 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat.");
162 seat = hashmap_get(m->seats, name);
164 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
171 static int property_get_idle_hint(
174 const char *interface,
175 const char *property,
176 sd_bus_message *reply,
178 sd_bus_error *error) {
180 Manager *m = userdata;
186 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
189 static int property_get_idle_since_hint(
192 const char *interface,
193 const char *property,
194 sd_bus_message *reply,
196 sd_bus_error *error) {
198 Manager *m = userdata;
199 dual_timestamp t = DUAL_TIMESTAMP_NULL;
205 manager_get_idle_hint(m, &t);
207 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
210 static int property_get_inhibited(
213 const char *interface,
214 const char *property,
215 sd_bus_message *reply,
217 sd_bus_error *error) {
219 Manager *m = userdata;
226 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
228 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
231 static int property_get_preparing(
234 const char *interface,
235 const char *property,
236 sd_bus_message *reply,
238 sd_bus_error *error) {
240 Manager *m = userdata;
247 if (streq(property, "PreparingForShutdown"))
248 b = !!(m->action_what & INHIBIT_SHUTDOWN);
250 b = !!(m->action_what & INHIBIT_SLEEP);
252 return sd_bus_message_append(reply, "b", b);
255 static int property_get_scheduled_shutdown(
258 const char *interface,
259 const char *property,
260 sd_bus_message *reply,
262 sd_bus_error *error) {
264 Manager *m = userdata;
271 r = sd_bus_message_open_container(reply, 'r', "st");
275 r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
279 return sd_bus_message_close_container(reply);
282 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
284 static int property_get_docked(
287 const char *interface,
288 const char *property,
289 sd_bus_message *reply,
291 sd_bus_error *error) {
293 Manager *m = userdata;
299 return sd_bus_message_append(reply, "b", manager_is_docked_or_external_displays(m));
302 static int property_get_current_sessions(
305 const char *interface,
306 const char *property,
307 sd_bus_message *reply,
309 sd_bus_error *error) {
311 Manager *m = userdata;
317 return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->sessions));
320 static int property_get_current_inhibitors(
323 const char *interface,
324 const char *property,
325 sd_bus_message *reply,
327 sd_bus_error *error) {
329 Manager *m = userdata;
335 return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->inhibitors));
338 static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
339 _cleanup_free_ char *p = NULL;
340 Manager *m = userdata;
348 r = sd_bus_message_read(message, "s", &name);
352 r = manager_get_session_from_creds(m, message, name, error, &session);
356 p = session_bus_path(session);
360 return sd_bus_reply_method_return(message, "o", p);
363 /* Get login session of a process. This is not what you are looking for these days,
364 * as apps may instead belong to a user service unit. This includes terminal
365 * emulators and hence command-line apps. */
366 static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
367 _cleanup_free_ char *p = NULL;
368 Session *session = NULL;
369 Manager *m = userdata;
376 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
378 r = sd_bus_message_read(message, "u", &pid);
385 r = manager_get_session_from_creds(m, message, NULL, error, &session);
389 r = manager_get_session_by_pid(m, pid, &session);
394 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
397 p = session_bus_path(session);
401 return sd_bus_reply_method_return(message, "o", p);
404 static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
405 _cleanup_free_ char *p = NULL;
406 Manager *m = userdata;
414 r = sd_bus_message_read(message, "u", &uid);
418 r = manager_get_user_from_creds(m, message, uid, error, &user);
422 p = user_bus_path(user);
426 return sd_bus_reply_method_return(message, "o", p);
429 static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
430 _cleanup_free_ char *p = NULL;
431 Manager *m = userdata;
439 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
441 r = sd_bus_message_read(message, "u", &pid);
448 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
452 r = manager_get_user_by_pid(m, pid, &user);
456 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID,
457 "PID "PID_FMT" does not belong to any logged in user or lingering user",
461 p = user_bus_path(user);
465 return sd_bus_reply_method_return(message, "o", p);
468 static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
469 _cleanup_free_ char *p = NULL;
470 Manager *m = userdata;
478 r = sd_bus_message_read(message, "s", &name);
482 r = manager_get_seat_from_creds(m, message, name, error, &seat);
486 p = seat_bus_path(seat);
490 return sd_bus_reply_method_return(message, "o", p);
493 static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
494 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
495 Manager *m = userdata;
503 r = sd_bus_message_new_method_return(message, &reply);
507 r = sd_bus_message_open_container(reply, 'a', "(susso)");
511 HASHMAP_FOREACH(session, m->sessions, i) {
512 _cleanup_free_ char *p = NULL;
514 p = session_bus_path(session);
518 r = sd_bus_message_append(reply, "(susso)",
520 (uint32_t) session->user->uid,
522 session->seat ? session->seat->id : "",
528 r = sd_bus_message_close_container(reply);
532 return sd_bus_send(NULL, reply, NULL);
535 static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
536 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
537 Manager *m = userdata;
545 r = sd_bus_message_new_method_return(message, &reply);
549 r = sd_bus_message_open_container(reply, 'a', "(uso)");
553 HASHMAP_FOREACH(user, m->users, i) {
554 _cleanup_free_ char *p = NULL;
556 p = user_bus_path(user);
560 r = sd_bus_message_append(reply, "(uso)",
561 (uint32_t) user->uid,
568 r = sd_bus_message_close_container(reply);
572 return sd_bus_send(NULL, reply, NULL);
575 static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
576 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
577 Manager *m = userdata;
585 r = sd_bus_message_new_method_return(message, &reply);
589 r = sd_bus_message_open_container(reply, 'a', "(so)");
593 HASHMAP_FOREACH(seat, m->seats, i) {
594 _cleanup_free_ char *p = NULL;
596 p = seat_bus_path(seat);
600 r = sd_bus_message_append(reply, "(so)", seat->id, p);
605 r = sd_bus_message_close_container(reply);
609 return sd_bus_send(NULL, reply, NULL);
612 static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
613 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
614 Manager *m = userdata;
615 Inhibitor *inhibitor;
622 r = sd_bus_message_new_method_return(message, &reply);
626 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
630 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
632 r = sd_bus_message_append(reply, "(ssssuu)",
633 strempty(inhibit_what_to_string(inhibitor->what)),
634 strempty(inhibitor->who),
635 strempty(inhibitor->why),
636 strempty(inhibit_mode_to_string(inhibitor->mode)),
637 (uint32_t) inhibitor->uid,
638 (uint32_t) inhibitor->pid);
643 r = sd_bus_message_close_container(reply);
647 return sd_bus_send(NULL, reply, NULL);
650 static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
651 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
652 _cleanup_free_ char *id = NULL;
653 Session *session = NULL;
654 uint32_t audit_id = 0;
655 Manager *m = userdata;
669 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
670 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
672 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
676 if (!uid_is_valid(uid))
677 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
678 if (leader < 0 || leader == 1 || leader == getpid_cached())
679 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
682 t = _SESSION_TYPE_INVALID;
684 t = session_type_from_string(type);
686 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
690 c = _SESSION_CLASS_INVALID;
692 c = session_class_from_string(class);
694 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
697 if (isempty(desktop))
700 if (!string_is_safe(desktop))
701 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
707 seat = hashmap_get(m->seats, cseat);
709 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
712 if (tty_is_vc(tty)) {
717 else if (seat != m->seat0)
718 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);
720 v = vtnr_from_tty(tty);
722 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
726 else if (vtnr != (uint32_t) v)
727 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
729 } else if (tty_is_console(tty)) {
733 else if (seat != m->seat0)
734 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
737 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
741 if (seat_has_vts(seat)) {
742 if (vtnr <= 0 || vtnr > 63)
743 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
746 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
750 if (t == _SESSION_TYPE_INVALID) {
751 if (!isempty(display))
753 else if (!isempty(tty))
756 t = SESSION_UNSPECIFIED;
759 if (c == _SESSION_CLASS_INVALID) {
760 if (t == SESSION_UNSPECIFIED)
761 c = SESSION_BACKGROUND;
767 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
769 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
773 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
778 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
779 * that avoids creating a logind session. */
780 r = manager_get_user_by_pid(m, leader, NULL);
784 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice");
787 * Old gdm and lightdm start the user-session on the same VT as
788 * the greeter session. But they destroy the greeter session
789 * after the user-session and want the user-session to take
790 * over the VT. We need to support this for
791 * backwards-compatibility, so make sure we allow new sessions
792 * on a VT that a greeter is running on. Furthermore, to allow
793 * re-logins, we have to allow a greeter to take over a used VT for
794 * the exact same reasons.
796 if (c != SESSION_GREETER &&
798 vtnr < m->seat0->position_count &&
799 m->seat0->positions[vtnr] &&
800 m->seat0->positions[vtnr]->class != SESSION_GREETER)
801 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
803 if (hashmap_size(m->sessions) >= m->sessions_max)
804 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
806 (void) audit_session_from_pid(leader, &audit_id);
807 if (audit_session_is_valid(audit_id)) {
808 /* Keep our session IDs and the audit session IDs in sync */
810 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
813 /* Wut? There's already a session by this name and we
814 * didn't find it above? Weird, then let's not trust
815 * the audit data and let's better register a new
817 if (hashmap_get(m->sessions, id)) {
818 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
819 audit_id = AUDIT_SESSION_INVALID;
828 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
831 } while (hashmap_get(m->sessions, id));
834 r = manager_add_user_by_uid(m, uid, &user);
838 r = manager_add_session(m, id, &session);
842 session_set_user(session, user);
844 session->leader = leader;
845 session->audit_id = audit_id;
848 session->remote = remote;
849 session->vtnr = vtnr;
852 session->tty = strdup(tty);
859 if (!isempty(display)) {
860 session->display = strdup(display);
861 if (!session->display) {
867 if (!isempty(remote_user)) {
868 session->remote_user = strdup(remote_user);
869 if (!session->remote_user) {
875 if (!isempty(remote_host)) {
876 session->remote_host = strdup(remote_host);
877 if (!session->remote_host) {
883 if (!isempty(service)) {
884 session->service = strdup(service);
885 if (!session->service) {
891 if (!isempty(desktop)) {
892 session->desktop = strdup(desktop);
893 if (!session->desktop) {
900 r = seat_attach_session(seat, session);
905 r = sd_bus_message_enter_container(message, 'a', "(sv)");
909 r = session_start(session, message);
913 r = sd_bus_message_exit_container(message);
917 session->create_message = sd_bus_message_ref(message);
919 #if 0 /// UNNEEDED by elogind
920 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
921 * session_send_create_reply(). */
923 /* We reply directly. */
925 r = session_send_create_reply(session, NULL);
934 session_add_to_gc_queue(session);
937 user_add_to_gc_queue(user);
942 static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
943 Manager *m = userdata;
951 r = sd_bus_message_read(message, "s", &name);
955 r = manager_get_session_from_creds(m, message, name, error, &session);
959 r = session_release(session);
963 #if 1 /// elogind must queue this session
964 session_add_to_gc_queue(session);
966 return sd_bus_reply_method_return(message, NULL);
969 static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
970 Manager *m = userdata;
978 r = sd_bus_message_read(message, "s", &name);
982 r = manager_get_session_from_creds(m, message, name, error, &session);
986 return bus_session_method_activate(message, session, error);
989 static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
990 const char *session_name, *seat_name;
991 Manager *m = userdata;
999 /* Same as ActivateSession() but refuses to work if
1000 * the seat doesn't match */
1002 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
1006 r = manager_get_session_from_creds(m, message, session_name, error, &session);
1010 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
1014 if (session->seat != seat)
1015 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
1017 r = session_activate(session);
1021 return sd_bus_reply_method_return(message, NULL);
1024 static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1025 Manager *m = userdata;
1033 r = sd_bus_message_read(message, "s", &name);
1037 r = manager_get_session_from_creds(m, message, name, error, &session);
1041 return bus_session_method_lock(message, session, error);
1044 static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1045 Manager *m = userdata;
1051 r = bus_verify_polkit_async(
1054 "org.freedesktop.login1.lock-sessions",
1058 &m->polkit_registry,
1063 return 1; /* Will call us back */
1065 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
1069 return sd_bus_reply_method_return(message, NULL);
1072 static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1074 Manager *m = userdata;
1081 r = sd_bus_message_read(message, "s", &name);
1085 r = manager_get_session_from_creds(m, message, name, error, &session);
1089 return bus_session_method_kill(message, session, error);
1092 static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1093 Manager *m = userdata;
1101 r = sd_bus_message_read(message, "u", &uid);
1105 r = manager_get_user_from_creds(m, message, uid, error, &user);
1109 return bus_user_method_kill(message, user, error);
1112 static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1113 Manager *m = userdata;
1121 r = sd_bus_message_read(message, "s", &name);
1125 r = manager_get_session_from_creds(m, message, name, error, &session);
1129 return bus_session_method_terminate(message, session, error);
1132 static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1133 Manager *m = userdata;
1141 r = sd_bus_message_read(message, "u", &uid);
1145 r = manager_get_user_from_creds(m, message, uid, error, &user);
1149 return bus_user_method_terminate(message, user, error);
1152 static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1153 Manager *m = userdata;
1161 r = sd_bus_message_read(message, "s", &name);
1165 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1169 return bus_seat_method_terminate(message, seat, error);
1172 static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1173 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1174 _cleanup_free_ char *cc = NULL;
1175 Manager *m = userdata;
1176 int r, b, interactive;
1179 uint32_t uid, auth_uid;
1184 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1188 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID |
1189 SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1193 if (!uid_is_valid(uid)) {
1194 /* Note that we get the owner UID of the session or user unit,
1195 * not the actual client UID here! */
1196 r = sd_bus_creds_get_owner_uid(creds, &uid);
1201 /* owner_uid is racy, so for authorization we must use euid */
1202 r = sd_bus_creds_get_euid(creds, &auth_uid);
1209 return errno > 0 ? -errno : -ENOENT;
1211 r = bus_verify_polkit_async(
1214 uid == auth_uid ? "org.freedesktop.login1.set-self-linger" :
1215 "org.freedesktop.login1.set-user-linger",
1219 &m->polkit_registry,
1224 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1226 mkdir_p_label("/var/lib/elogind", 0755);
1228 r = mkdir_safe_label("/var/lib/elogind/linger", 0755, 0, 0, MKDIR_WARN_MODE);
1232 cc = cescape(pw->pw_name);
1236 path = strjoina("/var/lib/elogind/linger/", cc);
1244 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1251 if (r < 0 && errno != ENOENT)
1254 u = hashmap_get(m->users, UID_TO_PTR(uid));
1256 user_add_to_gc_queue(u);
1259 return sd_bus_reply_method_return(message, NULL);
1262 static int trigger_device(Manager *m, struct udev_device *d) {
1263 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
1264 struct udev_list_entry *first, *item;
1269 e = udev_enumerate_new(m->udev);
1274 r = udev_enumerate_add_match_parent(e, d);
1279 r = udev_enumerate_scan_devices(e);
1283 first = udev_enumerate_get_list_entry(e);
1284 udev_list_entry_foreach(item, first) {
1285 _cleanup_free_ char *t = NULL;
1288 p = udev_list_entry_get_name(item);
1290 t = strappend(p, "/uevent");
1294 (void) write_string_file(t, "change", 0);
1300 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1301 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
1302 _cleanup_free_ char *rule = NULL, *file = NULL;
1303 const char *id_for_seat;
1310 d = udev_device_new_from_syspath(m->udev, sysfs);
1314 if (!udev_device_has_tag(d, "seat"))
1317 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
1321 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1324 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1327 mkdir_p_label("/etc/udev/rules.d", 0755);
1328 r = write_string_file_atomic_label(file, rule);
1332 return trigger_device(m, d);
1335 static int flush_devices(Manager *m) {
1336 _cleanup_closedir_ DIR *d;
1340 d = opendir("/etc/udev/rules.d");
1342 if (errno != ENOENT)
1343 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1347 FOREACH_DIRENT_ALL(de, d, break) {
1348 if (!dirent_is_file(de))
1351 if (!startswith(de->d_name, "72-seat-"))
1354 if (!endswith(de->d_name, ".rules"))
1357 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1358 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1362 return trigger_device(m, NULL);
1365 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1366 const char *sysfs, *seat;
1367 Manager *m = userdata;
1373 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1377 if (!path_startswith(sysfs, "/sys"))
1378 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1380 if (!seat_name_is_valid(seat))
1381 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1383 r = bus_verify_polkit_async(
1386 "org.freedesktop.login1.attach-device",
1390 &m->polkit_registry,
1395 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1397 r = attach_device(m, seat, sysfs);
1401 return sd_bus_reply_method_return(message, NULL);
1404 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1405 Manager *m = userdata;
1411 r = sd_bus_message_read(message, "b", &interactive);
1415 r = bus_verify_polkit_async(
1418 "org.freedesktop.login1.flush-devices",
1422 &m->polkit_registry,
1427 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1429 r = flush_devices(m);
1433 return sd_bus_reply_method_return(message, NULL);
1436 static int have_multiple_sessions(
1445 /* Check for other users' sessions. Greeter sessions do not
1446 * count, and non-login sessions do not count either. */
1447 HASHMAP_FOREACH(session, m->sessions, i)
1448 if (session->class == SESSION_USER &&
1449 session->user->uid != uid)
1455 #if 0 /// elogind has its own variant in elogind-dbus.c
1456 static int bus_manager_log_shutdown(
1458 const char *unit_name) {
1465 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1466 p = "MESSAGE=System is powering down";
1467 q = "SHUTDOWN=power-off";
1468 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1469 p = "MESSAGE=System is rebooting";
1470 q = "SHUTDOWN=reboot";
1471 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1472 p = "MESSAGE=System is halting";
1473 q = "SHUTDOWN=halt";
1474 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1475 p = "MESSAGE=System is rebooting with kexec";
1476 q = "SHUTDOWN=kexec";
1478 p = "MESSAGE=System is shutting down";
1482 if (isempty(m->wall_message))
1483 p = strjoina(p, ".");
1485 p = strjoina(p, " (", m->wall_message, ").");
1487 return log_struct(LOG_NOTICE,
1488 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
1495 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1496 Manager *m = userdata;
1501 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1505 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1510 if (until <= now(CLOCK_MONOTONIC))
1513 /* We want to ignore the lid switch for a while after each
1514 * suspend, and after boot-up. Hence let's install a timer for
1515 * this. As long as the event source exists we ignore the lid
1518 if (m->lid_switch_ignore_event_source) {
1521 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1528 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1530 r = sd_event_add_time(
1532 &m->lid_switch_ignore_event_source,
1535 lid_switch_ignore_handler, m);
1540 #if 0 /// elogind-dbus.c needs to access this
1541 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1543 int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1546 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1547 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1548 [INHIBIT_SLEEP] = "PrepareForSleep"
1551 int active = _active;
1555 assert(w < _INHIBIT_WHAT_MAX);
1556 assert(signal_name[w]);
1558 return sd_bus_emit_signal(m->bus,
1559 "/org/freedesktop/login1",
1560 "org.freedesktop.login1.Manager",
1566 #if 0 /// elogind has its own variant in elogind-dbus.c
1567 static int execute_shutdown_or_sleep(
1570 const char *unit_name,
1571 sd_bus_error *error) {
1573 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1580 assert(w < _INHIBIT_WHAT_MAX);
1583 if (w == INHIBIT_SHUTDOWN)
1584 bus_manager_log_shutdown(m, unit_name);
1586 r = sd_bus_call_method(
1588 "org.freedesktop.systemd1",
1589 "/org/freedesktop/systemd1",
1590 "org.freedesktop.systemd1.Manager",
1594 "ss", unit_name, "replace-irreversibly");
1598 r = sd_bus_message_read(reply, "o", &p);
1608 m->action_unit = unit_name;
1609 free(m->action_job);
1613 /* Make sure the lid switch is ignored for a while */
1614 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1619 /* Tell people that they now may take a lock again */
1620 (void) send_prepare_for(m, w, false);
1626 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1628 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1629 Inhibitor *offending = NULL;
1634 #if 0 /// elogind has no action_job, but a pending_action
1635 if (manager->action_what == 0 || manager->action_job)
1637 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
1641 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1642 _cleanup_free_ char *comm = NULL, *u = NULL;
1647 (void) get_process_comm(offending->pid, &comm);
1648 u = uid_to_name(offending->uid);
1650 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1651 offending->uid, strna(u),
1652 offending->pid, strna(comm));
1655 /* Actually do the operation */
1656 #if 0 /// elogind has no action_unit but a pending_action
1657 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1659 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
1662 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1663 bus_error_message(&error, r));
1666 #if 0 /// elogind has no action_unit but a pending_action
1667 manager->action_unit = NULL;
1668 manager->action_what = 0;
1671 manager->pending_action = HANDLE_IGNORE;
1672 manager->action_what = 0;
1673 /* It is not a critical error for elogind if suspending fails */
1680 static int manager_inhibit_timeout_handler(
1685 Manager *manager = userdata;
1689 assert(manager->inhibit_timeout_source == s);
1691 r = manager_dispatch_delayed(manager, true);
1692 return (r < 0) ? r : 0;
1695 #if 0 /// elogind does not have unit_name but action
1696 static int delay_shutdown_or_sleep(
1699 const char *unit_name) {
1702 int delay_shutdown_or_sleep(
1705 HandleAction action) {
1712 assert(w < _INHIBIT_WHAT_MAX);
1713 #if 0 /// UNNEEDED by elogind
1717 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1719 if (m->inhibit_timeout_source) {
1720 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1722 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1724 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1726 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1728 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1729 timeout_val, 0, manager_inhibit_timeout_handler, m);
1734 #if 0 /// elogind does not have unit_name but pendig_action
1735 m->action_unit = unit_name;
1737 m->pending_action = action;
1744 int bus_manager_shutdown_or_sleep_now_or_later(
1746 #if 0 /// elogind has HandleAction instead of const char* unit_name
1747 const char *unit_name,
1749 _cleanup_free_ char *load_state = NULL;
1751 HandleAction unit_name,
1754 sd_bus_error *error) {
1759 #if 0 /// for elogind only w has to be checked.
1762 assert(w <= _INHIBIT_WHAT_MAX);
1763 assert(!m->action_job);
1765 r = unit_load_state(m->bus, unit_name, &load_state);
1769 if (!streq(load_state, "loaded")) {
1770 log_notice("Unit %s is %s, refusing operation.", unit_name, load_state);
1775 assert(w <= _INHIBIT_WHAT_MAX);
1778 /* Tell everybody to prepare for shutdown/sleep */
1779 (void) send_prepare_for(m, w, true);
1782 m->inhibit_delay_max > 0 &&
1783 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1785 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
1786 handle_action_to_string(unit_name),
1787 delayed ? "" : "NOT ");
1789 /* Shutdown is delayed, keep in mind what we
1790 * want to do, and start a timeout */
1791 r = delay_shutdown_or_sleep(m, w, unit_name);
1793 /* Shutdown is not delayed, execute it
1795 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1800 static int verify_shutdown_creds(
1802 sd_bus_message *message,
1806 const char *action_multiple_sessions,
1807 const char *action_ignore_inhibit,
1808 sd_bus_error *error) {
1810 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1811 bool multiple_sessions, blocked;
1818 assert(w <= _INHIBIT_WHAT_MAX);
1820 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1824 r = sd_bus_creds_get_euid(creds, &uid);
1828 r = have_multiple_sessions(m, uid);
1832 multiple_sessions = r > 0;
1833 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1835 if (multiple_sessions && action_multiple_sessions) {
1836 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1840 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1843 if (blocked && action_ignore_inhibit) {
1844 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1848 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1851 if (!multiple_sessions && !blocked && action) {
1852 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1856 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1862 static int method_do_shutdown_or_sleep(
1864 sd_bus_message *message,
1865 #if 0 /// elogind has HandleAction instead of const char* unit_name
1866 const char *unit_name,
1868 HandleAction unit_name,
1872 const char *action_multiple_sessions,
1873 const char *action_ignore_inhibit,
1874 const char *sleep_verb,
1875 sd_bus_error *error) {
1881 #if 0 /// elogind does not need this to be checked
1885 assert(w <= _INHIBIT_WHAT_MAX);
1887 r = sd_bus_message_read(message, "b", &interactive);
1891 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
1892 __FUNCTION__, action, sleep_verb,
1893 interactive ? "" : "NOT ");
1894 /* Don't allow multiple jobs being executed at the same time */
1896 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1899 #if 0 /// Within elogind the manager m must be provided, too
1900 r = can_sleep(sleep_verb);
1902 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1903 "Not enough swap space for hibernation");
1905 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1906 "Sleep verb \"%s\" not supported", sleep_verb);
1908 r = can_sleep(m, sleep_verb);
1914 #if 0 /// Within elogind it does not make sense to verify shutdown creds when suspending
1915 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1916 action_ignore_inhibit, error);
1920 if (IN_SET(unit_name, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
1921 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1922 action_ignore_inhibit, error);
1923 log_debug_elogind("verify_shutdown_creds() returned %d", r);
1929 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1933 return sd_bus_reply_method_return(message, NULL);
1936 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1937 Manager *m = userdata;
1939 log_debug_elogind("%s called", __FUNCTION__);
1940 return method_do_shutdown_or_sleep(
1942 #if 0 /// elogind uses HandleAction instead of const char* unti names
1943 SPECIAL_POWEROFF_TARGET,
1948 "org.freedesktop.login1.power-off",
1949 "org.freedesktop.login1.power-off-multiple-sessions",
1950 "org.freedesktop.login1.power-off-ignore-inhibit",
1955 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1956 Manager *m = userdata;
1958 log_debug_elogind("%s called", __FUNCTION__);
1959 return method_do_shutdown_or_sleep(
1961 #if 0 /// elogind uses HandleAction instead of const char* unti names
1962 SPECIAL_REBOOT_TARGET,
1967 "org.freedesktop.login1.reboot",
1968 "org.freedesktop.login1.reboot-multiple-sessions",
1969 "org.freedesktop.login1.reboot-ignore-inhibit",
1974 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1975 Manager *m = userdata;
1977 log_debug_elogind("%s called", __FUNCTION__);
1978 return method_do_shutdown_or_sleep(
1980 #if 0 /// elogind uses HandleAction instead of const char* unti names
1981 SPECIAL_HALT_TARGET,
1986 "org.freedesktop.login1.halt",
1987 "org.freedesktop.login1.halt-multiple-sessions",
1988 "org.freedesktop.login1.halt-ignore-inhibit",
1993 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1994 Manager *m = userdata;
1996 log_debug_elogind("%s called", __FUNCTION__);
1997 return method_do_shutdown_or_sleep(
1999 #if 0 /// elogind uses HandleAction instead of const char* unti names
2000 SPECIAL_SUSPEND_TARGET,
2005 "org.freedesktop.login1.suspend",
2006 "org.freedesktop.login1.suspend-multiple-sessions",
2007 "org.freedesktop.login1.suspend-ignore-inhibit",
2012 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2013 Manager *m = userdata;
2015 log_debug_elogind("%s called", __FUNCTION__);
2016 return method_do_shutdown_or_sleep(
2018 #if 0 /// elogind uses HandleAction instead of const char* unti names
2019 SPECIAL_HIBERNATE_TARGET,
2024 "org.freedesktop.login1.hibernate",
2025 "org.freedesktop.login1.hibernate-multiple-sessions",
2026 "org.freedesktop.login1.hibernate-ignore-inhibit",
2031 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2032 Manager *m = userdata;
2034 log_debug_elogind("%s called", __FUNCTION__);
2035 return method_do_shutdown_or_sleep(
2037 #if 0 /// elogind uses HandleAction instead of const char* unti names
2038 SPECIAL_HYBRID_SLEEP_TARGET,
2040 "org.freedesktop.login1.hibernate",
2041 "org.freedesktop.login1.hibernate-multiple-sessions",
2042 "org.freedesktop.login1.hibernate-ignore-inhibit",
2047 static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2048 Manager *m = userdata;
2050 return method_do_shutdown_or_sleep(
2052 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
2054 HANDLE_HYBRID_SLEEP,
2057 "org.freedesktop.login1.hibernate",
2058 "org.freedesktop.login1.hibernate-multiple-sessions",
2059 "org.freedesktop.login1.hibernate-ignore-inhibit",
2064 static int nologin_timeout_handler(
2069 Manager *m = userdata;
2071 log_info("Creating /run/nologin, blocking further logins...");
2074 create_shutdown_run_nologin_or_warn() >= 0;
2079 static int update_schedule_file(Manager *m) {
2080 _cleanup_free_ char *temp_path = NULL;
2081 _cleanup_fclose_ FILE *f = NULL;
2086 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
2088 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
2090 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
2092 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
2094 (void) fchmod(fileno(f), 0644);
2100 m->scheduled_shutdown_timeout,
2101 m->enable_wall_messages,
2102 m->scheduled_shutdown_type);
2104 if (!isempty(m->wall_message)) {
2105 _cleanup_free_ char *t;
2107 t = cescape(m->wall_message);
2113 fprintf(f, "WALL_MESSAGE=%s\n", t);
2116 r = fflush_and_check(f);
2120 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
2128 (void) unlink(temp_path);
2129 (void) unlink("/run/systemd/shutdown/scheduled");
2131 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
2134 #if 0 /// elogind must access this from elogind-dbus.c
2135 static void reset_scheduled_shutdown(Manager *m) {
2137 void reset_scheduled_shutdown(Manager *m) {
2141 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2142 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
2143 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
2145 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
2146 m->scheduled_shutdown_timeout = 0;
2147 m->shutdown_dry_run = false;
2149 if (m->unlink_nologin) {
2150 (void) unlink_or_warn("/run/nologin");
2151 m->unlink_nologin = false;
2154 (void) unlink("/run/systemd/shutdown/scheduled");
2157 #if 0 /// elogind has its own variant in elogind-dbus.c
2158 static int manager_scheduled_shutdown_handler(
2163 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2164 Manager *m = userdata;
2170 if (isempty(m->scheduled_shutdown_type))
2173 if (streq(m->scheduled_shutdown_type, "poweroff"))
2174 target = SPECIAL_POWEROFF_TARGET;
2175 else if (streq(m->scheduled_shutdown_type, "reboot"))
2176 target = SPECIAL_REBOOT_TARGET;
2177 else if (streq(m->scheduled_shutdown_type, "halt"))
2178 target = SPECIAL_HALT_TARGET;
2180 assert_not_reached("unexpected shutdown type");
2182 /* Don't allow multiple jobs being executed at the same time */
2183 if (m->action_what) {
2185 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
2189 if (m->shutdown_dry_run) {
2190 /* We do not process delay inhibitors here. Otherwise, we
2191 * would have to be considered "in progress" (like the check
2192 * above) for some seconds after our admin has seen the final
2195 bus_manager_log_shutdown(m, target);
2196 log_info("Running in dry run, suppressing action.");
2197 reset_scheduled_shutdown(m);
2202 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
2204 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2211 reset_scheduled_shutdown(m);
2216 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2217 Manager *m = userdata;
2218 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2219 const char *action_multiple_sessions = NULL;
2220 const char *action_ignore_inhibit = NULL;
2221 const char *action = NULL;
2225 bool dry_run = false;
2230 log_debug_elogind("%s called", __FUNCTION__);
2231 r = sd_bus_message_read(message, "st", &type, &elapse);
2235 if (startswith(type, "dry-")) {
2240 if (streq(type, "poweroff")) {
2241 action = "org.freedesktop.login1.power-off";
2242 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2243 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2244 } else if (streq(type, "reboot")) {
2245 action = "org.freedesktop.login1.reboot";
2246 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2247 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2248 } else if (streq(type, "halt")) {
2249 action = "org.freedesktop.login1.halt";
2250 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2251 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2253 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2255 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2256 action, action_multiple_sessions, action_ignore_inhibit, error);
2260 if (m->scheduled_shutdown_timeout_source) {
2261 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2263 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2265 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2267 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2269 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2270 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2272 return log_error_errno(r, "sd_event_add_time() failed: %m");
2275 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2277 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2281 m->shutdown_dry_run = dry_run;
2283 if (m->nologin_timeout_source) {
2284 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2286 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2288 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2290 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2292 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2293 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2295 return log_error_errno(r, "sd_event_add_time() failed: %m");
2298 m->scheduled_shutdown_timeout = elapse;
2300 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2302 const char *tty = NULL;
2304 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2305 (void) sd_bus_creds_get_tty(creds, &tty);
2307 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2309 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2314 r = manager_setup_wall_message_timer(m);
2318 r = update_schedule_file(m);
2322 return sd_bus_reply_method_return(message, NULL);
2325 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2326 Manager *m = userdata;
2328 #if 1 /// elogind needs to construct the message to allow extra wall messages
2329 _cleanup_free_ char *l = NULL;
2335 log_debug_elogind("%s called", __FUNCTION__);
2336 cancelled = m->scheduled_shutdown_type != NULL;
2337 reset_scheduled_shutdown(m);
2340 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2341 const char *tty = NULL;
2345 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2347 (void) sd_bus_creds_get_uid(creds, &uid);
2348 (void) sd_bus_creds_get_tty(creds, &tty);
2351 #if 0 /// elogind wants to allow extra cancellation messages
2352 utmp_wall("The system shutdown has been cancelled",
2353 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2355 r = asprintf(&l, "%s%sThe system shutdown has been cancelled!",
2356 strempty(m->wall_message),
2357 isempty(m->wall_message) ? "" : "\n");
2363 utmp_wall(l, uid_to_name(uid), tty, logind_wall_tty_filter, m);
2367 return sd_bus_reply_method_return(message, "b", cancelled);
2370 static int method_can_shutdown_or_sleep(
2372 sd_bus_message *message,
2375 const char *action_multiple_sessions,
2376 const char *action_ignore_inhibit,
2377 const char *sleep_verb,
2378 sd_bus_error *error) {
2380 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2381 HandleAction handle;
2382 bool multiple_sessions, challenge, blocked;
2383 const char *result = NULL;
2390 assert(w <= _INHIBIT_WHAT_MAX);
2392 assert(action_multiple_sessions);
2393 assert(action_ignore_inhibit);
2396 #if 0 /// elogind needs to have the manager being passed
2397 r = can_sleep(sleep_verb);
2398 if (IN_SET(r, 0, -ENOSPC))
2399 return sd_bus_reply_method_return(message, "s", "na");
2401 r = can_sleep(m, sleep_verb);
2407 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2411 r = sd_bus_creds_get_euid(creds, &uid);
2415 r = have_multiple_sessions(m, uid);
2419 multiple_sessions = r > 0;
2420 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2422 handle = handle_action_from_string(sleep_verb);
2426 target = manager_target_for_action(handle);
2428 _cleanup_free_ char *load_state = NULL;
2430 r = unit_load_state(m->bus, target, &load_state);
2434 if (!streq(load_state, "loaded")) {
2441 if (multiple_sessions) {
2442 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2449 result = "challenge";
2455 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2459 if (r > 0 && !result)
2461 else if (challenge && (!result || streq(result, "yes")))
2462 result = "challenge";
2467 if (!multiple_sessions && !blocked) {
2468 /* If neither inhibit nor multiple sessions
2469 * apply then just check the normal policy */
2471 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2478 result = "challenge";
2484 return sd_bus_reply_method_return(message, "s", result);
2487 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2488 Manager *m = userdata;
2490 return method_can_shutdown_or_sleep(
2493 "org.freedesktop.login1.power-off",
2494 "org.freedesktop.login1.power-off-multiple-sessions",
2495 "org.freedesktop.login1.power-off-ignore-inhibit",
2500 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2501 Manager *m = userdata;
2503 return method_can_shutdown_or_sleep(
2506 "org.freedesktop.login1.reboot",
2507 "org.freedesktop.login1.reboot-multiple-sessions",
2508 "org.freedesktop.login1.reboot-ignore-inhibit",
2513 static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2514 Manager *m = userdata;
2516 return method_can_shutdown_or_sleep(
2519 "org.freedesktop.login1.halt",
2520 "org.freedesktop.login1.halt-multiple-sessions",
2521 "org.freedesktop.login1.halt-ignore-inhibit",
2526 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2527 Manager *m = userdata;
2529 return method_can_shutdown_or_sleep(
2532 "org.freedesktop.login1.suspend",
2533 "org.freedesktop.login1.suspend-multiple-sessions",
2534 "org.freedesktop.login1.suspend-ignore-inhibit",
2539 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2540 Manager *m = userdata;
2542 return method_can_shutdown_or_sleep(
2545 "org.freedesktop.login1.hibernate",
2546 "org.freedesktop.login1.hibernate-multiple-sessions",
2547 "org.freedesktop.login1.hibernate-ignore-inhibit",
2552 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2553 Manager *m = userdata;
2555 return method_can_shutdown_or_sleep(
2558 "org.freedesktop.login1.hibernate",
2559 "org.freedesktop.login1.hibernate-multiple-sessions",
2560 "org.freedesktop.login1.hibernate-ignore-inhibit",
2565 static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2566 Manager *m = userdata;
2568 return method_can_shutdown_or_sleep(
2571 "org.freedesktop.login1.hibernate",
2572 "org.freedesktop.login1.hibernate-multiple-sessions",
2573 "org.freedesktop.login1.hibernate-ignore-inhibit",
2574 "suspend-then-hibernate",
2578 static int property_get_reboot_to_firmware_setup(
2581 const char *interface,
2582 const char *property,
2583 sd_bus_message *reply,
2585 sd_bus_error *error) {
2586 #if 0 /// elogind does not support EFI
2593 r = efi_get_reboot_to_firmware();
2594 if (r < 0 && r != -EOPNOTSUPP)
2595 log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m");
2597 return sd_bus_message_append(reply, "b", r > 0);
2599 return sd_bus_message_append(reply, "b", false);
2603 static int method_set_reboot_to_firmware_setup(
2604 sd_bus_message *message,
2606 sd_bus_error *error) {
2609 Manager *m = userdata;
2614 r = sd_bus_message_read(message, "b", &b);
2618 r = bus_verify_polkit_async(message,
2620 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2624 &m->polkit_registry,
2629 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2631 #if 0 /// elogind does not support EFI
2632 r = efi_set_reboot_to_firmware(b);
2637 return sd_bus_reply_method_return(message, NULL);
2640 static int method_can_reboot_to_firmware_setup(
2641 sd_bus_message *message,
2643 sd_bus_error *error) {
2645 #if 0 /// elogind does not support EFI
2649 Manager *m = userdata;
2654 r = efi_reboot_to_firmware_supported();
2656 if (r != -EOPNOTSUPP)
2657 log_warning_errno(errno, "Failed to determine whether reboot to firmware is supported: %m");
2659 return sd_bus_reply_method_return(message, "s", "na");
2662 r = bus_test_polkit(message,
2664 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2675 result = "challenge";
2679 return sd_bus_reply_method_return(message, "s", result);
2681 return sd_bus_reply_method_return(message, "s", "no");
2685 static int method_set_wall_message(
2686 sd_bus_message *message,
2688 sd_bus_error *error) {
2691 Manager *m = userdata;
2693 int enable_wall_messages;
2698 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2702 #if 0 /// elogind only calls this for shutdown/reboot, which already needs authorization.
2703 r = bus_verify_polkit_async(message,
2705 "org.freedesktop.login1.set-wall-message",
2709 &m->polkit_registry,
2714 return 1; /* Will call us back */
2717 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
2721 m->enable_wall_messages = enable_wall_messages;
2723 return sd_bus_reply_method_return(message, NULL);
2726 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2727 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2728 const char *who, *why, *what, *mode;
2729 _cleanup_free_ char *id = NULL;
2730 _cleanup_close_ int fifo_fd = -1;
2731 Manager *m = userdata;
2732 Inhibitor *i = NULL;
2742 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2746 w = inhibit_what_from_string(what);
2748 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2750 mm = inhibit_mode_from_string(mode);
2752 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2754 /* Delay is only supported for shutdown/sleep */
2755 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2756 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2758 /* Don't allow taking delay locks while we are already
2759 * executing the operation. We shouldn't create the impression
2760 * that the lock was successful if the machine is about to go
2761 * down/suspend any moment. */
2762 if (m->action_what & w)
2763 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2765 r = bus_verify_polkit_async(
2768 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2769 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2770 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2771 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2772 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2773 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2774 "org.freedesktop.login1.inhibit-handle-lid-switch",
2778 &m->polkit_registry,
2783 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2785 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2789 r = sd_bus_creds_get_euid(creds, &uid);
2793 r = sd_bus_creds_get_pid(creds, &pid);
2797 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2798 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2803 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2806 } while (hashmap_get(m->inhibitors, id));
2808 r = manager_add_inhibitor(m, id, &i);
2816 i->why = strdup(why);
2817 i->who = strdup(who);
2819 if (!i->why || !i->who) {
2824 fifo_fd = inhibitor_create_fifo(i);
2832 return sd_bus_reply_method_return(message, "h", fifo_fd);
2841 const sd_bus_vtable manager_vtable[] = {
2842 SD_BUS_VTABLE_START(0),
2844 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2845 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2847 #if 0 /// UNNEEDED by elogind
2848 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2850 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2851 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2852 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2853 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2854 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2855 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2856 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2857 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2858 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2859 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2860 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2861 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2862 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2863 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2864 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
2865 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2866 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2867 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2868 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2869 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2870 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2871 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2872 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2873 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2874 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", bus_property_get_size, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2875 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2876 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_current_inhibitors, 0, 0),
2877 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2878 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_current_sessions, 0, 0),
2879 SD_BUS_PROPERTY("UserTasksMax", "t", NULL, offsetof(Manager, user_tasks_max), SD_BUS_VTABLE_PROPERTY_CONST),
2881 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2882 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2883 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2884 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2885 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2886 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2887 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2888 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2889 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2890 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2891 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2892 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2893 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2894 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2895 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2896 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2897 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2898 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2899 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2900 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2901 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2902 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2903 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2904 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2905 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2906 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2907 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2908 SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2909 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2910 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2911 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2912 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2913 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2914 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2915 SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2916 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2917 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2918 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2919 SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2920 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2921 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2922 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2923 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2924 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2925 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2927 SD_BUS_SIGNAL("SessionNew", "so", 0),
2928 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2929 SD_BUS_SIGNAL("UserNew", "uo", 0),
2930 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2931 SD_BUS_SIGNAL("SeatNew", "so", 0),
2932 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2933 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2934 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2939 #if 0 /// UNNEEDED by elogind
2940 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2949 if (streq(result, "done"))
2950 r = session_send_create_reply(s, NULL);
2952 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2954 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2955 r = session_send_create_reply(s, &e);
2961 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2962 const char *path, *result, *unit;
2963 Manager *m = userdata;
2972 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2974 bus_log_parse_error(r);
2978 if (m->action_job && streq(m->action_job, path)) {
2979 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2981 /* Tell people that they now may take a lock again */
2982 (void) send_prepare_for(m, m->action_what, false);
2984 m->action_job = mfree(m->action_job);
2985 m->action_unit = NULL;
2990 session = hashmap_get(m->session_units, unit);
2991 if (session && streq_ptr(path, session->scope_job)) {
2992 session->scope_job = mfree(session->scope_job);
2993 session_jobs_reply(session, unit, result);
2995 session_save(session);
2996 user_save(session->user);
2997 session_add_to_gc_queue(session);
3000 user = hashmap_get(m->user_units, unit);
3002 (streq_ptr(path, user->service_job) ||
3003 streq_ptr(path, user->slice_job))) {
3005 if (streq_ptr(path, user->service_job))
3006 user->service_job = mfree(user->service_job);
3008 if (streq_ptr(path, user->slice_job))
3009 user->slice_job = mfree(user->slice_job);
3011 LIST_FOREACH(sessions_by_user, session, user->sessions)
3012 session_jobs_reply(session, unit, result);
3015 user_add_to_gc_queue(user);
3021 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3022 const char *path, *unit;
3023 Manager *m = userdata;
3031 r = sd_bus_message_read(message, "so", &unit, &path);
3033 bus_log_parse_error(r);
3037 session = hashmap_get(m->session_units, unit);
3039 session_add_to_gc_queue(session);
3041 user = hashmap_get(m->user_units, unit);
3043 user_add_to_gc_queue(user);
3048 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3049 _cleanup_free_ char *unit = NULL;
3050 Manager *m = userdata;
3059 path = sd_bus_message_get_path(message);
3063 r = unit_name_from_dbus_path(path, &unit);
3064 if (r == -EINVAL) /* not a unit */
3071 session = hashmap_get(m->session_units, unit);
3073 session_add_to_gc_queue(session);
3075 user = hashmap_get(m->user_units, unit);
3077 user_add_to_gc_queue(user);
3082 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3083 Manager *m = userdata;
3091 r = sd_bus_message_read(message, "b", &b);
3093 bus_log_parse_error(r);
3100 /* systemd finished reloading, let's recheck all our sessions */
3101 log_debug("System manager has been reloaded, rechecking sessions...");
3103 HASHMAP_FOREACH(session, m->sessions, i)
3104 session_add_to_gc_queue(session);
3110 int manager_send_changed(Manager *manager, const char *property, ...) {
3115 l = strv_from_stdarg_alloca(property);
3117 return sd_bus_emit_properties_changed_strv(
3119 "/org/freedesktop/login1",
3120 "org.freedesktop.login1.Manager",
3124 #if 0 /// UNNEEDED by elogind
3125 static int strdup_job(sd_bus_message *reply, char **job) {
3130 r = sd_bus_message_read(reply, "o", &j);
3142 int manager_start_slice(
3145 const char *description,
3149 sd_bus_error *error,
3152 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
3159 r = sd_bus_message_new_method_call(
3162 "org.freedesktop.systemd1",
3163 "/org/freedesktop/systemd1",
3164 "org.freedesktop.systemd1.Manager",
3165 "StartTransientUnit");
3169 r = sd_bus_message_append(m, "ss", strempty(slice), "fail");
3173 r = sd_bus_message_open_container(m, 'a', "(sv)");
3177 if (!isempty(description)) {
3178 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3183 if (!isempty(after)) {
3184 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3189 if (!isempty(after2)) {
3190 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3195 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", tasks_max);
3199 r = sd_bus_message_close_container(m);
3203 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3207 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3211 return strdup_job(reply, job);
3214 int manager_start_scope(
3219 const char *description,
3222 sd_bus_message *more_properties,
3223 sd_bus_error *error,
3226 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
3234 r = sd_bus_message_new_method_call(
3237 "org.freedesktop.systemd1",
3238 "/org/freedesktop/systemd1",
3239 "org.freedesktop.systemd1.Manager",
3240 "StartTransientUnit");
3244 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
3248 r = sd_bus_message_open_container(m, 'a', "(sv)");
3252 if (!isempty(slice)) {
3253 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
3258 if (!isempty(description)) {
3259 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3264 if (!isempty(after)) {
3265 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3270 if (!isempty(after2)) {
3271 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3276 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3278 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
3282 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3286 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3287 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1);
3289 return bus_log_create_error(r);
3291 if (more_properties) {
3292 /* If TasksMax also appears here, it will overwrite the default value set above */
3293 r = sd_bus_message_copy(m, more_properties, true);
3298 r = sd_bus_message_close_container(m);
3302 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3306 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3310 return strdup_job(reply, job);
3313 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3314 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3321 r = sd_bus_call_method(
3323 "org.freedesktop.systemd1",
3324 "/org/freedesktop/systemd1",
3325 "org.freedesktop.systemd1.Manager",
3329 "ss", unit, "replace");
3333 return strdup_job(reply, job);
3336 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3337 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3344 r = sd_bus_call_method(
3346 "org.freedesktop.systemd1",
3347 "/org/freedesktop/systemd1",
3348 "org.freedesktop.systemd1.Manager",
3352 "ss", unit, "fail");
3354 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3355 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3358 sd_bus_error_free(error);
3365 return strdup_job(reply, job);
3368 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3369 _cleanup_free_ char *path = NULL;
3375 path = unit_dbus_path_from_name(scope);
3379 r = sd_bus_call_method(
3381 "org.freedesktop.systemd1",
3383 "org.freedesktop.systemd1.Scope",
3389 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3390 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3391 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3392 sd_bus_error_free(error);
3402 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3406 return sd_bus_call_method(
3408 "org.freedesktop.systemd1",
3409 "/org/freedesktop/systemd1",
3410 "org.freedesktop.systemd1.Manager",
3414 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3417 int manager_unit_is_active(Manager *manager, const char *unit) {
3418 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3419 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3420 _cleanup_free_ char *path = NULL;
3427 path = unit_dbus_path_from_name(unit);
3431 r = sd_bus_get_property(
3433 "org.freedesktop.systemd1",
3435 "org.freedesktop.systemd1.Unit",
3441 /* systemd might have droppped off momentarily, let's
3442 * not make this an error */
3443 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3444 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3447 /* If the unit is already unloaded then it's not
3449 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3450 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3456 r = sd_bus_message_read(reply, "s", &state);
3460 return !streq(state, "inactive") && !streq(state, "failed");
3463 int manager_job_is_active(Manager *manager, const char *path) {
3464 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3465 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3471 r = sd_bus_get_property(
3473 "org.freedesktop.systemd1",
3475 "org.freedesktop.systemd1.Job",
3481 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3482 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3485 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3491 /* We don't actually care about the state really. The fact
3492 * that we could read the job state is enough for us */