1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "sd-messages.h"
11 #include "alloc-util.h"
12 #include "audit-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-error.h"
17 #include "bus-get-properties.h"
18 #include "bus-locator.h"
19 #include "bus-object.h"
20 #include "bus-polkit.h"
21 #include "bus-unit-util.h"
23 #include "cgroup-util.h"
24 #include "device-util.h"
25 #include "dirent-util.h"
27 #include "efi-loader.h"
31 #include "errno-util.h"
33 #include "event-util.h"
36 #include "format-util.h"
39 #include "login-util.h"
40 #include "logind-session.h"
42 #include "logind-action.h"
43 #include "logind-dbus.h"
44 #include "logind-polkit.h"
45 #include "logind-seat.h"
46 #include "logind-seat-dbus.h"
47 #include "logind-session-dbus.h"
48 #include "logind-user.h"
49 #include "logind-user-dbus.h"
50 #include "logind-utmp.h"
51 #include "mkdir-label.h"
53 #include "parse-util.h"
54 #include "path-util.h"
55 #include "process-util.h"
56 #include "reboot-util.h"
57 #include "serialize.h"
58 #include "signal-util.h"
59 #include "sleep-config.h"
60 #include "stdio-util.h"
62 #include "terminal-util.h"
63 #include "tmpfile-util.h"
65 #include "user-record.h"
66 #include "user-util.h"
70 /* As a random fun fact sysvinit had a 252 (256-(strlen(" \r\n")+1))
71 * character limit for the wall message.
72 * https://git.savannah.nongnu.org/cgit/sysvinit.git/tree/src/shutdown.c#n72
73 * There is no real technical need for that but doesn't make sense
74 * to store arbitrary amounts either. As we are not stingy here, we
77 #define WALL_MESSAGE_MAX 4096U
79 #define SHUTDOWN_SCHEDULE_FILE "/run/systemd/shutdown/scheduled"
81 static void reset_scheduled_shutdown(Manager
*m
);
83 static int get_sender_session(
85 sd_bus_message
*message
,
90 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
91 Session
*session
= NULL
;
97 /* Acquire the sender's session. This first checks if the sending process is inside a session itself,
98 * and returns that. If not and 'consult_display' is true, this returns the display session of the
99 * owning user of the caller. */
101 r
= sd_bus_query_sender_creds(message
,
102 SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
|
103 (consult_display
? SD_BUS_CREDS_OWNER_UID
: 0), &creds
);
107 r
= sd_bus_creds_get_session(creds
, &name
);
112 if (consult_display
) {
115 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
122 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
124 session
= user
->display
;
128 session
= hashmap_get(m
->sessions
, name
);
131 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
133 "Caller does not belong to any known session and doesn't own any suitable session." :
134 "Caller does not belong to any known session.");
140 int manager_get_session_from_creds(
142 sd_bus_message
*message
,
152 if (session_is_self(name
)) /* the caller's own session */
153 return get_sender_session(m
, message
, false, error
, ret
);
154 if (session_is_auto(name
)) /* The caller's own session if they have one, otherwise their user's display session */
155 return get_sender_session(m
, message
, true, error
, ret
);
157 session
= hashmap_get(m
->sessions
, name
);
159 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
165 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
166 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
171 /* Note that we get the owner UID of the session, not the actual client UID here! */
172 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
176 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
183 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
186 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
187 "Caller does not belong to any logged in or lingering user");
193 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
199 if (!uid_is_valid(uid
))
200 return get_sender_user(m
, message
, error
, ret
);
202 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
204 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
,
205 "User ID "UID_FMT
" is not logged in or lingering", uid
);
211 int manager_get_seat_from_creds(
213 sd_bus_message
*message
,
224 if (seat_is_self(name
) || seat_is_auto(name
)) {
227 /* Use these special seat names as session names */
228 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
232 seat
= session
->seat
;
234 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session '%s' has no seat.", session
->id
);
236 seat
= hashmap_get(m
->seats
, name
);
238 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
245 static int return_test_polkit(
246 sd_bus_message
*message
,
248 const char **details
,
256 r
= bus_test_polkit(message
, action
, details
, good_user
, &challenge
, e
);
263 result
= "challenge";
267 return sd_bus_reply_method_return(message
, "s", result
);
270 static int property_get_idle_hint(
273 const char *interface
,
274 const char *property
,
275 sd_bus_message
*reply
,
277 sd_bus_error
*error
) {
279 Manager
*m
= ASSERT_PTR(userdata
);
284 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
287 static int property_get_idle_since_hint(
290 const char *interface
,
291 const char *property
,
292 sd_bus_message
*reply
,
294 sd_bus_error
*error
) {
296 Manager
*m
= ASSERT_PTR(userdata
);
297 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
302 manager_get_idle_hint(m
, &t
);
304 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
307 static int property_get_inhibited(
310 const char *interface
,
311 const char *property
,
312 sd_bus_message
*reply
,
314 sd_bus_error
*error
) {
316 Manager
*m
= ASSERT_PTR(userdata
);
322 if (streq(property
, "BlockInhibited"))
323 w
= manager_inhibit_what(m
, INHIBIT_BLOCK
);
324 else if (streq(property
, "BlockWeakInhibited"))
325 w
= manager_inhibit_what(m
, INHIBIT_BLOCK_WEAK
);
326 else if (streq(property
, "DelayInhibited"))
327 w
= manager_inhibit_what(m
, INHIBIT_DELAY
);
329 assert_not_reached();
331 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
334 static int property_get_preparing(
337 const char *interface
,
338 const char *property
,
339 sd_bus_message
*reply
,
341 sd_bus_error
*error
) {
343 Manager
*m
= ASSERT_PTR(userdata
);
349 if (m
->delayed_action
) {
350 if (streq(property
, "PreparingForShutdown"))
351 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SHUTDOWN
;
353 b
= m
->delayed_action
->inhibit_what
& INHIBIT_SLEEP
;
356 return sd_bus_message_append(reply
, "b", b
);
359 static int property_get_preparing_shutdown_with_metadata(
362 const char *interface
,
363 const char *property
,
364 sd_bus_message
*reply
,
366 sd_bus_error
*error
) {
368 Manager
*m
= ASSERT_PTR(userdata
);
373 if (!m
->delayed_action
|| !(m
->delayed_action
->inhibit_what
& INHIBIT_SHUTDOWN
))
374 return sd_bus_message_append(reply
, "a{sv}", 1, "preparing", "b", false);
376 return sd_bus_message_append(
385 handle_action_to_string(m
->delayed_action
->handle
));
388 static int property_get_sleep_operations(
391 const char *interface
,
392 const char *property
,
393 sd_bus_message
*reply
,
395 sd_bus_error
*error
) {
397 Manager
*m
= ASSERT_PTR(userdata
);
398 _cleanup_strv_free_
char **actions
= NULL
;
404 r
= handle_action_get_enabled_sleep_actions(m
->handle_action_sleep_mask
, &actions
);
408 return sd_bus_message_append_strv(reply
, actions
);
411 static int property_get_scheduled_shutdown(
414 const char *interface
,
415 const char *property
,
416 sd_bus_message
*reply
,
418 sd_bus_error
*error
) {
420 Manager
*m
= ASSERT_PTR(userdata
);
426 r
= sd_bus_message_open_container(reply
, 'r', "st");
430 if (m
->delayed_action
) {
431 usec_t t
= m
->scheduled_shutdown_timeout
; /* fall back to the schedule time on failure below */
433 if (m
->inhibit_timeout_source
) {
434 r
= sd_event_source_get_time(m
->inhibit_timeout_source
, &t
);
436 log_debug_errno(r
, "Failed to get time of inhibit timeout event source, ignoring: %m");
439 r
= sd_bus_message_append(
441 handle_action_to_string(m
->delayed_action
->handle
), t
);
443 r
= sd_bus_message_append(
445 handle_action_to_string(m
->scheduled_shutdown_action
),
446 m
->scheduled_shutdown_timeout
);
450 return sd_bus_message_close_container(reply
);
453 static int property_get_maintenance_time(
456 const char *interface
,
457 const char *property
,
458 sd_bus_message
*reply
,
460 sd_bus_error
*error
) {
462 Manager
*m
= ASSERT_PTR(userdata
);
463 _cleanup_free_
char *s
= NULL
;
469 if (m
->maintenance_time
) {
470 r
= calendar_spec_to_string(m
->maintenance_time
, &s
);
472 return log_error_errno(r
, "Failed to format calendar specification: %m");
475 return sd_bus_message_append(reply
, "s", s
);
478 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
479 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
480 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
481 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power());
482 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
483 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
485 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
486 _cleanup_free_
char *p
= NULL
;
487 Manager
*m
= ASSERT_PTR(userdata
);
494 r
= sd_bus_message_read(message
, "s", &name
);
498 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
502 p
= session_bus_path(session
);
506 return sd_bus_reply_method_return(message
, "o", p
);
509 /* Get login session of a process. This is not what you are looking for these days,
510 * as apps may instead belong to a user service unit. This includes terminal
511 * emulators and hence command-line apps. */
512 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
513 Manager
*m
= ASSERT_PTR(userdata
);
514 _cleanup_free_
char *p
= NULL
;
515 Session
*session
= NULL
;
521 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
523 r
= sd_bus_message_read(message
, "u", &pid
);
530 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
534 r
= manager_get_session_by_pidref(m
, &PIDREF_MAKE_FROM_PID(pid
), &session
);
539 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
540 "PID "PID_FMT
" does not belong to any known session", pid
);
543 p
= session_bus_path(session
);
547 return sd_bus_reply_method_return(message
, "o", p
);
550 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
551 _cleanup_free_
char *p
= NULL
;
552 Manager
*m
= ASSERT_PTR(userdata
);
559 r
= sd_bus_message_read(message
, "u", &uid
);
563 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
567 p
= user_bus_path(user
);
571 return sd_bus_reply_method_return(message
, "o", p
);
574 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
575 _cleanup_free_
char *p
= NULL
;
576 Manager
*m
= ASSERT_PTR(userdata
);
583 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
585 r
= sd_bus_message_read(message
, "u", &pid
);
592 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
596 r
= manager_get_user_by_pid(m
, pid
, &user
);
600 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
601 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
605 p
= user_bus_path(user
);
609 return sd_bus_reply_method_return(message
, "o", p
);
612 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
613 _cleanup_free_
char *p
= NULL
;
614 Manager
*m
= ASSERT_PTR(userdata
);
621 r
= sd_bus_message_read(message
, "s", &name
);
625 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
629 p
= seat_bus_path(seat
);
633 return sd_bus_reply_method_return(message
, "o", p
);
636 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
637 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
638 Manager
*m
= ASSERT_PTR(userdata
);
644 r
= sd_bus_message_new_method_return(message
, &reply
);
648 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
652 HASHMAP_FOREACH(session
, m
->sessions
) {
653 _cleanup_free_
char *p
= NULL
;
655 p
= session_bus_path(session
);
659 r
= sd_bus_message_append(reply
, "(susso)",
661 (uint32_t) session
->user
->user_record
->uid
,
662 session
->user
->user_record
->user_name
,
663 session
->seat
? session
->seat
->id
: "",
669 r
= sd_bus_message_close_container(reply
);
673 return sd_bus_message_send(reply
);
676 static int method_list_sessions_ex(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
677 Manager
*m
= ASSERT_PTR(userdata
);
678 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
683 r
= sd_bus_message_new_method_return(message
, &reply
);
687 r
= sd_bus_message_open_container(reply
, 'a', "(sussussbto)");
692 HASHMAP_FOREACH(s
, m
->sessions
) {
693 _cleanup_free_
char *path
= NULL
;
694 dual_timestamp idle_ts
;
699 path
= session_bus_path(s
);
703 r
= session_get_idle_hint(s
, &idle_ts
);
708 r
= sd_bus_message_append(reply
, "(sussussbto)",
710 (uint32_t) s
->user
->user_record
->uid
,
711 s
->user
->user_record
->user_name
,
712 s
->seat
? s
->seat
->id
: "",
713 (uint32_t) s
->leader
.pid
,
714 session_class_to_string(s
->class),
723 r
= sd_bus_message_close_container(reply
);
727 return sd_bus_message_send(reply
);
730 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
731 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
732 Manager
*m
= ASSERT_PTR(userdata
);
738 r
= sd_bus_message_new_method_return(message
, &reply
);
742 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
746 HASHMAP_FOREACH(user
, m
->users
) {
747 _cleanup_free_
char *p
= NULL
;
749 p
= user_bus_path(user
);
753 r
= sd_bus_message_append(reply
, "(uso)",
754 (uint32_t) user
->user_record
->uid
,
755 user
->user_record
->user_name
,
761 r
= sd_bus_message_close_container(reply
);
765 return sd_bus_message_send(reply
);
768 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
769 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
770 Manager
*m
= ASSERT_PTR(userdata
);
776 r
= sd_bus_message_new_method_return(message
, &reply
);
780 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
784 HASHMAP_FOREACH(seat
, m
->seats
) {
785 _cleanup_free_
char *p
= NULL
;
787 p
= seat_bus_path(seat
);
791 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
796 r
= sd_bus_message_close_container(reply
);
800 return sd_bus_message_send(reply
);
803 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
804 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
805 Manager
*m
= ASSERT_PTR(userdata
);
806 Inhibitor
*inhibitor
;
811 r
= sd_bus_message_new_method_return(message
, &reply
);
815 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
819 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
) {
821 r
= sd_bus_message_append(reply
, "(ssssuu)",
822 strempty(inhibit_what_to_string(inhibitor
->what
)),
823 strempty(inhibitor
->who
),
824 strempty(inhibitor
->why
),
825 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
826 (uint32_t) inhibitor
->uid
,
827 (uint32_t) inhibitor
->pid
.pid
);
832 r
= sd_bus_message_close_container(reply
);
836 return sd_bus_message_send(reply
);
839 static int manager_choose_session_id(
841 const PidRef
*leader
,
847 assert(pidref_is_set(leader
));
850 /* Try to keep our session IDs and the audit session IDs in sync */
851 _cleanup_free_
char *id
= NULL
;
852 uint32_t audit_id
= AUDIT_SESSION_INVALID
;
853 r
= audit_session_from_pid(leader
, &audit_id
);
856 log_debug_errno(r
, "Failed to read audit session ID of process " PID_FMT
", ignoring: %m", leader
->pid
);
858 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
861 /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's
862 * not trust the audit data and let's better register a new ID */
863 if (hashmap_contains(m
->sessions
, id
)) {
864 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
873 if (asprintf(&id
, "c%" PRIu64
, ++m
->session_counter
) < 0)
876 } while (hashmap_contains(m
->sessions
, id
));
878 /* The generated names should not clash with 'auto' or 'self' */
879 assert(!session_is_self(id
));
880 assert(!session_is_auto(id
));
882 *ret_id
= TAKE_PTR(id
);
886 int manager_create_session(
889 PidRef
*leader
, /* consumed */
899 const char *remote_user
,
900 const char *remote_host
,
901 Session
**ret_session
) {
903 bool mangle_class
= false;
907 assert(uid_is_valid(uid
));
908 assert(pidref_is_set(leader
));
912 * -EBUSY → client is already in a session
913 * -EADDRNOTAVAIL → VT is already taken
914 * -EUSERS → limit of sessions reached
917 if (type
== _SESSION_TYPE_INVALID
) {
918 if (!isempty(display
))
920 else if (!isempty(tty
))
923 type
= SESSION_UNSPECIFIED
;
926 if (class == _SESSION_CLASS_INVALID
) {
927 if (type
== SESSION_UNSPECIFIED
)
928 class = SESSION_BACKGROUND
;
930 class = SESSION_USER
;
932 /* If we determined the class automatically, then let's later potentially change it to early
933 * or light flavours, once we learn the disposition of the user */
937 /* Check if we are already in a logind session, and if so refuse. */
938 r
= manager_get_session_by_pidref(m
, leader
, /* ret= */ NULL
);
940 return log_debug_errno(
942 "Failed to check if process " PID_FMT
" is already in a session: %m",
945 return log_debug_errno(SYNTHETIC_ERRNO(EBUSY
), "Client is already in a session.");
947 /* Old gdm and lightdm start the user-session on the same VT as the greeter session. But they destroy
948 * the greeter session after the user-session and want the user-session to take over the VT. We need
949 * to support this for backwards-compatibility, so make sure we allow new sessions on a VT that a
950 * greeter is running on. Furthermore, to allow re-logins, we have to allow a greeter to take over a
951 * used VT for the exact same reasons. */
952 if (class != SESSION_GREETER
&&
954 vtnr
< MALLOC_ELEMENTSOF(m
->seat0
->positions
) &&
955 m
->seat0
->positions
[vtnr
] &&
956 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
957 return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL
), "VT already occupied by a session.");
959 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
960 return log_debug_errno(SYNTHETIC_ERRNO(EUSERS
), "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.", m
->sessions_max
);
962 _cleanup_free_
char *id
= NULL
;
963 r
= manager_choose_session_id(m
, leader
, &id
);
967 /* If we are not watching utmp already, try again */
968 manager_reconnect_utmp(m
);
971 Session
*session
= NULL
;
973 r
= manager_add_user_by_uid(m
, uid
, &user
);
977 /* If we picked the session class on our own, and the user is not a regular one, and the session is
978 * not a graphical one then do not pull in session manager by default. For root make a special
979 * exception: for TTY logins leave the service manager on, but relax the /run/nologin
982 IN_SET(user_record_disposition(user
->user_record
), USER_INTRINSIC
, USER_SYSTEM
, USER_DYNAMIC
)) {
984 if (class == SESSION_USER
) {
985 if (user_record_is_root(user
->user_record
))
986 class = SESSION_USER_EARLY
;
987 else if (SESSION_TYPE_IS_GRAPHICAL(type
))
988 class = SESSION_USER
;
990 class = SESSION_USER_LIGHT
;
992 } else if (class == SESSION_BACKGROUND
)
993 class = SESSION_BACKGROUND_LIGHT
;
996 r
= manager_add_session(m
, id
, &session
);
1000 session_set_user(session
, user
);
1001 r
= session_set_leader_consume(session
, TAKE_PIDREF(*leader
));
1005 session
->original_type
= session
->type
= type
;
1006 session
->remote
= remote
;
1007 session
->vtnr
= vtnr
;
1008 session
->class = class;
1010 /* Once the first session that is of a pinning class shows up we'll change the GC mode for the user
1011 * from USER_GC_BY_ANY to USER_GC_BY_PIN, so that the user goes away once the last pinning session
1012 * goes away. Background: we want that user@.service – when started manually – remains around (which
1013 * itself is a non-pinning session), but gets stopped when the last pinning session goes away. */
1015 if (SESSION_CLASS_PIN_USER(class))
1016 user
->gc_mode
= USER_GC_BY_PIN
;
1018 if (!isempty(tty
)) {
1019 r
= strdup_to(&session
->tty
, tty
);
1023 session
->tty_validity
= TTY_FROM_PAM
;
1026 if (!isempty(display
)) {
1027 r
= strdup_to(&session
->display
, display
);
1032 if (!isempty(remote_user
)) {
1033 r
= strdup_to(&session
->remote_user
, remote_user
);
1038 if (!isempty(remote_host
)) {
1039 r
= strdup_to(&session
->remote_host
, remote_host
);
1044 if (!isempty(service
)) {
1045 r
= strdup_to(&session
->service
, service
);
1050 if (!isempty(desktop
)) {
1051 r
= strdup_to(&session
->desktop
, desktop
);
1057 r
= seat_attach_session(seat
, session
);
1062 *ret_session
= session
;
1067 session_add_to_gc_queue(session
);
1070 user_add_to_gc_queue(user
);
1075 static int manager_create_session_by_bus(
1077 sd_bus_message
*message
,
1078 sd_bus_error
*error
,
1082 const char *service
,
1085 const char *desktop
,
1089 const char *display
,
1091 const char *remote_user
,
1092 const char *remote_host
,
1100 if (!uid_is_valid(uid
))
1101 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
1104 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Flags must be zero.");
1106 _cleanup_(pidref_done
) PidRef leader
= PIDREF_NULL
;
1107 if (leader_pidfd
>= 0)
1108 r
= pidref_set_pidfd(&leader
, leader_pidfd
);
1109 else if (leader_pid
== 0)
1110 r
= bus_query_sender_pidref(message
, &leader
);
1113 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Leader PID is not valid");
1115 r
= pidref_set_pid(&leader
, leader_pid
);
1120 if (leader
.pid
== 1 || pidref_is_self(&leader
))
1121 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
1124 return sd_bus_error_set_errnof(error
, EUNATCH
, "Leader PIDFD not available");
1128 t
= _SESSION_TYPE_INVALID
;
1130 t
= session_type_from_string(type
);
1132 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1133 "Invalid session type %s", type
);
1138 c
= _SESSION_CLASS_INVALID
;
1140 c
= session_class_from_string(class);
1142 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1143 "Invalid session class %s", class);
1144 if (c
== SESSION_NONE
)
1145 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1146 "Refusing session class %s", class);
1149 if (isempty(desktop
))
1152 if (!string_is_safe(desktop
))
1153 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1154 "Invalid desktop string %s", desktop
);
1161 seat
= hashmap_get(m
->seats
, cseat
);
1163 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
,
1164 "No seat '%s' known", cseat
);
1169 else if (tty_is_vc(tty
)) {
1174 else if (seat
!= m
->seat0
)
1175 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1176 "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
1178 v
= vtnr_from_tty(tty
);
1180 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1181 "Cannot determine VT number from virtual console TTY %s", tty
);
1184 vtnr
= (uint32_t) v
;
1185 else if (vtnr
!= (uint32_t) v
)
1186 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1187 "Specified TTY and VT number do not match");
1189 } else if (tty_is_console(tty
)) {
1193 else if (seat
!= m
->seat0
)
1194 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1195 "Console TTY specified but seat is not seat0");
1198 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1199 "Console TTY specified but VT number is not 0");
1203 if (seat_has_vts(seat
)) {
1204 if (!vtnr_is_valid(vtnr
))
1205 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1206 "VT number out of range");
1209 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
1210 "Seat has no VTs but VT number not 0");
1215 r
= manager_create_session(
1232 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already running in a session or user slice");
1233 if (r
== -EADDRNOTAVAIL
)
1234 return sd_bus_error_set(error
, BUS_ERROR_SESSION_BUSY
, "Virtual terminal already occupied by a session");
1236 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.", m
->sessions_max
);
1240 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
1243 r
= session_start(session
, message
, error
);
1246 r
= sd_bus_message_exit_container(message
);
1250 session
->create_message
= sd_bus_message_ref(message
);
1252 /* Now call into session_send_create_reply(), which will reply to this method call for us. Or it
1253 * won't – in case we just spawned a session scope and/or user service manager, and they aren't ready
1254 * yet. We'll call session_create_reply() again once the session scope or the user service manager is
1255 * ready, where the function will check again if a reply is then ready to be sent, and then do so if
1256 * all is complete - or wait again. */
1257 r
= session_send_create_reply(session
, /* error= */ NULL
);
1265 session_add_to_gc_queue(session
);
1270 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1271 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1279 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
1280 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
1282 r
= sd_bus_message_read(
1301 return manager_create_session_by_bus(
1307 /* leader_pidfd = */ -EBADF
,
1322 static int method_create_session_pidfd(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1323 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
1327 int leader_fd
= -EBADF
, remote
, r
;
1329 r
= sd_bus_message_read(
1349 return manager_create_session_by_bus(
1354 /* leader_pid = */ 0,
1370 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1371 Manager
*m
= ASSERT_PTR(userdata
);
1372 Session
*session
, *sender_session
;
1378 r
= sd_bus_message_read(message
, "s", &name
);
1382 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1386 r
= get_sender_session(m
, message
, /* consult_display= */ false, error
, &sender_session
);
1390 if (session
!= sender_session
)
1391 return sd_bus_error_set(error
, SD_BUS_ERROR_ACCESS_DENIED
,
1392 "Refused to release session, since it doesn't match the one of the client");
1394 r
= session_release(session
);
1398 return sd_bus_reply_method_return(message
, NULL
);
1401 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1402 Manager
*m
= ASSERT_PTR(userdata
);
1409 r
= sd_bus_message_read(message
, "s", &name
);
1413 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1417 /* PolicyKit is done by bus_session_method_activate() */
1419 return bus_session_method_activate(message
, session
, error
);
1422 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1423 const char *session_name
, *seat_name
;
1424 Manager
*m
= ASSERT_PTR(userdata
);
1431 /* Same as ActivateSession() but refuses to work if the seat doesn't match */
1433 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
1437 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
1441 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
1445 if (session
->seat
!= seat
)
1446 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
,
1447 "Session %s not on seat %s", session_name
, seat_name
);
1449 r
= check_polkit_chvt(message
, m
, error
);
1453 return 1; /* Will call us back */
1455 r
= session_activate(session
);
1459 return sd_bus_reply_method_return(message
, NULL
);
1462 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1463 Manager
*m
= ASSERT_PTR(userdata
);
1470 r
= sd_bus_message_read(message
, "s", &name
);
1474 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1478 return bus_session_method_lock(message
, session
, error
);
1481 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1482 Manager
*m
= ASSERT_PTR(userdata
);
1487 r
= bus_verify_polkit_async(
1489 "org.freedesktop.login1.lock-sessions",
1490 /* details= */ NULL
,
1491 &m
->polkit_registry
,
1496 return 1; /* Will call us back */
1498 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1502 return sd_bus_reply_method_return(message
, NULL
);
1505 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1507 Manager
*m
= ASSERT_PTR(userdata
);
1513 r
= sd_bus_message_read(message
, "s", &name
);
1517 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1521 return bus_session_method_kill(message
, session
, error
);
1524 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1525 Manager
*m
= ASSERT_PTR(userdata
);
1532 r
= sd_bus_message_read(message
, "u", &uid
);
1536 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1540 return bus_user_method_kill(message
, user
, error
);
1543 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1544 Manager
*m
= ASSERT_PTR(userdata
);
1551 r
= sd_bus_message_read(message
, "s", &name
);
1555 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1559 return bus_session_method_terminate(message
, session
, error
);
1562 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1563 Manager
*m
= ASSERT_PTR(userdata
);
1570 r
= sd_bus_message_read(message
, "u", &uid
);
1574 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1578 return bus_user_method_terminate(message
, user
, error
);
1581 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1582 Manager
*m
= ASSERT_PTR(userdata
);
1589 r
= sd_bus_message_read(message
, "s", &name
);
1593 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1597 return bus_seat_method_terminate(message
, seat
, error
);
1600 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1601 Manager
*m
= ASSERT_PTR(userdata
);
1602 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1603 uint32_t uid
, auth_uid
;
1604 int r
, enable
, interactive
;
1608 r
= sd_bus_message_read(message
, "ubb", &uid
, &enable
, &interactive
);
1612 r
= sd_bus_query_sender_creds(message
,
1613 SD_BUS_CREDS_EUID
|SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
,
1618 if (!uid_is_valid(uid
)) {
1619 /* Note that we get the owner UID of the session or user unit, not the actual client UID here! */
1620 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1625 /* owner_uid is racy, so for authorization we must use euid */
1626 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1630 _cleanup_free_
struct passwd
*pw
= NULL
;
1632 r
= getpwuid_malloc(uid
, &pw
);
1636 r
= bus_verify_polkit_async_full(
1638 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1639 "org.freedesktop.login1.set-user-linger",
1640 /* details= */ NULL
,
1641 /* good_user= */ UID_INVALID
,
1642 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
1643 &m
->polkit_registry
,
1648 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1650 (void) mkdir_p_label("/var/lib/systemd", 0755);
1651 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1655 _cleanup_free_
char *escaped
= NULL
;
1659 escaped
= cescape(pw
->pw_name
);
1663 path
= strjoina("/var/lib/systemd/linger/", escaped
);
1670 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0) {
1673 user_add_to_gc_queue(u
);
1680 if (r
< 0 && errno
!= ENOENT
)
1683 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1685 /* Make sure that disabling lingering will terminate the user tracking if no sessions pin it. */
1686 u
->gc_mode
= USER_GC_BY_PIN
;
1687 user_add_to_gc_queue(u
);
1691 return sd_bus_reply_method_return(message
, NULL
);
1694 static int trigger_device(Manager
*m
, sd_device
*parent
) {
1695 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1700 r
= sd_device_enumerator_new(&e
);
1704 r
= sd_device_enumerator_allow_uninitialized(e
);
1709 r
= sd_device_enumerator_add_match_parent(e
, parent
);
1714 FOREACH_DEVICE(e
, d
) {
1715 r
= sd_device_trigger(d
, SD_DEVICE_CHANGE
);
1717 log_device_debug_errno(d
, r
, "Failed to trigger device, ignoring: %m");
1723 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
, sd_bus_error
*error
) {
1724 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1725 _cleanup_free_
char *file
= NULL
;
1726 const char *id_for_seat
;
1733 r
= sd_device_new_from_syspath(&d
, sysfs
);
1735 return sd_bus_error_set_errnof(error
, r
, "Failed to open device '%s': %m", sysfs
);
1737 if (sd_device_has_current_tag(d
, "seat") <= 0)
1738 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'seat' udev tag.", sysfs
);
1740 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1741 return sd_bus_error_set_errnof(error
, ENODEV
, "Device '%s' lacks 'ID_FOR_SEAT' udev property.", sysfs
);
1743 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1746 r
= write_string_filef(
1748 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
|WRITE_STRING_FILE_MKDIR_0755
|WRITE_STRING_FILE_LABEL
,
1749 "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
);
1753 return trigger_device(m
, d
);
1756 static int flush_devices(Manager
*m
) {
1757 _cleanup_closedir_
DIR *d
= NULL
;
1761 d
= opendir("/etc/udev/rules.d");
1763 if (errno
!= ENOENT
)
1764 log_warning_errno(errno
, "Failed to open %s: %m", "/etc/udev/rules.d");
1766 FOREACH_DIRENT_ALL(de
, d
, break) {
1767 if (!dirent_is_file(de
))
1770 if (!startswith(de
->d_name
, "72-seat-"))
1773 if (!endswith(de
->d_name
, ".rules"))
1776 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1777 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1780 return trigger_device(m
, NULL
);
1783 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1784 const char *sysfs
, *seat
;
1785 Manager
*m
= ASSERT_PTR(userdata
);
1790 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1794 if (!path_is_normalized(sysfs
))
1795 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not normalized", sysfs
);
1796 if (!path_startswith(sysfs
, "/sys"))
1797 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1799 if (seat_is_self(seat
) || seat_is_auto(seat
)) {
1802 r
= manager_get_seat_from_creds(m
, message
, seat
, error
, &found
);
1808 } else if (!seat_name_is_valid(seat
)) /* Note that a seat does not have to exist yet for this operation to succeed */
1809 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat name %s is not valid", seat
);
1811 r
= bus_verify_polkit_async_full(
1813 "org.freedesktop.login1.attach-device",
1814 /* details= */ NULL
,
1815 /* good_user= */ UID_INVALID
,
1816 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
1817 &m
->polkit_registry
,
1822 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1824 r
= attach_device(m
, seat
, sysfs
, error
);
1828 return sd_bus_reply_method_return(message
, NULL
);
1831 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1832 Manager
*m
= ASSERT_PTR(userdata
);
1837 r
= sd_bus_message_read(message
, "b", &interactive
);
1841 r
= bus_verify_polkit_async_full(
1843 "org.freedesktop.login1.flush-devices",
1844 /* details= */ NULL
,
1845 /* good_user= */ UID_INVALID
,
1846 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
1847 &m
->polkit_registry
,
1852 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1854 r
= flush_devices(m
);
1858 return sd_bus_reply_method_return(message
, NULL
);
1861 static int have_multiple_sessions(
1869 /* Check for other users' sessions. Greeter sessions do not
1870 * count, and non-login sessions do not count either. */
1871 HASHMAP_FOREACH(session
, m
->sessions
)
1872 if (SESSION_CLASS_IS_INHIBITOR_LIKE(session
->class) &&
1873 session
->user
->user_record
->uid
!= uid
)
1879 static int bus_manager_log_shutdown(
1881 const HandleActionData
*a
) {
1885 const char *message
= a
->message
?: "System is shutting down";
1886 const char *log_verb
= a
->log_verb
? strjoina("SHUTDOWN=", a
->log_verb
) : NULL
;
1888 return log_struct(LOG_NOTICE
,
1889 LOG_ITEM("MESSAGE_ID=%s", a
->message_id
?: SD_MESSAGE_SHUTDOWN_STR
),
1890 LOG_MESSAGE("%s%s%s%s.",
1892 m
->wall_message
? " (" : "",
1893 strempty(m
->wall_message
),
1894 m
->wall_message
? ")" : ""),
1898 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1899 Manager
*m
= ASSERT_PTR(userdata
);
1903 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1907 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1912 if (until
<= now(CLOCK_MONOTONIC
))
1915 /* We want to ignore the lid switch for a while after each
1916 * suspend, and after boot-up. Hence let's install a timer for
1917 * this. As long as the event source exists we ignore the lid
1920 if (m
->lid_switch_ignore_event_source
) {
1923 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1930 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1932 r
= sd_event_add_time(
1934 &m
->lid_switch_ignore_event_source
,
1937 lid_switch_ignore_handler
, m
);
1942 static int send_prepare_for(Manager
*m
, const HandleActionData
*a
, bool _active
) {
1943 int k
= 0, r
, active
= _active
;
1947 assert(IN_SET(a
->inhibit_what
, INHIBIT_SHUTDOWN
, INHIBIT_SLEEP
));
1949 /* We need to send both old and new signal for backward compatibility. The newer one allows clients
1950 * to know which type of reboot is going to happen, as they might be doing different actions (e.g.:
1951 * on soft-reboot), and it is sent first, so that clients know that if they receive the old one
1952 * first then they don't have to wait for the new one, as it means it's not supported. So, do not
1953 * change the order here, as it is an API. */
1954 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
) {
1955 k
= sd_bus_emit_signal(m
->bus
,
1956 "/org/freedesktop/login1",
1957 "org.freedesktop.login1.Manager",
1958 "PrepareForShutdownWithMetadata",
1964 handle_action_to_string(a
->handle
));
1966 log_debug_errno(k
, "Failed to emit PrepareForShutdownWithMetadata(): %m");
1969 r
= sd_bus_emit_signal(m
->bus
,
1970 "/org/freedesktop/login1",
1971 "org.freedesktop.login1.Manager",
1972 a
->inhibit_what
== INHIBIT_SHUTDOWN
? "PrepareForShutdown" : "PrepareForSleep",
1976 log_debug_errno(r
, "Failed to emit PrepareForShutdown(): %m");
1978 return RET_GATHER(k
, r
);
1981 static int strdup_job(sd_bus_message
*reply
, char **ret
) {
1989 r
= sd_bus_message_read_basic(reply
, 'o', &j
);
2001 static int execute_shutdown_or_sleep(
2003 const HandleActionData
*a
,
2004 sd_bus_error
*error
) {
2006 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
2010 assert(!m
->action_job
);
2013 if (a
->inhibit_what
== INHIBIT_SHUTDOWN
)
2014 bus_manager_log_shutdown(m
, a
);
2016 r
= bus_call_method(
2022 "ss", a
->target
, "replace-irreversibly");
2026 r
= strdup_job(reply
, &m
->action_job
);
2030 /* Save the action to prevent another request of shutdown or friends before the current action being
2031 * finished. See method_do_shutdown_or_sleep(). This is also used in match_job_removed() to log what
2032 * kind of action is finished. */
2033 m
->delayed_action
= a
;
2035 /* Make sure the lid switch is ignored for a while. */
2036 manager_set_lid_switch_ignore(m
, usec_add(now(CLOCK_MONOTONIC
), m
->holdoff_timeout_usec
));
2041 /* Tell people that they now may take a lock again. */
2042 (void) send_prepare_for(m
, a
, false);
2047 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
2048 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2049 Inhibitor
*offending
= NULL
;
2054 if (!manager
->delayed_action
|| manager
->action_job
)
2057 if (manager_is_inhibited(
2059 manager
->delayed_action
->inhibit_what
,
2061 MANAGER_IS_INHIBITED_CHECK_DELAY
,
2067 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
2068 (void) pidref_get_comm(&offending
->pid
, &comm
);
2069 u
= uid_to_name(offending
->uid
);
2071 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
2072 offending
->uid
, strna(u
),
2073 offending
->pid
.pid
, strna(comm
));
2076 /* Actually do the operation */
2077 r
= execute_shutdown_or_sleep(manager
, manager
->delayed_action
, &error
);
2079 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
2080 bus_error_message(&error
, r
));
2082 manager
->delayed_action
= NULL
;
2085 return 1; /* We did some work. */
2088 static int manager_inhibit_timeout_handler(
2093 Manager
*manager
= ASSERT_PTR(userdata
);
2095 assert(manager
->inhibit_timeout_source
== s
);
2097 return manager_dispatch_delayed(manager
, true);
2100 static int delay_shutdown_or_sleep(
2102 const HandleActionData
*a
) {
2109 r
= event_reset_time_relative(
2110 m
->event
, &m
->inhibit_timeout_source
,
2111 CLOCK_MONOTONIC
, m
->inhibit_delay_max
, /* accuracy = */ 0,
2112 manager_inhibit_timeout_handler
, m
,
2113 /* priority = */ 0, "inhibit-timeout", /* force_reset = */ true);
2115 return log_error_errno(r
, "Failed to reset timer event source for inhibit timeout: %m");
2117 m
->delayed_action
= a
;
2122 static void cancel_delayed_action(Manager
*m
) {
2125 (void) sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_OFF
);
2127 /* When m->action_job is NULL, the delayed action has not been triggered yet. Let's clear it to
2128 * accept later shutdown and friends.
2130 * When m->action_job is non-NULL, the delayed action has been already triggered, and now we are
2131 * waiting for the job being finished. In match_job_removed(), the triggered action will be used.
2132 * Hence, do not clear it. */
2134 m
->delayed_action
= NULL
;
2137 int bus_manager_shutdown_or_sleep_now_or_later(
2139 const HandleActionData
*a
,
2140 sd_bus_error
*error
) {
2142 _cleanup_free_
char *load_state
= NULL
;
2148 assert(!m
->action_job
);
2150 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
2154 if (!streq(load_state
, "loaded"))
2155 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
2156 "Unit %s is %s, refusing operation.",
2157 a
->target
, load_state
);
2159 /* Tell everybody to prepare for shutdown/sleep */
2160 (void) send_prepare_for(m
, a
, true);
2163 m
->inhibit_delay_max
> 0 &&
2164 manager_is_inhibited(m
, a
->inhibit_what
, NULL
, MANAGER_IS_INHIBITED_CHECK_DELAY
, UID_INVALID
, NULL
);
2167 /* Shutdown is delayed, keep in mind what we
2168 * want to do, and start a timeout */
2169 r
= delay_shutdown_or_sleep(m
, a
);
2171 /* Shutdown is not delayed, execute it
2173 r
= execute_shutdown_or_sleep(m
, a
, error
);
2178 static int verify_shutdown_creds(
2180 sd_bus_message
*message
,
2181 const HandleActionData
*a
,
2183 sd_bus_error
*error
) {
2185 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2186 bool multiple_sessions
, blocked
, interactive
;
2187 _unused_
bool error_or_denial
= false;
2188 Inhibitor
*offending
= NULL
;
2196 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2200 r
= sd_bus_creds_get_euid(creds
, &uid
);
2204 r
= have_multiple_sessions(m
, uid
);
2208 multiple_sessions
= r
> 0;
2209 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, NULL
, /* flags= */ 0, uid
, &offending
);
2210 interactive
= flags
& SD_LOGIND_INTERACTIVE
;
2212 if (multiple_sessions
) {
2213 r
= bus_verify_polkit_async_full(
2215 a
->polkit_action_multiple_sessions
,
2216 /* details= */ NULL
,
2217 /* good_user= */ UID_INVALID
,
2218 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
2219 &m
->polkit_registry
,
2222 /* If we get -EBUSY, it means a polkit decision was made, but not for
2223 * this action in particular. Assuming we are blocked on inhibitors,
2224 * ignore that error and allow the decision to be revealed below. */
2225 if (blocked
&& r
== -EBUSY
)
2226 error_or_denial
= true;
2231 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2235 PolkitFlags polkit_flags
= 0;
2237 /* With a strong inhibitor, if the skip flag is not set, reject outright.
2238 * With a weak inhibitor, if root is asking and the root flag is set, reject outright.
2239 * All else, check polkit first. */
2240 if (!FLAGS_SET(flags
, SD_LOGIND_SKIP_INHIBITORS
) &&
2241 (offending
->mode
!= INHIBIT_BLOCK_WEAK
||
2242 (uid
== 0 && FLAGS_SET(flags
, SD_LOGIND_ROOT_CHECK_INHIBITORS
))))
2243 return sd_bus_error_setf(error
, BUS_ERROR_BLOCKED_BY_INHIBITOR_LOCK
,
2244 "Operation denied due to active block inhibitor");
2246 /* We want to always ask here, even for root, to only allow bypassing if explicitly allowed
2247 * by polkit, unless a weak blocker is used, in which case it will be authorized. */
2248 if (offending
->mode
!= INHIBIT_BLOCK_WEAK
)
2249 polkit_flags
|= POLKIT_ALWAYS_QUERY
;
2252 polkit_flags
|= POLKIT_ALLOW_INTERACTIVE
;
2254 r
= bus_verify_polkit_async_full(
2256 a
->polkit_action_ignore_inhibit
,
2257 /* details= */ NULL
,
2258 /* good_user= */ UID_INVALID
,
2260 &m
->polkit_registry
,
2265 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2268 if (!multiple_sessions
&& !blocked
) {
2269 r
= bus_verify_polkit_async_full(
2272 /* details= */ NULL
,
2273 /* good_user= */ UID_INVALID
,
2274 interactive
? POLKIT_ALLOW_INTERACTIVE
: 0,
2275 &m
->polkit_registry
,
2280 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2283 /* If error_or_denial was set above, it means that a polkit denial or
2284 * error was deferred for a future call to bus_verify_polkit_async_full()
2285 * to catch. In any case, it also means that the payload guarded by
2286 * these polkit calls should never be executed, and hence we should
2287 * never reach this point. */
2288 assert(!error_or_denial
);
2293 static int setup_wall_message_timer(Manager
*m
, sd_bus_message
* message
) {
2294 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2297 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2299 const char *tty
= NULL
;
2301 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2302 (void) sd_bus_creds_get_tty(creds
, &tty
);
2304 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2309 r
= manager_setup_wall_message_timer(m
);
2316 static int method_do_shutdown_or_sleep(
2318 sd_bus_message
*message
,
2319 HandleAction action
,
2321 sd_bus_error
*error
) {
2328 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2331 /* New style method: with flags parameter (and interactive bool in the bus message header) */
2332 r
= sd_bus_message_read(message
, "t", &flags
);
2335 if ((flags
& ~SD_LOGIND_SHUTDOWN_AND_SLEEP_FLAGS_PUBLIC
) != 0)
2336 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2337 "Invalid flags parameter");
2339 if (FLAGS_SET(flags
, (SD_LOGIND_REBOOT_VIA_KEXEC
|SD_LOGIND_SOFT_REBOOT
)))
2340 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2341 "Both reboot via kexec and soft reboot selected, which is not supported");
2343 if (action
!= HANDLE_REBOOT
) {
2344 if (FLAGS_SET(flags
, SD_LOGIND_REBOOT_VIA_KEXEC
))
2345 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2346 "Reboot via kexec option is only applicable with reboot operations");
2347 if (flags
& (SD_LOGIND_SOFT_REBOOT
|SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
))
2348 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
,
2349 "Soft reboot option is only applicable with reboot operations");
2352 /* Old style method: no flags parameter, but interactive bool passed as boolean in
2353 * payload. Let's convert this argument to the new-style flags parameter for our internal
2357 r
= sd_bus_message_read(message
, "b", &interactive
);
2361 flags
= interactive
? SD_LOGIND_INTERACTIVE
: 0;
2364 const HandleActionData
*a
= NULL
;
2366 if (FLAGS_SET(flags
, SD_LOGIND_SOFT_REBOOT
) ||
2367 (FLAGS_SET(flags
, SD_LOGIND_SOFT_REBOOT_IF_NEXTROOT_SET_UP
) && path_is_os_tree("/run/nextroot") > 0))
2368 a
= handle_action_lookup(HANDLE_SOFT_REBOOT
);
2369 else if (FLAGS_SET(flags
, SD_LOGIND_REBOOT_VIA_KEXEC
) && kexec_loaded())
2370 a
= handle_action_lookup(HANDLE_KEXEC
);
2372 if (action
== HANDLE_SLEEP
) {
2373 HandleAction selected
;
2375 selected
= handle_action_sleep_select(m
);
2377 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2378 "None of the configured sleep operations are supported");
2380 assert_se(a
= handle_action_lookup(selected
));
2382 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2383 SleepSupport support
;
2385 assert_se(a
= handle_action_lookup(action
));
2387 assert(a
->sleep_operation
>= 0);
2388 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2390 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2396 case SLEEP_DISABLED
:
2397 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2398 "Sleep verb '%s' is disabled by config",
2399 sleep_operation_to_string(a
->sleep_operation
));
2401 case SLEEP_NOT_CONFIGURED
:
2402 case SLEEP_STATE_OR_MODE_NOT_SUPPORTED
:
2403 case SLEEP_ALARM_NOT_SUPPORTED
:
2404 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2405 "Sleep verb '%s' is not configured or configuration is not supported by kernel",
2406 sleep_operation_to_string(a
->sleep_operation
));
2408 case SLEEP_RESUME_NOT_SUPPORTED
:
2409 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2410 "Not running on EFI and resume= is not set, or noresume is set. No available method to resume from hibernation");
2412 case SLEEP_RESUME_DEVICE_MISSING
:
2413 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2414 "Specified resume device is missing or is not an active swap device");
2416 case SLEEP_RESUME_MISCONFIGURED
:
2417 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2418 "Invalid resume config: resume= is not populated yet resume_offset= is");
2420 case SLEEP_NOT_ENOUGH_SWAP_SPACE
:
2421 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
2422 "Not enough suitable swap space for hibernation available on compatible block devices and file systems");
2425 assert_not_reached();
2429 assert_se(a
= handle_action_lookup(action
));
2431 r
= verify_shutdown_creds(m
, message
, a
, flags
, error
);
2435 if (m
->delayed_action
)
2436 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
2437 "Action %s already in progress, refusing requested %s operation.",
2438 handle_action_to_string(m
->delayed_action
->handle
),
2439 handle_action_to_string(a
->handle
));
2441 /* reset case we're shorting a scheduled shutdown */
2442 m
->unlink_nologin
= false;
2443 reset_scheduled_shutdown(m
);
2445 m
->scheduled_shutdown_timeout
= 0;
2446 m
->scheduled_shutdown_action
= action
;
2448 (void) setup_wall_message_timer(m
, message
);
2450 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, error
);
2454 return sd_bus_reply_method_return(message
, NULL
);
2457 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2458 Manager
*m
= userdata
;
2460 return method_do_shutdown_or_sleep(
2463 sd_bus_message_is_method_call(message
, NULL
, "PowerOffWithFlags"),
2467 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2468 Manager
*m
= userdata
;
2470 return method_do_shutdown_or_sleep(
2473 sd_bus_message_is_method_call(message
, NULL
, "RebootWithFlags"),
2477 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2478 Manager
*m
= userdata
;
2480 return method_do_shutdown_or_sleep(
2483 sd_bus_message_is_method_call(message
, NULL
, "HaltWithFlags"),
2487 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2488 Manager
*m
= userdata
;
2490 return method_do_shutdown_or_sleep(
2493 sd_bus_message_is_method_call(message
, NULL
, "SuspendWithFlags"),
2497 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2498 Manager
*m
= userdata
;
2500 return method_do_shutdown_or_sleep(
2503 sd_bus_message_is_method_call(message
, NULL
, "HibernateWithFlags"),
2507 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2508 Manager
*m
= userdata
;
2510 return method_do_shutdown_or_sleep(
2512 HANDLE_HYBRID_SLEEP
,
2513 sd_bus_message_is_method_call(message
, NULL
, "HybridSleepWithFlags"),
2517 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2518 Manager
*m
= userdata
;
2520 return method_do_shutdown_or_sleep(
2522 HANDLE_SUSPEND_THEN_HIBERNATE
,
2523 sd_bus_message_is_method_call(message
, NULL
, "SuspendThenHibernateWithFlags"),
2527 static int method_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2528 Manager
*m
= userdata
;
2530 return method_do_shutdown_or_sleep(
2533 /* with_flags = */ true,
2537 static int nologin_timeout_handler(
2542 Manager
*m
= ASSERT_PTR(userdata
);
2544 log_info("Creating /run/nologin, blocking further logins...");
2547 create_shutdown_run_nologin_or_warn() >= 0;
2552 static usec_t
nologin_timeout_usec(usec_t elapse
) {
2553 /* Issue /run/nologin five minutes before shutdown */
2554 return LESS_BY(elapse
, 5 * USEC_PER_MINUTE
);
2557 static void reset_scheduled_shutdown(Manager
*m
) {
2560 m
->scheduled_shutdown_timeout_source
= sd_event_source_disable_unref(m
->scheduled_shutdown_timeout_source
);
2561 m
->wall_message_timeout_source
= sd_event_source_disable_unref(m
->wall_message_timeout_source
);
2562 m
->nologin_timeout_source
= sd_event_source_disable_unref(m
->nologin_timeout_source
);
2564 m
->scheduled_shutdown_action
= _HANDLE_ACTION_INVALID
;
2565 m
->scheduled_shutdown_timeout
= USEC_INFINITY
;
2566 m
->scheduled_shutdown_uid
= UID_INVALID
;
2567 m
->scheduled_shutdown_tty
= mfree(m
->scheduled_shutdown_tty
);
2568 m
->shutdown_dry_run
= false;
2570 if (m
->unlink_nologin
) {
2571 (void) unlink_or_warn("/run/nologin");
2572 m
->unlink_nologin
= false;
2575 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2577 manager_send_changed(m
, "ScheduledShutdown");
2580 static int update_schedule_file(Manager
*m
) {
2581 _cleanup_(unlink_and_freep
) char *temp_path
= NULL
;
2582 _cleanup_fclose_
FILE *f
= NULL
;
2586 assert(handle_action_valid(m
->scheduled_shutdown_action
));
2588 r
= mkdir_parents_label(SHUTDOWN_SCHEDULE_FILE
, 0755);
2590 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
2592 r
= fopen_temporary(SHUTDOWN_SCHEDULE_FILE
, &f
, &temp_path
);
2594 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
2596 (void) fchmod(fileno(f
), 0644);
2598 serialize_usec(f
, "USEC", m
->scheduled_shutdown_timeout
);
2599 serialize_item_format(f
, "WARN_WALL", "%s", one_zero(m
->wall_messages
));
2600 serialize_item_format(f
, "MODE", "%s", handle_action_to_string(m
->scheduled_shutdown_action
));
2601 serialize_item_format(f
, "UID", UID_FMT
, m
->scheduled_shutdown_uid
);
2603 if (m
->scheduled_shutdown_tty
)
2604 serialize_item_format(f
, "TTY", "%s", m
->scheduled_shutdown_tty
);
2606 if (!isempty(m
->wall_message
)) {
2607 r
= serialize_item_escaped(f
, "WALL_MESSAGE", m
->wall_message
);
2612 r
= fflush_and_check(f
);
2616 if (rename(temp_path
, SHUTDOWN_SCHEDULE_FILE
) < 0) {
2621 temp_path
= mfree(temp_path
);
2625 (void) unlink(SHUTDOWN_SCHEDULE_FILE
);
2627 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
2630 static int manager_scheduled_shutdown_handler(
2635 Manager
*m
= ASSERT_PTR(userdata
);
2636 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2637 const HandleActionData
*a
;
2640 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2642 /* Don't allow multiple jobs being executed at the same time */
2643 if (m
->delayed_action
) {
2644 r
= log_error_errno(SYNTHETIC_ERRNO(EALREADY
),
2645 "Scheduled shutdown to %s failed: shutdown or sleep operation already in progress.",
2650 if (m
->shutdown_dry_run
) {
2651 /* We do not process delay inhibitors here. Otherwise, we
2652 * would have to be considered "in progress" (like the check
2653 * above) for some seconds after our admin has seen the final
2656 bus_manager_log_shutdown(m
, a
);
2657 log_info("Running in dry run, suppressing action.");
2658 reset_scheduled_shutdown(m
);
2663 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, a
, &error
);
2665 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", a
->target
);
2672 reset_scheduled_shutdown(m
);
2676 static int manager_setup_shutdown_timers(Manager
* m
) {
2681 r
= event_reset_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2683 m
->scheduled_shutdown_timeout
, 0,
2684 manager_scheduled_shutdown_handler
, m
,
2685 0, "scheduled-shutdown-timeout", true);
2689 r
= event_reset_time(m
->event
, &m
->nologin_timeout_source
,
2691 nologin_timeout_usec(m
->scheduled_shutdown_timeout
), 0,
2692 nologin_timeout_handler
, m
,
2693 0, "nologin-timeout", true);
2700 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2701 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
2706 void manager_load_scheduled_shutdown(Manager
*m
) {
2707 _cleanup_fclose_
FILE *f
= NULL
;
2708 _cleanup_free_
char *usec
= NULL
,
2711 *wall_message
= NULL
,
2718 r
= parse_env_file(f
, SHUTDOWN_SCHEDULE_FILE
,
2720 "WARN_WALL", &warn_wall
,
2722 "WALL_MESSAGE", &wall_message
,
2726 /* reset will delete the file */
2727 reset_scheduled_shutdown(m
);
2732 return (void) log_debug_errno(r
, "Failed to parse " SHUTDOWN_SCHEDULE_FILE
": %m");
2734 HandleAction handle
= handle_action_from_string(mode
);
2736 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to parse scheduled shutdown type: %s", mode
);
2739 return (void) log_debug_errno(SYNTHETIC_ERRNO(EINVAL
), "USEC is required");
2740 if (deserialize_usec(usec
, &m
->scheduled_shutdown_timeout
) < 0)
2743 /* assign parsed type only after we know usec is also valid */
2744 m
->scheduled_shutdown_action
= handle
;
2747 r
= parse_boolean(warn_wall
);
2749 log_debug_errno(r
, "Failed to parse enabling wall messages");
2751 m
->wall_messages
= r
;
2755 _cleanup_free_
char *unescaped
= NULL
;
2756 r
= cunescape(wall_message
, 0, &unescaped
);
2758 log_debug_errno(r
, "Failed to parse wall message: %s", wall_message
);
2760 free_and_replace(m
->wall_message
, unescaped
);
2764 r
= parse_uid(uid
, &m
->scheduled_shutdown_uid
);
2766 log_debug_errno(r
, "Failed to parse wall uid: %s", uid
);
2769 free_and_replace(m
->scheduled_shutdown_tty
, tty
);
2771 r
= manager_setup_shutdown_timers(m
);
2773 return reset_scheduled_shutdown(m
);
2775 (void) manager_setup_wall_message_timer(m
);
2776 (void) update_schedule_file(m
);
2781 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2782 Manager
*m
= ASSERT_PTR(userdata
);
2783 HandleAction handle
;
2784 const HandleActionData
*a
;
2788 bool dry_run
= false;
2792 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2796 if (startswith(type
, "dry-")) {
2801 handle
= handle_action_from_string(type
);
2802 if (!HANDLE_ACTION_IS_SHUTDOWN(handle
))
2803 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type: %s", type
);
2805 assert_se(a
= handle_action_lookup(handle
));
2806 assert(a
->polkit_action
);
2808 r
= verify_shutdown_creds(m
, message
, a
, 0, error
);
2812 if (elapse
== USEC_INFINITY
) {
2813 if (m
->maintenance_time
) {
2814 r
= calendar_spec_next_usec(m
->maintenance_time
, now(CLOCK_REALTIME
), &elapse
);
2817 return sd_bus_error_set(error
,
2818 BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED
,
2819 "No upcoming maintenance window scheduled");
2821 return sd_bus_error_set_errnof(error
, r
,
2822 "Failed to determine next maintenance window: %m");
2825 log_info("Scheduled %s at maintenance window %s", type
, FORMAT_TIMESTAMP(elapse
));
2827 /* the good old shutdown command uses one minute by default */
2828 elapse
= usec_add(now(CLOCK_REALTIME
), USEC_PER_MINUTE
);
2831 m
->scheduled_shutdown_action
= handle
;
2832 m
->shutdown_dry_run
= dry_run
;
2833 m
->scheduled_shutdown_timeout
= elapse
;
2835 r
= manager_setup_shutdown_timers(m
);
2839 r
= setup_wall_message_timer(m
, message
);
2841 r
= update_schedule_file(m
);
2844 reset_scheduled_shutdown(m
);
2848 manager_send_changed(m
, "ScheduledShutdown");
2850 return sd_bus_reply_method_return(message
, NULL
);
2853 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2854 Manager
*m
= ASSERT_PTR(userdata
);
2855 const HandleActionData
*a
;
2861 cancelled
= handle_action_valid(m
->scheduled_shutdown_action
) && m
->scheduled_shutdown_action
!= HANDLE_IGNORE
;
2863 return sd_bus_reply_method_return(message
, "b", false);
2865 assert_se(a
= handle_action_lookup(m
->scheduled_shutdown_action
));
2866 if (!a
->polkit_action
)
2867 return sd_bus_error_set(error
, SD_BUS_ERROR_AUTH_FAILED
, "Unsupported shutdown type");
2869 r
= bus_verify_polkit_async(
2872 /* details= */ NULL
,
2873 &m
->polkit_registry
,
2878 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2880 if (m
->wall_messages
) {
2881 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2882 const char *tty
= NULL
;
2885 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2887 (void) sd_bus_creds_get_uid(creds
, &uid
);
2888 (void) sd_bus_creds_get_tty(creds
, &tty
);
2891 _cleanup_free_
char *username
= uid_to_name(uid
);
2893 log_struct(LOG_INFO
,
2894 LOG_MESSAGE("System shutdown has been cancelled"),
2895 LOG_ITEM("ACTION=%s", handle_action_to_string(a
->handle
)),
2896 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN_CANCELED_STR
),
2897 username
? "OPERATOR=%s" : NULL
, username
);
2899 (void) wall("System shutdown has been cancelled",
2900 username
, tty
, logind_wall_tty_filter
, m
);
2903 cancel_delayed_action(m
);
2904 reset_scheduled_shutdown(m
);
2906 return sd_bus_reply_method_return(message
, "b", true);
2909 static int method_can_shutdown_or_sleep(
2911 sd_bus_message
*message
,
2912 HandleAction action
,
2913 sd_bus_error
*error
) {
2915 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2916 bool multiple_sessions
, challenge
, blocked
, check_unit_state
= true;
2917 const HandleActionData
*a
;
2918 const char *result
= NULL
;
2924 assert(HANDLE_ACTION_IS_SHUTDOWN(action
) || HANDLE_ACTION_IS_SLEEP(action
));
2926 if (action
== HANDLE_SLEEP
) {
2927 HandleAction selected
;
2929 selected
= handle_action_sleep_select(m
);
2931 return sd_bus_reply_method_return(message
, "s", "na");
2933 check_unit_state
= false; /* Already handled by handle_action_sleep_select */
2935 assert_se(a
= handle_action_lookup(selected
));
2937 } else if (HANDLE_ACTION_IS_SLEEP(action
)) {
2938 SleepSupport support
;
2940 assert_se(a
= handle_action_lookup(action
));
2942 assert(a
->sleep_operation
>= 0);
2943 assert(a
->sleep_operation
< _SLEEP_OPERATION_MAX
);
2945 r
= sleep_supported_full(a
->sleep_operation
, &support
);
2949 return sd_bus_reply_method_return(message
, "s", support
== SLEEP_DISABLED
? "no" : "na");
2951 assert_se(a
= handle_action_lookup(action
));
2953 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2957 r
= sd_bus_creds_get_euid(creds
, &uid
);
2961 r
= have_multiple_sessions(m
, uid
);
2965 multiple_sessions
= r
> 0;
2966 blocked
= manager_is_inhibited(m
, a
->inhibit_what
, NULL
, /* flags= */ 0, uid
, NULL
);
2968 if (check_unit_state
&& a
->target
) {
2969 _cleanup_free_
char *load_state
= NULL
;
2971 r
= unit_load_state(m
->bus
, a
->target
, &load_state
);
2975 if (!streq(load_state
, "loaded")) {
2981 if (multiple_sessions
) {
2982 r
= bus_test_polkit(
2984 a
->polkit_action_multiple_sessions
,
2985 /* details= */ NULL
,
2986 /* good_user= */ UID_INVALID
,
2995 result
= "challenge";
3001 r
= bus_test_polkit(
3003 a
->polkit_action_ignore_inhibit
,
3004 /* details= */ NULL
,
3005 /* good_user= */ UID_INVALID
,
3014 } else if (challenge
) {
3015 if (!result
|| streq(result
, "yes"))
3016 result
= "challenge";
3021 if (!multiple_sessions
&& !blocked
) {
3022 /* If neither inhibit nor multiple sessions
3023 * apply then just check the normal policy */
3025 r
= bus_test_polkit(
3028 /* details= */ NULL
,
3029 /* good_user= */ UID_INVALID
,
3038 result
= "challenge";
3044 return sd_bus_reply_method_return(message
, "s", result
);
3047 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3048 Manager
*m
= userdata
;
3050 return method_can_shutdown_or_sleep(m
, message
, HANDLE_POWEROFF
, error
);
3053 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3054 Manager
*m
= userdata
;
3056 return method_can_shutdown_or_sleep(m
, message
, HANDLE_REBOOT
, error
);
3059 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3060 Manager
*m
= userdata
;
3062 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HALT
, error
);
3065 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3066 Manager
*m
= userdata
;
3068 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND
, error
);
3071 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3072 Manager
*m
= userdata
;
3074 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HIBERNATE
, error
);
3077 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3078 Manager
*m
= userdata
;
3080 return method_can_shutdown_or_sleep(m
, message
, HANDLE_HYBRID_SLEEP
, error
);
3083 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3084 Manager
*m
= userdata
;
3086 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SUSPEND_THEN_HIBERNATE
, error
);
3089 static int method_can_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3090 Manager
*m
= userdata
;
3092 return method_can_shutdown_or_sleep(m
, message
, HANDLE_SLEEP
, error
);
3095 static int property_get_reboot_parameter(
3098 const char *interface
,
3099 const char *property
,
3100 sd_bus_message
*reply
,
3102 sd_bus_error
*error
) {
3103 _cleanup_free_
char *parameter
= NULL
;
3110 r
= read_reboot_parameter(¶meter
);
3114 return sd_bus_message_append(reply
, "s", parameter
);
3117 static int method_set_reboot_parameter(
3118 sd_bus_message
*message
,
3120 sd_bus_error
*error
) {
3122 Manager
*m
= ASSERT_PTR(userdata
);
3128 r
= sd_bus_message_read(message
, "s", &arg
);
3132 if (!reboot_parameter_is_valid(arg
))
3133 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid reboot parameter '%s'.", arg
);
3135 r
= detect_container();
3139 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
,
3140 "Reboot parameter not supported in containers, refusing.");
3142 r
= bus_verify_polkit_async(
3144 "org.freedesktop.login1.set-reboot-parameter",
3145 /* details= */ NULL
,
3146 &m
->polkit_registry
,
3151 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3153 r
= update_reboot_parameter_and_warn(arg
, false);
3157 return sd_bus_reply_method_return(message
, NULL
);
3160 static int method_can_reboot_parameter(
3161 sd_bus_message
*message
,
3163 sd_bus_error
*error
) {
3165 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3170 r
= detect_container();
3173 if (r
> 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */
3174 return sd_bus_reply_method_return(message
, "s", "na");
3176 return return_test_polkit(
3178 "org.freedesktop.login1.set-reboot-parameter",
3179 /* details= */ NULL
,
3180 /* good_user= */ UID_INVALID
,
3184 static int property_get_reboot_to_firmware_setup(
3187 const char *interface
,
3188 const char *property
,
3189 sd_bus_message
*reply
,
3191 sd_bus_error
*error
) {
3198 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
3200 /* EFI case: let's see what is currently configured in the EFI variables */
3201 r
= efi_get_reboot_to_firmware();
3202 if (r
< 0 && r
!= -EOPNOTSUPP
)
3203 log_warning_errno(r
, "Failed to determine reboot-to-firmware-setup state: %m");
3205 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
3207 /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */
3208 if (access("/run/systemd/reboot-to-firmware-setup", F_OK
) < 0) {
3209 if (errno
!= ENOENT
)
3210 log_warning_errno(errno
, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m");
3217 return sd_bus_message_append(reply
, "b", r
> 0);
3220 static int method_set_reboot_to_firmware_setup(
3221 sd_bus_message
*message
,
3223 sd_bus_error
*error
) {
3225 Manager
*m
= ASSERT_PTR(userdata
);
3231 r
= sd_bus_message_read(message
, "b", &b
);
3235 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
3237 /* EFI case: let's see what the firmware supports */
3239 r
= efi_reboot_to_firmware_supported();
3240 if (r
== -EOPNOTSUPP
)
3241 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
3247 } else if (r
<= 0) {
3248 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */
3251 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
3253 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Firmware does not support boot into firmware.");
3255 /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */
3258 r
= bus_verify_polkit_async(
3260 "org.freedesktop.login1.set-reboot-to-firmware-setup",
3261 /* details= */ NULL
,
3262 &m
->polkit_registry
,
3267 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3270 r
= efi_set_reboot_to_firmware(b
);
3275 r
= touch("/run/systemd/reboot-to-firmware-setup");
3279 if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno
!= ENOENT
)
3284 return sd_bus_reply_method_return(message
, NULL
);
3287 static int method_can_reboot_to_firmware_setup(
3288 sd_bus_message
*message
,
3290 sd_bus_error
*error
) {
3292 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3297 r
= getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP");
3299 /* EFI case: let's see what the firmware supports */
3301 r
= efi_reboot_to_firmware_supported();
3303 if (r
!= -EOPNOTSUPP
)
3304 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
3306 return sd_bus_reply_method_return(message
, "s", "na");
3309 } else if (r
<= 0) {
3310 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */
3313 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m");
3315 return sd_bus_reply_method_return(message
, "s", "na");
3318 return return_test_polkit(
3320 "org.freedesktop.login1.set-reboot-to-firmware-setup",
3321 /* details= */ NULL
,
3322 /* good_user= */ UID_INVALID
,
3326 static int property_get_reboot_to_boot_loader_menu(
3329 const char *interface
,
3330 const char *property
,
3331 sd_bus_message
*reply
,
3333 sd_bus_error
*error
) {
3335 uint64_t x
= UINT64_MAX
;
3342 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3344 /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distinguished:
3346 * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user)
3347 * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user)
3348 * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds
3351 r
= efi_loader_get_config_timeout_one_shot(&x
);
3354 log_warning_errno(r
, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m");
3358 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3360 _cleanup_free_
char *v
= NULL
;
3362 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */
3364 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v
);
3367 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m");
3369 r
= safe_atou64(v
, &x
);
3371 log_warning_errno(r
, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m");
3375 return sd_bus_message_append(reply
, "t", x
);
3378 static int method_set_reboot_to_boot_loader_menu(
3379 sd_bus_message
*message
,
3381 sd_bus_error
*error
) {
3383 Manager
*m
= ASSERT_PTR(userdata
);
3390 r
= sd_bus_message_read(message
, "t", &x
);
3394 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3398 /* EFI case: let's see if booting into boot loader menu is supported. */
3400 r
= efi_loader_get_features(&features
);
3402 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3403 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3404 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3408 } else if (r
<= 0) {
3409 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */
3412 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3414 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader does not support boot into boot loader menu.");
3416 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */
3419 r
= bus_verify_polkit_async(
3421 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3422 /* details= */ NULL
,
3423 &m
->polkit_registry
,
3428 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3431 if (x
== UINT64_MAX
)
3432 r
= efi_set_variable(EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"), NULL
, 0);
3434 char buf
[DECIMAL_STR_MAX(uint64_t) + 1];
3435 xsprintf(buf
, "%" PRIu64
, DIV_ROUND_UP(x
, USEC_PER_SEC
)); /* second granularity */
3437 r
= efi_set_variable_string(EFI_LOADER_VARIABLE_STR("LoaderConfigTimeoutOneShot"), buf
);
3442 if (x
== UINT64_MAX
) {
3443 if (unlink("/run/systemd/reboot-to-boot-loader-menu") < 0 && errno
!= ENOENT
)
3446 r
= write_string_filef("/run/systemd/reboot-to-boot-loader-menu",
3447 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
|WRITE_STRING_FILE_LABEL
,
3448 "%" PRIu64
, x
); /* μs granularity */
3454 return sd_bus_reply_method_return(message
, NULL
);
3457 static int method_can_reboot_to_boot_loader_menu(
3458 sd_bus_message
*message
,
3460 sd_bus_error
*error
) {
3462 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3467 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU");
3469 uint64_t features
= 0;
3471 /* EFI case, let's see if booting into boot loader menu is supported. */
3473 r
= efi_loader_get_features(&features
);
3475 log_warning_errno(r
, "Failed to determine whether reboot to boot loader menu is supported: %m");
3476 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT
))
3477 return sd_bus_reply_method_return(message
, "s", "na");
3479 } else if (r
<= 0) {
3480 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */
3483 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m");
3485 return sd_bus_reply_method_return(message
, "s", "na");
3488 return return_test_polkit(
3490 "org.freedesktop.login1.set-reboot-to-boot-loader-menu",
3491 /* details= */ NULL
,
3492 /* good_user= */ UID_INVALID
,
3496 static int property_get_reboot_to_boot_loader_entry(
3499 const char *interface
,
3500 const char *property
,
3501 sd_bus_message
*reply
,
3503 sd_bus_error
*error
) {
3505 _cleanup_free_
char *v
= NULL
;
3506 Manager
*m
= ASSERT_PTR(userdata
);
3507 const char *x
= NULL
;
3513 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3515 /* EFI case: let's read the LoaderEntryOneShot variable */
3517 r
= efi_loader_update_entry_one_shot_cache(&m
->efi_loader_entry_one_shot
, &m
->efi_loader_entry_one_shot_stat
);
3520 log_warning_errno(r
, "Failed to read LoaderEntryOneShot variable, ignoring: %m");
3522 x
= m
->efi_loader_entry_one_shot
;
3525 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3528 /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */
3530 r
= read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v
);
3533 log_warning_errno(r
, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m");
3534 } else if (!efi_loader_entry_name_valid(v
))
3535 log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring.");
3540 return sd_bus_message_append(reply
, "s", x
);
3543 static int boot_loader_entry_exists(Manager
*m
, const char *id
) {
3544 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3550 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3551 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if no GPT is found, hence skip ENOKEY */
3554 r
= manager_read_efi_boot_loader_entries(m
);
3556 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3558 return !!boot_config_find_entry(&config
, id
);
3561 static int method_set_reboot_to_boot_loader_entry(
3562 sd_bus_message
*message
,
3564 sd_bus_error
*error
) {
3566 Manager
*m
= ASSERT_PTR(userdata
);
3573 r
= sd_bus_message_read(message
, "s", &v
);
3579 else if (efi_loader_entry_name_valid(v
)) {
3580 r
= boot_loader_entry_exists(m
, v
);
3584 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Boot loader entry '%s' is not known.", v
);
3586 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Boot loader entry name '%s' is not valid, refusing.", v
);
3588 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3592 /* EFI case: let's see if booting into boot loader entry is supported. */
3594 r
= efi_loader_get_features(&features
);
3596 log_warning_errno(r
, "Failed to determine whether reboot into boot loader entry is supported: %m");
3597 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3598 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3602 } else if (r
<= 0) {
3603 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */
3606 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3608 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Loader does not support boot into boot loader entry.");
3610 /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */
3613 r
= bus_verify_polkit_async(
3615 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3616 /* details= */ NULL
,
3617 &m
->polkit_registry
,
3622 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3627 r
= efi_set_variable(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), NULL
, 0);
3629 r
= efi_set_variable_string(EFI_LOADER_VARIABLE_STR("LoaderEntryOneShot"), v
);
3634 if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno
!= ENOENT
)
3637 r
= write_string_file("/run/systemd/reboot-boot-to-loader-entry", v
, WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
|WRITE_STRING_FILE_LABEL
);
3643 return sd_bus_reply_method_return(message
, NULL
);
3646 static int method_can_reboot_to_boot_loader_entry(
3647 sd_bus_message
*message
,
3649 sd_bus_error
*error
) {
3651 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
3656 r
= getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY");
3658 uint64_t features
= 0;
3660 /* EFI case, let's see if booting into boot loader entry is supported. */
3662 r
= efi_loader_get_features(&features
);
3664 log_warning_errno(r
, "Failed to determine whether reboot to boot loader entry is supported: %m");
3665 if (r
< 0 || !FLAGS_SET(features
, EFI_LOADER_FEATURE_ENTRY_ONESHOT
))
3666 return sd_bus_reply_method_return(message
, "s", "na");
3668 } else if (r
<= 0) {
3669 /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */
3672 log_warning_errno(r
, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m");
3674 return sd_bus_reply_method_return(message
, "s", "na");
3677 return return_test_polkit(
3679 "org.freedesktop.login1.set-reboot-to-boot-loader-entry",
3680 /* details= */ NULL
,
3681 /* good_user= */ UID_INVALID
,
3685 static int property_get_boot_loader_entries(
3688 const char *interface
,
3689 const char *property
,
3690 sd_bus_message
*reply
,
3692 sd_bus_error
*error
) {
3694 _cleanup_(boot_config_free
) BootConfig config
= BOOT_CONFIG_NULL
;
3695 Manager
*m
= ASSERT_PTR(userdata
);
3702 r
= boot_config_load_auto(&config
, NULL
, NULL
);
3703 if (r
< 0 && r
!= -ENOKEY
) /* don't complain if there's no GPT found */
3706 r
= manager_read_efi_boot_loader_entries(m
);
3708 (void) boot_config_augment_from_loader(&config
, m
->efi_boot_loader_entries
, /* auto_only= */ true);
3710 r
= sd_bus_message_open_container(reply
, 'a', "s");
3714 for (i
= 0; i
< config
.n_entries
; i
++) {
3715 BootEntry
*e
= config
.entries
+ i
;
3717 r
= sd_bus_message_append(reply
, "s", e
->id
);
3722 return sd_bus_message_close_container(reply
);
3725 static int method_set_wall_message(
3726 sd_bus_message
*message
,
3728 sd_bus_error
*error
) {
3731 Manager
*m
= ASSERT_PTR(userdata
);
3733 int enable_wall_messages
;
3737 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
3741 if (strlen(wall_message
) > WALL_MESSAGE_MAX
)
3742 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3743 "Wall message too long, maximum permitted length is %u characters.",
3746 /* Short-circuit the operation if the desired state is already in place, to
3747 * avoid an unnecessary polkit permission check. */
3748 if (streq_ptr(m
->wall_message
, empty_to_null(wall_message
)) &&
3749 m
->wall_messages
== enable_wall_messages
)
3752 r
= bus_verify_polkit_async(
3754 "org.freedesktop.login1.set-wall-message",
3755 /* details= */ NULL
,
3756 &m
->polkit_registry
,
3761 return 1; /* Will call us back */
3763 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
3767 m
->wall_messages
= enable_wall_messages
;
3770 return sd_bus_reply_method_return(message
, NULL
);
3773 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
3774 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3775 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
3776 const char *who
, *why
, *what
, *mode
;
3777 _cleanup_free_
char *id
= NULL
;
3778 _cleanup_close_
int fifo_fd
= -EBADF
;
3779 Manager
*m
= ASSERT_PTR(userdata
);
3783 _unused_
bool error_or_denial
= false;
3788 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
3792 w
= inhibit_what_from_string(what
);
3794 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3795 "Invalid what specification %s", what
);
3797 mm
= inhibit_mode_from_string(mode
);
3799 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3800 "Invalid mode specification %s", mode
);
3802 /* Delay is only supported for shutdown/sleep */
3803 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
3804 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
,
3805 "Delay inhibitors only supported for shutdown and sleep");
3807 /* Don't allow taking delay locks while we are already
3808 * executing the operation. We shouldn't create the impression
3809 * that the lock was successful if the machine is about to go
3810 * down/suspend any moment. */
3811 if (m
->delayed_action
&& m
->delayed_action
->inhibit_what
& w
)
3812 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
,
3813 "The operation inhibition has been requested for is already running");
3816 const InhibitWhat v
= 1U << i
;
3818 r
= bus_verify_polkit_async(
3820 v
== INHIBIT_SHUTDOWN
? (IN_SET(mm
, INHIBIT_BLOCK
, INHIBIT_BLOCK_WEAK
) ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
3821 v
== INHIBIT_SLEEP
? (IN_SET(mm
, INHIBIT_BLOCK
, INHIBIT_BLOCK_WEAK
) ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
3822 v
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
3823 v
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
3824 v
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
3825 v
== INHIBIT_HANDLE_REBOOT_KEY
? "org.freedesktop.login1.inhibit-handle-reboot-key" :
3826 v
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
3827 "org.freedesktop.login1.inhibit-handle-lid-switch",
3828 /* details= */ NULL
,
3829 &m
->polkit_registry
,
3832 /* If we get -EBUSY, it means a polkit decision was made, but not for
3833 * this action in particular. Assuming there are more actions requested,
3834 * ignore that error and allow the decision to be revealed later. */
3835 if ((~v
& w
) && r
== -EBUSY
)
3836 error_or_denial
= true;
3841 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
3844 /* If error_or_denial was set above, it means that a polkit denial or
3845 * error was deferred for a future call to bus_verify_polkit_async()
3846 * to catch. In any case, it also means that the payload guarded by
3847 * these polkit calls should never be executed, and hence we should
3848 * never reach this point. */
3849 assert(!error_or_denial
);
3851 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
|SD_BUS_CREDS_PIDFD
, &creds
);
3855 r
= sd_bus_creds_get_euid(creds
, &uid
);
3859 r
= bus_creds_get_pidref(creds
, &pidref
);
3863 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
3864 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
,
3865 "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.",
3871 if (asprintf(&id
, "%" PRIu64
, ++m
->inhibit_counter
) < 0)
3874 } while (hashmap_get(m
->inhibitors
, id
));
3876 _cleanup_(inhibitor_freep
) Inhibitor
*i
= NULL
;
3877 r
= manager_add_inhibitor(m
, id
, &i
);
3883 i
->pid
= TAKE_PIDREF(pidref
);
3885 i
->why
= strdup(why
);
3886 i
->who
= strdup(who
);
3888 if (!i
->why
|| !i
->who
)
3891 fifo_fd
= inhibitor_create_fifo(i
);
3895 r
= inhibitor_start(i
);
3900 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
3903 static const sd_bus_vtable manager_vtable
[] = {
3904 SD_BUS_VTABLE_START(0),
3906 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", bus_property_get_bool
, bus_property_set_bool
, offsetof(Manager
, wall_messages
), 0),
3907 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
3909 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
3910 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3911 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
3912 SD_BUS_PROPERTY("KillUserProcesses", "b", bus_property_get_bool
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3913 SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter
, 0, 0),
3914 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
3915 SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu
, 0, 0),
3916 SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry
, 0, 0),
3917 SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3918 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3919 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3920 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3921 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3922 SD_BUS_PROPERTY("BlockWeakInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3923 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3924 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3925 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
3926 SD_BUS_PROPERTY("SleepOperation", "as", property_get_sleep_operations
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3927 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3928 SD_BUS_PROPERTY("HandlePowerKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3929 SD_BUS_PROPERTY("HandleRebootKey", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3930 SD_BUS_PROPERTY("HandleRebootKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_reboot_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3931 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3932 SD_BUS_PROPERTY("HandleSuspendKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3933 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3934 SD_BUS_PROPERTY("HandleHibernateKeyLongPress", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key_long_press
), SD_BUS_VTABLE_PROPERTY_CONST
),
3935 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
3936 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
3937 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
3938 SD_BUS_PROPERTY("HandleSecureAttentionKey", "s", property_get_handle_action
, offsetof(Manager
, handle_secure_attention_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
3939 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3940 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
3941 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3942 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
3943 SD_BUS_PROPERTY("PreparingForShutdownWithMetadata", "a{sv}", property_get_preparing_shutdown_with_metadata
, 0, 0),
3944 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
3945 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3946 SD_BUS_PROPERTY("DesignatedMaintenanceTime", "s", property_get_maintenance_time
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
3947 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
3948 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
3949 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
3950 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
3951 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
3952 SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL
, offsetof(Manager
, runtime_dir_inodes
), SD_BUS_VTABLE_PROPERTY_CONST
),
3953 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3954 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
3955 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
3956 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
3957 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
3958 SD_BUS_PROPERTY("StopIdleSessionUSec", "t", NULL
, offsetof(Manager
, stop_idle_session_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
3960 SD_BUS_METHOD_WITH_ARGS("GetSession",
3961 SD_BUS_ARGS("s", session_id
),
3962 SD_BUS_RESULT("o", object_path
),
3964 SD_BUS_VTABLE_UNPRIVILEGED
),
3965 SD_BUS_METHOD_WITH_ARGS("GetSessionByPID",
3966 SD_BUS_ARGS("u", pid
),
3967 SD_BUS_RESULT("o", object_path
),
3968 method_get_session_by_pid
,
3969 SD_BUS_VTABLE_UNPRIVILEGED
),
3970 SD_BUS_METHOD_WITH_ARGS("GetUser",
3971 SD_BUS_ARGS("u", uid
),
3972 SD_BUS_RESULT("o", object_path
),
3974 SD_BUS_VTABLE_UNPRIVILEGED
),
3975 SD_BUS_METHOD_WITH_ARGS("GetUserByPID",
3976 SD_BUS_ARGS("u", pid
),
3977 SD_BUS_RESULT("o", object_path
),
3978 method_get_user_by_pid
,
3979 SD_BUS_VTABLE_UNPRIVILEGED
),
3980 SD_BUS_METHOD_WITH_ARGS("GetSeat",
3981 SD_BUS_ARGS("s", seat_id
),
3982 SD_BUS_RESULT("o", object_path
),
3984 SD_BUS_VTABLE_UNPRIVILEGED
),
3985 SD_BUS_METHOD_WITH_ARGS("ListSessions",
3987 SD_BUS_RESULT("a(susso)", sessions
),
3988 method_list_sessions
,
3989 SD_BUS_VTABLE_UNPRIVILEGED
),
3990 SD_BUS_METHOD_WITH_ARGS("ListSessionsEx",
3992 SD_BUS_RESULT("a(sussussbto)", sessions
),
3993 method_list_sessions_ex
,
3994 SD_BUS_VTABLE_UNPRIVILEGED
),
3995 SD_BUS_METHOD_WITH_ARGS("ListUsers",
3997 SD_BUS_RESULT("a(uso)", users
),
3999 SD_BUS_VTABLE_UNPRIVILEGED
),
4000 SD_BUS_METHOD_WITH_ARGS("ListSeats",
4002 SD_BUS_RESULT("a(so)", seats
),
4004 SD_BUS_VTABLE_UNPRIVILEGED
),
4005 SD_BUS_METHOD_WITH_ARGS("ListInhibitors",
4007 SD_BUS_RESULT("a(ssssuu)", inhibitors
),
4008 method_list_inhibitors
,
4009 SD_BUS_VTABLE_UNPRIVILEGED
),
4010 SD_BUS_METHOD_WITH_ARGS("CreateSession",
4011 SD_BUS_ARGS("u", uid
,
4024 "a(sv)", properties
),
4025 SD_BUS_RESULT("s", session_id
,
4033 method_create_session
,
4035 SD_BUS_METHOD_WITH_ARGS("CreateSessionWithPIDFD",
4036 SD_BUS_ARGS("u", uid
,
4050 "a(sv)", properties
),
4051 SD_BUS_RESULT("s", session_id
,
4059 method_create_session_pidfd
,
4061 SD_BUS_METHOD_WITH_ARGS("ReleaseSession",
4062 SD_BUS_ARGS("s", session_id
),
4064 method_release_session
,
4065 SD_BUS_VTABLE_UNPRIVILEGED
),
4066 SD_BUS_METHOD_WITH_ARGS("ActivateSession",
4067 SD_BUS_ARGS("s", session_id
),
4069 method_activate_session
,
4070 SD_BUS_VTABLE_UNPRIVILEGED
),
4071 SD_BUS_METHOD_WITH_ARGS("ActivateSessionOnSeat",
4072 SD_BUS_ARGS("s", session_id
, "s", seat_id
),
4074 method_activate_session_on_seat
,
4075 SD_BUS_VTABLE_UNPRIVILEGED
),
4076 SD_BUS_METHOD_WITH_ARGS("LockSession",
4077 SD_BUS_ARGS("s", session_id
),
4079 method_lock_session
,
4080 SD_BUS_VTABLE_UNPRIVILEGED
),
4081 SD_BUS_METHOD_WITH_ARGS("UnlockSession",
4082 SD_BUS_ARGS("s", session_id
),
4084 method_lock_session
,
4085 SD_BUS_VTABLE_UNPRIVILEGED
),
4086 SD_BUS_METHOD("LockSessions",
4089 method_lock_sessions
,
4090 SD_BUS_VTABLE_UNPRIVILEGED
),
4091 SD_BUS_METHOD("UnlockSessions",
4094 method_lock_sessions
,
4095 SD_BUS_VTABLE_UNPRIVILEGED
),
4096 SD_BUS_METHOD_WITH_ARGS("KillSession",
4097 SD_BUS_ARGS("s", session_id
, "s", whom
, "i", signal_number
),
4099 method_kill_session
,
4100 SD_BUS_VTABLE_UNPRIVILEGED
),
4101 SD_BUS_METHOD_WITH_ARGS("KillUser",
4102 SD_BUS_ARGS("u", uid
, "i", signal_number
),
4105 SD_BUS_VTABLE_UNPRIVILEGED
),
4106 SD_BUS_METHOD_WITH_ARGS("TerminateSession",
4107 SD_BUS_ARGS("s", session_id
),
4109 method_terminate_session
,
4110 SD_BUS_VTABLE_UNPRIVILEGED
),
4111 SD_BUS_METHOD_WITH_ARGS("TerminateUser",
4112 SD_BUS_ARGS("u", uid
),
4114 method_terminate_user
,
4115 SD_BUS_VTABLE_UNPRIVILEGED
),
4116 SD_BUS_METHOD_WITH_ARGS("TerminateSeat",
4117 SD_BUS_ARGS("s", seat_id
),
4119 method_terminate_seat
,
4120 SD_BUS_VTABLE_UNPRIVILEGED
),
4121 SD_BUS_METHOD_WITH_ARGS("SetUserLinger",
4122 SD_BUS_ARGS("u", uid
, "b", enable
, "b", interactive
),
4124 method_set_user_linger
,
4125 SD_BUS_VTABLE_UNPRIVILEGED
),
4126 SD_BUS_METHOD_WITH_ARGS("AttachDevice",
4127 SD_BUS_ARGS("s", seat_id
, "s", sysfs_path
, "b", interactive
),
4129 method_attach_device
,
4130 SD_BUS_VTABLE_UNPRIVILEGED
),
4131 SD_BUS_METHOD_WITH_ARGS("FlushDevices",
4132 SD_BUS_ARGS("b", interactive
),
4134 method_flush_devices
,
4135 SD_BUS_VTABLE_UNPRIVILEGED
),
4136 SD_BUS_METHOD_WITH_ARGS("PowerOff",
4137 SD_BUS_ARGS("b", interactive
),
4140 SD_BUS_VTABLE_UNPRIVILEGED
),
4141 SD_BUS_METHOD_WITH_ARGS("PowerOffWithFlags",
4142 SD_BUS_ARGS("t", flags
),
4145 SD_BUS_VTABLE_UNPRIVILEGED
),
4146 SD_BUS_METHOD_WITH_ARGS("Reboot",
4147 SD_BUS_ARGS("b", interactive
),
4150 SD_BUS_VTABLE_UNPRIVILEGED
),
4151 SD_BUS_METHOD_WITH_ARGS("RebootWithFlags",
4152 SD_BUS_ARGS("t", flags
),
4155 SD_BUS_VTABLE_UNPRIVILEGED
),
4156 SD_BUS_METHOD_WITH_ARGS("Halt",
4157 SD_BUS_ARGS("b", interactive
),
4160 SD_BUS_VTABLE_UNPRIVILEGED
),
4161 SD_BUS_METHOD_WITH_ARGS("HaltWithFlags",
4162 SD_BUS_ARGS("t", flags
),
4165 SD_BUS_VTABLE_UNPRIVILEGED
),
4166 SD_BUS_METHOD_WITH_ARGS("Suspend",
4167 SD_BUS_ARGS("b", interactive
),
4170 SD_BUS_VTABLE_UNPRIVILEGED
),
4171 SD_BUS_METHOD_WITH_ARGS("SuspendWithFlags",
4172 SD_BUS_ARGS("t", flags
),
4175 SD_BUS_VTABLE_UNPRIVILEGED
),
4176 SD_BUS_METHOD_WITH_ARGS("Hibernate",
4177 SD_BUS_ARGS("b", interactive
),
4180 SD_BUS_VTABLE_UNPRIVILEGED
),
4181 SD_BUS_METHOD_WITH_ARGS("HibernateWithFlags",
4182 SD_BUS_ARGS("t", flags
),
4185 SD_BUS_VTABLE_UNPRIVILEGED
),
4186 SD_BUS_METHOD_WITH_ARGS("HybridSleep",
4187 SD_BUS_ARGS("b", interactive
),
4189 method_hybrid_sleep
,
4190 SD_BUS_VTABLE_UNPRIVILEGED
),
4191 SD_BUS_METHOD_WITH_ARGS("HybridSleepWithFlags",
4192 SD_BUS_ARGS("t", flags
),
4194 method_hybrid_sleep
,
4195 SD_BUS_VTABLE_UNPRIVILEGED
),
4196 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernate",
4197 SD_BUS_ARGS("b", interactive
),
4199 method_suspend_then_hibernate
,
4200 SD_BUS_VTABLE_UNPRIVILEGED
),
4201 SD_BUS_METHOD_WITH_ARGS("SuspendThenHibernateWithFlags",
4202 SD_BUS_ARGS("t", flags
),
4204 method_suspend_then_hibernate
,
4205 SD_BUS_VTABLE_UNPRIVILEGED
),
4206 SD_BUS_METHOD_WITH_ARGS("Sleep",
4207 SD_BUS_ARGS("t", flags
),
4210 SD_BUS_VTABLE_UNPRIVILEGED
),
4211 SD_BUS_METHOD_WITH_ARGS("CanPowerOff",
4213 SD_BUS_RESULT("s", result
),
4214 method_can_poweroff
,
4215 SD_BUS_VTABLE_UNPRIVILEGED
),
4216 SD_BUS_METHOD_WITH_ARGS("CanReboot",
4218 SD_BUS_RESULT("s", result
),
4220 SD_BUS_VTABLE_UNPRIVILEGED
),
4221 SD_BUS_METHOD_WITH_ARGS("CanHalt",
4223 SD_BUS_RESULT("s", result
),
4225 SD_BUS_VTABLE_UNPRIVILEGED
),
4226 SD_BUS_METHOD_WITH_ARGS("CanSuspend",
4228 SD_BUS_RESULT("s", result
),
4230 SD_BUS_VTABLE_UNPRIVILEGED
),
4231 SD_BUS_METHOD_WITH_ARGS("CanHibernate",
4233 SD_BUS_RESULT("s", result
),
4234 method_can_hibernate
,
4235 SD_BUS_VTABLE_UNPRIVILEGED
),
4236 SD_BUS_METHOD_WITH_ARGS("CanHybridSleep",
4238 SD_BUS_RESULT("s", result
),
4239 method_can_hybrid_sleep
,
4240 SD_BUS_VTABLE_UNPRIVILEGED
),
4241 SD_BUS_METHOD_WITH_ARGS("CanSuspendThenHibernate",
4243 SD_BUS_RESULT("s", result
),
4244 method_can_suspend_then_hibernate
,
4245 SD_BUS_VTABLE_UNPRIVILEGED
),
4246 SD_BUS_METHOD_WITH_ARGS("CanSleep",
4248 SD_BUS_RESULT("s", result
),
4250 SD_BUS_VTABLE_UNPRIVILEGED
),
4251 SD_BUS_METHOD_WITH_ARGS("ScheduleShutdown",
4252 SD_BUS_ARGS("s", type
, "t", usec
),
4254 method_schedule_shutdown
,
4255 SD_BUS_VTABLE_UNPRIVILEGED
),
4256 SD_BUS_METHOD_WITH_ARGS("CancelScheduledShutdown",
4258 SD_BUS_RESULT("b", cancelled
),
4259 method_cancel_scheduled_shutdown
,
4260 SD_BUS_VTABLE_UNPRIVILEGED
),
4261 SD_BUS_METHOD_WITH_ARGS("Inhibit",
4262 SD_BUS_ARGS("s", what
, "s", who
, "s", why
, "s", mode
),
4263 SD_BUS_RESULT("h", pipe_fd
),
4265 SD_BUS_VTABLE_UNPRIVILEGED
),
4266 SD_BUS_METHOD_WITH_ARGS("CanRebootParameter",
4268 SD_BUS_RESULT("s", result
),
4269 method_can_reboot_parameter
,
4270 SD_BUS_VTABLE_UNPRIVILEGED
),
4271 SD_BUS_METHOD_WITH_ARGS("SetRebootParameter",
4272 SD_BUS_ARGS("s", parameter
),
4274 method_set_reboot_parameter
,
4275 SD_BUS_VTABLE_UNPRIVILEGED
),
4276 SD_BUS_METHOD_WITH_ARGS("CanRebootToFirmwareSetup",
4278 SD_BUS_RESULT("s", result
),
4279 method_can_reboot_to_firmware_setup
,
4280 SD_BUS_VTABLE_UNPRIVILEGED
),
4281 SD_BUS_METHOD_WITH_ARGS("SetRebootToFirmwareSetup",
4282 SD_BUS_ARGS("b", enable
),
4284 method_set_reboot_to_firmware_setup
,
4285 SD_BUS_VTABLE_UNPRIVILEGED
),
4286 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderMenu",
4288 SD_BUS_RESULT("s", result
),
4289 method_can_reboot_to_boot_loader_menu
,
4290 SD_BUS_VTABLE_UNPRIVILEGED
),
4291 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderMenu",
4292 SD_BUS_ARGS("t", timeout
),
4294 method_set_reboot_to_boot_loader_menu
,
4295 SD_BUS_VTABLE_UNPRIVILEGED
),
4296 SD_BUS_METHOD_WITH_ARGS("CanRebootToBootLoaderEntry",
4298 SD_BUS_RESULT("s", result
),
4299 method_can_reboot_to_boot_loader_entry
,
4300 SD_BUS_VTABLE_UNPRIVILEGED
),
4301 SD_BUS_METHOD_WITH_ARGS("SetRebootToBootLoaderEntry",
4302 SD_BUS_ARGS("s", boot_loader_entry
),
4304 method_set_reboot_to_boot_loader_entry
,
4305 SD_BUS_VTABLE_UNPRIVILEGED
),
4306 SD_BUS_METHOD_WITH_ARGS("SetWallMessage",
4307 SD_BUS_ARGS("s", wall_message
, "b", enable
),
4309 method_set_wall_message
,
4310 SD_BUS_VTABLE_UNPRIVILEGED
),
4312 SD_BUS_SIGNAL_WITH_ARGS("SecureAttentionKey",
4313 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4315 SD_BUS_SIGNAL_WITH_ARGS("SessionNew",
4316 SD_BUS_ARGS("s", session_id
, "o", object_path
),
4318 SD_BUS_SIGNAL_WITH_ARGS("SessionRemoved",
4319 SD_BUS_ARGS("s", session_id
, "o", object_path
),
4321 SD_BUS_SIGNAL_WITH_ARGS("UserNew",
4322 SD_BUS_ARGS("u", uid
, "o", object_path
),
4324 SD_BUS_SIGNAL_WITH_ARGS("UserRemoved",
4325 SD_BUS_ARGS("u", uid
, "o", object_path
),
4327 SD_BUS_SIGNAL_WITH_ARGS("SeatNew",
4328 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4330 SD_BUS_SIGNAL_WITH_ARGS("SeatRemoved",
4331 SD_BUS_ARGS("s", seat_id
, "o", object_path
),
4333 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdown",
4334 SD_BUS_ARGS("b", start
),
4336 SD_BUS_SIGNAL_WITH_ARGS("PrepareForShutdownWithMetadata",
4337 SD_BUS_ARGS("b", start
, "a{sv}", metadata
),
4339 SD_BUS_SIGNAL_WITH_ARGS("PrepareForSleep",
4340 SD_BUS_ARGS("b", start
),
4346 const BusObjectImplementation manager_object
= {
4347 "/org/freedesktop/login1",
4348 "org.freedesktop.login1.Manager",
4349 .vtables
= BUS_VTABLES(manager_vtable
),
4350 .children
= BUS_IMPLEMENTATIONS(&seat_object
,
4355 static void session_jobs_reply(Session
*s
, uint32_t jid
, const char *unit
, const char *result
) {
4362 if (result
&& !streq(result
, "done")) {
4363 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
4365 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
,
4366 "Job %u for unit '%s' failed with '%s'", jid
, unit
, result
);
4368 (void) session_send_create_reply(s
, &e
);
4369 (void) session_send_upgrade_reply(s
, &e
);
4373 (void) session_send_create_reply(s
, /* error= */ NULL
);
4374 (void) session_send_upgrade_reply(s
, /* error= */ NULL
);
4377 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4378 Manager
*m
= ASSERT_PTR(userdata
);
4379 const char *path
, *result
, *unit
;
4385 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
4387 bus_log_parse_error(r
);
4391 if (m
->action_job
&& streq(m
->action_job
, path
)) {
4392 assert(m
->delayed_action
);
4393 log_info("Operation '%s' finished.", handle_action_to_string(m
->delayed_action
->handle
));
4395 /* Tell people that they now may take a lock again */
4396 (void) send_prepare_for(m
, m
->delayed_action
, false);
4398 m
->action_job
= mfree(m
->action_job
);
4399 m
->delayed_action
= NULL
;
4406 session
= hashmap_get(m
->session_units
, unit
);
4408 if (streq_ptr(path
, session
->scope_job
)) {
4409 session
->scope_job
= mfree(session
->scope_job
);
4410 session_jobs_reply(session
, id
, unit
, result
);
4412 session_save(session
);
4413 user_save(session
->user
);
4416 session_add_to_gc_queue(session
);
4419 user
= hashmap_get(m
->user_units
, unit
);
4421 /* If the user is stopping, we're tracking stop jobs here. So don't send reply. */
4422 if (!user
->stopping
) {
4424 FOREACH_ARGUMENT(user_job
, &user
->runtime_dir_job
, &user
->service_manager_job
)
4425 if (streq_ptr(path
, *user_job
)) {
4426 *user_job
= mfree(*user_job
);
4428 LIST_FOREACH(sessions_by_user
, s
, user
->sessions
)
4429 /* Don't propagate user service failures to the client */
4430 session_jobs_reply(s
, id
, unit
, /* result = */ NULL
);
4437 user_add_to_gc_queue(user
);
4443 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4444 const char *path
, *unit
;
4445 Manager
*m
= ASSERT_PTR(userdata
);
4452 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
4454 bus_log_parse_error(r
);
4458 session
= hashmap_get(m
->session_units
, unit
);
4460 session_add_to_gc_queue(session
);
4462 user
= hashmap_get(m
->user_units
, unit
);
4464 user_add_to_gc_queue(user
);
4469 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4470 _cleanup_free_
char *unit
= NULL
;
4471 Manager
*m
= ASSERT_PTR(userdata
);
4479 path
= sd_bus_message_get_path(message
);
4483 r
= unit_name_from_dbus_path(path
, &unit
);
4484 if (r
== -EINVAL
) /* not a unit */
4491 session
= hashmap_get(m
->session_units
, unit
);
4493 session_add_to_gc_queue(session
);
4495 user
= hashmap_get(m
->user_units
, unit
);
4497 user_add_to_gc_queue(user
);
4502 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
4503 Manager
*m
= ASSERT_PTR(userdata
);
4509 r
= sd_bus_message_read(message
, "b", &b
);
4511 bus_log_parse_error(r
);
4518 /* systemd finished reloading, let's recheck all our sessions */
4519 log_debug("System manager has been reloaded, rechecking sessions...");
4521 HASHMAP_FOREACH(session
, m
->sessions
)
4522 session_add_to_gc_queue(session
);
4527 int manager_send_changed_strv(Manager
*manager
, char **properties
) {
4530 return sd_bus_emit_properties_changed_strv(
4532 "/org/freedesktop/login1",
4533 "org.freedesktop.login1.Manager",
4537 int manager_start_scope(
4540 const PidRef
*pidref
,
4543 const char *description
,
4544 const char * const *requires
,
4545 const char * const *wants
,
4546 const char * const *extra_after
,
4547 const char *requires_mounts_for
,
4548 sd_bus_message
*more_properties
,
4549 sd_bus_error
*error
,
4552 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
4553 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
4558 assert(pidref_is_set(pidref
));
4561 r
= bus_message_new_method_call(manager
->bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
4565 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
4569 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
4573 if (!isempty(slice
)) {
4574 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
4579 if (!isempty(description
)) {
4580 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
4585 STRV_FOREACH(i
, requires
) {
4586 r
= sd_bus_message_append(m
, "(sv)", "Requires", "as", 1, *i
);
4590 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4595 STRV_FOREACH(i
, wants
) {
4596 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
4600 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4605 STRV_FOREACH(i
, extra_after
) {
4606 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
4611 if (!empty_or_root(requires_mounts_for
)) {
4612 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
4617 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
4619 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
4623 r
= bus_append_scope_pidref(m
, pidref
, allow_pidfd
);
4627 /* For login session scopes, if a process is OOM killed by the kernel, *don't* terminate the rest of
4629 r
= sd_bus_message_append(m
, "(sv)", "OOMPolicy", "s", "continue");
4633 /* disable TasksMax= for the session scope, rely on the slice setting for it */
4634 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_MAX
);
4636 return bus_log_create_error(r
);
4638 if (more_properties
) {
4639 /* If TasksMax also appears here, it will overwrite the default value set above */
4640 r
= sd_bus_message_copy(m
, more_properties
, true);
4645 r
= sd_bus_message_close_container(m
);
4649 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
4653 r
= sd_bus_call(manager
->bus
, m
, 0, &e
, &reply
);
4655 /* If this failed with a property we couldn't write, this is quite likely because the server
4656 * doesn't support PIDFDs yet, let's try without. */
4658 sd_bus_error_has_names(&e
, SD_BUS_ERROR_UNKNOWN_PROPERTY
, SD_BUS_ERROR_PROPERTY_READ_ONLY
))
4659 return manager_start_scope(
4663 /* allow_pidfd = */ false,
4669 requires_mounts_for
,
4674 return sd_bus_error_move(error
, &e
);
4677 return strdup_job(reply
, ret_job
);
4680 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **ret_job
) {
4681 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4688 r
= bus_call_method(
4694 "ss", unit
, "replace");
4698 return strdup_job(reply
, ret_job
);
4701 int manager_stop_unit(
4704 const char *job_mode
,
4705 sd_bus_error
*ret_error
,
4708 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4709 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4716 r
= bus_call_method(
4722 "ss", unit
, job_mode
?: "fail");
4724 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
, BUS_ERROR_LOAD_FAILED
)) {
4729 sd_bus_error_move(ret_error
, &error
);
4733 r
= strdup_job(reply
, ret_job
);
4740 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
4741 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4742 _cleanup_free_
char *path
= NULL
;
4748 path
= unit_dbus_path_from_name(scope
);
4752 r
= sd_bus_call_method(
4754 "org.freedesktop.systemd1",
4756 "org.freedesktop.systemd1.Scope",
4762 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4763 BUS_ERROR_LOAD_FAILED
,
4764 BUS_ERROR_SCOPE_NOT_RUNNING
))
4767 sd_bus_error_move(ret_error
, &error
);
4774 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWhom whom
, int signo
, sd_bus_error
*error
) {
4777 assert(SIGNAL_VALID(signo
));
4779 return bus_call_method(
4787 whom
== KILL_LEADER
? "main" : "all",
4791 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
4792 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4793 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4794 _cleanup_free_
char *path
= NULL
;
4801 path
= unit_dbus_path_from_name(unit
);
4805 r
= sd_bus_get_property(
4807 "org.freedesktop.systemd1",
4809 "org.freedesktop.systemd1.Unit",
4815 /* systemd might have dropped off momentarily, let's
4816 * not make this an error */
4817 if (bus_error_is_connection(&error
))
4820 /* If the unit is already unloaded then it's not
4822 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
4823 BUS_ERROR_LOAD_FAILED
))
4826 sd_bus_error_move(ret_error
, &error
);
4830 r
= sd_bus_message_read(reply
, "s", &state
);
4834 return !STR_IN_SET(state
, "inactive", "failed");
4837 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
4838 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
4839 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
4845 r
= sd_bus_get_property(
4847 "org.freedesktop.systemd1",
4849 "org.freedesktop.systemd1.Job",
4855 if (bus_error_is_connection(&error
))
4858 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
4861 sd_bus_error_move(ret_error
, &error
);
4865 /* We don't actually care about the state really. The fact
4866 * that we could read the job state is enough for us */