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 "stdio-util.h"
54 #include "terminal-util.h"
55 #include "tmpfile-util.h"
56 #include "unit-name.h"
57 #include "user-util.h"
58 #include "utmp-wtmp.h"
62 /* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1))
63 * character limit for the wall message.
64 * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72
65 * There is no real technical need for that but doesn't make sense
66 * to store arbitrary amounts either. As we are not stingy here, we
69 #define WALL_MESSAGE_MAX 4096U
71 #define SHUTDOWN_SCHEDULE_FILE "/run/systemd/shutdown/scheduled"
73 static int update_schedule_file(Manager
*m
);
74 static void reset_scheduled_shutdown(Manager
*m
);
75 static int manager_setup_shutdown_timers(Manager
* m
);
77 static int get_sender_session(
79 sd_bus_message
*message
,
84 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
85 Session
*session
= NULL
;
89 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
90 * and returns that. If not and 'consult_display' is true, this returns the display session of the
91 * owning user of the caller. */
93 r
= sd_bus_query_sender_creds(message
,
94 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
95 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
99 r
= sd_bus_creds_get_session(creds
, &name
);
104 if (consult_display
) {
107 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
114 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
116 session
= user
->display
;
120 session
= hashmap_get(m
->sessions
, name
);
123 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
125 "Caller does not belong to any known session and doesn't own any suitable session." :
126 "Caller does not belong to any known session.");
132 int manager_get_session_from_creds(
134 sd_bus_message
*message
,
144 if (SEAT_IS_SELF(name
)) /* the caller's own session */
145 return get_sender_session(m
, message
, false, error
, ret
);
146 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
147 return get_sender_session(m
, message
, true, error
, ret
);
149 session
= hashmap_get(m
->sessions
, name
);
151 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
157 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
158 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
163 /* Note that we get the owner UID of the session, not the actual client UID here! */
164 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
168 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
175 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
178 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
179 "Caller does not belong to any logged in or lingering user");
185 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
191 if (!uid_is_valid(uid
))
192 return get_sender_user(m
, message
, error
, ret
);
194 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
196 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
197 "User ID "UID_FMT
" is not logged in or lingering", uid
);
203 int manager_get_seat_from_creds(
205 sd_bus_message
*message
,
216 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
219 /* Use these special seat names as session names */
220 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
224 seat
= session
->seat
;
226 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
228 seat
= hashmap_get(m
->seats
, name
);
230 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
237 static int return_test_polkit(
238 sd_bus_message
*message
,
240 const char **details
,
248 r
= bus_test_polkit(message
, action
, details
, good_user
, &challenge
, e
);
255 result
= "challenge";
259 return sd_bus_reply_method_return(message
, "s", result
);
262 static int property_get_idle_hint(
265 const char *interface
,
266 const char *property
,
267 sd_bus_message
*reply
,
269 sd_bus_error
*error
) {
271 Manager
*m
= ASSERT_PTR(userdata
);
276 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
279 static int property_get_idle_since_hint(
282 const char *interface
,
283 const char *property
,
284 sd_bus_message
*reply
,
286 sd_bus_error
*error
) {
288 Manager
*m
= ASSERT_PTR(userdata
);
289 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
294 manager_get_idle_hint(m
, &t
);
296 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
299 static int property_get_inhibited(
302 const char *interface
,
303 const char *property
,
304 sd_bus_message
*reply
,
306 sd_bus_error
*error
) {
308 Manager
*m
= ASSERT_PTR(userdata
);
314 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
316 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
319 static int property_get_preparing(
322 const char *interface
,
323 const char *property
,
324 sd_bus_message
*reply
,
326 sd_bus_error
*error
) {
328 Manager
*m
= ASSERT_PTR(userdata
);
334 if (m
->delayed_action
) {
335 if (streq(property
, "PreparingForShutdown"))
336 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SHUTDOWN
;
338 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SLEEP
;
341 return sd_bus_message_append(reply
, "b", b
);
344 static int property_get_sleep_operations(
347 const char *interface
,
348 const char *property
,
349 sd_bus_message
*reply
,
351 sd_bus_error
*error
) {
353 Manager
*m
= ASSERT_PTR(userdata
);
354 _cleanup_strv_free_
char **actions
= NULL
;
360 r
= handle_action_get_enabled_sleep_actions(m
->handle_action_sleep_mask
, &actions
);
364 return sd_bus_message_append_strv(reply
, actions
);
367 static int property_get_scheduled_shutdown(
370 const char *interface
,
371 const char *property
,
372 sd_bus_message
*reply
,
374 sd_bus_error
*error
) {
376 Manager
*m
= ASSERT_PTR(userdata
);
382 r
= sd_bus_message_open_container(reply
, 'r', "st");
386 r
= sd_bus_message_append(
388 handle_action_to_string(m
->scheduled_shutdown_action
),
389 m
->scheduled_shutdown_timeout
);
393 return sd_bus_message_close_container(reply
);
396 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
397 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
398 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
399 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power());
400 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
401 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
403 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
404 _cleanup_free_
char *p
= NULL
;
405 Manager
*m
= ASSERT_PTR(userdata
);
412 r
= sd_bus_message_read(message
, "s", &name
);
416 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
420 p
= session_bus_path(session
);
424 return sd_bus_reply_method_return(message
, "o", p
);
427 /* Get login session of a process. This is not what you are looking for these days,
428 * as apps may instead belong to a user service unit. This includes terminal
429 * emulators and hence command-line apps. */
430 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
431 Manager
*m
= ASSERT_PTR(userdata
);
432 _cleanup_free_
char *p
= NULL
;
433 Session
*session
= NULL
;
439 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
441 r
= sd_bus_message_read(message
, "u", &pid
);
448 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
452 r
= manager_get_session_by_pidref(m
, &PIDREF_MAKE_FROM_PID(pid
), &session
);
457 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
458 "PID "PID_FMT
" does not belong to any known session", pid
);
461 p
= session_bus_path(session
);
465 return sd_bus_reply_method_return(message
, "o", p
);
468 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
469 _cleanup_free_
char *p
= NULL
;
470 Manager
*m
= ASSERT_PTR(userdata
);
477 r
= sd_bus_message_read(message
, "u", &uid
);
481 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
485 p
= user_bus_path(user
);
489 return sd_bus_reply_method_return(message
, "o", p
);
492 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
493 _cleanup_free_
char *p
= NULL
;
494 Manager
*m
= ASSERT_PTR(userdata
);
501 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
503 r
= sd_bus_message_read(message
, "u", &pid
);
510 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
514 r
= manager_get_user_by_pid(m
, pid
, &user
);
518 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
519 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
523 p
= user_bus_path(user
);
527 return sd_bus_reply_method_return(message
, "o", p
);
530 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
531 _cleanup_free_
char *p
= NULL
;
532 Manager
*m
= ASSERT_PTR(userdata
);
539 r
= sd_bus_message_read(message
, "s", &name
);
543 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
547 p
= seat_bus_path(seat
);
551 return sd_bus_reply_method_return(message
, "o", p
);
554 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
555 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
556 Manager
*m
= ASSERT_PTR(userdata
);
562 r
= sd_bus_message_new_method_return(message
, &reply
);
566 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
570 HASHMAP_FOREACH(session
, m
->sessions
) {
571 _cleanup_free_
char *p
= NULL
;
573 p
= session_bus_path(session
);
577 r
= sd_bus_message_append(reply
, "(susso)",
579 (uint32_t) session
->user
->user_record
->uid
,
580 session
->user
->user_record
->user_name
,
581 session
->seat
? session
->seat
->id
: "",
587 r
= sd_bus_message_close_container(reply
);
591 return sd_bus_send(NULL
, reply
, NULL
);
594 static int method_list_sessions_ex(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
595 Manager
*m
= ASSERT_PTR(userdata
);
596 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
601 r
= sd_bus_message_new_method_return(message
, &reply
);
605 r
= sd_bus_message_open_container(reply
, 'a', "(sussussbto)");
610 HASHMAP_FOREACH(s
, m
->sessions
) {
611 _cleanup_free_
char *path
= NULL
;
612 dual_timestamp idle_ts
;
617 path
= session_bus_path(s
);
621 r
= session_get_idle_hint(s
, &idle_ts
);
626 r
= sd_bus_message_append(reply
, "(sussussbto)",
628 (uint32_t) s
->user
->user_record
->uid
,
629 s
->user
->user_record
->user_name
,
630 s
->seat
? s
->seat
->id
: "",
631 (uint32_t) s
->leader
.pid
,
632 session_class_to_string(s
->class),
641 r
= sd_bus_message_close_container(reply
);
645 return sd_bus_send(NULL
, reply
, NULL
);
648 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
649 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
650 Manager
*m
= ASSERT_PTR(userdata
);
656 r
= sd_bus_message_new_method_return(message
, &reply
);
660 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
664 HASHMAP_FOREACH(user
, m
->users
) {
665 _cleanup_free_
char *p
= NULL
;
667 p
= user_bus_path(user
);
671 r
= sd_bus_message_append(reply
, "(uso)",
672 (uint32_t) user
->user_record
->uid
,
673 user
->user_record
->user_name
,
679 r
= sd_bus_message_close_container(reply
);
683 return sd_bus_send(NULL
, reply
, NULL
);
686 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
687 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
688 Manager
*m
= ASSERT_PTR(userdata
);
694 r
= sd_bus_message_new_method_return(message
, &reply
);
698 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
702 HASHMAP_FOREACH(seat
, m
->seats
) {
703 _cleanup_free_
char *p
= NULL
;
705 p
= seat_bus_path(seat
);
709 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
714 r
= sd_bus_message_close_container(reply
);
718 return sd_bus_send(NULL
, reply
, NULL
);
721 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
722 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
723 Manager
*m
= ASSERT_PTR(userdata
);
724 Inhibitor
*inhibitor
;
729 r
= sd_bus_message_new_method_return(message
, &reply
);
733 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
737 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
) {
739 r
= sd_bus_message_append(reply
, "(ssssuu)",
740 strempty(inhibit_what_to_string(inhibitor
->what
)),
741 strempty(inhibitor
->who
),
742 strempty(inhibitor
->why
),
743 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
744 (uint32_t) inhibitor
->uid
,
745 (uint32_t) inhibitor
->pid
.pid
);
750 r
= sd_bus_message_close_container(reply
);
754 return sd_bus_send(NULL
, reply
, NULL
);
757 static int create_session(
758 sd_bus_message
*message
,
773 const char *remote_user
,
774 const char *remote_host
,
777 _cleanup_(pidref_done
) PidRef leader
= PIDREF_NULL
;
778 Manager
*m
= ASSERT_PTR(userdata
);
779 _cleanup_free_
char *id
= NULL
;
780 Session
*session
= NULL
;
781 uint32_t audit_id
= 0;
790 if (!uid_is_valid(uid
))
791 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
794 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Flags must be zero.");
797 r
= pidref_set_pidfd(&leader
, pidfd
);
800 } else if (pid
== 0) {
801 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
804 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
808 r
= sd_bus_creds_get_pid(creds
, &p
);
812 r
= pidref_set_pid(&leader
, p
);
818 r
= pidref_set_pid(&leader
, pid
);
823 if (leader
.pid
== 1 || leader
.pid
== getpid_cached())
824 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
827 t
= _SESSION_TYPE_INVALID
;
829 t
= session_type_from_string(type
);
831 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
832 "Invalid session type %s", type
);
836 c
= _SESSION_CLASS_INVALID
;
838 c
= session_class_from_string(class);
840 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
841 "Invalid session class %s", class);
844 if (isempty(desktop
))
847 if (!string_is_safe(desktop
))
848 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
849 "Invalid desktop string %s", desktop
);
855 seat
= hashmap_get(m
->seats
, cseat
);
857 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
858 "No seat '%s' known", cseat
);
861 if (tty_is_vc(tty
)) {
866 else if (seat
!= m
->seat0
)
867 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
868 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
870 v
= vtnr_from_tty(tty
);
872 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
873 "Cannot determine VT number from virtual console TTY %s", tty
);
877 else if (vtnr
!= (uint32_t) v
)
878 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
879 "Specified TTY and VT number do not match");
881 } else if (tty_is_console(tty
)) {
885 else if (seat
!= m
->seat0
)
886 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
887 "Console TTY specified but seat is not seat0");
890 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
891 "Console TTY specified but VT number is not 0");
895 if (seat_has_vts(seat
)) {
896 if (vtnr
<= 0 || vtnr
> 63)
897 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
898 "VT number out of range");
901 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
902 "Seat has no VTs but VT number not 0");
906 if (t
== _SESSION_TYPE_INVALID
) {
907 if (!isempty(display
))
909 else if (!isempty(tty
))
912 t
= SESSION_UNSPECIFIED
;
915 if (c
== _SESSION_CLASS_INVALID
) {
916 if (t
== SESSION_UNSPECIFIED
)
917 c
= SESSION_BACKGROUND
;
922 /* Check if we are already in a logind session, and if so refuse. */
923 r
= manager_get_session_by_pidref(m
, &leader
, /* ret_session= */ NULL
);
927 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
928 "Already running in a session or user slice");
930 /* Old gdm and lightdm start the user-session on the same VT as the greeter session. But they destroy
931 * the greeter session after the user-session and want the user-session to take over the VT. We need
932 * to support this for backwards-compatibility, so make sure we allow new sessions on a VT that a
933 * greeter is running on. Furthermore, to allow re-logins, we have to allow a greeter to take over a
934 * used VT for the exact same reasons. */
935 if (c
!= SESSION_GREETER
&&
937 vtnr
< MALLOC_ELEMENTSOF(m
->seat0
->positions
) &&
938 m
->seat0
->positions
[vtnr
] &&
939 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
940 return sd_bus_error_set(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
942 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
943 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
944 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
947 (void) audit_session_from_pid(leader
.pid
, &audit_id
);
948 if (audit_session_is_valid(audit_id
)) {
949 /* Keep our session IDs and the audit session IDs in sync */
951 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
954 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
955 * not trust the audit data and let's better register a new ID */
956 if (hashmap_contains(m
->sessions
, id
)) {
957 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
958 audit_id
= AUDIT_SESSION_INVALID
;
967 if (asprintf(&id
, "c%" PRIu64
, ++m
->session_counter
) < 0)
970 } while (hashmap_contains(m
->sessions
, id
));
973 /* The generated names should not clash with 'auto' or 'self' */
974 assert(!SESSION_IS_SELF(id
));
975 assert(!SESSION_IS_AUTO(id
));
977 /* If we are not watching utmp already, try again */
978 manager_reconnect_utmp(m
);
980 r
= manager_add_user_by_uid(m
, uid
, &user
);
984 r
= manager_add_session(m
, id
, &session
);
988 session_set_user(session
, user
);
989 r
= session_set_leader_consume(session
, TAKE_PIDREF(leader
));
993 session
->original_type
= session
->type
= t
;
994 session
->remote
= remote
;
995 session
->vtnr
= vtnr
;
998 /* Once the first session that is of a pinning class shows up we'll change the GC mode for the user
999 * from USER_GC_BY_ANY to USER_GC_BY_PIN, so that the user goes away once the last pinning session
1000 * goes away. Background: we want that user@.service – when started manually – remains around (which
1001 * itself is a non-pinning session), but gets stopped when the last pinning session goes away. */
1003 if (SESSION_CLASS_PIN_USER(c
))
1004 user
->gc_mode
= USER_GC_BY_PIN
;
1006 if (!isempty(tty
)) {
1007 session
->tty
= strdup(tty
);
1008 if (!session
->tty
) {
1013 session
->tty_validity
= TTY_FROM_PAM
;
1016 if (!isempty(display
)) {
1017 session
->display
= strdup(display
);
1018 if (!session
->display
) {
1024 if (!isempty(remote_user
)) {
1025 session
->remote_user
= strdup(remote_user
);
1026 if (!session
->remote_user
) {
1032 if (!isempty(remote_host
)) {
1033 session
->remote_host
= strdup(remote_host
);
1034 if (!session
->remote_host
) {
1040 if (!isempty(service
)) {
1041 session
->service
= strdup(service
);
1042 if (!session
->service
) {
1048 if (!isempty(desktop
)) {
1049 session
->desktop
= strdup(desktop
);
1050 if (!session
->desktop
) {
1057 r
= seat_attach_session(seat
, session
);
1062 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1066 r
= session_start(session
, message
, error
);
1070 r
= sd_bus_message_exit_container(message
);
1074 session
->create_message
= sd_bus_message_ref(message
);
1076 /* Now call into session_send_create_reply(), which will reply to this method call for us. Or it
1077 * won't – in case we just spawned a session scope and/or user service manager, and they aren't ready
1078 * yet. We'll call session_create_reply() again once the session scope or the user service manager is
1079 * ready, where the function will check again if a reply is then ready to be sent, and then do so if
1080 * all is complete - or wait again. */
1081 r
= session_send_create_reply(session
, /* error= */ NULL
);
1089 session_add_to_gc_queue(session
);
1092 user_add_to_gc_queue(user
);
1097 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1098 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1107 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
1108 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
1110 r
= sd_bus_message_read(message
,
1128 return create_session(
1134 /* pidfd = */ -EBADF
,
1149 static int method_create_session_pidfd(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1150 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1151 int leaderfd
= -EBADF
;
1158 r
= sd_bus_message_read(message
,
1177 return create_session(
1198 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1199 Manager
*m
= ASSERT_PTR(userdata
);
1206 r
= sd_bus_message_read(message
, "s", &name
);
1210 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1214 r
= session_release(session
);
1218 return sd_bus_reply_method_return(message
, NULL
);
1221 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1222 Manager
*m
= ASSERT_PTR(userdata
);
1229 r
= sd_bus_message_read(message
, "s", &name
);
1233 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1237 /* PolicyKit is done by bus_session_method_activate() */
1239 return bus_session_method_activate(message
, session
, error
);
1242 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1243 const char *session_name
, *seat_name
;
1244 Manager
*m
= ASSERT_PTR(userdata
);
1251 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1253 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1257 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1261 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1265 if (session
->seat
!= seat
)
1266 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1267 "Session %s not on seat %s", session_name
, seat_name
);
1269 r
= check_polkit_chvt(message
, m
, error
);
1273 return 1; /* Will call us back */
1275 r
= session_activate(session
);
1279 return sd_bus_reply_method_return(message
, NULL
);
1282 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1283 Manager
*m
= ASSERT_PTR(userdata
);
1290 r
= sd_bus_message_read(message
, "s", &name
);
1294 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1298 return bus_session_method_lock(message
, session
, error
);
1301 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1302 Manager
*m
= ASSERT_PTR(userdata
);
1307 r
= bus_verify_polkit_async(
1309 "org.freedesktop.login1.lock-sessions",
1310 /* details= */ NULL
,
1311 &m
->polkit_registry
,
1316 return 1; /* Will call us back */
1318 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1322 return sd_bus_reply_method_return(message
, NULL
);
1325 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1327 Manager
*m
= ASSERT_PTR(userdata
);
1333 r
= sd_bus_message_read(message
, "s", &name
);
1337 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1341 return bus_session_method_kill(message
, session
, error
);
1344 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1345 Manager
*m
= ASSERT_PTR(userdata
);
1352 r
= sd_bus_message_read(message
, "u", &uid
);
1356 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1360 return bus_user_method_kill(message
, user
, error
);
1363 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1364 Manager
*m
= ASSERT_PTR(userdata
);
1371 r
= sd_bus_message_read(message
, "s", &name
);
1375 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1379 return bus_session_method_terminate(message
, session
, error
);
1382 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1383 Manager
*m
= ASSERT_PTR(userdata
);
1390 r
= sd_bus_message_read(message
, "u", &uid
);
1394 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1398 return bus_user_method_terminate(message
, user
, error
);
1401 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1402 Manager
*m
= ASSERT_PTR(userdata
);
1409 r
= sd_bus_message_read(message
, "s", &name
);
1413 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1417 return bus_seat_method_terminate(message
, seat
, error
);
1420 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1421 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1422 _cleanup_free_
char *cc
= NULL
;
1423 Manager
*m
= ASSERT_PTR(userdata
);
1424 int r
, b
, interactive
;
1427 uint32_t uid
, auth_uid
;
1431 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1435 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1436 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1440 if (!uid_is_valid(uid
)) {
1441 /* Note that we get the owner UID of the session or user unit,
1442 * not the actual client UID here! */
1443 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1448 /* owner_uid is racy, so for authorization we must use euid */
1449 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1456 return errno_or_else(ENOENT
);
1458 r
= bus_verify_polkit_async_full(
1460 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1461 "org.freedesktop.login1.set-user-linger",
1462 /* details= */ NULL
,
1464 /* good_user= */ UID_INVALID
,
1465 &m
->polkit_registry
,
1470 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1472 (void) mkdir_p_label("/var/lib/systemd", 0755);
1473 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1477 cc
= cescape(pw
->pw_name
);
1481 path
= strjoina("/var/lib/systemd/linger/", cc
);
1489 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1496 if (r
< 0 && errno
!= ENOENT
)
1499 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1501 user_add_to_gc_queue(u
);
1504 return sd_bus_reply_method_return(message
, NULL
);
1507 static int trigger_device(Manager
*m
, sd_device
*parent
) {
1508 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1513 r
= sd_device_enumerator_new(&e
);
1517 r
= sd_device_enumerator_allow_uninitialized(e
);
1522 r
= sd_device_enumerator_add_match_parent(e
, parent
);
1527 FOREACH_DEVICE(e
, d
) {
1528 r
= sd_device_trigger(d
, SD_DEVICE_CHANGE
);
1530 log_device_debug_errno(d
, r
, "Failed to trigger device, ignoring: %m");
1536 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
, sd_bus_error
*error
) {
1537 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1538 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1539 const char *id_for_seat
;
1546 r
= sd_device_new_from_syspath(&d
, sysfs
);
1548 return sd_bus_error_set_errnof(error
, r
, "Failed to open device '%s': %m", sysfs
);
1550 if (sd_device_has_current_tag(d
, "seat") <= 0)
1551 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'seat' udev tag.", sysfs
);
1553 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1554 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'ID_FOR_SEAT' udev property.", sysfs
);
1556 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1559 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1562 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1563 r
= write_string_file_atomic_label(file
, rule
);
1567 return trigger_device(m
, d
);
1570 static int flush_devices(Manager
*m
) {
1571 _cleanup_closedir_
DIR *d
= NULL
;
1575 d
= opendir("/etc/udev/rules.d");
1577 if (errno
!= ENOENT
)
1578 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1580 FOREACH_DIRENT_ALL(de
, d
, break) {
1581 if (!dirent_is_file(de
))
1584 if (!startswith(de
->d_name
, "72-seat-"))
1587 if (!endswith(de
->d_name
, ".rules"))
1590 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1591 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1594 return trigger_device(m
, NULL
);
1597 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1598 const char *sysfs
, *seat
;
1599 Manager
*m
= ASSERT_PTR(userdata
);
1604 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1608 if (!path_is_normalized(sysfs
))
1609 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1610 if (!path_startswith(sysfs
, "/sys"))
1611 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1613 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1616 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1622 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1623 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1625 r
= bus_verify_polkit_async_full(
1627 "org.freedesktop.login1.attach-device",
1628 /* details= */ NULL
,
1630 /* good_user= */ UID_INVALID
,
1631 &m
->polkit_registry
,
1636 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1638 r
= attach_device(m
, seat
, sysfs
, error
);
1642 return sd_bus_reply_method_return(message
, NULL
);
1645 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1646 Manager
*m
= ASSERT_PTR(userdata
);
1651 r
= sd_bus_message_read(message
, "b", &interactive
);
1655 r
= bus_verify_polkit_async_full(
1657 "org.freedesktop.login1.flush-devices",
1658 /* details= */ NULL
,
1660 /* good_user= */ UID_INVALID
,
1661 &m
->polkit_registry
,
1666 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1668 r
= flush_devices(m
);
1672 return sd_bus_reply_method_return(message
, NULL
);
1675 static int have_multiple_sessions(
1683 /* Check for other users' sessions. Greeter sessions do not
1684 * count, and non-login sessions do not count either. */
1685 HASHMAP_FOREACH(session
, m
->sessions
)
1686 if (session
->class == SESSION_USER
&&
1687 session
->user
->user_record
->uid
!= uid
)
1693 static int bus_manager_log_shutdown(
1695 const HandleActionData
*a
) {
1699 const char *message
= a
->message
?: "System is shutting down";
1700 const char *log_verb
= a
->log_verb
? strjoina("SHUTDOWN=", a
->log_verb
) : NULL
;
1702 return log_struct(LOG_NOTICE
,
1703 "MESSAGE_ID=%s", a
->message_id
?: SD_MESSAGE_SHUTDOWN_STR
,
1704 LOG_MESSAGE("%s%s%s%s.",
1706 m
->wall_message
? " (" : "",
1707 strempty(m
->wall_message
),
1708 m
->wall_message
? ")" : ""),
1712 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1713 Manager
*m
= ASSERT_PTR(userdata
);
1717 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1721 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1726 if (until
<= now(CLOCK_MONOTONIC
))
1729 /* We want to ignore the lid switch for a while after each
1730 * suspend, and after boot-up. Hence let's install a timer for
1731 * this. As long as the event source exists we ignore the lid
1734 if (m
->lid_switch_ignore_event_source
) {
1737 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1744 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1746 r
= sd_event_add_time(
1748 &m
->lid_switch_ignore_event_source
,
1751 lid_switch_ignore_handler
, m
);
1756 static int send_prepare_for(Manager
*m
, const HandleActionData
*a
, bool _active
) {
1757 int k
= 0, r
, active
= _active
;
1761 assert(IN_SET(a
->inhibit_what
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1763 /* We need to send both old and new signal for backward compatibility. The newer one allows clients
1764 * to know which type of reboot is going to happen, as they might be doing different actions (e.g.:
1765 * on soft-reboot), and it is sent first, so that clients know that if they receive the old one
1766 * first then they don't have to wait for the new one, as it means it's not supported. So, do not
1767 * change the order here, as it is an API. */
1768 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
) {
1769 k
= sd_bus_emit_signal(m
->bus
,
1770 "/org/freedesktop/login1",
1771 "org.freedesktop.login1.Manager",
1772 "PrepareForShutdownWithMetadata",
1778 handle_action_to_string(a
->handle
));
1780 log_debug_errno(k
, "Failed to emit PrepareForShutdownWithMetadata(): %m");
1783 r
= sd_bus_emit_signal(m
->bus
,
1784 "/org/freedesktop/login1",
1785 "org.freedesktop.login1.Manager",
1786 a
->inhibit_what
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1790 log_debug_errno(r
, "Failed to emit PrepareForShutdown(): %m");
1792 return RET_GATHER(k
, r
);
1795 static int execute_shutdown_or_sleep(
1797 const HandleActionData
*a
,
1798 sd_bus_error
*error
) {
1800 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1805 assert(!m
->action_job
);
1808 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
)
1809 bus_manager_log_shutdown(m
, a
);
1811 r
= bus_call_method(
1817 "ss", a
->target
, "replace-irreversibly");
1821 r
= sd_bus_message_read(reply
, "o", &p
);
1825 m
->action_job
= strdup(p
);
1826 if (!m
->action_job
) {
1831 m
->delayed_action
= a
;
1833 /* Make sure the lid switch is ignored for a while */
1834 manager_set_lid_switch_ignore(m
, usec_add(now(CLOCK_MONOTONIC
), m
->holdoff_timeout_usec
));
1839 /* Tell people that they now may take a lock again */
1840 (void) send_prepare_for(m
, a
, false);
1845 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1846 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1847 Inhibitor
*offending
= NULL
;
1852 if (!manager
->delayed_action
|| manager
->action_job
)
1855 if (manager_is_inhibited(manager
, manager
->delayed_action
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1856 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1861 (void) pidref_get_comm(&offending
->pid
, &comm
);
1862 u
= uid_to_name(offending
->uid
);
1864 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1865 offending
->uid
, strna(u
),
1866 offending
->pid
.pid
, strna(comm
));
1869 /* Actually do the operation */
1870 r
= execute_shutdown_or_sleep(manager
, manager
->delayed_action
, &error
);
1872 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1873 bus_error_message(&error
, r
));
1875 manager
->delayed_action
= NULL
;
1878 return 1; /* We did some work. */
1881 static int manager_inhibit_timeout_handler(
1886 Manager
*manager
= ASSERT_PTR(userdata
);
1888 assert(manager
->inhibit_timeout_source
== s
);
1890 return manager_dispatch_delayed(manager
, true);
1893 static int delay_shutdown_or_sleep(
1895 const HandleActionData
*a
) {
1902 if (m
->inhibit_timeout_source
) {
1903 r
= sd_event_source_set_time_relative(m
->inhibit_timeout_source
, m
->inhibit_delay_max
);
1905 return log_error_errno(r
, "sd_event_source_set_time_relative() failed: %m");
1907 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1909 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1911 r
= sd_event_add_time_relative(
1913 &m
->inhibit_timeout_source
,
1914 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, 0,
1915 manager_inhibit_timeout_handler
, m
);
1920 m
->delayed_action
= a
;
1925 int bus_manager_shutdown_or_sleep_now_or_later(
1927 const HandleActionData
*a
,
1928 sd_bus_error
*error
) {
1930 _cleanup_free_
char *load_state
= NULL
;
1936 assert(!m
->action_job
);
1938 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
1942 if (!streq(load_state
, "loaded"))
1943 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1944 "Unit %s is %s, refusing operation.",
1945 a
->target
, load_state
);
1947 /* Tell everybody to prepare for shutdown/sleep */
1948 (void) send_prepare_for(m
, a
, true);
1951 m
->inhibit_delay_max
> 0 &&
1952 manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1955 /* Shutdown is delayed, keep in mind what we
1956 * want to do, and start a timeout */
1957 r
= delay_shutdown_or_sleep(m
, a
);
1959 /* Shutdown is not delayed, execute it
1961 r
= execute_shutdown_or_sleep(m
, a
, error
);
1966 static int verify_shutdown_creds(
1968 sd_bus_message
*message
,
1969 const HandleActionData
*a
,
1971 sd_bus_error
*error
) {
1973 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1974 bool multiple_sessions
, blocked
, interactive
;
1982 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1986 r
= sd_bus_creds_get_euid(creds
, &uid
);
1990 r
= have_multiple_sessions(m
, uid
);
1994 multiple_sessions
= r
> 0;
1995 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1996 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
1998 if (multiple_sessions
) {
1999 r
= bus_verify_polkit_async_full(
2001 a
->polkit_action_multiple_sessions
,
2002 /* details= */ NULL
,
2004 /* good_user= */ UID_INVALID
,
2005 &m
->polkit_registry
,
2010 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2014 /* We don't check polkit for root here, because you can't be more privileged than root */
2015 if (uid
== 0 && (flags
& SD_LOGIND_ROOT_CHECK_INHIBITORS
))
2016 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
,
2017 "Access denied to root due to active block inhibitor");
2019 r
= bus_verify_polkit_async_full(
2021 a
->polkit_action_ignore_inhibit
,
2022 /* details= */ NULL
,
2024 /* good_user= */ UID_INVALID
,
2025 &m
->polkit_registry
,
2030 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2033 if (!multiple_sessions
&& !blocked
) {
2034 r
= bus_verify_polkit_async_full(
2037 /* details= */ NULL
,
2039 /* good_user= */ UID_INVALID
,
2040 &m
->polkit_registry
,
2045 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2051 static int setup_wall_message_timer(Manager
*m
, sd_bus_message
* message
) {
2052 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2055 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2057 const char *tty
= NULL
;
2059 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2060 (void) sd_bus_creds_get_tty(creds
, &tty
);
2062 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2067 r
= manager_setup_wall_message_timer(m
);
2074 static int method_do_shutdown_or_sleep(
2076 sd_bus_message
*message
,
2077 HandleAction action
,
2079 sd_bus_error
*error
) {
2086 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2089 /* New style method: with flags parameter (and interactive bool in the bus message header) */
2090 r
= sd_bus_message_read(message
, "t", &flags
);
2093 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
2094 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2095 "Invalid flags parameter");
2097 if (FLAGS_SET(flags
, (SD_LOGIND_REBOOT_VIA_KEXEC
|SD_LOGIND_SOFT_REBOOT
)))
2098 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2099 "Both reboot via kexec and soft reboot selected, which is not supported");
2101 if (action
!= HANDLE_REBOOT
) {
2102 if (flags
& SD_LOGIND_REBOOT_VIA_KEXEC
)
2103 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2104 "Reboot via kexec option is only applicable with reboot operations");
2105 if ((flags
& SD_LOGIND_SOFT_REBOOT
) || (flags
& SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
))
2106 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2107 "Soft reboot option is only applicable with reboot operations");
2110 /* Old style method: no flags parameter, but interactive bool passed as boolean in
2111 * payload. Let's convert this argument to the new-style flags parameter for our internal
2115 r
= sd_bus_message_read(message
, "b", &interactive
);
2119 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
2122 const HandleActionData
*a
= NULL
;
2124 if ((flags
& SD_LOGIND_SOFT_REBOOT
) ||
2125 ((flags
& SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
) && path_is_os_tree("/run/nextroot") > 0))
2126 a
= handle_action_lookup(HANDLE_SOFT_REBOOT
);
2127 else if ((flags
& SD_LOGIND_REBOOT_VIA_KEXEC
) && kexec_loaded())
2128 a
= handle_action_lookup(HANDLE_KEXEC
);
2130 if (action
== HANDLE_SLEEP
) {
2131 HandleAction selected
;
2133 selected
= handle_action_sleep_select(m
);
2135 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2136 "None of the configured sleep operations are supported");
2138 assert_se(a
= handle_action_lookup(selected
));
2140 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2141 SleepSupport support
;
2143 assert_se(a
= handle_action_lookup(action
));
2145 assert(a
->sleep_operation
>= 0);
2146 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2148 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2154 case SLEEP_DISABLED
:
2155 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2156 "Sleep verb '%s' is disabled by config",
2157 sleep_operation_to_string(a
->sleep_operation
));
2159 case SLEEP_NOT_CONFIGURED
:
2160 case SLEEP_STATE_OR_MODE_NOT_SUPPORTED
:
2161 case SLEEP_ALARM_NOT_SUPPORTED
:
2162 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2163 "Sleep verb '%s' is not configured or configuration is not supported by kernel",
2164 sleep_operation_to_string(a
->sleep_operation
));
2166 case SLEEP_RESUME_NOT_SUPPORTED
:
2167 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2168 "Not running on EFI and resume= is not set. No available method to resume from hibernation");
2170 case SLEEP_NOT_ENOUGH_SWAP_SPACE
:
2171 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2172 "Not enough suitable swap space for hibernation available on compatible block devices and file systems");
2175 assert_not_reached();
2179 assert_se(a
= handle_action_lookup(action
));
2181 r
= verify_shutdown_creds(m
, message
, a
, flags
, error
);
2185 if (m
->delayed_action
)
2186 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
2187 "Action %s already in progress, refusing requested %s operation.",
2188 handle_action_to_string(m
->delayed_action
->handle
),
2189 handle_action_to_string(a
->handle
));
2191 /* reset case we're shorting a scheduled shutdown */
2192 m
->unlink_nologin
= false;
2193 reset_scheduled_shutdown(m
);
2195 m
->scheduled_shutdown_timeout
= 0;
2196 m
->scheduled_shutdown_action
= action
;
2198 (void) setup_wall_message_timer(m
, message
);
2200 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, error
);
2204 return sd_bus_reply_method_return(message
, NULL
);
2207 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2208 Manager
*m
= userdata
;
2210 return method_do_shutdown_or_sleep(
2213 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
2217 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2218 Manager
*m
= userdata
;
2220 return method_do_shutdown_or_sleep(
2223 sd_bus_message_is_method_call(message
, NULL
, "RebootWithFlags"),
2227 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2228 Manager
*m
= userdata
;
2230 return method_do_shutdown_or_sleep(
2233 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
2237 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2238 Manager
*m
= userdata
;
2240 return method_do_shutdown_or_sleep(
2243 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
2247 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2248 Manager
*m
= userdata
;
2250 return method_do_shutdown_or_sleep(
2253 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
2257 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2258 Manager
*m
= userdata
;
2260 return method_do_shutdown_or_sleep(
2262 HANDLE_HYBRID_SLEEP
,
2263 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
2267 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2268 Manager
*m
= userdata
;
2270 return method_do_shutdown_or_sleep(
2272 HANDLE_SUSPEND_THEN_HIBERNATE
,
2273 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
2277 static int method_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2278 Manager
*m
= userdata
;
2280 return method_do_shutdown_or_sleep(
2283 /* with_flags = */ true,
2287 static int nologin_timeout_handler(
2292 Manager
*m
= userdata
;
2294 log_info("Creating /run/nologin, blocking further logins...");
2297 create_shutdown_run_nologin_or_warn() >= 0;
2302 static usec_t
nologin_timeout_usec(usec_t elapse
) {
2303 /* Issue /run/nologin five minutes before shutdown */
2304 return LESS_BY(elapse
, 5 * USEC_PER_MINUTE
);
2307 void manager_load_scheduled_shutdown(Manager
*m
) {
2308 _cleanup_fclose_
FILE *f
= NULL
;
2309 _cleanup_free_
char *usec
= NULL
,
2312 *wall_message
= NULL
,
2319 r
= parse_env_file(f
, SHUTDOWN_SCHEDULE_FILE
,
2321 "WARN_WALL", &warn_wall
,
2323 "WALL_MESSAGE", &wall_message
,
2327 /* reset will delete the file */
2328 reset_scheduled_shutdown(m
);
2333 return (void) log_debug_errno(r
, "Failed to parse " SHUTDOWN_SCHEDULE_FILE
": %m");
2335 HandleAction handle
= handle_action_from_string(mode
);
2337 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse scheduled shutdown type: %s", mode
);
2340 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "USEC is required");
2341 if (deserialize_usec(usec
, &m
->scheduled_shutdown_timeout
) < 0)
2344 /* assign parsed type only after we know usec is also valid */
2345 m
->scheduled_shutdown_action
= handle
;
2348 r
= parse_boolean(warn_wall
);
2350 log_debug_errno(r
, "Failed to parse enabling wall messages");
2352 m
->enable_wall_messages
= r
;
2356 _cleanup_free_
char *unescaped
= NULL
;
2357 r
= cunescape(wall_message
, 0, &unescaped
);
2359 log_debug_errno(r
, "Failed to parse wall message: %s", wall_message
);
2361 free_and_replace(m
->wall_message
, unescaped
);
2365 r
= parse_uid(uid
, &m
->scheduled_shutdown_uid
);
2367 log_debug_errno(r
, "Failed to parse wall uid: %s", uid
);
2370 free_and_replace(m
->scheduled_shutdown_tty
, tty
);
2372 r
= manager_setup_shutdown_timers(m
);
2374 return reset_scheduled_shutdown(m
);
2376 (void) manager_setup_wall_message_timer(m
);
2377 (void) update_schedule_file(m
);
2382 static int update_schedule_file(Manager
*m
) {
2383 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
2384 _cleanup_fclose_
FILE *f
= NULL
;
2388 assert(handle_action_valid(m
->scheduled_shutdown_action
));
2390 r
= mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE
, 0755);
2392 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2394 r
= fopen_temporary(SHUTDOWN_SCHEDULE_FILE
, &f
, &temp_path
);
2396 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2398 (void) fchmod(fileno(f
), 0644);
2400 serialize_usec(f
, "USEC", m
->scheduled_shutdown_timeout
);
2401 serialize_item_format(f
, "WARN_WALL", "%s", one_zero(m
->enable_wall_messages
));
2402 serialize_item_format(f
, "MODE", "%s", handle_action_to_string(m
->scheduled_shutdown_action
));
2403 serialize_item_format(f
, "UID", UID_FMT
, m
->scheduled_shutdown_uid
);
2405 if (m
->scheduled_shutdown_tty
)
2406 serialize_item_format(f
, "TTY", "%s", m
->scheduled_shutdown_tty
);
2408 if (!isempty(m
->wall_message
)) {
2409 r
= serialize_item_escaped(f
, "WALL_MESSAGE", m
->wall_message
);
2414 r
= fflush_and_check(f
);
2418 if (rename(temp_path
, SHUTDOWN_SCHEDULE_FILE
) < 0) {
2423 temp_path
= mfree(temp_path
);
2427 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2429 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2432 static void reset_scheduled_shutdown(Manager
*m
) {
2435 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2436 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2437 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2439 m
->scheduled_shutdown_action
= _HANDLE_ACTION_INVALID
;
2440 m
->scheduled_shutdown_timeout
= USEC_INFINITY
;
2441 m
->scheduled_shutdown_uid
= UID_INVALID
;
2442 m
->scheduled_shutdown_tty
= mfree(m
->scheduled_shutdown_tty
);
2443 m
->shutdown_dry_run
= false;
2445 if (m
->unlink_nologin
) {
2446 (void) unlink_or_warn("/run/nologin");
2447 m
->unlink_nologin
= false;
2450 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2453 static int manager_scheduled_shutdown_handler(
2458 Manager
*m
= ASSERT_PTR(userdata
);
2459 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2460 const HandleActionData
*a
;
2463 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2465 /* Don't allow multiple jobs being executed at the same time */
2466 if (m
->delayed_action
) {
2467 r
= log_error_errno(SYNTHETIC_ERRNO(EALREADY
),
2468 "Scheduled shutdown to %s failed: shutdown or sleep operation already in progress.",
2473 if (m
->shutdown_dry_run
) {
2474 /* We do not process delay inhibitors here. Otherwise, we
2475 * would have to be considered "in progress" (like the check
2476 * above) for some seconds after our admin has seen the final
2479 bus_manager_log_shutdown(m
, a
);
2480 log_info("Running in dry run, suppressing action.");
2481 reset_scheduled_shutdown(m
);
2486 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, &error
);
2488 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", a
->target
);
2495 reset_scheduled_shutdown(m
);
2499 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2500 Manager
*m
= ASSERT_PTR(userdata
);
2501 HandleAction handle
;
2502 const HandleActionData
*a
;
2506 bool dry_run
= false;
2510 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2514 if (startswith(type
, "dry-")) {
2519 handle
= handle_action_from_string(type
);
2520 if (!HANDLE_ACTION_IS_SHUTDOWN(handle
))
2521 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type: %s", type
);
2523 assert_se(a
= handle_action_lookup(handle
));
2524 assert(a
->polkit_action
);
2526 r
= verify_shutdown_creds(m
, message
, a
, 0, error
);
2530 m
->scheduled_shutdown_action
= handle
;
2531 m
->shutdown_dry_run
= dry_run
;
2532 m
->scheduled_shutdown_timeout
= elapse
;
2534 r
= manager_setup_shutdown_timers(m
);
2538 r
= setup_wall_message_timer(m
, message
);
2540 r
= update_schedule_file(m
);
2543 reset_scheduled_shutdown(m
);
2547 return sd_bus_reply_method_return(message
, NULL
);
2550 static int manager_setup_shutdown_timers(Manager
* m
) {
2553 r
= event_reset_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2555 m
->scheduled_shutdown_timeout
, 0,
2556 manager_scheduled_shutdown_handler
, m
,
2557 0, "scheduled-shutdown-timeout", true);
2561 r
= event_reset_time(m
->event
, &m
->nologin_timeout_source
,
2563 nologin_timeout_usec(m
->scheduled_shutdown_timeout
), 0,
2564 nologin_timeout_handler
, m
,
2565 0, "nologin-timeout", true);
2572 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2573 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2578 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2579 Manager
*m
= ASSERT_PTR(userdata
);
2580 const HandleActionData
*a
;
2586 cancelled
= handle_action_valid(m
->scheduled_shutdown_action
) && m
->scheduled_shutdown_action
!= HANDLE_IGNORE
;
2588 return sd_bus_reply_method_return(message
, "b", false);
2590 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2591 if (!a
->polkit_action
)
2592 return sd_bus_error_set(error
, SD_BUS_ERROR_AUTH_FAILED
, "Unsupported shutdown type");
2594 r
= bus_verify_polkit_async(
2597 /* details= */ NULL
,
2598 &m
->polkit_registry
,
2603 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2605 if (m
->enable_wall_messages
) {
2606 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2607 const char *tty
= NULL
;
2610 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2612 (void) sd_bus_creds_get_uid(creds
, &uid
);
2613 (void) sd_bus_creds_get_tty(creds
, &tty
);
2616 _cleanup_free_
char *username
= uid_to_name(uid
);
2618 log_struct(LOG_INFO
,
2619 LOG_MESSAGE("System shutdown has been cancelled"),
2620 "ACTION=%s", handle_action_to_string(a
->handle
),
2621 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_CANCELED_STR
,
2622 username
? "OPERATOR=%s" : NULL
, username
);
2624 (void) wall("System shutdown has been cancelled",
2625 username
, tty
, logind_wall_tty_filter
, m
);
2628 reset_scheduled_shutdown(m
);
2630 return sd_bus_reply_method_return(message
, "b", true);
2633 static int method_can_shutdown_or_sleep(
2635 sd_bus_message
*message
,
2636 HandleAction action
,
2637 sd_bus_error
*error
) {
2639 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2640 bool multiple_sessions
, challenge
, blocked
, check_unit_state
= true;
2641 const HandleActionData
*a
;
2642 const char *result
= NULL
;
2648 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2650 if (action
== HANDLE_SLEEP
) {
2651 HandleAction selected
;
2653 selected
= handle_action_sleep_select(m
);
2655 return sd_bus_reply_method_return(message
, "s", "na");
2657 check_unit_state
= false; /* Already handled by handle_action_sleep_select */
2659 assert_se(a
= handle_action_lookup(selected
));
2661 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2662 SleepSupport support
;
2664 assert_se(a
= handle_action_lookup(action
));
2666 assert(a
->sleep_operation
>= 0);
2667 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2669 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2673 return sd_bus_reply_method_return(message
, "s", support
== SLEEP_DISABLED
? "no" : "na");
2675 assert_se(a
= handle_action_lookup(action
));
2677 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2681 r
= sd_bus_creds_get_euid(creds
, &uid
);
2685 r
= have_multiple_sessions(m
, uid
);
2689 multiple_sessions
= r
> 0;
2690 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2692 if (check_unit_state
&& a
->target
) {
2693 _cleanup_free_
char *load_state
= NULL
;
2695 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
2699 if (!streq(load_state
, "loaded")) {
2705 if (multiple_sessions
) {
2706 r
= bus_test_polkit(
2708 a
->polkit_action_multiple_sessions
,
2709 /* details= */ NULL
,
2710 /* good_user= */ UID_INVALID
,
2719 result
= "challenge";
2725 r
= bus_test_polkit(
2727 a
->polkit_action_ignore_inhibit
,
2728 /* details= */ NULL
,
2729 /* good_user= */ UID_INVALID
,
2738 } else if (challenge
) {
2739 if (!result
|| streq(result
, "yes"))
2740 result
= "challenge";
2745 if (!multiple_sessions
&& !blocked
) {
2746 /* If neither inhibit nor multiple sessions
2747 * apply then just check the normal policy */
2749 r
= bus_test_polkit(
2752 /* details= */ NULL
,
2753 /* good_user= */ UID_INVALID
,
2762 result
= "challenge";
2768 return sd_bus_reply_method_return(message
, "s", result
);
2771 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2772 Manager
*m
= userdata
;
2774 return method_can_shutdown_or_sleep(m
, message
, HANDLE_POWEROFF
, error
);
2777 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2778 Manager
*m
= userdata
;
2780 return method_can_shutdown_or_sleep(m
, message
, HANDLE_REBOOT
, error
);
2783 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2784 Manager
*m
= userdata
;
2786 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HALT
, error
);
2789 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2790 Manager
*m
= userdata
;
2792 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND
, error
);
2795 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2796 Manager
*m
= userdata
;
2798 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HIBERNATE
, error
);
2801 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2802 Manager
*m
= userdata
;
2804 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HYBRID_SLEEP
, error
);
2807 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2808 Manager
*m
= userdata
;
2810 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND_THEN_HIBERNATE
, error
);
2813 static int method_can_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2814 Manager
*m
= userdata
;
2816 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SLEEP
, error
);
2819 static int property_get_reboot_parameter(
2822 const char *interface
,
2823 const char *property
,
2824 sd_bus_message
*reply
,
2826 sd_bus_error
*error
) {
2827 _cleanup_free_
char *parameter
= NULL
;
2834 r
= read_reboot_parameter(¶meter
);
2838 return sd_bus_message_append(reply
, "s", parameter
);
2841 static int method_set_reboot_parameter(
2842 sd_bus_message
*message
,
2844 sd_bus_error
*error
) {
2846 Manager
*m
= ASSERT_PTR(userdata
);
2852 r
= sd_bus_message_read(message
, "s", &arg
);
2856 r
= detect_container();
2860 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2861 "Reboot parameter not supported in containers, refusing.");
2863 r
= bus_verify_polkit_async(
2865 "org.freedesktop.login1.set-reboot-parameter",
2866 /* details= */ NULL
,
2867 &m
->polkit_registry
,
2872 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2874 r
= update_reboot_parameter_and_warn(arg
, false);
2878 return sd_bus_reply_method_return(message
, NULL
);
2881 static int method_can_reboot_parameter(
2882 sd_bus_message
*message
,
2884 sd_bus_error
*error
) {
2886 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2891 r
= detect_container();
2894 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2895 return sd_bus_reply_method_return(message
, "s", "na");
2897 return return_test_polkit(
2899 "org.freedesktop.login1.set-reboot-parameter",
2900 /* details= */ NULL
,
2901 /* good_user= */ UID_INVALID
,
2905 static int property_get_reboot_to_firmware_setup(
2908 const char *interface
,
2909 const char *property
,
2910 sd_bus_message
*reply
,
2912 sd_bus_error
*error
) {
2919 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2921 /* EFI case: let's see what is currently configured in the EFI variables */
2922 r
= efi_get_reboot_to_firmware();
2923 if (r
< 0 && r
!= -EOPNOTSUPP
)
2924 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2926 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2928 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2929 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2930 if (errno
!= ENOENT
)
2931 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2938 return sd_bus_message_append(reply
, "b", r
> 0);
2941 static int method_set_reboot_to_firmware_setup(
2942 sd_bus_message
*message
,
2944 sd_bus_error
*error
) {
2946 Manager
*m
= ASSERT_PTR(userdata
);
2952 r
= sd_bus_message_read(message
, "b", &b
);
2956 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2958 /* EFI case: let's see what the firmware supports */
2960 r
= efi_reboot_to_firmware_supported();
2961 if (r
== -EOPNOTSUPP
)
2962 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2968 } else if (r
<= 0) {
2969 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2972 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2974 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2976 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2979 r
= bus_verify_polkit_async(
2981 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2982 /* details= */ NULL
,
2983 &m
->polkit_registry
,
2988 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2991 r
= efi_set_reboot_to_firmware(b
);
2996 r
= touch("/run/systemd/reboot-to-firmware-setup");
3000 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
3005 return sd_bus_reply_method_return(message
, NULL
);
3008 static int method_can_reboot_to_firmware_setup(
3009 sd_bus_message
*message
,
3011 sd_bus_error
*error
) {
3013 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3018 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
3020 /* EFI case: let's see what the firmware supports */
3022 r
= efi_reboot_to_firmware_supported();
3024 if (r
!= -EOPNOTSUPP
)
3025 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
3027 return sd_bus_reply_method_return(message
, "s", "na");
3030 } else if (r
<= 0) {
3031 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
3034 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
3036 return sd_bus_reply_method_return(message
, "s", "na");
3039 return return_test_polkit(
3041 "org.freedesktop.login1.set-reboot-to-firmware-setup",
3042 /* details= */ NULL
,
3043 /* good_user= */ UID_INVALID
,
3047 static int property_get_reboot_to_boot_loader_menu(
3050 const char *interface
,
3051 const char *property
,
3052 sd_bus_message
*reply
,
3054 sd_bus_error
*error
) {
3056 uint64_t x
= UINT64_MAX
;
3063 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3065 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished:
3067 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
3068 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
3069 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
3072 r
= efi_loader_get_config_timeout_one_shot(&x
);
3075 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
3079 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3081 _cleanup_free_
char *v
= NULL
;
3083 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
3085 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
3088 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
3090 r
= safe_atou64(v
, &x
);
3092 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
3096 return sd_bus_message_append(reply
, "t", x
);
3099 static int method_set_reboot_to_boot_loader_menu(
3100 sd_bus_message
*message
,
3102 sd_bus_error
*error
) {
3104 Manager
*m
= ASSERT_PTR(userdata
);
3111 r
= sd_bus_message_read(message
, "t", &x
);
3115 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3119 /* EFI case: let's see if booting into boot loader menu is supported. */
3121 r
= efi_loader_get_features(&features
);
3123 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3124 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3125 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3129 } else if (r
<= 0) {
3130 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
3133 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3135 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3137 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
3140 r
= bus_verify_polkit_async(
3142 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3143 /* details= */ NULL
,
3144 &m
->polkit_registry
,
3149 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3152 if (x
== UINT64_MAX
)
3153 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), NULL
, 0);
3155 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3156 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
3158 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), buf
);
3163 if (x
== UINT64_MAX
) {
3164 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
3167 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3169 xsprintf(buf
, "%" PRIu64
, x
); /* μs granularity */
3171 r
= write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf
);
3177 return sd_bus_reply_method_return(message
, NULL
);
3180 static int method_can_reboot_to_boot_loader_menu(
3181 sd_bus_message
*message
,
3183 sd_bus_error
*error
) {
3185 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3190 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3192 uint64_t features
= 0;
3194 /* EFI case, let's see if booting into boot loader menu is supported. */
3196 r
= efi_loader_get_features(&features
);
3198 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3199 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3200 return sd_bus_reply_method_return(message
, "s", "na");
3202 } else if (r
<= 0) {
3203 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
3206 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3208 return sd_bus_reply_method_return(message
, "s", "na");
3211 return return_test_polkit(
3213 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3214 /* details= */ NULL
,
3215 /* good_user= */ UID_INVALID
,
3219 static int property_get_reboot_to_boot_loader_entry(
3222 const char *interface
,
3223 const char *property
,
3224 sd_bus_message
*reply
,
3226 sd_bus_error
*error
) {
3228 _cleanup_free_
char *v
= NULL
;
3229 Manager
*m
= ASSERT_PTR(userdata
);
3230 const char *x
= NULL
;
3236 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3238 /* EFI case: let's read the LoaderEntryOneShot variable */
3240 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
3243 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
3245 x
= m
->efi_loader_entry_one_shot
;
3248 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3251 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
3253 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
3256 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
3257 } else if (!efi_loader_entry_name_valid(v
))
3258 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
3263 return sd_bus_message_append(reply
, "s", x
);
3266 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
3267 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3273 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3274 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
3277 r
= manager_read_efi_boot_loader_entries(m
);
3279 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3281 return !!boot_config_find_entry(&config
, id
);
3284 static int method_set_reboot_to_boot_loader_entry(
3285 sd_bus_message
*message
,
3287 sd_bus_error
*error
) {
3289 Manager
*m
= ASSERT_PTR(userdata
);
3296 r
= sd_bus_message_read(message
, "s", &v
);
3302 else if (efi_loader_entry_name_valid(v
)) {
3303 r
= boot_loader_entry_exists(m
, v
);
3307 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3309 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3311 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3315 /* EFI case: let's see if booting into boot loader entry is supported. */
3317 r
= efi_loader_get_features(&features
);
3319 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3320 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3321 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3325 } else if (r
<= 0) {
3326 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3329 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3331 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3333 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3336 r
= bus_verify_polkit_async(
3338 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3339 /* details= */ NULL
,
3340 &m
->polkit_registry
,
3345 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3350 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), NULL
, 0);
3352 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), v
);
3357 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3360 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3366 return sd_bus_reply_method_return(message
, NULL
);
3369 static int method_can_reboot_to_boot_loader_entry(
3370 sd_bus_message
*message
,
3372 sd_bus_error
*error
) {
3374 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3379 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3381 uint64_t features
= 0;
3383 /* EFI case, let's see if booting into boot loader entry is supported. */
3385 r
= efi_loader_get_features(&features
);
3387 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3388 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3389 return sd_bus_reply_method_return(message
, "s", "na");
3391 } else if (r
<= 0) {
3392 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3395 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3397 return sd_bus_reply_method_return(message
, "s", "na");
3400 return return_test_polkit(
3402 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3403 /* details= */ NULL
,
3404 /* good_user= */ UID_INVALID
,
3408 static int property_get_boot_loader_entries(
3411 const char *interface
,
3412 const char *property
,
3413 sd_bus_message
*reply
,
3415 sd_bus_error
*error
) {
3417 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3418 Manager
*m
= ASSERT_PTR(userdata
);
3425 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3426 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3429 r
= manager_read_efi_boot_loader_entries(m
);
3431 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3433 r
= sd_bus_message_open_container(reply
, 'a', "s");
3437 for (i
= 0; i
< config
.n_entries
; i
++) {
3438 BootEntry
*e
= config
.entries
+ i
;
3440 r
= sd_bus_message_append(reply
, "s", e
->id
);
3445 return sd_bus_message_close_container(reply
);
3448 static int method_set_wall_message(
3449 sd_bus_message
*message
,
3451 sd_bus_error
*error
) {
3454 Manager
*m
= ASSERT_PTR(userdata
);
3456 int enable_wall_messages
;
3460 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3464 if (strlen(wall_message
) > WALL_MESSAGE_MAX
)
3465 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3466 "Wall message too long, maximum permitted length is %u characters.",
3469 /* Short-circuit the operation if the desired state is already in place, to
3470 * avoid an unnecessary polkit permission check. */
3471 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3472 m
->enable_wall_messages
== enable_wall_messages
)
3475 r
= bus_verify_polkit_async(
3477 "org.freedesktop.login1.set-wall-message",
3478 /* details= */ NULL
,
3479 &m
->polkit_registry
,
3484 return 1; /* Will call us back */
3486 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3490 m
->enable_wall_messages
= enable_wall_messages
;
3493 return sd_bus_reply_method_return(message
, NULL
);
3496 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3497 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3498 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
3499 const char *who
, *why
, *what
, *mode
;
3500 _cleanup_free_
char *id
= NULL
;
3501 _cleanup_close_
int fifo_fd
= -EBADF
;
3502 Manager
*m
= ASSERT_PTR(userdata
);
3511 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3515 w
= inhibit_what_from_string(what
);
3517 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3518 "Invalid what specification %s", what
);
3520 mm
= inhibit_mode_from_string(mode
);
3522 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3523 "Invalid mode specification %s", mode
);
3525 /* Delay is only supported for shutdown/sleep */
3526 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3527 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3528 "Delay inhibitors only supported for shutdown and sleep");
3530 /* Don't allow taking delay locks while we are already
3531 * executing the operation. We shouldn't create the impression
3532 * that the lock was successful if the machine is about to go
3533 * down/suspend any moment. */
3534 if (m
->delayed_action
&& m
->delayed_action
->inhibit_what
& w
)
3535 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3536 "The operation inhibition has been requested for is already running");
3538 r
= bus_verify_polkit_async(
3540 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3541 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3542 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3543 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3544 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3545 w
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3546 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3547 "org.freedesktop.login1.inhibit-handle-lid-switch",
3548 /* details= */ NULL
,
3549 &m
->polkit_registry
,
3554 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3556 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3560 r
= sd_bus_creds_get_euid(creds
, &uid
);
3564 r
= sd_bus_creds_get_pid(creds
, &pid
);
3568 r
= pidref_set_pid(&pidref
, pid
);
3570 return sd_bus_error_set_errnof(error
, r
, "Failed pin source process "PID_FMT
": %m", pid
);
3572 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3573 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3574 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3580 if (asprintf(&id
, "%" PRIu64
, ++m
->inhibit_counter
) < 0)
3583 } while (hashmap_get(m
->inhibitors
, id
));
3585 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3586 r
= manager_add_inhibitor(m
, id
, &i
);
3592 i
->pid
= TAKE_PIDREF(pidref
);
3594 i
->why
= strdup(why
);
3595 i
->who
= strdup(who
);
3597 if (!i
->why
|| !i
->who
)
3600 fifo_fd
= inhibitor_create_fifo(i
);
3604 r
= inhibitor_start(i
);
3609 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3612 static const sd_bus_vtable manager_vtable
[] = {
3613 SD_BUS_VTABLE_START(0),
3615 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool
, bus_property_set_bool
, offsetof(Manager
, enable_wall_messages
), 0),
3616 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3618 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3619 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3620 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3621 SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3622 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3623 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3624 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3625 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3626 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3627 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3628 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3629 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3630 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3631 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3632 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3633 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3634 SD_BUS_PROPERTY("SleepOperation", "as", property_get_sleep_operations
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3635 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3636 SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3637 SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3638 SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3639 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3640 SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3641 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3642 SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3643 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3644 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3645 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3646 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3647 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3648 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3649 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3650 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3651 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3652 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3653 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3654 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3655 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3656 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3657 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3658 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3659 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3660 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3661 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3662 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3663 SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL
, offsetof(Manager
, stop_idle_session_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3665 SD_BUS_METHOD_WITH_ARGS("GetSession",
3666 SD_BUS_ARGS("s", session_id
),
3667 SD_BUS_RESULT("o", object_path
),
3669 SD_BUS_VTABLE_UNPRIVILEGED
),
3670 SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
3671 SD_BUS_ARGS("u", pid
),
3672 SD_BUS_RESULT("o", object_path
),
3673 method_get_session_by_pid
,
3674 SD_BUS_VTABLE_UNPRIVILEGED
),
3675 SD_BUS_METHOD_WITH_ARGS("GetUser",
3676 SD_BUS_ARGS("u", uid
),
3677 SD_BUS_RESULT("o", object_path
),
3679 SD_BUS_VTABLE_UNPRIVILEGED
),
3680 SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
3681 SD_BUS_ARGS("u", pid
),
3682 SD_BUS_RESULT("o", object_path
),
3683 method_get_user_by_pid
,
3684 SD_BUS_VTABLE_UNPRIVILEGED
),
3685 SD_BUS_METHOD_WITH_ARGS("GetSeat",
3686 SD_BUS_ARGS("s", seat_id
),
3687 SD_BUS_RESULT("o", object_path
),
3689 SD_BUS_VTABLE_UNPRIVILEGED
),
3690 SD_BUS_METHOD_WITH_ARGS("ListSessions",
3692 SD_BUS_RESULT("a(susso)", sessions
),
3693 method_list_sessions
,
3694 SD_BUS_VTABLE_UNPRIVILEGED
),
3695 SD_BUS_METHOD_WITH_ARGS("ListSessionsEx",
3697 SD_BUS_RESULT("a(sussussbto)", sessions
),
3698 method_list_sessions_ex
,
3699 SD_BUS_VTABLE_UNPRIVILEGED
),
3700 SD_BUS_METHOD_WITH_ARGS("ListUsers",
3702 SD_BUS_RESULT("a(uso)", users
),
3704 SD_BUS_VTABLE_UNPRIVILEGED
),
3705 SD_BUS_METHOD_WITH_ARGS("ListSeats",
3707 SD_BUS_RESULT("a(so)", seats
),
3709 SD_BUS_VTABLE_UNPRIVILEGED
),
3710 SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
3712 SD_BUS_RESULT("a(ssssuu)", inhibitors
),
3713 method_list_inhibitors
,
3714 SD_BUS_VTABLE_UNPRIVILEGED
),
3715 SD_BUS_METHOD_WITH_ARGS("CreateSession",
3716 SD_BUS_ARGS("u", uid
,
3729 "a(sv)", properties
),
3730 SD_BUS_RESULT("s", session_id
,
3738 method_create_session
,
3740 SD_BUS_METHOD_WITH_ARGS("CreateSessionWithPIDFD",
3741 SD_BUS_ARGS("u", uid
,
3755 "a(sv)", properties
),
3756 SD_BUS_RESULT("s", session_id
,
3764 method_create_session_pidfd
,
3766 SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
3767 SD_BUS_ARGS("s", session_id
),
3769 method_release_session
,
3771 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
3772 SD_BUS_ARGS("s", session_id
),
3774 method_activate_session
,
3775 SD_BUS_VTABLE_UNPRIVILEGED
),
3776 SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
3777 SD_BUS_ARGS("s", session_id
, "s", seat_id
),
3779 method_activate_session_on_seat
,
3780 SD_BUS_VTABLE_UNPRIVILEGED
),
3781 SD_BUS_METHOD_WITH_ARGS("LockSession",
3782 SD_BUS_ARGS("s", session_id
),
3784 method_lock_session
,
3785 SD_BUS_VTABLE_UNPRIVILEGED
),
3786 SD_BUS_METHOD_WITH_ARGS("UnlockSession",
3787 SD_BUS_ARGS("s", session_id
),
3789 method_lock_session
,
3790 SD_BUS_VTABLE_UNPRIVILEGED
),
3791 SD_BUS_METHOD("LockSessions",
3794 method_lock_sessions
,
3795 SD_BUS_VTABLE_UNPRIVILEGED
),
3796 SD_BUS_METHOD("UnlockSessions",
3799 method_lock_sessions
,
3800 SD_BUS_VTABLE_UNPRIVILEGED
),
3801 SD_BUS_METHOD_WITH_ARGS("KillSession",
3802 SD_BUS_ARGS("s", session_id
, "s", who
, "i", signal_number
),
3804 method_kill_session
,
3805 SD_BUS_VTABLE_UNPRIVILEGED
),
3806 SD_BUS_METHOD_WITH_ARGS("KillUser",
3807 SD_BUS_ARGS("u", uid
, "i", signal_number
),
3810 SD_BUS_VTABLE_UNPRIVILEGED
),
3811 SD_BUS_METHOD_WITH_ARGS("TerminateSession",
3812 SD_BUS_ARGS("s", session_id
),
3814 method_terminate_session
,
3815 SD_BUS_VTABLE_UNPRIVILEGED
),
3816 SD_BUS_METHOD_WITH_ARGS("TerminateUser",
3817 SD_BUS_ARGS("u", uid
),
3819 method_terminate_user
,
3820 SD_BUS_VTABLE_UNPRIVILEGED
),
3821 SD_BUS_METHOD_WITH_ARGS("TerminateSeat",
3822 SD_BUS_ARGS("s", seat_id
),
3824 method_terminate_seat
,
3825 SD_BUS_VTABLE_UNPRIVILEGED
),
3826 SD_BUS_METHOD_WITH_ARGS("SetUserLinger",
3827 SD_BUS_ARGS("u", uid
, "b", enable
, "b", interactive
),
3829 method_set_user_linger
,
3830 SD_BUS_VTABLE_UNPRIVILEGED
),
3831 SD_BUS_METHOD_WITH_ARGS("AttachDevice",
3832 SD_BUS_ARGS("s", seat_id
, "s", sysfs_path
, "b", interactive
),
3834 method_attach_device
,
3835 SD_BUS_VTABLE_UNPRIVILEGED
),
3836 SD_BUS_METHOD_WITH_ARGS("FlushDevices",
3837 SD_BUS_ARGS("b", interactive
),
3839 method_flush_devices
,
3840 SD_BUS_VTABLE_UNPRIVILEGED
),
3841 SD_BUS_METHOD_WITH_ARGS("PowerOff",
3842 SD_BUS_ARGS("b", interactive
),
3845 SD_BUS_VTABLE_UNPRIVILEGED
),
3846 SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags",
3847 SD_BUS_ARGS("t", flags
),
3850 SD_BUS_VTABLE_UNPRIVILEGED
),
3851 SD_BUS_METHOD_WITH_ARGS("Reboot",
3852 SD_BUS_ARGS("b", interactive
),
3855 SD_BUS_VTABLE_UNPRIVILEGED
),
3856 SD_BUS_METHOD_WITH_ARGS("RebootWithFlags",
3857 SD_BUS_ARGS("t", flags
),
3860 SD_BUS_VTABLE_UNPRIVILEGED
),
3861 SD_BUS_METHOD_WITH_ARGS("Halt",
3862 SD_BUS_ARGS("b", interactive
),
3865 SD_BUS_VTABLE_UNPRIVILEGED
),
3866 SD_BUS_METHOD_WITH_ARGS("HaltWithFlags",
3867 SD_BUS_ARGS("t", flags
),
3870 SD_BUS_VTABLE_UNPRIVILEGED
),
3871 SD_BUS_METHOD_WITH_ARGS("Suspend",
3872 SD_BUS_ARGS("b", interactive
),
3875 SD_BUS_VTABLE_UNPRIVILEGED
),
3876 SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags",
3877 SD_BUS_ARGS("t", flags
),
3880 SD_BUS_VTABLE_UNPRIVILEGED
),
3881 SD_BUS_METHOD_WITH_ARGS("Hibernate",
3882 SD_BUS_ARGS("b", interactive
),
3885 SD_BUS_VTABLE_UNPRIVILEGED
),
3886 SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags",
3887 SD_BUS_ARGS("t", flags
),
3890 SD_BUS_VTABLE_UNPRIVILEGED
),
3891 SD_BUS_METHOD_WITH_ARGS("HybridSleep",
3892 SD_BUS_ARGS("b", interactive
),
3894 method_hybrid_sleep
,
3895 SD_BUS_VTABLE_UNPRIVILEGED
),
3896 SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags",
3897 SD_BUS_ARGS("t", flags
),
3899 method_hybrid_sleep
,
3900 SD_BUS_VTABLE_UNPRIVILEGED
),
3901 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate",
3902 SD_BUS_ARGS("b", interactive
),
3904 method_suspend_then_hibernate
,
3905 SD_BUS_VTABLE_UNPRIVILEGED
),
3906 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags",
3907 SD_BUS_ARGS("t", flags
),
3909 method_suspend_then_hibernate
,
3910 SD_BUS_VTABLE_UNPRIVILEGED
),
3911 SD_BUS_METHOD_WITH_ARGS("Sleep",
3912 SD_BUS_ARGS("t", flags
),
3915 SD_BUS_VTABLE_UNPRIVILEGED
),
3916 SD_BUS_METHOD_WITH_ARGS("CanPowerOff",
3918 SD_BUS_RESULT("s", result
),
3919 method_can_poweroff
,
3920 SD_BUS_VTABLE_UNPRIVILEGED
),
3921 SD_BUS_METHOD_WITH_ARGS("CanReboot",
3923 SD_BUS_RESULT("s", result
),
3925 SD_BUS_VTABLE_UNPRIVILEGED
),
3926 SD_BUS_METHOD_WITH_ARGS("CanHalt",
3928 SD_BUS_RESULT("s", result
),
3930 SD_BUS_VTABLE_UNPRIVILEGED
),
3931 SD_BUS_METHOD_WITH_ARGS("CanSuspend",
3933 SD_BUS_RESULT("s", result
),
3935 SD_BUS_VTABLE_UNPRIVILEGED
),
3936 SD_BUS_METHOD_WITH_ARGS("CanHibernate",
3938 SD_BUS_RESULT("s", result
),
3939 method_can_hibernate
,
3940 SD_BUS_VTABLE_UNPRIVILEGED
),
3941 SD_BUS_METHOD_WITH_ARGS("CanHybridSleep",
3943 SD_BUS_RESULT("s", result
),
3944 method_can_hybrid_sleep
,
3945 SD_BUS_VTABLE_UNPRIVILEGED
),
3946 SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate",
3948 SD_BUS_RESULT("s", result
),
3949 method_can_suspend_then_hibernate
,
3950 SD_BUS_VTABLE_UNPRIVILEGED
),
3951 SD_BUS_METHOD_WITH_ARGS("CanSleep",
3953 SD_BUS_RESULT("s", result
),
3955 SD_BUS_VTABLE_UNPRIVILEGED
),
3956 SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown",
3957 SD_BUS_ARGS("s", type
, "t", usec
),
3959 method_schedule_shutdown
,
3960 SD_BUS_VTABLE_UNPRIVILEGED
),
3961 SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown",
3963 SD_BUS_RESULT("b", cancelled
),
3964 method_cancel_scheduled_shutdown
,
3965 SD_BUS_VTABLE_UNPRIVILEGED
),
3966 SD_BUS_METHOD_WITH_ARGS("Inhibit",
3967 SD_BUS_ARGS("s", what
, "s", who
, "s", why
, "s", mode
),
3968 SD_BUS_RESULT("h", pipe_fd
),
3970 SD_BUS_VTABLE_UNPRIVILEGED
),
3971 SD_BUS_METHOD_WITH_ARGS("CanRebootParameter",
3973 SD_BUS_RESULT("s", result
),
3974 method_can_reboot_parameter
,
3975 SD_BUS_VTABLE_UNPRIVILEGED
),
3976 SD_BUS_METHOD_WITH_ARGS("SetRebootParameter",
3977 SD_BUS_ARGS("s", parameter
),
3979 method_set_reboot_parameter
,
3980 SD_BUS_VTABLE_UNPRIVILEGED
),
3981 SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup",
3983 SD_BUS_RESULT("s", result
),
3984 method_can_reboot_to_firmware_setup
,
3985 SD_BUS_VTABLE_UNPRIVILEGED
),
3986 SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup",
3987 SD_BUS_ARGS("b", enable
),
3989 method_set_reboot_to_firmware_setup
,
3990 SD_BUS_VTABLE_UNPRIVILEGED
),
3991 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu",
3993 SD_BUS_RESULT("s", result
),
3994 method_can_reboot_to_boot_loader_menu
,
3995 SD_BUS_VTABLE_UNPRIVILEGED
),
3996 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu",
3997 SD_BUS_ARGS("t", timeout
),
3999 method_set_reboot_to_boot_loader_menu
,
4000 SD_BUS_VTABLE_UNPRIVILEGED
),
4001 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry",
4003 SD_BUS_RESULT("s", result
),
4004 method_can_reboot_to_boot_loader_entry
,
4005 SD_BUS_VTABLE_UNPRIVILEGED
),
4006 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry",
4007 SD_BUS_ARGS("s", boot_loader_entry
),
4009 method_set_reboot_to_boot_loader_entry
,
4010 SD_BUS_VTABLE_UNPRIVILEGED
),
4011 SD_BUS_METHOD_WITH_ARGS("SetWallMessage",
4012 SD_BUS_ARGS("s", wall_message
, "b", enable
),
4014 method_set_wall_message
,
4015 SD_BUS_VTABLE_UNPRIVILEGED
),
4017 SD_BUS_SIGNAL_WITH_ARGS("SessionNew",
4018 SD_BUS_ARGS("s", session_id
, "o", object_path
),
4020 SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved",
4021 SD_BUS_ARGS("s", session_id
, "o", object_path
),
4023 SD_BUS_SIGNAL_WITH_ARGS("UserNew",
4024 SD_BUS_ARGS("u", uid
, "o", object_path
),
4026 SD_BUS_SIGNAL_WITH_ARGS("UserRemoved",
4027 SD_BUS_ARGS("u", uid
, "o", object_path
),
4029 SD_BUS_SIGNAL_WITH_ARGS("SeatNew",
4030 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4032 SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved",
4033 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4035 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown",
4036 SD_BUS_ARGS("b", start
),
4038 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdownWithMetadata",
4039 SD_BUS_ARGS("b", start
, "a{sv}", metadata
),
4041 SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep",
4042 SD_BUS_ARGS("b", start
),
4048 const BusObjectImplementation manager_object
= {
4049 "/org/freedesktop/login1",
4050 "org.freedesktop.login1.Manager",
4051 .vtables
= BUS_VTABLES(manager_vtable
),
4052 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
4057 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
4064 if (result
&& !streq(result
, "done")) {
4065 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
4067 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
4068 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
4069 return session_send_create_reply(s
, &e
);
4072 return session_send_create_reply(s
, NULL
);
4075 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4076 const char *path
, *result
, *unit
;
4077 Manager
*m
= ASSERT_PTR(userdata
);
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
;
4103 session
= hashmap_get(m
->session_units
, unit
);
4105 if (streq_ptr(path
, session
->scope_job
)) {
4106 session
->scope_job
= mfree(session
->scope_job
);
4107 (void) session_jobs_reply(session
, id
, unit
, result
);
4109 session_save(session
);
4110 user_save(session
->user
);
4113 session_add_to_gc_queue(session
);
4116 user
= hashmap_get(m
->user_units
, unit
);
4118 if (streq_ptr(path
, user
->service_job
)) {
4119 user
->service_job
= mfree(user
->service_job
);
4121 LIST_FOREACH(sessions_by_user
, s
, user
->sessions
)
4122 (void) session_jobs_reply(s
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
4127 user_add_to_gc_queue(user
);
4133 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4134 const char *path
, *unit
;
4135 Manager
*m
= ASSERT_PTR(userdata
);
4142 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
4144 bus_log_parse_error(r
);
4148 session
= hashmap_get(m
->session_units
, unit
);
4150 session_add_to_gc_queue(session
);
4152 user
= hashmap_get(m
->user_units
, unit
);
4154 user_add_to_gc_queue(user
);
4159 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4160 _cleanup_free_
char *unit
= NULL
;
4161 Manager
*m
= ASSERT_PTR(userdata
);
4169 path
= sd_bus_message_get_path(message
);
4173 r
= unit_name_from_dbus_path(path
, &unit
);
4174 if (r
== -EINVAL
) /* not a unit */
4181 session
= hashmap_get(m
->session_units
, unit
);
4183 session_add_to_gc_queue(session
);
4185 user
= hashmap_get(m
->user_units
, unit
);
4187 user_add_to_gc_queue(user
);
4192 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4193 Manager
*m
= ASSERT_PTR(userdata
);
4199 r
= sd_bus_message_read(message
, "b", &b
);
4201 bus_log_parse_error(r
);
4208 /* systemd finished reloading, let's recheck all our sessions */
4209 log_debug("System manager has been reloaded, rechecking sessions...");
4211 HASHMAP_FOREACH(session
, m
->sessions
)
4212 session_add_to_gc_queue(session
);
4217 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
4222 l
= strv_from_stdarg_alloca(property
);
4224 return sd_bus_emit_properties_changed_strv(
4226 "/org/freedesktop/login1",
4227 "org.freedesktop.login1.Manager",
4231 static int strdup_job(sd_bus_message
*reply
, char **job
) {
4236 r
= sd_bus_message_read(reply
, "o", &j
);
4248 int manager_start_scope(
4251 const PidRef
*pidref
,
4253 const char *description
,
4256 const char *requires_mounts_for
,
4257 sd_bus_message
*more_properties
,
4258 sd_bus_error
*error
,
4261 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
4266 assert(pidref_is_set(pidref
));
4269 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
4273 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
4277 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
4281 if (!isempty(slice
)) {
4282 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
4287 if (!isempty(description
)) {
4288 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4293 STRV_FOREACH(i
, wants
) {
4294 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
4299 STRV_FOREACH(i
, after
) {
4300 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4305 if (!empty_or_root(requires_mounts_for
)) {
4306 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4311 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4313 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4317 r
= bus_append_scope_pidref(m
, pidref
);
4321 /* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of
4323 r
= sd_bus_message_append(m
, "(sv)", "OOMPolicy", "s", "continue");
4327 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4328 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_MAX
);
4330 return bus_log_create_error(r
);
4332 if (more_properties
) {
4333 /* If TasksMax also appears here, it will overwrite the default value set above */
4334 r
= sd_bus_message_copy(m
, more_properties
, true);
4339 r
= sd_bus_message_close_container(m
);
4343 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4347 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4351 return strdup_job(reply
, job
);
4354 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4355 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4362 r
= bus_call_method(
4368 "ss", unit
, "replace");
4372 return strdup_job(reply
, job
);
4375 int manager_stop_unit(Manager
*manager
, const char *unit
, const char *job_mode
, sd_bus_error
*error
, char **ret_job
) {
4376 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4383 r
= bus_call_method(
4389 "ss", unit
, job_mode
?: "fail");
4391 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
4392 BUS_ERROR_LOAD_FAILED
)) {
4395 sd_bus_error_free(error
);
4402 return strdup_job(reply
, ret_job
);
4405 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4406 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4407 _cleanup_free_
char *path
= NULL
;
4413 path
= unit_dbus_path_from_name(scope
);
4417 r
= sd_bus_call_method(
4419 "org.freedesktop.systemd1",
4421 "org.freedesktop.systemd1.Scope",
4427 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4428 BUS_ERROR_LOAD_FAILED
,
4429 BUS_ERROR_SCOPE_NOT_RUNNING
))
4432 sd_bus_error_move(ret_error
, &error
);
4439 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4443 return bus_call_method(
4449 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4452 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4453 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4454 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4455 _cleanup_free_
char *path
= NULL
;
4462 path
= unit_dbus_path_from_name(unit
);
4466 r
= sd_bus_get_property(
4468 "org.freedesktop.systemd1",
4470 "org.freedesktop.systemd1.Unit",
4476 /* systemd might have dropped off momentarily, let's
4477 * not make this an error */
4478 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4479 SD_BUS_ERROR_DISCONNECTED
))
4482 /* If the unit is already unloaded then it's not
4484 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4485 BUS_ERROR_LOAD_FAILED
))
4488 sd_bus_error_move(ret_error
, &error
);
4492 r
= sd_bus_message_read(reply
, "s", &state
);
4496 return !STR_IN_SET(state
, "inactive", "failed");
4499 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4500 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4501 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4507 r
= sd_bus_get_property(
4509 "org.freedesktop.systemd1",
4511 "org.freedesktop.systemd1.Job",
4517 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4518 SD_BUS_ERROR_DISCONNECTED
))
4521 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4524 sd_bus_error_move(ret_error
, &error
);
4528 /* We don't actually care about the state really. The fact
4529 * that we could read the job state is enough for us */