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 session
->tty
= strdup(tty
);
998 session
->tty_validity
= TTY_FROM_PAM
;
1001 if (!isempty(display
)) {
1002 session
->display
= strdup(display
);
1003 if (!session
->display
) {
1009 if (!isempty(remote_user
)) {
1010 session
->remote_user
= strdup(remote_user
);
1011 if (!session
->remote_user
) {
1017 if (!isempty(remote_host
)) {
1018 session
->remote_host
= strdup(remote_host
);
1019 if (!session
->remote_host
) {
1025 if (!isempty(service
)) {
1026 session
->service
= strdup(service
);
1027 if (!session
->service
) {
1033 if (!isempty(desktop
)) {
1034 session
->desktop
= strdup(desktop
);
1035 if (!session
->desktop
) {
1042 r
= seat_attach_session(seat
, session
);
1047 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1051 r
= session_start(session
, message
, error
);
1055 r
= sd_bus_message_exit_container(message
);
1059 session
->create_message
= sd_bus_message_ref(message
);
1061 /* Now call into session_send_create_reply(), which will reply to this method call for us. Or it
1062 * won't – in case we just spawned a session scope and/or user service manager, and they aren't ready
1063 * yet. We'll call session_create_reply() again once the session scope or the user service manager is
1064 * ready, where the function will check again if a reply is then ready to be sent, and then do so if
1065 * all is complete - or wait again. */
1066 r
= session_send_create_reply(session
, /* error= */ NULL
);
1074 session_add_to_gc_queue(session
);
1077 user_add_to_gc_queue(user
);
1082 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1083 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1091 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
1092 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
1094 r
= sd_bus_message_read(
1113 return create_session(
1119 /* pidfd = */ -EBADF
,
1134 static int method_create_session_pidfd(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1135 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1139 int leader_fd
= -EBADF
, remote
, r
;
1141 r
= sd_bus_message_read(
1161 return create_session(
1166 /* leader_pid = */ 0,
1182 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1183 Manager
*m
= ASSERT_PTR(userdata
);
1190 r
= sd_bus_message_read(message
, "s", &name
);
1194 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1198 r
= session_release(session
);
1202 return sd_bus_reply_method_return(message
, NULL
);
1205 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1206 Manager
*m
= ASSERT_PTR(userdata
);
1213 r
= sd_bus_message_read(message
, "s", &name
);
1217 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1221 /* PolicyKit is done by bus_session_method_activate() */
1223 return bus_session_method_activate(message
, session
, error
);
1226 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1227 const char *session_name
, *seat_name
;
1228 Manager
*m
= ASSERT_PTR(userdata
);
1235 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1237 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1241 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1245 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1249 if (session
->seat
!= seat
)
1250 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1251 "Session %s not on seat %s", session_name
, seat_name
);
1253 r
= check_polkit_chvt(message
, m
, error
);
1257 return 1; /* Will call us back */
1259 r
= session_activate(session
);
1263 return sd_bus_reply_method_return(message
, NULL
);
1266 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1267 Manager
*m
= ASSERT_PTR(userdata
);
1274 r
= sd_bus_message_read(message
, "s", &name
);
1278 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1282 return bus_session_method_lock(message
, session
, error
);
1285 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1286 Manager
*m
= ASSERT_PTR(userdata
);
1291 r
= bus_verify_polkit_async(
1293 "org.freedesktop.login1.lock-sessions",
1294 /* details= */ NULL
,
1295 &m
->polkit_registry
,
1300 return 1; /* Will call us back */
1302 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1306 return sd_bus_reply_method_return(message
, NULL
);
1309 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1311 Manager
*m
= ASSERT_PTR(userdata
);
1317 r
= sd_bus_message_read(message
, "s", &name
);
1321 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1325 return bus_session_method_kill(message
, session
, error
);
1328 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1329 Manager
*m
= ASSERT_PTR(userdata
);
1336 r
= sd_bus_message_read(message
, "u", &uid
);
1340 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1344 return bus_user_method_kill(message
, user
, error
);
1347 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1348 Manager
*m
= ASSERT_PTR(userdata
);
1355 r
= sd_bus_message_read(message
, "s", &name
);
1359 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1363 return bus_session_method_terminate(message
, session
, error
);
1366 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1367 Manager
*m
= ASSERT_PTR(userdata
);
1374 r
= sd_bus_message_read(message
, "u", &uid
);
1378 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1382 return bus_user_method_terminate(message
, user
, error
);
1385 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1386 Manager
*m
= ASSERT_PTR(userdata
);
1393 r
= sd_bus_message_read(message
, "s", &name
);
1397 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1401 return bus_seat_method_terminate(message
, seat
, error
);
1404 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1405 Manager
*m
= ASSERT_PTR(userdata
);
1406 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1407 uint32_t uid
, auth_uid
;
1408 int r
, enable
, interactive
;
1412 r
= sd_bus_message_read(message
, "ubb", &uid
, &enable
, &interactive
);
1416 r
= sd_bus_query_sender_creds(message
,
1417 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
,
1422 if (!uid_is_valid(uid
)) {
1423 /* Note that we get the owner UID of the session or user unit, not the actual client UID here! */
1424 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1429 /* owner_uid is racy, so for authorization we must use euid */
1430 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1434 _cleanup_free_
struct passwd
*pw
= NULL
;
1436 r
= getpwuid_malloc(uid
, &pw
);
1440 r
= bus_verify_polkit_async_full(
1442 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1443 "org.freedesktop.login1.set-user-linger",
1444 /* details= */ NULL
,
1446 /* good_user= */ UID_INVALID
,
1447 &m
->polkit_registry
,
1452 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1454 (void) mkdir_p_label("/var/lib/systemd", 0755);
1455 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1459 _cleanup_free_
char *escaped
= NULL
;
1463 escaped
= cescape(pw
->pw_name
);
1467 path
= strjoina("/var/lib/systemd/linger/", escaped
);
1474 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0) {
1477 user_add_to_gc_queue(u
);
1484 if (r
< 0 && errno
!= ENOENT
)
1487 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1489 user_add_to_gc_queue(u
);
1492 return sd_bus_reply_method_return(message
, NULL
);
1495 static int trigger_device(Manager
*m
, sd_device
*parent
) {
1496 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1501 r
= sd_device_enumerator_new(&e
);
1505 r
= sd_device_enumerator_allow_uninitialized(e
);
1510 r
= sd_device_enumerator_add_match_parent(e
, parent
);
1515 FOREACH_DEVICE(e
, d
) {
1516 r
= sd_device_trigger(d
, SD_DEVICE_CHANGE
);
1518 log_device_debug_errno(d
, r
, "Failed to trigger device, ignoring: %m");
1524 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
, sd_bus_error
*error
) {
1525 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1526 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1527 const char *id_for_seat
;
1534 r
= sd_device_new_from_syspath(&d
, sysfs
);
1536 return sd_bus_error_set_errnof(error
, r
, "Failed to open device '%s': %m", sysfs
);
1538 if (sd_device_has_current_tag(d
, "seat") <= 0)
1539 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'seat' udev tag.", sysfs
);
1541 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1542 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'ID_FOR_SEAT' udev property.", sysfs
);
1544 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1547 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1550 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1551 r
= write_string_file_atomic_label(file
, rule
);
1555 return trigger_device(m
, d
);
1558 static int flush_devices(Manager
*m
) {
1559 _cleanup_closedir_
DIR *d
= NULL
;
1563 d
= opendir("/etc/udev/rules.d");
1565 if (errno
!= ENOENT
)
1566 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1568 FOREACH_DIRENT_ALL(de
, d
, break) {
1569 if (!dirent_is_file(de
))
1572 if (!startswith(de
->d_name
, "72-seat-"))
1575 if (!endswith(de
->d_name
, ".rules"))
1578 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1579 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1582 return trigger_device(m
, NULL
);
1585 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1586 const char *sysfs
, *seat
;
1587 Manager
*m
= ASSERT_PTR(userdata
);
1592 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1596 if (!path_is_normalized(sysfs
))
1597 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1598 if (!path_startswith(sysfs
, "/sys"))
1599 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1601 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1604 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1610 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1611 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1613 r
= bus_verify_polkit_async_full(
1615 "org.freedesktop.login1.attach-device",
1616 /* details= */ NULL
,
1618 /* good_user= */ UID_INVALID
,
1619 &m
->polkit_registry
,
1624 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1626 r
= attach_device(m
, seat
, sysfs
, error
);
1630 return sd_bus_reply_method_return(message
, NULL
);
1633 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1634 Manager
*m
= ASSERT_PTR(userdata
);
1639 r
= sd_bus_message_read(message
, "b", &interactive
);
1643 r
= bus_verify_polkit_async_full(
1645 "org.freedesktop.login1.flush-devices",
1646 /* details= */ NULL
,
1648 /* good_user= */ UID_INVALID
,
1649 &m
->polkit_registry
,
1654 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1656 r
= flush_devices(m
);
1660 return sd_bus_reply_method_return(message
, NULL
);
1663 static int have_multiple_sessions(
1671 /* Check for other users' sessions. Greeter sessions do not
1672 * count, and non-login sessions do not count either. */
1673 HASHMAP_FOREACH(session
, m
->sessions
)
1674 if (session
->class == SESSION_USER
&&
1675 session
->user
->user_record
->uid
!= uid
)
1681 static int bus_manager_log_shutdown(
1683 const HandleActionData
*a
) {
1687 const char *message
= a
->message
?: "System is shutting down";
1688 const char *log_verb
= a
->log_verb
? strjoina("SHUTDOWN=", a
->log_verb
) : NULL
;
1690 return log_struct(LOG_NOTICE
,
1691 "MESSAGE_ID=%s", a
->message_id
?: SD_MESSAGE_SHUTDOWN_STR
,
1692 LOG_MESSAGE("%s%s%s%s.",
1694 m
->wall_message
? " (" : "",
1695 strempty(m
->wall_message
),
1696 m
->wall_message
? ")" : ""),
1700 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1701 Manager
*m
= ASSERT_PTR(userdata
);
1705 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1709 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1714 if (until
<= now(CLOCK_MONOTONIC
))
1717 /* We want to ignore the lid switch for a while after each
1718 * suspend, and after boot-up. Hence let's install a timer for
1719 * this. As long as the event source exists we ignore the lid
1722 if (m
->lid_switch_ignore_event_source
) {
1725 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1732 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1734 r
= sd_event_add_time(
1736 &m
->lid_switch_ignore_event_source
,
1739 lid_switch_ignore_handler
, m
);
1744 static int send_prepare_for(Manager
*m
, const HandleActionData
*a
, bool _active
) {
1745 int k
= 0, r
, active
= _active
;
1749 assert(IN_SET(a
->inhibit_what
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1751 /* We need to send both old and new signal for backward compatibility. The newer one allows clients
1752 * to know which type of reboot is going to happen, as they might be doing different actions (e.g.:
1753 * on soft-reboot), and it is sent first, so that clients know that if they receive the old one
1754 * first then they don't have to wait for the new one, as it means it's not supported. So, do not
1755 * change the order here, as it is an API. */
1756 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
) {
1757 k
= sd_bus_emit_signal(m
->bus
,
1758 "/org/freedesktop/login1",
1759 "org.freedesktop.login1.Manager",
1760 "PrepareForShutdownWithMetadata",
1766 handle_action_to_string(a
->handle
));
1768 log_debug_errno(k
, "Failed to emit PrepareForShutdownWithMetadata(): %m");
1771 r
= sd_bus_emit_signal(m
->bus
,
1772 "/org/freedesktop/login1",
1773 "org.freedesktop.login1.Manager",
1774 a
->inhibit_what
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1778 log_debug_errno(r
, "Failed to emit PrepareForShutdown(): %m");
1780 return RET_GATHER(k
, r
);
1783 static int strdup_job(sd_bus_message
*reply
, char **ret
) {
1791 r
= sd_bus_message_read_basic(reply
, 'o', &j
);
1803 static int execute_shutdown_or_sleep(
1805 const HandleActionData
*a
,
1806 sd_bus_error
*error
) {
1808 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1812 assert(!m
->action_job
);
1815 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
)
1816 bus_manager_log_shutdown(m
, a
);
1818 r
= bus_call_method(
1824 "ss", a
->target
, "replace-irreversibly");
1828 r
= strdup_job(reply
, &m
->action_job
);
1832 m
->delayed_action
= a
;
1834 /* Make sure the lid switch is ignored for a while */
1835 manager_set_lid_switch_ignore(m
, usec_add(now(CLOCK_MONOTONIC
), m
->holdoff_timeout_usec
));
1840 /* Tell people that they now may take a lock again */
1841 (void) send_prepare_for(m
, a
, false);
1846 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1847 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1848 Inhibitor
*offending
= NULL
;
1853 if (!manager
->delayed_action
|| manager
->action_job
)
1856 if (manager_is_inhibited(manager
, manager
->delayed_action
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1857 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1862 (void) pidref_get_comm(&offending
->pid
, &comm
);
1863 u
= uid_to_name(offending
->uid
);
1865 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1866 offending
->uid
, strna(u
),
1867 offending
->pid
.pid
, strna(comm
));
1870 /* Actually do the operation */
1871 r
= execute_shutdown_or_sleep(manager
, manager
->delayed_action
, &error
);
1873 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1874 bus_error_message(&error
, r
));
1876 manager
->delayed_action
= NULL
;
1879 return 1; /* We did some work. */
1882 static int manager_inhibit_timeout_handler(
1887 Manager
*manager
= ASSERT_PTR(userdata
);
1889 assert(manager
->inhibit_timeout_source
== s
);
1891 return manager_dispatch_delayed(manager
, true);
1894 static int delay_shutdown_or_sleep(
1896 const HandleActionData
*a
) {
1903 if (m
->inhibit_timeout_source
) {
1904 r
= sd_event_source_set_time_relative(m
->inhibit_timeout_source
, m
->inhibit_delay_max
);
1906 return log_error_errno(r
, "sd_event_source_set_time_relative() failed: %m");
1908 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1910 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1912 r
= sd_event_add_time_relative(
1914 &m
->inhibit_timeout_source
,
1915 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, 0,
1916 manager_inhibit_timeout_handler
, m
);
1921 m
->delayed_action
= a
;
1926 int bus_manager_shutdown_or_sleep_now_or_later(
1928 const HandleActionData
*a
,
1929 sd_bus_error
*error
) {
1931 _cleanup_free_
char *load_state
= NULL
;
1937 assert(!m
->action_job
);
1939 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
1943 if (!streq(load_state
, "loaded"))
1944 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1945 "Unit %s is %s, refusing operation.",
1946 a
->target
, load_state
);
1948 /* Tell everybody to prepare for shutdown/sleep */
1949 (void) send_prepare_for(m
, a
, true);
1952 m
->inhibit_delay_max
> 0 &&
1953 manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1956 /* Shutdown is delayed, keep in mind what we
1957 * want to do, and start a timeout */
1958 r
= delay_shutdown_or_sleep(m
, a
);
1960 /* Shutdown is not delayed, execute it
1962 r
= execute_shutdown_or_sleep(m
, a
, error
);
1967 static int verify_shutdown_creds(
1969 sd_bus_message
*message
,
1970 const HandleActionData
*a
,
1972 sd_bus_error
*error
) {
1974 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1975 bool multiple_sessions
, blocked
, interactive
;
1983 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1987 r
= sd_bus_creds_get_euid(creds
, &uid
);
1991 r
= have_multiple_sessions(m
, uid
);
1995 multiple_sessions
= r
> 0;
1996 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1997 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
1999 if (multiple_sessions
) {
2000 r
= bus_verify_polkit_async_full(
2002 a
->polkit_action_multiple_sessions
,
2003 /* details= */ NULL
,
2005 /* good_user= */ UID_INVALID
,
2006 &m
->polkit_registry
,
2011 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2015 /* We don't check polkit for root here, because you can't be more privileged than root */
2016 if (uid
== 0 && (flags
& SD_LOGIND_ROOT_CHECK_INHIBITORS
))
2017 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
,
2018 "Access denied to root due to active block inhibitor");
2020 r
= bus_verify_polkit_async_full(
2022 a
->polkit_action_ignore_inhibit
,
2023 /* details= */ NULL
,
2025 /* good_user= */ UID_INVALID
,
2026 &m
->polkit_registry
,
2031 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2034 if (!multiple_sessions
&& !blocked
) {
2035 r
= bus_verify_polkit_async_full(
2038 /* details= */ NULL
,
2040 /* good_user= */ UID_INVALID
,
2041 &m
->polkit_registry
,
2046 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2052 static int setup_wall_message_timer(Manager
*m
, sd_bus_message
* message
) {
2053 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2056 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2058 const char *tty
= NULL
;
2060 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2061 (void) sd_bus_creds_get_tty(creds
, &tty
);
2063 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2068 r
= manager_setup_wall_message_timer(m
);
2075 static int method_do_shutdown_or_sleep(
2077 sd_bus_message
*message
,
2078 HandleAction action
,
2080 sd_bus_error
*error
) {
2087 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2090 /* New style method: with flags parameter (and interactive bool in the bus message header) */
2091 r
= sd_bus_message_read(message
, "t", &flags
);
2094 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
2095 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2096 "Invalid flags parameter");
2098 if (FLAGS_SET(flags
, (SD_LOGIND_REBOOT_VIA_KEXEC
|SD_LOGIND_SOFT_REBOOT
)))
2099 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2100 "Both reboot via kexec and soft reboot selected, which is not supported");
2102 if (action
!= HANDLE_REBOOT
) {
2103 if (flags
& SD_LOGIND_REBOOT_VIA_KEXEC
)
2104 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2105 "Reboot via kexec option is only applicable with reboot operations");
2106 if ((flags
& SD_LOGIND_SOFT_REBOOT
) || (flags
& SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
))
2107 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2108 "Soft reboot option is only applicable with reboot operations");
2111 /* Old style method: no flags parameter, but interactive bool passed as boolean in
2112 * payload. Let's convert this argument to the new-style flags parameter for our internal
2116 r
= sd_bus_message_read(message
, "b", &interactive
);
2120 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
2123 const HandleActionData
*a
= NULL
;
2125 if ((flags
& SD_LOGIND_SOFT_REBOOT
) ||
2126 ((flags
& SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
) && path_is_os_tree("/run/nextroot") > 0))
2127 a
= handle_action_lookup(HANDLE_SOFT_REBOOT
);
2128 else if ((flags
& SD_LOGIND_REBOOT_VIA_KEXEC
) && kexec_loaded())
2129 a
= handle_action_lookup(HANDLE_KEXEC
);
2131 if (action
== HANDLE_SLEEP
) {
2132 HandleAction selected
;
2134 selected
= handle_action_sleep_select(m
);
2136 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2137 "None of the configured sleep operations are supported");
2139 assert_se(a
= handle_action_lookup(selected
));
2141 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2142 SleepSupport support
;
2144 assert_se(a
= handle_action_lookup(action
));
2146 assert(a
->sleep_operation
>= 0);
2147 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2149 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2155 case SLEEP_DISABLED
:
2156 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2157 "Sleep verb '%s' is disabled by config",
2158 sleep_operation_to_string(a
->sleep_operation
));
2160 case SLEEP_NOT_CONFIGURED
:
2161 case SLEEP_STATE_OR_MODE_NOT_SUPPORTED
:
2162 case SLEEP_ALARM_NOT_SUPPORTED
:
2163 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2164 "Sleep verb '%s' is not configured or configuration is not supported by kernel",
2165 sleep_operation_to_string(a
->sleep_operation
));
2167 case SLEEP_RESUME_NOT_SUPPORTED
:
2168 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2169 "Not running on EFI and resume= is not set. No available method to resume from hibernation");
2171 case SLEEP_NOT_ENOUGH_SWAP_SPACE
:
2172 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2173 "Not enough suitable swap space for hibernation available on compatible block devices and file systems");
2176 assert_not_reached();
2180 assert_se(a
= handle_action_lookup(action
));
2182 r
= verify_shutdown_creds(m
, message
, a
, flags
, error
);
2186 if (m
->delayed_action
)
2187 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
2188 "Action %s already in progress, refusing requested %s operation.",
2189 handle_action_to_string(m
->delayed_action
->handle
),
2190 handle_action_to_string(a
->handle
));
2192 /* reset case we're shorting a scheduled shutdown */
2193 m
->unlink_nologin
= false;
2194 reset_scheduled_shutdown(m
);
2196 m
->scheduled_shutdown_timeout
= 0;
2197 m
->scheduled_shutdown_action
= action
;
2199 (void) setup_wall_message_timer(m
, message
);
2201 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, error
);
2205 return sd_bus_reply_method_return(message
, NULL
);
2208 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2209 Manager
*m
= userdata
;
2211 return method_do_shutdown_or_sleep(
2214 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
2218 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2219 Manager
*m
= userdata
;
2221 return method_do_shutdown_or_sleep(
2224 sd_bus_message_is_method_call(message
, NULL
, "RebootWithFlags"),
2228 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2229 Manager
*m
= userdata
;
2231 return method_do_shutdown_or_sleep(
2234 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
2238 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2239 Manager
*m
= userdata
;
2241 return method_do_shutdown_or_sleep(
2244 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
2248 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2249 Manager
*m
= userdata
;
2251 return method_do_shutdown_or_sleep(
2254 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
2258 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2259 Manager
*m
= userdata
;
2261 return method_do_shutdown_or_sleep(
2263 HANDLE_HYBRID_SLEEP
,
2264 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
2268 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2269 Manager
*m
= userdata
;
2271 return method_do_shutdown_or_sleep(
2273 HANDLE_SUSPEND_THEN_HIBERNATE
,
2274 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
2278 static int method_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2279 Manager
*m
= userdata
;
2281 return method_do_shutdown_or_sleep(
2284 /* with_flags = */ true,
2288 static int nologin_timeout_handler(
2293 Manager
*m
= ASSERT_PTR(userdata
);
2295 log_info("Creating /run/nologin, blocking further logins...");
2298 create_shutdown_run_nologin_or_warn() >= 0;
2303 static usec_t
nologin_timeout_usec(usec_t elapse
) {
2304 /* Issue /run/nologin five minutes before shutdown */
2305 return LESS_BY(elapse
, 5 * USEC_PER_MINUTE
);
2308 static void reset_scheduled_shutdown(Manager
*m
) {
2311 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2312 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2313 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2315 m
->scheduled_shutdown_action
= _HANDLE_ACTION_INVALID
;
2316 m
->scheduled_shutdown_timeout
= USEC_INFINITY
;
2317 m
->scheduled_shutdown_uid
= UID_INVALID
;
2318 m
->scheduled_shutdown_tty
= mfree(m
->scheduled_shutdown_tty
);
2319 m
->shutdown_dry_run
= false;
2321 if (m
->unlink_nologin
) {
2322 (void) unlink_or_warn("/run/nologin");
2323 m
->unlink_nologin
= false;
2326 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2329 static int update_schedule_file(Manager
*m
) {
2330 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
2331 _cleanup_fclose_
FILE *f
= NULL
;
2335 assert(handle_action_valid(m
->scheduled_shutdown_action
));
2337 r
= mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE
, 0755);
2339 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2341 r
= fopen_temporary(SHUTDOWN_SCHEDULE_FILE
, &f
, &temp_path
);
2343 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2345 (void) fchmod(fileno(f
), 0644);
2347 serialize_usec(f
, "USEC", m
->scheduled_shutdown_timeout
);
2348 serialize_item_format(f
, "WARN_WALL", "%s", one_zero(m
->enable_wall_messages
));
2349 serialize_item_format(f
, "MODE", "%s", handle_action_to_string(m
->scheduled_shutdown_action
));
2350 serialize_item_format(f
, "UID", UID_FMT
, m
->scheduled_shutdown_uid
);
2352 if (m
->scheduled_shutdown_tty
)
2353 serialize_item_format(f
, "TTY", "%s", m
->scheduled_shutdown_tty
);
2355 if (!isempty(m
->wall_message
)) {
2356 r
= serialize_item_escaped(f
, "WALL_MESSAGE", m
->wall_message
);
2361 r
= fflush_and_check(f
);
2365 if (rename(temp_path
, SHUTDOWN_SCHEDULE_FILE
) < 0) {
2370 temp_path
= mfree(temp_path
);
2374 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2376 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2379 static int manager_scheduled_shutdown_handler(
2384 Manager
*m
= ASSERT_PTR(userdata
);
2385 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2386 const HandleActionData
*a
;
2389 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2391 /* Don't allow multiple jobs being executed at the same time */
2392 if (m
->delayed_action
) {
2393 r
= log_error_errno(SYNTHETIC_ERRNO(EALREADY
),
2394 "Scheduled shutdown to %s failed: shutdown or sleep operation already in progress.",
2399 if (m
->shutdown_dry_run
) {
2400 /* We do not process delay inhibitors here. Otherwise, we
2401 * would have to be considered "in progress" (like the check
2402 * above) for some seconds after our admin has seen the final
2405 bus_manager_log_shutdown(m
, a
);
2406 log_info("Running in dry run, suppressing action.");
2407 reset_scheduled_shutdown(m
);
2412 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, &error
);
2414 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", a
->target
);
2421 reset_scheduled_shutdown(m
);
2425 static int manager_setup_shutdown_timers(Manager
* m
) {
2430 r
= event_reset_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2432 m
->scheduled_shutdown_timeout
, 0,
2433 manager_scheduled_shutdown_handler
, m
,
2434 0, "scheduled-shutdown-timeout", true);
2438 r
= event_reset_time(m
->event
, &m
->nologin_timeout_source
,
2440 nologin_timeout_usec(m
->scheduled_shutdown_timeout
), 0,
2441 nologin_timeout_handler
, m
,
2442 0, "nologin-timeout", true);
2449 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2450 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2455 void manager_load_scheduled_shutdown(Manager
*m
) {
2456 _cleanup_fclose_
FILE *f
= NULL
;
2457 _cleanup_free_
char *usec
= NULL
,
2460 *wall_message
= NULL
,
2467 r
= parse_env_file(f
, SHUTDOWN_SCHEDULE_FILE
,
2469 "WARN_WALL", &warn_wall
,
2471 "WALL_MESSAGE", &wall_message
,
2475 /* reset will delete the file */
2476 reset_scheduled_shutdown(m
);
2481 return (void) log_debug_errno(r
, "Failed to parse " SHUTDOWN_SCHEDULE_FILE
": %m");
2483 HandleAction handle
= handle_action_from_string(mode
);
2485 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse scheduled shutdown type: %s", mode
);
2488 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "USEC is required");
2489 if (deserialize_usec(usec
, &m
->scheduled_shutdown_timeout
) < 0)
2492 /* assign parsed type only after we know usec is also valid */
2493 m
->scheduled_shutdown_action
= handle
;
2496 r
= parse_boolean(warn_wall
);
2498 log_debug_errno(r
, "Failed to parse enabling wall messages");
2500 m
->enable_wall_messages
= r
;
2504 _cleanup_free_
char *unescaped
= NULL
;
2505 r
= cunescape(wall_message
, 0, &unescaped
);
2507 log_debug_errno(r
, "Failed to parse wall message: %s", wall_message
);
2509 free_and_replace(m
->wall_message
, unescaped
);
2513 r
= parse_uid(uid
, &m
->scheduled_shutdown_uid
);
2515 log_debug_errno(r
, "Failed to parse wall uid: %s", uid
);
2518 free_and_replace(m
->scheduled_shutdown_tty
, tty
);
2520 r
= manager_setup_shutdown_timers(m
);
2522 return reset_scheduled_shutdown(m
);
2524 (void) manager_setup_wall_message_timer(m
);
2525 (void) update_schedule_file(m
);
2530 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2531 Manager
*m
= ASSERT_PTR(userdata
);
2532 HandleAction handle
;
2533 const HandleActionData
*a
;
2537 bool dry_run
= false;
2541 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2545 if (startswith(type
, "dry-")) {
2550 handle
= handle_action_from_string(type
);
2551 if (!HANDLE_ACTION_IS_SHUTDOWN(handle
))
2552 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type: %s", type
);
2554 assert_se(a
= handle_action_lookup(handle
));
2555 assert(a
->polkit_action
);
2557 r
= verify_shutdown_creds(m
, message
, a
, 0, error
);
2561 m
->scheduled_shutdown_action
= handle
;
2562 m
->shutdown_dry_run
= dry_run
;
2563 m
->scheduled_shutdown_timeout
= elapse
;
2565 r
= manager_setup_shutdown_timers(m
);
2569 r
= setup_wall_message_timer(m
, message
);
2571 r
= update_schedule_file(m
);
2574 reset_scheduled_shutdown(m
);
2578 return sd_bus_reply_method_return(message
, NULL
);
2581 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2582 Manager
*m
= ASSERT_PTR(userdata
);
2583 const HandleActionData
*a
;
2589 cancelled
= handle_action_valid(m
->scheduled_shutdown_action
) && m
->scheduled_shutdown_action
!= HANDLE_IGNORE
;
2591 return sd_bus_reply_method_return(message
, "b", false);
2593 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2594 if (!a
->polkit_action
)
2595 return sd_bus_error_set(error
, SD_BUS_ERROR_AUTH_FAILED
, "Unsupported shutdown type");
2597 r
= bus_verify_polkit_async(
2600 /* details= */ NULL
,
2601 &m
->polkit_registry
,
2606 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2608 if (m
->enable_wall_messages
) {
2609 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2610 const char *tty
= NULL
;
2613 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2615 (void) sd_bus_creds_get_uid(creds
, &uid
);
2616 (void) sd_bus_creds_get_tty(creds
, &tty
);
2619 _cleanup_free_
char *username
= uid_to_name(uid
);
2621 log_struct(LOG_INFO
,
2622 LOG_MESSAGE("System shutdown has been cancelled"),
2623 "ACTION=%s", handle_action_to_string(a
->handle
),
2624 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_CANCELED_STR
,
2625 username
? "OPERATOR=%s" : NULL
, username
);
2627 (void) wall("System shutdown has been cancelled",
2628 username
, tty
, logind_wall_tty_filter
, m
);
2631 reset_scheduled_shutdown(m
);
2633 return sd_bus_reply_method_return(message
, "b", true);
2636 static int method_can_shutdown_or_sleep(
2638 sd_bus_message
*message
,
2639 HandleAction action
,
2640 sd_bus_error
*error
) {
2642 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2643 bool multiple_sessions
, challenge
, blocked
, check_unit_state
= true;
2644 const HandleActionData
*a
;
2645 const char *result
= NULL
;
2651 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2653 if (action
== HANDLE_SLEEP
) {
2654 HandleAction selected
;
2656 selected
= handle_action_sleep_select(m
);
2658 return sd_bus_reply_method_return(message
, "s", "na");
2660 check_unit_state
= false; /* Already handled by handle_action_sleep_select */
2662 assert_se(a
= handle_action_lookup(selected
));
2664 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2665 SleepSupport support
;
2667 assert_se(a
= handle_action_lookup(action
));
2669 assert(a
->sleep_operation
>= 0);
2670 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2672 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2676 return sd_bus_reply_method_return(message
, "s", support
== SLEEP_DISABLED
? "no" : "na");
2678 assert_se(a
= handle_action_lookup(action
));
2680 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2684 r
= sd_bus_creds_get_euid(creds
, &uid
);
2688 r
= have_multiple_sessions(m
, uid
);
2692 multiple_sessions
= r
> 0;
2693 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2695 if (check_unit_state
&& a
->target
) {
2696 _cleanup_free_
char *load_state
= NULL
;
2698 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
2702 if (!streq(load_state
, "loaded")) {
2708 if (multiple_sessions
) {
2709 r
= bus_test_polkit(
2711 a
->polkit_action_multiple_sessions
,
2712 /* details= */ NULL
,
2713 /* good_user= */ UID_INVALID
,
2722 result
= "challenge";
2728 r
= bus_test_polkit(
2730 a
->polkit_action_ignore_inhibit
,
2731 /* details= */ NULL
,
2732 /* good_user= */ UID_INVALID
,
2741 } else if (challenge
) {
2742 if (!result
|| streq(result
, "yes"))
2743 result
= "challenge";
2748 if (!multiple_sessions
&& !blocked
) {
2749 /* If neither inhibit nor multiple sessions
2750 * apply then just check the normal policy */
2752 r
= bus_test_polkit(
2755 /* details= */ NULL
,
2756 /* good_user= */ UID_INVALID
,
2765 result
= "challenge";
2771 return sd_bus_reply_method_return(message
, "s", result
);
2774 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2775 Manager
*m
= userdata
;
2777 return method_can_shutdown_or_sleep(m
, message
, HANDLE_POWEROFF
, error
);
2780 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2781 Manager
*m
= userdata
;
2783 return method_can_shutdown_or_sleep(m
, message
, HANDLE_REBOOT
, error
);
2786 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2787 Manager
*m
= userdata
;
2789 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HALT
, error
);
2792 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2793 Manager
*m
= userdata
;
2795 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND
, error
);
2798 static int method_can_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_HIBERNATE
, error
);
2804 static int method_can_hybrid_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_HYBRID_SLEEP
, error
);
2810 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2811 Manager
*m
= userdata
;
2813 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND_THEN_HIBERNATE
, error
);
2816 static int method_can_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2817 Manager
*m
= userdata
;
2819 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SLEEP
, error
);
2822 static int property_get_reboot_parameter(
2825 const char *interface
,
2826 const char *property
,
2827 sd_bus_message
*reply
,
2829 sd_bus_error
*error
) {
2830 _cleanup_free_
char *parameter
= NULL
;
2837 r
= read_reboot_parameter(¶meter
);
2841 return sd_bus_message_append(reply
, "s", parameter
);
2844 static int method_set_reboot_parameter(
2845 sd_bus_message
*message
,
2847 sd_bus_error
*error
) {
2849 Manager
*m
= ASSERT_PTR(userdata
);
2855 r
= sd_bus_message_read(message
, "s", &arg
);
2859 r
= detect_container();
2863 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2864 "Reboot parameter not supported in containers, refusing.");
2866 r
= bus_verify_polkit_async(
2868 "org.freedesktop.login1.set-reboot-parameter",
2869 /* details= */ NULL
,
2870 &m
->polkit_registry
,
2875 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2877 r
= update_reboot_parameter_and_warn(arg
, false);
2881 return sd_bus_reply_method_return(message
, NULL
);
2884 static int method_can_reboot_parameter(
2885 sd_bus_message
*message
,
2887 sd_bus_error
*error
) {
2889 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2894 r
= detect_container();
2897 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2898 return sd_bus_reply_method_return(message
, "s", "na");
2900 return return_test_polkit(
2902 "org.freedesktop.login1.set-reboot-parameter",
2903 /* details= */ NULL
,
2904 /* good_user= */ UID_INVALID
,
2908 static int property_get_reboot_to_firmware_setup(
2911 const char *interface
,
2912 const char *property
,
2913 sd_bus_message
*reply
,
2915 sd_bus_error
*error
) {
2922 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2924 /* EFI case: let's see what is currently configured in the EFI variables */
2925 r
= efi_get_reboot_to_firmware();
2926 if (r
< 0 && r
!= -EOPNOTSUPP
)
2927 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2929 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2931 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2932 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2933 if (errno
!= ENOENT
)
2934 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2941 return sd_bus_message_append(reply
, "b", r
> 0);
2944 static int method_set_reboot_to_firmware_setup(
2945 sd_bus_message
*message
,
2947 sd_bus_error
*error
) {
2949 Manager
*m
= ASSERT_PTR(userdata
);
2955 r
= sd_bus_message_read(message
, "b", &b
);
2959 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2961 /* EFI case: let's see what the firmware supports */
2963 r
= efi_reboot_to_firmware_supported();
2964 if (r
== -EOPNOTSUPP
)
2965 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2971 } else if (r
<= 0) {
2972 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2975 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2977 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2979 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2982 r
= bus_verify_polkit_async(
2984 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2985 /* details= */ NULL
,
2986 &m
->polkit_registry
,
2991 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2994 r
= efi_set_reboot_to_firmware(b
);
2999 r
= touch("/run/systemd/reboot-to-firmware-setup");
3003 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
3008 return sd_bus_reply_method_return(message
, NULL
);
3011 static int method_can_reboot_to_firmware_setup(
3012 sd_bus_message
*message
,
3014 sd_bus_error
*error
) {
3016 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3021 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
3023 /* EFI case: let's see what the firmware supports */
3025 r
= efi_reboot_to_firmware_supported();
3027 if (r
!= -EOPNOTSUPP
)
3028 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
3030 return sd_bus_reply_method_return(message
, "s", "na");
3033 } else if (r
<= 0) {
3034 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
3037 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
3039 return sd_bus_reply_method_return(message
, "s", "na");
3042 return return_test_polkit(
3044 "org.freedesktop.login1.set-reboot-to-firmware-setup",
3045 /* details= */ NULL
,
3046 /* good_user= */ UID_INVALID
,
3050 static int property_get_reboot_to_boot_loader_menu(
3053 const char *interface
,
3054 const char *property
,
3055 sd_bus_message
*reply
,
3057 sd_bus_error
*error
) {
3059 uint64_t x
= UINT64_MAX
;
3066 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3068 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished:
3070 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
3071 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
3072 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
3075 r
= efi_loader_get_config_timeout_one_shot(&x
);
3078 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
3082 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3084 _cleanup_free_
char *v
= NULL
;
3086 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
3088 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
3091 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
3093 r
= safe_atou64(v
, &x
);
3095 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
3099 return sd_bus_message_append(reply
, "t", x
);
3102 static int method_set_reboot_to_boot_loader_menu(
3103 sd_bus_message
*message
,
3105 sd_bus_error
*error
) {
3107 Manager
*m
= ASSERT_PTR(userdata
);
3114 r
= sd_bus_message_read(message
, "t", &x
);
3118 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3122 /* EFI case: let's see if booting into boot loader menu is supported. */
3124 r
= efi_loader_get_features(&features
);
3126 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3127 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3128 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3132 } else if (r
<= 0) {
3133 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
3136 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3138 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3140 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
3143 r
= bus_verify_polkit_async(
3145 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3146 /* details= */ NULL
,
3147 &m
->polkit_registry
,
3152 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3155 if (x
== UINT64_MAX
)
3156 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), NULL
, 0);
3158 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3159 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
3161 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), buf
);
3166 if (x
== UINT64_MAX
) {
3167 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
3170 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3172 xsprintf(buf
, "%" PRIu64
, x
); /* μs granularity */
3174 r
= write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf
);
3180 return sd_bus_reply_method_return(message
, NULL
);
3183 static int method_can_reboot_to_boot_loader_menu(
3184 sd_bus_message
*message
,
3186 sd_bus_error
*error
) {
3188 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3193 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3195 uint64_t features
= 0;
3197 /* EFI case, let's see if booting into boot loader menu is supported. */
3199 r
= efi_loader_get_features(&features
);
3201 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3202 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3203 return sd_bus_reply_method_return(message
, "s", "na");
3205 } else if (r
<= 0) {
3206 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
3209 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3211 return sd_bus_reply_method_return(message
, "s", "na");
3214 return return_test_polkit(
3216 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3217 /* details= */ NULL
,
3218 /* good_user= */ UID_INVALID
,
3222 static int property_get_reboot_to_boot_loader_entry(
3225 const char *interface
,
3226 const char *property
,
3227 sd_bus_message
*reply
,
3229 sd_bus_error
*error
) {
3231 _cleanup_free_
char *v
= NULL
;
3232 Manager
*m
= ASSERT_PTR(userdata
);
3233 const char *x
= NULL
;
3239 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3241 /* EFI case: let's read the LoaderEntryOneShot variable */
3243 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
3246 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
3248 x
= m
->efi_loader_entry_one_shot
;
3251 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3254 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
3256 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
3259 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
3260 } else if (!efi_loader_entry_name_valid(v
))
3261 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
3266 return sd_bus_message_append(reply
, "s", x
);
3269 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
3270 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3276 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3277 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
3280 r
= manager_read_efi_boot_loader_entries(m
);
3282 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3284 return !!boot_config_find_entry(&config
, id
);
3287 static int method_set_reboot_to_boot_loader_entry(
3288 sd_bus_message
*message
,
3290 sd_bus_error
*error
) {
3292 Manager
*m
= ASSERT_PTR(userdata
);
3299 r
= sd_bus_message_read(message
, "s", &v
);
3305 else if (efi_loader_entry_name_valid(v
)) {
3306 r
= boot_loader_entry_exists(m
, v
);
3310 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3312 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3314 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3318 /* EFI case: let's see if booting into boot loader entry is supported. */
3320 r
= efi_loader_get_features(&features
);
3322 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3323 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3324 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3328 } else if (r
<= 0) {
3329 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3332 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3334 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3336 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3339 r
= bus_verify_polkit_async(
3341 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3342 /* details= */ NULL
,
3343 &m
->polkit_registry
,
3348 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3353 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), NULL
, 0);
3355 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), v
);
3360 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3363 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3369 return sd_bus_reply_method_return(message
, NULL
);
3372 static int method_can_reboot_to_boot_loader_entry(
3373 sd_bus_message
*message
,
3375 sd_bus_error
*error
) {
3377 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3382 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3384 uint64_t features
= 0;
3386 /* EFI case, let's see if booting into boot loader entry is supported. */
3388 r
= efi_loader_get_features(&features
);
3390 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3391 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3392 return sd_bus_reply_method_return(message
, "s", "na");
3394 } else if (r
<= 0) {
3395 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3398 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3400 return sd_bus_reply_method_return(message
, "s", "na");
3403 return return_test_polkit(
3405 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3406 /* details= */ NULL
,
3407 /* good_user= */ UID_INVALID
,
3411 static int property_get_boot_loader_entries(
3414 const char *interface
,
3415 const char *property
,
3416 sd_bus_message
*reply
,
3418 sd_bus_error
*error
) {
3420 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3421 Manager
*m
= ASSERT_PTR(userdata
);
3428 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3429 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3432 r
= manager_read_efi_boot_loader_entries(m
);
3434 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3436 r
= sd_bus_message_open_container(reply
, 'a', "s");
3440 for (i
= 0; i
< config
.n_entries
; i
++) {
3441 BootEntry
*e
= config
.entries
+ i
;
3443 r
= sd_bus_message_append(reply
, "s", e
->id
);
3448 return sd_bus_message_close_container(reply
);
3451 static int method_set_wall_message(
3452 sd_bus_message
*message
,
3454 sd_bus_error
*error
) {
3457 Manager
*m
= ASSERT_PTR(userdata
);
3459 int enable_wall_messages
;
3463 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3467 if (strlen(wall_message
) > WALL_MESSAGE_MAX
)
3468 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3469 "Wall message too long, maximum permitted length is %u characters.",
3472 /* Short-circuit the operation if the desired state is already in place, to
3473 * avoid an unnecessary polkit permission check. */
3474 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3475 m
->enable_wall_messages
== enable_wall_messages
)
3478 r
= bus_verify_polkit_async(
3480 "org.freedesktop.login1.set-wall-message",
3481 /* details= */ NULL
,
3482 &m
->polkit_registry
,
3487 return 1; /* Will call us back */
3489 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3493 m
->enable_wall_messages
= enable_wall_messages
;
3496 return sd_bus_reply_method_return(message
, NULL
);
3499 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3500 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3501 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
3502 const char *who
, *why
, *what
, *mode
;
3503 _cleanup_free_
char *id
= NULL
;
3504 _cleanup_close_
int fifo_fd
= -EBADF
;
3505 Manager
*m
= ASSERT_PTR(userdata
);
3513 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3517 w
= inhibit_what_from_string(what
);
3519 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3520 "Invalid what specification %s", what
);
3522 mm
= inhibit_mode_from_string(mode
);
3524 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3525 "Invalid mode specification %s", mode
);
3527 /* Delay is only supported for shutdown/sleep */
3528 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3529 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3530 "Delay inhibitors only supported for shutdown and sleep");
3532 /* Don't allow taking delay locks while we are already
3533 * executing the operation. We shouldn't create the impression
3534 * that the lock was successful if the machine is about to go
3535 * down/suspend any moment. */
3536 if (m
->delayed_action
&& m
->delayed_action
->inhibit_what
& w
)
3537 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3538 "The operation inhibition has been requested for is already running");
3540 r
= bus_verify_polkit_async(
3542 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3543 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3544 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3545 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3546 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3547 w
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3548 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3549 "org.freedesktop.login1.inhibit-handle-lid-switch",
3550 /* details= */ NULL
,
3551 &m
->polkit_registry
,
3556 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3558 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
|SD_BUS_CREDS_PIDFD
, &creds
);
3562 r
= sd_bus_creds_get_euid(creds
, &uid
);
3566 r
= bus_creds_get_pidref(creds
, &pidref
);
3570 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3571 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3572 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3578 if (asprintf(&id
, "%" PRIu64
, ++m
->inhibit_counter
) < 0)
3581 } while (hashmap_get(m
->inhibitors
, id
));
3583 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3584 r
= manager_add_inhibitor(m
, id
, &i
);
3590 i
->pid
= TAKE_PIDREF(pidref
);
3592 i
->why
= strdup(why
);
3593 i
->who
= strdup(who
);
3595 if (!i
->why
|| !i
->who
)
3598 fifo_fd
= inhibitor_create_fifo(i
);
3602 r
= inhibitor_start(i
);
3607 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3610 static const sd_bus_vtable manager_vtable
[] = {
3611 SD_BUS_VTABLE_START(0),
3613 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool
, bus_property_set_bool
, offsetof(Manager
, enable_wall_messages
), 0),
3614 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3616 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3617 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3618 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3619 SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3620 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3621 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3622 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3623 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3624 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3625 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3626 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3627 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3628 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3629 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3630 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3631 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3632 SD_BUS_PROPERTY("SleepOperation", "as", property_get_sleep_operations
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3633 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3634 SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3635 SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3636 SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3637 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3638 SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3639 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3640 SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3641 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3642 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3643 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3644 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3645 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3646 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3647 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3648 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3649 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3650 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3651 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3652 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3653 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3654 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3655 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3656 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3657 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3658 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3659 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3660 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3661 SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL
, offsetof(Manager
, stop_idle_session_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3663 SD_BUS_METHOD_WITH_ARGS("GetSession",
3664 SD_BUS_ARGS("s", session_id
),
3665 SD_BUS_RESULT("o", object_path
),
3667 SD_BUS_VTABLE_UNPRIVILEGED
),
3668 SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
3669 SD_BUS_ARGS("u", pid
),
3670 SD_BUS_RESULT("o", object_path
),
3671 method_get_session_by_pid
,
3672 SD_BUS_VTABLE_UNPRIVILEGED
),
3673 SD_BUS_METHOD_WITH_ARGS("GetUser",
3674 SD_BUS_ARGS("u", uid
),
3675 SD_BUS_RESULT("o", object_path
),
3677 SD_BUS_VTABLE_UNPRIVILEGED
),
3678 SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
3679 SD_BUS_ARGS("u", pid
),
3680 SD_BUS_RESULT("o", object_path
),
3681 method_get_user_by_pid
,
3682 SD_BUS_VTABLE_UNPRIVILEGED
),
3683 SD_BUS_METHOD_WITH_ARGS("GetSeat",
3684 SD_BUS_ARGS("s", seat_id
),
3685 SD_BUS_RESULT("o", object_path
),
3687 SD_BUS_VTABLE_UNPRIVILEGED
),
3688 SD_BUS_METHOD_WITH_ARGS("ListSessions",
3690 SD_BUS_RESULT("a(susso)", sessions
),
3691 method_list_sessions
,
3692 SD_BUS_VTABLE_UNPRIVILEGED
),
3693 SD_BUS_METHOD_WITH_ARGS("ListSessionsEx",
3695 SD_BUS_RESULT("a(sussussbto)", sessions
),
3696 method_list_sessions_ex
,
3697 SD_BUS_VTABLE_UNPRIVILEGED
),
3698 SD_BUS_METHOD_WITH_ARGS("ListUsers",
3700 SD_BUS_RESULT("a(uso)", users
),
3702 SD_BUS_VTABLE_UNPRIVILEGED
),
3703 SD_BUS_METHOD_WITH_ARGS("ListSeats",
3705 SD_BUS_RESULT("a(so)", seats
),
3707 SD_BUS_VTABLE_UNPRIVILEGED
),
3708 SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
3710 SD_BUS_RESULT("a(ssssuu)", inhibitors
),
3711 method_list_inhibitors
,
3712 SD_BUS_VTABLE_UNPRIVILEGED
),
3713 SD_BUS_METHOD_WITH_ARGS("CreateSession",
3714 SD_BUS_ARGS("u", uid
,
3727 "a(sv)", properties
),
3728 SD_BUS_RESULT("s", session_id
,
3736 method_create_session
,
3738 SD_BUS_METHOD_WITH_ARGS("CreateSessionWithPIDFD",
3739 SD_BUS_ARGS("u", uid
,
3753 "a(sv)", properties
),
3754 SD_BUS_RESULT("s", session_id
,
3762 method_create_session_pidfd
,
3764 SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
3765 SD_BUS_ARGS("s", session_id
),
3767 method_release_session
,
3769 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
3770 SD_BUS_ARGS("s", session_id
),
3772 method_activate_session
,
3773 SD_BUS_VTABLE_UNPRIVILEGED
),
3774 SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
3775 SD_BUS_ARGS("s", session_id
, "s", seat_id
),
3777 method_activate_session_on_seat
,
3778 SD_BUS_VTABLE_UNPRIVILEGED
),
3779 SD_BUS_METHOD_WITH_ARGS("LockSession",
3780 SD_BUS_ARGS("s", session_id
),
3782 method_lock_session
,
3783 SD_BUS_VTABLE_UNPRIVILEGED
),
3784 SD_BUS_METHOD_WITH_ARGS("UnlockSession",
3785 SD_BUS_ARGS("s", session_id
),
3787 method_lock_session
,
3788 SD_BUS_VTABLE_UNPRIVILEGED
),
3789 SD_BUS_METHOD("LockSessions",
3792 method_lock_sessions
,
3793 SD_BUS_VTABLE_UNPRIVILEGED
),
3794 SD_BUS_METHOD("UnlockSessions",
3797 method_lock_sessions
,
3798 SD_BUS_VTABLE_UNPRIVILEGED
),
3799 SD_BUS_METHOD_WITH_ARGS("KillSession",
3800 SD_BUS_ARGS("s", session_id
, "s", who
, "i", signal_number
),
3802 method_kill_session
,
3803 SD_BUS_VTABLE_UNPRIVILEGED
),
3804 SD_BUS_METHOD_WITH_ARGS("KillUser",
3805 SD_BUS_ARGS("u", uid
, "i", signal_number
),
3808 SD_BUS_VTABLE_UNPRIVILEGED
),
3809 SD_BUS_METHOD_WITH_ARGS("TerminateSession",
3810 SD_BUS_ARGS("s", session_id
),
3812 method_terminate_session
,
3813 SD_BUS_VTABLE_UNPRIVILEGED
),
3814 SD_BUS_METHOD_WITH_ARGS("TerminateUser",
3815 SD_BUS_ARGS("u", uid
),
3817 method_terminate_user
,
3818 SD_BUS_VTABLE_UNPRIVILEGED
),
3819 SD_BUS_METHOD_WITH_ARGS("TerminateSeat",
3820 SD_BUS_ARGS("s", seat_id
),
3822 method_terminate_seat
,
3823 SD_BUS_VTABLE_UNPRIVILEGED
),
3824 SD_BUS_METHOD_WITH_ARGS("SetUserLinger",
3825 SD_BUS_ARGS("u", uid
, "b", enable
, "b", interactive
),
3827 method_set_user_linger
,
3828 SD_BUS_VTABLE_UNPRIVILEGED
),
3829 SD_BUS_METHOD_WITH_ARGS("AttachDevice",
3830 SD_BUS_ARGS("s", seat_id
, "s", sysfs_path
, "b", interactive
),
3832 method_attach_device
,
3833 SD_BUS_VTABLE_UNPRIVILEGED
),
3834 SD_BUS_METHOD_WITH_ARGS("FlushDevices",
3835 SD_BUS_ARGS("b", interactive
),
3837 method_flush_devices
,
3838 SD_BUS_VTABLE_UNPRIVILEGED
),
3839 SD_BUS_METHOD_WITH_ARGS("PowerOff",
3840 SD_BUS_ARGS("b", interactive
),
3843 SD_BUS_VTABLE_UNPRIVILEGED
),
3844 SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags",
3845 SD_BUS_ARGS("t", flags
),
3848 SD_BUS_VTABLE_UNPRIVILEGED
),
3849 SD_BUS_METHOD_WITH_ARGS("Reboot",
3850 SD_BUS_ARGS("b", interactive
),
3853 SD_BUS_VTABLE_UNPRIVILEGED
),
3854 SD_BUS_METHOD_WITH_ARGS("RebootWithFlags",
3855 SD_BUS_ARGS("t", flags
),
3858 SD_BUS_VTABLE_UNPRIVILEGED
),
3859 SD_BUS_METHOD_WITH_ARGS("Halt",
3860 SD_BUS_ARGS("b", interactive
),
3863 SD_BUS_VTABLE_UNPRIVILEGED
),
3864 SD_BUS_METHOD_WITH_ARGS("HaltWithFlags",
3865 SD_BUS_ARGS("t", flags
),
3868 SD_BUS_VTABLE_UNPRIVILEGED
),
3869 SD_BUS_METHOD_WITH_ARGS("Suspend",
3870 SD_BUS_ARGS("b", interactive
),
3873 SD_BUS_VTABLE_UNPRIVILEGED
),
3874 SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags",
3875 SD_BUS_ARGS("t", flags
),
3878 SD_BUS_VTABLE_UNPRIVILEGED
),
3879 SD_BUS_METHOD_WITH_ARGS("Hibernate",
3880 SD_BUS_ARGS("b", interactive
),
3883 SD_BUS_VTABLE_UNPRIVILEGED
),
3884 SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags",
3885 SD_BUS_ARGS("t", flags
),
3888 SD_BUS_VTABLE_UNPRIVILEGED
),
3889 SD_BUS_METHOD_WITH_ARGS("HybridSleep",
3890 SD_BUS_ARGS("b", interactive
),
3892 method_hybrid_sleep
,
3893 SD_BUS_VTABLE_UNPRIVILEGED
),
3894 SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags",
3895 SD_BUS_ARGS("t", flags
),
3897 method_hybrid_sleep
,
3898 SD_BUS_VTABLE_UNPRIVILEGED
),
3899 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate",
3900 SD_BUS_ARGS("b", interactive
),
3902 method_suspend_then_hibernate
,
3903 SD_BUS_VTABLE_UNPRIVILEGED
),
3904 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags",
3905 SD_BUS_ARGS("t", flags
),
3907 method_suspend_then_hibernate
,
3908 SD_BUS_VTABLE_UNPRIVILEGED
),
3909 SD_BUS_METHOD_WITH_ARGS("Sleep",
3910 SD_BUS_ARGS("t", flags
),
3913 SD_BUS_VTABLE_UNPRIVILEGED
),
3914 SD_BUS_METHOD_WITH_ARGS("CanPowerOff",
3916 SD_BUS_RESULT("s", result
),
3917 method_can_poweroff
,
3918 SD_BUS_VTABLE_UNPRIVILEGED
),
3919 SD_BUS_METHOD_WITH_ARGS("CanReboot",
3921 SD_BUS_RESULT("s", result
),
3923 SD_BUS_VTABLE_UNPRIVILEGED
),
3924 SD_BUS_METHOD_WITH_ARGS("CanHalt",
3926 SD_BUS_RESULT("s", result
),
3928 SD_BUS_VTABLE_UNPRIVILEGED
),
3929 SD_BUS_METHOD_WITH_ARGS("CanSuspend",
3931 SD_BUS_RESULT("s", result
),
3933 SD_BUS_VTABLE_UNPRIVILEGED
),
3934 SD_BUS_METHOD_WITH_ARGS("CanHibernate",
3936 SD_BUS_RESULT("s", result
),
3937 method_can_hibernate
,
3938 SD_BUS_VTABLE_UNPRIVILEGED
),
3939 SD_BUS_METHOD_WITH_ARGS("CanHybridSleep",
3941 SD_BUS_RESULT("s", result
),
3942 method_can_hybrid_sleep
,
3943 SD_BUS_VTABLE_UNPRIVILEGED
),
3944 SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate",
3946 SD_BUS_RESULT("s", result
),
3947 method_can_suspend_then_hibernate
,
3948 SD_BUS_VTABLE_UNPRIVILEGED
),
3949 SD_BUS_METHOD_WITH_ARGS("CanSleep",
3951 SD_BUS_RESULT("s", result
),
3953 SD_BUS_VTABLE_UNPRIVILEGED
),
3954 SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown",
3955 SD_BUS_ARGS("s", type
, "t", usec
),
3957 method_schedule_shutdown
,
3958 SD_BUS_VTABLE_UNPRIVILEGED
),
3959 SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown",
3961 SD_BUS_RESULT("b", cancelled
),
3962 method_cancel_scheduled_shutdown
,
3963 SD_BUS_VTABLE_UNPRIVILEGED
),
3964 SD_BUS_METHOD_WITH_ARGS("Inhibit",
3965 SD_BUS_ARGS("s", what
, "s", who
, "s", why
, "s", mode
),
3966 SD_BUS_RESULT("h", pipe_fd
),
3968 SD_BUS_VTABLE_UNPRIVILEGED
),
3969 SD_BUS_METHOD_WITH_ARGS("CanRebootParameter",
3971 SD_BUS_RESULT("s", result
),
3972 method_can_reboot_parameter
,
3973 SD_BUS_VTABLE_UNPRIVILEGED
),
3974 SD_BUS_METHOD_WITH_ARGS("SetRebootParameter",
3975 SD_BUS_ARGS("s", parameter
),
3977 method_set_reboot_parameter
,
3978 SD_BUS_VTABLE_UNPRIVILEGED
),
3979 SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup",
3981 SD_BUS_RESULT("s", result
),
3982 method_can_reboot_to_firmware_setup
,
3983 SD_BUS_VTABLE_UNPRIVILEGED
),
3984 SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup",
3985 SD_BUS_ARGS("b", enable
),
3987 method_set_reboot_to_firmware_setup
,
3988 SD_BUS_VTABLE_UNPRIVILEGED
),
3989 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu",
3991 SD_BUS_RESULT("s", result
),
3992 method_can_reboot_to_boot_loader_menu
,
3993 SD_BUS_VTABLE_UNPRIVILEGED
),
3994 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu",
3995 SD_BUS_ARGS("t", timeout
),
3997 method_set_reboot_to_boot_loader_menu
,
3998 SD_BUS_VTABLE_UNPRIVILEGED
),
3999 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry",
4001 SD_BUS_RESULT("s", result
),
4002 method_can_reboot_to_boot_loader_entry
,
4003 SD_BUS_VTABLE_UNPRIVILEGED
),
4004 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry",
4005 SD_BUS_ARGS("s", boot_loader_entry
),
4007 method_set_reboot_to_boot_loader_entry
,
4008 SD_BUS_VTABLE_UNPRIVILEGED
),
4009 SD_BUS_METHOD_WITH_ARGS("SetWallMessage",
4010 SD_BUS_ARGS("s", wall_message
, "b", enable
),
4012 method_set_wall_message
,
4013 SD_BUS_VTABLE_UNPRIVILEGED
),
4015 SD_BUS_SIGNAL_WITH_ARGS("SessionNew",
4016 SD_BUS_ARGS("s", session_id
, "o", object_path
),
4018 SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved",
4019 SD_BUS_ARGS("s", session_id
, "o", object_path
),
4021 SD_BUS_SIGNAL_WITH_ARGS("UserNew",
4022 SD_BUS_ARGS("u", uid
, "o", object_path
),
4024 SD_BUS_SIGNAL_WITH_ARGS("UserRemoved",
4025 SD_BUS_ARGS("u", uid
, "o", object_path
),
4027 SD_BUS_SIGNAL_WITH_ARGS("SeatNew",
4028 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4030 SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved",
4031 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4033 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown",
4034 SD_BUS_ARGS("b", start
),
4036 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdownWithMetadata",
4037 SD_BUS_ARGS("b", start
, "a{sv}", metadata
),
4039 SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep",
4040 SD_BUS_ARGS("b", start
),
4046 const BusObjectImplementation manager_object
= {
4047 "/org/freedesktop/login1",
4048 "org.freedesktop.login1.Manager",
4049 .vtables
= BUS_VTABLES(manager_vtable
),
4050 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
4055 static void session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
4062 if (result
&& !streq(result
, "done")) {
4063 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
4065 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
4066 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
4068 (void) session_send_create_reply(s
, &e
);
4069 (void) session_send_upgrade_reply(s
, &e
);
4073 (void) session_send_create_reply(s
, /* error= */ NULL
);
4074 (void) session_send_upgrade_reply(s
, /* error= */ NULL
);
4077 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4078 Manager
*m
= ASSERT_PTR(userdata
);
4079 const char *path
, *result
, *unit
;
4085 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
4087 bus_log_parse_error(r
);
4091 if (m
->action_job
&& streq(m
->action_job
, path
)) {
4092 assert(m
->delayed_action
);
4093 log_info("Operation '%s' finished.", handle_action_to_string(m
->delayed_action
->handle
));
4095 /* Tell people that they now may take a lock again */
4096 (void) send_prepare_for(m
, m
->delayed_action
, false);
4098 m
->action_job
= mfree(m
->action_job
);
4099 m
->delayed_action
= NULL
;
4106 session
= hashmap_get(m
->session_units
, unit
);
4108 if (streq_ptr(path
, session
->scope_job
)) {
4109 session
->scope_job
= mfree(session
->scope_job
);
4110 session_jobs_reply(session
, id
, unit
, result
);
4112 session_save(session
);
4113 user_save(session
->user
);
4116 session_add_to_gc_queue(session
);
4119 user
= hashmap_get(m
->user_units
, unit
);
4121 /* If the user is stopping, we're tracking stop jobs here. So don't send reply. */
4122 if (!user
->stopping
) {
4124 FOREACH_ARGUMENT(user_job
, &user
->runtime_dir_job
, &user
->service_manager_job
)
4125 if (streq_ptr(path
, *user_job
)) {
4126 *user_job
= mfree(*user_job
);
4128 LIST_FOREACH(sessions_by_user
, s
, user
->sessions
)
4129 /* Don't propagate user service failures to the client */
4130 session_jobs_reply(s
, id
, unit
, /* error = */ NULL
);
4137 user_add_to_gc_queue(user
);
4143 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4144 const char *path
, *unit
;
4145 Manager
*m
= ASSERT_PTR(userdata
);
4152 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
4154 bus_log_parse_error(r
);
4158 session
= hashmap_get(m
->session_units
, unit
);
4160 session_add_to_gc_queue(session
);
4162 user
= hashmap_get(m
->user_units
, unit
);
4164 user_add_to_gc_queue(user
);
4169 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4170 _cleanup_free_
char *unit
= NULL
;
4171 Manager
*m
= ASSERT_PTR(userdata
);
4179 path
= sd_bus_message_get_path(message
);
4183 r
= unit_name_from_dbus_path(path
, &unit
);
4184 if (r
== -EINVAL
) /* not a unit */
4191 session
= hashmap_get(m
->session_units
, unit
);
4193 session_add_to_gc_queue(session
);
4195 user
= hashmap_get(m
->user_units
, unit
);
4197 user_add_to_gc_queue(user
);
4202 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4203 Manager
*m
= ASSERT_PTR(userdata
);
4209 r
= sd_bus_message_read(message
, "b", &b
);
4211 bus_log_parse_error(r
);
4218 /* systemd finished reloading, let's recheck all our sessions */
4219 log_debug("System manager has been reloaded, rechecking sessions...");
4221 HASHMAP_FOREACH(session
, m
->sessions
)
4222 session_add_to_gc_queue(session
);
4227 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
4232 l
= strv_from_stdarg_alloca(property
);
4234 return sd_bus_emit_properties_changed_strv(
4236 "/org/freedesktop/login1",
4237 "org.freedesktop.login1.Manager",
4241 int manager_start_scope(
4244 const PidRef
*pidref
,
4246 const char *description
,
4247 const char * const *requires
,
4248 const char * const *extra_after
,
4249 const char *requires_mounts_for
,
4250 sd_bus_message
*more_properties
,
4251 sd_bus_error
*error
,
4254 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
4259 assert(pidref_is_set(pidref
));
4262 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
4266 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
4270 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
4274 if (!isempty(slice
)) {
4275 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
4280 if (!isempty(description
)) {
4281 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4286 STRV_FOREACH(i
, requires
) {
4287 r
= sd_bus_message_append(m
, "(sv)", "Requires", "as", 1, *i
);
4291 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4296 STRV_FOREACH(i
, extra_after
) {
4297 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4302 if (!empty_or_root(requires_mounts_for
)) {
4303 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4308 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4310 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4314 r
= bus_append_scope_pidref(m
, pidref
);
4318 /* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of
4320 r
= sd_bus_message_append(m
, "(sv)", "OOMPolicy", "s", "continue");
4324 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4325 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_MAX
);
4327 return bus_log_create_error(r
);
4329 if (more_properties
) {
4330 /* If TasksMax also appears here, it will overwrite the default value set above */
4331 r
= sd_bus_message_copy(m
, more_properties
, true);
4336 r
= sd_bus_message_close_container(m
);
4340 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4344 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4348 return strdup_job(reply
, ret_job
);
4351 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **ret_job
) {
4352 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4359 r
= bus_call_method(
4365 "ss", unit
, "replace");
4369 return strdup_job(reply
, ret_job
);
4372 int manager_stop_unit(
4375 const char *job_mode
,
4376 sd_bus_error
*ret_error
,
4379 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4380 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4387 r
= bus_call_method(
4393 "ss", unit
, job_mode
?: "fail");
4395 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
, BUS_ERROR_LOAD_FAILED
)) {
4400 sd_bus_error_move(ret_error
, &error
);
4404 r
= strdup_job(reply
, ret_job
);
4411 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4412 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4413 _cleanup_free_
char *path
= NULL
;
4419 path
= unit_dbus_path_from_name(scope
);
4423 r
= sd_bus_call_method(
4425 "org.freedesktop.systemd1",
4427 "org.freedesktop.systemd1.Scope",
4433 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4434 BUS_ERROR_LOAD_FAILED
,
4435 BUS_ERROR_SCOPE_NOT_RUNNING
))
4438 sd_bus_error_move(ret_error
, &error
);
4445 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4448 assert(SIGNAL_VALID(signo
));
4450 return bus_call_method(
4458 who
== KILL_LEADER
? "main" : "all",
4462 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4463 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4464 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4465 _cleanup_free_
char *path
= NULL
;
4472 path
= unit_dbus_path_from_name(unit
);
4476 r
= sd_bus_get_property(
4478 "org.freedesktop.systemd1",
4480 "org.freedesktop.systemd1.Unit",
4486 /* systemd might have dropped off momentarily, let's
4487 * not make this an error */
4488 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4489 SD_BUS_ERROR_DISCONNECTED
))
4492 /* If the unit is already unloaded then it's not
4494 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4495 BUS_ERROR_LOAD_FAILED
))
4498 sd_bus_error_move(ret_error
, &error
);
4502 r
= sd_bus_message_read(reply
, "s", &state
);
4506 return !STR_IN_SET(state
, "inactive", "failed");
4509 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4510 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4511 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4517 r
= sd_bus_get_property(
4519 "org.freedesktop.systemd1",
4521 "org.freedesktop.systemd1.Job",
4527 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4528 SD_BUS_ERROR_DISCONNECTED
))
4531 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4534 sd_bus_error_move(ret_error
, &error
);
4538 /* We don't actually care about the state really. The fact
4539 * that we could read the job state is enough for us */