1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "sd-messages.h"
12 #include "alloc-util.h"
13 #include "audit-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-error.h"
17 #include "bus-unit-util.h"
19 #include "cgroup-util.h"
20 #include "device-util.h"
21 #include "dirent-util.h"
23 #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
) {
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
,
195 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
198 /* Use these special seat names as session names */
199 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
203 seat
= session
->seat
;
205 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
207 seat
= hashmap_get(m
->seats
, name
);
209 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
216 static int return_test_polkit(
217 sd_bus_message
*message
,
220 const char **details
,
228 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
235 result
= "challenge";
239 return sd_bus_reply_method_return(message
, "s", result
);
242 static int property_get_idle_hint(
245 const char *interface
,
246 const char *property
,
247 sd_bus_message
*reply
,
249 sd_bus_error
*error
) {
251 Manager
*m
= userdata
;
257 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
260 static int property_get_idle_since_hint(
263 const char *interface
,
264 const char *property
,
265 sd_bus_message
*reply
,
267 sd_bus_error
*error
) {
269 Manager
*m
= userdata
;
270 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
276 manager_get_idle_hint(m
, &t
);
278 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
281 static int property_get_inhibited(
284 const char *interface
,
285 const char *property
,
286 sd_bus_message
*reply
,
288 sd_bus_error
*error
) {
290 Manager
*m
= userdata
;
297 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
299 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
302 static int property_get_preparing(
305 const char *interface
,
306 const char *property
,
307 sd_bus_message
*reply
,
309 sd_bus_error
*error
) {
311 Manager
*m
= userdata
;
318 if (streq(property
, "PreparingForShutdown"))
319 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
321 b
= m
->action_what
& INHIBIT_SLEEP
;
323 return sd_bus_message_append(reply
, "b", b
);
326 static int property_get_scheduled_shutdown(
329 const char *interface
,
330 const char *property
,
331 sd_bus_message
*reply
,
333 sd_bus_error
*error
) {
335 Manager
*m
= userdata
;
342 r
= sd_bus_message_open_container(reply
, 'r', "st");
346 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
350 return sd_bus_message_close_container(reply
);
353 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
354 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
355 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
356 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
357 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
358 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
360 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
361 _cleanup_free_
char *p
= NULL
;
362 Manager
*m
= userdata
;
370 r
= sd_bus_message_read(message
, "s", &name
);
374 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
378 p
= session_bus_path(session
);
382 return sd_bus_reply_method_return(message
, "o", p
);
385 /* Get login session of a process. This is not what you are looking for these days,
386 * as apps may instead belong to a user service unit. This includes terminal
387 * emulators and hence command-line apps. */
388 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
389 _cleanup_free_
char *p
= NULL
;
390 Session
*session
= NULL
;
391 Manager
*m
= userdata
;
398 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
400 r
= sd_bus_message_read(message
, "u", &pid
);
407 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
411 r
= manager_get_session_by_pid(m
, pid
, &session
);
416 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
417 "PID "PID_FMT
" does not belong to any known session", pid
);
420 p
= session_bus_path(session
);
424 return sd_bus_reply_method_return(message
, "o", p
);
427 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
428 _cleanup_free_
char *p
= NULL
;
429 Manager
*m
= userdata
;
437 r
= sd_bus_message_read(message
, "u", &uid
);
441 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
445 p
= user_bus_path(user
);
449 return sd_bus_reply_method_return(message
, "o", p
);
452 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
453 _cleanup_free_
char *p
= NULL
;
454 Manager
*m
= userdata
;
462 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
464 r
= sd_bus_message_read(message
, "u", &pid
);
471 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
475 r
= manager_get_user_by_pid(m
, pid
, &user
);
479 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
480 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
484 p
= user_bus_path(user
);
488 return sd_bus_reply_method_return(message
, "o", p
);
491 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
492 _cleanup_free_
char *p
= NULL
;
493 Manager
*m
= userdata
;
501 r
= sd_bus_message_read(message
, "s", &name
);
505 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
509 p
= seat_bus_path(seat
);
513 return sd_bus_reply_method_return(message
, "o", p
);
516 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
517 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
518 Manager
*m
= userdata
;
526 r
= sd_bus_message_new_method_return(message
, &reply
);
530 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
534 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
535 _cleanup_free_
char *p
= NULL
;
537 p
= session_bus_path(session
);
541 r
= sd_bus_message_append(reply
, "(susso)",
543 (uint32_t) session
->user
->uid
,
545 session
->seat
? session
->seat
->id
: "",
551 r
= sd_bus_message_close_container(reply
);
555 return sd_bus_send(NULL
, reply
, NULL
);
558 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
559 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
560 Manager
*m
= userdata
;
568 r
= sd_bus_message_new_method_return(message
, &reply
);
572 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
576 HASHMAP_FOREACH(user
, m
->users
, i
) {
577 _cleanup_free_
char *p
= NULL
;
579 p
= user_bus_path(user
);
583 r
= sd_bus_message_append(reply
, "(uso)",
584 (uint32_t) user
->uid
,
591 r
= sd_bus_message_close_container(reply
);
595 return sd_bus_send(NULL
, reply
, NULL
);
598 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
599 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
600 Manager
*m
= userdata
;
608 r
= sd_bus_message_new_method_return(message
, &reply
);
612 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
616 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
617 _cleanup_free_
char *p
= NULL
;
619 p
= seat_bus_path(seat
);
623 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
628 r
= sd_bus_message_close_container(reply
);
632 return sd_bus_send(NULL
, reply
, NULL
);
635 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
636 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
637 Manager
*m
= userdata
;
638 Inhibitor
*inhibitor
;
645 r
= sd_bus_message_new_method_return(message
, &reply
);
649 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
653 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
655 r
= sd_bus_message_append(reply
, "(ssssuu)",
656 strempty(inhibit_what_to_string(inhibitor
->what
)),
657 strempty(inhibitor
->who
),
658 strempty(inhibitor
->why
),
659 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
660 (uint32_t) inhibitor
->uid
,
661 (uint32_t) inhibitor
->pid
);
666 r
= sd_bus_message_close_container(reply
);
670 return sd_bus_send(NULL
, reply
, NULL
);
673 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
674 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
675 _cleanup_free_
char *id
= NULL
;
676 Session
*session
= NULL
;
677 uint32_t audit_id
= 0;
678 Manager
*m
= userdata
;
692 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
693 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
695 r
= sd_bus_message_read(message
, "uusssssussbss",
696 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
697 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
701 if (!uid_is_valid(uid
))
702 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
703 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
704 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
707 t
= _SESSION_TYPE_INVALID
;
709 t
= session_type_from_string(type
);
711 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
712 "Invalid session type %s", type
);
716 c
= _SESSION_CLASS_INVALID
;
718 c
= session_class_from_string(class);
720 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
721 "Invalid session class %s", class);
724 if (isempty(desktop
))
727 if (!string_is_safe(desktop
))
728 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
729 "Invalid desktop string %s", desktop
);
735 seat
= hashmap_get(m
->seats
, cseat
);
737 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
738 "No seat '%s' known", cseat
);
741 if (tty_is_vc(tty
)) {
746 else if (seat
!= m
->seat0
)
747 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
748 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
750 v
= vtnr_from_tty(tty
);
752 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
753 "Cannot determine VT number from virtual console TTY %s", tty
);
757 else if (vtnr
!= (uint32_t) v
)
758 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
759 "Specified TTY and VT number do not match");
761 } else if (tty_is_console(tty
)) {
765 else if (seat
!= m
->seat0
)
766 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
767 "Console TTY specified but seat is not seat0");
770 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
771 "Console TTY specified but VT number is not 0");
775 if (seat_has_vts(seat
)) {
776 if (vtnr
<= 0 || vtnr
> 63)
777 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
778 "VT number out of range");
781 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
782 "Seat has no VTs but VT number not 0");
786 if (t
== _SESSION_TYPE_INVALID
) {
787 if (!isempty(display
))
789 else if (!isempty(tty
))
792 t
= SESSION_UNSPECIFIED
;
795 if (c
== _SESSION_CLASS_INVALID
) {
796 if (t
== SESSION_UNSPECIFIED
)
797 c
= SESSION_BACKGROUND
;
803 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
805 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
809 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
814 /* Check if we are already in a logind session. Or if we are in user@.service
815 * which is a special PAM session that avoids creating a logind session. */
816 r
= manager_get_user_by_pid(m
, leader
, NULL
);
820 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
821 "Already running in a session or user slice");
824 * Old gdm and lightdm start the user-session on the same VT as
825 * the greeter session. But they destroy the greeter session
826 * after the user-session and want the user-session to take
827 * over the VT. We need to support this for
828 * backwards-compatibility, so make sure we allow new sessions
829 * on a VT that a greeter is running on. Furthermore, to allow
830 * re-logins, we have to allow a greeter to take over a used VT for
831 * the exact same reasons.
833 if (c
!= SESSION_GREETER
&&
835 vtnr
< m
->seat0
->position_count
&&
836 m
->seat0
->positions
[vtnr
] &&
837 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
838 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
840 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
841 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
842 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
845 (void) audit_session_from_pid(leader
, &audit_id
);
846 if (audit_session_is_valid(audit_id
)) {
847 /* Keep our session IDs and the audit session IDs in sync */
849 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
852 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
853 * not trust the audit data and let's better register a new ID */
854 if (hashmap_contains(m
->sessions
, id
)) {
855 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
856 audit_id
= AUDIT_SESSION_INVALID
;
865 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
868 } while (hashmap_contains(m
->sessions
, id
));
871 /* The generated names should not clash with 'auto' or 'self' */
872 assert(!SESSION_IS_SELF(id
));
873 assert(!SESSION_IS_AUTO(id
));
875 /* If we are not watching utmp already, try again */
876 manager_reconnect_utmp(m
);
878 r
= manager_add_user_by_uid(m
, uid
, &user
);
882 r
= manager_add_session(m
, id
, &session
);
886 session_set_user(session
, user
);
887 r
= session_set_leader(session
, leader
);
893 session
->remote
= remote
;
894 session
->vtnr
= vtnr
;
897 session
->tty
= strdup(tty
);
903 session
->tty_validity
= TTY_FROM_PAM
;
906 if (!isempty(display
)) {
907 session
->display
= strdup(display
);
908 if (!session
->display
) {
914 if (!isempty(remote_user
)) {
915 session
->remote_user
= strdup(remote_user
);
916 if (!session
->remote_user
) {
922 if (!isempty(remote_host
)) {
923 session
->remote_host
= strdup(remote_host
);
924 if (!session
->remote_host
) {
930 if (!isempty(service
)) {
931 session
->service
= strdup(service
);
932 if (!session
->service
) {
938 if (!isempty(desktop
)) {
939 session
->desktop
= strdup(desktop
);
940 if (!session
->desktop
) {
947 r
= seat_attach_session(seat
, session
);
952 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
956 r
= session_start(session
, message
, error
);
960 r
= sd_bus_message_exit_container(message
);
964 session
->create_message
= sd_bus_message_ref(message
);
966 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
967 * session_send_create_reply(). */
973 session_add_to_gc_queue(session
);
976 user_add_to_gc_queue(user
);
981 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
982 Manager
*m
= userdata
;
990 r
= sd_bus_message_read(message
, "s", &name
);
994 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
998 r
= session_release(session
);
1002 return sd_bus_reply_method_return(message
, NULL
);
1005 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1006 Manager
*m
= userdata
;
1014 r
= sd_bus_message_read(message
, "s", &name
);
1018 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
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
= session_activate(session
);
1057 return sd_bus_reply_method_return(message
, NULL
);
1060 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1061 Manager
*m
= userdata
;
1069 r
= sd_bus_message_read(message
, "s", &name
);
1073 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1077 return bus_session_method_lock(message
, session
, error
);
1080 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1081 Manager
*m
= userdata
;
1087 r
= bus_verify_polkit_async(
1090 "org.freedesktop.login1.lock-sessions",
1094 &m
->polkit_registry
,
1099 return 1; /* Will call us back */
1101 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1105 return sd_bus_reply_method_return(message
, NULL
);
1108 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1110 Manager
*m
= userdata
;
1117 r
= sd_bus_message_read(message
, "s", &name
);
1121 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1125 return bus_session_method_kill(message
, session
, error
);
1128 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1129 Manager
*m
= userdata
;
1137 r
= sd_bus_message_read(message
, "u", &uid
);
1141 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1145 return bus_user_method_kill(message
, user
, error
);
1148 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1149 Manager
*m
= userdata
;
1157 r
= sd_bus_message_read(message
, "s", &name
);
1161 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1165 return bus_session_method_terminate(message
, session
, error
);
1168 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1169 Manager
*m
= userdata
;
1177 r
= sd_bus_message_read(message
, "u", &uid
);
1181 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1185 return bus_user_method_terminate(message
, user
, error
);
1188 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1189 Manager
*m
= userdata
;
1197 r
= sd_bus_message_read(message
, "s", &name
);
1201 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1205 return bus_seat_method_terminate(message
, seat
, error
);
1208 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1209 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1210 _cleanup_free_
char *cc
= NULL
;
1211 Manager
*m
= userdata
;
1212 int r
, b
, interactive
;
1215 uint32_t uid
, auth_uid
;
1220 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1224 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1225 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1229 if (!uid_is_valid(uid
)) {
1230 /* Note that we get the owner UID of the session or user unit,
1231 * not the actual client UID here! */
1232 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1237 /* owner_uid is racy, so for authorization we must use euid */
1238 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1245 return errno_or_else(ENOENT
);
1247 r
= bus_verify_polkit_async(
1250 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1251 "org.freedesktop.login1.set-user-linger",
1255 &m
->polkit_registry
,
1260 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1262 (void) mkdir_p_label("/var/lib/systemd", 0755);
1263 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1267 cc
= cescape(pw
->pw_name
);
1271 path
= strjoina("/var/lib/systemd/linger/", cc
);
1279 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1286 if (r
< 0 && errno
!= ENOENT
)
1289 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1291 user_add_to_gc_queue(u
);
1294 return sd_bus_reply_method_return(message
, NULL
);
1297 static int trigger_device(Manager
*m
, sd_device
*d
) {
1298 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1303 r
= sd_device_enumerator_new(&e
);
1307 r
= sd_device_enumerator_allow_uninitialized(e
);
1312 r
= sd_device_enumerator_add_match_parent(e
, d
);
1317 FOREACH_DEVICE(e
, d
) {
1318 _cleanup_free_
char *t
= NULL
;
1321 r
= sd_device_get_syspath(d
, &p
);
1325 t
= path_join(p
, "uevent");
1329 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1335 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1336 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1337 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1338 const char *id_for_seat
;
1345 r
= sd_device_new_from_syspath(&d
, sysfs
);
1349 if (sd_device_has_tag(d
, "seat") <= 0)
1352 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1355 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1358 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1361 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1362 r
= write_string_file_atomic_label(file
, rule
);
1366 return trigger_device(m
, d
);
1369 static int flush_devices(Manager
*m
) {
1370 _cleanup_closedir_
DIR *d
;
1374 d
= opendir("/etc/udev/rules.d");
1376 if (errno
!= ENOENT
)
1377 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1381 FOREACH_DIRENT_ALL(de
, d
, break) {
1382 if (!dirent_is_file(de
))
1385 if (!startswith(de
->d_name
, "72-seat-"))
1388 if (!endswith(de
->d_name
, ".rules"))
1391 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1392 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1396 return trigger_device(m
, NULL
);
1399 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1400 const char *sysfs
, *seat
;
1401 Manager
*m
= userdata
;
1407 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1411 if (!path_is_normalized(sysfs
))
1412 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1413 if (!path_startswith(sysfs
, "/sys"))
1414 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1416 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1419 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1425 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1426 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1428 r
= bus_verify_polkit_async(
1431 "org.freedesktop.login1.attach-device",
1435 &m
->polkit_registry
,
1440 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1442 r
= attach_device(m
, seat
, sysfs
);
1446 return sd_bus_reply_method_return(message
, NULL
);
1449 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1450 Manager
*m
= userdata
;
1456 r
= sd_bus_message_read(message
, "b", &interactive
);
1460 r
= bus_verify_polkit_async(
1463 "org.freedesktop.login1.flush-devices",
1467 &m
->polkit_registry
,
1472 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1474 r
= flush_devices(m
);
1478 return sd_bus_reply_method_return(message
, NULL
);
1481 static int have_multiple_sessions(
1490 /* Check for other users' sessions. Greeter sessions do not
1491 * count, and non-login sessions do not count either. */
1492 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1493 if (session
->class == SESSION_USER
&&
1494 session
->user
->uid
!= uid
)
1500 static int bus_manager_log_shutdown(
1502 const char *unit_name
) {
1509 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1510 p
= "MESSAGE=System is powering down";
1511 q
= "SHUTDOWN=power-off";
1512 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1513 p
= "MESSAGE=System is rebooting";
1514 q
= "SHUTDOWN=reboot";
1515 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1516 p
= "MESSAGE=System is halting";
1517 q
= "SHUTDOWN=halt";
1518 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1519 p
= "MESSAGE=System is rebooting with kexec";
1520 q
= "SHUTDOWN=kexec";
1522 p
= "MESSAGE=System is shutting down";
1526 if (isempty(m
->wall_message
))
1527 p
= strjoina(p
, ".");
1529 p
= strjoina(p
, " (", m
->wall_message
, ").");
1531 return log_struct(LOG_NOTICE
,
1532 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1537 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1538 Manager
*m
= userdata
;
1543 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1547 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1552 if (until
<= now(CLOCK_MONOTONIC
))
1555 /* We want to ignore the lid switch for a while after each
1556 * suspend, and after boot-up. Hence let's install a timer for
1557 * this. As long as the event source exists we ignore the lid
1560 if (m
->lid_switch_ignore_event_source
) {
1563 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1570 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1572 r
= sd_event_add_time(
1574 &m
->lid_switch_ignore_event_source
,
1577 lid_switch_ignore_handler
, m
);
1582 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1583 int active
= _active
;
1586 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1588 return sd_bus_emit_signal(m
->bus
,
1589 "/org/freedesktop/login1",
1590 "org.freedesktop.login1.Manager",
1591 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1596 static int execute_shutdown_or_sleep(
1599 const char *unit_name
,
1600 sd_bus_error
*error
) {
1602 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1608 assert(w
< _INHIBIT_WHAT_MAX
);
1611 if (w
== INHIBIT_SHUTDOWN
)
1612 bus_manager_log_shutdown(m
, unit_name
);
1614 r
= sd_bus_call_method(
1616 "org.freedesktop.systemd1",
1617 "/org/freedesktop/systemd1",
1618 "org.freedesktop.systemd1.Manager",
1622 "ss", unit_name
, "replace-irreversibly");
1626 r
= sd_bus_message_read(reply
, "o", &p
);
1630 r
= free_and_strdup(&m
->action_job
, p
);
1634 m
->action_unit
= unit_name
;
1637 /* Make sure the lid switch is ignored for a while */
1638 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1643 /* Tell people that they now may take a lock again */
1644 (void) send_prepare_for(m
, w
, false);
1649 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1651 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1652 Inhibitor
*offending
= NULL
;
1657 if (manager
->action_what
== 0 || manager
->action_job
)
1660 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1661 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1666 (void) get_process_comm(offending
->pid
, &comm
);
1667 u
= uid_to_name(offending
->uid
);
1669 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1670 offending
->uid
, strna(u
),
1671 offending
->pid
, strna(comm
));
1674 /* Actually do the operation */
1675 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1677 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1678 bus_error_message(&error
, r
));
1680 manager
->action_unit
= NULL
;
1681 manager
->action_what
= 0;
1688 static int manager_inhibit_timeout_handler(
1693 Manager
*manager
= userdata
;
1697 assert(manager
->inhibit_timeout_source
== s
);
1699 r
= manager_dispatch_delayed(manager
, true);
1700 return (r
< 0) ? r
: 0;
1703 static int delay_shutdown_or_sleep(
1706 const char *unit_name
) {
1713 assert(w
< _INHIBIT_WHAT_MAX
);
1716 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1718 if (m
->inhibit_timeout_source
) {
1719 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1721 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1723 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1725 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1727 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1728 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1733 m
->action_unit
= unit_name
;
1739 int bus_manager_shutdown_or_sleep_now_or_later(
1741 const char *unit_name
,
1743 sd_bus_error
*error
) {
1745 _cleanup_free_
char *load_state
= NULL
;
1752 assert(w
< _INHIBIT_WHAT_MAX
);
1753 assert(!m
->action_job
);
1755 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1759 if (!streq(load_state
, "loaded"))
1760 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1761 "Unit %s is %s, refusing operation.",
1762 unit_name
, load_state
);
1764 /* Tell everybody to prepare for shutdown/sleep */
1765 (void) send_prepare_for(m
, w
, true);
1768 m
->inhibit_delay_max
> 0 &&
1769 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1772 /* Shutdown is delayed, keep in mind what we
1773 * want to do, and start a timeout */
1774 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1776 /* Shutdown is not delayed, execute it
1778 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1783 static int verify_shutdown_creds(
1785 sd_bus_message
*message
,
1789 const char *action_multiple_sessions
,
1790 const char *action_ignore_inhibit
,
1791 sd_bus_error
*error
) {
1793 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1794 bool multiple_sessions
, blocked
;
1801 assert(w
<= _INHIBIT_WHAT_MAX
);
1803 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1807 r
= sd_bus_creds_get_euid(creds
, &uid
);
1811 r
= have_multiple_sessions(m
, uid
);
1815 multiple_sessions
= r
> 0;
1816 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1818 if (multiple_sessions
&& action_multiple_sessions
) {
1819 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1823 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1826 if (blocked
&& action_ignore_inhibit
) {
1827 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1831 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1834 if (!multiple_sessions
&& !blocked
&& action
) {
1835 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1839 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1845 static int method_do_shutdown_or_sleep(
1847 sd_bus_message
*message
,
1848 const char *unit_name
,
1851 const char *action_multiple_sessions
,
1852 const char *action_ignore_inhibit
,
1853 const char *sleep_verb
,
1854 sd_bus_error
*error
) {
1862 assert(w
<= _INHIBIT_WHAT_MAX
);
1864 r
= sd_bus_message_read(message
, "b", &interactive
);
1868 /* Don't allow multiple jobs being executed at the same time */
1869 if (m
->action_what
> 0)
1870 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1871 "There's already a shutdown or sleep operation in progress");
1874 r
= can_sleep(sleep_verb
);
1876 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1877 "Not enough swap space for hibernation");
1879 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1880 "Sleep verb \"%s\" not supported", sleep_verb
);
1885 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1886 action_ignore_inhibit
, error
);
1890 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1894 return sd_bus_reply_method_return(message
, NULL
);
1897 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1898 Manager
*m
= userdata
;
1900 return method_do_shutdown_or_sleep(
1902 SPECIAL_POWEROFF_TARGET
,
1904 "org.freedesktop.login1.power-off",
1905 "org.freedesktop.login1.power-off-multiple-sessions",
1906 "org.freedesktop.login1.power-off-ignore-inhibit",
1911 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1912 Manager
*m
= userdata
;
1914 return method_do_shutdown_or_sleep(
1916 SPECIAL_REBOOT_TARGET
,
1918 "org.freedesktop.login1.reboot",
1919 "org.freedesktop.login1.reboot-multiple-sessions",
1920 "org.freedesktop.login1.reboot-ignore-inhibit",
1925 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1926 Manager
*m
= userdata
;
1928 return method_do_shutdown_or_sleep(
1930 SPECIAL_HALT_TARGET
,
1932 "org.freedesktop.login1.halt",
1933 "org.freedesktop.login1.halt-multiple-sessions",
1934 "org.freedesktop.login1.halt-ignore-inhibit",
1939 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1940 Manager
*m
= userdata
;
1942 return method_do_shutdown_or_sleep(
1944 SPECIAL_SUSPEND_TARGET
,
1946 "org.freedesktop.login1.suspend",
1947 "org.freedesktop.login1.suspend-multiple-sessions",
1948 "org.freedesktop.login1.suspend-ignore-inhibit",
1953 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1954 Manager
*m
= userdata
;
1956 return method_do_shutdown_or_sleep(
1958 SPECIAL_HIBERNATE_TARGET
,
1960 "org.freedesktop.login1.hibernate",
1961 "org.freedesktop.login1.hibernate-multiple-sessions",
1962 "org.freedesktop.login1.hibernate-ignore-inhibit",
1967 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1968 Manager
*m
= userdata
;
1970 return method_do_shutdown_or_sleep(
1972 SPECIAL_HYBRID_SLEEP_TARGET
,
1974 "org.freedesktop.login1.hibernate",
1975 "org.freedesktop.login1.hibernate-multiple-sessions",
1976 "org.freedesktop.login1.hibernate-ignore-inhibit",
1981 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1982 Manager
*m
= userdata
;
1984 return method_do_shutdown_or_sleep(
1986 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
1988 "org.freedesktop.login1.hibernate",
1989 "org.freedesktop.login1.hibernate-multiple-sessions",
1990 "org.freedesktop.login1.hibernate-ignore-inhibit",
1995 static int nologin_timeout_handler(
2000 Manager
*m
= userdata
;
2002 log_info("Creating /run/nologin, blocking further logins...");
2005 create_shutdown_run_nologin_or_warn() >= 0;
2010 static int update_schedule_file(Manager
*m
) {
2011 _cleanup_free_
char *temp_path
= NULL
;
2012 _cleanup_fclose_
FILE *f
= NULL
;
2017 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
2019 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2021 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
2023 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2025 (void) fchmod(fileno(f
), 0644);
2031 m
->scheduled_shutdown_timeout
,
2032 m
->enable_wall_messages
,
2033 m
->scheduled_shutdown_type
);
2035 if (!isempty(m
->wall_message
)) {
2036 _cleanup_free_
char *t
;
2038 t
= cescape(m
->wall_message
);
2044 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
2047 r
= fflush_and_check(f
);
2051 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2059 (void) unlink(temp_path
);
2060 (void) unlink("/run/systemd/shutdown/scheduled");
2062 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2065 static void reset_scheduled_shutdown(Manager
*m
) {
2068 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2069 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2070 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2072 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
2073 m
->scheduled_shutdown_timeout
= 0;
2074 m
->shutdown_dry_run
= false;
2076 if (m
->unlink_nologin
) {
2077 (void) unlink_or_warn("/run/nologin");
2078 m
->unlink_nologin
= false;
2081 (void) unlink("/run/systemd/shutdown/scheduled");
2084 static int manager_scheduled_shutdown_handler(
2089 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2090 Manager
*m
= userdata
;
2096 if (isempty(m
->scheduled_shutdown_type
))
2099 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2100 target
= SPECIAL_POWEROFF_TARGET
;
2101 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2102 target
= SPECIAL_REBOOT_TARGET
;
2103 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2104 target
= SPECIAL_HALT_TARGET
;
2106 assert_not_reached("unexpected shutdown type");
2108 /* Don't allow multiple jobs being executed at the same time */
2109 if (m
->action_what
> 0) {
2111 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2115 if (m
->shutdown_dry_run
) {
2116 /* We do not process delay inhibitors here. Otherwise, we
2117 * would have to be considered "in progress" (like the check
2118 * above) for some seconds after our admin has seen the final
2121 bus_manager_log_shutdown(m
, target
);
2122 log_info("Running in dry run, suppressing action.");
2123 reset_scheduled_shutdown(m
);
2128 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2130 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2137 reset_scheduled_shutdown(m
);
2141 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2142 Manager
*m
= userdata
;
2143 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2144 const char *action_multiple_sessions
= NULL
;
2145 const char *action_ignore_inhibit
= NULL
;
2146 const char *action
= NULL
;
2150 bool dry_run
= false;
2155 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2159 if (startswith(type
, "dry-")) {
2164 if (streq(type
, "poweroff")) {
2165 action
= "org.freedesktop.login1.power-off";
2166 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2167 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2168 } else if (streq(type
, "reboot")) {
2169 action
= "org.freedesktop.login1.reboot";
2170 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2171 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2172 } else if (streq(type
, "halt")) {
2173 action
= "org.freedesktop.login1.halt";
2174 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2175 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2177 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2179 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2180 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2184 if (m
->scheduled_shutdown_timeout_source
) {
2185 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2187 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2189 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2191 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2193 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2194 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2196 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2199 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2201 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2205 m
->shutdown_dry_run
= dry_run
;
2207 if (m
->nologin_timeout_source
) {
2208 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2210 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2212 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2214 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2216 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2217 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2219 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2222 m
->scheduled_shutdown_timeout
= elapse
;
2224 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2226 const char *tty
= NULL
;
2228 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2229 (void) sd_bus_creds_get_tty(creds
, &tty
);
2231 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2233 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2238 r
= manager_setup_wall_message_timer(m
);
2242 r
= update_schedule_file(m
);
2246 return sd_bus_reply_method_return(message
, NULL
);
2249 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2250 Manager
*m
= userdata
;
2256 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2257 reset_scheduled_shutdown(m
);
2259 if (cancelled
&& m
->enable_wall_messages
) {
2260 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2261 _cleanup_free_
char *username
= NULL
;
2262 const char *tty
= NULL
;
2266 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2268 (void) sd_bus_creds_get_uid(creds
, &uid
);
2269 (void) sd_bus_creds_get_tty(creds
, &tty
);
2272 username
= uid_to_name(uid
);
2273 utmp_wall("The system shutdown has been cancelled",
2274 username
, tty
, logind_wall_tty_filter
, m
);
2277 return sd_bus_reply_method_return(message
, "b", cancelled
);
2280 static int method_can_shutdown_or_sleep(
2282 sd_bus_message
*message
,
2285 const char *action_multiple_sessions
,
2286 const char *action_ignore_inhibit
,
2287 const char *sleep_verb
,
2288 sd_bus_error
*error
) {
2290 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2291 HandleAction handle
;
2292 bool multiple_sessions
, challenge
, blocked
;
2293 const char *result
= NULL
;
2300 assert(w
<= _INHIBIT_WHAT_MAX
);
2302 assert(action_multiple_sessions
);
2303 assert(action_ignore_inhibit
);
2306 r
= can_sleep(sleep_verb
);
2307 if (IN_SET(r
, 0, -ENOSPC
))
2308 return sd_bus_reply_method_return(message
, "s", "na");
2313 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2317 r
= sd_bus_creds_get_euid(creds
, &uid
);
2321 r
= have_multiple_sessions(m
, uid
);
2325 multiple_sessions
= r
> 0;
2326 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2328 handle
= handle_action_from_string(sleep_verb
);
2332 target
= manager_target_for_action(handle
);
2334 _cleanup_free_
char *load_state
= NULL
;
2336 r
= unit_load_state(m
->bus
, target
, &load_state
);
2340 if (!streq(load_state
, "loaded")) {
2347 if (multiple_sessions
) {
2348 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2355 result
= "challenge";
2361 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2368 } else if (challenge
) {
2369 if (!result
|| streq(result
, "yes"))
2370 result
= "challenge";
2375 if (!multiple_sessions
&& !blocked
) {
2376 /* If neither inhibit nor multiple sessions
2377 * apply then just check the normal policy */
2379 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2386 result
= "challenge";
2392 return sd_bus_reply_method_return(message
, "s", result
);
2395 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2396 Manager
*m
= userdata
;
2398 return method_can_shutdown_or_sleep(
2401 "org.freedesktop.login1.power-off",
2402 "org.freedesktop.login1.power-off-multiple-sessions",
2403 "org.freedesktop.login1.power-off-ignore-inhibit",
2408 static int method_can_reboot(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.reboot",
2415 "org.freedesktop.login1.reboot-multiple-sessions",
2416 "org.freedesktop.login1.reboot-ignore-inhibit",
2421 static int method_can_halt(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.halt",
2428 "org.freedesktop.login1.halt-multiple-sessions",
2429 "org.freedesktop.login1.halt-ignore-inhibit",
2434 static int method_can_suspend(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.suspend",
2441 "org.freedesktop.login1.suspend-multiple-sessions",
2442 "org.freedesktop.login1.suspend-ignore-inhibit",
2447 static int method_can_hibernate(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.hibernate",
2454 "org.freedesktop.login1.hibernate-multiple-sessions",
2455 "org.freedesktop.login1.hibernate-ignore-inhibit",
2460 static int method_can_hybrid_sleep(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_suspend_then_hibernate(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",
2482 "suspend-then-hibernate",
2486 static int property_get_reboot_parameter(
2489 const char *interface
,
2490 const char *property
,
2491 sd_bus_message
*reply
,
2493 sd_bus_error
*error
) {
2494 _cleanup_free_
char *parameter
= NULL
;
2501 r
= read_reboot_parameter(¶meter
);
2505 return sd_bus_message_append(reply
, "s", parameter
);
2508 static int method_set_reboot_parameter(
2509 sd_bus_message
*message
,
2511 sd_bus_error
*error
) {
2513 Manager
*m
= userdata
;
2520 r
= sd_bus_message_read(message
, "s", &arg
);
2524 r
= detect_container();
2528 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2529 "Reboot parameter not supported in containers, refusing.");
2531 r
= bus_verify_polkit_async(message
,
2533 "org.freedesktop.login1.set-reboot-parameter",
2537 &m
->polkit_registry
,
2542 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2544 r
= update_reboot_parameter_and_warn(arg
, false);
2548 return sd_bus_reply_method_return(message
, NULL
);
2551 static int method_can_reboot_parameter(
2552 sd_bus_message
*message
,
2554 sd_bus_error
*error
) {
2556 Manager
*m
= userdata
;
2562 r
= detect_container();
2565 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2566 return sd_bus_reply_method_return(message
, "s", "na");
2568 return return_test_polkit(
2571 "org.freedesktop.login1.set-reboot-parameter",
2577 static int property_get_reboot_to_firmware_setup(
2580 const char *interface
,
2581 const char *property
,
2582 sd_bus_message
*reply
,
2584 sd_bus_error
*error
) {
2591 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2593 /* EFI case: let's see what is currently configured in the EFI variables */
2594 r
= efi_get_reboot_to_firmware();
2595 if (r
< 0 && r
!= -EOPNOTSUPP
)
2596 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2598 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2600 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2601 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2602 if (errno
!= ENOENT
)
2603 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2610 return sd_bus_message_append(reply
, "b", r
> 0);
2613 static int method_set_reboot_to_firmware_setup(
2614 sd_bus_message
*message
,
2616 sd_bus_error
*error
) {
2618 Manager
*m
= userdata
;
2625 r
= sd_bus_message_read(message
, "b", &b
);
2629 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2631 /* EFI case: let's see what the firmware supports */
2633 r
= efi_reboot_to_firmware_supported();
2634 if (r
== -EOPNOTSUPP
)
2635 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2641 } else if (r
<= 0) {
2642 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2645 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2647 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2649 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2652 r
= bus_verify_polkit_async(message
,
2654 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2658 &m
->polkit_registry
,
2663 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2666 r
= efi_set_reboot_to_firmware(b
);
2671 r
= touch("/run/systemd/reboot-to-firmware-setup");
2675 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2680 return sd_bus_reply_method_return(message
, NULL
);
2683 static int method_can_reboot_to_firmware_setup(
2684 sd_bus_message
*message
,
2686 sd_bus_error
*error
) {
2688 Manager
*m
= userdata
;
2694 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2696 /* EFI case: let's see what the firmware supports */
2698 r
= efi_reboot_to_firmware_supported();
2700 if (r
!= -EOPNOTSUPP
)
2701 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2703 return sd_bus_reply_method_return(message
, "s", "na");
2706 } else if (r
<= 0) {
2707 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2710 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2712 return sd_bus_reply_method_return(message
, "s", "na");
2715 return return_test_polkit(
2718 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2724 static int property_get_reboot_to_boot_loader_menu(
2727 const char *interface
,
2728 const char *property
,
2729 sd_bus_message
*reply
,
2731 sd_bus_error
*error
) {
2733 uint64_t x
= UINT64_MAX
;
2740 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2742 _cleanup_free_
char *v
= NULL
;
2744 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2746 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2747 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2748 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2751 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", &v
);
2754 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable: %m");
2758 r
= safe_atou64(v
, &sec
);
2760 log_warning_errno(r
, "Failed to parse LoaderConfigTimeoutOneShot value '%s': %m", v
);
2761 else if (sec
> (USEC_INFINITY
/ USEC_PER_SEC
))
2762 log_warning("LoaderConfigTimeoutOneShot too large, ignoring: %m");
2764 x
= sec
* USEC_PER_SEC
; /* return in µs */
2768 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2770 _cleanup_free_
char *v
= NULL
;
2772 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2774 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2777 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2779 r
= safe_atou64(v
, &x
);
2781 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2785 return sd_bus_message_append(reply
, "t", x
);
2788 static int method_set_reboot_to_boot_loader_menu(
2789 sd_bus_message
*message
,
2791 sd_bus_error
*error
) {
2793 Manager
*m
= userdata
;
2801 r
= sd_bus_message_read(message
, "t", &x
);
2805 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2809 /* EFI case: let's see if booting into boot loader menu is supported. */
2811 r
= efi_loader_get_features(&features
);
2813 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2814 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2815 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2819 } else if (r
<= 0) {
2820 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2823 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2825 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2827 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2830 r
= bus_verify_polkit_async(message
,
2832 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2836 &m
->polkit_registry
,
2841 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2844 if (x
== UINT64_MAX
)
2845 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", NULL
, 0);
2847 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2848 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2850 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", buf
);
2855 if (x
== UINT64_MAX
) {
2856 if (unlink("/run/systemd/reboot-to-loader-menu") < 0 && errno
!= ENOENT
)
2859 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2861 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2863 r
= write_string_file_atomic_label("/run/systemd/reboot-to-loader-menu", buf
);
2869 return sd_bus_reply_method_return(message
, NULL
);
2872 static int method_can_reboot_to_boot_loader_menu(
2873 sd_bus_message
*message
,
2875 sd_bus_error
*error
) {
2877 Manager
*m
= userdata
;
2883 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2885 uint64_t features
= 0;
2887 /* EFI case, let's see if booting into boot loader menu is supported. */
2889 r
= efi_loader_get_features(&features
);
2891 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2892 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2893 return sd_bus_reply_method_return(message
, "s", "na");
2895 } else if (r
<= 0) {
2896 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2899 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2901 return sd_bus_reply_method_return(message
, "s", "na");
2904 return return_test_polkit(
2907 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2913 static int property_get_reboot_to_boot_loader_entry(
2916 const char *interface
,
2917 const char *property
,
2918 sd_bus_message
*reply
,
2920 sd_bus_error
*error
) {
2922 _cleanup_free_
char *v
= NULL
;
2929 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2931 /* EFI case: let's read the LoaderEntryOneShot variable */
2933 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &v
);
2936 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable: %m");
2937 } else if (!efi_loader_entry_name_valid(v
)) {
2938 log_warning("LoaderEntryOneShot contains invalid entry name '%s', ignoring.", v
);
2942 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2945 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2947 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2950 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry: %m");
2951 } else if (!efi_loader_entry_name_valid(v
)) {
2952 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2957 return sd_bus_message_append(reply
, "s", v
);
2960 static int boot_loader_entry_exists(const char *id
) {
2961 _cleanup_(boot_config_free
) BootConfig config
= {};
2966 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2967 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2970 (void) boot_entries_augment_from_loader(&config
, true);
2972 return boot_config_has_entry(&config
, id
);
2975 static int method_set_reboot_to_boot_loader_entry(
2976 sd_bus_message
*message
,
2978 sd_bus_error
*error
) {
2980 Manager
*m
= userdata
;
2988 r
= sd_bus_message_read(message
, "s", &v
);
2994 else if (efi_loader_entry_name_valid(v
)) {
2995 r
= boot_loader_entry_exists(v
);
2999 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3001 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3003 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3007 /* EFI case: let's see if booting into boot loader entry is supported. */
3009 r
= efi_loader_get_features(&features
);
3011 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3012 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3013 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3017 } else if (r
<= 0) {
3018 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3021 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3023 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3025 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3028 r
= bus_verify_polkit_async(message
,
3030 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3034 &m
->polkit_registry
,
3039 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3044 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", NULL
, 0);
3046 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", v
);
3051 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3054 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3060 return sd_bus_reply_method_return(message
, NULL
);
3063 static int method_can_reboot_to_boot_loader_entry(
3064 sd_bus_message
*message
,
3066 sd_bus_error
*error
) {
3068 Manager
*m
= userdata
;
3074 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3076 uint64_t features
= 0;
3078 /* EFI case, let's see if booting into boot loader entry is supported. */
3080 r
= efi_loader_get_features(&features
);
3082 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3083 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3084 return sd_bus_reply_method_return(message
, "s", "na");
3086 } else if (r
<= 0) {
3087 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3090 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3092 return sd_bus_reply_method_return(message
, "s", "na");
3095 return return_test_polkit(
3098 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3104 static int property_get_boot_loader_entries(
3107 const char *interface
,
3108 const char *property
,
3109 sd_bus_message
*reply
,
3111 sd_bus_error
*error
) {
3113 _cleanup_(boot_config_free
) BootConfig config
= {};
3121 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3122 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3125 (void) boot_entries_augment_from_loader(&config
, true);
3127 r
= sd_bus_message_open_container(reply
, 'a', "s");
3131 for (i
= 0; i
< config
.n_entries
; i
++) {
3132 BootEntry
*e
= config
.entries
+ i
;
3134 r
= sd_bus_message_append(reply
, "s", e
->id
);
3139 return sd_bus_message_close_container(reply
);
3142 static int method_set_wall_message(
3143 sd_bus_message
*message
,
3145 sd_bus_error
*error
) {
3148 Manager
*m
= userdata
;
3150 unsigned enable_wall_messages
;
3155 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3159 r
= bus_verify_polkit_async(message
,
3161 "org.freedesktop.login1.set-wall-message",
3165 &m
->polkit_registry
,
3170 return 1; /* Will call us back */
3172 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3176 m
->enable_wall_messages
= enable_wall_messages
;
3178 return sd_bus_reply_method_return(message
, NULL
);
3181 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3182 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3183 const char *who
, *why
, *what
, *mode
;
3184 _cleanup_free_
char *id
= NULL
;
3185 _cleanup_close_
int fifo_fd
= -1;
3186 Manager
*m
= userdata
;
3187 Inhibitor
*i
= NULL
;
3197 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3201 w
= inhibit_what_from_string(what
);
3203 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3204 "Invalid what specification %s", what
);
3206 mm
= inhibit_mode_from_string(mode
);
3208 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3209 "Invalid mode specification %s", mode
);
3211 /* Delay is only supported for shutdown/sleep */
3212 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3213 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3214 "Delay inhibitors only supported for shutdown and sleep");
3216 /* Don't allow taking delay locks while we are already
3217 * executing the operation. We shouldn't create the impression
3218 * that the lock was successful if the machine is about to go
3219 * down/suspend any moment. */
3220 if (m
->action_what
& w
)
3221 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3222 "The operation inhibition has been requested for is already running");
3224 r
= bus_verify_polkit_async(
3227 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3228 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3229 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3230 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3231 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3232 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3233 "org.freedesktop.login1.inhibit-handle-lid-switch",
3237 &m
->polkit_registry
,
3242 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3244 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3248 r
= sd_bus_creds_get_euid(creds
, &uid
);
3252 r
= sd_bus_creds_get_pid(creds
, &pid
);
3256 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3257 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3258 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3264 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3267 } while (hashmap_get(m
->inhibitors
, id
));
3269 r
= manager_add_inhibitor(m
, id
, &i
);
3277 i
->why
= strdup(why
);
3278 i
->who
= strdup(who
);
3280 if (!i
->why
|| !i
->who
) {
3285 fifo_fd
= inhibitor_create_fifo(i
);
3291 r
= inhibitor_start(i
);
3295 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3304 const sd_bus_vtable manager_vtable
[] = {
3305 SD_BUS_VTABLE_START(0),
3307 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3308 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3310 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3311 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3312 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3313 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3314 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3315 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3316 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3317 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3318 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3319 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3320 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3321 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3322 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3323 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3324 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3325 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3326 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3327 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3328 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3329 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3330 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3331 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3332 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3333 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3334 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3335 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3336 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3337 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3338 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3339 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3340 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3341 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3342 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3343 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3344 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3345 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3346 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3347 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3349 SD_BUS_METHOD("GetSession", "s", "o", method_get_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3350 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
3351 SD_BUS_METHOD("GetUser", "u", "o", method_get_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3352 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
3353 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3354 SD_BUS_METHOD("ListSessions", NULL
, "a(susso)", method_list_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3355 SD_BUS_METHOD("ListUsers", NULL
, "a(uso)", method_list_users
, SD_BUS_VTABLE_UNPRIVILEGED
),
3356 SD_BUS_METHOD("ListSeats", NULL
, "a(so)", method_list_seats
, SD_BUS_VTABLE_UNPRIVILEGED
),
3357 SD_BUS_METHOD("ListInhibitors", NULL
, "a(ssssuu)", method_list_inhibitors
, SD_BUS_VTABLE_UNPRIVILEGED
),
3358 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session
, 0),
3359 SD_BUS_METHOD("ReleaseSession", "s", NULL
, method_release_session
, 0),
3360 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3361 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL
, method_activate_session_on_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3362 SD_BUS_METHOD("LockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3363 SD_BUS_METHOD("UnlockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3364 SD_BUS_METHOD("LockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3365 SD_BUS_METHOD("UnlockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3366 SD_BUS_METHOD("KillSession", "ssi", NULL
, method_kill_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3367 SD_BUS_METHOD("KillUser", "ui", NULL
, method_kill_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3368 SD_BUS_METHOD("TerminateSession", "s", NULL
, method_terminate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3369 SD_BUS_METHOD("TerminateUser", "u", NULL
, method_terminate_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3370 SD_BUS_METHOD("TerminateSeat", "s", NULL
, method_terminate_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3371 SD_BUS_METHOD("SetUserLinger", "ubb", NULL
, method_set_user_linger
, SD_BUS_VTABLE_UNPRIVILEGED
),
3372 SD_BUS_METHOD("AttachDevice", "ssb", NULL
, method_attach_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
3373 SD_BUS_METHOD("FlushDevices", "b", NULL
, method_flush_devices
, SD_BUS_VTABLE_UNPRIVILEGED
),
3374 SD_BUS_METHOD("PowerOff", "b", NULL
, method_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
3375 SD_BUS_METHOD("Reboot", "b", NULL
, method_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
3376 SD_BUS_METHOD("Halt", "b", NULL
, method_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
3377 SD_BUS_METHOD("Suspend", "b", NULL
, method_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
3378 SD_BUS_METHOD("Hibernate", "b", NULL
, method_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3379 SD_BUS_METHOD("HybridSleep", "b", NULL
, method_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
3380 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL
, method_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3381 SD_BUS_METHOD("CanPowerOff", NULL
, "s", method_can_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
3382 SD_BUS_METHOD("CanReboot", NULL
, "s", method_can_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
3383 SD_BUS_METHOD("CanHalt", NULL
, "s", method_can_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
3384 SD_BUS_METHOD("CanSuspend", NULL
, "s", method_can_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
3385 SD_BUS_METHOD("CanHibernate", NULL
, "s", method_can_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3386 SD_BUS_METHOD("CanHybridSleep", NULL
, "s", method_can_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
3387 SD_BUS_METHOD("CanSuspendThenHibernate", NULL
, "s", method_can_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3388 SD_BUS_METHOD("ScheduleShutdown", "st", NULL
, method_schedule_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
3389 SD_BUS_METHOD("CancelScheduledShutdown", NULL
, "b", method_cancel_scheduled_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
3390 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit
, SD_BUS_VTABLE_UNPRIVILEGED
),
3391 SD_BUS_METHOD("CanRebootParameter", NULL
, "s", method_can_reboot_parameter
, SD_BUS_VTABLE_UNPRIVILEGED
),
3392 SD_BUS_METHOD("SetRebootParameter", "s", NULL
, method_set_reboot_parameter
, SD_BUS_VTABLE_UNPRIVILEGED
),
3393 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL
, "s", method_can_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
3394 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL
, method_set_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
3395 SD_BUS_METHOD("CanRebootToBootLoaderMenu", NULL
, "s", method_can_reboot_to_boot_loader_menu
, SD_BUS_VTABLE_UNPRIVILEGED
),
3396 SD_BUS_METHOD("SetRebootToBootLoaderMenu", "t", NULL
, method_set_reboot_to_boot_loader_menu
, SD_BUS_VTABLE_UNPRIVILEGED
),
3397 SD_BUS_METHOD("CanRebootToBootLoaderEntry", NULL
, "s", method_can_reboot_to_boot_loader_entry
, SD_BUS_VTABLE_UNPRIVILEGED
),
3398 SD_BUS_METHOD("SetRebootToBootLoaderEntry", "s", NULL
, method_set_reboot_to_boot_loader_entry
, SD_BUS_VTABLE_UNPRIVILEGED
),
3399 SD_BUS_METHOD("SetWallMessage", "sb", NULL
, method_set_wall_message
, SD_BUS_VTABLE_UNPRIVILEGED
),
3401 SD_BUS_SIGNAL("SessionNew", "so", 0),
3402 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
3403 SD_BUS_SIGNAL("UserNew", "uo", 0),
3404 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
3405 SD_BUS_SIGNAL("SeatNew", "so", 0),
3406 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
3407 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
3408 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
3413 static int session_jobs_reply(Session
*s
, const char *unit
, const char *result
) {
3420 if (result
&& !streq(result
, "done")) {
3421 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3423 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3424 "Start job for unit '%s' failed with '%s'", unit
, result
);
3425 return session_send_create_reply(s
, &e
);
3428 return session_send_create_reply(s
, NULL
);
3431 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3432 const char *path
, *result
, *unit
;
3433 Manager
*m
= userdata
;
3442 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3444 bus_log_parse_error(r
);
3448 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3449 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
3451 /* Tell people that they now may take a lock again */
3452 (void) send_prepare_for(m
, m
->action_what
, false);
3454 m
->action_job
= mfree(m
->action_job
);
3455 m
->action_unit
= NULL
;
3460 session
= hashmap_get(m
->session_units
, unit
);
3462 if (streq_ptr(path
, session
->scope_job
)) {
3463 session
->scope_job
= mfree(session
->scope_job
);
3464 (void) session_jobs_reply(session
, unit
, result
);
3466 session_save(session
);
3467 user_save(session
->user
);
3470 session_add_to_gc_queue(session
);
3473 user
= hashmap_get(m
->user_units
, unit
);
3475 if (streq_ptr(path
, user
->service_job
)) {
3476 user
->service_job
= mfree(user
->service_job
);
3478 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3479 (void) session_jobs_reply(session
, unit
, NULL
/* don't propagate user service failures to the client */);
3484 user_add_to_gc_queue(user
);
3490 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3491 const char *path
, *unit
;
3492 Manager
*m
= userdata
;
3500 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3502 bus_log_parse_error(r
);
3506 session
= hashmap_get(m
->session_units
, unit
);
3508 session_add_to_gc_queue(session
);
3510 user
= hashmap_get(m
->user_units
, unit
);
3512 user_add_to_gc_queue(user
);
3517 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3518 _cleanup_free_
char *unit
= NULL
;
3519 Manager
*m
= userdata
;
3528 path
= sd_bus_message_get_path(message
);
3532 r
= unit_name_from_dbus_path(path
, &unit
);
3533 if (r
== -EINVAL
) /* not a unit */
3540 session
= hashmap_get(m
->session_units
, unit
);
3542 session_add_to_gc_queue(session
);
3544 user
= hashmap_get(m
->user_units
, unit
);
3546 user_add_to_gc_queue(user
);
3551 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3552 Manager
*m
= userdata
;
3560 r
= sd_bus_message_read(message
, "b", &b
);
3562 bus_log_parse_error(r
);
3569 /* systemd finished reloading, let's recheck all our sessions */
3570 log_debug("System manager has been reloaded, rechecking sessions...");
3572 HASHMAP_FOREACH(session
, m
->sessions
, i
)
3573 session_add_to_gc_queue(session
);
3578 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3583 l
= strv_from_stdarg_alloca(property
);
3585 return sd_bus_emit_properties_changed_strv(
3587 "/org/freedesktop/login1",
3588 "org.freedesktop.login1.Manager",
3592 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3597 r
= sd_bus_message_read(reply
, "o", &j
);
3609 int manager_start_scope(
3614 const char *description
,
3617 const char *requires_mounts_for
,
3618 sd_bus_message
*more_properties
,
3619 sd_bus_error
*error
,
3622 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3631 r
= sd_bus_message_new_method_call(
3634 "org.freedesktop.systemd1",
3635 "/org/freedesktop/systemd1",
3636 "org.freedesktop.systemd1.Manager",
3637 "StartTransientUnit");
3641 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3645 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3649 if (!isempty(slice
)) {
3650 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3655 if (!isempty(description
)) {
3656 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
3661 STRV_FOREACH(i
, wants
) {
3662 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
3667 STRV_FOREACH(i
, after
) {
3668 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3673 if (!empty_or_root(requires_mounts_for
)) {
3674 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3679 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3681 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3685 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3689 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3690 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
3692 return bus_log_create_error(r
);
3694 if (more_properties
) {
3695 /* If TasksMax also appears here, it will overwrite the default value set above */
3696 r
= sd_bus_message_copy(m
, more_properties
, true);
3701 r
= sd_bus_message_close_container(m
);
3705 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3709 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3713 return strdup_job(reply
, job
);
3716 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3717 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3724 r
= sd_bus_call_method(
3726 "org.freedesktop.systemd1",
3727 "/org/freedesktop/systemd1",
3728 "org.freedesktop.systemd1.Manager",
3732 "ss", unit
, "replace");
3736 return strdup_job(reply
, job
);
3739 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3740 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3747 r
= sd_bus_call_method(
3749 "org.freedesktop.systemd1",
3750 "/org/freedesktop/systemd1",
3751 "org.freedesktop.systemd1.Manager",
3755 "ss", unit
, "fail");
3757 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3758 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
3761 sd_bus_error_free(error
);
3768 return strdup_job(reply
, job
);
3771 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
3772 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3773 _cleanup_free_
char *path
= NULL
;
3779 path
= unit_dbus_path_from_name(scope
);
3783 r
= sd_bus_call_method(
3785 "org.freedesktop.systemd1",
3787 "org.freedesktop.systemd1.Scope",
3793 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3794 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
3795 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
3798 sd_bus_error_move(ret_error
, &error
);
3805 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
3809 return sd_bus_call_method(
3811 "org.freedesktop.systemd1",
3812 "/org/freedesktop/systemd1",
3813 "org.freedesktop.systemd1.Manager",
3817 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
3820 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
3821 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3822 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3823 _cleanup_free_
char *path
= NULL
;
3830 path
= unit_dbus_path_from_name(unit
);
3834 r
= sd_bus_get_property(
3836 "org.freedesktop.systemd1",
3838 "org.freedesktop.systemd1.Unit",
3844 /* systemd might have dropped off momentarily, let's
3845 * not make this an error */
3846 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3847 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3850 /* If the unit is already unloaded then it's not
3852 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3853 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
3856 sd_bus_error_move(ret_error
, &error
);
3860 r
= sd_bus_message_read(reply
, "s", &state
);
3864 return !STR_IN_SET(state
, "inactive", "failed");
3867 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
3868 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3869 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3875 r
= sd_bus_get_property(
3877 "org.freedesktop.systemd1",
3879 "org.freedesktop.systemd1.Job",
3885 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3886 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3889 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
3892 sd_bus_error_move(ret_error
, &error
);
3896 /* We don't actually care about the state really. The fact
3897 * that we could read the job state is enough for us */