1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
13 #include "sd-messages.h"
15 #include "alloc-util.h"
16 #include "audit-util.h"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
19 #include "bus-unit-util.h"
21 #include "dirent-util.h"
25 #include "fileio-label.h"
26 #include "format-util.h"
30 #include "path-util.h"
31 #include "process-util.h"
32 #include "cgroup-util.h"
33 #include "selinux-util.h"
34 #include "sleep-config.h"
37 #include "terminal-util.h"
38 #include "udev-util.h"
39 #include "unit-name.h"
40 #include "user-util.h"
41 #include "utmp-wtmp.h"
43 static int get_sender_session(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, Session
**ret
) {
45 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
50 /* Get client login session. This is not what you are looking for these days,
51 * as apps may instead belong to a user service unit. This includes terminal
52 * emulators and hence command-line apps. */
53 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
57 r
= sd_bus_creds_get_session(creds
, &name
);
63 session
= hashmap_get(m
->sessions
, name
);
71 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
72 "Caller does not belong to any known session");
75 int manager_get_session_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Session
**ret
) {
83 return get_sender_session(m
, message
, error
, ret
);
85 session
= hashmap_get(m
->sessions
, name
);
87 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
93 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
95 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
100 /* Note that we get the owner UID of the session, not the actual client UID here! */
101 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
105 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
111 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
119 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
, "Caller does not belong to any logged in user or lingering user");
122 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
129 if (!uid_is_valid(uid
))
130 return get_sender_user(m
, message
, error
, ret
);
132 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
134 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
, "User ID "UID_FMT
" is not logged in or lingering", uid
);
140 int manager_get_seat_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Seat
**ret
) {
151 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
155 seat
= session
->seat
;
157 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session has no seat.");
159 seat
= hashmap_get(m
->seats
, name
);
161 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
168 static int property_get_idle_hint(
171 const char *interface
,
172 const char *property
,
173 sd_bus_message
*reply
,
175 sd_bus_error
*error
) {
177 Manager
*m
= userdata
;
183 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
186 static int property_get_idle_since_hint(
189 const char *interface
,
190 const char *property
,
191 sd_bus_message
*reply
,
193 sd_bus_error
*error
) {
195 Manager
*m
= userdata
;
196 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
202 manager_get_idle_hint(m
, &t
);
204 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
207 static int property_get_inhibited(
210 const char *interface
,
211 const char *property
,
212 sd_bus_message
*reply
,
214 sd_bus_error
*error
) {
216 Manager
*m
= userdata
;
223 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
225 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
228 static int property_get_preparing(
231 const char *interface
,
232 const char *property
,
233 sd_bus_message
*reply
,
235 sd_bus_error
*error
) {
237 Manager
*m
= userdata
;
244 if (streq(property
, "PreparingForShutdown"))
245 b
= !!(m
->action_what
& INHIBIT_SHUTDOWN
);
247 b
= !!(m
->action_what
& INHIBIT_SLEEP
);
249 return sd_bus_message_append(reply
, "b", b
);
252 static int property_get_scheduled_shutdown(
255 const char *interface
,
256 const char *property
,
257 sd_bus_message
*reply
,
259 sd_bus_error
*error
) {
261 Manager
*m
= userdata
;
268 r
= sd_bus_message_open_container(reply
, 'r', "st");
272 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
276 return sd_bus_message_close_container(reply
);
279 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
280 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
281 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
282 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
284 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
285 _cleanup_free_
char *p
= NULL
;
286 Manager
*m
= userdata
;
294 r
= sd_bus_message_read(message
, "s", &name
);
298 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
302 p
= session_bus_path(session
);
306 return sd_bus_reply_method_return(message
, "o", p
);
309 /* Get login session of a process. This is not what you are looking for these days,
310 * as apps may instead belong to a user service unit. This includes terminal
311 * emulators and hence command-line apps. */
312 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
313 _cleanup_free_
char *p
= NULL
;
314 Session
*session
= NULL
;
315 Manager
*m
= userdata
;
322 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
324 r
= sd_bus_message_read(message
, "u", &pid
);
331 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
335 r
= manager_get_session_by_pid(m
, pid
, &session
);
340 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
, "PID "PID_FMT
" does not belong to any known session", pid
);
343 p
= session_bus_path(session
);
347 return sd_bus_reply_method_return(message
, "o", p
);
350 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
351 _cleanup_free_
char *p
= NULL
;
352 Manager
*m
= userdata
;
360 r
= sd_bus_message_read(message
, "u", &uid
);
364 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
368 p
= user_bus_path(user
);
372 return sd_bus_reply_method_return(message
, "o", p
);
375 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
376 _cleanup_free_
char *p
= NULL
;
377 Manager
*m
= userdata
;
385 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
387 r
= sd_bus_message_read(message
, "u", &pid
);
394 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
398 r
= manager_get_user_by_pid(m
, pid
, &user
);
402 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
403 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
407 p
= user_bus_path(user
);
411 return sd_bus_reply_method_return(message
, "o", p
);
414 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
415 _cleanup_free_
char *p
= NULL
;
416 Manager
*m
= userdata
;
424 r
= sd_bus_message_read(message
, "s", &name
);
428 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
432 p
= seat_bus_path(seat
);
436 return sd_bus_reply_method_return(message
, "o", p
);
439 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
440 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
441 Manager
*m
= userdata
;
449 r
= sd_bus_message_new_method_return(message
, &reply
);
453 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
457 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
458 _cleanup_free_
char *p
= NULL
;
460 p
= session_bus_path(session
);
464 r
= sd_bus_message_append(reply
, "(susso)",
466 (uint32_t) session
->user
->uid
,
468 session
->seat
? session
->seat
->id
: "",
474 r
= sd_bus_message_close_container(reply
);
478 return sd_bus_send(NULL
, reply
, NULL
);
481 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
482 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
483 Manager
*m
= userdata
;
491 r
= sd_bus_message_new_method_return(message
, &reply
);
495 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
499 HASHMAP_FOREACH(user
, m
->users
, i
) {
500 _cleanup_free_
char *p
= NULL
;
502 p
= user_bus_path(user
);
506 r
= sd_bus_message_append(reply
, "(uso)",
507 (uint32_t) user
->uid
,
514 r
= sd_bus_message_close_container(reply
);
518 return sd_bus_send(NULL
, reply
, NULL
);
521 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
522 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
523 Manager
*m
= userdata
;
531 r
= sd_bus_message_new_method_return(message
, &reply
);
535 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
539 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
540 _cleanup_free_
char *p
= NULL
;
542 p
= seat_bus_path(seat
);
546 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
551 r
= sd_bus_message_close_container(reply
);
555 return sd_bus_send(NULL
, reply
, NULL
);
558 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
559 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
560 Manager
*m
= userdata
;
561 Inhibitor
*inhibitor
;
568 r
= sd_bus_message_new_method_return(message
, &reply
);
572 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
576 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
578 r
= sd_bus_message_append(reply
, "(ssssuu)",
579 strempty(inhibit_what_to_string(inhibitor
->what
)),
580 strempty(inhibitor
->who
),
581 strempty(inhibitor
->why
),
582 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
583 (uint32_t) inhibitor
->uid
,
584 (uint32_t) inhibitor
->pid
);
589 r
= sd_bus_message_close_container(reply
);
593 return sd_bus_send(NULL
, reply
, NULL
);
596 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
597 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
598 _cleanup_free_
char *id
= NULL
;
599 Session
*session
= NULL
;
600 uint32_t audit_id
= 0;
601 Manager
*m
= userdata
;
615 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
616 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
618 r
= sd_bus_message_read(message
, "uusssssussbss", &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
, &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
622 if (!uid_is_valid(uid
))
623 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
624 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
625 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
628 t
= _SESSION_TYPE_INVALID
;
630 t
= session_type_from_string(type
);
632 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid session type %s", type
);
636 c
= _SESSION_CLASS_INVALID
;
638 c
= session_class_from_string(class);
640 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid session class %s", class);
643 if (isempty(desktop
))
646 if (!string_is_safe(desktop
))
647 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid desktop string %s", desktop
);
653 seat
= hashmap_get(m
->seats
, cseat
);
655 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", cseat
);
658 if (tty_is_vc(tty
)) {
663 else if (seat
!= m
->seat0
)
664 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
);
666 v
= vtnr_from_tty(tty
);
668 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot determine VT number from virtual console TTY %s", tty
);
672 else if (vtnr
!= (uint32_t) v
)
673 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified TTY and VT number do not match");
675 } else if (tty_is_console(tty
)) {
679 else if (seat
!= m
->seat0
)
680 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Console TTY specified but seat is not seat0");
683 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Console TTY specified but VT number is not 0");
687 if (seat_has_vts(seat
)) {
688 if (vtnr
<= 0 || vtnr
> 63)
689 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "VT number out of range");
692 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat has no VTs but VT number not 0");
696 if (t
== _SESSION_TYPE_INVALID
) {
697 if (!isempty(display
))
699 else if (!isempty(tty
))
702 t
= SESSION_UNSPECIFIED
;
705 if (c
== _SESSION_CLASS_INVALID
) {
706 if (t
== SESSION_UNSPECIFIED
)
707 c
= SESSION_BACKGROUND
;
713 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
715 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
719 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
724 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
725 * that avoids creating a logind session. */
726 r
= manager_get_user_by_pid(m
, leader
, NULL
);
730 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already running in a session or user slice");
733 * Old gdm and lightdm start the user-session on the same VT as
734 * the greeter session. But they destroy the greeter session
735 * after the user-session and want the user-session to take
736 * over the VT. We need to support this for
737 * backwards-compatibility, so make sure we allow new sessions
738 * on a VT that a greeter is running on. Furthermore, to allow
739 * re-logins, we have to allow a greeter to take over a used VT for
740 * the exact same reasons.
742 if (c
!= SESSION_GREETER
&&
744 vtnr
< m
->seat0
->position_count
&&
745 m
->seat0
->positions
[vtnr
] &&
746 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
747 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
749 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
750 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.", m
->sessions_max
);
752 (void) audit_session_from_pid(leader
, &audit_id
);
753 if (audit_session_is_valid(audit_id
)) {
754 /* Keep our session IDs and the audit session IDs in sync */
756 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
759 /* Wut? There's already a session by this name and we
760 * didn't find it above? Weird, then let's not trust
761 * the audit data and let's better register a new
763 if (hashmap_get(m
->sessions
, id
)) {
764 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
765 audit_id
= AUDIT_SESSION_INVALID
;
774 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
777 } while (hashmap_get(m
->sessions
, id
));
780 r
= manager_add_user_by_uid(m
, uid
, &user
);
784 r
= manager_add_session(m
, id
, &session
);
788 session_set_user(session
, user
);
790 session
->leader
= leader
;
791 session
->audit_id
= audit_id
;
794 session
->remote
= remote
;
795 session
->vtnr
= vtnr
;
798 session
->tty
= strdup(tty
);
805 if (!isempty(display
)) {
806 session
->display
= strdup(display
);
807 if (!session
->display
) {
813 if (!isempty(remote_user
)) {
814 session
->remote_user
= strdup(remote_user
);
815 if (!session
->remote_user
) {
821 if (!isempty(remote_host
)) {
822 session
->remote_host
= strdup(remote_host
);
823 if (!session
->remote_host
) {
829 if (!isempty(service
)) {
830 session
->service
= strdup(service
);
831 if (!session
->service
) {
837 if (!isempty(desktop
)) {
838 session
->desktop
= strdup(desktop
);
839 if (!session
->desktop
) {
846 r
= seat_attach_session(seat
, session
);
851 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
855 r
= session_start(session
, message
);
859 r
= sd_bus_message_exit_container(message
);
863 session
->create_message
= sd_bus_message_ref(message
);
865 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
866 * session_send_create_reply(). */
872 session_add_to_gc_queue(session
);
875 user_add_to_gc_queue(user
);
880 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
881 Manager
*m
= userdata
;
889 r
= sd_bus_message_read(message
, "s", &name
);
893 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
897 r
= session_release(session
);
901 return sd_bus_reply_method_return(message
, NULL
);
904 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
905 Manager
*m
= userdata
;
913 r
= sd_bus_message_read(message
, "s", &name
);
917 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
921 return bus_session_method_activate(message
, session
, error
);
924 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
925 const char *session_name
, *seat_name
;
926 Manager
*m
= userdata
;
934 /* Same as ActivateSession() but refuses to work if
935 * the seat doesn't match */
937 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
941 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
945 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
949 if (session
->seat
!= seat
)
950 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", session_name
, seat_name
);
952 r
= session_activate(session
);
956 return sd_bus_reply_method_return(message
, NULL
);
959 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
960 Manager
*m
= userdata
;
968 r
= sd_bus_message_read(message
, "s", &name
);
972 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
976 return bus_session_method_lock(message
, session
, error
);
979 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
980 Manager
*m
= userdata
;
986 r
= bus_verify_polkit_async(
989 "org.freedesktop.login1.lock-sessions",
998 return 1; /* Will call us back */
1000 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1004 return sd_bus_reply_method_return(message
, NULL
);
1007 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1009 Manager
*m
= userdata
;
1016 r
= sd_bus_message_read(message
, "s", &name
);
1020 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1024 return bus_session_method_kill(message
, session
, error
);
1027 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1028 Manager
*m
= userdata
;
1036 r
= sd_bus_message_read(message
, "u", &uid
);
1040 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1044 return bus_user_method_kill(message
, user
, error
);
1047 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1048 Manager
*m
= userdata
;
1056 r
= sd_bus_message_read(message
, "s", &name
);
1060 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1064 return bus_session_method_terminate(message
, session
, error
);
1067 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1068 Manager
*m
= userdata
;
1076 r
= sd_bus_message_read(message
, "u", &uid
);
1080 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1084 return bus_user_method_terminate(message
, user
, error
);
1087 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1088 Manager
*m
= userdata
;
1096 r
= sd_bus_message_read(message
, "s", &name
);
1100 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1104 return bus_seat_method_terminate(message
, seat
, error
);
1107 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1108 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1109 _cleanup_free_
char *cc
= NULL
;
1110 Manager
*m
= userdata
;
1111 int r
, b
, interactive
;
1114 uint32_t uid
, auth_uid
;
1119 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1123 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1124 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1128 if (!uid_is_valid(uid
)) {
1129 /* Note that we get the owner UID of the session or user unit,
1130 * not the actual client UID here! */
1131 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1136 /* owner_uid is racy, so for authorization we must use euid */
1137 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1144 return errno
> 0 ? -errno
: -ENOENT
;
1146 r
= bus_verify_polkit_async(
1149 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1150 "org.freedesktop.login1.set-user-linger",
1154 &m
->polkit_registry
,
1159 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1161 mkdir_p_label("/var/lib/systemd", 0755);
1163 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1167 cc
= cescape(pw
->pw_name
);
1171 path
= strjoina("/var/lib/systemd/linger/", cc
);
1179 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1186 if (r
< 0 && errno
!= ENOENT
)
1189 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1191 user_add_to_gc_queue(u
);
1194 return sd_bus_reply_method_return(message
, NULL
);
1197 static int trigger_device(Manager
*m
, struct udev_device
*d
) {
1198 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*e
= NULL
;
1199 struct udev_list_entry
*first
, *item
;
1204 e
= udev_enumerate_new(m
->udev
);
1209 r
= udev_enumerate_add_match_parent(e
, d
);
1214 r
= udev_enumerate_scan_devices(e
);
1218 first
= udev_enumerate_get_list_entry(e
);
1219 udev_list_entry_foreach(item
, first
) {
1220 _cleanup_free_
char *t
= NULL
;
1223 p
= udev_list_entry_get_name(item
);
1225 t
= strappend(p
, "/uevent");
1229 (void) write_string_file(t
, "change", 0);
1235 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1236 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
1237 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1238 const char *id_for_seat
;
1245 d
= udev_device_new_from_syspath(m
->udev
, sysfs
);
1249 if (!udev_device_has_tag(d
, "seat"))
1252 id_for_seat
= udev_device_get_property_value(d
, "ID_FOR_SEAT");
1256 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1259 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1262 mkdir_p_label("/etc/udev/rules.d", 0755);
1263 r
= write_string_file_atomic_label(file
, rule
);
1267 return trigger_device(m
, d
);
1270 static int flush_devices(Manager
*m
) {
1271 _cleanup_closedir_
DIR *d
;
1275 d
= opendir("/etc/udev/rules.d");
1277 if (errno
!= ENOENT
)
1278 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1282 FOREACH_DIRENT_ALL(de
, d
, break) {
1283 if (!dirent_is_file(de
))
1286 if (!startswith(de
->d_name
, "72-seat-"))
1289 if (!endswith(de
->d_name
, ".rules"))
1292 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1293 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1297 return trigger_device(m
, NULL
);
1300 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1301 const char *sysfs
, *seat
;
1302 Manager
*m
= userdata
;
1308 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1312 if (!path_startswith(sysfs
, "/sys"))
1313 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1315 if (!seat_name_is_valid(seat
))
1316 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat %s is not valid", seat
);
1318 r
= bus_verify_polkit_async(
1321 "org.freedesktop.login1.attach-device",
1325 &m
->polkit_registry
,
1330 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1332 r
= attach_device(m
, seat
, sysfs
);
1336 return sd_bus_reply_method_return(message
, NULL
);
1339 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1340 Manager
*m
= userdata
;
1346 r
= sd_bus_message_read(message
, "b", &interactive
);
1350 r
= bus_verify_polkit_async(
1353 "org.freedesktop.login1.flush-devices",
1357 &m
->polkit_registry
,
1362 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1364 r
= flush_devices(m
);
1368 return sd_bus_reply_method_return(message
, NULL
);
1371 static int have_multiple_sessions(
1380 /* Check for other users' sessions. Greeter sessions do not
1381 * count, and non-login sessions do not count either. */
1382 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1383 if (session
->class == SESSION_USER
&&
1384 session
->user
->uid
!= uid
)
1390 static int bus_manager_log_shutdown(
1392 const char *unit_name
) {
1399 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1400 p
= "MESSAGE=System is powering down";
1401 q
= "SHUTDOWN=power-off";
1402 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1403 p
= "MESSAGE=System is rebooting";
1404 q
= "SHUTDOWN=reboot";
1405 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1406 p
= "MESSAGE=System is halting";
1407 q
= "SHUTDOWN=halt";
1408 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1409 p
= "MESSAGE=System is rebooting with kexec";
1410 q
= "SHUTDOWN=kexec";
1412 p
= "MESSAGE=System is shutting down";
1416 if (isempty(m
->wall_message
))
1417 p
= strjoina(p
, ".");
1419 p
= strjoina(p
, " (", m
->wall_message
, ").");
1421 return log_struct(LOG_NOTICE
,
1422 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1427 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1428 Manager
*m
= userdata
;
1433 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1437 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1442 if (until
<= now(CLOCK_MONOTONIC
))
1445 /* We want to ignore the lid switch for a while after each
1446 * suspend, and after boot-up. Hence let's install a timer for
1447 * this. As long as the event source exists we ignore the lid
1450 if (m
->lid_switch_ignore_event_source
) {
1453 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1460 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1462 r
= sd_event_add_time(
1464 &m
->lid_switch_ignore_event_source
,
1467 lid_switch_ignore_handler
, m
);
1472 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1474 static const char * const signal_name
[_INHIBIT_WHAT_MAX
] = {
1475 [INHIBIT_SHUTDOWN
] = "PrepareForShutdown",
1476 [INHIBIT_SLEEP
] = "PrepareForSleep"
1479 int active
= _active
;
1483 assert(w
< _INHIBIT_WHAT_MAX
);
1484 assert(signal_name
[w
]);
1486 return sd_bus_emit_signal(m
->bus
,
1487 "/org/freedesktop/login1",
1488 "org.freedesktop.login1.Manager",
1494 static int execute_shutdown_or_sleep(
1497 const char *unit_name
,
1498 sd_bus_error
*error
) {
1500 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1507 assert(w
< _INHIBIT_WHAT_MAX
);
1510 if (w
== INHIBIT_SHUTDOWN
)
1511 bus_manager_log_shutdown(m
, unit_name
);
1513 r
= sd_bus_call_method(
1515 "org.freedesktop.systemd1",
1516 "/org/freedesktop/systemd1",
1517 "org.freedesktop.systemd1.Manager",
1521 "ss", unit_name
, "replace-irreversibly");
1525 r
= sd_bus_message_read(reply
, "o", &p
);
1535 m
->action_unit
= unit_name
;
1536 free(m
->action_job
);
1540 /* Make sure the lid switch is ignored for a while */
1541 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1546 /* Tell people that they now may take a lock again */
1547 (void) send_prepare_for(m
, w
, false);
1552 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1554 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1555 Inhibitor
*offending
= NULL
;
1560 if (manager
->action_what
== 0 || manager
->action_job
)
1563 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1564 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1569 (void) get_process_comm(offending
->pid
, &comm
);
1570 u
= uid_to_name(offending
->uid
);
1572 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1573 offending
->uid
, strna(u
),
1574 offending
->pid
, strna(comm
));
1577 /* Actually do the operation */
1578 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1580 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1581 bus_error_message(&error
, r
));
1583 manager
->action_unit
= NULL
;
1584 manager
->action_what
= 0;
1591 static int manager_inhibit_timeout_handler(
1596 Manager
*manager
= userdata
;
1600 assert(manager
->inhibit_timeout_source
== s
);
1602 r
= manager_dispatch_delayed(manager
, true);
1603 return (r
< 0) ? r
: 0;
1606 static int delay_shutdown_or_sleep(
1609 const char *unit_name
) {
1616 assert(w
< _INHIBIT_WHAT_MAX
);
1619 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1621 if (m
->inhibit_timeout_source
) {
1622 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1624 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1626 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1628 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1630 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1631 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1636 m
->action_unit
= unit_name
;
1642 int bus_manager_shutdown_or_sleep_now_or_later(
1644 const char *unit_name
,
1646 sd_bus_error
*error
) {
1648 _cleanup_free_
char *load_state
= NULL
;
1655 assert(w
<= _INHIBIT_WHAT_MAX
);
1656 assert(!m
->action_job
);
1658 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1662 if (!streq(load_state
, "loaded")) {
1663 log_notice("Unit %s is %s, refusing operation.", unit_name
, load_state
);
1667 /* Tell everybody to prepare for shutdown/sleep */
1668 (void) send_prepare_for(m
, w
, true);
1671 m
->inhibit_delay_max
> 0 &&
1672 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1675 /* Shutdown is delayed, keep in mind what we
1676 * want to do, and start a timeout */
1677 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1679 /* Shutdown is not delayed, execute it
1681 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1686 static int verify_shutdown_creds(
1688 sd_bus_message
*message
,
1692 const char *action_multiple_sessions
,
1693 const char *action_ignore_inhibit
,
1694 sd_bus_error
*error
) {
1696 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1697 bool multiple_sessions
, blocked
;
1704 assert(w
<= _INHIBIT_WHAT_MAX
);
1706 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1710 r
= sd_bus_creds_get_euid(creds
, &uid
);
1714 r
= have_multiple_sessions(m
, uid
);
1718 multiple_sessions
= r
> 0;
1719 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1721 if (multiple_sessions
&& action_multiple_sessions
) {
1722 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1726 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1729 if (blocked
&& action_ignore_inhibit
) {
1730 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1734 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1737 if (!multiple_sessions
&& !blocked
&& action
) {
1738 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1742 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1748 static int method_do_shutdown_or_sleep(
1750 sd_bus_message
*message
,
1751 const char *unit_name
,
1754 const char *action_multiple_sessions
,
1755 const char *action_ignore_inhibit
,
1756 const char *sleep_verb
,
1757 sd_bus_error
*error
) {
1765 assert(w
<= _INHIBIT_WHAT_MAX
);
1767 r
= sd_bus_message_read(message
, "b", &interactive
);
1771 /* Don't allow multiple jobs being executed at the same time */
1773 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
, "There's already a shutdown or sleep operation in progress");
1776 r
= can_sleep(sleep_verb
);
1778 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1779 "Not enough swap space for hibernation");
1781 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1782 "Sleep verb \"%s\" not supported", sleep_verb
);
1787 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1788 action_ignore_inhibit
, error
);
1792 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1796 return sd_bus_reply_method_return(message
, NULL
);
1799 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1800 Manager
*m
= userdata
;
1802 return method_do_shutdown_or_sleep(
1804 SPECIAL_POWEROFF_TARGET
,
1806 "org.freedesktop.login1.power-off",
1807 "org.freedesktop.login1.power-off-multiple-sessions",
1808 "org.freedesktop.login1.power-off-ignore-inhibit",
1813 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1814 Manager
*m
= userdata
;
1816 return method_do_shutdown_or_sleep(
1818 SPECIAL_REBOOT_TARGET
,
1820 "org.freedesktop.login1.reboot",
1821 "org.freedesktop.login1.reboot-multiple-sessions",
1822 "org.freedesktop.login1.reboot-ignore-inhibit",
1827 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1828 Manager
*m
= userdata
;
1830 return method_do_shutdown_or_sleep(
1832 SPECIAL_HALT_TARGET
,
1834 "org.freedesktop.login1.halt",
1835 "org.freedesktop.login1.halt-multiple-sessions",
1836 "org.freedesktop.login1.halt-ignore-inhibit",
1841 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1842 Manager
*m
= userdata
;
1844 return method_do_shutdown_or_sleep(
1846 SPECIAL_SUSPEND_TARGET
,
1848 "org.freedesktop.login1.suspend",
1849 "org.freedesktop.login1.suspend-multiple-sessions",
1850 "org.freedesktop.login1.suspend-ignore-inhibit",
1855 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1856 Manager
*m
= userdata
;
1858 return method_do_shutdown_or_sleep(
1860 SPECIAL_HIBERNATE_TARGET
,
1862 "org.freedesktop.login1.hibernate",
1863 "org.freedesktop.login1.hibernate-multiple-sessions",
1864 "org.freedesktop.login1.hibernate-ignore-inhibit",
1869 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1870 Manager
*m
= userdata
;
1872 return method_do_shutdown_or_sleep(
1874 SPECIAL_HYBRID_SLEEP_TARGET
,
1876 "org.freedesktop.login1.hibernate",
1877 "org.freedesktop.login1.hibernate-multiple-sessions",
1878 "org.freedesktop.login1.hibernate-ignore-inhibit",
1883 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1884 Manager
*m
= userdata
;
1886 return method_do_shutdown_or_sleep(
1888 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
1890 "org.freedesktop.login1.hibernate",
1891 "org.freedesktop.login1.hibernate-multiple-sessions",
1892 "org.freedesktop.login1.hibernate-ignore-inhibit",
1897 static int nologin_timeout_handler(
1902 Manager
*m
= userdata
;
1904 log_info("Creating /run/nologin, blocking further logins...");
1907 create_shutdown_run_nologin_or_warn() >= 0;
1912 static int update_schedule_file(Manager
*m
) {
1913 _cleanup_free_
char *temp_path
= NULL
;
1914 _cleanup_fclose_
FILE *f
= NULL
;
1919 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
1921 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
1923 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
1925 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
1927 (void) fchmod(fileno(f
), 0644);
1933 m
->scheduled_shutdown_timeout
,
1934 m
->enable_wall_messages
,
1935 m
->scheduled_shutdown_type
);
1937 if (!isempty(m
->wall_message
)) {
1938 _cleanup_free_
char *t
;
1940 t
= cescape(m
->wall_message
);
1946 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
1949 r
= fflush_and_check(f
);
1953 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
1961 (void) unlink(temp_path
);
1962 (void) unlink("/run/systemd/shutdown/scheduled");
1964 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
1967 static void reset_scheduled_shutdown(Manager
*m
) {
1970 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
1971 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
1972 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
1974 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
1975 m
->scheduled_shutdown_timeout
= 0;
1976 m
->shutdown_dry_run
= false;
1978 if (m
->unlink_nologin
) {
1979 (void) unlink_or_warn("/run/nologin");
1980 m
->unlink_nologin
= false;
1983 (void) unlink("/run/systemd/shutdown/scheduled");
1986 static int manager_scheduled_shutdown_handler(
1991 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1992 Manager
*m
= userdata
;
1998 if (isempty(m
->scheduled_shutdown_type
))
2001 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2002 target
= SPECIAL_POWEROFF_TARGET
;
2003 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2004 target
= SPECIAL_REBOOT_TARGET
;
2005 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2006 target
= SPECIAL_HALT_TARGET
;
2008 assert_not_reached("unexpected shutdown type");
2010 /* Don't allow multiple jobs being executed at the same time */
2011 if (m
->action_what
) {
2013 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2017 if (m
->shutdown_dry_run
) {
2018 /* We do not process delay inhibitors here. Otherwise, we
2019 * would have to be considered "in progress" (like the check
2020 * above) for some seconds after our admin has seen the final
2023 bus_manager_log_shutdown(m
, target
);
2024 log_info("Running in dry run, suppressing action.");
2025 reset_scheduled_shutdown(m
);
2030 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2032 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2039 reset_scheduled_shutdown(m
);
2043 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2044 Manager
*m
= userdata
;
2045 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2046 const char *action_multiple_sessions
= NULL
;
2047 const char *action_ignore_inhibit
= NULL
;
2048 const char *action
= NULL
;
2052 bool dry_run
= false;
2057 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2061 if (startswith(type
, "dry-")) {
2066 if (streq(type
, "poweroff")) {
2067 action
= "org.freedesktop.login1.power-off";
2068 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2069 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2070 } else if (streq(type
, "reboot")) {
2071 action
= "org.freedesktop.login1.reboot";
2072 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2073 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2074 } else if (streq(type
, "halt")) {
2075 action
= "org.freedesktop.login1.halt";
2076 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2077 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2079 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2081 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2082 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2086 if (m
->scheduled_shutdown_timeout_source
) {
2087 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2089 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2091 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2093 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2095 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2096 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2098 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2101 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2103 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2107 m
->shutdown_dry_run
= dry_run
;
2109 if (m
->nologin_timeout_source
) {
2110 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2112 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2114 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2116 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2118 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2119 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2121 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2124 m
->scheduled_shutdown_timeout
= elapse
;
2126 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2128 const char *tty
= NULL
;
2130 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2131 (void) sd_bus_creds_get_tty(creds
, &tty
);
2133 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2135 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2140 r
= manager_setup_wall_message_timer(m
);
2144 r
= update_schedule_file(m
);
2148 return sd_bus_reply_method_return(message
, NULL
);
2151 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2152 Manager
*m
= userdata
;
2158 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2159 reset_scheduled_shutdown(m
);
2161 if (cancelled
&& m
->enable_wall_messages
) {
2162 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2163 const char *tty
= NULL
;
2167 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2169 (void) sd_bus_creds_get_uid(creds
, &uid
);
2170 (void) sd_bus_creds_get_tty(creds
, &tty
);
2173 utmp_wall("The system shutdown has been cancelled",
2174 uid_to_name(uid
), tty
, logind_wall_tty_filter
, m
);
2177 return sd_bus_reply_method_return(message
, "b", cancelled
);
2180 static int method_can_shutdown_or_sleep(
2182 sd_bus_message
*message
,
2185 const char *action_multiple_sessions
,
2186 const char *action_ignore_inhibit
,
2187 const char *sleep_verb
,
2188 sd_bus_error
*error
) {
2190 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2191 HandleAction handle
;
2192 bool multiple_sessions
, challenge
, blocked
;
2193 const char *result
= NULL
;
2200 assert(w
<= _INHIBIT_WHAT_MAX
);
2202 assert(action_multiple_sessions
);
2203 assert(action_ignore_inhibit
);
2206 r
= can_sleep(sleep_verb
);
2207 if (IN_SET(r
, 0, -ENOSPC
))
2208 return sd_bus_reply_method_return(message
, "s", "na");
2213 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2217 r
= sd_bus_creds_get_euid(creds
, &uid
);
2221 r
= have_multiple_sessions(m
, uid
);
2225 multiple_sessions
= r
> 0;
2226 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2228 handle
= handle_action_from_string(sleep_verb
);
2232 target
= manager_target_for_action(handle
);
2234 _cleanup_free_
char *load_state
= NULL
;
2236 r
= unit_load_state(m
->bus
, target
, &load_state
);
2240 if (!streq(load_state
, "loaded")) {
2247 if (multiple_sessions
) {
2248 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2255 result
= "challenge";
2261 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2265 if (r
> 0 && !result
)
2267 else if (challenge
&& (!result
|| streq(result
, "yes")))
2268 result
= "challenge";
2273 if (!multiple_sessions
&& !blocked
) {
2274 /* If neither inhibit nor multiple sessions
2275 * apply then just check the normal policy */
2277 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2284 result
= "challenge";
2290 return sd_bus_reply_method_return(message
, "s", result
);
2293 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2294 Manager
*m
= userdata
;
2296 return method_can_shutdown_or_sleep(
2299 "org.freedesktop.login1.power-off",
2300 "org.freedesktop.login1.power-off-multiple-sessions",
2301 "org.freedesktop.login1.power-off-ignore-inhibit",
2306 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2307 Manager
*m
= userdata
;
2309 return method_can_shutdown_or_sleep(
2312 "org.freedesktop.login1.reboot",
2313 "org.freedesktop.login1.reboot-multiple-sessions",
2314 "org.freedesktop.login1.reboot-ignore-inhibit",
2319 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2320 Manager
*m
= userdata
;
2322 return method_can_shutdown_or_sleep(
2325 "org.freedesktop.login1.halt",
2326 "org.freedesktop.login1.halt-multiple-sessions",
2327 "org.freedesktop.login1.halt-ignore-inhibit",
2332 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2333 Manager
*m
= userdata
;
2335 return method_can_shutdown_or_sleep(
2338 "org.freedesktop.login1.suspend",
2339 "org.freedesktop.login1.suspend-multiple-sessions",
2340 "org.freedesktop.login1.suspend-ignore-inhibit",
2345 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2346 Manager
*m
= userdata
;
2348 return method_can_shutdown_or_sleep(
2351 "org.freedesktop.login1.hibernate",
2352 "org.freedesktop.login1.hibernate-multiple-sessions",
2353 "org.freedesktop.login1.hibernate-ignore-inhibit",
2358 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2359 Manager
*m
= userdata
;
2361 return method_can_shutdown_or_sleep(
2364 "org.freedesktop.login1.hibernate",
2365 "org.freedesktop.login1.hibernate-multiple-sessions",
2366 "org.freedesktop.login1.hibernate-ignore-inhibit",
2371 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2372 Manager
*m
= userdata
;
2374 return method_can_shutdown_or_sleep(
2377 "org.freedesktop.login1.hibernate",
2378 "org.freedesktop.login1.hibernate-multiple-sessions",
2379 "org.freedesktop.login1.hibernate-ignore-inhibit",
2380 "suspend-then-hibernate",
2384 static int property_get_reboot_to_firmware_setup(
2387 const char *interface
,
2388 const char *property
,
2389 sd_bus_message
*reply
,
2391 sd_bus_error
*error
) {
2398 r
= efi_get_reboot_to_firmware();
2399 if (r
< 0 && r
!= -EOPNOTSUPP
)
2400 log_warning_errno(r
, "Failed to determine reboot-to-firmware state: %m");
2402 return sd_bus_message_append(reply
, "b", r
> 0);
2405 static int method_set_reboot_to_firmware_setup(
2406 sd_bus_message
*message
,
2408 sd_bus_error
*error
) {
2411 Manager
*m
= userdata
;
2416 r
= sd_bus_message_read(message
, "b", &b
);
2420 r
= bus_verify_polkit_async(message
,
2422 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2426 &m
->polkit_registry
,
2431 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2433 r
= efi_set_reboot_to_firmware(b
);
2437 return sd_bus_reply_method_return(message
, NULL
);
2440 static int method_can_reboot_to_firmware_setup(
2441 sd_bus_message
*message
,
2443 sd_bus_error
*error
) {
2448 Manager
*m
= userdata
;
2453 r
= efi_reboot_to_firmware_supported();
2455 if (r
!= -EOPNOTSUPP
)
2456 log_warning_errno(errno
, "Failed to determine whether reboot to firmware is supported: %m");
2458 return sd_bus_reply_method_return(message
, "s", "na");
2461 r
= bus_test_polkit(message
,
2463 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2474 result
= "challenge";
2478 return sd_bus_reply_method_return(message
, "s", result
);
2481 static int method_set_wall_message(
2482 sd_bus_message
*message
,
2484 sd_bus_error
*error
) {
2487 Manager
*m
= userdata
;
2489 unsigned enable_wall_messages
;
2494 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
2498 r
= bus_verify_polkit_async(message
,
2500 "org.freedesktop.login1.set-wall-message",
2504 &m
->polkit_registry
,
2509 return 1; /* Will call us back */
2511 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
2515 m
->enable_wall_messages
= enable_wall_messages
;
2517 return sd_bus_reply_method_return(message
, NULL
);
2520 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2521 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2522 const char *who
, *why
, *what
, *mode
;
2523 _cleanup_free_
char *id
= NULL
;
2524 _cleanup_close_
int fifo_fd
= -1;
2525 Manager
*m
= userdata
;
2526 Inhibitor
*i
= NULL
;
2536 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
2540 w
= inhibit_what_from_string(what
);
2542 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid what specification %s", what
);
2544 mm
= inhibit_mode_from_string(mode
);
2546 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid mode specification %s", mode
);
2548 /* Delay is only supported for shutdown/sleep */
2549 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
2550 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delay inhibitors only supported for shutdown and sleep");
2552 /* Don't allow taking delay locks while we are already
2553 * executing the operation. We shouldn't create the impression
2554 * that the lock was successful if the machine is about to go
2555 * down/suspend any moment. */
2556 if (m
->action_what
& w
)
2557 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
, "The operation inhibition has been requested for is already running");
2559 r
= bus_verify_polkit_async(
2562 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2563 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2564 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
2565 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
2566 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2567 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2568 "org.freedesktop.login1.inhibit-handle-lid-switch",
2572 &m
->polkit_registry
,
2577 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2579 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
2583 r
= sd_bus_creds_get_euid(creds
, &uid
);
2587 r
= sd_bus_creds_get_pid(creds
, &pid
);
2591 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
2592 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.", m
->inhibitors_max
);
2597 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
2600 } while (hashmap_get(m
->inhibitors
, id
));
2602 r
= manager_add_inhibitor(m
, id
, &i
);
2610 i
->why
= strdup(why
);
2611 i
->who
= strdup(who
);
2613 if (!i
->why
|| !i
->who
) {
2618 fifo_fd
= inhibitor_create_fifo(i
);
2626 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
2635 const sd_bus_vtable manager_vtable
[] = {
2636 SD_BUS_VTABLE_START(0),
2638 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
2639 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
2641 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
2642 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
2643 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
2644 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
2645 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
2646 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2647 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2648 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2649 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2650 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2651 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2652 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2653 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2654 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2655 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
2656 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
2657 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
2658 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
2659 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
2660 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
2661 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
2662 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
2663 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
2664 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
2665 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
2666 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
2667 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2668 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
2669 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2670 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
2671 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
2673 SD_BUS_METHOD("GetSession", "s", "o", method_get_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2674 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
2675 SD_BUS_METHOD("GetUser", "u", "o", method_get_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2676 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
2677 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2678 SD_BUS_METHOD("ListSessions", NULL
, "a(susso)", method_list_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2679 SD_BUS_METHOD("ListUsers", NULL
, "a(uso)", method_list_users
, SD_BUS_VTABLE_UNPRIVILEGED
),
2680 SD_BUS_METHOD("ListSeats", NULL
, "a(so)", method_list_seats
, SD_BUS_VTABLE_UNPRIVILEGED
),
2681 SD_BUS_METHOD("ListInhibitors", NULL
, "a(ssssuu)", method_list_inhibitors
, SD_BUS_VTABLE_UNPRIVILEGED
),
2682 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session
, 0),
2683 SD_BUS_METHOD("ReleaseSession", "s", NULL
, method_release_session
, 0),
2684 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2685 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL
, method_activate_session_on_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2686 SD_BUS_METHOD("LockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2687 SD_BUS_METHOD("UnlockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2688 SD_BUS_METHOD("LockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2689 SD_BUS_METHOD("UnlockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2690 SD_BUS_METHOD("KillSession", "ssi", NULL
, method_kill_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2691 SD_BUS_METHOD("KillUser", "ui", NULL
, method_kill_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2692 SD_BUS_METHOD("TerminateSession", "s", NULL
, method_terminate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2693 SD_BUS_METHOD("TerminateUser", "u", NULL
, method_terminate_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2694 SD_BUS_METHOD("TerminateSeat", "s", NULL
, method_terminate_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2695 SD_BUS_METHOD("SetUserLinger", "ubb", NULL
, method_set_user_linger
, SD_BUS_VTABLE_UNPRIVILEGED
),
2696 SD_BUS_METHOD("AttachDevice", "ssb", NULL
, method_attach_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
2697 SD_BUS_METHOD("FlushDevices", "b", NULL
, method_flush_devices
, SD_BUS_VTABLE_UNPRIVILEGED
),
2698 SD_BUS_METHOD("PowerOff", "b", NULL
, method_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
2699 SD_BUS_METHOD("Reboot", "b", NULL
, method_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
2700 SD_BUS_METHOD("Halt", "b", NULL
, method_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
2701 SD_BUS_METHOD("Suspend", "b", NULL
, method_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
2702 SD_BUS_METHOD("Hibernate", "b", NULL
, method_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2703 SD_BUS_METHOD("HybridSleep", "b", NULL
, method_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
2704 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL
, method_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2705 SD_BUS_METHOD("CanPowerOff", NULL
, "s", method_can_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
2706 SD_BUS_METHOD("CanReboot", NULL
, "s", method_can_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
2707 SD_BUS_METHOD("CanHalt", NULL
, "s", method_can_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
2708 SD_BUS_METHOD("CanSuspend", NULL
, "s", method_can_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
2709 SD_BUS_METHOD("CanHibernate", NULL
, "s", method_can_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2710 SD_BUS_METHOD("CanHybridSleep", NULL
, "s", method_can_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
2711 SD_BUS_METHOD("CanSuspendThenHibernate", NULL
, "s", method_can_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2712 SD_BUS_METHOD("ScheduleShutdown", "st", NULL
, method_schedule_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
2713 SD_BUS_METHOD("CancelScheduledShutdown", NULL
, "b", method_cancel_scheduled_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
2714 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit
, SD_BUS_VTABLE_UNPRIVILEGED
),
2715 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL
, "s", method_can_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
2716 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL
, method_set_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
2717 SD_BUS_METHOD("SetWallMessage", "sb", NULL
, method_set_wall_message
, SD_BUS_VTABLE_UNPRIVILEGED
),
2719 SD_BUS_SIGNAL("SessionNew", "so", 0),
2720 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2721 SD_BUS_SIGNAL("UserNew", "uo", 0),
2722 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2723 SD_BUS_SIGNAL("SeatNew", "so", 0),
2724 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2725 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2726 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2731 static int session_jobs_reply(Session
*s
, const char *unit
, const char *result
) {
2740 if (streq(result
, "done"))
2741 r
= session_send_create_reply(s
, NULL
);
2743 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
2745 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
2746 r
= session_send_create_reply(s
, &e
);
2752 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2753 const char *path
, *result
, *unit
;
2754 Manager
*m
= userdata
;
2763 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
2765 bus_log_parse_error(r
);
2769 if (m
->action_job
&& streq(m
->action_job
, path
)) {
2770 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
2772 /* Tell people that they now may take a lock again */
2773 (void) send_prepare_for(m
, m
->action_what
, false);
2775 m
->action_job
= mfree(m
->action_job
);
2776 m
->action_unit
= NULL
;
2781 session
= hashmap_get(m
->session_units
, unit
);
2782 if (session
&& streq_ptr(path
, session
->scope_job
)) {
2783 session
->scope_job
= mfree(session
->scope_job
);
2784 session_jobs_reply(session
, unit
, result
);
2786 session_save(session
);
2787 user_save(session
->user
);
2788 session_add_to_gc_queue(session
);
2791 user
= hashmap_get(m
->user_units
, unit
);
2793 (streq_ptr(path
, user
->service_job
) ||
2794 streq_ptr(path
, user
->slice_job
))) {
2796 if (streq_ptr(path
, user
->service_job
))
2797 user
->service_job
= mfree(user
->service_job
);
2799 if (streq_ptr(path
, user
->slice_job
))
2800 user
->slice_job
= mfree(user
->slice_job
);
2802 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
2803 session_jobs_reply(session
, unit
, result
);
2806 user_add_to_gc_queue(user
);
2812 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2813 const char *path
, *unit
;
2814 Manager
*m
= userdata
;
2822 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
2824 bus_log_parse_error(r
);
2828 session
= hashmap_get(m
->session_units
, unit
);
2830 session_add_to_gc_queue(session
);
2832 user
= hashmap_get(m
->user_units
, unit
);
2834 user_add_to_gc_queue(user
);
2839 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2840 _cleanup_free_
char *unit
= NULL
;
2841 Manager
*m
= userdata
;
2850 path
= sd_bus_message_get_path(message
);
2854 r
= unit_name_from_dbus_path(path
, &unit
);
2855 if (r
== -EINVAL
) /* not a unit */
2862 session
= hashmap_get(m
->session_units
, unit
);
2864 session_add_to_gc_queue(session
);
2866 user
= hashmap_get(m
->user_units
, unit
);
2868 user_add_to_gc_queue(user
);
2873 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2874 Manager
*m
= userdata
;
2882 r
= sd_bus_message_read(message
, "b", &b
);
2884 bus_log_parse_error(r
);
2891 /* systemd finished reloading, let's recheck all our sessions */
2892 log_debug("System manager has been reloaded, rechecking sessions...");
2894 HASHMAP_FOREACH(session
, m
->sessions
, i
)
2895 session_add_to_gc_queue(session
);
2900 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
2905 l
= strv_from_stdarg_alloca(property
);
2907 return sd_bus_emit_properties_changed_strv(
2909 "/org/freedesktop/login1",
2910 "org.freedesktop.login1.Manager",
2914 static int strdup_job(sd_bus_message
*reply
, char **job
) {
2919 r
= sd_bus_message_read(reply
, "o", &j
);
2931 int manager_start_scope(
2936 const char *description
,
2939 sd_bus_message
*more_properties
,
2940 sd_bus_error
*error
,
2943 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2951 r
= sd_bus_message_new_method_call(
2954 "org.freedesktop.systemd1",
2955 "/org/freedesktop/systemd1",
2956 "org.freedesktop.systemd1.Manager",
2957 "StartTransientUnit");
2961 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
2965 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
2969 if (!isempty(slice
)) {
2970 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
2975 if (!isempty(description
)) {
2976 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
2981 if (!isempty(after
)) {
2982 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, after
);
2987 if (!isempty(after2
)) {
2988 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, after2
);
2993 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
2995 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
2999 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3003 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3004 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
3006 return bus_log_create_error(r
);
3008 if (more_properties
) {
3009 /* If TasksMax also appears here, it will overwrite the default value set above */
3010 r
= sd_bus_message_copy(m
, more_properties
, true);
3015 r
= sd_bus_message_close_container(m
);
3019 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3023 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3027 return strdup_job(reply
, job
);
3030 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3031 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3038 r
= sd_bus_call_method(
3040 "org.freedesktop.systemd1",
3041 "/org/freedesktop/systemd1",
3042 "org.freedesktop.systemd1.Manager",
3046 "ss", unit
, "replace");
3050 return strdup_job(reply
, job
);
3053 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3054 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3061 r
= sd_bus_call_method(
3063 "org.freedesktop.systemd1",
3064 "/org/freedesktop/systemd1",
3065 "org.freedesktop.systemd1.Manager",
3069 "ss", unit
, "fail");
3071 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3072 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
3075 sd_bus_error_free(error
);
3082 return strdup_job(reply
, job
);
3085 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*error
) {
3086 _cleanup_free_
char *path
= NULL
;
3092 path
= unit_dbus_path_from_name(scope
);
3096 r
= sd_bus_call_method(
3098 "org.freedesktop.systemd1",
3100 "org.freedesktop.systemd1.Scope",
3106 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3107 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
) ||
3108 sd_bus_error_has_name(error
, BUS_ERROR_SCOPE_NOT_RUNNING
)) {
3109 sd_bus_error_free(error
);
3119 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
3123 return sd_bus_call_method(
3125 "org.freedesktop.systemd1",
3126 "/org/freedesktop/systemd1",
3127 "org.freedesktop.systemd1.Manager",
3131 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
3134 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
3135 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3136 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3137 _cleanup_free_
char *path
= NULL
;
3144 path
= unit_dbus_path_from_name(unit
);
3148 r
= sd_bus_get_property(
3150 "org.freedesktop.systemd1",
3152 "org.freedesktop.systemd1.Unit",
3158 /* systemd might have droppped off momentarily, let's
3159 * not make this an error */
3160 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3161 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3164 /* If the unit is already unloaded then it's not
3166 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3167 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
3173 r
= sd_bus_message_read(reply
, "s", &state
);
3177 return !streq(state
, "inactive") && !streq(state
, "failed");
3180 int manager_job_is_active(Manager
*manager
, const char *path
) {
3181 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3182 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3188 r
= sd_bus_get_property(
3190 "org.freedesktop.systemd1",
3192 "org.freedesktop.systemd1.Job",
3198 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3199 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3202 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
3208 /* We don't actually care about the state really. The fact
3209 * that we could read the job state is enough for us */