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
,
241 const char **details
,
249 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
256 result
= "challenge";
260 return sd_bus_reply_method_return(message
, "s", result
);
263 static int property_get_idle_hint(
266 const char *interface
,
267 const char *property
,
268 sd_bus_message
*reply
,
270 sd_bus_error
*error
) {
272 Manager
*m
= ASSERT_PTR(userdata
);
277 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
280 static int property_get_idle_since_hint(
283 const char *interface
,
284 const char *property
,
285 sd_bus_message
*reply
,
287 sd_bus_error
*error
) {
289 Manager
*m
= ASSERT_PTR(userdata
);
290 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
295 manager_get_idle_hint(m
, &t
);
297 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
300 static int property_get_inhibited(
303 const char *interface
,
304 const char *property
,
305 sd_bus_message
*reply
,
307 sd_bus_error
*error
) {
309 Manager
*m
= ASSERT_PTR(userdata
);
315 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
317 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
320 static int property_get_preparing(
323 const char *interface
,
324 const char *property
,
325 sd_bus_message
*reply
,
327 sd_bus_error
*error
) {
329 Manager
*m
= ASSERT_PTR(userdata
);
335 if (m
->delayed_action
) {
336 if (streq(property
, "PreparingForShutdown"))
337 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SHUTDOWN
;
339 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SLEEP
;
342 return sd_bus_message_append(reply
, "b", b
);
345 static int property_get_sleep_operations(
348 const char *interface
,
349 const char *property
,
350 sd_bus_message
*reply
,
352 sd_bus_error
*error
) {
354 Manager
*m
= ASSERT_PTR(userdata
);
355 _cleanup_strv_free_
char **actions
= NULL
;
361 r
= handle_action_get_enabled_sleep_actions(m
->handle_action_sleep_mask
, &actions
);
365 return sd_bus_message_append_strv(reply
, actions
);
368 static int property_get_scheduled_shutdown(
371 const char *interface
,
372 const char *property
,
373 sd_bus_message
*reply
,
375 sd_bus_error
*error
) {
377 Manager
*m
= ASSERT_PTR(userdata
);
383 r
= sd_bus_message_open_container(reply
, 'r', "st");
387 r
= sd_bus_message_append(
389 handle_action_to_string(m
->scheduled_shutdown_action
),
390 m
->scheduled_shutdown_timeout
);
394 return sd_bus_message_close_container(reply
);
397 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
398 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
399 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
400 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power());
401 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
402 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
404 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
405 _cleanup_free_
char *p
= NULL
;
406 Manager
*m
= ASSERT_PTR(userdata
);
413 r
= sd_bus_message_read(message
, "s", &name
);
417 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
421 p
= session_bus_path(session
);
425 return sd_bus_reply_method_return(message
, "o", p
);
428 /* Get login session of a process. This is not what you are looking for these days,
429 * as apps may instead belong to a user service unit. This includes terminal
430 * emulators and hence command-line apps. */
431 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
432 Manager
*m
= ASSERT_PTR(userdata
);
433 _cleanup_free_
char *p
= NULL
;
434 Session
*session
= NULL
;
440 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
442 r
= sd_bus_message_read(message
, "u", &pid
);
449 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
453 r
= manager_get_session_by_pidref(m
, &PIDREF_MAKE_FROM_PID(pid
), &session
);
458 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
459 "PID "PID_FMT
" does not belong to any known session", pid
);
462 p
= session_bus_path(session
);
466 return sd_bus_reply_method_return(message
, "o", p
);
469 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
470 _cleanup_free_
char *p
= NULL
;
471 Manager
*m
= ASSERT_PTR(userdata
);
478 r
= sd_bus_message_read(message
, "u", &uid
);
482 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
486 p
= user_bus_path(user
);
490 return sd_bus_reply_method_return(message
, "o", p
);
493 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
494 _cleanup_free_
char *p
= NULL
;
495 Manager
*m
= ASSERT_PTR(userdata
);
502 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
504 r
= sd_bus_message_read(message
, "u", &pid
);
511 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
515 r
= manager_get_user_by_pid(m
, pid
, &user
);
519 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
520 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
524 p
= user_bus_path(user
);
528 return sd_bus_reply_method_return(message
, "o", p
);
531 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
532 _cleanup_free_
char *p
= NULL
;
533 Manager
*m
= ASSERT_PTR(userdata
);
540 r
= sd_bus_message_read(message
, "s", &name
);
544 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
548 p
= seat_bus_path(seat
);
552 return sd_bus_reply_method_return(message
, "o", p
);
555 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
556 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
557 Manager
*m
= ASSERT_PTR(userdata
);
563 r
= sd_bus_message_new_method_return(message
, &reply
);
567 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
571 HASHMAP_FOREACH(session
, m
->sessions
) {
572 _cleanup_free_
char *p
= NULL
;
574 p
= session_bus_path(session
);
578 r
= sd_bus_message_append(reply
, "(susso)",
580 (uint32_t) session
->user
->user_record
->uid
,
581 session
->user
->user_record
->user_name
,
582 session
->seat
? session
->seat
->id
: "",
588 r
= sd_bus_message_close_container(reply
);
592 return sd_bus_send(NULL
, reply
, NULL
);
595 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
596 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
597 Manager
*m
= ASSERT_PTR(userdata
);
603 r
= sd_bus_message_new_method_return(message
, &reply
);
607 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
611 HASHMAP_FOREACH(user
, m
->users
) {
612 _cleanup_free_
char *p
= NULL
;
614 p
= user_bus_path(user
);
618 r
= sd_bus_message_append(reply
, "(uso)",
619 (uint32_t) user
->user_record
->uid
,
620 user
->user_record
->user_name
,
626 r
= sd_bus_message_close_container(reply
);
630 return sd_bus_send(NULL
, reply
, NULL
);
633 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
634 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
635 Manager
*m
= ASSERT_PTR(userdata
);
641 r
= sd_bus_message_new_method_return(message
, &reply
);
645 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
649 HASHMAP_FOREACH(seat
, m
->seats
) {
650 _cleanup_free_
char *p
= NULL
;
652 p
= seat_bus_path(seat
);
656 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
661 r
= sd_bus_message_close_container(reply
);
665 return sd_bus_send(NULL
, reply
, NULL
);
668 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
669 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
670 Manager
*m
= ASSERT_PTR(userdata
);
671 Inhibitor
*inhibitor
;
676 r
= sd_bus_message_new_method_return(message
, &reply
);
680 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
684 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
) {
686 r
= sd_bus_message_append(reply
, "(ssssuu)",
687 strempty(inhibit_what_to_string(inhibitor
->what
)),
688 strempty(inhibitor
->who
),
689 strempty(inhibitor
->why
),
690 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
691 (uint32_t) inhibitor
->uid
,
692 (uint32_t) inhibitor
->pid
.pid
);
697 r
= sd_bus_message_close_container(reply
);
701 return sd_bus_send(NULL
, reply
, NULL
);
704 static int create_session(
705 sd_bus_message
*message
,
720 const char *remote_user
,
721 const char *remote_host
,
724 _cleanup_(pidref_done
) PidRef leader
= PIDREF_NULL
;
725 Manager
*m
= ASSERT_PTR(userdata
);
726 _cleanup_free_
char *id
= NULL
;
727 Session
*session
= NULL
;
728 uint32_t audit_id
= 0;
737 if (!uid_is_valid(uid
))
738 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
741 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Flags must be zero.");
744 r
= pidref_set_pidfd(&leader
, pidfd
);
747 } else if (pid
== 0) {
748 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
751 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
755 r
= sd_bus_creds_get_pid(creds
, &p
);
759 r
= pidref_set_pid(&leader
, p
);
765 r
= pidref_set_pid(&leader
, pid
);
770 if (leader
.pid
== 1 || leader
.pid
== getpid_cached())
771 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
774 t
= _SESSION_TYPE_INVALID
;
776 t
= session_type_from_string(type
);
778 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
779 "Invalid session type %s", type
);
783 c
= _SESSION_CLASS_INVALID
;
785 c
= session_class_from_string(class);
787 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
788 "Invalid session class %s", class);
791 if (isempty(desktop
))
794 if (!string_is_safe(desktop
))
795 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
796 "Invalid desktop string %s", desktop
);
802 seat
= hashmap_get(m
->seats
, cseat
);
804 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
805 "No seat '%s' known", cseat
);
808 if (tty_is_vc(tty
)) {
813 else if (seat
!= m
->seat0
)
814 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
815 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
817 v
= vtnr_from_tty(tty
);
819 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
820 "Cannot determine VT number from virtual console TTY %s", tty
);
824 else if (vtnr
!= (uint32_t) v
)
825 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
826 "Specified TTY and VT number do not match");
828 } else if (tty_is_console(tty
)) {
832 else if (seat
!= m
->seat0
)
833 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
834 "Console TTY specified but seat is not seat0");
837 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
838 "Console TTY specified but VT number is not 0");
842 if (seat_has_vts(seat
)) {
843 if (vtnr
<= 0 || vtnr
> 63)
844 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
845 "VT number out of range");
848 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
849 "Seat has no VTs but VT number not 0");
853 if (t
== _SESSION_TYPE_INVALID
) {
854 if (!isempty(display
))
856 else if (!isempty(tty
))
859 t
= SESSION_UNSPECIFIED
;
862 if (c
== _SESSION_CLASS_INVALID
) {
863 if (t
== SESSION_UNSPECIFIED
)
864 c
= SESSION_BACKGROUND
;
869 /* Check if we are already in a logind session. Or if we are in user@.service
870 * which is a special PAM session that avoids creating a logind session. */
871 r
= manager_get_user_by_pid(m
, leader
.pid
, NULL
);
875 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
876 "Already running in a session or user slice");
879 * Old gdm and lightdm start the user-session on the same VT as
880 * the greeter session. But they destroy the greeter session
881 * after the user-session and want the user-session to take
882 * over the VT. We need to support this for
883 * backwards-compatibility, so make sure we allow new sessions
884 * on a VT that a greeter is running on. Furthermore, to allow
885 * re-logins, we have to allow a greeter to take over a used VT for
886 * the exact same reasons.
888 if (c
!= SESSION_GREETER
&&
890 vtnr
< MALLOC_ELEMENTSOF(m
->seat0
->positions
) &&
891 m
->seat0
->positions
[vtnr
] &&
892 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
893 return sd_bus_error_set(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
895 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
896 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
897 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
900 (void) audit_session_from_pid(leader
.pid
, &audit_id
);
901 if (audit_session_is_valid(audit_id
)) {
902 /* Keep our session IDs and the audit session IDs in sync */
904 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
907 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
908 * not trust the audit data and let's better register a new ID */
909 if (hashmap_contains(m
->sessions
, id
)) {
910 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
911 audit_id
= AUDIT_SESSION_INVALID
;
920 if (asprintf(&id
, "c%" PRIu64
, ++m
->session_counter
) < 0)
923 } while (hashmap_contains(m
->sessions
, id
));
926 /* The generated names should not clash with 'auto' or 'self' */
927 assert(!SESSION_IS_SELF(id
));
928 assert(!SESSION_IS_AUTO(id
));
930 /* If we are not watching utmp already, try again */
931 manager_reconnect_utmp(m
);
933 r
= manager_add_user_by_uid(m
, uid
, &user
);
937 r
= manager_add_session(m
, id
, &session
);
941 session_set_user(session
, user
);
942 r
= session_set_leader_consume(session
, TAKE_PIDREF(leader
));
946 session
->original_type
= session
->type
= t
;
948 session
->remote
= remote
;
949 session
->vtnr
= vtnr
;
952 session
->tty
= strdup(tty
);
958 session
->tty_validity
= TTY_FROM_PAM
;
961 if (!isempty(display
)) {
962 session
->display
= strdup(display
);
963 if (!session
->display
) {
969 if (!isempty(remote_user
)) {
970 session
->remote_user
= strdup(remote_user
);
971 if (!session
->remote_user
) {
977 if (!isempty(remote_host
)) {
978 session
->remote_host
= strdup(remote_host
);
979 if (!session
->remote_host
) {
985 if (!isempty(service
)) {
986 session
->service
= strdup(service
);
987 if (!session
->service
) {
993 if (!isempty(desktop
)) {
994 session
->desktop
= strdup(desktop
);
995 if (!session
->desktop
) {
1002 r
= seat_attach_session(seat
, session
);
1007 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1011 r
= session_start(session
, message
, error
);
1015 r
= sd_bus_message_exit_container(message
);
1019 session
->create_message
= sd_bus_message_ref(message
);
1021 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
1022 * session_send_create_reply(). */
1028 session_add_to_gc_queue(session
);
1031 user_add_to_gc_queue(user
);
1036 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1037 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1046 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
1047 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
1049 r
= sd_bus_message_read(message
,
1067 return create_session(
1073 /* pidfd = */ -EBADF
,
1088 static int method_create_session_pidfd(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1089 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1090 int leaderfd
= -EBADF
;
1097 r
= sd_bus_message_read(message
,
1116 return create_session(
1137 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1138 Manager
*m
= ASSERT_PTR(userdata
);
1145 r
= sd_bus_message_read(message
, "s", &name
);
1149 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1153 r
= session_release(session
);
1157 return sd_bus_reply_method_return(message
, NULL
);
1160 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1161 Manager
*m
= ASSERT_PTR(userdata
);
1168 r
= sd_bus_message_read(message
, "s", &name
);
1172 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1176 /* PolicyKit is done by bus_session_method_activate() */
1178 return bus_session_method_activate(message
, session
, error
);
1181 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1182 const char *session_name
, *seat_name
;
1183 Manager
*m
= ASSERT_PTR(userdata
);
1190 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1192 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1196 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1200 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1204 if (session
->seat
!= seat
)
1205 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1206 "Session %s not on seat %s", session_name
, seat_name
);
1208 r
= check_polkit_chvt(message
, m
, error
);
1212 return 1; /* Will call us back */
1214 r
= session_activate(session
);
1218 return sd_bus_reply_method_return(message
, NULL
);
1221 static int method_lock_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 return bus_session_method_lock(message
, session
, error
);
1240 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1241 Manager
*m
= ASSERT_PTR(userdata
);
1246 r
= bus_verify_polkit_async(
1249 "org.freedesktop.login1.lock-sessions",
1253 &m
->polkit_registry
,
1258 return 1; /* Will call us back */
1260 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1264 return sd_bus_reply_method_return(message
, NULL
);
1267 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1269 Manager
*m
= ASSERT_PTR(userdata
);
1275 r
= sd_bus_message_read(message
, "s", &name
);
1279 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1283 return bus_session_method_kill(message
, session
, error
);
1286 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1287 Manager
*m
= ASSERT_PTR(userdata
);
1294 r
= sd_bus_message_read(message
, "u", &uid
);
1298 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1302 return bus_user_method_kill(message
, user
, error
);
1305 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1306 Manager
*m
= ASSERT_PTR(userdata
);
1313 r
= sd_bus_message_read(message
, "s", &name
);
1317 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1321 return bus_session_method_terminate(message
, session
, error
);
1324 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1325 Manager
*m
= ASSERT_PTR(userdata
);
1332 r
= sd_bus_message_read(message
, "u", &uid
);
1336 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1340 return bus_user_method_terminate(message
, user
, error
);
1343 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1344 Manager
*m
= ASSERT_PTR(userdata
);
1351 r
= sd_bus_message_read(message
, "s", &name
);
1355 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1359 return bus_seat_method_terminate(message
, seat
, error
);
1362 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1363 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1364 _cleanup_free_
char *cc
= NULL
;
1365 Manager
*m
= ASSERT_PTR(userdata
);
1366 int r
, b
, interactive
;
1369 uint32_t uid
, auth_uid
;
1373 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1377 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1378 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1382 if (!uid_is_valid(uid
)) {
1383 /* Note that we get the owner UID of the session or user unit,
1384 * not the actual client UID here! */
1385 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1390 /* owner_uid is racy, so for authorization we must use euid */
1391 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1398 return errno_or_else(ENOENT
);
1400 r
= bus_verify_polkit_async(
1403 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1404 "org.freedesktop.login1.set-user-linger",
1408 &m
->polkit_registry
,
1413 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1415 (void) mkdir_p_label("/var/lib/systemd", 0755);
1416 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1420 cc
= cescape(pw
->pw_name
);
1424 path
= strjoina("/var/lib/systemd/linger/", cc
);
1432 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1439 if (r
< 0 && errno
!= ENOENT
)
1442 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1444 user_add_to_gc_queue(u
);
1447 return sd_bus_reply_method_return(message
, NULL
);
1450 static int trigger_device(Manager
*m
, sd_device
*parent
) {
1451 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1456 r
= sd_device_enumerator_new(&e
);
1460 r
= sd_device_enumerator_allow_uninitialized(e
);
1465 r
= sd_device_enumerator_add_match_parent(e
, parent
);
1470 FOREACH_DEVICE(e
, d
) {
1471 r
= sd_device_trigger(d
, SD_DEVICE_CHANGE
);
1473 log_device_debug_errno(d
, r
, "Failed to trigger device, ignoring: %m");
1479 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
, sd_bus_error
*error
) {
1480 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1481 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1482 const char *id_for_seat
;
1489 r
= sd_device_new_from_syspath(&d
, sysfs
);
1491 return sd_bus_error_set_errnof(error
, r
, "Failed to open device '%s': %m", sysfs
);
1493 if (sd_device_has_current_tag(d
, "seat") <= 0)
1494 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'seat' udev tag.", sysfs
);
1496 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1497 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'ID_FOR_SEAT' udev property.", sysfs
);
1499 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1502 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1505 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1506 r
= write_string_file_atomic_label(file
, rule
);
1510 return trigger_device(m
, d
);
1513 static int flush_devices(Manager
*m
) {
1514 _cleanup_closedir_
DIR *d
= NULL
;
1518 d
= opendir("/etc/udev/rules.d");
1520 if (errno
!= ENOENT
)
1521 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1523 FOREACH_DIRENT_ALL(de
, d
, break) {
1524 if (!dirent_is_file(de
))
1527 if (!startswith(de
->d_name
, "72-seat-"))
1530 if (!endswith(de
->d_name
, ".rules"))
1533 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1534 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1537 return trigger_device(m
, NULL
);
1540 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1541 const char *sysfs
, *seat
;
1542 Manager
*m
= ASSERT_PTR(userdata
);
1547 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1551 if (!path_is_normalized(sysfs
))
1552 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1553 if (!path_startswith(sysfs
, "/sys"))
1554 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1556 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1559 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1565 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1566 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1568 r
= bus_verify_polkit_async(
1571 "org.freedesktop.login1.attach-device",
1575 &m
->polkit_registry
,
1580 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1582 r
= attach_device(m
, seat
, sysfs
, error
);
1586 return sd_bus_reply_method_return(message
, NULL
);
1589 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1590 Manager
*m
= ASSERT_PTR(userdata
);
1595 r
= sd_bus_message_read(message
, "b", &interactive
);
1599 r
= bus_verify_polkit_async(
1602 "org.freedesktop.login1.flush-devices",
1606 &m
->polkit_registry
,
1611 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1613 r
= flush_devices(m
);
1617 return sd_bus_reply_method_return(message
, NULL
);
1620 static int have_multiple_sessions(
1628 /* Check for other users' sessions. Greeter sessions do not
1629 * count, and non-login sessions do not count either. */
1630 HASHMAP_FOREACH(session
, m
->sessions
)
1631 if (session
->class == SESSION_USER
&&
1632 session
->user
->user_record
->uid
!= uid
)
1638 static int bus_manager_log_shutdown(
1640 const HandleActionData
*a
) {
1644 const char *message
= a
->message
?: "System is shutting down";
1645 const char *log_verb
= a
->log_verb
? strjoina("SHUTDOWN=", a
->log_verb
) : NULL
;
1647 return log_struct(LOG_NOTICE
,
1648 "MESSAGE_ID=%s", a
->message_id
?: SD_MESSAGE_SHUTDOWN_STR
,
1649 LOG_MESSAGE("%s%s%s%s.",
1651 m
->wall_message
? " (" : "",
1652 strempty(m
->wall_message
),
1653 m
->wall_message
? ")" : ""),
1657 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1658 Manager
*m
= ASSERT_PTR(userdata
);
1662 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1666 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1671 if (until
<= now(CLOCK_MONOTONIC
))
1674 /* We want to ignore the lid switch for a while after each
1675 * suspend, and after boot-up. Hence let's install a timer for
1676 * this. As long as the event source exists we ignore the lid
1679 if (m
->lid_switch_ignore_event_source
) {
1682 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1689 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1691 r
= sd_event_add_time(
1693 &m
->lid_switch_ignore_event_source
,
1696 lid_switch_ignore_handler
, m
);
1701 static int send_prepare_for(Manager
*m
, const HandleActionData
*a
, bool _active
) {
1702 int k
= 0, r
, active
= _active
;
1706 assert(IN_SET(a
->inhibit_what
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1708 /* We need to send both old and new signal for backward compatibility. The newer one allows clients
1709 * to know which type of reboot is going to happen, as they might be doing different actions (e.g.:
1710 * on soft-reboot), and it is sent first, so that clients know that if they receive the old one
1711 * first then they don't have to wait for the new one, as it means it's not supported. So, do not
1712 * change the order here, as it is an API. */
1713 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
) {
1714 k
= sd_bus_emit_signal(m
->bus
,
1715 "/org/freedesktop/login1",
1716 "org.freedesktop.login1.Manager",
1717 "PrepareForShutdownWithMetadata",
1723 handle_action_to_string(a
->handle
));
1725 log_debug_errno(k
, "Failed to emit PrepareForShutdownWithMetadata(): %m");
1728 r
= sd_bus_emit_signal(m
->bus
,
1729 "/org/freedesktop/login1",
1730 "org.freedesktop.login1.Manager",
1731 a
->inhibit_what
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1735 log_debug_errno(r
, "Failed to emit PrepareForShutdown(): %m");
1737 return RET_GATHER(k
, r
);
1740 static int execute_shutdown_or_sleep(
1742 const HandleActionData
*a
,
1743 sd_bus_error
*error
) {
1745 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1752 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
)
1753 bus_manager_log_shutdown(m
, a
);
1755 r
= bus_call_method(
1761 "ss", a
->target
, "replace-irreversibly");
1765 r
= sd_bus_message_read(reply
, "o", &p
);
1769 r
= free_and_strdup(&m
->action_job
, p
);
1773 m
->delayed_action
= a
;
1775 /* Make sure the lid switch is ignored for a while */
1776 manager_set_lid_switch_ignore(m
, usec_add(now(CLOCK_MONOTONIC
), m
->holdoff_timeout_usec
));
1781 /* Tell people that they now may take a lock again */
1782 (void) send_prepare_for(m
, a
, false);
1787 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1788 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1789 Inhibitor
*offending
= NULL
;
1794 if (!manager
->delayed_action
|| manager
->action_job
)
1797 if (manager_is_inhibited(manager
, manager
->delayed_action
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1798 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1803 (void) pidref_get_comm(&offending
->pid
, &comm
);
1804 u
= uid_to_name(offending
->uid
);
1806 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1807 offending
->uid
, strna(u
),
1808 offending
->pid
.pid
, strna(comm
));
1811 /* Actually do the operation */
1812 r
= execute_shutdown_or_sleep(manager
, manager
->delayed_action
, &error
);
1814 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1815 bus_error_message(&error
, r
));
1817 manager
->delayed_action
= NULL
;
1820 return 1; /* We did some work. */
1823 static int manager_inhibit_timeout_handler(
1828 Manager
*manager
= ASSERT_PTR(userdata
);
1830 assert(manager
->inhibit_timeout_source
== s
);
1832 return manager_dispatch_delayed(manager
, true);
1835 static int delay_shutdown_or_sleep(
1837 const HandleActionData
*a
) {
1844 if (m
->inhibit_timeout_source
) {
1845 r
= sd_event_source_set_time_relative(m
->inhibit_timeout_source
, m
->inhibit_delay_max
);
1847 return log_error_errno(r
, "sd_event_source_set_time_relative() failed: %m");
1849 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1851 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1853 r
= sd_event_add_time_relative(
1855 &m
->inhibit_timeout_source
,
1856 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, 0,
1857 manager_inhibit_timeout_handler
, m
);
1862 m
->delayed_action
= a
;
1867 int bus_manager_shutdown_or_sleep_now_or_later(
1869 const HandleActionData
*a
,
1870 sd_bus_error
*error
) {
1872 _cleanup_free_
char *load_state
= NULL
;
1878 assert(!m
->action_job
);
1880 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
1884 if (!streq(load_state
, "loaded"))
1885 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1886 "Unit %s is %s, refusing operation.",
1887 a
->target
, load_state
);
1889 /* Tell everybody to prepare for shutdown/sleep */
1890 (void) send_prepare_for(m
, a
, true);
1893 m
->inhibit_delay_max
> 0 &&
1894 manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1897 /* Shutdown is delayed, keep in mind what we
1898 * want to do, and start a timeout */
1899 r
= delay_shutdown_or_sleep(m
, a
);
1901 /* Shutdown is not delayed, execute it
1903 r
= execute_shutdown_or_sleep(m
, a
, error
);
1908 static int verify_shutdown_creds(
1910 sd_bus_message
*message
,
1911 const HandleActionData
*a
,
1913 sd_bus_error
*error
) {
1915 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1916 bool multiple_sessions
, blocked
, interactive
;
1924 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1928 r
= sd_bus_creds_get_euid(creds
, &uid
);
1932 r
= have_multiple_sessions(m
, uid
);
1936 multiple_sessions
= r
> 0;
1937 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1938 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
1940 if (multiple_sessions
) {
1941 r
= bus_verify_polkit_async(
1944 a
->polkit_action_multiple_sessions
,
1948 &m
->polkit_registry
,
1953 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1957 /* We don't check polkit for root here, because you can't be more privileged than root */
1958 if (uid
== 0 && (flags
& SD_LOGIND_ROOT_CHECK_INHIBITORS
))
1959 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
,
1960 "Access denied to root due to active block inhibitor");
1962 r
= bus_verify_polkit_async(message
,
1964 a
->polkit_action_ignore_inhibit
,
1968 &m
->polkit_registry
,
1973 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1976 if (!multiple_sessions
&& !blocked
) {
1977 r
= bus_verify_polkit_async(message
,
1983 &m
->polkit_registry
,
1988 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1994 static int setup_wall_message_timer(Manager
*m
, sd_bus_message
* message
) {
1995 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1998 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2000 const char *tty
= NULL
;
2002 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2003 (void) sd_bus_creds_get_tty(creds
, &tty
);
2005 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2010 r
= manager_setup_wall_message_timer(m
);
2017 static int method_do_shutdown_or_sleep(
2019 sd_bus_message
*message
,
2020 HandleAction action
,
2022 sd_bus_error
*error
) {
2029 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2032 /* New style method: with flags parameter (and interactive bool in the bus message header) */
2033 r
= sd_bus_message_read(message
, "t", &flags
);
2036 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
2037 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2038 "Invalid flags parameter");
2040 if (FLAGS_SET(flags
, (SD_LOGIND_REBOOT_VIA_KEXEC
|SD_LOGIND_SOFT_REBOOT
)))
2041 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2042 "Both reboot via kexec and soft reboot selected, which is not supported");
2044 if (action
!= HANDLE_REBOOT
) {
2045 if (flags
& SD_LOGIND_REBOOT_VIA_KEXEC
)
2046 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2047 "Reboot via kexec option is only applicable with reboot operations");
2048 if ((flags
& SD_LOGIND_SOFT_REBOOT
) || (flags
& SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
))
2049 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2050 "Soft reboot option is only applicable with reboot operations");
2053 /* Old style method: no flags parameter, but interactive bool passed as boolean in
2054 * payload. Let's convert this argument to the new-style flags parameter for our internal
2058 r
= sd_bus_message_read(message
, "b", &interactive
);
2062 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
2065 const HandleActionData
*a
= NULL
;
2067 if ((flags
& SD_LOGIND_SOFT_REBOOT
) ||
2068 ((flags
& SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
) && path_is_os_tree("/run/nextroot") > 0))
2069 a
= handle_action_lookup(HANDLE_SOFT_REBOOT
);
2070 else if ((flags
& SD_LOGIND_REBOOT_VIA_KEXEC
) && kexec_loaded())
2071 a
= handle_action_lookup(HANDLE_KEXEC
);
2073 if (action
== HANDLE_SLEEP
) {
2074 HandleAction selected
;
2076 selected
= handle_action_sleep_select(m
->handle_action_sleep_mask
);
2078 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2079 "None of the configured sleep operations are supported");
2081 assert_se(a
= handle_action_lookup(selected
));
2083 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2084 SleepSupport support
;
2086 assert_se(a
= handle_action_lookup(action
));
2088 assert(a
->sleep_operation
>= 0);
2089 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2091 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2097 case SLEEP_DISABLED
:
2098 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2099 "Sleep verb '%s' is disabled by config",
2100 sleep_operation_to_string(a
->sleep_operation
));
2102 case SLEEP_NOT_CONFIGURED
:
2103 case SLEEP_STATE_OR_MODE_NOT_SUPPORTED
:
2104 case SLEEP_ALARM_NOT_SUPPORTED
:
2105 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2106 "Sleep verb '%s' is not configured or configuration is not supported by kernel",
2107 sleep_operation_to_string(a
->sleep_operation
));
2109 case SLEEP_RESUME_NOT_SUPPORTED
:
2110 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2111 "Not running on EFI and resume= is not set. No available method to resume from hibernation");
2113 case SLEEP_NOT_ENOUGH_SWAP_SPACE
:
2114 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2115 "Not enough suitable swap space for hibernation available on compatible block devices and file systems");
2118 assert_not_reached();
2122 assert_se(a
= handle_action_lookup(action
));
2124 r
= verify_shutdown_creds(m
, message
, a
, flags
, error
);
2128 /* reset case we're shorting a scheduled shutdown */
2129 m
->unlink_nologin
= false;
2130 reset_scheduled_shutdown(m
);
2132 m
->scheduled_shutdown_timeout
= 0;
2133 m
->scheduled_shutdown_action
= action
;
2135 (void) setup_wall_message_timer(m
, message
);
2137 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, error
);
2141 return sd_bus_reply_method_return(message
, NULL
);
2144 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2145 Manager
*m
= userdata
;
2147 return method_do_shutdown_or_sleep(
2150 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
2154 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2155 Manager
*m
= userdata
;
2157 return method_do_shutdown_or_sleep(
2160 sd_bus_message_is_method_call(message
, NULL
, "RebootWithFlags"),
2164 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2165 Manager
*m
= userdata
;
2167 return method_do_shutdown_or_sleep(
2170 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
2174 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2175 Manager
*m
= userdata
;
2177 return method_do_shutdown_or_sleep(
2180 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
2184 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2185 Manager
*m
= userdata
;
2187 return method_do_shutdown_or_sleep(
2190 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
2194 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2195 Manager
*m
= userdata
;
2197 return method_do_shutdown_or_sleep(
2199 HANDLE_HYBRID_SLEEP
,
2200 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
2204 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2205 Manager
*m
= userdata
;
2207 return method_do_shutdown_or_sleep(
2209 HANDLE_SUSPEND_THEN_HIBERNATE
,
2210 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
2214 static int method_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2215 Manager
*m
= userdata
;
2217 return method_do_shutdown_or_sleep(
2220 /* with_flags = */ true,
2224 static int nologin_timeout_handler(
2229 Manager
*m
= userdata
;
2231 log_info("Creating /run/nologin, blocking further logins...");
2234 create_shutdown_run_nologin_or_warn() >= 0;
2239 static usec_t
nologin_timeout_usec(usec_t elapse
) {
2240 /* Issue /run/nologin five minutes before shutdown */
2241 return LESS_BY(elapse
, 5 * USEC_PER_MINUTE
);
2244 void manager_load_scheduled_shutdown(Manager
*m
) {
2245 _cleanup_fclose_
FILE *f
= NULL
;
2246 _cleanup_free_
char *usec
= NULL
,
2249 *wall_message
= NULL
,
2256 r
= parse_env_file(f
, SHUTDOWN_SCHEDULE_FILE
,
2258 "WARN_WALL", &warn_wall
,
2260 "WALL_MESSAGE", &wall_message
,
2264 /* reset will delete the file */
2265 reset_scheduled_shutdown(m
);
2270 return (void) log_debug_errno(r
, "Failed to parse " SHUTDOWN_SCHEDULE_FILE
": %m");
2272 HandleAction handle
= handle_action_from_string(mode
);
2274 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse scheduled shutdown type: %s", mode
);
2277 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "USEC is required");
2278 if (deserialize_usec(usec
, &m
->scheduled_shutdown_timeout
) < 0)
2281 /* assign parsed type only after we know usec is also valid */
2282 m
->scheduled_shutdown_action
= handle
;
2285 r
= parse_boolean(warn_wall
);
2287 log_debug_errno(r
, "Failed to parse enabling wall messages");
2289 m
->enable_wall_messages
= r
;
2293 _cleanup_free_
char *unescaped
= NULL
;
2294 r
= cunescape(wall_message
, 0, &unescaped
);
2296 log_debug_errno(r
, "Failed to parse wall message: %s", wall_message
);
2298 free_and_replace(m
->wall_message
, unescaped
);
2302 r
= parse_uid(uid
, &m
->scheduled_shutdown_uid
);
2304 log_debug_errno(r
, "Failed to parse wall uid: %s", uid
);
2307 free_and_replace(m
->scheduled_shutdown_tty
, tty
);
2309 r
= manager_setup_shutdown_timers(m
);
2311 return reset_scheduled_shutdown(m
);
2313 (void) manager_setup_wall_message_timer(m
);
2314 (void) update_schedule_file(m
);
2319 static int update_schedule_file(Manager
*m
) {
2320 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
2321 _cleanup_fclose_
FILE *f
= NULL
;
2325 assert(handle_action_valid(m
->scheduled_shutdown_action
));
2327 r
= mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE
, 0755);
2329 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2331 r
= fopen_temporary(SHUTDOWN_SCHEDULE_FILE
, &f
, &temp_path
);
2333 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2335 (void) fchmod(fileno(f
), 0644);
2337 serialize_usec(f
, "USEC", m
->scheduled_shutdown_timeout
);
2338 serialize_item_format(f
, "WARN_WALL", "%s", one_zero(m
->enable_wall_messages
));
2339 serialize_item_format(f
, "MODE", "%s", handle_action_to_string(m
->scheduled_shutdown_action
));
2340 serialize_item_format(f
, "UID", UID_FMT
, m
->scheduled_shutdown_uid
);
2342 if (m
->scheduled_shutdown_tty
)
2343 serialize_item_format(f
, "TTY", "%s", m
->scheduled_shutdown_tty
);
2345 if (!isempty(m
->wall_message
)) {
2346 r
= serialize_item_escaped(f
, "WALL_MESSAGE", m
->wall_message
);
2351 r
= fflush_and_check(f
);
2355 if (rename(temp_path
, SHUTDOWN_SCHEDULE_FILE
) < 0) {
2360 temp_path
= mfree(temp_path
);
2364 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2366 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2369 static void reset_scheduled_shutdown(Manager
*m
) {
2372 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2373 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2374 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2376 m
->scheduled_shutdown_action
= _HANDLE_ACTION_INVALID
;
2377 m
->scheduled_shutdown_timeout
= USEC_INFINITY
;
2378 m
->scheduled_shutdown_uid
= UID_INVALID
;
2379 m
->scheduled_shutdown_tty
= mfree(m
->scheduled_shutdown_tty
);
2380 m
->shutdown_dry_run
= false;
2382 if (m
->unlink_nologin
) {
2383 (void) unlink_or_warn("/run/nologin");
2384 m
->unlink_nologin
= false;
2387 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2390 static int manager_scheduled_shutdown_handler(
2395 Manager
*m
= ASSERT_PTR(userdata
);
2396 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2397 const HandleActionData
*a
;
2400 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2402 /* Don't allow multiple jobs being executed at the same time */
2403 if (m
->delayed_action
) {
2405 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", a
->target
);
2409 if (m
->shutdown_dry_run
) {
2410 /* We do not process delay inhibitors here. Otherwise, we
2411 * would have to be considered "in progress" (like the check
2412 * above) for some seconds after our admin has seen the final
2415 bus_manager_log_shutdown(m
, a
);
2416 log_info("Running in dry run, suppressing action.");
2417 reset_scheduled_shutdown(m
);
2422 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, &error
);
2424 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", a
->target
);
2431 reset_scheduled_shutdown(m
);
2435 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2436 Manager
*m
= ASSERT_PTR(userdata
);
2437 HandleAction handle
;
2438 const HandleActionData
*a
;
2442 bool dry_run
= false;
2446 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2450 if (startswith(type
, "dry-")) {
2455 handle
= handle_action_from_string(type
);
2456 if (!HANDLE_ACTION_IS_SHUTDOWN(handle
))
2457 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type: %s", type
);
2459 assert_se(a
= handle_action_lookup(handle
));
2460 assert(a
->polkit_action
);
2462 r
= verify_shutdown_creds(m
, message
, a
, 0, error
);
2466 m
->scheduled_shutdown_action
= handle
;
2467 m
->shutdown_dry_run
= dry_run
;
2468 m
->scheduled_shutdown_timeout
= elapse
;
2470 r
= manager_setup_shutdown_timers(m
);
2474 r
= setup_wall_message_timer(m
, message
);
2476 r
= update_schedule_file(m
);
2479 reset_scheduled_shutdown(m
);
2483 return sd_bus_reply_method_return(message
, NULL
);
2486 static int manager_setup_shutdown_timers(Manager
* m
) {
2489 r
= event_reset_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2491 m
->scheduled_shutdown_timeout
, 0,
2492 manager_scheduled_shutdown_handler
, m
,
2493 0, "scheduled-shutdown-timeout", true);
2497 r
= event_reset_time(m
->event
, &m
->nologin_timeout_source
,
2499 nologin_timeout_usec(m
->scheduled_shutdown_timeout
), 0,
2500 nologin_timeout_handler
, m
,
2501 0, "nologin-timeout", true);
2508 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2509 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2514 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2515 Manager
*m
= ASSERT_PTR(userdata
);
2516 const HandleActionData
*a
;
2522 cancelled
= handle_action_valid(m
->scheduled_shutdown_action
) && m
->scheduled_shutdown_action
!= HANDLE_IGNORE
;
2524 return sd_bus_reply_method_return(message
, "b", false);
2526 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2527 if (!a
->polkit_action
)
2528 return sd_bus_error_set(error
, SD_BUS_ERROR_AUTH_FAILED
, "Unsupported shutdown type");
2530 r
= bus_verify_polkit_async(
2537 &m
->polkit_registry
,
2542 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2544 if (m
->enable_wall_messages
) {
2545 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2546 const char *tty
= NULL
;
2549 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2551 (void) sd_bus_creds_get_uid(creds
, &uid
);
2552 (void) sd_bus_creds_get_tty(creds
, &tty
);
2555 _cleanup_free_
char *username
= uid_to_name(uid
);
2557 log_struct(LOG_INFO
,
2558 LOG_MESSAGE("System shutdown has been cancelled"),
2559 "ACTION=%s", handle_action_to_string(a
->handle
),
2560 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_CANCELED_STR
,
2561 username
? "OPERATOR=%s" : NULL
, username
);
2563 (void) wall("System shutdown has been cancelled",
2564 username
, tty
, logind_wall_tty_filter
, m
);
2567 reset_scheduled_shutdown(m
);
2569 return sd_bus_reply_method_return(message
, "b", true);
2572 static int method_can_shutdown_or_sleep(
2574 sd_bus_message
*message
,
2575 HandleAction action
,
2576 sd_bus_error
*error
) {
2578 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2579 bool multiple_sessions
, challenge
, blocked
;
2580 const HandleActionData
*a
;
2581 const char *result
= NULL
;
2587 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2589 if (action
== HANDLE_SLEEP
) {
2590 HandleAction selected
;
2592 selected
= handle_action_sleep_select(m
->handle_action_sleep_mask
);
2594 return sd_bus_reply_method_return(message
, "s", "na");
2596 assert_se(a
= handle_action_lookup(selected
));
2598 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2599 SleepSupport support
;
2601 assert_se(a
= handle_action_lookup(action
));
2603 assert(a
->sleep_operation
>= 0);
2604 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2606 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2610 return sd_bus_reply_method_return(message
, "s", support
== SLEEP_DISABLED
? "no" : "na");
2612 assert_se(a
= handle_action_lookup(action
));
2614 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2618 r
= sd_bus_creds_get_euid(creds
, &uid
);
2622 r
= have_multiple_sessions(m
, uid
);
2626 multiple_sessions
= r
> 0;
2627 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2630 _cleanup_free_
char *load_state
= NULL
;
2632 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
2636 if (!streq(load_state
, "loaded")) {
2642 if (multiple_sessions
) {
2643 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2650 result
= "challenge";
2656 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2663 } else if (challenge
) {
2664 if (!result
|| streq(result
, "yes"))
2665 result
= "challenge";
2670 if (!multiple_sessions
&& !blocked
) {
2671 /* If neither inhibit nor multiple sessions
2672 * apply then just check the normal policy */
2674 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action
, NULL
, UID_INVALID
, &challenge
, error
);
2681 result
= "challenge";
2687 return sd_bus_reply_method_return(message
, "s", result
);
2690 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2691 Manager
*m
= userdata
;
2693 return method_can_shutdown_or_sleep(m
, message
, HANDLE_POWEROFF
, error
);
2696 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2697 Manager
*m
= userdata
;
2699 return method_can_shutdown_or_sleep(m
, message
, HANDLE_REBOOT
, error
);
2702 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2703 Manager
*m
= userdata
;
2705 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HALT
, error
);
2708 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2709 Manager
*m
= userdata
;
2711 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND
, error
);
2714 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2715 Manager
*m
= userdata
;
2717 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HIBERNATE
, error
);
2720 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2721 Manager
*m
= userdata
;
2723 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HYBRID_SLEEP
, error
);
2726 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2727 Manager
*m
= userdata
;
2729 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND_THEN_HIBERNATE
, error
);
2732 static int method_can_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2733 Manager
*m
= userdata
;
2735 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SLEEP
, error
);
2738 static int property_get_reboot_parameter(
2741 const char *interface
,
2742 const char *property
,
2743 sd_bus_message
*reply
,
2745 sd_bus_error
*error
) {
2746 _cleanup_free_
char *parameter
= NULL
;
2753 r
= read_reboot_parameter(¶meter
);
2757 return sd_bus_message_append(reply
, "s", parameter
);
2760 static int method_set_reboot_parameter(
2761 sd_bus_message
*message
,
2763 sd_bus_error
*error
) {
2765 Manager
*m
= ASSERT_PTR(userdata
);
2771 r
= sd_bus_message_read(message
, "s", &arg
);
2775 r
= detect_container();
2779 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2780 "Reboot parameter not supported in containers, refusing.");
2782 r
= bus_verify_polkit_async(message
,
2784 "org.freedesktop.login1.set-reboot-parameter",
2788 &m
->polkit_registry
,
2793 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2795 r
= update_reboot_parameter_and_warn(arg
, false);
2799 return sd_bus_reply_method_return(message
, NULL
);
2802 static int method_can_reboot_parameter(
2803 sd_bus_message
*message
,
2805 sd_bus_error
*error
) {
2807 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2812 r
= detect_container();
2815 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2816 return sd_bus_reply_method_return(message
, "s", "na");
2818 return return_test_polkit(
2821 "org.freedesktop.login1.set-reboot-parameter",
2827 static int property_get_reboot_to_firmware_setup(
2830 const char *interface
,
2831 const char *property
,
2832 sd_bus_message
*reply
,
2834 sd_bus_error
*error
) {
2841 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2843 /* EFI case: let's see what is currently configured in the EFI variables */
2844 r
= efi_get_reboot_to_firmware();
2845 if (r
< 0 && r
!= -EOPNOTSUPP
)
2846 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2848 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2850 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2851 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2852 if (errno
!= ENOENT
)
2853 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2860 return sd_bus_message_append(reply
, "b", r
> 0);
2863 static int method_set_reboot_to_firmware_setup(
2864 sd_bus_message
*message
,
2866 sd_bus_error
*error
) {
2868 Manager
*m
= ASSERT_PTR(userdata
);
2874 r
= sd_bus_message_read(message
, "b", &b
);
2878 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2880 /* EFI case: let's see what the firmware supports */
2882 r
= efi_reboot_to_firmware_supported();
2883 if (r
== -EOPNOTSUPP
)
2884 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2890 } else if (r
<= 0) {
2891 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2894 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2896 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2898 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2901 r
= bus_verify_polkit_async(message
,
2903 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2907 &m
->polkit_registry
,
2912 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2915 r
= efi_set_reboot_to_firmware(b
);
2920 r
= touch("/run/systemd/reboot-to-firmware-setup");
2924 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2929 return sd_bus_reply_method_return(message
, NULL
);
2932 static int method_can_reboot_to_firmware_setup(
2933 sd_bus_message
*message
,
2935 sd_bus_error
*error
) {
2937 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2942 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2944 /* EFI case: let's see what the firmware supports */
2946 r
= efi_reboot_to_firmware_supported();
2948 if (r
!= -EOPNOTSUPP
)
2949 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2951 return sd_bus_reply_method_return(message
, "s", "na");
2954 } else if (r
<= 0) {
2955 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2958 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2960 return sd_bus_reply_method_return(message
, "s", "na");
2963 return return_test_polkit(
2966 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2972 static int property_get_reboot_to_boot_loader_menu(
2975 const char *interface
,
2976 const char *property
,
2977 sd_bus_message
*reply
,
2979 sd_bus_error
*error
) {
2981 uint64_t x
= UINT64_MAX
;
2988 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2990 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished:
2992 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2993 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2994 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2997 r
= efi_loader_get_config_timeout_one_shot(&x
);
3000 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
3004 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3006 _cleanup_free_
char *v
= NULL
;
3008 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
3010 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
3013 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
3015 r
= safe_atou64(v
, &x
);
3017 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
3021 return sd_bus_message_append(reply
, "t", x
);
3024 static int method_set_reboot_to_boot_loader_menu(
3025 sd_bus_message
*message
,
3027 sd_bus_error
*error
) {
3029 Manager
*m
= ASSERT_PTR(userdata
);
3036 r
= sd_bus_message_read(message
, "t", &x
);
3040 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3044 /* EFI case: let's see if booting into boot loader menu is supported. */
3046 r
= efi_loader_get_features(&features
);
3048 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3049 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3050 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3054 } else if (r
<= 0) {
3055 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
3058 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3060 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3062 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
3065 r
= bus_verify_polkit_async(message
,
3067 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3071 &m
->polkit_registry
,
3076 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3079 if (x
== UINT64_MAX
)
3080 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), NULL
, 0);
3082 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3083 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
3085 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), buf
);
3090 if (x
== UINT64_MAX
) {
3091 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
3094 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3096 xsprintf(buf
, "%" PRIu64
, x
); /* μs granularity */
3098 r
= write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf
);
3104 return sd_bus_reply_method_return(message
, NULL
);
3107 static int method_can_reboot_to_boot_loader_menu(
3108 sd_bus_message
*message
,
3110 sd_bus_error
*error
) {
3112 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3117 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3119 uint64_t features
= 0;
3121 /* EFI case, let's see if booting into boot loader menu is supported. */
3123 r
= efi_loader_get_features(&features
);
3125 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3126 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3127 return sd_bus_reply_method_return(message
, "s", "na");
3129 } else if (r
<= 0) {
3130 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
3133 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3135 return sd_bus_reply_method_return(message
, "s", "na");
3138 return return_test_polkit(
3141 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3147 static int property_get_reboot_to_boot_loader_entry(
3150 const char *interface
,
3151 const char *property
,
3152 sd_bus_message
*reply
,
3154 sd_bus_error
*error
) {
3156 _cleanup_free_
char *v
= NULL
;
3157 Manager
*m
= ASSERT_PTR(userdata
);
3158 const char *x
= NULL
;
3164 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3166 /* EFI case: let's read the LoaderEntryOneShot variable */
3168 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
3171 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
3173 x
= m
->efi_loader_entry_one_shot
;
3176 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3179 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
3181 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
3184 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
3185 } else if (!efi_loader_entry_name_valid(v
))
3186 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
3191 return sd_bus_message_append(reply
, "s", x
);
3194 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
3195 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3201 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3202 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
3205 r
= manager_read_efi_boot_loader_entries(m
);
3207 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3209 return !!boot_config_find_entry(&config
, id
);
3212 static int method_set_reboot_to_boot_loader_entry(
3213 sd_bus_message
*message
,
3215 sd_bus_error
*error
) {
3217 Manager
*m
= ASSERT_PTR(userdata
);
3224 r
= sd_bus_message_read(message
, "s", &v
);
3230 else if (efi_loader_entry_name_valid(v
)) {
3231 r
= boot_loader_entry_exists(m
, v
);
3235 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3237 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3239 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3243 /* EFI case: let's see if booting into boot loader entry is supported. */
3245 r
= efi_loader_get_features(&features
);
3247 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3248 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3249 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3253 } else if (r
<= 0) {
3254 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3257 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3259 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3261 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3264 r
= bus_verify_polkit_async(message
,
3266 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3270 &m
->polkit_registry
,
3275 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3280 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), NULL
, 0);
3282 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), v
);
3287 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3290 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3296 return sd_bus_reply_method_return(message
, NULL
);
3299 static int method_can_reboot_to_boot_loader_entry(
3300 sd_bus_message
*message
,
3302 sd_bus_error
*error
) {
3304 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3309 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3311 uint64_t features
= 0;
3313 /* EFI case, let's see if booting into boot loader entry is supported. */
3315 r
= efi_loader_get_features(&features
);
3317 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3318 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3319 return sd_bus_reply_method_return(message
, "s", "na");
3321 } else if (r
<= 0) {
3322 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3325 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3327 return sd_bus_reply_method_return(message
, "s", "na");
3330 return return_test_polkit(
3333 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3339 static int property_get_boot_loader_entries(
3342 const char *interface
,
3343 const char *property
,
3344 sd_bus_message
*reply
,
3346 sd_bus_error
*error
) {
3348 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3349 Manager
*m
= ASSERT_PTR(userdata
);
3356 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3357 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3360 r
= manager_read_efi_boot_loader_entries(m
);
3362 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3364 r
= sd_bus_message_open_container(reply
, 'a', "s");
3368 for (i
= 0; i
< config
.n_entries
; i
++) {
3369 BootEntry
*e
= config
.entries
+ i
;
3371 r
= sd_bus_message_append(reply
, "s", e
->id
);
3376 return sd_bus_message_close_container(reply
);
3379 static int method_set_wall_message(
3380 sd_bus_message
*message
,
3382 sd_bus_error
*error
) {
3385 Manager
*m
= ASSERT_PTR(userdata
);
3387 int enable_wall_messages
;
3391 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3395 if (strlen(wall_message
) > WALL_MESSAGE_MAX
)
3396 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3397 "Wall message too long, maximum permitted length is %u characters.",
3400 /* Short-circuit the operation if the desired state is already in place, to
3401 * avoid an unnecessary polkit permission check. */
3402 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3403 m
->enable_wall_messages
== enable_wall_messages
)
3406 r
= bus_verify_polkit_async(message
,
3408 "org.freedesktop.login1.set-wall-message",
3412 &m
->polkit_registry
,
3417 return 1; /* Will call us back */
3419 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3423 m
->enable_wall_messages
= enable_wall_messages
;
3426 return sd_bus_reply_method_return(message
, NULL
);
3429 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3430 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3431 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
3432 const char *who
, *why
, *what
, *mode
;
3433 _cleanup_free_
char *id
= NULL
;
3434 _cleanup_close_
int fifo_fd
= -EBADF
;
3435 Manager
*m
= ASSERT_PTR(userdata
);
3444 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3448 w
= inhibit_what_from_string(what
);
3450 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3451 "Invalid what specification %s", what
);
3453 mm
= inhibit_mode_from_string(mode
);
3455 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3456 "Invalid mode specification %s", mode
);
3458 /* Delay is only supported for shutdown/sleep */
3459 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3460 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3461 "Delay inhibitors only supported for shutdown and sleep");
3463 /* Don't allow taking delay locks while we are already
3464 * executing the operation. We shouldn't create the impression
3465 * that the lock was successful if the machine is about to go
3466 * down/suspend any moment. */
3467 if (m
->delayed_action
&& m
->delayed_action
->inhibit_what
& w
)
3468 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3469 "The operation inhibition has been requested for is already running");
3471 r
= bus_verify_polkit_async(
3474 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3475 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3476 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3477 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3478 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3479 w
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3480 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3481 "org.freedesktop.login1.inhibit-handle-lid-switch",
3485 &m
->polkit_registry
,
3490 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3492 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3496 r
= sd_bus_creds_get_euid(creds
, &uid
);
3500 r
= sd_bus_creds_get_pid(creds
, &pid
);
3504 r
= pidref_set_pid(&pidref
, pid
);
3506 return sd_bus_error_set_errnof(error
, r
, "Failed pin source process "PID_FMT
": %m", pid
);
3508 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3509 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3510 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3516 if (asprintf(&id
, "%" PRIu64
, ++m
->inhibit_counter
) < 0)
3519 } while (hashmap_get(m
->inhibitors
, id
));
3521 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3522 r
= manager_add_inhibitor(m
, id
, &i
);
3528 i
->pid
= TAKE_PIDREF(pidref
);
3530 i
->why
= strdup(why
);
3531 i
->who
= strdup(who
);
3533 if (!i
->why
|| !i
->who
)
3536 fifo_fd
= inhibitor_create_fifo(i
);
3540 r
= inhibitor_start(i
);
3545 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3548 static const sd_bus_vtable manager_vtable
[] = {
3549 SD_BUS_VTABLE_START(0),
3551 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool
, bus_property_set_bool
, offsetof(Manager
, enable_wall_messages
), 0),
3552 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3554 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3555 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3556 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3557 SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3558 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3559 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3560 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3561 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3562 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3563 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3564 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3565 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3566 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3567 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3568 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3569 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3570 SD_BUS_PROPERTY("SleepOperation", "as", property_get_sleep_operations
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3571 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3572 SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3573 SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3574 SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3575 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3576 SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3577 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3578 SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3579 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3580 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3581 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3582 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3583 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3584 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3585 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3586 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3587 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3588 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3589 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3590 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3591 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3592 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3593 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3594 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3595 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3596 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3597 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3598 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3599 SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL
, offsetof(Manager
, stop_idle_session_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3601 SD_BUS_METHOD_WITH_ARGS("GetSession",
3602 SD_BUS_ARGS("s", session_id
),
3603 SD_BUS_RESULT("o", object_path
),
3605 SD_BUS_VTABLE_UNPRIVILEGED
),
3606 SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
3607 SD_BUS_ARGS("u", pid
),
3608 SD_BUS_RESULT("o", object_path
),
3609 method_get_session_by_pid
,
3610 SD_BUS_VTABLE_UNPRIVILEGED
),
3611 SD_BUS_METHOD_WITH_ARGS("GetUser",
3612 SD_BUS_ARGS("u", uid
),
3613 SD_BUS_RESULT("o", object_path
),
3615 SD_BUS_VTABLE_UNPRIVILEGED
),
3616 SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
3617 SD_BUS_ARGS("u", pid
),
3618 SD_BUS_RESULT("o", object_path
),
3619 method_get_user_by_pid
,
3620 SD_BUS_VTABLE_UNPRIVILEGED
),
3621 SD_BUS_METHOD_WITH_ARGS("GetSeat",
3622 SD_BUS_ARGS("s", seat_id
),
3623 SD_BUS_RESULT("o", object_path
),
3625 SD_BUS_VTABLE_UNPRIVILEGED
),
3626 SD_BUS_METHOD_WITH_ARGS("ListSessions",
3628 SD_BUS_RESULT("a(susso)", sessions
),
3629 method_list_sessions
,
3630 SD_BUS_VTABLE_UNPRIVILEGED
),
3631 SD_BUS_METHOD_WITH_ARGS("ListUsers",
3633 SD_BUS_RESULT("a(uso)", users
),
3635 SD_BUS_VTABLE_UNPRIVILEGED
),
3636 SD_BUS_METHOD_WITH_ARGS("ListSeats",
3638 SD_BUS_RESULT("a(so)", seats
),
3640 SD_BUS_VTABLE_UNPRIVILEGED
),
3641 SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
3643 SD_BUS_RESULT("a(ssssuu)", inhibitors
),
3644 method_list_inhibitors
,
3645 SD_BUS_VTABLE_UNPRIVILEGED
),
3646 SD_BUS_METHOD_WITH_ARGS("CreateSession",
3647 SD_BUS_ARGS("u", uid
,
3660 "a(sv)", properties
),
3661 SD_BUS_RESULT("s", session_id
,
3669 method_create_session
,
3671 SD_BUS_METHOD_WITH_ARGS("CreateSessionWithPIDFD",
3672 SD_BUS_ARGS("u", uid
,
3686 "a(sv)", properties
),
3687 SD_BUS_RESULT("s", session_id
,
3695 method_create_session_pidfd
,
3697 SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
3698 SD_BUS_ARGS("s", session_id
),
3700 method_release_session
,
3702 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
3703 SD_BUS_ARGS("s", session_id
),
3705 method_activate_session
,
3706 SD_BUS_VTABLE_UNPRIVILEGED
),
3707 SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
3708 SD_BUS_ARGS("s", session_id
, "s", seat_id
),
3710 method_activate_session_on_seat
,
3711 SD_BUS_VTABLE_UNPRIVILEGED
),
3712 SD_BUS_METHOD_WITH_ARGS("LockSession",
3713 SD_BUS_ARGS("s", session_id
),
3715 method_lock_session
,
3716 SD_BUS_VTABLE_UNPRIVILEGED
),
3717 SD_BUS_METHOD_WITH_ARGS("UnlockSession",
3718 SD_BUS_ARGS("s", session_id
),
3720 method_lock_session
,
3721 SD_BUS_VTABLE_UNPRIVILEGED
),
3722 SD_BUS_METHOD("LockSessions",
3725 method_lock_sessions
,
3726 SD_BUS_VTABLE_UNPRIVILEGED
),
3727 SD_BUS_METHOD("UnlockSessions",
3730 method_lock_sessions
,
3731 SD_BUS_VTABLE_UNPRIVILEGED
),
3732 SD_BUS_METHOD_WITH_ARGS("KillSession",
3733 SD_BUS_ARGS("s", session_id
, "s", who
, "i", signal_number
),
3735 method_kill_session
,
3736 SD_BUS_VTABLE_UNPRIVILEGED
),
3737 SD_BUS_METHOD_WITH_ARGS("KillUser",
3738 SD_BUS_ARGS("u", uid
, "i", signal_number
),
3741 SD_BUS_VTABLE_UNPRIVILEGED
),
3742 SD_BUS_METHOD_WITH_ARGS("TerminateSession",
3743 SD_BUS_ARGS("s", session_id
),
3745 method_terminate_session
,
3746 SD_BUS_VTABLE_UNPRIVILEGED
),
3747 SD_BUS_METHOD_WITH_ARGS("TerminateUser",
3748 SD_BUS_ARGS("u", uid
),
3750 method_terminate_user
,
3751 SD_BUS_VTABLE_UNPRIVILEGED
),
3752 SD_BUS_METHOD_WITH_ARGS("TerminateSeat",
3753 SD_BUS_ARGS("s", seat_id
),
3755 method_terminate_seat
,
3756 SD_BUS_VTABLE_UNPRIVILEGED
),
3757 SD_BUS_METHOD_WITH_ARGS("SetUserLinger",
3758 SD_BUS_ARGS("u", uid
, "b", enable
, "b", interactive
),
3760 method_set_user_linger
,
3761 SD_BUS_VTABLE_UNPRIVILEGED
),
3762 SD_BUS_METHOD_WITH_ARGS("AttachDevice",
3763 SD_BUS_ARGS("s", seat_id
, "s", sysfs_path
, "b", interactive
),
3765 method_attach_device
,
3766 SD_BUS_VTABLE_UNPRIVILEGED
),
3767 SD_BUS_METHOD_WITH_ARGS("FlushDevices",
3768 SD_BUS_ARGS("b", interactive
),
3770 method_flush_devices
,
3771 SD_BUS_VTABLE_UNPRIVILEGED
),
3772 SD_BUS_METHOD_WITH_ARGS("PowerOff",
3773 SD_BUS_ARGS("b", interactive
),
3776 SD_BUS_VTABLE_UNPRIVILEGED
),
3777 SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags",
3778 SD_BUS_ARGS("t", flags
),
3781 SD_BUS_VTABLE_UNPRIVILEGED
),
3782 SD_BUS_METHOD_WITH_ARGS("Reboot",
3783 SD_BUS_ARGS("b", interactive
),
3786 SD_BUS_VTABLE_UNPRIVILEGED
),
3787 SD_BUS_METHOD_WITH_ARGS("RebootWithFlags",
3788 SD_BUS_ARGS("t", flags
),
3791 SD_BUS_VTABLE_UNPRIVILEGED
),
3792 SD_BUS_METHOD_WITH_ARGS("Halt",
3793 SD_BUS_ARGS("b", interactive
),
3796 SD_BUS_VTABLE_UNPRIVILEGED
),
3797 SD_BUS_METHOD_WITH_ARGS("HaltWithFlags",
3798 SD_BUS_ARGS("t", flags
),
3801 SD_BUS_VTABLE_UNPRIVILEGED
),
3802 SD_BUS_METHOD_WITH_ARGS("Suspend",
3803 SD_BUS_ARGS("b", interactive
),
3806 SD_BUS_VTABLE_UNPRIVILEGED
),
3807 SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags",
3808 SD_BUS_ARGS("t", flags
),
3811 SD_BUS_VTABLE_UNPRIVILEGED
),
3812 SD_BUS_METHOD_WITH_ARGS("Hibernate",
3813 SD_BUS_ARGS("b", interactive
),
3816 SD_BUS_VTABLE_UNPRIVILEGED
),
3817 SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags",
3818 SD_BUS_ARGS("t", flags
),
3821 SD_BUS_VTABLE_UNPRIVILEGED
),
3822 SD_BUS_METHOD_WITH_ARGS("HybridSleep",
3823 SD_BUS_ARGS("b", interactive
),
3825 method_hybrid_sleep
,
3826 SD_BUS_VTABLE_UNPRIVILEGED
),
3827 SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags",
3828 SD_BUS_ARGS("t", flags
),
3830 method_hybrid_sleep
,
3831 SD_BUS_VTABLE_UNPRIVILEGED
),
3832 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate",
3833 SD_BUS_ARGS("b", interactive
),
3835 method_suspend_then_hibernate
,
3836 SD_BUS_VTABLE_UNPRIVILEGED
),
3837 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags",
3838 SD_BUS_ARGS("t", flags
),
3840 method_suspend_then_hibernate
,
3841 SD_BUS_VTABLE_UNPRIVILEGED
),
3842 SD_BUS_METHOD_WITH_ARGS("Sleep",
3843 SD_BUS_ARGS("t", flags
),
3846 SD_BUS_VTABLE_UNPRIVILEGED
),
3847 SD_BUS_METHOD_WITH_ARGS("CanPowerOff",
3849 SD_BUS_RESULT("s", result
),
3850 method_can_poweroff
,
3851 SD_BUS_VTABLE_UNPRIVILEGED
),
3852 SD_BUS_METHOD_WITH_ARGS("CanReboot",
3854 SD_BUS_RESULT("s", result
),
3856 SD_BUS_VTABLE_UNPRIVILEGED
),
3857 SD_BUS_METHOD_WITH_ARGS("CanHalt",
3859 SD_BUS_RESULT("s", result
),
3861 SD_BUS_VTABLE_UNPRIVILEGED
),
3862 SD_BUS_METHOD_WITH_ARGS("CanSuspend",
3864 SD_BUS_RESULT("s", result
),
3866 SD_BUS_VTABLE_UNPRIVILEGED
),
3867 SD_BUS_METHOD_WITH_ARGS("CanHibernate",
3869 SD_BUS_RESULT("s", result
),
3870 method_can_hibernate
,
3871 SD_BUS_VTABLE_UNPRIVILEGED
),
3872 SD_BUS_METHOD_WITH_ARGS("CanHybridSleep",
3874 SD_BUS_RESULT("s", result
),
3875 method_can_hybrid_sleep
,
3876 SD_BUS_VTABLE_UNPRIVILEGED
),
3877 SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate",
3879 SD_BUS_RESULT("s", result
),
3880 method_can_suspend_then_hibernate
,
3881 SD_BUS_VTABLE_UNPRIVILEGED
),
3882 SD_BUS_METHOD_WITH_ARGS("CanSleep",
3884 SD_BUS_RESULT("s", result
),
3886 SD_BUS_VTABLE_UNPRIVILEGED
),
3887 SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown",
3888 SD_BUS_ARGS("s", type
, "t", usec
),
3890 method_schedule_shutdown
,
3891 SD_BUS_VTABLE_UNPRIVILEGED
),
3892 SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown",
3894 SD_BUS_RESULT("b", cancelled
),
3895 method_cancel_scheduled_shutdown
,
3896 SD_BUS_VTABLE_UNPRIVILEGED
),
3897 SD_BUS_METHOD_WITH_ARGS("Inhibit",
3898 SD_BUS_ARGS("s", what
, "s", who
, "s", why
, "s", mode
),
3899 SD_BUS_RESULT("h", pipe_fd
),
3901 SD_BUS_VTABLE_UNPRIVILEGED
),
3902 SD_BUS_METHOD_WITH_ARGS("CanRebootParameter",
3904 SD_BUS_RESULT("s", result
),
3905 method_can_reboot_parameter
,
3906 SD_BUS_VTABLE_UNPRIVILEGED
),
3907 SD_BUS_METHOD_WITH_ARGS("SetRebootParameter",
3908 SD_BUS_ARGS("s", parameter
),
3910 method_set_reboot_parameter
,
3911 SD_BUS_VTABLE_UNPRIVILEGED
),
3912 SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup",
3914 SD_BUS_RESULT("s", result
),
3915 method_can_reboot_to_firmware_setup
,
3916 SD_BUS_VTABLE_UNPRIVILEGED
),
3917 SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup",
3918 SD_BUS_ARGS("b", enable
),
3920 method_set_reboot_to_firmware_setup
,
3921 SD_BUS_VTABLE_UNPRIVILEGED
),
3922 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu",
3924 SD_BUS_RESULT("s", result
),
3925 method_can_reboot_to_boot_loader_menu
,
3926 SD_BUS_VTABLE_UNPRIVILEGED
),
3927 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu",
3928 SD_BUS_ARGS("t", timeout
),
3930 method_set_reboot_to_boot_loader_menu
,
3931 SD_BUS_VTABLE_UNPRIVILEGED
),
3932 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry",
3934 SD_BUS_RESULT("s", result
),
3935 method_can_reboot_to_boot_loader_entry
,
3936 SD_BUS_VTABLE_UNPRIVILEGED
),
3937 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry",
3938 SD_BUS_ARGS("s", boot_loader_entry
),
3940 method_set_reboot_to_boot_loader_entry
,
3941 SD_BUS_VTABLE_UNPRIVILEGED
),
3942 SD_BUS_METHOD_WITH_ARGS("SetWallMessage",
3943 SD_BUS_ARGS("s", wall_message
, "b", enable
),
3945 method_set_wall_message
,
3946 SD_BUS_VTABLE_UNPRIVILEGED
),
3948 SD_BUS_SIGNAL_WITH_ARGS("SessionNew",
3949 SD_BUS_ARGS("s", session_id
, "o", object_path
),
3951 SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved",
3952 SD_BUS_ARGS("s", session_id
, "o", object_path
),
3954 SD_BUS_SIGNAL_WITH_ARGS("UserNew",
3955 SD_BUS_ARGS("u", uid
, "o", object_path
),
3957 SD_BUS_SIGNAL_WITH_ARGS("UserRemoved",
3958 SD_BUS_ARGS("u", uid
, "o", object_path
),
3960 SD_BUS_SIGNAL_WITH_ARGS("SeatNew",
3961 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
3963 SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved",
3964 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
3966 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown",
3967 SD_BUS_ARGS("b", start
),
3969 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdownWithMetadata",
3970 SD_BUS_ARGS("b", start
, "a{sv}", metadata
),
3972 SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep",
3973 SD_BUS_ARGS("b", start
),
3979 const BusObjectImplementation manager_object
= {
3980 "/org/freedesktop/login1",
3981 "org.freedesktop.login1.Manager",
3982 .vtables
= BUS_VTABLES(manager_vtable
),
3983 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
3988 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
3995 if (result
&& !streq(result
, "done")) {
3996 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3998 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3999 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
4000 return session_send_create_reply(s
, &e
);
4003 return session_send_create_reply(s
, NULL
);
4006 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4007 const char *path
, *result
, *unit
;
4008 Manager
*m
= ASSERT_PTR(userdata
);
4016 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
4018 bus_log_parse_error(r
);
4022 if (m
->action_job
&& streq(m
->action_job
, path
)) {
4023 assert(m
->delayed_action
);
4024 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->delayed_action
->inhibit_what
));
4026 /* Tell people that they now may take a lock again */
4027 (void) send_prepare_for(m
, m
->delayed_action
, false);
4029 m
->action_job
= mfree(m
->action_job
);
4030 m
->delayed_action
= NULL
;
4034 session
= hashmap_get(m
->session_units
, unit
);
4036 if (streq_ptr(path
, session
->scope_job
)) {
4037 session
->scope_job
= mfree(session
->scope_job
);
4038 (void) session_jobs_reply(session
, id
, unit
, result
);
4040 session_save(session
);
4041 user_save(session
->user
);
4044 session_add_to_gc_queue(session
);
4047 user
= hashmap_get(m
->user_units
, unit
);
4049 if (streq_ptr(path
, user
->service_job
)) {
4050 user
->service_job
= mfree(user
->service_job
);
4052 LIST_FOREACH(sessions_by_user
, s
, user
->sessions
)
4053 (void) session_jobs_reply(s
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
4058 user_add_to_gc_queue(user
);
4064 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4065 const char *path
, *unit
;
4066 Manager
*m
= ASSERT_PTR(userdata
);
4073 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
4075 bus_log_parse_error(r
);
4079 session
= hashmap_get(m
->session_units
, unit
);
4081 session_add_to_gc_queue(session
);
4083 user
= hashmap_get(m
->user_units
, unit
);
4085 user_add_to_gc_queue(user
);
4090 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4091 _cleanup_free_
char *unit
= NULL
;
4092 Manager
*m
= ASSERT_PTR(userdata
);
4100 path
= sd_bus_message_get_path(message
);
4104 r
= unit_name_from_dbus_path(path
, &unit
);
4105 if (r
== -EINVAL
) /* not a unit */
4112 session
= hashmap_get(m
->session_units
, unit
);
4114 session_add_to_gc_queue(session
);
4116 user
= hashmap_get(m
->user_units
, unit
);
4118 user_add_to_gc_queue(user
);
4123 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4124 Manager
*m
= ASSERT_PTR(userdata
);
4130 r
= sd_bus_message_read(message
, "b", &b
);
4132 bus_log_parse_error(r
);
4139 /* systemd finished reloading, let's recheck all our sessions */
4140 log_debug("System manager has been reloaded, rechecking sessions...");
4142 HASHMAP_FOREACH(session
, m
->sessions
)
4143 session_add_to_gc_queue(session
);
4148 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
4153 l
= strv_from_stdarg_alloca(property
);
4155 return sd_bus_emit_properties_changed_strv(
4157 "/org/freedesktop/login1",
4158 "org.freedesktop.login1.Manager",
4162 static int strdup_job(sd_bus_message
*reply
, char **job
) {
4167 r
= sd_bus_message_read(reply
, "o", &j
);
4179 int manager_start_scope(
4182 const PidRef
*pidref
,
4184 const char *description
,
4187 const char *requires_mounts_for
,
4188 sd_bus_message
*more_properties
,
4189 sd_bus_error
*error
,
4192 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
4197 assert(pidref_is_set(pidref
));
4200 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
4204 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
4208 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
4212 if (!isempty(slice
)) {
4213 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
4218 if (!isempty(description
)) {
4219 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4224 STRV_FOREACH(i
, wants
) {
4225 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
4230 STRV_FOREACH(i
, after
) {
4231 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4236 if (!empty_or_root(requires_mounts_for
)) {
4237 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4242 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4244 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4248 r
= bus_append_scope_pidref(m
, pidref
);
4252 /* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of
4254 r
= sd_bus_message_append(m
, "(sv)", "OOMPolicy", "s", "continue");
4258 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4259 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_MAX
);
4261 return bus_log_create_error(r
);
4263 if (more_properties
) {
4264 /* If TasksMax also appears here, it will overwrite the default value set above */
4265 r
= sd_bus_message_copy(m
, more_properties
, true);
4270 r
= sd_bus_message_close_container(m
);
4274 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4278 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4282 return strdup_job(reply
, job
);
4285 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4286 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4293 r
= bus_call_method(
4299 "ss", unit
, "replace");
4303 return strdup_job(reply
, job
);
4306 int manager_stop_unit(Manager
*manager
, const char *unit
, const char *job_mode
, sd_bus_error
*error
, char **ret_job
) {
4307 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4314 r
= bus_call_method(
4320 "ss", unit
, job_mode
?: "fail");
4322 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
4323 BUS_ERROR_LOAD_FAILED
)) {
4326 sd_bus_error_free(error
);
4333 return strdup_job(reply
, ret_job
);
4336 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4337 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4338 _cleanup_free_
char *path
= NULL
;
4344 path
= unit_dbus_path_from_name(scope
);
4348 r
= sd_bus_call_method(
4350 "org.freedesktop.systemd1",
4352 "org.freedesktop.systemd1.Scope",
4358 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4359 BUS_ERROR_LOAD_FAILED
,
4360 BUS_ERROR_SCOPE_NOT_RUNNING
))
4363 sd_bus_error_move(ret_error
, &error
);
4370 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4374 return bus_call_method(
4380 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4383 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4384 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4385 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4386 _cleanup_free_
char *path
= NULL
;
4393 path
= unit_dbus_path_from_name(unit
);
4397 r
= sd_bus_get_property(
4399 "org.freedesktop.systemd1",
4401 "org.freedesktop.systemd1.Unit",
4407 /* systemd might have dropped off momentarily, let's
4408 * not make this an error */
4409 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4410 SD_BUS_ERROR_DISCONNECTED
))
4413 /* If the unit is already unloaded then it's not
4415 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4416 BUS_ERROR_LOAD_FAILED
))
4419 sd_bus_error_move(ret_error
, &error
);
4423 r
= sd_bus_message_read(reply
, "s", &state
);
4427 return !STR_IN_SET(state
, "inactive", "failed");
4430 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4431 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4432 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4438 r
= sd_bus_get_property(
4440 "org.freedesktop.systemd1",
4442 "org.freedesktop.systemd1.Job",
4448 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4449 SD_BUS_ERROR_DISCONNECTED
))
4452 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4455 sd_bus_error_move(ret_error
, &error
);
4459 /* We don't actually care about the state really. The fact
4460 * that we could read the job state is enough for us */