1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "audit-util.h"
13 #include "bus-common-errors.h"
14 #include "bus-error.h"
15 #include "bus-get-properties.h"
16 #include "bus-locator.h"
17 #include "bus-polkit.h"
18 #include "bus-unit-util.h"
20 #include "cgroup-util.h"
21 #include "device-util.h"
22 #include "dirent-util.h"
24 #include "efi-loader.h"
29 #include "event-util.h"
31 #include "fileio-label.h"
33 #include "format-util.h"
35 #include "logind-action.h"
36 #include "logind-dbus.h"
37 #include "logind-polkit.h"
38 #include "logind-seat-dbus.h"
39 #include "logind-session-dbus.h"
40 #include "logind-user-dbus.h"
42 #include "missing_capability.h"
43 #include "mkdir-label.h"
44 #include "parse-util.h"
45 #include "path-util.h"
46 #include "process-util.h"
47 #include "reboot-util.h"
48 #include "selinux-util.h"
49 #include "sleep-config.h"
51 #include "serialize.h"
52 #include "signal-util.h"
53 #include "stdio-util.h"
55 #include "terminal-util.h"
56 #include "tmpfile-util.h"
57 #include "unit-name.h"
58 #include "user-util.h"
59 #include "utmp-wtmp.h"
63 /* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1))
64 * character limit for the wall message.
65 * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72
66 * There is no real technical need for that but doesn't make sense
67 * to store arbitrary amounts either. As we are not stingy here, we
70 #define WALL_MESSAGE_MAX 4096U
72 #define SHUTDOWN_SCHEDULE_FILE "/run/systemd/shutdown/scheduled"
74 static void reset_scheduled_shutdown(Manager
*m
);
76 static int get_sender_session(
78 sd_bus_message
*message
,
83 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
84 Session
*session
= NULL
;
88 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
89 * and returns that. If not and 'consult_display' is true, this returns the display session of the
90 * owning user of the caller. */
92 r
= sd_bus_query_sender_creds(message
,
93 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
94 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
98 r
= sd_bus_creds_get_session(creds
, &name
);
103 if (consult_display
) {
106 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
113 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
115 session
= user
->display
;
119 session
= hashmap_get(m
->sessions
, name
);
122 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
124 "Caller does not belong to any known session and doesn't own any suitable session." :
125 "Caller does not belong to any known session.");
131 int manager_get_session_from_creds(
133 sd_bus_message
*message
,
143 if (SESSION_IS_SELF(name
)) /* the caller's own session */
144 return get_sender_session(m
, message
, false, error
, ret
);
145 if (SESSION_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
146 return get_sender_session(m
, message
, true, error
, ret
);
148 session
= hashmap_get(m
->sessions
, name
);
150 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
156 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
157 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
162 /* Note that we get the owner UID of the session, not the actual client UID here! */
163 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
167 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
174 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
177 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
178 "Caller does not belong to any logged in or lingering user");
184 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
190 if (!uid_is_valid(uid
))
191 return get_sender_user(m
, message
, error
, ret
);
193 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
195 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
196 "User ID "UID_FMT
" is not logged in or lingering", uid
);
202 int manager_get_seat_from_creds(
204 sd_bus_message
*message
,
215 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
218 /* Use these special seat names as session names */
219 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
223 seat
= session
->seat
;
225 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
227 seat
= hashmap_get(m
->seats
, name
);
229 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
236 static int return_test_polkit(
237 sd_bus_message
*message
,
239 const char **details
,
247 r
= bus_test_polkit(message
, action
, details
, good_user
, &challenge
, e
);
254 result
= "challenge";
258 return sd_bus_reply_method_return(message
, "s", result
);
261 static int property_get_idle_hint(
264 const char *interface
,
265 const char *property
,
266 sd_bus_message
*reply
,
268 sd_bus_error
*error
) {
270 Manager
*m
= ASSERT_PTR(userdata
);
275 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
278 static int property_get_idle_since_hint(
281 const char *interface
,
282 const char *property
,
283 sd_bus_message
*reply
,
285 sd_bus_error
*error
) {
287 Manager
*m
= ASSERT_PTR(userdata
);
288 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
293 manager_get_idle_hint(m
, &t
);
295 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
298 static int property_get_inhibited(
301 const char *interface
,
302 const char *property
,
303 sd_bus_message
*reply
,
305 sd_bus_error
*error
) {
307 Manager
*m
= ASSERT_PTR(userdata
);
313 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
315 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
318 static int property_get_preparing(
321 const char *interface
,
322 const char *property
,
323 sd_bus_message
*reply
,
325 sd_bus_error
*error
) {
327 Manager
*m
= ASSERT_PTR(userdata
);
333 if (m
->delayed_action
) {
334 if (streq(property
, "PreparingForShutdown"))
335 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SHUTDOWN
;
337 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SLEEP
;
340 return sd_bus_message_append(reply
, "b", b
);
343 static int property_get_sleep_operations(
346 const char *interface
,
347 const char *property
,
348 sd_bus_message
*reply
,
350 sd_bus_error
*error
) {
352 Manager
*m
= ASSERT_PTR(userdata
);
353 _cleanup_strv_free_
char **actions
= NULL
;
359 r
= handle_action_get_enabled_sleep_actions(m
->handle_action_sleep_mask
, &actions
);
363 return sd_bus_message_append_strv(reply
, actions
);
366 static int property_get_scheduled_shutdown(
369 const char *interface
,
370 const char *property
,
371 sd_bus_message
*reply
,
373 sd_bus_error
*error
) {
375 Manager
*m
= ASSERT_PTR(userdata
);
381 r
= sd_bus_message_open_container(reply
, 'r', "st");
385 r
= sd_bus_message_append(
387 handle_action_to_string(m
->scheduled_shutdown_action
),
388 m
->scheduled_shutdown_timeout
);
392 return sd_bus_message_close_container(reply
);
395 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
396 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
397 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
398 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power());
399 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
400 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
402 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
403 _cleanup_free_
char *p
= NULL
;
404 Manager
*m
= ASSERT_PTR(userdata
);
411 r
= sd_bus_message_read(message
, "s", &name
);
415 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
419 p
= session_bus_path(session
);
423 return sd_bus_reply_method_return(message
, "o", p
);
426 /* Get login session of a process. This is not what you are looking for these days,
427 * as apps may instead belong to a user service unit. This includes terminal
428 * emulators and hence command-line apps. */
429 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
430 Manager
*m
= ASSERT_PTR(userdata
);
431 _cleanup_free_
char *p
= NULL
;
432 Session
*session
= NULL
;
438 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
440 r
= sd_bus_message_read(message
, "u", &pid
);
447 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
451 r
= manager_get_session_by_pidref(m
, &PIDREF_MAKE_FROM_PID(pid
), &session
);
456 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
457 "PID "PID_FMT
" does not belong to any known session", pid
);
460 p
= session_bus_path(session
);
464 return sd_bus_reply_method_return(message
, "o", p
);
467 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
468 _cleanup_free_
char *p
= NULL
;
469 Manager
*m
= ASSERT_PTR(userdata
);
476 r
= sd_bus_message_read(message
, "u", &uid
);
480 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
484 p
= user_bus_path(user
);
488 return sd_bus_reply_method_return(message
, "o", p
);
491 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
492 _cleanup_free_
char *p
= NULL
;
493 Manager
*m
= ASSERT_PTR(userdata
);
500 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
502 r
= sd_bus_message_read(message
, "u", &pid
);
509 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
513 r
= manager_get_user_by_pid(m
, pid
, &user
);
517 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
518 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
522 p
= user_bus_path(user
);
526 return sd_bus_reply_method_return(message
, "o", p
);
529 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
530 _cleanup_free_
char *p
= NULL
;
531 Manager
*m
= ASSERT_PTR(userdata
);
538 r
= sd_bus_message_read(message
, "s", &name
);
542 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
546 p
= seat_bus_path(seat
);
550 return sd_bus_reply_method_return(message
, "o", p
);
553 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
554 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
555 Manager
*m
= ASSERT_PTR(userdata
);
561 r
= sd_bus_message_new_method_return(message
, &reply
);
565 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
569 HASHMAP_FOREACH(session
, m
->sessions
) {
570 _cleanup_free_
char *p
= NULL
;
572 p
= session_bus_path(session
);
576 r
= sd_bus_message_append(reply
, "(susso)",
578 (uint32_t) session
->user
->user_record
->uid
,
579 session
->user
->user_record
->user_name
,
580 session
->seat
? session
->seat
->id
: "",
586 r
= sd_bus_message_close_container(reply
);
590 return sd_bus_send(NULL
, reply
, NULL
);
593 static int method_list_sessions_ex(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
594 Manager
*m
= ASSERT_PTR(userdata
);
595 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
600 r
= sd_bus_message_new_method_return(message
, &reply
);
604 r
= sd_bus_message_open_container(reply
, 'a', "(sussussbto)");
609 HASHMAP_FOREACH(s
, m
->sessions
) {
610 _cleanup_free_
char *path
= NULL
;
611 dual_timestamp idle_ts
;
616 path
= session_bus_path(s
);
620 r
= session_get_idle_hint(s
, &idle_ts
);
625 r
= sd_bus_message_append(reply
, "(sussussbto)",
627 (uint32_t) s
->user
->user_record
->uid
,
628 s
->user
->user_record
->user_name
,
629 s
->seat
? s
->seat
->id
: "",
630 (uint32_t) s
->leader
.pid
,
631 session_class_to_string(s
->class),
640 r
= sd_bus_message_close_container(reply
);
644 return sd_bus_send(NULL
, reply
, NULL
);
647 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
648 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
649 Manager
*m
= ASSERT_PTR(userdata
);
655 r
= sd_bus_message_new_method_return(message
, &reply
);
659 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
663 HASHMAP_FOREACH(user
, m
->users
) {
664 _cleanup_free_
char *p
= NULL
;
666 p
= user_bus_path(user
);
670 r
= sd_bus_message_append(reply
, "(uso)",
671 (uint32_t) user
->user_record
->uid
,
672 user
->user_record
->user_name
,
678 r
= sd_bus_message_close_container(reply
);
682 return sd_bus_send(NULL
, reply
, NULL
);
685 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
686 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
687 Manager
*m
= ASSERT_PTR(userdata
);
693 r
= sd_bus_message_new_method_return(message
, &reply
);
697 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
701 HASHMAP_FOREACH(seat
, m
->seats
) {
702 _cleanup_free_
char *p
= NULL
;
704 p
= seat_bus_path(seat
);
708 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
713 r
= sd_bus_message_close_container(reply
);
717 return sd_bus_send(NULL
, reply
, NULL
);
720 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
721 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
722 Manager
*m
= ASSERT_PTR(userdata
);
723 Inhibitor
*inhibitor
;
728 r
= sd_bus_message_new_method_return(message
, &reply
);
732 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
736 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
) {
738 r
= sd_bus_message_append(reply
, "(ssssuu)",
739 strempty(inhibit_what_to_string(inhibitor
->what
)),
740 strempty(inhibitor
->who
),
741 strempty(inhibitor
->why
),
742 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
743 (uint32_t) inhibitor
->uid
,
744 (uint32_t) inhibitor
->pid
.pid
);
749 r
= sd_bus_message_close_container(reply
);
753 return sd_bus_send(NULL
, reply
, NULL
);
756 static int create_session(
757 sd_bus_message
*message
,
772 const char *remote_user
,
773 const char *remote_host
,
776 _cleanup_(pidref_done
) PidRef leader
= PIDREF_NULL
;
777 Manager
*m
= ASSERT_PTR(userdata
);
778 _cleanup_free_
char *id
= NULL
;
779 Session
*session
= NULL
;
780 uint32_t audit_id
= 0;
789 if (!uid_is_valid(uid
))
790 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
793 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Flags must be zero.");
795 if (leader_pidfd
>= 0)
796 r
= pidref_set_pidfd(&leader
, leader_pidfd
);
797 else if (leader_pid
== 0)
798 r
= bus_query_sender_pidref(message
, &leader
);
801 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Leader PID is not valid");
803 r
= pidref_set_pid(&leader
, leader_pid
);
808 if (leader
.pid
== 1 || leader
.pid
== getpid_cached())
809 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
812 t
= _SESSION_TYPE_INVALID
;
814 t
= session_type_from_string(type
);
816 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
817 "Invalid session type %s", type
);
821 c
= _SESSION_CLASS_INVALID
;
823 c
= session_class_from_string(class);
825 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
826 "Invalid session class %s", class);
829 if (isempty(desktop
))
832 if (!string_is_safe(desktop
))
833 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
834 "Invalid desktop string %s", desktop
);
840 seat
= hashmap_get(m
->seats
, cseat
);
842 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
843 "No seat '%s' known", cseat
);
846 if (tty_is_vc(tty
)) {
851 else if (seat
!= m
->seat0
)
852 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
853 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
855 v
= vtnr_from_tty(tty
);
857 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
858 "Cannot determine VT number from virtual console TTY %s", tty
);
862 else if (vtnr
!= (uint32_t) v
)
863 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
864 "Specified TTY and VT number do not match");
866 } else if (tty_is_console(tty
)) {
870 else if (seat
!= m
->seat0
)
871 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
872 "Console TTY specified but seat is not seat0");
875 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
876 "Console TTY specified but VT number is not 0");
880 if (seat_has_vts(seat
)) {
881 if (vtnr
<= 0 || vtnr
> 63)
882 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
883 "VT number out of range");
886 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
887 "Seat has no VTs but VT number not 0");
891 if (t
== _SESSION_TYPE_INVALID
) {
892 if (!isempty(display
))
894 else if (!isempty(tty
))
897 t
= SESSION_UNSPECIFIED
;
900 if (c
== _SESSION_CLASS_INVALID
) {
901 if (t
== SESSION_UNSPECIFIED
)
902 c
= SESSION_BACKGROUND
;
907 /* Check if we are already in a logind session, and if so refuse. */
908 r
= manager_get_session_by_pidref(m
, &leader
, /* ret_session= */ NULL
);
912 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
913 "Already running in a session or user slice");
915 /* Old gdm and lightdm start the user-session on the same VT as the greeter session. But they destroy
916 * the greeter session after the user-session and want the user-session to take over the VT. We need
917 * to support this for backwards-compatibility, so make sure we allow new sessions on a VT that a
918 * greeter is running on. Furthermore, to allow re-logins, we have to allow a greeter to take over a
919 * used VT for the exact same reasons. */
920 if (c
!= SESSION_GREETER
&&
922 vtnr
< MALLOC_ELEMENTSOF(m
->seat0
->positions
) &&
923 m
->seat0
->positions
[vtnr
] &&
924 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
925 return sd_bus_error_set(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
927 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
928 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
929 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
932 (void) audit_session_from_pid(leader
.pid
, &audit_id
);
933 if (audit_session_is_valid(audit_id
)) {
934 /* Keep our session IDs and the audit session IDs in sync */
936 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
939 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
940 * not trust the audit data and let's better register a new ID */
941 if (hashmap_contains(m
->sessions
, id
)) {
942 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
943 audit_id
= AUDIT_SESSION_INVALID
;
952 if (asprintf(&id
, "c%" PRIu64
, ++m
->session_counter
) < 0)
955 } while (hashmap_contains(m
->sessions
, id
));
958 /* The generated names should not clash with 'auto' or 'self' */
959 assert(!SESSION_IS_SELF(id
));
960 assert(!SESSION_IS_AUTO(id
));
962 /* If we are not watching utmp already, try again */
963 manager_reconnect_utmp(m
);
965 r
= manager_add_user_by_uid(m
, uid
, &user
);
969 r
= manager_add_session(m
, id
, &session
);
973 session_set_user(session
, user
);
974 r
= session_set_leader_consume(session
, TAKE_PIDREF(leader
));
978 session
->original_type
= session
->type
= t
;
979 session
->remote
= remote
;
980 session
->vtnr
= vtnr
;
983 /* Once the first session that is of a pinning class shows up we'll change the GC mode for the user
984 * from USER_GC_BY_ANY to USER_GC_BY_PIN, so that the user goes away once the last pinning session
985 * goes away. Background: we want that user@.service – when started manually – remains around (which
986 * itself is a non-pinning session), but gets stopped when the last pinning session goes away. */
988 if (SESSION_CLASS_PIN_USER(c
))
989 user
->gc_mode
= USER_GC_BY_PIN
;
992 r
= strdup_to(&session
->tty
, tty
);
996 session
->tty_validity
= TTY_FROM_PAM
;
999 if (!isempty(display
)) {
1000 r
= strdup_to(&session
->display
, display
);
1005 if (!isempty(remote_user
)) {
1006 r
= strdup_to(&session
->remote_user
, remote_user
);
1011 if (!isempty(remote_host
)) {
1012 r
= strdup_to(&session
->remote_host
, remote_host
);
1017 if (!isempty(service
)) {
1018 r
= strdup_to(&session
->service
, service
);
1023 if (!isempty(desktop
)) {
1024 r
= strdup_to(&session
->desktop
, desktop
);
1030 r
= seat_attach_session(seat
, session
);
1035 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1039 r
= session_start(session
, message
, error
);
1043 r
= sd_bus_message_exit_container(message
);
1047 session
->create_message
= sd_bus_message_ref(message
);
1049 /* Now call into session_send_create_reply(), which will reply to this method call for us. Or it
1050 * won't – in case we just spawned a session scope and/or user service manager, and they aren't ready
1051 * yet. We'll call session_create_reply() again once the session scope or the user service manager is
1052 * ready, where the function will check again if a reply is then ready to be sent, and then do so if
1053 * all is complete - or wait again. */
1054 r
= session_send_create_reply(session
, /* error= */ NULL
);
1062 session_add_to_gc_queue(session
);
1065 user_add_to_gc_queue(user
);
1070 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1071 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1079 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
1080 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
1082 r
= sd_bus_message_read(
1101 return create_session(
1107 /* pidfd = */ -EBADF
,
1122 static int method_create_session_pidfd(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1123 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1127 int leader_fd
= -EBADF
, remote
, r
;
1129 r
= sd_bus_message_read(
1149 return create_session(
1154 /* leader_pid = */ 0,
1170 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1171 Manager
*m
= ASSERT_PTR(userdata
);
1178 r
= sd_bus_message_read(message
, "s", &name
);
1182 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1186 r
= session_release(session
);
1190 return sd_bus_reply_method_return(message
, NULL
);
1193 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1194 Manager
*m
= ASSERT_PTR(userdata
);
1201 r
= sd_bus_message_read(message
, "s", &name
);
1205 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1209 /* PolicyKit is done by bus_session_method_activate() */
1211 return bus_session_method_activate(message
, session
, error
);
1214 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1215 const char *session_name
, *seat_name
;
1216 Manager
*m
= ASSERT_PTR(userdata
);
1223 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1225 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1229 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1233 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1237 if (session
->seat
!= seat
)
1238 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1239 "Session %s not on seat %s", session_name
, seat_name
);
1241 r
= check_polkit_chvt(message
, m
, error
);
1245 return 1; /* Will call us back */
1247 r
= session_activate(session
);
1251 return sd_bus_reply_method_return(message
, NULL
);
1254 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1255 Manager
*m
= ASSERT_PTR(userdata
);
1262 r
= sd_bus_message_read(message
, "s", &name
);
1266 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1270 return bus_session_method_lock(message
, session
, error
);
1273 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1274 Manager
*m
= ASSERT_PTR(userdata
);
1279 r
= bus_verify_polkit_async(
1281 "org.freedesktop.login1.lock-sessions",
1282 /* details= */ NULL
,
1283 &m
->polkit_registry
,
1288 return 1; /* Will call us back */
1290 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1294 return sd_bus_reply_method_return(message
, NULL
);
1297 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1299 Manager
*m
= ASSERT_PTR(userdata
);
1305 r
= sd_bus_message_read(message
, "s", &name
);
1309 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1313 return bus_session_method_kill(message
, session
, error
);
1316 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1317 Manager
*m
= ASSERT_PTR(userdata
);
1324 r
= sd_bus_message_read(message
, "u", &uid
);
1328 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1332 return bus_user_method_kill(message
, user
, error
);
1335 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1336 Manager
*m
= ASSERT_PTR(userdata
);
1343 r
= sd_bus_message_read(message
, "s", &name
);
1347 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1351 return bus_session_method_terminate(message
, session
, error
);
1354 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1355 Manager
*m
= ASSERT_PTR(userdata
);
1362 r
= sd_bus_message_read(message
, "u", &uid
);
1366 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1370 return bus_user_method_terminate(message
, user
, error
);
1373 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1374 Manager
*m
= ASSERT_PTR(userdata
);
1381 r
= sd_bus_message_read(message
, "s", &name
);
1385 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1389 return bus_seat_method_terminate(message
, seat
, error
);
1392 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1393 Manager
*m
= ASSERT_PTR(userdata
);
1394 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1395 uint32_t uid
, auth_uid
;
1396 int r
, enable
, interactive
;
1400 r
= sd_bus_message_read(message
, "ubb", &uid
, &enable
, &interactive
);
1404 r
= sd_bus_query_sender_creds(message
,
1405 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
,
1410 if (!uid_is_valid(uid
)) {
1411 /* Note that we get the owner UID of the session or user unit, not the actual client UID here! */
1412 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1417 /* owner_uid is racy, so for authorization we must use euid */
1418 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1422 _cleanup_free_
struct passwd
*pw
= NULL
;
1424 r
= getpwuid_malloc(uid
, &pw
);
1428 r
= bus_verify_polkit_async_full(
1430 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1431 "org.freedesktop.login1.set-user-linger",
1432 /* details= */ NULL
,
1433 /* good_user= */ UID_INVALID
,
1434 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
1435 &m
->polkit_registry
,
1440 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1442 (void) mkdir_p_label("/var/lib/systemd", 0755);
1443 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1447 _cleanup_free_
char *escaped
= NULL
;
1451 escaped
= cescape(pw
->pw_name
);
1455 path
= strjoina("/var/lib/systemd/linger/", escaped
);
1462 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0) {
1465 user_add_to_gc_queue(u
);
1472 if (r
< 0 && errno
!= ENOENT
)
1475 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1477 user_add_to_gc_queue(u
);
1480 return sd_bus_reply_method_return(message
, NULL
);
1483 static int trigger_device(Manager
*m
, sd_device
*parent
) {
1484 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1489 r
= sd_device_enumerator_new(&e
);
1493 r
= sd_device_enumerator_allow_uninitialized(e
);
1498 r
= sd_device_enumerator_add_match_parent(e
, parent
);
1503 FOREACH_DEVICE(e
, d
) {
1504 r
= sd_device_trigger(d
, SD_DEVICE_CHANGE
);
1506 log_device_debug_errno(d
, r
, "Failed to trigger device, ignoring: %m");
1512 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
, sd_bus_error
*error
) {
1513 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1514 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1515 const char *id_for_seat
;
1522 r
= sd_device_new_from_syspath(&d
, sysfs
);
1524 return sd_bus_error_set_errnof(error
, r
, "Failed to open device '%s': %m", sysfs
);
1526 if (sd_device_has_current_tag(d
, "seat") <= 0)
1527 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'seat' udev tag.", sysfs
);
1529 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1530 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'ID_FOR_SEAT' udev property.", sysfs
);
1532 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1535 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1538 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1539 r
= write_string_file_atomic_label(file
, rule
);
1543 return trigger_device(m
, d
);
1546 static int flush_devices(Manager
*m
) {
1547 _cleanup_closedir_
DIR *d
= NULL
;
1551 d
= opendir("/etc/udev/rules.d");
1553 if (errno
!= ENOENT
)
1554 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1556 FOREACH_DIRENT_ALL(de
, d
, break) {
1557 if (!dirent_is_file(de
))
1560 if (!startswith(de
->d_name
, "72-seat-"))
1563 if (!endswith(de
->d_name
, ".rules"))
1566 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1567 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1570 return trigger_device(m
, NULL
);
1573 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1574 const char *sysfs
, *seat
;
1575 Manager
*m
= ASSERT_PTR(userdata
);
1580 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1584 if (!path_is_normalized(sysfs
))
1585 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1586 if (!path_startswith(sysfs
, "/sys"))
1587 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1589 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1592 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1598 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1599 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1601 r
= bus_verify_polkit_async_full(
1603 "org.freedesktop.login1.attach-device",
1604 /* details= */ NULL
,
1605 /* good_user= */ UID_INVALID
,
1606 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
1607 &m
->polkit_registry
,
1612 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1614 r
= attach_device(m
, seat
, sysfs
, error
);
1618 return sd_bus_reply_method_return(message
, NULL
);
1621 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1622 Manager
*m
= ASSERT_PTR(userdata
);
1627 r
= sd_bus_message_read(message
, "b", &interactive
);
1631 r
= bus_verify_polkit_async_full(
1633 "org.freedesktop.login1.flush-devices",
1634 /* details= */ NULL
,
1635 /* good_user= */ UID_INVALID
,
1636 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
1637 &m
->polkit_registry
,
1642 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1644 r
= flush_devices(m
);
1648 return sd_bus_reply_method_return(message
, NULL
);
1651 static int have_multiple_sessions(
1659 /* Check for other users' sessions. Greeter sessions do not
1660 * count, and non-login sessions do not count either. */
1661 HASHMAP_FOREACH(session
, m
->sessions
)
1662 if (IN_SET(session
->class, SESSION_USER
, SESSION_USER_EARLY
) &&
1663 session
->user
->user_record
->uid
!= uid
)
1669 static int bus_manager_log_shutdown(
1671 const HandleActionData
*a
) {
1675 const char *message
= a
->message
?: "System is shutting down";
1676 const char *log_verb
= a
->log_verb
? strjoina("SHUTDOWN=", a
->log_verb
) : NULL
;
1678 return log_struct(LOG_NOTICE
,
1679 "MESSAGE_ID=%s", a
->message_id
?: SD_MESSAGE_SHUTDOWN_STR
,
1680 LOG_MESSAGE("%s%s%s%s.",
1682 m
->wall_message
? " (" : "",
1683 strempty(m
->wall_message
),
1684 m
->wall_message
? ")" : ""),
1688 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1689 Manager
*m
= ASSERT_PTR(userdata
);
1693 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1697 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1702 if (until
<= now(CLOCK_MONOTONIC
))
1705 /* We want to ignore the lid switch for a while after each
1706 * suspend, and after boot-up. Hence let's install a timer for
1707 * this. As long as the event source exists we ignore the lid
1710 if (m
->lid_switch_ignore_event_source
) {
1713 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1720 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1722 r
= sd_event_add_time(
1724 &m
->lid_switch_ignore_event_source
,
1727 lid_switch_ignore_handler
, m
);
1732 static int send_prepare_for(Manager
*m
, const HandleActionData
*a
, bool _active
) {
1733 int k
= 0, r
, active
= _active
;
1737 assert(IN_SET(a
->inhibit_what
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1739 /* We need to send both old and new signal for backward compatibility. The newer one allows clients
1740 * to know which type of reboot is going to happen, as they might be doing different actions (e.g.:
1741 * on soft-reboot), and it is sent first, so that clients know that if they receive the old one
1742 * first then they don't have to wait for the new one, as it means it's not supported. So, do not
1743 * change the order here, as it is an API. */
1744 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
) {
1745 k
= sd_bus_emit_signal(m
->bus
,
1746 "/org/freedesktop/login1",
1747 "org.freedesktop.login1.Manager",
1748 "PrepareForShutdownWithMetadata",
1754 handle_action_to_string(a
->handle
));
1756 log_debug_errno(k
, "Failed to emit PrepareForShutdownWithMetadata(): %m");
1759 r
= sd_bus_emit_signal(m
->bus
,
1760 "/org/freedesktop/login1",
1761 "org.freedesktop.login1.Manager",
1762 a
->inhibit_what
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1766 log_debug_errno(r
, "Failed to emit PrepareForShutdown(): %m");
1768 return RET_GATHER(k
, r
);
1771 static int strdup_job(sd_bus_message
*reply
, char **ret
) {
1779 r
= sd_bus_message_read_basic(reply
, 'o', &j
);
1791 static int execute_shutdown_or_sleep(
1793 const HandleActionData
*a
,
1794 sd_bus_error
*error
) {
1796 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1800 assert(!m
->action_job
);
1803 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
)
1804 bus_manager_log_shutdown(m
, a
);
1806 r
= bus_call_method(
1812 "ss", a
->target
, "replace-irreversibly");
1816 r
= strdup_job(reply
, &m
->action_job
);
1820 m
->delayed_action
= a
;
1822 /* Make sure the lid switch is ignored for a while */
1823 manager_set_lid_switch_ignore(m
, usec_add(now(CLOCK_MONOTONIC
), m
->holdoff_timeout_usec
));
1828 /* Tell people that they now may take a lock again */
1829 (void) send_prepare_for(m
, a
, false);
1834 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1835 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1836 Inhibitor
*offending
= NULL
;
1841 if (!manager
->delayed_action
|| manager
->action_job
)
1844 if (manager_is_inhibited(manager
, manager
->delayed_action
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1845 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1850 (void) pidref_get_comm(&offending
->pid
, &comm
);
1851 u
= uid_to_name(offending
->uid
);
1853 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1854 offending
->uid
, strna(u
),
1855 offending
->pid
.pid
, strna(comm
));
1858 /* Actually do the operation */
1859 r
= execute_shutdown_or_sleep(manager
, manager
->delayed_action
, &error
);
1861 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1862 bus_error_message(&error
, r
));
1864 manager
->delayed_action
= NULL
;
1867 return 1; /* We did some work. */
1870 static int manager_inhibit_timeout_handler(
1875 Manager
*manager
= ASSERT_PTR(userdata
);
1877 assert(manager
->inhibit_timeout_source
== s
);
1879 return manager_dispatch_delayed(manager
, true);
1882 static int delay_shutdown_or_sleep(
1884 const HandleActionData
*a
) {
1891 if (m
->inhibit_timeout_source
) {
1892 r
= sd_event_source_set_time_relative(m
->inhibit_timeout_source
, m
->inhibit_delay_max
);
1894 return log_error_errno(r
, "sd_event_source_set_time_relative() failed: %m");
1896 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1898 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1900 r
= sd_event_add_time_relative(
1902 &m
->inhibit_timeout_source
,
1903 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, 0,
1904 manager_inhibit_timeout_handler
, m
);
1909 m
->delayed_action
= a
;
1914 int bus_manager_shutdown_or_sleep_now_or_later(
1916 const HandleActionData
*a
,
1917 sd_bus_error
*error
) {
1919 _cleanup_free_
char *load_state
= NULL
;
1925 assert(!m
->action_job
);
1927 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
1931 if (!streq(load_state
, "loaded"))
1932 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1933 "Unit %s is %s, refusing operation.",
1934 a
->target
, load_state
);
1936 /* Tell everybody to prepare for shutdown/sleep */
1937 (void) send_prepare_for(m
, a
, true);
1940 m
->inhibit_delay_max
> 0 &&
1941 manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1944 /* Shutdown is delayed, keep in mind what we
1945 * want to do, and start a timeout */
1946 r
= delay_shutdown_or_sleep(m
, a
);
1948 /* Shutdown is not delayed, execute it
1950 r
= execute_shutdown_or_sleep(m
, a
, error
);
1955 static int verify_shutdown_creds(
1957 sd_bus_message
*message
,
1958 const HandleActionData
*a
,
1960 sd_bus_error
*error
) {
1962 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1963 bool multiple_sessions
, blocked
, interactive
;
1971 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1975 r
= sd_bus_creds_get_euid(creds
, &uid
);
1979 r
= have_multiple_sessions(m
, uid
);
1983 multiple_sessions
= r
> 0;
1984 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1985 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
1987 if (multiple_sessions
) {
1988 r
= bus_verify_polkit_async_full(
1990 a
->polkit_action_multiple_sessions
,
1991 /* details= */ NULL
,
1992 /* good_user= */ UID_INVALID
,
1993 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
1994 &m
->polkit_registry
,
1999 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2003 /* We don't check polkit for root here, because you can't be more privileged than root */
2004 if (uid
== 0 && (flags
& SD_LOGIND_ROOT_CHECK_INHIBITORS
))
2005 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
,
2006 "Access denied to root due to active block inhibitor");
2008 r
= bus_verify_polkit_async_full(
2010 a
->polkit_action_ignore_inhibit
,
2011 /* details= */ NULL
,
2012 /* good_user= */ UID_INVALID
,
2013 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
2014 &m
->polkit_registry
,
2019 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2022 if (!multiple_sessions
&& !blocked
) {
2023 r
= bus_verify_polkit_async_full(
2026 /* details= */ NULL
,
2027 /* good_user= */ UID_INVALID
,
2028 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
2029 &m
->polkit_registry
,
2034 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2040 static int setup_wall_message_timer(Manager
*m
, sd_bus_message
* message
) {
2041 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2044 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2046 const char *tty
= NULL
;
2048 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2049 (void) sd_bus_creds_get_tty(creds
, &tty
);
2051 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2056 r
= manager_setup_wall_message_timer(m
);
2063 static int method_do_shutdown_or_sleep(
2065 sd_bus_message
*message
,
2066 HandleAction action
,
2068 sd_bus_error
*error
) {
2075 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2078 /* New style method: with flags parameter (and interactive bool in the bus message header) */
2079 r
= sd_bus_message_read(message
, "t", &flags
);
2082 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
2083 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2084 "Invalid flags parameter");
2086 if (FLAGS_SET(flags
, (SD_LOGIND_REBOOT_VIA_KEXEC
|SD_LOGIND_SOFT_REBOOT
)))
2087 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2088 "Both reboot via kexec and soft reboot selected, which is not supported");
2090 if (action
!= HANDLE_REBOOT
) {
2091 if (flags
& SD_LOGIND_REBOOT_VIA_KEXEC
)
2092 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2093 "Reboot via kexec option is only applicable with reboot operations");
2094 if ((flags
& SD_LOGIND_SOFT_REBOOT
) || (flags
& SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
))
2095 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2096 "Soft reboot option is only applicable with reboot operations");
2099 /* Old style method: no flags parameter, but interactive bool passed as boolean in
2100 * payload. Let's convert this argument to the new-style flags parameter for our internal
2104 r
= sd_bus_message_read(message
, "b", &interactive
);
2108 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
2111 const HandleActionData
*a
= NULL
;
2113 if ((flags
& SD_LOGIND_SOFT_REBOOT
) ||
2114 ((flags
& SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
) && path_is_os_tree("/run/nextroot") > 0))
2115 a
= handle_action_lookup(HANDLE_SOFT_REBOOT
);
2116 else if ((flags
& SD_LOGIND_REBOOT_VIA_KEXEC
) && kexec_loaded())
2117 a
= handle_action_lookup(HANDLE_KEXEC
);
2119 if (action
== HANDLE_SLEEP
) {
2120 HandleAction selected
;
2122 selected
= handle_action_sleep_select(m
);
2124 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2125 "None of the configured sleep operations are supported");
2127 assert_se(a
= handle_action_lookup(selected
));
2129 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2130 SleepSupport support
;
2132 assert_se(a
= handle_action_lookup(action
));
2134 assert(a
->sleep_operation
>= 0);
2135 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2137 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2143 case SLEEP_DISABLED
:
2144 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2145 "Sleep verb '%s' is disabled by config",
2146 sleep_operation_to_string(a
->sleep_operation
));
2148 case SLEEP_NOT_CONFIGURED
:
2149 case SLEEP_STATE_OR_MODE_NOT_SUPPORTED
:
2150 case SLEEP_ALARM_NOT_SUPPORTED
:
2151 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2152 "Sleep verb '%s' is not configured or configuration is not supported by kernel",
2153 sleep_operation_to_string(a
->sleep_operation
));
2155 case SLEEP_RESUME_NOT_SUPPORTED
:
2156 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2157 "Not running on EFI and resume= is not set. No available method to resume from hibernation");
2159 case SLEEP_NOT_ENOUGH_SWAP_SPACE
:
2160 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2161 "Not enough suitable swap space for hibernation available on compatible block devices and file systems");
2164 assert_not_reached();
2168 assert_se(a
= handle_action_lookup(action
));
2170 r
= verify_shutdown_creds(m
, message
, a
, flags
, error
);
2174 if (m
->delayed_action
)
2175 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
2176 "Action %s already in progress, refusing requested %s operation.",
2177 handle_action_to_string(m
->delayed_action
->handle
),
2178 handle_action_to_string(a
->handle
));
2180 /* reset case we're shorting a scheduled shutdown */
2181 m
->unlink_nologin
= false;
2182 reset_scheduled_shutdown(m
);
2184 m
->scheduled_shutdown_timeout
= 0;
2185 m
->scheduled_shutdown_action
= action
;
2187 (void) setup_wall_message_timer(m
, message
);
2189 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, error
);
2193 return sd_bus_reply_method_return(message
, NULL
);
2196 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2197 Manager
*m
= userdata
;
2199 return method_do_shutdown_or_sleep(
2202 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
2206 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2207 Manager
*m
= userdata
;
2209 return method_do_shutdown_or_sleep(
2212 sd_bus_message_is_method_call(message
, NULL
, "RebootWithFlags"),
2216 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2217 Manager
*m
= userdata
;
2219 return method_do_shutdown_or_sleep(
2222 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
2226 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2227 Manager
*m
= userdata
;
2229 return method_do_shutdown_or_sleep(
2232 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
2236 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2237 Manager
*m
= userdata
;
2239 return method_do_shutdown_or_sleep(
2242 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
2246 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2247 Manager
*m
= userdata
;
2249 return method_do_shutdown_or_sleep(
2251 HANDLE_HYBRID_SLEEP
,
2252 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
2256 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2257 Manager
*m
= userdata
;
2259 return method_do_shutdown_or_sleep(
2261 HANDLE_SUSPEND_THEN_HIBERNATE
,
2262 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
2266 static int method_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2267 Manager
*m
= userdata
;
2269 return method_do_shutdown_or_sleep(
2272 /* with_flags = */ true,
2276 static int nologin_timeout_handler(
2281 Manager
*m
= ASSERT_PTR(userdata
);
2283 log_info("Creating /run/nologin, blocking further logins...");
2286 create_shutdown_run_nologin_or_warn() >= 0;
2291 static usec_t
nologin_timeout_usec(usec_t elapse
) {
2292 /* Issue /run/nologin five minutes before shutdown */
2293 return LESS_BY(elapse
, 5 * USEC_PER_MINUTE
);
2296 static void reset_scheduled_shutdown(Manager
*m
) {
2299 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2300 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2301 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2303 m
->scheduled_shutdown_action
= _HANDLE_ACTION_INVALID
;
2304 m
->scheduled_shutdown_timeout
= USEC_INFINITY
;
2305 m
->scheduled_shutdown_uid
= UID_INVALID
;
2306 m
->scheduled_shutdown_tty
= mfree(m
->scheduled_shutdown_tty
);
2307 m
->shutdown_dry_run
= false;
2309 if (m
->unlink_nologin
) {
2310 (void) unlink_or_warn("/run/nologin");
2311 m
->unlink_nologin
= false;
2314 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2317 static int update_schedule_file(Manager
*m
) {
2318 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
2319 _cleanup_fclose_
FILE *f
= NULL
;
2323 assert(handle_action_valid(m
->scheduled_shutdown_action
));
2325 r
= mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE
, 0755);
2327 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2329 r
= fopen_temporary(SHUTDOWN_SCHEDULE_FILE
, &f
, &temp_path
);
2331 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2333 (void) fchmod(fileno(f
), 0644);
2335 serialize_usec(f
, "USEC", m
->scheduled_shutdown_timeout
);
2336 serialize_item_format(f
, "WARN_WALL", "%s", one_zero(m
->enable_wall_messages
));
2337 serialize_item_format(f
, "MODE", "%s", handle_action_to_string(m
->scheduled_shutdown_action
));
2338 serialize_item_format(f
, "UID", UID_FMT
, m
->scheduled_shutdown_uid
);
2340 if (m
->scheduled_shutdown_tty
)
2341 serialize_item_format(f
, "TTY", "%s", m
->scheduled_shutdown_tty
);
2343 if (!isempty(m
->wall_message
)) {
2344 r
= serialize_item_escaped(f
, "WALL_MESSAGE", m
->wall_message
);
2349 r
= fflush_and_check(f
);
2353 if (rename(temp_path
, SHUTDOWN_SCHEDULE_FILE
) < 0) {
2358 temp_path
= mfree(temp_path
);
2362 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2364 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2367 static int manager_scheduled_shutdown_handler(
2372 Manager
*m
= ASSERT_PTR(userdata
);
2373 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2374 const HandleActionData
*a
;
2377 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2379 /* Don't allow multiple jobs being executed at the same time */
2380 if (m
->delayed_action
) {
2381 r
= log_error_errno(SYNTHETIC_ERRNO(EALREADY
),
2382 "Scheduled shutdown to %s failed: shutdown or sleep operation already in progress.",
2387 if (m
->shutdown_dry_run
) {
2388 /* We do not process delay inhibitors here. Otherwise, we
2389 * would have to be considered "in progress" (like the check
2390 * above) for some seconds after our admin has seen the final
2393 bus_manager_log_shutdown(m
, a
);
2394 log_info("Running in dry run, suppressing action.");
2395 reset_scheduled_shutdown(m
);
2400 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, &error
);
2402 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", a
->target
);
2409 reset_scheduled_shutdown(m
);
2413 static int manager_setup_shutdown_timers(Manager
* m
) {
2418 r
= event_reset_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2420 m
->scheduled_shutdown_timeout
, 0,
2421 manager_scheduled_shutdown_handler
, m
,
2422 0, "scheduled-shutdown-timeout", true);
2426 r
= event_reset_time(m
->event
, &m
->nologin_timeout_source
,
2428 nologin_timeout_usec(m
->scheduled_shutdown_timeout
), 0,
2429 nologin_timeout_handler
, m
,
2430 0, "nologin-timeout", true);
2437 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2438 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2443 void manager_load_scheduled_shutdown(Manager
*m
) {
2444 _cleanup_fclose_
FILE *f
= NULL
;
2445 _cleanup_free_
char *usec
= NULL
,
2448 *wall_message
= NULL
,
2455 r
= parse_env_file(f
, SHUTDOWN_SCHEDULE_FILE
,
2457 "WARN_WALL", &warn_wall
,
2459 "WALL_MESSAGE", &wall_message
,
2463 /* reset will delete the file */
2464 reset_scheduled_shutdown(m
);
2469 return (void) log_debug_errno(r
, "Failed to parse " SHUTDOWN_SCHEDULE_FILE
": %m");
2471 HandleAction handle
= handle_action_from_string(mode
);
2473 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse scheduled shutdown type: %s", mode
);
2476 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "USEC is required");
2477 if (deserialize_usec(usec
, &m
->scheduled_shutdown_timeout
) < 0)
2480 /* assign parsed type only after we know usec is also valid */
2481 m
->scheduled_shutdown_action
= handle
;
2484 r
= parse_boolean(warn_wall
);
2486 log_debug_errno(r
, "Failed to parse enabling wall messages");
2488 m
->enable_wall_messages
= r
;
2492 _cleanup_free_
char *unescaped
= NULL
;
2493 r
= cunescape(wall_message
, 0, &unescaped
);
2495 log_debug_errno(r
, "Failed to parse wall message: %s", wall_message
);
2497 free_and_replace(m
->wall_message
, unescaped
);
2501 r
= parse_uid(uid
, &m
->scheduled_shutdown_uid
);
2503 log_debug_errno(r
, "Failed to parse wall uid: %s", uid
);
2506 free_and_replace(m
->scheduled_shutdown_tty
, tty
);
2508 r
= manager_setup_shutdown_timers(m
);
2510 return reset_scheduled_shutdown(m
);
2512 (void) manager_setup_wall_message_timer(m
);
2513 (void) update_schedule_file(m
);
2518 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2519 Manager
*m
= ASSERT_PTR(userdata
);
2520 HandleAction handle
;
2521 const HandleActionData
*a
;
2525 bool dry_run
= false;
2529 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2533 if (startswith(type
, "dry-")) {
2538 handle
= handle_action_from_string(type
);
2539 if (!HANDLE_ACTION_IS_SHUTDOWN(handle
))
2540 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type: %s", type
);
2542 assert_se(a
= handle_action_lookup(handle
));
2543 assert(a
->polkit_action
);
2545 r
= verify_shutdown_creds(m
, message
, a
, 0, error
);
2549 m
->scheduled_shutdown_action
= handle
;
2550 m
->shutdown_dry_run
= dry_run
;
2551 m
->scheduled_shutdown_timeout
= elapse
;
2553 r
= manager_setup_shutdown_timers(m
);
2557 r
= setup_wall_message_timer(m
, message
);
2559 r
= update_schedule_file(m
);
2562 reset_scheduled_shutdown(m
);
2566 return sd_bus_reply_method_return(message
, NULL
);
2569 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2570 Manager
*m
= ASSERT_PTR(userdata
);
2571 const HandleActionData
*a
;
2577 cancelled
= handle_action_valid(m
->scheduled_shutdown_action
) && m
->scheduled_shutdown_action
!= HANDLE_IGNORE
;
2579 return sd_bus_reply_method_return(message
, "b", false);
2581 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2582 if (!a
->polkit_action
)
2583 return sd_bus_error_set(error
, SD_BUS_ERROR_AUTH_FAILED
, "Unsupported shutdown type");
2585 r
= bus_verify_polkit_async(
2588 /* details= */ NULL
,
2589 &m
->polkit_registry
,
2594 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2596 if (m
->enable_wall_messages
) {
2597 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2598 const char *tty
= NULL
;
2601 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2603 (void) sd_bus_creds_get_uid(creds
, &uid
);
2604 (void) sd_bus_creds_get_tty(creds
, &tty
);
2607 _cleanup_free_
char *username
= uid_to_name(uid
);
2609 log_struct(LOG_INFO
,
2610 LOG_MESSAGE("System shutdown has been cancelled"),
2611 "ACTION=%s", handle_action_to_string(a
->handle
),
2612 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_CANCELED_STR
,
2613 username
? "OPERATOR=%s" : NULL
, username
);
2615 (void) wall("System shutdown has been cancelled",
2616 username
, tty
, logind_wall_tty_filter
, m
);
2619 reset_scheduled_shutdown(m
);
2621 return sd_bus_reply_method_return(message
, "b", true);
2624 static int method_can_shutdown_or_sleep(
2626 sd_bus_message
*message
,
2627 HandleAction action
,
2628 sd_bus_error
*error
) {
2630 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2631 bool multiple_sessions
, challenge
, blocked
, check_unit_state
= true;
2632 const HandleActionData
*a
;
2633 const char *result
= NULL
;
2639 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2641 if (action
== HANDLE_SLEEP
) {
2642 HandleAction selected
;
2644 selected
= handle_action_sleep_select(m
);
2646 return sd_bus_reply_method_return(message
, "s", "na");
2648 check_unit_state
= false; /* Already handled by handle_action_sleep_select */
2650 assert_se(a
= handle_action_lookup(selected
));
2652 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2653 SleepSupport support
;
2655 assert_se(a
= handle_action_lookup(action
));
2657 assert(a
->sleep_operation
>= 0);
2658 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2660 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2664 return sd_bus_reply_method_return(message
, "s", support
== SLEEP_DISABLED
? "no" : "na");
2666 assert_se(a
= handle_action_lookup(action
));
2668 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2672 r
= sd_bus_creds_get_euid(creds
, &uid
);
2676 r
= have_multiple_sessions(m
, uid
);
2680 multiple_sessions
= r
> 0;
2681 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2683 if (check_unit_state
&& a
->target
) {
2684 _cleanup_free_
char *load_state
= NULL
;
2686 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
2690 if (!streq(load_state
, "loaded")) {
2696 if (multiple_sessions
) {
2697 r
= bus_test_polkit(
2699 a
->polkit_action_multiple_sessions
,
2700 /* details= */ NULL
,
2701 /* good_user= */ UID_INVALID
,
2710 result
= "challenge";
2716 r
= bus_test_polkit(
2718 a
->polkit_action_ignore_inhibit
,
2719 /* details= */ NULL
,
2720 /* good_user= */ UID_INVALID
,
2729 } else if (challenge
) {
2730 if (!result
|| streq(result
, "yes"))
2731 result
= "challenge";
2736 if (!multiple_sessions
&& !blocked
) {
2737 /* If neither inhibit nor multiple sessions
2738 * apply then just check the normal policy */
2740 r
= bus_test_polkit(
2743 /* details= */ NULL
,
2744 /* good_user= */ UID_INVALID
,
2753 result
= "challenge";
2759 return sd_bus_reply_method_return(message
, "s", result
);
2762 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2763 Manager
*m
= userdata
;
2765 return method_can_shutdown_or_sleep(m
, message
, HANDLE_POWEROFF
, error
);
2768 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2769 Manager
*m
= userdata
;
2771 return method_can_shutdown_or_sleep(m
, message
, HANDLE_REBOOT
, error
);
2774 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2775 Manager
*m
= userdata
;
2777 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HALT
, error
);
2780 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2781 Manager
*m
= userdata
;
2783 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND
, error
);
2786 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2787 Manager
*m
= userdata
;
2789 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HIBERNATE
, error
);
2792 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2793 Manager
*m
= userdata
;
2795 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HYBRID_SLEEP
, error
);
2798 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2799 Manager
*m
= userdata
;
2801 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND_THEN_HIBERNATE
, error
);
2804 static int method_can_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2805 Manager
*m
= userdata
;
2807 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SLEEP
, error
);
2810 static int property_get_reboot_parameter(
2813 const char *interface
,
2814 const char *property
,
2815 sd_bus_message
*reply
,
2817 sd_bus_error
*error
) {
2818 _cleanup_free_
char *parameter
= NULL
;
2825 r
= read_reboot_parameter(¶meter
);
2829 return sd_bus_message_append(reply
, "s", parameter
);
2832 static int method_set_reboot_parameter(
2833 sd_bus_message
*message
,
2835 sd_bus_error
*error
) {
2837 Manager
*m
= ASSERT_PTR(userdata
);
2843 r
= sd_bus_message_read(message
, "s", &arg
);
2847 r
= detect_container();
2851 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2852 "Reboot parameter not supported in containers, refusing.");
2854 r
= bus_verify_polkit_async(
2856 "org.freedesktop.login1.set-reboot-parameter",
2857 /* details= */ NULL
,
2858 &m
->polkit_registry
,
2863 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2865 r
= update_reboot_parameter_and_warn(arg
, false);
2869 return sd_bus_reply_method_return(message
, NULL
);
2872 static int method_can_reboot_parameter(
2873 sd_bus_message
*message
,
2875 sd_bus_error
*error
) {
2877 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2882 r
= detect_container();
2885 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2886 return sd_bus_reply_method_return(message
, "s", "na");
2888 return return_test_polkit(
2890 "org.freedesktop.login1.set-reboot-parameter",
2891 /* details= */ NULL
,
2892 /* good_user= */ UID_INVALID
,
2896 static int property_get_reboot_to_firmware_setup(
2899 const char *interface
,
2900 const char *property
,
2901 sd_bus_message
*reply
,
2903 sd_bus_error
*error
) {
2910 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2912 /* EFI case: let's see what is currently configured in the EFI variables */
2913 r
= efi_get_reboot_to_firmware();
2914 if (r
< 0 && r
!= -EOPNOTSUPP
)
2915 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2917 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2919 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2920 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2921 if (errno
!= ENOENT
)
2922 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2929 return sd_bus_message_append(reply
, "b", r
> 0);
2932 static int method_set_reboot_to_firmware_setup(
2933 sd_bus_message
*message
,
2935 sd_bus_error
*error
) {
2937 Manager
*m
= ASSERT_PTR(userdata
);
2943 r
= sd_bus_message_read(message
, "b", &b
);
2947 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2949 /* EFI case: let's see what the firmware supports */
2951 r
= efi_reboot_to_firmware_supported();
2952 if (r
== -EOPNOTSUPP
)
2953 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2959 } else if (r
<= 0) {
2960 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2963 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2965 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2967 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2970 r
= bus_verify_polkit_async(
2972 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2973 /* details= */ NULL
,
2974 &m
->polkit_registry
,
2979 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2982 r
= efi_set_reboot_to_firmware(b
);
2987 r
= touch("/run/systemd/reboot-to-firmware-setup");
2991 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2996 return sd_bus_reply_method_return(message
, NULL
);
2999 static int method_can_reboot_to_firmware_setup(
3000 sd_bus_message
*message
,
3002 sd_bus_error
*error
) {
3004 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3009 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
3011 /* EFI case: let's see what the firmware supports */
3013 r
= efi_reboot_to_firmware_supported();
3015 if (r
!= -EOPNOTSUPP
)
3016 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
3018 return sd_bus_reply_method_return(message
, "s", "na");
3021 } else if (r
<= 0) {
3022 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
3025 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
3027 return sd_bus_reply_method_return(message
, "s", "na");
3030 return return_test_polkit(
3032 "org.freedesktop.login1.set-reboot-to-firmware-setup",
3033 /* details= */ NULL
,
3034 /* good_user= */ UID_INVALID
,
3038 static int property_get_reboot_to_boot_loader_menu(
3041 const char *interface
,
3042 const char *property
,
3043 sd_bus_message
*reply
,
3045 sd_bus_error
*error
) {
3047 uint64_t x
= UINT64_MAX
;
3054 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3056 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished:
3058 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
3059 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
3060 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
3063 r
= efi_loader_get_config_timeout_one_shot(&x
);
3066 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
3070 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3072 _cleanup_free_
char *v
= NULL
;
3074 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
3076 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
3079 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
3081 r
= safe_atou64(v
, &x
);
3083 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
3087 return sd_bus_message_append(reply
, "t", x
);
3090 static int method_set_reboot_to_boot_loader_menu(
3091 sd_bus_message
*message
,
3093 sd_bus_error
*error
) {
3095 Manager
*m
= ASSERT_PTR(userdata
);
3102 r
= sd_bus_message_read(message
, "t", &x
);
3106 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3110 /* EFI case: let's see if booting into boot loader menu is supported. */
3112 r
= efi_loader_get_features(&features
);
3114 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3115 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3116 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3120 } else if (r
<= 0) {
3121 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
3124 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3126 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3128 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
3131 r
= bus_verify_polkit_async(
3133 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3134 /* details= */ NULL
,
3135 &m
->polkit_registry
,
3140 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3143 if (x
== UINT64_MAX
)
3144 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), NULL
, 0);
3146 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3147 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
3149 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), buf
);
3154 if (x
== UINT64_MAX
) {
3155 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
3158 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3160 xsprintf(buf
, "%" PRIu64
, x
); /* μs granularity */
3162 r
= write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf
);
3168 return sd_bus_reply_method_return(message
, NULL
);
3171 static int method_can_reboot_to_boot_loader_menu(
3172 sd_bus_message
*message
,
3174 sd_bus_error
*error
) {
3176 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3181 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3183 uint64_t features
= 0;
3185 /* EFI case, let's see if booting into boot loader menu is supported. */
3187 r
= efi_loader_get_features(&features
);
3189 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3190 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3191 return sd_bus_reply_method_return(message
, "s", "na");
3193 } else if (r
<= 0) {
3194 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
3197 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3199 return sd_bus_reply_method_return(message
, "s", "na");
3202 return return_test_polkit(
3204 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3205 /* details= */ NULL
,
3206 /* good_user= */ UID_INVALID
,
3210 static int property_get_reboot_to_boot_loader_entry(
3213 const char *interface
,
3214 const char *property
,
3215 sd_bus_message
*reply
,
3217 sd_bus_error
*error
) {
3219 _cleanup_free_
char *v
= NULL
;
3220 Manager
*m
= ASSERT_PTR(userdata
);
3221 const char *x
= NULL
;
3227 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3229 /* EFI case: let's read the LoaderEntryOneShot variable */
3231 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
3234 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
3236 x
= m
->efi_loader_entry_one_shot
;
3239 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3242 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
3244 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
3247 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
3248 } else if (!efi_loader_entry_name_valid(v
))
3249 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
3254 return sd_bus_message_append(reply
, "s", x
);
3257 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
3258 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3264 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3265 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
3268 r
= manager_read_efi_boot_loader_entries(m
);
3270 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3272 return !!boot_config_find_entry(&config
, id
);
3275 static int method_set_reboot_to_boot_loader_entry(
3276 sd_bus_message
*message
,
3278 sd_bus_error
*error
) {
3280 Manager
*m
= ASSERT_PTR(userdata
);
3287 r
= sd_bus_message_read(message
, "s", &v
);
3293 else if (efi_loader_entry_name_valid(v
)) {
3294 r
= boot_loader_entry_exists(m
, v
);
3298 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3300 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3302 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3306 /* EFI case: let's see if booting into boot loader entry is supported. */
3308 r
= efi_loader_get_features(&features
);
3310 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3311 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3312 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3316 } else if (r
<= 0) {
3317 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3320 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3322 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3324 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3327 r
= bus_verify_polkit_async(
3329 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3330 /* details= */ NULL
,
3331 &m
->polkit_registry
,
3336 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3341 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), NULL
, 0);
3343 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), v
);
3348 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3351 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3357 return sd_bus_reply_method_return(message
, NULL
);
3360 static int method_can_reboot_to_boot_loader_entry(
3361 sd_bus_message
*message
,
3363 sd_bus_error
*error
) {
3365 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3370 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3372 uint64_t features
= 0;
3374 /* EFI case, let's see if booting into boot loader entry is supported. */
3376 r
= efi_loader_get_features(&features
);
3378 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3379 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3380 return sd_bus_reply_method_return(message
, "s", "na");
3382 } else if (r
<= 0) {
3383 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3386 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3388 return sd_bus_reply_method_return(message
, "s", "na");
3391 return return_test_polkit(
3393 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3394 /* details= */ NULL
,
3395 /* good_user= */ UID_INVALID
,
3399 static int property_get_boot_loader_entries(
3402 const char *interface
,
3403 const char *property
,
3404 sd_bus_message
*reply
,
3406 sd_bus_error
*error
) {
3408 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3409 Manager
*m
= ASSERT_PTR(userdata
);
3416 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3417 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3420 r
= manager_read_efi_boot_loader_entries(m
);
3422 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3424 r
= sd_bus_message_open_container(reply
, 'a', "s");
3428 for (i
= 0; i
< config
.n_entries
; i
++) {
3429 BootEntry
*e
= config
.entries
+ i
;
3431 r
= sd_bus_message_append(reply
, "s", e
->id
);
3436 return sd_bus_message_close_container(reply
);
3439 static int method_set_wall_message(
3440 sd_bus_message
*message
,
3442 sd_bus_error
*error
) {
3445 Manager
*m
= ASSERT_PTR(userdata
);
3447 int enable_wall_messages
;
3451 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3455 if (strlen(wall_message
) > WALL_MESSAGE_MAX
)
3456 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3457 "Wall message too long, maximum permitted length is %u characters.",
3460 /* Short-circuit the operation if the desired state is already in place, to
3461 * avoid an unnecessary polkit permission check. */
3462 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3463 m
->enable_wall_messages
== enable_wall_messages
)
3466 r
= bus_verify_polkit_async(
3468 "org.freedesktop.login1.set-wall-message",
3469 /* details= */ NULL
,
3470 &m
->polkit_registry
,
3475 return 1; /* Will call us back */
3477 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3481 m
->enable_wall_messages
= enable_wall_messages
;
3484 return sd_bus_reply_method_return(message
, NULL
);
3487 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3488 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3489 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
3490 const char *who
, *why
, *what
, *mode
;
3491 _cleanup_free_
char *id
= NULL
;
3492 _cleanup_close_
int fifo_fd
= -EBADF
;
3493 Manager
*m
= ASSERT_PTR(userdata
);
3501 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3505 w
= inhibit_what_from_string(what
);
3507 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3508 "Invalid what specification %s", what
);
3510 mm
= inhibit_mode_from_string(mode
);
3512 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3513 "Invalid mode specification %s", mode
);
3515 /* Delay is only supported for shutdown/sleep */
3516 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3517 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3518 "Delay inhibitors only supported for shutdown and sleep");
3520 /* Don't allow taking delay locks while we are already
3521 * executing the operation. We shouldn't create the impression
3522 * that the lock was successful if the machine is about to go
3523 * down/suspend any moment. */
3524 if (m
->delayed_action
&& m
->delayed_action
->inhibit_what
& w
)
3525 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3526 "The operation inhibition has been requested for is already running");
3528 r
= bus_verify_polkit_async(
3530 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3531 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3532 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3533 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3534 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3535 w
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3536 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3537 "org.freedesktop.login1.inhibit-handle-lid-switch",
3538 /* details= */ NULL
,
3539 &m
->polkit_registry
,
3544 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3546 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
|SD_BUS_CREDS_PIDFD
, &creds
);
3550 r
= sd_bus_creds_get_euid(creds
, &uid
);
3554 r
= bus_creds_get_pidref(creds
, &pidref
);
3558 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3559 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3560 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3566 if (asprintf(&id
, "%" PRIu64
, ++m
->inhibit_counter
) < 0)
3569 } while (hashmap_get(m
->inhibitors
, id
));
3571 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3572 r
= manager_add_inhibitor(m
, id
, &i
);
3578 i
->pid
= TAKE_PIDREF(pidref
);
3580 i
->why
= strdup(why
);
3581 i
->who
= strdup(who
);
3583 if (!i
->why
|| !i
->who
)
3586 fifo_fd
= inhibitor_create_fifo(i
);
3590 r
= inhibitor_start(i
);
3595 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3598 static const sd_bus_vtable manager_vtable
[] = {
3599 SD_BUS_VTABLE_START(0),
3601 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool
, bus_property_set_bool
, offsetof(Manager
, enable_wall_messages
), 0),
3602 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3604 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3605 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3606 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3607 SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3608 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3609 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3610 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3611 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3612 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3613 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3614 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3615 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3616 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3617 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3618 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3619 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3620 SD_BUS_PROPERTY("SleepOperation", "as", property_get_sleep_operations
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3621 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3622 SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3623 SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3624 SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3625 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3626 SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3627 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3628 SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3629 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3630 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3631 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3632 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3633 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3634 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3635 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3636 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3637 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3638 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3639 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3640 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3641 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3642 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3643 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3644 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3645 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3646 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3647 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3648 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3649 SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL
, offsetof(Manager
, stop_idle_session_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3651 SD_BUS_METHOD_WITH_ARGS("GetSession",
3652 SD_BUS_ARGS("s", session_id
),
3653 SD_BUS_RESULT("o", object_path
),
3655 SD_BUS_VTABLE_UNPRIVILEGED
),
3656 SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
3657 SD_BUS_ARGS("u", pid
),
3658 SD_BUS_RESULT("o", object_path
),
3659 method_get_session_by_pid
,
3660 SD_BUS_VTABLE_UNPRIVILEGED
),
3661 SD_BUS_METHOD_WITH_ARGS("GetUser",
3662 SD_BUS_ARGS("u", uid
),
3663 SD_BUS_RESULT("o", object_path
),
3665 SD_BUS_VTABLE_UNPRIVILEGED
),
3666 SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
3667 SD_BUS_ARGS("u", pid
),
3668 SD_BUS_RESULT("o", object_path
),
3669 method_get_user_by_pid
,
3670 SD_BUS_VTABLE_UNPRIVILEGED
),
3671 SD_BUS_METHOD_WITH_ARGS("GetSeat",
3672 SD_BUS_ARGS("s", seat_id
),
3673 SD_BUS_RESULT("o", object_path
),
3675 SD_BUS_VTABLE_UNPRIVILEGED
),
3676 SD_BUS_METHOD_WITH_ARGS("ListSessions",
3678 SD_BUS_RESULT("a(susso)", sessions
),
3679 method_list_sessions
,
3680 SD_BUS_VTABLE_UNPRIVILEGED
),
3681 SD_BUS_METHOD_WITH_ARGS("ListSessionsEx",
3683 SD_BUS_RESULT("a(sussussbto)", sessions
),
3684 method_list_sessions_ex
,
3685 SD_BUS_VTABLE_UNPRIVILEGED
),
3686 SD_BUS_METHOD_WITH_ARGS("ListUsers",
3688 SD_BUS_RESULT("a(uso)", users
),
3690 SD_BUS_VTABLE_UNPRIVILEGED
),
3691 SD_BUS_METHOD_WITH_ARGS("ListSeats",
3693 SD_BUS_RESULT("a(so)", seats
),
3695 SD_BUS_VTABLE_UNPRIVILEGED
),
3696 SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
3698 SD_BUS_RESULT("a(ssssuu)", inhibitors
),
3699 method_list_inhibitors
,
3700 SD_BUS_VTABLE_UNPRIVILEGED
),
3701 SD_BUS_METHOD_WITH_ARGS("CreateSession",
3702 SD_BUS_ARGS("u", uid
,
3715 "a(sv)", properties
),
3716 SD_BUS_RESULT("s", session_id
,
3724 method_create_session
,
3726 SD_BUS_METHOD_WITH_ARGS("CreateSessionWithPIDFD",
3727 SD_BUS_ARGS("u", uid
,
3741 "a(sv)", properties
),
3742 SD_BUS_RESULT("s", session_id
,
3750 method_create_session_pidfd
,
3752 SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
3753 SD_BUS_ARGS("s", session_id
),
3755 method_release_session
,
3757 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
3758 SD_BUS_ARGS("s", session_id
),
3760 method_activate_session
,
3761 SD_BUS_VTABLE_UNPRIVILEGED
),
3762 SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
3763 SD_BUS_ARGS("s", session_id
, "s", seat_id
),
3765 method_activate_session_on_seat
,
3766 SD_BUS_VTABLE_UNPRIVILEGED
),
3767 SD_BUS_METHOD_WITH_ARGS("LockSession",
3768 SD_BUS_ARGS("s", session_id
),
3770 method_lock_session
,
3771 SD_BUS_VTABLE_UNPRIVILEGED
),
3772 SD_BUS_METHOD_WITH_ARGS("UnlockSession",
3773 SD_BUS_ARGS("s", session_id
),
3775 method_lock_session
,
3776 SD_BUS_VTABLE_UNPRIVILEGED
),
3777 SD_BUS_METHOD("LockSessions",
3780 method_lock_sessions
,
3781 SD_BUS_VTABLE_UNPRIVILEGED
),
3782 SD_BUS_METHOD("UnlockSessions",
3785 method_lock_sessions
,
3786 SD_BUS_VTABLE_UNPRIVILEGED
),
3787 SD_BUS_METHOD_WITH_ARGS("KillSession",
3788 SD_BUS_ARGS("s", session_id
, "s", who
, "i", signal_number
),
3790 method_kill_session
,
3791 SD_BUS_VTABLE_UNPRIVILEGED
),
3792 SD_BUS_METHOD_WITH_ARGS("KillUser",
3793 SD_BUS_ARGS("u", uid
, "i", signal_number
),
3796 SD_BUS_VTABLE_UNPRIVILEGED
),
3797 SD_BUS_METHOD_WITH_ARGS("TerminateSession",
3798 SD_BUS_ARGS("s", session_id
),
3800 method_terminate_session
,
3801 SD_BUS_VTABLE_UNPRIVILEGED
),
3802 SD_BUS_METHOD_WITH_ARGS("TerminateUser",
3803 SD_BUS_ARGS("u", uid
),
3805 method_terminate_user
,
3806 SD_BUS_VTABLE_UNPRIVILEGED
),
3807 SD_BUS_METHOD_WITH_ARGS("TerminateSeat",
3808 SD_BUS_ARGS("s", seat_id
),
3810 method_terminate_seat
,
3811 SD_BUS_VTABLE_UNPRIVILEGED
),
3812 SD_BUS_METHOD_WITH_ARGS("SetUserLinger",
3813 SD_BUS_ARGS("u", uid
, "b", enable
, "b", interactive
),
3815 method_set_user_linger
,
3816 SD_BUS_VTABLE_UNPRIVILEGED
),
3817 SD_BUS_METHOD_WITH_ARGS("AttachDevice",
3818 SD_BUS_ARGS("s", seat_id
, "s", sysfs_path
, "b", interactive
),
3820 method_attach_device
,
3821 SD_BUS_VTABLE_UNPRIVILEGED
),
3822 SD_BUS_METHOD_WITH_ARGS("FlushDevices",
3823 SD_BUS_ARGS("b", interactive
),
3825 method_flush_devices
,
3826 SD_BUS_VTABLE_UNPRIVILEGED
),
3827 SD_BUS_METHOD_WITH_ARGS("PowerOff",
3828 SD_BUS_ARGS("b", interactive
),
3831 SD_BUS_VTABLE_UNPRIVILEGED
),
3832 SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags",
3833 SD_BUS_ARGS("t", flags
),
3836 SD_BUS_VTABLE_UNPRIVILEGED
),
3837 SD_BUS_METHOD_WITH_ARGS("Reboot",
3838 SD_BUS_ARGS("b", interactive
),
3841 SD_BUS_VTABLE_UNPRIVILEGED
),
3842 SD_BUS_METHOD_WITH_ARGS("RebootWithFlags",
3843 SD_BUS_ARGS("t", flags
),
3846 SD_BUS_VTABLE_UNPRIVILEGED
),
3847 SD_BUS_METHOD_WITH_ARGS("Halt",
3848 SD_BUS_ARGS("b", interactive
),
3851 SD_BUS_VTABLE_UNPRIVILEGED
),
3852 SD_BUS_METHOD_WITH_ARGS("HaltWithFlags",
3853 SD_BUS_ARGS("t", flags
),
3856 SD_BUS_VTABLE_UNPRIVILEGED
),
3857 SD_BUS_METHOD_WITH_ARGS("Suspend",
3858 SD_BUS_ARGS("b", interactive
),
3861 SD_BUS_VTABLE_UNPRIVILEGED
),
3862 SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags",
3863 SD_BUS_ARGS("t", flags
),
3866 SD_BUS_VTABLE_UNPRIVILEGED
),
3867 SD_BUS_METHOD_WITH_ARGS("Hibernate",
3868 SD_BUS_ARGS("b", interactive
),
3871 SD_BUS_VTABLE_UNPRIVILEGED
),
3872 SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags",
3873 SD_BUS_ARGS("t", flags
),
3876 SD_BUS_VTABLE_UNPRIVILEGED
),
3877 SD_BUS_METHOD_WITH_ARGS("HybridSleep",
3878 SD_BUS_ARGS("b", interactive
),
3880 method_hybrid_sleep
,
3881 SD_BUS_VTABLE_UNPRIVILEGED
),
3882 SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags",
3883 SD_BUS_ARGS("t", flags
),
3885 method_hybrid_sleep
,
3886 SD_BUS_VTABLE_UNPRIVILEGED
),
3887 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate",
3888 SD_BUS_ARGS("b", interactive
),
3890 method_suspend_then_hibernate
,
3891 SD_BUS_VTABLE_UNPRIVILEGED
),
3892 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags",
3893 SD_BUS_ARGS("t", flags
),
3895 method_suspend_then_hibernate
,
3896 SD_BUS_VTABLE_UNPRIVILEGED
),
3897 SD_BUS_METHOD_WITH_ARGS("Sleep",
3898 SD_BUS_ARGS("t", flags
),
3901 SD_BUS_VTABLE_UNPRIVILEGED
),
3902 SD_BUS_METHOD_WITH_ARGS("CanPowerOff",
3904 SD_BUS_RESULT("s", result
),
3905 method_can_poweroff
,
3906 SD_BUS_VTABLE_UNPRIVILEGED
),
3907 SD_BUS_METHOD_WITH_ARGS("CanReboot",
3909 SD_BUS_RESULT("s", result
),
3911 SD_BUS_VTABLE_UNPRIVILEGED
),
3912 SD_BUS_METHOD_WITH_ARGS("CanHalt",
3914 SD_BUS_RESULT("s", result
),
3916 SD_BUS_VTABLE_UNPRIVILEGED
),
3917 SD_BUS_METHOD_WITH_ARGS("CanSuspend",
3919 SD_BUS_RESULT("s", result
),
3921 SD_BUS_VTABLE_UNPRIVILEGED
),
3922 SD_BUS_METHOD_WITH_ARGS("CanHibernate",
3924 SD_BUS_RESULT("s", result
),
3925 method_can_hibernate
,
3926 SD_BUS_VTABLE_UNPRIVILEGED
),
3927 SD_BUS_METHOD_WITH_ARGS("CanHybridSleep",
3929 SD_BUS_RESULT("s", result
),
3930 method_can_hybrid_sleep
,
3931 SD_BUS_VTABLE_UNPRIVILEGED
),
3932 SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate",
3934 SD_BUS_RESULT("s", result
),
3935 method_can_suspend_then_hibernate
,
3936 SD_BUS_VTABLE_UNPRIVILEGED
),
3937 SD_BUS_METHOD_WITH_ARGS("CanSleep",
3939 SD_BUS_RESULT("s", result
),
3941 SD_BUS_VTABLE_UNPRIVILEGED
),
3942 SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown",
3943 SD_BUS_ARGS("s", type
, "t", usec
),
3945 method_schedule_shutdown
,
3946 SD_BUS_VTABLE_UNPRIVILEGED
),
3947 SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown",
3949 SD_BUS_RESULT("b", cancelled
),
3950 method_cancel_scheduled_shutdown
,
3951 SD_BUS_VTABLE_UNPRIVILEGED
),
3952 SD_BUS_METHOD_WITH_ARGS("Inhibit",
3953 SD_BUS_ARGS("s", what
, "s", who
, "s", why
, "s", mode
),
3954 SD_BUS_RESULT("h", pipe_fd
),
3956 SD_BUS_VTABLE_UNPRIVILEGED
),
3957 SD_BUS_METHOD_WITH_ARGS("CanRebootParameter",
3959 SD_BUS_RESULT("s", result
),
3960 method_can_reboot_parameter
,
3961 SD_BUS_VTABLE_UNPRIVILEGED
),
3962 SD_BUS_METHOD_WITH_ARGS("SetRebootParameter",
3963 SD_BUS_ARGS("s", parameter
),
3965 method_set_reboot_parameter
,
3966 SD_BUS_VTABLE_UNPRIVILEGED
),
3967 SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup",
3969 SD_BUS_RESULT("s", result
),
3970 method_can_reboot_to_firmware_setup
,
3971 SD_BUS_VTABLE_UNPRIVILEGED
),
3972 SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup",
3973 SD_BUS_ARGS("b", enable
),
3975 method_set_reboot_to_firmware_setup
,
3976 SD_BUS_VTABLE_UNPRIVILEGED
),
3977 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu",
3979 SD_BUS_RESULT("s", result
),
3980 method_can_reboot_to_boot_loader_menu
,
3981 SD_BUS_VTABLE_UNPRIVILEGED
),
3982 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu",
3983 SD_BUS_ARGS("t", timeout
),
3985 method_set_reboot_to_boot_loader_menu
,
3986 SD_BUS_VTABLE_UNPRIVILEGED
),
3987 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry",
3989 SD_BUS_RESULT("s", result
),
3990 method_can_reboot_to_boot_loader_entry
,
3991 SD_BUS_VTABLE_UNPRIVILEGED
),
3992 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry",
3993 SD_BUS_ARGS("s", boot_loader_entry
),
3995 method_set_reboot_to_boot_loader_entry
,
3996 SD_BUS_VTABLE_UNPRIVILEGED
),
3997 SD_BUS_METHOD_WITH_ARGS("SetWallMessage",
3998 SD_BUS_ARGS("s", wall_message
, "b", enable
),
4000 method_set_wall_message
,
4001 SD_BUS_VTABLE_UNPRIVILEGED
),
4003 SD_BUS_SIGNAL_WITH_ARGS("SessionNew",
4004 SD_BUS_ARGS("s", session_id
, "o", object_path
),
4006 SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved",
4007 SD_BUS_ARGS("s", session_id
, "o", object_path
),
4009 SD_BUS_SIGNAL_WITH_ARGS("UserNew",
4010 SD_BUS_ARGS("u", uid
, "o", object_path
),
4012 SD_BUS_SIGNAL_WITH_ARGS("UserRemoved",
4013 SD_BUS_ARGS("u", uid
, "o", object_path
),
4015 SD_BUS_SIGNAL_WITH_ARGS("SeatNew",
4016 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4018 SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved",
4019 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4021 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown",
4022 SD_BUS_ARGS("b", start
),
4024 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdownWithMetadata",
4025 SD_BUS_ARGS("b", start
, "a{sv}", metadata
),
4027 SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep",
4028 SD_BUS_ARGS("b", start
),
4034 const BusObjectImplementation manager_object
= {
4035 "/org/freedesktop/login1",
4036 "org.freedesktop.login1.Manager",
4037 .vtables
= BUS_VTABLES(manager_vtable
),
4038 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
4043 static void session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
4050 if (result
&& !streq(result
, "done")) {
4051 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
4053 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
4054 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
4056 (void) session_send_create_reply(s
, &e
);
4057 (void) session_send_upgrade_reply(s
, &e
);
4061 (void) session_send_create_reply(s
, /* error= */ NULL
);
4062 (void) session_send_upgrade_reply(s
, /* error= */ NULL
);
4065 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4066 Manager
*m
= ASSERT_PTR(userdata
);
4067 const char *path
, *result
, *unit
;
4073 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
4075 bus_log_parse_error(r
);
4079 if (m
->action_job
&& streq(m
->action_job
, path
)) {
4080 assert(m
->delayed_action
);
4081 log_info("Operation '%s' finished.", handle_action_to_string(m
->delayed_action
->handle
));
4083 /* Tell people that they now may take a lock again */
4084 (void) send_prepare_for(m
, m
->delayed_action
, false);
4086 m
->action_job
= mfree(m
->action_job
);
4087 m
->delayed_action
= NULL
;
4094 session
= hashmap_get(m
->session_units
, unit
);
4096 if (streq_ptr(path
, session
->scope_job
)) {
4097 session
->scope_job
= mfree(session
->scope_job
);
4098 session_jobs_reply(session
, id
, unit
, result
);
4100 session_save(session
);
4101 user_save(session
->user
);
4104 session_add_to_gc_queue(session
);
4107 user
= hashmap_get(m
->user_units
, unit
);
4109 /* If the user is stopping, we're tracking stop jobs here. So don't send reply. */
4110 if (!user
->stopping
) {
4112 FOREACH_ARGUMENT(user_job
, &user
->runtime_dir_job
, &user
->service_manager_job
)
4113 if (streq_ptr(path
, *user_job
)) {
4114 *user_job
= mfree(*user_job
);
4116 LIST_FOREACH(sessions_by_user
, s
, user
->sessions
)
4117 /* Don't propagate user service failures to the client */
4118 session_jobs_reply(s
, id
, unit
, /* error = */ NULL
);
4125 user_add_to_gc_queue(user
);
4131 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4132 const char *path
, *unit
;
4133 Manager
*m
= ASSERT_PTR(userdata
);
4140 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
4142 bus_log_parse_error(r
);
4146 session
= hashmap_get(m
->session_units
, unit
);
4148 session_add_to_gc_queue(session
);
4150 user
= hashmap_get(m
->user_units
, unit
);
4152 user_add_to_gc_queue(user
);
4157 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4158 _cleanup_free_
char *unit
= NULL
;
4159 Manager
*m
= ASSERT_PTR(userdata
);
4167 path
= sd_bus_message_get_path(message
);
4171 r
= unit_name_from_dbus_path(path
, &unit
);
4172 if (r
== -EINVAL
) /* not a unit */
4179 session
= hashmap_get(m
->session_units
, unit
);
4181 session_add_to_gc_queue(session
);
4183 user
= hashmap_get(m
->user_units
, unit
);
4185 user_add_to_gc_queue(user
);
4190 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4191 Manager
*m
= ASSERT_PTR(userdata
);
4197 r
= sd_bus_message_read(message
, "b", &b
);
4199 bus_log_parse_error(r
);
4206 /* systemd finished reloading, let's recheck all our sessions */
4207 log_debug("System manager has been reloaded, rechecking sessions...");
4209 HASHMAP_FOREACH(session
, m
->sessions
)
4210 session_add_to_gc_queue(session
);
4215 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
4220 l
= strv_from_stdarg_alloca(property
);
4222 return sd_bus_emit_properties_changed_strv(
4224 "/org/freedesktop/login1",
4225 "org.freedesktop.login1.Manager",
4229 int manager_start_scope(
4232 const PidRef
*pidref
,
4235 const char *description
,
4236 const char * const *requires
,
4237 const char * const *extra_after
,
4238 const char *requires_mounts_for
,
4239 sd_bus_message
*more_properties
,
4240 sd_bus_error
*error
,
4243 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
4248 assert(pidref_is_set(pidref
));
4251 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
4255 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
4259 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
4263 if (!isempty(slice
)) {
4264 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
4269 if (!isempty(description
)) {
4270 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4275 STRV_FOREACH(i
, requires
) {
4276 r
= sd_bus_message_append(m
, "(sv)", "Requires", "as", 1, *i
);
4280 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4285 STRV_FOREACH(i
, extra_after
) {
4286 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4291 if (!empty_or_root(requires_mounts_for
)) {
4292 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4297 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4299 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4303 r
= bus_append_scope_pidref(m
, pidref
, allow_pidfd
);
4307 /* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of
4309 r
= sd_bus_message_append(m
, "(sv)", "OOMPolicy", "s", "continue");
4313 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4314 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_MAX
);
4316 return bus_log_create_error(r
);
4318 if (more_properties
) {
4319 /* If TasksMax also appears here, it will overwrite the default value set above */
4320 r
= sd_bus_message_copy(m
, more_properties
, true);
4325 r
= sd_bus_message_close_container(m
);
4329 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4333 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4335 /* If this failed with a property we couldn't write, this is quite likely because the server
4336 * doesn't support PIDFDs yet, let's try without. */
4338 sd_bus_error_has_names(error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
, SD_BUS_ERROR_PROPERTY_READ_ONLY
))
4339 return manager_start_scope(
4343 /* allow_pidfd = */ false,
4348 requires_mounts_for
,
4356 return strdup_job(reply
, ret_job
);
4359 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **ret_job
) {
4360 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4367 r
= bus_call_method(
4373 "ss", unit
, "replace");
4377 return strdup_job(reply
, ret_job
);
4380 int manager_stop_unit(
4383 const char *job_mode
,
4384 sd_bus_error
*ret_error
,
4387 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4388 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4395 r
= bus_call_method(
4401 "ss", unit
, job_mode
?: "fail");
4403 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
, BUS_ERROR_LOAD_FAILED
)) {
4408 sd_bus_error_move(ret_error
, &error
);
4412 r
= strdup_job(reply
, ret_job
);
4419 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4420 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4421 _cleanup_free_
char *path
= NULL
;
4427 path
= unit_dbus_path_from_name(scope
);
4431 r
= sd_bus_call_method(
4433 "org.freedesktop.systemd1",
4435 "org.freedesktop.systemd1.Scope",
4441 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4442 BUS_ERROR_LOAD_FAILED
,
4443 BUS_ERROR_SCOPE_NOT_RUNNING
))
4446 sd_bus_error_move(ret_error
, &error
);
4453 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4456 assert(SIGNAL_VALID(signo
));
4458 return bus_call_method(
4466 who
== KILL_LEADER
? "main" : "all",
4470 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4471 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4472 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4473 _cleanup_free_
char *path
= NULL
;
4480 path
= unit_dbus_path_from_name(unit
);
4484 r
= sd_bus_get_property(
4486 "org.freedesktop.systemd1",
4488 "org.freedesktop.systemd1.Unit",
4494 /* systemd might have dropped off momentarily, let's
4495 * not make this an error */
4496 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4497 SD_BUS_ERROR_DISCONNECTED
))
4500 /* If the unit is already unloaded then it's not
4502 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4503 BUS_ERROR_LOAD_FAILED
))
4506 sd_bus_error_move(ret_error
, &error
);
4510 r
= sd_bus_message_read(reply
, "s", &state
);
4514 return !STR_IN_SET(state
, "inactive", "failed");
4517 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4518 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4519 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4525 r
= sd_bus_get_property(
4527 "org.freedesktop.systemd1",
4529 "org.freedesktop.systemd1.Job",
4535 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4536 SD_BUS_ERROR_DISCONNECTED
))
4539 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4542 sd_bus_error_move(ret_error
, &error
);
4546 /* We don't actually care about the state really. The fact
4547 * that we could read the job state is enough for us */