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"
25 #include "format-util.h"
28 #include "missing_capability.h"
30 #include "path-util.h"
31 #include "process-util.h"
32 #include "selinux-util.h"
33 #include "sleep-config.h"
36 #include "terminal-util.h"
37 #include "tmpfile-util.h"
38 #include "unit-name.h"
39 #include "user-util.h"
40 #include "utmp-wtmp.h"
42 static int get_sender_session(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, Session
**ret
) {
44 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
49 /* Get client login session. This is not what you are looking for these days,
50 * as apps may instead belong to a user service unit. This includes terminal
51 * emulators and hence command-line apps. */
52 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_SESSION
|SD_BUS_CREDS_AUGMENT
, &creds
);
56 r
= sd_bus_creds_get_session(creds
, &name
);
62 session
= hashmap_get(m
->sessions
, name
);
70 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
,
71 "Caller does not belong to any known session");
74 int manager_get_session_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Session
**ret
) {
82 return get_sender_session(m
, message
, error
, ret
);
84 session
= hashmap_get(m
->sessions
, name
);
86 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SESSION
, "No session '%s' known", name
);
92 static int get_sender_user(Manager
*m
, sd_bus_message
*message
, sd_bus_error
*error
, User
**ret
) {
94 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
99 /* Note that we get the owner UID of the session, not the actual client UID here! */
100 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
104 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
110 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
118 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
, "Caller does not belong to any logged in user or lingering user");
121 int manager_get_user_from_creds(Manager
*m
, sd_bus_message
*message
, uid_t uid
, sd_bus_error
*error
, User
**ret
) {
128 if (!uid_is_valid(uid
))
129 return get_sender_user(m
, message
, error
, ret
);
131 user
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
133 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER
, "User ID "UID_FMT
" is not logged in or lingering", uid
);
139 int manager_get_seat_from_creds(Manager
*m
, sd_bus_message
*message
, const char *name
, sd_bus_error
*error
, Seat
**ret
) {
150 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
154 seat
= session
->seat
;
156 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "Session has no seat.");
158 seat
= hashmap_get(m
->seats
, name
);
160 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", name
);
167 static int property_get_idle_hint(
170 const char *interface
,
171 const char *property
,
172 sd_bus_message
*reply
,
174 sd_bus_error
*error
) {
176 Manager
*m
= userdata
;
182 return sd_bus_message_append(reply
, "b", manager_get_idle_hint(m
, NULL
) > 0);
185 static int property_get_idle_since_hint(
188 const char *interface
,
189 const char *property
,
190 sd_bus_message
*reply
,
192 sd_bus_error
*error
) {
194 Manager
*m
= userdata
;
195 dual_timestamp t
= DUAL_TIMESTAMP_NULL
;
201 manager_get_idle_hint(m
, &t
);
203 return sd_bus_message_append(reply
, "t", streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
);
206 static int property_get_inhibited(
209 const char *interface
,
210 const char *property
,
211 sd_bus_message
*reply
,
213 sd_bus_error
*error
) {
215 Manager
*m
= userdata
;
222 w
= manager_inhibit_what(m
, streq(property
, "BlockInhibited") ? INHIBIT_BLOCK
: INHIBIT_DELAY
);
224 return sd_bus_message_append(reply
, "s", inhibit_what_to_string(w
));
227 static int property_get_preparing(
230 const char *interface
,
231 const char *property
,
232 sd_bus_message
*reply
,
234 sd_bus_error
*error
) {
236 Manager
*m
= userdata
;
243 if (streq(property
, "PreparingForShutdown"))
244 b
= m
->action_what
& INHIBIT_SHUTDOWN
;
246 b
= m
->action_what
& INHIBIT_SLEEP
;
248 return sd_bus_message_append(reply
, "b", b
);
251 static int property_get_scheduled_shutdown(
254 const char *interface
,
255 const char *property
,
256 sd_bus_message
*reply
,
258 sd_bus_error
*error
) {
260 Manager
*m
= userdata
;
267 r
= sd_bus_message_open_container(reply
, 'r', "st");
271 r
= sd_bus_message_append(reply
, "st", m
->scheduled_shutdown_type
, m
->scheduled_shutdown_timeout
);
275 return sd_bus_message_close_container(reply
);
278 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action
, handle_action
, HandleAction
);
279 static BUS_DEFINE_PROPERTY_GET(property_get_docked
, "b", Manager
, manager_is_docked_or_external_displays
);
280 static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed
, "b", Manager
, manager_is_lid_closed
);
281 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power
, "b", manager_is_on_external_power
);
282 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max
, "t", CGROUP_LIMIT_MAX
);
283 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size
, "t", Hashmap
*, (uint64_t) hashmap_size
);
285 static int method_get_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
286 _cleanup_free_
char *p
= NULL
;
287 Manager
*m
= userdata
;
295 r
= sd_bus_message_read(message
, "s", &name
);
299 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
303 p
= session_bus_path(session
);
307 return sd_bus_reply_method_return(message
, "o", p
);
310 /* Get login session of a process. This is not what you are looking for these days,
311 * as apps may instead belong to a user service unit. This includes terminal
312 * emulators and hence command-line apps. */
313 static int method_get_session_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
314 _cleanup_free_
char *p
= NULL
;
315 Session
*session
= NULL
;
316 Manager
*m
= userdata
;
323 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
325 r
= sd_bus_message_read(message
, "u", &pid
);
332 r
= manager_get_session_from_creds(m
, message
, NULL
, error
, &session
);
336 r
= manager_get_session_by_pid(m
, pid
, &session
);
341 return sd_bus_error_setf(error
, BUS_ERROR_NO_SESSION_FOR_PID
, "PID "PID_FMT
" does not belong to any known session", pid
);
344 p
= session_bus_path(session
);
348 return sd_bus_reply_method_return(message
, "o", p
);
351 static int method_get_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
352 _cleanup_free_
char *p
= NULL
;
353 Manager
*m
= userdata
;
361 r
= sd_bus_message_read(message
, "u", &uid
);
365 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
369 p
= user_bus_path(user
);
373 return sd_bus_reply_method_return(message
, "o", p
);
376 static int method_get_user_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
377 _cleanup_free_
char *p
= NULL
;
378 Manager
*m
= userdata
;
386 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
388 r
= sd_bus_message_read(message
, "u", &pid
);
395 r
= manager_get_user_from_creds(m
, message
, UID_INVALID
, error
, &user
);
399 r
= manager_get_user_by_pid(m
, pid
, &user
);
403 return sd_bus_error_setf(error
, BUS_ERROR_NO_USER_FOR_PID
,
404 "PID "PID_FMT
" does not belong to any logged in user or lingering user",
408 p
= user_bus_path(user
);
412 return sd_bus_reply_method_return(message
, "o", p
);
415 static int method_get_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
416 _cleanup_free_
char *p
= NULL
;
417 Manager
*m
= userdata
;
425 r
= sd_bus_message_read(message
, "s", &name
);
429 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
433 p
= seat_bus_path(seat
);
437 return sd_bus_reply_method_return(message
, "o", p
);
440 static int method_list_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
441 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
442 Manager
*m
= userdata
;
450 r
= sd_bus_message_new_method_return(message
, &reply
);
454 r
= sd_bus_message_open_container(reply
, 'a', "(susso)");
458 HASHMAP_FOREACH(session
, m
->sessions
, i
) {
459 _cleanup_free_
char *p
= NULL
;
461 p
= session_bus_path(session
);
465 r
= sd_bus_message_append(reply
, "(susso)",
467 (uint32_t) session
->user
->uid
,
469 session
->seat
? session
->seat
->id
: "",
475 r
= sd_bus_message_close_container(reply
);
479 return sd_bus_send(NULL
, reply
, NULL
);
482 static int method_list_users(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
483 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
484 Manager
*m
= userdata
;
492 r
= sd_bus_message_new_method_return(message
, &reply
);
496 r
= sd_bus_message_open_container(reply
, 'a', "(uso)");
500 HASHMAP_FOREACH(user
, m
->users
, i
) {
501 _cleanup_free_
char *p
= NULL
;
503 p
= user_bus_path(user
);
507 r
= sd_bus_message_append(reply
, "(uso)",
508 (uint32_t) user
->uid
,
515 r
= sd_bus_message_close_container(reply
);
519 return sd_bus_send(NULL
, reply
, NULL
);
522 static int method_list_seats(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
523 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
524 Manager
*m
= userdata
;
532 r
= sd_bus_message_new_method_return(message
, &reply
);
536 r
= sd_bus_message_open_container(reply
, 'a', "(so)");
540 HASHMAP_FOREACH(seat
, m
->seats
, i
) {
541 _cleanup_free_
char *p
= NULL
;
543 p
= seat_bus_path(seat
);
547 r
= sd_bus_message_append(reply
, "(so)", seat
->id
, p
);
552 r
= sd_bus_message_close_container(reply
);
556 return sd_bus_send(NULL
, reply
, NULL
);
559 static int method_list_inhibitors(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
560 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
561 Manager
*m
= userdata
;
562 Inhibitor
*inhibitor
;
569 r
= sd_bus_message_new_method_return(message
, &reply
);
573 r
= sd_bus_message_open_container(reply
, 'a', "(ssssuu)");
577 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
579 r
= sd_bus_message_append(reply
, "(ssssuu)",
580 strempty(inhibit_what_to_string(inhibitor
->what
)),
581 strempty(inhibitor
->who
),
582 strempty(inhibitor
->why
),
583 strempty(inhibit_mode_to_string(inhibitor
->mode
)),
584 (uint32_t) inhibitor
->uid
,
585 (uint32_t) inhibitor
->pid
);
590 r
= sd_bus_message_close_container(reply
);
594 return sd_bus_send(NULL
, reply
, NULL
);
597 static int method_create_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
598 const char *service
, *type
, *class, *cseat
, *tty
, *display
, *remote_user
, *remote_host
, *desktop
;
599 _cleanup_free_
char *id
= NULL
;
600 Session
*session
= NULL
;
601 uint32_t audit_id
= 0;
602 Manager
*m
= userdata
;
616 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
617 assert_cc(sizeof(uid_t
) == sizeof(uint32_t));
619 r
= sd_bus_message_read(message
, "uusssssussbss", &uid
, &leader
, &service
, &type
, &class, &desktop
, &cseat
, &vtnr
, &tty
, &display
, &remote
, &remote_user
, &remote_host
);
623 if (!uid_is_valid(uid
))
624 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid UID");
625 if (leader
< 0 || leader
== 1 || leader
== getpid_cached())
626 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
629 t
= _SESSION_TYPE_INVALID
;
631 t
= session_type_from_string(type
);
633 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid session type %s", type
);
637 c
= _SESSION_CLASS_INVALID
;
639 c
= session_class_from_string(class);
641 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid session class %s", class);
644 if (isempty(desktop
))
647 if (!string_is_safe(desktop
))
648 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid desktop string %s", desktop
);
654 seat
= hashmap_get(m
->seats
, cseat
);
656 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_SEAT
, "No seat '%s' known", cseat
);
659 if (tty_is_vc(tty
)) {
664 else if (seat
!= m
->seat0
)
665 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
);
667 v
= vtnr_from_tty(tty
);
669 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Cannot determine VT number from virtual console TTY %s", tty
);
673 else if (vtnr
!= (uint32_t) v
)
674 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified TTY and VT number do not match");
676 } else if (tty_is_console(tty
)) {
680 else if (seat
!= m
->seat0
)
681 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Console TTY specified but seat is not seat0");
684 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Console TTY specified but VT number is not 0");
688 if (seat_has_vts(seat
)) {
689 if (vtnr
<= 0 || vtnr
> 63)
690 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "VT number out of range");
693 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat has no VTs but VT number not 0");
697 if (t
== _SESSION_TYPE_INVALID
) {
698 if (!isempty(display
))
700 else if (!isempty(tty
))
703 t
= SESSION_UNSPECIFIED
;
706 if (c
== _SESSION_CLASS_INVALID
) {
707 if (t
== SESSION_UNSPECIFIED
)
708 c
= SESSION_BACKGROUND
;
714 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
716 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
720 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
725 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
726 * that avoids creating a logind session. */
727 r
= manager_get_user_by_pid(m
, leader
, NULL
);
731 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already running in a session or user slice");
734 * Old gdm and lightdm start the user-session on the same VT as
735 * the greeter session. But they destroy the greeter session
736 * after the user-session and want the user-session to take
737 * over the VT. We need to support this for
738 * backwards-compatibility, so make sure we allow new sessions
739 * on a VT that a greeter is running on. Furthermore, to allow
740 * re-logins, we have to allow a greeter to take over a used VT for
741 * the exact same reasons.
743 if (c
!= SESSION_GREETER
&&
745 vtnr
< m
->seat0
->position_count
&&
746 m
->seat0
->positions
[vtnr
] &&
747 m
->seat0
->positions
[vtnr
]->class != SESSION_GREETER
)
748 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_BUSY
, "Already occupied by a session");
750 if (hashmap_size(m
->sessions
) >= m
->sessions_max
)
751 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of sessions (%" PRIu64
") reached, refusing further sessions.", m
->sessions_max
);
753 (void) audit_session_from_pid(leader
, &audit_id
);
754 if (audit_session_is_valid(audit_id
)) {
755 /* Keep our session IDs and the audit session IDs in sync */
757 if (asprintf(&id
, "%"PRIu32
, audit_id
) < 0)
760 /* Wut? There's already a session by this name and we
761 * didn't find it above? Weird, then let's not trust
762 * the audit data and let's better register a new
764 if (hashmap_get(m
->sessions
, id
)) {
765 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id
);
766 audit_id
= AUDIT_SESSION_INVALID
;
775 if (asprintf(&id
, "c%lu", ++m
->session_counter
) < 0)
778 } while (hashmap_get(m
->sessions
, id
));
781 /* If we are not watching utmp aleady, try again */
782 manager_reconnect_utmp(m
);
784 r
= manager_add_user_by_uid(m
, uid
, &user
);
788 r
= manager_add_session(m
, id
, &session
);
792 session_set_user(session
, user
);
793 session_set_leader(session
, leader
);
797 session
->remote
= remote
;
798 session
->vtnr
= vtnr
;
801 session
->tty
= strdup(tty
);
807 session
->tty_validity
= TTY_FROM_PAM
;
810 if (!isempty(display
)) {
811 session
->display
= strdup(display
);
812 if (!session
->display
) {
818 if (!isempty(remote_user
)) {
819 session
->remote_user
= strdup(remote_user
);
820 if (!session
->remote_user
) {
826 if (!isempty(remote_host
)) {
827 session
->remote_host
= strdup(remote_host
);
828 if (!session
->remote_host
) {
834 if (!isempty(service
)) {
835 session
->service
= strdup(service
);
836 if (!session
->service
) {
842 if (!isempty(desktop
)) {
843 session
->desktop
= strdup(desktop
);
844 if (!session
->desktop
) {
851 r
= seat_attach_session(seat
, session
);
856 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
860 r
= session_start(session
, message
, error
);
864 r
= sd_bus_message_exit_container(message
);
868 session
->create_message
= sd_bus_message_ref(message
);
870 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
871 * session_send_create_reply(). */
877 session_add_to_gc_queue(session
);
880 user_add_to_gc_queue(user
);
885 static int method_release_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
886 Manager
*m
= userdata
;
894 r
= sd_bus_message_read(message
, "s", &name
);
898 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
902 r
= session_release(session
);
906 return sd_bus_reply_method_return(message
, NULL
);
909 static int method_activate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
910 Manager
*m
= userdata
;
918 r
= sd_bus_message_read(message
, "s", &name
);
922 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
926 return bus_session_method_activate(message
, session
, error
);
929 static int method_activate_session_on_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
930 const char *session_name
, *seat_name
;
931 Manager
*m
= userdata
;
939 /* Same as ActivateSession() but refuses to work if
940 * the seat doesn't match */
942 r
= sd_bus_message_read(message
, "ss", &session_name
, &seat_name
);
946 r
= manager_get_session_from_creds(m
, message
, session_name
, error
, &session
);
950 r
= manager_get_seat_from_creds(m
, message
, seat_name
, error
, &seat
);
954 if (session
->seat
!= seat
)
955 return sd_bus_error_setf(error
, BUS_ERROR_SESSION_NOT_ON_SEAT
, "Session %s not on seat %s", session_name
, seat_name
);
957 r
= session_activate(session
);
961 return sd_bus_reply_method_return(message
, NULL
);
964 static int method_lock_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
965 Manager
*m
= userdata
;
973 r
= sd_bus_message_read(message
, "s", &name
);
977 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
981 return bus_session_method_lock(message
, session
, error
);
984 static int method_lock_sessions(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
985 Manager
*m
= userdata
;
991 r
= bus_verify_polkit_async(
994 "org.freedesktop.login1.lock-sessions",
1003 return 1; /* Will call us back */
1005 r
= session_send_lock_all(m
, streq(sd_bus_message_get_member(message
), "LockSessions"));
1009 return sd_bus_reply_method_return(message
, NULL
);
1012 static int method_kill_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1014 Manager
*m
= userdata
;
1021 r
= sd_bus_message_read(message
, "s", &name
);
1025 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1029 return bus_session_method_kill(message
, session
, error
);
1032 static int method_kill_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1033 Manager
*m
= userdata
;
1041 r
= sd_bus_message_read(message
, "u", &uid
);
1045 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1049 return bus_user_method_kill(message
, user
, error
);
1052 static int method_terminate_session(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1053 Manager
*m
= userdata
;
1061 r
= sd_bus_message_read(message
, "s", &name
);
1065 r
= manager_get_session_from_creds(m
, message
, name
, error
, &session
);
1069 return bus_session_method_terminate(message
, session
, error
);
1072 static int method_terminate_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1073 Manager
*m
= userdata
;
1081 r
= sd_bus_message_read(message
, "u", &uid
);
1085 r
= manager_get_user_from_creds(m
, message
, uid
, error
, &user
);
1089 return bus_user_method_terminate(message
, user
, error
);
1092 static int method_terminate_seat(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1093 Manager
*m
= userdata
;
1101 r
= sd_bus_message_read(message
, "s", &name
);
1105 r
= manager_get_seat_from_creds(m
, message
, name
, error
, &seat
);
1109 return bus_seat_method_terminate(message
, seat
, error
);
1112 static int method_set_user_linger(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1113 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1114 _cleanup_free_
char *cc
= NULL
;
1115 Manager
*m
= userdata
;
1116 int r
, b
, interactive
;
1119 uint32_t uid
, auth_uid
;
1124 r
= sd_bus_message_read(message
, "ubb", &uid
, &b
, &interactive
);
1128 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|
1129 SD_BUS_CREDS_OWNER_UID
|SD_BUS_CREDS_AUGMENT
, &creds
);
1133 if (!uid_is_valid(uid
)) {
1134 /* Note that we get the owner UID of the session or user unit,
1135 * not the actual client UID here! */
1136 r
= sd_bus_creds_get_owner_uid(creds
, &uid
);
1141 /* owner_uid is racy, so for authorization we must use euid */
1142 r
= sd_bus_creds_get_euid(creds
, &auth_uid
);
1149 return errno
> 0 ? -errno
: -ENOENT
;
1151 r
= bus_verify_polkit_async(
1154 uid
== auth_uid
? "org.freedesktop.login1.set-self-linger" :
1155 "org.freedesktop.login1.set-user-linger",
1159 &m
->polkit_registry
,
1164 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1166 mkdir_p_label("/var/lib/systemd", 0755);
1168 r
= mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE
);
1172 cc
= cescape(pw
->pw_name
);
1176 path
= strjoina("/var/lib/systemd/linger/", cc
);
1184 if (manager_add_user_by_uid(m
, uid
, &u
) >= 0)
1191 if (r
< 0 && errno
!= ENOENT
)
1194 u
= hashmap_get(m
->users
, UID_TO_PTR(uid
));
1196 user_add_to_gc_queue(u
);
1199 return sd_bus_reply_method_return(message
, NULL
);
1202 static int trigger_device(Manager
*m
, sd_device
*d
) {
1203 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1208 r
= sd_device_enumerator_new(&e
);
1212 r
= sd_device_enumerator_allow_uninitialized(e
);
1217 r
= sd_device_enumerator_add_match_parent(e
, d
);
1222 FOREACH_DEVICE(e
, d
) {
1223 _cleanup_free_
char *t
= NULL
;
1226 r
= sd_device_get_syspath(d
, &p
);
1230 t
= strappend(p
, "/uevent");
1234 (void) write_string_file(t
, "change", WRITE_STRING_FILE_DISABLE_BUFFER
);
1240 static int attach_device(Manager
*m
, const char *seat
, const char *sysfs
) {
1241 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1242 _cleanup_free_
char *rule
= NULL
, *file
= NULL
;
1243 const char *id_for_seat
;
1250 r
= sd_device_new_from_syspath(&d
, sysfs
);
1254 if (sd_device_has_tag(d
, "seat") <= 0)
1257 if (sd_device_get_property_value(d
, "ID_FOR_SEAT", &id_for_seat
) < 0)
1260 if (asprintf(&file
, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat
) < 0)
1263 if (asprintf(&rule
, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat
, seat
) < 0)
1266 (void) mkdir_p_label("/etc/udev/rules.d", 0755);
1267 r
= write_string_file_atomic_label(file
, rule
);
1271 return trigger_device(m
, d
);
1274 static int flush_devices(Manager
*m
) {
1275 _cleanup_closedir_
DIR *d
;
1279 d
= opendir("/etc/udev/rules.d");
1281 if (errno
!= ENOENT
)
1282 log_warning_errno(errno
, "Failed to open /etc/udev/rules.d: %m");
1286 FOREACH_DIRENT_ALL(de
, d
, break) {
1287 if (!dirent_is_file(de
))
1290 if (!startswith(de
->d_name
, "72-seat-"))
1293 if (!endswith(de
->d_name
, ".rules"))
1296 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
1297 log_warning_errno(errno
, "Failed to unlink %s: %m", de
->d_name
);
1301 return trigger_device(m
, NULL
);
1304 static int method_attach_device(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1305 const char *sysfs
, *seat
;
1306 Manager
*m
= userdata
;
1312 r
= sd_bus_message_read(message
, "ssb", &seat
, &sysfs
, &interactive
);
1316 if (!path_startswith(sysfs
, "/sys"))
1317 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Path %s is not in /sys", sysfs
);
1319 if (!seat_name_is_valid(seat
))
1320 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Seat %s is not valid", seat
);
1322 r
= bus_verify_polkit_async(
1325 "org.freedesktop.login1.attach-device",
1329 &m
->polkit_registry
,
1334 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1336 r
= attach_device(m
, seat
, sysfs
);
1340 return sd_bus_reply_method_return(message
, NULL
);
1343 static int method_flush_devices(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1344 Manager
*m
= userdata
;
1350 r
= sd_bus_message_read(message
, "b", &interactive
);
1354 r
= bus_verify_polkit_async(
1357 "org.freedesktop.login1.flush-devices",
1361 &m
->polkit_registry
,
1366 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1368 r
= flush_devices(m
);
1372 return sd_bus_reply_method_return(message
, NULL
);
1375 static int have_multiple_sessions(
1384 /* Check for other users' sessions. Greeter sessions do not
1385 * count, and non-login sessions do not count either. */
1386 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1387 if (session
->class == SESSION_USER
&&
1388 session
->user
->uid
!= uid
)
1394 static int bus_manager_log_shutdown(
1396 const char *unit_name
) {
1403 if (streq(unit_name
, SPECIAL_POWEROFF_TARGET
)) {
1404 p
= "MESSAGE=System is powering down";
1405 q
= "SHUTDOWN=power-off";
1406 } else if (streq(unit_name
, SPECIAL_REBOOT_TARGET
)) {
1407 p
= "MESSAGE=System is rebooting";
1408 q
= "SHUTDOWN=reboot";
1409 } else if (streq(unit_name
, SPECIAL_HALT_TARGET
)) {
1410 p
= "MESSAGE=System is halting";
1411 q
= "SHUTDOWN=halt";
1412 } else if (streq(unit_name
, SPECIAL_KEXEC_TARGET
)) {
1413 p
= "MESSAGE=System is rebooting with kexec";
1414 q
= "SHUTDOWN=kexec";
1416 p
= "MESSAGE=System is shutting down";
1420 if (isempty(m
->wall_message
))
1421 p
= strjoina(p
, ".");
1423 p
= strjoina(p
, " (", m
->wall_message
, ").");
1425 return log_struct(LOG_NOTICE
,
1426 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR
,
1431 static int lid_switch_ignore_handler(sd_event_source
*e
, uint64_t usec
, void *userdata
) {
1432 Manager
*m
= userdata
;
1437 m
->lid_switch_ignore_event_source
= sd_event_source_unref(m
->lid_switch_ignore_event_source
);
1441 int manager_set_lid_switch_ignore(Manager
*m
, usec_t until
) {
1446 if (until
<= now(CLOCK_MONOTONIC
))
1449 /* We want to ignore the lid switch for a while after each
1450 * suspend, and after boot-up. Hence let's install a timer for
1451 * this. As long as the event source exists we ignore the lid
1454 if (m
->lid_switch_ignore_event_source
) {
1457 r
= sd_event_source_get_time(m
->lid_switch_ignore_event_source
, &u
);
1464 r
= sd_event_source_set_time(m
->lid_switch_ignore_event_source
, until
);
1466 r
= sd_event_add_time(
1468 &m
->lid_switch_ignore_event_source
,
1471 lid_switch_ignore_handler
, m
);
1476 static int send_prepare_for(Manager
*m
, InhibitWhat w
, bool _active
) {
1478 static const char * const signal_name
[_INHIBIT_WHAT_MAX
] = {
1479 [INHIBIT_SHUTDOWN
] = "PrepareForShutdown",
1480 [INHIBIT_SLEEP
] = "PrepareForSleep"
1483 int active
= _active
;
1487 assert(w
< _INHIBIT_WHAT_MAX
);
1488 assert(signal_name
[w
]);
1490 return sd_bus_emit_signal(m
->bus
,
1491 "/org/freedesktop/login1",
1492 "org.freedesktop.login1.Manager",
1498 static int execute_shutdown_or_sleep(
1501 const char *unit_name
,
1502 sd_bus_error
*error
) {
1504 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1511 assert(w
< _INHIBIT_WHAT_MAX
);
1514 if (w
== INHIBIT_SHUTDOWN
)
1515 bus_manager_log_shutdown(m
, unit_name
);
1517 r
= sd_bus_call_method(
1519 "org.freedesktop.systemd1",
1520 "/org/freedesktop/systemd1",
1521 "org.freedesktop.systemd1.Manager",
1525 "ss", unit_name
, "replace-irreversibly");
1529 r
= sd_bus_message_read(reply
, "o", &p
);
1539 m
->action_unit
= unit_name
;
1540 free(m
->action_job
);
1544 /* Make sure the lid switch is ignored for a while */
1545 manager_set_lid_switch_ignore(m
, now(CLOCK_MONOTONIC
) + m
->holdoff_timeout_usec
);
1550 /* Tell people that they now may take a lock again */
1551 (void) send_prepare_for(m
, w
, false);
1556 int manager_dispatch_delayed(Manager
*manager
, bool timeout
) {
1558 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1559 Inhibitor
*offending
= NULL
;
1564 if (manager
->action_what
== 0 || manager
->action_job
)
1567 if (manager_is_inhibited(manager
, manager
->action_what
, INHIBIT_DELAY
, NULL
, false, false, 0, &offending
)) {
1568 _cleanup_free_
char *comm
= NULL
, *u
= NULL
;
1573 (void) get_process_comm(offending
->pid
, &comm
);
1574 u
= uid_to_name(offending
->uid
);
1576 log_notice("Delay lock is active (UID "UID_FMT
"/%s, PID "PID_FMT
"/%s) but inhibitor timeout is reached.",
1577 offending
->uid
, strna(u
),
1578 offending
->pid
, strna(comm
));
1581 /* Actually do the operation */
1582 r
= execute_shutdown_or_sleep(manager
, manager
->action_what
, manager
->action_unit
, &error
);
1584 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1585 bus_error_message(&error
, r
));
1587 manager
->action_unit
= NULL
;
1588 manager
->action_what
= 0;
1595 static int manager_inhibit_timeout_handler(
1600 Manager
*manager
= userdata
;
1604 assert(manager
->inhibit_timeout_source
== s
);
1606 r
= manager_dispatch_delayed(manager
, true);
1607 return (r
< 0) ? r
: 0;
1610 static int delay_shutdown_or_sleep(
1613 const char *unit_name
) {
1620 assert(w
< _INHIBIT_WHAT_MAX
);
1623 timeout_val
= now(CLOCK_MONOTONIC
) + m
->inhibit_delay_max
;
1625 if (m
->inhibit_timeout_source
) {
1626 r
= sd_event_source_set_time(m
->inhibit_timeout_source
, timeout_val
);
1628 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
1630 r
= sd_event_source_set_enabled(m
->inhibit_timeout_source
, SD_EVENT_ONESHOT
);
1632 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
1634 r
= sd_event_add_time(m
->event
, &m
->inhibit_timeout_source
, CLOCK_MONOTONIC
,
1635 timeout_val
, 0, manager_inhibit_timeout_handler
, m
);
1640 m
->action_unit
= unit_name
;
1646 int bus_manager_shutdown_or_sleep_now_or_later(
1648 const char *unit_name
,
1650 sd_bus_error
*error
) {
1652 _cleanup_free_
char *load_state
= NULL
;
1659 assert(w
<= _INHIBIT_WHAT_MAX
);
1660 assert(!m
->action_job
);
1662 r
= unit_load_state(m
->bus
, unit_name
, &load_state
);
1666 if (!streq(load_state
, "loaded"))
1667 return log_notice_errno(SYNTHETIC_ERRNO(EACCES
),
1668 "Unit %s is %s, refusing operation.",
1669 unit_name
, load_state
);
1671 /* Tell everybody to prepare for shutdown/sleep */
1672 (void) send_prepare_for(m
, w
, true);
1675 m
->inhibit_delay_max
> 0 &&
1676 manager_is_inhibited(m
, w
, INHIBIT_DELAY
, NULL
, false, false, 0, NULL
);
1679 /* Shutdown is delayed, keep in mind what we
1680 * want to do, and start a timeout */
1681 r
= delay_shutdown_or_sleep(m
, w
, unit_name
);
1683 /* Shutdown is not delayed, execute it
1685 r
= execute_shutdown_or_sleep(m
, w
, unit_name
, error
);
1690 static int verify_shutdown_creds(
1692 sd_bus_message
*message
,
1696 const char *action_multiple_sessions
,
1697 const char *action_ignore_inhibit
,
1698 sd_bus_error
*error
) {
1700 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1701 bool multiple_sessions
, blocked
;
1708 assert(w
<= _INHIBIT_WHAT_MAX
);
1710 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
1714 r
= sd_bus_creds_get_euid(creds
, &uid
);
1718 r
= have_multiple_sessions(m
, uid
);
1722 multiple_sessions
= r
> 0;
1723 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
1725 if (multiple_sessions
&& action_multiple_sessions
) {
1726 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1730 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1733 if (blocked
&& action_ignore_inhibit
) {
1734 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1738 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1741 if (!multiple_sessions
&& !blocked
&& action
) {
1742 r
= bus_verify_polkit_async(message
, CAP_SYS_BOOT
, action
, NULL
, interactive
, UID_INVALID
, &m
->polkit_registry
, error
);
1746 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1752 static int method_do_shutdown_or_sleep(
1754 sd_bus_message
*message
,
1755 const char *unit_name
,
1758 const char *action_multiple_sessions
,
1759 const char *action_ignore_inhibit
,
1760 const char *sleep_verb
,
1761 sd_bus_error
*error
) {
1769 assert(w
<= _INHIBIT_WHAT_MAX
);
1771 r
= sd_bus_message_read(message
, "b", &interactive
);
1775 /* Don't allow multiple jobs being executed at the same time */
1777 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
, "There's already a shutdown or sleep operation in progress");
1780 r
= can_sleep(sleep_verb
);
1782 return sd_bus_error_set(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1783 "Not enough swap space for hibernation");
1785 return sd_bus_error_setf(error
, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED
,
1786 "Sleep verb \"%s\" not supported", sleep_verb
);
1791 r
= verify_shutdown_creds(m
, message
, w
, interactive
, action
, action_multiple_sessions
,
1792 action_ignore_inhibit
, error
);
1796 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, unit_name
, w
, error
);
1800 return sd_bus_reply_method_return(message
, NULL
);
1803 static int method_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1804 Manager
*m
= userdata
;
1806 return method_do_shutdown_or_sleep(
1808 SPECIAL_POWEROFF_TARGET
,
1810 "org.freedesktop.login1.power-off",
1811 "org.freedesktop.login1.power-off-multiple-sessions",
1812 "org.freedesktop.login1.power-off-ignore-inhibit",
1817 static int method_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1818 Manager
*m
= userdata
;
1820 return method_do_shutdown_or_sleep(
1822 SPECIAL_REBOOT_TARGET
,
1824 "org.freedesktop.login1.reboot",
1825 "org.freedesktop.login1.reboot-multiple-sessions",
1826 "org.freedesktop.login1.reboot-ignore-inhibit",
1831 static int method_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1832 Manager
*m
= userdata
;
1834 return method_do_shutdown_or_sleep(
1836 SPECIAL_HALT_TARGET
,
1838 "org.freedesktop.login1.halt",
1839 "org.freedesktop.login1.halt-multiple-sessions",
1840 "org.freedesktop.login1.halt-ignore-inhibit",
1845 static int method_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1846 Manager
*m
= userdata
;
1848 return method_do_shutdown_or_sleep(
1850 SPECIAL_SUSPEND_TARGET
,
1852 "org.freedesktop.login1.suspend",
1853 "org.freedesktop.login1.suspend-multiple-sessions",
1854 "org.freedesktop.login1.suspend-ignore-inhibit",
1859 static int method_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1860 Manager
*m
= userdata
;
1862 return method_do_shutdown_or_sleep(
1864 SPECIAL_HIBERNATE_TARGET
,
1866 "org.freedesktop.login1.hibernate",
1867 "org.freedesktop.login1.hibernate-multiple-sessions",
1868 "org.freedesktop.login1.hibernate-ignore-inhibit",
1873 static int method_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1874 Manager
*m
= userdata
;
1876 return method_do_shutdown_or_sleep(
1878 SPECIAL_HYBRID_SLEEP_TARGET
,
1880 "org.freedesktop.login1.hibernate",
1881 "org.freedesktop.login1.hibernate-multiple-sessions",
1882 "org.freedesktop.login1.hibernate-ignore-inhibit",
1887 static int method_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1888 Manager
*m
= userdata
;
1890 return method_do_shutdown_or_sleep(
1892 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET
,
1894 "org.freedesktop.login1.hibernate",
1895 "org.freedesktop.login1.hibernate-multiple-sessions",
1896 "org.freedesktop.login1.hibernate-ignore-inhibit",
1901 static int nologin_timeout_handler(
1906 Manager
*m
= userdata
;
1908 log_info("Creating /run/nologin, blocking further logins...");
1911 create_shutdown_run_nologin_or_warn() >= 0;
1916 static int update_schedule_file(Manager
*m
) {
1917 _cleanup_free_
char *temp_path
= NULL
;
1918 _cleanup_fclose_
FILE *f
= NULL
;
1923 r
= mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE
);
1925 return log_error_errno(r
, "Failed to create shutdown subdirectory: %m");
1927 r
= fopen_temporary("/run/systemd/shutdown/scheduled", &f
, &temp_path
);
1929 return log_error_errno(r
, "Failed to save information about scheduled shutdowns: %m");
1931 (void) fchmod(fileno(f
), 0644);
1937 m
->scheduled_shutdown_timeout
,
1938 m
->enable_wall_messages
,
1939 m
->scheduled_shutdown_type
);
1941 if (!isempty(m
->wall_message
)) {
1942 _cleanup_free_
char *t
;
1944 t
= cescape(m
->wall_message
);
1950 fprintf(f
, "WALL_MESSAGE=%s\n", t
);
1953 r
= fflush_and_check(f
);
1957 if (rename(temp_path
, "/run/systemd/shutdown/scheduled") < 0) {
1965 (void) unlink(temp_path
);
1966 (void) unlink("/run/systemd/shutdown/scheduled");
1968 return log_error_errno(r
, "Failed to write information about scheduled shutdowns: %m");
1971 static void reset_scheduled_shutdown(Manager
*m
) {
1974 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
1975 m
->wall_message_timeout_source
= sd_event_source_unref(m
->wall_message_timeout_source
);
1976 m
->nologin_timeout_source
= sd_event_source_unref(m
->nologin_timeout_source
);
1978 m
->scheduled_shutdown_type
= mfree(m
->scheduled_shutdown_type
);
1979 m
->scheduled_shutdown_timeout
= 0;
1980 m
->shutdown_dry_run
= false;
1982 if (m
->unlink_nologin
) {
1983 (void) unlink_or_warn("/run/nologin");
1984 m
->unlink_nologin
= false;
1987 (void) unlink("/run/systemd/shutdown/scheduled");
1990 static int manager_scheduled_shutdown_handler(
1995 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1996 Manager
*m
= userdata
;
2002 if (isempty(m
->scheduled_shutdown_type
))
2005 if (streq(m
->scheduled_shutdown_type
, "poweroff"))
2006 target
= SPECIAL_POWEROFF_TARGET
;
2007 else if (streq(m
->scheduled_shutdown_type
, "reboot"))
2008 target
= SPECIAL_REBOOT_TARGET
;
2009 else if (streq(m
->scheduled_shutdown_type
, "halt"))
2010 target
= SPECIAL_HALT_TARGET
;
2012 assert_not_reached("unexpected shutdown type");
2014 /* Don't allow multiple jobs being executed at the same time */
2015 if (m
->action_what
) {
2017 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target
);
2021 if (m
->shutdown_dry_run
) {
2022 /* We do not process delay inhibitors here. Otherwise, we
2023 * would have to be considered "in progress" (like the check
2024 * above) for some seconds after our admin has seen the final
2027 bus_manager_log_shutdown(m
, target
);
2028 log_info("Running in dry run, suppressing action.");
2029 reset_scheduled_shutdown(m
);
2034 r
= bus_manager_shutdown_or_sleep_now_or_later(m
, target
, INHIBIT_SHUTDOWN
, &error
);
2036 log_error_errno(r
, "Scheduled shutdown to %s failed: %m", target
);
2043 reset_scheduled_shutdown(m
);
2047 static int method_schedule_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2048 Manager
*m
= userdata
;
2049 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2050 const char *action_multiple_sessions
= NULL
;
2051 const char *action_ignore_inhibit
= NULL
;
2052 const char *action
= NULL
;
2056 bool dry_run
= false;
2061 r
= sd_bus_message_read(message
, "st", &type
, &elapse
);
2065 if (startswith(type
, "dry-")) {
2070 if (streq(type
, "poweroff")) {
2071 action
= "org.freedesktop.login1.power-off";
2072 action_multiple_sessions
= "org.freedesktop.login1.power-off-multiple-sessions";
2073 action_ignore_inhibit
= "org.freedesktop.login1.power-off-ignore-inhibit";
2074 } else if (streq(type
, "reboot")) {
2075 action
= "org.freedesktop.login1.reboot";
2076 action_multiple_sessions
= "org.freedesktop.login1.reboot-multiple-sessions";
2077 action_ignore_inhibit
= "org.freedesktop.login1.reboot-ignore-inhibit";
2078 } else if (streq(type
, "halt")) {
2079 action
= "org.freedesktop.login1.halt";
2080 action_multiple_sessions
= "org.freedesktop.login1.halt-multiple-sessions";
2081 action_ignore_inhibit
= "org.freedesktop.login1.halt-ignore-inhibit";
2083 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unsupported shutdown type");
2085 r
= verify_shutdown_creds(m
, message
, INHIBIT_SHUTDOWN
, false,
2086 action
, action_multiple_sessions
, action_ignore_inhibit
, error
);
2090 if (m
->scheduled_shutdown_timeout_source
) {
2091 r
= sd_event_source_set_time(m
->scheduled_shutdown_timeout_source
, elapse
);
2093 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2095 r
= sd_event_source_set_enabled(m
->scheduled_shutdown_timeout_source
, SD_EVENT_ONESHOT
);
2097 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2099 r
= sd_event_add_time(m
->event
, &m
->scheduled_shutdown_timeout_source
,
2100 CLOCK_REALTIME
, elapse
, 0, manager_scheduled_shutdown_handler
, m
);
2102 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2105 r
= free_and_strdup(&m
->scheduled_shutdown_type
, type
);
2107 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2111 m
->shutdown_dry_run
= dry_run
;
2113 if (m
->nologin_timeout_source
) {
2114 r
= sd_event_source_set_time(m
->nologin_timeout_source
, elapse
);
2116 return log_error_errno(r
, "sd_event_source_set_time() failed: %m");
2118 r
= sd_event_source_set_enabled(m
->nologin_timeout_source
, SD_EVENT_ONESHOT
);
2120 return log_error_errno(r
, "sd_event_source_set_enabled() failed: %m");
2122 r
= sd_event_add_time(m
->event
, &m
->nologin_timeout_source
,
2123 CLOCK_REALTIME
, elapse
- 5 * USEC_PER_MINUTE
, 0, nologin_timeout_handler
, m
);
2125 return log_error_errno(r
, "sd_event_add_time() failed: %m");
2128 m
->scheduled_shutdown_timeout
= elapse
;
2130 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2132 const char *tty
= NULL
;
2134 (void) sd_bus_creds_get_uid(creds
, &m
->scheduled_shutdown_uid
);
2135 (void) sd_bus_creds_get_tty(creds
, &tty
);
2137 r
= free_and_strdup(&m
->scheduled_shutdown_tty
, tty
);
2139 m
->scheduled_shutdown_timeout_source
= sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
2144 r
= manager_setup_wall_message_timer(m
);
2148 r
= update_schedule_file(m
);
2152 return sd_bus_reply_method_return(message
, NULL
);
2155 static int method_cancel_scheduled_shutdown(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2156 Manager
*m
= userdata
;
2162 cancelled
= m
->scheduled_shutdown_type
!= NULL
;
2163 reset_scheduled_shutdown(m
);
2165 if (cancelled
&& m
->enable_wall_messages
) {
2166 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2167 _cleanup_free_
char *username
= NULL
;
2168 const char *tty
= NULL
;
2172 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_AUGMENT
|SD_BUS_CREDS_TTY
|SD_BUS_CREDS_UID
, &creds
);
2174 (void) sd_bus_creds_get_uid(creds
, &uid
);
2175 (void) sd_bus_creds_get_tty(creds
, &tty
);
2178 username
= uid_to_name(uid
);
2179 utmp_wall("The system shutdown has been cancelled",
2180 username
, tty
, logind_wall_tty_filter
, m
);
2183 return sd_bus_reply_method_return(message
, "b", cancelled
);
2186 static int method_can_shutdown_or_sleep(
2188 sd_bus_message
*message
,
2191 const char *action_multiple_sessions
,
2192 const char *action_ignore_inhibit
,
2193 const char *sleep_verb
,
2194 sd_bus_error
*error
) {
2196 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2197 HandleAction handle
;
2198 bool multiple_sessions
, challenge
, blocked
;
2199 const char *result
= NULL
;
2206 assert(w
<= _INHIBIT_WHAT_MAX
);
2208 assert(action_multiple_sessions
);
2209 assert(action_ignore_inhibit
);
2212 r
= can_sleep(sleep_verb
);
2213 if (IN_SET(r
, 0, -ENOSPC
))
2214 return sd_bus_reply_method_return(message
, "s", "na");
2219 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
, &creds
);
2223 r
= sd_bus_creds_get_euid(creds
, &uid
);
2227 r
= have_multiple_sessions(m
, uid
);
2231 multiple_sessions
= r
> 0;
2232 blocked
= manager_is_inhibited(m
, w
, INHIBIT_BLOCK
, NULL
, false, true, uid
, NULL
);
2234 handle
= handle_action_from_string(sleep_verb
);
2238 target
= manager_target_for_action(handle
);
2240 _cleanup_free_
char *load_state
= NULL
;
2242 r
= unit_load_state(m
->bus
, target
, &load_state
);
2246 if (!streq(load_state
, "loaded")) {
2253 if (multiple_sessions
) {
2254 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_multiple_sessions
, NULL
, UID_INVALID
, &challenge
, error
);
2261 result
= "challenge";
2267 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action_ignore_inhibit
, NULL
, UID_INVALID
, &challenge
, error
);
2274 } else if (challenge
) {
2275 if (!result
|| streq(result
, "yes"))
2276 result
= "challenge";
2281 if (!multiple_sessions
&& !blocked
) {
2282 /* If neither inhibit nor multiple sessions
2283 * apply then just check the normal policy */
2285 r
= bus_test_polkit(message
, CAP_SYS_BOOT
, action
, NULL
, UID_INVALID
, &challenge
, error
);
2292 result
= "challenge";
2298 return sd_bus_reply_method_return(message
, "s", result
);
2301 static int method_can_poweroff(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2302 Manager
*m
= userdata
;
2304 return method_can_shutdown_or_sleep(
2307 "org.freedesktop.login1.power-off",
2308 "org.freedesktop.login1.power-off-multiple-sessions",
2309 "org.freedesktop.login1.power-off-ignore-inhibit",
2314 static int method_can_reboot(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2315 Manager
*m
= userdata
;
2317 return method_can_shutdown_or_sleep(
2320 "org.freedesktop.login1.reboot",
2321 "org.freedesktop.login1.reboot-multiple-sessions",
2322 "org.freedesktop.login1.reboot-ignore-inhibit",
2327 static int method_can_halt(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2328 Manager
*m
= userdata
;
2330 return method_can_shutdown_or_sleep(
2333 "org.freedesktop.login1.halt",
2334 "org.freedesktop.login1.halt-multiple-sessions",
2335 "org.freedesktop.login1.halt-ignore-inhibit",
2340 static int method_can_suspend(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2341 Manager
*m
= userdata
;
2343 return method_can_shutdown_or_sleep(
2346 "org.freedesktop.login1.suspend",
2347 "org.freedesktop.login1.suspend-multiple-sessions",
2348 "org.freedesktop.login1.suspend-ignore-inhibit",
2353 static int method_can_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2354 Manager
*m
= userdata
;
2356 return method_can_shutdown_or_sleep(
2359 "org.freedesktop.login1.hibernate",
2360 "org.freedesktop.login1.hibernate-multiple-sessions",
2361 "org.freedesktop.login1.hibernate-ignore-inhibit",
2366 static int method_can_hybrid_sleep(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2367 Manager
*m
= userdata
;
2369 return method_can_shutdown_or_sleep(
2372 "org.freedesktop.login1.hibernate",
2373 "org.freedesktop.login1.hibernate-multiple-sessions",
2374 "org.freedesktop.login1.hibernate-ignore-inhibit",
2379 static int method_can_suspend_then_hibernate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2380 Manager
*m
= userdata
;
2382 return method_can_shutdown_or_sleep(
2385 "org.freedesktop.login1.hibernate",
2386 "org.freedesktop.login1.hibernate-multiple-sessions",
2387 "org.freedesktop.login1.hibernate-ignore-inhibit",
2388 "suspend-then-hibernate",
2392 static int property_get_reboot_to_firmware_setup(
2395 const char *interface
,
2396 const char *property
,
2397 sd_bus_message
*reply
,
2399 sd_bus_error
*error
) {
2406 r
= efi_get_reboot_to_firmware();
2407 if (r
< 0 && r
!= -EOPNOTSUPP
)
2408 log_warning_errno(r
, "Failed to determine reboot-to-firmware state: %m");
2410 return sd_bus_message_append(reply
, "b", r
> 0);
2413 static int method_set_reboot_to_firmware_setup(
2414 sd_bus_message
*message
,
2416 sd_bus_error
*error
) {
2419 Manager
*m
= userdata
;
2424 r
= sd_bus_message_read(message
, "b", &b
);
2428 r
= bus_verify_polkit_async(message
,
2430 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2434 &m
->polkit_registry
,
2439 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2441 r
= efi_set_reboot_to_firmware(b
);
2445 return sd_bus_reply_method_return(message
, NULL
);
2448 static int method_can_reboot_to_firmware_setup(
2449 sd_bus_message
*message
,
2451 sd_bus_error
*error
) {
2456 Manager
*m
= userdata
;
2461 r
= efi_reboot_to_firmware_supported();
2463 if (r
!= -EOPNOTSUPP
)
2464 log_warning_errno(r
, "Failed to determine whether reboot to firmware is supported: %m");
2466 return sd_bus_reply_method_return(message
, "s", "na");
2469 r
= bus_test_polkit(message
,
2471 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2482 result
= "challenge";
2486 return sd_bus_reply_method_return(message
, "s", result
);
2489 static int method_set_wall_message(
2490 sd_bus_message
*message
,
2492 sd_bus_error
*error
) {
2495 Manager
*m
= userdata
;
2497 unsigned enable_wall_messages
;
2502 r
= sd_bus_message_read(message
, "sb", &wall_message
, &enable_wall_messages
);
2506 r
= bus_verify_polkit_async(message
,
2508 "org.freedesktop.login1.set-wall-message",
2512 &m
->polkit_registry
,
2517 return 1; /* Will call us back */
2519 r
= free_and_strdup(&m
->wall_message
, empty_to_null(wall_message
));
2523 m
->enable_wall_messages
= enable_wall_messages
;
2525 return sd_bus_reply_method_return(message
, NULL
);
2528 static int method_inhibit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2529 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
2530 const char *who
, *why
, *what
, *mode
;
2531 _cleanup_free_
char *id
= NULL
;
2532 _cleanup_close_
int fifo_fd
= -1;
2533 Manager
*m
= userdata
;
2534 Inhibitor
*i
= NULL
;
2544 r
= sd_bus_message_read(message
, "ssss", &what
, &who
, &why
, &mode
);
2548 w
= inhibit_what_from_string(what
);
2550 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid what specification %s", what
);
2552 mm
= inhibit_mode_from_string(mode
);
2554 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid mode specification %s", mode
);
2556 /* Delay is only supported for shutdown/sleep */
2557 if (mm
== INHIBIT_DELAY
&& (w
& ~(INHIBIT_SHUTDOWN
|INHIBIT_SLEEP
)))
2558 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Delay inhibitors only supported for shutdown and sleep");
2560 /* Don't allow taking delay locks while we are already
2561 * executing the operation. We shouldn't create the impression
2562 * that the lock was successful if the machine is about to go
2563 * down/suspend any moment. */
2564 if (m
->action_what
& w
)
2565 return sd_bus_error_setf(error
, BUS_ERROR_OPERATION_IN_PROGRESS
, "The operation inhibition has been requested for is already running");
2567 r
= bus_verify_polkit_async(
2570 w
== INHIBIT_SHUTDOWN
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2571 w
== INHIBIT_SLEEP
? (mm
== INHIBIT_BLOCK
? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2572 w
== INHIBIT_IDLE
? "org.freedesktop.login1.inhibit-block-idle" :
2573 w
== INHIBIT_HANDLE_POWER_KEY
? "org.freedesktop.login1.inhibit-handle-power-key" :
2574 w
== INHIBIT_HANDLE_SUSPEND_KEY
? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2575 w
== INHIBIT_HANDLE_HIBERNATE_KEY
? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2576 "org.freedesktop.login1.inhibit-handle-lid-switch",
2580 &m
->polkit_registry
,
2585 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2587 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_EUID
|SD_BUS_CREDS_PID
, &creds
);
2591 r
= sd_bus_creds_get_euid(creds
, &uid
);
2595 r
= sd_bus_creds_get_pid(creds
, &pid
);
2599 if (hashmap_size(m
->inhibitors
) >= m
->inhibitors_max
)
2600 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Maximum number of inhibitors (%" PRIu64
") reached, refusing further inhibitors.", m
->inhibitors_max
);
2605 if (asprintf(&id
, "%lu", ++m
->inhibit_counter
) < 0)
2608 } while (hashmap_get(m
->inhibitors
, id
));
2610 r
= manager_add_inhibitor(m
, id
, &i
);
2618 i
->why
= strdup(why
);
2619 i
->who
= strdup(who
);
2621 if (!i
->why
|| !i
->who
) {
2626 fifo_fd
= inhibitor_create_fifo(i
);
2634 return sd_bus_reply_method_return(message
, "h", fifo_fd
);
2643 const sd_bus_vtable manager_vtable
[] = {
2644 SD_BUS_VTABLE_START(0),
2646 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL
, NULL
, offsetof(Manager
, enable_wall_messages
), 0),
2647 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL
, NULL
, offsetof(Manager
, wall_message
), 0),
2649 SD_BUS_PROPERTY("NAutoVTs", "u", NULL
, offsetof(Manager
, n_autovts
), SD_BUS_VTABLE_PROPERTY_CONST
),
2650 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL
, offsetof(Manager
, kill_only_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
2651 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL
, offsetof(Manager
, kill_exclude_users
), SD_BUS_VTABLE_PROPERTY_CONST
),
2652 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL
, offsetof(Manager
, kill_user_processes
), SD_BUS_VTABLE_PROPERTY_CONST
),
2653 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup
, 0, 0),
2654 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2655 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2656 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2657 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2658 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited
, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
2659 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL
, offsetof(Manager
, inhibit_delay_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2660 SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL
, offsetof(Manager
, user_stop_delay
), SD_BUS_VTABLE_PROPERTY_CONST
),
2661 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action
, offsetof(Manager
, handle_power_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2662 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action
, offsetof(Manager
, handle_suspend_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2663 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action
, offsetof(Manager
, handle_hibernate_key
), SD_BUS_VTABLE_PROPERTY_CONST
),
2664 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch
), SD_BUS_VTABLE_PROPERTY_CONST
),
2665 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_ep
), SD_BUS_VTABLE_PROPERTY_CONST
),
2666 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action
, offsetof(Manager
, handle_lid_switch_docked
), SD_BUS_VTABLE_PROPERTY_CONST
),
2667 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL
, offsetof(Manager
, holdoff_timeout_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
2668 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action
, offsetof(Manager
, idle_action
), SD_BUS_VTABLE_PROPERTY_CONST
),
2669 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL
, offsetof(Manager
, idle_action_usec
), SD_BUS_VTABLE_PROPERTY_CONST
),
2670 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing
, 0, 0),
2671 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing
, 0, 0),
2672 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown
, 0, 0),
2673 SD_BUS_PROPERTY("Docked", "b", property_get_docked
, 0, 0),
2674 SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed
, 0, 0),
2675 SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power
, 0, 0),
2676 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool
, offsetof(Manager
, remove_ipc
), SD_BUS_VTABLE_PROPERTY_CONST
),
2677 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL
, offsetof(Manager
, runtime_dir_size
), SD_BUS_VTABLE_PROPERTY_CONST
),
2678 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL
, offsetof(Manager
, inhibitors_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2679 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size
, offsetof(Manager
, inhibitors
), 0),
2680 SD_BUS_PROPERTY("SessionsMax", "t", NULL
, offsetof(Manager
, sessions_max
), SD_BUS_VTABLE_PROPERTY_CONST
),
2681 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size
, offsetof(Manager
, sessions
), 0),
2682 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max
, 0, SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
2684 SD_BUS_METHOD("GetSession", "s", "o", method_get_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2685 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
2686 SD_BUS_METHOD("GetUser", "u", "o", method_get_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2687 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
2688 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2689 SD_BUS_METHOD("ListSessions", NULL
, "a(susso)", method_list_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2690 SD_BUS_METHOD("ListUsers", NULL
, "a(uso)", method_list_users
, SD_BUS_VTABLE_UNPRIVILEGED
),
2691 SD_BUS_METHOD("ListSeats", NULL
, "a(so)", method_list_seats
, SD_BUS_VTABLE_UNPRIVILEGED
),
2692 SD_BUS_METHOD("ListInhibitors", NULL
, "a(ssssuu)", method_list_inhibitors
, SD_BUS_VTABLE_UNPRIVILEGED
),
2693 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session
, 0),
2694 SD_BUS_METHOD("ReleaseSession", "s", NULL
, method_release_session
, 0),
2695 SD_BUS_METHOD("ActivateSession", "s", NULL
, method_activate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2696 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL
, method_activate_session_on_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2697 SD_BUS_METHOD("LockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2698 SD_BUS_METHOD("UnlockSession", "s", NULL
, method_lock_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2699 SD_BUS_METHOD("LockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2700 SD_BUS_METHOD("UnlockSessions", NULL
, NULL
, method_lock_sessions
, SD_BUS_VTABLE_UNPRIVILEGED
),
2701 SD_BUS_METHOD("KillSession", "ssi", NULL
, method_kill_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2702 SD_BUS_METHOD("KillUser", "ui", NULL
, method_kill_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2703 SD_BUS_METHOD("TerminateSession", "s", NULL
, method_terminate_session
, SD_BUS_VTABLE_UNPRIVILEGED
),
2704 SD_BUS_METHOD("TerminateUser", "u", NULL
, method_terminate_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
2705 SD_BUS_METHOD("TerminateSeat", "s", NULL
, method_terminate_seat
, SD_BUS_VTABLE_UNPRIVILEGED
),
2706 SD_BUS_METHOD("SetUserLinger", "ubb", NULL
, method_set_user_linger
, SD_BUS_VTABLE_UNPRIVILEGED
),
2707 SD_BUS_METHOD("AttachDevice", "ssb", NULL
, method_attach_device
, SD_BUS_VTABLE_UNPRIVILEGED
),
2708 SD_BUS_METHOD("FlushDevices", "b", NULL
, method_flush_devices
, SD_BUS_VTABLE_UNPRIVILEGED
),
2709 SD_BUS_METHOD("PowerOff", "b", NULL
, method_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
2710 SD_BUS_METHOD("Reboot", "b", NULL
, method_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
2711 SD_BUS_METHOD("Halt", "b", NULL
, method_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
2712 SD_BUS_METHOD("Suspend", "b", NULL
, method_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
2713 SD_BUS_METHOD("Hibernate", "b", NULL
, method_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2714 SD_BUS_METHOD("HybridSleep", "b", NULL
, method_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
2715 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL
, method_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2716 SD_BUS_METHOD("CanPowerOff", NULL
, "s", method_can_poweroff
, SD_BUS_VTABLE_UNPRIVILEGED
),
2717 SD_BUS_METHOD("CanReboot", NULL
, "s", method_can_reboot
, SD_BUS_VTABLE_UNPRIVILEGED
),
2718 SD_BUS_METHOD("CanHalt", NULL
, "s", method_can_halt
, SD_BUS_VTABLE_UNPRIVILEGED
),
2719 SD_BUS_METHOD("CanSuspend", NULL
, "s", method_can_suspend
, SD_BUS_VTABLE_UNPRIVILEGED
),
2720 SD_BUS_METHOD("CanHibernate", NULL
, "s", method_can_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2721 SD_BUS_METHOD("CanHybridSleep", NULL
, "s", method_can_hybrid_sleep
, SD_BUS_VTABLE_UNPRIVILEGED
),
2722 SD_BUS_METHOD("CanSuspendThenHibernate", NULL
, "s", method_can_suspend_then_hibernate
, SD_BUS_VTABLE_UNPRIVILEGED
),
2723 SD_BUS_METHOD("ScheduleShutdown", "st", NULL
, method_schedule_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
2724 SD_BUS_METHOD("CancelScheduledShutdown", NULL
, "b", method_cancel_scheduled_shutdown
, SD_BUS_VTABLE_UNPRIVILEGED
),
2725 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit
, SD_BUS_VTABLE_UNPRIVILEGED
),
2726 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL
, "s", method_can_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
2727 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL
, method_set_reboot_to_firmware_setup
, SD_BUS_VTABLE_UNPRIVILEGED
),
2728 SD_BUS_METHOD("SetWallMessage", "sb", NULL
, method_set_wall_message
, SD_BUS_VTABLE_UNPRIVILEGED
),
2730 SD_BUS_SIGNAL("SessionNew", "so", 0),
2731 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2732 SD_BUS_SIGNAL("UserNew", "uo", 0),
2733 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2734 SD_BUS_SIGNAL("SeatNew", "so", 0),
2735 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2736 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2737 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2742 static int session_jobs_reply(Session
*s
, const char *unit
, const char *result
) {
2749 if (result
&& !streq(result
, "done")) {
2750 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
2752 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit '%s' failed with '%s'", unit
, result
);
2753 return session_send_create_reply(s
, &e
);
2756 return session_send_create_reply(s
, NULL
);
2759 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2760 const char *path
, *result
, *unit
;
2761 Manager
*m
= userdata
;
2770 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
2772 bus_log_parse_error(r
);
2776 if (m
->action_job
&& streq(m
->action_job
, path
)) {
2777 log_info("Operation '%s' finished.", inhibit_what_to_string(m
->action_what
));
2779 /* Tell people that they now may take a lock again */
2780 (void) send_prepare_for(m
, m
->action_what
, false);
2782 m
->action_job
= mfree(m
->action_job
);
2783 m
->action_unit
= NULL
;
2788 session
= hashmap_get(m
->session_units
, unit
);
2790 if (streq_ptr(path
, session
->scope_job
)) {
2791 session
->scope_job
= mfree(session
->scope_job
);
2792 (void) session_jobs_reply(session
, unit
, result
);
2794 session_save(session
);
2795 user_save(session
->user
);
2798 session_add_to_gc_queue(session
);
2801 user
= hashmap_get(m
->user_units
, unit
);
2803 if (streq_ptr(path
, user
->service_job
)) {
2804 user
->service_job
= mfree(user
->service_job
);
2806 LIST_FOREACH(sessions_by_user
, session
, user
->sessions
)
2807 (void) session_jobs_reply(session
, unit
, NULL
/* don't propagate user service failures to the client */);
2812 user_add_to_gc_queue(user
);
2818 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2819 const char *path
, *unit
;
2820 Manager
*m
= userdata
;
2828 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
2830 bus_log_parse_error(r
);
2834 session
= hashmap_get(m
->session_units
, unit
);
2836 session_add_to_gc_queue(session
);
2838 user
= hashmap_get(m
->user_units
, unit
);
2840 user_add_to_gc_queue(user
);
2845 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2846 _cleanup_free_
char *unit
= NULL
;
2847 Manager
*m
= userdata
;
2856 path
= sd_bus_message_get_path(message
);
2860 r
= unit_name_from_dbus_path(path
, &unit
);
2861 if (r
== -EINVAL
) /* not a unit */
2868 session
= hashmap_get(m
->session_units
, unit
);
2870 session_add_to_gc_queue(session
);
2872 user
= hashmap_get(m
->user_units
, unit
);
2874 user_add_to_gc_queue(user
);
2879 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
2880 Manager
*m
= userdata
;
2888 r
= sd_bus_message_read(message
, "b", &b
);
2890 bus_log_parse_error(r
);
2897 /* systemd finished reloading, let's recheck all our sessions */
2898 log_debug("System manager has been reloaded, rechecking sessions...");
2900 HASHMAP_FOREACH(session
, m
->sessions
, i
)
2901 session_add_to_gc_queue(session
);
2906 int manager_send_changed(Manager
*manager
, const char *property
, ...) {
2911 l
= strv_from_stdarg_alloca(property
);
2913 return sd_bus_emit_properties_changed_strv(
2915 "/org/freedesktop/login1",
2916 "org.freedesktop.login1.Manager",
2920 static int strdup_job(sd_bus_message
*reply
, char **job
) {
2925 r
= sd_bus_message_read(reply
, "o", &j
);
2937 int manager_start_scope(
2942 const char *description
,
2945 const char *requires_mounts_for
,
2946 sd_bus_message
*more_properties
,
2947 sd_bus_error
*error
,
2950 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
2959 r
= sd_bus_message_new_method_call(
2962 "org.freedesktop.systemd1",
2963 "/org/freedesktop/systemd1",
2964 "org.freedesktop.systemd1.Manager",
2965 "StartTransientUnit");
2969 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
2973 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
2977 if (!isempty(slice
)) {
2978 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
2983 if (!isempty(description
)) {
2984 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
2989 STRV_FOREACH(i
, wants
) {
2990 r
= sd_bus_message_append(m
, "(sv)", "Wants", "as", 1, *i
);
2995 STRV_FOREACH(i
, after
) {
2996 r
= sd_bus_message_append(m
, "(sv)", "After", "as", 1, *i
);
3001 if (!empty_or_root(requires_mounts_for
)) {
3002 r
= sd_bus_message_append(m
, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for
);
3007 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3009 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", true);
3013 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
3017 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3018 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", (uint64_t)-1);
3020 return bus_log_create_error(r
);
3022 if (more_properties
) {
3023 /* If TasksMax also appears here, it will overwrite the default value set above */
3024 r
= sd_bus_message_copy(m
, more_properties
, true);
3029 r
= sd_bus_message_close_container(m
);
3033 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
3037 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
3041 return strdup_job(reply
, job
);
3044 int manager_start_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3045 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3052 r
= sd_bus_call_method(
3054 "org.freedesktop.systemd1",
3055 "/org/freedesktop/systemd1",
3056 "org.freedesktop.systemd1.Manager",
3060 "ss", unit
, "replace");
3064 return strdup_job(reply
, job
);
3067 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
3068 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3075 r
= sd_bus_call_method(
3077 "org.freedesktop.systemd1",
3078 "/org/freedesktop/systemd1",
3079 "org.freedesktop.systemd1.Manager",
3083 "ss", unit
, "fail");
3085 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3086 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
3089 sd_bus_error_free(error
);
3096 return strdup_job(reply
, job
);
3099 int manager_abandon_scope(Manager
*manager
, const char *scope
, sd_bus_error
*ret_error
) {
3100 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3101 _cleanup_free_
char *path
= NULL
;
3107 path
= unit_dbus_path_from_name(scope
);
3111 r
= sd_bus_call_method(
3113 "org.freedesktop.systemd1",
3115 "org.freedesktop.systemd1.Scope",
3121 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3122 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
) ||
3123 sd_bus_error_has_name(&error
, BUS_ERROR_SCOPE_NOT_RUNNING
))
3126 sd_bus_error_move(ret_error
, &error
);
3133 int manager_kill_unit(Manager
*manager
, const char *unit
, KillWho who
, int signo
, sd_bus_error
*error
) {
3137 return sd_bus_call_method(
3139 "org.freedesktop.systemd1",
3140 "/org/freedesktop/systemd1",
3141 "org.freedesktop.systemd1.Manager",
3145 "ssi", unit
, who
== KILL_LEADER
? "main" : "all", signo
);
3148 int manager_unit_is_active(Manager
*manager
, const char *unit
, sd_bus_error
*ret_error
) {
3149 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3150 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3151 _cleanup_free_
char *path
= NULL
;
3158 path
= unit_dbus_path_from_name(unit
);
3162 r
= sd_bus_get_property(
3164 "org.freedesktop.systemd1",
3166 "org.freedesktop.systemd1.Unit",
3172 /* systemd might have droppped off momentarily, let's
3173 * not make this an error */
3174 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3175 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3178 /* If the unit is already unloaded then it's not
3180 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
3181 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
3184 sd_bus_error_move(ret_error
, &error
);
3188 r
= sd_bus_message_read(reply
, "s", &state
);
3192 return !STR_IN_SET(state
, "inactive", "failed");
3195 int manager_job_is_active(Manager
*manager
, const char *path
, sd_bus_error
*ret_error
) {
3196 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
3197 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
3203 r
= sd_bus_get_property(
3205 "org.freedesktop.systemd1",
3207 "org.freedesktop.systemd1.Job",
3213 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
3214 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
3217 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
3220 sd_bus_error_move(ret_error
, &error
);
3224 /* We don't actually care about the state really. The fact
3225 * that we could read the job state is enough for us */