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
) {
168 if (!uid_is_valid(uid
))
169 return get_sender_user(m
, message
, error
, ret
);
171 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
173 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
174 "User ID "UID_FMT
" is not logged in or lingering", uid
);
180 int manager_get_seat_from_creds(
182 sd_bus_message
*message
,
193 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
196 /* Use these special seat names as session names */
197 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
201 seat
= session
->seat
;
203 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
205 seat
= hashmap_get(m
->seats
, name
);
207 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
214 static int return_test_polkit(
215 sd_bus_message
*message
,
218 const char **details
,
226 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
233 result
= "challenge";
237 return sd_bus_reply_method_return(message
, "s", result
);
240 static int property_get_idle_hint(
243 const char *interface
,
244 const char *property
,
245 sd_bus_message
*reply
,
247 sd_bus_error
*error
) {
249 Manager
*m
= userdata
;
255 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
258 static int property_get_idle_since_hint(
261 const char *interface
,
262 const char *property
,
263 sd_bus_message
*reply
,
265 sd_bus_error
*error
) {
267 Manager
*m
= userdata
;
268 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
274 manager_get_idle_hint(m
, &t
);
276 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
279 static int property_get_inhibited(
282 const char *interface
,
283 const char *property
,
284 sd_bus_message
*reply
,
286 sd_bus_error
*error
) {
288 Manager
*m
= userdata
;
295 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
297 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
300 static int property_get_preparing(
303 const char *interface
,
304 const char *property
,
305 sd_bus_message
*reply
,
307 sd_bus_error
*error
) {
309 Manager
*m
= userdata
;
316 if (streq(property
, "PreparingForShutdown"))
317 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
319 b
= m
->action_what
& INHIBIT_SLEEP
;
321 return sd_bus_message_append(reply
, "b", b
);
324 static int property_get_scheduled_shutdown(
327 const char *interface
,
328 const char *property
,
329 sd_bus_message
*reply
,
331 sd_bus_error
*error
) {
333 Manager
*m
= userdata
;
340 r
= sd_bus_message_open_container(reply
, 'r', "st");
344 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
348 return sd_bus_message_close_container(reply
);
351 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
352 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
353 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
354 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
355 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
356 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
358 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
359 _cleanup_free_
char *p
= NULL
;
360 Manager
*m
= userdata
;
368 r
= sd_bus_message_read(message
, "s", &name
);
372 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
376 p
= session_bus_path(session
);
380 return sd_bus_reply_method_return(message
, "o", p
);
383 /* Get login session of a process. This is not what you are looking for these days,
384 * as apps may instead belong to a user service unit. This includes terminal
385 * emulators and hence command-line apps. */
386 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
387 _cleanup_free_
char *p
= NULL
;
388 Session
*session
= NULL
;
389 Manager
*m
= userdata
;
396 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
398 r
= sd_bus_message_read(message
, "u", &pid
);
405 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
409 r
= manager_get_session_by_pid(m
, pid
, &session
);
414 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
415 "PID "PID_FMT
" does not belong to any known session", pid
);
418 p
= session_bus_path(session
);
422 return sd_bus_reply_method_return(message
, "o", p
);
425 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
426 _cleanup_free_
char *p
= NULL
;
427 Manager
*m
= userdata
;
435 r
= sd_bus_message_read(message
, "u", &uid
);
439 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
443 p
= user_bus_path(user
);
447 return sd_bus_reply_method_return(message
, "o", p
);
450 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
451 _cleanup_free_
char *p
= NULL
;
452 Manager
*m
= userdata
;
460 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
462 r
= sd_bus_message_read(message
, "u", &pid
);
469 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
473 r
= manager_get_user_by_pid(m
, pid
, &user
);
477 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
478 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
482 p
= user_bus_path(user
);
486 return sd_bus_reply_method_return(message
, "o", p
);
489 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
490 _cleanup_free_
char *p
= NULL
;
491 Manager
*m
= userdata
;
499 r
= sd_bus_message_read(message
, "s", &name
);
503 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
507 p
= seat_bus_path(seat
);
511 return sd_bus_reply_method_return(message
, "o", p
);
514 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
515 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
516 Manager
*m
= userdata
;
524 r
= sd_bus_message_new_method_return(message
, &reply
);
528 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
532 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
533 _cleanup_free_
char *p
= NULL
;
535 p
= session_bus_path(session
);
539 r
= sd_bus_message_append(reply
, "(susso)",
541 (uint32_t) session
->user
->uid
,
543 session
->seat
? session
->seat
->id
: "",
549 r
= sd_bus_message_close_container(reply
);
553 return sd_bus_send(NULL
, reply
, NULL
);
556 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
557 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
558 Manager
*m
= userdata
;
566 r
= sd_bus_message_new_method_return(message
, &reply
);
570 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
574 HASHMAP_FOREACH(user
, m
->users
, i
) {
575 _cleanup_free_
char *p
= NULL
;
577 p
= user_bus_path(user
);
581 r
= sd_bus_message_append(reply
, "(uso)",
582 (uint32_t) user
->uid
,
589 r
= sd_bus_message_close_container(reply
);
593 return sd_bus_send(NULL
, reply
, NULL
);
596 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
597 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
598 Manager
*m
= userdata
;
606 r
= sd_bus_message_new_method_return(message
, &reply
);
610 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
614 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
615 _cleanup_free_
char *p
= NULL
;
617 p
= seat_bus_path(seat
);
621 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
626 r
= sd_bus_message_close_container(reply
);
630 return sd_bus_send(NULL
, reply
, NULL
);
633 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
634 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
635 Manager
*m
= userdata
;
636 Inhibitor
*inhibitor
;
643 r
= sd_bus_message_new_method_return(message
, &reply
);
647 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
651 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
653 r
= sd_bus_message_append(reply
, "(ssssuu)",
654 strempty(inhibit_what_to_string(inhibitor
->what
)),
655 strempty(inhibitor
->who
),
656 strempty(inhibitor
->why
),
657 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
658 (uint32_t) inhibitor
->uid
,
659 (uint32_t) inhibitor
->pid
);
664 r
= sd_bus_message_close_container(reply
);
668 return sd_bus_send(NULL
, reply
, NULL
);
671 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
672 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
673 _cleanup_free_
char *id
= NULL
;
674 Session
*session
= NULL
;
675 uint32_t audit_id
= 0;
676 Manager
*m
= userdata
;
690 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
691 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
693 r
= sd_bus_message_read(message
, "uusssssussbss",
694 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
695 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
699 if (!uid_is_valid(uid
))
700 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
701 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
702 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
705 t
= _SESSION_TYPE_INVALID
;
707 t
= session_type_from_string(type
);
709 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
710 "Invalid session type %s", type
);
714 c
= _SESSION_CLASS_INVALID
;
716 c
= session_class_from_string(class);
718 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
719 "Invalid session class %s", class);
722 if (isempty(desktop
))
725 if (!string_is_safe(desktop
))
726 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
727 "Invalid desktop string %s", desktop
);
733 seat
= hashmap_get(m
->seats
, cseat
);
735 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
736 "No seat '%s' known", cseat
);
739 if (tty_is_vc(tty
)) {
744 else if (seat
!= m
->seat0
)
745 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
746 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
748 v
= vtnr_from_tty(tty
);
750 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
751 "Cannot determine VT number from virtual console TTY %s", tty
);
755 else if (vtnr
!= (uint32_t) v
)
756 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
757 "Specified TTY and VT number do not match");
759 } else if (tty_is_console(tty
)) {
763 else if (seat
!= m
->seat0
)
764 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
765 "Console TTY specified but seat is not seat0");
768 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
769 "Console TTY specified but VT number is not 0");
773 if (seat_has_vts(seat
)) {
774 if (vtnr
<= 0 || vtnr
> 63)
775 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
776 "VT number out of range");
779 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
780 "Seat has no VTs but VT number not 0");
784 if (t
== _SESSION_TYPE_INVALID
) {
785 if (!isempty(display
))
787 else if (!isempty(tty
))
790 t
= SESSION_UNSPECIFIED
;
793 if (c
== _SESSION_CLASS_INVALID
) {
794 if (t
== SESSION_UNSPECIFIED
)
795 c
= SESSION_BACKGROUND
;
801 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
803 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
807 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
812 /* Check if we are already in a logind session. Or if we are in user@.service
813 * which is a special PAM session that avoids creating a logind session. */
814 r
= manager_get_user_by_pid(m
, leader
, NULL
);
818 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
819 "Already running in a session or user slice");
822 * Old gdm and lightdm start the user-session on the same VT as
823 * the greeter session. But they destroy the greeter session
824 * after the user-session and want the user-session to take
825 * over the VT. We need to support this for
826 * backwards-compatibility, so make sure we allow new sessions
827 * on a VT that a greeter is running on. Furthermore, to allow
828 * re-logins, we have to allow a greeter to take over a used VT for
829 * the exact same reasons.
831 if (c
!= SESSION_GREETER
&&
833 vtnr
< m
->seat0
->position_count
&&
834 m
->seat0
->positions
[vtnr
] &&
835 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
836 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
838 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
839 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
840 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
843 (void) audit_session_from_pid(leader
, &audit_id
);
844 if (audit_session_is_valid(audit_id
)) {
845 /* Keep our session IDs and the audit session IDs in sync */
847 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
850 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
851 * not trust the audit data and let's better register a new ID */
852 if (hashmap_contains(m
->sessions
, id
)) {
853 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
854 audit_id
= AUDIT_SESSION_INVALID
;
863 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
866 } while (hashmap_contains(m
->sessions
, id
));
869 /* The generated names should not clash with 'auto' or 'self' */
870 assert(!SESSION_IS_SELF(id
));
871 assert(!SESSION_IS_AUTO(id
));
873 /* If we are not watching utmp already, try again */
874 manager_reconnect_utmp(m
);
876 r
= manager_add_user_by_uid(m
, uid
, &user
);
880 r
= manager_add_session(m
, id
, &session
);
884 session_set_user(session
, user
);
885 r
= session_set_leader(session
, leader
);
891 session
->remote
= remote
;
892 session
->vtnr
= vtnr
;
895 session
->tty
= strdup(tty
);
901 session
->tty_validity
= TTY_FROM_PAM
;
904 if (!isempty(display
)) {
905 session
->display
= strdup(display
);
906 if (!session
->display
) {
912 if (!isempty(remote_user
)) {
913 session
->remote_user
= strdup(remote_user
);
914 if (!session
->remote_user
) {
920 if (!isempty(remote_host
)) {
921 session
->remote_host
= strdup(remote_host
);
922 if (!session
->remote_host
) {
928 if (!isempty(service
)) {
929 session
->service
= strdup(service
);
930 if (!session
->service
) {
936 if (!isempty(desktop
)) {
937 session
->desktop
= strdup(desktop
);
938 if (!session
->desktop
) {
945 r
= seat_attach_session(seat
, session
);
950 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
954 r
= session_start(session
, message
, error
);
958 r
= sd_bus_message_exit_container(message
);
962 session
->create_message
= sd_bus_message_ref(message
);
964 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
965 * session_send_create_reply(). */
971 session_add_to_gc_queue(session
);
974 user_add_to_gc_queue(user
);
979 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
980 Manager
*m
= userdata
;
988 r
= sd_bus_message_read(message
, "s", &name
);
992 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
996 r
= session_release(session
);
1000 return sd_bus_reply_method_return(message
, NULL
);
1003 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1004 Manager
*m
= userdata
;
1012 r
= sd_bus_message_read(message
, "s", &name
);
1016 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1020 return bus_session_method_activate(message
, session
, error
);
1023 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1024 const char *session_name
, *seat_name
;
1025 Manager
*m
= userdata
;
1033 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1035 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1039 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1043 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1047 if (session
->seat
!= seat
)
1048 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1049 "Session %s not on seat %s", session_name
, seat_name
);
1051 r
= session_activate(session
);
1055 return sd_bus_reply_method_return(message
, NULL
);
1058 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1059 Manager
*m
= userdata
;
1067 r
= sd_bus_message_read(message
, "s", &name
);
1071 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1075 return bus_session_method_lock(message
, session
, error
);
1078 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1079 Manager
*m
= userdata
;
1085 r
= bus_verify_polkit_async(
1088 "org.freedesktop.login1.lock-sessions",
1092 &m
->polkit_registry
,
1097 return 1; /* Will call us back */
1099 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1103 return sd_bus_reply_method_return(message
, NULL
);
1106 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1108 Manager
*m
= userdata
;
1115 r
= sd_bus_message_read(message
, "s", &name
);
1119 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1123 return bus_session_method_kill(message
, session
, error
);
1126 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1127 Manager
*m
= userdata
;
1135 r
= sd_bus_message_read(message
, "u", &uid
);
1139 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1143 return bus_user_method_kill(message
, user
, error
);
1146 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1147 Manager
*m
= userdata
;
1155 r
= sd_bus_message_read(message
, "s", &name
);
1159 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1163 return bus_session_method_terminate(message
, session
, error
);
1166 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1167 Manager
*m
= userdata
;
1175 r
= sd_bus_message_read(message
, "u", &uid
);
1179 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1183 return bus_user_method_terminate(message
, user
, error
);
1186 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1187 Manager
*m
= userdata
;
1195 r
= sd_bus_message_read(message
, "s", &name
);
1199 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1203 return bus_seat_method_terminate(message
, seat
, error
);
1206 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1207 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1208 _cleanup_free_
char *cc
= NULL
;
1209 Manager
*m
= userdata
;
1210 int r
, b
, interactive
;
1213 uint32_t uid
, auth_uid
;
1218 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1222 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1223 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1227 if (!uid_is_valid(uid
)) {
1228 /* Note that we get the owner UID of the session or user unit,
1229 * not the actual client UID here! */
1230 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1235 /* owner_uid is racy, so for authorization we must use euid */
1236 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1243 return errno_or_else(ENOENT
);
1245 r
= bus_verify_polkit_async(
1248 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1249 "org.freedesktop.login1.set-user-linger",
1253 &m
->polkit_registry
,
1258 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1260 (void) mkdir_p_label("/var/lib/systemd", 0755);
1261 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1265 cc
= cescape(pw
->pw_name
);
1269 path
= strjoina("/var/lib/systemd/linger/", cc
);
1277 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1284 if (r
< 0 && errno
!= ENOENT
)
1287 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1289 user_add_to_gc_queue(u
);
1292 return sd_bus_reply_method_return(message
, NULL
);
1295 static int trigger_device(Manager
*m
, sd_device
*d
) {
1296 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1301 r
= sd_device_enumerator_new(&e
);
1305 r
= sd_device_enumerator_allow_uninitialized(e
);
1310 r
= sd_device_enumerator_add_match_parent(e
, d
);
1315 FOREACH_DEVICE(e
, d
) {
1316 _cleanup_free_
char *t
= NULL
;
1319 r
= sd_device_get_syspath(d
, &p
);
1323 t
= path_join(p
, "uevent");
1327 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1333 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1334 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1335 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1336 const char *id_for_seat
;
1343 r
= sd_device_new_from_syspath(&d
, sysfs
);
1347 if (sd_device_has_tag(d
, "seat") <= 0)
1350 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1353 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1356 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1359 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1360 r
= write_string_file_atomic_label(file
, rule
);
1364 return trigger_device(m
, d
);
1367 static int flush_devices(Manager
*m
) {
1368 _cleanup_closedir_
DIR *d
;
1372 d
= opendir("/etc/udev/rules.d");
1374 if (errno
!= ENOENT
)
1375 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1379 FOREACH_DIRENT_ALL(de
, d
, break) {
1380 dirent_ensure_type(d
, de
);
1381 if (!dirent_is_file(de
))
1384 if (!startswith(de
->d_name
, "72-seat-"))
1387 if (!endswith(de
->d_name
, ".rules"))
1390 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1391 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1395 return trigger_device(m
, NULL
);
1398 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1399 const char *sysfs
, *seat
;
1400 Manager
*m
= userdata
;
1406 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1410 if (!path_is_normalized(sysfs
))
1411 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1412 if (!path_startswith(sysfs
, "/sys"))
1413 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1415 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1418 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1424 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1425 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1427 r
= bus_verify_polkit_async(
1430 "org.freedesktop.login1.attach-device",
1434 &m
->polkit_registry
,
1439 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1441 r
= attach_device(m
, seat
, sysfs
);
1445 return sd_bus_reply_method_return(message
, NULL
);
1448 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1449 Manager
*m
= userdata
;
1455 r
= sd_bus_message_read(message
, "b", &interactive
);
1459 r
= bus_verify_polkit_async(
1462 "org.freedesktop.login1.flush-devices",
1466 &m
->polkit_registry
,
1471 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1473 r
= flush_devices(m
);
1477 return sd_bus_reply_method_return(message
, NULL
);
1480 static int have_multiple_sessions(
1489 /* Check for other users' sessions. Greeter sessions do not
1490 * count, and non-login sessions do not count either. */
1491 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1492 if (session
->class == SESSION_USER
&&
1493 session
->user
->uid
!= uid
)
1499 static int bus_manager_log_shutdown(
1501 const char *unit_name
) {
1508 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1509 p
= "MESSAGE=System is powering down";
1510 q
= "SHUTDOWN=power-off";
1511 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1512 p
= "MESSAGE=System is rebooting";
1513 q
= "SHUTDOWN=reboot";
1514 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1515 p
= "MESSAGE=System is halting";
1516 q
= "SHUTDOWN=halt";
1517 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1518 p
= "MESSAGE=System is rebooting with kexec";
1519 q
= "SHUTDOWN=kexec";
1521 p
= "MESSAGE=System is shutting down";
1525 if (isempty(m
->wall_message
))
1526 p
= strjoina(p
, ".");
1528 p
= strjoina(p
, " (", m
->wall_message
, ").");
1530 return log_struct(LOG_NOTICE
,
1531 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1536 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1537 Manager
*m
= userdata
;
1542 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1546 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1551 if (until
<= now(CLOCK_MONOTONIC
))
1554 /* We want to ignore the lid switch for a while after each
1555 * suspend, and after boot-up. Hence let's install a timer for
1556 * this. As long as the event source exists we ignore the lid
1559 if (m
->lid_switch_ignore_event_source
) {
1562 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1569 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1571 r
= sd_event_add_time(
1573 &m
->lid_switch_ignore_event_source
,
1576 lid_switch_ignore_handler
, m
);
1581 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1582 int active
= _active
;
1585 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1587 return sd_bus_emit_signal(m
->bus
,
1588 "/org/freedesktop/login1",
1589 "org.freedesktop.login1.Manager",
1590 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1595 static int execute_shutdown_or_sleep(
1598 const char *unit_name
,
1599 sd_bus_error
*error
) {
1601 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1607 assert(w
< _INHIBIT_WHAT_MAX
);
1610 if (w
== INHIBIT_SHUTDOWN
)
1611 bus_manager_log_shutdown(m
, unit_name
);
1613 r
= sd_bus_call_method(
1615 "org.freedesktop.systemd1",
1616 "/org/freedesktop/systemd1",
1617 "org.freedesktop.systemd1.Manager",
1621 "ss", unit_name
, "replace-irreversibly");
1625 r
= sd_bus_message_read(reply
, "o", &p
);
1629 r
= free_and_strdup(&m
->action_job
, p
);
1633 m
->action_unit
= unit_name
;
1636 /* Make sure the lid switch is ignored for a while */
1637 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1642 /* Tell people that they now may take a lock again */
1643 (void) send_prepare_for(m
, w
, false);
1648 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1650 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1651 Inhibitor
*offending
= NULL
;
1656 if (manager
->action_what
== 0 || manager
->action_job
)
1659 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1660 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1665 (void) get_process_comm(offending
->pid
, &comm
);
1666 u
= uid_to_name(offending
->uid
);
1668 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1669 offending
->uid
, strna(u
),
1670 offending
->pid
, strna(comm
));
1673 /* Actually do the operation */
1674 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1676 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1677 bus_error_message(&error
, r
));
1679 manager
->action_unit
= NULL
;
1680 manager
->action_what
= 0;
1687 static int manager_inhibit_timeout_handler(
1692 Manager
*manager
= userdata
;
1696 assert(manager
->inhibit_timeout_source
== s
);
1698 r
= manager_dispatch_delayed(manager
, true);
1699 return (r
< 0) ? r
: 0;
1702 static int delay_shutdown_or_sleep(
1705 const char *unit_name
) {
1712 assert(w
< _INHIBIT_WHAT_MAX
);
1715 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1717 if (m
->inhibit_timeout_source
) {
1718 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1720 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1722 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1724 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1726 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1727 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1732 m
->action_unit
= unit_name
;
1738 int bus_manager_shutdown_or_sleep_now_or_later(
1740 const char *unit_name
,
1742 sd_bus_error
*error
) {
1744 _cleanup_free_
char *load_state
= NULL
;
1751 assert(w
< _INHIBIT_WHAT_MAX
);
1752 assert(!m
->action_job
);
1754 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1758 if (!streq(load_state
, "loaded"))
1759 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1760 "Unit %s is %s, refusing operation.",
1761 unit_name
, load_state
);
1763 /* Tell everybody to prepare for shutdown/sleep */
1764 (void) send_prepare_for(m
, w
, true);
1767 m
->inhibit_delay_max
> 0 &&
1768 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1771 /* Shutdown is delayed, keep in mind what we
1772 * want to do, and start a timeout */
1773 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1775 /* Shutdown is not delayed, execute it
1777 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1782 static int verify_shutdown_creds(
1784 sd_bus_message
*message
,
1788 const char *action_multiple_sessions
,
1789 const char *action_ignore_inhibit
,
1790 sd_bus_error
*error
) {
1792 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1793 bool multiple_sessions
, blocked
;
1800 assert(w
<= _INHIBIT_WHAT_MAX
);
1802 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1806 r
= sd_bus_creds_get_euid(creds
, &uid
);
1810 r
= have_multiple_sessions(m
, uid
);
1814 multiple_sessions
= r
> 0;
1815 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1817 if (multiple_sessions
&& action_multiple_sessions
) {
1818 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1822 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1825 if (blocked
&& action_ignore_inhibit
) {
1826 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1830 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1833 if (!multiple_sessions
&& !blocked
&& action
) {
1834 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1838 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1844 static int method_do_shutdown_or_sleep(
1846 sd_bus_message
*message
,
1847 const char *unit_name
,
1850 const char *action_multiple_sessions
,
1851 const char *action_ignore_inhibit
,
1852 const char *sleep_verb
,
1853 sd_bus_error
*error
) {
1861 assert(w
<= _INHIBIT_WHAT_MAX
);
1863 r
= sd_bus_message_read(message
, "b", &interactive
);
1867 /* Don't allow multiple jobs being executed at the same time */
1868 if (m
->action_what
> 0)
1869 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1870 "There's already a shutdown or sleep operation in progress");
1873 r
= can_sleep(sleep_verb
);
1875 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1876 "Not enough swap space for hibernation");
1878 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1879 "Sleep verb \"%s\" not supported", sleep_verb
);
1884 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1885 action_ignore_inhibit
, error
);
1889 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1893 return sd_bus_reply_method_return(message
, NULL
);
1896 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1897 Manager
*m
= userdata
;
1899 return method_do_shutdown_or_sleep(
1901 SPECIAL_POWEROFF_TARGET
,
1903 "org.freedesktop.login1.power-off",
1904 "org.freedesktop.login1.power-off-multiple-sessions",
1905 "org.freedesktop.login1.power-off-ignore-inhibit",
1910 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1911 Manager
*m
= userdata
;
1913 return method_do_shutdown_or_sleep(
1915 SPECIAL_REBOOT_TARGET
,
1917 "org.freedesktop.login1.reboot",
1918 "org.freedesktop.login1.reboot-multiple-sessions",
1919 "org.freedesktop.login1.reboot-ignore-inhibit",
1924 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1925 Manager
*m
= userdata
;
1927 return method_do_shutdown_or_sleep(
1929 SPECIAL_HALT_TARGET
,
1931 "org.freedesktop.login1.halt",
1932 "org.freedesktop.login1.halt-multiple-sessions",
1933 "org.freedesktop.login1.halt-ignore-inhibit",
1938 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1939 Manager
*m
= userdata
;
1941 return method_do_shutdown_or_sleep(
1943 SPECIAL_SUSPEND_TARGET
,
1945 "org.freedesktop.login1.suspend",
1946 "org.freedesktop.login1.suspend-multiple-sessions",
1947 "org.freedesktop.login1.suspend-ignore-inhibit",
1952 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1953 Manager
*m
= userdata
;
1955 return method_do_shutdown_or_sleep(
1957 SPECIAL_HIBERNATE_TARGET
,
1959 "org.freedesktop.login1.hibernate",
1960 "org.freedesktop.login1.hibernate-multiple-sessions",
1961 "org.freedesktop.login1.hibernate-ignore-inhibit",
1966 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1967 Manager
*m
= userdata
;
1969 return method_do_shutdown_or_sleep(
1971 SPECIAL_HYBRID_SLEEP_TARGET
,
1973 "org.freedesktop.login1.hibernate",
1974 "org.freedesktop.login1.hibernate-multiple-sessions",
1975 "org.freedesktop.login1.hibernate-ignore-inhibit",
1980 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1981 Manager
*m
= userdata
;
1983 return method_do_shutdown_or_sleep(
1985 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
1987 "org.freedesktop.login1.hibernate",
1988 "org.freedesktop.login1.hibernate-multiple-sessions",
1989 "org.freedesktop.login1.hibernate-ignore-inhibit",
1994 static int nologin_timeout_handler(
1999 Manager
*m
= userdata
;
2001 log_info("Creating /run/nologin, blocking further logins...");
2004 create_shutdown_run_nologin_or_warn() >= 0;
2009 static int update_schedule_file(Manager
*m
) {
2010 _cleanup_free_
char *temp_path
= NULL
;
2011 _cleanup_fclose_
FILE *f
= NULL
;
2016 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
2018 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2020 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
2022 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2024 (void) fchmod(fileno(f
), 0644);
2030 m
->scheduled_shutdown_timeout
,
2031 m
->enable_wall_messages
,
2032 m
->scheduled_shutdown_type
);
2034 if (!isempty(m
->wall_message
)) {
2035 _cleanup_free_
char *t
;
2037 t
= cescape(m
->wall_message
);
2043 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
2046 r
= fflush_and_check(f
);
2050 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2058 (void) unlink(temp_path
);
2059 (void) unlink("/run/systemd/shutdown/scheduled");
2061 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2064 static void reset_scheduled_shutdown(Manager
*m
) {
2067 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2068 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2069 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2071 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
2072 m
->scheduled_shutdown_timeout
= 0;
2073 m
->shutdown_dry_run
= false;
2075 if (m
->unlink_nologin
) {
2076 (void) unlink_or_warn("/run/nologin");
2077 m
->unlink_nologin
= false;
2080 (void) unlink("/run/systemd/shutdown/scheduled");
2083 static int manager_scheduled_shutdown_handler(
2088 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2089 Manager
*m
= userdata
;
2095 if (isempty(m
->scheduled_shutdown_type
))
2098 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2099 target
= SPECIAL_POWEROFF_TARGET
;
2100 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2101 target
= SPECIAL_REBOOT_TARGET
;
2102 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2103 target
= SPECIAL_HALT_TARGET
;
2105 assert_not_reached("unexpected shutdown type");
2107 /* Don't allow multiple jobs being executed at the same time */
2108 if (m
->action_what
> 0) {
2110 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2114 if (m
->shutdown_dry_run
) {
2115 /* We do not process delay inhibitors here. Otherwise, we
2116 * would have to be considered "in progress" (like the check
2117 * above) for some seconds after our admin has seen the final
2120 bus_manager_log_shutdown(m
, target
);
2121 log_info("Running in dry run, suppressing action.");
2122 reset_scheduled_shutdown(m
);
2127 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2129 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2136 reset_scheduled_shutdown(m
);
2140 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2141 Manager
*m
= userdata
;
2142 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2143 const char *action_multiple_sessions
= NULL
;
2144 const char *action_ignore_inhibit
= NULL
;
2145 const char *action
= NULL
;
2149 bool dry_run
= false;
2154 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2158 if (startswith(type
, "dry-")) {
2163 if (streq(type
, "poweroff")) {
2164 action
= "org.freedesktop.login1.power-off";
2165 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2166 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2167 } else if (streq(type
, "reboot")) {
2168 action
= "org.freedesktop.login1.reboot";
2169 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2170 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2171 } else if (streq(type
, "halt")) {
2172 action
= "org.freedesktop.login1.halt";
2173 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2174 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2176 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2178 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2179 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2183 if (m
->scheduled_shutdown_timeout_source
) {
2184 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2186 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2188 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2190 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2192 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2193 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2195 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2198 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2200 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2204 m
->shutdown_dry_run
= dry_run
;
2206 if (m
->nologin_timeout_source
) {
2207 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2209 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2211 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2213 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2215 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2216 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2218 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2221 m
->scheduled_shutdown_timeout
= elapse
;
2223 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2225 const char *tty
= NULL
;
2227 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2228 (void) sd_bus_creds_get_tty(creds
, &tty
);
2230 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2232 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2237 r
= manager_setup_wall_message_timer(m
);
2241 r
= update_schedule_file(m
);
2245 return sd_bus_reply_method_return(message
, NULL
);
2248 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2249 Manager
*m
= userdata
;
2255 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2256 reset_scheduled_shutdown(m
);
2258 if (cancelled
&& m
->enable_wall_messages
) {
2259 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2260 _cleanup_free_
char *username
= NULL
;
2261 const char *tty
= NULL
;
2265 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2267 (void) sd_bus_creds_get_uid(creds
, &uid
);
2268 (void) sd_bus_creds_get_tty(creds
, &tty
);
2271 username
= uid_to_name(uid
);
2272 utmp_wall("The system shutdown has been cancelled",
2273 username
, tty
, logind_wall_tty_filter
, m
);
2276 return sd_bus_reply_method_return(message
, "b", cancelled
);
2279 static int method_can_shutdown_or_sleep(
2281 sd_bus_message
*message
,
2284 const char *action_multiple_sessions
,
2285 const char *action_ignore_inhibit
,
2286 const char *sleep_verb
,
2287 sd_bus_error
*error
) {
2289 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2290 HandleAction handle
;
2291 bool multiple_sessions
, challenge
, blocked
;
2292 const char *result
= NULL
;
2299 assert(w
<= _INHIBIT_WHAT_MAX
);
2301 assert(action_multiple_sessions
);
2302 assert(action_ignore_inhibit
);
2305 r
= can_sleep(sleep_verb
);
2306 if (IN_SET(r
, 0, -ENOSPC
))
2307 return sd_bus_reply_method_return(message
, "s", "na");
2312 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2316 r
= sd_bus_creds_get_euid(creds
, &uid
);
2320 r
= have_multiple_sessions(m
, uid
);
2324 multiple_sessions
= r
> 0;
2325 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2327 handle
= handle_action_from_string(sleep_verb
);
2331 target
= manager_target_for_action(handle
);
2333 _cleanup_free_
char *load_state
= NULL
;
2335 r
= unit_load_state(m
->bus
, target
, &load_state
);
2339 if (!streq(load_state
, "loaded")) {
2346 if (multiple_sessions
) {
2347 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2354 result
= "challenge";
2360 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2367 } else if (challenge
) {
2368 if (!result
|| streq(result
, "yes"))
2369 result
= "challenge";
2374 if (!multiple_sessions
&& !blocked
) {
2375 /* If neither inhibit nor multiple sessions
2376 * apply then just check the normal policy */
2378 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2385 result
= "challenge";
2391 return sd_bus_reply_method_return(message
, "s", result
);
2394 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2395 Manager
*m
= userdata
;
2397 return method_can_shutdown_or_sleep(
2400 "org.freedesktop.login1.power-off",
2401 "org.freedesktop.login1.power-off-multiple-sessions",
2402 "org.freedesktop.login1.power-off-ignore-inhibit",
2407 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2408 Manager
*m
= userdata
;
2410 return method_can_shutdown_or_sleep(
2413 "org.freedesktop.login1.reboot",
2414 "org.freedesktop.login1.reboot-multiple-sessions",
2415 "org.freedesktop.login1.reboot-ignore-inhibit",
2420 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2421 Manager
*m
= userdata
;
2423 return method_can_shutdown_or_sleep(
2426 "org.freedesktop.login1.halt",
2427 "org.freedesktop.login1.halt-multiple-sessions",
2428 "org.freedesktop.login1.halt-ignore-inhibit",
2433 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2434 Manager
*m
= userdata
;
2436 return method_can_shutdown_or_sleep(
2439 "org.freedesktop.login1.suspend",
2440 "org.freedesktop.login1.suspend-multiple-sessions",
2441 "org.freedesktop.login1.suspend-ignore-inhibit",
2446 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2447 Manager
*m
= userdata
;
2449 return method_can_shutdown_or_sleep(
2452 "org.freedesktop.login1.hibernate",
2453 "org.freedesktop.login1.hibernate-multiple-sessions",
2454 "org.freedesktop.login1.hibernate-ignore-inhibit",
2459 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2460 Manager
*m
= userdata
;
2462 return method_can_shutdown_or_sleep(
2465 "org.freedesktop.login1.hibernate",
2466 "org.freedesktop.login1.hibernate-multiple-sessions",
2467 "org.freedesktop.login1.hibernate-ignore-inhibit",
2472 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2473 Manager
*m
= userdata
;
2475 return method_can_shutdown_or_sleep(
2478 "org.freedesktop.login1.hibernate",
2479 "org.freedesktop.login1.hibernate-multiple-sessions",
2480 "org.freedesktop.login1.hibernate-ignore-inhibit",
2481 "suspend-then-hibernate",
2485 static int property_get_reboot_parameter(
2488 const char *interface
,
2489 const char *property
,
2490 sd_bus_message
*reply
,
2492 sd_bus_error
*error
) {
2493 _cleanup_free_
char *parameter
= NULL
;
2500 r
= read_reboot_parameter(¶meter
);
2504 return sd_bus_message_append(reply
, "s", parameter
);
2507 static int method_set_reboot_parameter(
2508 sd_bus_message
*message
,
2510 sd_bus_error
*error
) {
2512 Manager
*m
= userdata
;
2519 r
= sd_bus_message_read(message
, "s", &arg
);
2523 r
= detect_container();
2527 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2528 "Reboot parameter not supported in containers, refusing.");
2530 r
= bus_verify_polkit_async(message
,
2532 "org.freedesktop.login1.set-reboot-parameter",
2536 &m
->polkit_registry
,
2541 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2543 r
= update_reboot_parameter_and_warn(arg
, false);
2547 return sd_bus_reply_method_return(message
, NULL
);
2550 static int method_can_reboot_parameter(
2551 sd_bus_message
*message
,
2553 sd_bus_error
*error
) {
2555 Manager
*m
= userdata
;
2561 r
= detect_container();
2564 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2565 return sd_bus_reply_method_return(message
, "s", "na");
2567 return return_test_polkit(
2570 "org.freedesktop.login1.set-reboot-parameter",
2576 static int property_get_reboot_to_firmware_setup(
2579 const char *interface
,
2580 const char *property
,
2581 sd_bus_message
*reply
,
2583 sd_bus_error
*error
) {
2590 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2592 /* EFI case: let's see what is currently configured in the EFI variables */
2593 r
= efi_get_reboot_to_firmware();
2594 if (r
< 0 && r
!= -EOPNOTSUPP
)
2595 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2597 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2599 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2600 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2601 if (errno
!= ENOENT
)
2602 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2609 return sd_bus_message_append(reply
, "b", r
> 0);
2612 static int method_set_reboot_to_firmware_setup(
2613 sd_bus_message
*message
,
2615 sd_bus_error
*error
) {
2617 Manager
*m
= userdata
;
2624 r
= sd_bus_message_read(message
, "b", &b
);
2628 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2630 /* EFI case: let's see what the firmware supports */
2632 r
= efi_reboot_to_firmware_supported();
2633 if (r
== -EOPNOTSUPP
)
2634 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2640 } else if (r
<= 0) {
2641 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2644 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2646 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2648 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2651 r
= bus_verify_polkit_async(message
,
2653 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2657 &m
->polkit_registry
,
2662 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2665 r
= efi_set_reboot_to_firmware(b
);
2670 r
= touch("/run/systemd/reboot-to-firmware-setup");
2674 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2679 return sd_bus_reply_method_return(message
, NULL
);
2682 static int method_can_reboot_to_firmware_setup(
2683 sd_bus_message
*message
,
2685 sd_bus_error
*error
) {
2687 Manager
*m
= userdata
;
2693 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2695 /* EFI case: let's see what the firmware supports */
2697 r
= efi_reboot_to_firmware_supported();
2699 if (r
!= -EOPNOTSUPP
)
2700 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2702 return sd_bus_reply_method_return(message
, "s", "na");
2705 } else if (r
<= 0) {
2706 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2709 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2711 return sd_bus_reply_method_return(message
, "s", "na");
2714 return return_test_polkit(
2717 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2723 static int property_get_reboot_to_boot_loader_menu(
2726 const char *interface
,
2727 const char *property
,
2728 sd_bus_message
*reply
,
2730 sd_bus_error
*error
) {
2732 uint64_t x
= UINT64_MAX
;
2739 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2741 _cleanup_free_
char *v
= NULL
;
2743 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2745 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2746 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2747 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2750 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", &v
);
2753 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable: %m");
2757 r
= safe_atou64(v
, &sec
);
2759 log_warning_errno(r
, "Failed to parse LoaderConfigTimeoutOneShot value '%s': %m", v
);
2760 else if (sec
> (USEC_INFINITY
/ USEC_PER_SEC
))
2761 log_warning("LoaderConfigTimeoutOneShot too large, ignoring: %m");
2763 x
= sec
* USEC_PER_SEC
; /* return in µs */
2767 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2769 _cleanup_free_
char *v
= NULL
;
2771 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2773 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2776 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2778 r
= safe_atou64(v
, &x
);
2780 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2784 return sd_bus_message_append(reply
, "t", x
);
2787 static int method_set_reboot_to_boot_loader_menu(
2788 sd_bus_message
*message
,
2790 sd_bus_error
*error
) {
2792 Manager
*m
= userdata
;
2800 r
= sd_bus_message_read(message
, "t", &x
);
2804 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2808 /* EFI case: let's see if booting into boot loader menu is supported. */
2810 r
= efi_loader_get_features(&features
);
2812 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2813 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2814 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2818 } else if (r
<= 0) {
2819 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2822 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2824 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2826 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2829 r
= bus_verify_polkit_async(message
,
2831 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2835 &m
->polkit_registry
,
2840 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2843 if (x
== UINT64_MAX
)
2844 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", NULL
, 0);
2846 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2847 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2849 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", buf
);
2854 if (x
== UINT64_MAX
) {
2855 if (unlink("/run/systemd/reboot-to-loader-menu") < 0 && errno
!= ENOENT
)
2858 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2860 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2862 r
= write_string_file_atomic_label("/run/systemd/reboot-to-loader-menu", buf
);
2868 return sd_bus_reply_method_return(message
, NULL
);
2871 static int method_can_reboot_to_boot_loader_menu(
2872 sd_bus_message
*message
,
2874 sd_bus_error
*error
) {
2876 Manager
*m
= userdata
;
2882 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2884 uint64_t features
= 0;
2886 /* EFI case, let's see if booting into boot loader menu is supported. */
2888 r
= efi_loader_get_features(&features
);
2890 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2891 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2892 return sd_bus_reply_method_return(message
, "s", "na");
2894 } else if (r
<= 0) {
2895 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2898 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2900 return sd_bus_reply_method_return(message
, "s", "na");
2903 return return_test_polkit(
2906 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2912 static int property_get_reboot_to_boot_loader_entry(
2915 const char *interface
,
2916 const char *property
,
2917 sd_bus_message
*reply
,
2919 sd_bus_error
*error
) {
2921 _cleanup_free_
char *v
= NULL
;
2928 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2930 /* EFI case: let's read the LoaderEntryOneShot variable */
2932 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &v
);
2935 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable: %m");
2936 } else if (!efi_loader_entry_name_valid(v
)) {
2937 log_warning("LoaderEntryOneShot contains invalid entry name '%s', ignoring.", v
);
2941 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2944 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2946 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2949 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry: %m");
2950 } else if (!efi_loader_entry_name_valid(v
)) {
2951 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2956 return sd_bus_message_append(reply
, "s", v
);
2959 static int boot_loader_entry_exists(const char *id
) {
2960 _cleanup_(boot_config_free
) BootConfig config
= {};
2965 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2966 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2969 (void) boot_entries_augment_from_loader(&config
, true);
2971 return boot_config_has_entry(&config
, id
);
2974 static int method_set_reboot_to_boot_loader_entry(
2975 sd_bus_message
*message
,
2977 sd_bus_error
*error
) {
2979 Manager
*m
= userdata
;
2987 r
= sd_bus_message_read(message
, "s", &v
);
2993 else if (efi_loader_entry_name_valid(v
)) {
2994 r
= boot_loader_entry_exists(v
);
2998 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3000 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3002 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3006 /* EFI case: let's see if booting into boot loader entry is supported. */
3008 r
= efi_loader_get_features(&features
);
3010 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3011 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3012 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3016 } else if (r
<= 0) {
3017 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3020 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3022 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3024 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3027 r
= bus_verify_polkit_async(message
,
3029 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3033 &m
->polkit_registry
,
3038 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3043 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", NULL
, 0);
3045 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", v
);
3050 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3053 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3059 return sd_bus_reply_method_return(message
, NULL
);
3062 static int method_can_reboot_to_boot_loader_entry(
3063 sd_bus_message
*message
,
3065 sd_bus_error
*error
) {
3067 Manager
*m
= userdata
;
3073 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3075 uint64_t features
= 0;
3077 /* EFI case, let's see if booting into boot loader entry is supported. */
3079 r
= efi_loader_get_features(&features
);
3081 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3082 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3083 return sd_bus_reply_method_return(message
, "s", "na");
3085 } else if (r
<= 0) {
3086 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3089 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3091 return sd_bus_reply_method_return(message
, "s", "na");
3094 return return_test_polkit(
3097 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3103 static int property_get_boot_loader_entries(
3106 const char *interface
,
3107 const char *property
,
3108 sd_bus_message
*reply
,
3110 sd_bus_error
*error
) {
3112 _cleanup_(boot_config_free
) BootConfig config
= {};
3120 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3121 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3124 (void) boot_entries_augment_from_loader(&config
, true);
3126 r
= sd_bus_message_open_container(reply
, 'a', "s");
3130 for (i
= 0; i
< config
.n_entries
; i
++) {
3131 BootEntry
*e
= config
.entries
+ i
;
3133 r
= sd_bus_message_append(reply
, "s", e
->id
);
3138 return sd_bus_message_close_container(reply
);
3141 static int method_set_wall_message(
3142 sd_bus_message
*message
,
3144 sd_bus_error
*error
) {
3147 Manager
*m
= userdata
;
3149 unsigned enable_wall_messages
;
3154 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3158 r
= bus_verify_polkit_async(message
,
3160 "org.freedesktop.login1.set-wall-message",
3164 &m
->polkit_registry
,
3169 return 1; /* Will call us back */
3171 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3175 m
->enable_wall_messages
= enable_wall_messages
;
3177 return sd_bus_reply_method_return(message
, NULL
);
3180 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3181 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3182 const char *who
, *why
, *what
, *mode
;
3183 _cleanup_free_
char *id
= NULL
;
3184 _cleanup_close_
int fifo_fd
= -1;
3185 Manager
*m
= userdata
;
3186 Inhibitor
*i
= NULL
;
3196 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3200 w
= inhibit_what_from_string(what
);
3202 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3203 "Invalid what specification %s", what
);
3205 mm
= inhibit_mode_from_string(mode
);
3207 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3208 "Invalid mode specification %s", mode
);
3210 /* Delay is only supported for shutdown/sleep */
3211 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3212 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3213 "Delay inhibitors only supported for shutdown and sleep");
3215 /* Don't allow taking delay locks while we are already
3216 * executing the operation. We shouldn't create the impression
3217 * that the lock was successful if the machine is about to go
3218 * down/suspend any moment. */
3219 if (m
->action_what
& w
)
3220 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3221 "The operation inhibition has been requested for is already running");
3223 r
= bus_verify_polkit_async(
3226 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3227 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3228 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3229 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3230 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3231 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3232 "org.freedesktop.login1.inhibit-handle-lid-switch",
3236 &m
->polkit_registry
,
3241 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3243 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3247 r
= sd_bus_creds_get_euid(creds
, &uid
);
3251 r
= sd_bus_creds_get_pid(creds
, &pid
);
3255 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3256 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3257 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3263 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3266 } while (hashmap_get(m
->inhibitors
, id
));
3268 r
= manager_add_inhibitor(m
, id
, &i
);
3276 i
->why
= strdup(why
);
3277 i
->who
= strdup(who
);
3279 if (!i
->why
|| !i
->who
) {
3284 fifo_fd
= inhibitor_create_fifo(i
);
3290 r
= inhibitor_start(i
);
3294 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3303 const sd_bus_vtable manager_vtable
[] = {
3304 SD_BUS_VTABLE_START(0),
3306 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3307 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3309 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3310 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3311 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3312 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3313 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3314 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3315 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3316 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3317 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3318 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3319 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3320 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3321 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3322 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3323 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3324 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3325 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3326 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3327 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3328 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3329 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3330 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3331 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3332 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3333 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3334 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3335 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3336 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3337 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3338 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3339 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3340 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3341 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3342 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3343 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3344 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3345 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3346 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3348 SD_BUS_METHOD("GetSession", "s", "o", method_get_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3349 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
3350 SD_BUS_METHOD("GetUser", "u", "o", method_get_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3351 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
3352 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3353 SD_BUS_METHOD("ListSessions", NULL
, "a(susso)", method_list_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3354 SD_BUS_METHOD("ListUsers", NULL
, "a(uso)", method_list_users
, SD_BUS_VTABLE_UNPRIVILEGED
),
3355 SD_BUS_METHOD("ListSeats", NULL
, "a(so)", method_list_seats
, SD_BUS_VTABLE_UNPRIVILEGED
),
3356 SD_BUS_METHOD("ListInhibitors", NULL
, "a(ssssuu)", method_list_inhibitors
, SD_BUS_VTABLE_UNPRIVILEGED
),
3357 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session
, 0),
3358 SD_BUS_METHOD("ReleaseSession", "s", NULL
, method_release_session
, 0),
3359 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3360 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL
, method_activate_session_on_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3361 SD_BUS_METHOD("LockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3362 SD_BUS_METHOD("UnlockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3363 SD_BUS_METHOD("LockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3364 SD_BUS_METHOD("UnlockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3365 SD_BUS_METHOD("KillSession", "ssi", NULL
, method_kill_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3366 SD_BUS_METHOD("KillUser", "ui", NULL
, method_kill_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3367 SD_BUS_METHOD("TerminateSession", "s", NULL
, method_terminate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3368 SD_BUS_METHOD("TerminateUser", "u", NULL
, method_terminate_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3369 SD_BUS_METHOD("TerminateSeat", "s", NULL
, method_terminate_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3370 SD_BUS_METHOD("SetUserLinger", "ubb", NULL
, method_set_user_linger
, SD_BUS_VTABLE_UNPRIVILEGED
),
3371 SD_BUS_METHOD("AttachDevice", "ssb", NULL
, method_attach_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
3372 SD_BUS_METHOD("FlushDevices", "b", NULL
, method_flush_devices
, SD_BUS_VTABLE_UNPRIVILEGED
),
3373 SD_BUS_METHOD("PowerOff", "b", NULL
, method_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
3374 SD_BUS_METHOD("Reboot", "b", NULL
, method_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
3375 SD_BUS_METHOD("Halt", "b", NULL
, method_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
3376 SD_BUS_METHOD("Suspend", "b", NULL
, method_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
3377 SD_BUS_METHOD("Hibernate", "b", NULL
, method_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3378 SD_BUS_METHOD("HybridSleep", "b", NULL
, method_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
3379 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL
, method_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3380 SD_BUS_METHOD("CanPowerOff", NULL
, "s", method_can_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
3381 SD_BUS_METHOD("CanReboot", NULL
, "s", method_can_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
3382 SD_BUS_METHOD("CanHalt", NULL
, "s", method_can_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
3383 SD_BUS_METHOD("CanSuspend", NULL
, "s", method_can_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
3384 SD_BUS_METHOD("CanHibernate", NULL
, "s", method_can_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3385 SD_BUS_METHOD("CanHybridSleep", NULL
, "s", method_can_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
3386 SD_BUS_METHOD("CanSuspendThenHibernate", NULL
, "s", method_can_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3387 SD_BUS_METHOD("ScheduleShutdown", "st", NULL
, method_schedule_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
3388 SD_BUS_METHOD("CancelScheduledShutdown", NULL
, "b", method_cancel_scheduled_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
3389 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit
, SD_BUS_VTABLE_UNPRIVILEGED
),
3390 SD_BUS_METHOD("CanRebootParameter", NULL
, "s", method_can_reboot_parameter
, SD_BUS_VTABLE_UNPRIVILEGED
),
3391 SD_BUS_METHOD("SetRebootParameter", "s", NULL
, method_set_reboot_parameter
, SD_BUS_VTABLE_UNPRIVILEGED
),
3392 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL
, "s", method_can_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
3393 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL
, method_set_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
3394 SD_BUS_METHOD("CanRebootToBootLoaderMenu", NULL
, "s", method_can_reboot_to_boot_loader_menu
, SD_BUS_VTABLE_UNPRIVILEGED
),
3395 SD_BUS_METHOD("SetRebootToBootLoaderMenu", "t", NULL
, method_set_reboot_to_boot_loader_menu
, SD_BUS_VTABLE_UNPRIVILEGED
),
3396 SD_BUS_METHOD("CanRebootToBootLoaderEntry", NULL
, "s", method_can_reboot_to_boot_loader_entry
, SD_BUS_VTABLE_UNPRIVILEGED
),
3397 SD_BUS_METHOD("SetRebootToBootLoaderEntry", "s", NULL
, method_set_reboot_to_boot_loader_entry
, SD_BUS_VTABLE_UNPRIVILEGED
),
3398 SD_BUS_METHOD("SetWallMessage", "sb", NULL
, method_set_wall_message
, SD_BUS_VTABLE_UNPRIVILEGED
),
3400 SD_BUS_SIGNAL("SessionNew", "so", 0),
3401 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
3402 SD_BUS_SIGNAL("UserNew", "uo", 0),
3403 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
3404 SD_BUS_SIGNAL("SeatNew", "so", 0),
3405 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
3406 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
3407 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
3412 static int session_jobs_reply(Session
*s
, const char *unit
, const char *result
) {
3419 if (result
&& !streq(result
, "done")) {
3420 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3422 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3423 "Start job for unit '%s' failed with '%s'", unit
, result
);
3424 return session_send_create_reply(s
, &e
);
3427 return session_send_create_reply(s
, NULL
);
3430 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3431 const char *path
, *result
, *unit
;
3432 Manager
*m
= userdata
;
3441 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3443 bus_log_parse_error(r
);
3447 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3448 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
3450 /* Tell people that they now may take a lock again */
3451 (void) send_prepare_for(m
, m
->action_what
, false);
3453 m
->action_job
= mfree(m
->action_job
);
3454 m
->action_unit
= NULL
;
3459 session
= hashmap_get(m
->session_units
, unit
);
3461 if (streq_ptr(path
, session
->scope_job
)) {
3462 session
->scope_job
= mfree(session
->scope_job
);
3463 (void) session_jobs_reply(session
, unit
, result
);
3465 session_save(session
);
3466 user_save(session
->user
);
3469 session_add_to_gc_queue(session
);
3472 user
= hashmap_get(m
->user_units
, unit
);
3474 if (streq_ptr(path
, user
->service_job
)) {
3475 user
->service_job
= mfree(user
->service_job
);
3477 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3478 (void) session_jobs_reply(session
, unit
, NULL
/* don't propagate user service failures to the client */);
3483 user_add_to_gc_queue(user
);
3489 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3490 const char *path
, *unit
;
3491 Manager
*m
= userdata
;
3499 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3501 bus_log_parse_error(r
);
3505 session
= hashmap_get(m
->session_units
, unit
);
3507 session_add_to_gc_queue(session
);
3509 user
= hashmap_get(m
->user_units
, unit
);
3511 user_add_to_gc_queue(user
);
3516 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3517 _cleanup_free_
char *unit
= NULL
;
3518 Manager
*m
= userdata
;
3527 path
= sd_bus_message_get_path(message
);
3531 r
= unit_name_from_dbus_path(path
, &unit
);
3532 if (r
== -EINVAL
) /* not a unit */
3539 session
= hashmap_get(m
->session_units
, unit
);
3541 session_add_to_gc_queue(session
);
3543 user
= hashmap_get(m
->user_units
, unit
);
3545 user_add_to_gc_queue(user
);
3550 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3551 Manager
*m
= userdata
;
3559 r
= sd_bus_message_read(message
, "b", &b
);
3561 bus_log_parse_error(r
);
3568 /* systemd finished reloading, let's recheck all our sessions */
3569 log_debug("System manager has been reloaded, rechecking sessions...");
3571 HASHMAP_FOREACH(session
, m
->sessions
, i
)
3572 session_add_to_gc_queue(session
);
3577 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3582 l
= strv_from_stdarg_alloca(property
);
3584 return sd_bus_emit_properties_changed_strv(
3586 "/org/freedesktop/login1",
3587 "org.freedesktop.login1.Manager",
3591 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3596 r
= sd_bus_message_read(reply
, "o", &j
);
3608 int manager_start_scope(
3613 const char *description
,
3616 const char *requires_mounts_for
,
3617 sd_bus_message
*more_properties
,
3618 sd_bus_error
*error
,
3621 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3630 r
= sd_bus_message_new_method_call(
3633 "org.freedesktop.systemd1",
3634 "/org/freedesktop/systemd1",
3635 "org.freedesktop.systemd1.Manager",
3636 "StartTransientUnit");
3640 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3644 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3648 if (!isempty(slice
)) {
3649 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3654 if (!isempty(description
)) {
3655 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
3660 STRV_FOREACH(i
, wants
) {
3661 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
3666 STRV_FOREACH(i
, after
) {
3667 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3672 if (!empty_or_root(requires_mounts_for
)) {
3673 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3678 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3680 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3684 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3688 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3689 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
3691 return bus_log_create_error(r
);
3693 if (more_properties
) {
3694 /* If TasksMax also appears here, it will overwrite the default value set above */
3695 r
= sd_bus_message_copy(m
, more_properties
, true);
3700 r
= sd_bus_message_close_container(m
);
3704 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3708 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3712 return strdup_job(reply
, job
);
3715 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3716 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3723 r
= sd_bus_call_method(
3725 "org.freedesktop.systemd1",
3726 "/org/freedesktop/systemd1",
3727 "org.freedesktop.systemd1.Manager",
3731 "ss", unit
, "replace");
3735 return strdup_job(reply
, job
);
3738 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3739 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3746 r
= sd_bus_call_method(
3748 "org.freedesktop.systemd1",
3749 "/org/freedesktop/systemd1",
3750 "org.freedesktop.systemd1.Manager",
3754 "ss", unit
, "fail");
3756 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3757 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
3760 sd_bus_error_free(error
);
3767 return strdup_job(reply
, job
);
3770 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
3771 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3772 _cleanup_free_
char *path
= NULL
;
3778 path
= unit_dbus_path_from_name(scope
);
3782 r
= sd_bus_call_method(
3784 "org.freedesktop.systemd1",
3786 "org.freedesktop.systemd1.Scope",
3792 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3793 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
3794 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
3797 sd_bus_error_move(ret_error
, &error
);
3804 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
3808 return sd_bus_call_method(
3810 "org.freedesktop.systemd1",
3811 "/org/freedesktop/systemd1",
3812 "org.freedesktop.systemd1.Manager",
3816 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
3819 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
3820 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3821 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3822 _cleanup_free_
char *path
= NULL
;
3829 path
= unit_dbus_path_from_name(unit
);
3833 r
= sd_bus_get_property(
3835 "org.freedesktop.systemd1",
3837 "org.freedesktop.systemd1.Unit",
3843 /* systemd might have dropped off momentarily, let's
3844 * not make this an error */
3845 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3846 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3849 /* If the unit is already unloaded then it's not
3851 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3852 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
3855 sd_bus_error_move(ret_error
, &error
);
3859 r
= sd_bus_message_read(reply
, "s", &state
);
3863 return !STR_IN_SET(state
, "inactive", "failed");
3866 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
3867 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3868 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3874 r
= sd_bus_get_property(
3876 "org.freedesktop.systemd1",
3878 "org.freedesktop.systemd1.Job",
3884 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3885 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3888 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
3891 sd_bus_error_move(ret_error
, &error
);
3895 /* We don't actually care about the state really. The fact
3896 * that we could read the job state is enough for us */