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-dbus.h"
33 #include "logind-seat-dbus.h"
34 #include "logind-session-dbus.h"
35 #include "logind-user-dbus.h"
37 #include "missing_capability.h"
39 #include "parse-util.h"
40 #include "path-util.h"
41 #include "process-util.h"
42 #include "reboot-util.h"
43 #include "selinux-util.h"
44 #include "sleep-config.h"
46 #include "stdio-util.h"
48 #include "terminal-util.h"
49 #include "tmpfile-util.h"
50 #include "unit-name.h"
51 #include "user-util.h"
52 #include "utmp-wtmp.h"
55 static int get_sender_session(
57 sd_bus_message
*message
,
62 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
63 Session
*session
= NULL
;
67 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
68 * and returns that. If not and 'consult_display' is true, this returns the display session of the
69 * owning user of the caller. */
71 r
= sd_bus_query_sender_creds(message
,
72 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
73 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
77 r
= sd_bus_creds_get_session(creds
, &name
);
82 if (consult_display
) {
85 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
92 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
94 session
= user
->display
;
98 session
= hashmap_get(m
->sessions
, name
);
101 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
103 "Caller does not belong to any known session and doesn't own any suitable session." :
104 "Caller does not belong to any known session.");
110 int manager_get_session_from_creds(
112 sd_bus_message
*message
,
122 if (SEAT_IS_SELF(name
)) /* the caller's own session */
123 return get_sender_session(m
, message
, false, error
, ret
);
124 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
125 return get_sender_session(m
, message
, true, error
, ret
);
127 session
= hashmap_get(m
->sessions
, name
);
129 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
135 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
136 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
141 /* Note that we get the owner UID of the session, not the actual client UID here! */
142 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
146 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
153 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
156 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
157 "Caller does not belong to any logged in or lingering user");
163 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
169 if (!uid_is_valid(uid
))
170 return get_sender_user(m
, message
, error
, ret
);
172 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
174 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
175 "User ID "UID_FMT
" is not logged in or lingering", uid
);
181 int manager_get_seat_from_creds(
183 sd_bus_message
*message
,
194 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
197 /* Use these special seat names as session names */
198 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
202 seat
= session
->seat
;
204 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
206 seat
= hashmap_get(m
->seats
, name
);
208 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
215 static int return_test_polkit(
216 sd_bus_message
*message
,
219 const char **details
,
227 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
234 result
= "challenge";
238 return sd_bus_reply_method_return(message
, "s", result
);
241 static int property_get_idle_hint(
244 const char *interface
,
245 const char *property
,
246 sd_bus_message
*reply
,
248 sd_bus_error
*error
) {
250 Manager
*m
= userdata
;
256 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
259 static int property_get_idle_since_hint(
262 const char *interface
,
263 const char *property
,
264 sd_bus_message
*reply
,
266 sd_bus_error
*error
) {
268 Manager
*m
= userdata
;
269 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
275 manager_get_idle_hint(m
, &t
);
277 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
280 static int property_get_inhibited(
283 const char *interface
,
284 const char *property
,
285 sd_bus_message
*reply
,
287 sd_bus_error
*error
) {
289 Manager
*m
= userdata
;
296 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
298 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
301 static int property_get_preparing(
304 const char *interface
,
305 const char *property
,
306 sd_bus_message
*reply
,
308 sd_bus_error
*error
) {
310 Manager
*m
= userdata
;
317 if (streq(property
, "PreparingForShutdown"))
318 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
320 b
= m
->action_what
& INHIBIT_SLEEP
;
322 return sd_bus_message_append(reply
, "b", b
);
325 static int property_get_scheduled_shutdown(
328 const char *interface
,
329 const char *property
,
330 sd_bus_message
*reply
,
332 sd_bus_error
*error
) {
334 Manager
*m
= userdata
;
341 r
= sd_bus_message_open_container(reply
, 'r', "st");
345 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
349 return sd_bus_message_close_container(reply
);
352 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
353 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
354 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
355 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
356 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
357 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
359 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
360 _cleanup_free_
char *p
= NULL
;
361 Manager
*m
= userdata
;
369 r
= sd_bus_message_read(message
, "s", &name
);
373 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
377 p
= session_bus_path(session
);
381 return sd_bus_reply_method_return(message
, "o", p
);
384 /* Get login session of a process. This is not what you are looking for these days,
385 * as apps may instead belong to a user service unit. This includes terminal
386 * emulators and hence command-line apps. */
387 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
388 _cleanup_free_
char *p
= NULL
;
389 Session
*session
= NULL
;
390 Manager
*m
= userdata
;
397 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
399 r
= sd_bus_message_read(message
, "u", &pid
);
406 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
410 r
= manager_get_session_by_pid(m
, pid
, &session
);
415 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
416 "PID "PID_FMT
" does not belong to any known session", pid
);
419 p
= session_bus_path(session
);
423 return sd_bus_reply_method_return(message
, "o", p
);
426 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
427 _cleanup_free_
char *p
= NULL
;
428 Manager
*m
= userdata
;
436 r
= sd_bus_message_read(message
, "u", &uid
);
440 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
444 p
= user_bus_path(user
);
448 return sd_bus_reply_method_return(message
, "o", p
);
451 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
452 _cleanup_free_
char *p
= NULL
;
453 Manager
*m
= userdata
;
461 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
463 r
= sd_bus_message_read(message
, "u", &pid
);
470 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
474 r
= manager_get_user_by_pid(m
, pid
, &user
);
478 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
479 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
483 p
= user_bus_path(user
);
487 return sd_bus_reply_method_return(message
, "o", p
);
490 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
491 _cleanup_free_
char *p
= NULL
;
492 Manager
*m
= userdata
;
500 r
= sd_bus_message_read(message
, "s", &name
);
504 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
508 p
= seat_bus_path(seat
);
512 return sd_bus_reply_method_return(message
, "o", p
);
515 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
516 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
517 Manager
*m
= userdata
;
524 r
= sd_bus_message_new_method_return(message
, &reply
);
528 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
532 HASHMAP_FOREACH(session
, m
->sessions
) {
533 _cleanup_free_
char *p
= NULL
;
535 p
= session_bus_path(session
);
539 r
= sd_bus_message_append(reply
, "(susso)",
541 (uint32_t) session
->user
->user_record
->uid
,
542 session
->user
->user_record
->user_name
,
543 session
->seat
? session
->seat
->id
: "",
549 r
= sd_bus_message_close_container(reply
);
553 return sd_bus_send(NULL
, reply
, NULL
);
556 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
557 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
558 Manager
*m
= userdata
;
565 r
= sd_bus_message_new_method_return(message
, &reply
);
569 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
573 HASHMAP_FOREACH(user
, m
->users
) {
574 _cleanup_free_
char *p
= NULL
;
576 p
= user_bus_path(user
);
580 r
= sd_bus_message_append(reply
, "(uso)",
581 (uint32_t) user
->user_record
->uid
,
582 user
->user_record
->user_name
,
588 r
= sd_bus_message_close_container(reply
);
592 return sd_bus_send(NULL
, reply
, NULL
);
595 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
596 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
597 Manager
*m
= userdata
;
604 r
= sd_bus_message_new_method_return(message
, &reply
);
608 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
612 HASHMAP_FOREACH(seat
, m
->seats
) {
613 _cleanup_free_
char *p
= NULL
;
615 p
= seat_bus_path(seat
);
619 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
624 r
= sd_bus_message_close_container(reply
);
628 return sd_bus_send(NULL
, reply
, NULL
);
631 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
632 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
633 Manager
*m
= userdata
;
634 Inhibitor
*inhibitor
;
640 r
= sd_bus_message_new_method_return(message
, &reply
);
644 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
648 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
) {
650 r
= sd_bus_message_append(reply
, "(ssssuu)",
651 strempty(inhibit_what_to_string(inhibitor
->what
)),
652 strempty(inhibitor
->who
),
653 strempty(inhibitor
->why
),
654 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
655 (uint32_t) inhibitor
->uid
,
656 (uint32_t) inhibitor
->pid
);
661 r
= sd_bus_message_close_container(reply
);
665 return sd_bus_send(NULL
, reply
, NULL
);
668 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
669 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
670 _cleanup_free_
char *id
= NULL
;
671 Session
*session
= NULL
;
672 uint32_t audit_id
= 0;
673 Manager
*m
= userdata
;
687 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
688 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
690 r
= sd_bus_message_read(message
, "uusssssussbss",
691 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
692 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
696 if (!uid_is_valid(uid
))
697 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
698 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
699 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
702 t
= _SESSION_TYPE_INVALID
;
704 t
= session_type_from_string(type
);
706 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
707 "Invalid session type %s", type
);
711 c
= _SESSION_CLASS_INVALID
;
713 c
= session_class_from_string(class);
715 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
716 "Invalid session class %s", class);
719 if (isempty(desktop
))
722 if (!string_is_safe(desktop
))
723 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
724 "Invalid desktop string %s", desktop
);
730 seat
= hashmap_get(m
->seats
, cseat
);
732 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
733 "No seat '%s' known", cseat
);
736 if (tty_is_vc(tty
)) {
741 else if (seat
!= m
->seat0
)
742 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
743 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
745 v
= vtnr_from_tty(tty
);
747 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
748 "Cannot determine VT number from virtual console TTY %s", tty
);
752 else if (vtnr
!= (uint32_t) v
)
753 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
754 "Specified TTY and VT number do not match");
756 } else if (tty_is_console(tty
)) {
760 else if (seat
!= m
->seat0
)
761 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
762 "Console TTY specified but seat is not seat0");
765 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
766 "Console TTY specified but VT number is not 0");
770 if (seat_has_vts(seat
)) {
771 if (vtnr
<= 0 || vtnr
> 63)
772 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
773 "VT number out of range");
776 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
777 "Seat has no VTs but VT number not 0");
781 if (t
== _SESSION_TYPE_INVALID
) {
782 if (!isempty(display
))
784 else if (!isempty(tty
))
787 t
= SESSION_UNSPECIFIED
;
790 if (c
== _SESSION_CLASS_INVALID
) {
791 if (t
== SESSION_UNSPECIFIED
)
792 c
= SESSION_BACKGROUND
;
798 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
800 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
804 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
809 /* Check if we are already in a logind session. Or if we are in user@.service
810 * which is a special PAM session that avoids creating a logind session. */
811 r
= manager_get_user_by_pid(m
, leader
, NULL
);
815 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
816 "Already running in a session or user slice");
819 * Old gdm and lightdm start the user-session on the same VT as
820 * the greeter session. But they destroy the greeter session
821 * after the user-session and want the user-session to take
822 * over the VT. We need to support this for
823 * backwards-compatibility, so make sure we allow new sessions
824 * on a VT that a greeter is running on. Furthermore, to allow
825 * re-logins, we have to allow a greeter to take over a used VT for
826 * the exact same reasons.
828 if (c
!= SESSION_GREETER
&&
830 vtnr
< m
->seat0
->position_count
&&
831 m
->seat0
->positions
[vtnr
] &&
832 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
833 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
835 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
836 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
837 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
840 (void) audit_session_from_pid(leader
, &audit_id
);
841 if (audit_session_is_valid(audit_id
)) {
842 /* Keep our session IDs and the audit session IDs in sync */
844 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
847 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
848 * not trust the audit data and let's better register a new ID */
849 if (hashmap_contains(m
->sessions
, id
)) {
850 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
851 audit_id
= AUDIT_SESSION_INVALID
;
860 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
863 } while (hashmap_contains(m
->sessions
, id
));
866 /* The generated names should not clash with 'auto' or 'self' */
867 assert(!SESSION_IS_SELF(id
));
868 assert(!SESSION_IS_AUTO(id
));
870 /* If we are not watching utmp already, try again */
871 manager_reconnect_utmp(m
);
873 r
= manager_add_user_by_uid(m
, uid
, &user
);
877 r
= manager_add_session(m
, id
, &session
);
881 session_set_user(session
, user
);
882 r
= session_set_leader(session
, leader
);
886 session
->original_type
= session
->type
= t
;
888 session
->remote
= remote
;
889 session
->vtnr
= vtnr
;
892 session
->tty
= strdup(tty
);
898 session
->tty_validity
= TTY_FROM_PAM
;
901 if (!isempty(display
)) {
902 session
->display
= strdup(display
);
903 if (!session
->display
) {
909 if (!isempty(remote_user
)) {
910 session
->remote_user
= strdup(remote_user
);
911 if (!session
->remote_user
) {
917 if (!isempty(remote_host
)) {
918 session
->remote_host
= strdup(remote_host
);
919 if (!session
->remote_host
) {
925 if (!isempty(service
)) {
926 session
->service
= strdup(service
);
927 if (!session
->service
) {
933 if (!isempty(desktop
)) {
934 session
->desktop
= strdup(desktop
);
935 if (!session
->desktop
) {
942 r
= seat_attach_session(seat
, session
);
947 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
951 r
= session_start(session
, message
, error
);
955 r
= sd_bus_message_exit_container(message
);
959 session
->create_message
= sd_bus_message_ref(message
);
961 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
962 * session_send_create_reply(). */
968 session_add_to_gc_queue(session
);
971 user_add_to_gc_queue(user
);
976 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
977 Manager
*m
= userdata
;
985 r
= sd_bus_message_read(message
, "s", &name
);
989 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
993 r
= session_release(session
);
997 return sd_bus_reply_method_return(message
, NULL
);
1000 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1001 Manager
*m
= userdata
;
1009 r
= sd_bus_message_read(message
, "s", &name
);
1013 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1017 /* PolicyKit is done by bus_session_method_activate() */
1019 return bus_session_method_activate(message
, session
, error
);
1022 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1023 const char *session_name
, *seat_name
;
1024 Manager
*m
= userdata
;
1032 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1034 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1038 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1042 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1046 if (session
->seat
!= seat
)
1047 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1048 "Session %s not on seat %s", session_name
, seat_name
);
1050 r
= bus_verify_polkit_async(
1053 "org.freedesktop.login1.chvt",
1057 &m
->polkit_registry
,
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 _cleanup_free_
char *t
= NULL
;
1332 r
= sd_device_get_syspath(d
, &p
);
1336 t
= path_join(p
, "uevent");
1340 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1346 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1347 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1348 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1349 const char *id_for_seat
;
1356 r
= sd_device_new_from_syspath(&d
, sysfs
);
1360 if (sd_device_has_current_tag(d
, "seat") <= 0)
1363 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1366 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1369 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1372 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1373 r
= write_string_file_atomic_label(file
, rule
);
1377 return trigger_device(m
, d
);
1380 static int flush_devices(Manager
*m
) {
1381 _cleanup_closedir_
DIR *d
;
1385 d
= opendir("/etc/udev/rules.d");
1387 if (errno
!= ENOENT
)
1388 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1392 FOREACH_DIRENT_ALL(de
, d
, break) {
1393 dirent_ensure_type(d
, de
);
1394 if (!dirent_is_file(de
))
1397 if (!startswith(de
->d_name
, "72-seat-"))
1400 if (!endswith(de
->d_name
, ".rules"))
1403 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1404 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1408 return trigger_device(m
, NULL
);
1411 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1412 const char *sysfs
, *seat
;
1413 Manager
*m
= userdata
;
1419 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1423 if (!path_is_normalized(sysfs
))
1424 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1425 if (!path_startswith(sysfs
, "/sys"))
1426 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1428 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1431 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1437 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1438 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1440 r
= bus_verify_polkit_async(
1443 "org.freedesktop.login1.attach-device",
1447 &m
->polkit_registry
,
1452 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1454 r
= attach_device(m
, seat
, sysfs
);
1458 return sd_bus_reply_method_return(message
, NULL
);
1461 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1462 Manager
*m
= userdata
;
1468 r
= sd_bus_message_read(message
, "b", &interactive
);
1472 r
= bus_verify_polkit_async(
1475 "org.freedesktop.login1.flush-devices",
1479 &m
->polkit_registry
,
1484 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1486 r
= flush_devices(m
);
1490 return sd_bus_reply_method_return(message
, NULL
);
1493 static int have_multiple_sessions(
1501 /* Check for other users' sessions. Greeter sessions do not
1502 * count, and non-login sessions do not count either. */
1503 HASHMAP_FOREACH(session
, m
->sessions
)
1504 if (session
->class == SESSION_USER
&&
1505 session
->user
->user_record
->uid
!= uid
)
1511 static int bus_manager_log_shutdown(
1513 const char *unit_name
) {
1520 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1521 p
= "MESSAGE=System is powering down";
1522 q
= "SHUTDOWN=power-off";
1523 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1524 p
= "MESSAGE=System is rebooting";
1525 q
= "SHUTDOWN=reboot";
1526 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1527 p
= "MESSAGE=System is halting";
1528 q
= "SHUTDOWN=halt";
1529 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1530 p
= "MESSAGE=System is rebooting with kexec";
1531 q
= "SHUTDOWN=kexec";
1533 p
= "MESSAGE=System is shutting down";
1537 if (isempty(m
->wall_message
))
1538 p
= strjoina(p
, ".");
1540 p
= strjoina(p
, " (", m
->wall_message
, ").");
1542 return log_struct(LOG_NOTICE
,
1543 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1548 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1549 Manager
*m
= userdata
;
1554 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1558 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1563 if (until
<= now(CLOCK_MONOTONIC
))
1566 /* We want to ignore the lid switch for a while after each
1567 * suspend, and after boot-up. Hence let's install a timer for
1568 * this. As long as the event source exists we ignore the lid
1571 if (m
->lid_switch_ignore_event_source
) {
1574 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1581 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1583 r
= sd_event_add_time(
1585 &m
->lid_switch_ignore_event_source
,
1588 lid_switch_ignore_handler
, m
);
1593 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1594 int active
= _active
;
1597 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1599 return sd_bus_emit_signal(m
->bus
,
1600 "/org/freedesktop/login1",
1601 "org.freedesktop.login1.Manager",
1602 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1607 static int execute_shutdown_or_sleep(
1610 const char *unit_name
,
1611 sd_bus_error
*error
) {
1613 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1619 assert(w
< _INHIBIT_WHAT_MAX
);
1622 if (w
== INHIBIT_SHUTDOWN
)
1623 bus_manager_log_shutdown(m
, unit_name
);
1625 r
= bus_call_method(
1631 "ss", unit_name
, "replace-irreversibly");
1635 r
= sd_bus_message_read(reply
, "o", &p
);
1639 r
= free_and_strdup(&m
->action_job
, p
);
1643 m
->action_unit
= unit_name
;
1646 /* Make sure the lid switch is ignored for a while */
1647 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1652 /* Tell people that they now may take a lock again */
1653 (void) send_prepare_for(m
, w
, false);
1658 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1660 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1661 Inhibitor
*offending
= NULL
;
1666 if (manager
->action_what
== 0 || manager
->action_job
)
1669 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1670 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1675 (void) get_process_comm(offending
->pid
, &comm
);
1676 u
= uid_to_name(offending
->uid
);
1678 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1679 offending
->uid
, strna(u
),
1680 offending
->pid
, strna(comm
));
1683 /* Actually do the operation */
1684 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1686 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1687 bus_error_message(&error
, r
));
1689 manager
->action_unit
= NULL
;
1690 manager
->action_what
= 0;
1697 static int manager_inhibit_timeout_handler(
1702 Manager
*manager
= userdata
;
1706 assert(manager
->inhibit_timeout_source
== s
);
1708 r
= manager_dispatch_delayed(manager
, true);
1709 return (r
< 0) ? r
: 0;
1712 static int delay_shutdown_or_sleep(
1715 const char *unit_name
) {
1721 assert(w
< _INHIBIT_WHAT_MAX
);
1724 if (m
->inhibit_timeout_source
) {
1725 r
= sd_event_source_set_time_relative(m
->inhibit_timeout_source
, m
->inhibit_delay_max
);
1727 return log_error_errno(r
, "sd_event_source_set_time_relative() failed: %m");
1729 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1731 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1733 r
= sd_event_add_time_relative(
1735 &m
->inhibit_timeout_source
,
1736 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, 0,
1737 manager_inhibit_timeout_handler
, m
);
1742 m
->action_unit
= unit_name
;
1748 int bus_manager_shutdown_or_sleep_now_or_later(
1750 const char *unit_name
,
1752 sd_bus_error
*error
) {
1754 _cleanup_free_
char *load_state
= NULL
;
1761 assert(w
< _INHIBIT_WHAT_MAX
);
1762 assert(!m
->action_job
);
1764 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1768 if (!streq(load_state
, "loaded"))
1769 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1770 "Unit %s is %s, refusing operation.",
1771 unit_name
, load_state
);
1773 /* Tell everybody to prepare for shutdown/sleep */
1774 (void) send_prepare_for(m
, w
, true);
1777 m
->inhibit_delay_max
> 0 &&
1778 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1781 /* Shutdown is delayed, keep in mind what we
1782 * want to do, and start a timeout */
1783 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1785 /* Shutdown is not delayed, execute it
1787 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1792 static int verify_shutdown_creds(
1794 sd_bus_message
*message
,
1797 const char *action_multiple_sessions
,
1798 const char *action_ignore_inhibit
,
1800 sd_bus_error
*error
) {
1802 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1803 bool multiple_sessions
, blocked
, interactive
;
1810 assert(w
<= _INHIBIT_WHAT_MAX
);
1812 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1816 r
= sd_bus_creds_get_euid(creds
, &uid
);
1820 r
= have_multiple_sessions(m
, uid
);
1824 multiple_sessions
= r
> 0;
1825 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1826 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
1828 if (multiple_sessions
&& action_multiple_sessions
) {
1829 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1833 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1837 /* We don't check polkit for root here, because you can't be more privileged than root */
1838 if (uid
== 0 && (flags
& SD_LOGIND_ROOT_CHECK_INHIBITORS
))
1839 return sd_bus_error_setf(error
, SD_BUS_ERROR_ACCESS_DENIED
,
1840 "Access denied to root due to active block inhibitor");
1842 if (action_ignore_inhibit
) {
1843 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1847 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1851 if (!multiple_sessions
&& !blocked
&& action
) {
1852 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1856 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1862 static int method_do_shutdown_or_sleep(
1864 sd_bus_message
*message
,
1865 const char *unit_name
,
1868 const char *action_multiple_sessions
,
1869 const char *action_ignore_inhibit
,
1870 const char *sleep_verb
,
1872 sd_bus_error
*error
) {
1881 assert(w
<= _INHIBIT_WHAT_MAX
);
1884 /* New style method: with flags parameter (and interactive bool in the bus message header) */
1885 r
= sd_bus_message_read(message
, "t", &flags
);
1888 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
1889 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid flags parameter");
1891 /* Old style method: no flags parameter, but interactive bool passed as boolean in
1892 * payload. Let's convert this argument to the new-style flags parameter for our internal
1896 r
= sd_bus_message_read(message
, "b", &interactive
);
1900 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
1903 /* Don't allow multiple jobs being executed at the same time */
1904 if (m
->action_what
> 0)
1905 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1906 "There's already a shutdown or sleep operation in progress");
1909 r
= can_sleep(sleep_verb
);
1911 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1912 "Not enough swap space for hibernation");
1914 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1915 "Sleep verb \"%s\" not supported", sleep_verb
);
1920 r
= verify_shutdown_creds(m
, message
, w
, action
, action_multiple_sessions
,
1921 action_ignore_inhibit
, flags
, error
);
1925 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1929 return sd_bus_reply_method_return(message
, NULL
);
1932 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1933 Manager
*m
= userdata
;
1935 return method_do_shutdown_or_sleep(
1937 SPECIAL_POWEROFF_TARGET
,
1939 "org.freedesktop.login1.power-off",
1940 "org.freedesktop.login1.power-off-multiple-sessions",
1941 "org.freedesktop.login1.power-off-ignore-inhibit",
1943 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
1947 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1948 Manager
*m
= userdata
;
1950 return method_do_shutdown_or_sleep(
1952 SPECIAL_REBOOT_TARGET
,
1954 "org.freedesktop.login1.reboot",
1955 "org.freedesktop.login1.reboot-multiple-sessions",
1956 "org.freedesktop.login1.reboot-ignore-inhibit",
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 SPECIAL_HALT_TARGET
,
1969 "org.freedesktop.login1.halt",
1970 "org.freedesktop.login1.halt-multiple-sessions",
1971 "org.freedesktop.login1.halt-ignore-inhibit",
1973 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
1977 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1978 Manager
*m
= userdata
;
1980 return method_do_shutdown_or_sleep(
1982 SPECIAL_SUSPEND_TARGET
,
1984 "org.freedesktop.login1.suspend",
1985 "org.freedesktop.login1.suspend-multiple-sessions",
1986 "org.freedesktop.login1.suspend-ignore-inhibit",
1988 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
1992 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1993 Manager
*m
= userdata
;
1995 return method_do_shutdown_or_sleep(
1997 SPECIAL_HIBERNATE_TARGET
,
1999 "org.freedesktop.login1.hibernate",
2000 "org.freedesktop.login1.hibernate-multiple-sessions",
2001 "org.freedesktop.login1.hibernate-ignore-inhibit",
2003 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
2007 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2008 Manager
*m
= userdata
;
2010 return method_do_shutdown_or_sleep(
2012 SPECIAL_HYBRID_SLEEP_TARGET
,
2014 "org.freedesktop.login1.hibernate",
2015 "org.freedesktop.login1.hibernate-multiple-sessions",
2016 "org.freedesktop.login1.hibernate-ignore-inhibit",
2018 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
2022 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2023 Manager
*m
= userdata
;
2025 return method_do_shutdown_or_sleep(
2027 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
2029 "org.freedesktop.login1.hibernate",
2030 "org.freedesktop.login1.hibernate-multiple-sessions",
2031 "org.freedesktop.login1.hibernate-ignore-inhibit",
2033 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
2037 static int nologin_timeout_handler(
2042 Manager
*m
= userdata
;
2044 log_info("Creating /run/nologin, blocking further logins...");
2047 create_shutdown_run_nologin_or_warn() >= 0;
2052 static int update_schedule_file(Manager
*m
) {
2053 _cleanup_free_
char *temp_path
= NULL
;
2054 _cleanup_fclose_
FILE *f
= NULL
;
2059 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
2061 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2063 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
2065 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2067 (void) fchmod(fileno(f
), 0644);
2073 m
->scheduled_shutdown_timeout
,
2074 m
->enable_wall_messages
,
2075 m
->scheduled_shutdown_type
);
2077 if (!isempty(m
->wall_message
)) {
2078 _cleanup_free_
char *t
;
2080 t
= cescape(m
->wall_message
);
2086 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
2089 r
= fflush_and_check(f
);
2093 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2101 (void) unlink(temp_path
);
2102 (void) unlink("/run/systemd/shutdown/scheduled");
2104 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2107 static void reset_scheduled_shutdown(Manager
*m
) {
2110 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2111 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2112 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2114 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
2115 m
->scheduled_shutdown_timeout
= 0;
2116 m
->shutdown_dry_run
= false;
2118 if (m
->unlink_nologin
) {
2119 (void) unlink_or_warn("/run/nologin");
2120 m
->unlink_nologin
= false;
2123 (void) unlink("/run/systemd/shutdown/scheduled");
2126 static int manager_scheduled_shutdown_handler(
2131 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2132 Manager
*m
= userdata
;
2138 if (isempty(m
->scheduled_shutdown_type
))
2141 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2142 target
= SPECIAL_POWEROFF_TARGET
;
2143 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2144 target
= SPECIAL_REBOOT_TARGET
;
2145 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2146 target
= SPECIAL_HALT_TARGET
;
2148 assert_not_reached("unexpected shutdown type");
2150 /* Don't allow multiple jobs being executed at the same time */
2151 if (m
->action_what
> 0) {
2153 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2157 if (m
->shutdown_dry_run
) {
2158 /* We do not process delay inhibitors here. Otherwise, we
2159 * would have to be considered "in progress" (like the check
2160 * above) for some seconds after our admin has seen the final
2163 bus_manager_log_shutdown(m
, target
);
2164 log_info("Running in dry run, suppressing action.");
2165 reset_scheduled_shutdown(m
);
2170 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2172 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2179 reset_scheduled_shutdown(m
);
2183 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2184 Manager
*m
= userdata
;
2185 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2186 const char *action_multiple_sessions
= NULL
;
2187 const char *action_ignore_inhibit
= NULL
;
2188 const char *action
= NULL
;
2192 bool dry_run
= false;
2197 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2201 if (startswith(type
, "dry-")) {
2206 if (streq(type
, "poweroff")) {
2207 action
= "org.freedesktop.login1.power-off";
2208 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2209 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2210 } else if (streq(type
, "reboot")) {
2211 action
= "org.freedesktop.login1.reboot";
2212 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2213 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2214 } else if (streq(type
, "halt")) {
2215 action
= "org.freedesktop.login1.halt";
2216 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2217 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2219 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2221 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, action
, action_multiple_sessions
,
2222 action_ignore_inhibit
, 0, error
);
2226 if (m
->scheduled_shutdown_timeout_source
) {
2227 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2229 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2231 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2233 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2235 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2236 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2238 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2241 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2243 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2247 m
->shutdown_dry_run
= dry_run
;
2249 if (m
->nologin_timeout_source
) {
2250 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2252 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2254 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2256 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2258 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2259 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2261 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2264 m
->scheduled_shutdown_timeout
= elapse
;
2266 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2268 const char *tty
= NULL
;
2270 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2271 (void) sd_bus_creds_get_tty(creds
, &tty
);
2273 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2275 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2280 r
= manager_setup_wall_message_timer(m
);
2284 r
= update_schedule_file(m
);
2288 return sd_bus_reply_method_return(message
, NULL
);
2291 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2292 Manager
*m
= userdata
;
2298 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2299 reset_scheduled_shutdown(m
);
2301 if (cancelled
&& m
->enable_wall_messages
) {
2302 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2303 _cleanup_free_
char *username
= NULL
;
2304 const char *tty
= NULL
;
2308 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2310 (void) sd_bus_creds_get_uid(creds
, &uid
);
2311 (void) sd_bus_creds_get_tty(creds
, &tty
);
2314 username
= uid_to_name(uid
);
2315 utmp_wall("The system shutdown has been cancelled",
2316 username
, tty
, logind_wall_tty_filter
, m
);
2319 return sd_bus_reply_method_return(message
, "b", cancelled
);
2322 static int method_can_shutdown_or_sleep(
2324 sd_bus_message
*message
,
2327 const char *action_multiple_sessions
,
2328 const char *action_ignore_inhibit
,
2329 const char *sleep_verb
,
2330 sd_bus_error
*error
) {
2332 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2333 HandleAction handle
;
2334 bool multiple_sessions
, challenge
, blocked
;
2335 const char *result
= NULL
;
2342 assert(w
<= _INHIBIT_WHAT_MAX
);
2344 assert(action_multiple_sessions
);
2345 assert(action_ignore_inhibit
);
2348 r
= can_sleep(sleep_verb
);
2349 if (IN_SET(r
, 0, -ENOSPC
))
2350 return sd_bus_reply_method_return(message
, "s", "na");
2355 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2359 r
= sd_bus_creds_get_euid(creds
, &uid
);
2363 r
= have_multiple_sessions(m
, uid
);
2367 multiple_sessions
= r
> 0;
2368 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2370 handle
= handle_action_from_string(sleep_verb
);
2374 target
= manager_target_for_action(handle
);
2376 _cleanup_free_
char *load_state
= NULL
;
2378 r
= unit_load_state(m
->bus
, target
, &load_state
);
2382 if (!streq(load_state
, "loaded")) {
2389 if (multiple_sessions
) {
2390 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2397 result
= "challenge";
2403 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2410 } else if (challenge
) {
2411 if (!result
|| streq(result
, "yes"))
2412 result
= "challenge";
2417 if (!multiple_sessions
&& !blocked
) {
2418 /* If neither inhibit nor multiple sessions
2419 * apply then just check the normal policy */
2421 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2428 result
= "challenge";
2434 return sd_bus_reply_method_return(message
, "s", result
);
2437 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2438 Manager
*m
= userdata
;
2440 return method_can_shutdown_or_sleep(
2443 "org.freedesktop.login1.power-off",
2444 "org.freedesktop.login1.power-off-multiple-sessions",
2445 "org.freedesktop.login1.power-off-ignore-inhibit",
2450 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2451 Manager
*m
= userdata
;
2453 return method_can_shutdown_or_sleep(
2456 "org.freedesktop.login1.reboot",
2457 "org.freedesktop.login1.reboot-multiple-sessions",
2458 "org.freedesktop.login1.reboot-ignore-inhibit",
2463 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2464 Manager
*m
= userdata
;
2466 return method_can_shutdown_or_sleep(
2469 "org.freedesktop.login1.halt",
2470 "org.freedesktop.login1.halt-multiple-sessions",
2471 "org.freedesktop.login1.halt-ignore-inhibit",
2476 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2477 Manager
*m
= userdata
;
2479 return method_can_shutdown_or_sleep(
2482 "org.freedesktop.login1.suspend",
2483 "org.freedesktop.login1.suspend-multiple-sessions",
2484 "org.freedesktop.login1.suspend-ignore-inhibit",
2489 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2490 Manager
*m
= userdata
;
2492 return method_can_shutdown_or_sleep(
2495 "org.freedesktop.login1.hibernate",
2496 "org.freedesktop.login1.hibernate-multiple-sessions",
2497 "org.freedesktop.login1.hibernate-ignore-inhibit",
2502 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2503 Manager
*m
= userdata
;
2505 return method_can_shutdown_or_sleep(
2508 "org.freedesktop.login1.hibernate",
2509 "org.freedesktop.login1.hibernate-multiple-sessions",
2510 "org.freedesktop.login1.hibernate-ignore-inhibit",
2515 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2516 Manager
*m
= userdata
;
2518 return method_can_shutdown_or_sleep(
2521 "org.freedesktop.login1.hibernate",
2522 "org.freedesktop.login1.hibernate-multiple-sessions",
2523 "org.freedesktop.login1.hibernate-ignore-inhibit",
2524 "suspend-then-hibernate",
2528 static int property_get_reboot_parameter(
2531 const char *interface
,
2532 const char *property
,
2533 sd_bus_message
*reply
,
2535 sd_bus_error
*error
) {
2536 _cleanup_free_
char *parameter
= NULL
;
2543 r
= read_reboot_parameter(¶meter
);
2547 return sd_bus_message_append(reply
, "s", parameter
);
2550 static int method_set_reboot_parameter(
2551 sd_bus_message
*message
,
2553 sd_bus_error
*error
) {
2555 Manager
*m
= userdata
;
2562 r
= sd_bus_message_read(message
, "s", &arg
);
2566 r
= detect_container();
2570 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2571 "Reboot parameter not supported in containers, refusing.");
2573 r
= bus_verify_polkit_async(message
,
2575 "org.freedesktop.login1.set-reboot-parameter",
2579 &m
->polkit_registry
,
2584 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2586 r
= update_reboot_parameter_and_warn(arg
, false);
2590 return sd_bus_reply_method_return(message
, NULL
);
2593 static int method_can_reboot_parameter(
2594 sd_bus_message
*message
,
2596 sd_bus_error
*error
) {
2598 _unused_ Manager
*m
= userdata
;
2604 r
= detect_container();
2607 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2608 return sd_bus_reply_method_return(message
, "s", "na");
2610 return return_test_polkit(
2613 "org.freedesktop.login1.set-reboot-parameter",
2619 static int property_get_reboot_to_firmware_setup(
2622 const char *interface
,
2623 const char *property
,
2624 sd_bus_message
*reply
,
2626 sd_bus_error
*error
) {
2633 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2635 /* EFI case: let's see what is currently configured in the EFI variables */
2636 r
= efi_get_reboot_to_firmware();
2637 if (r
< 0 && r
!= -EOPNOTSUPP
)
2638 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2640 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2642 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2643 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2644 if (errno
!= ENOENT
)
2645 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2652 return sd_bus_message_append(reply
, "b", r
> 0);
2655 static int method_set_reboot_to_firmware_setup(
2656 sd_bus_message
*message
,
2658 sd_bus_error
*error
) {
2660 Manager
*m
= userdata
;
2667 r
= sd_bus_message_read(message
, "b", &b
);
2671 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2673 /* EFI case: let's see what the firmware supports */
2675 r
= efi_reboot_to_firmware_supported();
2676 if (r
== -EOPNOTSUPP
)
2677 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2683 } else if (r
<= 0) {
2684 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2687 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2689 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2691 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2694 r
= bus_verify_polkit_async(message
,
2696 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2700 &m
->polkit_registry
,
2705 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2708 r
= efi_set_reboot_to_firmware(b
);
2713 r
= touch("/run/systemd/reboot-to-firmware-setup");
2717 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2722 return sd_bus_reply_method_return(message
, NULL
);
2725 static int method_can_reboot_to_firmware_setup(
2726 sd_bus_message
*message
,
2728 sd_bus_error
*error
) {
2730 _unused_ Manager
*m
= userdata
;
2736 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2738 /* EFI case: let's see what the firmware supports */
2740 r
= efi_reboot_to_firmware_supported();
2742 if (r
!= -EOPNOTSUPP
)
2743 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2745 return sd_bus_reply_method_return(message
, "s", "na");
2748 } else if (r
<= 0) {
2749 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2752 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2754 return sd_bus_reply_method_return(message
, "s", "na");
2757 return return_test_polkit(
2760 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2766 static int property_get_reboot_to_boot_loader_menu(
2769 const char *interface
,
2770 const char *property
,
2771 sd_bus_message
*reply
,
2773 sd_bus_error
*error
) {
2775 uint64_t x
= UINT64_MAX
;
2782 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2784 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2786 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2787 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2788 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2791 r
= efi_loader_get_config_timeout_one_shot(&x
);
2794 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
2798 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2800 _cleanup_free_
char *v
= NULL
;
2802 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2804 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2807 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2809 r
= safe_atou64(v
, &x
);
2811 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2815 return sd_bus_message_append(reply
, "t", x
);
2818 static int method_set_reboot_to_boot_loader_menu(
2819 sd_bus_message
*message
,
2821 sd_bus_error
*error
) {
2823 Manager
*m
= userdata
;
2831 r
= sd_bus_message_read(message
, "t", &x
);
2835 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
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_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2849 } else if (r
<= 0) {
2850 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2853 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2855 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2857 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2860 r
= bus_verify_polkit_async(message
,
2862 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2866 &m
->polkit_registry
,
2871 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2874 if (x
== UINT64_MAX
)
2875 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", NULL
, 0);
2877 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2878 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2880 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", buf
);
2885 if (x
== UINT64_MAX
) {
2886 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
2889 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2891 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2893 r
= write_string_file_atomic_label("/run/systemd/reboot-to-boot-loader-menu", buf
);
2899 return sd_bus_reply_method_return(message
, NULL
);
2902 static int method_can_reboot_to_boot_loader_menu(
2903 sd_bus_message
*message
,
2905 sd_bus_error
*error
) {
2907 _unused_ Manager
*m
= userdata
;
2913 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2915 uint64_t features
= 0;
2917 /* EFI case, let's see if booting into boot loader menu is supported. */
2919 r
= efi_loader_get_features(&features
);
2921 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2922 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2923 return sd_bus_reply_method_return(message
, "s", "na");
2925 } else if (r
<= 0) {
2926 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2929 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2931 return sd_bus_reply_method_return(message
, "s", "na");
2934 return return_test_polkit(
2937 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2943 static int property_get_reboot_to_boot_loader_entry(
2946 const char *interface
,
2947 const char *property
,
2948 sd_bus_message
*reply
,
2950 sd_bus_error
*error
) {
2952 _cleanup_free_
char *v
= NULL
;
2953 Manager
*m
= userdata
;
2954 const char *x
= NULL
;
2961 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2963 /* EFI case: let's read the LoaderEntryOneShot variable */
2965 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
2968 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
2970 x
= m
->efi_loader_entry_one_shot
;
2973 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2976 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2978 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2981 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
2982 } else if (!efi_loader_entry_name_valid(v
))
2983 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2988 return sd_bus_message_append(reply
, "s", x
);
2991 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
2992 _cleanup_(boot_config_free
) BootConfig config
= {};
2998 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2999 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
3002 r
= manager_read_efi_boot_loader_entries(m
);
3004 (void) boot_entries_augment_from_loader(&config
, m
->efi_boot_loader_entries
, true);
3006 return boot_config_has_entry(&config
, id
);
3009 static int method_set_reboot_to_boot_loader_entry(
3010 sd_bus_message
*message
,
3012 sd_bus_error
*error
) {
3014 Manager
*m
= userdata
;
3022 r
= sd_bus_message_read(message
, "s", &v
);
3028 else if (efi_loader_entry_name_valid(v
)) {
3029 r
= boot_loader_entry_exists(m
, v
);
3033 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3035 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3037 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3041 /* EFI case: let's see if booting into boot loader entry is supported. */
3043 r
= efi_loader_get_features(&features
);
3045 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3046 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3047 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3051 } else if (r
<= 0) {
3052 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3055 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3057 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3059 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3062 r
= bus_verify_polkit_async(message
,
3064 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3068 &m
->polkit_registry
,
3073 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3078 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", NULL
, 0);
3080 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", v
);
3085 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3088 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3094 return sd_bus_reply_method_return(message
, NULL
);
3097 static int method_can_reboot_to_boot_loader_entry(
3098 sd_bus_message
*message
,
3100 sd_bus_error
*error
) {
3102 _unused_ Manager
*m
= userdata
;
3108 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3110 uint64_t features
= 0;
3112 /* EFI case, let's see if booting into boot loader entry is supported. */
3114 r
= efi_loader_get_features(&features
);
3116 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3117 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3118 return sd_bus_reply_method_return(message
, "s", "na");
3120 } else if (r
<= 0) {
3121 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3124 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3126 return sd_bus_reply_method_return(message
, "s", "na");
3129 return return_test_polkit(
3132 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3138 static int property_get_boot_loader_entries(
3141 const char *interface
,
3142 const char *property
,
3143 sd_bus_message
*reply
,
3145 sd_bus_error
*error
) {
3147 _cleanup_(boot_config_free
) BootConfig config
= {};
3148 Manager
*m
= userdata
;
3156 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3157 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3160 r
= manager_read_efi_boot_loader_entries(m
);
3162 (void) boot_entries_augment_from_loader(&config
, m
->efi_boot_loader_entries
, true);
3164 r
= sd_bus_message_open_container(reply
, 'a', "s");
3168 for (i
= 0; i
< config
.n_entries
; i
++) {
3169 BootEntry
*e
= config
.entries
+ i
;
3171 r
= sd_bus_message_append(reply
, "s", e
->id
);
3176 return sd_bus_message_close_container(reply
);
3179 static int method_set_wall_message(
3180 sd_bus_message
*message
,
3182 sd_bus_error
*error
) {
3185 Manager
*m
= userdata
;
3187 unsigned enable_wall_messages
;
3192 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3196 /* Short-circuit the operation if the desired state is already in place, to
3197 * avoid an unnecessary polkit permission check. */
3198 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3199 m
->enable_wall_messages
== enable_wall_messages
)
3202 r
= bus_verify_polkit_async(message
,
3204 "org.freedesktop.login1.set-wall-message",
3208 &m
->polkit_registry
,
3213 return 1; /* Will call us back */
3215 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3219 m
->enable_wall_messages
= enable_wall_messages
;
3222 return sd_bus_reply_method_return(message
, NULL
);
3225 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3226 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3227 const char *who
, *why
, *what
, *mode
;
3228 _cleanup_free_
char *id
= NULL
;
3229 _cleanup_close_
int fifo_fd
= -1;
3230 Manager
*m
= userdata
;
3240 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3244 w
= inhibit_what_from_string(what
);
3246 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3247 "Invalid what specification %s", what
);
3249 mm
= inhibit_mode_from_string(mode
);
3251 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3252 "Invalid mode specification %s", mode
);
3254 /* Delay is only supported for shutdown/sleep */
3255 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3256 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3257 "Delay inhibitors only supported for shutdown and sleep");
3259 /* Don't allow taking delay locks while we are already
3260 * executing the operation. We shouldn't create the impression
3261 * that the lock was successful if the machine is about to go
3262 * down/suspend any moment. */
3263 if (m
->action_what
& w
)
3264 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3265 "The operation inhibition has been requested for is already running");
3267 r
= bus_verify_polkit_async(
3270 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3271 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3272 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3273 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3274 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3275 w
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3276 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3277 "org.freedesktop.login1.inhibit-handle-lid-switch",
3281 &m
->polkit_registry
,
3286 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3288 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3292 r
= sd_bus_creds_get_euid(creds
, &uid
);
3296 r
= sd_bus_creds_get_pid(creds
, &pid
);
3300 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3301 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3302 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3308 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3311 } while (hashmap_get(m
->inhibitors
, id
));
3313 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3314 r
= manager_add_inhibitor(m
, id
, &i
);
3322 i
->why
= strdup(why
);
3323 i
->who
= strdup(who
);
3325 if (!i
->why
|| !i
->who
)
3328 fifo_fd
= inhibitor_create_fifo(i
);
3332 r
= inhibitor_start(i
);
3337 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3340 static const sd_bus_vtable manager_vtable
[] = {
3341 SD_BUS_VTABLE_START(0),
3343 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3344 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3346 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3347 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3348 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3349 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3350 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3351 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3352 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3353 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3354 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3355 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3356 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3357 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3358 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3359 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3360 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3361 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3362 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3363 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3364 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3365 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3366 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3367 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3368 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3369 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3370 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3371 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3372 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3373 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3374 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3375 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3376 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3377 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3378 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3379 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3380 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3381 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3382 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3383 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3384 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3386 SD_BUS_METHOD_WITH_NAMES("GetSession",
3388 SD_BUS_PARAM(session_id
),
3390 SD_BUS_PARAM(object_path
),
3392 SD_BUS_VTABLE_UNPRIVILEGED
),
3393 SD_BUS_METHOD_WITH_NAMES("GetSessionByPID",
3397 SD_BUS_PARAM(object_path
),
3398 method_get_session_by_pid
,
3399 SD_BUS_VTABLE_UNPRIVILEGED
),
3400 SD_BUS_METHOD_WITH_NAMES("GetUser",
3404 SD_BUS_PARAM(object_path
),
3406 SD_BUS_VTABLE_UNPRIVILEGED
),
3407 SD_BUS_METHOD_WITH_NAMES("GetUserByPID",
3411 SD_BUS_PARAM(object_path
),
3412 method_get_user_by_pid
,
3413 SD_BUS_VTABLE_UNPRIVILEGED
),
3414 SD_BUS_METHOD_WITH_NAMES("GetSeat",
3416 SD_BUS_PARAM(seat_id
),
3418 SD_BUS_PARAM(object_path
),
3420 SD_BUS_VTABLE_UNPRIVILEGED
),
3421 SD_BUS_METHOD_WITH_NAMES("ListSessions",
3424 SD_BUS_PARAM(sessions
),
3425 method_list_sessions
,
3426 SD_BUS_VTABLE_UNPRIVILEGED
),
3427 SD_BUS_METHOD_WITH_NAMES("ListUsers",
3430 SD_BUS_PARAM(users
),
3432 SD_BUS_VTABLE_UNPRIVILEGED
),
3433 SD_BUS_METHOD_WITH_NAMES("ListSeats",
3436 SD_BUS_PARAM(seats
),
3438 SD_BUS_VTABLE_UNPRIVILEGED
),
3439 SD_BUS_METHOD_WITH_NAMES("ListInhibitors",
3442 SD_BUS_PARAM(inhibitors
),
3443 method_list_inhibitors
,
3444 SD_BUS_VTABLE_UNPRIVILEGED
),
3445 SD_BUS_METHOD_WITH_NAMES("CreateSession",
3446 "uusssssussbssa(sv)",
3449 SD_BUS_PARAM(service
)
3452 SD_BUS_PARAM(desktop
)
3453 SD_BUS_PARAM(seat_id
)
3456 SD_BUS_PARAM(display
)
3457 SD_BUS_PARAM(remote
)
3458 SD_BUS_PARAM(remote_user
)
3459 SD_BUS_PARAM(remote_host
)
3460 SD_BUS_PARAM(properties
),
3462 SD_BUS_PARAM(session_id
)
3463 SD_BUS_PARAM(object_path
)
3464 SD_BUS_PARAM(runtime_path
)
3465 SD_BUS_PARAM(fifo_fd
)
3467 SD_BUS_PARAM(seat_id
)
3469 SD_BUS_PARAM(existing
),
3470 method_create_session
,
3472 SD_BUS_METHOD_WITH_NAMES("ReleaseSession",
3474 SD_BUS_PARAM(session_id
),
3476 method_release_session
,
3478 SD_BUS_METHOD_WITH_NAMES("ActivateSession",
3480 SD_BUS_PARAM(session_id
),
3482 method_activate_session
,
3483 SD_BUS_VTABLE_UNPRIVILEGED
),
3484 SD_BUS_METHOD_WITH_NAMES("ActivateSessionOnSeat",
3486 SD_BUS_PARAM(session_id
)
3487 SD_BUS_PARAM(seat_id
),
3489 method_activate_session_on_seat
,
3490 SD_BUS_VTABLE_UNPRIVILEGED
),
3491 SD_BUS_METHOD_WITH_NAMES("LockSession",
3493 SD_BUS_PARAM(session_id
),
3495 method_lock_session
,
3496 SD_BUS_VTABLE_UNPRIVILEGED
),
3497 SD_BUS_METHOD_WITH_NAMES("UnlockSession",
3499 SD_BUS_PARAM(session_id
),
3501 method_lock_session
,
3502 SD_BUS_VTABLE_UNPRIVILEGED
),
3503 SD_BUS_METHOD("LockSessions",
3506 method_lock_sessions
,
3507 SD_BUS_VTABLE_UNPRIVILEGED
),
3508 SD_BUS_METHOD("UnlockSessions",
3511 method_lock_sessions
,
3512 SD_BUS_VTABLE_UNPRIVILEGED
),
3513 SD_BUS_METHOD_WITH_NAMES("KillSession",
3515 SD_BUS_PARAM(session_id
)
3517 SD_BUS_PARAM(signal_number
),
3519 method_kill_session
,
3520 SD_BUS_VTABLE_UNPRIVILEGED
),
3521 SD_BUS_METHOD_WITH_NAMES("KillUser",
3524 SD_BUS_PARAM(signal_number
),
3527 SD_BUS_VTABLE_UNPRIVILEGED
),
3528 SD_BUS_METHOD_WITH_NAMES("TerminateSession",
3530 SD_BUS_PARAM(session_id
),
3532 method_terminate_session
,
3533 SD_BUS_VTABLE_UNPRIVILEGED
),
3534 SD_BUS_METHOD_WITH_NAMES("TerminateUser",
3538 method_terminate_user
,
3539 SD_BUS_VTABLE_UNPRIVILEGED
),
3540 SD_BUS_METHOD_WITH_NAMES("TerminateSeat",
3542 SD_BUS_PARAM(seat_id
),
3544 method_terminate_seat
,
3545 SD_BUS_VTABLE_UNPRIVILEGED
),
3546 SD_BUS_METHOD_WITH_NAMES("SetUserLinger",
3549 SD_BUS_PARAM(enable
)
3550 SD_BUS_PARAM(interactive
),
3552 method_set_user_linger
,
3553 SD_BUS_VTABLE_UNPRIVILEGED
),
3554 SD_BUS_METHOD_WITH_NAMES("AttachDevice",
3556 SD_BUS_PARAM(seat_id
)
3557 SD_BUS_PARAM(sysfs_path
)
3558 SD_BUS_PARAM(interactive
),
3560 method_attach_device
,
3561 SD_BUS_VTABLE_UNPRIVILEGED
),
3562 SD_BUS_METHOD_WITH_NAMES("FlushDevices",
3564 SD_BUS_PARAM(interactive
),
3566 method_flush_devices
,
3567 SD_BUS_VTABLE_UNPRIVILEGED
),
3568 SD_BUS_METHOD_WITH_NAMES("PowerOff",
3570 SD_BUS_PARAM(interactive
),
3573 SD_BUS_VTABLE_UNPRIVILEGED
),
3574 SD_BUS_METHOD_WITH_NAMES("PowerOffWithFlags",
3576 SD_BUS_PARAM(flags
),
3579 SD_BUS_VTABLE_UNPRIVILEGED
),
3580 SD_BUS_METHOD_WITH_NAMES("Reboot",
3582 SD_BUS_PARAM(interactive
),
3585 SD_BUS_VTABLE_UNPRIVILEGED
),
3586 SD_BUS_METHOD_WITH_NAMES("RebootWithFlags",
3588 SD_BUS_PARAM(flags
),
3591 SD_BUS_VTABLE_UNPRIVILEGED
),
3592 SD_BUS_METHOD_WITH_NAMES("Halt",
3594 SD_BUS_PARAM(interactive
),
3597 SD_BUS_VTABLE_UNPRIVILEGED
),
3598 SD_BUS_METHOD_WITH_NAMES("HaltWithFlags",
3600 SD_BUS_PARAM(flags
),
3603 SD_BUS_VTABLE_UNPRIVILEGED
),
3604 SD_BUS_METHOD_WITH_NAMES("Suspend",
3606 SD_BUS_PARAM(interactive
),
3609 SD_BUS_VTABLE_UNPRIVILEGED
),
3610 SD_BUS_METHOD_WITH_NAMES("SuspendWithFlags",
3612 SD_BUS_PARAM(flags
),
3615 SD_BUS_VTABLE_UNPRIVILEGED
),
3616 SD_BUS_METHOD_WITH_NAMES("Hibernate",
3618 SD_BUS_PARAM(interactive
),
3621 SD_BUS_VTABLE_UNPRIVILEGED
),
3622 SD_BUS_METHOD_WITH_NAMES("HibernateWithFlags",
3624 SD_BUS_PARAM(flags
),
3627 SD_BUS_VTABLE_UNPRIVILEGED
),
3628 SD_BUS_METHOD_WITH_NAMES("HybridSleep",
3630 SD_BUS_PARAM(interactive
),
3632 method_hybrid_sleep
,
3633 SD_BUS_VTABLE_UNPRIVILEGED
),
3634 SD_BUS_METHOD_WITH_NAMES("HybridSleepWithFlags",
3636 SD_BUS_PARAM(flags
),
3638 method_hybrid_sleep
,
3639 SD_BUS_VTABLE_UNPRIVILEGED
),
3640 SD_BUS_METHOD_WITH_NAMES("SuspendThenHibernate",
3642 SD_BUS_PARAM(interactive
),
3644 method_suspend_then_hibernate
,
3645 SD_BUS_VTABLE_UNPRIVILEGED
),
3646 SD_BUS_METHOD_WITH_NAMES("SuspendThenHibernateWithFlags",
3648 SD_BUS_PARAM(flags
),
3650 method_suspend_then_hibernate
,
3651 SD_BUS_VTABLE_UNPRIVILEGED
),
3652 SD_BUS_METHOD_WITH_NAMES("CanPowerOff",
3655 SD_BUS_PARAM(result
),
3656 method_can_poweroff
,
3657 SD_BUS_VTABLE_UNPRIVILEGED
),
3658 SD_BUS_METHOD_WITH_NAMES("CanReboot",
3661 SD_BUS_PARAM(result
),
3663 SD_BUS_VTABLE_UNPRIVILEGED
),
3664 SD_BUS_METHOD_WITH_NAMES("CanHalt",
3667 SD_BUS_PARAM(result
),
3669 SD_BUS_VTABLE_UNPRIVILEGED
),
3670 SD_BUS_METHOD_WITH_NAMES("CanSuspend",
3673 SD_BUS_PARAM(result
),
3675 SD_BUS_VTABLE_UNPRIVILEGED
),
3676 SD_BUS_METHOD_WITH_NAMES("CanHibernate",
3679 SD_BUS_PARAM(result
),
3680 method_can_hibernate
,
3681 SD_BUS_VTABLE_UNPRIVILEGED
),
3682 SD_BUS_METHOD_WITH_NAMES("CanHybridSleep",
3685 SD_BUS_PARAM(result
),
3686 method_can_hybrid_sleep
,
3687 SD_BUS_VTABLE_UNPRIVILEGED
),
3688 SD_BUS_METHOD_WITH_NAMES("CanSuspendThenHibernate",
3691 SD_BUS_PARAM(result
),
3692 method_can_suspend_then_hibernate
,
3693 SD_BUS_VTABLE_UNPRIVILEGED
),
3694 SD_BUS_METHOD_WITH_NAMES("ScheduleShutdown",
3699 method_schedule_shutdown
,
3700 SD_BUS_VTABLE_UNPRIVILEGED
),
3701 SD_BUS_METHOD_WITH_NAMES("CancelScheduledShutdown",
3704 SD_BUS_PARAM(cancelled
),
3705 method_cancel_scheduled_shutdown
,
3706 SD_BUS_VTABLE_UNPRIVILEGED
),
3707 SD_BUS_METHOD_WITH_NAMES("Inhibit",
3714 SD_BUS_PARAM(pipe_fd
),
3716 SD_BUS_VTABLE_UNPRIVILEGED
),
3717 SD_BUS_METHOD_WITH_NAMES("CanRebootParameter",
3720 SD_BUS_PARAM(result
),
3721 method_can_reboot_parameter
,
3722 SD_BUS_VTABLE_UNPRIVILEGED
),
3723 SD_BUS_METHOD_WITH_NAMES("SetRebootParameter",
3725 SD_BUS_PARAM(parameter
),
3727 method_set_reboot_parameter
,
3728 SD_BUS_VTABLE_UNPRIVILEGED
),
3729 SD_BUS_METHOD_WITH_NAMES("CanRebootToFirmwareSetup",
3732 SD_BUS_PARAM(result
),
3733 method_can_reboot_to_firmware_setup
,
3734 SD_BUS_VTABLE_UNPRIVILEGED
),
3735 SD_BUS_METHOD_WITH_NAMES("SetRebootToFirmwareSetup",
3737 SD_BUS_PARAM(enable
),
3739 method_set_reboot_to_firmware_setup
,
3740 SD_BUS_VTABLE_UNPRIVILEGED
),
3741 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderMenu",
3744 SD_BUS_PARAM(result
),
3745 method_can_reboot_to_boot_loader_menu
,
3746 SD_BUS_VTABLE_UNPRIVILEGED
),
3747 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderMenu",
3749 SD_BUS_PARAM(timeout
),
3751 method_set_reboot_to_boot_loader_menu
,
3752 SD_BUS_VTABLE_UNPRIVILEGED
),
3753 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderEntry",
3756 SD_BUS_PARAM(result
),
3757 method_can_reboot_to_boot_loader_entry
,
3758 SD_BUS_VTABLE_UNPRIVILEGED
),
3759 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderEntry",
3761 SD_BUS_PARAM(boot_loader_entry
),
3763 method_set_reboot_to_boot_loader_entry
,
3764 SD_BUS_VTABLE_UNPRIVILEGED
),
3765 SD_BUS_METHOD_WITH_NAMES("SetWallMessage",
3767 SD_BUS_PARAM(wall_message
)
3768 SD_BUS_PARAM(enable
),
3770 method_set_wall_message
,
3771 SD_BUS_VTABLE_UNPRIVILEGED
),
3773 SD_BUS_SIGNAL_WITH_NAMES("SessionNew",
3775 SD_BUS_PARAM(session_id
)
3776 SD_BUS_PARAM(object_path
),
3778 SD_BUS_SIGNAL_WITH_NAMES("SessionRemoved",
3780 SD_BUS_PARAM(session_id
)
3781 SD_BUS_PARAM(object_path
),
3783 SD_BUS_SIGNAL_WITH_NAMES("UserNew",
3786 SD_BUS_PARAM(object_path
),
3788 SD_BUS_SIGNAL_WITH_NAMES("UserRemoved",
3791 SD_BUS_PARAM(object_path
),
3793 SD_BUS_SIGNAL_WITH_NAMES("SeatNew",
3795 SD_BUS_PARAM(seat_id
)
3796 SD_BUS_PARAM(object_path
),
3798 SD_BUS_SIGNAL_WITH_NAMES("SeatRemoved",
3800 SD_BUS_PARAM(seat_id
)
3801 SD_BUS_PARAM(object_path
),
3803 SD_BUS_SIGNAL_WITH_NAMES("PrepareForShutdown",
3805 SD_BUS_PARAM(start
),
3807 SD_BUS_SIGNAL_WITH_NAMES("PrepareForSleep",
3809 SD_BUS_PARAM(start
),
3815 const BusObjectImplementation manager_object
= {
3816 "/org/freedesktop/login1",
3817 "org.freedesktop.login1.Manager",
3818 .vtables
= BUS_VTABLES(manager_vtable
),
3819 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
3824 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
3831 if (result
&& !streq(result
, "done")) {
3832 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3834 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3835 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
3836 return session_send_create_reply(s
, &e
);
3839 return session_send_create_reply(s
, NULL
);
3842 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3843 const char *path
, *result
, *unit
;
3844 Manager
*m
= userdata
;
3853 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3855 bus_log_parse_error(r
);
3859 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3860 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
3862 /* Tell people that they now may take a lock again */
3863 (void) send_prepare_for(m
, m
->action_what
, false);
3865 m
->action_job
= mfree(m
->action_job
);
3866 m
->action_unit
= NULL
;
3871 session
= hashmap_get(m
->session_units
, unit
);
3873 if (streq_ptr(path
, session
->scope_job
)) {
3874 session
->scope_job
= mfree(session
->scope_job
);
3875 (void) session_jobs_reply(session
, id
, unit
, result
);
3877 session_save(session
);
3878 user_save(session
->user
);
3881 session_add_to_gc_queue(session
);
3884 user
= hashmap_get(m
->user_units
, unit
);
3886 if (streq_ptr(path
, user
->service_job
)) {
3887 user
->service_job
= mfree(user
->service_job
);
3889 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3890 (void) session_jobs_reply(session
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
3895 user_add_to_gc_queue(user
);
3901 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3902 const char *path
, *unit
;
3903 Manager
*m
= userdata
;
3911 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3913 bus_log_parse_error(r
);
3917 session
= hashmap_get(m
->session_units
, unit
);
3919 session_add_to_gc_queue(session
);
3921 user
= hashmap_get(m
->user_units
, unit
);
3923 user_add_to_gc_queue(user
);
3928 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3929 _cleanup_free_
char *unit
= NULL
;
3930 Manager
*m
= userdata
;
3939 path
= sd_bus_message_get_path(message
);
3943 r
= unit_name_from_dbus_path(path
, &unit
);
3944 if (r
== -EINVAL
) /* not a unit */
3951 session
= hashmap_get(m
->session_units
, unit
);
3953 session_add_to_gc_queue(session
);
3955 user
= hashmap_get(m
->user_units
, unit
);
3957 user_add_to_gc_queue(user
);
3962 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3963 Manager
*m
= userdata
;
3970 r
= sd_bus_message_read(message
, "b", &b
);
3972 bus_log_parse_error(r
);
3979 /* systemd finished reloading, let's recheck all our sessions */
3980 log_debug("System manager has been reloaded, rechecking sessions...");
3982 HASHMAP_FOREACH(session
, m
->sessions
)
3983 session_add_to_gc_queue(session
);
3988 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3993 l
= strv_from_stdarg_alloca(property
);
3995 return sd_bus_emit_properties_changed_strv(
3997 "/org/freedesktop/login1",
3998 "org.freedesktop.login1.Manager",
4002 static int strdup_job(sd_bus_message
*reply
, char **job
) {
4007 r
= sd_bus_message_read(reply
, "o", &j
);
4019 int manager_start_scope(
4024 const char *description
,
4027 const char *requires_mounts_for
,
4028 sd_bus_message
*more_properties
,
4029 sd_bus_error
*error
,
4032 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
4041 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
4045 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
4049 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
4053 if (!isempty(slice
)) {
4054 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
4059 if (!isempty(description
)) {
4060 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4065 STRV_FOREACH(i
, wants
) {
4066 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
4071 STRV_FOREACH(i
, after
) {
4072 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4077 if (!empty_or_root(requires_mounts_for
)) {
4078 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4083 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4085 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4089 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
4093 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4094 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
4096 return bus_log_create_error(r
);
4098 if (more_properties
) {
4099 /* If TasksMax also appears here, it will overwrite the default value set above */
4100 r
= sd_bus_message_copy(m
, more_properties
, true);
4105 r
= sd_bus_message_close_container(m
);
4109 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4113 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4117 return strdup_job(reply
, job
);
4120 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4121 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4128 r
= bus_call_method(
4134 "ss", unit
, "replace");
4138 return strdup_job(reply
, job
);
4141 int manager_stop_unit(Manager
*manager
, const char *unit
, const char *job_mode
, sd_bus_error
*error
, char **ret_job
) {
4142 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4149 r
= bus_call_method(
4155 "ss", unit
, job_mode
?: "fail");
4157 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
4158 BUS_ERROR_LOAD_FAILED
)) {
4161 sd_bus_error_free(error
);
4168 return strdup_job(reply
, ret_job
);
4171 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4172 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4173 _cleanup_free_
char *path
= NULL
;
4179 path
= unit_dbus_path_from_name(scope
);
4183 r
= sd_bus_call_method(
4185 "org.freedesktop.systemd1",
4187 "org.freedesktop.systemd1.Scope",
4193 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4194 BUS_ERROR_LOAD_FAILED
,
4195 BUS_ERROR_SCOPE_NOT_RUNNING
))
4198 sd_bus_error_move(ret_error
, &error
);
4205 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4209 return bus_call_method(
4215 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4218 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4219 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4220 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4221 _cleanup_free_
char *path
= NULL
;
4228 path
= unit_dbus_path_from_name(unit
);
4232 r
= sd_bus_get_property(
4234 "org.freedesktop.systemd1",
4236 "org.freedesktop.systemd1.Unit",
4242 /* systemd might have dropped off momentarily, let's
4243 * not make this an error */
4244 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4245 SD_BUS_ERROR_DISCONNECTED
))
4248 /* If the unit is already unloaded then it's not
4250 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4251 BUS_ERROR_LOAD_FAILED
))
4254 sd_bus_error_move(ret_error
, &error
);
4258 r
= sd_bus_message_read(reply
, "s", &state
);
4262 return !STR_IN_SET(state
, "inactive", "failed");
4265 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4266 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4267 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4273 r
= sd_bus_get_property(
4275 "org.freedesktop.systemd1",
4277 "org.freedesktop.systemd1.Job",
4283 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
4284 SD_BUS_ERROR_DISCONNECTED
))
4287 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4290 sd_bus_error_move(ret_error
, &error
);
4294 /* We don't actually care about the state really. The fact
4295 * that we could read the job state is enough for us */