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"
26 #include "fileio-label.h"
28 #include "format-util.h"
31 #include "missing_capability.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "process-util.h"
36 #include "reboot-util.h"
37 #include "selinux-util.h"
38 #include "sleep-config.h"
40 #include "stdio-util.h"
42 #include "terminal-util.h"
43 #include "tmpfile-util.h"
44 #include "unit-name.h"
45 #include "user-util.h"
46 #include "utmp-wtmp.h"
49 static int get_sender_session(
51 sd_bus_message
*message
,
56 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
57 Session
*session
= NULL
;
61 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
62 * and returns that. If not and 'consult_display' is true, this returns the display session of the
63 * owning user of the caller. */
65 r
= sd_bus_query_sender_creds(message
,
66 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
67 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
71 r
= sd_bus_creds_get_session(creds
, &name
);
76 if (consult_display
) {
79 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
86 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
88 session
= user
->display
;
92 session
= hashmap_get(m
->sessions
, name
);
95 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
97 "Caller does not belong to any known session and doesn't own any suitable session." :
98 "Caller does not belong to any known session.");
104 int manager_get_session_from_creds(
106 sd_bus_message
*message
,
117 if (SEAT_IS_SELF(name
)) /* the caller's own session */
118 return get_sender_session(m
, message
, false, error
, ret
);
119 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
120 return get_sender_session(m
, message
, true, error
, ret
);
122 session
= hashmap_get(m
->sessions
, name
);
124 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
130 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
131 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
136 /* Note that we get the owner UID of the session, not the actual client UID here! */
137 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
141 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
148 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
151 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
152 "Caller does not belong to any logged in or lingering user");
158 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
165 if (!uid_is_valid(uid
))
166 return get_sender_user(m
, message
, error
, ret
);
168 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
170 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
171 "User ID "UID_FMT
" is not logged in or lingering", uid
);
177 int manager_get_seat_from_creds(
179 sd_bus_message
*message
,
191 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
194 /* Use these special seat names as session names */
195 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
199 seat
= session
->seat
;
201 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
203 seat
= hashmap_get(m
->seats
, name
);
205 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
212 static int return_test_polkit(
213 sd_bus_message
*message
,
216 const char **details
,
224 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
231 result
= "challenge";
235 return sd_bus_reply_method_return(message
, "s", result
);
238 static int property_get_idle_hint(
241 const char *interface
,
242 const char *property
,
243 sd_bus_message
*reply
,
245 sd_bus_error
*error
) {
247 Manager
*m
= userdata
;
253 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
256 static int property_get_idle_since_hint(
259 const char *interface
,
260 const char *property
,
261 sd_bus_message
*reply
,
263 sd_bus_error
*error
) {
265 Manager
*m
= userdata
;
266 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
272 manager_get_idle_hint(m
, &t
);
274 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
277 static int property_get_inhibited(
280 const char *interface
,
281 const char *property
,
282 sd_bus_message
*reply
,
284 sd_bus_error
*error
) {
286 Manager
*m
= userdata
;
293 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
295 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
298 static int property_get_preparing(
301 const char *interface
,
302 const char *property
,
303 sd_bus_message
*reply
,
305 sd_bus_error
*error
) {
307 Manager
*m
= userdata
;
314 if (streq(property
, "PreparingForShutdown"))
315 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
317 b
= m
->action_what
& INHIBIT_SLEEP
;
319 return sd_bus_message_append(reply
, "b", b
);
322 static int property_get_scheduled_shutdown(
325 const char *interface
,
326 const char *property
,
327 sd_bus_message
*reply
,
329 sd_bus_error
*error
) {
331 Manager
*m
= userdata
;
338 r
= sd_bus_message_open_container(reply
, 'r', "st");
342 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
346 return sd_bus_message_close_container(reply
);
349 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
350 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
351 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
352 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
353 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
354 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
356 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
357 _cleanup_free_
char *p
= NULL
;
358 Manager
*m
= userdata
;
366 r
= sd_bus_message_read(message
, "s", &name
);
370 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
374 p
= session_bus_path(session
);
378 return sd_bus_reply_method_return(message
, "o", p
);
381 /* Get login session of a process. This is not what you are looking for these days,
382 * as apps may instead belong to a user service unit. This includes terminal
383 * emulators and hence command-line apps. */
384 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
385 _cleanup_free_
char *p
= NULL
;
386 Session
*session
= NULL
;
387 Manager
*m
= userdata
;
394 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
396 r
= sd_bus_message_read(message
, "u", &pid
);
403 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
407 r
= manager_get_session_by_pid(m
, pid
, &session
);
412 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
413 "PID "PID_FMT
" does not belong to any known session", pid
);
416 p
= session_bus_path(session
);
420 return sd_bus_reply_method_return(message
, "o", p
);
423 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
424 _cleanup_free_
char *p
= NULL
;
425 Manager
*m
= userdata
;
433 r
= sd_bus_message_read(message
, "u", &uid
);
437 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
441 p
= user_bus_path(user
);
445 return sd_bus_reply_method_return(message
, "o", p
);
448 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
449 _cleanup_free_
char *p
= NULL
;
450 Manager
*m
= userdata
;
458 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
460 r
= sd_bus_message_read(message
, "u", &pid
);
467 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
471 r
= manager_get_user_by_pid(m
, pid
, &user
);
475 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
476 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
480 p
= user_bus_path(user
);
484 return sd_bus_reply_method_return(message
, "o", p
);
487 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
488 _cleanup_free_
char *p
= NULL
;
489 Manager
*m
= userdata
;
497 r
= sd_bus_message_read(message
, "s", &name
);
501 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
505 p
= seat_bus_path(seat
);
509 return sd_bus_reply_method_return(message
, "o", p
);
512 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
513 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
514 Manager
*m
= userdata
;
522 r
= sd_bus_message_new_method_return(message
, &reply
);
526 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
530 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
531 _cleanup_free_
char *p
= NULL
;
533 p
= session_bus_path(session
);
537 r
= sd_bus_message_append(reply
, "(susso)",
539 (uint32_t) session
->user
->uid
,
541 session
->seat
? session
->seat
->id
: "",
547 r
= sd_bus_message_close_container(reply
);
551 return sd_bus_send(NULL
, reply
, NULL
);
554 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
555 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
556 Manager
*m
= userdata
;
564 r
= sd_bus_message_new_method_return(message
, &reply
);
568 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
572 HASHMAP_FOREACH(user
, m
->users
, i
) {
573 _cleanup_free_
char *p
= NULL
;
575 p
= user_bus_path(user
);
579 r
= sd_bus_message_append(reply
, "(uso)",
580 (uint32_t) user
->uid
,
587 r
= sd_bus_message_close_container(reply
);
591 return sd_bus_send(NULL
, reply
, NULL
);
594 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
595 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
596 Manager
*m
= userdata
;
604 r
= sd_bus_message_new_method_return(message
, &reply
);
608 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
612 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
613 _cleanup_free_
char *p
= NULL
;
615 p
= seat_bus_path(seat
);
619 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
624 r
= sd_bus_message_close_container(reply
);
628 return sd_bus_send(NULL
, reply
, NULL
);
631 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
632 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
633 Manager
*m
= userdata
;
634 Inhibitor
*inhibitor
;
641 r
= sd_bus_message_new_method_return(message
, &reply
);
645 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
649 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
651 r
= sd_bus_message_append(reply
, "(ssssuu)",
652 strempty(inhibit_what_to_string(inhibitor
->what
)),
653 strempty(inhibitor
->who
),
654 strempty(inhibitor
->why
),
655 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
656 (uint32_t) inhibitor
->uid
,
657 (uint32_t) inhibitor
->pid
);
662 r
= sd_bus_message_close_container(reply
);
666 return sd_bus_send(NULL
, reply
, NULL
);
669 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
670 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
671 _cleanup_free_
char *id
= NULL
;
672 Session
*session
= NULL
;
673 uint32_t audit_id
= 0;
674 Manager
*m
= userdata
;
688 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
689 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
691 r
= sd_bus_message_read(message
, "uusssssussbss",
692 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
693 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
697 if (!uid_is_valid(uid
))
698 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
699 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
700 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
703 t
= _SESSION_TYPE_INVALID
;
705 t
= session_type_from_string(type
);
707 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
708 "Invalid session type %s", type
);
712 c
= _SESSION_CLASS_INVALID
;
714 c
= session_class_from_string(class);
716 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
717 "Invalid session class %s", class);
720 if (isempty(desktop
))
723 if (!string_is_safe(desktop
))
724 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
725 "Invalid desktop string %s", desktop
);
731 seat
= hashmap_get(m
->seats
, cseat
);
733 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
734 "No seat '%s' known", cseat
);
737 if (tty_is_vc(tty
)) {
742 else if (seat
!= m
->seat0
)
743 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
744 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
746 v
= vtnr_from_tty(tty
);
748 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
749 "Cannot determine VT number from virtual console TTY %s", tty
);
753 else if (vtnr
!= (uint32_t) v
)
754 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
755 "Specified TTY and VT number do not match");
757 } else if (tty_is_console(tty
)) {
761 else if (seat
!= m
->seat0
)
762 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
763 "Console TTY specified but seat is not seat0");
766 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
767 "Console TTY specified but VT number is not 0");
771 if (seat_has_vts(seat
)) {
772 if (vtnr
<= 0 || vtnr
> 63)
773 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
774 "VT number out of range");
777 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
778 "Seat has no VTs but VT number not 0");
782 if (t
== _SESSION_TYPE_INVALID
) {
783 if (!isempty(display
))
785 else if (!isempty(tty
))
788 t
= SESSION_UNSPECIFIED
;
791 if (c
== _SESSION_CLASS_INVALID
) {
792 if (t
== SESSION_UNSPECIFIED
)
793 c
= SESSION_BACKGROUND
;
799 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
801 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
805 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
810 /* Check if we are already in a logind session. Or if we are in user@.service
811 * which is a special PAM session that avoids creating a logind session. */
812 r
= manager_get_user_by_pid(m
, leader
, NULL
);
816 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
817 "Already running in a session or user slice");
820 * Old gdm and lightdm start the user-session on the same VT as
821 * the greeter session. But they destroy the greeter session
822 * after the user-session and want the user-session to take
823 * over the VT. We need to support this for
824 * backwards-compatibility, so make sure we allow new sessions
825 * on a VT that a greeter is running on. Furthermore, to allow
826 * re-logins, we have to allow a greeter to take over a used VT for
827 * the exact same reasons.
829 if (c
!= SESSION_GREETER
&&
831 vtnr
< m
->seat0
->position_count
&&
832 m
->seat0
->positions
[vtnr
] &&
833 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
834 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
836 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
837 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
838 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
841 (void) audit_session_from_pid(leader
, &audit_id
);
842 if (audit_session_is_valid(audit_id
)) {
843 /* Keep our session IDs and the audit session IDs in sync */
845 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
848 /* Wut? There's already a session by this name and we
849 * didn't find it above? Weird, then let's not trust
850 * the audit data and let's better register a new
852 if (hashmap_get(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_get(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
> 0 ? -errno
: -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
= strappend(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 if (!dirent_is_file(de
))
1383 if (!startswith(de
->d_name
, "72-seat-"))
1386 if (!endswith(de
->d_name
, ".rules"))
1389 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1390 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1394 return trigger_device(m
, NULL
);
1397 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1398 const char *sysfs
, *seat
;
1399 Manager
*m
= userdata
;
1405 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1409 if (!path_startswith(sysfs
, "/sys"))
1410 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1412 if (!seat_name_is_valid(seat
))
1413 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat %s is not valid", seat
);
1415 r
= bus_verify_polkit_async(
1418 "org.freedesktop.login1.attach-device",
1422 &m
->polkit_registry
,
1427 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1429 r
= attach_device(m
, seat
, sysfs
);
1433 return sd_bus_reply_method_return(message
, NULL
);
1436 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1437 Manager
*m
= userdata
;
1443 r
= sd_bus_message_read(message
, "b", &interactive
);
1447 r
= bus_verify_polkit_async(
1450 "org.freedesktop.login1.flush-devices",
1454 &m
->polkit_registry
,
1459 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1461 r
= flush_devices(m
);
1465 return sd_bus_reply_method_return(message
, NULL
);
1468 static int have_multiple_sessions(
1477 /* Check for other users' sessions. Greeter sessions do not
1478 * count, and non-login sessions do not count either. */
1479 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1480 if (session
->class == SESSION_USER
&&
1481 session
->user
->uid
!= uid
)
1487 static int bus_manager_log_shutdown(
1489 const char *unit_name
) {
1496 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1497 p
= "MESSAGE=System is powering down";
1498 q
= "SHUTDOWN=power-off";
1499 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1500 p
= "MESSAGE=System is rebooting";
1501 q
= "SHUTDOWN=reboot";
1502 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1503 p
= "MESSAGE=System is halting";
1504 q
= "SHUTDOWN=halt";
1505 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1506 p
= "MESSAGE=System is rebooting with kexec";
1507 q
= "SHUTDOWN=kexec";
1509 p
= "MESSAGE=System is shutting down";
1513 if (isempty(m
->wall_message
))
1514 p
= strjoina(p
, ".");
1516 p
= strjoina(p
, " (", m
->wall_message
, ").");
1518 return log_struct(LOG_NOTICE
,
1519 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1524 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1525 Manager
*m
= userdata
;
1530 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1534 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1539 if (until
<= now(CLOCK_MONOTONIC
))
1542 /* We want to ignore the lid switch for a while after each
1543 * suspend, and after boot-up. Hence let's install a timer for
1544 * this. As long as the event source exists we ignore the lid
1547 if (m
->lid_switch_ignore_event_source
) {
1550 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1557 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1559 r
= sd_event_add_time(
1561 &m
->lid_switch_ignore_event_source
,
1564 lid_switch_ignore_handler
, m
);
1569 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1570 int active
= _active
;
1573 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1575 return sd_bus_emit_signal(m
->bus
,
1576 "/org/freedesktop/login1",
1577 "org.freedesktop.login1.Manager",
1578 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1583 static int execute_shutdown_or_sleep(
1586 const char *unit_name
,
1587 sd_bus_error
*error
) {
1589 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1595 assert(w
< _INHIBIT_WHAT_MAX
);
1598 if (w
== INHIBIT_SHUTDOWN
)
1599 bus_manager_log_shutdown(m
, unit_name
);
1601 r
= sd_bus_call_method(
1603 "org.freedesktop.systemd1",
1604 "/org/freedesktop/systemd1",
1605 "org.freedesktop.systemd1.Manager",
1609 "ss", unit_name
, "replace-irreversibly");
1613 r
= sd_bus_message_read(reply
, "o", &p
);
1617 r
= free_and_strdup(&m
->action_job
, p
);
1621 m
->action_unit
= unit_name
;
1624 /* Make sure the lid switch is ignored for a while */
1625 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1630 /* Tell people that they now may take a lock again */
1631 (void) send_prepare_for(m
, w
, false);
1636 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1638 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1639 Inhibitor
*offending
= NULL
;
1644 if (manager
->action_what
== 0 || manager
->action_job
)
1647 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1648 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1653 (void) get_process_comm(offending
->pid
, &comm
);
1654 u
= uid_to_name(offending
->uid
);
1656 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1657 offending
->uid
, strna(u
),
1658 offending
->pid
, strna(comm
));
1661 /* Actually do the operation */
1662 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1664 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1665 bus_error_message(&error
, r
));
1667 manager
->action_unit
= NULL
;
1668 manager
->action_what
= 0;
1675 static int manager_inhibit_timeout_handler(
1680 Manager
*manager
= userdata
;
1684 assert(manager
->inhibit_timeout_source
== s
);
1686 r
= manager_dispatch_delayed(manager
, true);
1687 return (r
< 0) ? r
: 0;
1690 static int delay_shutdown_or_sleep(
1693 const char *unit_name
) {
1700 assert(w
< _INHIBIT_WHAT_MAX
);
1703 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1705 if (m
->inhibit_timeout_source
) {
1706 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1708 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1710 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1712 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1714 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1715 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1720 m
->action_unit
= unit_name
;
1726 int bus_manager_shutdown_or_sleep_now_or_later(
1728 const char *unit_name
,
1730 sd_bus_error
*error
) {
1732 _cleanup_free_
char *load_state
= NULL
;
1739 assert(w
< _INHIBIT_WHAT_MAX
);
1740 assert(!m
->action_job
);
1742 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1746 if (!streq(load_state
, "loaded"))
1747 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1748 "Unit %s is %s, refusing operation.",
1749 unit_name
, load_state
);
1751 /* Tell everybody to prepare for shutdown/sleep */
1752 (void) send_prepare_for(m
, w
, true);
1755 m
->inhibit_delay_max
> 0 &&
1756 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1759 /* Shutdown is delayed, keep in mind what we
1760 * want to do, and start a timeout */
1761 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1763 /* Shutdown is not delayed, execute it
1765 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1770 static int verify_shutdown_creds(
1772 sd_bus_message
*message
,
1776 const char *action_multiple_sessions
,
1777 const char *action_ignore_inhibit
,
1778 sd_bus_error
*error
) {
1780 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1781 bool multiple_sessions
, blocked
;
1788 assert(w
<= _INHIBIT_WHAT_MAX
);
1790 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1794 r
= sd_bus_creds_get_euid(creds
, &uid
);
1798 r
= have_multiple_sessions(m
, uid
);
1802 multiple_sessions
= r
> 0;
1803 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1805 if (multiple_sessions
&& action_multiple_sessions
) {
1806 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1810 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1813 if (blocked
&& action_ignore_inhibit
) {
1814 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1818 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1821 if (!multiple_sessions
&& !blocked
&& action
) {
1822 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1826 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1832 static int method_do_shutdown_or_sleep(
1834 sd_bus_message
*message
,
1835 const char *unit_name
,
1838 const char *action_multiple_sessions
,
1839 const char *action_ignore_inhibit
,
1840 const char *sleep_verb
,
1841 sd_bus_error
*error
) {
1849 assert(w
<= _INHIBIT_WHAT_MAX
);
1851 r
= sd_bus_message_read(message
, "b", &interactive
);
1855 /* Don't allow multiple jobs being executed at the same time */
1856 if (m
->action_what
> 0)
1857 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1858 "There's already a shutdown or sleep operation in progress");
1861 r
= can_sleep(sleep_verb
);
1863 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1864 "Not enough swap space for hibernation");
1866 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1867 "Sleep verb \"%s\" not supported", sleep_verb
);
1872 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1873 action_ignore_inhibit
, error
);
1877 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1881 return sd_bus_reply_method_return(message
, NULL
);
1884 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1885 Manager
*m
= userdata
;
1887 return method_do_shutdown_or_sleep(
1889 SPECIAL_POWEROFF_TARGET
,
1891 "org.freedesktop.login1.power-off",
1892 "org.freedesktop.login1.power-off-multiple-sessions",
1893 "org.freedesktop.login1.power-off-ignore-inhibit",
1898 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1899 Manager
*m
= userdata
;
1901 return method_do_shutdown_or_sleep(
1903 SPECIAL_REBOOT_TARGET
,
1905 "org.freedesktop.login1.reboot",
1906 "org.freedesktop.login1.reboot-multiple-sessions",
1907 "org.freedesktop.login1.reboot-ignore-inhibit",
1912 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1913 Manager
*m
= userdata
;
1915 return method_do_shutdown_or_sleep(
1917 SPECIAL_HALT_TARGET
,
1919 "org.freedesktop.login1.halt",
1920 "org.freedesktop.login1.halt-multiple-sessions",
1921 "org.freedesktop.login1.halt-ignore-inhibit",
1926 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1927 Manager
*m
= userdata
;
1929 return method_do_shutdown_or_sleep(
1931 SPECIAL_SUSPEND_TARGET
,
1933 "org.freedesktop.login1.suspend",
1934 "org.freedesktop.login1.suspend-multiple-sessions",
1935 "org.freedesktop.login1.suspend-ignore-inhibit",
1940 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1941 Manager
*m
= userdata
;
1943 return method_do_shutdown_or_sleep(
1945 SPECIAL_HIBERNATE_TARGET
,
1947 "org.freedesktop.login1.hibernate",
1948 "org.freedesktop.login1.hibernate-multiple-sessions",
1949 "org.freedesktop.login1.hibernate-ignore-inhibit",
1954 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1955 Manager
*m
= userdata
;
1957 return method_do_shutdown_or_sleep(
1959 SPECIAL_HYBRID_SLEEP_TARGET
,
1961 "org.freedesktop.login1.hibernate",
1962 "org.freedesktop.login1.hibernate-multiple-sessions",
1963 "org.freedesktop.login1.hibernate-ignore-inhibit",
1968 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1969 Manager
*m
= userdata
;
1971 return method_do_shutdown_or_sleep(
1973 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
1975 "org.freedesktop.login1.hibernate",
1976 "org.freedesktop.login1.hibernate-multiple-sessions",
1977 "org.freedesktop.login1.hibernate-ignore-inhibit",
1982 static int nologin_timeout_handler(
1987 Manager
*m
= userdata
;
1989 log_info("Creating /run/nologin, blocking further logins...");
1992 create_shutdown_run_nologin_or_warn() >= 0;
1997 static int update_schedule_file(Manager
*m
) {
1998 _cleanup_free_
char *temp_path
= NULL
;
1999 _cleanup_fclose_
FILE *f
= NULL
;
2004 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
2006 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2008 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
2010 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2012 (void) fchmod(fileno(f
), 0644);
2018 m
->scheduled_shutdown_timeout
,
2019 m
->enable_wall_messages
,
2020 m
->scheduled_shutdown_type
);
2022 if (!isempty(m
->wall_message
)) {
2023 _cleanup_free_
char *t
;
2025 t
= cescape(m
->wall_message
);
2031 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
2034 r
= fflush_and_check(f
);
2038 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2046 (void) unlink(temp_path
);
2047 (void) unlink("/run/systemd/shutdown/scheduled");
2049 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2052 static void reset_scheduled_shutdown(Manager
*m
) {
2055 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2056 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2057 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2059 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
2060 m
->scheduled_shutdown_timeout
= 0;
2061 m
->shutdown_dry_run
= false;
2063 if (m
->unlink_nologin
) {
2064 (void) unlink_or_warn("/run/nologin");
2065 m
->unlink_nologin
= false;
2068 (void) unlink("/run/systemd/shutdown/scheduled");
2071 static int manager_scheduled_shutdown_handler(
2076 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2077 Manager
*m
= userdata
;
2083 if (isempty(m
->scheduled_shutdown_type
))
2086 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2087 target
= SPECIAL_POWEROFF_TARGET
;
2088 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2089 target
= SPECIAL_REBOOT_TARGET
;
2090 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2091 target
= SPECIAL_HALT_TARGET
;
2093 assert_not_reached("unexpected shutdown type");
2095 /* Don't allow multiple jobs being executed at the same time */
2096 if (m
->action_what
> 0) {
2098 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2102 if (m
->shutdown_dry_run
) {
2103 /* We do not process delay inhibitors here. Otherwise, we
2104 * would have to be considered "in progress" (like the check
2105 * above) for some seconds after our admin has seen the final
2108 bus_manager_log_shutdown(m
, target
);
2109 log_info("Running in dry run, suppressing action.");
2110 reset_scheduled_shutdown(m
);
2115 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2117 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2124 reset_scheduled_shutdown(m
);
2128 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2129 Manager
*m
= userdata
;
2130 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2131 const char *action_multiple_sessions
= NULL
;
2132 const char *action_ignore_inhibit
= NULL
;
2133 const char *action
= NULL
;
2137 bool dry_run
= false;
2142 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2146 if (startswith(type
, "dry-")) {
2151 if (streq(type
, "poweroff")) {
2152 action
= "org.freedesktop.login1.power-off";
2153 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2154 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2155 } else if (streq(type
, "reboot")) {
2156 action
= "org.freedesktop.login1.reboot";
2157 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2158 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2159 } else if (streq(type
, "halt")) {
2160 action
= "org.freedesktop.login1.halt";
2161 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2162 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2164 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2166 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2167 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2171 if (m
->scheduled_shutdown_timeout_source
) {
2172 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2174 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2176 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2178 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2180 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2181 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2183 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2186 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2188 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2192 m
->shutdown_dry_run
= dry_run
;
2194 if (m
->nologin_timeout_source
) {
2195 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2197 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2199 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2201 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2203 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2204 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2206 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2209 m
->scheduled_shutdown_timeout
= elapse
;
2211 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2213 const char *tty
= NULL
;
2215 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2216 (void) sd_bus_creds_get_tty(creds
, &tty
);
2218 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2220 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2225 r
= manager_setup_wall_message_timer(m
);
2229 r
= update_schedule_file(m
);
2233 return sd_bus_reply_method_return(message
, NULL
);
2236 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2237 Manager
*m
= userdata
;
2243 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2244 reset_scheduled_shutdown(m
);
2246 if (cancelled
&& m
->enable_wall_messages
) {
2247 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2248 _cleanup_free_
char *username
= NULL
;
2249 const char *tty
= NULL
;
2253 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2255 (void) sd_bus_creds_get_uid(creds
, &uid
);
2256 (void) sd_bus_creds_get_tty(creds
, &tty
);
2259 username
= uid_to_name(uid
);
2260 utmp_wall("The system shutdown has been cancelled",
2261 username
, tty
, logind_wall_tty_filter
, m
);
2264 return sd_bus_reply_method_return(message
, "b", cancelled
);
2267 static int method_can_shutdown_or_sleep(
2269 sd_bus_message
*message
,
2272 const char *action_multiple_sessions
,
2273 const char *action_ignore_inhibit
,
2274 const char *sleep_verb
,
2275 sd_bus_error
*error
) {
2277 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2278 HandleAction handle
;
2279 bool multiple_sessions
, challenge
, blocked
;
2280 const char *result
= NULL
;
2287 assert(w
<= _INHIBIT_WHAT_MAX
);
2289 assert(action_multiple_sessions
);
2290 assert(action_ignore_inhibit
);
2293 r
= can_sleep(sleep_verb
);
2294 if (IN_SET(r
, 0, -ENOSPC
))
2295 return sd_bus_reply_method_return(message
, "s", "na");
2300 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2304 r
= sd_bus_creds_get_euid(creds
, &uid
);
2308 r
= have_multiple_sessions(m
, uid
);
2312 multiple_sessions
= r
> 0;
2313 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2315 handle
= handle_action_from_string(sleep_verb
);
2319 target
= manager_target_for_action(handle
);
2321 _cleanup_free_
char *load_state
= NULL
;
2323 r
= unit_load_state(m
->bus
, target
, &load_state
);
2327 if (!streq(load_state
, "loaded")) {
2334 if (multiple_sessions
) {
2335 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2342 result
= "challenge";
2348 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2355 } else if (challenge
) {
2356 if (!result
|| streq(result
, "yes"))
2357 result
= "challenge";
2362 if (!multiple_sessions
&& !blocked
) {
2363 /* If neither inhibit nor multiple sessions
2364 * apply then just check the normal policy */
2366 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2373 result
= "challenge";
2379 return sd_bus_reply_method_return(message
, "s", result
);
2382 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2383 Manager
*m
= userdata
;
2385 return method_can_shutdown_or_sleep(
2388 "org.freedesktop.login1.power-off",
2389 "org.freedesktop.login1.power-off-multiple-sessions",
2390 "org.freedesktop.login1.power-off-ignore-inhibit",
2395 static int method_can_reboot(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.reboot",
2402 "org.freedesktop.login1.reboot-multiple-sessions",
2403 "org.freedesktop.login1.reboot-ignore-inhibit",
2408 static int method_can_halt(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.halt",
2415 "org.freedesktop.login1.halt-multiple-sessions",
2416 "org.freedesktop.login1.halt-ignore-inhibit",
2421 static int method_can_suspend(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.suspend",
2428 "org.freedesktop.login1.suspend-multiple-sessions",
2429 "org.freedesktop.login1.suspend-ignore-inhibit",
2434 static int method_can_hibernate(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.hibernate",
2441 "org.freedesktop.login1.hibernate-multiple-sessions",
2442 "org.freedesktop.login1.hibernate-ignore-inhibit",
2447 static int method_can_hybrid_sleep(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_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2461 Manager
*m
= userdata
;
2463 return method_can_shutdown_or_sleep(
2466 "org.freedesktop.login1.hibernate",
2467 "org.freedesktop.login1.hibernate-multiple-sessions",
2468 "org.freedesktop.login1.hibernate-ignore-inhibit",
2469 "suspend-then-hibernate",
2473 static int property_get_reboot_parameter(
2476 const char *interface
,
2477 const char *property
,
2478 sd_bus_message
*reply
,
2480 sd_bus_error
*error
) {
2481 _cleanup_free_
char *parameter
= NULL
;
2488 r
= read_reboot_parameter(¶meter
);
2492 return sd_bus_message_append(reply
, "s", parameter
);
2495 static int method_set_reboot_parameter(
2496 sd_bus_message
*message
,
2498 sd_bus_error
*error
) {
2500 Manager
*m
= userdata
;
2507 r
= sd_bus_message_read(message
, "s", &arg
);
2511 r
= detect_container();
2515 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2516 "Reboot parameter not supported in containers, refusing.");
2518 r
= bus_verify_polkit_async(message
,
2520 "org.freedesktop.login1.set-reboot-parameter",
2524 &m
->polkit_registry
,
2529 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2531 r
= update_reboot_parameter_and_warn(arg
, false);
2535 return sd_bus_reply_method_return(message
, NULL
);
2538 static int method_can_reboot_parameter(
2539 sd_bus_message
*message
,
2541 sd_bus_error
*error
) {
2543 Manager
*m
= userdata
;
2549 r
= detect_container();
2552 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2553 return sd_bus_reply_method_return(message
, "s", "na");
2555 return return_test_polkit(
2558 "org.freedesktop.login1.set-reboot-parameter",
2564 static int property_get_reboot_to_firmware_setup(
2567 const char *interface
,
2568 const char *property
,
2569 sd_bus_message
*reply
,
2571 sd_bus_error
*error
) {
2578 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2580 /* EFI case: let's see what is currently configured in the EFI variables */
2581 r
= efi_get_reboot_to_firmware();
2582 if (r
< 0 && r
!= -EOPNOTSUPP
)
2583 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2585 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2587 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2588 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2589 if (errno
!= ENOENT
)
2590 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2597 return sd_bus_message_append(reply
, "b", r
> 0);
2600 static int method_set_reboot_to_firmware_setup(
2601 sd_bus_message
*message
,
2603 sd_bus_error
*error
) {
2605 Manager
*m
= userdata
;
2612 r
= sd_bus_message_read(message
, "b", &b
);
2616 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2618 /* EFI case: let's see what the firmware supports */
2620 r
= efi_reboot_to_firmware_supported();
2621 if (r
== -EOPNOTSUPP
)
2622 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2628 } else if (r
<= 0) {
2629 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2632 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2634 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2636 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2639 r
= bus_verify_polkit_async(message
,
2641 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2645 &m
->polkit_registry
,
2650 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2653 r
= efi_set_reboot_to_firmware(b
);
2658 r
= touch("/run/systemd/reboot-to-firmware-setup");
2662 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2667 return sd_bus_reply_method_return(message
, NULL
);
2670 static int method_can_reboot_to_firmware_setup(
2671 sd_bus_message
*message
,
2673 sd_bus_error
*error
) {
2675 Manager
*m
= userdata
;
2681 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2683 /* EFI case: let's see what the firmware supports */
2685 r
= efi_reboot_to_firmware_supported();
2687 if (r
!= -EOPNOTSUPP
)
2688 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2690 return sd_bus_reply_method_return(message
, "s", "na");
2693 } else if (r
<= 0) {
2694 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2697 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2699 return sd_bus_reply_method_return(message
, "s", "na");
2702 return return_test_polkit(
2705 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2711 static int property_get_reboot_to_boot_loader_menu(
2714 const char *interface
,
2715 const char *property
,
2716 sd_bus_message
*reply
,
2718 sd_bus_error
*error
) {
2720 uint64_t x
= UINT64_MAX
;
2727 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2729 _cleanup_free_
char *v
= NULL
;
2731 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2733 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2734 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2735 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2738 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", &v
);
2741 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable: %m");
2745 r
= safe_atou64(v
, &sec
);
2747 log_warning_errno(r
, "Failed to parse LoaderConfigTimeoutOneShot value '%s': %m", v
);
2748 else if (sec
> (USEC_INFINITY
/ USEC_PER_SEC
))
2749 log_warning("LoaderConfigTimeoutOneShot too large, ignoring: %m");
2751 x
= sec
* USEC_PER_SEC
; /* return in µs */
2755 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2757 _cleanup_free_
char *v
= NULL
;
2759 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2761 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2764 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2766 r
= safe_atou64(v
, &x
);
2768 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2772 return sd_bus_message_append(reply
, "t", x
);
2775 static int method_set_reboot_to_boot_loader_menu(
2776 sd_bus_message
*message
,
2778 sd_bus_error
*error
) {
2780 Manager
*m
= userdata
;
2788 r
= sd_bus_message_read(message
, "t", &x
);
2792 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2796 /* EFI case: let's see if booting into boot loader menu is supported. */
2798 r
= efi_loader_get_features(&features
);
2800 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2801 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2802 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2806 } else if (r
<= 0) {
2807 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2810 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2812 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2814 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2817 r
= bus_verify_polkit_async(message
,
2819 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2823 &m
->polkit_registry
,
2828 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2831 if (x
== UINT64_MAX
)
2832 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", NULL
, 0);
2834 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2835 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2837 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", buf
);
2842 if (x
== UINT64_MAX
) {
2843 if (unlink("/run/systemd/reboot-to-loader-menu") < 0 && errno
!= ENOENT
)
2846 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2848 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2850 r
= write_string_file_atomic_label("/run/systemd/reboot-to-loader-menu", buf
);
2856 return sd_bus_reply_method_return(message
, NULL
);
2859 static int method_can_reboot_to_boot_loader_menu(
2860 sd_bus_message
*message
,
2862 sd_bus_error
*error
) {
2864 Manager
*m
= userdata
;
2870 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2872 uint64_t features
= 0;
2874 /* EFI case, let's see if booting into boot loader menu is supported. */
2876 r
= efi_loader_get_features(&features
);
2878 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2879 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2880 return sd_bus_reply_method_return(message
, "s", "na");
2882 } else if (r
<= 0) {
2883 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2886 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2888 return sd_bus_reply_method_return(message
, "s", "na");
2891 return return_test_polkit(
2894 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2900 static int property_get_reboot_to_boot_loader_entry(
2903 const char *interface
,
2904 const char *property
,
2905 sd_bus_message
*reply
,
2907 sd_bus_error
*error
) {
2909 _cleanup_free_
char *v
= NULL
;
2916 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2918 /* EFI case: let's read the LoaderEntryOneShot variable */
2920 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &v
);
2923 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable: %m");
2924 } else if (!efi_loader_entry_name_valid(v
)) {
2925 log_warning("LoaderEntryOneShot contains invalid entry name '%s', ignoring.", v
);
2929 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2932 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2934 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2937 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry: %m");
2938 } else if (!efi_loader_entry_name_valid(v
)) {
2939 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2944 return sd_bus_message_append(reply
, "s", v
);
2947 static int boot_loader_entry_exists(const char *id
) {
2948 _cleanup_(boot_config_free
) BootConfig config
= {};
2953 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2954 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2957 (void) boot_entries_augment_from_loader(&config
, true);
2959 return boot_config_has_entry(&config
, id
);
2962 static int method_set_reboot_to_boot_loader_entry(
2963 sd_bus_message
*message
,
2965 sd_bus_error
*error
) {
2967 Manager
*m
= userdata
;
2975 r
= sd_bus_message_read(message
, "s", &v
);
2981 else if (efi_loader_entry_name_valid(v
)) {
2982 r
= boot_loader_entry_exists(v
);
2986 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
2988 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
2990 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2994 /* EFI case: let's see if booting into boot loader entry is supported. */
2996 r
= efi_loader_get_features(&features
);
2998 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
2999 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3000 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3004 } else if (r
<= 0) {
3005 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3008 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3010 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3012 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3015 r
= bus_verify_polkit_async(message
,
3017 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3021 &m
->polkit_registry
,
3026 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3031 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", NULL
, 0);
3033 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", v
);
3038 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3041 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3047 return sd_bus_reply_method_return(message
, NULL
);
3050 static int method_can_reboot_to_boot_loader_entry(
3051 sd_bus_message
*message
,
3053 sd_bus_error
*error
) {
3055 Manager
*m
= userdata
;
3061 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3063 uint64_t features
= 0;
3065 /* EFI case, let's see if booting into boot loader entry is supported. */
3067 r
= efi_loader_get_features(&features
);
3069 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3070 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3071 return sd_bus_reply_method_return(message
, "s", "na");
3073 } else if (r
<= 0) {
3074 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3077 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3079 return sd_bus_reply_method_return(message
, "s", "na");
3082 return return_test_polkit(
3085 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3091 static int property_get_boot_loader_entries(
3094 const char *interface
,
3095 const char *property
,
3096 sd_bus_message
*reply
,
3098 sd_bus_error
*error
) {
3100 _cleanup_(boot_config_free
) BootConfig config
= {};
3108 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3109 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3112 (void) boot_entries_augment_from_loader(&config
, true);
3114 r
= sd_bus_message_open_container(reply
, 'a', "s");
3118 for (i
= 0; i
< config
.n_entries
; i
++) {
3119 BootEntry
*e
= config
.entries
+ i
;
3121 r
= sd_bus_message_append(reply
, "s", e
->id
);
3126 return sd_bus_message_close_container(reply
);
3129 static int method_set_wall_message(
3130 sd_bus_message
*message
,
3132 sd_bus_error
*error
) {
3135 Manager
*m
= userdata
;
3137 unsigned enable_wall_messages
;
3142 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3146 r
= bus_verify_polkit_async(message
,
3148 "org.freedesktop.login1.set-wall-message",
3152 &m
->polkit_registry
,
3157 return 1; /* Will call us back */
3159 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3163 m
->enable_wall_messages
= enable_wall_messages
;
3165 return sd_bus_reply_method_return(message
, NULL
);
3168 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3169 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3170 const char *who
, *why
, *what
, *mode
;
3171 _cleanup_free_
char *id
= NULL
;
3172 _cleanup_close_
int fifo_fd
= -1;
3173 Manager
*m
= userdata
;
3174 Inhibitor
*i
= NULL
;
3184 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3188 w
= inhibit_what_from_string(what
);
3190 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3191 "Invalid what specification %s", what
);
3193 mm
= inhibit_mode_from_string(mode
);
3195 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3196 "Invalid mode specification %s", mode
);
3198 /* Delay is only supported for shutdown/sleep */
3199 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3200 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3201 "Delay inhibitors only supported for shutdown and sleep");
3203 /* Don't allow taking delay locks while we are already
3204 * executing the operation. We shouldn't create the impression
3205 * that the lock was successful if the machine is about to go
3206 * down/suspend any moment. */
3207 if (m
->action_what
& w
)
3208 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3209 "The operation inhibition has been requested for is already running");
3211 r
= bus_verify_polkit_async(
3214 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3215 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3216 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3217 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3218 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3219 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3220 "org.freedesktop.login1.inhibit-handle-lid-switch",
3224 &m
->polkit_registry
,
3229 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3231 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3235 r
= sd_bus_creds_get_euid(creds
, &uid
);
3239 r
= sd_bus_creds_get_pid(creds
, &pid
);
3243 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3244 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3245 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3251 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3254 } while (hashmap_get(m
->inhibitors
, id
));
3256 r
= manager_add_inhibitor(m
, id
, &i
);
3264 i
->why
= strdup(why
);
3265 i
->who
= strdup(who
);
3267 if (!i
->why
|| !i
->who
) {
3272 fifo_fd
= inhibitor_create_fifo(i
);
3280 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3289 const sd_bus_vtable manager_vtable
[] = {
3290 SD_BUS_VTABLE_START(0),
3292 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3293 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3295 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3296 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3297 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3298 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3299 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3300 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3301 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3302 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3303 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3304 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3305 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3306 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3307 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3308 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3309 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3310 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3311 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3312 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3313 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3314 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3315 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3316 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3317 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3318 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3319 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3320 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3321 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3322 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3323 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3324 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3325 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3326 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3327 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3328 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3329 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3330 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3331 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3332 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3334 SD_BUS_METHOD("GetSession", "s", "o", method_get_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3335 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
3336 SD_BUS_METHOD("GetUser", "u", "o", method_get_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3337 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
3338 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3339 SD_BUS_METHOD("ListSessions", NULL
, "a(susso)", method_list_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3340 SD_BUS_METHOD("ListUsers", NULL
, "a(uso)", method_list_users
, SD_BUS_VTABLE_UNPRIVILEGED
),
3341 SD_BUS_METHOD("ListSeats", NULL
, "a(so)", method_list_seats
, SD_BUS_VTABLE_UNPRIVILEGED
),
3342 SD_BUS_METHOD("ListInhibitors", NULL
, "a(ssssuu)", method_list_inhibitors
, SD_BUS_VTABLE_UNPRIVILEGED
),
3343 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session
, 0),
3344 SD_BUS_METHOD("ReleaseSession", "s", NULL
, method_release_session
, 0),
3345 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3346 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL
, method_activate_session_on_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3347 SD_BUS_METHOD("LockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3348 SD_BUS_METHOD("UnlockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3349 SD_BUS_METHOD("LockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3350 SD_BUS_METHOD("UnlockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3351 SD_BUS_METHOD("KillSession", "ssi", NULL
, method_kill_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3352 SD_BUS_METHOD("KillUser", "ui", NULL
, method_kill_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3353 SD_BUS_METHOD("TerminateSession", "s", NULL
, method_terminate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3354 SD_BUS_METHOD("TerminateUser", "u", NULL
, method_terminate_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3355 SD_BUS_METHOD("TerminateSeat", "s", NULL
, method_terminate_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3356 SD_BUS_METHOD("SetUserLinger", "ubb", NULL
, method_set_user_linger
, SD_BUS_VTABLE_UNPRIVILEGED
),
3357 SD_BUS_METHOD("AttachDevice", "ssb", NULL
, method_attach_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
3358 SD_BUS_METHOD("FlushDevices", "b", NULL
, method_flush_devices
, SD_BUS_VTABLE_UNPRIVILEGED
),
3359 SD_BUS_METHOD("PowerOff", "b", NULL
, method_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
3360 SD_BUS_METHOD("Reboot", "b", NULL
, method_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
3361 SD_BUS_METHOD("Halt", "b", NULL
, method_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
3362 SD_BUS_METHOD("Suspend", "b", NULL
, method_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
3363 SD_BUS_METHOD("Hibernate", "b", NULL
, method_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3364 SD_BUS_METHOD("HybridSleep", "b", NULL
, method_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
3365 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL
, method_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3366 SD_BUS_METHOD("CanPowerOff", NULL
, "s", method_can_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
3367 SD_BUS_METHOD("CanReboot", NULL
, "s", method_can_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
3368 SD_BUS_METHOD("CanHalt", NULL
, "s", method_can_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
3369 SD_BUS_METHOD("CanSuspend", NULL
, "s", method_can_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
3370 SD_BUS_METHOD("CanHibernate", NULL
, "s", method_can_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3371 SD_BUS_METHOD("CanHybridSleep", NULL
, "s", method_can_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
3372 SD_BUS_METHOD("CanSuspendThenHibernate", NULL
, "s", method_can_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3373 SD_BUS_METHOD("ScheduleShutdown", "st", NULL
, method_schedule_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
3374 SD_BUS_METHOD("CancelScheduledShutdown", NULL
, "b", method_cancel_scheduled_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
3375 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit
, SD_BUS_VTABLE_UNPRIVILEGED
),
3376 SD_BUS_METHOD("CanRebootParameter", NULL
, "s", method_can_reboot_parameter
, SD_BUS_VTABLE_UNPRIVILEGED
),
3377 SD_BUS_METHOD("SetRebootParameter", "s", NULL
, method_set_reboot_parameter
, SD_BUS_VTABLE_UNPRIVILEGED
),
3378 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL
, "s", method_can_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
3379 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL
, method_set_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
3380 SD_BUS_METHOD("CanRebootToBootLoaderMenu", NULL
, "s", method_can_reboot_to_boot_loader_menu
, SD_BUS_VTABLE_UNPRIVILEGED
),
3381 SD_BUS_METHOD("SetRebootToBootLoaderMenu", "t", NULL
, method_set_reboot_to_boot_loader_menu
, SD_BUS_VTABLE_UNPRIVILEGED
),
3382 SD_BUS_METHOD("CanRebootToBootLoaderEntry", NULL
, "s", method_can_reboot_to_boot_loader_entry
, SD_BUS_VTABLE_UNPRIVILEGED
),
3383 SD_BUS_METHOD("SetRebootToBootLoaderEntry", "s", NULL
, method_set_reboot_to_boot_loader_entry
, SD_BUS_VTABLE_UNPRIVILEGED
),
3384 SD_BUS_METHOD("SetWallMessage", "sb", NULL
, method_set_wall_message
, SD_BUS_VTABLE_UNPRIVILEGED
),
3386 SD_BUS_SIGNAL("SessionNew", "so", 0),
3387 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
3388 SD_BUS_SIGNAL("UserNew", "uo", 0),
3389 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
3390 SD_BUS_SIGNAL("SeatNew", "so", 0),
3391 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
3392 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
3393 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
3398 static int session_jobs_reply(Session
*s
, const char *unit
, const char *result
) {
3405 if (result
&& !streq(result
, "done")) {
3406 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3408 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3409 "Start job for unit '%s' failed with '%s'", unit
, result
);
3410 return session_send_create_reply(s
, &e
);
3413 return session_send_create_reply(s
, NULL
);
3416 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3417 const char *path
, *result
, *unit
;
3418 Manager
*m
= userdata
;
3427 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3429 bus_log_parse_error(r
);
3433 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3434 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
3436 /* Tell people that they now may take a lock again */
3437 (void) send_prepare_for(m
, m
->action_what
, false);
3439 m
->action_job
= mfree(m
->action_job
);
3440 m
->action_unit
= NULL
;
3445 session
= hashmap_get(m
->session_units
, unit
);
3447 if (streq_ptr(path
, session
->scope_job
)) {
3448 session
->scope_job
= mfree(session
->scope_job
);
3449 (void) session_jobs_reply(session
, unit
, result
);
3451 session_save(session
);
3452 user_save(session
->user
);
3455 session_add_to_gc_queue(session
);
3458 user
= hashmap_get(m
->user_units
, unit
);
3460 if (streq_ptr(path
, user
->service_job
)) {
3461 user
->service_job
= mfree(user
->service_job
);
3463 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3464 (void) session_jobs_reply(session
, unit
, NULL
/* don't propagate user service failures to the client */);
3469 user_add_to_gc_queue(user
);
3475 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3476 const char *path
, *unit
;
3477 Manager
*m
= userdata
;
3485 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3487 bus_log_parse_error(r
);
3491 session
= hashmap_get(m
->session_units
, unit
);
3493 session_add_to_gc_queue(session
);
3495 user
= hashmap_get(m
->user_units
, unit
);
3497 user_add_to_gc_queue(user
);
3502 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3503 _cleanup_free_
char *unit
= NULL
;
3504 Manager
*m
= userdata
;
3513 path
= sd_bus_message_get_path(message
);
3517 r
= unit_name_from_dbus_path(path
, &unit
);
3518 if (r
== -EINVAL
) /* not a unit */
3525 session
= hashmap_get(m
->session_units
, unit
);
3527 session_add_to_gc_queue(session
);
3529 user
= hashmap_get(m
->user_units
, unit
);
3531 user_add_to_gc_queue(user
);
3536 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3537 Manager
*m
= userdata
;
3545 r
= sd_bus_message_read(message
, "b", &b
);
3547 bus_log_parse_error(r
);
3554 /* systemd finished reloading, let's recheck all our sessions */
3555 log_debug("System manager has been reloaded, rechecking sessions...");
3557 HASHMAP_FOREACH(session
, m
->sessions
, i
)
3558 session_add_to_gc_queue(session
);
3563 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3568 l
= strv_from_stdarg_alloca(property
);
3570 return sd_bus_emit_properties_changed_strv(
3572 "/org/freedesktop/login1",
3573 "org.freedesktop.login1.Manager",
3577 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3582 r
= sd_bus_message_read(reply
, "o", &j
);
3594 int manager_start_scope(
3599 const char *description
,
3602 const char *requires_mounts_for
,
3603 sd_bus_message
*more_properties
,
3604 sd_bus_error
*error
,
3607 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3616 r
= sd_bus_message_new_method_call(
3619 "org.freedesktop.systemd1",
3620 "/org/freedesktop/systemd1",
3621 "org.freedesktop.systemd1.Manager",
3622 "StartTransientUnit");
3626 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3630 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3634 if (!isempty(slice
)) {
3635 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3640 if (!isempty(description
)) {
3641 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
3646 STRV_FOREACH(i
, wants
) {
3647 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
3652 STRV_FOREACH(i
, after
) {
3653 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3658 if (!empty_or_root(requires_mounts_for
)) {
3659 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3664 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3666 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3670 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3674 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3675 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
3677 return bus_log_create_error(r
);
3679 if (more_properties
) {
3680 /* If TasksMax also appears here, it will overwrite the default value set above */
3681 r
= sd_bus_message_copy(m
, more_properties
, true);
3686 r
= sd_bus_message_close_container(m
);
3690 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3694 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3698 return strdup_job(reply
, job
);
3701 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3702 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3709 r
= sd_bus_call_method(
3711 "org.freedesktop.systemd1",
3712 "/org/freedesktop/systemd1",
3713 "org.freedesktop.systemd1.Manager",
3717 "ss", unit
, "replace");
3721 return strdup_job(reply
, job
);
3724 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3725 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3732 r
= sd_bus_call_method(
3734 "org.freedesktop.systemd1",
3735 "/org/freedesktop/systemd1",
3736 "org.freedesktop.systemd1.Manager",
3740 "ss", unit
, "fail");
3742 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3743 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
3746 sd_bus_error_free(error
);
3753 return strdup_job(reply
, job
);
3756 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
3757 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3758 _cleanup_free_
char *path
= NULL
;
3764 path
= unit_dbus_path_from_name(scope
);
3768 r
= sd_bus_call_method(
3770 "org.freedesktop.systemd1",
3772 "org.freedesktop.systemd1.Scope",
3778 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3779 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
3780 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
3783 sd_bus_error_move(ret_error
, &error
);
3790 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
3794 return sd_bus_call_method(
3796 "org.freedesktop.systemd1",
3797 "/org/freedesktop/systemd1",
3798 "org.freedesktop.systemd1.Manager",
3802 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
3805 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
3806 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3807 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3808 _cleanup_free_
char *path
= NULL
;
3815 path
= unit_dbus_path_from_name(unit
);
3819 r
= sd_bus_get_property(
3821 "org.freedesktop.systemd1",
3823 "org.freedesktop.systemd1.Unit",
3829 /* systemd might have dropped off momentarily, let's
3830 * not make this an error */
3831 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3832 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3835 /* If the unit is already unloaded then it's not
3837 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3838 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
3841 sd_bus_error_move(ret_error
, &error
);
3845 r
= sd_bus_message_read(reply
, "s", &state
);
3849 return !STR_IN_SET(state
, "inactive", "failed");
3852 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
3853 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3854 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3860 r
= sd_bus_get_property(
3862 "org.freedesktop.systemd1",
3864 "org.freedesktop.systemd1.Job",
3870 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3871 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3874 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
3877 sd_bus_error_move(ret_error
, &error
);
3881 /* We don't actually care about the state really. The fact
3882 * that we could read the job state is enough for us */