1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "sd-messages.h"
12 #include "alloc-util.h"
13 #include "audit-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-error.h"
17 #include "bus-unit-util.h"
19 #include "cgroup-util.h"
20 #include "device-util.h"
21 #include "dirent-util.h"
26 #include "fileio-label.h"
28 #include "format-util.h"
31 #include "missing_capability.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "process-util.h"
36 #include "reboot-util.h"
37 #include "selinux-util.h"
38 #include "sleep-config.h"
40 #include "stdio-util.h"
42 #include "terminal-util.h"
43 #include "tmpfile-util.h"
44 #include "unit-name.h"
45 #include "user-util.h"
46 #include "utmp-wtmp.h"
49 static int get_sender_session(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, Session
**ret
) {
51 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
56 /* Get client login session. This is not what you are looking for these days,
57 * as apps may instead belong to a user service unit. This includes terminal
58 * emulators and hence command-line apps. */
59 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
63 r
= sd_bus_creds_get_session(creds
, &name
);
69 session
= hashmap_get(m
->sessions
, name
);
77 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
78 "Caller does not belong to any known session");
81 int manager_get_session_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Session
**ret
) {
89 return get_sender_session(m
, message
, error
, ret
);
91 session
= hashmap_get(m
->sessions
, name
);
93 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
99 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
101 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
106 /* Note that we get the owner UID of the session, not the actual client UID here! */
107 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
111 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
117 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
125 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
126 "Caller does not belong to any logged in user or lingering user");
129 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
136 if (!uid_is_valid(uid
))
137 return get_sender_user(m
, message
, error
, ret
);
139 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
141 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
142 "User ID "UID_FMT
" is not logged in or lingering", uid
);
148 int manager_get_seat_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Seat
**ret
) {
159 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
163 seat
= session
->seat
;
165 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session has no seat.");
167 seat
= hashmap_get(m
->seats
, name
);
169 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
176 static int return_test_polkit(
177 sd_bus_message
*message
,
180 const char **details
,
188 r
= bus_test_polkit(message
, capability
, action
, details
, good_user
, &challenge
, e
);
195 result
= "challenge";
199 return sd_bus_reply_method_return(message
, "s", result
);
202 static int property_get_idle_hint(
205 const char *interface
,
206 const char *property
,
207 sd_bus_message
*reply
,
209 sd_bus_error
*error
) {
211 Manager
*m
= userdata
;
217 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
220 static int property_get_idle_since_hint(
223 const char *interface
,
224 const char *property
,
225 sd_bus_message
*reply
,
227 sd_bus_error
*error
) {
229 Manager
*m
= userdata
;
230 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
236 manager_get_idle_hint(m
, &t
);
238 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
241 static int property_get_inhibited(
244 const char *interface
,
245 const char *property
,
246 sd_bus_message
*reply
,
248 sd_bus_error
*error
) {
250 Manager
*m
= userdata
;
257 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
259 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
262 static int property_get_preparing(
265 const char *interface
,
266 const char *property
,
267 sd_bus_message
*reply
,
269 sd_bus_error
*error
) {
271 Manager
*m
= userdata
;
278 if (streq(property
, "PreparingForShutdown"))
279 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
281 b
= m
->action_what
& INHIBIT_SLEEP
;
283 return sd_bus_message_append(reply
, "b", b
);
286 static int property_get_scheduled_shutdown(
289 const char *interface
,
290 const char *property
,
291 sd_bus_message
*reply
,
293 sd_bus_error
*error
) {
295 Manager
*m
= userdata
;
302 r
= sd_bus_message_open_container(reply
, 'r', "st");
306 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
310 return sd_bus_message_close_container(reply
);
313 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
314 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
315 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
316 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
317 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
318 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
320 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
321 _cleanup_free_
char *p
= NULL
;
322 Manager
*m
= userdata
;
330 r
= sd_bus_message_read(message
, "s", &name
);
334 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
338 p
= session_bus_path(session
);
342 return sd_bus_reply_method_return(message
, "o", p
);
345 /* Get login session of a process. This is not what you are looking for these days,
346 * as apps may instead belong to a user service unit. This includes terminal
347 * emulators and hence command-line apps. */
348 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
349 _cleanup_free_
char *p
= NULL
;
350 Session
*session
= NULL
;
351 Manager
*m
= userdata
;
358 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
360 r
= sd_bus_message_read(message
, "u", &pid
);
367 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
371 r
= manager_get_session_by_pid(m
, pid
, &session
);
376 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
377 "PID "PID_FMT
" does not belong to any known session", pid
);
380 p
= session_bus_path(session
);
384 return sd_bus_reply_method_return(message
, "o", p
);
387 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
388 _cleanup_free_
char *p
= NULL
;
389 Manager
*m
= userdata
;
397 r
= sd_bus_message_read(message
, "u", &uid
);
401 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
405 p
= user_bus_path(user
);
409 return sd_bus_reply_method_return(message
, "o", p
);
412 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
413 _cleanup_free_
char *p
= NULL
;
414 Manager
*m
= userdata
;
422 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
424 r
= sd_bus_message_read(message
, "u", &pid
);
431 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
435 r
= manager_get_user_by_pid(m
, pid
, &user
);
439 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
440 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
444 p
= user_bus_path(user
);
448 return sd_bus_reply_method_return(message
, "o", p
);
451 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
452 _cleanup_free_
char *p
= NULL
;
453 Manager
*m
= userdata
;
461 r
= sd_bus_message_read(message
, "s", &name
);
465 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
469 p
= seat_bus_path(seat
);
473 return sd_bus_reply_method_return(message
, "o", p
);
476 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
477 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
478 Manager
*m
= userdata
;
486 r
= sd_bus_message_new_method_return(message
, &reply
);
490 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
494 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
495 _cleanup_free_
char *p
= NULL
;
497 p
= session_bus_path(session
);
501 r
= sd_bus_message_append(reply
, "(susso)",
503 (uint32_t) session
->user
->uid
,
505 session
->seat
? session
->seat
->id
: "",
511 r
= sd_bus_message_close_container(reply
);
515 return sd_bus_send(NULL
, reply
, NULL
);
518 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
519 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
520 Manager
*m
= userdata
;
528 r
= sd_bus_message_new_method_return(message
, &reply
);
532 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
536 HASHMAP_FOREACH(user
, m
->users
, i
) {
537 _cleanup_free_
char *p
= NULL
;
539 p
= user_bus_path(user
);
543 r
= sd_bus_message_append(reply
, "(uso)",
544 (uint32_t) user
->uid
,
551 r
= sd_bus_message_close_container(reply
);
555 return sd_bus_send(NULL
, reply
, NULL
);
558 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
559 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
560 Manager
*m
= userdata
;
568 r
= sd_bus_message_new_method_return(message
, &reply
);
572 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
576 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
577 _cleanup_free_
char *p
= NULL
;
579 p
= seat_bus_path(seat
);
583 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
588 r
= sd_bus_message_close_container(reply
);
592 return sd_bus_send(NULL
, reply
, NULL
);
595 static int method_list_inhibitors(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
;
598 Inhibitor
*inhibitor
;
605 r
= sd_bus_message_new_method_return(message
, &reply
);
609 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
613 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
615 r
= sd_bus_message_append(reply
, "(ssssuu)",
616 strempty(inhibit_what_to_string(inhibitor
->what
)),
617 strempty(inhibitor
->who
),
618 strempty(inhibitor
->why
),
619 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
620 (uint32_t) inhibitor
->uid
,
621 (uint32_t) inhibitor
->pid
);
626 r
= sd_bus_message_close_container(reply
);
630 return sd_bus_send(NULL
, reply
, NULL
);
633 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
634 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
635 _cleanup_free_
char *id
= NULL
;
636 Session
*session
= NULL
;
637 uint32_t audit_id
= 0;
638 Manager
*m
= userdata
;
652 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
653 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
655 r
= sd_bus_message_read(message
, "uusssssussbss",
656 &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
,
657 &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
661 if (!uid_is_valid(uid
))
662 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
663 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
664 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
667 t
= _SESSION_TYPE_INVALID
;
669 t
= session_type_from_string(type
);
671 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
672 "Invalid session type %s", type
);
676 c
= _SESSION_CLASS_INVALID
;
678 c
= session_class_from_string(class);
680 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
681 "Invalid session class %s", class);
684 if (isempty(desktop
))
687 if (!string_is_safe(desktop
))
688 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
689 "Invalid desktop string %s", desktop
);
695 seat
= hashmap_get(m
->seats
, cseat
);
697 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
698 "No seat '%s' known", cseat
);
701 if (tty_is_vc(tty
)) {
706 else if (seat
!= m
->seat0
)
707 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
708 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
710 v
= vtnr_from_tty(tty
);
712 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
713 "Cannot determine VT number from virtual console TTY %s", tty
);
717 else if (vtnr
!= (uint32_t) v
)
718 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
719 "Specified TTY and VT number do not match");
721 } else if (tty_is_console(tty
)) {
725 else if (seat
!= m
->seat0
)
726 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
727 "Console TTY specified but seat is not seat0");
730 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
731 "Console TTY specified but VT number is not 0");
735 if (seat_has_vts(seat
)) {
736 if (vtnr
<= 0 || vtnr
> 63)
737 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
738 "VT number out of range");
741 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
742 "Seat has no VTs but VT number not 0");
746 if (t
== _SESSION_TYPE_INVALID
) {
747 if (!isempty(display
))
749 else if (!isempty(tty
))
752 t
= SESSION_UNSPECIFIED
;
755 if (c
== _SESSION_CLASS_INVALID
) {
756 if (t
== SESSION_UNSPECIFIED
)
757 c
= SESSION_BACKGROUND
;
763 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
765 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
769 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
774 /* Check if we are already in a logind session. Or if we are in user@.service
775 * which is a special PAM session that avoids creating a logind session. */
776 r
= manager_get_user_by_pid(m
, leader
, NULL
);
780 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
,
781 "Already running in a session or user slice");
784 * Old gdm and lightdm start the user-session on the same VT as
785 * the greeter session. But they destroy the greeter session
786 * after the user-session and want the user-session to take
787 * over the VT. We need to support this for
788 * backwards-compatibility, so make sure we allow new sessions
789 * on a VT that a greeter is running on. Furthermore, to allow
790 * re-logins, we have to allow a greeter to take over a used VT for
791 * the exact same reasons.
793 if (c
!= SESSION_GREETER
&&
795 vtnr
< m
->seat0
->position_count
&&
796 m
->seat0
->positions
[vtnr
] &&
797 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
798 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
800 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
801 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
802 "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.",
805 (void) audit_session_from_pid(leader
, &audit_id
);
806 if (audit_session_is_valid(audit_id
)) {
807 /* Keep our session IDs and the audit session IDs in sync */
809 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
812 /* Wut? There's already a session by this name and we
813 * didn't find it above? Weird, then let's not trust
814 * the audit data and let's better register a new
816 if (hashmap_get(m
->sessions
, id
)) {
817 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
818 audit_id
= AUDIT_SESSION_INVALID
;
827 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
830 } while (hashmap_get(m
->sessions
, id
));
833 /* If we are not watching utmp aleady, try again */
834 manager_reconnect_utmp(m
);
836 r
= manager_add_user_by_uid(m
, uid
, &user
);
840 r
= manager_add_session(m
, id
, &session
);
844 session_set_user(session
, user
);
845 r
= session_set_leader(session
, leader
);
851 session
->remote
= remote
;
852 session
->vtnr
= vtnr
;
855 session
->tty
= strdup(tty
);
861 session
->tty_validity
= TTY_FROM_PAM
;
864 if (!isempty(display
)) {
865 session
->display
= strdup(display
);
866 if (!session
->display
) {
872 if (!isempty(remote_user
)) {
873 session
->remote_user
= strdup(remote_user
);
874 if (!session
->remote_user
) {
880 if (!isempty(remote_host
)) {
881 session
->remote_host
= strdup(remote_host
);
882 if (!session
->remote_host
) {
888 if (!isempty(service
)) {
889 session
->service
= strdup(service
);
890 if (!session
->service
) {
896 if (!isempty(desktop
)) {
897 session
->desktop
= strdup(desktop
);
898 if (!session
->desktop
) {
905 r
= seat_attach_session(seat
, session
);
910 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
914 r
= session_start(session
, message
, error
);
918 r
= sd_bus_message_exit_container(message
);
922 session
->create_message
= sd_bus_message_ref(message
);
924 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
925 * session_send_create_reply(). */
931 session_add_to_gc_queue(session
);
934 user_add_to_gc_queue(user
);
939 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
940 Manager
*m
= userdata
;
948 r
= sd_bus_message_read(message
, "s", &name
);
952 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
956 r
= session_release(session
);
960 return sd_bus_reply_method_return(message
, NULL
);
963 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
964 Manager
*m
= userdata
;
972 r
= sd_bus_message_read(message
, "s", &name
);
976 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
980 return bus_session_method_activate(message
, session
, error
);
983 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
984 const char *session_name
, *seat_name
;
985 Manager
*m
= userdata
;
993 /* Same as ActivateSession() but refuses to work if
994 * the seat doesn't match */
996 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1000 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1004 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1008 if (session
->seat
!= seat
)
1009 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1010 "Session %s not on seat %s", session_name
, seat_name
);
1012 r
= session_activate(session
);
1016 return sd_bus_reply_method_return(message
, NULL
);
1019 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1020 Manager
*m
= userdata
;
1028 r
= sd_bus_message_read(message
, "s", &name
);
1032 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1036 return bus_session_method_lock(message
, session
, error
);
1039 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1040 Manager
*m
= userdata
;
1046 r
= bus_verify_polkit_async(
1049 "org.freedesktop.login1.lock-sessions",
1053 &m
->polkit_registry
,
1058 return 1; /* Will call us back */
1060 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1064 return sd_bus_reply_method_return(message
, NULL
);
1067 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1069 Manager
*m
= userdata
;
1076 r
= sd_bus_message_read(message
, "s", &name
);
1080 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1084 return bus_session_method_kill(message
, session
, error
);
1087 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1088 Manager
*m
= userdata
;
1096 r
= sd_bus_message_read(message
, "u", &uid
);
1100 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1104 return bus_user_method_kill(message
, user
, error
);
1107 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1108 Manager
*m
= userdata
;
1116 r
= sd_bus_message_read(message
, "s", &name
);
1120 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1124 return bus_session_method_terminate(message
, session
, error
);
1127 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1128 Manager
*m
= userdata
;
1136 r
= sd_bus_message_read(message
, "u", &uid
);
1140 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1144 return bus_user_method_terminate(message
, user
, error
);
1147 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1148 Manager
*m
= userdata
;
1156 r
= sd_bus_message_read(message
, "s", &name
);
1160 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1164 return bus_seat_method_terminate(message
, seat
, error
);
1167 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1168 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1169 _cleanup_free_
char *cc
= NULL
;
1170 Manager
*m
= userdata
;
1171 int r
, b
, interactive
;
1174 uint32_t uid
, auth_uid
;
1179 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1183 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1184 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1188 if (!uid_is_valid(uid
)) {
1189 /* Note that we get the owner UID of the session or user unit,
1190 * not the actual client UID here! */
1191 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1196 /* owner_uid is racy, so for authorization we must use euid */
1197 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1204 return errno
> 0 ? -errno
: -ENOENT
;
1206 r
= bus_verify_polkit_async(
1209 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1210 "org.freedesktop.login1.set-user-linger",
1214 &m
->polkit_registry
,
1219 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1221 (void) mkdir_p_label("/var/lib/systemd", 0755);
1222 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1226 cc
= cescape(pw
->pw_name
);
1230 path
= strjoina("/var/lib/systemd/linger/", cc
);
1238 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1245 if (r
< 0 && errno
!= ENOENT
)
1248 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1250 user_add_to_gc_queue(u
);
1253 return sd_bus_reply_method_return(message
, NULL
);
1256 static int trigger_device(Manager
*m
, sd_device
*d
) {
1257 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1262 r
= sd_device_enumerator_new(&e
);
1266 r
= sd_device_enumerator_allow_uninitialized(e
);
1271 r
= sd_device_enumerator_add_match_parent(e
, d
);
1276 FOREACH_DEVICE(e
, d
) {
1277 _cleanup_free_
char *t
= NULL
;
1280 r
= sd_device_get_syspath(d
, &p
);
1284 t
= strappend(p
, "/uevent");
1288 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1294 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1295 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1296 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1297 const char *id_for_seat
;
1304 r
= sd_device_new_from_syspath(&d
, sysfs
);
1308 if (sd_device_has_tag(d
, "seat") <= 0)
1311 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1314 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1317 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1320 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1321 r
= write_string_file_atomic_label(file
, rule
);
1325 return trigger_device(m
, d
);
1328 static int flush_devices(Manager
*m
) {
1329 _cleanup_closedir_
DIR *d
;
1333 d
= opendir("/etc/udev/rules.d");
1335 if (errno
!= ENOENT
)
1336 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1340 FOREACH_DIRENT_ALL(de
, d
, break) {
1341 if (!dirent_is_file(de
))
1344 if (!startswith(de
->d_name
, "72-seat-"))
1347 if (!endswith(de
->d_name
, ".rules"))
1350 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1351 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1355 return trigger_device(m
, NULL
);
1358 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1359 const char *sysfs
, *seat
;
1360 Manager
*m
= userdata
;
1366 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1370 if (!path_startswith(sysfs
, "/sys"))
1371 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1373 if (!seat_name_is_valid(seat
))
1374 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat %s is not valid", seat
);
1376 r
= bus_verify_polkit_async(
1379 "org.freedesktop.login1.attach-device",
1383 &m
->polkit_registry
,
1388 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1390 r
= attach_device(m
, seat
, sysfs
);
1394 return sd_bus_reply_method_return(message
, NULL
);
1397 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1398 Manager
*m
= userdata
;
1404 r
= sd_bus_message_read(message
, "b", &interactive
);
1408 r
= bus_verify_polkit_async(
1411 "org.freedesktop.login1.flush-devices",
1415 &m
->polkit_registry
,
1420 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1422 r
= flush_devices(m
);
1426 return sd_bus_reply_method_return(message
, NULL
);
1429 static int have_multiple_sessions(
1438 /* Check for other users' sessions. Greeter sessions do not
1439 * count, and non-login sessions do not count either. */
1440 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1441 if (session
->class == SESSION_USER
&&
1442 session
->user
->uid
!= uid
)
1448 static int bus_manager_log_shutdown(
1450 const char *unit_name
) {
1457 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1458 p
= "MESSAGE=System is powering down";
1459 q
= "SHUTDOWN=power-off";
1460 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1461 p
= "MESSAGE=System is rebooting";
1462 q
= "SHUTDOWN=reboot";
1463 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1464 p
= "MESSAGE=System is halting";
1465 q
= "SHUTDOWN=halt";
1466 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1467 p
= "MESSAGE=System is rebooting with kexec";
1468 q
= "SHUTDOWN=kexec";
1470 p
= "MESSAGE=System is shutting down";
1474 if (isempty(m
->wall_message
))
1475 p
= strjoina(p
, ".");
1477 p
= strjoina(p
, " (", m
->wall_message
, ").");
1479 return log_struct(LOG_NOTICE
,
1480 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1485 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1486 Manager
*m
= userdata
;
1491 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1495 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1500 if (until
<= now(CLOCK_MONOTONIC
))
1503 /* We want to ignore the lid switch for a while after each
1504 * suspend, and after boot-up. Hence let's install a timer for
1505 * this. As long as the event source exists we ignore the lid
1508 if (m
->lid_switch_ignore_event_source
) {
1511 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1518 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1520 r
= sd_event_add_time(
1522 &m
->lid_switch_ignore_event_source
,
1525 lid_switch_ignore_handler
, m
);
1530 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1531 int active
= _active
;
1534 assert(IN_SET(w
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1536 return sd_bus_emit_signal(m
->bus
,
1537 "/org/freedesktop/login1",
1538 "org.freedesktop.login1.Manager",
1539 w
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1544 static int execute_shutdown_or_sleep(
1547 const char *unit_name
,
1548 sd_bus_error
*error
) {
1550 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1556 assert(w
< _INHIBIT_WHAT_MAX
);
1559 if (w
== INHIBIT_SHUTDOWN
)
1560 bus_manager_log_shutdown(m
, unit_name
);
1562 r
= sd_bus_call_method(
1564 "org.freedesktop.systemd1",
1565 "/org/freedesktop/systemd1",
1566 "org.freedesktop.systemd1.Manager",
1570 "ss", unit_name
, "replace-irreversibly");
1574 r
= sd_bus_message_read(reply
, "o", &p
);
1578 r
= free_and_strdup(&m
->action_job
, p
);
1582 m
->action_unit
= unit_name
;
1585 /* Make sure the lid switch is ignored for a while */
1586 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1591 /* Tell people that they now may take a lock again */
1592 (void) send_prepare_for(m
, w
, false);
1597 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1599 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1600 Inhibitor
*offending
= NULL
;
1605 if (manager
->action_what
== 0 || manager
->action_job
)
1608 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1609 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1614 (void) get_process_comm(offending
->pid
, &comm
);
1615 u
= uid_to_name(offending
->uid
);
1617 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1618 offending
->uid
, strna(u
),
1619 offending
->pid
, strna(comm
));
1622 /* Actually do the operation */
1623 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1625 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1626 bus_error_message(&error
, r
));
1628 manager
->action_unit
= NULL
;
1629 manager
->action_what
= 0;
1636 static int manager_inhibit_timeout_handler(
1641 Manager
*manager
= userdata
;
1645 assert(manager
->inhibit_timeout_source
== s
);
1647 r
= manager_dispatch_delayed(manager
, true);
1648 return (r
< 0) ? r
: 0;
1651 static int delay_shutdown_or_sleep(
1654 const char *unit_name
) {
1661 assert(w
< _INHIBIT_WHAT_MAX
);
1664 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1666 if (m
->inhibit_timeout_source
) {
1667 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1669 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1671 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1673 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1675 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1676 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1681 m
->action_unit
= unit_name
;
1687 int bus_manager_shutdown_or_sleep_now_or_later(
1689 const char *unit_name
,
1691 sd_bus_error
*error
) {
1693 _cleanup_free_
char *load_state
= NULL
;
1700 assert(w
< _INHIBIT_WHAT_MAX
);
1701 assert(!m
->action_job
);
1703 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1707 if (!streq(load_state
, "loaded"))
1708 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1709 "Unit %s is %s, refusing operation.",
1710 unit_name
, load_state
);
1712 /* Tell everybody to prepare for shutdown/sleep */
1713 (void) send_prepare_for(m
, w
, true);
1716 m
->inhibit_delay_max
> 0 &&
1717 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1720 /* Shutdown is delayed, keep in mind what we
1721 * want to do, and start a timeout */
1722 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1724 /* Shutdown is not delayed, execute it
1726 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1731 static int verify_shutdown_creds(
1733 sd_bus_message
*message
,
1737 const char *action_multiple_sessions
,
1738 const char *action_ignore_inhibit
,
1739 sd_bus_error
*error
) {
1741 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1742 bool multiple_sessions
, blocked
;
1749 assert(w
<= _INHIBIT_WHAT_MAX
);
1751 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1755 r
= sd_bus_creds_get_euid(creds
, &uid
);
1759 r
= have_multiple_sessions(m
, uid
);
1763 multiple_sessions
= r
> 0;
1764 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1766 if (multiple_sessions
&& action_multiple_sessions
) {
1767 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1771 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1774 if (blocked
&& action_ignore_inhibit
) {
1775 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1779 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1782 if (!multiple_sessions
&& !blocked
&& action
) {
1783 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1787 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1793 static int method_do_shutdown_or_sleep(
1795 sd_bus_message
*message
,
1796 const char *unit_name
,
1799 const char *action_multiple_sessions
,
1800 const char *action_ignore_inhibit
,
1801 const char *sleep_verb
,
1802 sd_bus_error
*error
) {
1810 assert(w
<= _INHIBIT_WHAT_MAX
);
1812 r
= sd_bus_message_read(message
, "b", &interactive
);
1816 /* Don't allow multiple jobs being executed at the same time */
1817 if (m
->action_what
> 0)
1818 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
1819 "There's already a shutdown or sleep operation in progress");
1822 r
= can_sleep(sleep_verb
);
1824 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1825 "Not enough swap space for hibernation");
1827 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1828 "Sleep verb \"%s\" not supported", sleep_verb
);
1833 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1834 action_ignore_inhibit
, error
);
1838 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1842 return sd_bus_reply_method_return(message
, NULL
);
1845 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1846 Manager
*m
= userdata
;
1848 return method_do_shutdown_or_sleep(
1850 SPECIAL_POWEROFF_TARGET
,
1852 "org.freedesktop.login1.power-off",
1853 "org.freedesktop.login1.power-off-multiple-sessions",
1854 "org.freedesktop.login1.power-off-ignore-inhibit",
1859 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1860 Manager
*m
= userdata
;
1862 return method_do_shutdown_or_sleep(
1864 SPECIAL_REBOOT_TARGET
,
1866 "org.freedesktop.login1.reboot",
1867 "org.freedesktop.login1.reboot-multiple-sessions",
1868 "org.freedesktop.login1.reboot-ignore-inhibit",
1873 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1874 Manager
*m
= userdata
;
1876 return method_do_shutdown_or_sleep(
1878 SPECIAL_HALT_TARGET
,
1880 "org.freedesktop.login1.halt",
1881 "org.freedesktop.login1.halt-multiple-sessions",
1882 "org.freedesktop.login1.halt-ignore-inhibit",
1887 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1888 Manager
*m
= userdata
;
1890 return method_do_shutdown_or_sleep(
1892 SPECIAL_SUSPEND_TARGET
,
1894 "org.freedesktop.login1.suspend",
1895 "org.freedesktop.login1.suspend-multiple-sessions",
1896 "org.freedesktop.login1.suspend-ignore-inhibit",
1901 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1902 Manager
*m
= userdata
;
1904 return method_do_shutdown_or_sleep(
1906 SPECIAL_HIBERNATE_TARGET
,
1908 "org.freedesktop.login1.hibernate",
1909 "org.freedesktop.login1.hibernate-multiple-sessions",
1910 "org.freedesktop.login1.hibernate-ignore-inhibit",
1915 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1916 Manager
*m
= userdata
;
1918 return method_do_shutdown_or_sleep(
1920 SPECIAL_HYBRID_SLEEP_TARGET
,
1922 "org.freedesktop.login1.hibernate",
1923 "org.freedesktop.login1.hibernate-multiple-sessions",
1924 "org.freedesktop.login1.hibernate-ignore-inhibit",
1929 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1930 Manager
*m
= userdata
;
1932 return method_do_shutdown_or_sleep(
1934 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
1936 "org.freedesktop.login1.hibernate",
1937 "org.freedesktop.login1.hibernate-multiple-sessions",
1938 "org.freedesktop.login1.hibernate-ignore-inhibit",
1943 static int nologin_timeout_handler(
1948 Manager
*m
= userdata
;
1950 log_info("Creating /run/nologin, blocking further logins...");
1953 create_shutdown_run_nologin_or_warn() >= 0;
1958 static int update_schedule_file(Manager
*m
) {
1959 _cleanup_free_
char *temp_path
= NULL
;
1960 _cleanup_fclose_
FILE *f
= NULL
;
1965 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
1967 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
1969 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
1971 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
1973 (void) fchmod(fileno(f
), 0644);
1979 m
->scheduled_shutdown_timeout
,
1980 m
->enable_wall_messages
,
1981 m
->scheduled_shutdown_type
);
1983 if (!isempty(m
->wall_message
)) {
1984 _cleanup_free_
char *t
;
1986 t
= cescape(m
->wall_message
);
1992 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
1995 r
= fflush_and_check(f
);
1999 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
2007 (void) unlink(temp_path
);
2008 (void) unlink("/run/systemd/shutdown/scheduled");
2010 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2013 static void reset_scheduled_shutdown(Manager
*m
) {
2016 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2017 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
2018 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2020 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
2021 m
->scheduled_shutdown_timeout
= 0;
2022 m
->shutdown_dry_run
= false;
2024 if (m
->unlink_nologin
) {
2025 (void) unlink_or_warn("/run/nologin");
2026 m
->unlink_nologin
= false;
2029 (void) unlink("/run/systemd/shutdown/scheduled");
2032 static int manager_scheduled_shutdown_handler(
2037 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2038 Manager
*m
= userdata
;
2044 if (isempty(m
->scheduled_shutdown_type
))
2047 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2048 target
= SPECIAL_POWEROFF_TARGET
;
2049 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2050 target
= SPECIAL_REBOOT_TARGET
;
2051 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2052 target
= SPECIAL_HALT_TARGET
;
2054 assert_not_reached("unexpected shutdown type");
2056 /* Don't allow multiple jobs being executed at the same time */
2057 if (m
->action_what
> 0) {
2059 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2063 if (m
->shutdown_dry_run
) {
2064 /* We do not process delay inhibitors here. Otherwise, we
2065 * would have to be considered "in progress" (like the check
2066 * above) for some seconds after our admin has seen the final
2069 bus_manager_log_shutdown(m
, target
);
2070 log_info("Running in dry run, suppressing action.");
2071 reset_scheduled_shutdown(m
);
2076 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2078 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2085 reset_scheduled_shutdown(m
);
2089 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2090 Manager
*m
= userdata
;
2091 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2092 const char *action_multiple_sessions
= NULL
;
2093 const char *action_ignore_inhibit
= NULL
;
2094 const char *action
= NULL
;
2098 bool dry_run
= false;
2103 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2107 if (startswith(type
, "dry-")) {
2112 if (streq(type
, "poweroff")) {
2113 action
= "org.freedesktop.login1.power-off";
2114 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2115 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2116 } else if (streq(type
, "reboot")) {
2117 action
= "org.freedesktop.login1.reboot";
2118 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2119 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2120 } else if (streq(type
, "halt")) {
2121 action
= "org.freedesktop.login1.halt";
2122 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2123 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2125 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2127 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2128 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2132 if (m
->scheduled_shutdown_timeout_source
) {
2133 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2135 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2137 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2139 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2141 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2142 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2144 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2147 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2149 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2153 m
->shutdown_dry_run
= dry_run
;
2155 if (m
->nologin_timeout_source
) {
2156 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2158 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2160 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2162 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2164 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2165 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2167 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2170 m
->scheduled_shutdown_timeout
= elapse
;
2172 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2174 const char *tty
= NULL
;
2176 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2177 (void) sd_bus_creds_get_tty(creds
, &tty
);
2179 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2181 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2186 r
= manager_setup_wall_message_timer(m
);
2190 r
= update_schedule_file(m
);
2194 return sd_bus_reply_method_return(message
, NULL
);
2197 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2198 Manager
*m
= userdata
;
2204 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2205 reset_scheduled_shutdown(m
);
2207 if (cancelled
&& m
->enable_wall_messages
) {
2208 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2209 _cleanup_free_
char *username
= NULL
;
2210 const char *tty
= NULL
;
2214 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2216 (void) sd_bus_creds_get_uid(creds
, &uid
);
2217 (void) sd_bus_creds_get_tty(creds
, &tty
);
2220 username
= uid_to_name(uid
);
2221 utmp_wall("The system shutdown has been cancelled",
2222 username
, tty
, logind_wall_tty_filter
, m
);
2225 return sd_bus_reply_method_return(message
, "b", cancelled
);
2228 static int method_can_shutdown_or_sleep(
2230 sd_bus_message
*message
,
2233 const char *action_multiple_sessions
,
2234 const char *action_ignore_inhibit
,
2235 const char *sleep_verb
,
2236 sd_bus_error
*error
) {
2238 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2239 HandleAction handle
;
2240 bool multiple_sessions
, challenge
, blocked
;
2241 const char *result
= NULL
;
2248 assert(w
<= _INHIBIT_WHAT_MAX
);
2250 assert(action_multiple_sessions
);
2251 assert(action_ignore_inhibit
);
2254 r
= can_sleep(sleep_verb
);
2255 if (IN_SET(r
, 0, -ENOSPC
))
2256 return sd_bus_reply_method_return(message
, "s", "na");
2261 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2265 r
= sd_bus_creds_get_euid(creds
, &uid
);
2269 r
= have_multiple_sessions(m
, uid
);
2273 multiple_sessions
= r
> 0;
2274 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2276 handle
= handle_action_from_string(sleep_verb
);
2280 target
= manager_target_for_action(handle
);
2282 _cleanup_free_
char *load_state
= NULL
;
2284 r
= unit_load_state(m
->bus
, target
, &load_state
);
2288 if (!streq(load_state
, "loaded")) {
2295 if (multiple_sessions
) {
2296 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2303 result
= "challenge";
2309 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2316 } else if (challenge
) {
2317 if (!result
|| streq(result
, "yes"))
2318 result
= "challenge";
2323 if (!multiple_sessions
&& !blocked
) {
2324 /* If neither inhibit nor multiple sessions
2325 * apply then just check the normal policy */
2327 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2334 result
= "challenge";
2340 return sd_bus_reply_method_return(message
, "s", result
);
2343 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2344 Manager
*m
= userdata
;
2346 return method_can_shutdown_or_sleep(
2349 "org.freedesktop.login1.power-off",
2350 "org.freedesktop.login1.power-off-multiple-sessions",
2351 "org.freedesktop.login1.power-off-ignore-inhibit",
2356 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2357 Manager
*m
= userdata
;
2359 return method_can_shutdown_or_sleep(
2362 "org.freedesktop.login1.reboot",
2363 "org.freedesktop.login1.reboot-multiple-sessions",
2364 "org.freedesktop.login1.reboot-ignore-inhibit",
2369 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2370 Manager
*m
= userdata
;
2372 return method_can_shutdown_or_sleep(
2375 "org.freedesktop.login1.halt",
2376 "org.freedesktop.login1.halt-multiple-sessions",
2377 "org.freedesktop.login1.halt-ignore-inhibit",
2382 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2383 Manager
*m
= userdata
;
2385 return method_can_shutdown_or_sleep(
2388 "org.freedesktop.login1.suspend",
2389 "org.freedesktop.login1.suspend-multiple-sessions",
2390 "org.freedesktop.login1.suspend-ignore-inhibit",
2395 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2396 Manager
*m
= userdata
;
2398 return method_can_shutdown_or_sleep(
2401 "org.freedesktop.login1.hibernate",
2402 "org.freedesktop.login1.hibernate-multiple-sessions",
2403 "org.freedesktop.login1.hibernate-ignore-inhibit",
2408 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2409 Manager
*m
= userdata
;
2411 return method_can_shutdown_or_sleep(
2414 "org.freedesktop.login1.hibernate",
2415 "org.freedesktop.login1.hibernate-multiple-sessions",
2416 "org.freedesktop.login1.hibernate-ignore-inhibit",
2421 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2422 Manager
*m
= userdata
;
2424 return method_can_shutdown_or_sleep(
2427 "org.freedesktop.login1.hibernate",
2428 "org.freedesktop.login1.hibernate-multiple-sessions",
2429 "org.freedesktop.login1.hibernate-ignore-inhibit",
2430 "suspend-then-hibernate",
2434 static int property_get_reboot_parameter(
2437 const char *interface
,
2438 const char *property
,
2439 sd_bus_message
*reply
,
2441 sd_bus_error
*error
) {
2442 _cleanup_free_
char *parameter
= NULL
;
2449 r
= read_reboot_parameter(¶meter
);
2453 return sd_bus_message_append(reply
, "s", parameter
);
2456 static int method_set_reboot_parameter(
2457 sd_bus_message
*message
,
2459 sd_bus_error
*error
) {
2461 Manager
*m
= userdata
;
2468 r
= sd_bus_message_read(message
, "s", &arg
);
2472 r
= detect_container();
2476 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
2477 "Reboot parameter not supported in containers, refusing.");
2479 r
= bus_verify_polkit_async(message
,
2481 "org.freedesktop.login1.set-reboot-parameter",
2485 &m
->polkit_registry
,
2490 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2492 r
= update_reboot_parameter_and_warn(arg
, false);
2496 return sd_bus_reply_method_return(message
, NULL
);
2499 static int method_can_reboot_parameter(
2500 sd_bus_message
*message
,
2502 sd_bus_error
*error
) {
2504 Manager
*m
= userdata
;
2510 r
= detect_container();
2513 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
2514 return sd_bus_reply_method_return(message
, "s", "na");
2516 return return_test_polkit(
2519 "org.freedesktop.login1.set-reboot-parameter",
2525 static int property_get_reboot_to_firmware_setup(
2528 const char *interface
,
2529 const char *property
,
2530 sd_bus_message
*reply
,
2532 sd_bus_error
*error
) {
2539 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2541 /* EFI case: let's see what is currently configured in the EFI variables */
2542 r
= efi_get_reboot_to_firmware();
2543 if (r
< 0 && r
!= -EOPNOTSUPP
)
2544 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
2546 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2548 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
2549 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
2550 if (errno
!= ENOENT
)
2551 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
2558 return sd_bus_message_append(reply
, "b", r
> 0);
2561 static int method_set_reboot_to_firmware_setup(
2562 sd_bus_message
*message
,
2564 sd_bus_error
*error
) {
2566 Manager
*m
= userdata
;
2573 r
= sd_bus_message_read(message
, "b", &b
);
2577 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2579 /* EFI case: let's see what the firmware supports */
2581 r
= efi_reboot_to_firmware_supported();
2582 if (r
== -EOPNOTSUPP
)
2583 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2589 } else if (r
<= 0) {
2590 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
2593 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2595 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
2597 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
2600 r
= bus_verify_polkit_async(message
,
2602 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2606 &m
->polkit_registry
,
2611 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2614 r
= efi_set_reboot_to_firmware(b
);
2619 r
= touch("/run/systemd/reboot-to-firmware-setup");
2623 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
2628 return sd_bus_reply_method_return(message
, NULL
);
2631 static int method_can_reboot_to_firmware_setup(
2632 sd_bus_message
*message
,
2634 sd_bus_error
*error
) {
2636 Manager
*m
= userdata
;
2642 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
2644 /* EFI case: let's see what the firmware supports */
2646 r
= efi_reboot_to_firmware_supported();
2648 if (r
!= -EOPNOTSUPP
)
2649 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2651 return sd_bus_reply_method_return(message
, "s", "na");
2654 } else if (r
<= 0) {
2655 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
2658 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
2660 return sd_bus_reply_method_return(message
, "s", "na");
2663 return return_test_polkit(
2666 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2672 static int property_get_reboot_to_boot_loader_menu(
2675 const char *interface
,
2676 const char *property
,
2677 sd_bus_message
*reply
,
2679 sd_bus_error
*error
) {
2681 uint64_t x
= UINT64_MAX
;
2688 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2690 _cleanup_free_
char *v
= NULL
;
2692 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished:
2694 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
2695 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
2696 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
2699 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", &v
);
2702 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable: %m");
2706 r
= safe_atou64(v
, &sec
);
2708 log_warning_errno(r
, "Failed to parse LoaderConfigTimeoutOneShot value '%s': %m", v
);
2709 else if (sec
> (USEC_INFINITY
/ USEC_PER_SEC
))
2710 log_warning("LoaderConfigTimeoutOneShot too large, ignoring: %m");
2712 x
= sec
* USEC_PER_SEC
; /* return in µs */
2716 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2718 _cleanup_free_
char *v
= NULL
;
2720 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
2722 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
2725 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
2727 r
= safe_atou64(v
, &x
);
2729 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
2733 return sd_bus_message_append(reply
, "t", x
);
2736 static int method_set_reboot_to_boot_loader_menu(
2737 sd_bus_message
*message
,
2739 sd_bus_error
*error
) {
2741 Manager
*m
= userdata
;
2749 r
= sd_bus_message_read(message
, "t", &x
);
2753 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2757 /* EFI case: let's see if booting into boot loader menu is supported. */
2759 r
= efi_loader_get_features(&features
);
2761 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2762 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2763 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2767 } else if (r
<= 0) {
2768 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
2771 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2773 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
2775 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
2778 r
= bus_verify_polkit_async(message
,
2780 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2784 &m
->polkit_registry
,
2789 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2792 if (x
== UINT64_MAX
)
2793 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", NULL
, 0);
2795 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2796 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
2798 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderConfigTimeoutOneShot", buf
);
2803 if (x
== UINT64_MAX
) {
2804 if (unlink("/run/systemd/reboot-to-loader-menu") < 0 && errno
!= ENOENT
)
2807 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
2809 xsprintf(buf
, "%" PRIu64
, x
); /* µs granularity */
2811 r
= write_string_file_atomic_label("/run/systemd/reboot-to-loader-menu", buf
);
2817 return sd_bus_reply_method_return(message
, NULL
);
2820 static int method_can_reboot_to_boot_loader_menu(
2821 sd_bus_message
*message
,
2823 sd_bus_error
*error
) {
2825 Manager
*m
= userdata
;
2831 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
2833 uint64_t features
= 0;
2835 /* EFI case, let's see if booting into boot loader menu is supported. */
2837 r
= efi_loader_get_features(&features
);
2839 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
2840 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
2841 return sd_bus_reply_method_return(message
, "s", "na");
2843 } else if (r
<= 0) {
2844 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
2847 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
2849 return sd_bus_reply_method_return(message
, "s", "na");
2852 return return_test_polkit(
2855 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
2861 static int property_get_reboot_to_boot_loader_entry(
2864 const char *interface
,
2865 const char *property
,
2866 sd_bus_message
*reply
,
2868 sd_bus_error
*error
) {
2870 _cleanup_free_
char *v
= NULL
;
2877 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2879 /* EFI case: let's read the LoaderEntryOneShot variable */
2881 r
= efi_get_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", &v
);
2884 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable: %m");
2885 } else if (!efi_loader_entry_name_valid(v
)) {
2886 log_warning("LoaderEntryOneShot contains invalid entry name '%s', ignoring.", v
);
2890 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2893 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
2895 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
2898 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry: %m");
2899 } else if (!efi_loader_entry_name_valid(v
)) {
2900 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
2905 return sd_bus_message_append(reply
, "s", v
);
2908 static int boot_loader_entry_exists(const char *id
) {
2909 _cleanup_(boot_config_free
) BootConfig config
= {};
2914 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
2915 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
2918 (void) boot_entries_augment_from_loader(&config
, true);
2920 return boot_config_has_entry(&config
, id
);
2923 static int method_set_reboot_to_boot_loader_entry(
2924 sd_bus_message
*message
,
2926 sd_bus_error
*error
) {
2928 Manager
*m
= userdata
;
2936 r
= sd_bus_message_read(message
, "s", &v
);
2942 else if (efi_loader_entry_name_valid(v
)) {
2943 r
= boot_loader_entry_exists(v
);
2947 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
2949 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
2951 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
2955 /* EFI case: let's see if booting into boot loader entry is supported. */
2957 r
= efi_loader_get_features(&features
);
2959 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
2960 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
2961 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
2965 } else if (r
<= 0) {
2966 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
2969 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
2971 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
2973 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
2976 r
= bus_verify_polkit_async(message
,
2978 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
2982 &m
->polkit_registry
,
2987 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2992 r
= efi_set_variable(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", NULL
, 0);
2994 r
= efi_set_variable_string(EFI_VENDOR_LOADER
, "LoaderEntryOneShot", v
);
2999 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3002 r
= write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v
);
3008 return sd_bus_reply_method_return(message
, NULL
);
3011 static int method_can_reboot_to_boot_loader_entry(
3012 sd_bus_message
*message
,
3014 sd_bus_error
*error
) {
3016 Manager
*m
= userdata
;
3022 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3024 uint64_t features
= 0;
3026 /* EFI case, let's see if booting into boot loader entry is supported. */
3028 r
= efi_loader_get_features(&features
);
3030 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3031 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3032 return sd_bus_reply_method_return(message
, "s", "na");
3034 } else if (r
<= 0) {
3035 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3038 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3040 return sd_bus_reply_method_return(message
, "s", "na");
3043 return return_test_polkit(
3046 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3052 static int property_get_boot_loader_entries(
3055 const char *interface
,
3056 const char *property
,
3057 sd_bus_message
*reply
,
3059 sd_bus_error
*error
) {
3061 _cleanup_(boot_config_free
) BootConfig config
= {};
3069 r
= boot_entries_load_config_auto(NULL
, NULL
, &config
);
3070 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3073 (void) boot_entries_augment_from_loader(&config
, true);
3075 r
= sd_bus_message_open_container(reply
, 'a', "s");
3079 for (i
= 0; i
< config
.n_entries
; i
++) {
3080 BootEntry
*e
= config
.entries
+ i
;
3082 r
= sd_bus_message_append(reply
, "s", e
->id
);
3087 return sd_bus_message_close_container(reply
);
3090 static int method_set_wall_message(
3091 sd_bus_message
*message
,
3093 sd_bus_error
*error
) {
3096 Manager
*m
= userdata
;
3098 unsigned enable_wall_messages
;
3103 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3107 r
= bus_verify_polkit_async(message
,
3109 "org.freedesktop.login1.set-wall-message",
3113 &m
->polkit_registry
,
3118 return 1; /* Will call us back */
3120 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3124 m
->enable_wall_messages
= enable_wall_messages
;
3126 return sd_bus_reply_method_return(message
, NULL
);
3129 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3130 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3131 const char *who
, *why
, *what
, *mode
;
3132 _cleanup_free_
char *id
= NULL
;
3133 _cleanup_close_
int fifo_fd
= -1;
3134 Manager
*m
= userdata
;
3135 Inhibitor
*i
= NULL
;
3145 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3149 w
= inhibit_what_from_string(what
);
3151 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3152 "Invalid what specification %s", what
);
3154 mm
= inhibit_mode_from_string(mode
);
3156 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3157 "Invalid mode specification %s", mode
);
3159 /* Delay is only supported for shutdown/sleep */
3160 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3161 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3162 "Delay inhibitors only supported for shutdown and sleep");
3164 /* Don't allow taking delay locks while we are already
3165 * executing the operation. We shouldn't create the impression
3166 * that the lock was successful if the machine is about to go
3167 * down/suspend any moment. */
3168 if (m
->action_what
& w
)
3169 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3170 "The operation inhibition has been requested for is already running");
3172 r
= bus_verify_polkit_async(
3175 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3176 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3177 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3178 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3179 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3180 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3181 "org.freedesktop.login1.inhibit-handle-lid-switch",
3185 &m
->polkit_registry
,
3190 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3192 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
3196 r
= sd_bus_creds_get_euid(creds
, &uid
);
3200 r
= sd_bus_creds_get_pid(creds
, &pid
);
3204 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3205 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3206 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3212 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
3215 } while (hashmap_get(m
->inhibitors
, id
));
3217 r
= manager_add_inhibitor(m
, id
, &i
);
3225 i
->why
= strdup(why
);
3226 i
->who
= strdup(who
);
3228 if (!i
->why
|| !i
->who
) {
3233 fifo_fd
= inhibitor_create_fifo(i
);
3241 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3250 const sd_bus_vtable manager_vtable
[] = {
3251 SD_BUS_VTABLE_START(0),
3253 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
3254 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3256 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3257 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3258 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3259 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3260 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3261 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3262 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3263 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3264 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3265 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3266 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3267 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3268 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3269 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3270 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3271 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3272 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3273 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3274 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3275 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3276 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3277 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3278 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3279 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3280 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3281 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3282 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3283 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
3284 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3285 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
3286 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3287 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3288 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3289 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3290 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3291 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3292 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3293 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3295 SD_BUS_METHOD("GetSession", "s", "o", method_get_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3296 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
3297 SD_BUS_METHOD("GetUser", "u", "o", method_get_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3298 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
3299 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3300 SD_BUS_METHOD("ListSessions", NULL
, "a(susso)", method_list_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3301 SD_BUS_METHOD("ListUsers", NULL
, "a(uso)", method_list_users
, SD_BUS_VTABLE_UNPRIVILEGED
),
3302 SD_BUS_METHOD("ListSeats", NULL
, "a(so)", method_list_seats
, SD_BUS_VTABLE_UNPRIVILEGED
),
3303 SD_BUS_METHOD("ListInhibitors", NULL
, "a(ssssuu)", method_list_inhibitors
, SD_BUS_VTABLE_UNPRIVILEGED
),
3304 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session
, 0),
3305 SD_BUS_METHOD("ReleaseSession", "s", NULL
, method_release_session
, 0),
3306 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3307 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL
, method_activate_session_on_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3308 SD_BUS_METHOD("LockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3309 SD_BUS_METHOD("UnlockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3310 SD_BUS_METHOD("LockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3311 SD_BUS_METHOD("UnlockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
3312 SD_BUS_METHOD("KillSession", "ssi", NULL
, method_kill_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3313 SD_BUS_METHOD("KillUser", "ui", NULL
, method_kill_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3314 SD_BUS_METHOD("TerminateSession", "s", NULL
, method_terminate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
3315 SD_BUS_METHOD("TerminateUser", "u", NULL
, method_terminate_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
3316 SD_BUS_METHOD("TerminateSeat", "s", NULL
, method_terminate_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
3317 SD_BUS_METHOD("SetUserLinger", "ubb", NULL
, method_set_user_linger
, SD_BUS_VTABLE_UNPRIVILEGED
),
3318 SD_BUS_METHOD("AttachDevice", "ssb", NULL
, method_attach_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
3319 SD_BUS_METHOD("FlushDevices", "b", NULL
, method_flush_devices
, SD_BUS_VTABLE_UNPRIVILEGED
),
3320 SD_BUS_METHOD("PowerOff", "b", NULL
, method_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
3321 SD_BUS_METHOD("Reboot", "b", NULL
, method_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
3322 SD_BUS_METHOD("Halt", "b", NULL
, method_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
3323 SD_BUS_METHOD("Suspend", "b", NULL
, method_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
3324 SD_BUS_METHOD("Hibernate", "b", NULL
, method_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3325 SD_BUS_METHOD("HybridSleep", "b", NULL
, method_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
3326 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL
, method_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3327 SD_BUS_METHOD("CanPowerOff", NULL
, "s", method_can_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
3328 SD_BUS_METHOD("CanReboot", NULL
, "s", method_can_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
3329 SD_BUS_METHOD("CanHalt", NULL
, "s", method_can_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
3330 SD_BUS_METHOD("CanSuspend", NULL
, "s", method_can_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
3331 SD_BUS_METHOD("CanHibernate", NULL
, "s", method_can_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3332 SD_BUS_METHOD("CanHybridSleep", NULL
, "s", method_can_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
3333 SD_BUS_METHOD("CanSuspendThenHibernate", NULL
, "s", method_can_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
3334 SD_BUS_METHOD("ScheduleShutdown", "st", NULL
, method_schedule_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
3335 SD_BUS_METHOD("CancelScheduledShutdown", NULL
, "b", method_cancel_scheduled_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
3336 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit
, SD_BUS_VTABLE_UNPRIVILEGED
),
3337 SD_BUS_METHOD("CanRebootParameter", NULL
, "s", method_can_reboot_parameter
, SD_BUS_VTABLE_UNPRIVILEGED
),
3338 SD_BUS_METHOD("SetRebootParameter", "s", NULL
, method_set_reboot_parameter
, SD_BUS_VTABLE_UNPRIVILEGED
),
3339 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL
, "s", method_can_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
3340 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL
, method_set_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
3341 SD_BUS_METHOD("CanRebootToBootLoaderMenu", NULL
, "s", method_can_reboot_to_boot_loader_menu
, SD_BUS_VTABLE_UNPRIVILEGED
),
3342 SD_BUS_METHOD("SetRebootToBootLoaderMenu", "t", NULL
, method_set_reboot_to_boot_loader_menu
, SD_BUS_VTABLE_UNPRIVILEGED
),
3343 SD_BUS_METHOD("CanRebootToBootLoaderEntry", NULL
, "s", method_can_reboot_to_boot_loader_entry
, SD_BUS_VTABLE_UNPRIVILEGED
),
3344 SD_BUS_METHOD("SetRebootToBootLoaderEntry", "s", NULL
, method_set_reboot_to_boot_loader_entry
, SD_BUS_VTABLE_UNPRIVILEGED
),
3345 SD_BUS_METHOD("SetWallMessage", "sb", NULL
, method_set_wall_message
, SD_BUS_VTABLE_UNPRIVILEGED
),
3347 SD_BUS_SIGNAL("SessionNew", "so", 0),
3348 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
3349 SD_BUS_SIGNAL("UserNew", "uo", 0),
3350 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
3351 SD_BUS_SIGNAL("SeatNew", "so", 0),
3352 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
3353 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
3354 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
3359 static int session_jobs_reply(Session
*s
, const char *unit
, const char *result
) {
3366 if (result
&& !streq(result
, "done")) {
3367 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
3369 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
3370 "Start job for unit '%s' failed with '%s'", unit
, result
);
3371 return session_send_create_reply(s
, &e
);
3374 return session_send_create_reply(s
, NULL
);
3377 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3378 const char *path
, *result
, *unit
;
3379 Manager
*m
= userdata
;
3388 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
3390 bus_log_parse_error(r
);
3394 if (m
->action_job
&& streq(m
->action_job
, path
)) {
3395 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
3397 /* Tell people that they now may take a lock again */
3398 (void) send_prepare_for(m
, m
->action_what
, false);
3400 m
->action_job
= mfree(m
->action_job
);
3401 m
->action_unit
= NULL
;
3406 session
= hashmap_get(m
->session_units
, unit
);
3408 if (streq_ptr(path
, session
->scope_job
)) {
3409 session
->scope_job
= mfree(session
->scope_job
);
3410 (void) session_jobs_reply(session
, unit
, result
);
3412 session_save(session
);
3413 user_save(session
->user
);
3416 session_add_to_gc_queue(session
);
3419 user
= hashmap_get(m
->user_units
, unit
);
3421 if (streq_ptr(path
, user
->service_job
)) {
3422 user
->service_job
= mfree(user
->service_job
);
3424 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
3425 (void) session_jobs_reply(session
, unit
, NULL
/* don't propagate user service failures to the client */);
3430 user_add_to_gc_queue(user
);
3436 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3437 const char *path
, *unit
;
3438 Manager
*m
= userdata
;
3446 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
3448 bus_log_parse_error(r
);
3452 session
= hashmap_get(m
->session_units
, unit
);
3454 session_add_to_gc_queue(session
);
3456 user
= hashmap_get(m
->user_units
, unit
);
3458 user_add_to_gc_queue(user
);
3463 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3464 _cleanup_free_
char *unit
= NULL
;
3465 Manager
*m
= userdata
;
3474 path
= sd_bus_message_get_path(message
);
3478 r
= unit_name_from_dbus_path(path
, &unit
);
3479 if (r
== -EINVAL
) /* not a unit */
3486 session
= hashmap_get(m
->session_units
, unit
);
3488 session_add_to_gc_queue(session
);
3490 user
= hashmap_get(m
->user_units
, unit
);
3492 user_add_to_gc_queue(user
);
3497 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3498 Manager
*m
= userdata
;
3506 r
= sd_bus_message_read(message
, "b", &b
);
3508 bus_log_parse_error(r
);
3515 /* systemd finished reloading, let's recheck all our sessions */
3516 log_debug("System manager has been reloaded, rechecking sessions...");
3518 HASHMAP_FOREACH(session
, m
->sessions
, i
)
3519 session_add_to_gc_queue(session
);
3524 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
3529 l
= strv_from_stdarg_alloca(property
);
3531 return sd_bus_emit_properties_changed_strv(
3533 "/org/freedesktop/login1",
3534 "org.freedesktop.login1.Manager",
3538 static int strdup_job(sd_bus_message
*reply
, char **job
) {
3543 r
= sd_bus_message_read(reply
, "o", &j
);
3555 int manager_start_scope(
3560 const char *description
,
3563 const char *requires_mounts_for
,
3564 sd_bus_message
*more_properties
,
3565 sd_bus_error
*error
,
3568 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
3577 r
= sd_bus_message_new_method_call(
3580 "org.freedesktop.systemd1",
3581 "/org/freedesktop/systemd1",
3582 "org.freedesktop.systemd1.Manager",
3583 "StartTransientUnit");
3587 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
3591 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
3595 if (!isempty(slice
)) {
3596 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
3601 if (!isempty(description
)) {
3602 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
3607 STRV_FOREACH(i
, wants
) {
3608 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
3613 STRV_FOREACH(i
, after
) {
3614 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3619 if (!empty_or_root(requires_mounts_for
)) {
3620 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3625 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3627 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3631 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3635 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3636 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
3638 return bus_log_create_error(r
);
3640 if (more_properties
) {
3641 /* If TasksMax also appears here, it will overwrite the default value set above */
3642 r
= sd_bus_message_copy(m
, more_properties
, true);
3647 r
= sd_bus_message_close_container(m
);
3651 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3655 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3659 return strdup_job(reply
, job
);
3662 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3663 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3670 r
= sd_bus_call_method(
3672 "org.freedesktop.systemd1",
3673 "/org/freedesktop/systemd1",
3674 "org.freedesktop.systemd1.Manager",
3678 "ss", unit
, "replace");
3682 return strdup_job(reply
, job
);
3685 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3686 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3693 r
= sd_bus_call_method(
3695 "org.freedesktop.systemd1",
3696 "/org/freedesktop/systemd1",
3697 "org.freedesktop.systemd1.Manager",
3701 "ss", unit
, "fail");
3703 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3704 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
3707 sd_bus_error_free(error
);
3714 return strdup_job(reply
, job
);
3717 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
3718 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3719 _cleanup_free_
char *path
= NULL
;
3725 path
= unit_dbus_path_from_name(scope
);
3729 r
= sd_bus_call_method(
3731 "org.freedesktop.systemd1",
3733 "org.freedesktop.systemd1.Scope",
3739 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3740 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
3741 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
3744 sd_bus_error_move(ret_error
, &error
);
3751 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
3755 return sd_bus_call_method(
3757 "org.freedesktop.systemd1",
3758 "/org/freedesktop/systemd1",
3759 "org.freedesktop.systemd1.Manager",
3763 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
3766 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
3767 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3768 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3769 _cleanup_free_
char *path
= NULL
;
3776 path
= unit_dbus_path_from_name(unit
);
3780 r
= sd_bus_get_property(
3782 "org.freedesktop.systemd1",
3784 "org.freedesktop.systemd1.Unit",
3790 /* systemd might have dropped off momentarily, let's
3791 * not make this an error */
3792 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3793 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3796 /* If the unit is already unloaded then it's not
3798 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3799 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
3802 sd_bus_error_move(ret_error
, &error
);
3806 r
= sd_bus_message_read(reply
, "s", &state
);
3810 return !STR_IN_SET(state
, "inactive", "failed");
3813 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
3814 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3815 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3821 r
= sd_bus_get_property(
3823 "org.freedesktop.systemd1",
3825 "org.freedesktop.systemd1.Job",
3831 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3832 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3835 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
3838 sd_bus_error_move(ret_error
, &error
);
3842 /* We don't actually care about the state really. The fact
3843 * that we could read the job state is enough for us */