1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "audit-util.h"
13 #include "bus-common-errors.h"
14 #include "bus-error.h"
15 #include "bus-locator.h"
16 #include "bus-polkit.h"
17 #include "bus-unit-util.h"
19 #include "cgroup-util.h"
20 #include "device-util.h"
21 #include "dirent-util.h"
22 #include "efi-loader.h"
27 #include "fileio-label.h"
29 #include "format-util.h"
31 #include "logind-dbus.h"
32 #include "logind-seat-dbus.h"
33 #include "logind-session-dbus.h"
34 #include "logind-user-dbus.h"
36 #include "missing_capability.h"
38 #include "parse-util.h"
39 #include "path-util.h"
40 #include "process-util.h"
41 #include "reboot-util.h"
42 #include "selinux-util.h"
43 #include "sleep-config.h"
45 #include "stdio-util.h"
47 #include "terminal-util.h"
48 #include "tmpfile-util.h"
49 #include "unit-name.h"
50 #include "user-util.h"
51 #include "utmp-wtmp.h"
54 static int get_sender_session(
56 sd_bus_message
*message
,
61 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
62 Session
*session
= NULL
;
66 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
67 * and returns that. If not and 'consult_display' is true, this returns the display session of the
68 * owning user of the caller. */
70 r
= sd_bus_query_sender_creds(message
,
71 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
72 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
76 r
= sd_bus_creds_get_session(creds
, &name
);
81 if (consult_display
) {
84 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
91 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
93 session
= user
->display
;
97 session
= hashmap_get(m
->sessions
, name
);
100 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
102 "Caller does not belong to any known session and doesn't own any suitable session." :
103 "Caller does not belong to any known session.");
109 int manager_get_session_from_creds(
111 sd_bus_message
*message
,
121 if (SEAT_IS_SELF(name
)) /* the caller's own session */
122 return get_sender_session(m
, message
, false, error
, ret
);
123 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
124 return get_sender_session(m
, message
, true, error
, ret
);
126 session
= hashmap_get(m
->sessions
, name
);
128 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
134 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
135 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
140 /* Note that we get the owner UID of the session, not the actual client UID here! */
141 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
145 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
152 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
155 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
156 "Caller does not belong to any logged in or lingering user");
162 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
168 if (!uid_is_valid(uid
))
169 return get_sender_user(m
, message
, error
, ret
);
171 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
173 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
174 "User ID "UID_FMT
" is not logged in or lingering", uid
);
180 int manager_get_seat_from_creds(
182 sd_bus_message
*message
,
193 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
196 /* Use these special seat names as session names */
197 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
201 seat
= session
->seat
;
203 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
205 seat
= hashmap_get(m
->seats
, name
);
207 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
214 static int return_test_polkit(
215 sd_bus_message
*message
,
218 const char **details
,
226 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
233 result
= "challenge";
237 return sd_bus_reply_method_return(message
, "s", result
);
240 static int property_get_idle_hint(
243 const char *interface
,
244 const char *property
,
245 sd_bus_message
*reply
,
247 sd_bus_error
*error
) {
249 Manager
*m
= userdata
;
255 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
258 static int property_get_idle_since_hint(
261 const char *interface
,
262 const char *property
,
263 sd_bus_message
*reply
,
265 sd_bus_error
*error
) {
267 Manager
*m
= userdata
;
268 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
274 manager_get_idle_hint(m
, &t
);
276 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
279 static int property_get_inhibited(
282 const char *interface
,
283 const char *property
,
284 sd_bus_message
*reply
,
286 sd_bus_error
*error
) {
288 Manager
*m
= userdata
;
295 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
297 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
300 static int property_get_preparing(
303 const char *interface
,
304 const char *property
,
305 sd_bus_message
*reply
,
307 sd_bus_error
*error
) {
309 Manager
*m
= userdata
;
316 if (streq(property
, "PreparingForShutdown"))
317 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
319 b
= m
->action_what
& INHIBIT_SLEEP
;
321 return sd_bus_message_append(reply
, "b", b
);
324 static int property_get_scheduled_shutdown(
327 const char *interface
,
328 const char *property
,
329 sd_bus_message
*reply
,
331 sd_bus_error
*error
) {
333 Manager
*m
= userdata
;
340 r
= sd_bus_message_open_container(reply
, 'r', "st");
344 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
348 return sd_bus_message_close_container(reply
);
351 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
352 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
353 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
354 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
355 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
356 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
358 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
359 _cleanup_free_
char *p
= NULL
;
360 Manager
*m
= userdata
;
368 r
= sd_bus_message_read(message
, "s", &name
);
372 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
376 p
= session_bus_path(session
);
380 return sd_bus_reply_method_return(message
, "o", p
);
383 /* Get login session of a process. This is not what you are looking for these days,
384 * as apps may instead belong to a user service unit. This includes terminal
385 * emulators and hence command-line apps. */
386 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
387 _cleanup_free_
char *p
= NULL
;
388 Session
*session
= NULL
;
389 Manager
*m
= userdata
;
396 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
398 r
= sd_bus_message_read(message
, "u", &pid
);
405 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
409 r
= manager_get_session_by_pid(m
, pid
, &session
);
414 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
415 "PID "PID_FMT
" does not belong to any known session", pid
);
418 p
= session_bus_path(session
);
422 return sd_bus_reply_method_return(message
, "o", p
);
425 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
426 _cleanup_free_
char *p
= NULL
;
427 Manager
*m
= userdata
;
435 r
= sd_bus_message_read(message
, "u", &uid
);
439 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
443 p
= user_bus_path(user
);
447 return sd_bus_reply_method_return(message
, "o", p
);
450 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
451 _cleanup_free_
char *p
= NULL
;
452 Manager
*m
= userdata
;
460 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
462 r
= sd_bus_message_read(message
, "u", &pid
);
469 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
473 r
= manager_get_user_by_pid(m
, pid
, &user
);
477 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
478 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
482 p
= user_bus_path(user
);
486 return sd_bus_reply_method_return(message
, "o", p
);
489 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
490 _cleanup_free_
char *p
= NULL
;
491 Manager
*m
= userdata
;
499 r
= sd_bus_message_read(message
, "s", &name
);
503 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
507 p
= seat_bus_path(seat
);
511 return sd_bus_reply_method_return(message
, "o", p
);
514 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
515 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
516 Manager
*m
= userdata
;
524 r
= sd_bus_message_new_method_return(message
, &reply
);
528 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
532 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
533 _cleanup_free_
char *p
= NULL
;
535 p
= session_bus_path(session
);
539 r
= sd_bus_message_append(reply
, "(susso)",
541 (uint32_t) session
->user
->user_record
->uid
,
542 session
->user
->user_record
->user_name
,
543 session
->seat
? session
->seat
->id
: "",
549 r
= sd_bus_message_close_container(reply
);
553 return sd_bus_send(NULL
, reply
, NULL
);
556 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
557 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
558 Manager
*m
= userdata
;
566 r
= sd_bus_message_new_method_return(message
, &reply
);
570 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
574 HASHMAP_FOREACH(user
, m
->users
, i
) {
575 _cleanup_free_
char *p
= NULL
;
577 p
= user_bus_path(user
);
581 r
= sd_bus_message_append(reply
, "(uso)",
582 (uint32_t) user
->user_record
->uid
,
583 user
->user_record
->user_name
,
589 r
= sd_bus_message_close_container(reply
);
593 return sd_bus_send(NULL
, reply
, NULL
);
596 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
597 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
598 Manager
*m
= userdata
;
606 r
= sd_bus_message_new_method_return(message
, &reply
);
610 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
614 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
615 _cleanup_free_
char *p
= NULL
;
617 p
= seat_bus_path(seat
);
621 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
626 r
= sd_bus_message_close_container(reply
);
630 return sd_bus_send(NULL
, reply
, NULL
);
633 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
634 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
635 Manager
*m
= userdata
;
636 Inhibitor
*inhibitor
;
643 r
= sd_bus_message_new_method_return(message
, &reply
);
647 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
651 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
653 r
= sd_bus_message_append(reply
, "(ssssuu)",
654 strempty(inhibit_what_to_string(inhibitor
->what
)),
655 strempty(inhibitor
->who
),
656 strempty(inhibitor
->why
),
657 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
658 (uint32_t) inhibitor
->uid
,
659 (uint32_t) inhibitor
->pid
);
664 r
= sd_bus_message_close_container(reply
);
668 return sd_bus_send(NULL
, reply
, NULL
);
671 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
672 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
673 _cleanup_free_
char *id
= NULL
;
674 Session
*session
= NULL
;
675 uint32_t audit_id
= 0;
676 Manager
*m
= userdata
;
690 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
691 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
693 r
= sd_bus_message_read(message
, "uusssssussbss",
694 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
695 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
699 if (!uid_is_valid(uid
))
700 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
701 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
702 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
705 t
= _SESSION_TYPE_INVALID
;
707 t
= session_type_from_string(type
);
709 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
710 "Invalid session type %s", type
);
714 c
= _SESSION_CLASS_INVALID
;
716 c
= session_class_from_string(class);
718 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
719 "Invalid session class %s", class);
722 if (isempty(desktop
))
725 if (!string_is_safe(desktop
))
726 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
727 "Invalid desktop string %s", desktop
);
733 seat
= hashmap_get(m
->seats
, cseat
);
735 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
736 "No seat '%s' known", cseat
);
739 if (tty_is_vc(tty
)) {
744 else if (seat
!= m
->seat0
)
745 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
746 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
748 v
= vtnr_from_tty(tty
);
750 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
751 "Cannot determine VT number from virtual console TTY %s", tty
);
755 else if (vtnr
!= (uint32_t) v
)
756 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
757 "Specified TTY and VT number do not match");
759 } else if (tty_is_console(tty
)) {
763 else if (seat
!= m
->seat0
)
764 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
765 "Console TTY specified but seat is not seat0");
768 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
769 "Console TTY specified but VT number is not 0");
773 if (seat_has_vts(seat
)) {
774 if (vtnr
<= 0 || vtnr
> 63)
775 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
776 "VT number out of range");
779 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
780 "Seat has no VTs but VT number not 0");
784 if (t
== _SESSION_TYPE_INVALID
) {
785 if (!isempty(display
))
787 else if (!isempty(tty
))
790 t
= SESSION_UNSPECIFIED
;
793 if (c
== _SESSION_CLASS_INVALID
) {
794 if (t
== SESSION_UNSPECIFIED
)
795 c
= SESSION_BACKGROUND
;
801 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
803 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
807 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
812 /* Check if we are already in a logind session. Or if we are in user@.service
813 * which is a special PAM session that avoids creating a logind session. */
814 r
= manager_get_user_by_pid(m
, leader
, NULL
);
818 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
819 "Already running in a session or user slice");
822 * Old gdm and lightdm start the user-session on the same VT as
823 * the greeter session. But they destroy the greeter session
824 * after the user-session and want the user-session to take
825 * over the VT. We need to support this for
826 * backwards-compatibility, so make sure we allow new sessions
827 * on a VT that a greeter is running on. Furthermore, to allow
828 * re-logins, we have to allow a greeter to take over a used VT for
829 * the exact same reasons.
831 if (c
!= SESSION_GREETER
&&
833 vtnr
< m
->seat0
->position_count
&&
834 m
->seat0
->positions
[vtnr
] &&
835 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
836 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
838 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
839 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
840 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
843 (void) audit_session_from_pid(leader
, &audit_id
);
844 if (audit_session_is_valid(audit_id
)) {
845 /* Keep our session IDs and the audit session IDs in sync */
847 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
850 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
851 * not trust the audit data and let's better register a new ID */
852 if (hashmap_contains(m
->sessions
, id
)) {
853 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
854 audit_id
= AUDIT_SESSION_INVALID
;
863 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
866 } while (hashmap_contains(m
->sessions
, id
));
869 /* The generated names should not clash with 'auto' or 'self' */
870 assert(!SESSION_IS_SELF(id
));
871 assert(!SESSION_IS_AUTO(id
));
873 /* If we are not watching utmp already, try again */
874 manager_reconnect_utmp(m
);
876 r
= manager_add_user_by_uid(m
, uid
, &user
);
880 r
= manager_add_session(m
, id
, &session
);
884 session_set_user(session
, user
);
885 r
= session_set_leader(session
, leader
);
889 session
->original_type
= session
->type
= t
;
891 session
->remote
= remote
;
892 session
->vtnr
= vtnr
;
895 session
->tty
= strdup(tty
);
901 session
->tty_validity
= TTY_FROM_PAM
;
904 if (!isempty(display
)) {
905 session
->display
= strdup(display
);
906 if (!session
->display
) {
912 if (!isempty(remote_user
)) {
913 session
->remote_user
= strdup(remote_user
);
914 if (!session
->remote_user
) {
920 if (!isempty(remote_host
)) {
921 session
->remote_host
= strdup(remote_host
);
922 if (!session
->remote_host
) {
928 if (!isempty(service
)) {
929 session
->service
= strdup(service
);
930 if (!session
->service
) {
936 if (!isempty(desktop
)) {
937 session
->desktop
= strdup(desktop
);
938 if (!session
->desktop
) {
945 r
= seat_attach_session(seat
, session
);
950 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
954 r
= session_start(session
, message
, error
);
958 r
= sd_bus_message_exit_container(message
);
962 session
->create_message
= sd_bus_message_ref(message
);
964 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
965 * session_send_create_reply(). */
971 session_add_to_gc_queue(session
);
974 user_add_to_gc_queue(user
);
979 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
980 Manager
*m
= userdata
;
988 r
= sd_bus_message_read(message
, "s", &name
);
992 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
996 r
= session_release(session
);
1000 return sd_bus_reply_method_return(message
, NULL
);
1003 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1004 Manager
*m
= userdata
;
1012 r
= sd_bus_message_read(message
, "s", &name
);
1016 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1020 /* PolicyKit is done by bus_session_method_activate() */
1022 return bus_session_method_activate(message
, session
, error
);
1025 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1026 const char *session_name
, *seat_name
;
1027 Manager
*m
= userdata
;
1035 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1037 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1041 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1045 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1049 if (session
->seat
!= seat
)
1050 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1051 "Session %s not on seat %s", session_name
, seat_name
);
1053 r
= bus_verify_polkit_async(
1056 "org.freedesktop.login1.chvt",
1060 &m
->polkit_registry
,
1065 return 1; /* Will call us back */
1067 r
= session_activate(session
);
1071 return sd_bus_reply_method_return(message
, NULL
);
1074 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1075 Manager
*m
= userdata
;
1083 r
= sd_bus_message_read(message
, "s", &name
);
1087 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1091 return bus_session_method_lock(message
, session
, error
);
1094 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1095 Manager
*m
= userdata
;
1101 r
= bus_verify_polkit_async(
1104 "org.freedesktop.login1.lock-sessions",
1108 &m
->polkit_registry
,
1113 return 1; /* Will call us back */
1115 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1119 return sd_bus_reply_method_return(message
, NULL
);
1122 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1124 Manager
*m
= userdata
;
1131 r
= sd_bus_message_read(message
, "s", &name
);
1135 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1139 return bus_session_method_kill(message
, session
, error
);
1142 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1143 Manager
*m
= userdata
;
1151 r
= sd_bus_message_read(message
, "u", &uid
);
1155 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1159 return bus_user_method_kill(message
, user
, error
);
1162 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1163 Manager
*m
= userdata
;
1171 r
= sd_bus_message_read(message
, "s", &name
);
1175 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1179 return bus_session_method_terminate(message
, session
, error
);
1182 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1183 Manager
*m
= userdata
;
1191 r
= sd_bus_message_read(message
, "u", &uid
);
1195 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1199 return bus_user_method_terminate(message
, user
, error
);
1202 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1203 Manager
*m
= userdata
;
1211 r
= sd_bus_message_read(message
, "s", &name
);
1215 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1219 return bus_seat_method_terminate(message
, seat
, error
);
1222 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1223 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1224 _cleanup_free_
char *cc
= NULL
;
1225 Manager
*m
= userdata
;
1226 int r
, b
, interactive
;
1229 uint32_t uid
, auth_uid
;
1234 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1238 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1239 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1243 if (!uid_is_valid(uid
)) {
1244 /* Note that we get the owner UID of the session or user unit,
1245 * not the actual client UID here! */
1246 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1251 /* owner_uid is racy, so for authorization we must use euid */
1252 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1259 return errno_or_else(ENOENT
);
1261 r
= bus_verify_polkit_async(
1264 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1265 "org.freedesktop.login1.set-user-linger",
1269 &m
->polkit_registry
,
1274 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1276 (void) mkdir_p_label("/var/lib/systemd", 0755);
1277 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1281 cc
= cescape(pw
->pw_name
);
1285 path
= strjoina("/var/lib/systemd/linger/", cc
);
1293 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1300 if (r
< 0 && errno
!= ENOENT
)
1303 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1305 user_add_to_gc_queue(u
);
1308 return sd_bus_reply_method_return(message
, NULL
);
1311 static int trigger_device(Manager
*m
, sd_device
*d
) {
1312 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1317 r
= sd_device_enumerator_new(&e
);
1321 r
= sd_device_enumerator_allow_uninitialized(e
);
1326 r
= sd_device_enumerator_add_match_parent(e
, d
);
1331 FOREACH_DEVICE(e
, d
) {
1332 _cleanup_free_
char *t
= NULL
;
1335 r
= sd_device_get_syspath(d
, &p
);
1339 t
= path_join(p
, "uevent");
1343 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1349 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1350 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1351 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1352 const char *id_for_seat
;
1359 r
= sd_device_new_from_syspath(&d
, sysfs
);
1363 if (sd_device_has_tag(d
, "seat") <= 0)
1366 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1369 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1372 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1375 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1376 r
= write_string_file_atomic_label(file
, rule
);
1380 return trigger_device(m
, d
);
1383 static int flush_devices(Manager
*m
) {
1384 _cleanup_closedir_
DIR *d
;
1388 d
= opendir("/etc/udev/rules.d");
1390 if (errno
!= ENOENT
)
1391 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1395 FOREACH_DIRENT_ALL(de
, d
, break) {
1396 dirent_ensure_type(d
, de
);
1397 if (!dirent_is_file(de
))
1400 if (!startswith(de
->d_name
, "72-seat-"))
1403 if (!endswith(de
->d_name
, ".rules"))
1406 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1407 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1411 return trigger_device(m
, NULL
);
1414 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1415 const char *sysfs
, *seat
;
1416 Manager
*m
= userdata
;
1422 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1426 if (!path_is_normalized(sysfs
))
1427 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1428 if (!path_startswith(sysfs
, "/sys"))
1429 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1431 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1434 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1440 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1441 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1443 r
= bus_verify_polkit_async(
1446 "org.freedesktop.login1.attach-device",
1450 &m
->polkit_registry
,
1455 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1457 r
= attach_device(m
, seat
, sysfs
);
1461 return sd_bus_reply_method_return(message
, NULL
);
1464 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1465 Manager
*m
= userdata
;
1471 r
= sd_bus_message_read(message
, "b", &interactive
);
1475 r
= bus_verify_polkit_async(
1478 "org.freedesktop.login1.flush-devices",
1482 &m
->polkit_registry
,
1487 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1489 r
= flush_devices(m
);
1493 return sd_bus_reply_method_return(message
, NULL
);
1496 static int have_multiple_sessions(
1505 /* Check for other users' sessions. Greeter sessions do not
1506 * count, and non-login sessions do not count either. */
1507 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1508 if (session
->class == SESSION_USER
&&
1509 session
->user
->user_record
->uid
!= uid
)
1515 static int bus_manager_log_shutdown(
1517 const char *unit_name
) {
1524 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1525 p
= "MESSAGE=System is powering down";
1526 q
= "SHUTDOWN=power-off";
1527 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1528 p
= "MESSAGE=System is rebooting";
1529 q
= "SHUTDOWN=reboot";
1530 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1531 p
= "MESSAGE=System is halting";
1532 q
= "SHUTDOWN=halt";
1533 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1534 p
= "MESSAGE=System is rebooting with kexec";
1535 q
= "SHUTDOWN=kexec";
1537 p
= "MESSAGE=System is shutting down";
1541 if (isempty(m
->wall_message
))
1542 p
= strjoina(p
, ".");
1544 p
= strjoina(p
, " (", m
->wall_message
, ").");
1546 return log_struct(LOG_NOTICE
,
1547 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1552 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1553 Manager
*m
= userdata
;
1558 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1562 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1567 if (until
<= now(CLOCK_MONOTONIC
))
1570 /* We want to ignore the lid switch for a while after each
1571 * suspend, and after boot-up. Hence let's install a timer for
1572 * this. As long as the event source exists we ignore the lid
1575 if (m
->lid_switch_ignore_event_source
) {
1578 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1585 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1587 r
= sd_event_add_time(
1589 &m
->lid_switch_ignore_event_source
,
1592 lid_switch_ignore_handler
, m
);
1597 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1598 int active
= _active
;
1601 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1603 return sd_bus_emit_signal(m
->bus
,
1604 "/org/freedesktop/login1",
1605 "org.freedesktop.login1.Manager",
1606 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1611 static int execute_shutdown_or_sleep(
1614 const char *unit_name
,
1615 sd_bus_error
*error
) {
1617 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1623 assert(w
< _INHIBIT_WHAT_MAX
);
1626 if (w
== INHIBIT_SHUTDOWN
)
1627 bus_manager_log_shutdown(m
, unit_name
);
1629 r
= bus_call_method(
1635 "ss", unit_name
, "replace-irreversibly");
1639 r
= sd_bus_message_read(reply
, "o", &p
);
1643 r
= free_and_strdup(&m
->action_job
, p
);
1647 m
->action_unit
= unit_name
;
1650 /* Make sure the lid switch is ignored for a while */
1651 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1656 /* Tell people that they now may take a lock again */
1657 (void) send_prepare_for(m
, w
, false);
1662 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1664 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1665 Inhibitor
*offending
= NULL
;
1670 if (manager
->action_what
== 0 || manager
->action_job
)
1673 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1674 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1679 (void) get_process_comm(offending
->pid
, &comm
);
1680 u
= uid_to_name(offending
->uid
);
1682 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1683 offending
->uid
, strna(u
),
1684 offending
->pid
, strna(comm
));
1687 /* Actually do the operation */
1688 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1690 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1691 bus_error_message(&error
, r
));
1693 manager
->action_unit
= NULL
;
1694 manager
->action_what
= 0;
1701 static int manager_inhibit_timeout_handler(
1706 Manager
*manager
= userdata
;
1710 assert(manager
->inhibit_timeout_source
== s
);
1712 r
= manager_dispatch_delayed(manager
, true);
1713 return (r
< 0) ? r
: 0;
1716 static int delay_shutdown_or_sleep(
1719 const char *unit_name
) {
1726 assert(w
< _INHIBIT_WHAT_MAX
);
1729 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1731 if (m
->inhibit_timeout_source
) {
1732 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1734 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1736 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1738 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1740 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1741 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1746 m
->action_unit
= unit_name
;
1752 int bus_manager_shutdown_or_sleep_now_or_later(
1754 const char *unit_name
,
1756 sd_bus_error
*error
) {
1758 _cleanup_free_
char *load_state
= NULL
;
1765 assert(w
< _INHIBIT_WHAT_MAX
);
1766 assert(!m
->action_job
);
1768 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1772 if (!streq(load_state
, "loaded"))
1773 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1774 "Unit %s is %s, refusing operation.",
1775 unit_name
, load_state
);
1777 /* Tell everybody to prepare for shutdown/sleep */
1778 (void) send_prepare_for(m
, w
, true);
1781 m
->inhibit_delay_max
> 0 &&
1782 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1785 /* Shutdown is delayed, keep in mind what we
1786 * want to do, and start a timeout */
1787 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1789 /* Shutdown is not delayed, execute it
1791 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1796 static int verify_shutdown_creds(
1798 sd_bus_message
*message
,
1802 const char *action_multiple_sessions
,
1803 const char *action_ignore_inhibit
,
1804 sd_bus_error
*error
) {
1806 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1807 bool multiple_sessions
, blocked
;
1814 assert(w
<= _INHIBIT_WHAT_MAX
);
1816 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1820 r
= sd_bus_creds_get_euid(creds
, &uid
);
1824 r
= have_multiple_sessions(m
, uid
);
1828 multiple_sessions
= r
> 0;
1829 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1831 if (multiple_sessions
&& action_multiple_sessions
) {
1832 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1836 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1839 if (blocked
&& action_ignore_inhibit
) {
1840 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1844 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1847 if (!multiple_sessions
&& !blocked
&& action
) {
1848 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1852 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1858 static int method_do_shutdown_or_sleep(
1860 sd_bus_message
*message
,
1861 const char *unit_name
,
1864 const char *action_multiple_sessions
,
1865 const char *action_ignore_inhibit
,
1866 const char *sleep_verb
,
1867 sd_bus_error
*error
) {
1875 assert(w
<= _INHIBIT_WHAT_MAX
);
1877 r
= sd_bus_message_read(message
, "b", &interactive
);
1881 /* Don't allow multiple jobs being executed at the same time */
1882 if (m
->action_what
> 0)
1883 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1884 "There's already a shutdown or sleep operation in progress");
1887 r
= can_sleep(sleep_verb
);
1889 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1890 "Not enough swap space for hibernation");
1892 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1893 "Sleep verb \"%s\" not supported", sleep_verb
);
1898 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1899 action_ignore_inhibit
, error
);
1903 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1907 return sd_bus_reply_method_return(message
, NULL
);
1910 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1911 Manager
*m
= userdata
;
1913 return method_do_shutdown_or_sleep(
1915 SPECIAL_POWEROFF_TARGET
,
1917 "org.freedesktop.login1.power-off",
1918 "org.freedesktop.login1.power-off-multiple-sessions",
1919 "org.freedesktop.login1.power-off-ignore-inhibit",
1924 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1925 Manager
*m
= userdata
;
1927 return method_do_shutdown_or_sleep(
1929 SPECIAL_REBOOT_TARGET
,
1931 "org.freedesktop.login1.reboot",
1932 "org.freedesktop.login1.reboot-multiple-sessions",
1933 "org.freedesktop.login1.reboot-ignore-inhibit",
1938 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1939 Manager
*m
= userdata
;
1941 return method_do_shutdown_or_sleep(
1943 SPECIAL_HALT_TARGET
,
1945 "org.freedesktop.login1.halt",
1946 "org.freedesktop.login1.halt-multiple-sessions",
1947 "org.freedesktop.login1.halt-ignore-inhibit",
1952 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1953 Manager
*m
= userdata
;
1955 return method_do_shutdown_or_sleep(
1957 SPECIAL_SUSPEND_TARGET
,
1959 "org.freedesktop.login1.suspend",
1960 "org.freedesktop.login1.suspend-multiple-sessions",
1961 "org.freedesktop.login1.suspend-ignore-inhibit",
1966 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1967 Manager
*m
= userdata
;
1969 return method_do_shutdown_or_sleep(
1971 SPECIAL_HIBERNATE_TARGET
,
1973 "org.freedesktop.login1.hibernate",
1974 "org.freedesktop.login1.hibernate-multiple-sessions",
1975 "org.freedesktop.login1.hibernate-ignore-inhibit",
1980 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1981 Manager
*m
= userdata
;
1983 return method_do_shutdown_or_sleep(
1985 SPECIAL_HYBRID_SLEEP_TARGET
,
1987 "org.freedesktop.login1.hibernate",
1988 "org.freedesktop.login1.hibernate-multiple-sessions",
1989 "org.freedesktop.login1.hibernate-ignore-inhibit",
1994 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1995 Manager
*m
= userdata
;
1997 return method_do_shutdown_or_sleep(
1999 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
2001 "org.freedesktop.login1.hibernate",
2002 "org.freedesktop.login1.hibernate-multiple-sessions",
2003 "org.freedesktop.login1.hibernate-ignore-inhibit",
2008 static int nologin_timeout_handler(
2013 Manager
*m
= userdata
;
2015 log_info("Creating /run/nologin, blocking further logins...");
2018 create_shutdown_run_nologin_or_warn() >= 0;
2023 static int update_schedule_file(Manager
*m
) {
2024 _cleanup_free_
char *temp_path
= NULL
;
2025 _cleanup_fclose_
FILE *f
= NULL
;
2030 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
2032 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2034 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
2036 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2038 (void) fchmod(fileno(f
), 0644);
2044 m
->scheduled_shutdown_timeout
,
2045 m
->enable_wall_messages
,
2046 m
->scheduled_shutdown_type
);
2048 if (!isempty(m
->wall_message
)) {
2049 _cleanup_free_
char *t
;
2051 t
= cescape(m
->wall_message
);
2057 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
2060 r
= fflush_and_check(f
);
2064 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2072 (void) unlink(temp_path
);
2073 (void) unlink("/run/systemd/shutdown/scheduled");
2075 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2078 static void reset_scheduled_shutdown(Manager
*m
) {
2081 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2082 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2083 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2085 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
2086 m
->scheduled_shutdown_timeout
= 0;
2087 m
->shutdown_dry_run
= false;
2089 if (m
->unlink_nologin
) {
2090 (void) unlink_or_warn("/run/nologin");
2091 m
->unlink_nologin
= false;
2094 (void) unlink("/run/systemd/shutdown/scheduled");
2097 static int manager_scheduled_shutdown_handler(
2102 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2103 Manager
*m
= userdata
;
2109 if (isempty(m
->scheduled_shutdown_type
))
2112 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2113 target
= SPECIAL_POWEROFF_TARGET
;
2114 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2115 target
= SPECIAL_REBOOT_TARGET
;
2116 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2117 target
= SPECIAL_HALT_TARGET
;
2119 assert_not_reached("unexpected shutdown type");
2121 /* Don't allow multiple jobs being executed at the same time */
2122 if (m
->action_what
> 0) {
2124 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2128 if (m
->shutdown_dry_run
) {
2129 /* We do not process delay inhibitors here. Otherwise, we
2130 * would have to be considered "in progress" (like the check
2131 * above) for some seconds after our admin has seen the final
2134 bus_manager_log_shutdown(m
, target
);
2135 log_info("Running in dry run, suppressing action.");
2136 reset_scheduled_shutdown(m
);
2141 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2143 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2150 reset_scheduled_shutdown(m
);
2154 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2155 Manager
*m
= userdata
;
2156 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2157 const char *action_multiple_sessions
= NULL
;
2158 const char *action_ignore_inhibit
= NULL
;
2159 const char *action
= NULL
;
2163 bool dry_run
= false;
2168 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2172 if (startswith(type
, "dry-")) {
2177 if (streq(type
, "poweroff")) {
2178 action
= "org.freedesktop.login1.power-off";
2179 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2180 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2181 } else if (streq(type
, "reboot")) {
2182 action
= "org.freedesktop.login1.reboot";
2183 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2184 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2185 } else if (streq(type
, "halt")) {
2186 action
= "org.freedesktop.login1.halt";
2187 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2188 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2190 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2192 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2193 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2197 if (m
->scheduled_shutdown_timeout_source
) {
2198 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2200 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2202 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2204 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2206 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2207 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2209 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2212 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2214 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2218 m
->shutdown_dry_run
= dry_run
;
2220 if (m
->nologin_timeout_source
) {
2221 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2223 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2225 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2227 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2229 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2230 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2232 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2235 m
->scheduled_shutdown_timeout
= elapse
;
2237 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2239 const char *tty
= NULL
;
2241 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2242 (void) sd_bus_creds_get_tty(creds
, &tty
);
2244 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2246 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2251 r
= manager_setup_wall_message_timer(m
);
2255 r
= update_schedule_file(m
);
2259 return sd_bus_reply_method_return(message
, NULL
);
2262 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2263 Manager
*m
= userdata
;
2269 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2270 reset_scheduled_shutdown(m
);
2272 if (cancelled
&& m
->enable_wall_messages
) {
2273 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2274 _cleanup_free_
char *username
= NULL
;
2275 const char *tty
= NULL
;
2279 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2281 (void) sd_bus_creds_get_uid(creds
, &uid
);
2282 (void) sd_bus_creds_get_tty(creds
, &tty
);
2285 username
= uid_to_name(uid
);
2286 utmp_wall("The system shutdown has been cancelled",
2287 username
, tty
, logind_wall_tty_filter
, m
);
2290 return sd_bus_reply_method_return(message
, "b", cancelled
);
2293 static int method_can_shutdown_or_sleep(
2295 sd_bus_message
*message
,
2298 const char *action_multiple_sessions
,
2299 const char *action_ignore_inhibit
,
2300 const char *sleep_verb
,
2301 sd_bus_error
*error
) {
2303 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2304 HandleAction handle
;
2305 bool multiple_sessions
, challenge
, blocked
;
2306 const char *result
= NULL
;
2313 assert(w
<= _INHIBIT_WHAT_MAX
);
2315 assert(action_multiple_sessions
);
2316 assert(action_ignore_inhibit
);
2319 r
= can_sleep(sleep_verb
);
2320 if (IN_SET(r
, 0, -ENOSPC
))
2321 return sd_bus_reply_method_return(message
, "s", "na");
2326 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2330 r
= sd_bus_creds_get_euid(creds
, &uid
);
2334 r
= have_multiple_sessions(m
, uid
);
2338 multiple_sessions
= r
> 0;
2339 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2341 handle
= handle_action_from_string(sleep_verb
);
2345 target
= manager_target_for_action(handle
);
2347 _cleanup_free_
char *load_state
= NULL
;
2349 r
= unit_load_state(m
->bus
, target
, &load_state
);
2353 if (!streq(load_state
, "loaded")) {
2360 if (multiple_sessions
) {
2361 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2368 result
= "challenge";
2374 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2381 } else if (challenge
) {
2382 if (!result
|| streq(result
, "yes"))
2383 result
= "challenge";
2388 if (!multiple_sessions
&& !blocked
) {
2389 /* If neither inhibit nor multiple sessions
2390 * apply then just check the normal policy */
2392 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2399 result
= "challenge";
2405 return sd_bus_reply_method_return(message
, "s", result
);
2408 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2409 Manager
*m
= userdata
;
2411 return method_can_shutdown_or_sleep(
2414 "org.freedesktop.login1.power-off",
2415 "org.freedesktop.login1.power-off-multiple-sessions",
2416 "org.freedesktop.login1.power-off-ignore-inhibit",
2421 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2422 Manager
*m
= userdata
;
2424 return method_can_shutdown_or_sleep(
2427 "org.freedesktop.login1.reboot",
2428 "org.freedesktop.login1.reboot-multiple-sessions",
2429 "org.freedesktop.login1.reboot-ignore-inhibit",
2434 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2435 Manager
*m
= userdata
;
2437 return method_can_shutdown_or_sleep(
2440 "org.freedesktop.login1.halt",
2441 "org.freedesktop.login1.halt-multiple-sessions",
2442 "org.freedesktop.login1.halt-ignore-inhibit",
2447 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2448 Manager
*m
= userdata
;
2450 return method_can_shutdown_or_sleep(
2453 "org.freedesktop.login1.suspend",
2454 "org.freedesktop.login1.suspend-multiple-sessions",
2455 "org.freedesktop.login1.suspend-ignore-inhibit",
2460 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2461 Manager
*m
= userdata
;
2463 return method_can_shutdown_or_sleep(
2466 "org.freedesktop.login1.hibernate",
2467 "org.freedesktop.login1.hibernate-multiple-sessions",
2468 "org.freedesktop.login1.hibernate-ignore-inhibit",
2473 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2474 Manager
*m
= userdata
;
2476 return method_can_shutdown_or_sleep(
2479 "org.freedesktop.login1.hibernate",
2480 "org.freedesktop.login1.hibernate-multiple-sessions",
2481 "org.freedesktop.login1.hibernate-ignore-inhibit",
2486 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2487 Manager
*m
= userdata
;
2489 return method_can_shutdown_or_sleep(
2492 "org.freedesktop.login1.hibernate",
2493 "org.freedesktop.login1.hibernate-multiple-sessions",
2494 "org.freedesktop.login1.hibernate-ignore-inhibit",
2495 "suspend-then-hibernate",
2499 static int property_get_reboot_parameter(
2502 const char *interface
,
2503 const char *property
,
2504 sd_bus_message
*reply
,
2506 sd_bus_error
*error
) {
2507 _cleanup_free_
char *parameter
= NULL
;
2514 r
= read_reboot_parameter(¶meter
);
2518 return sd_bus_message_append(reply
, "s", parameter
);
2521 static int method_set_reboot_parameter(
2522 sd_bus_message
*message
,
2524 sd_bus_error
*error
) {
2526 Manager
*m
= userdata
;
2533 r
= sd_bus_message_read(message
, "s", &arg
);
2537 r
= detect_container();
2541 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2542 "Reboot parameter not supported in containers, refusing.");
2544 r
= bus_verify_polkit_async(message
,
2546 "org.freedesktop.login1.set-reboot-parameter",
2550 &m
->polkit_registry
,
2555 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2557 r
= update_reboot_parameter_and_warn(arg
, false);
2561 return sd_bus_reply_method_return(message
, NULL
);
2564 static int method_can_reboot_parameter(
2565 sd_bus_message
*message
,
2567 sd_bus_error
*error
) {
2569 _unused_ Manager
*m
= userdata
;
2575 r
= detect_container();
2578 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2579 return sd_bus_reply_method_return(message
, "s", "na");
2581 return return_test_polkit(
2584 "org.freedesktop.login1.set-reboot-parameter",
2590 static int property_get_reboot_to_firmware_setup(
2593 const char *interface
,
2594 const char *property
,
2595 sd_bus_message
*reply
,
2597 sd_bus_error
*error
) {
2604 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2606 /* EFI case: let's see what is currently configured in the EFI variables */
2607 r
= efi_get_reboot_to_firmware();
2608 if (r
< 0 && r
!= -EOPNOTSUPP
)
2609 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2611 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2613 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2614 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2615 if (errno
!= ENOENT
)
2616 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2623 return sd_bus_message_append(reply
, "b", r
> 0);
2626 static int method_set_reboot_to_firmware_setup(
2627 sd_bus_message
*message
,
2629 sd_bus_error
*error
) {
2631 Manager
*m
= userdata
;
2638 r
= sd_bus_message_read(message
, "b", &b
);
2642 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2644 /* EFI case: let's see what the firmware supports */
2646 r
= efi_reboot_to_firmware_supported();
2647 if (r
== -EOPNOTSUPP
)
2648 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2654 } else if (r
<= 0) {
2655 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2658 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2660 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2662 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2665 r
= bus_verify_polkit_async(message
,
2667 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2671 &m
->polkit_registry
,
2676 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2679 r
= efi_set_reboot_to_firmware(b
);
2684 r
= touch("/run/systemd/reboot-to-firmware-setup");
2688 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2693 return sd_bus_reply_method_return(message
, NULL
);
2696 static int method_can_reboot_to_firmware_setup(
2697 sd_bus_message
*message
,
2699 sd_bus_error
*error
) {
2701 _unused_ Manager
*m
= userdata
;
2707 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2709 /* EFI case: let's see what the firmware supports */
2711 r
= efi_reboot_to_firmware_supported();
2713 if (r
!= -EOPNOTSUPP
)
2714 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2716 return sd_bus_reply_method_return(message
, "s", "na");
2719 } else if (r
<= 0) {
2720 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2723 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2725 return sd_bus_reply_method_return(message
, "s", "na");
2728 return return_test_polkit(
2731 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2737 static int property_get_reboot_to_boot_loader_menu(
2740 const char *interface
,
2741 const char *property
,
2742 sd_bus_message
*reply
,
2744 sd_bus_error
*error
) {
2746 uint64_t x
= UINT64_MAX
;
2753 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2755 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2757 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2758 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2759 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2762 r
= efi_loader_get_config_timeout_one_shot(&x
);
2765 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
2769 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2771 _cleanup_free_
char *v
= NULL
;
2773 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2775 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2778 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2780 r
= safe_atou64(v
, &x
);
2782 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2786 return sd_bus_message_append(reply
, "t", x
);
2789 static int method_set_reboot_to_boot_loader_menu(
2790 sd_bus_message
*message
,
2792 sd_bus_error
*error
) {
2794 Manager
*m
= userdata
;
2802 r
= sd_bus_message_read(message
, "t", &x
);
2806 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2810 /* EFI case: let's see if booting into boot loader menu is supported. */
2812 r
= efi_loader_get_features(&features
);
2814 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2815 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2816 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2820 } else if (r
<= 0) {
2821 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2824 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2826 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2828 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2831 r
= bus_verify_polkit_async(message
,
2833 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2837 &m
->polkit_registry
,
2842 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2845 if (x
== UINT64_MAX
)
2846 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", NULL
, 0);
2848 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2849 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2851 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", buf
);
2856 if (x
== UINT64_MAX
) {
2857 if (unlink("/run/systemd/reboot-to-loader-menu") < 0 && errno
!= ENOENT
)
2860 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2862 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2864 r
= write_string_file_atomic_label("/run/systemd/reboot-to-loader-menu", buf
);
2870 return sd_bus_reply_method_return(message
, NULL
);
2873 static int method_can_reboot_to_boot_loader_menu(
2874 sd_bus_message
*message
,
2876 sd_bus_error
*error
) {
2878 _unused_ Manager
*m
= userdata
;
2884 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2886 uint64_t features
= 0;
2888 /* EFI case, let's see if booting into boot loader menu is supported. */
2890 r
= efi_loader_get_features(&features
);
2892 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2893 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2894 return sd_bus_reply_method_return(message
, "s", "na");
2896 } else if (r
<= 0) {
2897 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2900 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2902 return sd_bus_reply_method_return(message
, "s", "na");
2905 return return_test_polkit(
2908 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2914 static int property_get_reboot_to_boot_loader_entry(
2917 const char *interface
,
2918 const char *property
,
2919 sd_bus_message
*reply
,
2921 sd_bus_error
*error
) {
2923 _cleanup_free_
char *v
= NULL
;
2924 Manager
*m
= userdata
;
2925 const char *x
= NULL
;
2932 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2934 /* EFI case: let's read the LoaderEntryOneShot variable */
2936 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
2939 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
2941 x
= m
->efi_loader_entry_one_shot
;
2944 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2947 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2949 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2952 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
2953 } else if (!efi_loader_entry_name_valid(v
))
2954 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2959 return sd_bus_message_append(reply
, "s", x
);
2962 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
2963 _cleanup_(boot_config_free
) BootConfig config
= {};
2969 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2970 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2973 r
= manager_read_efi_boot_loader_entries(m
);
2975 (void) boot_entries_augment_from_loader(&config
, m
->efi_boot_loader_entries
, true);
2977 return boot_config_has_entry(&config
, id
);
2980 static int method_set_reboot_to_boot_loader_entry(
2981 sd_bus_message
*message
,
2983 sd_bus_error
*error
) {
2985 Manager
*m
= userdata
;
2993 r
= sd_bus_message_read(message
, "s", &v
);
2999 else if (efi_loader_entry_name_valid(v
)) {
3000 r
= boot_loader_entry_exists(m
, v
);
3004 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3006 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3008 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3012 /* EFI case: let's see if booting into boot loader entry is supported. */
3014 r
= efi_loader_get_features(&features
);
3016 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3017 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3018 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3022 } else if (r
<= 0) {
3023 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3026 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3028 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3030 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3033 r
= bus_verify_polkit_async(message
,
3035 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3039 &m
->polkit_registry
,
3044 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3049 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", NULL
, 0);
3051 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", v
);
3056 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3059 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3065 return sd_bus_reply_method_return(message
, NULL
);
3068 static int method_can_reboot_to_boot_loader_entry(
3069 sd_bus_message
*message
,
3071 sd_bus_error
*error
) {
3073 _unused_ Manager
*m
= userdata
;
3079 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3081 uint64_t features
= 0;
3083 /* EFI case, let's see if booting into boot loader entry is supported. */
3085 r
= efi_loader_get_features(&features
);
3087 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3088 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3089 return sd_bus_reply_method_return(message
, "s", "na");
3091 } else if (r
<= 0) {
3092 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3095 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3097 return sd_bus_reply_method_return(message
, "s", "na");
3100 return return_test_polkit(
3103 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3109 static int property_get_boot_loader_entries(
3112 const char *interface
,
3113 const char *property
,
3114 sd_bus_message
*reply
,
3116 sd_bus_error
*error
) {
3118 _cleanup_(boot_config_free
) BootConfig config
= {};
3119 Manager
*m
= userdata
;
3127 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3128 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3131 r
= manager_read_efi_boot_loader_entries(m
);
3133 (void) boot_entries_augment_from_loader(&config
, m
->efi_boot_loader_entries
, true);
3135 r
= sd_bus_message_open_container(reply
, 'a', "s");
3139 for (i
= 0; i
< config
.n_entries
; i
++) {
3140 BootEntry
*e
= config
.entries
+ i
;
3142 r
= sd_bus_message_append(reply
, "s", e
->id
);
3147 return sd_bus_message_close_container(reply
);
3150 static int method_set_wall_message(
3151 sd_bus_message
*message
,
3153 sd_bus_error
*error
) {
3156 Manager
*m
= userdata
;
3158 unsigned enable_wall_messages
;
3163 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3167 /* Short-circuit the operation if the desired state is already in place, to
3168 * avoid an unnecessary polkit permission check. */
3169 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3170 m
->enable_wall_messages
== enable_wall_messages
)
3173 r
= bus_verify_polkit_async(message
,
3175 "org.freedesktop.login1.set-wall-message",
3179 &m
->polkit_registry
,
3184 return 1; /* Will call us back */
3186 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3190 m
->enable_wall_messages
= enable_wall_messages
;
3193 return sd_bus_reply_method_return(message
, NULL
);
3196 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3197 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3198 const char *who
, *why
, *what
, *mode
;
3199 _cleanup_free_
char *id
= NULL
;
3200 _cleanup_close_
int fifo_fd
= -1;
3201 Manager
*m
= userdata
;
3202 Inhibitor
*i
= NULL
;
3212 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3216 w
= inhibit_what_from_string(what
);
3218 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3219 "Invalid what specification %s", what
);
3221 mm
= inhibit_mode_from_string(mode
);
3223 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3224 "Invalid mode specification %s", mode
);
3226 /* Delay is only supported for shutdown/sleep */
3227 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3228 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3229 "Delay inhibitors only supported for shutdown and sleep");
3231 /* Don't allow taking delay locks while we are already
3232 * executing the operation. We shouldn't create the impression
3233 * that the lock was successful if the machine is about to go
3234 * down/suspend any moment. */
3235 if (m
->action_what
& w
)
3236 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3237 "The operation inhibition has been requested for is already running");
3239 r
= bus_verify_polkit_async(
3242 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3243 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3244 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3245 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3246 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3247 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3248 "org.freedesktop.login1.inhibit-handle-lid-switch",
3252 &m
->polkit_registry
,
3257 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3259 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3263 r
= sd_bus_creds_get_euid(creds
, &uid
);
3267 r
= sd_bus_creds_get_pid(creds
, &pid
);
3271 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3272 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3273 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3279 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3282 } while (hashmap_get(m
->inhibitors
, id
));
3284 r
= manager_add_inhibitor(m
, id
, &i
);
3292 i
->why
= strdup(why
);
3293 i
->who
= strdup(who
);
3295 if (!i
->why
|| !i
->who
) {
3300 fifo_fd
= inhibitor_create_fifo(i
);
3306 r
= inhibitor_start(i
);
3310 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3319 static const sd_bus_vtable manager_vtable
[] = {
3320 SD_BUS_VTABLE_START(0),
3322 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3323 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3325 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3326 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3327 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3328 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3329 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3330 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3331 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3332 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3333 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3334 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3335 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3336 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3337 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3338 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3339 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3340 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3341 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3342 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3343 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3344 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3345 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3346 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3347 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3348 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3349 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3350 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3351 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3352 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3353 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3354 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3355 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3356 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3357 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3358 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3359 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3360 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3361 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3362 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3363 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3365 SD_BUS_METHOD_WITH_NAMES("GetSession",
3367 SD_BUS_PARAM(session_id
),
3369 SD_BUS_PARAM(object_path
),
3371 SD_BUS_VTABLE_UNPRIVILEGED
),
3372 SD_BUS_METHOD_WITH_NAMES("GetSessionByPID",
3376 SD_BUS_PARAM(object_path
),
3377 method_get_session_by_pid
,
3378 SD_BUS_VTABLE_UNPRIVILEGED
),
3379 SD_BUS_METHOD_WITH_NAMES("GetUser",
3383 SD_BUS_PARAM(object_path
),
3385 SD_BUS_VTABLE_UNPRIVILEGED
),
3386 SD_BUS_METHOD_WITH_NAMES("GetUserByPID",
3390 SD_BUS_PARAM(object_path
),
3391 method_get_user_by_pid
,
3392 SD_BUS_VTABLE_UNPRIVILEGED
),
3393 SD_BUS_METHOD_WITH_NAMES("GetSeat",
3395 SD_BUS_PARAM(seat_id
),
3397 SD_BUS_PARAM(object_path
),
3399 SD_BUS_VTABLE_UNPRIVILEGED
),
3400 SD_BUS_METHOD_WITH_NAMES("ListSessions",
3403 SD_BUS_PARAM(sessions
),
3404 method_list_sessions
,
3405 SD_BUS_VTABLE_UNPRIVILEGED
),
3406 SD_BUS_METHOD_WITH_NAMES("ListUsers",
3409 SD_BUS_PARAM(users
),
3411 SD_BUS_VTABLE_UNPRIVILEGED
),
3412 SD_BUS_METHOD_WITH_NAMES("ListSeats",
3415 SD_BUS_PARAM(seats
),
3417 SD_BUS_VTABLE_UNPRIVILEGED
),
3418 SD_BUS_METHOD_WITH_NAMES("ListInhibitors",
3421 SD_BUS_PARAM(inhibitors
),
3422 method_list_inhibitors
,
3423 SD_BUS_VTABLE_UNPRIVILEGED
),
3424 SD_BUS_METHOD_WITH_NAMES("CreateSession",
3425 "uusssssussbssa(sv)",
3428 SD_BUS_PARAM(service
)
3431 SD_BUS_PARAM(desktop
)
3432 SD_BUS_PARAM(seat_id
)
3435 SD_BUS_PARAM(display
)
3436 SD_BUS_PARAM(remote
)
3437 SD_BUS_PARAM(remote_user
)
3438 SD_BUS_PARAM(remote_host
)
3439 SD_BUS_PARAM(properties
),
3441 SD_BUS_PARAM(session_id
)
3442 SD_BUS_PARAM(object_path
)
3443 SD_BUS_PARAM(runtime_path
)
3444 SD_BUS_PARAM(fifo_fd
)
3446 SD_BUS_PARAM(seat_id
)
3448 SD_BUS_PARAM(existing
),
3449 method_create_session
,
3451 SD_BUS_METHOD_WITH_NAMES("ReleaseSession",
3453 SD_BUS_PARAM(session_id
),
3455 method_release_session
,
3457 SD_BUS_METHOD_WITH_NAMES("ActivateSession",
3459 SD_BUS_PARAM(session_id
),
3461 method_activate_session
,
3462 SD_BUS_VTABLE_UNPRIVILEGED
),
3463 SD_BUS_METHOD_WITH_NAMES("ActivateSessionOnSeat",
3465 SD_BUS_PARAM(session_id
)
3466 SD_BUS_PARAM(seat_id
),
3468 method_activate_session_on_seat
,
3469 SD_BUS_VTABLE_UNPRIVILEGED
),
3470 SD_BUS_METHOD_WITH_NAMES("LockSession",
3472 SD_BUS_PARAM(session_id
),
3474 method_lock_session
,
3475 SD_BUS_VTABLE_UNPRIVILEGED
),
3476 SD_BUS_METHOD_WITH_NAMES("UnlockSession",
3478 SD_BUS_PARAM(session_id
),
3480 method_lock_session
,
3481 SD_BUS_VTABLE_UNPRIVILEGED
),
3482 SD_BUS_METHOD("LockSessions",
3485 method_lock_sessions
,
3486 SD_BUS_VTABLE_UNPRIVILEGED
),
3487 SD_BUS_METHOD("UnlockSessions",
3490 method_lock_sessions
,
3491 SD_BUS_VTABLE_UNPRIVILEGED
),
3492 SD_BUS_METHOD_WITH_NAMES("KillSession",
3494 SD_BUS_PARAM(session_id
)
3496 SD_BUS_PARAM(signal_number
),
3498 method_kill_session
,
3499 SD_BUS_VTABLE_UNPRIVILEGED
),
3500 SD_BUS_METHOD_WITH_NAMES("KillUser",
3503 SD_BUS_PARAM(signal_number
),
3506 SD_BUS_VTABLE_UNPRIVILEGED
),
3507 SD_BUS_METHOD_WITH_NAMES("TerminateSession",
3509 SD_BUS_PARAM(session_id
),
3511 method_terminate_session
,
3512 SD_BUS_VTABLE_UNPRIVILEGED
),
3513 SD_BUS_METHOD_WITH_NAMES("TerminateUser",
3517 method_terminate_user
,
3518 SD_BUS_VTABLE_UNPRIVILEGED
),
3519 SD_BUS_METHOD_WITH_NAMES("TerminateSeat",
3521 SD_BUS_PARAM(seat_id
),
3523 method_terminate_seat
,
3524 SD_BUS_VTABLE_UNPRIVILEGED
),
3525 SD_BUS_METHOD_WITH_NAMES("SetUserLinger",
3528 SD_BUS_PARAM(enable
)
3529 SD_BUS_PARAM(interactive
),
3531 method_set_user_linger
,
3532 SD_BUS_VTABLE_UNPRIVILEGED
),
3533 SD_BUS_METHOD_WITH_NAMES("AttachDevice",
3535 SD_BUS_PARAM(seat_id
)
3536 SD_BUS_PARAM(sysfs_path
)
3537 SD_BUS_PARAM(interactive
),
3539 method_attach_device
,
3540 SD_BUS_VTABLE_UNPRIVILEGED
),
3541 SD_BUS_METHOD_WITH_NAMES("FlushDevices",
3543 SD_BUS_PARAM(interactive
),
3545 method_flush_devices
,
3546 SD_BUS_VTABLE_UNPRIVILEGED
),
3547 SD_BUS_METHOD_WITH_NAMES("PowerOff",
3549 SD_BUS_PARAM(interactive
),
3552 SD_BUS_VTABLE_UNPRIVILEGED
),
3553 SD_BUS_METHOD_WITH_NAMES("Reboot",
3555 SD_BUS_PARAM(interactive
),
3558 SD_BUS_VTABLE_UNPRIVILEGED
),
3559 SD_BUS_METHOD_WITH_NAMES("Halt",
3561 SD_BUS_PARAM(interactive
),
3564 SD_BUS_VTABLE_UNPRIVILEGED
),
3565 SD_BUS_METHOD_WITH_NAMES("Suspend",
3567 SD_BUS_PARAM(interactive
),
3570 SD_BUS_VTABLE_UNPRIVILEGED
),
3571 SD_BUS_METHOD_WITH_NAMES("Hibernate",
3573 SD_BUS_PARAM(interactive
),
3576 SD_BUS_VTABLE_UNPRIVILEGED
),
3577 SD_BUS_METHOD_WITH_NAMES("HybridSleep",
3579 SD_BUS_PARAM(interactive
),
3581 method_hybrid_sleep
,
3582 SD_BUS_VTABLE_UNPRIVILEGED
),
3583 SD_BUS_METHOD_WITH_NAMES("SuspendThenHibernate",
3585 SD_BUS_PARAM(interactive
),
3587 method_suspend_then_hibernate
,
3588 SD_BUS_VTABLE_UNPRIVILEGED
),
3589 SD_BUS_METHOD_WITH_NAMES("CanPowerOff",
3592 SD_BUS_PARAM(result
),
3593 method_can_poweroff
,
3594 SD_BUS_VTABLE_UNPRIVILEGED
),
3595 SD_BUS_METHOD_WITH_NAMES("CanReboot",
3598 SD_BUS_PARAM(result
),
3600 SD_BUS_VTABLE_UNPRIVILEGED
),
3601 SD_BUS_METHOD_WITH_NAMES("CanHalt",
3604 SD_BUS_PARAM(result
),
3606 SD_BUS_VTABLE_UNPRIVILEGED
),
3607 SD_BUS_METHOD_WITH_NAMES("CanSuspend",
3610 SD_BUS_PARAM(result
),
3612 SD_BUS_VTABLE_UNPRIVILEGED
),
3613 SD_BUS_METHOD_WITH_NAMES("CanHibernate",
3616 SD_BUS_PARAM(result
),
3617 method_can_hibernate
,
3618 SD_BUS_VTABLE_UNPRIVILEGED
),
3619 SD_BUS_METHOD_WITH_NAMES("CanHybridSleep",
3622 SD_BUS_PARAM(result
),
3623 method_can_hybrid_sleep
,
3624 SD_BUS_VTABLE_UNPRIVILEGED
),
3625 SD_BUS_METHOD_WITH_NAMES("CanSuspendThenHibernate",
3628 SD_BUS_PARAM(result
),
3629 method_can_suspend_then_hibernate
,
3630 SD_BUS_VTABLE_UNPRIVILEGED
),
3631 SD_BUS_METHOD_WITH_NAMES("ScheduleShutdown",
3636 method_schedule_shutdown
,
3637 SD_BUS_VTABLE_UNPRIVILEGED
),
3638 SD_BUS_METHOD_WITH_NAMES("CancelScheduledShutdown",
3641 SD_BUS_PARAM(cancelled
),
3642 method_cancel_scheduled_shutdown
,
3643 SD_BUS_VTABLE_UNPRIVILEGED
),
3644 SD_BUS_METHOD_WITH_NAMES("Inhibit",
3651 SD_BUS_PARAM(pipe_fd
),
3653 SD_BUS_VTABLE_UNPRIVILEGED
),
3654 SD_BUS_METHOD_WITH_NAMES("CanRebootParameter",
3657 SD_BUS_PARAM(result
),
3658 method_can_reboot_parameter
,
3659 SD_BUS_VTABLE_UNPRIVILEGED
),
3660 SD_BUS_METHOD_WITH_NAMES("SetRebootParameter",
3662 SD_BUS_PARAM(parameter
),
3664 method_set_reboot_parameter
,
3665 SD_BUS_VTABLE_UNPRIVILEGED
),
3666 SD_BUS_METHOD_WITH_NAMES("CanRebootToFirmwareSetup",
3669 SD_BUS_PARAM(result
),
3670 method_can_reboot_to_firmware_setup
,
3671 SD_BUS_VTABLE_UNPRIVILEGED
),
3672 SD_BUS_METHOD_WITH_NAMES("SetRebootToFirmwareSetup",
3674 SD_BUS_PARAM(enable
),
3676 method_set_reboot_to_firmware_setup
,
3677 SD_BUS_VTABLE_UNPRIVILEGED
),
3678 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderMenu",
3681 SD_BUS_PARAM(result
),
3682 method_can_reboot_to_boot_loader_menu
,
3683 SD_BUS_VTABLE_UNPRIVILEGED
),
3684 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderMenu",
3686 SD_BUS_PARAM(timeout
),
3688 method_set_reboot_to_boot_loader_menu
,
3689 SD_BUS_VTABLE_UNPRIVILEGED
),
3690 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderEntry",
3693 SD_BUS_PARAM(result
),
3694 method_can_reboot_to_boot_loader_entry
,
3695 SD_BUS_VTABLE_UNPRIVILEGED
),
3696 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderEntry",
3698 SD_BUS_PARAM(boot_loader_entry
),
3700 method_set_reboot_to_boot_loader_entry
,
3701 SD_BUS_VTABLE_UNPRIVILEGED
),
3702 SD_BUS_METHOD_WITH_NAMES("SetWallMessage",
3704 SD_BUS_PARAM(wall_message
)
3705 SD_BUS_PARAM(enable
),
3707 method_set_wall_message
,
3708 SD_BUS_VTABLE_UNPRIVILEGED
),
3710 SD_BUS_SIGNAL_WITH_NAMES("SessionNew",
3712 SD_BUS_PARAM(session_id
)
3713 SD_BUS_PARAM(object_path
),
3715 SD_BUS_SIGNAL_WITH_NAMES("SessionRemoved",
3717 SD_BUS_PARAM(session_id
)
3718 SD_BUS_PARAM(object_path
),
3720 SD_BUS_SIGNAL_WITH_NAMES("UserNew",
3723 SD_BUS_PARAM(object_path
),
3725 SD_BUS_SIGNAL_WITH_NAMES("UserRemoved",
3728 SD_BUS_PARAM(object_path
),
3730 SD_BUS_SIGNAL_WITH_NAMES("SeatNew",
3732 SD_BUS_PARAM(seat_id
)
3733 SD_BUS_PARAM(object_path
),
3735 SD_BUS_SIGNAL_WITH_NAMES("SeatRemoved",
3737 SD_BUS_PARAM(seat_id
)
3738 SD_BUS_PARAM(object_path
),
3740 SD_BUS_SIGNAL_WITH_NAMES("PrepareForShutdown",
3742 SD_BUS_PARAM(start
),
3744 SD_BUS_SIGNAL_WITH_NAMES("PrepareForSleep",
3746 SD_BUS_PARAM(start
),
3752 const BusObjectImplementation manager_object
= {
3753 "/org/freedesktop/login1",
3754 "org.freedesktop.login1.Manager",
3755 .vtables
= BUS_VTABLES(manager_vtable
),
3756 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
3761 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
3768 if (result
&& !streq(result
, "done")) {
3769 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3771 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3772 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
3773 return session_send_create_reply(s
, &e
);
3776 return session_send_create_reply(s
, NULL
);
3779 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3780 const char *path
, *result
, *unit
;
3781 Manager
*m
= userdata
;
3790 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3792 bus_log_parse_error(r
);
3796 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3797 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
3799 /* Tell people that they now may take a lock again */
3800 (void) send_prepare_for(m
, m
->action_what
, false);
3802 m
->action_job
= mfree(m
->action_job
);
3803 m
->action_unit
= NULL
;
3808 session
= hashmap_get(m
->session_units
, unit
);
3810 if (streq_ptr(path
, session
->scope_job
)) {
3811 session
->scope_job
= mfree(session
->scope_job
);
3812 (void) session_jobs_reply(session
, id
, unit
, result
);
3814 session_save(session
);
3815 user_save(session
->user
);
3818 session_add_to_gc_queue(session
);
3821 user
= hashmap_get(m
->user_units
, unit
);
3823 if (streq_ptr(path
, user
->service_job
)) {
3824 user
->service_job
= mfree(user
->service_job
);
3826 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3827 (void) session_jobs_reply(session
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
3832 user_add_to_gc_queue(user
);
3838 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3839 const char *path
, *unit
;
3840 Manager
*m
= userdata
;
3848 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3850 bus_log_parse_error(r
);
3854 session
= hashmap_get(m
->session_units
, unit
);
3856 session_add_to_gc_queue(session
);
3858 user
= hashmap_get(m
->user_units
, unit
);
3860 user_add_to_gc_queue(user
);
3865 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3866 _cleanup_free_
char *unit
= NULL
;
3867 Manager
*m
= userdata
;
3876 path
= sd_bus_message_get_path(message
);
3880 r
= unit_name_from_dbus_path(path
, &unit
);
3881 if (r
== -EINVAL
) /* not a unit */
3888 session
= hashmap_get(m
->session_units
, unit
);
3890 session_add_to_gc_queue(session
);
3892 user
= hashmap_get(m
->user_units
, unit
);
3894 user_add_to_gc_queue(user
);
3899 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3900 Manager
*m
= userdata
;
3908 r
= sd_bus_message_read(message
, "b", &b
);
3910 bus_log_parse_error(r
);
3917 /* systemd finished reloading, let's recheck all our sessions */
3918 log_debug("System manager has been reloaded, rechecking sessions...");
3920 HASHMAP_FOREACH(session
, m
->sessions
, i
)
3921 session_add_to_gc_queue(session
);
3926 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3931 l
= strv_from_stdarg_alloca(property
);
3933 return sd_bus_emit_properties_changed_strv(
3935 "/org/freedesktop/login1",
3936 "org.freedesktop.login1.Manager",
3940 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3945 r
= sd_bus_message_read(reply
, "o", &j
);
3957 int manager_start_scope(
3962 const char *description
,
3965 const char *requires_mounts_for
,
3966 sd_bus_message
*more_properties
,
3967 sd_bus_error
*error
,
3970 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3979 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
3983 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3987 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3991 if (!isempty(slice
)) {
3992 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3997 if (!isempty(description
)) {
3998 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4003 STRV_FOREACH(i
, wants
) {
4004 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
4009 STRV_FOREACH(i
, after
) {
4010 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4015 if (!empty_or_root(requires_mounts_for
)) {
4016 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4021 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4023 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4027 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
4031 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4032 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
4034 return bus_log_create_error(r
);
4036 if (more_properties
) {
4037 /* If TasksMax also appears here, it will overwrite the default value set above */
4038 r
= sd_bus_message_copy(m
, more_properties
, true);
4043 r
= sd_bus_message_close_container(m
);
4047 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4051 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4055 return strdup_job(reply
, job
);
4058 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4059 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4066 r
= bus_call_method(
4072 "ss", unit
, "replace");
4076 return strdup_job(reply
, job
);
4079 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4080 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4087 r
= bus_call_method(
4093 "ss", unit
, "fail");
4095 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4096 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
4099 sd_bus_error_free(error
);
4106 return strdup_job(reply
, job
);
4109 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4110 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4111 _cleanup_free_
char *path
= NULL
;
4117 path
= unit_dbus_path_from_name(scope
);
4121 r
= sd_bus_call_method(
4123 "org.freedesktop.systemd1",
4125 "org.freedesktop.systemd1.Scope",
4131 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4132 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
4133 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
4136 sd_bus_error_move(ret_error
, &error
);
4143 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4147 return bus_call_method(
4153 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4156 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4157 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4158 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4159 _cleanup_free_
char *path
= NULL
;
4166 path
= unit_dbus_path_from_name(unit
);
4170 r
= sd_bus_get_property(
4172 "org.freedesktop.systemd1",
4174 "org.freedesktop.systemd1.Unit",
4180 /* systemd might have dropped off momentarily, let's
4181 * not make this an error */
4182 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
4183 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
4186 /* If the unit is already unloaded then it's not
4188 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4189 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
4192 sd_bus_error_move(ret_error
, &error
);
4196 r
= sd_bus_message_read(reply
, "s", &state
);
4200 return !STR_IN_SET(state
, "inactive", "failed");
4203 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4204 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4205 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4211 r
= sd_bus_get_property(
4213 "org.freedesktop.systemd1",
4215 "org.freedesktop.systemd1.Job",
4221 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
4222 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
4225 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4228 sd_bus_error_move(ret_error
, &error
);
4232 /* We don't actually care about the state really. The fact
4233 * that we could read the job state is enough for us */