1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
13 #include "sd-messages.h"
15 #include "alloc-util.h"
16 #include "audit-util.h"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
19 //#include "bus-unit-util.h"
21 #include "dirent-util.h"
22 //#include "efivars.h"
25 #include "fileio-label.h"
26 #include "format-util.h"
30 #include "path-util.h"
31 #include "process-util.h"
32 //#include "cgroup-util.h"
33 #include "selinux-util.h"
34 #include "sleep-config.h"
35 //#include "special.h"
37 #include "terminal-util.h"
38 #include "udev-util.h"
39 #include "unit-name.h"
40 #include "user-util.h"
41 #include "utmp-wtmp.h"
43 /// Additional includes needed by elogind
44 #include "elogind-dbus.h"
46 static int get_sender_session(Manager *m, sd_bus_message *message, sd_bus_error *error, Session **ret) {
48 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
53 /* Get client login session. This is not what you are looking for these days,
54 * as apps may instead belong to a user service unit. This includes terminal
55 * emulators and hence command-line apps. */
56 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
60 r = sd_bus_creds_get_session(creds, &name);
66 session = hashmap_get(m->sessions, name);
74 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID,
75 "Caller does not belong to any known session");
78 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
86 return get_sender_session(m, message, error, ret);
88 session = hashmap_get(m->sessions, name);
90 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
96 static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) {
98 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
103 /* Note that we get the owner UID of the session, not the actual client UID here! */
104 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
108 r = sd_bus_creds_get_owner_uid(creds, &uid);
114 user = hashmap_get(m->users, UID_TO_PTR(uid));
122 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "Caller does not belong to any logged in user or lingering user");
125 int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
132 if (!uid_is_valid(uid))
133 return get_sender_user(m, message, error, ret);
135 user = hashmap_get(m->users, UID_TO_PTR(uid));
137 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "User ID "UID_FMT" is not logged in or lingering", uid);
143 int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) {
154 r = manager_get_session_from_creds(m, message, NULL, error, &session);
158 seat = session->seat;
160 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat.");
162 seat = hashmap_get(m->seats, name);
164 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
171 static int property_get_idle_hint(
174 const char *interface,
175 const char *property,
176 sd_bus_message *reply,
178 sd_bus_error *error) {
180 Manager *m = userdata;
186 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
189 static int property_get_idle_since_hint(
192 const char *interface,
193 const char *property,
194 sd_bus_message *reply,
196 sd_bus_error *error) {
198 Manager *m = userdata;
199 dual_timestamp t = DUAL_TIMESTAMP_NULL;
205 manager_get_idle_hint(m, &t);
207 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
210 static int property_get_inhibited(
213 const char *interface,
214 const char *property,
215 sd_bus_message *reply,
217 sd_bus_error *error) {
219 Manager *m = userdata;
226 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
228 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
231 static int property_get_preparing(
234 const char *interface,
235 const char *property,
236 sd_bus_message *reply,
238 sd_bus_error *error) {
240 Manager *m = userdata;
247 if (streq(property, "PreparingForShutdown"))
248 b = m->action_what & INHIBIT_SHUTDOWN;
250 b = m->action_what & INHIBIT_SLEEP;
252 return sd_bus_message_append(reply, "b", b);
255 static int property_get_scheduled_shutdown(
258 const char *interface,
259 const char *property,
260 sd_bus_message *reply,
262 sd_bus_error *error) {
264 Manager *m = userdata;
271 r = sd_bus_message_open_container(reply, 'r', "st");
275 r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
279 return sd_bus_message_close_container(reply);
282 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
283 static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays);
284 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX);
285 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size);
287 static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
288 _cleanup_free_ char *p = NULL;
289 Manager *m = userdata;
297 r = sd_bus_message_read(message, "s", &name);
301 r = manager_get_session_from_creds(m, message, name, error, &session);
305 p = session_bus_path(session);
309 return sd_bus_reply_method_return(message, "o", p);
312 /* Get login session of a process. This is not what you are looking for these days,
313 * as apps may instead belong to a user service unit. This includes terminal
314 * emulators and hence command-line apps. */
315 static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
316 _cleanup_free_ char *p = NULL;
317 Session *session = NULL;
318 Manager *m = userdata;
325 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
327 r = sd_bus_message_read(message, "u", &pid);
334 r = manager_get_session_from_creds(m, message, NULL, error, &session);
338 r = manager_get_session_by_pid(m, pid, &session);
343 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
346 p = session_bus_path(session);
350 return sd_bus_reply_method_return(message, "o", p);
353 static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
354 _cleanup_free_ char *p = NULL;
355 Manager *m = userdata;
363 r = sd_bus_message_read(message, "u", &uid);
367 r = manager_get_user_from_creds(m, message, uid, error, &user);
371 p = user_bus_path(user);
375 return sd_bus_reply_method_return(message, "o", p);
378 static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
379 _cleanup_free_ char *p = NULL;
380 Manager *m = userdata;
388 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
390 r = sd_bus_message_read(message, "u", &pid);
397 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
401 r = manager_get_user_by_pid(m, pid, &user);
405 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID,
406 "PID "PID_FMT" does not belong to any logged in user or lingering user",
410 p = user_bus_path(user);
414 return sd_bus_reply_method_return(message, "o", p);
417 static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
418 _cleanup_free_ char *p = NULL;
419 Manager *m = userdata;
427 r = sd_bus_message_read(message, "s", &name);
431 r = manager_get_seat_from_creds(m, message, name, error, &seat);
435 p = seat_bus_path(seat);
439 return sd_bus_reply_method_return(message, "o", p);
442 static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
443 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
444 Manager *m = userdata;
452 r = sd_bus_message_new_method_return(message, &reply);
456 r = sd_bus_message_open_container(reply, 'a', "(susso)");
460 HASHMAP_FOREACH(session, m->sessions, i) {
461 _cleanup_free_ char *p = NULL;
463 p = session_bus_path(session);
467 r = sd_bus_message_append(reply, "(susso)",
469 (uint32_t) session->user->uid,
471 session->seat ? session->seat->id : "",
477 r = sd_bus_message_close_container(reply);
481 return sd_bus_send(NULL, reply, NULL);
484 static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
485 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
486 Manager *m = userdata;
494 r = sd_bus_message_new_method_return(message, &reply);
498 r = sd_bus_message_open_container(reply, 'a', "(uso)");
502 HASHMAP_FOREACH(user, m->users, i) {
503 _cleanup_free_ char *p = NULL;
505 p = user_bus_path(user);
509 r = sd_bus_message_append(reply, "(uso)",
510 (uint32_t) user->uid,
517 r = sd_bus_message_close_container(reply);
521 return sd_bus_send(NULL, reply, NULL);
524 static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
525 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
526 Manager *m = userdata;
534 r = sd_bus_message_new_method_return(message, &reply);
538 r = sd_bus_message_open_container(reply, 'a', "(so)");
542 HASHMAP_FOREACH(seat, m->seats, i) {
543 _cleanup_free_ char *p = NULL;
545 p = seat_bus_path(seat);
549 r = sd_bus_message_append(reply, "(so)", seat->id, p);
554 r = sd_bus_message_close_container(reply);
558 return sd_bus_send(NULL, reply, NULL);
561 static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
562 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
563 Manager *m = userdata;
564 Inhibitor *inhibitor;
571 r = sd_bus_message_new_method_return(message, &reply);
575 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
579 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
581 r = sd_bus_message_append(reply, "(ssssuu)",
582 strempty(inhibit_what_to_string(inhibitor->what)),
583 strempty(inhibitor->who),
584 strempty(inhibitor->why),
585 strempty(inhibit_mode_to_string(inhibitor->mode)),
586 (uint32_t) inhibitor->uid,
587 (uint32_t) inhibitor->pid);
592 r = sd_bus_message_close_container(reply);
596 return sd_bus_send(NULL, reply, NULL);
599 static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
600 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
601 _cleanup_free_ char *id = NULL;
602 Session *session = NULL;
603 uint32_t audit_id = 0;
604 Manager *m = userdata;
618 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
619 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
621 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
625 if (!uid_is_valid(uid))
626 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
627 if (leader < 0 || leader == 1 || leader == getpid_cached())
628 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
631 t = _SESSION_TYPE_INVALID;
633 t = session_type_from_string(type);
635 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
639 c = _SESSION_CLASS_INVALID;
641 c = session_class_from_string(class);
643 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
646 if (isempty(desktop))
649 if (!string_is_safe(desktop))
650 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
656 seat = hashmap_get(m->seats, cseat);
658 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
661 if (tty_is_vc(tty)) {
666 else if (seat != m->seat0)
667 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);
669 v = vtnr_from_tty(tty);
671 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
675 else if (vtnr != (uint32_t) v)
676 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
678 } else if (tty_is_console(tty)) {
682 else if (seat != m->seat0)
683 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
686 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
690 if (seat_has_vts(seat)) {
691 if (vtnr <= 0 || vtnr > 63)
692 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
695 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
699 if (t == _SESSION_TYPE_INVALID) {
700 if (!isempty(display))
702 else if (!isempty(tty))
705 t = SESSION_UNSPECIFIED;
708 if (c == _SESSION_CLASS_INVALID) {
709 if (t == SESSION_UNSPECIFIED)
710 c = SESSION_BACKGROUND;
716 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
718 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
722 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
727 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
728 * that avoids creating a logind session. */
729 r = manager_get_user_by_pid(m, leader, NULL);
733 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice");
736 * Old gdm and lightdm start the user-session on the same VT as
737 * the greeter session. But they destroy the greeter session
738 * after the user-session and want the user-session to take
739 * over the VT. We need to support this for
740 * backwards-compatibility, so make sure we allow new sessions
741 * on a VT that a greeter is running on. Furthermore, to allow
742 * re-logins, we have to allow a greeter to take over a used VT for
743 * the exact same reasons.
745 if (c != SESSION_GREETER &&
747 vtnr < m->seat0->position_count &&
748 m->seat0->positions[vtnr] &&
749 m->seat0->positions[vtnr]->class != SESSION_GREETER)
750 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
752 if (hashmap_size(m->sessions) >= m->sessions_max)
753 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
755 (void) audit_session_from_pid(leader, &audit_id);
756 if (audit_session_is_valid(audit_id)) {
757 /* Keep our session IDs and the audit session IDs in sync */
759 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
762 /* Wut? There's already a session by this name and we
763 * didn't find it above? Weird, then let's not trust
764 * the audit data and let's better register a new
766 if (hashmap_get(m->sessions, id)) {
767 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
768 audit_id = AUDIT_SESSION_INVALID;
777 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
780 } while (hashmap_get(m->sessions, id));
783 r = manager_add_user_by_uid(m, uid, &user);
787 r = manager_add_session(m, id, &session);
791 session_set_user(session, user);
793 session->leader = leader;
794 session->audit_id = audit_id;
797 session->remote = remote;
798 session->vtnr = vtnr;
801 session->tty = strdup(tty);
808 if (!isempty(display)) {
809 session->display = strdup(display);
810 if (!session->display) {
816 if (!isempty(remote_user)) {
817 session->remote_user = strdup(remote_user);
818 if (!session->remote_user) {
824 if (!isempty(remote_host)) {
825 session->remote_host = strdup(remote_host);
826 if (!session->remote_host) {
832 if (!isempty(service)) {
833 session->service = strdup(service);
834 if (!session->service) {
840 if (!isempty(desktop)) {
841 session->desktop = strdup(desktop);
842 if (!session->desktop) {
849 r = seat_attach_session(seat, session);
854 r = sd_bus_message_enter_container(message, 'a', "(sv)");
858 r = session_start(session, message);
862 r = sd_bus_message_exit_container(message);
866 session->create_message = sd_bus_message_ref(message);
868 #if 0 /// UNNEEDED by elogind
869 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
870 * session_send_create_reply(). */
872 /* We reply directly. */
874 r = session_send_create_reply(session, NULL);
883 session_add_to_gc_queue(session);
886 user_add_to_gc_queue(user);
891 static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
892 Manager *m = userdata;
900 r = sd_bus_message_read(message, "s", &name);
904 r = manager_get_session_from_creds(m, message, name, error, &session);
908 r = session_release(session);
912 #if 1 /// elogind must queue this session
913 session_add_to_gc_queue(session);
915 return sd_bus_reply_method_return(message, NULL);
918 static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
919 Manager *m = userdata;
927 r = sd_bus_message_read(message, "s", &name);
931 r = manager_get_session_from_creds(m, message, name, error, &session);
935 return bus_session_method_activate(message, session, error);
938 static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
939 const char *session_name, *seat_name;
940 Manager *m = userdata;
948 /* Same as ActivateSession() but refuses to work if
949 * the seat doesn't match */
951 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
955 r = manager_get_session_from_creds(m, message, session_name, error, &session);
959 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
963 if (session->seat != seat)
964 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
966 r = session_activate(session);
970 return sd_bus_reply_method_return(message, NULL);
973 static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
974 Manager *m = userdata;
982 r = sd_bus_message_read(message, "s", &name);
986 r = manager_get_session_from_creds(m, message, name, error, &session);
990 return bus_session_method_lock(message, session, error);
993 static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
994 Manager *m = userdata;
1000 r = bus_verify_polkit_async(
1003 "org.freedesktop.login1.lock-sessions",
1007 &m->polkit_registry,
1012 return 1; /* Will call us back */
1014 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
1018 return sd_bus_reply_method_return(message, NULL);
1021 static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1023 Manager *m = userdata;
1030 r = sd_bus_message_read(message, "s", &name);
1034 r = manager_get_session_from_creds(m, message, name, error, &session);
1038 return bus_session_method_kill(message, session, error);
1041 static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1042 Manager *m = userdata;
1050 r = sd_bus_message_read(message, "u", &uid);
1054 r = manager_get_user_from_creds(m, message, uid, error, &user);
1058 return bus_user_method_kill(message, user, error);
1061 static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1062 Manager *m = userdata;
1070 r = sd_bus_message_read(message, "s", &name);
1074 r = manager_get_session_from_creds(m, message, name, error, &session);
1078 return bus_session_method_terminate(message, session, error);
1081 static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1082 Manager *m = userdata;
1090 r = sd_bus_message_read(message, "u", &uid);
1094 r = manager_get_user_from_creds(m, message, uid, error, &user);
1098 return bus_user_method_terminate(message, user, error);
1101 static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1102 Manager *m = userdata;
1110 r = sd_bus_message_read(message, "s", &name);
1114 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1118 return bus_seat_method_terminate(message, seat, error);
1121 static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1122 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1123 _cleanup_free_ char *cc = NULL;
1124 Manager *m = userdata;
1125 int r, b, interactive;
1128 uint32_t uid, auth_uid;
1133 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1137 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID |
1138 SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1142 if (!uid_is_valid(uid)) {
1143 /* Note that we get the owner UID of the session or user unit,
1144 * not the actual client UID here! */
1145 r = sd_bus_creds_get_owner_uid(creds, &uid);
1150 /* owner_uid is racy, so for authorization we must use euid */
1151 r = sd_bus_creds_get_euid(creds, &auth_uid);
1158 return errno > 0 ? -errno : -ENOENT;
1160 r = bus_verify_polkit_async(
1163 uid == auth_uid ? "org.freedesktop.login1.set-self-linger" :
1164 "org.freedesktop.login1.set-user-linger",
1168 &m->polkit_registry,
1173 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1175 mkdir_p_label("/var/lib/elogind", 0755);
1177 r = mkdir_safe_label("/var/lib/elogind/linger", 0755, 0, 0, MKDIR_WARN_MODE);
1181 cc = cescape(pw->pw_name);
1185 path = strjoina("/var/lib/elogind/linger/", cc);
1193 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1200 if (r < 0 && errno != ENOENT)
1203 u = hashmap_get(m->users, UID_TO_PTR(uid));
1205 user_add_to_gc_queue(u);
1208 return sd_bus_reply_method_return(message, NULL);
1211 static int trigger_device(Manager *m, struct udev_device *d) {
1212 _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
1213 struct udev_list_entry *first, *item;
1218 e = udev_enumerate_new(m->udev);
1223 r = udev_enumerate_add_match_parent(e, d);
1228 r = udev_enumerate_scan_devices(e);
1232 first = udev_enumerate_get_list_entry(e);
1233 udev_list_entry_foreach(item, first) {
1234 _cleanup_free_ char *t = NULL;
1237 p = udev_list_entry_get_name(item);
1239 t = strappend(p, "/uevent");
1243 (void) write_string_file(t, "change", 0);
1249 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1250 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
1251 _cleanup_free_ char *rule = NULL, *file = NULL;
1252 const char *id_for_seat;
1259 d = udev_device_new_from_syspath(m->udev, sysfs);
1263 if (!udev_device_has_tag(d, "seat"))
1266 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
1270 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1273 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1276 mkdir_p_label("/etc/udev/rules.d", 0755);
1277 r = write_string_file_atomic_label(file, rule);
1281 return trigger_device(m, d);
1284 static int flush_devices(Manager *m) {
1285 _cleanup_closedir_ DIR *d;
1289 d = opendir("/etc/udev/rules.d");
1291 if (errno != ENOENT)
1292 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1296 FOREACH_DIRENT_ALL(de, d, break) {
1297 if (!dirent_is_file(de))
1300 if (!startswith(de->d_name, "72-seat-"))
1303 if (!endswith(de->d_name, ".rules"))
1306 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1307 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1311 return trigger_device(m, NULL);
1314 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1315 const char *sysfs, *seat;
1316 Manager *m = userdata;
1322 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1326 if (!path_startswith(sysfs, "/sys"))
1327 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1329 if (!seat_name_is_valid(seat))
1330 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1332 r = bus_verify_polkit_async(
1335 "org.freedesktop.login1.attach-device",
1339 &m->polkit_registry,
1344 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1346 r = attach_device(m, seat, sysfs);
1350 return sd_bus_reply_method_return(message, NULL);
1353 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1354 Manager *m = userdata;
1360 r = sd_bus_message_read(message, "b", &interactive);
1364 r = bus_verify_polkit_async(
1367 "org.freedesktop.login1.flush-devices",
1371 &m->polkit_registry,
1376 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1378 r = flush_devices(m);
1382 return sd_bus_reply_method_return(message, NULL);
1385 static int have_multiple_sessions(
1394 /* Check for other users' sessions. Greeter sessions do not
1395 * count, and non-login sessions do not count either. */
1396 HASHMAP_FOREACH(session, m->sessions, i)
1397 if (session->class == SESSION_USER &&
1398 session->user->uid != uid)
1404 #if 0 /// elogind has its own variant in elogind-dbus.c
1405 static int bus_manager_log_shutdown(
1407 const char *unit_name) {
1414 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1415 p = "MESSAGE=System is powering down";
1416 q = "SHUTDOWN=power-off";
1417 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1418 p = "MESSAGE=System is rebooting";
1419 q = "SHUTDOWN=reboot";
1420 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1421 p = "MESSAGE=System is halting";
1422 q = "SHUTDOWN=halt";
1423 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1424 p = "MESSAGE=System is rebooting with kexec";
1425 q = "SHUTDOWN=kexec";
1427 p = "MESSAGE=System is shutting down";
1431 if (isempty(m->wall_message))
1432 p = strjoina(p, ".");
1434 p = strjoina(p, " (", m->wall_message, ").");
1436 return log_struct(LOG_NOTICE,
1437 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
1443 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1444 Manager *m = userdata;
1449 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1453 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1458 if (until <= now(CLOCK_MONOTONIC))
1461 /* We want to ignore the lid switch for a while after each
1462 * suspend, and after boot-up. Hence let's install a timer for
1463 * this. As long as the event source exists we ignore the lid
1466 if (m->lid_switch_ignore_event_source) {
1469 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1476 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1478 r = sd_event_add_time(
1480 &m->lid_switch_ignore_event_source,
1483 lid_switch_ignore_handler, m);
1488 #if 0 /// elogind-dbus.c needs to access this
1489 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1491 int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1494 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1495 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1496 [INHIBIT_SLEEP] = "PrepareForSleep"
1499 int active = _active;
1503 assert(w < _INHIBIT_WHAT_MAX);
1504 assert(signal_name[w]);
1506 return sd_bus_emit_signal(m->bus,
1507 "/org/freedesktop/login1",
1508 "org.freedesktop.login1.Manager",
1514 #if 0 /// elogind has its own variant in elogind-dbus.c
1515 static int execute_shutdown_or_sleep(
1518 const char *unit_name,
1519 sd_bus_error *error) {
1521 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1528 assert(w < _INHIBIT_WHAT_MAX);
1531 if (w == INHIBIT_SHUTDOWN)
1532 bus_manager_log_shutdown(m, unit_name);
1534 r = sd_bus_call_method(
1536 "org.freedesktop.systemd1",
1537 "/org/freedesktop/systemd1",
1538 "org.freedesktop.systemd1.Manager",
1542 "ss", unit_name, "replace-irreversibly");
1546 r = sd_bus_message_read(reply, "o", &p);
1556 m->action_unit = unit_name;
1557 free(m->action_job);
1561 /* Make sure the lid switch is ignored for a while */
1562 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1567 /* Tell people that they now may take a lock again */
1568 (void) send_prepare_for(m, w, false);
1574 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1576 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1577 Inhibitor *offending = NULL;
1582 #if 0 /// elogind has no action_job, but a pending_action
1583 if (manager->action_what == 0 || manager->action_job)
1585 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
1589 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1590 _cleanup_free_ char *comm = NULL, *u = NULL;
1595 (void) get_process_comm(offending->pid, &comm);
1596 u = uid_to_name(offending->uid);
1598 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1599 offending->uid, strna(u),
1600 offending->pid, strna(comm));
1603 /* Actually do the operation */
1604 #if 0 /// elogind has no action_unit but a pending_action
1605 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1607 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
1610 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1611 bus_error_message(&error, r));
1614 #if 0 /// elogind has no action_unit but a pending_action
1615 manager->action_unit = NULL;
1616 manager->action_what = 0;
1619 manager->pending_action = HANDLE_IGNORE;
1620 manager->action_what = 0;
1621 /* It is not a critical error for elogind if suspending fails */
1628 static int manager_inhibit_timeout_handler(
1633 Manager *manager = userdata;
1637 assert(manager->inhibit_timeout_source == s);
1639 r = manager_dispatch_delayed(manager, true);
1640 return (r < 0) ? r : 0;
1643 #if 0 /// elogind does not have unit_name but action
1644 static int delay_shutdown_or_sleep(
1647 const char *unit_name) {
1650 int delay_shutdown_or_sleep(
1653 HandleAction action) {
1660 assert(w < _INHIBIT_WHAT_MAX);
1661 #if 0 /// UNNEEDED by elogind
1665 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1667 if (m->inhibit_timeout_source) {
1668 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1670 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1672 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1674 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1676 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1677 timeout_val, 0, manager_inhibit_timeout_handler, m);
1682 #if 0 /// elogind does not have unit_name but pendig_action
1683 m->action_unit = unit_name;
1685 m->pending_action = action;
1692 int bus_manager_shutdown_or_sleep_now_or_later(
1694 #if 0 /// elogind has HandleAction instead of const char* unit_name
1695 const char *unit_name,
1697 _cleanup_free_ char *load_state = NULL;
1699 HandleAction unit_name,
1702 sd_bus_error *error) {
1707 #if 0 /// for elogind only w has to be checked.
1710 assert(w <= _INHIBIT_WHAT_MAX);
1711 assert(!m->action_job);
1713 r = unit_load_state(m->bus, unit_name, &load_state);
1717 if (!streq(load_state, "loaded")) {
1718 log_notice("Unit %s is %s, refusing operation.", unit_name, load_state);
1723 assert(w <= _INHIBIT_WHAT_MAX);
1726 /* Tell everybody to prepare for shutdown/sleep */
1727 (void) send_prepare_for(m, w, true);
1730 m->inhibit_delay_max > 0 &&
1731 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1733 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
1734 handle_action_to_string(unit_name),
1735 delayed ? "" : "NOT ");
1737 /* Shutdown is delayed, keep in mind what we
1738 * want to do, and start a timeout */
1739 r = delay_shutdown_or_sleep(m, w, unit_name);
1741 /* Shutdown is not delayed, execute it
1743 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1748 static int verify_shutdown_creds(
1750 sd_bus_message *message,
1754 const char *action_multiple_sessions,
1755 const char *action_ignore_inhibit,
1756 sd_bus_error *error) {
1758 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1759 bool multiple_sessions, blocked;
1766 assert(w <= _INHIBIT_WHAT_MAX);
1768 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1772 r = sd_bus_creds_get_euid(creds, &uid);
1776 r = have_multiple_sessions(m, uid);
1780 multiple_sessions = r > 0;
1781 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1783 if (multiple_sessions && action_multiple_sessions) {
1784 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1788 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1791 if (blocked && action_ignore_inhibit) {
1792 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1796 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1799 if (!multiple_sessions && !blocked && action) {
1800 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1804 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1810 static int method_do_shutdown_or_sleep(
1812 sd_bus_message *message,
1813 #if 0 /// elogind has HandleAction instead of const char* unit_name
1814 const char *unit_name,
1816 HandleAction unit_name,
1820 const char *action_multiple_sessions,
1821 const char *action_ignore_inhibit,
1822 const char *sleep_verb,
1823 sd_bus_error *error) {
1829 #if 0 /// elogind does not need this to be checked
1833 assert(w <= _INHIBIT_WHAT_MAX);
1835 r = sd_bus_message_read(message, "b", &interactive);
1839 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
1840 __FUNCTION__, action, sleep_verb,
1841 interactive ? "" : "NOT ");
1842 /* Don't allow multiple jobs being executed at the same time */
1844 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1847 #if 0 /// Within elogind the manager m must be provided, too
1848 r = can_sleep(sleep_verb);
1850 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1851 "Not enough swap space for hibernation");
1853 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1854 "Sleep verb \"%s\" not supported", sleep_verb);
1856 r = can_sleep(m, sleep_verb);
1862 #if 0 /// Within elogind it does not make sense to verify shutdown creds when suspending
1863 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1864 action_ignore_inhibit, error);
1868 if (IN_SET(unit_name, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
1869 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1870 action_ignore_inhibit, error);
1871 log_debug_elogind("verify_shutdown_creds() returned %d", r);
1877 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1881 return sd_bus_reply_method_return(message, NULL);
1884 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1885 Manager *m = userdata;
1887 log_debug_elogind("%s called", __FUNCTION__);
1888 return method_do_shutdown_or_sleep(
1890 #if 0 /// elogind uses HandleAction instead of const char* unti names
1891 SPECIAL_POWEROFF_TARGET,
1896 "org.freedesktop.login1.power-off",
1897 "org.freedesktop.login1.power-off-multiple-sessions",
1898 "org.freedesktop.login1.power-off-ignore-inhibit",
1903 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1904 Manager *m = userdata;
1906 log_debug_elogind("%s called", __FUNCTION__);
1907 return method_do_shutdown_or_sleep(
1909 #if 0 /// elogind uses HandleAction instead of const char* unti names
1910 SPECIAL_REBOOT_TARGET,
1915 "org.freedesktop.login1.reboot",
1916 "org.freedesktop.login1.reboot-multiple-sessions",
1917 "org.freedesktop.login1.reboot-ignore-inhibit",
1922 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1923 Manager *m = userdata;
1925 log_debug_elogind("%s called", __FUNCTION__);
1926 return method_do_shutdown_or_sleep(
1928 #if 0 /// elogind uses HandleAction instead of const char* unti names
1929 SPECIAL_HALT_TARGET,
1934 "org.freedesktop.login1.halt",
1935 "org.freedesktop.login1.halt-multiple-sessions",
1936 "org.freedesktop.login1.halt-ignore-inhibit",
1941 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1942 Manager *m = userdata;
1944 log_debug_elogind("%s called", __FUNCTION__);
1945 return method_do_shutdown_or_sleep(
1947 #if 0 /// elogind uses HandleAction instead of const char* unti names
1948 SPECIAL_SUSPEND_TARGET,
1953 "org.freedesktop.login1.suspend",
1954 "org.freedesktop.login1.suspend-multiple-sessions",
1955 "org.freedesktop.login1.suspend-ignore-inhibit",
1960 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1961 Manager *m = userdata;
1963 log_debug_elogind("%s called", __FUNCTION__);
1964 return method_do_shutdown_or_sleep(
1966 #if 0 /// elogind uses HandleAction instead of const char* unti names
1967 SPECIAL_HIBERNATE_TARGET,
1972 "org.freedesktop.login1.hibernate",
1973 "org.freedesktop.login1.hibernate-multiple-sessions",
1974 "org.freedesktop.login1.hibernate-ignore-inhibit",
1979 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1980 Manager *m = userdata;
1982 log_debug_elogind("%s called", __FUNCTION__);
1983 return method_do_shutdown_or_sleep(
1985 #if 0 /// elogind uses HandleAction instead of const char* unti names
1986 SPECIAL_HYBRID_SLEEP_TARGET,
1988 "org.freedesktop.login1.hibernate",
1989 "org.freedesktop.login1.hibernate-multiple-sessions",
1990 "org.freedesktop.login1.hibernate-ignore-inhibit",
1995 static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1996 Manager *m = userdata;
1998 return method_do_shutdown_or_sleep(
2000 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
2002 HANDLE_HYBRID_SLEEP,
2005 "org.freedesktop.login1.hibernate",
2006 "org.freedesktop.login1.hibernate-multiple-sessions",
2007 "org.freedesktop.login1.hibernate-ignore-inhibit",
2012 static int nologin_timeout_handler(
2017 Manager *m = userdata;
2019 log_info("Creating /run/nologin, blocking further logins...");
2022 create_shutdown_run_nologin_or_warn() >= 0;
2027 static int update_schedule_file(Manager *m) {
2028 _cleanup_free_ char *temp_path = NULL;
2029 _cleanup_fclose_ FILE *f = NULL;
2034 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
2036 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
2038 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
2040 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
2042 (void) fchmod(fileno(f), 0644);
2048 m->scheduled_shutdown_timeout,
2049 m->enable_wall_messages,
2050 m->scheduled_shutdown_type);
2052 if (!isempty(m->wall_message)) {
2053 _cleanup_free_ char *t;
2055 t = cescape(m->wall_message);
2061 fprintf(f, "WALL_MESSAGE=%s\n", t);
2064 r = fflush_and_check(f);
2068 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
2076 (void) unlink(temp_path);
2077 (void) unlink("/run/systemd/shutdown/scheduled");
2079 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
2082 #if 0 /// elogind must access this from elogind-dbus.c
2083 static void reset_scheduled_shutdown(Manager *m) {
2085 void reset_scheduled_shutdown(Manager *m) {
2089 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2090 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
2091 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
2093 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
2094 m->scheduled_shutdown_timeout = 0;
2095 m->shutdown_dry_run = false;
2097 if (m->unlink_nologin) {
2098 (void) unlink_or_warn("/run/nologin");
2099 m->unlink_nologin = false;
2102 (void) unlink("/run/systemd/shutdown/scheduled");
2105 #if 0 /// elogind has its own variant in elogind-dbus.c
2106 static int manager_scheduled_shutdown_handler(
2111 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2112 Manager *m = userdata;
2118 if (isempty(m->scheduled_shutdown_type))
2121 if (streq(m->scheduled_shutdown_type, "poweroff"))
2122 target = SPECIAL_POWEROFF_TARGET;
2123 else if (streq(m->scheduled_shutdown_type, "reboot"))
2124 target = SPECIAL_REBOOT_TARGET;
2125 else if (streq(m->scheduled_shutdown_type, "halt"))
2126 target = SPECIAL_HALT_TARGET;
2128 assert_not_reached("unexpected shutdown type");
2130 /* Don't allow multiple jobs being executed at the same time */
2131 if (m->action_what) {
2133 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
2137 if (m->shutdown_dry_run) {
2138 /* We do not process delay inhibitors here. Otherwise, we
2139 * would have to be considered "in progress" (like the check
2140 * above) for some seconds after our admin has seen the final
2143 bus_manager_log_shutdown(m, target);
2144 log_info("Running in dry run, suppressing action.");
2145 reset_scheduled_shutdown(m);
2150 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
2152 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2159 reset_scheduled_shutdown(m);
2164 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2165 Manager *m = userdata;
2166 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2167 const char *action_multiple_sessions = NULL;
2168 const char *action_ignore_inhibit = NULL;
2169 const char *action = NULL;
2173 bool dry_run = false;
2178 log_debug_elogind("%s called", __FUNCTION__);
2179 r = sd_bus_message_read(message, "st", &type, &elapse);
2183 if (startswith(type, "dry-")) {
2188 if (streq(type, "poweroff")) {
2189 action = "org.freedesktop.login1.power-off";
2190 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2191 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2192 } else if (streq(type, "reboot")) {
2193 action = "org.freedesktop.login1.reboot";
2194 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2195 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2196 } else if (streq(type, "halt")) {
2197 action = "org.freedesktop.login1.halt";
2198 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2199 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2201 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2203 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2204 action, action_multiple_sessions, action_ignore_inhibit, error);
2208 if (m->scheduled_shutdown_timeout_source) {
2209 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2211 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2213 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2215 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2217 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2218 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2220 return log_error_errno(r, "sd_event_add_time() failed: %m");
2223 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2225 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2229 m->shutdown_dry_run = dry_run;
2231 if (m->nologin_timeout_source) {
2232 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2234 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2236 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2238 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2240 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2241 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2243 return log_error_errno(r, "sd_event_add_time() failed: %m");
2246 m->scheduled_shutdown_timeout = elapse;
2248 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2250 const char *tty = NULL;
2252 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2253 (void) sd_bus_creds_get_tty(creds, &tty);
2255 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2257 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2262 r = manager_setup_wall_message_timer(m);
2266 r = update_schedule_file(m);
2270 return sd_bus_reply_method_return(message, NULL);
2273 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2274 Manager *m = userdata;
2276 #if 1 /// elogind needs to construct the message to allow extra wall messages
2277 _cleanup_free_ char *l = NULL;
2283 log_debug_elogind("%s called", __FUNCTION__);
2284 cancelled = m->scheduled_shutdown_type != NULL;
2285 reset_scheduled_shutdown(m);
2287 if (cancelled && m->enable_wall_messages) {
2288 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2289 const char *tty = NULL;
2293 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2295 (void) sd_bus_creds_get_uid(creds, &uid);
2296 (void) sd_bus_creds_get_tty(creds, &tty);
2299 #if 0 /// elogind wants to allow extra cancellation messages
2300 utmp_wall("The system shutdown has been cancelled",
2301 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2303 r = asprintf(&l, "%s%sThe system shutdown has been cancelled!",
2304 strempty(m->wall_message),
2305 isempty(m->wall_message) ? "" : "\n");
2311 utmp_wall(l, uid_to_name(uid), tty, logind_wall_tty_filter, m);
2315 return sd_bus_reply_method_return(message, "b", cancelled);
2318 static int method_can_shutdown_or_sleep(
2320 sd_bus_message *message,
2323 const char *action_multiple_sessions,
2324 const char *action_ignore_inhibit,
2325 const char *sleep_verb,
2326 sd_bus_error *error) {
2328 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2329 HandleAction handle;
2330 bool multiple_sessions, challenge, blocked;
2331 const char *result = NULL;
2338 assert(w <= _INHIBIT_WHAT_MAX);
2340 assert(action_multiple_sessions);
2341 assert(action_ignore_inhibit);
2344 #if 0 /// elogind needs to have the manager being passed
2345 r = can_sleep(sleep_verb);
2346 if (IN_SET(r, 0, -ENOSPC))
2347 return sd_bus_reply_method_return(message, "s", "na");
2349 r = can_sleep(m, sleep_verb);
2355 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2359 r = sd_bus_creds_get_euid(creds, &uid);
2363 r = have_multiple_sessions(m, uid);
2367 multiple_sessions = r > 0;
2368 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2370 handle = handle_action_from_string(sleep_verb);
2374 target = manager_target_for_action(handle);
2376 _cleanup_free_ char *load_state = NULL;
2378 r = unit_load_state(m->bus, target, &load_state);
2382 if (!streq(load_state, "loaded")) {
2389 if (multiple_sessions) {
2390 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2397 result = "challenge";
2403 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2407 if (r > 0 && !result)
2409 else if (challenge && (!result || streq(result, "yes")))
2410 result = "challenge";
2415 if (!multiple_sessions && !blocked) {
2416 /* If neither inhibit nor multiple sessions
2417 * apply then just check the normal policy */
2419 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2426 result = "challenge";
2432 return sd_bus_reply_method_return(message, "s", result);
2435 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2436 Manager *m = userdata;
2438 return method_can_shutdown_or_sleep(
2441 "org.freedesktop.login1.power-off",
2442 "org.freedesktop.login1.power-off-multiple-sessions",
2443 "org.freedesktop.login1.power-off-ignore-inhibit",
2448 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2449 Manager *m = userdata;
2451 return method_can_shutdown_or_sleep(
2454 "org.freedesktop.login1.reboot",
2455 "org.freedesktop.login1.reboot-multiple-sessions",
2456 "org.freedesktop.login1.reboot-ignore-inhibit",
2461 static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2462 Manager *m = userdata;
2464 return method_can_shutdown_or_sleep(
2467 "org.freedesktop.login1.halt",
2468 "org.freedesktop.login1.halt-multiple-sessions",
2469 "org.freedesktop.login1.halt-ignore-inhibit",
2474 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2475 Manager *m = userdata;
2477 return method_can_shutdown_or_sleep(
2480 "org.freedesktop.login1.suspend",
2481 "org.freedesktop.login1.suspend-multiple-sessions",
2482 "org.freedesktop.login1.suspend-ignore-inhibit",
2487 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2488 Manager *m = userdata;
2490 return method_can_shutdown_or_sleep(
2493 "org.freedesktop.login1.hibernate",
2494 "org.freedesktop.login1.hibernate-multiple-sessions",
2495 "org.freedesktop.login1.hibernate-ignore-inhibit",
2500 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2501 Manager *m = userdata;
2503 return method_can_shutdown_or_sleep(
2506 "org.freedesktop.login1.hibernate",
2507 "org.freedesktop.login1.hibernate-multiple-sessions",
2508 "org.freedesktop.login1.hibernate-ignore-inhibit",
2513 static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2514 Manager *m = userdata;
2516 return method_can_shutdown_or_sleep(
2519 "org.freedesktop.login1.hibernate",
2520 "org.freedesktop.login1.hibernate-multiple-sessions",
2521 "org.freedesktop.login1.hibernate-ignore-inhibit",
2522 "suspend-then-hibernate",
2526 static int property_get_reboot_to_firmware_setup(
2529 const char *interface,
2530 const char *property,
2531 sd_bus_message *reply,
2533 sd_bus_error *error) {
2534 #if 0 /// elogind does not support EFI
2541 r = efi_get_reboot_to_firmware();
2542 if (r < 0 && r != -EOPNOTSUPP)
2543 log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m");
2545 return sd_bus_message_append(reply, "b", r > 0);
2547 return sd_bus_message_append(reply, "b", false);
2551 static int method_set_reboot_to_firmware_setup(
2552 sd_bus_message *message,
2554 sd_bus_error *error) {
2557 Manager *m = userdata;
2562 r = sd_bus_message_read(message, "b", &b);
2566 r = bus_verify_polkit_async(message,
2568 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2572 &m->polkit_registry,
2577 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2579 #if 0 /// elogind does not support EFI
2580 r = efi_set_reboot_to_firmware(b);
2585 return sd_bus_reply_method_return(message, NULL);
2588 static int method_can_reboot_to_firmware_setup(
2589 sd_bus_message *message,
2591 sd_bus_error *error) {
2593 #if 0 /// elogind does not support EFI
2597 Manager *m = userdata;
2602 r = efi_reboot_to_firmware_supported();
2604 if (r != -EOPNOTSUPP)
2605 log_warning_errno(errno, "Failed to determine whether reboot to firmware is supported: %m");
2607 return sd_bus_reply_method_return(message, "s", "na");
2610 r = bus_test_polkit(message,
2612 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2623 result = "challenge";
2627 return sd_bus_reply_method_return(message, "s", result);
2629 return sd_bus_reply_method_return(message, "s", "no");
2633 static int method_set_wall_message(
2634 sd_bus_message *message,
2636 sd_bus_error *error) {
2639 Manager *m = userdata;
2641 unsigned enable_wall_messages;
2646 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2650 #if 0 /// elogind only calls this for shutdown/reboot, which already needs authorization.
2651 r = bus_verify_polkit_async(message,
2653 "org.freedesktop.login1.set-wall-message",
2657 &m->polkit_registry,
2662 return 1; /* Will call us back */
2665 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
2669 m->enable_wall_messages = enable_wall_messages;
2671 return sd_bus_reply_method_return(message, NULL);
2674 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2675 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2676 const char *who, *why, *what, *mode;
2677 _cleanup_free_ char *id = NULL;
2678 _cleanup_close_ int fifo_fd = -1;
2679 Manager *m = userdata;
2680 Inhibitor *i = NULL;
2690 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2694 w = inhibit_what_from_string(what);
2696 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2698 mm = inhibit_mode_from_string(mode);
2700 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2702 /* Delay is only supported for shutdown/sleep */
2703 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2704 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2706 /* Don't allow taking delay locks while we are already
2707 * executing the operation. We shouldn't create the impression
2708 * that the lock was successful if the machine is about to go
2709 * down/suspend any moment. */
2710 if (m->action_what & w)
2711 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2713 r = bus_verify_polkit_async(
2716 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2717 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2718 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2719 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2720 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2721 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2722 "org.freedesktop.login1.inhibit-handle-lid-switch",
2726 &m->polkit_registry,
2731 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2733 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2737 r = sd_bus_creds_get_euid(creds, &uid);
2741 r = sd_bus_creds_get_pid(creds, &pid);
2745 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2746 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2751 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2754 } while (hashmap_get(m->inhibitors, id));
2756 r = manager_add_inhibitor(m, id, &i);
2764 i->why = strdup(why);
2765 i->who = strdup(who);
2767 if (!i->why || !i->who) {
2772 fifo_fd = inhibitor_create_fifo(i);
2780 return sd_bus_reply_method_return(message, "h", fifo_fd);
2789 const sd_bus_vtable manager_vtable[] = {
2790 SD_BUS_VTABLE_START(0),
2792 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2793 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2795 #if 0 /// UNNEEDED by elogind
2796 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2798 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2799 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2800 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2801 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2802 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2803 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2804 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2805 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2806 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2807 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2808 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2809 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2810 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2811 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2812 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
2813 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2814 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2815 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2816 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2817 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2818 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2819 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2820 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2821 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2822 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2823 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2824 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0),
2825 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2826 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
2827 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
2829 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2830 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2831 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2832 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2833 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2834 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2835 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2836 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2837 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2838 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2839 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2840 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2841 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2842 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2843 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2844 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2845 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2846 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2847 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2848 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2849 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2850 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2851 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2852 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2853 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2854 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2855 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2856 SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2857 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2858 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2859 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2860 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2861 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2862 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2863 SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2864 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2865 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2866 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2867 SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2868 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2869 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2870 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2871 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2872 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2873 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2875 SD_BUS_SIGNAL("SessionNew", "so", 0),
2876 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2877 SD_BUS_SIGNAL("UserNew", "uo", 0),
2878 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2879 SD_BUS_SIGNAL("SeatNew", "so", 0),
2880 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2881 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2882 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2887 #if 0 /// UNNEEDED by elogind
2888 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2897 if (streq(result, "done"))
2898 r = session_send_create_reply(s, NULL);
2900 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2902 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2903 r = session_send_create_reply(s, &e);
2909 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2910 const char *path, *result, *unit;
2911 Manager *m = userdata;
2920 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2922 bus_log_parse_error(r);
2926 if (m->action_job && streq(m->action_job, path)) {
2927 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2929 /* Tell people that they now may take a lock again */
2930 (void) send_prepare_for(m, m->action_what, false);
2932 m->action_job = mfree(m->action_job);
2933 m->action_unit = NULL;
2938 session = hashmap_get(m->session_units, unit);
2939 if (session && streq_ptr(path, session->scope_job)) {
2940 session->scope_job = mfree(session->scope_job);
2941 session_jobs_reply(session, unit, result);
2943 session_save(session);
2944 user_save(session->user);
2945 session_add_to_gc_queue(session);
2948 user = hashmap_get(m->user_units, unit);
2950 (streq_ptr(path, user->service_job) ||
2951 streq_ptr(path, user->slice_job))) {
2953 if (streq_ptr(path, user->service_job))
2954 user->service_job = mfree(user->service_job);
2956 if (streq_ptr(path, user->slice_job))
2957 user->slice_job = mfree(user->slice_job);
2959 LIST_FOREACH(sessions_by_user, session, user->sessions)
2960 session_jobs_reply(session, unit, result);
2963 user_add_to_gc_queue(user);
2969 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2970 const char *path, *unit;
2971 Manager *m = userdata;
2979 r = sd_bus_message_read(message, "so", &unit, &path);
2981 bus_log_parse_error(r);
2985 session = hashmap_get(m->session_units, unit);
2987 session_add_to_gc_queue(session);
2989 user = hashmap_get(m->user_units, unit);
2991 user_add_to_gc_queue(user);
2996 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2997 _cleanup_free_ char *unit = NULL;
2998 Manager *m = userdata;
3007 path = sd_bus_message_get_path(message);
3011 r = unit_name_from_dbus_path(path, &unit);
3012 if (r == -EINVAL) /* not a unit */
3019 session = hashmap_get(m->session_units, unit);
3021 session_add_to_gc_queue(session);
3023 user = hashmap_get(m->user_units, unit);
3025 user_add_to_gc_queue(user);
3030 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3031 Manager *m = userdata;
3039 r = sd_bus_message_read(message, "b", &b);
3041 bus_log_parse_error(r);
3048 /* systemd finished reloading, let's recheck all our sessions */
3049 log_debug("System manager has been reloaded, rechecking sessions...");
3051 HASHMAP_FOREACH(session, m->sessions, i)
3052 session_add_to_gc_queue(session);
3058 int manager_send_changed(Manager *manager, const char *property, ...) {
3063 l = strv_from_stdarg_alloca(property);
3065 return sd_bus_emit_properties_changed_strv(
3067 "/org/freedesktop/login1",
3068 "org.freedesktop.login1.Manager",
3072 #if 0 /// UNNEEDED by elogind
3073 static int strdup_job(sd_bus_message *reply, char **job) {
3078 r = sd_bus_message_read(reply, "o", &j);
3090 int manager_start_scope(
3095 const char *description,
3098 sd_bus_message *more_properties,
3099 sd_bus_error *error,
3102 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
3110 r = sd_bus_message_new_method_call(
3113 "org.freedesktop.systemd1",
3114 "/org/freedesktop/systemd1",
3115 "org.freedesktop.systemd1.Manager",
3116 "StartTransientUnit");
3120 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
3124 r = sd_bus_message_open_container(m, 'a', "(sv)");
3128 if (!isempty(slice)) {
3129 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
3134 if (!isempty(description)) {
3135 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3140 if (!isempty(after)) {
3141 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3146 if (!isempty(after2)) {
3147 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3152 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3154 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
3158 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3162 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3163 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1);
3165 return bus_log_create_error(r);
3167 if (more_properties) {
3168 /* If TasksMax also appears here, it will overwrite the default value set above */
3169 r = sd_bus_message_copy(m, more_properties, true);
3174 r = sd_bus_message_close_container(m);
3178 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3182 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3186 return strdup_job(reply, job);
3189 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3190 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3197 r = sd_bus_call_method(
3199 "org.freedesktop.systemd1",
3200 "/org/freedesktop/systemd1",
3201 "org.freedesktop.systemd1.Manager",
3205 "ss", unit, "replace");
3209 return strdup_job(reply, job);
3212 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3213 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3220 r = sd_bus_call_method(
3222 "org.freedesktop.systemd1",
3223 "/org/freedesktop/systemd1",
3224 "org.freedesktop.systemd1.Manager",
3228 "ss", unit, "fail");
3230 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3231 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3234 sd_bus_error_free(error);
3241 return strdup_job(reply, job);
3244 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3245 _cleanup_free_ char *path = NULL;
3251 path = unit_dbus_path_from_name(scope);
3255 r = sd_bus_call_method(
3257 "org.freedesktop.systemd1",
3259 "org.freedesktop.systemd1.Scope",
3265 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3266 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3267 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3268 sd_bus_error_free(error);
3278 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3282 return sd_bus_call_method(
3284 "org.freedesktop.systemd1",
3285 "/org/freedesktop/systemd1",
3286 "org.freedesktop.systemd1.Manager",
3290 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3293 int manager_unit_is_active(Manager *manager, const char *unit) {
3294 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3295 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3296 _cleanup_free_ char *path = NULL;
3303 path = unit_dbus_path_from_name(unit);
3307 r = sd_bus_get_property(
3309 "org.freedesktop.systemd1",
3311 "org.freedesktop.systemd1.Unit",
3317 /* systemd might have droppped off momentarily, let's
3318 * not make this an error */
3319 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3320 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3323 /* If the unit is already unloaded then it's not
3325 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3326 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3332 r = sd_bus_message_read(reply, "s", &state);
3336 return !streq(state, "inactive") && !streq(state, "failed");
3339 int manager_job_is_active(Manager *manager, const char *path) {
3340 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3341 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3347 r = sd_bus_get_property(
3349 "org.freedesktop.systemd1",
3351 "org.freedesktop.systemd1.Job",
3357 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3358 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3361 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3367 /* We don't actually care about the state really. The fact
3368 * that we could read the job state is enough for us */