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"
61 /* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1))
62 * character limit for the wall message.
63 * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72
64 * There is no real technical need for that but doesn't make sense
65 * to store arbitrary amounts either. As we are not stingy here, we
68 #define WALL_MESSAGE_MAX 4096U
70 #define SHUTDOWN_SCHEDULE_FILE "/run/systemd/shutdown/scheduled"
72 static int update_schedule_file(Manager
*m
);
73 static void reset_scheduled_shutdown(Manager
*m
);
74 static int manager_setup_shutdown_timers(Manager
* m
);
76 static int get_sender_session(
78 sd_bus_message
*message
,
83 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
84 Session
*session
= NULL
;
88 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
89 * and returns that. If not and 'consult_display' is true, this returns the display session of the
90 * owning user of the caller. */
92 r
= sd_bus_query_sender_creds(message
,
93 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
94 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
98 r
= sd_bus_creds_get_session(creds
, &name
);
103 if (consult_display
) {
106 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
113 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
115 session
= user
->display
;
119 session
= hashmap_get(m
->sessions
, name
);
122 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
124 "Caller does not belong to any known session and doesn't own any suitable session." :
125 "Caller does not belong to any known session.");
131 int manager_get_session_from_creds(
133 sd_bus_message
*message
,
143 if (SEAT_IS_SELF(name
)) /* the caller's own session */
144 return get_sender_session(m
, message
, false, error
, ret
);
145 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
146 return get_sender_session(m
, message
, true, error
, ret
);
148 session
= hashmap_get(m
->sessions
, name
);
150 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
156 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
157 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
162 /* Note that we get the owner UID of the session, not the actual client UID here! */
163 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
167 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
174 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
177 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
178 "Caller does not belong to any logged in or lingering user");
184 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
190 if (!uid_is_valid(uid
))
191 return get_sender_user(m
, message
, error
, ret
);
193 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
195 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
196 "User ID "UID_FMT
" is not logged in or lingering", uid
);
202 int manager_get_seat_from_creds(
204 sd_bus_message
*message
,
215 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
218 /* Use these special seat names as session names */
219 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
223 seat
= session
->seat
;
225 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
227 seat
= hashmap_get(m
->seats
, name
);
229 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
236 static int return_test_polkit(
237 sd_bus_message
*message
,
240 const char **details
,
248 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
255 result
= "challenge";
259 return sd_bus_reply_method_return(message
, "s", result
);
262 static int property_get_idle_hint(
265 const char *interface
,
266 const char *property
,
267 sd_bus_message
*reply
,
269 sd_bus_error
*error
) {
271 Manager
*m
= ASSERT_PTR(userdata
);
276 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
279 static int property_get_idle_since_hint(
282 const char *interface
,
283 const char *property
,
284 sd_bus_message
*reply
,
286 sd_bus_error
*error
) {
288 Manager
*m
= ASSERT_PTR(userdata
);
289 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
294 manager_get_idle_hint(m
, &t
);
296 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
299 static int property_get_inhibited(
302 const char *interface
,
303 const char *property
,
304 sd_bus_message
*reply
,
306 sd_bus_error
*error
) {
308 Manager
*m
= ASSERT_PTR(userdata
);
314 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
316 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
319 static int property_get_preparing(
322 const char *interface
,
323 const char *property
,
324 sd_bus_message
*reply
,
326 sd_bus_error
*error
) {
328 Manager
*m
= ASSERT_PTR(userdata
);
334 if (m
->delayed_action
) {
335 if (streq(property
, "PreparingForShutdown"))
336 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SHUTDOWN
;
338 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SLEEP
;
341 return sd_bus_message_append(reply
, "b", b
);
344 static int property_get_scheduled_shutdown(
347 const char *interface
,
348 const char *property
,
349 sd_bus_message
*reply
,
351 sd_bus_error
*error
) {
353 Manager
*m
= ASSERT_PTR(userdata
);
359 r
= sd_bus_message_open_container(reply
, 'r', "st");
363 r
= sd_bus_message_append(reply
, "st",
364 m
->scheduled_shutdown_action
? handle_action_to_string(m
->scheduled_shutdown_action
->handle
) : NULL
,
365 m
->scheduled_shutdown_timeout
);
369 return sd_bus_message_close_container(reply
);
372 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
373 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
374 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
375 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
376 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
377 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
379 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
380 _cleanup_free_
char *p
= NULL
;
381 Manager
*m
= ASSERT_PTR(userdata
);
388 r
= sd_bus_message_read(message
, "s", &name
);
392 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
396 p
= session_bus_path(session
);
400 return sd_bus_reply_method_return(message
, "o", p
);
403 /* Get login session of a process. This is not what you are looking for these days,
404 * as apps may instead belong to a user service unit. This includes terminal
405 * emulators and hence command-line apps. */
406 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
407 _cleanup_free_
char *p
= NULL
;
408 Session
*session
= NULL
;
409 Manager
*m
= ASSERT_PTR(userdata
);
415 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
417 r
= sd_bus_message_read(message
, "u", &pid
);
424 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
428 r
= manager_get_session_by_pid(m
, pid
, &session
);
433 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
434 "PID "PID_FMT
" does not belong to any known session", pid
);
437 p
= session_bus_path(session
);
441 return sd_bus_reply_method_return(message
, "o", p
);
444 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
445 _cleanup_free_
char *p
= NULL
;
446 Manager
*m
= ASSERT_PTR(userdata
);
453 r
= sd_bus_message_read(message
, "u", &uid
);
457 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
461 p
= user_bus_path(user
);
465 return sd_bus_reply_method_return(message
, "o", p
);
468 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
469 _cleanup_free_
char *p
= NULL
;
470 Manager
*m
= ASSERT_PTR(userdata
);
477 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
479 r
= sd_bus_message_read(message
, "u", &pid
);
486 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
490 r
= manager_get_user_by_pid(m
, pid
, &user
);
494 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
495 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
499 p
= user_bus_path(user
);
503 return sd_bus_reply_method_return(message
, "o", p
);
506 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
507 _cleanup_free_
char *p
= NULL
;
508 Manager
*m
= ASSERT_PTR(userdata
);
515 r
= sd_bus_message_read(message
, "s", &name
);
519 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
523 p
= seat_bus_path(seat
);
527 return sd_bus_reply_method_return(message
, "o", p
);
530 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
531 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
532 Manager
*m
= ASSERT_PTR(userdata
);
538 r
= sd_bus_message_new_method_return(message
, &reply
);
542 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
546 HASHMAP_FOREACH(session
, m
->sessions
) {
547 _cleanup_free_
char *p
= NULL
;
549 p
= session_bus_path(session
);
553 r
= sd_bus_message_append(reply
, "(susso)",
555 (uint32_t) session
->user
->user_record
->uid
,
556 session
->user
->user_record
->user_name
,
557 session
->seat
? session
->seat
->id
: "",
563 r
= sd_bus_message_close_container(reply
);
567 return sd_bus_send(NULL
, reply
, NULL
);
570 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
571 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
572 Manager
*m
= ASSERT_PTR(userdata
);
578 r
= sd_bus_message_new_method_return(message
, &reply
);
582 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
586 HASHMAP_FOREACH(user
, m
->users
) {
587 _cleanup_free_
char *p
= NULL
;
589 p
= user_bus_path(user
);
593 r
= sd_bus_message_append(reply
, "(uso)",
594 (uint32_t) user
->user_record
->uid
,
595 user
->user_record
->user_name
,
601 r
= sd_bus_message_close_container(reply
);
605 return sd_bus_send(NULL
, reply
, NULL
);
608 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
609 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
610 Manager
*m
= ASSERT_PTR(userdata
);
616 r
= sd_bus_message_new_method_return(message
, &reply
);
620 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
624 HASHMAP_FOREACH(seat
, m
->seats
) {
625 _cleanup_free_
char *p
= NULL
;
627 p
= seat_bus_path(seat
);
631 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
636 r
= sd_bus_message_close_container(reply
);
640 return sd_bus_send(NULL
, reply
, NULL
);
643 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
644 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
645 Manager
*m
= ASSERT_PTR(userdata
);
646 Inhibitor
*inhibitor
;
651 r
= sd_bus_message_new_method_return(message
, &reply
);
655 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
659 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
) {
661 r
= sd_bus_message_append(reply
, "(ssssuu)",
662 strempty(inhibit_what_to_string(inhibitor
->what
)),
663 strempty(inhibitor
->who
),
664 strempty(inhibitor
->why
),
665 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
666 (uint32_t) inhibitor
->uid
,
667 (uint32_t) inhibitor
->pid
);
672 r
= sd_bus_message_close_container(reply
);
676 return sd_bus_send(NULL
, reply
, NULL
);
679 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
680 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
681 _cleanup_free_
char *id
= NULL
;
682 Session
*session
= NULL
;
683 uint32_t audit_id
= 0;
684 Manager
*m
= ASSERT_PTR(userdata
);
697 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
698 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
700 r
= sd_bus_message_read(message
, "uusssssussbss",
701 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
702 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
706 if (!uid_is_valid(uid
))
707 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
708 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
709 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
712 t
= _SESSION_TYPE_INVALID
;
714 t
= session_type_from_string(type
);
716 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
717 "Invalid session type %s", type
);
721 c
= _SESSION_CLASS_INVALID
;
723 c
= session_class_from_string(class);
725 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
726 "Invalid session class %s", class);
729 if (isempty(desktop
))
732 if (!string_is_safe(desktop
))
733 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
734 "Invalid desktop string %s", desktop
);
740 seat
= hashmap_get(m
->seats
, cseat
);
742 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
743 "No seat '%s' known", cseat
);
746 if (tty_is_vc(tty
)) {
751 else if (seat
!= m
->seat0
)
752 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
753 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
755 v
= vtnr_from_tty(tty
);
757 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
758 "Cannot determine VT number from virtual console TTY %s", tty
);
762 else if (vtnr
!= (uint32_t) v
)
763 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
764 "Specified TTY and VT number do not match");
766 } else if (tty_is_console(tty
)) {
770 else if (seat
!= m
->seat0
)
771 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
772 "Console TTY specified but seat is not seat0");
775 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
776 "Console TTY specified but VT number is not 0");
780 if (seat_has_vts(seat
)) {
781 if (vtnr
<= 0 || vtnr
> 63)
782 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
783 "VT number out of range");
786 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
787 "Seat has no VTs but VT number not 0");
791 if (t
== _SESSION_TYPE_INVALID
) {
792 if (!isempty(display
))
794 else if (!isempty(tty
))
797 t
= SESSION_UNSPECIFIED
;
800 if (c
== _SESSION_CLASS_INVALID
) {
801 if (t
== SESSION_UNSPECIFIED
)
802 c
= SESSION_BACKGROUND
;
808 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
810 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
814 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
819 /* Check if we are already in a logind session. Or if we are in user@.service
820 * which is a special PAM session that avoids creating a logind session. */
821 r
= manager_get_user_by_pid(m
, leader
, NULL
);
825 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
826 "Already running in a session or user slice");
829 * Old gdm and lightdm start the user-session on the same VT as
830 * the greeter session. But they destroy the greeter session
831 * after the user-session and want the user-session to take
832 * over the VT. We need to support this for
833 * backwards-compatibility, so make sure we allow new sessions
834 * on a VT that a greeter is running on. Furthermore, to allow
835 * re-logins, we have to allow a greeter to take over a used VT for
836 * the exact same reasons.
838 if (c
!= SESSION_GREETER
&&
840 vtnr
< MALLOC_ELEMENTSOF(m
->seat0
->positions
) &&
841 m
->seat0
->positions
[vtnr
] &&
842 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
843 return sd_bus_error_set(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
845 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
846 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
847 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
850 (void) audit_session_from_pid(leader
, &audit_id
);
851 if (audit_session_is_valid(audit_id
)) {
852 /* Keep our session IDs and the audit session IDs in sync */
854 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
857 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
858 * not trust the audit data and let's better register a new ID */
859 if (hashmap_contains(m
->sessions
, id
)) {
860 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
861 audit_id
= AUDIT_SESSION_INVALID
;
870 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
873 } while (hashmap_contains(m
->sessions
, id
));
876 /* The generated names should not clash with 'auto' or 'self' */
877 assert(!SESSION_IS_SELF(id
));
878 assert(!SESSION_IS_AUTO(id
));
880 /* If we are not watching utmp already, try again */
881 manager_reconnect_utmp(m
);
883 r
= manager_add_user_by_uid(m
, uid
, &user
);
887 r
= manager_add_session(m
, id
, &session
);
891 session_set_user(session
, user
);
892 r
= session_set_leader(session
, leader
);
896 session
->original_type
= session
->type
= t
;
898 session
->remote
= remote
;
899 session
->vtnr
= vtnr
;
902 session
->tty
= strdup(tty
);
908 session
->tty_validity
= TTY_FROM_PAM
;
911 if (!isempty(display
)) {
912 session
->display
= strdup(display
);
913 if (!session
->display
) {
919 if (!isempty(remote_user
)) {
920 session
->remote_user
= strdup(remote_user
);
921 if (!session
->remote_user
) {
927 if (!isempty(remote_host
)) {
928 session
->remote_host
= strdup(remote_host
);
929 if (!session
->remote_host
) {
935 if (!isempty(service
)) {
936 session
->service
= strdup(service
);
937 if (!session
->service
) {
943 if (!isempty(desktop
)) {
944 session
->desktop
= strdup(desktop
);
945 if (!session
->desktop
) {
952 r
= seat_attach_session(seat
, session
);
957 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
961 r
= session_start(session
, message
, error
);
965 r
= sd_bus_message_exit_container(message
);
969 session
->create_message
= sd_bus_message_ref(message
);
971 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
972 * session_send_create_reply(). */
978 session_add_to_gc_queue(session
);
981 user_add_to_gc_queue(user
);
986 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
987 Manager
*m
= ASSERT_PTR(userdata
);
994 r
= sd_bus_message_read(message
, "s", &name
);
998 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1002 r
= session_release(session
);
1006 return sd_bus_reply_method_return(message
, NULL
);
1009 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1010 Manager
*m
= ASSERT_PTR(userdata
);
1017 r
= sd_bus_message_read(message
, "s", &name
);
1021 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1025 /* PolicyKit is done by bus_session_method_activate() */
1027 return bus_session_method_activate(message
, session
, error
);
1030 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1031 const char *session_name
, *seat_name
;
1032 Manager
*m
= ASSERT_PTR(userdata
);
1039 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1041 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1045 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1049 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1053 if (session
->seat
!= seat
)
1054 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1055 "Session %s not on seat %s", session_name
, seat_name
);
1057 r
= check_polkit_chvt(message
, m
, error
);
1061 return 1; /* Will call us back */
1063 r
= session_activate(session
);
1067 return sd_bus_reply_method_return(message
, NULL
);
1070 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1071 Manager
*m
= ASSERT_PTR(userdata
);
1078 r
= sd_bus_message_read(message
, "s", &name
);
1082 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1086 return bus_session_method_lock(message
, session
, error
);
1089 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1090 Manager
*m
= ASSERT_PTR(userdata
);
1095 r
= bus_verify_polkit_async(
1098 "org.freedesktop.login1.lock-sessions",
1102 &m
->polkit_registry
,
1107 return 1; /* Will call us back */
1109 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1113 return sd_bus_reply_method_return(message
, NULL
);
1116 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1118 Manager
*m
= ASSERT_PTR(userdata
);
1124 r
= sd_bus_message_read(message
, "s", &name
);
1128 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1132 return bus_session_method_kill(message
, session
, error
);
1135 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1136 Manager
*m
= ASSERT_PTR(userdata
);
1143 r
= sd_bus_message_read(message
, "u", &uid
);
1147 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1151 return bus_user_method_kill(message
, user
, error
);
1154 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1155 Manager
*m
= ASSERT_PTR(userdata
);
1162 r
= sd_bus_message_read(message
, "s", &name
);
1166 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1170 return bus_session_method_terminate(message
, session
, error
);
1173 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1174 Manager
*m
= ASSERT_PTR(userdata
);
1181 r
= sd_bus_message_read(message
, "u", &uid
);
1185 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1189 return bus_user_method_terminate(message
, user
, error
);
1192 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1193 Manager
*m
= ASSERT_PTR(userdata
);
1200 r
= sd_bus_message_read(message
, "s", &name
);
1204 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1208 return bus_seat_method_terminate(message
, seat
, error
);
1211 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1212 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1213 _cleanup_free_
char *cc
= NULL
;
1214 Manager
*m
= ASSERT_PTR(userdata
);
1215 int r
, b
, interactive
;
1218 uint32_t uid
, auth_uid
;
1222 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1226 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1227 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1231 if (!uid_is_valid(uid
)) {
1232 /* Note that we get the owner UID of the session or user unit,
1233 * not the actual client UID here! */
1234 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1239 /* owner_uid is racy, so for authorization we must use euid */
1240 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1247 return errno_or_else(ENOENT
);
1249 r
= bus_verify_polkit_async(
1252 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1253 "org.freedesktop.login1.set-user-linger",
1257 &m
->polkit_registry
,
1262 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1264 (void) mkdir_p_label("/var/lib/systemd", 0755);
1265 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1269 cc
= cescape(pw
->pw_name
);
1273 path
= strjoina("/var/lib/systemd/linger/", cc
);
1281 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1288 if (r
< 0 && errno
!= ENOENT
)
1291 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1293 user_add_to_gc_queue(u
);
1296 return sd_bus_reply_method_return(message
, NULL
);
1299 static int trigger_device(Manager
*m
, sd_device
*d
) {
1300 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1305 r
= sd_device_enumerator_new(&e
);
1309 r
= sd_device_enumerator_allow_uninitialized(e
);
1314 r
= sd_device_enumerator_add_match_parent(e
, d
);
1319 FOREACH_DEVICE(e
, d
) {
1320 r
= sd_device_trigger(d
, SD_DEVICE_CHANGE
);
1322 log_device_debug_errno(d
, r
, "Failed to trigger device, ignoring: %m");
1328 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1329 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1330 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1331 const char *id_for_seat
;
1338 r
= sd_device_new_from_syspath(&d
, sysfs
);
1342 if (sd_device_has_current_tag(d
, "seat") <= 0)
1345 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1348 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1351 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1354 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1355 r
= write_string_file_atomic_label(file
, rule
);
1359 return trigger_device(m
, d
);
1362 static int flush_devices(Manager
*m
) {
1363 _cleanup_closedir_
DIR *d
= NULL
;
1367 d
= opendir("/etc/udev/rules.d");
1369 if (errno
!= ENOENT
)
1370 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1372 FOREACH_DIRENT_ALL(de
, d
, break) {
1373 if (!dirent_is_file(de
))
1376 if (!startswith(de
->d_name
, "72-seat-"))
1379 if (!endswith(de
->d_name
, ".rules"))
1382 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1383 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1386 return trigger_device(m
, NULL
);
1389 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1390 const char *sysfs
, *seat
;
1391 Manager
*m
= ASSERT_PTR(userdata
);
1396 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1400 if (!path_is_normalized(sysfs
))
1401 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1402 if (!path_startswith(sysfs
, "/sys"))
1403 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1405 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1408 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1414 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1415 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1417 r
= bus_verify_polkit_async(
1420 "org.freedesktop.login1.attach-device",
1424 &m
->polkit_registry
,
1429 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1431 r
= attach_device(m
, seat
, sysfs
);
1435 return sd_bus_reply_method_return(message
, NULL
);
1438 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1439 Manager
*m
= ASSERT_PTR(userdata
);
1444 r
= sd_bus_message_read(message
, "b", &interactive
);
1448 r
= bus_verify_polkit_async(
1451 "org.freedesktop.login1.flush-devices",
1455 &m
->polkit_registry
,
1460 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1462 r
= flush_devices(m
);
1466 return sd_bus_reply_method_return(message
, NULL
);
1469 static int have_multiple_sessions(
1477 /* Check for other users' sessions. Greeter sessions do not
1478 * count, and non-login sessions do not count either. */
1479 HASHMAP_FOREACH(session
, m
->sessions
)
1480 if (session
->class == SESSION_USER
&&
1481 session
->user
->user_record
->uid
!= uid
)
1487 static int bus_manager_log_shutdown(
1489 const HandleActionData
*a
) {
1493 const char *message
= a
->message
?: "System is shutting down";
1494 const char *log_verb
= a
->log_verb
? strjoina("SHUTDOWN=", a
->log_verb
) : NULL
;
1496 return log_struct(LOG_NOTICE
,
1497 "MESSAGE_ID=%s", a
->message_id
?: SD_MESSAGE_SHUTDOWN_STR
,
1498 LOG_MESSAGE("%s%s%s%s.",
1500 m
->wall_message
? " (" : "",
1501 strempty(m
->wall_message
),
1502 m
->wall_message
? ")" : ""),
1506 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1507 Manager
*m
= ASSERT_PTR(userdata
);
1511 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1515 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1520 if (until
<= now(CLOCK_MONOTONIC
))
1523 /* We want to ignore the lid switch for a while after each
1524 * suspend, and after boot-up. Hence let's install a timer for
1525 * this. As long as the event source exists we ignore the lid
1528 if (m
->lid_switch_ignore_event_source
) {
1531 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1538 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1540 r
= sd_event_add_time(
1542 &m
->lid_switch_ignore_event_source
,
1545 lid_switch_ignore_handler
, m
);
1550 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1551 int active
= _active
;
1554 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1556 return sd_bus_emit_signal(m
->bus
,
1557 "/org/freedesktop/login1",
1558 "org.freedesktop.login1.Manager",
1559 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1564 static int execute_shutdown_or_sleep(
1566 const HandleActionData
*a
,
1567 sd_bus_error
*error
) {
1569 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1576 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
)
1577 bus_manager_log_shutdown(m
, a
);
1579 r
= bus_call_method(
1585 "ss", a
->target
, "replace-irreversibly");
1589 r
= sd_bus_message_read(reply
, "o", &p
);
1593 r
= free_and_strdup(&m
->action_job
, p
);
1597 m
->delayed_action
= a
;
1599 /* Make sure the lid switch is ignored for a while */
1600 manager_set_lid_switch_ignore(m
, usec_add(now(CLOCK_MONOTONIC
), m
->holdoff_timeout_usec
));
1605 /* Tell people that they now may take a lock again */
1606 (void) send_prepare_for(m
, a
->inhibit_what
, false);
1611 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1612 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1613 Inhibitor
*offending
= NULL
;
1618 if (!manager
->delayed_action
|| manager
->action_job
)
1621 if (manager_is_inhibited(manager
, manager
->delayed_action
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1622 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1627 (void) get_process_comm(offending
->pid
, &comm
);
1628 u
= uid_to_name(offending
->uid
);
1630 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1631 offending
->uid
, strna(u
),
1632 offending
->pid
, strna(comm
));
1635 /* Actually do the operation */
1636 r
= execute_shutdown_or_sleep(manager
, manager
->delayed_action
, &error
);
1638 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1639 bus_error_message(&error
, r
));
1641 manager
->delayed_action
= NULL
;
1644 return 1; /* We did some work. */
1647 static int manager_inhibit_timeout_handler(
1652 Manager
*manager
= ASSERT_PTR(userdata
);
1654 assert(manager
->inhibit_timeout_source
== s
);
1656 return manager_dispatch_delayed(manager
, true);
1659 static int delay_shutdown_or_sleep(
1661 const HandleActionData
*a
) {
1668 if (m
->inhibit_timeout_source
) {
1669 r
= sd_event_source_set_time_relative(m
->inhibit_timeout_source
, m
->inhibit_delay_max
);
1671 return log_error_errno(r
, "sd_event_source_set_time_relative() failed: %m");
1673 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1675 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1677 r
= sd_event_add_time_relative(
1679 &m
->inhibit_timeout_source
,
1680 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, 0,
1681 manager_inhibit_timeout_handler
, m
);
1686 m
->delayed_action
= a
;
1691 int bus_manager_shutdown_or_sleep_now_or_later(
1693 const HandleActionData
*a
,
1694 sd_bus_error
*error
) {
1696 _cleanup_free_
char *load_state
= NULL
;
1702 assert(!m
->action_job
);
1704 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
1708 if (!streq(load_state
, "loaded"))
1709 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1710 "Unit %s is %s, refusing operation.",
1711 a
->target
, load_state
);
1713 /* Tell everybody to prepare for shutdown/sleep */
1714 (void) send_prepare_for(m
, a
->inhibit_what
, true);
1717 m
->inhibit_delay_max
> 0 &&
1718 manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1721 /* Shutdown is delayed, keep in mind what we
1722 * want to do, and start a timeout */
1723 r
= delay_shutdown_or_sleep(m
, a
);
1725 /* Shutdown is not delayed, execute it
1727 r
= execute_shutdown_or_sleep(m
, a
, error
);
1732 static int verify_shutdown_creds(
1734 sd_bus_message
*message
,
1735 const HandleActionData
*a
,
1737 sd_bus_error
*error
) {
1739 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1740 bool multiple_sessions
, blocked
, interactive
;
1748 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1752 r
= sd_bus_creds_get_euid(creds
, &uid
);
1756 r
= have_multiple_sessions(m
, uid
);
1760 multiple_sessions
= r
> 0;
1761 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1762 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
1764 if (multiple_sessions
) {
1765 r
= bus_verify_polkit_async(
1768 a
->polkit_action_multiple_sessions
,
1772 &m
->polkit_registry
,
1777 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1781 /* We don't check polkit for root here, because you can't be more privileged than root */
1782 if (uid
== 0 && (flags
& SD_LOGIND_ROOT_CHECK_INHIBITORS
))
1783 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
,
1784 "Access denied to root due to active block inhibitor");
1786 r
= bus_verify_polkit_async(message
,
1788 a
->polkit_action_ignore_inhibit
,
1792 &m
->polkit_registry
,
1797 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1800 if (!multiple_sessions
&& !blocked
) {
1801 r
= bus_verify_polkit_async(message
,
1807 &m
->polkit_registry
,
1812 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1818 static int setup_wall_message_timer(Manager
*m
, sd_bus_message
* message
) {
1819 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1822 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
1824 const char *tty
= NULL
;
1826 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
1827 (void) sd_bus_creds_get_tty(creds
, &tty
);
1829 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
1834 r
= manager_setup_wall_message_timer(m
);
1841 static int method_do_shutdown_or_sleep(
1843 sd_bus_message
*message
,
1844 const HandleActionData
*a
,
1846 sd_bus_error
*error
) {
1856 /* New style method: with flags parameter (and interactive bool in the bus message header) */
1857 r
= sd_bus_message_read(message
, "t", &flags
);
1860 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
1861 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
1862 "Invalid flags parameter");
1863 if (a
->handle
!= HANDLE_REBOOT
&& (flags
& SD_LOGIND_REBOOT_VIA_KEXEC
))
1864 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
1865 "Reboot via kexec is only applicable with reboot operations");
1867 /* Old style method: no flags parameter, but interactive bool passed as boolean in
1868 * payload. Let's convert this argument to the new-style flags parameter for our internal
1872 r
= sd_bus_message_read(message
, "b", &interactive
);
1876 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
1879 if ((flags
& SD_LOGIND_REBOOT_VIA_KEXEC
) && kexec_loaded())
1880 a
= handle_action_lookup(HANDLE_KEXEC
);
1882 /* Don't allow multiple jobs being executed at the same time */
1883 if (m
->delayed_action
)
1884 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1885 "There's already a shutdown or sleep operation in progress");
1887 if (a
->sleep_operation
>= 0) {
1888 r
= can_sleep(a
->sleep_operation
);
1890 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1891 "Not enough swap space for hibernation");
1893 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1894 "Sleep verb \"%s\" not supported",
1895 sleep_operation_to_string(a
->sleep_operation
));
1900 r
= verify_shutdown_creds(m
, message
, a
, flags
, error
);
1904 /* reset case we're shorting a scheduled shutdown */
1905 m
->unlink_nologin
= false;
1906 reset_scheduled_shutdown(m
);
1908 m
->scheduled_shutdown_timeout
= 0;
1909 m
->scheduled_shutdown_action
= a
;
1911 (void) setup_wall_message_timer(m
, message
);
1913 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, error
);
1917 return sd_bus_reply_method_return(message
, NULL
);
1920 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1921 Manager
*m
= userdata
;
1923 return method_do_shutdown_or_sleep(
1925 handle_action_lookup(HANDLE_POWEROFF
),
1926 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
1930 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1931 Manager
*m
= userdata
;
1933 return method_do_shutdown_or_sleep(
1935 handle_action_lookup(HANDLE_REBOOT
),
1936 sd_bus_message_is_method_call(message
, NULL
, "RebootWithFlags"),
1940 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1941 Manager
*m
= userdata
;
1943 return method_do_shutdown_or_sleep(
1945 handle_action_lookup(HANDLE_HALT
),
1946 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
1950 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1951 Manager
*m
= userdata
;
1953 return method_do_shutdown_or_sleep(
1955 handle_action_lookup(HANDLE_SUSPEND
),
1956 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
1960 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1961 Manager
*m
= userdata
;
1963 return method_do_shutdown_or_sleep(
1965 handle_action_lookup(HANDLE_HIBERNATE
),
1966 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
1970 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1971 Manager
*m
= userdata
;
1973 return method_do_shutdown_or_sleep(
1975 handle_action_lookup(HANDLE_HYBRID_SLEEP
),
1976 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
1980 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1981 Manager
*m
= userdata
;
1983 return method_do_shutdown_or_sleep(
1985 handle_action_lookup(HANDLE_SUSPEND_THEN_HIBERNATE
),
1986 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
1990 static int nologin_timeout_handler(
1995 Manager
*m
= userdata
;
1997 log_info("Creating /run/nologin, blocking further logins...");
2000 create_shutdown_run_nologin_or_warn() >= 0;
2005 static usec_t
nologin_timeout_usec(usec_t elapse
) {
2006 /* Issue /run/nologin five minutes before shutdown */
2007 return LESS_BY(elapse
, 5 * USEC_PER_MINUTE
);
2010 void manager_load_scheduled_shutdown(Manager
*m
) {
2011 _cleanup_fclose_
FILE *f
= NULL
;
2012 _cleanup_free_
char *usec
= NULL
,
2015 *wall_message
= NULL
,
2022 r
= parse_env_file(f
, SHUTDOWN_SCHEDULE_FILE
,
2024 "WARN_WALL", &warn_wall
,
2026 "WALL_MESSAGE", &wall_message
,
2030 /* reset will delete the file */
2031 reset_scheduled_shutdown(m
);
2036 return (void) log_debug_errno(r
, "Failed to parse " SHUTDOWN_SCHEDULE_FILE
": %m");
2038 HandleAction handle
= handle_action_from_string(mode
);
2040 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse scheduled shutdown type: %s", mode
);
2043 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "USEC is required");
2044 if (deserialize_usec(usec
, &m
->scheduled_shutdown_timeout
) < 0)
2047 /* assign parsed type only after we know usec is also valid */
2048 m
->scheduled_shutdown_action
= handle_action_lookup(handle
);
2051 r
= parse_boolean(warn_wall
);
2053 log_debug_errno(r
, "Failed to parse enabling wall messages");
2055 m
->enable_wall_messages
= r
;
2059 _cleanup_free_
char *unescaped
= NULL
;
2060 r
= cunescape(wall_message
, 0, &unescaped
);
2062 log_debug_errno(r
, "Failed to parse wall message: %s", wall_message
);
2064 free_and_replace(m
->wall_message
, unescaped
);
2068 r
= parse_uid(uid
, &m
->scheduled_shutdown_uid
);
2070 log_debug_errno(r
, "Failed to parse wall uid: %s", uid
);
2073 free_and_replace(m
->scheduled_shutdown_tty
, tty
);
2075 r
= manager_setup_shutdown_timers(m
);
2077 return reset_scheduled_shutdown(m
);
2079 (void) manager_setup_wall_message_timer(m
);
2080 (void) update_schedule_file(m
);
2085 static int update_schedule_file(Manager
*m
) {
2086 _cleanup_free_
char *temp_path
= NULL
;
2087 _cleanup_fclose_
FILE *f
= NULL
;
2091 assert(m
->scheduled_shutdown_action
);
2093 r
= mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE
, 0755);
2095 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2097 r
= fopen_temporary(SHUTDOWN_SCHEDULE_FILE
, &f
, &temp_path
);
2099 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2101 (void) fchmod(fileno(f
), 0644);
2103 serialize_usec(f
, "USEC", m
->scheduled_shutdown_timeout
);
2104 serialize_item_format(f
, "WARN_WALL", "%s", one_zero(m
->enable_wall_messages
));
2105 serialize_item_format(f
, "MODE", "%s", handle_action_to_string(m
->scheduled_shutdown_action
->handle
));
2106 serialize_item_format(f
, "UID", UID_FMT
, m
->scheduled_shutdown_uid
);
2108 if (m
->scheduled_shutdown_tty
)
2109 serialize_item_format(f
, "TTY", "%s", m
->scheduled_shutdown_tty
);
2111 if (!isempty(m
->wall_message
)) {
2112 r
= serialize_item_escaped(f
, "WALL_MESSAGE", m
->wall_message
);
2117 r
= fflush_and_check(f
);
2121 if (rename(temp_path
, SHUTDOWN_SCHEDULE_FILE
) < 0) {
2129 (void) unlink(temp_path
);
2130 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2132 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2135 static void reset_scheduled_shutdown(Manager
*m
) {
2138 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2139 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2140 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2142 m
->scheduled_shutdown_action
= NULL
;
2143 m
->scheduled_shutdown_timeout
= USEC_INFINITY
;
2144 m
->scheduled_shutdown_uid
= UID_INVALID
;
2145 m
->scheduled_shutdown_tty
= mfree(m
->scheduled_shutdown_tty
);
2146 m
->wall_message
= mfree(m
->wall_message
);
2147 m
->shutdown_dry_run
= false;
2149 if (m
->unlink_nologin
) {
2150 (void) unlink_or_warn("/run/nologin");
2151 m
->unlink_nologin
= false;
2154 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2157 static int manager_scheduled_shutdown_handler(
2162 const HandleActionData
*a
= NULL
;
2163 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2164 Manager
*m
= ASSERT_PTR(userdata
);
2167 a
= m
->scheduled_shutdown_action
;
2170 /* Don't allow multiple jobs being executed at the same time */
2171 if (m
->delayed_action
) {
2173 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", a
->target
);
2177 if (m
->shutdown_dry_run
) {
2178 /* We do not process delay inhibitors here. Otherwise, we
2179 * would have to be considered "in progress" (like the check
2180 * above) for some seconds after our admin has seen the final
2183 bus_manager_log_shutdown(m
, a
);
2184 log_info("Running in dry run, suppressing action.");
2185 reset_scheduled_shutdown(m
);
2190 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, m
->scheduled_shutdown_action
, &error
);
2192 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", a
->target
);
2199 reset_scheduled_shutdown(m
);
2203 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2204 Manager
*m
= ASSERT_PTR(userdata
);
2205 HandleAction handle
;
2206 const HandleActionData
*a
;
2210 bool dry_run
= false;
2214 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2218 if (startswith(type
, "dry-")) {
2223 handle
= handle_action_from_string(type
);
2224 if (!IN_SET(handle
, HANDLE_POWEROFF
, HANDLE_REBOOT
, HANDLE_HALT
, HANDLE_KEXEC
))
2225 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2227 a
= handle_action_lookup(handle
);
2229 assert(a
->polkit_action
);
2231 r
= verify_shutdown_creds(m
, message
, a
, 0, error
);
2235 m
->scheduled_shutdown_action
= a
;
2236 m
->shutdown_dry_run
= dry_run
;
2237 m
->scheduled_shutdown_timeout
= elapse
;
2239 r
= manager_setup_shutdown_timers(m
);
2243 r
= setup_wall_message_timer(m
, message
);
2245 r
= update_schedule_file(m
);
2248 reset_scheduled_shutdown(m
);
2252 return sd_bus_reply_method_return(message
, NULL
);
2255 static int manager_setup_shutdown_timers(Manager
* m
) {
2258 r
= event_reset_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2260 m
->scheduled_shutdown_timeout
, 0,
2261 manager_scheduled_shutdown_handler
, m
,
2262 0, "scheduled-shutdown-timeout", true);
2266 r
= event_reset_time(m
->event
, &m
->nologin_timeout_source
,
2268 nologin_timeout_usec(m
->scheduled_shutdown_timeout
), 0,
2269 nologin_timeout_handler
, m
,
2270 0, "nologin-timeout", true);
2277 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2278 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2283 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2284 Manager
*m
= ASSERT_PTR(userdata
);
2285 const HandleActionData
*a
;
2291 cancelled
= m
->scheduled_shutdown_action
2292 && !IN_SET(m
->scheduled_shutdown_action
->handle
, HANDLE_IGNORE
, _HANDLE_ACTION_INVALID
);
2294 return sd_bus_reply_method_return(message
, "b", false);
2296 a
= m
->scheduled_shutdown_action
;
2297 if (!a
->polkit_action
)
2298 return sd_bus_error_set(error
, SD_BUS_ERROR_AUTH_FAILED
, "Unsupported shutdown type");
2300 r
= bus_verify_polkit_async(
2307 &m
->polkit_registry
,
2312 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2314 if (m
->enable_wall_messages
) {
2315 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2316 const char *tty
= NULL
;
2319 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2321 (void) sd_bus_creds_get_uid(creds
, &uid
);
2322 (void) sd_bus_creds_get_tty(creds
, &tty
);
2325 _cleanup_free_
char *username
= uid_to_name(uid
);
2327 log_struct(LOG_INFO
,
2328 LOG_MESSAGE("System shutdown has been cancelled"),
2329 "ACTION=%s", handle_action_to_string(a
->handle
),
2330 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_CANCELED_STR
,
2331 username
? "OPERATOR=%s" : NULL
, username
);
2333 utmp_wall("System shutdown has been cancelled",
2334 username
, tty
, logind_wall_tty_filter
, m
);
2337 reset_scheduled_shutdown(m
);
2339 return sd_bus_reply_method_return(message
, "b", true);
2342 static int method_can_shutdown_or_sleep(
2344 sd_bus_message
*message
,
2345 const HandleActionData
*a
,
2346 sd_bus_error
*error
) {
2348 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2349 bool multiple_sessions
, challenge
, blocked
;
2350 const char *result
= NULL
;
2358 if (a
->sleep_operation
>= 0) {
2359 r
= can_sleep(a
->sleep_operation
);
2360 if (IN_SET(r
, 0, -ENOSPC
))
2361 return sd_bus_reply_method_return(message
, "s", "na");
2366 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2370 r
= sd_bus_creds_get_euid(creds
, &uid
);
2374 r
= have_multiple_sessions(m
, uid
);
2378 multiple_sessions
= r
> 0;
2379 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2381 HandleAction handle
= handle_action_from_string(sleep_operation_to_string(a
->sleep_operation
));
2385 target
= handle_action_lookup(handle
)->target
;
2387 _cleanup_free_
char *load_state
= NULL
;
2389 r
= unit_load_state(m
->bus
, target
, &load_state
);
2393 if (!streq(load_state
, "loaded")) {
2400 if (multiple_sessions
) {
2401 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2408 result
= "challenge";
2414 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2421 } else if (challenge
) {
2422 if (!result
|| streq(result
, "yes"))
2423 result
= "challenge";
2428 if (!multiple_sessions
&& !blocked
) {
2429 /* If neither inhibit nor multiple sessions
2430 * apply then just check the normal policy */
2432 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action
, NULL
, UID_INVALID
, &challenge
, error
);
2439 result
= "challenge";
2445 return sd_bus_reply_method_return(message
, "s", result
);
2448 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2449 Manager
*m
= userdata
;
2451 return method_can_shutdown_or_sleep(
2452 m
, message
, handle_action_lookup(HANDLE_POWEROFF
),
2456 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2457 Manager
*m
= userdata
;
2459 return method_can_shutdown_or_sleep(
2460 m
, message
, handle_action_lookup(HANDLE_REBOOT
),
2464 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2465 Manager
*m
= userdata
;
2467 return method_can_shutdown_or_sleep(
2468 m
, message
, handle_action_lookup(HANDLE_HALT
),
2472 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2473 Manager
*m
= userdata
;
2475 return method_can_shutdown_or_sleep(
2476 m
, message
, handle_action_lookup(HANDLE_SUSPEND
),
2480 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2481 Manager
*m
= userdata
;
2483 return method_can_shutdown_or_sleep(
2484 m
, message
, handle_action_lookup(HANDLE_HIBERNATE
),
2488 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2489 Manager
*m
= userdata
;
2491 return method_can_shutdown_or_sleep(
2492 m
, message
, handle_action_lookup(HANDLE_HYBRID_SLEEP
),
2496 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2497 Manager
*m
= userdata
;
2499 return method_can_shutdown_or_sleep(
2500 m
, message
, handle_action_lookup(HANDLE_SUSPEND_THEN_HIBERNATE
),
2504 static int property_get_reboot_parameter(
2507 const char *interface
,
2508 const char *property
,
2509 sd_bus_message
*reply
,
2511 sd_bus_error
*error
) {
2512 _cleanup_free_
char *parameter
= NULL
;
2519 r
= read_reboot_parameter(¶meter
);
2523 return sd_bus_message_append(reply
, "s", parameter
);
2526 static int method_set_reboot_parameter(
2527 sd_bus_message
*message
,
2529 sd_bus_error
*error
) {
2531 Manager
*m
= ASSERT_PTR(userdata
);
2537 r
= sd_bus_message_read(message
, "s", &arg
);
2541 r
= detect_container();
2545 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2546 "Reboot parameter not supported in containers, refusing.");
2548 r
= bus_verify_polkit_async(message
,
2550 "org.freedesktop.login1.set-reboot-parameter",
2554 &m
->polkit_registry
,
2559 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2561 r
= update_reboot_parameter_and_warn(arg
, false);
2565 return sd_bus_reply_method_return(message
, NULL
);
2568 static int method_can_reboot_parameter(
2569 sd_bus_message
*message
,
2571 sd_bus_error
*error
) {
2573 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2578 r
= detect_container();
2581 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2582 return sd_bus_reply_method_return(message
, "s", "na");
2584 return return_test_polkit(
2587 "org.freedesktop.login1.set-reboot-parameter",
2593 static int property_get_reboot_to_firmware_setup(
2596 const char *interface
,
2597 const char *property
,
2598 sd_bus_message
*reply
,
2600 sd_bus_error
*error
) {
2607 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2609 /* EFI case: let's see what is currently configured in the EFI variables */
2610 r
= efi_get_reboot_to_firmware();
2611 if (r
< 0 && r
!= -EOPNOTSUPP
)
2612 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2614 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2616 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2617 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2618 if (errno
!= ENOENT
)
2619 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2626 return sd_bus_message_append(reply
, "b", r
> 0);
2629 static int method_set_reboot_to_firmware_setup(
2630 sd_bus_message
*message
,
2632 sd_bus_error
*error
) {
2634 Manager
*m
= ASSERT_PTR(userdata
);
2640 r
= sd_bus_message_read(message
, "b", &b
);
2644 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2646 /* EFI case: let's see what the firmware supports */
2648 r
= efi_reboot_to_firmware_supported();
2649 if (r
== -EOPNOTSUPP
)
2650 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2656 } else if (r
<= 0) {
2657 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2660 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2662 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2664 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2667 r
= bus_verify_polkit_async(message
,
2669 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2673 &m
->polkit_registry
,
2678 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2681 r
= efi_set_reboot_to_firmware(b
);
2686 r
= touch("/run/systemd/reboot-to-firmware-setup");
2690 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2695 return sd_bus_reply_method_return(message
, NULL
);
2698 static int method_can_reboot_to_firmware_setup(
2699 sd_bus_message
*message
,
2701 sd_bus_error
*error
) {
2703 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2708 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2710 /* EFI case: let's see what the firmware supports */
2712 r
= efi_reboot_to_firmware_supported();
2714 if (r
!= -EOPNOTSUPP
)
2715 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2717 return sd_bus_reply_method_return(message
, "s", "na");
2720 } else if (r
<= 0) {
2721 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2724 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2726 return sd_bus_reply_method_return(message
, "s", "na");
2729 return return_test_polkit(
2732 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2738 static int property_get_reboot_to_boot_loader_menu(
2741 const char *interface
,
2742 const char *property
,
2743 sd_bus_message
*reply
,
2745 sd_bus_error
*error
) {
2747 uint64_t x
= UINT64_MAX
;
2754 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2756 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished:
2758 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2759 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2760 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2763 r
= efi_loader_get_config_timeout_one_shot(&x
);
2766 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
2770 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2772 _cleanup_free_
char *v
= NULL
;
2774 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2776 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2779 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2781 r
= safe_atou64(v
, &x
);
2783 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2787 return sd_bus_message_append(reply
, "t", x
);
2790 static int method_set_reboot_to_boot_loader_menu(
2791 sd_bus_message
*message
,
2793 sd_bus_error
*error
) {
2795 Manager
*m
= ASSERT_PTR(userdata
);
2802 r
= sd_bus_message_read(message
, "t", &x
);
2806 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2810 /* EFI case: let's see if booting into boot loader menu is supported. */
2812 r
= efi_loader_get_features(&features
);
2814 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2815 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2816 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2820 } else if (r
<= 0) {
2821 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2824 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2826 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2828 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2831 r
= bus_verify_polkit_async(message
,
2833 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2837 &m
->polkit_registry
,
2842 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2845 if (x
== UINT64_MAX
)
2846 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), NULL
, 0);
2848 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2849 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2851 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), buf
);
2856 if (x
== UINT64_MAX
) {
2857 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
2860 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2862 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2864 r
= write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf
);
2870 return sd_bus_reply_method_return(message
, NULL
);
2873 static int method_can_reboot_to_boot_loader_menu(
2874 sd_bus_message
*message
,
2876 sd_bus_error
*error
) {
2878 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2883 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2885 uint64_t features
= 0;
2887 /* EFI case, let's see if booting into boot loader menu is supported. */
2889 r
= efi_loader_get_features(&features
);
2891 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2892 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2893 return sd_bus_reply_method_return(message
, "s", "na");
2895 } else if (r
<= 0) {
2896 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2899 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2901 return sd_bus_reply_method_return(message
, "s", "na");
2904 return return_test_polkit(
2907 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2913 static int property_get_reboot_to_boot_loader_entry(
2916 const char *interface
,
2917 const char *property
,
2918 sd_bus_message
*reply
,
2920 sd_bus_error
*error
) {
2922 _cleanup_free_
char *v
= NULL
;
2923 Manager
*m
= ASSERT_PTR(userdata
);
2924 const char *x
= NULL
;
2930 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2932 /* EFI case: let's read the LoaderEntryOneShot variable */
2934 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
2937 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
2939 x
= m
->efi_loader_entry_one_shot
;
2942 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2945 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2947 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2950 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
2951 } else if (!efi_loader_entry_name_valid(v
))
2952 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2957 return sd_bus_message_append(reply
, "s", x
);
2960 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
2961 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
2967 r
= boot_config_load_auto(&config
, NULL
, NULL
);
2968 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2971 r
= manager_read_efi_boot_loader_entries(m
);
2973 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
2975 return !!boot_config_find_entry(&config
, id
);
2978 static int method_set_reboot_to_boot_loader_entry(
2979 sd_bus_message
*message
,
2981 sd_bus_error
*error
) {
2983 Manager
*m
= ASSERT_PTR(userdata
);
2990 r
= sd_bus_message_read(message
, "s", &v
);
2996 else if (efi_loader_entry_name_valid(v
)) {
2997 r
= boot_loader_entry_exists(m
, v
);
3001 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3003 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3005 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3009 /* EFI case: let's see if booting into boot loader entry is supported. */
3011 r
= efi_loader_get_features(&features
);
3013 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3014 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3015 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3019 } else if (r
<= 0) {
3020 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3023 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3025 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3027 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3030 r
= bus_verify_polkit_async(message
,
3032 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3036 &m
->polkit_registry
,
3041 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3046 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), NULL
, 0);
3048 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), v
);
3053 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3056 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3062 return sd_bus_reply_method_return(message
, NULL
);
3065 static int method_can_reboot_to_boot_loader_entry(
3066 sd_bus_message
*message
,
3068 sd_bus_error
*error
) {
3070 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3075 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3077 uint64_t features
= 0;
3079 /* EFI case, let's see if booting into boot loader entry is supported. */
3081 r
= efi_loader_get_features(&features
);
3083 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3084 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3085 return sd_bus_reply_method_return(message
, "s", "na");
3087 } else if (r
<= 0) {
3088 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3091 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3093 return sd_bus_reply_method_return(message
, "s", "na");
3096 return return_test_polkit(
3099 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3105 static int property_get_boot_loader_entries(
3108 const char *interface
,
3109 const char *property
,
3110 sd_bus_message
*reply
,
3112 sd_bus_error
*error
) {
3114 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3115 Manager
*m
= ASSERT_PTR(userdata
);
3122 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3123 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3126 r
= manager_read_efi_boot_loader_entries(m
);
3128 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3130 r
= sd_bus_message_open_container(reply
, 'a', "s");
3134 for (i
= 0; i
< config
.n_entries
; i
++) {
3135 BootEntry
*e
= config
.entries
+ i
;
3137 r
= sd_bus_message_append(reply
, "s", e
->id
);
3142 return sd_bus_message_close_container(reply
);
3145 static int method_set_wall_message(
3146 sd_bus_message
*message
,
3148 sd_bus_error
*error
) {
3151 Manager
*m
= ASSERT_PTR(userdata
);
3153 int enable_wall_messages
;
3157 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3161 if (strlen(wall_message
) > WALL_MESSAGE_MAX
)
3162 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3163 "Wall message too long, maximum permitted length is %u characters.",
3166 /* Short-circuit the operation if the desired state is already in place, to
3167 * avoid an unnecessary polkit permission check. */
3168 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3169 m
->enable_wall_messages
== enable_wall_messages
)
3172 r
= bus_verify_polkit_async(message
,
3174 "org.freedesktop.login1.set-wall-message",
3178 &m
->polkit_registry
,
3183 return 1; /* Will call us back */
3185 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3189 m
->enable_wall_messages
= enable_wall_messages
;
3192 return sd_bus_reply_method_return(message
, NULL
);
3195 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3196 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3197 const char *who
, *why
, *what
, *mode
;
3198 _cleanup_free_
char *id
= NULL
;
3199 _cleanup_close_
int fifo_fd
= -1;
3200 Manager
*m
= ASSERT_PTR(userdata
);
3209 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3213 w
= inhibit_what_from_string(what
);
3215 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3216 "Invalid what specification %s", what
);
3218 mm
= inhibit_mode_from_string(mode
);
3220 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3221 "Invalid mode specification %s", mode
);
3223 /* Delay is only supported for shutdown/sleep */
3224 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3225 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3226 "Delay inhibitors only supported for shutdown and sleep");
3228 /* Don't allow taking delay locks while we are already
3229 * executing the operation. We shouldn't create the impression
3230 * that the lock was successful if the machine is about to go
3231 * down/suspend any moment. */
3232 if (m
->delayed_action
&& m
->delayed_action
->inhibit_what
& w
)
3233 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3234 "The operation inhibition has been requested for is already running");
3236 r
= bus_verify_polkit_async(
3239 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3240 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3241 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3242 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3243 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3244 w
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3245 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3246 "org.freedesktop.login1.inhibit-handle-lid-switch",
3250 &m
->polkit_registry
,
3255 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3257 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3261 r
= sd_bus_creds_get_euid(creds
, &uid
);
3265 r
= sd_bus_creds_get_pid(creds
, &pid
);
3269 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3270 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3271 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3277 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3280 } while (hashmap_get(m
->inhibitors
, id
));
3282 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3283 r
= manager_add_inhibitor(m
, id
, &i
);
3291 i
->why
= strdup(why
);
3292 i
->who
= strdup(who
);
3294 if (!i
->why
|| !i
->who
)
3297 fifo_fd
= inhibitor_create_fifo(i
);
3301 r
= inhibitor_start(i
);
3306 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3309 static const sd_bus_vtable manager_vtable
[] = {
3310 SD_BUS_VTABLE_START(0),
3312 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool
, bus_property_set_bool
, offsetof(Manager
, enable_wall_messages
), 0),
3313 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3315 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3316 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3317 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3318 SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3319 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3320 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3321 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3322 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3323 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3324 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3325 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3326 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3327 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3328 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3329 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3330 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3331 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3332 SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3333 SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3334 SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3335 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3336 SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3337 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3338 SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3339 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3340 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3341 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3342 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3343 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3344 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3345 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3346 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3347 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3348 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3349 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3350 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3351 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3352 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3353 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3354 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3355 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3356 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3357 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3358 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3359 SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL
, offsetof(Manager
, stop_idle_session_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3361 SD_BUS_METHOD_WITH_ARGS("GetSession",
3362 SD_BUS_ARGS("s", session_id
),
3363 SD_BUS_RESULT("o", object_path
),
3365 SD_BUS_VTABLE_UNPRIVILEGED
),
3366 SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
3367 SD_BUS_ARGS("u", pid
),
3368 SD_BUS_RESULT("o", object_path
),
3369 method_get_session_by_pid
,
3370 SD_BUS_VTABLE_UNPRIVILEGED
),
3371 SD_BUS_METHOD_WITH_ARGS("GetUser",
3372 SD_BUS_ARGS("u", uid
),
3373 SD_BUS_RESULT("o", object_path
),
3375 SD_BUS_VTABLE_UNPRIVILEGED
),
3376 SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
3377 SD_BUS_ARGS("u", pid
),
3378 SD_BUS_RESULT("o", object_path
),
3379 method_get_user_by_pid
,
3380 SD_BUS_VTABLE_UNPRIVILEGED
),
3381 SD_BUS_METHOD_WITH_ARGS("GetSeat",
3382 SD_BUS_ARGS("s", seat_id
),
3383 SD_BUS_RESULT("o", object_path
),
3385 SD_BUS_VTABLE_UNPRIVILEGED
),
3386 SD_BUS_METHOD_WITH_ARGS("ListSessions",
3388 SD_BUS_RESULT("a(susso)", sessions
),
3389 method_list_sessions
,
3390 SD_BUS_VTABLE_UNPRIVILEGED
),
3391 SD_BUS_METHOD_WITH_ARGS("ListUsers",
3393 SD_BUS_RESULT("a(uso)", users
),
3395 SD_BUS_VTABLE_UNPRIVILEGED
),
3396 SD_BUS_METHOD_WITH_ARGS("ListSeats",
3398 SD_BUS_RESULT("a(so)", seats
),
3400 SD_BUS_VTABLE_UNPRIVILEGED
),
3401 SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
3403 SD_BUS_RESULT("a(ssssuu)", inhibitors
),
3404 method_list_inhibitors
,
3405 SD_BUS_VTABLE_UNPRIVILEGED
),
3406 SD_BUS_METHOD_WITH_ARGS("CreateSession",
3407 SD_BUS_ARGS("u", uid
,
3420 "a(sv)", properties
),
3421 SD_BUS_RESULT("s", session_id
,
3429 method_create_session
,
3431 SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
3432 SD_BUS_ARGS("s", session_id
),
3434 method_release_session
,
3436 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
3437 SD_BUS_ARGS("s", session_id
),
3439 method_activate_session
,
3440 SD_BUS_VTABLE_UNPRIVILEGED
),
3441 SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
3442 SD_BUS_ARGS("s", session_id
, "s", seat_id
),
3444 method_activate_session_on_seat
,
3445 SD_BUS_VTABLE_UNPRIVILEGED
),
3446 SD_BUS_METHOD_WITH_ARGS("LockSession",
3447 SD_BUS_ARGS("s", session_id
),
3449 method_lock_session
,
3450 SD_BUS_VTABLE_UNPRIVILEGED
),
3451 SD_BUS_METHOD_WITH_ARGS("UnlockSession",
3452 SD_BUS_ARGS("s", session_id
),
3454 method_lock_session
,
3455 SD_BUS_VTABLE_UNPRIVILEGED
),
3456 SD_BUS_METHOD("LockSessions",
3459 method_lock_sessions
,
3460 SD_BUS_VTABLE_UNPRIVILEGED
),
3461 SD_BUS_METHOD("UnlockSessions",
3464 method_lock_sessions
,
3465 SD_BUS_VTABLE_UNPRIVILEGED
),
3466 SD_BUS_METHOD_WITH_ARGS("KillSession",
3467 SD_BUS_ARGS("s", session_id
, "s", who
, "i", signal_number
),
3469 method_kill_session
,
3470 SD_BUS_VTABLE_UNPRIVILEGED
),
3471 SD_BUS_METHOD_WITH_ARGS("KillUser",
3472 SD_BUS_ARGS("u", uid
, "i", signal_number
),
3475 SD_BUS_VTABLE_UNPRIVILEGED
),
3476 SD_BUS_METHOD_WITH_ARGS("TerminateSession",
3477 SD_BUS_ARGS("s", session_id
),
3479 method_terminate_session
,
3480 SD_BUS_VTABLE_UNPRIVILEGED
),
3481 SD_BUS_METHOD_WITH_ARGS("TerminateUser",
3482 SD_BUS_ARGS("u", uid
),
3484 method_terminate_user
,
3485 SD_BUS_VTABLE_UNPRIVILEGED
),
3486 SD_BUS_METHOD_WITH_ARGS("TerminateSeat",
3487 SD_BUS_ARGS("s", seat_id
),
3489 method_terminate_seat
,
3490 SD_BUS_VTABLE_UNPRIVILEGED
),
3491 SD_BUS_METHOD_WITH_ARGS("SetUserLinger",
3492 SD_BUS_ARGS("u", uid
, "b", enable
, "b", interactive
),
3494 method_set_user_linger
,
3495 SD_BUS_VTABLE_UNPRIVILEGED
),
3496 SD_BUS_METHOD_WITH_ARGS("AttachDevice",
3497 SD_BUS_ARGS("s", seat_id
, "s", sysfs_path
, "b", interactive
),
3499 method_attach_device
,
3500 SD_BUS_VTABLE_UNPRIVILEGED
),
3501 SD_BUS_METHOD_WITH_ARGS("FlushDevices",
3502 SD_BUS_ARGS("b", interactive
),
3504 method_flush_devices
,
3505 SD_BUS_VTABLE_UNPRIVILEGED
),
3506 SD_BUS_METHOD_WITH_ARGS("PowerOff",
3507 SD_BUS_ARGS("b", interactive
),
3510 SD_BUS_VTABLE_UNPRIVILEGED
),
3511 SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags",
3512 SD_BUS_ARGS("t", flags
),
3515 SD_BUS_VTABLE_UNPRIVILEGED
),
3516 SD_BUS_METHOD_WITH_ARGS("Reboot",
3517 SD_BUS_ARGS("b", interactive
),
3520 SD_BUS_VTABLE_UNPRIVILEGED
),
3521 SD_BUS_METHOD_WITH_ARGS("RebootWithFlags",
3522 SD_BUS_ARGS("t", flags
),
3525 SD_BUS_VTABLE_UNPRIVILEGED
),
3526 SD_BUS_METHOD_WITH_ARGS("Halt",
3527 SD_BUS_ARGS("b", interactive
),
3530 SD_BUS_VTABLE_UNPRIVILEGED
),
3531 SD_BUS_METHOD_WITH_ARGS("HaltWithFlags",
3532 SD_BUS_ARGS("t", flags
),
3535 SD_BUS_VTABLE_UNPRIVILEGED
),
3536 SD_BUS_METHOD_WITH_ARGS("Suspend",
3537 SD_BUS_ARGS("b", interactive
),
3540 SD_BUS_VTABLE_UNPRIVILEGED
),
3541 SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags",
3542 SD_BUS_ARGS("t", flags
),
3545 SD_BUS_VTABLE_UNPRIVILEGED
),
3546 SD_BUS_METHOD_WITH_ARGS("Hibernate",
3547 SD_BUS_ARGS("b", interactive
),
3550 SD_BUS_VTABLE_UNPRIVILEGED
),
3551 SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags",
3552 SD_BUS_ARGS("t", flags
),
3555 SD_BUS_VTABLE_UNPRIVILEGED
),
3556 SD_BUS_METHOD_WITH_ARGS("HybridSleep",
3557 SD_BUS_ARGS("b", interactive
),
3559 method_hybrid_sleep
,
3560 SD_BUS_VTABLE_UNPRIVILEGED
),
3561 SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags",
3562 SD_BUS_ARGS("t", flags
),
3564 method_hybrid_sleep
,
3565 SD_BUS_VTABLE_UNPRIVILEGED
),
3566 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate",
3567 SD_BUS_ARGS("b", interactive
),
3569 method_suspend_then_hibernate
,
3570 SD_BUS_VTABLE_UNPRIVILEGED
),
3571 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags",
3572 SD_BUS_ARGS("t", flags
),
3574 method_suspend_then_hibernate
,
3575 SD_BUS_VTABLE_UNPRIVILEGED
),
3576 SD_BUS_METHOD_WITH_ARGS("CanPowerOff",
3578 SD_BUS_RESULT("s", result
),
3579 method_can_poweroff
,
3580 SD_BUS_VTABLE_UNPRIVILEGED
),
3581 SD_BUS_METHOD_WITH_ARGS("CanReboot",
3583 SD_BUS_RESULT("s", result
),
3585 SD_BUS_VTABLE_UNPRIVILEGED
),
3586 SD_BUS_METHOD_WITH_ARGS("CanHalt",
3588 SD_BUS_RESULT("s", result
),
3590 SD_BUS_VTABLE_UNPRIVILEGED
),
3591 SD_BUS_METHOD_WITH_ARGS("CanSuspend",
3593 SD_BUS_RESULT("s", result
),
3595 SD_BUS_VTABLE_UNPRIVILEGED
),
3596 SD_BUS_METHOD_WITH_ARGS("CanHibernate",
3598 SD_BUS_RESULT("s", result
),
3599 method_can_hibernate
,
3600 SD_BUS_VTABLE_UNPRIVILEGED
),
3601 SD_BUS_METHOD_WITH_ARGS("CanHybridSleep",
3603 SD_BUS_RESULT("s", result
),
3604 method_can_hybrid_sleep
,
3605 SD_BUS_VTABLE_UNPRIVILEGED
),
3606 SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate",
3608 SD_BUS_RESULT("s", result
),
3609 method_can_suspend_then_hibernate
,
3610 SD_BUS_VTABLE_UNPRIVILEGED
),
3611 SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown",
3612 SD_BUS_ARGS("s", type
, "t", usec
),
3614 method_schedule_shutdown
,
3615 SD_BUS_VTABLE_UNPRIVILEGED
),
3616 SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown",
3618 SD_BUS_RESULT("b", cancelled
),
3619 method_cancel_scheduled_shutdown
,
3620 SD_BUS_VTABLE_UNPRIVILEGED
),
3621 SD_BUS_METHOD_WITH_ARGS("Inhibit",
3622 SD_BUS_ARGS("s", what
, "s", who
, "s", why
, "s", mode
),
3623 SD_BUS_RESULT("h", pipe_fd
),
3625 SD_BUS_VTABLE_UNPRIVILEGED
),
3626 SD_BUS_METHOD_WITH_ARGS("CanRebootParameter",
3628 SD_BUS_RESULT("s", result
),
3629 method_can_reboot_parameter
,
3630 SD_BUS_VTABLE_UNPRIVILEGED
),
3631 SD_BUS_METHOD_WITH_ARGS("SetRebootParameter",
3632 SD_BUS_ARGS("s", parameter
),
3634 method_set_reboot_parameter
,
3635 SD_BUS_VTABLE_UNPRIVILEGED
),
3636 SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup",
3638 SD_BUS_RESULT("s", result
),
3639 method_can_reboot_to_firmware_setup
,
3640 SD_BUS_VTABLE_UNPRIVILEGED
),
3641 SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup",
3642 SD_BUS_ARGS("b", enable
),
3644 method_set_reboot_to_firmware_setup
,
3645 SD_BUS_VTABLE_UNPRIVILEGED
),
3646 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu",
3648 SD_BUS_RESULT("s", result
),
3649 method_can_reboot_to_boot_loader_menu
,
3650 SD_BUS_VTABLE_UNPRIVILEGED
),
3651 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu",
3652 SD_BUS_ARGS("t", timeout
),
3654 method_set_reboot_to_boot_loader_menu
,
3655 SD_BUS_VTABLE_UNPRIVILEGED
),
3656 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry",
3658 SD_BUS_RESULT("s", result
),
3659 method_can_reboot_to_boot_loader_entry
,
3660 SD_BUS_VTABLE_UNPRIVILEGED
),
3661 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry",
3662 SD_BUS_ARGS("s", boot_loader_entry
),
3664 method_set_reboot_to_boot_loader_entry
,
3665 SD_BUS_VTABLE_UNPRIVILEGED
),
3666 SD_BUS_METHOD_WITH_ARGS("SetWallMessage",
3667 SD_BUS_ARGS("s", wall_message
, "b", enable
),
3669 method_set_wall_message
,
3670 SD_BUS_VTABLE_UNPRIVILEGED
),
3672 SD_BUS_SIGNAL_WITH_ARGS("SessionNew",
3673 SD_BUS_ARGS("s", session_id
, "o", object_path
),
3675 SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved",
3676 SD_BUS_ARGS("s", session_id
, "o", object_path
),
3678 SD_BUS_SIGNAL_WITH_ARGS("UserNew",
3679 SD_BUS_ARGS("u", uid
, "o", object_path
),
3681 SD_BUS_SIGNAL_WITH_ARGS("UserRemoved",
3682 SD_BUS_ARGS("u", uid
, "o", object_path
),
3684 SD_BUS_SIGNAL_WITH_ARGS("SeatNew",
3685 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
3687 SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved",
3688 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
3690 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown",
3691 SD_BUS_ARGS("b", start
),
3693 SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep",
3694 SD_BUS_ARGS("b", start
),
3700 const BusObjectImplementation manager_object
= {
3701 "/org/freedesktop/login1",
3702 "org.freedesktop.login1.Manager",
3703 .vtables
= BUS_VTABLES(manager_vtable
),
3704 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
3709 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
3716 if (result
&& !streq(result
, "done")) {
3717 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3719 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3720 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
3721 return session_send_create_reply(s
, &e
);
3724 return session_send_create_reply(s
, NULL
);
3727 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3728 const char *path
, *result
, *unit
;
3729 Manager
*m
= ASSERT_PTR(userdata
);
3737 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3739 bus_log_parse_error(r
);
3743 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3744 assert(m
->delayed_action
);
3745 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->delayed_action
->inhibit_what
));
3747 /* Tell people that they now may take a lock again */
3748 (void) send_prepare_for(m
, m
->delayed_action
->inhibit_what
, false);
3750 m
->action_job
= mfree(m
->action_job
);
3751 m
->delayed_action
= NULL
;
3755 session
= hashmap_get(m
->session_units
, unit
);
3757 if (streq_ptr(path
, session
->scope_job
)) {
3758 session
->scope_job
= mfree(session
->scope_job
);
3759 (void) session_jobs_reply(session
, id
, unit
, result
);
3761 session_save(session
);
3762 user_save(session
->user
);
3765 session_add_to_gc_queue(session
);
3768 user
= hashmap_get(m
->user_units
, unit
);
3770 if (streq_ptr(path
, user
->service_job
)) {
3771 user
->service_job
= mfree(user
->service_job
);
3773 LIST_FOREACH(sessions_by_user
, s
, user
->sessions
)
3774 (void) session_jobs_reply(s
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
3779 user_add_to_gc_queue(user
);
3785 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3786 const char *path
, *unit
;
3787 Manager
*m
= ASSERT_PTR(userdata
);
3794 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3796 bus_log_parse_error(r
);
3800 session
= hashmap_get(m
->session_units
, unit
);
3802 session_add_to_gc_queue(session
);
3804 user
= hashmap_get(m
->user_units
, unit
);
3806 user_add_to_gc_queue(user
);
3811 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3812 _cleanup_free_
char *unit
= NULL
;
3813 Manager
*m
= ASSERT_PTR(userdata
);
3821 path
= sd_bus_message_get_path(message
);
3825 r
= unit_name_from_dbus_path(path
, &unit
);
3826 if (r
== -EINVAL
) /* not a unit */
3833 session
= hashmap_get(m
->session_units
, unit
);
3835 session_add_to_gc_queue(session
);
3837 user
= hashmap_get(m
->user_units
, unit
);
3839 user_add_to_gc_queue(user
);
3844 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3845 Manager
*m
= ASSERT_PTR(userdata
);
3851 r
= sd_bus_message_read(message
, "b", &b
);
3853 bus_log_parse_error(r
);
3860 /* systemd finished reloading, let's recheck all our sessions */
3861 log_debug("System manager has been reloaded, rechecking sessions...");
3863 HASHMAP_FOREACH(session
, m
->sessions
)
3864 session_add_to_gc_queue(session
);
3869 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3874 l
= strv_from_stdarg_alloca(property
);
3876 return sd_bus_emit_properties_changed_strv(
3878 "/org/freedesktop/login1",
3879 "org.freedesktop.login1.Manager",
3883 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3888 r
= sd_bus_message_read(reply
, "o", &j
);
3900 int manager_start_scope(
3905 const char *description
,
3908 const char *requires_mounts_for
,
3909 sd_bus_message
*more_properties
,
3910 sd_bus_error
*error
,
3913 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3921 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
3925 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3929 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3933 if (!isempty(slice
)) {
3934 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3939 if (!isempty(description
)) {
3940 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
3945 STRV_FOREACH(i
, wants
) {
3946 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
3951 STRV_FOREACH(i
, after
) {
3952 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3957 if (!empty_or_root(requires_mounts_for
)) {
3958 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3963 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3965 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3969 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3973 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3974 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_MAX
);
3976 return bus_log_create_error(r
);
3978 if (more_properties
) {
3979 /* If TasksMax also appears here, it will overwrite the default value set above */
3980 r
= sd_bus_message_copy(m
, more_properties
, true);
3985 r
= sd_bus_message_close_container(m
);
3989 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3993 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3997 return strdup_job(reply
, job
);
4000 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4001 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4008 r
= bus_call_method(
4014 "ss", unit
, "replace");
4018 return strdup_job(reply
, job
);
4021 int manager_stop_unit(Manager
*manager
, const char *unit
, const char *job_mode
, sd_bus_error
*error
, char **ret_job
) {
4022 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4029 r
= bus_call_method(
4035 "ss", unit
, job_mode
?: "fail");
4037 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
4038 BUS_ERROR_LOAD_FAILED
)) {
4041 sd_bus_error_free(error
);
4048 return strdup_job(reply
, ret_job
);
4051 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4052 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4053 _cleanup_free_
char *path
= NULL
;
4059 path
= unit_dbus_path_from_name(scope
);
4063 r
= sd_bus_call_method(
4065 "org.freedesktop.systemd1",
4067 "org.freedesktop.systemd1.Scope",
4073 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4074 BUS_ERROR_LOAD_FAILED
,
4075 BUS_ERROR_SCOPE_NOT_RUNNING
))
4078 sd_bus_error_move(ret_error
, &error
);
4085 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4089 return bus_call_method(
4095 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4098 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4099 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4100 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4101 _cleanup_free_
char *path
= NULL
;
4108 path
= unit_dbus_path_from_name(unit
);
4112 r
= sd_bus_get_property(
4114 "org.freedesktop.systemd1",
4116 "org.freedesktop.systemd1.Unit",
4122 /* systemd might have dropped off momentarily, let's
4123 * not make this an error */
4124 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4125 SD_BUS_ERROR_DISCONNECTED
))
4128 /* If the unit is already unloaded then it's not
4130 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4131 BUS_ERROR_LOAD_FAILED
))
4134 sd_bus_error_move(ret_error
, &error
);
4138 r
= sd_bus_message_read(reply
, "s", &state
);
4142 return !STR_IN_SET(state
, "inactive", "failed");
4145 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4146 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4147 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4153 r
= sd_bus_get_property(
4155 "org.freedesktop.systemd1",
4157 "org.freedesktop.systemd1.Job",
4163 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4164 SD_BUS_ERROR_DISCONNECTED
))
4167 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4170 sd_bus_error_move(ret_error
, &error
);
4174 /* We don't actually care about the state really. The fact
4175 * that we could read the job state is enough for us */