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-util.h"
51 #include "serialize.h"
52 #include "stdio-util.h"
54 #include "terminal-util.h"
55 #include "tmpfile-util.h"
56 #include "unit-name.h"
57 #include "user-util.h"
58 #include "utmp-wtmp.h"
62 /* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1))
63 * character limit for the wall message.
64 * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72
65 * There is no real technical need for that but doesn't make sense
66 * to store arbitrary amounts either. As we are not stingy here, we
69 #define WALL_MESSAGE_MAX 4096U
71 #define SHUTDOWN_SCHEDULE_FILE "/run/systemd/shutdown/scheduled"
73 static int update_schedule_file(Manager
*m
);
74 static void reset_scheduled_shutdown(Manager
*m
);
75 static int manager_setup_shutdown_timers(Manager
* m
);
77 static int get_sender_session(
79 sd_bus_message
*message
,
84 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
85 Session
*session
= NULL
;
89 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
90 * and returns that. If not and 'consult_display' is true, this returns the display session of the
91 * owning user of the caller. */
93 r
= sd_bus_query_sender_creds(message
,
94 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
95 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
99 r
= sd_bus_creds_get_session(creds
, &name
);
104 if (consult_display
) {
107 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
114 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
116 session
= user
->display
;
120 session
= hashmap_get(m
->sessions
, name
);
123 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
125 "Caller does not belong to any known session and doesn't own any suitable session." :
126 "Caller does not belong to any known session.");
132 int manager_get_session_from_creds(
134 sd_bus_message
*message
,
144 if (SEAT_IS_SELF(name
)) /* the caller's own session */
145 return get_sender_session(m
, message
, false, error
, ret
);
146 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
147 return get_sender_session(m
, message
, true, error
, ret
);
149 session
= hashmap_get(m
->sessions
, name
);
151 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
157 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
158 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
163 /* Note that we get the owner UID of the session, not the actual client UID here! */
164 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
168 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
175 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
178 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
179 "Caller does not belong to any logged in or lingering user");
185 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
191 if (!uid_is_valid(uid
))
192 return get_sender_user(m
, message
, error
, ret
);
194 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
196 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
197 "User ID "UID_FMT
" is not logged in or lingering", uid
);
203 int manager_get_seat_from_creds(
205 sd_bus_message
*message
,
216 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
219 /* Use these special seat names as session names */
220 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
224 seat
= session
->seat
;
226 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
228 seat
= hashmap_get(m
->seats
, name
);
230 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
237 static int return_test_polkit(
238 sd_bus_message
*message
,
241 const char **details
,
249 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
256 result
= "challenge";
260 return sd_bus_reply_method_return(message
, "s", result
);
263 static int property_get_idle_hint(
266 const char *interface
,
267 const char *property
,
268 sd_bus_message
*reply
,
270 sd_bus_error
*error
) {
272 Manager
*m
= ASSERT_PTR(userdata
);
277 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
280 static int property_get_idle_since_hint(
283 const char *interface
,
284 const char *property
,
285 sd_bus_message
*reply
,
287 sd_bus_error
*error
) {
289 Manager
*m
= ASSERT_PTR(userdata
);
290 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
295 manager_get_idle_hint(m
, &t
);
297 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
300 static int property_get_inhibited(
303 const char *interface
,
304 const char *property
,
305 sd_bus_message
*reply
,
307 sd_bus_error
*error
) {
309 Manager
*m
= ASSERT_PTR(userdata
);
315 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
317 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
320 static int property_get_preparing(
323 const char *interface
,
324 const char *property
,
325 sd_bus_message
*reply
,
327 sd_bus_error
*error
) {
329 Manager
*m
= ASSERT_PTR(userdata
);
335 if (m
->delayed_action
) {
336 if (streq(property
, "PreparingForShutdown"))
337 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SHUTDOWN
;
339 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SLEEP
;
342 return sd_bus_message_append(reply
, "b", b
);
345 static int property_get_scheduled_shutdown(
348 const char *interface
,
349 const char *property
,
350 sd_bus_message
*reply
,
352 sd_bus_error
*error
) {
354 Manager
*m
= ASSERT_PTR(userdata
);
360 r
= sd_bus_message_open_container(reply
, 'r', "st");
364 r
= sd_bus_message_append(reply
, "st",
365 m
->scheduled_shutdown_action
? handle_action_to_string(m
->scheduled_shutdown_action
->handle
) : NULL
,
366 m
->scheduled_shutdown_timeout
);
370 return sd_bus_message_close_container(reply
);
373 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
374 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
375 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
376 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power());
377 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
378 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
380 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
381 _cleanup_free_
char *p
= NULL
;
382 Manager
*m
= ASSERT_PTR(userdata
);
389 r
= sd_bus_message_read(message
, "s", &name
);
393 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
397 p
= session_bus_path(session
);
401 return sd_bus_reply_method_return(message
, "o", p
);
404 /* Get login session of a process. This is not what you are looking for these days,
405 * as apps may instead belong to a user service unit. This includes terminal
406 * emulators and hence command-line apps. */
407 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
408 _cleanup_free_
char *p
= NULL
;
409 Session
*session
= NULL
;
410 Manager
*m
= ASSERT_PTR(userdata
);
416 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
418 r
= sd_bus_message_read(message
, "u", &pid
);
425 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
429 r
= manager_get_session_by_pid(m
, pid
, &session
);
434 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
435 "PID "PID_FMT
" does not belong to any known session", pid
);
438 p
= session_bus_path(session
);
442 return sd_bus_reply_method_return(message
, "o", p
);
445 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
446 _cleanup_free_
char *p
= NULL
;
447 Manager
*m
= ASSERT_PTR(userdata
);
454 r
= sd_bus_message_read(message
, "u", &uid
);
458 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
462 p
= user_bus_path(user
);
466 return sd_bus_reply_method_return(message
, "o", p
);
469 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
470 _cleanup_free_
char *p
= NULL
;
471 Manager
*m
= ASSERT_PTR(userdata
);
478 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
480 r
= sd_bus_message_read(message
, "u", &pid
);
487 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
491 r
= manager_get_user_by_pid(m
, pid
, &user
);
495 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
496 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
500 p
= user_bus_path(user
);
504 return sd_bus_reply_method_return(message
, "o", p
);
507 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
508 _cleanup_free_
char *p
= NULL
;
509 Manager
*m
= ASSERT_PTR(userdata
);
516 r
= sd_bus_message_read(message
, "s", &name
);
520 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
524 p
= seat_bus_path(seat
);
528 return sd_bus_reply_method_return(message
, "o", p
);
531 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
532 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
533 Manager
*m
= ASSERT_PTR(userdata
);
539 r
= sd_bus_message_new_method_return(message
, &reply
);
543 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
547 HASHMAP_FOREACH(session
, m
->sessions
) {
548 _cleanup_free_
char *p
= NULL
;
550 p
= session_bus_path(session
);
554 r
= sd_bus_message_append(reply
, "(susso)",
556 (uint32_t) session
->user
->user_record
->uid
,
557 session
->user
->user_record
->user_name
,
558 session
->seat
? session
->seat
->id
: "",
564 r
= sd_bus_message_close_container(reply
);
568 return sd_bus_send(NULL
, reply
, NULL
);
571 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
572 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
573 Manager
*m
= ASSERT_PTR(userdata
);
579 r
= sd_bus_message_new_method_return(message
, &reply
);
583 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
587 HASHMAP_FOREACH(user
, m
->users
) {
588 _cleanup_free_
char *p
= NULL
;
590 p
= user_bus_path(user
);
594 r
= sd_bus_message_append(reply
, "(uso)",
595 (uint32_t) user
->user_record
->uid
,
596 user
->user_record
->user_name
,
602 r
= sd_bus_message_close_container(reply
);
606 return sd_bus_send(NULL
, reply
, NULL
);
609 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
610 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
611 Manager
*m
= ASSERT_PTR(userdata
);
617 r
= sd_bus_message_new_method_return(message
, &reply
);
621 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
625 HASHMAP_FOREACH(seat
, m
->seats
) {
626 _cleanup_free_
char *p
= NULL
;
628 p
= seat_bus_path(seat
);
632 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
637 r
= sd_bus_message_close_container(reply
);
641 return sd_bus_send(NULL
, reply
, NULL
);
644 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
645 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
646 Manager
*m
= ASSERT_PTR(userdata
);
647 Inhibitor
*inhibitor
;
652 r
= sd_bus_message_new_method_return(message
, &reply
);
656 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
660 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
) {
662 r
= sd_bus_message_append(reply
, "(ssssuu)",
663 strempty(inhibit_what_to_string(inhibitor
->what
)),
664 strempty(inhibitor
->who
),
665 strempty(inhibitor
->why
),
666 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
667 (uint32_t) inhibitor
->uid
,
668 (uint32_t) inhibitor
->pid
.pid
);
673 r
= sd_bus_message_close_container(reply
);
677 return sd_bus_send(NULL
, reply
, NULL
);
680 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
681 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
682 _cleanup_free_
char *id
= NULL
;
683 Session
*session
= NULL
;
684 uint32_t audit_id
= 0;
685 Manager
*m
= ASSERT_PTR(userdata
);
698 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
699 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
701 r
= sd_bus_message_read(message
, "uusssssussbss",
702 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
703 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
707 if (!uid_is_valid(uid
))
708 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
709 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
710 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
713 t
= _SESSION_TYPE_INVALID
;
715 t
= session_type_from_string(type
);
717 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
718 "Invalid session type %s", type
);
722 c
= _SESSION_CLASS_INVALID
;
724 c
= session_class_from_string(class);
726 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
727 "Invalid session class %s", class);
730 if (isempty(desktop
))
733 if (!string_is_safe(desktop
))
734 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
735 "Invalid desktop string %s", desktop
);
741 seat
= hashmap_get(m
->seats
, cseat
);
743 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
744 "No seat '%s' known", cseat
);
747 if (tty_is_vc(tty
)) {
752 else if (seat
!= m
->seat0
)
753 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
754 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
756 v
= vtnr_from_tty(tty
);
758 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
759 "Cannot determine VT number from virtual console TTY %s", tty
);
763 else if (vtnr
!= (uint32_t) v
)
764 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
765 "Specified TTY and VT number do not match");
767 } else if (tty_is_console(tty
)) {
771 else if (seat
!= m
->seat0
)
772 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
773 "Console TTY specified but seat is not seat0");
776 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
777 "Console TTY specified but VT number is not 0");
781 if (seat_has_vts(seat
)) {
782 if (vtnr
<= 0 || vtnr
> 63)
783 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
784 "VT number out of range");
787 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
788 "Seat has no VTs but VT number not 0");
792 if (t
== _SESSION_TYPE_INVALID
) {
793 if (!isempty(display
))
795 else if (!isempty(tty
))
798 t
= SESSION_UNSPECIFIED
;
801 if (c
== _SESSION_CLASS_INVALID
) {
802 if (t
== SESSION_UNSPECIFIED
)
803 c
= SESSION_BACKGROUND
;
809 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
811 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
815 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
820 /* Check if we are already in a logind session. Or if we are in user@.service
821 * which is a special PAM session that avoids creating a logind session. */
822 r
= manager_get_user_by_pid(m
, leader
, NULL
);
826 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
827 "Already running in a session or user slice");
830 * Old gdm and lightdm start the user-session on the same VT as
831 * the greeter session. But they destroy the greeter session
832 * after the user-session and want the user-session to take
833 * over the VT. We need to support this for
834 * backwards-compatibility, so make sure we allow new sessions
835 * on a VT that a greeter is running on. Furthermore, to allow
836 * re-logins, we have to allow a greeter to take over a used VT for
837 * the exact same reasons.
839 if (c
!= SESSION_GREETER
&&
841 vtnr
< MALLOC_ELEMENTSOF(m
->seat0
->positions
) &&
842 m
->seat0
->positions
[vtnr
] &&
843 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
844 return sd_bus_error_set(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
846 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
847 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
848 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
851 (void) audit_session_from_pid(leader
, &audit_id
);
852 if (audit_session_is_valid(audit_id
)) {
853 /* Keep our session IDs and the audit session IDs in sync */
855 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
858 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
859 * not trust the audit data and let's better register a new ID */
860 if (hashmap_contains(m
->sessions
, id
)) {
861 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
862 audit_id
= AUDIT_SESSION_INVALID
;
871 if (asprintf(&id
, "c%" PRIu64
, ++m
->session_counter
) < 0)
874 } while (hashmap_contains(m
->sessions
, id
));
877 /* The generated names should not clash with 'auto' or 'self' */
878 assert(!SESSION_IS_SELF(id
));
879 assert(!SESSION_IS_AUTO(id
));
881 /* If we are not watching utmp already, try again */
882 manager_reconnect_utmp(m
);
884 r
= manager_add_user_by_uid(m
, uid
, &user
);
888 r
= manager_add_session(m
, id
, &session
);
892 session_set_user(session
, user
);
893 r
= session_set_leader(session
, leader
);
897 session
->original_type
= session
->type
= t
;
899 session
->remote
= remote
;
900 session
->vtnr
= vtnr
;
903 session
->tty
= strdup(tty
);
909 session
->tty_validity
= TTY_FROM_PAM
;
912 if (!isempty(display
)) {
913 session
->display
= strdup(display
);
914 if (!session
->display
) {
920 if (!isempty(remote_user
)) {
921 session
->remote_user
= strdup(remote_user
);
922 if (!session
->remote_user
) {
928 if (!isempty(remote_host
)) {
929 session
->remote_host
= strdup(remote_host
);
930 if (!session
->remote_host
) {
936 if (!isempty(service
)) {
937 session
->service
= strdup(service
);
938 if (!session
->service
) {
944 if (!isempty(desktop
)) {
945 session
->desktop
= strdup(desktop
);
946 if (!session
->desktop
) {
953 r
= seat_attach_session(seat
, session
);
958 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
962 r
= session_start(session
, message
, error
);
966 r
= sd_bus_message_exit_container(message
);
970 session
->create_message
= sd_bus_message_ref(message
);
972 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
973 * session_send_create_reply(). */
979 session_add_to_gc_queue(session
);
982 user_add_to_gc_queue(user
);
987 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
988 Manager
*m
= ASSERT_PTR(userdata
);
995 r
= sd_bus_message_read(message
, "s", &name
);
999 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1003 r
= session_release(session
);
1007 return sd_bus_reply_method_return(message
, NULL
);
1010 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1011 Manager
*m
= ASSERT_PTR(userdata
);
1018 r
= sd_bus_message_read(message
, "s", &name
);
1022 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1026 /* PolicyKit is done by bus_session_method_activate() */
1028 return bus_session_method_activate(message
, session
, error
);
1031 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1032 const char *session_name
, *seat_name
;
1033 Manager
*m
= ASSERT_PTR(userdata
);
1040 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1042 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1046 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1050 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1054 if (session
->seat
!= seat
)
1055 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1056 "Session %s not on seat %s", session_name
, seat_name
);
1058 r
= check_polkit_chvt(message
, m
, error
);
1062 return 1; /* Will call us back */
1064 r
= session_activate(session
);
1068 return sd_bus_reply_method_return(message
, NULL
);
1071 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1072 Manager
*m
= ASSERT_PTR(userdata
);
1079 r
= sd_bus_message_read(message
, "s", &name
);
1083 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1087 return bus_session_method_lock(message
, session
, error
);
1090 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1091 Manager
*m
= ASSERT_PTR(userdata
);
1096 r
= bus_verify_polkit_async(
1099 "org.freedesktop.login1.lock-sessions",
1103 &m
->polkit_registry
,
1108 return 1; /* Will call us back */
1110 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1114 return sd_bus_reply_method_return(message
, NULL
);
1117 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1119 Manager
*m
= ASSERT_PTR(userdata
);
1125 r
= sd_bus_message_read(message
, "s", &name
);
1129 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1133 return bus_session_method_kill(message
, session
, error
);
1136 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1137 Manager
*m
= ASSERT_PTR(userdata
);
1144 r
= sd_bus_message_read(message
, "u", &uid
);
1148 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1152 return bus_user_method_kill(message
, user
, error
);
1155 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1156 Manager
*m
= ASSERT_PTR(userdata
);
1163 r
= sd_bus_message_read(message
, "s", &name
);
1167 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1171 return bus_session_method_terminate(message
, session
, error
);
1174 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1175 Manager
*m
= ASSERT_PTR(userdata
);
1182 r
= sd_bus_message_read(message
, "u", &uid
);
1186 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1190 return bus_user_method_terminate(message
, user
, error
);
1193 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1194 Manager
*m
= ASSERT_PTR(userdata
);
1201 r
= sd_bus_message_read(message
, "s", &name
);
1205 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1209 return bus_seat_method_terminate(message
, seat
, error
);
1212 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1213 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1214 _cleanup_free_
char *cc
= NULL
;
1215 Manager
*m
= ASSERT_PTR(userdata
);
1216 int r
, b
, interactive
;
1219 uint32_t uid
, auth_uid
;
1223 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1227 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1228 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1232 if (!uid_is_valid(uid
)) {
1233 /* Note that we get the owner UID of the session or user unit,
1234 * not the actual client UID here! */
1235 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1240 /* owner_uid is racy, so for authorization we must use euid */
1241 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1248 return errno_or_else(ENOENT
);
1250 r
= bus_verify_polkit_async(
1253 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1254 "org.freedesktop.login1.set-user-linger",
1258 &m
->polkit_registry
,
1263 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1265 (void) mkdir_p_label("/var/lib/systemd", 0755);
1266 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1270 cc
= cescape(pw
->pw_name
);
1274 path
= strjoina("/var/lib/systemd/linger/", cc
);
1282 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1289 if (r
< 0 && errno
!= ENOENT
)
1292 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1294 user_add_to_gc_queue(u
);
1297 return sd_bus_reply_method_return(message
, NULL
);
1300 static int trigger_device(Manager
*m
, sd_device
*parent
) {
1301 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1306 r
= sd_device_enumerator_new(&e
);
1310 r
= sd_device_enumerator_allow_uninitialized(e
);
1315 r
= sd_device_enumerator_add_match_parent(e
, parent
);
1320 FOREACH_DEVICE(e
, d
) {
1321 r
= sd_device_trigger(d
, SD_DEVICE_CHANGE
);
1323 log_device_debug_errno(d
, r
, "Failed to trigger device, ignoring: %m");
1329 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
, sd_bus_error
*error
) {
1330 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1331 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1332 const char *id_for_seat
;
1339 r
= sd_device_new_from_syspath(&d
, sysfs
);
1341 return sd_bus_error_set_errnof(error
, r
, "Failed to open device '%s': %m", sysfs
);
1343 if (sd_device_has_current_tag(d
, "seat") <= 0)
1344 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'seat' udev tag.", sysfs
);
1346 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1347 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'ID_FOR_SEAT' udev property.", sysfs
);
1349 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1352 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1355 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1356 r
= write_string_file_atomic_label(file
, rule
);
1360 return trigger_device(m
, d
);
1363 static int flush_devices(Manager
*m
) {
1364 _cleanup_closedir_
DIR *d
= NULL
;
1368 d
= opendir("/etc/udev/rules.d");
1370 if (errno
!= ENOENT
)
1371 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1373 FOREACH_DIRENT_ALL(de
, d
, break) {
1374 if (!dirent_is_file(de
))
1377 if (!startswith(de
->d_name
, "72-seat-"))
1380 if (!endswith(de
->d_name
, ".rules"))
1383 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1384 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1387 return trigger_device(m
, NULL
);
1390 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1391 const char *sysfs
, *seat
;
1392 Manager
*m
= ASSERT_PTR(userdata
);
1397 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1401 if (!path_is_normalized(sysfs
))
1402 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1403 if (!path_startswith(sysfs
, "/sys"))
1404 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1406 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1409 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1415 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1416 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1418 r
= bus_verify_polkit_async(
1421 "org.freedesktop.login1.attach-device",
1425 &m
->polkit_registry
,
1430 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1432 r
= attach_device(m
, seat
, sysfs
, error
);
1436 return sd_bus_reply_method_return(message
, NULL
);
1439 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1440 Manager
*m
= ASSERT_PTR(userdata
);
1445 r
= sd_bus_message_read(message
, "b", &interactive
);
1449 r
= bus_verify_polkit_async(
1452 "org.freedesktop.login1.flush-devices",
1456 &m
->polkit_registry
,
1461 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1463 r
= flush_devices(m
);
1467 return sd_bus_reply_method_return(message
, NULL
);
1470 static int have_multiple_sessions(
1478 /* Check for other users' sessions. Greeter sessions do not
1479 * count, and non-login sessions do not count either. */
1480 HASHMAP_FOREACH(session
, m
->sessions
)
1481 if (session
->class == SESSION_USER
&&
1482 session
->user
->user_record
->uid
!= uid
)
1488 static int bus_manager_log_shutdown(
1490 const HandleActionData
*a
) {
1494 const char *message
= a
->message
?: "System is shutting down";
1495 const char *log_verb
= a
->log_verb
? strjoina("SHUTDOWN=", a
->log_verb
) : NULL
;
1497 return log_struct(LOG_NOTICE
,
1498 "MESSAGE_ID=%s", a
->message_id
?: SD_MESSAGE_SHUTDOWN_STR
,
1499 LOG_MESSAGE("%s%s%s%s.",
1501 m
->wall_message
? " (" : "",
1502 strempty(m
->wall_message
),
1503 m
->wall_message
? ")" : ""),
1507 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1508 Manager
*m
= ASSERT_PTR(userdata
);
1512 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1516 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1521 if (until
<= now(CLOCK_MONOTONIC
))
1524 /* We want to ignore the lid switch for a while after each
1525 * suspend, and after boot-up. Hence let's install a timer for
1526 * this. As long as the event source exists we ignore the lid
1529 if (m
->lid_switch_ignore_event_source
) {
1532 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1539 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1541 r
= sd_event_add_time(
1543 &m
->lid_switch_ignore_event_source
,
1546 lid_switch_ignore_handler
, m
);
1551 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1552 int active
= _active
;
1555 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1557 return sd_bus_emit_signal(m
->bus
,
1558 "/org/freedesktop/login1",
1559 "org.freedesktop.login1.Manager",
1560 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1565 static int execute_shutdown_or_sleep(
1567 const HandleActionData
*a
,
1568 sd_bus_error
*error
) {
1570 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1577 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
)
1578 bus_manager_log_shutdown(m
, a
);
1580 r
= bus_call_method(
1586 "ss", a
->target
, "replace-irreversibly");
1590 r
= sd_bus_message_read(reply
, "o", &p
);
1594 r
= free_and_strdup(&m
->action_job
, p
);
1598 m
->delayed_action
= a
;
1600 /* Make sure the lid switch is ignored for a while */
1601 manager_set_lid_switch_ignore(m
, usec_add(now(CLOCK_MONOTONIC
), m
->holdoff_timeout_usec
));
1606 /* Tell people that they now may take a lock again */
1607 (void) send_prepare_for(m
, a
->inhibit_what
, false);
1612 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1613 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1614 Inhibitor
*offending
= NULL
;
1619 if (!manager
->delayed_action
|| manager
->action_job
)
1622 if (manager_is_inhibited(manager
, manager
->delayed_action
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1623 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1628 (void) get_process_comm(offending
->pid
.pid
, &comm
);
1629 u
= uid_to_name(offending
->uid
);
1631 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1632 offending
->uid
, strna(u
),
1633 offending
->pid
.pid
, strna(comm
));
1636 /* Actually do the operation */
1637 r
= execute_shutdown_or_sleep(manager
, manager
->delayed_action
, &error
);
1639 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1640 bus_error_message(&error
, r
));
1642 manager
->delayed_action
= NULL
;
1645 return 1; /* We did some work. */
1648 static int manager_inhibit_timeout_handler(
1653 Manager
*manager
= ASSERT_PTR(userdata
);
1655 assert(manager
->inhibit_timeout_source
== s
);
1657 return manager_dispatch_delayed(manager
, true);
1660 static int delay_shutdown_or_sleep(
1662 const HandleActionData
*a
) {
1669 if (m
->inhibit_timeout_source
) {
1670 r
= sd_event_source_set_time_relative(m
->inhibit_timeout_source
, m
->inhibit_delay_max
);
1672 return log_error_errno(r
, "sd_event_source_set_time_relative() failed: %m");
1674 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1676 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1678 r
= sd_event_add_time_relative(
1680 &m
->inhibit_timeout_source
,
1681 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, 0,
1682 manager_inhibit_timeout_handler
, m
);
1687 m
->delayed_action
= a
;
1692 int bus_manager_shutdown_or_sleep_now_or_later(
1694 const HandleActionData
*a
,
1695 sd_bus_error
*error
) {
1697 _cleanup_free_
char *load_state
= NULL
;
1703 assert(!m
->action_job
);
1705 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
1709 if (!streq(load_state
, "loaded"))
1710 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1711 "Unit %s is %s, refusing operation.",
1712 a
->target
, load_state
);
1714 /* Tell everybody to prepare for shutdown/sleep */
1715 (void) send_prepare_for(m
, a
->inhibit_what
, true);
1718 m
->inhibit_delay_max
> 0 &&
1719 manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1722 /* Shutdown is delayed, keep in mind what we
1723 * want to do, and start a timeout */
1724 r
= delay_shutdown_or_sleep(m
, a
);
1726 /* Shutdown is not delayed, execute it
1728 r
= execute_shutdown_or_sleep(m
, a
, error
);
1733 static int verify_shutdown_creds(
1735 sd_bus_message
*message
,
1736 const HandleActionData
*a
,
1738 sd_bus_error
*error
) {
1740 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1741 bool multiple_sessions
, blocked
, interactive
;
1749 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1753 r
= sd_bus_creds_get_euid(creds
, &uid
);
1757 r
= have_multiple_sessions(m
, uid
);
1761 multiple_sessions
= r
> 0;
1762 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1763 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
1765 if (multiple_sessions
) {
1766 r
= bus_verify_polkit_async(
1769 a
->polkit_action_multiple_sessions
,
1773 &m
->polkit_registry
,
1778 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1782 /* We don't check polkit for root here, because you can't be more privileged than root */
1783 if (uid
== 0 && (flags
& SD_LOGIND_ROOT_CHECK_INHIBITORS
))
1784 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
,
1785 "Access denied to root due to active block inhibitor");
1787 r
= bus_verify_polkit_async(message
,
1789 a
->polkit_action_ignore_inhibit
,
1793 &m
->polkit_registry
,
1798 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1801 if (!multiple_sessions
&& !blocked
) {
1802 r
= bus_verify_polkit_async(message
,
1808 &m
->polkit_registry
,
1813 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1819 static int setup_wall_message_timer(Manager
*m
, sd_bus_message
* message
) {
1820 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1823 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
1825 const char *tty
= NULL
;
1827 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
1828 (void) sd_bus_creds_get_tty(creds
, &tty
);
1830 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
1835 r
= manager_setup_wall_message_timer(m
);
1842 static int method_do_shutdown_or_sleep(
1844 sd_bus_message
*message
,
1845 const HandleActionData
*a
,
1847 sd_bus_error
*error
) {
1857 /* New style method: with flags parameter (and interactive bool in the bus message header) */
1858 r
= sd_bus_message_read(message
, "t", &flags
);
1861 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
1862 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
1863 "Invalid flags parameter");
1865 if (FLAGS_SET(flags
, (SD_LOGIND_REBOOT_VIA_KEXEC
|SD_LOGIND_SOFT_REBOOT
)))
1866 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
1867 "Both reboot via kexec and soft reboot selected, which is not supported");
1869 if (a
->handle
!= HANDLE_REBOOT
) {
1870 if (flags
& SD_LOGIND_REBOOT_VIA_KEXEC
)
1871 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
1872 "Reboot via kexec option is only applicable with reboot operations");
1873 if (flags
& SD_LOGIND_SOFT_REBOOT
)
1874 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
1875 "Soft reboot option is only applicable with reboot operations");
1878 /* Old style method: no flags parameter, but interactive bool passed as boolean in
1879 * payload. Let's convert this argument to the new-style flags parameter for our internal
1883 r
= sd_bus_message_read(message
, "b", &interactive
);
1887 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
1890 if ((flags
& SD_LOGIND_REBOOT_VIA_KEXEC
) && kexec_loaded())
1891 a
= handle_action_lookup(HANDLE_KEXEC
);
1892 else if ((flags
& SD_LOGIND_SOFT_REBOOT
))
1893 a
= handle_action_lookup(HANDLE_SOFT_REBOOT
);
1895 /* Don't allow multiple jobs being executed at the same time */
1896 if (m
->delayed_action
)
1897 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1898 "There's already a shutdown or sleep operation in progress");
1900 if (a
->sleep_operation
>= 0) {
1901 r
= can_sleep(a
->sleep_operation
);
1903 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1904 "Not enough swap space for hibernation");
1906 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1907 "Sleep verb \"%s\" not supported",
1908 sleep_operation_to_string(a
->sleep_operation
));
1913 r
= verify_shutdown_creds(m
, message
, a
, flags
, error
);
1917 /* reset case we're shorting a scheduled shutdown */
1918 m
->unlink_nologin
= false;
1919 reset_scheduled_shutdown(m
);
1921 m
->scheduled_shutdown_timeout
= 0;
1922 m
->scheduled_shutdown_action
= a
;
1924 (void) setup_wall_message_timer(m
, message
);
1926 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, error
);
1930 return sd_bus_reply_method_return(message
, NULL
);
1933 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1934 Manager
*m
= userdata
;
1936 return method_do_shutdown_or_sleep(
1938 handle_action_lookup(HANDLE_POWEROFF
),
1939 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
1943 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1944 Manager
*m
= userdata
;
1946 return method_do_shutdown_or_sleep(
1948 handle_action_lookup(HANDLE_REBOOT
),
1949 sd_bus_message_is_method_call(message
, NULL
, "RebootWithFlags"),
1953 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1954 Manager
*m
= userdata
;
1956 return method_do_shutdown_or_sleep(
1958 handle_action_lookup(HANDLE_HALT
),
1959 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
1963 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1964 Manager
*m
= userdata
;
1966 return method_do_shutdown_or_sleep(
1968 handle_action_lookup(HANDLE_SUSPEND
),
1969 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
1973 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1974 Manager
*m
= userdata
;
1976 return method_do_shutdown_or_sleep(
1978 handle_action_lookup(HANDLE_HIBERNATE
),
1979 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
1983 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1984 Manager
*m
= userdata
;
1986 return method_do_shutdown_or_sleep(
1988 handle_action_lookup(HANDLE_HYBRID_SLEEP
),
1989 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
1993 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1994 Manager
*m
= userdata
;
1996 return method_do_shutdown_or_sleep(
1998 handle_action_lookup(HANDLE_SUSPEND_THEN_HIBERNATE
),
1999 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
2003 static int nologin_timeout_handler(
2008 Manager
*m
= userdata
;
2010 log_info("Creating /run/nologin, blocking further logins...");
2013 create_shutdown_run_nologin_or_warn() >= 0;
2018 static usec_t
nologin_timeout_usec(usec_t elapse
) {
2019 /* Issue /run/nologin five minutes before shutdown */
2020 return LESS_BY(elapse
, 5 * USEC_PER_MINUTE
);
2023 void manager_load_scheduled_shutdown(Manager
*m
) {
2024 _cleanup_fclose_
FILE *f
= NULL
;
2025 _cleanup_free_
char *usec
= NULL
,
2028 *wall_message
= NULL
,
2035 r
= parse_env_file(f
, SHUTDOWN_SCHEDULE_FILE
,
2037 "WARN_WALL", &warn_wall
,
2039 "WALL_MESSAGE", &wall_message
,
2043 /* reset will delete the file */
2044 reset_scheduled_shutdown(m
);
2049 return (void) log_debug_errno(r
, "Failed to parse " SHUTDOWN_SCHEDULE_FILE
": %m");
2051 HandleAction handle
= handle_action_from_string(mode
);
2053 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse scheduled shutdown type: %s", mode
);
2056 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "USEC is required");
2057 if (deserialize_usec(usec
, &m
->scheduled_shutdown_timeout
) < 0)
2060 /* assign parsed type only after we know usec is also valid */
2061 m
->scheduled_shutdown_action
= handle_action_lookup(handle
);
2064 r
= parse_boolean(warn_wall
);
2066 log_debug_errno(r
, "Failed to parse enabling wall messages");
2068 m
->enable_wall_messages
= r
;
2072 _cleanup_free_
char *unescaped
= NULL
;
2073 r
= cunescape(wall_message
, 0, &unescaped
);
2075 log_debug_errno(r
, "Failed to parse wall message: %s", wall_message
);
2077 free_and_replace(m
->wall_message
, unescaped
);
2081 r
= parse_uid(uid
, &m
->scheduled_shutdown_uid
);
2083 log_debug_errno(r
, "Failed to parse wall uid: %s", uid
);
2086 free_and_replace(m
->scheduled_shutdown_tty
, tty
);
2088 r
= manager_setup_shutdown_timers(m
);
2090 return reset_scheduled_shutdown(m
);
2092 (void) manager_setup_wall_message_timer(m
);
2093 (void) update_schedule_file(m
);
2098 static int update_schedule_file(Manager
*m
) {
2099 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
2100 _cleanup_fclose_
FILE *f
= NULL
;
2104 assert(m
->scheduled_shutdown_action
);
2106 r
= mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE
, 0755);
2108 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2110 r
= fopen_temporary(SHUTDOWN_SCHEDULE_FILE
, &f
, &temp_path
);
2112 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2114 (void) fchmod(fileno(f
), 0644);
2116 serialize_usec(f
, "USEC", m
->scheduled_shutdown_timeout
);
2117 serialize_item_format(f
, "WARN_WALL", "%s", one_zero(m
->enable_wall_messages
));
2118 serialize_item_format(f
, "MODE", "%s", handle_action_to_string(m
->scheduled_shutdown_action
->handle
));
2119 serialize_item_format(f
, "UID", UID_FMT
, m
->scheduled_shutdown_uid
);
2121 if (m
->scheduled_shutdown_tty
)
2122 serialize_item_format(f
, "TTY", "%s", m
->scheduled_shutdown_tty
);
2124 if (!isempty(m
->wall_message
)) {
2125 r
= serialize_item_escaped(f
, "WALL_MESSAGE", m
->wall_message
);
2130 r
= fflush_and_check(f
);
2134 if (rename(temp_path
, SHUTDOWN_SCHEDULE_FILE
) < 0) {
2139 temp_path
= mfree(temp_path
);
2143 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2145 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2148 static void reset_scheduled_shutdown(Manager
*m
) {
2151 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2152 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2153 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2155 m
->scheduled_shutdown_action
= NULL
;
2156 m
->scheduled_shutdown_timeout
= USEC_INFINITY
;
2157 m
->scheduled_shutdown_uid
= UID_INVALID
;
2158 m
->scheduled_shutdown_tty
= mfree(m
->scheduled_shutdown_tty
);
2159 m
->wall_message
= mfree(m
->wall_message
);
2160 m
->shutdown_dry_run
= false;
2162 if (m
->unlink_nologin
) {
2163 (void) unlink_or_warn("/run/nologin");
2164 m
->unlink_nologin
= false;
2167 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2170 static int manager_scheduled_shutdown_handler(
2175 const HandleActionData
*a
= NULL
;
2176 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2177 Manager
*m
= ASSERT_PTR(userdata
);
2180 a
= m
->scheduled_shutdown_action
;
2183 /* Don't allow multiple jobs being executed at the same time */
2184 if (m
->delayed_action
) {
2186 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", a
->target
);
2190 if (m
->shutdown_dry_run
) {
2191 /* We do not process delay inhibitors here. Otherwise, we
2192 * would have to be considered "in progress" (like the check
2193 * above) for some seconds after our admin has seen the final
2196 bus_manager_log_shutdown(m
, a
);
2197 log_info("Running in dry run, suppressing action.");
2198 reset_scheduled_shutdown(m
);
2203 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, m
->scheduled_shutdown_action
, &error
);
2205 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", a
->target
);
2212 reset_scheduled_shutdown(m
);
2216 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2217 Manager
*m
= ASSERT_PTR(userdata
);
2218 HandleAction handle
;
2219 const HandleActionData
*a
;
2223 bool dry_run
= false;
2227 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2231 if (startswith(type
, "dry-")) {
2236 handle
= handle_action_from_string(type
);
2237 if (!IN_SET(handle
, HANDLE_POWEROFF
, HANDLE_REBOOT
, HANDLE_SOFT_REBOOT
, HANDLE_HALT
, HANDLE_KEXEC
))
2238 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2240 a
= handle_action_lookup(handle
);
2242 assert(a
->polkit_action
);
2244 r
= verify_shutdown_creds(m
, message
, a
, 0, error
);
2248 m
->scheduled_shutdown_action
= a
;
2249 m
->shutdown_dry_run
= dry_run
;
2250 m
->scheduled_shutdown_timeout
= elapse
;
2252 r
= manager_setup_shutdown_timers(m
);
2256 r
= setup_wall_message_timer(m
, message
);
2258 r
= update_schedule_file(m
);
2261 reset_scheduled_shutdown(m
);
2265 return sd_bus_reply_method_return(message
, NULL
);
2268 static int manager_setup_shutdown_timers(Manager
* m
) {
2271 r
= event_reset_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2273 m
->scheduled_shutdown_timeout
, 0,
2274 manager_scheduled_shutdown_handler
, m
,
2275 0, "scheduled-shutdown-timeout", true);
2279 r
= event_reset_time(m
->event
, &m
->nologin_timeout_source
,
2281 nologin_timeout_usec(m
->scheduled_shutdown_timeout
), 0,
2282 nologin_timeout_handler
, m
,
2283 0, "nologin-timeout", true);
2290 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2291 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2296 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2297 Manager
*m
= ASSERT_PTR(userdata
);
2298 const HandleActionData
*a
;
2304 cancelled
= m
->scheduled_shutdown_action
2305 && !IN_SET(m
->scheduled_shutdown_action
->handle
, HANDLE_IGNORE
, _HANDLE_ACTION_INVALID
);
2307 return sd_bus_reply_method_return(message
, "b", false);
2309 a
= m
->scheduled_shutdown_action
;
2310 if (!a
->polkit_action
)
2311 return sd_bus_error_set(error
, SD_BUS_ERROR_AUTH_FAILED
, "Unsupported shutdown type");
2313 r
= bus_verify_polkit_async(
2320 &m
->polkit_registry
,
2325 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2327 if (m
->enable_wall_messages
) {
2328 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2329 const char *tty
= NULL
;
2332 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2334 (void) sd_bus_creds_get_uid(creds
, &uid
);
2335 (void) sd_bus_creds_get_tty(creds
, &tty
);
2338 _cleanup_free_
char *username
= uid_to_name(uid
);
2340 log_struct(LOG_INFO
,
2341 LOG_MESSAGE("System shutdown has been cancelled"),
2342 "ACTION=%s", handle_action_to_string(a
->handle
),
2343 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_CANCELED_STR
,
2344 username
? "OPERATOR=%s" : NULL
, username
);
2346 (void) wall("System shutdown has been cancelled",
2347 username
, tty
, logind_wall_tty_filter
, m
);
2350 reset_scheduled_shutdown(m
);
2352 return sd_bus_reply_method_return(message
, "b", true);
2355 static int method_can_shutdown_or_sleep(
2357 sd_bus_message
*message
,
2358 const HandleActionData
*a
,
2359 sd_bus_error
*error
) {
2361 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2362 bool multiple_sessions
, challenge
, blocked
;
2363 const char *result
= NULL
;
2371 if (a
->sleep_operation
>= 0) {
2372 r
= can_sleep(a
->sleep_operation
);
2373 if (IN_SET(r
, 0, -ENOSPC
))
2374 return sd_bus_reply_method_return(message
, "s", "na");
2379 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2383 r
= sd_bus_creds_get_euid(creds
, &uid
);
2387 r
= have_multiple_sessions(m
, uid
);
2391 multiple_sessions
= r
> 0;
2392 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2394 HandleAction handle
= handle_action_from_string(sleep_operation_to_string(a
->sleep_operation
));
2398 target
= handle_action_lookup(handle
)->target
;
2400 _cleanup_free_
char *load_state
= NULL
;
2402 r
= unit_load_state(m
->bus
, target
, &load_state
);
2406 if (!streq(load_state
, "loaded")) {
2413 if (multiple_sessions
) {
2414 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2421 result
= "challenge";
2427 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2434 } else if (challenge
) {
2435 if (!result
|| streq(result
, "yes"))
2436 result
= "challenge";
2441 if (!multiple_sessions
&& !blocked
) {
2442 /* If neither inhibit nor multiple sessions
2443 * apply then just check the normal policy */
2445 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action
, NULL
, UID_INVALID
, &challenge
, error
);
2452 result
= "challenge";
2458 return sd_bus_reply_method_return(message
, "s", result
);
2461 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2462 Manager
*m
= userdata
;
2464 return method_can_shutdown_or_sleep(
2465 m
, message
, handle_action_lookup(HANDLE_POWEROFF
),
2469 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2470 Manager
*m
= userdata
;
2472 return method_can_shutdown_or_sleep(
2473 m
, message
, handle_action_lookup(HANDLE_REBOOT
),
2477 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2478 Manager
*m
= userdata
;
2480 return method_can_shutdown_or_sleep(
2481 m
, message
, handle_action_lookup(HANDLE_HALT
),
2485 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2486 Manager
*m
= userdata
;
2488 return method_can_shutdown_or_sleep(
2489 m
, message
, handle_action_lookup(HANDLE_SUSPEND
),
2493 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2494 Manager
*m
= userdata
;
2496 return method_can_shutdown_or_sleep(
2497 m
, message
, handle_action_lookup(HANDLE_HIBERNATE
),
2501 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2502 Manager
*m
= userdata
;
2504 return method_can_shutdown_or_sleep(
2505 m
, message
, handle_action_lookup(HANDLE_HYBRID_SLEEP
),
2509 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2510 Manager
*m
= userdata
;
2512 return method_can_shutdown_or_sleep(
2513 m
, message
, handle_action_lookup(HANDLE_SUSPEND_THEN_HIBERNATE
),
2517 static int property_get_reboot_parameter(
2520 const char *interface
,
2521 const char *property
,
2522 sd_bus_message
*reply
,
2524 sd_bus_error
*error
) {
2525 _cleanup_free_
char *parameter
= NULL
;
2532 r
= read_reboot_parameter(¶meter
);
2536 return sd_bus_message_append(reply
, "s", parameter
);
2539 static int method_set_reboot_parameter(
2540 sd_bus_message
*message
,
2542 sd_bus_error
*error
) {
2544 Manager
*m
= ASSERT_PTR(userdata
);
2550 r
= sd_bus_message_read(message
, "s", &arg
);
2554 r
= detect_container();
2558 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2559 "Reboot parameter not supported in containers, refusing.");
2561 r
= bus_verify_polkit_async(message
,
2563 "org.freedesktop.login1.set-reboot-parameter",
2567 &m
->polkit_registry
,
2572 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2574 r
= update_reboot_parameter_and_warn(arg
, false);
2578 return sd_bus_reply_method_return(message
, NULL
);
2581 static int method_can_reboot_parameter(
2582 sd_bus_message
*message
,
2584 sd_bus_error
*error
) {
2586 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2591 r
= detect_container();
2594 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2595 return sd_bus_reply_method_return(message
, "s", "na");
2597 return return_test_polkit(
2600 "org.freedesktop.login1.set-reboot-parameter",
2606 static int property_get_reboot_to_firmware_setup(
2609 const char *interface
,
2610 const char *property
,
2611 sd_bus_message
*reply
,
2613 sd_bus_error
*error
) {
2620 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2622 /* EFI case: let's see what is currently configured in the EFI variables */
2623 r
= efi_get_reboot_to_firmware();
2624 if (r
< 0 && r
!= -EOPNOTSUPP
)
2625 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2627 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2629 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2630 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2631 if (errno
!= ENOENT
)
2632 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2639 return sd_bus_message_append(reply
, "b", r
> 0);
2642 static int method_set_reboot_to_firmware_setup(
2643 sd_bus_message
*message
,
2645 sd_bus_error
*error
) {
2647 Manager
*m
= ASSERT_PTR(userdata
);
2653 r
= sd_bus_message_read(message
, "b", &b
);
2657 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2659 /* EFI case: let's see what the firmware supports */
2661 r
= efi_reboot_to_firmware_supported();
2662 if (r
== -EOPNOTSUPP
)
2663 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2669 } else if (r
<= 0) {
2670 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2673 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2675 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2677 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2680 r
= bus_verify_polkit_async(message
,
2682 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2686 &m
->polkit_registry
,
2691 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2694 r
= efi_set_reboot_to_firmware(b
);
2699 r
= touch("/run/systemd/reboot-to-firmware-setup");
2703 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2708 return sd_bus_reply_method_return(message
, NULL
);
2711 static int method_can_reboot_to_firmware_setup(
2712 sd_bus_message
*message
,
2714 sd_bus_error
*error
) {
2716 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2721 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2723 /* EFI case: let's see what the firmware supports */
2725 r
= efi_reboot_to_firmware_supported();
2727 if (r
!= -EOPNOTSUPP
)
2728 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2730 return sd_bus_reply_method_return(message
, "s", "na");
2733 } else if (r
<= 0) {
2734 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2737 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2739 return sd_bus_reply_method_return(message
, "s", "na");
2742 return return_test_polkit(
2745 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2751 static int property_get_reboot_to_boot_loader_menu(
2754 const char *interface
,
2755 const char *property
,
2756 sd_bus_message
*reply
,
2758 sd_bus_error
*error
) {
2760 uint64_t x
= UINT64_MAX
;
2767 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2769 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished:
2771 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2772 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2773 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2776 r
= efi_loader_get_config_timeout_one_shot(&x
);
2779 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
2783 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2785 _cleanup_free_
char *v
= NULL
;
2787 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2789 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2792 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2794 r
= safe_atou64(v
, &x
);
2796 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2800 return sd_bus_message_append(reply
, "t", x
);
2803 static int method_set_reboot_to_boot_loader_menu(
2804 sd_bus_message
*message
,
2806 sd_bus_error
*error
) {
2808 Manager
*m
= ASSERT_PTR(userdata
);
2815 r
= sd_bus_message_read(message
, "t", &x
);
2819 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2823 /* EFI case: let's see if booting into boot loader menu is supported. */
2825 r
= efi_loader_get_features(&features
);
2827 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2828 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2829 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2833 } else if (r
<= 0) {
2834 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2837 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2839 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2841 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2844 r
= bus_verify_polkit_async(message
,
2846 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2850 &m
->polkit_registry
,
2855 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2858 if (x
== UINT64_MAX
)
2859 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), NULL
, 0);
2861 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2862 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2864 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), buf
);
2869 if (x
== UINT64_MAX
) {
2870 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
2873 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2875 xsprintf(buf
, "%" PRIu64
, x
); /* μs granularity */
2877 r
= write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf
);
2883 return sd_bus_reply_method_return(message
, NULL
);
2886 static int method_can_reboot_to_boot_loader_menu(
2887 sd_bus_message
*message
,
2889 sd_bus_error
*error
) {
2891 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
2896 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2898 uint64_t features
= 0;
2900 /* EFI case, let's see if booting into boot loader menu is supported. */
2902 r
= efi_loader_get_features(&features
);
2904 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2905 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2906 return sd_bus_reply_method_return(message
, "s", "na");
2908 } else if (r
<= 0) {
2909 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2912 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2914 return sd_bus_reply_method_return(message
, "s", "na");
2917 return return_test_polkit(
2920 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2926 static int property_get_reboot_to_boot_loader_entry(
2929 const char *interface
,
2930 const char *property
,
2931 sd_bus_message
*reply
,
2933 sd_bus_error
*error
) {
2935 _cleanup_free_
char *v
= NULL
;
2936 Manager
*m
= ASSERT_PTR(userdata
);
2937 const char *x
= NULL
;
2943 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2945 /* EFI case: let's read the LoaderEntryOneShot variable */
2947 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
2950 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
2952 x
= m
->efi_loader_entry_one_shot
;
2955 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2958 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2960 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2963 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
2964 } else if (!efi_loader_entry_name_valid(v
))
2965 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2970 return sd_bus_message_append(reply
, "s", x
);
2973 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
2974 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
2980 r
= boot_config_load_auto(&config
, NULL
, NULL
);
2981 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2984 r
= manager_read_efi_boot_loader_entries(m
);
2986 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
2988 return !!boot_config_find_entry(&config
, id
);
2991 static int method_set_reboot_to_boot_loader_entry(
2992 sd_bus_message
*message
,
2994 sd_bus_error
*error
) {
2996 Manager
*m
= ASSERT_PTR(userdata
);
3003 r
= sd_bus_message_read(message
, "s", &v
);
3009 else if (efi_loader_entry_name_valid(v
)) {
3010 r
= boot_loader_entry_exists(m
, v
);
3014 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3016 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3018 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3022 /* EFI case: let's see if booting into boot loader entry is supported. */
3024 r
= efi_loader_get_features(&features
);
3026 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3027 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3028 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3032 } else if (r
<= 0) {
3033 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3036 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3038 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3040 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3043 r
= bus_verify_polkit_async(message
,
3045 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3049 &m
->polkit_registry
,
3054 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3059 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), NULL
, 0);
3061 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), v
);
3066 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3069 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3075 return sd_bus_reply_method_return(message
, NULL
);
3078 static int method_can_reboot_to_boot_loader_entry(
3079 sd_bus_message
*message
,
3081 sd_bus_error
*error
) {
3083 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3088 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3090 uint64_t features
= 0;
3092 /* EFI case, let's see if booting into boot loader entry is supported. */
3094 r
= efi_loader_get_features(&features
);
3096 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3097 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3098 return sd_bus_reply_method_return(message
, "s", "na");
3100 } else if (r
<= 0) {
3101 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3104 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3106 return sd_bus_reply_method_return(message
, "s", "na");
3109 return return_test_polkit(
3112 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3118 static int property_get_boot_loader_entries(
3121 const char *interface
,
3122 const char *property
,
3123 sd_bus_message
*reply
,
3125 sd_bus_error
*error
) {
3127 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3128 Manager
*m
= ASSERT_PTR(userdata
);
3135 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3136 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3139 r
= manager_read_efi_boot_loader_entries(m
);
3141 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3143 r
= sd_bus_message_open_container(reply
, 'a', "s");
3147 for (i
= 0; i
< config
.n_entries
; i
++) {
3148 BootEntry
*e
= config
.entries
+ i
;
3150 r
= sd_bus_message_append(reply
, "s", e
->id
);
3155 return sd_bus_message_close_container(reply
);
3158 static int method_set_wall_message(
3159 sd_bus_message
*message
,
3161 sd_bus_error
*error
) {
3164 Manager
*m
= ASSERT_PTR(userdata
);
3166 int enable_wall_messages
;
3170 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3174 if (strlen(wall_message
) > WALL_MESSAGE_MAX
)
3175 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3176 "Wall message too long, maximum permitted length is %u characters.",
3179 /* Short-circuit the operation if the desired state is already in place, to
3180 * avoid an unnecessary polkit permission check. */
3181 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3182 m
->enable_wall_messages
== enable_wall_messages
)
3185 r
= bus_verify_polkit_async(message
,
3187 "org.freedesktop.login1.set-wall-message",
3191 &m
->polkit_registry
,
3196 return 1; /* Will call us back */
3198 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3202 m
->enable_wall_messages
= enable_wall_messages
;
3205 return sd_bus_reply_method_return(message
, NULL
);
3208 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3209 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3210 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
3211 const char *who
, *why
, *what
, *mode
;
3212 _cleanup_free_
char *id
= NULL
;
3213 _cleanup_close_
int fifo_fd
= -EBADF
;
3214 Manager
*m
= ASSERT_PTR(userdata
);
3223 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3227 w
= inhibit_what_from_string(what
);
3229 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3230 "Invalid what specification %s", what
);
3232 mm
= inhibit_mode_from_string(mode
);
3234 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3235 "Invalid mode specification %s", mode
);
3237 /* Delay is only supported for shutdown/sleep */
3238 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3239 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3240 "Delay inhibitors only supported for shutdown and sleep");
3242 /* Don't allow taking delay locks while we are already
3243 * executing the operation. We shouldn't create the impression
3244 * that the lock was successful if the machine is about to go
3245 * down/suspend any moment. */
3246 if (m
->delayed_action
&& m
->delayed_action
->inhibit_what
& w
)
3247 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3248 "The operation inhibition has been requested for is already running");
3250 r
= bus_verify_polkit_async(
3253 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3254 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3255 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3256 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3257 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3258 w
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3259 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3260 "org.freedesktop.login1.inhibit-handle-lid-switch",
3264 &m
->polkit_registry
,
3269 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3271 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3275 r
= sd_bus_creds_get_euid(creds
, &uid
);
3279 r
= sd_bus_creds_get_pid(creds
, &pid
);
3283 r
= pidref_set_pid(&pidref
, pid
);
3285 return sd_bus_error_set_errnof(error
, r
, "Failed pin source process "PID_FMT
": %m", pid
);
3287 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3288 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3289 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3295 if (asprintf(&id
, "%" PRIu64
, ++m
->inhibit_counter
) < 0)
3298 } while (hashmap_get(m
->inhibitors
, id
));
3300 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3301 r
= manager_add_inhibitor(m
, id
, &i
);
3307 i
->pid
= TAKE_PIDREF(pidref
);
3309 i
->why
= strdup(why
);
3310 i
->who
= strdup(who
);
3312 if (!i
->why
|| !i
->who
)
3315 fifo_fd
= inhibitor_create_fifo(i
);
3319 r
= inhibitor_start(i
);
3324 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3327 static const sd_bus_vtable manager_vtable
[] = {
3328 SD_BUS_VTABLE_START(0),
3330 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool
, bus_property_set_bool
, offsetof(Manager
, enable_wall_messages
), 0),
3331 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3333 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3334 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3335 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3336 SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3337 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3338 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3339 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3340 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3341 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3342 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3343 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3344 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3345 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3346 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3347 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3348 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3349 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3350 SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3351 SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3352 SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3353 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3354 SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3355 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3356 SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3357 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3358 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3359 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3360 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3361 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3362 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3363 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3364 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3365 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3366 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3367 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3368 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3369 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3370 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3371 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3372 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3373 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3374 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3375 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3376 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3377 SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL
, offsetof(Manager
, stop_idle_session_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3379 SD_BUS_METHOD_WITH_ARGS("GetSession",
3380 SD_BUS_ARGS("s", session_id
),
3381 SD_BUS_RESULT("o", object_path
),
3383 SD_BUS_VTABLE_UNPRIVILEGED
),
3384 SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
3385 SD_BUS_ARGS("u", pid
),
3386 SD_BUS_RESULT("o", object_path
),
3387 method_get_session_by_pid
,
3388 SD_BUS_VTABLE_UNPRIVILEGED
),
3389 SD_BUS_METHOD_WITH_ARGS("GetUser",
3390 SD_BUS_ARGS("u", uid
),
3391 SD_BUS_RESULT("o", object_path
),
3393 SD_BUS_VTABLE_UNPRIVILEGED
),
3394 SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
3395 SD_BUS_ARGS("u", pid
),
3396 SD_BUS_RESULT("o", object_path
),
3397 method_get_user_by_pid
,
3398 SD_BUS_VTABLE_UNPRIVILEGED
),
3399 SD_BUS_METHOD_WITH_ARGS("GetSeat",
3400 SD_BUS_ARGS("s", seat_id
),
3401 SD_BUS_RESULT("o", object_path
),
3403 SD_BUS_VTABLE_UNPRIVILEGED
),
3404 SD_BUS_METHOD_WITH_ARGS("ListSessions",
3406 SD_BUS_RESULT("a(susso)", sessions
),
3407 method_list_sessions
,
3408 SD_BUS_VTABLE_UNPRIVILEGED
),
3409 SD_BUS_METHOD_WITH_ARGS("ListUsers",
3411 SD_BUS_RESULT("a(uso)", users
),
3413 SD_BUS_VTABLE_UNPRIVILEGED
),
3414 SD_BUS_METHOD_WITH_ARGS("ListSeats",
3416 SD_BUS_RESULT("a(so)", seats
),
3418 SD_BUS_VTABLE_UNPRIVILEGED
),
3419 SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
3421 SD_BUS_RESULT("a(ssssuu)", inhibitors
),
3422 method_list_inhibitors
,
3423 SD_BUS_VTABLE_UNPRIVILEGED
),
3424 SD_BUS_METHOD_WITH_ARGS("CreateSession",
3425 SD_BUS_ARGS("u", uid
,
3438 "a(sv)", properties
),
3439 SD_BUS_RESULT("s", session_id
,
3447 method_create_session
,
3449 SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
3450 SD_BUS_ARGS("s", session_id
),
3452 method_release_session
,
3454 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
3455 SD_BUS_ARGS("s", session_id
),
3457 method_activate_session
,
3458 SD_BUS_VTABLE_UNPRIVILEGED
),
3459 SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
3460 SD_BUS_ARGS("s", session_id
, "s", seat_id
),
3462 method_activate_session_on_seat
,
3463 SD_BUS_VTABLE_UNPRIVILEGED
),
3464 SD_BUS_METHOD_WITH_ARGS("LockSession",
3465 SD_BUS_ARGS("s", session_id
),
3467 method_lock_session
,
3468 SD_BUS_VTABLE_UNPRIVILEGED
),
3469 SD_BUS_METHOD_WITH_ARGS("UnlockSession",
3470 SD_BUS_ARGS("s", session_id
),
3472 method_lock_session
,
3473 SD_BUS_VTABLE_UNPRIVILEGED
),
3474 SD_BUS_METHOD("LockSessions",
3477 method_lock_sessions
,
3478 SD_BUS_VTABLE_UNPRIVILEGED
),
3479 SD_BUS_METHOD("UnlockSessions",
3482 method_lock_sessions
,
3483 SD_BUS_VTABLE_UNPRIVILEGED
),
3484 SD_BUS_METHOD_WITH_ARGS("KillSession",
3485 SD_BUS_ARGS("s", session_id
, "s", who
, "i", signal_number
),
3487 method_kill_session
,
3488 SD_BUS_VTABLE_UNPRIVILEGED
),
3489 SD_BUS_METHOD_WITH_ARGS("KillUser",
3490 SD_BUS_ARGS("u", uid
, "i", signal_number
),
3493 SD_BUS_VTABLE_UNPRIVILEGED
),
3494 SD_BUS_METHOD_WITH_ARGS("TerminateSession",
3495 SD_BUS_ARGS("s", session_id
),
3497 method_terminate_session
,
3498 SD_BUS_VTABLE_UNPRIVILEGED
),
3499 SD_BUS_METHOD_WITH_ARGS("TerminateUser",
3500 SD_BUS_ARGS("u", uid
),
3502 method_terminate_user
,
3503 SD_BUS_VTABLE_UNPRIVILEGED
),
3504 SD_BUS_METHOD_WITH_ARGS("TerminateSeat",
3505 SD_BUS_ARGS("s", seat_id
),
3507 method_terminate_seat
,
3508 SD_BUS_VTABLE_UNPRIVILEGED
),
3509 SD_BUS_METHOD_WITH_ARGS("SetUserLinger",
3510 SD_BUS_ARGS("u", uid
, "b", enable
, "b", interactive
),
3512 method_set_user_linger
,
3513 SD_BUS_VTABLE_UNPRIVILEGED
),
3514 SD_BUS_METHOD_WITH_ARGS("AttachDevice",
3515 SD_BUS_ARGS("s", seat_id
, "s", sysfs_path
, "b", interactive
),
3517 method_attach_device
,
3518 SD_BUS_VTABLE_UNPRIVILEGED
),
3519 SD_BUS_METHOD_WITH_ARGS("FlushDevices",
3520 SD_BUS_ARGS("b", interactive
),
3522 method_flush_devices
,
3523 SD_BUS_VTABLE_UNPRIVILEGED
),
3524 SD_BUS_METHOD_WITH_ARGS("PowerOff",
3525 SD_BUS_ARGS("b", interactive
),
3528 SD_BUS_VTABLE_UNPRIVILEGED
),
3529 SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags",
3530 SD_BUS_ARGS("t", flags
),
3533 SD_BUS_VTABLE_UNPRIVILEGED
),
3534 SD_BUS_METHOD_WITH_ARGS("Reboot",
3535 SD_BUS_ARGS("b", interactive
),
3538 SD_BUS_VTABLE_UNPRIVILEGED
),
3539 SD_BUS_METHOD_WITH_ARGS("RebootWithFlags",
3540 SD_BUS_ARGS("t", flags
),
3543 SD_BUS_VTABLE_UNPRIVILEGED
),
3544 SD_BUS_METHOD_WITH_ARGS("Halt",
3545 SD_BUS_ARGS("b", interactive
),
3548 SD_BUS_VTABLE_UNPRIVILEGED
),
3549 SD_BUS_METHOD_WITH_ARGS("HaltWithFlags",
3550 SD_BUS_ARGS("t", flags
),
3553 SD_BUS_VTABLE_UNPRIVILEGED
),
3554 SD_BUS_METHOD_WITH_ARGS("Suspend",
3555 SD_BUS_ARGS("b", interactive
),
3558 SD_BUS_VTABLE_UNPRIVILEGED
),
3559 SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags",
3560 SD_BUS_ARGS("t", flags
),
3563 SD_BUS_VTABLE_UNPRIVILEGED
),
3564 SD_BUS_METHOD_WITH_ARGS("Hibernate",
3565 SD_BUS_ARGS("b", interactive
),
3568 SD_BUS_VTABLE_UNPRIVILEGED
),
3569 SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags",
3570 SD_BUS_ARGS("t", flags
),
3573 SD_BUS_VTABLE_UNPRIVILEGED
),
3574 SD_BUS_METHOD_WITH_ARGS("HybridSleep",
3575 SD_BUS_ARGS("b", interactive
),
3577 method_hybrid_sleep
,
3578 SD_BUS_VTABLE_UNPRIVILEGED
),
3579 SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags",
3580 SD_BUS_ARGS("t", flags
),
3582 method_hybrid_sleep
,
3583 SD_BUS_VTABLE_UNPRIVILEGED
),
3584 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate",
3585 SD_BUS_ARGS("b", interactive
),
3587 method_suspend_then_hibernate
,
3588 SD_BUS_VTABLE_UNPRIVILEGED
),
3589 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags",
3590 SD_BUS_ARGS("t", flags
),
3592 method_suspend_then_hibernate
,
3593 SD_BUS_VTABLE_UNPRIVILEGED
),
3594 SD_BUS_METHOD_WITH_ARGS("CanPowerOff",
3596 SD_BUS_RESULT("s", result
),
3597 method_can_poweroff
,
3598 SD_BUS_VTABLE_UNPRIVILEGED
),
3599 SD_BUS_METHOD_WITH_ARGS("CanReboot",
3601 SD_BUS_RESULT("s", result
),
3603 SD_BUS_VTABLE_UNPRIVILEGED
),
3604 SD_BUS_METHOD_WITH_ARGS("CanHalt",
3606 SD_BUS_RESULT("s", result
),
3608 SD_BUS_VTABLE_UNPRIVILEGED
),
3609 SD_BUS_METHOD_WITH_ARGS("CanSuspend",
3611 SD_BUS_RESULT("s", result
),
3613 SD_BUS_VTABLE_UNPRIVILEGED
),
3614 SD_BUS_METHOD_WITH_ARGS("CanHibernate",
3616 SD_BUS_RESULT("s", result
),
3617 method_can_hibernate
,
3618 SD_BUS_VTABLE_UNPRIVILEGED
),
3619 SD_BUS_METHOD_WITH_ARGS("CanHybridSleep",
3621 SD_BUS_RESULT("s", result
),
3622 method_can_hybrid_sleep
,
3623 SD_BUS_VTABLE_UNPRIVILEGED
),
3624 SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate",
3626 SD_BUS_RESULT("s", result
),
3627 method_can_suspend_then_hibernate
,
3628 SD_BUS_VTABLE_UNPRIVILEGED
),
3629 SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown",
3630 SD_BUS_ARGS("s", type
, "t", usec
),
3632 method_schedule_shutdown
,
3633 SD_BUS_VTABLE_UNPRIVILEGED
),
3634 SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown",
3636 SD_BUS_RESULT("b", cancelled
),
3637 method_cancel_scheduled_shutdown
,
3638 SD_BUS_VTABLE_UNPRIVILEGED
),
3639 SD_BUS_METHOD_WITH_ARGS("Inhibit",
3640 SD_BUS_ARGS("s", what
, "s", who
, "s", why
, "s", mode
),
3641 SD_BUS_RESULT("h", pipe_fd
),
3643 SD_BUS_VTABLE_UNPRIVILEGED
),
3644 SD_BUS_METHOD_WITH_ARGS("CanRebootParameter",
3646 SD_BUS_RESULT("s", result
),
3647 method_can_reboot_parameter
,
3648 SD_BUS_VTABLE_UNPRIVILEGED
),
3649 SD_BUS_METHOD_WITH_ARGS("SetRebootParameter",
3650 SD_BUS_ARGS("s", parameter
),
3652 method_set_reboot_parameter
,
3653 SD_BUS_VTABLE_UNPRIVILEGED
),
3654 SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup",
3656 SD_BUS_RESULT("s", result
),
3657 method_can_reboot_to_firmware_setup
,
3658 SD_BUS_VTABLE_UNPRIVILEGED
),
3659 SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup",
3660 SD_BUS_ARGS("b", enable
),
3662 method_set_reboot_to_firmware_setup
,
3663 SD_BUS_VTABLE_UNPRIVILEGED
),
3664 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu",
3666 SD_BUS_RESULT("s", result
),
3667 method_can_reboot_to_boot_loader_menu
,
3668 SD_BUS_VTABLE_UNPRIVILEGED
),
3669 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu",
3670 SD_BUS_ARGS("t", timeout
),
3672 method_set_reboot_to_boot_loader_menu
,
3673 SD_BUS_VTABLE_UNPRIVILEGED
),
3674 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry",
3676 SD_BUS_RESULT("s", result
),
3677 method_can_reboot_to_boot_loader_entry
,
3678 SD_BUS_VTABLE_UNPRIVILEGED
),
3679 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry",
3680 SD_BUS_ARGS("s", boot_loader_entry
),
3682 method_set_reboot_to_boot_loader_entry
,
3683 SD_BUS_VTABLE_UNPRIVILEGED
),
3684 SD_BUS_METHOD_WITH_ARGS("SetWallMessage",
3685 SD_BUS_ARGS("s", wall_message
, "b", enable
),
3687 method_set_wall_message
,
3688 SD_BUS_VTABLE_UNPRIVILEGED
),
3690 SD_BUS_SIGNAL_WITH_ARGS("SessionNew",
3691 SD_BUS_ARGS("s", session_id
, "o", object_path
),
3693 SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved",
3694 SD_BUS_ARGS("s", session_id
, "o", object_path
),
3696 SD_BUS_SIGNAL_WITH_ARGS("UserNew",
3697 SD_BUS_ARGS("u", uid
, "o", object_path
),
3699 SD_BUS_SIGNAL_WITH_ARGS("UserRemoved",
3700 SD_BUS_ARGS("u", uid
, "o", object_path
),
3702 SD_BUS_SIGNAL_WITH_ARGS("SeatNew",
3703 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
3705 SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved",
3706 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
3708 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown",
3709 SD_BUS_ARGS("b", start
),
3711 SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep",
3712 SD_BUS_ARGS("b", start
),
3718 const BusObjectImplementation manager_object
= {
3719 "/org/freedesktop/login1",
3720 "org.freedesktop.login1.Manager",
3721 .vtables
= BUS_VTABLES(manager_vtable
),
3722 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
3727 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
3734 if (result
&& !streq(result
, "done")) {
3735 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3737 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3738 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
3739 return session_send_create_reply(s
, &e
);
3742 return session_send_create_reply(s
, NULL
);
3745 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3746 const char *path
, *result
, *unit
;
3747 Manager
*m
= ASSERT_PTR(userdata
);
3755 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3757 bus_log_parse_error(r
);
3761 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3762 assert(m
->delayed_action
);
3763 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->delayed_action
->inhibit_what
));
3765 /* Tell people that they now may take a lock again */
3766 (void) send_prepare_for(m
, m
->delayed_action
->inhibit_what
, false);
3768 m
->action_job
= mfree(m
->action_job
);
3769 m
->delayed_action
= NULL
;
3773 session
= hashmap_get(m
->session_units
, unit
);
3775 if (streq_ptr(path
, session
->scope_job
)) {
3776 session
->scope_job
= mfree(session
->scope_job
);
3777 (void) session_jobs_reply(session
, id
, unit
, result
);
3779 session_save(session
);
3780 user_save(session
->user
);
3783 session_add_to_gc_queue(session
);
3786 user
= hashmap_get(m
->user_units
, unit
);
3788 if (streq_ptr(path
, user
->service_job
)) {
3789 user
->service_job
= mfree(user
->service_job
);
3791 LIST_FOREACH(sessions_by_user
, s
, user
->sessions
)
3792 (void) session_jobs_reply(s
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
3797 user_add_to_gc_queue(user
);
3803 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3804 const char *path
, *unit
;
3805 Manager
*m
= ASSERT_PTR(userdata
);
3812 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3814 bus_log_parse_error(r
);
3818 session
= hashmap_get(m
->session_units
, unit
);
3820 session_add_to_gc_queue(session
);
3822 user
= hashmap_get(m
->user_units
, unit
);
3824 user_add_to_gc_queue(user
);
3829 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3830 _cleanup_free_
char *unit
= NULL
;
3831 Manager
*m
= ASSERT_PTR(userdata
);
3839 path
= sd_bus_message_get_path(message
);
3843 r
= unit_name_from_dbus_path(path
, &unit
);
3844 if (r
== -EINVAL
) /* not a unit */
3851 session
= hashmap_get(m
->session_units
, unit
);
3853 session_add_to_gc_queue(session
);
3855 user
= hashmap_get(m
->user_units
, unit
);
3857 user_add_to_gc_queue(user
);
3862 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3863 Manager
*m
= ASSERT_PTR(userdata
);
3869 r
= sd_bus_message_read(message
, "b", &b
);
3871 bus_log_parse_error(r
);
3878 /* systemd finished reloading, let's recheck all our sessions */
3879 log_debug("System manager has been reloaded, rechecking sessions...");
3881 HASHMAP_FOREACH(session
, m
->sessions
)
3882 session_add_to_gc_queue(session
);
3887 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3892 l
= strv_from_stdarg_alloca(property
);
3894 return sd_bus_emit_properties_changed_strv(
3896 "/org/freedesktop/login1",
3897 "org.freedesktop.login1.Manager",
3901 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3906 r
= sd_bus_message_read(reply
, "o", &j
);
3918 int manager_start_scope(
3923 const char *description
,
3926 const char *requires_mounts_for
,
3927 sd_bus_message
*more_properties
,
3928 sd_bus_error
*error
,
3931 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3939 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
3943 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3947 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3951 if (!isempty(slice
)) {
3952 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3957 if (!isempty(description
)) {
3958 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
3963 STRV_FOREACH(i
, wants
) {
3964 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
3969 STRV_FOREACH(i
, after
) {
3970 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3975 if (!empty_or_root(requires_mounts_for
)) {
3976 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3981 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3983 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3987 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3991 /* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of
3993 r
= sd_bus_message_append(m
, "(sv)", "OOMPolicy", "s", "continue");
3997 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3998 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_MAX
);
4000 return bus_log_create_error(r
);
4002 if (more_properties
) {
4003 /* If TasksMax also appears here, it will overwrite the default value set above */
4004 r
= sd_bus_message_copy(m
, more_properties
, true);
4009 r
= sd_bus_message_close_container(m
);
4013 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4017 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4021 return strdup_job(reply
, job
);
4024 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4025 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4032 r
= bus_call_method(
4038 "ss", unit
, "replace");
4042 return strdup_job(reply
, job
);
4045 int manager_stop_unit(Manager
*manager
, const char *unit
, const char *job_mode
, sd_bus_error
*error
, char **ret_job
) {
4046 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4053 r
= bus_call_method(
4059 "ss", unit
, job_mode
?: "fail");
4061 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
4062 BUS_ERROR_LOAD_FAILED
)) {
4065 sd_bus_error_free(error
);
4072 return strdup_job(reply
, ret_job
);
4075 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4076 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4077 _cleanup_free_
char *path
= NULL
;
4083 path
= unit_dbus_path_from_name(scope
);
4087 r
= sd_bus_call_method(
4089 "org.freedesktop.systemd1",
4091 "org.freedesktop.systemd1.Scope",
4097 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4098 BUS_ERROR_LOAD_FAILED
,
4099 BUS_ERROR_SCOPE_NOT_RUNNING
))
4102 sd_bus_error_move(ret_error
, &error
);
4109 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4113 return bus_call_method(
4119 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4122 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4123 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4124 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4125 _cleanup_free_
char *path
= NULL
;
4132 path
= unit_dbus_path_from_name(unit
);
4136 r
= sd_bus_get_property(
4138 "org.freedesktop.systemd1",
4140 "org.freedesktop.systemd1.Unit",
4146 /* systemd might have dropped off momentarily, let's
4147 * not make this an error */
4148 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4149 SD_BUS_ERROR_DISCONNECTED
))
4152 /* If the unit is already unloaded then it's not
4154 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4155 BUS_ERROR_LOAD_FAILED
))
4158 sd_bus_error_move(ret_error
, &error
);
4162 r
= sd_bus_message_read(reply
, "s", &state
);
4166 return !STR_IN_SET(state
, "inactive", "failed");
4169 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4170 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4171 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4177 r
= sd_bus_get_property(
4179 "org.freedesktop.systemd1",
4181 "org.freedesktop.systemd1.Job",
4187 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4188 SD_BUS_ERROR_DISCONNECTED
))
4191 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4194 sd_bus_error_move(ret_error
, &error
);
4198 /* We don't actually care about the state really. The fact
4199 * that we could read the job state is enough for us */