1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "audit-util.h"
12 #include "bus-common-errors.h"
13 #include "bus-error.h"
14 #include "bus-unit-util.h"
16 #include "dirent-util.h"
20 #include "fileio-label.h"
21 #include "format-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 #include "cgroup-util.h"
28 #include "selinux-util.h"
29 #include "sleep-config.h"
32 #include "terminal-util.h"
33 #include "udev-util.h"
34 #include "unit-name.h"
35 #include "user-util.h"
36 #include "utmp-wtmp.h"
38 static int get_sender_session(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, Session
**ret
) {
40 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
45 /* Get client login session. This is not what you are looking for these days,
46 * as apps may instead belong to a user service unit. This includes terminal
47 * emulators and hence command-line apps. */
48 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
52 r
= sd_bus_creds_get_session(creds
, &name
);
58 session
= hashmap_get(m
->sessions
, name
);
66 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
67 "Caller does not belong to any known session");
70 int manager_get_session_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Session
**ret
) {
78 return get_sender_session(m
, message
, error
, ret
);
80 session
= hashmap_get(m
->sessions
, name
);
82 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
88 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
90 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
95 /* Note that we get the owner UID of the session, not the actual client UID here! */
96 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
100 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
106 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
114 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
, "Caller does not belong to any logged in user or lingering user");
117 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
124 if (!uid_is_valid(uid
))
125 return get_sender_user(m
, message
, error
, ret
);
127 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
129 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
, "User ID "UID_FMT
" is not logged in or lingering", uid
);
135 int manager_get_seat_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Seat
**ret
) {
146 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
150 seat
= session
->seat
;
152 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session has no seat.");
154 seat
= hashmap_get(m
->seats
, name
);
156 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
163 static int property_get_idle_hint(
166 const char *interface
,
167 const char *property
,
168 sd_bus_message
*reply
,
170 sd_bus_error
*error
) {
172 Manager
*m
= userdata
;
178 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
181 static int property_get_idle_since_hint(
184 const char *interface
,
185 const char *property
,
186 sd_bus_message
*reply
,
188 sd_bus_error
*error
) {
190 Manager
*m
= userdata
;
191 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
197 manager_get_idle_hint(m
, &t
);
199 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
202 static int property_get_inhibited(
205 const char *interface
,
206 const char *property
,
207 sd_bus_message
*reply
,
209 sd_bus_error
*error
) {
211 Manager
*m
= userdata
;
218 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
220 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
223 static int property_get_preparing(
226 const char *interface
,
227 const char *property
,
228 sd_bus_message
*reply
,
230 sd_bus_error
*error
) {
232 Manager
*m
= userdata
;
239 if (streq(property
, "PreparingForShutdown"))
240 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
242 b
= m
->action_what
& INHIBIT_SLEEP
;
244 return sd_bus_message_append(reply
, "b", b
);
247 static int property_get_scheduled_shutdown(
250 const char *interface
,
251 const char *property
,
252 sd_bus_message
*reply
,
254 sd_bus_error
*error
) {
256 Manager
*m
= userdata
;
263 r
= sd_bus_message_open_container(reply
, 'r', "st");
267 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
271 return sd_bus_message_close_container(reply
);
274 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
275 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
276 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
277 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
279 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
280 _cleanup_free_
char *p
= NULL
;
281 Manager
*m
= userdata
;
289 r
= sd_bus_message_read(message
, "s", &name
);
293 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
297 p
= session_bus_path(session
);
301 return sd_bus_reply_method_return(message
, "o", p
);
304 /* Get login session of a process. This is not what you are looking for these days,
305 * as apps may instead belong to a user service unit. This includes terminal
306 * emulators and hence command-line apps. */
307 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
308 _cleanup_free_
char *p
= NULL
;
309 Session
*session
= NULL
;
310 Manager
*m
= userdata
;
317 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
319 r
= sd_bus_message_read(message
, "u", &pid
);
326 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
330 r
= manager_get_session_by_pid(m
, pid
, &session
);
335 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
, "PID "PID_FMT
" does not belong to any known session", pid
);
338 p
= session_bus_path(session
);
342 return sd_bus_reply_method_return(message
, "o", p
);
345 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
346 _cleanup_free_
char *p
= NULL
;
347 Manager
*m
= userdata
;
355 r
= sd_bus_message_read(message
, "u", &uid
);
359 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
363 p
= user_bus_path(user
);
367 return sd_bus_reply_method_return(message
, "o", p
);
370 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
371 _cleanup_free_
char *p
= NULL
;
372 Manager
*m
= userdata
;
380 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
382 r
= sd_bus_message_read(message
, "u", &pid
);
389 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
393 r
= manager_get_user_by_pid(m
, pid
, &user
);
397 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
398 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
402 p
= user_bus_path(user
);
406 return sd_bus_reply_method_return(message
, "o", p
);
409 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
410 _cleanup_free_
char *p
= NULL
;
411 Manager
*m
= userdata
;
419 r
= sd_bus_message_read(message
, "s", &name
);
423 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
427 p
= seat_bus_path(seat
);
431 return sd_bus_reply_method_return(message
, "o", p
);
434 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
435 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
436 Manager
*m
= userdata
;
444 r
= sd_bus_message_new_method_return(message
, &reply
);
448 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
452 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
453 _cleanup_free_
char *p
= NULL
;
455 p
= session_bus_path(session
);
459 r
= sd_bus_message_append(reply
, "(susso)",
461 (uint32_t) session
->user
->uid
,
463 session
->seat
? session
->seat
->id
: "",
469 r
= sd_bus_message_close_container(reply
);
473 return sd_bus_send(NULL
, reply
, NULL
);
476 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
477 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
478 Manager
*m
= userdata
;
486 r
= sd_bus_message_new_method_return(message
, &reply
);
490 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
494 HASHMAP_FOREACH(user
, m
->users
, i
) {
495 _cleanup_free_
char *p
= NULL
;
497 p
= user_bus_path(user
);
501 r
= sd_bus_message_append(reply
, "(uso)",
502 (uint32_t) user
->uid
,
509 r
= sd_bus_message_close_container(reply
);
513 return sd_bus_send(NULL
, reply
, NULL
);
516 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
517 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
518 Manager
*m
= userdata
;
526 r
= sd_bus_message_new_method_return(message
, &reply
);
530 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
534 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
535 _cleanup_free_
char *p
= NULL
;
537 p
= seat_bus_path(seat
);
541 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
546 r
= sd_bus_message_close_container(reply
);
550 return sd_bus_send(NULL
, reply
, NULL
);
553 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
554 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
555 Manager
*m
= userdata
;
556 Inhibitor
*inhibitor
;
563 r
= sd_bus_message_new_method_return(message
, &reply
);
567 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
571 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
573 r
= sd_bus_message_append(reply
, "(ssssuu)",
574 strempty(inhibit_what_to_string(inhibitor
->what
)),
575 strempty(inhibitor
->who
),
576 strempty(inhibitor
->why
),
577 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
578 (uint32_t) inhibitor
->uid
,
579 (uint32_t) inhibitor
->pid
);
584 r
= sd_bus_message_close_container(reply
);
588 return sd_bus_send(NULL
, reply
, NULL
);
591 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
592 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
593 _cleanup_free_
char *id
= NULL
;
594 Session
*session
= NULL
;
595 uint32_t audit_id
= 0;
596 Manager
*m
= userdata
;
610 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
611 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
613 r
= sd_bus_message_read(message
, "uusssssussbss", &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
, &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
617 if (!uid_is_valid(uid
))
618 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
619 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
620 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
623 t
= _SESSION_TYPE_INVALID
;
625 t
= session_type_from_string(type
);
627 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid session type %s", type
);
631 c
= _SESSION_CLASS_INVALID
;
633 c
= session_class_from_string(class);
635 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid session class %s", class);
638 if (isempty(desktop
))
641 if (!string_is_safe(desktop
))
642 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid desktop string %s", desktop
);
648 seat
= hashmap_get(m
->seats
, cseat
);
650 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", cseat
);
653 if (tty_is_vc(tty
)) {
658 else if (seat
!= m
->seat0
)
659 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "TTY %s is virtual console but seat %s is not seat0", tty
, seat
->id
);
661 v
= vtnr_from_tty(tty
);
663 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot determine VT number from virtual console TTY %s", tty
);
667 else if (vtnr
!= (uint32_t) v
)
668 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified TTY and VT number do not match");
670 } else if (tty_is_console(tty
)) {
674 else if (seat
!= m
->seat0
)
675 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Console TTY specified but seat is not seat0");
678 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Console TTY specified but VT number is not 0");
682 if (seat_has_vts(seat
)) {
683 if (vtnr
<= 0 || vtnr
> 63)
684 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "VT number out of range");
687 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat has no VTs but VT number not 0");
691 if (t
== _SESSION_TYPE_INVALID
) {
692 if (!isempty(display
))
694 else if (!isempty(tty
))
697 t
= SESSION_UNSPECIFIED
;
700 if (c
== _SESSION_CLASS_INVALID
) {
701 if (t
== SESSION_UNSPECIFIED
)
702 c
= SESSION_BACKGROUND
;
708 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
710 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
714 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
719 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
720 * that avoids creating a logind session. */
721 r
= manager_get_user_by_pid(m
, leader
, NULL
);
725 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already running in a session or user slice");
728 * Old gdm and lightdm start the user-session on the same VT as
729 * the greeter session. But they destroy the greeter session
730 * after the user-session and want the user-session to take
731 * over the VT. We need to support this for
732 * backwards-compatibility, so make sure we allow new sessions
733 * on a VT that a greeter is running on. Furthermore, to allow
734 * re-logins, we have to allow a greeter to take over a used VT for
735 * the exact same reasons.
737 if (c
!= SESSION_GREETER
&&
739 vtnr
< m
->seat0
->position_count
&&
740 m
->seat0
->positions
[vtnr
] &&
741 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
742 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
744 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
745 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.", m
->sessions_max
);
747 (void) audit_session_from_pid(leader
, &audit_id
);
748 if (audit_session_is_valid(audit_id
)) {
749 /* Keep our session IDs and the audit session IDs in sync */
751 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
754 /* Wut? There's already a session by this name and we
755 * didn't find it above? Weird, then let's not trust
756 * the audit data and let's better register a new
758 if (hashmap_get(m
->sessions
, id
)) {
759 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
760 audit_id
= AUDIT_SESSION_INVALID
;
769 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
772 } while (hashmap_get(m
->sessions
, id
));
775 r
= manager_add_user_by_uid(m
, uid
, &user
);
779 r
= manager_add_session(m
, id
, &session
);
783 session_set_user(session
, user
);
785 session
->leader
= leader
;
786 session
->audit_id
= audit_id
;
789 session
->remote
= remote
;
790 session
->vtnr
= vtnr
;
793 session
->tty
= strdup(tty
);
800 if (!isempty(display
)) {
801 session
->display
= strdup(display
);
802 if (!session
->display
) {
808 if (!isempty(remote_user
)) {
809 session
->remote_user
= strdup(remote_user
);
810 if (!session
->remote_user
) {
816 if (!isempty(remote_host
)) {
817 session
->remote_host
= strdup(remote_host
);
818 if (!session
->remote_host
) {
824 if (!isempty(service
)) {
825 session
->service
= strdup(service
);
826 if (!session
->service
) {
832 if (!isempty(desktop
)) {
833 session
->desktop
= strdup(desktop
);
834 if (!session
->desktop
) {
841 r
= seat_attach_session(seat
, session
);
846 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
850 r
= session_start(session
, message
);
854 r
= sd_bus_message_exit_container(message
);
858 session
->create_message
= sd_bus_message_ref(message
);
860 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
861 * session_send_create_reply(). */
867 session_add_to_gc_queue(session
);
870 user_add_to_gc_queue(user
);
875 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
876 Manager
*m
= userdata
;
884 r
= sd_bus_message_read(message
, "s", &name
);
888 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
892 r
= session_release(session
);
896 return sd_bus_reply_method_return(message
, NULL
);
899 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
900 Manager
*m
= userdata
;
908 r
= sd_bus_message_read(message
, "s", &name
);
912 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
916 return bus_session_method_activate(message
, session
, error
);
919 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
920 const char *session_name
, *seat_name
;
921 Manager
*m
= userdata
;
929 /* Same as ActivateSession() but refuses to work if
930 * the seat doesn't match */
932 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
936 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
940 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
944 if (session
->seat
!= seat
)
945 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", session_name
, seat_name
);
947 r
= session_activate(session
);
951 return sd_bus_reply_method_return(message
, NULL
);
954 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
955 Manager
*m
= userdata
;
963 r
= sd_bus_message_read(message
, "s", &name
);
967 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
971 return bus_session_method_lock(message
, session
, error
);
974 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
975 Manager
*m
= userdata
;
981 r
= bus_verify_polkit_async(
984 "org.freedesktop.login1.lock-sessions",
993 return 1; /* Will call us back */
995 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
999 return sd_bus_reply_method_return(message
, NULL
);
1002 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1004 Manager
*m
= userdata
;
1011 r
= sd_bus_message_read(message
, "s", &name
);
1015 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1019 return bus_session_method_kill(message
, session
, error
);
1022 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1023 Manager
*m
= userdata
;
1031 r
= sd_bus_message_read(message
, "u", &uid
);
1035 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1039 return bus_user_method_kill(message
, user
, error
);
1042 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1043 Manager
*m
= userdata
;
1051 r
= sd_bus_message_read(message
, "s", &name
);
1055 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1059 return bus_session_method_terminate(message
, session
, error
);
1062 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1063 Manager
*m
= userdata
;
1071 r
= sd_bus_message_read(message
, "u", &uid
);
1075 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1079 return bus_user_method_terminate(message
, user
, error
);
1082 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1083 Manager
*m
= userdata
;
1091 r
= sd_bus_message_read(message
, "s", &name
);
1095 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1099 return bus_seat_method_terminate(message
, seat
, error
);
1102 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1103 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1104 _cleanup_free_
char *cc
= NULL
;
1105 Manager
*m
= userdata
;
1106 int r
, b
, interactive
;
1109 uint32_t uid
, auth_uid
;
1114 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1118 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1119 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1123 if (!uid_is_valid(uid
)) {
1124 /* Note that we get the owner UID of the session or user unit,
1125 * not the actual client UID here! */
1126 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1131 /* owner_uid is racy, so for authorization we must use euid */
1132 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1139 return errno
> 0 ? -errno
: -ENOENT
;
1141 r
= bus_verify_polkit_async(
1144 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1145 "org.freedesktop.login1.set-user-linger",
1149 &m
->polkit_registry
,
1154 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1156 mkdir_p_label("/var/lib/systemd", 0755);
1158 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1162 cc
= cescape(pw
->pw_name
);
1166 path
= strjoina("/var/lib/systemd/linger/", cc
);
1174 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1181 if (r
< 0 && errno
!= ENOENT
)
1184 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1186 user_add_to_gc_queue(u
);
1189 return sd_bus_reply_method_return(message
, NULL
);
1192 static int trigger_device(Manager
*m
, struct udev_device
*d
) {
1193 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*e
= NULL
;
1194 struct udev_list_entry
*first
, *item
;
1199 e
= udev_enumerate_new(m
->udev
);
1204 r
= udev_enumerate_add_match_parent(e
, d
);
1209 r
= udev_enumerate_scan_devices(e
);
1213 first
= udev_enumerate_get_list_entry(e
);
1214 udev_list_entry_foreach(item
, first
) {
1215 _cleanup_free_
char *t
= NULL
;
1218 p
= udev_list_entry_get_name(item
);
1220 t
= strappend(p
, "/uevent");
1224 (void) write_string_file(t
, "change", 0);
1230 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1231 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
1232 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1233 const char *id_for_seat
;
1240 d
= udev_device_new_from_syspath(m
->udev
, sysfs
);
1244 if (!udev_device_has_tag(d
, "seat"))
1247 id_for_seat
= udev_device_get_property_value(d
, "ID_FOR_SEAT");
1251 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1254 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1257 mkdir_p_label("/etc/udev/rules.d", 0755);
1258 r
= write_string_file_atomic_label(file
, rule
);
1262 return trigger_device(m
, d
);
1265 static int flush_devices(Manager
*m
) {
1266 _cleanup_closedir_
DIR *d
;
1270 d
= opendir("/etc/udev/rules.d");
1272 if (errno
!= ENOENT
)
1273 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1277 FOREACH_DIRENT_ALL(de
, d
, break) {
1278 if (!dirent_is_file(de
))
1281 if (!startswith(de
->d_name
, "72-seat-"))
1284 if (!endswith(de
->d_name
, ".rules"))
1287 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1288 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1292 return trigger_device(m
, NULL
);
1295 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1296 const char *sysfs
, *seat
;
1297 Manager
*m
= userdata
;
1303 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1307 if (!path_startswith(sysfs
, "/sys"))
1308 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1310 if (!seat_name_is_valid(seat
))
1311 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat %s is not valid", seat
);
1313 r
= bus_verify_polkit_async(
1316 "org.freedesktop.login1.attach-device",
1320 &m
->polkit_registry
,
1325 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1327 r
= attach_device(m
, seat
, sysfs
);
1331 return sd_bus_reply_method_return(message
, NULL
);
1334 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1335 Manager
*m
= userdata
;
1341 r
= sd_bus_message_read(message
, "b", &interactive
);
1345 r
= bus_verify_polkit_async(
1348 "org.freedesktop.login1.flush-devices",
1352 &m
->polkit_registry
,
1357 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1359 r
= flush_devices(m
);
1363 return sd_bus_reply_method_return(message
, NULL
);
1366 static int have_multiple_sessions(
1375 /* Check for other users' sessions. Greeter sessions do not
1376 * count, and non-login sessions do not count either. */
1377 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1378 if (session
->class == SESSION_USER
&&
1379 session
->user
->uid
!= uid
)
1385 static int bus_manager_log_shutdown(
1387 const char *unit_name
) {
1394 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1395 p
= "MESSAGE=System is powering down";
1396 q
= "SHUTDOWN=power-off";
1397 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1398 p
= "MESSAGE=System is rebooting";
1399 q
= "SHUTDOWN=reboot";
1400 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1401 p
= "MESSAGE=System is halting";
1402 q
= "SHUTDOWN=halt";
1403 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1404 p
= "MESSAGE=System is rebooting with kexec";
1405 q
= "SHUTDOWN=kexec";
1407 p
= "MESSAGE=System is shutting down";
1411 if (isempty(m
->wall_message
))
1412 p
= strjoina(p
, ".");
1414 p
= strjoina(p
, " (", m
->wall_message
, ").");
1416 return log_struct(LOG_NOTICE
,
1417 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1422 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1423 Manager
*m
= userdata
;
1428 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1432 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1437 if (until
<= now(CLOCK_MONOTONIC
))
1440 /* We want to ignore the lid switch for a while after each
1441 * suspend, and after boot-up. Hence let's install a timer for
1442 * this. As long as the event source exists we ignore the lid
1445 if (m
->lid_switch_ignore_event_source
) {
1448 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1455 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1457 r
= sd_event_add_time(
1459 &m
->lid_switch_ignore_event_source
,
1462 lid_switch_ignore_handler
, m
);
1467 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1469 static const char * const signal_name
[_INHIBIT_WHAT_MAX
] = {
1470 [INHIBIT_SHUTDOWN
] = "PrepareForShutdown",
1471 [INHIBIT_SLEEP
] = "PrepareForSleep"
1474 int active
= _active
;
1478 assert(w
< _INHIBIT_WHAT_MAX
);
1479 assert(signal_name
[w
]);
1481 return sd_bus_emit_signal(m
->bus
,
1482 "/org/freedesktop/login1",
1483 "org.freedesktop.login1.Manager",
1489 static int execute_shutdown_or_sleep(
1492 const char *unit_name
,
1493 sd_bus_error
*error
) {
1495 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1502 assert(w
< _INHIBIT_WHAT_MAX
);
1505 if (w
== INHIBIT_SHUTDOWN
)
1506 bus_manager_log_shutdown(m
, unit_name
);
1508 r
= sd_bus_call_method(
1510 "org.freedesktop.systemd1",
1511 "/org/freedesktop/systemd1",
1512 "org.freedesktop.systemd1.Manager",
1516 "ss", unit_name
, "replace-irreversibly");
1520 r
= sd_bus_message_read(reply
, "o", &p
);
1530 m
->action_unit
= unit_name
;
1531 free(m
->action_job
);
1535 /* Make sure the lid switch is ignored for a while */
1536 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1541 /* Tell people that they now may take a lock again */
1542 (void) send_prepare_for(m
, w
, false);
1547 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1549 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1550 Inhibitor
*offending
= NULL
;
1555 if (manager
->action_what
== 0 || manager
->action_job
)
1558 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1559 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1564 (void) get_process_comm(offending
->pid
, &comm
);
1565 u
= uid_to_name(offending
->uid
);
1567 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1568 offending
->uid
, strna(u
),
1569 offending
->pid
, strna(comm
));
1572 /* Actually do the operation */
1573 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1575 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1576 bus_error_message(&error
, r
));
1578 manager
->action_unit
= NULL
;
1579 manager
->action_what
= 0;
1586 static int manager_inhibit_timeout_handler(
1591 Manager
*manager
= userdata
;
1595 assert(manager
->inhibit_timeout_source
== s
);
1597 r
= manager_dispatch_delayed(manager
, true);
1598 return (r
< 0) ? r
: 0;
1601 static int delay_shutdown_or_sleep(
1604 const char *unit_name
) {
1611 assert(w
< _INHIBIT_WHAT_MAX
);
1614 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1616 if (m
->inhibit_timeout_source
) {
1617 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1619 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1621 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1623 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1625 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1626 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1631 m
->action_unit
= unit_name
;
1637 int bus_manager_shutdown_or_sleep_now_or_later(
1639 const char *unit_name
,
1641 sd_bus_error
*error
) {
1643 _cleanup_free_
char *load_state
= NULL
;
1650 assert(w
<= _INHIBIT_WHAT_MAX
);
1651 assert(!m
->action_job
);
1653 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1657 if (!streq(load_state
, "loaded")) {
1658 log_notice("Unit %s is %s, refusing operation.", unit_name
, load_state
);
1662 /* Tell everybody to prepare for shutdown/sleep */
1663 (void) send_prepare_for(m
, w
, true);
1666 m
->inhibit_delay_max
> 0 &&
1667 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1670 /* Shutdown is delayed, keep in mind what we
1671 * want to do, and start a timeout */
1672 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1674 /* Shutdown is not delayed, execute it
1676 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1681 static int verify_shutdown_creds(
1683 sd_bus_message
*message
,
1687 const char *action_multiple_sessions
,
1688 const char *action_ignore_inhibit
,
1689 sd_bus_error
*error
) {
1691 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1692 bool multiple_sessions
, blocked
;
1699 assert(w
<= _INHIBIT_WHAT_MAX
);
1701 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1705 r
= sd_bus_creds_get_euid(creds
, &uid
);
1709 r
= have_multiple_sessions(m
, uid
);
1713 multiple_sessions
= r
> 0;
1714 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1716 if (multiple_sessions
&& action_multiple_sessions
) {
1717 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1721 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1724 if (blocked
&& action_ignore_inhibit
) {
1725 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1729 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1732 if (!multiple_sessions
&& !blocked
&& action
) {
1733 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1737 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1743 static int method_do_shutdown_or_sleep(
1745 sd_bus_message
*message
,
1746 const char *unit_name
,
1749 const char *action_multiple_sessions
,
1750 const char *action_ignore_inhibit
,
1751 const char *sleep_verb
,
1752 sd_bus_error
*error
) {
1760 assert(w
<= _INHIBIT_WHAT_MAX
);
1762 r
= sd_bus_message_read(message
, "b", &interactive
);
1766 /* Don't allow multiple jobs being executed at the same time */
1768 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
, "There's already a shutdown or sleep operation in progress");
1771 r
= can_sleep(sleep_verb
);
1773 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1774 "Not enough swap space for hibernation");
1776 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1777 "Sleep verb \"%s\" not supported", sleep_verb
);
1782 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1783 action_ignore_inhibit
, error
);
1787 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1791 return sd_bus_reply_method_return(message
, NULL
);
1794 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1795 Manager
*m
= userdata
;
1797 return method_do_shutdown_or_sleep(
1799 SPECIAL_POWEROFF_TARGET
,
1801 "org.freedesktop.login1.power-off",
1802 "org.freedesktop.login1.power-off-multiple-sessions",
1803 "org.freedesktop.login1.power-off-ignore-inhibit",
1808 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1809 Manager
*m
= userdata
;
1811 return method_do_shutdown_or_sleep(
1813 SPECIAL_REBOOT_TARGET
,
1815 "org.freedesktop.login1.reboot",
1816 "org.freedesktop.login1.reboot-multiple-sessions",
1817 "org.freedesktop.login1.reboot-ignore-inhibit",
1822 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1823 Manager
*m
= userdata
;
1825 return method_do_shutdown_or_sleep(
1827 SPECIAL_HALT_TARGET
,
1829 "org.freedesktop.login1.halt",
1830 "org.freedesktop.login1.halt-multiple-sessions",
1831 "org.freedesktop.login1.halt-ignore-inhibit",
1836 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1837 Manager
*m
= userdata
;
1839 return method_do_shutdown_or_sleep(
1841 SPECIAL_SUSPEND_TARGET
,
1843 "org.freedesktop.login1.suspend",
1844 "org.freedesktop.login1.suspend-multiple-sessions",
1845 "org.freedesktop.login1.suspend-ignore-inhibit",
1850 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1851 Manager
*m
= userdata
;
1853 return method_do_shutdown_or_sleep(
1855 SPECIAL_HIBERNATE_TARGET
,
1857 "org.freedesktop.login1.hibernate",
1858 "org.freedesktop.login1.hibernate-multiple-sessions",
1859 "org.freedesktop.login1.hibernate-ignore-inhibit",
1864 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1865 Manager
*m
= userdata
;
1867 return method_do_shutdown_or_sleep(
1869 SPECIAL_HYBRID_SLEEP_TARGET
,
1871 "org.freedesktop.login1.hibernate",
1872 "org.freedesktop.login1.hibernate-multiple-sessions",
1873 "org.freedesktop.login1.hibernate-ignore-inhibit",
1878 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1879 Manager
*m
= userdata
;
1881 return method_do_shutdown_or_sleep(
1883 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
1885 "org.freedesktop.login1.hibernate",
1886 "org.freedesktop.login1.hibernate-multiple-sessions",
1887 "org.freedesktop.login1.hibernate-ignore-inhibit",
1892 static int nologin_timeout_handler(
1897 Manager
*m
= userdata
;
1899 log_info("Creating /run/nologin, blocking further logins...");
1902 create_shutdown_run_nologin_or_warn() >= 0;
1907 static int update_schedule_file(Manager
*m
) {
1908 _cleanup_free_
char *temp_path
= NULL
;
1909 _cleanup_fclose_
FILE *f
= NULL
;
1914 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
1916 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
1918 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
1920 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
1922 (void) fchmod(fileno(f
), 0644);
1928 m
->scheduled_shutdown_timeout
,
1929 m
->enable_wall_messages
,
1930 m
->scheduled_shutdown_type
);
1932 if (!isempty(m
->wall_message
)) {
1933 _cleanup_free_
char *t
;
1935 t
= cescape(m
->wall_message
);
1941 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
1944 r
= fflush_and_check(f
);
1948 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
1956 (void) unlink(temp_path
);
1957 (void) unlink("/run/systemd/shutdown/scheduled");
1959 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
1962 static void reset_scheduled_shutdown(Manager
*m
) {
1965 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
1966 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
1967 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
1969 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
1970 m
->scheduled_shutdown_timeout
= 0;
1971 m
->shutdown_dry_run
= false;
1973 if (m
->unlink_nologin
) {
1974 (void) unlink_or_warn("/run/nologin");
1975 m
->unlink_nologin
= false;
1978 (void) unlink("/run/systemd/shutdown/scheduled");
1981 static int manager_scheduled_shutdown_handler(
1986 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1987 Manager
*m
= userdata
;
1993 if (isempty(m
->scheduled_shutdown_type
))
1996 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
1997 target
= SPECIAL_POWEROFF_TARGET
;
1998 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
1999 target
= SPECIAL_REBOOT_TARGET
;
2000 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2001 target
= SPECIAL_HALT_TARGET
;
2003 assert_not_reached("unexpected shutdown type");
2005 /* Don't allow multiple jobs being executed at the same time */
2006 if (m
->action_what
) {
2008 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2012 if (m
->shutdown_dry_run
) {
2013 /* We do not process delay inhibitors here. Otherwise, we
2014 * would have to be considered "in progress" (like the check
2015 * above) for some seconds after our admin has seen the final
2018 bus_manager_log_shutdown(m
, target
);
2019 log_info("Running in dry run, suppressing action.");
2020 reset_scheduled_shutdown(m
);
2025 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2027 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2034 reset_scheduled_shutdown(m
);
2038 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2039 Manager
*m
= userdata
;
2040 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2041 const char *action_multiple_sessions
= NULL
;
2042 const char *action_ignore_inhibit
= NULL
;
2043 const char *action
= NULL
;
2047 bool dry_run
= false;
2052 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2056 if (startswith(type
, "dry-")) {
2061 if (streq(type
, "poweroff")) {
2062 action
= "org.freedesktop.login1.power-off";
2063 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2064 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2065 } else if (streq(type
, "reboot")) {
2066 action
= "org.freedesktop.login1.reboot";
2067 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2068 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2069 } else if (streq(type
, "halt")) {
2070 action
= "org.freedesktop.login1.halt";
2071 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2072 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2074 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2076 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2077 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2081 if (m
->scheduled_shutdown_timeout_source
) {
2082 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2084 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2086 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2088 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2090 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2091 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2093 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2096 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2098 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2102 m
->shutdown_dry_run
= dry_run
;
2104 if (m
->nologin_timeout_source
) {
2105 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2107 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2109 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2111 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2113 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2114 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2116 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2119 m
->scheduled_shutdown_timeout
= elapse
;
2121 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2123 const char *tty
= NULL
;
2125 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2126 (void) sd_bus_creds_get_tty(creds
, &tty
);
2128 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2130 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2135 r
= manager_setup_wall_message_timer(m
);
2139 r
= update_schedule_file(m
);
2143 return sd_bus_reply_method_return(message
, NULL
);
2146 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2147 Manager
*m
= userdata
;
2153 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2154 reset_scheduled_shutdown(m
);
2156 if (cancelled
&& m
->enable_wall_messages
) {
2157 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2158 const char *tty
= NULL
;
2162 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2164 (void) sd_bus_creds_get_uid(creds
, &uid
);
2165 (void) sd_bus_creds_get_tty(creds
, &tty
);
2168 utmp_wall("The system shutdown has been cancelled",
2169 uid_to_name(uid
), tty
, logind_wall_tty_filter
, m
);
2172 return sd_bus_reply_method_return(message
, "b", cancelled
);
2175 static int method_can_shutdown_or_sleep(
2177 sd_bus_message
*message
,
2180 const char *action_multiple_sessions
,
2181 const char *action_ignore_inhibit
,
2182 const char *sleep_verb
,
2183 sd_bus_error
*error
) {
2185 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2186 HandleAction handle
;
2187 bool multiple_sessions
, challenge
, blocked
;
2188 const char *result
= NULL
;
2195 assert(w
<= _INHIBIT_WHAT_MAX
);
2197 assert(action_multiple_sessions
);
2198 assert(action_ignore_inhibit
);
2201 r
= can_sleep(sleep_verb
);
2202 if (IN_SET(r
, 0, -ENOSPC
))
2203 return sd_bus_reply_method_return(message
, "s", "na");
2208 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2212 r
= sd_bus_creds_get_euid(creds
, &uid
);
2216 r
= have_multiple_sessions(m
, uid
);
2220 multiple_sessions
= r
> 0;
2221 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2223 handle
= handle_action_from_string(sleep_verb
);
2227 target
= manager_target_for_action(handle
);
2229 _cleanup_free_
char *load_state
= NULL
;
2231 r
= unit_load_state(m
->bus
, target
, &load_state
);
2235 if (!streq(load_state
, "loaded")) {
2242 if (multiple_sessions
) {
2243 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2250 result
= "challenge";
2256 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2260 if (r
> 0 && !result
)
2262 else if (challenge
&& (!result
|| streq(result
, "yes")))
2263 result
= "challenge";
2268 if (!multiple_sessions
&& !blocked
) {
2269 /* If neither inhibit nor multiple sessions
2270 * apply then just check the normal policy */
2272 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2279 result
= "challenge";
2285 return sd_bus_reply_method_return(message
, "s", result
);
2288 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2289 Manager
*m
= userdata
;
2291 return method_can_shutdown_or_sleep(
2294 "org.freedesktop.login1.power-off",
2295 "org.freedesktop.login1.power-off-multiple-sessions",
2296 "org.freedesktop.login1.power-off-ignore-inhibit",
2301 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2302 Manager
*m
= userdata
;
2304 return method_can_shutdown_or_sleep(
2307 "org.freedesktop.login1.reboot",
2308 "org.freedesktop.login1.reboot-multiple-sessions",
2309 "org.freedesktop.login1.reboot-ignore-inhibit",
2314 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2315 Manager
*m
= userdata
;
2317 return method_can_shutdown_or_sleep(
2320 "org.freedesktop.login1.halt",
2321 "org.freedesktop.login1.halt-multiple-sessions",
2322 "org.freedesktop.login1.halt-ignore-inhibit",
2327 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2328 Manager
*m
= userdata
;
2330 return method_can_shutdown_or_sleep(
2333 "org.freedesktop.login1.suspend",
2334 "org.freedesktop.login1.suspend-multiple-sessions",
2335 "org.freedesktop.login1.suspend-ignore-inhibit",
2340 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2341 Manager
*m
= userdata
;
2343 return method_can_shutdown_or_sleep(
2346 "org.freedesktop.login1.hibernate",
2347 "org.freedesktop.login1.hibernate-multiple-sessions",
2348 "org.freedesktop.login1.hibernate-ignore-inhibit",
2353 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2354 Manager
*m
= userdata
;
2356 return method_can_shutdown_or_sleep(
2359 "org.freedesktop.login1.hibernate",
2360 "org.freedesktop.login1.hibernate-multiple-sessions",
2361 "org.freedesktop.login1.hibernate-ignore-inhibit",
2366 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2367 Manager
*m
= userdata
;
2369 return method_can_shutdown_or_sleep(
2372 "org.freedesktop.login1.hibernate",
2373 "org.freedesktop.login1.hibernate-multiple-sessions",
2374 "org.freedesktop.login1.hibernate-ignore-inhibit",
2375 "suspend-then-hibernate",
2379 static int property_get_reboot_to_firmware_setup(
2382 const char *interface
,
2383 const char *property
,
2384 sd_bus_message
*reply
,
2386 sd_bus_error
*error
) {
2393 r
= efi_get_reboot_to_firmware();
2394 if (r
< 0 && r
!= -EOPNOTSUPP
)
2395 log_warning_errno(r
, "Failed to determine reboot-to-firmware state: %m");
2397 return sd_bus_message_append(reply
, "b", r
> 0);
2400 static int method_set_reboot_to_firmware_setup(
2401 sd_bus_message
*message
,
2403 sd_bus_error
*error
) {
2406 Manager
*m
= userdata
;
2411 r
= sd_bus_message_read(message
, "b", &b
);
2415 r
= bus_verify_polkit_async(message
,
2417 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2421 &m
->polkit_registry
,
2426 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2428 r
= efi_set_reboot_to_firmware(b
);
2432 return sd_bus_reply_method_return(message
, NULL
);
2435 static int method_can_reboot_to_firmware_setup(
2436 sd_bus_message
*message
,
2438 sd_bus_error
*error
) {
2443 Manager
*m
= userdata
;
2448 r
= efi_reboot_to_firmware_supported();
2450 if (r
!= -EOPNOTSUPP
)
2451 log_warning_errno(errno
, "Failed to determine whether reboot to firmware is supported: %m");
2453 return sd_bus_reply_method_return(message
, "s", "na");
2456 r
= bus_test_polkit(message
,
2458 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2469 result
= "challenge";
2473 return sd_bus_reply_method_return(message
, "s", result
);
2476 static int method_set_wall_message(
2477 sd_bus_message
*message
,
2479 sd_bus_error
*error
) {
2482 Manager
*m
= userdata
;
2484 unsigned enable_wall_messages
;
2489 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
2493 r
= bus_verify_polkit_async(message
,
2495 "org.freedesktop.login1.set-wall-message",
2499 &m
->polkit_registry
,
2504 return 1; /* Will call us back */
2506 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
2510 m
->enable_wall_messages
= enable_wall_messages
;
2512 return sd_bus_reply_method_return(message
, NULL
);
2515 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2516 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2517 const char *who
, *why
, *what
, *mode
;
2518 _cleanup_free_
char *id
= NULL
;
2519 _cleanup_close_
int fifo_fd
= -1;
2520 Manager
*m
= userdata
;
2521 Inhibitor
*i
= NULL
;
2531 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
2535 w
= inhibit_what_from_string(what
);
2537 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid what specification %s", what
);
2539 mm
= inhibit_mode_from_string(mode
);
2541 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid mode specification %s", mode
);
2543 /* Delay is only supported for shutdown/sleep */
2544 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
2545 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delay inhibitors only supported for shutdown and sleep");
2547 /* Don't allow taking delay locks while we are already
2548 * executing the operation. We shouldn't create the impression
2549 * that the lock was successful if the machine is about to go
2550 * down/suspend any moment. */
2551 if (m
->action_what
& w
)
2552 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
, "The operation inhibition has been requested for is already running");
2554 r
= bus_verify_polkit_async(
2557 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2558 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2559 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
2560 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
2561 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2562 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2563 "org.freedesktop.login1.inhibit-handle-lid-switch",
2567 &m
->polkit_registry
,
2572 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2574 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
2578 r
= sd_bus_creds_get_euid(creds
, &uid
);
2582 r
= sd_bus_creds_get_pid(creds
, &pid
);
2586 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
2587 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.", m
->inhibitors_max
);
2592 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
2595 } while (hashmap_get(m
->inhibitors
, id
));
2597 r
= manager_add_inhibitor(m
, id
, &i
);
2605 i
->why
= strdup(why
);
2606 i
->who
= strdup(who
);
2608 if (!i
->why
|| !i
->who
) {
2613 fifo_fd
= inhibitor_create_fifo(i
);
2621 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
2630 const sd_bus_vtable manager_vtable
[] = {
2631 SD_BUS_VTABLE_START(0),
2633 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
2634 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
2636 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
2637 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
2638 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
2639 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
2640 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
2641 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2642 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2643 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2644 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2645 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2646 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2647 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2648 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2649 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2650 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
2651 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
2652 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
2653 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
2654 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
2655 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
2656 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
2657 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
2658 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
2659 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
2660 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
2661 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
2662 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2663 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
2664 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2665 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
2666 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
2668 SD_BUS_METHOD("GetSession", "s", "o", method_get_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2669 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
2670 SD_BUS_METHOD("GetUser", "u", "o", method_get_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2671 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
2672 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2673 SD_BUS_METHOD("ListSessions", NULL
, "a(susso)", method_list_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2674 SD_BUS_METHOD("ListUsers", NULL
, "a(uso)", method_list_users
, SD_BUS_VTABLE_UNPRIVILEGED
),
2675 SD_BUS_METHOD("ListSeats", NULL
, "a(so)", method_list_seats
, SD_BUS_VTABLE_UNPRIVILEGED
),
2676 SD_BUS_METHOD("ListInhibitors", NULL
, "a(ssssuu)", method_list_inhibitors
, SD_BUS_VTABLE_UNPRIVILEGED
),
2677 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session
, 0),
2678 SD_BUS_METHOD("ReleaseSession", "s", NULL
, method_release_session
, 0),
2679 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2680 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL
, method_activate_session_on_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2681 SD_BUS_METHOD("LockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2682 SD_BUS_METHOD("UnlockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2683 SD_BUS_METHOD("LockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2684 SD_BUS_METHOD("UnlockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2685 SD_BUS_METHOD("KillSession", "ssi", NULL
, method_kill_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2686 SD_BUS_METHOD("KillUser", "ui", NULL
, method_kill_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2687 SD_BUS_METHOD("TerminateSession", "s", NULL
, method_terminate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2688 SD_BUS_METHOD("TerminateUser", "u", NULL
, method_terminate_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2689 SD_BUS_METHOD("TerminateSeat", "s", NULL
, method_terminate_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2690 SD_BUS_METHOD("SetUserLinger", "ubb", NULL
, method_set_user_linger
, SD_BUS_VTABLE_UNPRIVILEGED
),
2691 SD_BUS_METHOD("AttachDevice", "ssb", NULL
, method_attach_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
2692 SD_BUS_METHOD("FlushDevices", "b", NULL
, method_flush_devices
, SD_BUS_VTABLE_UNPRIVILEGED
),
2693 SD_BUS_METHOD("PowerOff", "b", NULL
, method_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
2694 SD_BUS_METHOD("Reboot", "b", NULL
, method_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
2695 SD_BUS_METHOD("Halt", "b", NULL
, method_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
2696 SD_BUS_METHOD("Suspend", "b", NULL
, method_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
2697 SD_BUS_METHOD("Hibernate", "b", NULL
, method_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2698 SD_BUS_METHOD("HybridSleep", "b", NULL
, method_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
2699 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL
, method_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2700 SD_BUS_METHOD("CanPowerOff", NULL
, "s", method_can_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
2701 SD_BUS_METHOD("CanReboot", NULL
, "s", method_can_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
2702 SD_BUS_METHOD("CanHalt", NULL
, "s", method_can_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
2703 SD_BUS_METHOD("CanSuspend", NULL
, "s", method_can_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
2704 SD_BUS_METHOD("CanHibernate", NULL
, "s", method_can_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2705 SD_BUS_METHOD("CanHybridSleep", NULL
, "s", method_can_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
2706 SD_BUS_METHOD("CanSuspendThenHibernate", NULL
, "s", method_can_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2707 SD_BUS_METHOD("ScheduleShutdown", "st", NULL
, method_schedule_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
2708 SD_BUS_METHOD("CancelScheduledShutdown", NULL
, "b", method_cancel_scheduled_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
2709 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit
, SD_BUS_VTABLE_UNPRIVILEGED
),
2710 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL
, "s", method_can_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
2711 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL
, method_set_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
2712 SD_BUS_METHOD("SetWallMessage", "sb", NULL
, method_set_wall_message
, SD_BUS_VTABLE_UNPRIVILEGED
),
2714 SD_BUS_SIGNAL("SessionNew", "so", 0),
2715 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2716 SD_BUS_SIGNAL("UserNew", "uo", 0),
2717 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2718 SD_BUS_SIGNAL("SeatNew", "so", 0),
2719 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2720 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2721 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2726 static int session_jobs_reply(Session
*s
, const char *unit
, const char *result
) {
2735 if (streq(result
, "done"))
2736 r
= session_send_create_reply(s
, NULL
);
2738 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
2740 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
2741 r
= session_send_create_reply(s
, &e
);
2747 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2748 const char *path
, *result
, *unit
;
2749 Manager
*m
= userdata
;
2758 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
2760 bus_log_parse_error(r
);
2764 if (m
->action_job
&& streq(m
->action_job
, path
)) {
2765 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
2767 /* Tell people that they now may take a lock again */
2768 (void) send_prepare_for(m
, m
->action_what
, false);
2770 m
->action_job
= mfree(m
->action_job
);
2771 m
->action_unit
= NULL
;
2776 session
= hashmap_get(m
->session_units
, unit
);
2777 if (session
&& streq_ptr(path
, session
->scope_job
)) {
2778 session
->scope_job
= mfree(session
->scope_job
);
2779 session_jobs_reply(session
, unit
, result
);
2781 session_save(session
);
2782 user_save(session
->user
);
2783 session_add_to_gc_queue(session
);
2786 user
= hashmap_get(m
->user_units
, unit
);
2788 (streq_ptr(path
, user
->service_job
) ||
2789 streq_ptr(path
, user
->slice_job
))) {
2791 if (streq_ptr(path
, user
->service_job
))
2792 user
->service_job
= mfree(user
->service_job
);
2794 if (streq_ptr(path
, user
->slice_job
))
2795 user
->slice_job
= mfree(user
->slice_job
);
2797 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
2798 session_jobs_reply(session
, unit
, result
);
2801 user_add_to_gc_queue(user
);
2807 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2808 const char *path
, *unit
;
2809 Manager
*m
= userdata
;
2817 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
2819 bus_log_parse_error(r
);
2823 session
= hashmap_get(m
->session_units
, unit
);
2825 session_add_to_gc_queue(session
);
2827 user
= hashmap_get(m
->user_units
, unit
);
2829 user_add_to_gc_queue(user
);
2834 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2835 _cleanup_free_
char *unit
= NULL
;
2836 Manager
*m
= userdata
;
2845 path
= sd_bus_message_get_path(message
);
2849 r
= unit_name_from_dbus_path(path
, &unit
);
2850 if (r
== -EINVAL
) /* not a unit */
2857 session
= hashmap_get(m
->session_units
, unit
);
2859 session_add_to_gc_queue(session
);
2861 user
= hashmap_get(m
->user_units
, unit
);
2863 user_add_to_gc_queue(user
);
2868 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2869 Manager
*m
= userdata
;
2877 r
= sd_bus_message_read(message
, "b", &b
);
2879 bus_log_parse_error(r
);
2886 /* systemd finished reloading, let's recheck all our sessions */
2887 log_debug("System manager has been reloaded, rechecking sessions...");
2889 HASHMAP_FOREACH(session
, m
->sessions
, i
)
2890 session_add_to_gc_queue(session
);
2895 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
2900 l
= strv_from_stdarg_alloca(property
);
2902 return sd_bus_emit_properties_changed_strv(
2904 "/org/freedesktop/login1",
2905 "org.freedesktop.login1.Manager",
2909 static int strdup_job(sd_bus_message
*reply
, char **job
) {
2914 r
= sd_bus_message_read(reply
, "o", &j
);
2926 int manager_start_scope(
2931 const char *description
,
2934 sd_bus_message
*more_properties
,
2935 sd_bus_error
*error
,
2938 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2946 r
= sd_bus_message_new_method_call(
2949 "org.freedesktop.systemd1",
2950 "/org/freedesktop/systemd1",
2951 "org.freedesktop.systemd1.Manager",
2952 "StartTransientUnit");
2956 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
2960 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
2964 if (!isempty(slice
)) {
2965 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
2970 if (!isempty(description
)) {
2971 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
2976 if (!isempty(after
)) {
2977 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, after
);
2982 if (!isempty(after2
)) {
2983 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, after2
);
2988 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
2990 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
2994 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
2998 /* disable TasksMax= for the session scope, rely on the slice setting for it */
2999 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
3001 return bus_log_create_error(r
);
3003 if (more_properties
) {
3004 /* If TasksMax also appears here, it will overwrite the default value set above */
3005 r
= sd_bus_message_copy(m
, more_properties
, true);
3010 r
= sd_bus_message_close_container(m
);
3014 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3018 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3022 return strdup_job(reply
, job
);
3025 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3026 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3033 r
= sd_bus_call_method(
3035 "org.freedesktop.systemd1",
3036 "/org/freedesktop/systemd1",
3037 "org.freedesktop.systemd1.Manager",
3041 "ss", unit
, "replace");
3045 return strdup_job(reply
, job
);
3048 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3049 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3056 r
= sd_bus_call_method(
3058 "org.freedesktop.systemd1",
3059 "/org/freedesktop/systemd1",
3060 "org.freedesktop.systemd1.Manager",
3064 "ss", unit
, "fail");
3066 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3067 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
3070 sd_bus_error_free(error
);
3077 return strdup_job(reply
, job
);
3080 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*error
) {
3081 _cleanup_free_
char *path
= NULL
;
3087 path
= unit_dbus_path_from_name(scope
);
3091 r
= sd_bus_call_method(
3093 "org.freedesktop.systemd1",
3095 "org.freedesktop.systemd1.Scope",
3101 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3102 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
) ||
3103 sd_bus_error_has_name(error
, BUS_ERROR_SCOPE_NOT_RUNNING
)) {
3104 sd_bus_error_free(error
);
3114 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
3118 return sd_bus_call_method(
3120 "org.freedesktop.systemd1",
3121 "/org/freedesktop/systemd1",
3122 "org.freedesktop.systemd1.Manager",
3126 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
3129 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
3130 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3131 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3132 _cleanup_free_
char *path
= NULL
;
3139 path
= unit_dbus_path_from_name(unit
);
3143 r
= sd_bus_get_property(
3145 "org.freedesktop.systemd1",
3147 "org.freedesktop.systemd1.Unit",
3153 /* systemd might have droppped off momentarily, let's
3154 * not make this an error */
3155 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3156 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3159 /* If the unit is already unloaded then it's not
3161 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3162 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
3168 r
= sd_bus_message_read(reply
, "s", &state
);
3172 return !streq(state
, "inactive") && !streq(state
, "failed");
3175 int manager_job_is_active(Manager
*manager
, const char *path
) {
3176 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3177 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3183 r
= sd_bus_get_property(
3185 "org.freedesktop.systemd1",
3187 "org.freedesktop.systemd1.Job",
3193 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3194 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3197 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
3203 /* We don't actually care about the state really. The fact
3204 * that we could read the job state is enough for us */