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,
1444 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1445 Manager *m = userdata;
1450 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1454 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1459 if (until <= now(CLOCK_MONOTONIC))
1462 /* We want to ignore the lid switch for a while after each
1463 * suspend, and after boot-up. Hence let's install a timer for
1464 * this. As long as the event source exists we ignore the lid
1467 if (m->lid_switch_ignore_event_source) {
1470 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1477 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1479 r = sd_event_add_time(
1481 &m->lid_switch_ignore_event_source,
1484 lid_switch_ignore_handler, m);
1489 #if 0 /// elogind-dbus.c needs to access this
1490 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1492 int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1495 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1496 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1497 [INHIBIT_SLEEP] = "PrepareForSleep"
1500 int active = _active;
1504 assert(w < _INHIBIT_WHAT_MAX);
1505 assert(signal_name[w]);
1507 return sd_bus_emit_signal(m->bus,
1508 "/org/freedesktop/login1",
1509 "org.freedesktop.login1.Manager",
1515 #if 0 /// elogind has its own variant in elogind-dbus.c
1516 static int execute_shutdown_or_sleep(
1519 const char *unit_name,
1520 sd_bus_error *error) {
1522 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1529 assert(w < _INHIBIT_WHAT_MAX);
1532 if (w == INHIBIT_SHUTDOWN)
1533 bus_manager_log_shutdown(m, unit_name);
1535 r = sd_bus_call_method(
1537 "org.freedesktop.systemd1",
1538 "/org/freedesktop/systemd1",
1539 "org.freedesktop.systemd1.Manager",
1543 "ss", unit_name, "replace-irreversibly");
1547 r = sd_bus_message_read(reply, "o", &p);
1557 m->action_unit = unit_name;
1558 free(m->action_job);
1562 /* Make sure the lid switch is ignored for a while */
1563 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1568 /* Tell people that they now may take a lock again */
1569 (void) send_prepare_for(m, w, false);
1575 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1577 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1578 Inhibitor *offending = NULL;
1583 #if 0 /// elogind has no action_job, but a pending_action
1584 if (manager->action_what == 0 || manager->action_job)
1586 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
1590 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1591 _cleanup_free_ char *comm = NULL, *u = NULL;
1596 (void) get_process_comm(offending->pid, &comm);
1597 u = uid_to_name(offending->uid);
1599 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1600 offending->uid, strna(u),
1601 offending->pid, strna(comm));
1604 /* Actually do the operation */
1605 #if 0 /// elogind has no action_unit but a pending_action
1606 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1608 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
1611 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1612 bus_error_message(&error, r));
1615 #if 0 /// elogind has no action_unit but a pending_action
1616 manager->action_unit = NULL;
1617 manager->action_what = 0;
1620 manager->pending_action = HANDLE_IGNORE;
1621 manager->action_what = 0;
1622 /* It is not a critical error for elogind if suspending fails */
1629 static int manager_inhibit_timeout_handler(
1634 Manager *manager = userdata;
1638 assert(manager->inhibit_timeout_source == s);
1640 r = manager_dispatch_delayed(manager, true);
1641 return (r < 0) ? r : 0;
1644 #if 0 /// elogind does not have unit_name but action
1645 static int delay_shutdown_or_sleep(
1648 const char *unit_name) {
1651 int delay_shutdown_or_sleep(
1654 HandleAction action) {
1661 assert(w < _INHIBIT_WHAT_MAX);
1662 #if 0 /// UNNEEDED by elogind
1666 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1668 if (m->inhibit_timeout_source) {
1669 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1671 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1673 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1675 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1677 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1678 timeout_val, 0, manager_inhibit_timeout_handler, m);
1683 #if 0 /// elogind does not have unit_name but pendig_action
1684 m->action_unit = unit_name;
1686 m->pending_action = action;
1693 int bus_manager_shutdown_or_sleep_now_or_later(
1695 #if 0 /// elogind has HandleAction instead of const char* unit_name
1696 const char *unit_name,
1698 _cleanup_free_ char *load_state = NULL;
1700 HandleAction unit_name,
1703 sd_bus_error *error) {
1708 #if 0 /// for elogind only w has to be checked.
1711 assert(w <= _INHIBIT_WHAT_MAX);
1712 assert(!m->action_job);
1714 r = unit_load_state(m->bus, unit_name, &load_state);
1718 if (!streq(load_state, "loaded")) {
1719 log_notice("Unit %s is %s, refusing operation.", unit_name, load_state);
1724 assert(w <= _INHIBIT_WHAT_MAX);
1727 /* Tell everybody to prepare for shutdown/sleep */
1728 (void) send_prepare_for(m, w, true);
1731 m->inhibit_delay_max > 0 &&
1732 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1734 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
1735 handle_action_to_string(unit_name),
1736 delayed ? "" : "NOT ");
1738 /* Shutdown is delayed, keep in mind what we
1739 * want to do, and start a timeout */
1740 r = delay_shutdown_or_sleep(m, w, unit_name);
1742 /* Shutdown is not delayed, execute it
1744 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1749 static int verify_shutdown_creds(
1751 sd_bus_message *message,
1755 const char *action_multiple_sessions,
1756 const char *action_ignore_inhibit,
1757 sd_bus_error *error) {
1759 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1760 bool multiple_sessions, blocked;
1767 assert(w <= _INHIBIT_WHAT_MAX);
1769 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1773 r = sd_bus_creds_get_euid(creds, &uid);
1777 r = have_multiple_sessions(m, uid);
1781 multiple_sessions = r > 0;
1782 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1784 if (multiple_sessions && action_multiple_sessions) {
1785 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1789 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1792 if (blocked && action_ignore_inhibit) {
1793 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1797 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1800 if (!multiple_sessions && !blocked && action) {
1801 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1805 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1811 static int method_do_shutdown_or_sleep(
1813 sd_bus_message *message,
1814 #if 0 /// elogind has HandleAction instead of const char* unit_name
1815 const char *unit_name,
1817 HandleAction unit_name,
1821 const char *action_multiple_sessions,
1822 const char *action_ignore_inhibit,
1823 const char *sleep_verb,
1824 sd_bus_error *error) {
1830 #if 0 /// elogind does not need this to be checked
1834 assert(w <= _INHIBIT_WHAT_MAX);
1836 r = sd_bus_message_read(message, "b", &interactive);
1840 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
1841 __FUNCTION__, action, sleep_verb,
1842 interactive ? "" : "NOT ");
1843 /* Don't allow multiple jobs being executed at the same time */
1845 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1848 #if 0 /// Within elogind the manager m must be provided, too
1849 r = can_sleep(sleep_verb);
1851 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1852 "Not enough swap space for hibernation");
1854 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1855 "Sleep verb \"%s\" not supported", sleep_verb);
1857 r = can_sleep(m, sleep_verb);
1863 #if 0 /// Within elogind it does not make sense to verify shutdown creds when suspending
1864 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1865 action_ignore_inhibit, error);
1869 if (IN_SET(unit_name, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
1870 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1871 action_ignore_inhibit, error);
1872 log_debug_elogind("verify_shutdown_creds() returned %d", r);
1878 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1882 return sd_bus_reply_method_return(message, NULL);
1885 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1886 Manager *m = userdata;
1888 log_debug_elogind("%s called", __FUNCTION__);
1889 return method_do_shutdown_or_sleep(
1891 #if 0 /// elogind uses HandleAction instead of const char* unti names
1892 SPECIAL_POWEROFF_TARGET,
1897 "org.freedesktop.login1.power-off",
1898 "org.freedesktop.login1.power-off-multiple-sessions",
1899 "org.freedesktop.login1.power-off-ignore-inhibit",
1904 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1905 Manager *m = userdata;
1907 log_debug_elogind("%s called", __FUNCTION__);
1908 return method_do_shutdown_or_sleep(
1910 #if 0 /// elogind uses HandleAction instead of const char* unti names
1911 SPECIAL_REBOOT_TARGET,
1916 "org.freedesktop.login1.reboot",
1917 "org.freedesktop.login1.reboot-multiple-sessions",
1918 "org.freedesktop.login1.reboot-ignore-inhibit",
1923 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1924 Manager *m = userdata;
1926 log_debug_elogind("%s called", __FUNCTION__);
1927 return method_do_shutdown_or_sleep(
1929 #if 0 /// elogind uses HandleAction instead of const char* unti names
1930 SPECIAL_HALT_TARGET,
1935 "org.freedesktop.login1.halt",
1936 "org.freedesktop.login1.halt-multiple-sessions",
1937 "org.freedesktop.login1.halt-ignore-inhibit",
1942 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1943 Manager *m = userdata;
1945 log_debug_elogind("%s called", __FUNCTION__);
1946 return method_do_shutdown_or_sleep(
1948 #if 0 /// elogind uses HandleAction instead of const char* unti names
1949 SPECIAL_SUSPEND_TARGET,
1954 "org.freedesktop.login1.suspend",
1955 "org.freedesktop.login1.suspend-multiple-sessions",
1956 "org.freedesktop.login1.suspend-ignore-inhibit",
1961 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1962 Manager *m = userdata;
1964 log_debug_elogind("%s called", __FUNCTION__);
1965 return method_do_shutdown_or_sleep(
1967 #if 0 /// elogind uses HandleAction instead of const char* unti names
1968 SPECIAL_HIBERNATE_TARGET,
1973 "org.freedesktop.login1.hibernate",
1974 "org.freedesktop.login1.hibernate-multiple-sessions",
1975 "org.freedesktop.login1.hibernate-ignore-inhibit",
1980 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1981 Manager *m = userdata;
1983 log_debug_elogind("%s called", __FUNCTION__);
1984 return method_do_shutdown_or_sleep(
1986 #if 0 /// elogind uses HandleAction instead of const char* unti names
1987 SPECIAL_HYBRID_SLEEP_TARGET,
1989 "org.freedesktop.login1.hibernate",
1990 "org.freedesktop.login1.hibernate-multiple-sessions",
1991 "org.freedesktop.login1.hibernate-ignore-inhibit",
1996 static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1997 Manager *m = userdata;
1999 return method_do_shutdown_or_sleep(
2001 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
2003 HANDLE_HYBRID_SLEEP,
2006 "org.freedesktop.login1.hibernate",
2007 "org.freedesktop.login1.hibernate-multiple-sessions",
2008 "org.freedesktop.login1.hibernate-ignore-inhibit",
2013 static int nologin_timeout_handler(
2018 Manager *m = userdata;
2020 log_info("Creating /run/nologin, blocking further logins...");
2023 create_shutdown_run_nologin_or_warn() >= 0;
2028 static int update_schedule_file(Manager *m) {
2029 _cleanup_free_ char *temp_path = NULL;
2030 _cleanup_fclose_ FILE *f = NULL;
2035 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
2037 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
2039 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
2041 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
2043 (void) fchmod(fileno(f), 0644);
2049 m->scheduled_shutdown_timeout,
2050 m->enable_wall_messages,
2051 m->scheduled_shutdown_type);
2053 if (!isempty(m->wall_message)) {
2054 _cleanup_free_ char *t;
2056 t = cescape(m->wall_message);
2062 fprintf(f, "WALL_MESSAGE=%s\n", t);
2065 r = fflush_and_check(f);
2069 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
2077 (void) unlink(temp_path);
2078 (void) unlink("/run/systemd/shutdown/scheduled");
2080 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
2083 #if 0 /// elogind must access this from elogind-dbus.c
2084 static void reset_scheduled_shutdown(Manager *m) {
2086 void reset_scheduled_shutdown(Manager *m) {
2090 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2091 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
2092 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
2094 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
2095 m->scheduled_shutdown_timeout = 0;
2096 m->shutdown_dry_run = false;
2098 if (m->unlink_nologin) {
2099 (void) unlink_or_warn("/run/nologin");
2100 m->unlink_nologin = false;
2103 (void) unlink("/run/systemd/shutdown/scheduled");
2106 #if 0 /// elogind has its own variant in elogind-dbus.c
2107 static int manager_scheduled_shutdown_handler(
2112 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2113 Manager *m = userdata;
2119 if (isempty(m->scheduled_shutdown_type))
2122 if (streq(m->scheduled_shutdown_type, "poweroff"))
2123 target = SPECIAL_POWEROFF_TARGET;
2124 else if (streq(m->scheduled_shutdown_type, "reboot"))
2125 target = SPECIAL_REBOOT_TARGET;
2126 else if (streq(m->scheduled_shutdown_type, "halt"))
2127 target = SPECIAL_HALT_TARGET;
2129 assert_not_reached("unexpected shutdown type");
2131 /* Don't allow multiple jobs being executed at the same time */
2132 if (m->action_what) {
2134 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
2138 if (m->shutdown_dry_run) {
2139 /* We do not process delay inhibitors here. Otherwise, we
2140 * would have to be considered "in progress" (like the check
2141 * above) for some seconds after our admin has seen the final
2144 bus_manager_log_shutdown(m, target);
2145 log_info("Running in dry run, suppressing action.");
2146 reset_scheduled_shutdown(m);
2151 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
2153 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2160 reset_scheduled_shutdown(m);
2165 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2166 Manager *m = userdata;
2167 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2168 const char *action_multiple_sessions = NULL;
2169 const char *action_ignore_inhibit = NULL;
2170 const char *action = NULL;
2174 bool dry_run = false;
2179 log_debug_elogind("%s called", __FUNCTION__);
2180 r = sd_bus_message_read(message, "st", &type, &elapse);
2184 if (startswith(type, "dry-")) {
2189 if (streq(type, "poweroff")) {
2190 action = "org.freedesktop.login1.power-off";
2191 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2192 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2193 } else if (streq(type, "reboot")) {
2194 action = "org.freedesktop.login1.reboot";
2195 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2196 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2197 } else if (streq(type, "halt")) {
2198 action = "org.freedesktop.login1.halt";
2199 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2200 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2202 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2204 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2205 action, action_multiple_sessions, action_ignore_inhibit, error);
2209 if (m->scheduled_shutdown_timeout_source) {
2210 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2212 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2214 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2216 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2218 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2219 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2221 return log_error_errno(r, "sd_event_add_time() failed: %m");
2224 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2226 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2230 m->shutdown_dry_run = dry_run;
2232 if (m->nologin_timeout_source) {
2233 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2235 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2237 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2239 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2241 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2242 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2244 return log_error_errno(r, "sd_event_add_time() failed: %m");
2247 m->scheduled_shutdown_timeout = elapse;
2249 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2251 const char *tty = NULL;
2253 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2254 (void) sd_bus_creds_get_tty(creds, &tty);
2256 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2258 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2263 r = manager_setup_wall_message_timer(m);
2267 r = update_schedule_file(m);
2271 return sd_bus_reply_method_return(message, NULL);
2274 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2275 Manager *m = userdata;
2277 #if 1 /// elogind needs to construct the message to allow extra wall messages
2278 _cleanup_free_ char *l = NULL;
2284 log_debug_elogind("%s called", __FUNCTION__);
2285 cancelled = m->scheduled_shutdown_type != NULL;
2286 reset_scheduled_shutdown(m);
2288 if (cancelled && m->enable_wall_messages) {
2289 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2290 const char *tty = NULL;
2294 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2296 (void) sd_bus_creds_get_uid(creds, &uid);
2297 (void) sd_bus_creds_get_tty(creds, &tty);
2300 #if 0 /// elogind wants to allow extra cancellation messages
2301 utmp_wall("The system shutdown has been cancelled",
2302 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2304 r = asprintf(&l, "%s%sThe system shutdown has been cancelled!",
2305 strempty(m->wall_message),
2306 isempty(m->wall_message) ? "" : "\n");
2312 utmp_wall(l, uid_to_name(uid), tty, logind_wall_tty_filter, m);
2316 return sd_bus_reply_method_return(message, "b", cancelled);
2319 static int method_can_shutdown_or_sleep(
2321 sd_bus_message *message,
2324 const char *action_multiple_sessions,
2325 const char *action_ignore_inhibit,
2326 const char *sleep_verb,
2327 sd_bus_error *error) {
2329 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2330 HandleAction handle;
2331 bool multiple_sessions, challenge, blocked;
2332 const char *result = NULL;
2339 assert(w <= _INHIBIT_WHAT_MAX);
2341 assert(action_multiple_sessions);
2342 assert(action_ignore_inhibit);
2345 #if 0 /// elogind needs to have the manager being passed
2346 r = can_sleep(sleep_verb);
2347 if (IN_SET(r, 0, -ENOSPC))
2348 return sd_bus_reply_method_return(message, "s", "na");
2350 r = can_sleep(m, sleep_verb);
2356 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2360 r = sd_bus_creds_get_euid(creds, &uid);
2364 r = have_multiple_sessions(m, uid);
2368 multiple_sessions = r > 0;
2369 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2371 handle = handle_action_from_string(sleep_verb);
2375 target = manager_target_for_action(handle);
2377 _cleanup_free_ char *load_state = NULL;
2379 r = unit_load_state(m->bus, target, &load_state);
2383 if (!streq(load_state, "loaded")) {
2390 if (multiple_sessions) {
2391 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2398 result = "challenge";
2404 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2408 if (r > 0 && !result)
2410 else if (challenge && (!result || streq(result, "yes")))
2411 result = "challenge";
2416 if (!multiple_sessions && !blocked) {
2417 /* If neither inhibit nor multiple sessions
2418 * apply then just check the normal policy */
2420 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2427 result = "challenge";
2433 return sd_bus_reply_method_return(message, "s", result);
2436 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2437 Manager *m = userdata;
2439 return method_can_shutdown_or_sleep(
2442 "org.freedesktop.login1.power-off",
2443 "org.freedesktop.login1.power-off-multiple-sessions",
2444 "org.freedesktop.login1.power-off-ignore-inhibit",
2449 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2450 Manager *m = userdata;
2452 return method_can_shutdown_or_sleep(
2455 "org.freedesktop.login1.reboot",
2456 "org.freedesktop.login1.reboot-multiple-sessions",
2457 "org.freedesktop.login1.reboot-ignore-inhibit",
2462 static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2463 Manager *m = userdata;
2465 return method_can_shutdown_or_sleep(
2468 "org.freedesktop.login1.halt",
2469 "org.freedesktop.login1.halt-multiple-sessions",
2470 "org.freedesktop.login1.halt-ignore-inhibit",
2475 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2476 Manager *m = userdata;
2478 return method_can_shutdown_or_sleep(
2481 "org.freedesktop.login1.suspend",
2482 "org.freedesktop.login1.suspend-multiple-sessions",
2483 "org.freedesktop.login1.suspend-ignore-inhibit",
2488 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2489 Manager *m = userdata;
2491 return method_can_shutdown_or_sleep(
2494 "org.freedesktop.login1.hibernate",
2495 "org.freedesktop.login1.hibernate-multiple-sessions",
2496 "org.freedesktop.login1.hibernate-ignore-inhibit",
2501 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2502 Manager *m = userdata;
2504 return method_can_shutdown_or_sleep(
2507 "org.freedesktop.login1.hibernate",
2508 "org.freedesktop.login1.hibernate-multiple-sessions",
2509 "org.freedesktop.login1.hibernate-ignore-inhibit",
2514 static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2515 Manager *m = userdata;
2517 return method_can_shutdown_or_sleep(
2520 "org.freedesktop.login1.hibernate",
2521 "org.freedesktop.login1.hibernate-multiple-sessions",
2522 "org.freedesktop.login1.hibernate-ignore-inhibit",
2523 "suspend-then-hibernate",
2527 static int property_get_reboot_to_firmware_setup(
2530 const char *interface,
2531 const char *property,
2532 sd_bus_message *reply,
2534 sd_bus_error *error) {
2535 #if 0 /// elogind does not support EFI
2542 r = efi_get_reboot_to_firmware();
2543 if (r < 0 && r != -EOPNOTSUPP)
2544 log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m");
2546 return sd_bus_message_append(reply, "b", r > 0);
2548 return sd_bus_message_append(reply, "b", false);
2552 static int method_set_reboot_to_firmware_setup(
2553 sd_bus_message *message,
2555 sd_bus_error *error) {
2558 Manager *m = userdata;
2563 r = sd_bus_message_read(message, "b", &b);
2567 r = bus_verify_polkit_async(message,
2569 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2573 &m->polkit_registry,
2578 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2580 #if 0 /// elogind does not support EFI
2581 r = efi_set_reboot_to_firmware(b);
2586 return sd_bus_reply_method_return(message, NULL);
2589 static int method_can_reboot_to_firmware_setup(
2590 sd_bus_message *message,
2592 sd_bus_error *error) {
2594 #if 0 /// elogind does not support EFI
2598 Manager *m = userdata;
2603 r = efi_reboot_to_firmware_supported();
2605 if (r != -EOPNOTSUPP)
2606 log_warning_errno(errno, "Failed to determine whether reboot to firmware is supported: %m");
2608 return sd_bus_reply_method_return(message, "s", "na");
2611 r = bus_test_polkit(message,
2613 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2624 result = "challenge";
2628 return sd_bus_reply_method_return(message, "s", result);
2630 return sd_bus_reply_method_return(message, "s", "no");
2634 static int method_set_wall_message(
2635 sd_bus_message *message,
2637 sd_bus_error *error) {
2640 Manager *m = userdata;
2642 unsigned enable_wall_messages;
2647 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2651 #if 0 /// elogind only calls this for shutdown/reboot, which already needs authorization.
2652 r = bus_verify_polkit_async(message,
2654 "org.freedesktop.login1.set-wall-message",
2658 &m->polkit_registry,
2663 return 1; /* Will call us back */
2666 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
2670 m->enable_wall_messages = enable_wall_messages;
2672 return sd_bus_reply_method_return(message, NULL);
2675 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2676 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2677 const char *who, *why, *what, *mode;
2678 _cleanup_free_ char *id = NULL;
2679 _cleanup_close_ int fifo_fd = -1;
2680 Manager *m = userdata;
2681 Inhibitor *i = NULL;
2691 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2695 w = inhibit_what_from_string(what);
2697 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2699 mm = inhibit_mode_from_string(mode);
2701 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2703 /* Delay is only supported for shutdown/sleep */
2704 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2705 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2707 /* Don't allow taking delay locks while we are already
2708 * executing the operation. We shouldn't create the impression
2709 * that the lock was successful if the machine is about to go
2710 * down/suspend any moment. */
2711 if (m->action_what & w)
2712 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2714 r = bus_verify_polkit_async(
2717 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2718 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2719 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2720 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2721 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2722 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2723 "org.freedesktop.login1.inhibit-handle-lid-switch",
2727 &m->polkit_registry,
2732 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2734 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2738 r = sd_bus_creds_get_euid(creds, &uid);
2742 r = sd_bus_creds_get_pid(creds, &pid);
2746 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2747 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2752 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2755 } while (hashmap_get(m->inhibitors, id));
2757 r = manager_add_inhibitor(m, id, &i);
2765 i->why = strdup(why);
2766 i->who = strdup(who);
2768 if (!i->why || !i->who) {
2773 fifo_fd = inhibitor_create_fifo(i);
2781 return sd_bus_reply_method_return(message, "h", fifo_fd);
2790 const sd_bus_vtable manager_vtable[] = {
2791 SD_BUS_VTABLE_START(0),
2793 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2794 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2796 #if 0 /// UNNEEDED by elogind
2797 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2799 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2800 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2801 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2802 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2803 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2804 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2805 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2806 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2807 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2808 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2809 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2810 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2811 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2812 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2813 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
2814 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2815 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2816 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2817 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2818 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2819 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2820 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2821 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2822 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2823 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2824 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2825 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0),
2826 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2827 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
2828 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
2830 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2831 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2832 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2833 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2834 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2835 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2836 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2837 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2838 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2839 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2840 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2841 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2842 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2843 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2844 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2845 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2846 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2847 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2848 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2849 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2850 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2851 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2852 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2853 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2854 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2855 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2856 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2857 SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2858 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2859 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2860 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2861 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2862 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2863 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2864 SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2865 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2866 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2867 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2868 SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2869 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2870 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2871 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2872 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2873 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2874 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2876 SD_BUS_SIGNAL("SessionNew", "so", 0),
2877 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2878 SD_BUS_SIGNAL("UserNew", "uo", 0),
2879 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2880 SD_BUS_SIGNAL("SeatNew", "so", 0),
2881 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2882 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2883 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2888 #if 0 /// UNNEEDED by elogind
2889 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2898 if (streq(result, "done"))
2899 r = session_send_create_reply(s, NULL);
2901 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2903 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2904 r = session_send_create_reply(s, &e);
2910 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2911 const char *path, *result, *unit;
2912 Manager *m = userdata;
2921 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2923 bus_log_parse_error(r);
2927 if (m->action_job && streq(m->action_job, path)) {
2928 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2930 /* Tell people that they now may take a lock again */
2931 (void) send_prepare_for(m, m->action_what, false);
2933 m->action_job = mfree(m->action_job);
2934 m->action_unit = NULL;
2939 session = hashmap_get(m->session_units, unit);
2940 if (session && streq_ptr(path, session->scope_job)) {
2941 session->scope_job = mfree(session->scope_job);
2942 session_jobs_reply(session, unit, result);
2944 session_save(session);
2945 user_save(session->user);
2946 session_add_to_gc_queue(session);
2949 user = hashmap_get(m->user_units, unit);
2951 (streq_ptr(path, user->service_job) ||
2952 streq_ptr(path, user->slice_job))) {
2954 if (streq_ptr(path, user->service_job))
2955 user->service_job = mfree(user->service_job);
2957 if (streq_ptr(path, user->slice_job))
2958 user->slice_job = mfree(user->slice_job);
2960 LIST_FOREACH(sessions_by_user, session, user->sessions)
2961 session_jobs_reply(session, unit, result);
2964 user_add_to_gc_queue(user);
2970 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2971 const char *path, *unit;
2972 Manager *m = userdata;
2980 r = sd_bus_message_read(message, "so", &unit, &path);
2982 bus_log_parse_error(r);
2986 session = hashmap_get(m->session_units, unit);
2988 session_add_to_gc_queue(session);
2990 user = hashmap_get(m->user_units, unit);
2992 user_add_to_gc_queue(user);
2997 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2998 _cleanup_free_ char *unit = NULL;
2999 Manager *m = userdata;
3008 path = sd_bus_message_get_path(message);
3012 r = unit_name_from_dbus_path(path, &unit);
3013 if (r == -EINVAL) /* not a unit */
3020 session = hashmap_get(m->session_units, unit);
3022 session_add_to_gc_queue(session);
3024 user = hashmap_get(m->user_units, unit);
3026 user_add_to_gc_queue(user);
3031 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3032 Manager *m = userdata;
3040 r = sd_bus_message_read(message, "b", &b);
3042 bus_log_parse_error(r);
3049 /* systemd finished reloading, let's recheck all our sessions */
3050 log_debug("System manager has been reloaded, rechecking sessions...");
3052 HASHMAP_FOREACH(session, m->sessions, i)
3053 session_add_to_gc_queue(session);
3059 int manager_send_changed(Manager *manager, const char *property, ...) {
3064 l = strv_from_stdarg_alloca(property);
3066 return sd_bus_emit_properties_changed_strv(
3068 "/org/freedesktop/login1",
3069 "org.freedesktop.login1.Manager",
3073 #if 0 /// UNNEEDED by elogind
3074 static int strdup_job(sd_bus_message *reply, char **job) {
3079 r = sd_bus_message_read(reply, "o", &j);
3091 int manager_start_scope(
3096 const char *description,
3099 sd_bus_message *more_properties,
3100 sd_bus_error *error,
3103 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
3111 r = sd_bus_message_new_method_call(
3114 "org.freedesktop.systemd1",
3115 "/org/freedesktop/systemd1",
3116 "org.freedesktop.systemd1.Manager",
3117 "StartTransientUnit");
3121 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
3125 r = sd_bus_message_open_container(m, 'a', "(sv)");
3129 if (!isempty(slice)) {
3130 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
3135 if (!isempty(description)) {
3136 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3141 if (!isempty(after)) {
3142 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3147 if (!isempty(after2)) {
3148 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3153 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3155 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
3159 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3163 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3164 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1);
3166 return bus_log_create_error(r);
3168 if (more_properties) {
3169 /* If TasksMax also appears here, it will overwrite the default value set above */
3170 r = sd_bus_message_copy(m, more_properties, true);
3175 r = sd_bus_message_close_container(m);
3179 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3183 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3187 return strdup_job(reply, job);
3190 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3191 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3198 r = sd_bus_call_method(
3200 "org.freedesktop.systemd1",
3201 "/org/freedesktop/systemd1",
3202 "org.freedesktop.systemd1.Manager",
3206 "ss", unit, "replace");
3210 return strdup_job(reply, job);
3213 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3214 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3221 r = sd_bus_call_method(
3223 "org.freedesktop.systemd1",
3224 "/org/freedesktop/systemd1",
3225 "org.freedesktop.systemd1.Manager",
3229 "ss", unit, "fail");
3231 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3232 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3235 sd_bus_error_free(error);
3242 return strdup_job(reply, job);
3245 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3246 _cleanup_free_ char *path = NULL;
3252 path = unit_dbus_path_from_name(scope);
3256 r = sd_bus_call_method(
3258 "org.freedesktop.systemd1",
3260 "org.freedesktop.systemd1.Scope",
3266 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3267 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3268 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3269 sd_bus_error_free(error);
3279 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3283 return sd_bus_call_method(
3285 "org.freedesktop.systemd1",
3286 "/org/freedesktop/systemd1",
3287 "org.freedesktop.systemd1.Manager",
3291 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3294 int manager_unit_is_active(Manager *manager, const char *unit) {
3295 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3296 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3297 _cleanup_free_ char *path = NULL;
3304 path = unit_dbus_path_from_name(unit);
3308 r = sd_bus_get_property(
3310 "org.freedesktop.systemd1",
3312 "org.freedesktop.systemd1.Unit",
3318 /* systemd might have droppped off momentarily, let's
3319 * not make this an error */
3320 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3321 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3324 /* If the unit is already unloaded then it's not
3326 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3327 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3333 r = sd_bus_message_read(reply, "s", &state);
3337 return !streq(state, "inactive") && !streq(state, "failed");
3340 int manager_job_is_active(Manager *manager, const char *path) {
3341 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3342 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3348 r = sd_bus_get_property(
3350 "org.freedesktop.systemd1",
3352 "org.freedesktop.systemd1.Job",
3358 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3359 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3362 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3368 /* We don't actually care about the state really. The fact
3369 * that we could read the job state is enough for us */