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-get-properties.h"
16 #include "bus-locator.h"
17 #include "bus-polkit.h"
18 #include "bus-unit-util.h"
20 #include "cgroup-util.h"
21 #include "device-util.h"
22 #include "dirent-util.h"
23 #include "efi-loader.h"
28 #include "fileio-label.h"
30 #include "format-util.h"
32 #include "logind-dbus.h"
33 #include "logind-seat-dbus.h"
34 #include "logind-session-dbus.h"
35 #include "logind-user-dbus.h"
37 #include "missing_capability.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "process-util.h"
42 #include "reboot-util.h"
43 #include "selinux-util.h"
44 #include "sleep-config.h"
46 #include "stdio-util.h"
48 #include "terminal-util.h"
49 #include "tmpfile-util.h"
50 #include "unit-name.h"
51 #include "user-util.h"
52 #include "utmp-wtmp.h"
55 static int get_sender_session(
57 sd_bus_message
*message
,
62 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
63 Session
*session
= NULL
;
67 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
68 * and returns that. If not and 'consult_display' is true, this returns the display session of the
69 * owning user of the caller. */
71 r
= sd_bus_query_sender_creds(message
,
72 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
73 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
77 r
= sd_bus_creds_get_session(creds
, &name
);
82 if (consult_display
) {
85 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
92 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
94 session
= user
->display
;
98 session
= hashmap_get(m
->sessions
, name
);
101 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
103 "Caller does not belong to any known session and doesn't own any suitable session." :
104 "Caller does not belong to any known session.");
110 int manager_get_session_from_creds(
112 sd_bus_message
*message
,
122 if (SEAT_IS_SELF(name
)) /* the caller's own session */
123 return get_sender_session(m
, message
, false, error
, ret
);
124 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
125 return get_sender_session(m
, message
, true, error
, ret
);
127 session
= hashmap_get(m
->sessions
, name
);
129 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
135 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
136 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
141 /* Note that we get the owner UID of the session, not the actual client UID here! */
142 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
146 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
153 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
156 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
157 "Caller does not belong to any logged in or lingering user");
163 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
169 if (!uid_is_valid(uid
))
170 return get_sender_user(m
, message
, error
, ret
);
172 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
174 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
175 "User ID "UID_FMT
" is not logged in or lingering", uid
);
181 int manager_get_seat_from_creds(
183 sd_bus_message
*message
,
194 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
197 /* Use these special seat names as session names */
198 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
202 seat
= session
->seat
;
204 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
206 seat
= hashmap_get(m
->seats
, name
);
208 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
215 static int return_test_polkit(
216 sd_bus_message
*message
,
219 const char **details
,
227 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
234 result
= "challenge";
238 return sd_bus_reply_method_return(message
, "s", result
);
241 static int property_get_idle_hint(
244 const char *interface
,
245 const char *property
,
246 sd_bus_message
*reply
,
248 sd_bus_error
*error
) {
250 Manager
*m
= userdata
;
256 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
259 static int property_get_idle_since_hint(
262 const char *interface
,
263 const char *property
,
264 sd_bus_message
*reply
,
266 sd_bus_error
*error
) {
268 Manager
*m
= userdata
;
269 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
275 manager_get_idle_hint(m
, &t
);
277 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
280 static int property_get_inhibited(
283 const char *interface
,
284 const char *property
,
285 sd_bus_message
*reply
,
287 sd_bus_error
*error
) {
289 Manager
*m
= userdata
;
296 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
298 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
301 static int property_get_preparing(
304 const char *interface
,
305 const char *property
,
306 sd_bus_message
*reply
,
308 sd_bus_error
*error
) {
310 Manager
*m
= userdata
;
317 if (streq(property
, "PreparingForShutdown"))
318 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
320 b
= m
->action_what
& INHIBIT_SLEEP
;
322 return sd_bus_message_append(reply
, "b", b
);
325 static int property_get_scheduled_shutdown(
328 const char *interface
,
329 const char *property
,
330 sd_bus_message
*reply
,
332 sd_bus_error
*error
) {
334 Manager
*m
= userdata
;
341 r
= sd_bus_message_open_container(reply
, 'r', "st");
345 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
349 return sd_bus_message_close_container(reply
);
352 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
353 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
354 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
355 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
356 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
357 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
359 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
360 _cleanup_free_
char *p
= NULL
;
361 Manager
*m
= userdata
;
369 r
= sd_bus_message_read(message
, "s", &name
);
373 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
377 p
= session_bus_path(session
);
381 return sd_bus_reply_method_return(message
, "o", p
);
384 /* Get login session of a process. This is not what you are looking for these days,
385 * as apps may instead belong to a user service unit. This includes terminal
386 * emulators and hence command-line apps. */
387 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
388 _cleanup_free_
char *p
= NULL
;
389 Session
*session
= NULL
;
390 Manager
*m
= userdata
;
397 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
399 r
= sd_bus_message_read(message
, "u", &pid
);
406 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
410 r
= manager_get_session_by_pid(m
, pid
, &session
);
415 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
416 "PID "PID_FMT
" does not belong to any known session", pid
);
419 p
= session_bus_path(session
);
423 return sd_bus_reply_method_return(message
, "o", p
);
426 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
427 _cleanup_free_
char *p
= NULL
;
428 Manager
*m
= userdata
;
436 r
= sd_bus_message_read(message
, "u", &uid
);
440 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
444 p
= user_bus_path(user
);
448 return sd_bus_reply_method_return(message
, "o", p
);
451 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
452 _cleanup_free_
char *p
= NULL
;
453 Manager
*m
= userdata
;
461 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
463 r
= sd_bus_message_read(message
, "u", &pid
);
470 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
474 r
= manager_get_user_by_pid(m
, pid
, &user
);
478 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
479 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
483 p
= user_bus_path(user
);
487 return sd_bus_reply_method_return(message
, "o", p
);
490 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
491 _cleanup_free_
char *p
= NULL
;
492 Manager
*m
= userdata
;
500 r
= sd_bus_message_read(message
, "s", &name
);
504 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
508 p
= seat_bus_path(seat
);
512 return sd_bus_reply_method_return(message
, "o", p
);
515 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
516 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
517 Manager
*m
= userdata
;
525 r
= sd_bus_message_new_method_return(message
, &reply
);
529 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
533 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
534 _cleanup_free_
char *p
= NULL
;
536 p
= session_bus_path(session
);
540 r
= sd_bus_message_append(reply
, "(susso)",
542 (uint32_t) session
->user
->user_record
->uid
,
543 session
->user
->user_record
->user_name
,
544 session
->seat
? session
->seat
->id
: "",
550 r
= sd_bus_message_close_container(reply
);
554 return sd_bus_send(NULL
, reply
, NULL
);
557 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
558 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
559 Manager
*m
= userdata
;
567 r
= sd_bus_message_new_method_return(message
, &reply
);
571 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
575 HASHMAP_FOREACH(user
, m
->users
, i
) {
576 _cleanup_free_
char *p
= NULL
;
578 p
= user_bus_path(user
);
582 r
= sd_bus_message_append(reply
, "(uso)",
583 (uint32_t) user
->user_record
->uid
,
584 user
->user_record
->user_name
,
590 r
= sd_bus_message_close_container(reply
);
594 return sd_bus_send(NULL
, reply
, NULL
);
597 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
598 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
599 Manager
*m
= userdata
;
607 r
= sd_bus_message_new_method_return(message
, &reply
);
611 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
615 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
616 _cleanup_free_
char *p
= NULL
;
618 p
= seat_bus_path(seat
);
622 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
627 r
= sd_bus_message_close_container(reply
);
631 return sd_bus_send(NULL
, reply
, NULL
);
634 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
635 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
636 Manager
*m
= userdata
;
637 Inhibitor
*inhibitor
;
644 r
= sd_bus_message_new_method_return(message
, &reply
);
648 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
652 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
654 r
= sd_bus_message_append(reply
, "(ssssuu)",
655 strempty(inhibit_what_to_string(inhibitor
->what
)),
656 strempty(inhibitor
->who
),
657 strempty(inhibitor
->why
),
658 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
659 (uint32_t) inhibitor
->uid
,
660 (uint32_t) inhibitor
->pid
);
665 r
= sd_bus_message_close_container(reply
);
669 return sd_bus_send(NULL
, reply
, NULL
);
672 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
673 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
674 _cleanup_free_
char *id
= NULL
;
675 Session
*session
= NULL
;
676 uint32_t audit_id
= 0;
677 Manager
*m
= userdata
;
691 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
692 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
694 r
= sd_bus_message_read(message
, "uusssssussbss",
695 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
696 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
700 if (!uid_is_valid(uid
))
701 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
702 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
703 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
706 t
= _SESSION_TYPE_INVALID
;
708 t
= session_type_from_string(type
);
710 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
711 "Invalid session type %s", type
);
715 c
= _SESSION_CLASS_INVALID
;
717 c
= session_class_from_string(class);
719 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
720 "Invalid session class %s", class);
723 if (isempty(desktop
))
726 if (!string_is_safe(desktop
))
727 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
728 "Invalid desktop string %s", desktop
);
734 seat
= hashmap_get(m
->seats
, cseat
);
736 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
737 "No seat '%s' known", cseat
);
740 if (tty_is_vc(tty
)) {
745 else if (seat
!= m
->seat0
)
746 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
747 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
749 v
= vtnr_from_tty(tty
);
751 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
752 "Cannot determine VT number from virtual console TTY %s", tty
);
756 else if (vtnr
!= (uint32_t) v
)
757 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
758 "Specified TTY and VT number do not match");
760 } else if (tty_is_console(tty
)) {
764 else if (seat
!= m
->seat0
)
765 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
766 "Console TTY specified but seat is not seat0");
769 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
770 "Console TTY specified but VT number is not 0");
774 if (seat_has_vts(seat
)) {
775 if (vtnr
<= 0 || vtnr
> 63)
776 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
777 "VT number out of range");
780 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
781 "Seat has no VTs but VT number not 0");
785 if (t
== _SESSION_TYPE_INVALID
) {
786 if (!isempty(display
))
788 else if (!isempty(tty
))
791 t
= SESSION_UNSPECIFIED
;
794 if (c
== _SESSION_CLASS_INVALID
) {
795 if (t
== SESSION_UNSPECIFIED
)
796 c
= SESSION_BACKGROUND
;
802 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
804 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
808 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
813 /* Check if we are already in a logind session. Or if we are in user@.service
814 * which is a special PAM session that avoids creating a logind session. */
815 r
= manager_get_user_by_pid(m
, leader
, NULL
);
819 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
820 "Already running in a session or user slice");
823 * Old gdm and lightdm start the user-session on the same VT as
824 * the greeter session. But they destroy the greeter session
825 * after the user-session and want the user-session to take
826 * over the VT. We need to support this for
827 * backwards-compatibility, so make sure we allow new sessions
828 * on a VT that a greeter is running on. Furthermore, to allow
829 * re-logins, we have to allow a greeter to take over a used VT for
830 * the exact same reasons.
832 if (c
!= SESSION_GREETER
&&
834 vtnr
< m
->seat0
->position_count
&&
835 m
->seat0
->positions
[vtnr
] &&
836 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
837 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
839 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
840 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
841 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
844 (void) audit_session_from_pid(leader
, &audit_id
);
845 if (audit_session_is_valid(audit_id
)) {
846 /* Keep our session IDs and the audit session IDs in sync */
848 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
851 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
852 * not trust the audit data and let's better register a new ID */
853 if (hashmap_contains(m
->sessions
, id
)) {
854 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
855 audit_id
= AUDIT_SESSION_INVALID
;
864 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
867 } while (hashmap_contains(m
->sessions
, id
));
870 /* The generated names should not clash with 'auto' or 'self' */
871 assert(!SESSION_IS_SELF(id
));
872 assert(!SESSION_IS_AUTO(id
));
874 /* If we are not watching utmp already, try again */
875 manager_reconnect_utmp(m
);
877 r
= manager_add_user_by_uid(m
, uid
, &user
);
881 r
= manager_add_session(m
, id
, &session
);
885 session_set_user(session
, user
);
886 r
= session_set_leader(session
, leader
);
890 session
->original_type
= session
->type
= t
;
892 session
->remote
= remote
;
893 session
->vtnr
= vtnr
;
896 session
->tty
= strdup(tty
);
902 session
->tty_validity
= TTY_FROM_PAM
;
905 if (!isempty(display
)) {
906 session
->display
= strdup(display
);
907 if (!session
->display
) {
913 if (!isempty(remote_user
)) {
914 session
->remote_user
= strdup(remote_user
);
915 if (!session
->remote_user
) {
921 if (!isempty(remote_host
)) {
922 session
->remote_host
= strdup(remote_host
);
923 if (!session
->remote_host
) {
929 if (!isempty(service
)) {
930 session
->service
= strdup(service
);
931 if (!session
->service
) {
937 if (!isempty(desktop
)) {
938 session
->desktop
= strdup(desktop
);
939 if (!session
->desktop
) {
946 r
= seat_attach_session(seat
, session
);
951 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
955 r
= session_start(session
, message
, error
);
959 r
= sd_bus_message_exit_container(message
);
963 session
->create_message
= sd_bus_message_ref(message
);
965 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
966 * session_send_create_reply(). */
972 session_add_to_gc_queue(session
);
975 user_add_to_gc_queue(user
);
980 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
981 Manager
*m
= userdata
;
989 r
= sd_bus_message_read(message
, "s", &name
);
993 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
997 r
= session_release(session
);
1001 return sd_bus_reply_method_return(message
, NULL
);
1004 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1005 Manager
*m
= userdata
;
1013 r
= sd_bus_message_read(message
, "s", &name
);
1017 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1021 /* PolicyKit is done by bus_session_method_activate() */
1023 return bus_session_method_activate(message
, session
, error
);
1026 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1027 const char *session_name
, *seat_name
;
1028 Manager
*m
= userdata
;
1036 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1038 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1042 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1046 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1050 if (session
->seat
!= seat
)
1051 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1052 "Session %s not on seat %s", session_name
, seat_name
);
1054 r
= bus_verify_polkit_async(
1057 "org.freedesktop.login1.chvt",
1061 &m
->polkit_registry
,
1066 return 1; /* Will call us back */
1068 r
= session_activate(session
);
1072 return sd_bus_reply_method_return(message
, NULL
);
1075 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1076 Manager
*m
= userdata
;
1084 r
= sd_bus_message_read(message
, "s", &name
);
1088 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1092 return bus_session_method_lock(message
, session
, error
);
1095 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1096 Manager
*m
= userdata
;
1102 r
= bus_verify_polkit_async(
1105 "org.freedesktop.login1.lock-sessions",
1109 &m
->polkit_registry
,
1114 return 1; /* Will call us back */
1116 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1120 return sd_bus_reply_method_return(message
, NULL
);
1123 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1125 Manager
*m
= userdata
;
1132 r
= sd_bus_message_read(message
, "s", &name
);
1136 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1140 return bus_session_method_kill(message
, session
, error
);
1143 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1144 Manager
*m
= userdata
;
1152 r
= sd_bus_message_read(message
, "u", &uid
);
1156 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1160 return bus_user_method_kill(message
, user
, error
);
1163 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1164 Manager
*m
= userdata
;
1172 r
= sd_bus_message_read(message
, "s", &name
);
1176 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1180 return bus_session_method_terminate(message
, session
, error
);
1183 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1184 Manager
*m
= userdata
;
1192 r
= sd_bus_message_read(message
, "u", &uid
);
1196 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1200 return bus_user_method_terminate(message
, user
, error
);
1203 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1204 Manager
*m
= userdata
;
1212 r
= sd_bus_message_read(message
, "s", &name
);
1216 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1220 return bus_seat_method_terminate(message
, seat
, error
);
1223 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1224 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1225 _cleanup_free_
char *cc
= NULL
;
1226 Manager
*m
= userdata
;
1227 int r
, b
, interactive
;
1230 uint32_t uid
, auth_uid
;
1235 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1239 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1240 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1244 if (!uid_is_valid(uid
)) {
1245 /* Note that we get the owner UID of the session or user unit,
1246 * not the actual client UID here! */
1247 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1252 /* owner_uid is racy, so for authorization we must use euid */
1253 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1260 return errno_or_else(ENOENT
);
1262 r
= bus_verify_polkit_async(
1265 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1266 "org.freedesktop.login1.set-user-linger",
1270 &m
->polkit_registry
,
1275 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1277 (void) mkdir_p_label("/var/lib/systemd", 0755);
1278 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1282 cc
= cescape(pw
->pw_name
);
1286 path
= strjoina("/var/lib/systemd/linger/", cc
);
1294 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1301 if (r
< 0 && errno
!= ENOENT
)
1304 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1306 user_add_to_gc_queue(u
);
1309 return sd_bus_reply_method_return(message
, NULL
);
1312 static int trigger_device(Manager
*m
, sd_device
*d
) {
1313 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1318 r
= sd_device_enumerator_new(&e
);
1322 r
= sd_device_enumerator_allow_uninitialized(e
);
1327 r
= sd_device_enumerator_add_match_parent(e
, d
);
1332 FOREACH_DEVICE(e
, d
) {
1333 _cleanup_free_
char *t
= NULL
;
1336 r
= sd_device_get_syspath(d
, &p
);
1340 t
= path_join(p
, "uevent");
1344 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1350 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1351 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1352 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1353 const char *id_for_seat
;
1360 r
= sd_device_new_from_syspath(&d
, sysfs
);
1364 if (sd_device_has_tag(d
, "seat") <= 0)
1367 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1370 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1373 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1376 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1377 r
= write_string_file_atomic_label(file
, rule
);
1381 return trigger_device(m
, d
);
1384 static int flush_devices(Manager
*m
) {
1385 _cleanup_closedir_
DIR *d
;
1389 d
= opendir("/etc/udev/rules.d");
1391 if (errno
!= ENOENT
)
1392 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1396 FOREACH_DIRENT_ALL(de
, d
, break) {
1397 dirent_ensure_type(d
, de
);
1398 if (!dirent_is_file(de
))
1401 if (!startswith(de
->d_name
, "72-seat-"))
1404 if (!endswith(de
->d_name
, ".rules"))
1407 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1408 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1412 return trigger_device(m
, NULL
);
1415 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1416 const char *sysfs
, *seat
;
1417 Manager
*m
= userdata
;
1423 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1427 if (!path_is_normalized(sysfs
))
1428 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1429 if (!path_startswith(sysfs
, "/sys"))
1430 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1432 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1435 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1441 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1442 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1444 r
= bus_verify_polkit_async(
1447 "org.freedesktop.login1.attach-device",
1451 &m
->polkit_registry
,
1456 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1458 r
= attach_device(m
, seat
, sysfs
);
1462 return sd_bus_reply_method_return(message
, NULL
);
1465 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1466 Manager
*m
= userdata
;
1472 r
= sd_bus_message_read(message
, "b", &interactive
);
1476 r
= bus_verify_polkit_async(
1479 "org.freedesktop.login1.flush-devices",
1483 &m
->polkit_registry
,
1488 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1490 r
= flush_devices(m
);
1494 return sd_bus_reply_method_return(message
, NULL
);
1497 static int have_multiple_sessions(
1506 /* Check for other users' sessions. Greeter sessions do not
1507 * count, and non-login sessions do not count either. */
1508 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1509 if (session
->class == SESSION_USER
&&
1510 session
->user
->user_record
->uid
!= uid
)
1516 static int bus_manager_log_shutdown(
1518 const char *unit_name
) {
1525 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1526 p
= "MESSAGE=System is powering down";
1527 q
= "SHUTDOWN=power-off";
1528 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1529 p
= "MESSAGE=System is rebooting";
1530 q
= "SHUTDOWN=reboot";
1531 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1532 p
= "MESSAGE=System is halting";
1533 q
= "SHUTDOWN=halt";
1534 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1535 p
= "MESSAGE=System is rebooting with kexec";
1536 q
= "SHUTDOWN=kexec";
1538 p
= "MESSAGE=System is shutting down";
1542 if (isempty(m
->wall_message
))
1543 p
= strjoina(p
, ".");
1545 p
= strjoina(p
, " (", m
->wall_message
, ").");
1547 return log_struct(LOG_NOTICE
,
1548 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1553 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1554 Manager
*m
= userdata
;
1559 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1563 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1568 if (until
<= now(CLOCK_MONOTONIC
))
1571 /* We want to ignore the lid switch for a while after each
1572 * suspend, and after boot-up. Hence let's install a timer for
1573 * this. As long as the event source exists we ignore the lid
1576 if (m
->lid_switch_ignore_event_source
) {
1579 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1586 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1588 r
= sd_event_add_time(
1590 &m
->lid_switch_ignore_event_source
,
1593 lid_switch_ignore_handler
, m
);
1598 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1599 int active
= _active
;
1602 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1604 return sd_bus_emit_signal(m
->bus
,
1605 "/org/freedesktop/login1",
1606 "org.freedesktop.login1.Manager",
1607 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1612 static int execute_shutdown_or_sleep(
1615 const char *unit_name
,
1616 sd_bus_error
*error
) {
1618 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1624 assert(w
< _INHIBIT_WHAT_MAX
);
1627 if (w
== INHIBIT_SHUTDOWN
)
1628 bus_manager_log_shutdown(m
, unit_name
);
1630 r
= bus_call_method(
1636 "ss", unit_name
, "replace-irreversibly");
1640 r
= sd_bus_message_read(reply
, "o", &p
);
1644 r
= free_and_strdup(&m
->action_job
, p
);
1648 m
->action_unit
= unit_name
;
1651 /* Make sure the lid switch is ignored for a while */
1652 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1657 /* Tell people that they now may take a lock again */
1658 (void) send_prepare_for(m
, w
, false);
1663 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1665 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1666 Inhibitor
*offending
= NULL
;
1671 if (manager
->action_what
== 0 || manager
->action_job
)
1674 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1675 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1680 (void) get_process_comm(offending
->pid
, &comm
);
1681 u
= uid_to_name(offending
->uid
);
1683 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1684 offending
->uid
, strna(u
),
1685 offending
->pid
, strna(comm
));
1688 /* Actually do the operation */
1689 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1691 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1692 bus_error_message(&error
, r
));
1694 manager
->action_unit
= NULL
;
1695 manager
->action_what
= 0;
1702 static int manager_inhibit_timeout_handler(
1707 Manager
*manager
= userdata
;
1711 assert(manager
->inhibit_timeout_source
== s
);
1713 r
= manager_dispatch_delayed(manager
, true);
1714 return (r
< 0) ? r
: 0;
1717 static int delay_shutdown_or_sleep(
1720 const char *unit_name
) {
1727 assert(w
< _INHIBIT_WHAT_MAX
);
1730 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1732 if (m
->inhibit_timeout_source
) {
1733 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1735 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1737 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1739 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1741 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1742 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1747 m
->action_unit
= unit_name
;
1753 int bus_manager_shutdown_or_sleep_now_or_later(
1755 const char *unit_name
,
1757 sd_bus_error
*error
) {
1759 _cleanup_free_
char *load_state
= NULL
;
1766 assert(w
< _INHIBIT_WHAT_MAX
);
1767 assert(!m
->action_job
);
1769 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1773 if (!streq(load_state
, "loaded"))
1774 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1775 "Unit %s is %s, refusing operation.",
1776 unit_name
, load_state
);
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
);
1786 /* Shutdown is delayed, keep in mind what we
1787 * want to do, and start a timeout */
1788 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1790 /* Shutdown is not delayed, execute it
1792 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1797 static int verify_shutdown_creds(
1799 sd_bus_message
*message
,
1803 const char *action_multiple_sessions
,
1804 const char *action_ignore_inhibit
,
1805 sd_bus_error
*error
) {
1807 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1808 bool multiple_sessions
, blocked
;
1815 assert(w
<= _INHIBIT_WHAT_MAX
);
1817 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1821 r
= sd_bus_creds_get_euid(creds
, &uid
);
1825 r
= have_multiple_sessions(m
, uid
);
1829 multiple_sessions
= r
> 0;
1830 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1832 if (multiple_sessions
&& action_multiple_sessions
) {
1833 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1837 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1840 if (blocked
&& action_ignore_inhibit
) {
1841 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1845 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1848 if (!multiple_sessions
&& !blocked
&& action
) {
1849 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1853 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1859 static int method_do_shutdown_or_sleep(
1861 sd_bus_message
*message
,
1862 const char *unit_name
,
1865 const char *action_multiple_sessions
,
1866 const char *action_ignore_inhibit
,
1867 const char *sleep_verb
,
1868 sd_bus_error
*error
) {
1876 assert(w
<= _INHIBIT_WHAT_MAX
);
1878 r
= sd_bus_message_read(message
, "b", &interactive
);
1882 /* Don't allow multiple jobs being executed at the same time */
1883 if (m
->action_what
> 0)
1884 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1885 "There's already a shutdown or sleep operation in progress");
1888 r
= can_sleep(sleep_verb
);
1890 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1891 "Not enough swap space for hibernation");
1893 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1894 "Sleep verb \"%s\" not supported", sleep_verb
);
1899 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1900 action_ignore_inhibit
, error
);
1904 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1908 return sd_bus_reply_method_return(message
, NULL
);
1911 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1912 Manager
*m
= userdata
;
1914 return method_do_shutdown_or_sleep(
1916 SPECIAL_POWEROFF_TARGET
,
1918 "org.freedesktop.login1.power-off",
1919 "org.freedesktop.login1.power-off-multiple-sessions",
1920 "org.freedesktop.login1.power-off-ignore-inhibit",
1925 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1926 Manager
*m
= userdata
;
1928 return method_do_shutdown_or_sleep(
1930 SPECIAL_REBOOT_TARGET
,
1932 "org.freedesktop.login1.reboot",
1933 "org.freedesktop.login1.reboot-multiple-sessions",
1934 "org.freedesktop.login1.reboot-ignore-inhibit",
1939 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1940 Manager
*m
= userdata
;
1942 return method_do_shutdown_or_sleep(
1944 SPECIAL_HALT_TARGET
,
1946 "org.freedesktop.login1.halt",
1947 "org.freedesktop.login1.halt-multiple-sessions",
1948 "org.freedesktop.login1.halt-ignore-inhibit",
1953 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1954 Manager
*m
= userdata
;
1956 return method_do_shutdown_or_sleep(
1958 SPECIAL_SUSPEND_TARGET
,
1960 "org.freedesktop.login1.suspend",
1961 "org.freedesktop.login1.suspend-multiple-sessions",
1962 "org.freedesktop.login1.suspend-ignore-inhibit",
1967 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1968 Manager
*m
= userdata
;
1970 return method_do_shutdown_or_sleep(
1972 SPECIAL_HIBERNATE_TARGET
,
1974 "org.freedesktop.login1.hibernate",
1975 "org.freedesktop.login1.hibernate-multiple-sessions",
1976 "org.freedesktop.login1.hibernate-ignore-inhibit",
1981 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1982 Manager
*m
= userdata
;
1984 return method_do_shutdown_or_sleep(
1986 SPECIAL_HYBRID_SLEEP_TARGET
,
1988 "org.freedesktop.login1.hibernate",
1989 "org.freedesktop.login1.hibernate-multiple-sessions",
1990 "org.freedesktop.login1.hibernate-ignore-inhibit",
1995 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1996 Manager
*m
= userdata
;
1998 return method_do_shutdown_or_sleep(
2000 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
2002 "org.freedesktop.login1.hibernate",
2003 "org.freedesktop.login1.hibernate-multiple-sessions",
2004 "org.freedesktop.login1.hibernate-ignore-inhibit",
2009 static int nologin_timeout_handler(
2014 Manager
*m
= userdata
;
2016 log_info("Creating /run/nologin, blocking further logins...");
2019 create_shutdown_run_nologin_or_warn() >= 0;
2024 static int update_schedule_file(Manager
*m
) {
2025 _cleanup_free_
char *temp_path
= NULL
;
2026 _cleanup_fclose_
FILE *f
= NULL
;
2031 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
2033 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2035 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
2037 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2039 (void) fchmod(fileno(f
), 0644);
2045 m
->scheduled_shutdown_timeout
,
2046 m
->enable_wall_messages
,
2047 m
->scheduled_shutdown_type
);
2049 if (!isempty(m
->wall_message
)) {
2050 _cleanup_free_
char *t
;
2052 t
= cescape(m
->wall_message
);
2058 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
2061 r
= fflush_and_check(f
);
2065 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2073 (void) unlink(temp_path
);
2074 (void) unlink("/run/systemd/shutdown/scheduled");
2076 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2079 static void reset_scheduled_shutdown(Manager
*m
) {
2082 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2083 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2084 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2086 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
2087 m
->scheduled_shutdown_timeout
= 0;
2088 m
->shutdown_dry_run
= false;
2090 if (m
->unlink_nologin
) {
2091 (void) unlink_or_warn("/run/nologin");
2092 m
->unlink_nologin
= false;
2095 (void) unlink("/run/systemd/shutdown/scheduled");
2098 static int manager_scheduled_shutdown_handler(
2103 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2104 Manager
*m
= userdata
;
2110 if (isempty(m
->scheduled_shutdown_type
))
2113 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2114 target
= SPECIAL_POWEROFF_TARGET
;
2115 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2116 target
= SPECIAL_REBOOT_TARGET
;
2117 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2118 target
= SPECIAL_HALT_TARGET
;
2120 assert_not_reached("unexpected shutdown type");
2122 /* Don't allow multiple jobs being executed at the same time */
2123 if (m
->action_what
> 0) {
2125 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2129 if (m
->shutdown_dry_run
) {
2130 /* We do not process delay inhibitors here. Otherwise, we
2131 * would have to be considered "in progress" (like the check
2132 * above) for some seconds after our admin has seen the final
2135 bus_manager_log_shutdown(m
, target
);
2136 log_info("Running in dry run, suppressing action.");
2137 reset_scheduled_shutdown(m
);
2142 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2144 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2151 reset_scheduled_shutdown(m
);
2155 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2156 Manager
*m
= userdata
;
2157 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2158 const char *action_multiple_sessions
= NULL
;
2159 const char *action_ignore_inhibit
= NULL
;
2160 const char *action
= NULL
;
2164 bool dry_run
= false;
2169 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2173 if (startswith(type
, "dry-")) {
2178 if (streq(type
, "poweroff")) {
2179 action
= "org.freedesktop.login1.power-off";
2180 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2181 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2182 } else if (streq(type
, "reboot")) {
2183 action
= "org.freedesktop.login1.reboot";
2184 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2185 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2186 } else if (streq(type
, "halt")) {
2187 action
= "org.freedesktop.login1.halt";
2188 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2189 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2191 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2193 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2194 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2198 if (m
->scheduled_shutdown_timeout_source
) {
2199 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2201 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2203 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2205 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2207 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2208 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2210 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2213 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2215 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2219 m
->shutdown_dry_run
= dry_run
;
2221 if (m
->nologin_timeout_source
) {
2222 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2224 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2226 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2228 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2230 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2231 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2233 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2236 m
->scheduled_shutdown_timeout
= elapse
;
2238 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2240 const char *tty
= NULL
;
2242 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2243 (void) sd_bus_creds_get_tty(creds
, &tty
);
2245 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2247 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2252 r
= manager_setup_wall_message_timer(m
);
2256 r
= update_schedule_file(m
);
2260 return sd_bus_reply_method_return(message
, NULL
);
2263 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2264 Manager
*m
= userdata
;
2270 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2271 reset_scheduled_shutdown(m
);
2273 if (cancelled
&& m
->enable_wall_messages
) {
2274 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2275 _cleanup_free_
char *username
= NULL
;
2276 const char *tty
= NULL
;
2280 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2282 (void) sd_bus_creds_get_uid(creds
, &uid
);
2283 (void) sd_bus_creds_get_tty(creds
, &tty
);
2286 username
= uid_to_name(uid
);
2287 utmp_wall("The system shutdown has been cancelled",
2288 username
, tty
, logind_wall_tty_filter
, m
);
2291 return sd_bus_reply_method_return(message
, "b", cancelled
);
2294 static int method_can_shutdown_or_sleep(
2296 sd_bus_message
*message
,
2299 const char *action_multiple_sessions
,
2300 const char *action_ignore_inhibit
,
2301 const char *sleep_verb
,
2302 sd_bus_error
*error
) {
2304 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2305 HandleAction handle
;
2306 bool multiple_sessions
, challenge
, blocked
;
2307 const char *result
= NULL
;
2314 assert(w
<= _INHIBIT_WHAT_MAX
);
2316 assert(action_multiple_sessions
);
2317 assert(action_ignore_inhibit
);
2320 r
= can_sleep(sleep_verb
);
2321 if (IN_SET(r
, 0, -ENOSPC
))
2322 return sd_bus_reply_method_return(message
, "s", "na");
2327 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2331 r
= sd_bus_creds_get_euid(creds
, &uid
);
2335 r
= have_multiple_sessions(m
, uid
);
2339 multiple_sessions
= r
> 0;
2340 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2342 handle
= handle_action_from_string(sleep_verb
);
2346 target
= manager_target_for_action(handle
);
2348 _cleanup_free_
char *load_state
= NULL
;
2350 r
= unit_load_state(m
->bus
, target
, &load_state
);
2354 if (!streq(load_state
, "loaded")) {
2361 if (multiple_sessions
) {
2362 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2369 result
= "challenge";
2375 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2382 } else if (challenge
) {
2383 if (!result
|| streq(result
, "yes"))
2384 result
= "challenge";
2389 if (!multiple_sessions
&& !blocked
) {
2390 /* If neither inhibit nor multiple sessions
2391 * apply then just check the normal policy */
2393 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2400 result
= "challenge";
2406 return sd_bus_reply_method_return(message
, "s", result
);
2409 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2410 Manager
*m
= userdata
;
2412 return method_can_shutdown_or_sleep(
2415 "org.freedesktop.login1.power-off",
2416 "org.freedesktop.login1.power-off-multiple-sessions",
2417 "org.freedesktop.login1.power-off-ignore-inhibit",
2422 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2423 Manager
*m
= userdata
;
2425 return method_can_shutdown_or_sleep(
2428 "org.freedesktop.login1.reboot",
2429 "org.freedesktop.login1.reboot-multiple-sessions",
2430 "org.freedesktop.login1.reboot-ignore-inhibit",
2435 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2436 Manager
*m
= userdata
;
2438 return method_can_shutdown_or_sleep(
2441 "org.freedesktop.login1.halt",
2442 "org.freedesktop.login1.halt-multiple-sessions",
2443 "org.freedesktop.login1.halt-ignore-inhibit",
2448 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2449 Manager
*m
= userdata
;
2451 return method_can_shutdown_or_sleep(
2454 "org.freedesktop.login1.suspend",
2455 "org.freedesktop.login1.suspend-multiple-sessions",
2456 "org.freedesktop.login1.suspend-ignore-inhibit",
2461 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2462 Manager
*m
= userdata
;
2464 return method_can_shutdown_or_sleep(
2467 "org.freedesktop.login1.hibernate",
2468 "org.freedesktop.login1.hibernate-multiple-sessions",
2469 "org.freedesktop.login1.hibernate-ignore-inhibit",
2474 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2475 Manager
*m
= userdata
;
2477 return method_can_shutdown_or_sleep(
2480 "org.freedesktop.login1.hibernate",
2481 "org.freedesktop.login1.hibernate-multiple-sessions",
2482 "org.freedesktop.login1.hibernate-ignore-inhibit",
2487 static int method_can_suspend_then_hibernate(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.hibernate",
2494 "org.freedesktop.login1.hibernate-multiple-sessions",
2495 "org.freedesktop.login1.hibernate-ignore-inhibit",
2496 "suspend-then-hibernate",
2500 static int property_get_reboot_parameter(
2503 const char *interface
,
2504 const char *property
,
2505 sd_bus_message
*reply
,
2507 sd_bus_error
*error
) {
2508 _cleanup_free_
char *parameter
= NULL
;
2515 r
= read_reboot_parameter(¶meter
);
2519 return sd_bus_message_append(reply
, "s", parameter
);
2522 static int method_set_reboot_parameter(
2523 sd_bus_message
*message
,
2525 sd_bus_error
*error
) {
2527 Manager
*m
= userdata
;
2534 r
= sd_bus_message_read(message
, "s", &arg
);
2538 r
= detect_container();
2542 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2543 "Reboot parameter not supported in containers, refusing.");
2545 r
= bus_verify_polkit_async(message
,
2547 "org.freedesktop.login1.set-reboot-parameter",
2551 &m
->polkit_registry
,
2556 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2558 r
= update_reboot_parameter_and_warn(arg
, false);
2562 return sd_bus_reply_method_return(message
, NULL
);
2565 static int method_can_reboot_parameter(
2566 sd_bus_message
*message
,
2568 sd_bus_error
*error
) {
2570 _unused_ Manager
*m
= userdata
;
2576 r
= detect_container();
2579 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2580 return sd_bus_reply_method_return(message
, "s", "na");
2582 return return_test_polkit(
2585 "org.freedesktop.login1.set-reboot-parameter",
2591 static int property_get_reboot_to_firmware_setup(
2594 const char *interface
,
2595 const char *property
,
2596 sd_bus_message
*reply
,
2598 sd_bus_error
*error
) {
2605 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2607 /* EFI case: let's see what is currently configured in the EFI variables */
2608 r
= efi_get_reboot_to_firmware();
2609 if (r
< 0 && r
!= -EOPNOTSUPP
)
2610 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2612 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2614 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2615 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2616 if (errno
!= ENOENT
)
2617 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2624 return sd_bus_message_append(reply
, "b", r
> 0);
2627 static int method_set_reboot_to_firmware_setup(
2628 sd_bus_message
*message
,
2630 sd_bus_error
*error
) {
2632 Manager
*m
= userdata
;
2639 r
= sd_bus_message_read(message
, "b", &b
);
2643 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2645 /* EFI case: let's see what the firmware supports */
2647 r
= efi_reboot_to_firmware_supported();
2648 if (r
== -EOPNOTSUPP
)
2649 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2655 } else if (r
<= 0) {
2656 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2659 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2661 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2663 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2666 r
= bus_verify_polkit_async(message
,
2668 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2672 &m
->polkit_registry
,
2677 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2680 r
= efi_set_reboot_to_firmware(b
);
2685 r
= touch("/run/systemd/reboot-to-firmware-setup");
2689 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2694 return sd_bus_reply_method_return(message
, NULL
);
2697 static int method_can_reboot_to_firmware_setup(
2698 sd_bus_message
*message
,
2700 sd_bus_error
*error
) {
2702 _unused_ Manager
*m
= userdata
;
2708 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2710 /* EFI case: let's see what the firmware supports */
2712 r
= efi_reboot_to_firmware_supported();
2714 if (r
!= -EOPNOTSUPP
)
2715 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2717 return sd_bus_reply_method_return(message
, "s", "na");
2720 } else if (r
<= 0) {
2721 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2724 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2726 return sd_bus_reply_method_return(message
, "s", "na");
2729 return return_test_polkit(
2732 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2738 static int property_get_reboot_to_boot_loader_menu(
2741 const char *interface
,
2742 const char *property
,
2743 sd_bus_message
*reply
,
2745 sd_bus_error
*error
) {
2747 uint64_t x
= UINT64_MAX
;
2754 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2756 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2758 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2759 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2760 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2763 r
= efi_loader_get_config_timeout_one_shot(&x
);
2766 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
2770 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2772 _cleanup_free_
char *v
= NULL
;
2774 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2776 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2779 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2781 r
= safe_atou64(v
, &x
);
2783 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2787 return sd_bus_message_append(reply
, "t", x
);
2790 static int method_set_reboot_to_boot_loader_menu(
2791 sd_bus_message
*message
,
2793 sd_bus_error
*error
) {
2795 Manager
*m
= userdata
;
2803 r
= sd_bus_message_read(message
, "t", &x
);
2807 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2811 /* EFI case: let's see if booting into boot loader menu is supported. */
2813 r
= efi_loader_get_features(&features
);
2815 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2816 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2817 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2821 } else if (r
<= 0) {
2822 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2825 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2827 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2829 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2832 r
= bus_verify_polkit_async(message
,
2834 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2838 &m
->polkit_registry
,
2843 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2846 if (x
== UINT64_MAX
)
2847 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", NULL
, 0);
2849 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2850 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2852 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", buf
);
2857 if (x
== UINT64_MAX
) {
2858 if (unlink("/run/systemd/reboot-to-loader-menu") < 0 && errno
!= ENOENT
)
2861 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2863 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2865 r
= write_string_file_atomic_label("/run/systemd/reboot-to-loader-menu", buf
);
2871 return sd_bus_reply_method_return(message
, NULL
);
2874 static int method_can_reboot_to_boot_loader_menu(
2875 sd_bus_message
*message
,
2877 sd_bus_error
*error
) {
2879 _unused_ Manager
*m
= userdata
;
2885 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2887 uint64_t features
= 0;
2889 /* EFI case, let's see if booting into boot loader menu is supported. */
2891 r
= efi_loader_get_features(&features
);
2893 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2894 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2895 return sd_bus_reply_method_return(message
, "s", "na");
2897 } else if (r
<= 0) {
2898 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2901 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2903 return sd_bus_reply_method_return(message
, "s", "na");
2906 return return_test_polkit(
2909 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2915 static int property_get_reboot_to_boot_loader_entry(
2918 const char *interface
,
2919 const char *property
,
2920 sd_bus_message
*reply
,
2922 sd_bus_error
*error
) {
2924 _cleanup_free_
char *v
= NULL
;
2925 Manager
*m
= userdata
;
2926 const char *x
= NULL
;
2933 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2935 /* EFI case: let's read the LoaderEntryOneShot variable */
2937 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
2940 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
2942 x
= m
->efi_loader_entry_one_shot
;
2945 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2948 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2950 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2953 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
2954 } else if (!efi_loader_entry_name_valid(v
))
2955 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2960 return sd_bus_message_append(reply
, "s", x
);
2963 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
2964 _cleanup_(boot_config_free
) BootConfig config
= {};
2970 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2971 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2974 r
= manager_read_efi_boot_loader_entries(m
);
2976 (void) boot_entries_augment_from_loader(&config
, m
->efi_boot_loader_entries
, true);
2978 return boot_config_has_entry(&config
, id
);
2981 static int method_set_reboot_to_boot_loader_entry(
2982 sd_bus_message
*message
,
2984 sd_bus_error
*error
) {
2986 Manager
*m
= userdata
;
2994 r
= sd_bus_message_read(message
, "s", &v
);
3000 else if (efi_loader_entry_name_valid(v
)) {
3001 r
= boot_loader_entry_exists(m
, v
);
3005 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3007 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3009 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3013 /* EFI case: let's see if booting into boot loader entry is supported. */
3015 r
= efi_loader_get_features(&features
);
3017 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3018 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3019 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3023 } else if (r
<= 0) {
3024 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3027 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3029 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3031 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3034 r
= bus_verify_polkit_async(message
,
3036 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3040 &m
->polkit_registry
,
3045 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3050 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", NULL
, 0);
3052 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", v
);
3057 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3060 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3066 return sd_bus_reply_method_return(message
, NULL
);
3069 static int method_can_reboot_to_boot_loader_entry(
3070 sd_bus_message
*message
,
3072 sd_bus_error
*error
) {
3074 _unused_ Manager
*m
= userdata
;
3080 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3082 uint64_t features
= 0;
3084 /* EFI case, let's see if booting into boot loader entry is supported. */
3086 r
= efi_loader_get_features(&features
);
3088 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3089 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3090 return sd_bus_reply_method_return(message
, "s", "na");
3092 } else if (r
<= 0) {
3093 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3096 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3098 return sd_bus_reply_method_return(message
, "s", "na");
3101 return return_test_polkit(
3104 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3110 static int property_get_boot_loader_entries(
3113 const char *interface
,
3114 const char *property
,
3115 sd_bus_message
*reply
,
3117 sd_bus_error
*error
) {
3119 _cleanup_(boot_config_free
) BootConfig config
= {};
3120 Manager
*m
= userdata
;
3128 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3129 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3132 r
= manager_read_efi_boot_loader_entries(m
);
3134 (void) boot_entries_augment_from_loader(&config
, m
->efi_boot_loader_entries
, true);
3136 r
= sd_bus_message_open_container(reply
, 'a', "s");
3140 for (i
= 0; i
< config
.n_entries
; i
++) {
3141 BootEntry
*e
= config
.entries
+ i
;
3143 r
= sd_bus_message_append(reply
, "s", e
->id
);
3148 return sd_bus_message_close_container(reply
);
3151 static int method_set_wall_message(
3152 sd_bus_message
*message
,
3154 sd_bus_error
*error
) {
3157 Manager
*m
= userdata
;
3159 unsigned enable_wall_messages
;
3164 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3168 /* Short-circuit the operation if the desired state is already in place, to
3169 * avoid an unnecessary polkit permission check. */
3170 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3171 m
->enable_wall_messages
== enable_wall_messages
)
3174 r
= bus_verify_polkit_async(message
,
3176 "org.freedesktop.login1.set-wall-message",
3180 &m
->polkit_registry
,
3185 return 1; /* Will call us back */
3187 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3191 m
->enable_wall_messages
= enable_wall_messages
;
3194 return sd_bus_reply_method_return(message
, NULL
);
3197 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3198 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3199 const char *who
, *why
, *what
, *mode
;
3200 _cleanup_free_
char *id
= NULL
;
3201 _cleanup_close_
int fifo_fd
= -1;
3202 Manager
*m
= userdata
;
3203 Inhibitor
*i
= NULL
;
3213 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3217 w
= inhibit_what_from_string(what
);
3219 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3220 "Invalid what specification %s", what
);
3222 mm
= inhibit_mode_from_string(mode
);
3224 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3225 "Invalid mode specification %s", mode
);
3227 /* Delay is only supported for shutdown/sleep */
3228 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3229 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3230 "Delay inhibitors only supported for shutdown and sleep");
3232 /* Don't allow taking delay locks while we are already
3233 * executing the operation. We shouldn't create the impression
3234 * that the lock was successful if the machine is about to go
3235 * down/suspend any moment. */
3236 if (m
->action_what
& w
)
3237 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3238 "The operation inhibition has been requested for is already running");
3240 r
= bus_verify_polkit_async(
3243 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3244 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3245 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3246 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3247 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3248 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3249 "org.freedesktop.login1.inhibit-handle-lid-switch",
3253 &m
->polkit_registry
,
3258 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3260 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3264 r
= sd_bus_creds_get_euid(creds
, &uid
);
3268 r
= sd_bus_creds_get_pid(creds
, &pid
);
3272 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3273 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3274 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3280 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3283 } while (hashmap_get(m
->inhibitors
, id
));
3285 r
= manager_add_inhibitor(m
, id
, &i
);
3293 i
->why
= strdup(why
);
3294 i
->who
= strdup(who
);
3296 if (!i
->why
|| !i
->who
) {
3301 fifo_fd
= inhibitor_create_fifo(i
);
3307 r
= inhibitor_start(i
);
3311 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3320 static const sd_bus_vtable manager_vtable
[] = {
3321 SD_BUS_VTABLE_START(0),
3323 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3324 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3326 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3327 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3328 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3329 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3330 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3331 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3332 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3333 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3334 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3335 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3336 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3337 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3338 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3339 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3340 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3341 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3342 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3343 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3344 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3345 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3346 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3347 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3348 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3349 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3350 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3351 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3352 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3353 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3354 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3355 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3356 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3357 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3358 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3359 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3360 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3361 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3362 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3363 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3364 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3366 SD_BUS_METHOD_WITH_NAMES("GetSession",
3368 SD_BUS_PARAM(session_id
),
3370 SD_BUS_PARAM(object_path
),
3372 SD_BUS_VTABLE_UNPRIVILEGED
),
3373 SD_BUS_METHOD_WITH_NAMES("GetSessionByPID",
3377 SD_BUS_PARAM(object_path
),
3378 method_get_session_by_pid
,
3379 SD_BUS_VTABLE_UNPRIVILEGED
),
3380 SD_BUS_METHOD_WITH_NAMES("GetUser",
3384 SD_BUS_PARAM(object_path
),
3386 SD_BUS_VTABLE_UNPRIVILEGED
),
3387 SD_BUS_METHOD_WITH_NAMES("GetUserByPID",
3391 SD_BUS_PARAM(object_path
),
3392 method_get_user_by_pid
,
3393 SD_BUS_VTABLE_UNPRIVILEGED
),
3394 SD_BUS_METHOD_WITH_NAMES("GetSeat",
3396 SD_BUS_PARAM(seat_id
),
3398 SD_BUS_PARAM(object_path
),
3400 SD_BUS_VTABLE_UNPRIVILEGED
),
3401 SD_BUS_METHOD_WITH_NAMES("ListSessions",
3404 SD_BUS_PARAM(sessions
),
3405 method_list_sessions
,
3406 SD_BUS_VTABLE_UNPRIVILEGED
),
3407 SD_BUS_METHOD_WITH_NAMES("ListUsers",
3410 SD_BUS_PARAM(users
),
3412 SD_BUS_VTABLE_UNPRIVILEGED
),
3413 SD_BUS_METHOD_WITH_NAMES("ListSeats",
3416 SD_BUS_PARAM(seats
),
3418 SD_BUS_VTABLE_UNPRIVILEGED
),
3419 SD_BUS_METHOD_WITH_NAMES("ListInhibitors",
3422 SD_BUS_PARAM(inhibitors
),
3423 method_list_inhibitors
,
3424 SD_BUS_VTABLE_UNPRIVILEGED
),
3425 SD_BUS_METHOD_WITH_NAMES("CreateSession",
3426 "uusssssussbssa(sv)",
3429 SD_BUS_PARAM(service
)
3432 SD_BUS_PARAM(desktop
)
3433 SD_BUS_PARAM(seat_id
)
3436 SD_BUS_PARAM(display
)
3437 SD_BUS_PARAM(remote
)
3438 SD_BUS_PARAM(remote_user
)
3439 SD_BUS_PARAM(remote_host
)
3440 SD_BUS_PARAM(properties
),
3442 SD_BUS_PARAM(session_id
)
3443 SD_BUS_PARAM(object_path
)
3444 SD_BUS_PARAM(runtime_path
)
3445 SD_BUS_PARAM(fifo_fd
)
3447 SD_BUS_PARAM(seat_id
)
3449 SD_BUS_PARAM(existing
),
3450 method_create_session
,
3452 SD_BUS_METHOD_WITH_NAMES("ReleaseSession",
3454 SD_BUS_PARAM(session_id
),
3456 method_release_session
,
3458 SD_BUS_METHOD_WITH_NAMES("ActivateSession",
3460 SD_BUS_PARAM(session_id
),
3462 method_activate_session
,
3463 SD_BUS_VTABLE_UNPRIVILEGED
),
3464 SD_BUS_METHOD_WITH_NAMES("ActivateSessionOnSeat",
3466 SD_BUS_PARAM(session_id
)
3467 SD_BUS_PARAM(seat_id
),
3469 method_activate_session_on_seat
,
3470 SD_BUS_VTABLE_UNPRIVILEGED
),
3471 SD_BUS_METHOD_WITH_NAMES("LockSession",
3473 SD_BUS_PARAM(session_id
),
3475 method_lock_session
,
3476 SD_BUS_VTABLE_UNPRIVILEGED
),
3477 SD_BUS_METHOD_WITH_NAMES("UnlockSession",
3479 SD_BUS_PARAM(session_id
),
3481 method_lock_session
,
3482 SD_BUS_VTABLE_UNPRIVILEGED
),
3483 SD_BUS_METHOD("LockSessions",
3486 method_lock_sessions
,
3487 SD_BUS_VTABLE_UNPRIVILEGED
),
3488 SD_BUS_METHOD("UnlockSessions",
3491 method_lock_sessions
,
3492 SD_BUS_VTABLE_UNPRIVILEGED
),
3493 SD_BUS_METHOD_WITH_NAMES("KillSession",
3495 SD_BUS_PARAM(session_id
)
3497 SD_BUS_PARAM(signal_number
),
3499 method_kill_session
,
3500 SD_BUS_VTABLE_UNPRIVILEGED
),
3501 SD_BUS_METHOD_WITH_NAMES("KillUser",
3504 SD_BUS_PARAM(signal_number
),
3507 SD_BUS_VTABLE_UNPRIVILEGED
),
3508 SD_BUS_METHOD_WITH_NAMES("TerminateSession",
3510 SD_BUS_PARAM(session_id
),
3512 method_terminate_session
,
3513 SD_BUS_VTABLE_UNPRIVILEGED
),
3514 SD_BUS_METHOD_WITH_NAMES("TerminateUser",
3518 method_terminate_user
,
3519 SD_BUS_VTABLE_UNPRIVILEGED
),
3520 SD_BUS_METHOD_WITH_NAMES("TerminateSeat",
3522 SD_BUS_PARAM(seat_id
),
3524 method_terminate_seat
,
3525 SD_BUS_VTABLE_UNPRIVILEGED
),
3526 SD_BUS_METHOD_WITH_NAMES("SetUserLinger",
3529 SD_BUS_PARAM(enable
)
3530 SD_BUS_PARAM(interactive
),
3532 method_set_user_linger
,
3533 SD_BUS_VTABLE_UNPRIVILEGED
),
3534 SD_BUS_METHOD_WITH_NAMES("AttachDevice",
3536 SD_BUS_PARAM(seat_id
)
3537 SD_BUS_PARAM(sysfs_path
)
3538 SD_BUS_PARAM(interactive
),
3540 method_attach_device
,
3541 SD_BUS_VTABLE_UNPRIVILEGED
),
3542 SD_BUS_METHOD_WITH_NAMES("FlushDevices",
3544 SD_BUS_PARAM(interactive
),
3546 method_flush_devices
,
3547 SD_BUS_VTABLE_UNPRIVILEGED
),
3548 SD_BUS_METHOD_WITH_NAMES("PowerOff",
3550 SD_BUS_PARAM(interactive
),
3553 SD_BUS_VTABLE_UNPRIVILEGED
),
3554 SD_BUS_METHOD_WITH_NAMES("Reboot",
3556 SD_BUS_PARAM(interactive
),
3559 SD_BUS_VTABLE_UNPRIVILEGED
),
3560 SD_BUS_METHOD_WITH_NAMES("Halt",
3562 SD_BUS_PARAM(interactive
),
3565 SD_BUS_VTABLE_UNPRIVILEGED
),
3566 SD_BUS_METHOD_WITH_NAMES("Suspend",
3568 SD_BUS_PARAM(interactive
),
3571 SD_BUS_VTABLE_UNPRIVILEGED
),
3572 SD_BUS_METHOD_WITH_NAMES("Hibernate",
3574 SD_BUS_PARAM(interactive
),
3577 SD_BUS_VTABLE_UNPRIVILEGED
),
3578 SD_BUS_METHOD_WITH_NAMES("HybridSleep",
3580 SD_BUS_PARAM(interactive
),
3582 method_hybrid_sleep
,
3583 SD_BUS_VTABLE_UNPRIVILEGED
),
3584 SD_BUS_METHOD_WITH_NAMES("SuspendThenHibernate",
3586 SD_BUS_PARAM(interactive
),
3588 method_suspend_then_hibernate
,
3589 SD_BUS_VTABLE_UNPRIVILEGED
),
3590 SD_BUS_METHOD_WITH_NAMES("CanPowerOff",
3593 SD_BUS_PARAM(result
),
3594 method_can_poweroff
,
3595 SD_BUS_VTABLE_UNPRIVILEGED
),
3596 SD_BUS_METHOD_WITH_NAMES("CanReboot",
3599 SD_BUS_PARAM(result
),
3601 SD_BUS_VTABLE_UNPRIVILEGED
),
3602 SD_BUS_METHOD_WITH_NAMES("CanHalt",
3605 SD_BUS_PARAM(result
),
3607 SD_BUS_VTABLE_UNPRIVILEGED
),
3608 SD_BUS_METHOD_WITH_NAMES("CanSuspend",
3611 SD_BUS_PARAM(result
),
3613 SD_BUS_VTABLE_UNPRIVILEGED
),
3614 SD_BUS_METHOD_WITH_NAMES("CanHibernate",
3617 SD_BUS_PARAM(result
),
3618 method_can_hibernate
,
3619 SD_BUS_VTABLE_UNPRIVILEGED
),
3620 SD_BUS_METHOD_WITH_NAMES("CanHybridSleep",
3623 SD_BUS_PARAM(result
),
3624 method_can_hybrid_sleep
,
3625 SD_BUS_VTABLE_UNPRIVILEGED
),
3626 SD_BUS_METHOD_WITH_NAMES("CanSuspendThenHibernate",
3629 SD_BUS_PARAM(result
),
3630 method_can_suspend_then_hibernate
,
3631 SD_BUS_VTABLE_UNPRIVILEGED
),
3632 SD_BUS_METHOD_WITH_NAMES("ScheduleShutdown",
3637 method_schedule_shutdown
,
3638 SD_BUS_VTABLE_UNPRIVILEGED
),
3639 SD_BUS_METHOD_WITH_NAMES("CancelScheduledShutdown",
3642 SD_BUS_PARAM(cancelled
),
3643 method_cancel_scheduled_shutdown
,
3644 SD_BUS_VTABLE_UNPRIVILEGED
),
3645 SD_BUS_METHOD_WITH_NAMES("Inhibit",
3652 SD_BUS_PARAM(pipe_fd
),
3654 SD_BUS_VTABLE_UNPRIVILEGED
),
3655 SD_BUS_METHOD_WITH_NAMES("CanRebootParameter",
3658 SD_BUS_PARAM(result
),
3659 method_can_reboot_parameter
,
3660 SD_BUS_VTABLE_UNPRIVILEGED
),
3661 SD_BUS_METHOD_WITH_NAMES("SetRebootParameter",
3663 SD_BUS_PARAM(parameter
),
3665 method_set_reboot_parameter
,
3666 SD_BUS_VTABLE_UNPRIVILEGED
),
3667 SD_BUS_METHOD_WITH_NAMES("CanRebootToFirmwareSetup",
3670 SD_BUS_PARAM(result
),
3671 method_can_reboot_to_firmware_setup
,
3672 SD_BUS_VTABLE_UNPRIVILEGED
),
3673 SD_BUS_METHOD_WITH_NAMES("SetRebootToFirmwareSetup",
3675 SD_BUS_PARAM(enable
),
3677 method_set_reboot_to_firmware_setup
,
3678 SD_BUS_VTABLE_UNPRIVILEGED
),
3679 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderMenu",
3682 SD_BUS_PARAM(result
),
3683 method_can_reboot_to_boot_loader_menu
,
3684 SD_BUS_VTABLE_UNPRIVILEGED
),
3685 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderMenu",
3687 SD_BUS_PARAM(timeout
),
3689 method_set_reboot_to_boot_loader_menu
,
3690 SD_BUS_VTABLE_UNPRIVILEGED
),
3691 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderEntry",
3694 SD_BUS_PARAM(result
),
3695 method_can_reboot_to_boot_loader_entry
,
3696 SD_BUS_VTABLE_UNPRIVILEGED
),
3697 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderEntry",
3699 SD_BUS_PARAM(boot_loader_entry
),
3701 method_set_reboot_to_boot_loader_entry
,
3702 SD_BUS_VTABLE_UNPRIVILEGED
),
3703 SD_BUS_METHOD_WITH_NAMES("SetWallMessage",
3705 SD_BUS_PARAM(wall_message
)
3706 SD_BUS_PARAM(enable
),
3708 method_set_wall_message
,
3709 SD_BUS_VTABLE_UNPRIVILEGED
),
3711 SD_BUS_SIGNAL_WITH_NAMES("SessionNew",
3713 SD_BUS_PARAM(session_id
)
3714 SD_BUS_PARAM(object_path
),
3716 SD_BUS_SIGNAL_WITH_NAMES("SessionRemoved",
3718 SD_BUS_PARAM(session_id
)
3719 SD_BUS_PARAM(object_path
),
3721 SD_BUS_SIGNAL_WITH_NAMES("UserNew",
3724 SD_BUS_PARAM(object_path
),
3726 SD_BUS_SIGNAL_WITH_NAMES("UserRemoved",
3729 SD_BUS_PARAM(object_path
),
3731 SD_BUS_SIGNAL_WITH_NAMES("SeatNew",
3733 SD_BUS_PARAM(seat_id
)
3734 SD_BUS_PARAM(object_path
),
3736 SD_BUS_SIGNAL_WITH_NAMES("SeatRemoved",
3738 SD_BUS_PARAM(seat_id
)
3739 SD_BUS_PARAM(object_path
),
3741 SD_BUS_SIGNAL_WITH_NAMES("PrepareForShutdown",
3743 SD_BUS_PARAM(start
),
3745 SD_BUS_SIGNAL_WITH_NAMES("PrepareForSleep",
3747 SD_BUS_PARAM(start
),
3753 const BusObjectImplementation manager_object
= {
3754 "/org/freedesktop/login1",
3755 "org.freedesktop.login1.Manager",
3756 .vtables
= BUS_VTABLES(manager_vtable
),
3757 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
3762 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
3769 if (result
&& !streq(result
, "done")) {
3770 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3772 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3773 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
3774 return session_send_create_reply(s
, &e
);
3777 return session_send_create_reply(s
, NULL
);
3780 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3781 const char *path
, *result
, *unit
;
3782 Manager
*m
= userdata
;
3791 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3793 bus_log_parse_error(r
);
3797 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3798 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
3800 /* Tell people that they now may take a lock again */
3801 (void) send_prepare_for(m
, m
->action_what
, false);
3803 m
->action_job
= mfree(m
->action_job
);
3804 m
->action_unit
= NULL
;
3809 session
= hashmap_get(m
->session_units
, unit
);
3811 if (streq_ptr(path
, session
->scope_job
)) {
3812 session
->scope_job
= mfree(session
->scope_job
);
3813 (void) session_jobs_reply(session
, id
, unit
, result
);
3815 session_save(session
);
3816 user_save(session
->user
);
3819 session_add_to_gc_queue(session
);
3822 user
= hashmap_get(m
->user_units
, unit
);
3824 if (streq_ptr(path
, user
->service_job
)) {
3825 user
->service_job
= mfree(user
->service_job
);
3827 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3828 (void) session_jobs_reply(session
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
3833 user_add_to_gc_queue(user
);
3839 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3840 const char *path
, *unit
;
3841 Manager
*m
= userdata
;
3849 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3851 bus_log_parse_error(r
);
3855 session
= hashmap_get(m
->session_units
, unit
);
3857 session_add_to_gc_queue(session
);
3859 user
= hashmap_get(m
->user_units
, unit
);
3861 user_add_to_gc_queue(user
);
3866 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3867 _cleanup_free_
char *unit
= NULL
;
3868 Manager
*m
= userdata
;
3877 path
= sd_bus_message_get_path(message
);
3881 r
= unit_name_from_dbus_path(path
, &unit
);
3882 if (r
== -EINVAL
) /* not a unit */
3889 session
= hashmap_get(m
->session_units
, unit
);
3891 session_add_to_gc_queue(session
);
3893 user
= hashmap_get(m
->user_units
, unit
);
3895 user_add_to_gc_queue(user
);
3900 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3901 Manager
*m
= userdata
;
3909 r
= sd_bus_message_read(message
, "b", &b
);
3911 bus_log_parse_error(r
);
3918 /* systemd finished reloading, let's recheck all our sessions */
3919 log_debug("System manager has been reloaded, rechecking sessions...");
3921 HASHMAP_FOREACH(session
, m
->sessions
, i
)
3922 session_add_to_gc_queue(session
);
3927 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3932 l
= strv_from_stdarg_alloca(property
);
3934 return sd_bus_emit_properties_changed_strv(
3936 "/org/freedesktop/login1",
3937 "org.freedesktop.login1.Manager",
3941 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3946 r
= sd_bus_message_read(reply
, "o", &j
);
3958 int manager_start_scope(
3963 const char *description
,
3966 const char *requires_mounts_for
,
3967 sd_bus_message
*more_properties
,
3968 sd_bus_error
*error
,
3971 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3980 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
3984 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3988 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3992 if (!isempty(slice
)) {
3993 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3998 if (!isempty(description
)) {
3999 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4004 STRV_FOREACH(i
, wants
) {
4005 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
4010 STRV_FOREACH(i
, after
) {
4011 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4016 if (!empty_or_root(requires_mounts_for
)) {
4017 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4022 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4024 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4028 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
4032 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4033 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
4035 return bus_log_create_error(r
);
4037 if (more_properties
) {
4038 /* If TasksMax also appears here, it will overwrite the default value set above */
4039 r
= sd_bus_message_copy(m
, more_properties
, true);
4044 r
= sd_bus_message_close_container(m
);
4048 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4052 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4056 return strdup_job(reply
, job
);
4059 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4060 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4067 r
= bus_call_method(
4073 "ss", unit
, "replace");
4077 return strdup_job(reply
, job
);
4080 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4081 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4088 r
= bus_call_method(
4094 "ss", unit
, "fail");
4096 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4097 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
4100 sd_bus_error_free(error
);
4107 return strdup_job(reply
, job
);
4110 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4111 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4112 _cleanup_free_
char *path
= NULL
;
4118 path
= unit_dbus_path_from_name(scope
);
4122 r
= sd_bus_call_method(
4124 "org.freedesktop.systemd1",
4126 "org.freedesktop.systemd1.Scope",
4132 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4133 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
4134 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
4137 sd_bus_error_move(ret_error
, &error
);
4144 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4148 return bus_call_method(
4154 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4157 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4158 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4159 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4160 _cleanup_free_
char *path
= NULL
;
4167 path
= unit_dbus_path_from_name(unit
);
4171 r
= sd_bus_get_property(
4173 "org.freedesktop.systemd1",
4175 "org.freedesktop.systemd1.Unit",
4181 /* systemd might have dropped off momentarily, let's
4182 * not make this an error */
4183 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
4184 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
4187 /* If the unit is already unloaded then it's not
4189 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4190 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
4193 sd_bus_error_move(ret_error
, &error
);
4197 r
= sd_bus_message_read(reply
, "s", &state
);
4201 return !STR_IN_SET(state
, "inactive", "failed");
4204 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4205 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4206 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4212 r
= sd_bus_get_property(
4214 "org.freedesktop.systemd1",
4216 "org.freedesktop.systemd1.Job",
4222 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
4223 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
4226 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4229 sd_bus_error_move(ret_error
, &error
);
4233 /* We don't actually care about the state really. The fact
4234 * that we could read the job state is enough for us */