1 /* SPDX-License-Identifier: LGPL-2.1+ */
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-polkit.h"
16 #include "bus-unit-util.h"
18 #include "cgroup-util.h"
19 #include "device-util.h"
20 #include "dirent-util.h"
21 #include "efi-loader.h"
26 #include "fileio-label.h"
28 #include "format-util.h"
30 #include "logind-dbus.h"
31 #include "logind-seat-dbus.h"
32 #include "logind-session-dbus.h"
33 #include "logind-user-dbus.h"
35 #include "missing_capability.h"
37 #include "parse-util.h"
38 #include "path-util.h"
39 #include "process-util.h"
40 #include "reboot-util.h"
41 #include "selinux-util.h"
42 #include "sleep-config.h"
44 #include "stdio-util.h"
46 #include "terminal-util.h"
47 #include "tmpfile-util.h"
48 #include "unit-name.h"
49 #include "user-util.h"
50 #include "utmp-wtmp.h"
53 static int get_sender_session(
55 sd_bus_message
*message
,
60 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
61 Session
*session
= NULL
;
65 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
66 * and returns that. If not and 'consult_display' is true, this returns the display session of the
67 * owning user of the caller. */
69 r
= sd_bus_query_sender_creds(message
,
70 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
71 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
75 r
= sd_bus_creds_get_session(creds
, &name
);
80 if (consult_display
) {
83 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
90 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
92 session
= user
->display
;
96 session
= hashmap_get(m
->sessions
, name
);
99 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
101 "Caller does not belong to any known session and doesn't own any suitable session." :
102 "Caller does not belong to any known session.");
108 int manager_get_session_from_creds(
110 sd_bus_message
*message
,
120 if (SEAT_IS_SELF(name
)) /* the caller's own session */
121 return get_sender_session(m
, message
, false, error
, ret
);
122 if (SEAT_IS_AUTO(name
)) /* The caller's own session if they have one, otherwise their user's display session */
123 return get_sender_session(m
, message
, true, error
, ret
);
125 session
= hashmap_get(m
->sessions
, name
);
127 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
133 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
134 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
139 /* Note that we get the owner UID of the session, not the actual client UID here! */
140 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
144 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
151 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
154 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
155 "Caller does not belong to any logged in or lingering user");
161 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
167 if (!uid_is_valid(uid
))
168 return get_sender_user(m
, message
, error
, ret
);
170 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
172 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
173 "User ID "UID_FMT
" is not logged in or lingering", uid
);
179 int manager_get_seat_from_creds(
181 sd_bus_message
*message
,
192 if (SEAT_IS_SELF(name
) || SEAT_IS_AUTO(name
)) {
195 /* Use these special seat names as session names */
196 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
200 seat
= session
->seat
;
202 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
204 seat
= hashmap_get(m
->seats
, name
);
206 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
213 static int return_test_polkit(
214 sd_bus_message
*message
,
217 const char **details
,
225 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
232 result
= "challenge";
236 return sd_bus_reply_method_return(message
, "s", result
);
239 static int property_get_idle_hint(
242 const char *interface
,
243 const char *property
,
244 sd_bus_message
*reply
,
246 sd_bus_error
*error
) {
248 Manager
*m
= userdata
;
254 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
257 static int property_get_idle_since_hint(
260 const char *interface
,
261 const char *property
,
262 sd_bus_message
*reply
,
264 sd_bus_error
*error
) {
266 Manager
*m
= userdata
;
267 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
273 manager_get_idle_hint(m
, &t
);
275 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
278 static int property_get_inhibited(
281 const char *interface
,
282 const char *property
,
283 sd_bus_message
*reply
,
285 sd_bus_error
*error
) {
287 Manager
*m
= userdata
;
294 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
296 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
299 static int property_get_preparing(
302 const char *interface
,
303 const char *property
,
304 sd_bus_message
*reply
,
306 sd_bus_error
*error
) {
308 Manager
*m
= userdata
;
315 if (streq(property
, "PreparingForShutdown"))
316 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
318 b
= m
->action_what
& INHIBIT_SLEEP
;
320 return sd_bus_message_append(reply
, "b", b
);
323 static int property_get_scheduled_shutdown(
326 const char *interface
,
327 const char *property
,
328 sd_bus_message
*reply
,
330 sd_bus_error
*error
) {
332 Manager
*m
= userdata
;
339 r
= sd_bus_message_open_container(reply
, 'r', "st");
343 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
347 return sd_bus_message_close_container(reply
);
350 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
351 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
352 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
353 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
354 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
355 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
357 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
358 _cleanup_free_
char *p
= NULL
;
359 Manager
*m
= userdata
;
367 r
= sd_bus_message_read(message
, "s", &name
);
371 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
375 p
= session_bus_path(session
);
379 return sd_bus_reply_method_return(message
, "o", p
);
382 /* Get login session of a process. This is not what you are looking for these days,
383 * as apps may instead belong to a user service unit. This includes terminal
384 * emulators and hence command-line apps. */
385 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
386 _cleanup_free_
char *p
= NULL
;
387 Session
*session
= NULL
;
388 Manager
*m
= userdata
;
395 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
397 r
= sd_bus_message_read(message
, "u", &pid
);
404 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
408 r
= manager_get_session_by_pid(m
, pid
, &session
);
413 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
414 "PID "PID_FMT
" does not belong to any known session", pid
);
417 p
= session_bus_path(session
);
421 return sd_bus_reply_method_return(message
, "o", p
);
424 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
425 _cleanup_free_
char *p
= NULL
;
426 Manager
*m
= userdata
;
434 r
= sd_bus_message_read(message
, "u", &uid
);
438 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
442 p
= user_bus_path(user
);
446 return sd_bus_reply_method_return(message
, "o", p
);
449 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
450 _cleanup_free_
char *p
= NULL
;
451 Manager
*m
= userdata
;
459 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
461 r
= sd_bus_message_read(message
, "u", &pid
);
468 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
472 r
= manager_get_user_by_pid(m
, pid
, &user
);
476 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
477 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
481 p
= user_bus_path(user
);
485 return sd_bus_reply_method_return(message
, "o", p
);
488 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
489 _cleanup_free_
char *p
= NULL
;
490 Manager
*m
= userdata
;
498 r
= sd_bus_message_read(message
, "s", &name
);
502 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
506 p
= seat_bus_path(seat
);
510 return sd_bus_reply_method_return(message
, "o", p
);
513 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
514 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
515 Manager
*m
= userdata
;
523 r
= sd_bus_message_new_method_return(message
, &reply
);
527 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
531 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
532 _cleanup_free_
char *p
= NULL
;
534 p
= session_bus_path(session
);
538 r
= sd_bus_message_append(reply
, "(susso)",
540 (uint32_t) session
->user
->user_record
->uid
,
541 session
->user
->user_record
->user_name
,
542 session
->seat
? session
->seat
->id
: "",
548 r
= sd_bus_message_close_container(reply
);
552 return sd_bus_send(NULL
, reply
, NULL
);
555 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
556 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
557 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
, i
) {
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
;
605 r
= sd_bus_message_new_method_return(message
, &reply
);
609 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
613 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
614 _cleanup_free_
char *p
= NULL
;
616 p
= seat_bus_path(seat
);
620 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
625 r
= sd_bus_message_close_container(reply
);
629 return sd_bus_send(NULL
, reply
, NULL
);
632 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
633 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
634 Manager
*m
= userdata
;
635 Inhibitor
*inhibitor
;
642 r
= sd_bus_message_new_method_return(message
, &reply
);
646 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
650 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
652 r
= sd_bus_message_append(reply
, "(ssssuu)",
653 strempty(inhibit_what_to_string(inhibitor
->what
)),
654 strempty(inhibitor
->who
),
655 strempty(inhibitor
->why
),
656 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
657 (uint32_t) inhibitor
->uid
,
658 (uint32_t) inhibitor
->pid
);
663 r
= sd_bus_message_close_container(reply
);
667 return sd_bus_send(NULL
, reply
, NULL
);
670 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
671 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
672 _cleanup_free_
char *id
= NULL
;
673 Session
*session
= NULL
;
674 uint32_t audit_id
= 0;
675 Manager
*m
= userdata
;
689 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
690 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
692 r
= sd_bus_message_read(message
, "uusssssussbss",
693 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
694 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
698 if (!uid_is_valid(uid
))
699 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
700 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
701 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
704 t
= _SESSION_TYPE_INVALID
;
706 t
= session_type_from_string(type
);
708 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
709 "Invalid session type %s", type
);
713 c
= _SESSION_CLASS_INVALID
;
715 c
= session_class_from_string(class);
717 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
718 "Invalid session class %s", class);
721 if (isempty(desktop
))
724 if (!string_is_safe(desktop
))
725 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
726 "Invalid desktop string %s", desktop
);
732 seat
= hashmap_get(m
->seats
, cseat
);
734 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
735 "No seat '%s' known", cseat
);
738 if (tty_is_vc(tty
)) {
743 else if (seat
!= m
->seat0
)
744 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
745 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
747 v
= vtnr_from_tty(tty
);
749 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
750 "Cannot determine VT number from virtual console TTY %s", tty
);
754 else if (vtnr
!= (uint32_t) v
)
755 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
756 "Specified TTY and VT number do not match");
758 } else if (tty_is_console(tty
)) {
762 else if (seat
!= m
->seat0
)
763 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
764 "Console TTY specified but seat is not seat0");
767 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
768 "Console TTY specified but VT number is not 0");
772 if (seat_has_vts(seat
)) {
773 if (vtnr
<= 0 || vtnr
> 63)
774 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
775 "VT number out of range");
778 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
779 "Seat has no VTs but VT number not 0");
783 if (t
== _SESSION_TYPE_INVALID
) {
784 if (!isempty(display
))
786 else if (!isempty(tty
))
789 t
= SESSION_UNSPECIFIED
;
792 if (c
== _SESSION_CLASS_INVALID
) {
793 if (t
== SESSION_UNSPECIFIED
)
794 c
= SESSION_BACKGROUND
;
800 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
802 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
806 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
811 /* Check if we are already in a logind session. Or if we are in user@.service
812 * which is a special PAM session that avoids creating a logind session. */
813 r
= manager_get_user_by_pid(m
, leader
, NULL
);
817 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
818 "Already running in a session or user slice");
821 * Old gdm and lightdm start the user-session on the same VT as
822 * the greeter session. But they destroy the greeter session
823 * after the user-session and want the user-session to take
824 * over the VT. We need to support this for
825 * backwards-compatibility, so make sure we allow new sessions
826 * on a VT that a greeter is running on. Furthermore, to allow
827 * re-logins, we have to allow a greeter to take over a used VT for
828 * the exact same reasons.
830 if (c
!= SESSION_GREETER
&&
832 vtnr
< m
->seat0
->position_count
&&
833 m
->seat0
->positions
[vtnr
] &&
834 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
835 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
837 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
838 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
839 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
842 (void) audit_session_from_pid(leader
, &audit_id
);
843 if (audit_session_is_valid(audit_id
)) {
844 /* Keep our session IDs and the audit session IDs in sync */
846 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
849 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
850 * not trust the audit data and let's better register a new ID */
851 if (hashmap_contains(m
->sessions
, id
)) {
852 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
853 audit_id
= AUDIT_SESSION_INVALID
;
862 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
865 } while (hashmap_contains(m
->sessions
, id
));
868 /* The generated names should not clash with 'auto' or 'self' */
869 assert(!SESSION_IS_SELF(id
));
870 assert(!SESSION_IS_AUTO(id
));
872 /* If we are not watching utmp already, try again */
873 manager_reconnect_utmp(m
);
875 r
= manager_add_user_by_uid(m
, uid
, &user
);
879 r
= manager_add_session(m
, id
, &session
);
883 session_set_user(session
, user
);
884 r
= session_set_leader(session
, leader
);
890 session
->remote
= remote
;
891 session
->vtnr
= vtnr
;
894 session
->tty
= strdup(tty
);
900 session
->tty_validity
= TTY_FROM_PAM
;
903 if (!isempty(display
)) {
904 session
->display
= strdup(display
);
905 if (!session
->display
) {
911 if (!isempty(remote_user
)) {
912 session
->remote_user
= strdup(remote_user
);
913 if (!session
->remote_user
) {
919 if (!isempty(remote_host
)) {
920 session
->remote_host
= strdup(remote_host
);
921 if (!session
->remote_host
) {
927 if (!isempty(service
)) {
928 session
->service
= strdup(service
);
929 if (!session
->service
) {
935 if (!isempty(desktop
)) {
936 session
->desktop
= strdup(desktop
);
937 if (!session
->desktop
) {
944 r
= seat_attach_session(seat
, session
);
949 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
953 r
= session_start(session
, message
, error
);
957 r
= sd_bus_message_exit_container(message
);
961 session
->create_message
= sd_bus_message_ref(message
);
963 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
964 * session_send_create_reply(). */
970 session_add_to_gc_queue(session
);
973 user_add_to_gc_queue(user
);
978 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
979 Manager
*m
= userdata
;
987 r
= sd_bus_message_read(message
, "s", &name
);
991 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
995 r
= session_release(session
);
999 return sd_bus_reply_method_return(message
, NULL
);
1002 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1003 Manager
*m
= userdata
;
1011 r
= sd_bus_message_read(message
, "s", &name
);
1015 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1019 /* PolicyKit is done by bus_session_method_activate() */
1021 return bus_session_method_activate(message
, session
, error
);
1024 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1025 const char *session_name
, *seat_name
;
1026 Manager
*m
= userdata
;
1034 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1036 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1040 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1044 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1048 if (session
->seat
!= seat
)
1049 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1050 "Session %s not on seat %s", session_name
, seat_name
);
1052 r
= bus_verify_polkit_async(
1055 "org.freedesktop.login1.chvt",
1059 &m
->polkit_registry
,
1064 return 1; /* Will call us back */
1066 r
= session_activate(session
);
1070 return sd_bus_reply_method_return(message
, NULL
);
1073 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1074 Manager
*m
= userdata
;
1082 r
= sd_bus_message_read(message
, "s", &name
);
1086 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1090 return bus_session_method_lock(message
, session
, error
);
1093 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1094 Manager
*m
= userdata
;
1100 r
= bus_verify_polkit_async(
1103 "org.freedesktop.login1.lock-sessions",
1107 &m
->polkit_registry
,
1112 return 1; /* Will call us back */
1114 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1118 return sd_bus_reply_method_return(message
, NULL
);
1121 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1123 Manager
*m
= userdata
;
1130 r
= sd_bus_message_read(message
, "s", &name
);
1134 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1138 return bus_session_method_kill(message
, session
, error
);
1141 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1142 Manager
*m
= userdata
;
1150 r
= sd_bus_message_read(message
, "u", &uid
);
1154 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1158 return bus_user_method_kill(message
, user
, error
);
1161 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1162 Manager
*m
= userdata
;
1170 r
= sd_bus_message_read(message
, "s", &name
);
1174 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1178 return bus_session_method_terminate(message
, session
, error
);
1181 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1182 Manager
*m
= userdata
;
1190 r
= sd_bus_message_read(message
, "u", &uid
);
1194 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1198 return bus_user_method_terminate(message
, user
, error
);
1201 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1202 Manager
*m
= userdata
;
1210 r
= sd_bus_message_read(message
, "s", &name
);
1214 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1218 return bus_seat_method_terminate(message
, seat
, error
);
1221 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1222 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1223 _cleanup_free_
char *cc
= NULL
;
1224 Manager
*m
= userdata
;
1225 int r
, b
, interactive
;
1228 uint32_t uid
, auth_uid
;
1233 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1237 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1238 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1242 if (!uid_is_valid(uid
)) {
1243 /* Note that we get the owner UID of the session or user unit,
1244 * not the actual client UID here! */
1245 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1250 /* owner_uid is racy, so for authorization we must use euid */
1251 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1258 return errno_or_else(ENOENT
);
1260 r
= bus_verify_polkit_async(
1263 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1264 "org.freedesktop.login1.set-user-linger",
1268 &m
->polkit_registry
,
1273 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1275 (void) mkdir_p_label("/var/lib/systemd", 0755);
1276 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1280 cc
= cescape(pw
->pw_name
);
1284 path
= strjoina("/var/lib/systemd/linger/", cc
);
1292 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1299 if (r
< 0 && errno
!= ENOENT
)
1302 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1304 user_add_to_gc_queue(u
);
1307 return sd_bus_reply_method_return(message
, NULL
);
1310 static int trigger_device(Manager
*m
, sd_device
*d
) {
1311 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1316 r
= sd_device_enumerator_new(&e
);
1320 r
= sd_device_enumerator_allow_uninitialized(e
);
1325 r
= sd_device_enumerator_add_match_parent(e
, d
);
1330 FOREACH_DEVICE(e
, d
) {
1331 _cleanup_free_
char *t
= NULL
;
1334 r
= sd_device_get_syspath(d
, &p
);
1338 t
= path_join(p
, "uevent");
1342 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1348 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1349 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1350 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1351 const char *id_for_seat
;
1358 r
= sd_device_new_from_syspath(&d
, sysfs
);
1362 if (sd_device_has_tag(d
, "seat") <= 0)
1365 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1368 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1371 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1374 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1375 r
= write_string_file_atomic_label(file
, rule
);
1379 return trigger_device(m
, d
);
1382 static int flush_devices(Manager
*m
) {
1383 _cleanup_closedir_
DIR *d
;
1387 d
= opendir("/etc/udev/rules.d");
1389 if (errno
!= ENOENT
)
1390 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1394 FOREACH_DIRENT_ALL(de
, d
, break) {
1395 dirent_ensure_type(d
, de
);
1396 if (!dirent_is_file(de
))
1399 if (!startswith(de
->d_name
, "72-seat-"))
1402 if (!endswith(de
->d_name
, ".rules"))
1405 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1406 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1410 return trigger_device(m
, NULL
);
1413 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1414 const char *sysfs
, *seat
;
1415 Manager
*m
= userdata
;
1421 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1425 if (!path_is_normalized(sysfs
))
1426 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1427 if (!path_startswith(sysfs
, "/sys"))
1428 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1430 if (SEAT_IS_SELF(seat
) || SEAT_IS_AUTO(seat
)) {
1433 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1439 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1440 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1442 r
= bus_verify_polkit_async(
1445 "org.freedesktop.login1.attach-device",
1449 &m
->polkit_registry
,
1454 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1456 r
= attach_device(m
, seat
, sysfs
);
1460 return sd_bus_reply_method_return(message
, NULL
);
1463 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1464 Manager
*m
= userdata
;
1470 r
= sd_bus_message_read(message
, "b", &interactive
);
1474 r
= bus_verify_polkit_async(
1477 "org.freedesktop.login1.flush-devices",
1481 &m
->polkit_registry
,
1486 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1488 r
= flush_devices(m
);
1492 return sd_bus_reply_method_return(message
, NULL
);
1495 static int have_multiple_sessions(
1504 /* Check for other users' sessions. Greeter sessions do not
1505 * count, and non-login sessions do not count either. */
1506 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1507 if (session
->class == SESSION_USER
&&
1508 session
->user
->user_record
->uid
!= uid
)
1514 static int bus_manager_log_shutdown(
1516 const char *unit_name
) {
1523 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1524 p
= "MESSAGE=System is powering down";
1525 q
= "SHUTDOWN=power-off";
1526 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1527 p
= "MESSAGE=System is rebooting";
1528 q
= "SHUTDOWN=reboot";
1529 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1530 p
= "MESSAGE=System is halting";
1531 q
= "SHUTDOWN=halt";
1532 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1533 p
= "MESSAGE=System is rebooting with kexec";
1534 q
= "SHUTDOWN=kexec";
1536 p
= "MESSAGE=System is shutting down";
1540 if (isempty(m
->wall_message
))
1541 p
= strjoina(p
, ".");
1543 p
= strjoina(p
, " (", m
->wall_message
, ").");
1545 return log_struct(LOG_NOTICE
,
1546 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1551 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1552 Manager
*m
= userdata
;
1557 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1561 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1566 if (until
<= now(CLOCK_MONOTONIC
))
1569 /* We want to ignore the lid switch for a while after each
1570 * suspend, and after boot-up. Hence let's install a timer for
1571 * this. As long as the event source exists we ignore the lid
1574 if (m
->lid_switch_ignore_event_source
) {
1577 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1584 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1586 r
= sd_event_add_time(
1588 &m
->lid_switch_ignore_event_source
,
1591 lid_switch_ignore_handler
, m
);
1596 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1597 int active
= _active
;
1600 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1602 return sd_bus_emit_signal(m
->bus
,
1603 "/org/freedesktop/login1",
1604 "org.freedesktop.login1.Manager",
1605 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1610 static int execute_shutdown_or_sleep(
1613 const char *unit_name
,
1614 sd_bus_error
*error
) {
1616 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1622 assert(w
< _INHIBIT_WHAT_MAX
);
1625 if (w
== INHIBIT_SHUTDOWN
)
1626 bus_manager_log_shutdown(m
, unit_name
);
1628 r
= sd_bus_call_method(
1630 "org.freedesktop.systemd1",
1631 "/org/freedesktop/systemd1",
1632 "org.freedesktop.systemd1.Manager",
1636 "ss", unit_name
, "replace-irreversibly");
1640 r
= sd_bus_message_read(reply
, "o", &p
);
1644 r
= free_and_strdup(&m
->action_job
, p
);
1648 m
->action_unit
= unit_name
;
1651 /* Make sure the lid switch is ignored for a while */
1652 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1657 /* Tell people that they now may take a lock again */
1658 (void) send_prepare_for(m
, w
, false);
1663 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1665 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1666 Inhibitor
*offending
= NULL
;
1671 if (manager
->action_what
== 0 || manager
->action_job
)
1674 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1675 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1680 (void) get_process_comm(offending
->pid
, &comm
);
1681 u
= uid_to_name(offending
->uid
);
1683 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1684 offending
->uid
, strna(u
),
1685 offending
->pid
, strna(comm
));
1688 /* Actually do the operation */
1689 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1691 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1692 bus_error_message(&error
, r
));
1694 manager
->action_unit
= NULL
;
1695 manager
->action_what
= 0;
1702 static int manager_inhibit_timeout_handler(
1707 Manager
*manager
= userdata
;
1711 assert(manager
->inhibit_timeout_source
== s
);
1713 r
= manager_dispatch_delayed(manager
, true);
1714 return (r
< 0) ? r
: 0;
1717 static int delay_shutdown_or_sleep(
1720 const char *unit_name
) {
1727 assert(w
< _INHIBIT_WHAT_MAX
);
1730 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1732 if (m
->inhibit_timeout_source
) {
1733 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1735 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1737 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1739 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1741 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1742 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1747 m
->action_unit
= unit_name
;
1753 int bus_manager_shutdown_or_sleep_now_or_later(
1755 const char *unit_name
,
1757 sd_bus_error
*error
) {
1759 _cleanup_free_
char *load_state
= NULL
;
1766 assert(w
< _INHIBIT_WHAT_MAX
);
1767 assert(!m
->action_job
);
1769 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1773 if (!streq(load_state
, "loaded"))
1774 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1775 "Unit %s is %s, refusing operation.",
1776 unit_name
, load_state
);
1778 /* Tell everybody to prepare for shutdown/sleep */
1779 (void) send_prepare_for(m
, w
, true);
1782 m
->inhibit_delay_max
> 0 &&
1783 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1786 /* Shutdown is delayed, keep in mind what we
1787 * want to do, and start a timeout */
1788 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1790 /* Shutdown is not delayed, execute it
1792 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1797 static int verify_shutdown_creds(
1799 sd_bus_message
*message
,
1803 const char *action_multiple_sessions
,
1804 const char *action_ignore_inhibit
,
1805 sd_bus_error
*error
) {
1807 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1808 bool multiple_sessions
, blocked
;
1815 assert(w
<= _INHIBIT_WHAT_MAX
);
1817 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1821 r
= sd_bus_creds_get_euid(creds
, &uid
);
1825 r
= have_multiple_sessions(m
, uid
);
1829 multiple_sessions
= r
> 0;
1830 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1832 if (multiple_sessions
&& action_multiple_sessions
) {
1833 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1837 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1840 if (blocked
&& action_ignore_inhibit
) {
1841 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1845 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1848 if (!multiple_sessions
&& !blocked
&& action
) {
1849 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1853 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1859 static int method_do_shutdown_or_sleep(
1861 sd_bus_message
*message
,
1862 const char *unit_name
,
1865 const char *action_multiple_sessions
,
1866 const char *action_ignore_inhibit
,
1867 const char *sleep_verb
,
1868 sd_bus_error
*error
) {
1876 assert(w
<= _INHIBIT_WHAT_MAX
);
1878 r
= sd_bus_message_read(message
, "b", &interactive
);
1882 /* Don't allow multiple jobs being executed at the same time */
1883 if (m
->action_what
> 0)
1884 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1885 "There's already a shutdown or sleep operation in progress");
1888 r
= can_sleep(sleep_verb
);
1890 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1891 "Not enough swap space for hibernation");
1893 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1894 "Sleep verb \"%s\" not supported", sleep_verb
);
1899 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1900 action_ignore_inhibit
, error
);
1904 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1908 return sd_bus_reply_method_return(message
, NULL
);
1911 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1912 Manager
*m
= userdata
;
1914 return method_do_shutdown_or_sleep(
1916 SPECIAL_POWEROFF_TARGET
,
1918 "org.freedesktop.login1.power-off",
1919 "org.freedesktop.login1.power-off-multiple-sessions",
1920 "org.freedesktop.login1.power-off-ignore-inhibit",
1925 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1926 Manager
*m
= userdata
;
1928 return method_do_shutdown_or_sleep(
1930 SPECIAL_REBOOT_TARGET
,
1932 "org.freedesktop.login1.reboot",
1933 "org.freedesktop.login1.reboot-multiple-sessions",
1934 "org.freedesktop.login1.reboot-ignore-inhibit",
1939 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1940 Manager
*m
= userdata
;
1942 return method_do_shutdown_or_sleep(
1944 SPECIAL_HALT_TARGET
,
1946 "org.freedesktop.login1.halt",
1947 "org.freedesktop.login1.halt-multiple-sessions",
1948 "org.freedesktop.login1.halt-ignore-inhibit",
1953 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1954 Manager
*m
= userdata
;
1956 return method_do_shutdown_or_sleep(
1958 SPECIAL_SUSPEND_TARGET
,
1960 "org.freedesktop.login1.suspend",
1961 "org.freedesktop.login1.suspend-multiple-sessions",
1962 "org.freedesktop.login1.suspend-ignore-inhibit",
1967 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1968 Manager
*m
= userdata
;
1970 return method_do_shutdown_or_sleep(
1972 SPECIAL_HIBERNATE_TARGET
,
1974 "org.freedesktop.login1.hibernate",
1975 "org.freedesktop.login1.hibernate-multiple-sessions",
1976 "org.freedesktop.login1.hibernate-ignore-inhibit",
1981 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1982 Manager
*m
= userdata
;
1984 return method_do_shutdown_or_sleep(
1986 SPECIAL_HYBRID_SLEEP_TARGET
,
1988 "org.freedesktop.login1.hibernate",
1989 "org.freedesktop.login1.hibernate-multiple-sessions",
1990 "org.freedesktop.login1.hibernate-ignore-inhibit",
1995 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1996 Manager
*m
= userdata
;
1998 return method_do_shutdown_or_sleep(
2000 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
2002 "org.freedesktop.login1.hibernate",
2003 "org.freedesktop.login1.hibernate-multiple-sessions",
2004 "org.freedesktop.login1.hibernate-ignore-inhibit",
2009 static int nologin_timeout_handler(
2014 Manager
*m
= userdata
;
2016 log_info("Creating /run/nologin, blocking further logins...");
2019 create_shutdown_run_nologin_or_warn() >= 0;
2024 static int update_schedule_file(Manager
*m
) {
2025 _cleanup_free_
char *temp_path
= NULL
;
2026 _cleanup_fclose_
FILE *f
= NULL
;
2031 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
2033 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2035 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
2037 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2039 (void) fchmod(fileno(f
), 0644);
2045 m
->scheduled_shutdown_timeout
,
2046 m
->enable_wall_messages
,
2047 m
->scheduled_shutdown_type
);
2049 if (!isempty(m
->wall_message
)) {
2050 _cleanup_free_
char *t
;
2052 t
= cescape(m
->wall_message
);
2058 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
2061 r
= fflush_and_check(f
);
2065 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2073 (void) unlink(temp_path
);
2074 (void) unlink("/run/systemd/shutdown/scheduled");
2076 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2079 static void reset_scheduled_shutdown(Manager
*m
) {
2082 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2083 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2084 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2086 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
2087 m
->scheduled_shutdown_timeout
= 0;
2088 m
->shutdown_dry_run
= false;
2090 if (m
->unlink_nologin
) {
2091 (void) unlink_or_warn("/run/nologin");
2092 m
->unlink_nologin
= false;
2095 (void) unlink("/run/systemd/shutdown/scheduled");
2098 static int manager_scheduled_shutdown_handler(
2103 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2104 Manager
*m
= userdata
;
2110 if (isempty(m
->scheduled_shutdown_type
))
2113 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2114 target
= SPECIAL_POWEROFF_TARGET
;
2115 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2116 target
= SPECIAL_REBOOT_TARGET
;
2117 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2118 target
= SPECIAL_HALT_TARGET
;
2120 assert_not_reached("unexpected shutdown type");
2122 /* Don't allow multiple jobs being executed at the same time */
2123 if (m
->action_what
> 0) {
2125 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2129 if (m
->shutdown_dry_run
) {
2130 /* We do not process delay inhibitors here. Otherwise, we
2131 * would have to be considered "in progress" (like the check
2132 * above) for some seconds after our admin has seen the final
2135 bus_manager_log_shutdown(m
, target
);
2136 log_info("Running in dry run, suppressing action.");
2137 reset_scheduled_shutdown(m
);
2142 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2144 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2151 reset_scheduled_shutdown(m
);
2155 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2156 Manager
*m
= userdata
;
2157 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2158 const char *action_multiple_sessions
= NULL
;
2159 const char *action_ignore_inhibit
= NULL
;
2160 const char *action
= NULL
;
2164 bool dry_run
= false;
2169 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2173 if (startswith(type
, "dry-")) {
2178 if (streq(type
, "poweroff")) {
2179 action
= "org.freedesktop.login1.power-off";
2180 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2181 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2182 } else if (streq(type
, "reboot")) {
2183 action
= "org.freedesktop.login1.reboot";
2184 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2185 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2186 } else if (streq(type
, "halt")) {
2187 action
= "org.freedesktop.login1.halt";
2188 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2189 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2191 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2193 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2194 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2198 if (m
->scheduled_shutdown_timeout_source
) {
2199 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2201 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2203 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2205 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2207 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2208 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2210 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2213 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2215 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2219 m
->shutdown_dry_run
= dry_run
;
2221 if (m
->nologin_timeout_source
) {
2222 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2224 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2226 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2228 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2230 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2231 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2233 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2236 m
->scheduled_shutdown_timeout
= elapse
;
2238 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2240 const char *tty
= NULL
;
2242 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2243 (void) sd_bus_creds_get_tty(creds
, &tty
);
2245 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2247 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2252 r
= manager_setup_wall_message_timer(m
);
2256 r
= update_schedule_file(m
);
2260 return sd_bus_reply_method_return(message
, NULL
);
2263 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2264 Manager
*m
= userdata
;
2270 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2271 reset_scheduled_shutdown(m
);
2273 if (cancelled
&& m
->enable_wall_messages
) {
2274 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2275 _cleanup_free_
char *username
= NULL
;
2276 const char *tty
= NULL
;
2280 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2282 (void) sd_bus_creds_get_uid(creds
, &uid
);
2283 (void) sd_bus_creds_get_tty(creds
, &tty
);
2286 username
= uid_to_name(uid
);
2287 utmp_wall("The system shutdown has been cancelled",
2288 username
, tty
, logind_wall_tty_filter
, m
);
2291 return sd_bus_reply_method_return(message
, "b", cancelled
);
2294 static int method_can_shutdown_or_sleep(
2296 sd_bus_message
*message
,
2299 const char *action_multiple_sessions
,
2300 const char *action_ignore_inhibit
,
2301 const char *sleep_verb
,
2302 sd_bus_error
*error
) {
2304 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2305 HandleAction handle
;
2306 bool multiple_sessions
, challenge
, blocked
;
2307 const char *result
= NULL
;
2314 assert(w
<= _INHIBIT_WHAT_MAX
);
2316 assert(action_multiple_sessions
);
2317 assert(action_ignore_inhibit
);
2320 r
= can_sleep(sleep_verb
);
2321 if (IN_SET(r
, 0, -ENOSPC
))
2322 return sd_bus_reply_method_return(message
, "s", "na");
2327 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2331 r
= sd_bus_creds_get_euid(creds
, &uid
);
2335 r
= have_multiple_sessions(m
, uid
);
2339 multiple_sessions
= r
> 0;
2340 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2342 handle
= handle_action_from_string(sleep_verb
);
2346 target
= manager_target_for_action(handle
);
2348 _cleanup_free_
char *load_state
= NULL
;
2350 r
= unit_load_state(m
->bus
, target
, &load_state
);
2354 if (!streq(load_state
, "loaded")) {
2361 if (multiple_sessions
) {
2362 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2369 result
= "challenge";
2375 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2382 } else if (challenge
) {
2383 if (!result
|| streq(result
, "yes"))
2384 result
= "challenge";
2389 if (!multiple_sessions
&& !blocked
) {
2390 /* If neither inhibit nor multiple sessions
2391 * apply then just check the normal policy */
2393 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2400 result
= "challenge";
2406 return sd_bus_reply_method_return(message
, "s", result
);
2409 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2410 Manager
*m
= userdata
;
2412 return method_can_shutdown_or_sleep(
2415 "org.freedesktop.login1.power-off",
2416 "org.freedesktop.login1.power-off-multiple-sessions",
2417 "org.freedesktop.login1.power-off-ignore-inhibit",
2422 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2423 Manager
*m
= userdata
;
2425 return method_can_shutdown_or_sleep(
2428 "org.freedesktop.login1.reboot",
2429 "org.freedesktop.login1.reboot-multiple-sessions",
2430 "org.freedesktop.login1.reboot-ignore-inhibit",
2435 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2436 Manager
*m
= userdata
;
2438 return method_can_shutdown_or_sleep(
2441 "org.freedesktop.login1.halt",
2442 "org.freedesktop.login1.halt-multiple-sessions",
2443 "org.freedesktop.login1.halt-ignore-inhibit",
2448 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2449 Manager
*m
= userdata
;
2451 return method_can_shutdown_or_sleep(
2454 "org.freedesktop.login1.suspend",
2455 "org.freedesktop.login1.suspend-multiple-sessions",
2456 "org.freedesktop.login1.suspend-ignore-inhibit",
2461 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2462 Manager
*m
= userdata
;
2464 return method_can_shutdown_or_sleep(
2467 "org.freedesktop.login1.hibernate",
2468 "org.freedesktop.login1.hibernate-multiple-sessions",
2469 "org.freedesktop.login1.hibernate-ignore-inhibit",
2474 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2475 Manager
*m
= userdata
;
2477 return method_can_shutdown_or_sleep(
2480 "org.freedesktop.login1.hibernate",
2481 "org.freedesktop.login1.hibernate-multiple-sessions",
2482 "org.freedesktop.login1.hibernate-ignore-inhibit",
2487 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2488 Manager
*m
= userdata
;
2490 return method_can_shutdown_or_sleep(
2493 "org.freedesktop.login1.hibernate",
2494 "org.freedesktop.login1.hibernate-multiple-sessions",
2495 "org.freedesktop.login1.hibernate-ignore-inhibit",
2496 "suspend-then-hibernate",
2500 static int property_get_reboot_parameter(
2503 const char *interface
,
2504 const char *property
,
2505 sd_bus_message
*reply
,
2507 sd_bus_error
*error
) {
2508 _cleanup_free_
char *parameter
= NULL
;
2515 r
= read_reboot_parameter(¶meter
);
2519 return sd_bus_message_append(reply
, "s", parameter
);
2522 static int method_set_reboot_parameter(
2523 sd_bus_message
*message
,
2525 sd_bus_error
*error
) {
2527 Manager
*m
= userdata
;
2534 r
= sd_bus_message_read(message
, "s", &arg
);
2538 r
= detect_container();
2542 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2543 "Reboot parameter not supported in containers, refusing.");
2545 r
= bus_verify_polkit_async(message
,
2547 "org.freedesktop.login1.set-reboot-parameter",
2551 &m
->polkit_registry
,
2556 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2558 r
= update_reboot_parameter_and_warn(arg
, false);
2562 return sd_bus_reply_method_return(message
, NULL
);
2565 static int method_can_reboot_parameter(
2566 sd_bus_message
*message
,
2568 sd_bus_error
*error
) {
2570 Manager
*m
= userdata
;
2576 r
= detect_container();
2579 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2580 return sd_bus_reply_method_return(message
, "s", "na");
2582 return return_test_polkit(
2585 "org.freedesktop.login1.set-reboot-parameter",
2591 static int property_get_reboot_to_firmware_setup(
2594 const char *interface
,
2595 const char *property
,
2596 sd_bus_message
*reply
,
2598 sd_bus_error
*error
) {
2605 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2607 /* EFI case: let's see what is currently configured in the EFI variables */
2608 r
= efi_get_reboot_to_firmware();
2609 if (r
< 0 && r
!= -EOPNOTSUPP
)
2610 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2612 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2614 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2615 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2616 if (errno
!= ENOENT
)
2617 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2624 return sd_bus_message_append(reply
, "b", r
> 0);
2627 static int method_set_reboot_to_firmware_setup(
2628 sd_bus_message
*message
,
2630 sd_bus_error
*error
) {
2632 Manager
*m
= userdata
;
2639 r
= sd_bus_message_read(message
, "b", &b
);
2643 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2645 /* EFI case: let's see what the firmware supports */
2647 r
= efi_reboot_to_firmware_supported();
2648 if (r
== -EOPNOTSUPP
)
2649 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2655 } else if (r
<= 0) {
2656 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2659 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2661 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2663 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2666 r
= bus_verify_polkit_async(message
,
2668 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2672 &m
->polkit_registry
,
2677 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2680 r
= efi_set_reboot_to_firmware(b
);
2685 r
= touch("/run/systemd/reboot-to-firmware-setup");
2689 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2694 return sd_bus_reply_method_return(message
, NULL
);
2697 static int method_can_reboot_to_firmware_setup(
2698 sd_bus_message
*message
,
2700 sd_bus_error
*error
) {
2702 Manager
*m
= userdata
;
2708 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2710 /* EFI case: let's see what the firmware supports */
2712 r
= efi_reboot_to_firmware_supported();
2714 if (r
!= -EOPNOTSUPP
)
2715 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2717 return sd_bus_reply_method_return(message
, "s", "na");
2720 } else if (r
<= 0) {
2721 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2724 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2726 return sd_bus_reply_method_return(message
, "s", "na");
2729 return return_test_polkit(
2732 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2738 static int property_get_reboot_to_boot_loader_menu(
2741 const char *interface
,
2742 const char *property
,
2743 sd_bus_message
*reply
,
2745 sd_bus_error
*error
) {
2747 uint64_t x
= UINT64_MAX
;
2754 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2756 _cleanup_free_
char *v
= NULL
;
2758 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2760 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2761 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2762 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2765 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", &v
);
2768 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable: %m");
2772 r
= safe_atou64(v
, &sec
);
2774 log_warning_errno(r
, "Failed to parse LoaderConfigTimeoutOneShot value '%s': %m", v
);
2775 else if (sec
> (USEC_INFINITY
/ USEC_PER_SEC
))
2776 log_warning("LoaderConfigTimeoutOneShot too large, ignoring: %m");
2778 x
= sec
* USEC_PER_SEC
; /* return in µs */
2782 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2784 _cleanup_free_
char *v
= NULL
;
2786 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2788 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2791 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2793 r
= safe_atou64(v
, &x
);
2795 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2799 return sd_bus_message_append(reply
, "t", x
);
2802 static int method_set_reboot_to_boot_loader_menu(
2803 sd_bus_message
*message
,
2805 sd_bus_error
*error
) {
2807 Manager
*m
= userdata
;
2815 r
= sd_bus_message_read(message
, "t", &x
);
2819 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2823 /* EFI case: let's see if booting into boot loader menu is supported. */
2825 r
= efi_loader_get_features(&features
);
2827 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2828 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2829 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2833 } else if (r
<= 0) {
2834 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2837 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2839 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2841 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2844 r
= bus_verify_polkit_async(message
,
2846 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2850 &m
->polkit_registry
,
2855 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2858 if (x
== UINT64_MAX
)
2859 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", NULL
, 0);
2861 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2862 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2864 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", buf
);
2869 if (x
== UINT64_MAX
) {
2870 if (unlink("/run/systemd/reboot-to-loader-menu") < 0 && errno
!= ENOENT
)
2873 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2875 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2877 r
= write_string_file_atomic_label("/run/systemd/reboot-to-loader-menu", buf
);
2883 return sd_bus_reply_method_return(message
, NULL
);
2886 static int method_can_reboot_to_boot_loader_menu(
2887 sd_bus_message
*message
,
2889 sd_bus_error
*error
) {
2891 Manager
*m
= userdata
;
2897 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2899 uint64_t features
= 0;
2901 /* EFI case, let's see if booting into boot loader menu is supported. */
2903 r
= efi_loader_get_features(&features
);
2905 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2906 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2907 return sd_bus_reply_method_return(message
, "s", "na");
2909 } else if (r
<= 0) {
2910 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2913 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2915 return sd_bus_reply_method_return(message
, "s", "na");
2918 return return_test_polkit(
2921 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2927 static int property_get_reboot_to_boot_loader_entry(
2930 const char *interface
,
2931 const char *property
,
2932 sd_bus_message
*reply
,
2934 sd_bus_error
*error
) {
2936 _cleanup_free_
char *v
= NULL
;
2943 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2945 /* EFI case: let's read the LoaderEntryOneShot variable */
2947 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &v
);
2950 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable: %m");
2951 } else if (!efi_loader_entry_name_valid(v
)) {
2952 log_warning("LoaderEntryOneShot contains invalid entry name '%s', ignoring.", v
);
2956 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2959 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2961 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2964 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry: %m");
2965 } else if (!efi_loader_entry_name_valid(v
)) {
2966 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2971 return sd_bus_message_append(reply
, "s", v
);
2974 static int boot_loader_entry_exists(const char *id
) {
2975 _cleanup_(boot_config_free
) BootConfig config
= {};
2980 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2981 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2984 (void) boot_entries_augment_from_loader(&config
, true);
2986 return boot_config_has_entry(&config
, id
);
2989 static int method_set_reboot_to_boot_loader_entry(
2990 sd_bus_message
*message
,
2992 sd_bus_error
*error
) {
2994 Manager
*m
= userdata
;
3002 r
= sd_bus_message_read(message
, "s", &v
);
3008 else if (efi_loader_entry_name_valid(v
)) {
3009 r
= boot_loader_entry_exists(v
);
3013 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3015 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3017 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3021 /* EFI case: let's see if booting into boot loader entry is supported. */
3023 r
= efi_loader_get_features(&features
);
3025 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3026 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3027 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3031 } else if (r
<= 0) {
3032 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3035 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3037 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3039 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3042 r
= bus_verify_polkit_async(message
,
3044 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3048 &m
->polkit_registry
,
3053 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3058 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", NULL
, 0);
3060 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", v
);
3065 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3068 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3074 return sd_bus_reply_method_return(message
, NULL
);
3077 static int method_can_reboot_to_boot_loader_entry(
3078 sd_bus_message
*message
,
3080 sd_bus_error
*error
) {
3082 Manager
*m
= userdata
;
3088 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3090 uint64_t features
= 0;
3092 /* EFI case, let's see if booting into boot loader entry is supported. */
3094 r
= efi_loader_get_features(&features
);
3096 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3097 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3098 return sd_bus_reply_method_return(message
, "s", "na");
3100 } else if (r
<= 0) {
3101 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3104 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3106 return sd_bus_reply_method_return(message
, "s", "na");
3109 return return_test_polkit(
3112 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3118 static int property_get_boot_loader_entries(
3121 const char *interface
,
3122 const char *property
,
3123 sd_bus_message
*reply
,
3125 sd_bus_error
*error
) {
3127 _cleanup_(boot_config_free
) BootConfig config
= {};
3135 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3136 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3139 (void) boot_entries_augment_from_loader(&config
, true);
3141 r
= sd_bus_message_open_container(reply
, 'a', "s");
3145 for (i
= 0; i
< config
.n_entries
; i
++) {
3146 BootEntry
*e
= config
.entries
+ i
;
3148 r
= sd_bus_message_append(reply
, "s", e
->id
);
3153 return sd_bus_message_close_container(reply
);
3156 static int method_set_wall_message(
3157 sd_bus_message
*message
,
3159 sd_bus_error
*error
) {
3162 Manager
*m
= userdata
;
3164 unsigned enable_wall_messages
;
3169 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3173 /* Short-circuit the operation if the desired state is already in place, to
3174 * avoid an unnecessary polkit permission check. */
3175 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3176 m
->enable_wall_messages
== enable_wall_messages
)
3179 r
= bus_verify_polkit_async(message
,
3181 "org.freedesktop.login1.set-wall-message",
3185 &m
->polkit_registry
,
3190 return 1; /* Will call us back */
3192 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3196 m
->enable_wall_messages
= enable_wall_messages
;
3199 return sd_bus_reply_method_return(message
, NULL
);
3202 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3203 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3204 const char *who
, *why
, *what
, *mode
;
3205 _cleanup_free_
char *id
= NULL
;
3206 _cleanup_close_
int fifo_fd
= -1;
3207 Manager
*m
= userdata
;
3208 Inhibitor
*i
= NULL
;
3218 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3222 w
= inhibit_what_from_string(what
);
3224 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3225 "Invalid what specification %s", what
);
3227 mm
= inhibit_mode_from_string(mode
);
3229 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3230 "Invalid mode specification %s", mode
);
3232 /* Delay is only supported for shutdown/sleep */
3233 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3234 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3235 "Delay inhibitors only supported for shutdown and sleep");
3237 /* Don't allow taking delay locks while we are already
3238 * executing the operation. We shouldn't create the impression
3239 * that the lock was successful if the machine is about to go
3240 * down/suspend any moment. */
3241 if (m
->action_what
& w
)
3242 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3243 "The operation inhibition has been requested for is already running");
3245 r
= bus_verify_polkit_async(
3248 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3249 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3250 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3251 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3252 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3253 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3254 "org.freedesktop.login1.inhibit-handle-lid-switch",
3258 &m
->polkit_registry
,
3263 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3265 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3269 r
= sd_bus_creds_get_euid(creds
, &uid
);
3273 r
= sd_bus_creds_get_pid(creds
, &pid
);
3277 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3278 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3279 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3285 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3288 } while (hashmap_get(m
->inhibitors
, id
));
3290 r
= manager_add_inhibitor(m
, id
, &i
);
3298 i
->why
= strdup(why
);
3299 i
->who
= strdup(who
);
3301 if (!i
->why
|| !i
->who
) {
3306 fifo_fd
= inhibitor_create_fifo(i
);
3312 r
= inhibitor_start(i
);
3316 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3325 const sd_bus_vtable manager_vtable
[] = {
3326 SD_BUS_VTABLE_START(0),
3328 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3329 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3331 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3332 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3333 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3334 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3335 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3336 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3337 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3338 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3339 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3340 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3341 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3342 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3343 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3344 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3345 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3346 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3347 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3348 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3349 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3350 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3351 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3352 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3353 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3354 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3355 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3356 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3357 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3358 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3359 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3360 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3361 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3362 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3363 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3364 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3365 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3366 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3367 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3368 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3370 SD_BUS_METHOD_WITH_NAMES("GetSession",
3372 SD_BUS_PARAM(session_id
),
3374 SD_BUS_PARAM(object_path
),
3376 SD_BUS_VTABLE_UNPRIVILEGED
),
3377 SD_BUS_METHOD_WITH_NAMES("GetSessionByPID",
3381 SD_BUS_PARAM(object_path
),
3382 method_get_session_by_pid
,
3383 SD_BUS_VTABLE_UNPRIVILEGED
),
3384 SD_BUS_METHOD_WITH_NAMES("GetUser",
3388 SD_BUS_PARAM(object_path
),
3390 SD_BUS_VTABLE_UNPRIVILEGED
),
3391 SD_BUS_METHOD_WITH_NAMES("GetUserByPID",
3395 SD_BUS_PARAM(object_path
),
3396 method_get_user_by_pid
,
3397 SD_BUS_VTABLE_UNPRIVILEGED
),
3398 SD_BUS_METHOD_WITH_NAMES("GetSeat",
3400 SD_BUS_PARAM(seat_id
),
3402 SD_BUS_PARAM(object_path
),
3404 SD_BUS_VTABLE_UNPRIVILEGED
),
3405 SD_BUS_METHOD_WITH_NAMES("ListSessions",
3408 SD_BUS_PARAM(sessions
),
3409 method_list_sessions
,
3410 SD_BUS_VTABLE_UNPRIVILEGED
),
3411 SD_BUS_METHOD_WITH_NAMES("ListUsers",
3414 SD_BUS_PARAM(users
),
3416 SD_BUS_VTABLE_UNPRIVILEGED
),
3417 SD_BUS_METHOD_WITH_NAMES("ListSeats",
3420 SD_BUS_PARAM(seats
),
3422 SD_BUS_VTABLE_UNPRIVILEGED
),
3423 SD_BUS_METHOD_WITH_NAMES("ListInhibitors",
3426 SD_BUS_PARAM(inhibitors
),
3427 method_list_inhibitors
,
3428 SD_BUS_VTABLE_UNPRIVILEGED
),
3429 SD_BUS_METHOD_WITH_NAMES("CreateSession",
3430 "uusssssussbssa(sv)",
3433 SD_BUS_PARAM(service
)
3436 SD_BUS_PARAM(desktop
)
3437 SD_BUS_PARAM(seat_id
)
3440 SD_BUS_PARAM(display
)
3441 SD_BUS_PARAM(remote
)
3442 SD_BUS_PARAM(remote_user
)
3443 SD_BUS_PARAM(remote_host
)
3444 SD_BUS_PARAM(properties
),
3446 SD_BUS_PARAM(session_id
)
3447 SD_BUS_PARAM(object_path
)
3448 SD_BUS_PARAM(runtime_path
)
3449 SD_BUS_PARAM(fifo_fd
)
3451 SD_BUS_PARAM(seat_id
)
3453 SD_BUS_PARAM(existing
),
3454 method_create_session
,
3456 SD_BUS_METHOD_WITH_NAMES("ReleaseSession",
3458 SD_BUS_PARAM(session_id
),
3460 method_release_session
,
3462 SD_BUS_METHOD_WITH_NAMES("ActivateSession",
3464 SD_BUS_PARAM(session_id
),
3466 method_activate_session
,
3467 SD_BUS_VTABLE_UNPRIVILEGED
),
3468 SD_BUS_METHOD_WITH_NAMES("ActivateSessionOnSeat",
3470 SD_BUS_PARAM(session_id
)
3471 SD_BUS_PARAM(seat_id
),
3473 method_activate_session_on_seat
,
3474 SD_BUS_VTABLE_UNPRIVILEGED
),
3475 SD_BUS_METHOD_WITH_NAMES("LockSession",
3477 SD_BUS_PARAM(session_id
),
3479 method_lock_session
,
3480 SD_BUS_VTABLE_UNPRIVILEGED
),
3481 SD_BUS_METHOD_WITH_NAMES("UnlockSession",
3483 SD_BUS_PARAM(session_id
),
3485 method_lock_session
,
3486 SD_BUS_VTABLE_UNPRIVILEGED
),
3487 SD_BUS_METHOD("LockSessions",
3490 method_lock_sessions
,
3491 SD_BUS_VTABLE_UNPRIVILEGED
),
3492 SD_BUS_METHOD("UnlockSessions",
3495 method_lock_sessions
,
3496 SD_BUS_VTABLE_UNPRIVILEGED
),
3497 SD_BUS_METHOD_WITH_NAMES("KillSession",
3499 SD_BUS_PARAM(session_id
)
3501 SD_BUS_PARAM(signal_number
),
3503 method_kill_session
,
3504 SD_BUS_VTABLE_UNPRIVILEGED
),
3505 SD_BUS_METHOD_WITH_NAMES("KillUser",
3508 SD_BUS_PARAM(signal_number
),
3511 SD_BUS_VTABLE_UNPRIVILEGED
),
3512 SD_BUS_METHOD_WITH_NAMES("TerminateSession",
3514 SD_BUS_PARAM(session_id
),
3516 method_terminate_session
,
3517 SD_BUS_VTABLE_UNPRIVILEGED
),
3518 SD_BUS_METHOD_WITH_NAMES("TerminateUser",
3522 method_terminate_user
,
3523 SD_BUS_VTABLE_UNPRIVILEGED
),
3524 SD_BUS_METHOD_WITH_NAMES("TerminateSeat",
3526 SD_BUS_PARAM(seat_id
),
3528 method_terminate_seat
,
3529 SD_BUS_VTABLE_UNPRIVILEGED
),
3530 SD_BUS_METHOD_WITH_NAMES("SetUserLinger",
3533 SD_BUS_PARAM(enable
)
3534 SD_BUS_PARAM(interactive
),
3536 method_set_user_linger
,
3537 SD_BUS_VTABLE_UNPRIVILEGED
),
3538 SD_BUS_METHOD_WITH_NAMES("AttachDevice",
3540 SD_BUS_PARAM(seat_id
)
3541 SD_BUS_PARAM(sysfs_path
)
3542 SD_BUS_PARAM(interactive
),
3544 method_attach_device
,
3545 SD_BUS_VTABLE_UNPRIVILEGED
),
3546 SD_BUS_METHOD_WITH_NAMES("FlushDevices",
3548 SD_BUS_PARAM(interactive
),
3550 method_flush_devices
,
3551 SD_BUS_VTABLE_UNPRIVILEGED
),
3552 SD_BUS_METHOD_WITH_NAMES("PowerOff",
3554 SD_BUS_PARAM(interactive
),
3557 SD_BUS_VTABLE_UNPRIVILEGED
),
3558 SD_BUS_METHOD_WITH_NAMES("Reboot",
3560 SD_BUS_PARAM(interactive
),
3563 SD_BUS_VTABLE_UNPRIVILEGED
),
3564 SD_BUS_METHOD_WITH_NAMES("Halt",
3566 SD_BUS_PARAM(interactive
),
3569 SD_BUS_VTABLE_UNPRIVILEGED
),
3570 SD_BUS_METHOD_WITH_NAMES("Suspend",
3572 SD_BUS_PARAM(interactive
),
3575 SD_BUS_VTABLE_UNPRIVILEGED
),
3576 SD_BUS_METHOD_WITH_NAMES("Hibernate",
3578 SD_BUS_PARAM(interactive
),
3581 SD_BUS_VTABLE_UNPRIVILEGED
),
3582 SD_BUS_METHOD_WITH_NAMES("HybridSleep",
3584 SD_BUS_PARAM(interactive
),
3586 method_hybrid_sleep
,
3587 SD_BUS_VTABLE_UNPRIVILEGED
),
3588 SD_BUS_METHOD_WITH_NAMES("SuspendThenHibernate",
3590 SD_BUS_PARAM(interactive
),
3592 method_suspend_then_hibernate
,
3593 SD_BUS_VTABLE_UNPRIVILEGED
),
3594 SD_BUS_METHOD_WITH_NAMES("CanPowerOff",
3597 SD_BUS_PARAM(result
),
3598 method_can_poweroff
,
3599 SD_BUS_VTABLE_UNPRIVILEGED
),
3600 SD_BUS_METHOD_WITH_NAMES("CanReboot",
3603 SD_BUS_PARAM(result
),
3605 SD_BUS_VTABLE_UNPRIVILEGED
),
3606 SD_BUS_METHOD_WITH_NAMES("CanHalt",
3609 SD_BUS_PARAM(result
),
3611 SD_BUS_VTABLE_UNPRIVILEGED
),
3612 SD_BUS_METHOD_WITH_NAMES("CanSuspend",
3615 SD_BUS_PARAM(result
),
3617 SD_BUS_VTABLE_UNPRIVILEGED
),
3618 SD_BUS_METHOD_WITH_NAMES("CanHibernate",
3621 SD_BUS_PARAM(result
),
3622 method_can_hibernate
,
3623 SD_BUS_VTABLE_UNPRIVILEGED
),
3624 SD_BUS_METHOD_WITH_NAMES("CanHybridSleep",
3627 SD_BUS_PARAM(result
),
3628 method_can_hybrid_sleep
,
3629 SD_BUS_VTABLE_UNPRIVILEGED
),
3630 SD_BUS_METHOD_WITH_NAMES("CanSuspendThenHibernate",
3633 SD_BUS_PARAM(result
),
3634 method_can_suspend_then_hibernate
,
3635 SD_BUS_VTABLE_UNPRIVILEGED
),
3636 SD_BUS_METHOD_WITH_NAMES("ScheduleShutdown",
3641 method_schedule_shutdown
,
3642 SD_BUS_VTABLE_UNPRIVILEGED
),
3643 SD_BUS_METHOD_WITH_NAMES("CancelScheduledShutdown",
3646 SD_BUS_PARAM(cancelled
),
3647 method_cancel_scheduled_shutdown
,
3648 SD_BUS_VTABLE_UNPRIVILEGED
),
3649 SD_BUS_METHOD_WITH_NAMES("Inhibit",
3656 SD_BUS_PARAM(pipe_fd
),
3658 SD_BUS_VTABLE_UNPRIVILEGED
),
3659 SD_BUS_METHOD_WITH_NAMES("CanRebootParameter",
3662 SD_BUS_PARAM(result
),
3663 method_can_reboot_parameter
,
3664 SD_BUS_VTABLE_UNPRIVILEGED
),
3665 SD_BUS_METHOD_WITH_NAMES("SetRebootParameter",
3667 SD_BUS_PARAM(parameter
),
3669 method_set_reboot_parameter
,
3670 SD_BUS_VTABLE_UNPRIVILEGED
),
3671 SD_BUS_METHOD_WITH_NAMES("CanRebootToFirmwareSetup",
3674 SD_BUS_PARAM(result
),
3675 method_can_reboot_to_firmware_setup
,
3676 SD_BUS_VTABLE_UNPRIVILEGED
),
3677 SD_BUS_METHOD_WITH_NAMES("SetRebootToFirmwareSetup",
3679 SD_BUS_PARAM(enable
),
3681 method_set_reboot_to_firmware_setup
,
3682 SD_BUS_VTABLE_UNPRIVILEGED
),
3683 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderMenu",
3686 SD_BUS_PARAM(result
),
3687 method_can_reboot_to_boot_loader_menu
,
3688 SD_BUS_VTABLE_UNPRIVILEGED
),
3689 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderMenu",
3691 SD_BUS_PARAM(timeout
),
3693 method_set_reboot_to_boot_loader_menu
,
3694 SD_BUS_VTABLE_UNPRIVILEGED
),
3695 SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderEntry",
3698 SD_BUS_PARAM(result
),
3699 method_can_reboot_to_boot_loader_entry
,
3700 SD_BUS_VTABLE_UNPRIVILEGED
),
3701 SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderEntry",
3703 SD_BUS_PARAM(boot_loader_entry
),
3705 method_set_reboot_to_boot_loader_entry
,
3706 SD_BUS_VTABLE_UNPRIVILEGED
),
3707 SD_BUS_METHOD_WITH_NAMES("SetWallMessage",
3709 SD_BUS_PARAM(wall_message
)
3710 SD_BUS_PARAM(enable
),
3712 method_set_wall_message
,
3713 SD_BUS_VTABLE_UNPRIVILEGED
),
3715 SD_BUS_SIGNAL_WITH_NAMES("SessionNew",
3717 SD_BUS_PARAM(session_id
)
3718 SD_BUS_PARAM(object_path
),
3720 SD_BUS_SIGNAL_WITH_NAMES("SessionRemoved",
3722 SD_BUS_PARAM(session_id
)
3723 SD_BUS_PARAM(object_path
),
3725 SD_BUS_SIGNAL_WITH_NAMES("UserNew",
3728 SD_BUS_PARAM(object_path
),
3730 SD_BUS_SIGNAL_WITH_NAMES("UserRemoved",
3733 SD_BUS_PARAM(object_path
),
3735 SD_BUS_SIGNAL_WITH_NAMES("SeatNew",
3737 SD_BUS_PARAM(seat_id
)
3738 SD_BUS_PARAM(object_path
),
3740 SD_BUS_SIGNAL_WITH_NAMES("SeatRemoved",
3742 SD_BUS_PARAM(seat_id
)
3743 SD_BUS_PARAM(object_path
),
3745 SD_BUS_SIGNAL_WITH_NAMES("PrepareForShutdown",
3747 SD_BUS_PARAM(start
),
3749 SD_BUS_SIGNAL_WITH_NAMES("PrepareForSleep",
3751 SD_BUS_PARAM(start
),
3757 static int session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
3764 if (result
&& !streq(result
, "done")) {
3765 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3767 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3768 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
3769 return session_send_create_reply(s
, &e
);
3772 return session_send_create_reply(s
, NULL
);
3775 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3776 const char *path
, *result
, *unit
;
3777 Manager
*m
= userdata
;
3786 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3788 bus_log_parse_error(r
);
3792 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3793 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
3795 /* Tell people that they now may take a lock again */
3796 (void) send_prepare_for(m
, m
->action_what
, false);
3798 m
->action_job
= mfree(m
->action_job
);
3799 m
->action_unit
= NULL
;
3804 session
= hashmap_get(m
->session_units
, unit
);
3806 if (streq_ptr(path
, session
->scope_job
)) {
3807 session
->scope_job
= mfree(session
->scope_job
);
3808 (void) session_jobs_reply(session
, id
, unit
, result
);
3810 session_save(session
);
3811 user_save(session
->user
);
3814 session_add_to_gc_queue(session
);
3817 user
= hashmap_get(m
->user_units
, unit
);
3819 if (streq_ptr(path
, user
->service_job
)) {
3820 user
->service_job
= mfree(user
->service_job
);
3822 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3823 (void) session_jobs_reply(session
, id
, unit
, NULL
/* don't propagate user service failures to the client */);
3828 user_add_to_gc_queue(user
);
3834 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3835 const char *path
, *unit
;
3836 Manager
*m
= userdata
;
3844 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3846 bus_log_parse_error(r
);
3850 session
= hashmap_get(m
->session_units
, unit
);
3852 session_add_to_gc_queue(session
);
3854 user
= hashmap_get(m
->user_units
, unit
);
3856 user_add_to_gc_queue(user
);
3861 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3862 _cleanup_free_
char *unit
= NULL
;
3863 Manager
*m
= userdata
;
3872 path
= sd_bus_message_get_path(message
);
3876 r
= unit_name_from_dbus_path(path
, &unit
);
3877 if (r
== -EINVAL
) /* not a unit */
3884 session
= hashmap_get(m
->session_units
, unit
);
3886 session_add_to_gc_queue(session
);
3888 user
= hashmap_get(m
->user_units
, unit
);
3890 user_add_to_gc_queue(user
);
3895 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3896 Manager
*m
= userdata
;
3904 r
= sd_bus_message_read(message
, "b", &b
);
3906 bus_log_parse_error(r
);
3913 /* systemd finished reloading, let's recheck all our sessions */
3914 log_debug("System manager has been reloaded, rechecking sessions...");
3916 HASHMAP_FOREACH(session
, m
->sessions
, i
)
3917 session_add_to_gc_queue(session
);
3922 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3927 l
= strv_from_stdarg_alloca(property
);
3929 return sd_bus_emit_properties_changed_strv(
3931 "/org/freedesktop/login1",
3932 "org.freedesktop.login1.Manager",
3936 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3941 r
= sd_bus_message_read(reply
, "o", &j
);
3953 int manager_start_scope(
3958 const char *description
,
3961 const char *requires_mounts_for
,
3962 sd_bus_message
*more_properties
,
3963 sd_bus_error
*error
,
3966 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3975 r
= sd_bus_message_new_method_call(
3978 "org.freedesktop.systemd1",
3979 "/org/freedesktop/systemd1",
3980 "org.freedesktop.systemd1.Manager",
3981 "StartTransientUnit");
3985 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3989 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3993 if (!isempty(slice
)) {
3994 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3999 if (!isempty(description
)) {
4000 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4005 STRV_FOREACH(i
, wants
) {
4006 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
4011 STRV_FOREACH(i
, after
) {
4012 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4017 if (!empty_or_root(requires_mounts_for
)) {
4018 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4023 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4025 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4029 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
4033 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4034 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
4036 return bus_log_create_error(r
);
4038 if (more_properties
) {
4039 /* If TasksMax also appears here, it will overwrite the default value set above */
4040 r
= sd_bus_message_copy(m
, more_properties
, true);
4045 r
= sd_bus_message_close_container(m
);
4049 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4053 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
4057 return strdup_job(reply
, job
);
4060 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4061 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4068 r
= sd_bus_call_method(
4070 "org.freedesktop.systemd1",
4071 "/org/freedesktop/systemd1",
4072 "org.freedesktop.systemd1.Manager",
4076 "ss", unit
, "replace");
4080 return strdup_job(reply
, job
);
4083 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
4084 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4091 r
= sd_bus_call_method(
4093 "org.freedesktop.systemd1",
4094 "/org/freedesktop/systemd1",
4095 "org.freedesktop.systemd1.Manager",
4099 "ss", unit
, "fail");
4101 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4102 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
4105 sd_bus_error_free(error
);
4112 return strdup_job(reply
, job
);
4115 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4116 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4117 _cleanup_free_
char *path
= NULL
;
4123 path
= unit_dbus_path_from_name(scope
);
4127 r
= sd_bus_call_method(
4129 "org.freedesktop.systemd1",
4131 "org.freedesktop.systemd1.Scope",
4137 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4138 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
4139 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
4142 sd_bus_error_move(ret_error
, &error
);
4149 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
4153 return sd_bus_call_method(
4155 "org.freedesktop.systemd1",
4156 "/org/freedesktop/systemd1",
4157 "org.freedesktop.systemd1.Manager",
4161 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
4164 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4165 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4166 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4167 _cleanup_free_
char *path
= NULL
;
4174 path
= unit_dbus_path_from_name(unit
);
4178 r
= sd_bus_get_property(
4180 "org.freedesktop.systemd1",
4182 "org.freedesktop.systemd1.Unit",
4188 /* systemd might have dropped off momentarily, let's
4189 * not make this an error */
4190 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
4191 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
4194 /* If the unit is already unloaded then it's not
4196 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
4197 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
4200 sd_bus_error_move(ret_error
, &error
);
4204 r
= sd_bus_message_read(reply
, "s", &state
);
4208 return !STR_IN_SET(state
, "inactive", "failed");
4211 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4212 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4213 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4219 r
= sd_bus_get_property(
4221 "org.freedesktop.systemd1",
4223 "org.freedesktop.systemd1.Job",
4229 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
4230 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
4233 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4236 sd_bus_error_move(ret_error
, &error
);
4240 /* We don't actually care about the state really. The fact
4241 * that we could read the job state is enough for us */