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"
23 #include "efi-loader.h"
28 #include "fileio-label.h"
30 #include "format-util.h"
32 #include "logind-action.h"
33 #include "logind-dbus.h"
34 #include "logind-polkit.h"
35 #include "logind-seat-dbus.h"
36 #include "logind-session-dbus.h"
37 #include "logind-user-dbus.h"
39 #include "missing_capability.h"
40 #include "mkdir-label.h"
41 #include "parse-util.h"
42 #include "path-util.h"
43 #include "process-util.h"
44 #include "reboot-util.h"
45 #include "selinux-util.h"
46 #include "sleep-config.h"
48 #include "stdio-util.h"
50 #include "terminal-util.h"
51 #include "tmpfile-util.h"
52 #include "unit-name.h"
53 #include "user-util.h"
54 #include "utmp-wtmp.h"
57 static void reset_scheduled_shutdown(Manager
*m
);
59 static int get_sender_session(
61 sd_bus_message
*message
,
66 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
67 Session
*session
= NULL
;
71 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
72 * and returns that. If not and 'consult_display' is true, this returns the display session of the
73 * owning user of the caller. */
75 r
= sd_bus_query_sender_creds(message
,
76 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
77 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
81 r
= sd_bus_creds_get_session(creds
, &name
);
86 if (consult_display
) {
89 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
96 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
98 session
= user
->display
;
102 session
= hashmap_get(m
->sessions
, name
);
105 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
107 "Caller does not belong to any known session and doesn't own any suitable session." :
108 "Caller does not belong to any known session.");
114 int manager_get_session_from_creds(
116 sd_bus_message
*message
,
126 if (SEAT_IS_SELF(name
)) /* the caller's own session */
127 return get_sender_session(m
, message
, false, error
, ret
);
128 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
129 return get_sender_session(m
, message
, true, error
, ret
);
131 session
= hashmap_get(m
->sessions
, name
);
133 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
139 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
140 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
145 /* Note that we get the owner UID of the session, not the actual client UID here! */
146 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
150 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
157 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
160 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
161 "Caller does not belong to any logged in or lingering user");
167 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
173 if (!uid_is_valid(uid
))
174 return get_sender_user(m
, message
, error
, ret
);
176 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
178 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
179 "User ID "UID_FMT
" is not logged in or lingering", uid
);
185 int manager_get_seat_from_creds(
187 sd_bus_message
*message
,
198 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
201 /* Use these special seat names as session names */
202 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
206 seat
= session
->seat
;
208 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
210 seat
= hashmap_get(m
->seats
, name
);
212 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
219 static int return_test_polkit(
220 sd_bus_message
*message
,
223 const char **details
,
231 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
238 result
= "challenge";
242 return sd_bus_reply_method_return(message
, "s", result
);
245 static int property_get_idle_hint(
248 const char *interface
,
249 const char *property
,
250 sd_bus_message
*reply
,
252 sd_bus_error
*error
) {
254 Manager
*m
= userdata
;
260 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
263 static int property_get_idle_since_hint(
266 const char *interface
,
267 const char *property
,
268 sd_bus_message
*reply
,
270 sd_bus_error
*error
) {
272 Manager
*m
= userdata
;
273 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
279 manager_get_idle_hint(m
, &t
);
281 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
284 static int property_get_inhibited(
287 const char *interface
,
288 const char *property
,
289 sd_bus_message
*reply
,
291 sd_bus_error
*error
) {
293 Manager
*m
= userdata
;
300 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
302 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
305 static int property_get_preparing(
308 const char *interface
,
309 const char *property
,
310 sd_bus_message
*reply
,
312 sd_bus_error
*error
) {
314 Manager
*m
= userdata
;
321 if (m
->delayed_action
) {
322 if (streq(property
, "PreparingForShutdown"))
323 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SHUTDOWN
;
325 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SLEEP
;
328 return sd_bus_message_append(reply
, "b", b
);
331 static int property_get_scheduled_shutdown(
334 const char *interface
,
335 const char *property
,
336 sd_bus_message
*reply
,
338 sd_bus_error
*error
) {
340 Manager
*m
= userdata
;
347 r
= sd_bus_message_open_container(reply
, 'r', "st");
351 r
= sd_bus_message_append(reply
, "st",
352 handle_action_to_string(manager_handle_for_item(m
->scheduled_shutdown_type
)),
353 m
->scheduled_shutdown_timeout
);
357 return sd_bus_message_close_container(reply
);
360 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
361 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
362 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
363 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
364 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
365 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
367 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
368 _cleanup_free_
char *p
= NULL
;
369 Manager
*m
= userdata
;
377 r
= sd_bus_message_read(message
, "s", &name
);
381 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
385 p
= session_bus_path(session
);
389 return sd_bus_reply_method_return(message
, "o", p
);
392 /* Get login session of a process. This is not what you are looking for these days,
393 * as apps may instead belong to a user service unit. This includes terminal
394 * emulators and hence command-line apps. */
395 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
396 _cleanup_free_
char *p
= NULL
;
397 Session
*session
= NULL
;
398 Manager
*m
= userdata
;
405 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
407 r
= sd_bus_message_read(message
, "u", &pid
);
414 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
418 r
= manager_get_session_by_pid(m
, pid
, &session
);
423 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
424 "PID "PID_FMT
" does not belong to any known session", pid
);
427 p
= session_bus_path(session
);
431 return sd_bus_reply_method_return(message
, "o", p
);
434 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
435 _cleanup_free_
char *p
= NULL
;
436 Manager
*m
= userdata
;
444 r
= sd_bus_message_read(message
, "u", &uid
);
448 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
452 p
= user_bus_path(user
);
456 return sd_bus_reply_method_return(message
, "o", p
);
459 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
460 _cleanup_free_
char *p
= NULL
;
461 Manager
*m
= userdata
;
469 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
471 r
= sd_bus_message_read(message
, "u", &pid
);
478 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
482 r
= manager_get_user_by_pid(m
, pid
, &user
);
486 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
487 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
491 p
= user_bus_path(user
);
495 return sd_bus_reply_method_return(message
, "o", p
);
498 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
499 _cleanup_free_
char *p
= NULL
;
500 Manager
*m
= userdata
;
508 r
= sd_bus_message_read(message
, "s", &name
);
512 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
516 p
= seat_bus_path(seat
);
520 return sd_bus_reply_method_return(message
, "o", p
);
523 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
524 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
525 Manager
*m
= userdata
;
532 r
= sd_bus_message_new_method_return(message
, &reply
);
536 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
540 HASHMAP_FOREACH(session
, m
->sessions
) {
541 _cleanup_free_
char *p
= NULL
;
543 p
= session_bus_path(session
);
547 r
= sd_bus_message_append(reply
, "(susso)",
549 (uint32_t) session
->user
->user_record
->uid
,
550 session
->user
->user_record
->user_name
,
551 session
->seat
? session
->seat
->id
: "",
557 r
= sd_bus_message_close_container(reply
);
561 return sd_bus_send(NULL
, reply
, NULL
);
564 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
565 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
566 Manager
*m
= userdata
;
573 r
= sd_bus_message_new_method_return(message
, &reply
);
577 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
581 HASHMAP_FOREACH(user
, m
->users
) {
582 _cleanup_free_
char *p
= NULL
;
584 p
= user_bus_path(user
);
588 r
= sd_bus_message_append(reply
, "(uso)",
589 (uint32_t) user
->user_record
->uid
,
590 user
->user_record
->user_name
,
596 r
= sd_bus_message_close_container(reply
);
600 return sd_bus_send(NULL
, reply
, NULL
);
603 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
604 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
605 Manager
*m
= userdata
;
612 r
= sd_bus_message_new_method_return(message
, &reply
);
616 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
620 HASHMAP_FOREACH(seat
, m
->seats
) {
621 _cleanup_free_
char *p
= NULL
;
623 p
= seat_bus_path(seat
);
627 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
632 r
= sd_bus_message_close_container(reply
);
636 return sd_bus_send(NULL
, reply
, NULL
);
639 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
640 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
641 Manager
*m
= userdata
;
642 Inhibitor
*inhibitor
;
648 r
= sd_bus_message_new_method_return(message
, &reply
);
652 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
656 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
) {
658 r
= sd_bus_message_append(reply
, "(ssssuu)",
659 strempty(inhibit_what_to_string(inhibitor
->what
)),
660 strempty(inhibitor
->who
),
661 strempty(inhibitor
->why
),
662 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
663 (uint32_t) inhibitor
->uid
,
664 (uint32_t) inhibitor
->pid
);
669 r
= sd_bus_message_close_container(reply
);
673 return sd_bus_send(NULL
, reply
, NULL
);
676 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
677 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
678 _cleanup_free_
char *id
= NULL
;
679 Session
*session
= NULL
;
680 uint32_t audit_id
= 0;
681 Manager
*m
= userdata
;
695 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
696 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
698 r
= sd_bus_message_read(message
, "uusssssussbss",
699 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
700 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
704 if (!uid_is_valid(uid
))
705 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
706 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
707 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
710 t
= _SESSION_TYPE_INVALID
;
712 t
= session_type_from_string(type
);
714 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
715 "Invalid session type %s", type
);
719 c
= _SESSION_CLASS_INVALID
;
721 c
= session_class_from_string(class);
723 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
724 "Invalid session class %s", class);
727 if (isempty(desktop
))
730 if (!string_is_safe(desktop
))
731 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
732 "Invalid desktop string %s", desktop
);
738 seat
= hashmap_get(m
->seats
, cseat
);
740 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
741 "No seat '%s' known", cseat
);
744 if (tty_is_vc(tty
)) {
749 else if (seat
!= m
->seat0
)
750 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
751 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
753 v
= vtnr_from_tty(tty
);
755 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
756 "Cannot determine VT number from virtual console TTY %s", tty
);
760 else if (vtnr
!= (uint32_t) v
)
761 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
762 "Specified TTY and VT number do not match");
764 } else if (tty_is_console(tty
)) {
768 else if (seat
!= m
->seat0
)
769 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
770 "Console TTY specified but seat is not seat0");
773 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
774 "Console TTY specified but VT number is not 0");
778 if (seat_has_vts(seat
)) {
779 if (vtnr
<= 0 || vtnr
> 63)
780 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
781 "VT number out of range");
784 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
785 "Seat has no VTs but VT number not 0");
789 if (t
== _SESSION_TYPE_INVALID
) {
790 if (!isempty(display
))
792 else if (!isempty(tty
))
795 t
= SESSION_UNSPECIFIED
;
798 if (c
== _SESSION_CLASS_INVALID
) {
799 if (t
== SESSION_UNSPECIFIED
)
800 c
= SESSION_BACKGROUND
;
806 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
808 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
812 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
817 /* Check if we are already in a logind session. Or if we are in user@.service
818 * which is a special PAM session that avoids creating a logind session. */
819 r
= manager_get_user_by_pid(m
, leader
, NULL
);
823 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
824 "Already running in a session or user slice");
827 * Old gdm and lightdm start the user-session on the same VT as
828 * the greeter session. But they destroy the greeter session
829 * after the user-session and want the user-session to take
830 * over the VT. We need to support this for
831 * backwards-compatibility, so make sure we allow new sessions
832 * on a VT that a greeter is running on. Furthermore, to allow
833 * re-logins, we have to allow a greeter to take over a used VT for
834 * the exact same reasons.
836 if (c
!= SESSION_GREETER
&&
838 vtnr
< MALLOC_ELEMENTSOF(m
->seat0
->positions
) &&
839 m
->seat0
->positions
[vtnr
] &&
840 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
841 return sd_bus_error_set(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
843 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
844 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
845 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
848 (void) audit_session_from_pid(leader
, &audit_id
);
849 if (audit_session_is_valid(audit_id
)) {
850 /* Keep our session IDs and the audit session IDs in sync */
852 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
855 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
856 * not trust the audit data and let's better register a new ID */
857 if (hashmap_contains(m
->sessions
, id
)) {
858 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
859 audit_id
= AUDIT_SESSION_INVALID
;
868 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
871 } while (hashmap_contains(m
->sessions
, id
));
874 /* The generated names should not clash with 'auto' or 'self' */
875 assert(!SESSION_IS_SELF(id
));
876 assert(!SESSION_IS_AUTO(id
));
878 /* If we are not watching utmp already, try again */
879 manager_reconnect_utmp(m
);
881 r
= manager_add_user_by_uid(m
, uid
, &user
);
885 r
= manager_add_session(m
, id
, &session
);
889 session_set_user(session
, user
);
890 r
= session_set_leader(session
, leader
);
894 session
->original_type
= session
->type
= t
;
896 session
->remote
= remote
;
897 session
->vtnr
= vtnr
;
900 session
->tty
= strdup(tty
);
906 session
->tty_validity
= TTY_FROM_PAM
;
909 if (!isempty(display
)) {
910 session
->display
= strdup(display
);
911 if (!session
->display
) {
917 if (!isempty(remote_user
)) {
918 session
->remote_user
= strdup(remote_user
);
919 if (!session
->remote_user
) {
925 if (!isempty(remote_host
)) {
926 session
->remote_host
= strdup(remote_host
);
927 if (!session
->remote_host
) {
933 if (!isempty(service
)) {
934 session
->service
= strdup(service
);
935 if (!session
->service
) {
941 if (!isempty(desktop
)) {
942 session
->desktop
= strdup(desktop
);
943 if (!session
->desktop
) {
950 r
= seat_attach_session(seat
, session
);
955 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
959 r
= session_start(session
, message
, error
);
963 r
= sd_bus_message_exit_container(message
);
967 session
->create_message
= sd_bus_message_ref(message
);
969 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
970 * session_send_create_reply(). */
976 session_add_to_gc_queue(session
);
979 user_add_to_gc_queue(user
);
984 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
985 Manager
*m
= userdata
;
993 r
= sd_bus_message_read(message
, "s", &name
);
997 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1001 r
= session_release(session
);
1005 return sd_bus_reply_method_return(message
, NULL
);
1008 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1009 Manager
*m
= userdata
;
1017 r
= sd_bus_message_read(message
, "s", &name
);
1021 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1025 /* PolicyKit is done by bus_session_method_activate() */
1027 return bus_session_method_activate(message
, session
, error
);
1030 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1031 const char *session_name
, *seat_name
;
1032 Manager
*m
= 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
= userdata
;
1080 r
= sd_bus_message_read(message
, "s", &name
);
1084 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1088 return bus_session_method_lock(message
, session
, error
);
1091 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1092 Manager
*m
= userdata
;
1098 r
= bus_verify_polkit_async(
1101 "org.freedesktop.login1.lock-sessions",
1105 &m
->polkit_registry
,
1110 return 1; /* Will call us back */
1112 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1116 return sd_bus_reply_method_return(message
, NULL
);
1119 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1121 Manager
*m
= userdata
;
1128 r
= sd_bus_message_read(message
, "s", &name
);
1132 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1136 return bus_session_method_kill(message
, session
, error
);
1139 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1140 Manager
*m
= userdata
;
1148 r
= sd_bus_message_read(message
, "u", &uid
);
1152 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1156 return bus_user_method_kill(message
, user
, error
);
1159 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1160 Manager
*m
= userdata
;
1168 r
= sd_bus_message_read(message
, "s", &name
);
1172 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1176 return bus_session_method_terminate(message
, session
, error
);
1179 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1180 Manager
*m
= userdata
;
1188 r
= sd_bus_message_read(message
, "u", &uid
);
1192 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1196 return bus_user_method_terminate(message
, user
, error
);
1199 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1200 Manager
*m
= userdata
;
1208 r
= sd_bus_message_read(message
, "s", &name
);
1212 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1216 return bus_seat_method_terminate(message
, seat
, error
);
1219 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1220 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1221 _cleanup_free_
char *cc
= NULL
;
1222 Manager
*m
= userdata
;
1223 int r
, b
, interactive
;
1226 uint32_t uid
, auth_uid
;
1231 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1235 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1236 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1240 if (!uid_is_valid(uid
)) {
1241 /* Note that we get the owner UID of the session or user unit,
1242 * not the actual client UID here! */
1243 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1248 /* owner_uid is racy, so for authorization we must use euid */
1249 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1256 return errno_or_else(ENOENT
);
1258 r
= bus_verify_polkit_async(
1261 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1262 "org.freedesktop.login1.set-user-linger",
1266 &m
->polkit_registry
,
1271 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1273 (void) mkdir_p_label("/var/lib/systemd", 0755);
1274 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1278 cc
= cescape(pw
->pw_name
);
1282 path
= strjoina("/var/lib/systemd/linger/", cc
);
1290 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1297 if (r
< 0 && errno
!= ENOENT
)
1300 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1302 user_add_to_gc_queue(u
);
1305 return sd_bus_reply_method_return(message
, NULL
);
1308 static int trigger_device(Manager
*m
, sd_device
*d
) {
1309 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1314 r
= sd_device_enumerator_new(&e
);
1318 r
= sd_device_enumerator_allow_uninitialized(e
);
1323 r
= sd_device_enumerator_add_match_parent(e
, d
);
1328 FOREACH_DEVICE(e
, d
) {
1329 r
= sd_device_trigger(d
, SD_DEVICE_CHANGE
);
1331 log_device_debug_errno(d
, r
, "Failed to trigger device, ignoring: %m");
1337 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1338 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1339 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1340 const char *id_for_seat
;
1347 r
= sd_device_new_from_syspath(&d
, sysfs
);
1351 if (sd_device_has_current_tag(d
, "seat") <= 0)
1354 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1357 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1360 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1363 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1364 r
= write_string_file_atomic_label(file
, rule
);
1368 return trigger_device(m
, d
);
1371 static int flush_devices(Manager
*m
) {
1372 _cleanup_closedir_
DIR *d
= NULL
;
1376 d
= opendir("/etc/udev/rules.d");
1378 if (errno
!= ENOENT
)
1379 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1381 FOREACH_DIRENT_ALL(de
, d
, break) {
1382 if (!dirent_is_file(de
))
1385 if (!startswith(de
->d_name
, "72-seat-"))
1388 if (!endswith(de
->d_name
, ".rules"))
1391 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1392 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1395 return trigger_device(m
, NULL
);
1398 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1399 const char *sysfs
, *seat
;
1400 Manager
*m
= userdata
;
1406 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1410 if (!path_is_normalized(sysfs
))
1411 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1412 if (!path_startswith(sysfs
, "/sys"))
1413 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1415 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1418 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1424 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1425 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1427 r
= bus_verify_polkit_async(
1430 "org.freedesktop.login1.attach-device",
1434 &m
->polkit_registry
,
1439 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1441 r
= attach_device(m
, seat
, sysfs
);
1445 return sd_bus_reply_method_return(message
, NULL
);
1448 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1449 Manager
*m
= userdata
;
1455 r
= sd_bus_message_read(message
, "b", &interactive
);
1459 r
= bus_verify_polkit_async(
1462 "org.freedesktop.login1.flush-devices",
1466 &m
->polkit_registry
,
1471 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1473 r
= flush_devices(m
);
1477 return sd_bus_reply_method_return(message
, NULL
);
1480 static int have_multiple_sessions(
1488 /* Check for other users' sessions. Greeter sessions do not
1489 * count, and non-login sessions do not count either. */
1490 HASHMAP_FOREACH(session
, m
->sessions
)
1491 if (session
->class == SESSION_USER
&&
1492 session
->user
->user_record
->uid
!= uid
)
1498 static int bus_manager_log_shutdown(
1500 const ActionTableItem
*a
) {
1502 const char *message
, *log_str
;
1507 message
= a
->message
;
1508 log_str
= a
->log_str
;
1511 message
= strjoina("MESSAGE=", message
);
1513 message
= "MESSAGE=System is shutting down";
1515 if (isempty(m
->wall_message
))
1516 message
= strjoina(message
, ".");
1518 message
= strjoina(message
, " (", m
->wall_message
, ").");
1521 log_str
= strjoina("SHUTDOWN=", log_str
);
1523 return log_struct(LOG_NOTICE
,
1524 "MESSAGE_ID=%s", a
->message_id
? a
->message_id
: SD_MESSAGE_SHUTDOWN_STR
,
1529 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1530 Manager
*m
= userdata
;
1535 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1539 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1544 if (until
<= now(CLOCK_MONOTONIC
))
1547 /* We want to ignore the lid switch for a while after each
1548 * suspend, and after boot-up. Hence let's install a timer for
1549 * this. As long as the event source exists we ignore the lid
1552 if (m
->lid_switch_ignore_event_source
) {
1555 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1562 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1564 r
= sd_event_add_time(
1566 &m
->lid_switch_ignore_event_source
,
1569 lid_switch_ignore_handler
, m
);
1574 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1575 int active
= _active
;
1578 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1580 return sd_bus_emit_signal(m
->bus
,
1581 "/org/freedesktop/login1",
1582 "org.freedesktop.login1.Manager",
1583 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1588 static int execute_shutdown_or_sleep(
1590 const ActionTableItem
*a
,
1591 sd_bus_error
*error
) {
1593 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1600 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
)
1601 bus_manager_log_shutdown(m
, a
);
1603 r
= bus_call_method(
1609 "ss", a
->target
, "replace-irreversibly");
1613 r
= sd_bus_message_read(reply
, "o", &p
);
1617 r
= free_and_strdup(&m
->action_job
, p
);
1621 m
->delayed_action
= a
;
1623 /* Make sure the lid switch is ignored for a while */
1624 manager_set_lid_switch_ignore(m
, usec_add(now(CLOCK_MONOTONIC
), m
->holdoff_timeout_usec
));
1629 /* Tell people that they now may take a lock again */
1630 (void) send_prepare_for(m
, a
->inhibit_what
, false);
1635 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1636 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1637 Inhibitor
*offending
= NULL
;
1642 if (!manager
->delayed_action
|| manager
->action_job
)
1645 if (manager_is_inhibited(manager
, manager
->delayed_action
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1646 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1651 (void) get_process_comm(offending
->pid
, &comm
);
1652 u
= uid_to_name(offending
->uid
);
1654 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1655 offending
->uid
, strna(u
),
1656 offending
->pid
, strna(comm
));
1659 /* Actually do the operation */
1660 r
= execute_shutdown_or_sleep(manager
, manager
->delayed_action
, &error
);
1662 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1663 bus_error_message(&error
, r
));
1665 manager
->delayed_action
= NULL
;
1668 return 1; /* We did some work. */
1671 static int manager_inhibit_timeout_handler(
1676 Manager
*manager
= userdata
;
1679 assert(manager
->inhibit_timeout_source
== s
);
1681 return manager_dispatch_delayed(manager
, true);
1684 static int delay_shutdown_or_sleep(
1686 const ActionTableItem
*a
) {
1693 if (m
->inhibit_timeout_source
) {
1694 r
= sd_event_source_set_time_relative(m
->inhibit_timeout_source
, m
->inhibit_delay_max
);
1696 return log_error_errno(r
, "sd_event_source_set_time_relative() failed: %m");
1698 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1700 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1702 r
= sd_event_add_time_relative(
1704 &m
->inhibit_timeout_source
,
1705 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, 0,
1706 manager_inhibit_timeout_handler
, m
);
1711 m
->delayed_action
= a
;
1716 int bus_manager_shutdown_or_sleep_now_or_later(
1718 const ActionTableItem
*a
,
1719 sd_bus_error
*error
) {
1721 _cleanup_free_
char *load_state
= NULL
;
1727 assert(!m
->action_job
);
1729 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
1733 if (!streq(load_state
, "loaded"))
1734 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1735 "Unit %s is %s, refusing operation.",
1736 a
->target
, load_state
);
1738 /* Tell everybody to prepare for shutdown/sleep */
1739 (void) send_prepare_for(m
, a
->inhibit_what
, true);
1742 m
->inhibit_delay_max
> 0 &&
1743 manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1746 /* Shutdown is delayed, keep in mind what we
1747 * want to do, and start a timeout */
1748 r
= delay_shutdown_or_sleep(m
, a
);
1750 /* Shutdown is not delayed, execute it
1752 r
= execute_shutdown_or_sleep(m
, a
, error
);
1757 static int verify_shutdown_creds(
1759 sd_bus_message
*message
,
1760 const ActionTableItem
*a
,
1762 sd_bus_error
*error
) {
1764 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1765 bool multiple_sessions
, blocked
, interactive
;
1773 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1777 r
= sd_bus_creds_get_euid(creds
, &uid
);
1781 r
= have_multiple_sessions(m
, uid
);
1785 multiple_sessions
= r
> 0;
1786 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1787 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
1789 if (multiple_sessions
) {
1790 r
= bus_verify_polkit_async(
1793 a
->polkit_action_multiple_sessions
,
1797 &m
->polkit_registry
,
1802 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1806 /* We don't check polkit for root here, because you can't be more privileged than root */
1807 if (uid
== 0 && (flags
& SD_LOGIND_ROOT_CHECK_INHIBITORS
))
1808 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
,
1809 "Access denied to root due to active block inhibitor");
1811 r
= bus_verify_polkit_async(message
,
1813 a
->polkit_action_ignore_inhibit
,
1817 &m
->polkit_registry
,
1822 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1825 if (!multiple_sessions
&& !blocked
) {
1826 r
= bus_verify_polkit_async(message
,
1832 &m
->polkit_registry
,
1837 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1843 static int setup_wall_message_timer(Manager
*m
, sd_bus_message
* message
) {
1844 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1847 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
1849 const char *tty
= NULL
;
1851 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
1852 (void) sd_bus_creds_get_tty(creds
, &tty
);
1854 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
1859 r
= manager_setup_wall_message_timer(m
);
1866 static int method_do_shutdown_or_sleep(
1868 sd_bus_message
*message
,
1869 const ActionTableItem
*a
,
1871 sd_bus_error
*error
) {
1881 /* New style method: with flags parameter (and interactive bool in the bus message header) */
1882 r
= sd_bus_message_read(message
, "t", &flags
);
1885 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
1886 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid flags parameter");
1887 if (manager_handle_for_item(a
) != HANDLE_REBOOT
&& (flags
& SD_LOGIND_REBOOT_VIA_KEXEC
))
1888 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Reboot via kexec is only applicable with reboot operations");
1890 /* Old style method: no flags parameter, but interactive bool passed as boolean in
1891 * payload. Let's convert this argument to the new-style flags parameter for our internal
1895 r
= sd_bus_message_read(message
, "b", &interactive
);
1899 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
1902 if ((flags
& SD_LOGIND_REBOOT_VIA_KEXEC
) && kexec_loaded())
1903 a
= manager_item_for_handle(HANDLE_KEXEC
);
1905 /* Don't allow multiple jobs being executed at the same time */
1906 if (m
->delayed_action
)
1907 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1908 "There's already a shutdown or sleep operation in progress");
1910 if (a
->sleep_operation
>= 0) {
1911 r
= can_sleep(a
->sleep_operation
);
1913 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1914 "Not enough swap space for hibernation");
1916 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1917 "Sleep verb \"%s\" not supported", sleep_operation_to_string(a
->sleep_operation
));
1922 r
= verify_shutdown_creds(m
, message
, a
, flags
, error
);
1926 /* reset case we're shorting a scheduled shutdown */
1927 m
->unlink_nologin
= false;
1928 reset_scheduled_shutdown(m
);
1930 m
->scheduled_shutdown_timeout
= 0;
1931 m
->scheduled_shutdown_type
= a
;
1933 (void) setup_wall_message_timer(m
, message
);
1935 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, error
);
1939 return sd_bus_reply_method_return(message
, NULL
);
1942 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1943 Manager
*m
= userdata
;
1945 return method_do_shutdown_or_sleep(
1947 manager_item_for_handle(HANDLE_POWEROFF
),
1948 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
1952 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1953 Manager
*m
= userdata
;
1955 return method_do_shutdown_or_sleep(
1957 manager_item_for_handle(HANDLE_REBOOT
),
1958 sd_bus_message_is_method_call(message
, NULL
, "RebootWithFlags"),
1962 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1963 Manager
*m
= userdata
;
1965 return method_do_shutdown_or_sleep(
1967 manager_item_for_handle(HANDLE_HALT
),
1968 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
1972 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1973 Manager
*m
= userdata
;
1975 return method_do_shutdown_or_sleep(
1977 manager_item_for_handle(HANDLE_SUSPEND
),
1978 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
1982 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1983 Manager
*m
= userdata
;
1985 return method_do_shutdown_or_sleep(
1987 manager_item_for_handle(HANDLE_HIBERNATE
),
1988 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
1992 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1993 Manager
*m
= userdata
;
1995 return method_do_shutdown_or_sleep(
1997 manager_item_for_handle(HANDLE_HYBRID_SLEEP
),
1998 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
2002 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2003 Manager
*m
= userdata
;
2005 return method_do_shutdown_or_sleep(
2007 manager_item_for_handle(HANDLE_SUSPEND_THEN_HIBERNATE
),
2008 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
2012 static int nologin_timeout_handler(
2017 Manager
*m
= userdata
;
2019 log_info("Creating /run/nologin, blocking further logins...");
2022 create_shutdown_run_nologin_or_warn() >= 0;
2027 static usec_t
nologin_timeout_usec(usec_t elapse
) {
2028 /* Issue /run/nologin five minutes before shutdown */
2029 return LESS_BY(elapse
, 5 * USEC_PER_MINUTE
);
2032 static int update_schedule_file(Manager
*m
) {
2033 _cleanup_free_
char *temp_path
= NULL
;
2034 _cleanup_fclose_
FILE *f
= NULL
;
2039 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
2041 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2043 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
2045 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2047 (void) fchmod(fileno(f
), 0644);
2053 m
->scheduled_shutdown_timeout
,
2054 m
->enable_wall_messages
,
2055 handle_action_to_string(manager_handle_for_item(m
->scheduled_shutdown_type
)));
2057 if (!isempty(m
->wall_message
)) {
2058 _cleanup_free_
char *t
= NULL
;
2060 t
= cescape(m
->wall_message
);
2066 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
2069 r
= fflush_and_check(f
);
2073 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2081 (void) unlink(temp_path
);
2082 (void) unlink("/run/systemd/shutdown/scheduled");
2084 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2087 static void reset_scheduled_shutdown(Manager
*m
) {
2090 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2091 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2092 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2094 m
->scheduled_shutdown_type
= NULL
;
2095 m
->shutdown_dry_run
= false;
2097 if (m
->unlink_nologin
) {
2098 (void) unlink_or_warn("/run/nologin");
2099 m
->unlink_nologin
= false;
2102 (void) unlink("/run/systemd/shutdown/scheduled");
2105 static int manager_scheduled_shutdown_handler(
2110 const ActionTableItem
*a
= NULL
;
2111 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2112 Manager
*m
= userdata
;
2117 a
= m
->scheduled_shutdown_type
;
2120 /* Don't allow multiple jobs being executed at the same time */
2121 if (m
->delayed_action
) {
2123 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", a
->target
);
2127 if (m
->shutdown_dry_run
) {
2128 /* We do not process delay inhibitors here. Otherwise, we
2129 * would have to be considered "in progress" (like the check
2130 * above) for some seconds after our admin has seen the final
2133 bus_manager_log_shutdown(m
, a
);
2134 log_info("Running in dry run, suppressing action.");
2135 reset_scheduled_shutdown(m
);
2140 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, m
->scheduled_shutdown_type
, &error
);
2142 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", a
->target
);
2149 reset_scheduled_shutdown(m
);
2153 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2154 Manager
*m
= userdata
;
2155 HandleAction handle
;
2156 const ActionTableItem
*a
;
2160 bool dry_run
= false;
2165 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2169 if (startswith(type
, "dry-")) {
2174 handle
= handle_action_from_string(type
);
2175 if (!IN_SET(handle
, HANDLE_POWEROFF
, HANDLE_REBOOT
, HANDLE_HALT
, HANDLE_KEXEC
))
2176 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2178 a
= manager_item_for_handle(handle
);
2180 assert(a
->polkit_action
);
2182 r
= verify_shutdown_creds(m
, message
, a
, 0, error
);
2186 if (m
->scheduled_shutdown_timeout_source
) {
2187 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2189 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2191 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2193 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2195 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2196 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2198 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2201 m
->scheduled_shutdown_type
= a
;
2202 m
->shutdown_dry_run
= dry_run
;
2204 if (m
->nologin_timeout_source
) {
2205 r
= sd_event_source_set_time(m
->nologin_timeout_source
, nologin_timeout_usec(elapse
));
2207 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2209 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2211 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2213 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2214 CLOCK_REALTIME
, nologin_timeout_usec(elapse
), 0, nologin_timeout_handler
, m
);
2216 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2219 m
->scheduled_shutdown_timeout
= elapse
;
2221 r
= setup_wall_message_timer(m
, message
);
2223 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2227 r
= update_schedule_file(m
);
2231 return sd_bus_reply_method_return(message
, NULL
);
2234 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2235 Manager
*m
= userdata
;
2236 const ActionTableItem
*a
;
2243 cancelled
= !IN_SET(manager_handle_for_item(m
->scheduled_shutdown_type
), HANDLE_IGNORE
, _HANDLE_ACTION_INVALID
);
2247 a
= m
->scheduled_shutdown_type
;
2248 if (!a
->polkit_action
)
2249 return sd_bus_error_set(error
, SD_BUS_ERROR_AUTH_FAILED
, "Unsupported shutdown type");
2251 r
= bus_verify_polkit_async(
2258 &m
->polkit_registry
,
2263 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2265 reset_scheduled_shutdown(m
);
2267 if (m
->enable_wall_messages
) {
2268 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2269 _cleanup_free_
char *username
= NULL
;
2270 const char *tty
= NULL
;
2273 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2275 (void) sd_bus_creds_get_uid(creds
, &uid
);
2276 (void) sd_bus_creds_get_tty(creds
, &tty
);
2279 username
= uid_to_name(uid
);
2280 utmp_wall("The system shutdown has been cancelled",
2281 username
, tty
, logind_wall_tty_filter
, m
);
2285 return sd_bus_reply_method_return(message
, "b", cancelled
);
2288 static int method_can_shutdown_or_sleep(
2290 sd_bus_message
*message
,
2291 const ActionTableItem
*a
,
2292 sd_bus_error
*error
) {
2294 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2295 bool multiple_sessions
, challenge
, blocked
;
2296 const char *result
= NULL
;
2304 if (a
->sleep_operation
>= 0) {
2305 r
= can_sleep(a
->sleep_operation
);
2306 if (IN_SET(r
, 0, -ENOSPC
))
2307 return sd_bus_reply_method_return(message
, "s", "na");
2312 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2316 r
= sd_bus_creds_get_euid(creds
, &uid
);
2320 r
= have_multiple_sessions(m
, uid
);
2324 multiple_sessions
= r
> 0;
2325 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2327 HandleAction handle
= handle_action_from_string(sleep_operation_to_string(a
->sleep_operation
));
2331 target
= manager_target_for_action(handle
);
2333 _cleanup_free_
char *load_state
= NULL
;
2335 r
= unit_load_state(m
->bus
, target
, &load_state
);
2339 if (!streq(load_state
, "loaded")) {
2346 if (multiple_sessions
) {
2347 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2354 result
= "challenge";
2360 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2367 } else if (challenge
) {
2368 if (!result
|| streq(result
, "yes"))
2369 result
= "challenge";
2374 if (!multiple_sessions
&& !blocked
) {
2375 /* If neither inhibit nor multiple sessions
2376 * apply then just check the normal policy */
2378 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, a
->polkit_action
, NULL
, UID_INVALID
, &challenge
, error
);
2385 result
= "challenge";
2391 return sd_bus_reply_method_return(message
, "s", result
);
2394 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2395 Manager
*m
= userdata
;
2397 return method_can_shutdown_or_sleep(
2398 m
, message
, manager_item_for_handle(HANDLE_POWEROFF
),
2402 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2403 Manager
*m
= userdata
;
2405 return method_can_shutdown_or_sleep(
2406 m
, message
, manager_item_for_handle(HANDLE_REBOOT
),
2410 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2411 Manager
*m
= userdata
;
2413 return method_can_shutdown_or_sleep(
2414 m
, message
, manager_item_for_handle(HANDLE_HALT
),
2418 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2419 Manager
*m
= userdata
;
2421 return method_can_shutdown_or_sleep(
2422 m
, message
, manager_item_for_handle(HANDLE_SUSPEND
),
2426 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2427 Manager
*m
= userdata
;
2429 return method_can_shutdown_or_sleep(
2430 m
, message
, manager_item_for_handle(HANDLE_HIBERNATE
),
2434 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2435 Manager
*m
= userdata
;
2437 return method_can_shutdown_or_sleep(
2438 m
, message
, manager_item_for_handle(HANDLE_HYBRID_SLEEP
),
2442 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2443 Manager
*m
= userdata
;
2445 return method_can_shutdown_or_sleep(
2446 m
, message
, manager_item_for_handle(HANDLE_SUSPEND_THEN_HIBERNATE
),
2450 static int property_get_reboot_parameter(
2453 const char *interface
,
2454 const char *property
,
2455 sd_bus_message
*reply
,
2457 sd_bus_error
*error
) {
2458 _cleanup_free_
char *parameter
= NULL
;
2465 r
= read_reboot_parameter(¶meter
);
2469 return sd_bus_message_append(reply
, "s", parameter
);
2472 static int method_set_reboot_parameter(
2473 sd_bus_message
*message
,
2475 sd_bus_error
*error
) {
2477 Manager
*m
= userdata
;
2484 r
= sd_bus_message_read(message
, "s", &arg
);
2488 r
= detect_container();
2492 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2493 "Reboot parameter not supported in containers, refusing.");
2495 r
= bus_verify_polkit_async(message
,
2497 "org.freedesktop.login1.set-reboot-parameter",
2501 &m
->polkit_registry
,
2506 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2508 r
= update_reboot_parameter_and_warn(arg
, false);
2512 return sd_bus_reply_method_return(message
, NULL
);
2515 static int method_can_reboot_parameter(
2516 sd_bus_message
*message
,
2518 sd_bus_error
*error
) {
2520 _unused_ Manager
*m
= userdata
;
2526 r
= detect_container();
2529 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2530 return sd_bus_reply_method_return(message
, "s", "na");
2532 return return_test_polkit(
2535 "org.freedesktop.login1.set-reboot-parameter",
2541 static int property_get_reboot_to_firmware_setup(
2544 const char *interface
,
2545 const char *property
,
2546 sd_bus_message
*reply
,
2548 sd_bus_error
*error
) {
2555 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2557 /* EFI case: let's see what is currently configured in the EFI variables */
2558 r
= efi_get_reboot_to_firmware();
2559 if (r
< 0 && r
!= -EOPNOTSUPP
)
2560 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2562 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2564 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2565 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2566 if (errno
!= ENOENT
)
2567 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2574 return sd_bus_message_append(reply
, "b", r
> 0);
2577 static int method_set_reboot_to_firmware_setup(
2578 sd_bus_message
*message
,
2580 sd_bus_error
*error
) {
2582 Manager
*m
= userdata
;
2589 r
= sd_bus_message_read(message
, "b", &b
);
2593 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2595 /* EFI case: let's see what the firmware supports */
2597 r
= efi_reboot_to_firmware_supported();
2598 if (r
== -EOPNOTSUPP
)
2599 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2605 } else if (r
<= 0) {
2606 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2609 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2611 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2613 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2616 r
= bus_verify_polkit_async(message
,
2618 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2622 &m
->polkit_registry
,
2627 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2630 r
= efi_set_reboot_to_firmware(b
);
2635 r
= touch("/run/systemd/reboot-to-firmware-setup");
2639 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2644 return sd_bus_reply_method_return(message
, NULL
);
2647 static int method_can_reboot_to_firmware_setup(
2648 sd_bus_message
*message
,
2650 sd_bus_error
*error
) {
2652 _unused_ Manager
*m
= userdata
;
2658 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2660 /* EFI case: let's see what the firmware supports */
2662 r
= efi_reboot_to_firmware_supported();
2664 if (r
!= -EOPNOTSUPP
)
2665 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2667 return sd_bus_reply_method_return(message
, "s", "na");
2670 } else if (r
<= 0) {
2671 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2674 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2676 return sd_bus_reply_method_return(message
, "s", "na");
2679 return return_test_polkit(
2682 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2688 static int property_get_reboot_to_boot_loader_menu(
2691 const char *interface
,
2692 const char *property
,
2693 sd_bus_message
*reply
,
2695 sd_bus_error
*error
) {
2697 uint64_t x
= UINT64_MAX
;
2704 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2706 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2708 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2709 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2710 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2713 r
= efi_loader_get_config_timeout_one_shot(&x
);
2716 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
2720 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2722 _cleanup_free_
char *v
= NULL
;
2724 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2726 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2729 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2731 r
= safe_atou64(v
, &x
);
2733 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2737 return sd_bus_message_append(reply
, "t", x
);
2740 static int method_set_reboot_to_boot_loader_menu(
2741 sd_bus_message
*message
,
2743 sd_bus_error
*error
) {
2745 Manager
*m
= userdata
;
2753 r
= sd_bus_message_read(message
, "t", &x
);
2757 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2761 /* EFI case: let's see if booting into boot loader menu is supported. */
2763 r
= efi_loader_get_features(&features
);
2765 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2766 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2767 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2771 } else if (r
<= 0) {
2772 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2775 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2777 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2779 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2782 r
= bus_verify_polkit_async(message
,
2784 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2788 &m
->polkit_registry
,
2793 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2796 if (x
== UINT64_MAX
)
2797 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), NULL
, 0);
2799 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2800 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2802 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderConfigTimeoutOneShot
), buf
);
2807 if (x
== UINT64_MAX
) {
2808 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
2811 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2813 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2815 r
= write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf
);
2821 return sd_bus_reply_method_return(message
, NULL
);
2824 static int method_can_reboot_to_boot_loader_menu(
2825 sd_bus_message
*message
,
2827 sd_bus_error
*error
) {
2829 _unused_ Manager
*m
= userdata
;
2835 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2837 uint64_t features
= 0;
2839 /* EFI case, let's see if booting into boot loader menu is supported. */
2841 r
= efi_loader_get_features(&features
);
2843 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2844 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2845 return sd_bus_reply_method_return(message
, "s", "na");
2847 } else if (r
<= 0) {
2848 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2851 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2853 return sd_bus_reply_method_return(message
, "s", "na");
2856 return return_test_polkit(
2859 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2865 static int property_get_reboot_to_boot_loader_entry(
2868 const char *interface
,
2869 const char *property
,
2870 sd_bus_message
*reply
,
2872 sd_bus_error
*error
) {
2874 _cleanup_free_
char *v
= NULL
;
2875 Manager
*m
= userdata
;
2876 const char *x
= NULL
;
2883 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2885 /* EFI case: let's read the LoaderEntryOneShot variable */
2887 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
2890 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
2892 x
= m
->efi_loader_entry_one_shot
;
2895 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2898 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2900 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2903 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
2904 } else if (!efi_loader_entry_name_valid(v
))
2905 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2910 return sd_bus_message_append(reply
, "s", x
);
2913 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
2914 _cleanup_(boot_config_free
) BootConfig config
= {};
2920 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2921 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2924 r
= manager_read_efi_boot_loader_entries(m
);
2926 (void) boot_entries_augment_from_loader(&config
, m
->efi_boot_loader_entries
);
2928 return boot_config_has_entry(&config
, id
);
2931 static int method_set_reboot_to_boot_loader_entry(
2932 sd_bus_message
*message
,
2934 sd_bus_error
*error
) {
2936 Manager
*m
= userdata
;
2944 r
= sd_bus_message_read(message
, "s", &v
);
2950 else if (efi_loader_entry_name_valid(v
)) {
2951 r
= boot_loader_entry_exists(m
, v
);
2955 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
2957 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
2959 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2963 /* EFI case: let's see if booting into boot loader entry is supported. */
2965 r
= efi_loader_get_features(&features
);
2967 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
2968 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
2969 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
2973 } else if (r
<= 0) {
2974 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
2977 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2979 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
2981 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
2984 r
= bus_verify_polkit_async(message
,
2986 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
2990 &m
->polkit_registry
,
2995 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3000 r
= efi_set_variable(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), NULL
, 0);
3002 r
= efi_set_variable_string(EFI_LOADER_VARIABLE(LoaderEntryOneShot
), v
);
3007 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3010 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3016 return sd_bus_reply_method_return(message
, NULL
);
3019 static int method_can_reboot_to_boot_loader_entry(
3020 sd_bus_message
*message
,
3022 sd_bus_error
*error
) {
3024 _unused_ Manager
*m
= userdata
;
3030 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3032 uint64_t features
= 0;
3034 /* EFI case, let's see if booting into boot loader entry is supported. */
3036 r
= efi_loader_get_features(&features
);
3038 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3039 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3040 return sd_bus_reply_method_return(message
, "s", "na");
3042 } else if (r
<= 0) {
3043 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3046 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3048 return sd_bus_reply_method_return(message
, "s", "na");
3051 return return_test_polkit(
3054 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3060 static int property_get_boot_loader_entries(
3063 const char *interface
,
3064 const char *property
,
3065 sd_bus_message
*reply
,
3067 sd_bus_error
*error
) {
3069 _cleanup_(boot_config_free
) BootConfig config
= {};
3070 Manager
*m
= userdata
;
3078 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3079 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3082 r
= manager_read_efi_boot_loader_entries(m
);
3084 (void) boot_entries_augment_from_loader(&config
, m
->efi_boot_loader_entries
);
3086 r
= sd_bus_message_open_container(reply
, 'a', "s");
3090 for (i
= 0; i
< config
.n_entries
; i
++) {
3091 BootEntry
*e
= config
.entries
+ i
;
3093 r
= sd_bus_message_append(reply
, "s", e
->id
);
3098 return sd_bus_message_close_container(reply
);
3101 static int method_set_wall_message(
3102 sd_bus_message
*message
,
3104 sd_bus_error
*error
) {
3107 Manager
*m
= userdata
;
3109 unsigned enable_wall_messages
;
3114 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3118 /* sysvinit has a 252 (256-(strlen(" \r\n")+1)) character
3119 * limit for the wall message. There is no real technical
3120 * need for that but doesn't make sense to store arbitrary
3122 * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72)
3124 if (strlen(wall_message
) > 252)
3127 /* Short-circuit the operation if the desired state is already in place, to
3128 * avoid an unnecessary polkit permission check. */
3129 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3130 m
->enable_wall_messages
== enable_wall_messages
)
3133 r
= bus_verify_polkit_async(message
,
3135 "org.freedesktop.login1.set-wall-message",
3139 &m
->polkit_registry
,
3144 return 1; /* Will call us back */
3146 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3150 m
->enable_wall_messages
= enable_wall_messages
;
3153 return sd_bus_reply_method_return(message
, NULL
);
3156 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3157 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3158 const char *who
, *why
, *what
, *mode
;
3159 _cleanup_free_
char *id
= NULL
;
3160 _cleanup_close_
int fifo_fd
= -1;
3161 Manager
*m
= userdata
;
3171 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3175 w
= inhibit_what_from_string(what
);
3177 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3178 "Invalid what specification %s", what
);
3180 mm
= inhibit_mode_from_string(mode
);
3182 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3183 "Invalid mode specification %s", mode
);
3185 /* Delay is only supported for shutdown/sleep */
3186 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3187 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3188 "Delay inhibitors only supported for shutdown and sleep");
3190 /* Don't allow taking delay locks while we are already
3191 * executing the operation. We shouldn't create the impression
3192 * that the lock was successful if the machine is about to go
3193 * down/suspend any moment. */
3194 if (m
->delayed_action
&& m
->delayed_action
->inhibit_what
& w
)
3195 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3196 "The operation inhibition has been requested for is already running");
3198 r
= bus_verify_polkit_async(
3201 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3202 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3203 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3204 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3205 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3206 w
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3207 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3208 "org.freedesktop.login1.inhibit-handle-lid-switch",
3212 &m
->polkit_registry
,
3217 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3219 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3223 r
= sd_bus_creds_get_euid(creds
, &uid
);
3227 r
= sd_bus_creds_get_pid(creds
, &pid
);
3231 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3232 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3233 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3239 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3242 } while (hashmap_get(m
->inhibitors
, id
));
3244 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3245 r
= manager_add_inhibitor(m
, id
, &i
);
3253 i
->why
= strdup(why
);
3254 i
->who
= strdup(who
);
3256 if (!i
->why
|| !i
->who
)
3259 fifo_fd
= inhibitor_create_fifo(i
);
3263 r
= inhibitor_start(i
);
3268 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3271 static const sd_bus_vtable manager_vtable
[] = {
3272 SD_BUS_VTABLE_START(0),
3274 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3275 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3277 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3278 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3279 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3280 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3281 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3282 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3283 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3284 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3285 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3286 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3287 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3288 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3289 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3290 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3291 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3292 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3293 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3294 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3295 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3296 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3297 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3298 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3299 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3300 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3301 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3302 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3303 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3304 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3305 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3306 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3307 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3308 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3309 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3310 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3311 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3312 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3313 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3314 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3315 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3317 SD_BUS_METHOD_WITH_ARGS("GetSession",
3318 SD_BUS_ARGS("s", session_id
),
3319 SD_BUS_RESULT("o", object_path
),
3321 SD_BUS_VTABLE_UNPRIVILEGED
),
3322 SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
3323 SD_BUS_ARGS("u", pid
),
3324 SD_BUS_RESULT("o", object_path
),
3325 method_get_session_by_pid
,
3326 SD_BUS_VTABLE_UNPRIVILEGED
),
3327 SD_BUS_METHOD_WITH_ARGS("GetUser",
3328 SD_BUS_ARGS("u", uid
),
3329 SD_BUS_RESULT("o", object_path
),
3331 SD_BUS_VTABLE_UNPRIVILEGED
),
3332 SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
3333 SD_BUS_ARGS("u", pid
),
3334 SD_BUS_RESULT("o", object_path
),
3335 method_get_user_by_pid
,
3336 SD_BUS_VTABLE_UNPRIVILEGED
),
3337 SD_BUS_METHOD_WITH_ARGS("GetSeat",
3338 SD_BUS_ARGS("s", seat_id
),
3339 SD_BUS_RESULT("o", object_path
),
3341 SD_BUS_VTABLE_UNPRIVILEGED
),
3342 SD_BUS_METHOD_WITH_ARGS("ListSessions",
3344 SD_BUS_RESULT("a(susso)", sessions
),
3345 method_list_sessions
,
3346 SD_BUS_VTABLE_UNPRIVILEGED
),
3347 SD_BUS_METHOD_WITH_ARGS("ListUsers",
3349 SD_BUS_RESULT("a(uso)", users
),
3351 SD_BUS_VTABLE_UNPRIVILEGED
),
3352 SD_BUS_METHOD_WITH_ARGS("ListSeats",
3354 SD_BUS_RESULT("a(so)", seats
),
3356 SD_BUS_VTABLE_UNPRIVILEGED
),
3357 SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
3359 SD_BUS_RESULT("a(ssssuu)", inhibitors
),
3360 method_list_inhibitors
,
3361 SD_BUS_VTABLE_UNPRIVILEGED
),
3362 SD_BUS_METHOD_WITH_ARGS("CreateSession",
3363 SD_BUS_ARGS("u", uid
,
3376 "a(sv)", properties
),
3377 SD_BUS_RESULT("s", session_id
,
3385 method_create_session
,
3387 SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
3388 SD_BUS_ARGS("s", session_id
),
3390 method_release_session
,
3392 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
3393 SD_BUS_ARGS("s", session_id
),
3395 method_activate_session
,
3396 SD_BUS_VTABLE_UNPRIVILEGED
),
3397 SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
3398 SD_BUS_ARGS("s", session_id
, "s", seat_id
),
3400 method_activate_session_on_seat
,
3401 SD_BUS_VTABLE_UNPRIVILEGED
),
3402 SD_BUS_METHOD_WITH_ARGS("LockSession",
3403 SD_BUS_ARGS("s", session_id
),
3405 method_lock_session
,
3406 SD_BUS_VTABLE_UNPRIVILEGED
),
3407 SD_BUS_METHOD_WITH_ARGS("UnlockSession",
3408 SD_BUS_ARGS("s", session_id
),
3410 method_lock_session
,
3411 SD_BUS_VTABLE_UNPRIVILEGED
),
3412 SD_BUS_METHOD("LockSessions",
3415 method_lock_sessions
,
3416 SD_BUS_VTABLE_UNPRIVILEGED
),
3417 SD_BUS_METHOD("UnlockSessions",
3420 method_lock_sessions
,
3421 SD_BUS_VTABLE_UNPRIVILEGED
),
3422 SD_BUS_METHOD_WITH_ARGS("KillSession",
3423 SD_BUS_ARGS("s", session_id
, "s", who
, "i", signal_number
),
3425 method_kill_session
,
3426 SD_BUS_VTABLE_UNPRIVILEGED
),
3427 SD_BUS_METHOD_WITH_ARGS("KillUser",
3428 SD_BUS_ARGS("u", uid
, "i", signal_number
),
3431 SD_BUS_VTABLE_UNPRIVILEGED
),
3432 SD_BUS_METHOD_WITH_ARGS("TerminateSession",
3433 SD_BUS_ARGS("s", session_id
),
3435 method_terminate_session
,
3436 SD_BUS_VTABLE_UNPRIVILEGED
),
3437 SD_BUS_METHOD_WITH_ARGS("TerminateUser",
3438 SD_BUS_ARGS("u", uid
),
3440 method_terminate_user
,
3441 SD_BUS_VTABLE_UNPRIVILEGED
),
3442 SD_BUS_METHOD_WITH_ARGS("TerminateSeat",
3443 SD_BUS_ARGS("s", seat_id
),
3445 method_terminate_seat
,
3446 SD_BUS_VTABLE_UNPRIVILEGED
),
3447 SD_BUS_METHOD_WITH_ARGS("SetUserLinger",
3448 SD_BUS_ARGS("u", uid
, "b", enable
, "b", interactive
),
3450 method_set_user_linger
,
3451 SD_BUS_VTABLE_UNPRIVILEGED
),
3452 SD_BUS_METHOD_WITH_ARGS("AttachDevice",
3453 SD_BUS_ARGS("s", seat_id
, "s", sysfs_path
, "b", interactive
),
3455 method_attach_device
,
3456 SD_BUS_VTABLE_UNPRIVILEGED
),
3457 SD_BUS_METHOD_WITH_ARGS("FlushDevices",
3458 SD_BUS_ARGS("b", interactive
),
3460 method_flush_devices
,
3461 SD_BUS_VTABLE_UNPRIVILEGED
),
3462 SD_BUS_METHOD_WITH_ARGS("PowerOff",
3463 SD_BUS_ARGS("b", interactive
),
3466 SD_BUS_VTABLE_UNPRIVILEGED
),
3467 SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags",
3468 SD_BUS_ARGS("t", flags
),
3471 SD_BUS_VTABLE_UNPRIVILEGED
),
3472 SD_BUS_METHOD_WITH_ARGS("Reboot",
3473 SD_BUS_ARGS("b", interactive
),
3476 SD_BUS_VTABLE_UNPRIVILEGED
),
3477 SD_BUS_METHOD_WITH_ARGS("RebootWithFlags",
3478 SD_BUS_ARGS("t", flags
),
3481 SD_BUS_VTABLE_UNPRIVILEGED
),
3482 SD_BUS_METHOD_WITH_ARGS("Halt",
3483 SD_BUS_ARGS("b", interactive
),
3486 SD_BUS_VTABLE_UNPRIVILEGED
),
3487 SD_BUS_METHOD_WITH_ARGS("HaltWithFlags",
3488 SD_BUS_ARGS("t", flags
),
3491 SD_BUS_VTABLE_UNPRIVILEGED
),
3492 SD_BUS_METHOD_WITH_ARGS("Suspend",
3493 SD_BUS_ARGS("b", interactive
),
3496 SD_BUS_VTABLE_UNPRIVILEGED
),
3497 SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags",
3498 SD_BUS_ARGS("t", flags
),
3501 SD_BUS_VTABLE_UNPRIVILEGED
),
3502 SD_BUS_METHOD_WITH_ARGS("Hibernate",
3503 SD_BUS_ARGS("b", interactive
),
3506 SD_BUS_VTABLE_UNPRIVILEGED
),
3507 SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags",
3508 SD_BUS_ARGS("t", flags
),
3511 SD_BUS_VTABLE_UNPRIVILEGED
),
3512 SD_BUS_METHOD_WITH_ARGS("HybridSleep",
3513 SD_BUS_ARGS("b", interactive
),
3515 method_hybrid_sleep
,
3516 SD_BUS_VTABLE_UNPRIVILEGED
),
3517 SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags",
3518 SD_BUS_ARGS("t", flags
),
3520 method_hybrid_sleep
,
3521 SD_BUS_VTABLE_UNPRIVILEGED
),
3522 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate",
3523 SD_BUS_ARGS("b", interactive
),
3525 method_suspend_then_hibernate
,
3526 SD_BUS_VTABLE_UNPRIVILEGED
),
3527 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags",
3528 SD_BUS_ARGS("t", flags
),
3530 method_suspend_then_hibernate
,
3531 SD_BUS_VTABLE_UNPRIVILEGED
),
3532 SD_BUS_METHOD_WITH_ARGS("CanPowerOff",
3534 SD_BUS_RESULT("s", result
),
3535 method_can_poweroff
,
3536 SD_BUS_VTABLE_UNPRIVILEGED
),
3537 SD_BUS_METHOD_WITH_ARGS("CanReboot",
3539 SD_BUS_RESULT("s", result
),
3541 SD_BUS_VTABLE_UNPRIVILEGED
),
3542 SD_BUS_METHOD_WITH_ARGS("CanHalt",
3544 SD_BUS_RESULT("s", result
),
3546 SD_BUS_VTABLE_UNPRIVILEGED
),
3547 SD_BUS_METHOD_WITH_ARGS("CanSuspend",
3549 SD_BUS_RESULT("s", result
),
3551 SD_BUS_VTABLE_UNPRIVILEGED
),
3552 SD_BUS_METHOD_WITH_ARGS("CanHibernate",
3554 SD_BUS_RESULT("s", result
),
3555 method_can_hibernate
,
3556 SD_BUS_VTABLE_UNPRIVILEGED
),
3557 SD_BUS_METHOD_WITH_ARGS("CanHybridSleep",
3559 SD_BUS_RESULT("s", result
),
3560 method_can_hybrid_sleep
,
3561 SD_BUS_VTABLE_UNPRIVILEGED
),
3562 SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate",
3564 SD_BUS_RESULT("s", result
),
3565 method_can_suspend_then_hibernate
,
3566 SD_BUS_VTABLE_UNPRIVILEGED
),
3567 SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown",
3568 SD_BUS_ARGS("s", type
, "t", usec
),
3570 method_schedule_shutdown
,
3571 SD_BUS_VTABLE_UNPRIVILEGED
),
3572 SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown",
3574 SD_BUS_RESULT("b", cancelled
),
3575 method_cancel_scheduled_shutdown
,
3576 SD_BUS_VTABLE_UNPRIVILEGED
),
3577 SD_BUS_METHOD_WITH_ARGS("Inhibit",
3578 SD_BUS_ARGS("s", what
, "s", who
, "s", why
, "s", mode
),
3579 SD_BUS_RESULT("h", pipe_fd
),
3581 SD_BUS_VTABLE_UNPRIVILEGED
),
3582 SD_BUS_METHOD_WITH_ARGS("CanRebootParameter",
3584 SD_BUS_RESULT("s", result
),
3585 method_can_reboot_parameter
,
3586 SD_BUS_VTABLE_UNPRIVILEGED
),
3587 SD_BUS_METHOD_WITH_ARGS("SetRebootParameter",
3588 SD_BUS_ARGS("s", parameter
),
3590 method_set_reboot_parameter
,
3591 SD_BUS_VTABLE_UNPRIVILEGED
),
3592 SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup",
3594 SD_BUS_RESULT("s", result
),
3595 method_can_reboot_to_firmware_setup
,
3596 SD_BUS_VTABLE_UNPRIVILEGED
),
3597 SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup",
3598 SD_BUS_ARGS("b", enable
),
3600 method_set_reboot_to_firmware_setup
,
3601 SD_BUS_VTABLE_UNPRIVILEGED
),
3602 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu",
3604 SD_BUS_RESULT("s", result
),
3605 method_can_reboot_to_boot_loader_menu
,
3606 SD_BUS_VTABLE_UNPRIVILEGED
),
3607 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu",
3608 SD_BUS_ARGS("t", timeout
),
3610 method_set_reboot_to_boot_loader_menu
,
3611 SD_BUS_VTABLE_UNPRIVILEGED
),
3612 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry",
3614 SD_BUS_RESULT("s", result
),
3615 method_can_reboot_to_boot_loader_entry
,
3616 SD_BUS_VTABLE_UNPRIVILEGED
),
3617 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry",
3618 SD_BUS_ARGS("s", boot_loader_entry
),
3620 method_set_reboot_to_boot_loader_entry
,
3621 SD_BUS_VTABLE_UNPRIVILEGED
),
3622 SD_BUS_METHOD_WITH_ARGS("SetWallMessage",
3623 SD_BUS_ARGS("s", wall_message
, "b", enable
),
3625 method_set_wall_message
,
3626 SD_BUS_VTABLE_UNPRIVILEGED
),
3628 SD_BUS_SIGNAL_WITH_ARGS("SessionNew",
3629 SD_BUS_ARGS("s", session_id
, "o", object_path
),
3631 SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved",
3632 SD_BUS_ARGS("s", session_id
, "o", object_path
),
3634 SD_BUS_SIGNAL_WITH_ARGS("UserNew",
3635 SD_BUS_ARGS("u", uid
, "o", object_path
),
3637 SD_BUS_SIGNAL_WITH_ARGS("UserRemoved",
3638 SD_BUS_ARGS("u", uid
, "o", object_path
),
3640 SD_BUS_SIGNAL_WITH_ARGS("SeatNew",
3641 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
3643 SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved",
3644 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
3646 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown",
3647 SD_BUS_ARGS("b", start
),
3649 SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep",
3650 SD_BUS_ARGS("b", start
),
3656 const BusObjectImplementation manager_object
= {
3657 "/org/freedesktop/login1",
3658 "org.freedesktop.login1.Manager",
3659 .vtables
= BUS_VTABLES(manager_vtable
),
3660 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
3665 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
3672 if (result
&& !streq(result
, "done")) {
3673 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3675 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3676 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
3677 return session_send_create_reply(s
, &e
);
3680 return session_send_create_reply(s
, NULL
);
3683 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3684 const char *path
, *result
, *unit
;
3685 Manager
*m
= userdata
;
3694 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3696 bus_log_parse_error(r
);
3700 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3701 assert(m
->delayed_action
);
3702 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->delayed_action
->inhibit_what
));
3704 /* Tell people that they now may take a lock again */
3705 (void) send_prepare_for(m
, m
->delayed_action
->inhibit_what
, false);
3707 m
->action_job
= mfree(m
->action_job
);
3708 m
->delayed_action
= NULL
;
3712 session
= hashmap_get(m
->session_units
, unit
);
3714 if (streq_ptr(path
, session
->scope_job
)) {
3715 session
->scope_job
= mfree(session
->scope_job
);
3716 (void) session_jobs_reply(session
, id
, unit
, result
);
3718 session_save(session
);
3719 user_save(session
->user
);
3722 session_add_to_gc_queue(session
);
3725 user
= hashmap_get(m
->user_units
, unit
);
3727 if (streq_ptr(path
, user
->service_job
)) {
3728 user
->service_job
= mfree(user
->service_job
);
3730 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3731 (void) session_jobs_reply(session
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
3736 user_add_to_gc_queue(user
);
3742 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3743 const char *path
, *unit
;
3744 Manager
*m
= userdata
;
3752 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3754 bus_log_parse_error(r
);
3758 session
= hashmap_get(m
->session_units
, unit
);
3760 session_add_to_gc_queue(session
);
3762 user
= hashmap_get(m
->user_units
, unit
);
3764 user_add_to_gc_queue(user
);
3769 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3770 _cleanup_free_
char *unit
= NULL
;
3771 Manager
*m
= userdata
;
3780 path
= sd_bus_message_get_path(message
);
3784 r
= unit_name_from_dbus_path(path
, &unit
);
3785 if (r
== -EINVAL
) /* not a unit */
3792 session
= hashmap_get(m
->session_units
, unit
);
3794 session_add_to_gc_queue(session
);
3796 user
= hashmap_get(m
->user_units
, unit
);
3798 user_add_to_gc_queue(user
);
3803 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3804 Manager
*m
= userdata
;
3811 r
= sd_bus_message_read(message
, "b", &b
);
3813 bus_log_parse_error(r
);
3820 /* systemd finished reloading, let's recheck all our sessions */
3821 log_debug("System manager has been reloaded, rechecking sessions...");
3823 HASHMAP_FOREACH(session
, m
->sessions
)
3824 session_add_to_gc_queue(session
);
3829 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3834 l
= strv_from_stdarg_alloca(property
);
3836 return sd_bus_emit_properties_changed_strv(
3838 "/org/freedesktop/login1",
3839 "org.freedesktop.login1.Manager",
3843 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3848 r
= sd_bus_message_read(reply
, "o", &j
);
3860 int manager_start_scope(
3865 const char *description
,
3868 const char *requires_mounts_for
,
3869 sd_bus_message
*more_properties
,
3870 sd_bus_error
*error
,
3873 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3882 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
3886 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3890 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3894 if (!isempty(slice
)) {
3895 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3900 if (!isempty(description
)) {
3901 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
3906 STRV_FOREACH(i
, wants
) {
3907 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
3912 STRV_FOREACH(i
, after
) {
3913 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3918 if (!empty_or_root(requires_mounts_for
)) {
3919 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3924 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3926 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3930 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3934 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3935 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_MAX
);
3937 return bus_log_create_error(r
);
3939 if (more_properties
) {
3940 /* If TasksMax also appears here, it will overwrite the default value set above */
3941 r
= sd_bus_message_copy(m
, more_properties
, true);
3946 r
= sd_bus_message_close_container(m
);
3950 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3954 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3958 return strdup_job(reply
, job
);
3961 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3962 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3969 r
= bus_call_method(
3975 "ss", unit
, "replace");
3979 return strdup_job(reply
, job
);
3982 int manager_stop_unit(Manager
*manager
, const char *unit
, const char *job_mode
, sd_bus_error
*error
, char **ret_job
) {
3983 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3990 r
= bus_call_method(
3996 "ss", unit
, job_mode
?: "fail");
3998 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
3999 BUS_ERROR_LOAD_FAILED
)) {
4002 sd_bus_error_free(error
);
4009 return strdup_job(reply
, ret_job
);
4012 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4013 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4014 _cleanup_free_
char *path
= NULL
;
4020 path
= unit_dbus_path_from_name(scope
);
4024 r
= sd_bus_call_method(
4026 "org.freedesktop.systemd1",
4028 "org.freedesktop.systemd1.Scope",
4034 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4035 BUS_ERROR_LOAD_FAILED
,
4036 BUS_ERROR_SCOPE_NOT_RUNNING
))
4039 sd_bus_error_move(ret_error
, &error
);
4046 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4050 return bus_call_method(
4056 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4059 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4060 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4061 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4062 _cleanup_free_
char *path
= NULL
;
4069 path
= unit_dbus_path_from_name(unit
);
4073 r
= sd_bus_get_property(
4075 "org.freedesktop.systemd1",
4077 "org.freedesktop.systemd1.Unit",
4083 /* systemd might have dropped off momentarily, let's
4084 * not make this an error */
4085 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4086 SD_BUS_ERROR_DISCONNECTED
))
4089 /* If the unit is already unloaded then it's not
4091 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4092 BUS_ERROR_LOAD_FAILED
))
4095 sd_bus_error_move(ret_error
, &error
);
4099 r
= sd_bus_message_read(reply
, "s", &state
);
4103 return !STR_IN_SET(state
, "inactive", "failed");
4106 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4107 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4108 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4114 r
= sd_bus_get_property(
4116 "org.freedesktop.systemd1",
4118 "org.freedesktop.systemd1.Job",
4124 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4125 SD_BUS_ERROR_DISCONNECTED
))
4128 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4131 sd_bus_error_move(ret_error
, &error
);
4135 /* We don't actually care about the state really. The fact
4136 * that we could read the job state is enough for us */