1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "sd-messages.h"
11 #include "alloc-util.h"
12 #include "audit-util.h"
13 #include "bus-common-errors.h"
14 #include "bus-error.h"
15 #include "bus-unit-util.h"
17 #include "cgroup-util.h"
18 #include "device-util.h"
19 #include "dirent-util.h"
23 #include "fileio-label.h"
24 #include "format-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "selinux-util.h"
31 #include "sleep-config.h"
34 #include "terminal-util.h"
35 #include "unit-name.h"
36 #include "user-util.h"
37 #include "utmp-wtmp.h"
39 static int get_sender_session(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, Session
**ret
) {
41 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
46 /* Get client login session. This is not what you are looking for these days,
47 * as apps may instead belong to a user service unit. This includes terminal
48 * emulators and hence command-line apps. */
49 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
53 r
= sd_bus_creds_get_session(creds
, &name
);
59 session
= hashmap_get(m
->sessions
, name
);
67 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
68 "Caller does not belong to any known session");
71 int manager_get_session_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Session
**ret
) {
79 return get_sender_session(m
, message
, error
, ret
);
81 session
= hashmap_get(m
->sessions
, name
);
83 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
89 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
91 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
96 /* Note that we get the owner UID of the session, not the actual client UID here! */
97 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
101 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
107 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
115 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
, "Caller does not belong to any logged in user or lingering user");
118 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
125 if (!uid_is_valid(uid
))
126 return get_sender_user(m
, message
, error
, ret
);
128 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
130 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
, "User ID "UID_FMT
" is not logged in or lingering", uid
);
136 int manager_get_seat_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Seat
**ret
) {
147 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
151 seat
= session
->seat
;
153 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session has no seat.");
155 seat
= hashmap_get(m
->seats
, name
);
157 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
164 static int property_get_idle_hint(
167 const char *interface
,
168 const char *property
,
169 sd_bus_message
*reply
,
171 sd_bus_error
*error
) {
173 Manager
*m
= userdata
;
179 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
182 static int property_get_idle_since_hint(
185 const char *interface
,
186 const char *property
,
187 sd_bus_message
*reply
,
189 sd_bus_error
*error
) {
191 Manager
*m
= userdata
;
192 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
198 manager_get_idle_hint(m
, &t
);
200 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
203 static int property_get_inhibited(
206 const char *interface
,
207 const char *property
,
208 sd_bus_message
*reply
,
210 sd_bus_error
*error
) {
212 Manager
*m
= userdata
;
219 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
221 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
224 static int property_get_preparing(
227 const char *interface
,
228 const char *property
,
229 sd_bus_message
*reply
,
231 sd_bus_error
*error
) {
233 Manager
*m
= userdata
;
240 if (streq(property
, "PreparingForShutdown"))
241 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
243 b
= m
->action_what
& INHIBIT_SLEEP
;
245 return sd_bus_message_append(reply
, "b", b
);
248 static int property_get_scheduled_shutdown(
251 const char *interface
,
252 const char *property
,
253 sd_bus_message
*reply
,
255 sd_bus_error
*error
) {
257 Manager
*m
= userdata
;
264 r
= sd_bus_message_open_container(reply
, 'r', "st");
268 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
272 return sd_bus_message_close_container(reply
);
275 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
276 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
277 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
278 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
279 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
280 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
282 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
283 _cleanup_free_
char *p
= NULL
;
284 Manager
*m
= userdata
;
292 r
= sd_bus_message_read(message
, "s", &name
);
296 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
300 p
= session_bus_path(session
);
304 return sd_bus_reply_method_return(message
, "o", p
);
307 /* Get login session of a process. This is not what you are looking for these days,
308 * as apps may instead belong to a user service unit. This includes terminal
309 * emulators and hence command-line apps. */
310 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
311 _cleanup_free_
char *p
= NULL
;
312 Session
*session
= NULL
;
313 Manager
*m
= userdata
;
320 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
322 r
= sd_bus_message_read(message
, "u", &pid
);
329 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
333 r
= manager_get_session_by_pid(m
, pid
, &session
);
338 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
, "PID "PID_FMT
" does not belong to any known session", pid
);
341 p
= session_bus_path(session
);
345 return sd_bus_reply_method_return(message
, "o", p
);
348 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
349 _cleanup_free_
char *p
= NULL
;
350 Manager
*m
= userdata
;
358 r
= sd_bus_message_read(message
, "u", &uid
);
362 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
366 p
= user_bus_path(user
);
370 return sd_bus_reply_method_return(message
, "o", p
);
373 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
374 _cleanup_free_
char *p
= NULL
;
375 Manager
*m
= userdata
;
383 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
385 r
= sd_bus_message_read(message
, "u", &pid
);
392 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
396 r
= manager_get_user_by_pid(m
, pid
, &user
);
400 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
401 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
405 p
= user_bus_path(user
);
409 return sd_bus_reply_method_return(message
, "o", p
);
412 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
413 _cleanup_free_
char *p
= NULL
;
414 Manager
*m
= userdata
;
422 r
= sd_bus_message_read(message
, "s", &name
);
426 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
430 p
= seat_bus_path(seat
);
434 return sd_bus_reply_method_return(message
, "o", p
);
437 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
438 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
439 Manager
*m
= userdata
;
447 r
= sd_bus_message_new_method_return(message
, &reply
);
451 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
455 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
456 _cleanup_free_
char *p
= NULL
;
458 p
= session_bus_path(session
);
462 r
= sd_bus_message_append(reply
, "(susso)",
464 (uint32_t) session
->user
->uid
,
466 session
->seat
? session
->seat
->id
: "",
472 r
= sd_bus_message_close_container(reply
);
476 return sd_bus_send(NULL
, reply
, NULL
);
479 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
480 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
481 Manager
*m
= userdata
;
489 r
= sd_bus_message_new_method_return(message
, &reply
);
493 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
497 HASHMAP_FOREACH(user
, m
->users
, i
) {
498 _cleanup_free_
char *p
= NULL
;
500 p
= user_bus_path(user
);
504 r
= sd_bus_message_append(reply
, "(uso)",
505 (uint32_t) user
->uid
,
512 r
= sd_bus_message_close_container(reply
);
516 return sd_bus_send(NULL
, reply
, NULL
);
519 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
520 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
521 Manager
*m
= userdata
;
529 r
= sd_bus_message_new_method_return(message
, &reply
);
533 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
537 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
538 _cleanup_free_
char *p
= NULL
;
540 p
= seat_bus_path(seat
);
544 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
549 r
= sd_bus_message_close_container(reply
);
553 return sd_bus_send(NULL
, reply
, NULL
);
556 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
557 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
558 Manager
*m
= userdata
;
559 Inhibitor
*inhibitor
;
566 r
= sd_bus_message_new_method_return(message
, &reply
);
570 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
574 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
576 r
= sd_bus_message_append(reply
, "(ssssuu)",
577 strempty(inhibit_what_to_string(inhibitor
->what
)),
578 strempty(inhibitor
->who
),
579 strempty(inhibitor
->why
),
580 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
581 (uint32_t) inhibitor
->uid
,
582 (uint32_t) inhibitor
->pid
);
587 r
= sd_bus_message_close_container(reply
);
591 return sd_bus_send(NULL
, reply
, NULL
);
594 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
595 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
596 _cleanup_free_
char *id
= NULL
;
597 Session
*session
= NULL
;
598 uint32_t audit_id
= 0;
599 Manager
*m
= userdata
;
613 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
614 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
616 r
= sd_bus_message_read(message
, "uusssssussbss", &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
, &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
620 if (!uid_is_valid(uid
))
621 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
622 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
623 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
626 t
= _SESSION_TYPE_INVALID
;
628 t
= session_type_from_string(type
);
630 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid session type %s", type
);
634 c
= _SESSION_CLASS_INVALID
;
636 c
= session_class_from_string(class);
638 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid session class %s", class);
641 if (isempty(desktop
))
644 if (!string_is_safe(desktop
))
645 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid desktop string %s", desktop
);
651 seat
= hashmap_get(m
->seats
, cseat
);
653 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", cseat
);
656 if (tty_is_vc(tty
)) {
661 else if (seat
!= m
->seat0
)
662 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
);
664 v
= vtnr_from_tty(tty
);
666 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot determine VT number from virtual console TTY %s", tty
);
670 else if (vtnr
!= (uint32_t) v
)
671 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified TTY and VT number do not match");
673 } else if (tty_is_console(tty
)) {
677 else if (seat
!= m
->seat0
)
678 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Console TTY specified but seat is not seat0");
681 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Console TTY specified but VT number is not 0");
685 if (seat_has_vts(seat
)) {
686 if (vtnr
<= 0 || vtnr
> 63)
687 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "VT number out of range");
690 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat has no VTs but VT number not 0");
694 if (t
== _SESSION_TYPE_INVALID
) {
695 if (!isempty(display
))
697 else if (!isempty(tty
))
700 t
= SESSION_UNSPECIFIED
;
703 if (c
== _SESSION_CLASS_INVALID
) {
704 if (t
== SESSION_UNSPECIFIED
)
705 c
= SESSION_BACKGROUND
;
711 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
713 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
717 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
722 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
723 * that avoids creating a logind session. */
724 r
= manager_get_user_by_pid(m
, leader
, NULL
);
728 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already running in a session or user slice");
731 * Old gdm and lightdm start the user-session on the same VT as
732 * the greeter session. But they destroy the greeter session
733 * after the user-session and want the user-session to take
734 * over the VT. We need to support this for
735 * backwards-compatibility, so make sure we allow new sessions
736 * on a VT that a greeter is running on. Furthermore, to allow
737 * re-logins, we have to allow a greeter to take over a used VT for
738 * the exact same reasons.
740 if (c
!= SESSION_GREETER
&&
742 vtnr
< m
->seat0
->position_count
&&
743 m
->seat0
->positions
[vtnr
] &&
744 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
745 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
747 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
748 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.", m
->sessions_max
);
750 (void) audit_session_from_pid(leader
, &audit_id
);
751 if (audit_session_is_valid(audit_id
)) {
752 /* Keep our session IDs and the audit session IDs in sync */
754 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
757 /* Wut? There's already a session by this name and we
758 * didn't find it above? Weird, then let's not trust
759 * the audit data and let's better register a new
761 if (hashmap_get(m
->sessions
, id
)) {
762 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
763 audit_id
= AUDIT_SESSION_INVALID
;
772 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
775 } while (hashmap_get(m
->sessions
, id
));
778 /* If we are not watching utmp aleady, try again */
779 manager_reconnect_utmp(m
);
781 r
= manager_add_user_by_uid(m
, uid
, &user
);
785 r
= manager_add_session(m
, id
, &session
);
789 session_set_user(session
, user
);
790 session_set_leader(session
, leader
);
794 session
->remote
= remote
;
795 session
->vtnr
= vtnr
;
798 session
->tty
= strdup(tty
);
804 session
->tty_validity
= TTY_FROM_PAM
;
807 if (!isempty(display
)) {
808 session
->display
= strdup(display
);
809 if (!session
->display
) {
815 if (!isempty(remote_user
)) {
816 session
->remote_user
= strdup(remote_user
);
817 if (!session
->remote_user
) {
823 if (!isempty(remote_host
)) {
824 session
->remote_host
= strdup(remote_host
);
825 if (!session
->remote_host
) {
831 if (!isempty(service
)) {
832 session
->service
= strdup(service
);
833 if (!session
->service
) {
839 if (!isempty(desktop
)) {
840 session
->desktop
= strdup(desktop
);
841 if (!session
->desktop
) {
848 r
= seat_attach_session(seat
, session
);
853 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
857 r
= session_start(session
, message
, error
);
861 r
= sd_bus_message_exit_container(message
);
865 session
->create_message
= sd_bus_message_ref(message
);
867 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
868 * session_send_create_reply(). */
874 session_add_to_gc_queue(session
);
877 user_add_to_gc_queue(user
);
882 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
883 Manager
*m
= userdata
;
891 r
= sd_bus_message_read(message
, "s", &name
);
895 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
899 r
= session_release(session
);
903 return sd_bus_reply_method_return(message
, NULL
);
906 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
907 Manager
*m
= userdata
;
915 r
= sd_bus_message_read(message
, "s", &name
);
919 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
923 return bus_session_method_activate(message
, session
, error
);
926 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
927 const char *session_name
, *seat_name
;
928 Manager
*m
= userdata
;
936 /* Same as ActivateSession() but refuses to work if
937 * the seat doesn't match */
939 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
943 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
947 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
951 if (session
->seat
!= seat
)
952 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", session_name
, seat_name
);
954 r
= session_activate(session
);
958 return sd_bus_reply_method_return(message
, NULL
);
961 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
962 Manager
*m
= userdata
;
970 r
= sd_bus_message_read(message
, "s", &name
);
974 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
978 return bus_session_method_lock(message
, session
, error
);
981 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
982 Manager
*m
= userdata
;
988 r
= bus_verify_polkit_async(
991 "org.freedesktop.login1.lock-sessions",
1000 return 1; /* Will call us back */
1002 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1006 return sd_bus_reply_method_return(message
, NULL
);
1009 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1011 Manager
*m
= userdata
;
1018 r
= sd_bus_message_read(message
, "s", &name
);
1022 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1026 return bus_session_method_kill(message
, session
, error
);
1029 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1030 Manager
*m
= userdata
;
1038 r
= sd_bus_message_read(message
, "u", &uid
);
1042 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1046 return bus_user_method_kill(message
, user
, error
);
1049 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1050 Manager
*m
= userdata
;
1058 r
= sd_bus_message_read(message
, "s", &name
);
1062 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1066 return bus_session_method_terminate(message
, session
, error
);
1069 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1070 Manager
*m
= userdata
;
1078 r
= sd_bus_message_read(message
, "u", &uid
);
1082 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1086 return bus_user_method_terminate(message
, user
, error
);
1089 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1090 Manager
*m
= userdata
;
1098 r
= sd_bus_message_read(message
, "s", &name
);
1102 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1106 return bus_seat_method_terminate(message
, seat
, error
);
1109 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1110 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1111 _cleanup_free_
char *cc
= NULL
;
1112 Manager
*m
= userdata
;
1113 int r
, b
, interactive
;
1116 uint32_t uid
, auth_uid
;
1121 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1125 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1126 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1130 if (!uid_is_valid(uid
)) {
1131 /* Note that we get the owner UID of the session or user unit,
1132 * not the actual client UID here! */
1133 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1138 /* owner_uid is racy, so for authorization we must use euid */
1139 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1146 return errno
> 0 ? -errno
: -ENOENT
;
1148 r
= bus_verify_polkit_async(
1151 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1152 "org.freedesktop.login1.set-user-linger",
1156 &m
->polkit_registry
,
1161 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1163 mkdir_p_label("/var/lib/systemd", 0755);
1165 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1169 cc
= cescape(pw
->pw_name
);
1173 path
= strjoina("/var/lib/systemd/linger/", cc
);
1181 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1188 if (r
< 0 && errno
!= ENOENT
)
1191 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1193 user_add_to_gc_queue(u
);
1196 return sd_bus_reply_method_return(message
, NULL
);
1199 static int trigger_device(Manager
*m
, sd_device
*d
) {
1200 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1205 r
= sd_device_enumerator_new(&e
);
1209 r
= sd_device_enumerator_allow_uninitialized(e
);
1214 r
= sd_device_enumerator_add_match_parent(e
, d
);
1219 FOREACH_DEVICE(e
, d
) {
1220 _cleanup_free_
char *t
= NULL
;
1223 r
= sd_device_get_syspath(d
, &p
);
1227 t
= strappend(p
, "/uevent");
1231 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1237 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1238 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1239 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1240 const char *id_for_seat
;
1247 r
= sd_device_new_from_syspath(&d
, sysfs
);
1251 if (sd_device_has_tag(d
, "seat") <= 0)
1254 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1257 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1260 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1263 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1264 r
= write_string_file_atomic_label(file
, rule
);
1268 return trigger_device(m
, d
);
1271 static int flush_devices(Manager
*m
) {
1272 _cleanup_closedir_
DIR *d
;
1276 d
= opendir("/etc/udev/rules.d");
1278 if (errno
!= ENOENT
)
1279 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1283 FOREACH_DIRENT_ALL(de
, d
, break) {
1284 if (!dirent_is_file(de
))
1287 if (!startswith(de
->d_name
, "72-seat-"))
1290 if (!endswith(de
->d_name
, ".rules"))
1293 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1294 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1298 return trigger_device(m
, NULL
);
1301 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1302 const char *sysfs
, *seat
;
1303 Manager
*m
= userdata
;
1309 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1313 if (!path_startswith(sysfs
, "/sys"))
1314 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1316 if (!seat_name_is_valid(seat
))
1317 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat %s is not valid", seat
);
1319 r
= bus_verify_polkit_async(
1322 "org.freedesktop.login1.attach-device",
1326 &m
->polkit_registry
,
1331 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1333 r
= attach_device(m
, seat
, sysfs
);
1337 return sd_bus_reply_method_return(message
, NULL
);
1340 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1341 Manager
*m
= userdata
;
1347 r
= sd_bus_message_read(message
, "b", &interactive
);
1351 r
= bus_verify_polkit_async(
1354 "org.freedesktop.login1.flush-devices",
1358 &m
->polkit_registry
,
1363 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1365 r
= flush_devices(m
);
1369 return sd_bus_reply_method_return(message
, NULL
);
1372 static int have_multiple_sessions(
1381 /* Check for other users' sessions. Greeter sessions do not
1382 * count, and non-login sessions do not count either. */
1383 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1384 if (session
->class == SESSION_USER
&&
1385 session
->user
->uid
!= uid
)
1391 static int bus_manager_log_shutdown(
1393 const char *unit_name
) {
1400 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1401 p
= "MESSAGE=System is powering down";
1402 q
= "SHUTDOWN=power-off";
1403 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1404 p
= "MESSAGE=System is rebooting";
1405 q
= "SHUTDOWN=reboot";
1406 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1407 p
= "MESSAGE=System is halting";
1408 q
= "SHUTDOWN=halt";
1409 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1410 p
= "MESSAGE=System is rebooting with kexec";
1411 q
= "SHUTDOWN=kexec";
1413 p
= "MESSAGE=System is shutting down";
1417 if (isempty(m
->wall_message
))
1418 p
= strjoina(p
, ".");
1420 p
= strjoina(p
, " (", m
->wall_message
, ").");
1422 return log_struct(LOG_NOTICE
,
1423 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1428 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1429 Manager
*m
= userdata
;
1434 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1438 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1443 if (until
<= now(CLOCK_MONOTONIC
))
1446 /* We want to ignore the lid switch for a while after each
1447 * suspend, and after boot-up. Hence let's install a timer for
1448 * this. As long as the event source exists we ignore the lid
1451 if (m
->lid_switch_ignore_event_source
) {
1454 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1461 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1463 r
= sd_event_add_time(
1465 &m
->lid_switch_ignore_event_source
,
1468 lid_switch_ignore_handler
, m
);
1473 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1475 static const char * const signal_name
[_INHIBIT_WHAT_MAX
] = {
1476 [INHIBIT_SHUTDOWN
] = "PrepareForShutdown",
1477 [INHIBIT_SLEEP
] = "PrepareForSleep"
1480 int active
= _active
;
1484 assert(w
< _INHIBIT_WHAT_MAX
);
1485 assert(signal_name
[w
]);
1487 return sd_bus_emit_signal(m
->bus
,
1488 "/org/freedesktop/login1",
1489 "org.freedesktop.login1.Manager",
1495 static int execute_shutdown_or_sleep(
1498 const char *unit_name
,
1499 sd_bus_error
*error
) {
1501 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1508 assert(w
< _INHIBIT_WHAT_MAX
);
1511 if (w
== INHIBIT_SHUTDOWN
)
1512 bus_manager_log_shutdown(m
, unit_name
);
1514 r
= sd_bus_call_method(
1516 "org.freedesktop.systemd1",
1517 "/org/freedesktop/systemd1",
1518 "org.freedesktop.systemd1.Manager",
1522 "ss", unit_name
, "replace-irreversibly");
1526 r
= sd_bus_message_read(reply
, "o", &p
);
1536 m
->action_unit
= unit_name
;
1537 free(m
->action_job
);
1541 /* Make sure the lid switch is ignored for a while */
1542 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1547 /* Tell people that they now may take a lock again */
1548 (void) send_prepare_for(m
, w
, false);
1553 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1555 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1556 Inhibitor
*offending
= NULL
;
1561 if (manager
->action_what
== 0 || manager
->action_job
)
1564 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1565 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1570 (void) get_process_comm(offending
->pid
, &comm
);
1571 u
= uid_to_name(offending
->uid
);
1573 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1574 offending
->uid
, strna(u
),
1575 offending
->pid
, strna(comm
));
1578 /* Actually do the operation */
1579 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1581 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1582 bus_error_message(&error
, r
));
1584 manager
->action_unit
= NULL
;
1585 manager
->action_what
= 0;
1592 static int manager_inhibit_timeout_handler(
1597 Manager
*manager
= userdata
;
1601 assert(manager
->inhibit_timeout_source
== s
);
1603 r
= manager_dispatch_delayed(manager
, true);
1604 return (r
< 0) ? r
: 0;
1607 static int delay_shutdown_or_sleep(
1610 const char *unit_name
) {
1617 assert(w
< _INHIBIT_WHAT_MAX
);
1620 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1622 if (m
->inhibit_timeout_source
) {
1623 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1625 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1627 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1629 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1631 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1632 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1637 m
->action_unit
= unit_name
;
1643 int bus_manager_shutdown_or_sleep_now_or_later(
1645 const char *unit_name
,
1647 sd_bus_error
*error
) {
1649 _cleanup_free_
char *load_state
= NULL
;
1656 assert(w
<= _INHIBIT_WHAT_MAX
);
1657 assert(!m
->action_job
);
1659 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1663 if (!streq(load_state
, "loaded")) {
1664 log_notice("Unit %s is %s, refusing operation.", unit_name
, load_state
);
1668 /* Tell everybody to prepare for shutdown/sleep */
1669 (void) send_prepare_for(m
, w
, true);
1672 m
->inhibit_delay_max
> 0 &&
1673 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1676 /* Shutdown is delayed, keep in mind what we
1677 * want to do, and start a timeout */
1678 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1680 /* Shutdown is not delayed, execute it
1682 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1687 static int verify_shutdown_creds(
1689 sd_bus_message
*message
,
1693 const char *action_multiple_sessions
,
1694 const char *action_ignore_inhibit
,
1695 sd_bus_error
*error
) {
1697 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1698 bool multiple_sessions
, blocked
;
1705 assert(w
<= _INHIBIT_WHAT_MAX
);
1707 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1711 r
= sd_bus_creds_get_euid(creds
, &uid
);
1715 r
= have_multiple_sessions(m
, uid
);
1719 multiple_sessions
= r
> 0;
1720 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1722 if (multiple_sessions
&& action_multiple_sessions
) {
1723 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1727 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1730 if (blocked
&& action_ignore_inhibit
) {
1731 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1735 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1738 if (!multiple_sessions
&& !blocked
&& action
) {
1739 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1743 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1749 static int method_do_shutdown_or_sleep(
1751 sd_bus_message
*message
,
1752 const char *unit_name
,
1755 const char *action_multiple_sessions
,
1756 const char *action_ignore_inhibit
,
1757 const char *sleep_verb
,
1758 sd_bus_error
*error
) {
1766 assert(w
<= _INHIBIT_WHAT_MAX
);
1768 r
= sd_bus_message_read(message
, "b", &interactive
);
1772 /* Don't allow multiple jobs being executed at the same time */
1774 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
, "There's already a shutdown or sleep operation in progress");
1777 r
= can_sleep(sleep_verb
);
1779 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
, "Not enough swap space for hibernation");
1780 if (r
== -ENOMEDIUM
)
1781 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
, "Kernel image has been removed, can't hibernate");
1783 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
, "Resume not configured, can't hibernate");
1785 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
, "Sleep verb \"%s\" not supported", sleep_verb
);
1790 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1791 action_ignore_inhibit
, error
);
1795 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1799 return sd_bus_reply_method_return(message
, NULL
);
1802 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1803 Manager
*m
= userdata
;
1805 return method_do_shutdown_or_sleep(
1807 SPECIAL_POWEROFF_TARGET
,
1809 "org.freedesktop.login1.power-off",
1810 "org.freedesktop.login1.power-off-multiple-sessions",
1811 "org.freedesktop.login1.power-off-ignore-inhibit",
1816 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1817 Manager
*m
= userdata
;
1819 return method_do_shutdown_or_sleep(
1821 SPECIAL_REBOOT_TARGET
,
1823 "org.freedesktop.login1.reboot",
1824 "org.freedesktop.login1.reboot-multiple-sessions",
1825 "org.freedesktop.login1.reboot-ignore-inhibit",
1830 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1831 Manager
*m
= userdata
;
1833 return method_do_shutdown_or_sleep(
1835 SPECIAL_HALT_TARGET
,
1837 "org.freedesktop.login1.halt",
1838 "org.freedesktop.login1.halt-multiple-sessions",
1839 "org.freedesktop.login1.halt-ignore-inhibit",
1844 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1845 Manager
*m
= userdata
;
1847 return method_do_shutdown_or_sleep(
1849 SPECIAL_SUSPEND_TARGET
,
1851 "org.freedesktop.login1.suspend",
1852 "org.freedesktop.login1.suspend-multiple-sessions",
1853 "org.freedesktop.login1.suspend-ignore-inhibit",
1858 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1859 Manager
*m
= userdata
;
1861 return method_do_shutdown_or_sleep(
1863 SPECIAL_HIBERNATE_TARGET
,
1865 "org.freedesktop.login1.hibernate",
1866 "org.freedesktop.login1.hibernate-multiple-sessions",
1867 "org.freedesktop.login1.hibernate-ignore-inhibit",
1872 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1873 Manager
*m
= userdata
;
1875 return method_do_shutdown_or_sleep(
1877 SPECIAL_HYBRID_SLEEP_TARGET
,
1879 "org.freedesktop.login1.hibernate",
1880 "org.freedesktop.login1.hibernate-multiple-sessions",
1881 "org.freedesktop.login1.hibernate-ignore-inhibit",
1886 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1887 Manager
*m
= userdata
;
1889 return method_do_shutdown_or_sleep(
1891 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
1893 "org.freedesktop.login1.hibernate",
1894 "org.freedesktop.login1.hibernate-multiple-sessions",
1895 "org.freedesktop.login1.hibernate-ignore-inhibit",
1900 static int nologin_timeout_handler(
1905 Manager
*m
= userdata
;
1907 log_info("Creating /run/nologin, blocking further logins...");
1910 create_shutdown_run_nologin_or_warn() >= 0;
1915 static int update_schedule_file(Manager
*m
) {
1916 _cleanup_free_
char *temp_path
= NULL
;
1917 _cleanup_fclose_
FILE *f
= NULL
;
1922 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
1924 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
1926 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
1928 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
1930 (void) fchmod(fileno(f
), 0644);
1936 m
->scheduled_shutdown_timeout
,
1937 m
->enable_wall_messages
,
1938 m
->scheduled_shutdown_type
);
1940 if (!isempty(m
->wall_message
)) {
1941 _cleanup_free_
char *t
;
1943 t
= cescape(m
->wall_message
);
1949 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
1952 r
= fflush_and_check(f
);
1956 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
1964 (void) unlink(temp_path
);
1965 (void) unlink("/run/systemd/shutdown/scheduled");
1967 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
1970 static void reset_scheduled_shutdown(Manager
*m
) {
1973 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
1974 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
1975 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
1977 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
1978 m
->scheduled_shutdown_timeout
= 0;
1979 m
->shutdown_dry_run
= false;
1981 if (m
->unlink_nologin
) {
1982 (void) unlink_or_warn("/run/nologin");
1983 m
->unlink_nologin
= false;
1986 (void) unlink("/run/systemd/shutdown/scheduled");
1989 static int manager_scheduled_shutdown_handler(
1994 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1995 Manager
*m
= userdata
;
2001 if (isempty(m
->scheduled_shutdown_type
))
2004 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2005 target
= SPECIAL_POWEROFF_TARGET
;
2006 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2007 target
= SPECIAL_REBOOT_TARGET
;
2008 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2009 target
= SPECIAL_HALT_TARGET
;
2011 assert_not_reached("unexpected shutdown type");
2013 /* Don't allow multiple jobs being executed at the same time */
2014 if (m
->action_what
) {
2016 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2020 if (m
->shutdown_dry_run
) {
2021 /* We do not process delay inhibitors here. Otherwise, we
2022 * would have to be considered "in progress" (like the check
2023 * above) for some seconds after our admin has seen the final
2026 bus_manager_log_shutdown(m
, target
);
2027 log_info("Running in dry run, suppressing action.");
2028 reset_scheduled_shutdown(m
);
2033 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2035 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2042 reset_scheduled_shutdown(m
);
2046 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2047 Manager
*m
= userdata
;
2048 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2049 const char *action_multiple_sessions
= NULL
;
2050 const char *action_ignore_inhibit
= NULL
;
2051 const char *action
= NULL
;
2055 bool dry_run
= false;
2060 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2064 if (startswith(type
, "dry-")) {
2069 if (streq(type
, "poweroff")) {
2070 action
= "org.freedesktop.login1.power-off";
2071 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2072 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2073 } else if (streq(type
, "reboot")) {
2074 action
= "org.freedesktop.login1.reboot";
2075 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2076 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2077 } else if (streq(type
, "halt")) {
2078 action
= "org.freedesktop.login1.halt";
2079 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2080 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2082 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2084 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2085 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2089 if (m
->scheduled_shutdown_timeout_source
) {
2090 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2092 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2094 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2096 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2098 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2099 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2101 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2104 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2106 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2110 m
->shutdown_dry_run
= dry_run
;
2112 if (m
->nologin_timeout_source
) {
2113 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2115 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2117 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2119 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2121 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2122 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2124 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2127 m
->scheduled_shutdown_timeout
= elapse
;
2129 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2131 const char *tty
= NULL
;
2133 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2134 (void) sd_bus_creds_get_tty(creds
, &tty
);
2136 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2138 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2143 r
= manager_setup_wall_message_timer(m
);
2147 r
= update_schedule_file(m
);
2151 return sd_bus_reply_method_return(message
, NULL
);
2154 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2155 Manager
*m
= userdata
;
2161 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2162 reset_scheduled_shutdown(m
);
2164 if (cancelled
&& m
->enable_wall_messages
) {
2165 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2166 _cleanup_free_
char *username
= NULL
;
2167 const char *tty
= NULL
;
2171 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2173 (void) sd_bus_creds_get_uid(creds
, &uid
);
2174 (void) sd_bus_creds_get_tty(creds
, &tty
);
2177 username
= uid_to_name(uid
);
2178 utmp_wall("The system shutdown has been cancelled",
2179 username
, tty
, logind_wall_tty_filter
, m
);
2182 return sd_bus_reply_method_return(message
, "b", cancelled
);
2185 static int method_can_shutdown_or_sleep(
2187 sd_bus_message
*message
,
2190 const char *action_multiple_sessions
,
2191 const char *action_ignore_inhibit
,
2192 const char *sleep_verb
,
2193 sd_bus_error
*error
) {
2195 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2196 HandleAction handle
;
2197 bool multiple_sessions
, challenge
, blocked
;
2198 const char *result
= NULL
;
2205 assert(w
<= _INHIBIT_WHAT_MAX
);
2207 assert(action_multiple_sessions
);
2208 assert(action_ignore_inhibit
);
2211 r
= can_sleep(sleep_verb
);
2212 if (IN_SET(r
, 0, -ENOSPC
, -ENOMEDIUM
, -EADV
))
2213 return sd_bus_reply_method_return(message
, "s", "na");
2218 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2222 r
= sd_bus_creds_get_euid(creds
, &uid
);
2226 r
= have_multiple_sessions(m
, uid
);
2230 multiple_sessions
= r
> 0;
2231 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2233 handle
= handle_action_from_string(sleep_verb
);
2237 target
= manager_target_for_action(handle
);
2239 _cleanup_free_
char *load_state
= NULL
;
2241 r
= unit_load_state(m
->bus
, target
, &load_state
);
2245 if (!streq(load_state
, "loaded")) {
2252 if (multiple_sessions
) {
2253 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2260 result
= "challenge";
2266 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2273 } else if (challenge
) {
2274 if (!result
|| streq(result
, "yes"))
2275 result
= "challenge";
2280 if (!multiple_sessions
&& !blocked
) {
2281 /* If neither inhibit nor multiple sessions
2282 * apply then just check the normal policy */
2284 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2291 result
= "challenge";
2297 return sd_bus_reply_method_return(message
, "s", result
);
2300 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2301 Manager
*m
= userdata
;
2303 return method_can_shutdown_or_sleep(
2306 "org.freedesktop.login1.power-off",
2307 "org.freedesktop.login1.power-off-multiple-sessions",
2308 "org.freedesktop.login1.power-off-ignore-inhibit",
2313 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2314 Manager
*m
= userdata
;
2316 return method_can_shutdown_or_sleep(
2319 "org.freedesktop.login1.reboot",
2320 "org.freedesktop.login1.reboot-multiple-sessions",
2321 "org.freedesktop.login1.reboot-ignore-inhibit",
2326 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2327 Manager
*m
= userdata
;
2329 return method_can_shutdown_or_sleep(
2332 "org.freedesktop.login1.halt",
2333 "org.freedesktop.login1.halt-multiple-sessions",
2334 "org.freedesktop.login1.halt-ignore-inhibit",
2339 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2340 Manager
*m
= userdata
;
2342 return method_can_shutdown_or_sleep(
2345 "org.freedesktop.login1.suspend",
2346 "org.freedesktop.login1.suspend-multiple-sessions",
2347 "org.freedesktop.login1.suspend-ignore-inhibit",
2352 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2353 Manager
*m
= userdata
;
2355 return method_can_shutdown_or_sleep(
2358 "org.freedesktop.login1.hibernate",
2359 "org.freedesktop.login1.hibernate-multiple-sessions",
2360 "org.freedesktop.login1.hibernate-ignore-inhibit",
2365 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2366 Manager
*m
= userdata
;
2368 return method_can_shutdown_or_sleep(
2371 "org.freedesktop.login1.hibernate",
2372 "org.freedesktop.login1.hibernate-multiple-sessions",
2373 "org.freedesktop.login1.hibernate-ignore-inhibit",
2378 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2379 Manager
*m
= userdata
;
2381 return method_can_shutdown_or_sleep(
2384 "org.freedesktop.login1.hibernate",
2385 "org.freedesktop.login1.hibernate-multiple-sessions",
2386 "org.freedesktop.login1.hibernate-ignore-inhibit",
2387 "suspend-then-hibernate",
2391 static int property_get_reboot_to_firmware_setup(
2394 const char *interface
,
2395 const char *property
,
2396 sd_bus_message
*reply
,
2398 sd_bus_error
*error
) {
2405 r
= efi_get_reboot_to_firmware();
2406 if (r
< 0 && r
!= -EOPNOTSUPP
)
2407 log_warning_errno(r
, "Failed to determine reboot-to-firmware state: %m");
2409 return sd_bus_message_append(reply
, "b", r
> 0);
2412 static int method_set_reboot_to_firmware_setup(
2413 sd_bus_message
*message
,
2415 sd_bus_error
*error
) {
2418 Manager
*m
= userdata
;
2423 r
= sd_bus_message_read(message
, "b", &b
);
2427 r
= bus_verify_polkit_async(message
,
2429 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2433 &m
->polkit_registry
,
2438 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2440 r
= efi_set_reboot_to_firmware(b
);
2444 return sd_bus_reply_method_return(message
, NULL
);
2447 static int method_can_reboot_to_firmware_setup(
2448 sd_bus_message
*message
,
2450 sd_bus_error
*error
) {
2455 Manager
*m
= userdata
;
2460 r
= efi_reboot_to_firmware_supported();
2462 if (r
!= -EOPNOTSUPP
)
2463 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2465 return sd_bus_reply_method_return(message
, "s", "na");
2468 r
= bus_test_polkit(message
,
2470 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2481 result
= "challenge";
2485 return sd_bus_reply_method_return(message
, "s", result
);
2488 static int method_set_wall_message(
2489 sd_bus_message
*message
,
2491 sd_bus_error
*error
) {
2494 Manager
*m
= userdata
;
2496 unsigned enable_wall_messages
;
2501 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
2505 r
= bus_verify_polkit_async(message
,
2507 "org.freedesktop.login1.set-wall-message",
2511 &m
->polkit_registry
,
2516 return 1; /* Will call us back */
2518 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
2522 m
->enable_wall_messages
= enable_wall_messages
;
2524 return sd_bus_reply_method_return(message
, NULL
);
2527 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2528 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2529 const char *who
, *why
, *what
, *mode
;
2530 _cleanup_free_
char *id
= NULL
;
2531 _cleanup_close_
int fifo_fd
= -1;
2532 Manager
*m
= userdata
;
2533 Inhibitor
*i
= NULL
;
2543 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
2547 w
= inhibit_what_from_string(what
);
2549 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid what specification %s", what
);
2551 mm
= inhibit_mode_from_string(mode
);
2553 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid mode specification %s", mode
);
2555 /* Delay is only supported for shutdown/sleep */
2556 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
2557 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delay inhibitors only supported for shutdown and sleep");
2559 /* Don't allow taking delay locks while we are already
2560 * executing the operation. We shouldn't create the impression
2561 * that the lock was successful if the machine is about to go
2562 * down/suspend any moment. */
2563 if (m
->action_what
& w
)
2564 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
, "The operation inhibition has been requested for is already running");
2566 r
= bus_verify_polkit_async(
2569 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2570 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2571 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
2572 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
2573 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2574 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2575 "org.freedesktop.login1.inhibit-handle-lid-switch",
2579 &m
->polkit_registry
,
2584 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2586 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
2590 r
= sd_bus_creds_get_euid(creds
, &uid
);
2594 r
= sd_bus_creds_get_pid(creds
, &pid
);
2598 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
2599 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.", m
->inhibitors_max
);
2604 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
2607 } while (hashmap_get(m
->inhibitors
, id
));
2609 r
= manager_add_inhibitor(m
, id
, &i
);
2617 i
->why
= strdup(why
);
2618 i
->who
= strdup(who
);
2620 if (!i
->why
|| !i
->who
) {
2625 fifo_fd
= inhibitor_create_fifo(i
);
2633 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
2642 const sd_bus_vtable manager_vtable
[] = {
2643 SD_BUS_VTABLE_START(0),
2645 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
2646 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
2648 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
2649 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
2650 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
2651 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
2652 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
2653 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2654 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2655 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2656 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2657 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2658 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2659 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
2660 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2661 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2662 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2663 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
2664 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
2665 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
2666 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
2667 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
2668 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
2669 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
2670 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
2671 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
2672 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
2673 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
2674 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
2675 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
2676 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
2677 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2678 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
2679 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2680 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
2681 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
2683 SD_BUS_METHOD("GetSession", "s", "o", method_get_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2684 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
2685 SD_BUS_METHOD("GetUser", "u", "o", method_get_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2686 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
2687 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2688 SD_BUS_METHOD("ListSessions", NULL
, "a(susso)", method_list_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2689 SD_BUS_METHOD("ListUsers", NULL
, "a(uso)", method_list_users
, SD_BUS_VTABLE_UNPRIVILEGED
),
2690 SD_BUS_METHOD("ListSeats", NULL
, "a(so)", method_list_seats
, SD_BUS_VTABLE_UNPRIVILEGED
),
2691 SD_BUS_METHOD("ListInhibitors", NULL
, "a(ssssuu)", method_list_inhibitors
, SD_BUS_VTABLE_UNPRIVILEGED
),
2692 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session
, 0),
2693 SD_BUS_METHOD("ReleaseSession", "s", NULL
, method_release_session
, 0),
2694 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2695 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL
, method_activate_session_on_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2696 SD_BUS_METHOD("LockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2697 SD_BUS_METHOD("UnlockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2698 SD_BUS_METHOD("LockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2699 SD_BUS_METHOD("UnlockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2700 SD_BUS_METHOD("KillSession", "ssi", NULL
, method_kill_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2701 SD_BUS_METHOD("KillUser", "ui", NULL
, method_kill_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2702 SD_BUS_METHOD("TerminateSession", "s", NULL
, method_terminate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2703 SD_BUS_METHOD("TerminateUser", "u", NULL
, method_terminate_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2704 SD_BUS_METHOD("TerminateSeat", "s", NULL
, method_terminate_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2705 SD_BUS_METHOD("SetUserLinger", "ubb", NULL
, method_set_user_linger
, SD_BUS_VTABLE_UNPRIVILEGED
),
2706 SD_BUS_METHOD("AttachDevice", "ssb", NULL
, method_attach_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
2707 SD_BUS_METHOD("FlushDevices", "b", NULL
, method_flush_devices
, SD_BUS_VTABLE_UNPRIVILEGED
),
2708 SD_BUS_METHOD("PowerOff", "b", NULL
, method_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
2709 SD_BUS_METHOD("Reboot", "b", NULL
, method_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
2710 SD_BUS_METHOD("Halt", "b", NULL
, method_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
2711 SD_BUS_METHOD("Suspend", "b", NULL
, method_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
2712 SD_BUS_METHOD("Hibernate", "b", NULL
, method_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2713 SD_BUS_METHOD("HybridSleep", "b", NULL
, method_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
2714 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL
, method_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2715 SD_BUS_METHOD("CanPowerOff", NULL
, "s", method_can_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
2716 SD_BUS_METHOD("CanReboot", NULL
, "s", method_can_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
2717 SD_BUS_METHOD("CanHalt", NULL
, "s", method_can_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
2718 SD_BUS_METHOD("CanSuspend", NULL
, "s", method_can_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
2719 SD_BUS_METHOD("CanHibernate", NULL
, "s", method_can_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2720 SD_BUS_METHOD("CanHybridSleep", NULL
, "s", method_can_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
2721 SD_BUS_METHOD("CanSuspendThenHibernate", NULL
, "s", method_can_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2722 SD_BUS_METHOD("ScheduleShutdown", "st", NULL
, method_schedule_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
2723 SD_BUS_METHOD("CancelScheduledShutdown", NULL
, "b", method_cancel_scheduled_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
2724 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit
, SD_BUS_VTABLE_UNPRIVILEGED
),
2725 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL
, "s", method_can_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
2726 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL
, method_set_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
2727 SD_BUS_METHOD("SetWallMessage", "sb", NULL
, method_set_wall_message
, SD_BUS_VTABLE_UNPRIVILEGED
),
2729 SD_BUS_SIGNAL("SessionNew", "so", 0),
2730 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2731 SD_BUS_SIGNAL("UserNew", "uo", 0),
2732 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2733 SD_BUS_SIGNAL("SeatNew", "so", 0),
2734 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2735 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2736 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2741 static int session_jobs_reply(Session
*s
, const char *unit
, const char *result
) {
2748 if (result
&& !streq(result
, "done")) {
2749 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
2751 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit '%s' failed with '%s'", unit
, result
);
2752 return session_send_create_reply(s
, &e
);
2755 return session_send_create_reply(s
, NULL
);
2758 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2759 const char *path
, *result
, *unit
;
2760 Manager
*m
= userdata
;
2769 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
2771 bus_log_parse_error(r
);
2775 if (m
->action_job
&& streq(m
->action_job
, path
)) {
2776 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
2778 /* Tell people that they now may take a lock again */
2779 (void) send_prepare_for(m
, m
->action_what
, false);
2781 m
->action_job
= mfree(m
->action_job
);
2782 m
->action_unit
= NULL
;
2787 session
= hashmap_get(m
->session_units
, unit
);
2789 if (streq_ptr(path
, session
->scope_job
)) {
2790 session
->scope_job
= mfree(session
->scope_job
);
2791 (void) session_jobs_reply(session
, unit
, result
);
2793 session_save(session
);
2794 user_save(session
->user
);
2797 session_add_to_gc_queue(session
);
2800 user
= hashmap_get(m
->user_units
, unit
);
2802 if (streq_ptr(path
, user
->service_job
)) {
2803 user
->service_job
= mfree(user
->service_job
);
2805 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
2806 (void) session_jobs_reply(session
, unit
, NULL
/* don't propagate user service failures to the client */);
2811 user_add_to_gc_queue(user
);
2817 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2818 const char *path
, *unit
;
2819 Manager
*m
= userdata
;
2827 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
2829 bus_log_parse_error(r
);
2833 session
= hashmap_get(m
->session_units
, unit
);
2835 session_add_to_gc_queue(session
);
2837 user
= hashmap_get(m
->user_units
, unit
);
2839 user_add_to_gc_queue(user
);
2844 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2845 _cleanup_free_
char *unit
= NULL
;
2846 Manager
*m
= userdata
;
2855 path
= sd_bus_message_get_path(message
);
2859 r
= unit_name_from_dbus_path(path
, &unit
);
2860 if (r
== -EINVAL
) /* not a unit */
2867 session
= hashmap_get(m
->session_units
, unit
);
2869 session_add_to_gc_queue(session
);
2871 user
= hashmap_get(m
->user_units
, unit
);
2873 user_add_to_gc_queue(user
);
2878 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2879 Manager
*m
= userdata
;
2887 r
= sd_bus_message_read(message
, "b", &b
);
2889 bus_log_parse_error(r
);
2896 /* systemd finished reloading, let's recheck all our sessions */
2897 log_debug("System manager has been reloaded, rechecking sessions...");
2899 HASHMAP_FOREACH(session
, m
->sessions
, i
)
2900 session_add_to_gc_queue(session
);
2905 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
2910 l
= strv_from_stdarg_alloca(property
);
2912 return sd_bus_emit_properties_changed_strv(
2914 "/org/freedesktop/login1",
2915 "org.freedesktop.login1.Manager",
2919 static int strdup_job(sd_bus_message
*reply
, char **job
) {
2924 r
= sd_bus_message_read(reply
, "o", &j
);
2936 int manager_start_scope(
2941 const char *description
,
2944 const char *requires_mounts_for
,
2945 sd_bus_message
*more_properties
,
2946 sd_bus_error
*error
,
2949 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2958 r
= sd_bus_message_new_method_call(
2961 "org.freedesktop.systemd1",
2962 "/org/freedesktop/systemd1",
2963 "org.freedesktop.systemd1.Manager",
2964 "StartTransientUnit");
2968 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
2972 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
2976 if (!isempty(slice
)) {
2977 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
2982 if (!isempty(description
)) {
2983 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
2988 STRV_FOREACH(i
, wants
) {
2989 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
2994 STRV_FOREACH(i
, after
) {
2995 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3000 if (!empty_or_root(requires_mounts_for
)) {
3001 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3006 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3008 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3012 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3016 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3017 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
3019 return bus_log_create_error(r
);
3021 if (more_properties
) {
3022 /* If TasksMax also appears here, it will overwrite the default value set above */
3023 r
= sd_bus_message_copy(m
, more_properties
, true);
3028 r
= sd_bus_message_close_container(m
);
3032 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3036 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3040 return strdup_job(reply
, job
);
3043 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3044 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3051 r
= sd_bus_call_method(
3053 "org.freedesktop.systemd1",
3054 "/org/freedesktop/systemd1",
3055 "org.freedesktop.systemd1.Manager",
3059 "ss", unit
, "replace");
3063 return strdup_job(reply
, job
);
3066 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3067 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3074 r
= sd_bus_call_method(
3076 "org.freedesktop.systemd1",
3077 "/org/freedesktop/systemd1",
3078 "org.freedesktop.systemd1.Manager",
3082 "ss", unit
, "fail");
3084 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3085 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
3088 sd_bus_error_free(error
);
3095 return strdup_job(reply
, job
);
3098 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
3099 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3100 _cleanup_free_
char *path
= NULL
;
3106 path
= unit_dbus_path_from_name(scope
);
3110 r
= sd_bus_call_method(
3112 "org.freedesktop.systemd1",
3114 "org.freedesktop.systemd1.Scope",
3120 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3121 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
3122 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
3125 sd_bus_error_move(ret_error
, &error
);
3132 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
3136 return sd_bus_call_method(
3138 "org.freedesktop.systemd1",
3139 "/org/freedesktop/systemd1",
3140 "org.freedesktop.systemd1.Manager",
3144 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
3147 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
3148 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3149 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3150 _cleanup_free_
char *path
= NULL
;
3157 path
= unit_dbus_path_from_name(unit
);
3161 r
= sd_bus_get_property(
3163 "org.freedesktop.systemd1",
3165 "org.freedesktop.systemd1.Unit",
3171 /* systemd might have droppped off momentarily, let's
3172 * not make this an error */
3173 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3174 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3177 /* If the unit is already unloaded then it's not
3179 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3180 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
3183 sd_bus_error_move(ret_error
, &error
);
3187 r
= sd_bus_message_read(reply
, "s", &state
);
3191 return !STR_IN_SET(state
, "inactive", "failed");
3194 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
3195 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3196 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3202 r
= sd_bus_get_property(
3204 "org.freedesktop.systemd1",
3206 "org.freedesktop.systemd1.Job",
3212 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3213 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3216 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
3219 sd_bus_error_move(ret_error
, &error
);
3223 /* We don't actually care about the state really. The fact
3224 * that we could read the job state is enough for us */