1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "sd-messages.h"
10 #include "alloc-util.h"
11 #include "audit-util.h"
12 #include "bus-common-errors.h"
13 #include "bus-error.h"
14 //#include "bus-unit-util.h"
16 #include "dirent-util.h"
17 //#include "efivars.h"
20 #include "fileio-label.h"
21 #include "format-util.h"
25 #include "path-util.h"
26 #include "process-util.h"
27 //#include "cgroup-util.h"
28 #include "selinux-util.h"
29 #include "sleep-config.h"
30 //#include "special.h"
32 #include "terminal-util.h"
33 #include "udev-util.h"
34 #include "unit-name.h"
35 #include "user-util.h"
36 #include "utmp-wtmp.h"
38 /// Additional includes needed by elogind
39 #include "elogind-dbus.h"
41 static int get_sender_session(Manager *m, sd_bus_message *message, sd_bus_error *error, Session **ret) {
43 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
48 /* Get client login session. This is not what you are looking for these days,
49 * as apps may instead belong to a user service unit. This includes terminal
50 * emulators and hence command-line apps. */
51 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
55 r = sd_bus_creds_get_session(creds, &name);
61 session = hashmap_get(m->sessions, name);
69 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID,
70 "Caller does not belong to any known session");
73 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
81 return get_sender_session(m, message, error, ret);
83 session = hashmap_get(m->sessions, name);
85 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
91 static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) {
93 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
98 /* Note that we get the owner UID of the session, not the actual client UID here! */
99 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
103 r = sd_bus_creds_get_owner_uid(creds, &uid);
109 user = hashmap_get(m->users, UID_TO_PTR(uid));
117 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "Caller does not belong to any logged in user or lingering user");
120 int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
127 if (!uid_is_valid(uid))
128 return get_sender_user(m, message, error, ret);
130 user = hashmap_get(m->users, UID_TO_PTR(uid));
132 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "User ID "UID_FMT" is not logged in or lingering", uid);
138 int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) {
149 r = manager_get_session_from_creds(m, message, NULL, error, &session);
153 seat = session->seat;
155 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat.");
157 seat = hashmap_get(m->seats, name);
159 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
166 static int property_get_idle_hint(
169 const char *interface,
170 const char *property,
171 sd_bus_message *reply,
173 sd_bus_error *error) {
175 Manager *m = userdata;
181 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
184 static int property_get_idle_since_hint(
187 const char *interface,
188 const char *property,
189 sd_bus_message *reply,
191 sd_bus_error *error) {
193 Manager *m = userdata;
194 dual_timestamp t = DUAL_TIMESTAMP_NULL;
200 manager_get_idle_hint(m, &t);
202 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
205 static int property_get_inhibited(
208 const char *interface,
209 const char *property,
210 sd_bus_message *reply,
212 sd_bus_error *error) {
214 Manager *m = userdata;
221 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
223 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
226 static int property_get_preparing(
229 const char *interface,
230 const char *property,
231 sd_bus_message *reply,
233 sd_bus_error *error) {
235 Manager *m = userdata;
242 if (streq(property, "PreparingForShutdown"))
243 b = m->action_what & INHIBIT_SHUTDOWN;
245 b = m->action_what & INHIBIT_SLEEP;
247 return sd_bus_message_append(reply, "b", b);
250 static int property_get_scheduled_shutdown(
253 const char *interface,
254 const char *property,
255 sd_bus_message *reply,
257 sd_bus_error *error) {
259 Manager *m = userdata;
266 r = sd_bus_message_open_container(reply, 'r', "st");
270 r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
274 return sd_bus_message_close_container(reply);
277 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
278 static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays);
279 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX);
280 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size);
282 static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
283 _cleanup_free_ char *p = NULL;
284 Manager *m = userdata;
292 r = sd_bus_message_read(message, "s", &name);
296 r = manager_get_session_from_creds(m, message, name, error, &session);
300 p = session_bus_path(session);
304 return sd_bus_reply_method_return(message, "o", p);
307 /* Get login session of a process. This is not what you are looking for these days,
308 * as apps may instead belong to a user service unit. This includes terminal
309 * emulators and hence command-line apps. */
310 static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
311 _cleanup_free_ char *p = NULL;
312 Session *session = NULL;
313 Manager *m = userdata;
320 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
322 r = sd_bus_message_read(message, "u", &pid);
329 r = manager_get_session_from_creds(m, message, NULL, error, &session);
333 r = manager_get_session_by_pid(m, pid, &session);
338 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
341 p = session_bus_path(session);
345 return sd_bus_reply_method_return(message, "o", p);
348 static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
349 _cleanup_free_ char *p = NULL;
350 Manager *m = userdata;
358 r = sd_bus_message_read(message, "u", &uid);
362 r = manager_get_user_from_creds(m, message, uid, error, &user);
366 p = user_bus_path(user);
370 return sd_bus_reply_method_return(message, "o", p);
373 static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
374 _cleanup_free_ char *p = NULL;
375 Manager *m = userdata;
383 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
385 r = sd_bus_message_read(message, "u", &pid);
392 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
396 r = manager_get_user_by_pid(m, pid, &user);
400 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID,
401 "PID "PID_FMT" does not belong to any logged in user or lingering user",
405 p = user_bus_path(user);
409 return sd_bus_reply_method_return(message, "o", p);
412 static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
413 _cleanup_free_ char *p = NULL;
414 Manager *m = userdata;
422 r = sd_bus_message_read(message, "s", &name);
426 r = manager_get_seat_from_creds(m, message, name, error, &seat);
430 p = seat_bus_path(seat);
434 return sd_bus_reply_method_return(message, "o", p);
437 static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
438 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
439 Manager *m = userdata;
447 r = sd_bus_message_new_method_return(message, &reply);
451 r = sd_bus_message_open_container(reply, 'a', "(susso)");
455 HASHMAP_FOREACH(session, m->sessions, i) {
456 _cleanup_free_ char *p = NULL;
458 p = session_bus_path(session);
462 r = sd_bus_message_append(reply, "(susso)",
464 (uint32_t) session->user->uid,
466 session->seat ? session->seat->id : "",
472 r = sd_bus_message_close_container(reply);
476 return sd_bus_send(NULL, reply, NULL);
479 static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
480 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
481 Manager *m = userdata;
489 r = sd_bus_message_new_method_return(message, &reply);
493 r = sd_bus_message_open_container(reply, 'a', "(uso)");
497 HASHMAP_FOREACH(user, m->users, i) {
498 _cleanup_free_ char *p = NULL;
500 p = user_bus_path(user);
504 r = sd_bus_message_append(reply, "(uso)",
505 (uint32_t) user->uid,
512 r = sd_bus_message_close_container(reply);
516 return sd_bus_send(NULL, reply, NULL);
519 static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
520 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
521 Manager *m = userdata;
529 r = sd_bus_message_new_method_return(message, &reply);
533 r = sd_bus_message_open_container(reply, 'a', "(so)");
537 HASHMAP_FOREACH(seat, m->seats, i) {
538 _cleanup_free_ char *p = NULL;
540 p = seat_bus_path(seat);
544 r = sd_bus_message_append(reply, "(so)", seat->id, p);
549 r = sd_bus_message_close_container(reply);
553 return sd_bus_send(NULL, reply, NULL);
556 static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
557 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
558 Manager *m = userdata;
559 Inhibitor *inhibitor;
566 r = sd_bus_message_new_method_return(message, &reply);
570 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
574 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
576 r = sd_bus_message_append(reply, "(ssssuu)",
577 strempty(inhibit_what_to_string(inhibitor->what)),
578 strempty(inhibitor->who),
579 strempty(inhibitor->why),
580 strempty(inhibit_mode_to_string(inhibitor->mode)),
581 (uint32_t) inhibitor->uid,
582 (uint32_t) inhibitor->pid);
587 r = sd_bus_message_close_container(reply);
591 return sd_bus_send(NULL, reply, NULL);
594 static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
595 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
596 _cleanup_free_ char *id = NULL;
597 Session *session = NULL;
598 uint32_t audit_id = 0;
599 Manager *m = userdata;
613 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
614 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
616 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
620 if (!uid_is_valid(uid))
621 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
622 if (leader < 0 || leader == 1 || leader == getpid_cached())
623 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
626 t = _SESSION_TYPE_INVALID;
628 t = session_type_from_string(type);
630 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
634 c = _SESSION_CLASS_INVALID;
636 c = session_class_from_string(class);
638 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
641 if (isempty(desktop))
644 if (!string_is_safe(desktop))
645 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
651 seat = hashmap_get(m->seats, cseat);
653 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
656 if (tty_is_vc(tty)) {
661 else if (seat != m->seat0)
662 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TTY %s is virtual console but seat %s is not seat0", tty, seat->id);
664 v = vtnr_from_tty(tty);
666 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
670 else if (vtnr != (uint32_t) v)
671 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
673 } else if (tty_is_console(tty)) {
677 else if (seat != m->seat0)
678 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
681 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
685 if (seat_has_vts(seat)) {
686 if (vtnr <= 0 || vtnr > 63)
687 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
690 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
694 if (t == _SESSION_TYPE_INVALID) {
695 if (!isempty(display))
697 else if (!isempty(tty))
700 t = SESSION_UNSPECIFIED;
703 if (c == _SESSION_CLASS_INVALID) {
704 if (t == SESSION_UNSPECIFIED)
705 c = SESSION_BACKGROUND;
711 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
713 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
717 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
722 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
723 * that avoids creating a logind session. */
724 r = manager_get_user_by_pid(m, leader, NULL);
728 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice");
731 * Old gdm and lightdm start the user-session on the same VT as
732 * the greeter session. But they destroy the greeter session
733 * after the user-session and want the user-session to take
734 * over the VT. We need to support this for
735 * backwards-compatibility, so make sure we allow new sessions
736 * on a VT that a greeter is running on. Furthermore, to allow
737 * re-logins, we have to allow a greeter to take over a used VT for
738 * the exact same reasons.
740 if (c != SESSION_GREETER &&
742 vtnr < m->seat0->position_count &&
743 m->seat0->positions[vtnr] &&
744 m->seat0->positions[vtnr]->class != SESSION_GREETER)
745 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
747 if (hashmap_size(m->sessions) >= m->sessions_max)
748 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
750 (void) audit_session_from_pid(leader, &audit_id);
751 if (audit_session_is_valid(audit_id)) {
752 /* Keep our session IDs and the audit session IDs in sync */
754 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
757 /* Wut? There's already a session by this name and we
758 * didn't find it above? Weird, then let's not trust
759 * the audit data and let's better register a new
761 if (hashmap_get(m->sessions, id)) {
762 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
763 audit_id = AUDIT_SESSION_INVALID;
772 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
775 } while (hashmap_get(m->sessions, id));
778 r = manager_add_user_by_uid(m, uid, &user);
782 r = manager_add_session(m, id, &session);
786 session_set_user(session, user);
788 session->leader = leader;
789 session->audit_id = audit_id;
792 session->remote = remote;
793 session->vtnr = vtnr;
796 session->tty = strdup(tty);
803 if (!isempty(display)) {
804 session->display = strdup(display);
805 if (!session->display) {
811 if (!isempty(remote_user)) {
812 session->remote_user = strdup(remote_user);
813 if (!session->remote_user) {
819 if (!isempty(remote_host)) {
820 session->remote_host = strdup(remote_host);
821 if (!session->remote_host) {
827 if (!isempty(service)) {
828 session->service = strdup(service);
829 if (!session->service) {
835 if (!isempty(desktop)) {
836 session->desktop = strdup(desktop);
837 if (!session->desktop) {
844 r = seat_attach_session(seat, session);
849 r = sd_bus_message_enter_container(message, 'a', "(sv)");
853 r = session_start(session, message);
857 r = sd_bus_message_exit_container(message);
861 session->create_message = sd_bus_message_ref(message);
863 #if 0 /// UNNEEDED by elogind
864 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
865 * session_send_create_reply(). */
867 /* We reply directly. */
869 r = session_send_create_reply(session, NULL);
878 session_add_to_gc_queue(session);
881 user_add_to_gc_queue(user);
886 static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
887 Manager *m = userdata;
895 r = sd_bus_message_read(message, "s", &name);
899 r = manager_get_session_from_creds(m, message, name, error, &session);
903 r = session_release(session);
907 #if 1 /// elogind must queue this session
908 session_add_to_gc_queue(session);
910 return sd_bus_reply_method_return(message, NULL);
913 static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
914 Manager *m = userdata;
922 r = sd_bus_message_read(message, "s", &name);
926 r = manager_get_session_from_creds(m, message, name, error, &session);
930 return bus_session_method_activate(message, session, error);
933 static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
934 const char *session_name, *seat_name;
935 Manager *m = userdata;
943 /* Same as ActivateSession() but refuses to work if
944 * the seat doesn't match */
946 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
950 r = manager_get_session_from_creds(m, message, session_name, error, &session);
954 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
958 if (session->seat != seat)
959 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
961 r = session_activate(session);
965 return sd_bus_reply_method_return(message, NULL);
968 static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
969 Manager *m = userdata;
977 r = sd_bus_message_read(message, "s", &name);
981 r = manager_get_session_from_creds(m, message, name, error, &session);
985 return bus_session_method_lock(message, session, error);
988 static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
989 Manager *m = userdata;
995 r = bus_verify_polkit_async(
998 "org.freedesktop.login1.lock-sessions",
1002 &m->polkit_registry,
1007 return 1; /* Will call us back */
1009 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
1013 return sd_bus_reply_method_return(message, NULL);
1016 static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1018 Manager *m = userdata;
1025 r = sd_bus_message_read(message, "s", &name);
1029 r = manager_get_session_from_creds(m, message, name, error, &session);
1033 return bus_session_method_kill(message, session, error);
1036 static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1037 Manager *m = userdata;
1045 r = sd_bus_message_read(message, "u", &uid);
1049 r = manager_get_user_from_creds(m, message, uid, error, &user);
1053 return bus_user_method_kill(message, user, error);
1056 static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1057 Manager *m = userdata;
1065 r = sd_bus_message_read(message, "s", &name);
1069 r = manager_get_session_from_creds(m, message, name, error, &session);
1073 return bus_session_method_terminate(message, session, error);
1076 static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1077 Manager *m = userdata;
1085 r = sd_bus_message_read(message, "u", &uid);
1089 r = manager_get_user_from_creds(m, message, uid, error, &user);
1093 return bus_user_method_terminate(message, user, error);
1096 static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1097 Manager *m = userdata;
1105 r = sd_bus_message_read(message, "s", &name);
1109 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1113 return bus_seat_method_terminate(message, seat, error);
1116 static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1117 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1118 _cleanup_free_ char *cc = NULL;
1119 Manager *m = userdata;
1120 int r, b, interactive;
1123 uint32_t uid, auth_uid;
1128 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1132 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID |
1133 SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1137 if (!uid_is_valid(uid)) {
1138 /* Note that we get the owner UID of the session or user unit,
1139 * not the actual client UID here! */
1140 r = sd_bus_creds_get_owner_uid(creds, &uid);
1145 /* owner_uid is racy, so for authorization we must use euid */
1146 r = sd_bus_creds_get_euid(creds, &auth_uid);
1153 return errno > 0 ? -errno : -ENOENT;
1155 r = bus_verify_polkit_async(
1158 uid == auth_uid ? "org.freedesktop.login1.set-self-linger" :
1159 "org.freedesktop.login1.set-user-linger",
1163 &m->polkit_registry,
1168 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1170 mkdir_p_label("/var/lib/elogind", 0755);
1172 r = mkdir_safe_label("/var/lib/elogind/linger", 0755, 0, 0, MKDIR_WARN_MODE);
1176 cc = cescape(pw->pw_name);
1180 path = strjoina("/var/lib/elogind/linger/", cc);
1188 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1195 if (r < 0 && errno != ENOENT)
1198 u = hashmap_get(m->users, UID_TO_PTR(uid));
1200 user_add_to_gc_queue(u);
1203 return sd_bus_reply_method_return(message, NULL);
1206 static int trigger_device(Manager *m, struct udev_device *d) {
1207 _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
1208 struct udev_list_entry *first, *item;
1213 e = udev_enumerate_new(m->udev);
1218 r = udev_enumerate_add_match_parent(e, d);
1223 r = udev_enumerate_scan_devices(e);
1227 first = udev_enumerate_get_list_entry(e);
1228 udev_list_entry_foreach(item, first) {
1229 _cleanup_free_ char *t = NULL;
1232 p = udev_list_entry_get_name(item);
1234 t = strappend(p, "/uevent");
1238 (void) write_string_file(t, "change", 0);
1244 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1245 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
1246 _cleanup_free_ char *rule = NULL, *file = NULL;
1247 const char *id_for_seat;
1254 d = udev_device_new_from_syspath(m->udev, sysfs);
1258 if (!udev_device_has_tag(d, "seat"))
1261 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
1265 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1268 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1271 mkdir_p_label("/etc/udev/rules.d", 0755);
1272 r = write_string_file_atomic_label(file, rule);
1276 return trigger_device(m, d);
1279 static int flush_devices(Manager *m) {
1280 _cleanup_closedir_ DIR *d;
1284 d = opendir("/etc/udev/rules.d");
1286 if (errno != ENOENT)
1287 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1291 FOREACH_DIRENT_ALL(de, d, break) {
1292 if (!dirent_is_file(de))
1295 if (!startswith(de->d_name, "72-seat-"))
1298 if (!endswith(de->d_name, ".rules"))
1301 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1302 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1306 return trigger_device(m, NULL);
1309 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1310 const char *sysfs, *seat;
1311 Manager *m = userdata;
1317 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1321 if (!path_startswith(sysfs, "/sys"))
1322 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1324 if (!seat_name_is_valid(seat))
1325 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1327 r = bus_verify_polkit_async(
1330 "org.freedesktop.login1.attach-device",
1334 &m->polkit_registry,
1339 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1341 r = attach_device(m, seat, sysfs);
1345 return sd_bus_reply_method_return(message, NULL);
1348 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1349 Manager *m = userdata;
1355 r = sd_bus_message_read(message, "b", &interactive);
1359 r = bus_verify_polkit_async(
1362 "org.freedesktop.login1.flush-devices",
1366 &m->polkit_registry,
1371 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1373 r = flush_devices(m);
1377 return sd_bus_reply_method_return(message, NULL);
1380 static int have_multiple_sessions(
1389 /* Check for other users' sessions. Greeter sessions do not
1390 * count, and non-login sessions do not count either. */
1391 HASHMAP_FOREACH(session, m->sessions, i)
1392 if (session->class == SESSION_USER &&
1393 session->user->uid != uid)
1399 #if 0 /// elogind has its own variant in elogind-dbus.c
1400 static int bus_manager_log_shutdown(
1402 const char *unit_name) {
1409 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1410 p = "MESSAGE=System is powering down";
1411 q = "SHUTDOWN=power-off";
1412 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1413 p = "MESSAGE=System is rebooting";
1414 q = "SHUTDOWN=reboot";
1415 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1416 p = "MESSAGE=System is halting";
1417 q = "SHUTDOWN=halt";
1418 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1419 p = "MESSAGE=System is rebooting with kexec";
1420 q = "SHUTDOWN=kexec";
1422 p = "MESSAGE=System is shutting down";
1426 if (isempty(m->wall_message))
1427 p = strjoina(p, ".");
1429 p = strjoina(p, " (", m->wall_message, ").");
1431 return log_struct(LOG_NOTICE,
1432 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
1438 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1439 Manager *m = userdata;
1444 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1448 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1453 if (until <= now(CLOCK_MONOTONIC))
1456 /* We want to ignore the lid switch for a while after each
1457 * suspend, and after boot-up. Hence let's install a timer for
1458 * this. As long as the event source exists we ignore the lid
1461 if (m->lid_switch_ignore_event_source) {
1464 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1471 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1473 r = sd_event_add_time(
1475 &m->lid_switch_ignore_event_source,
1478 lid_switch_ignore_handler, m);
1483 #if 0 /// elogind-dbus.c needs to access this
1484 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1486 int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1489 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1490 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1491 [INHIBIT_SLEEP] = "PrepareForSleep"
1494 int active = _active;
1498 assert(w < _INHIBIT_WHAT_MAX);
1499 assert(signal_name[w]);
1501 return sd_bus_emit_signal(m->bus,
1502 "/org/freedesktop/login1",
1503 "org.freedesktop.login1.Manager",
1509 #if 0 /// elogind has its own variant in elogind-dbus.c
1510 static int execute_shutdown_or_sleep(
1513 const char *unit_name,
1514 sd_bus_error *error) {
1516 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1523 assert(w < _INHIBIT_WHAT_MAX);
1526 if (w == INHIBIT_SHUTDOWN)
1527 bus_manager_log_shutdown(m, unit_name);
1529 r = sd_bus_call_method(
1531 "org.freedesktop.systemd1",
1532 "/org/freedesktop/systemd1",
1533 "org.freedesktop.systemd1.Manager",
1537 "ss", unit_name, "replace-irreversibly");
1541 r = sd_bus_message_read(reply, "o", &p);
1551 m->action_unit = unit_name;
1552 free(m->action_job);
1556 /* Make sure the lid switch is ignored for a while */
1557 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1562 /* Tell people that they now may take a lock again */
1563 (void) send_prepare_for(m, w, false);
1569 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1571 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1572 Inhibitor *offending = NULL;
1577 #if 0 /// elogind has no action_job, but a pending_action
1578 if (manager->action_what == 0 || manager->action_job)
1580 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
1584 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1585 _cleanup_free_ char *comm = NULL, *u = NULL;
1590 (void) get_process_comm(offending->pid, &comm);
1591 u = uid_to_name(offending->uid);
1593 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1594 offending->uid, strna(u),
1595 offending->pid, strna(comm));
1598 /* Actually do the operation */
1599 #if 0 /// elogind has no action_unit but a pending_action
1600 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1602 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
1605 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1606 bus_error_message(&error, r));
1609 #if 0 /// elogind has no action_unit but a pending_action
1610 manager->action_unit = NULL;
1611 manager->action_what = 0;
1614 manager->pending_action = HANDLE_IGNORE;
1615 manager->action_what = 0;
1616 /* It is not a critical error for elogind if suspending fails */
1623 static int manager_inhibit_timeout_handler(
1628 Manager *manager = userdata;
1632 assert(manager->inhibit_timeout_source == s);
1634 r = manager_dispatch_delayed(manager, true);
1635 return (r < 0) ? r : 0;
1638 #if 0 /// elogind does not have unit_name but action
1639 static int delay_shutdown_or_sleep(
1642 const char *unit_name) {
1645 int delay_shutdown_or_sleep(
1648 HandleAction action) {
1655 assert(w < _INHIBIT_WHAT_MAX);
1656 #if 0 /// UNNEEDED by elogind
1660 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1662 if (m->inhibit_timeout_source) {
1663 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1665 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1667 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1669 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1671 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1672 timeout_val, 0, manager_inhibit_timeout_handler, m);
1677 #if 0 /// elogind does not have unit_name but pendig_action
1678 m->action_unit = unit_name;
1680 m->pending_action = action;
1687 int bus_manager_shutdown_or_sleep_now_or_later(
1689 #if 0 /// elogind has HandleAction instead of const char* unit_name
1690 const char *unit_name,
1692 _cleanup_free_ char *load_state = NULL;
1694 HandleAction unit_name,
1697 sd_bus_error *error) {
1702 #if 0 /// for elogind only w has to be checked.
1705 assert(w <= _INHIBIT_WHAT_MAX);
1706 assert(!m->action_job);
1708 r = unit_load_state(m->bus, unit_name, &load_state);
1712 if (!streq(load_state, "loaded")) {
1713 log_notice("Unit %s is %s, refusing operation.", unit_name, load_state);
1718 assert(w <= _INHIBIT_WHAT_MAX);
1721 /* Tell everybody to prepare for shutdown/sleep */
1722 (void) send_prepare_for(m, w, true);
1725 m->inhibit_delay_max > 0 &&
1726 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1728 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
1729 handle_action_to_string(unit_name),
1730 delayed ? "" : "NOT ");
1732 /* Shutdown is delayed, keep in mind what we
1733 * want to do, and start a timeout */
1734 r = delay_shutdown_or_sleep(m, w, unit_name);
1736 /* Shutdown is not delayed, execute it
1738 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1743 static int verify_shutdown_creds(
1745 sd_bus_message *message,
1749 const char *action_multiple_sessions,
1750 const char *action_ignore_inhibit,
1751 sd_bus_error *error) {
1753 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1754 bool multiple_sessions, blocked;
1761 assert(w <= _INHIBIT_WHAT_MAX);
1763 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1767 r = sd_bus_creds_get_euid(creds, &uid);
1771 r = have_multiple_sessions(m, uid);
1775 multiple_sessions = r > 0;
1776 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1778 if (multiple_sessions && action_multiple_sessions) {
1779 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1783 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1786 if (blocked && action_ignore_inhibit) {
1787 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1791 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1794 if (!multiple_sessions && !blocked && action) {
1795 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1799 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1805 static int method_do_shutdown_or_sleep(
1807 sd_bus_message *message,
1808 #if 0 /// elogind has HandleAction instead of const char* unit_name
1809 const char *unit_name,
1811 HandleAction unit_name,
1815 const char *action_multiple_sessions,
1816 const char *action_ignore_inhibit,
1817 const char *sleep_verb,
1818 sd_bus_error *error) {
1824 #if 0 /// elogind does not need this to be checked
1828 assert(w <= _INHIBIT_WHAT_MAX);
1830 r = sd_bus_message_read(message, "b", &interactive);
1834 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
1835 __FUNCTION__, action, sleep_verb,
1836 interactive ? "" : "NOT ");
1837 /* Don't allow multiple jobs being executed at the same time */
1839 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1842 #if 0 /// Within elogind the manager m must be provided, too
1843 r = can_sleep(sleep_verb);
1845 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1846 "Not enough swap space for hibernation");
1848 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1849 "Sleep verb \"%s\" not supported", sleep_verb);
1851 r = can_sleep(m, sleep_verb);
1857 #if 0 /// Within elogind it does not make sense to verify shutdown creds when suspending
1858 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1859 action_ignore_inhibit, error);
1863 if (IN_SET(unit_name, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
1864 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1865 action_ignore_inhibit, error);
1866 log_debug_elogind("verify_shutdown_creds() returned %d", r);
1872 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1876 return sd_bus_reply_method_return(message, NULL);
1879 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1880 Manager *m = userdata;
1882 log_debug_elogind("%s called", __FUNCTION__);
1883 return method_do_shutdown_or_sleep(
1885 #if 0 /// elogind uses HandleAction instead of const char* unti names
1886 SPECIAL_POWEROFF_TARGET,
1891 "org.freedesktop.login1.power-off",
1892 "org.freedesktop.login1.power-off-multiple-sessions",
1893 "org.freedesktop.login1.power-off-ignore-inhibit",
1898 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1899 Manager *m = userdata;
1901 log_debug_elogind("%s called", __FUNCTION__);
1902 return method_do_shutdown_or_sleep(
1904 #if 0 /// elogind uses HandleAction instead of const char* unti names
1905 SPECIAL_REBOOT_TARGET,
1910 "org.freedesktop.login1.reboot",
1911 "org.freedesktop.login1.reboot-multiple-sessions",
1912 "org.freedesktop.login1.reboot-ignore-inhibit",
1917 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1918 Manager *m = userdata;
1920 log_debug_elogind("%s called", __FUNCTION__);
1921 return method_do_shutdown_or_sleep(
1923 #if 0 /// elogind uses HandleAction instead of const char* unti names
1924 SPECIAL_HALT_TARGET,
1929 "org.freedesktop.login1.halt",
1930 "org.freedesktop.login1.halt-multiple-sessions",
1931 "org.freedesktop.login1.halt-ignore-inhibit",
1936 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1937 Manager *m = userdata;
1939 log_debug_elogind("%s called", __FUNCTION__);
1940 return method_do_shutdown_or_sleep(
1942 #if 0 /// elogind uses HandleAction instead of const char* unti names
1943 SPECIAL_SUSPEND_TARGET,
1948 "org.freedesktop.login1.suspend",
1949 "org.freedesktop.login1.suspend-multiple-sessions",
1950 "org.freedesktop.login1.suspend-ignore-inhibit",
1955 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1956 Manager *m = userdata;
1958 log_debug_elogind("%s called", __FUNCTION__);
1959 return method_do_shutdown_or_sleep(
1961 #if 0 /// elogind uses HandleAction instead of const char* unti names
1962 SPECIAL_HIBERNATE_TARGET,
1967 "org.freedesktop.login1.hibernate",
1968 "org.freedesktop.login1.hibernate-multiple-sessions",
1969 "org.freedesktop.login1.hibernate-ignore-inhibit",
1974 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1975 Manager *m = userdata;
1977 log_debug_elogind("%s called", __FUNCTION__);
1978 return method_do_shutdown_or_sleep(
1980 #if 0 /// elogind uses HandleAction instead of const char* unti names
1981 SPECIAL_HYBRID_SLEEP_TARGET,
1983 HANDLE_HYBRID_SLEEP,
1986 "org.freedesktop.login1.hibernate",
1987 "org.freedesktop.login1.hibernate-multiple-sessions",
1988 "org.freedesktop.login1.hibernate-ignore-inhibit",
1993 static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1994 Manager *m = userdata;
1996 return method_do_shutdown_or_sleep(
1998 #if 0 /// elogind uses HandleAction instead of const char* unti names
1999 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
2001 HANDLE_SUSPEND_THEN_HIBERNATE,
2004 "org.freedesktop.login1.hibernate",
2005 "org.freedesktop.login1.hibernate-multiple-sessions",
2006 "org.freedesktop.login1.hibernate-ignore-inhibit",
2011 static int nologin_timeout_handler(
2016 Manager *m = userdata;
2018 log_info("Creating /run/nologin, blocking further logins...");
2021 create_shutdown_run_nologin_or_warn() >= 0;
2026 static int update_schedule_file(Manager *m) {
2027 _cleanup_free_ char *temp_path = NULL;
2028 _cleanup_fclose_ FILE *f = NULL;
2033 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
2035 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
2037 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
2039 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
2041 (void) fchmod(fileno(f), 0644);
2047 m->scheduled_shutdown_timeout,
2048 m->enable_wall_messages,
2049 m->scheduled_shutdown_type);
2051 if (!isempty(m->wall_message)) {
2052 _cleanup_free_ char *t;
2054 t = cescape(m->wall_message);
2060 fprintf(f, "WALL_MESSAGE=%s\n", t);
2063 r = fflush_and_check(f);
2067 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
2075 (void) unlink(temp_path);
2076 (void) unlink("/run/systemd/shutdown/scheduled");
2078 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
2081 #if 0 /// elogind must access this from elogind-dbus.c
2082 static void reset_scheduled_shutdown(Manager *m) {
2084 void reset_scheduled_shutdown(Manager *m) {
2088 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2089 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
2090 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
2092 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
2093 m->scheduled_shutdown_timeout = 0;
2094 m->shutdown_dry_run = false;
2096 if (m->unlink_nologin) {
2097 (void) unlink_or_warn("/run/nologin");
2098 m->unlink_nologin = false;
2101 (void) unlink("/run/systemd/shutdown/scheduled");
2104 #if 0 /// elogind has its own variant in elogind-dbus.c
2105 static int manager_scheduled_shutdown_handler(
2110 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2111 Manager *m = userdata;
2117 if (isempty(m->scheduled_shutdown_type))
2120 if (streq(m->scheduled_shutdown_type, "poweroff"))
2121 target = SPECIAL_POWEROFF_TARGET;
2122 else if (streq(m->scheduled_shutdown_type, "reboot"))
2123 target = SPECIAL_REBOOT_TARGET;
2124 else if (streq(m->scheduled_shutdown_type, "halt"))
2125 target = SPECIAL_HALT_TARGET;
2127 assert_not_reached("unexpected shutdown type");
2129 /* Don't allow multiple jobs being executed at the same time */
2130 if (m->action_what) {
2132 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
2136 if (m->shutdown_dry_run) {
2137 /* We do not process delay inhibitors here. Otherwise, we
2138 * would have to be considered "in progress" (like the check
2139 * above) for some seconds after our admin has seen the final
2142 bus_manager_log_shutdown(m, target);
2143 log_info("Running in dry run, suppressing action.");
2144 reset_scheduled_shutdown(m);
2149 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
2151 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2158 reset_scheduled_shutdown(m);
2163 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2164 Manager *m = userdata;
2165 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2166 const char *action_multiple_sessions = NULL;
2167 const char *action_ignore_inhibit = NULL;
2168 const char *action = NULL;
2172 bool dry_run = false;
2177 log_debug_elogind("%s called", __FUNCTION__);
2178 r = sd_bus_message_read(message, "st", &type, &elapse);
2182 if (startswith(type, "dry-")) {
2187 if (streq(type, "poweroff")) {
2188 action = "org.freedesktop.login1.power-off";
2189 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2190 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2191 } else if (streq(type, "reboot")) {
2192 action = "org.freedesktop.login1.reboot";
2193 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2194 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2195 } else if (streq(type, "halt")) {
2196 action = "org.freedesktop.login1.halt";
2197 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2198 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2200 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2202 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2203 action, action_multiple_sessions, action_ignore_inhibit, error);
2207 if (m->scheduled_shutdown_timeout_source) {
2208 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2210 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2212 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2214 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2216 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2217 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2219 return log_error_errno(r, "sd_event_add_time() failed: %m");
2222 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2224 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2228 m->shutdown_dry_run = dry_run;
2230 if (m->nologin_timeout_source) {
2231 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2233 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2235 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2237 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2239 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2240 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2242 return log_error_errno(r, "sd_event_add_time() failed: %m");
2245 m->scheduled_shutdown_timeout = elapse;
2247 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2249 const char *tty = NULL;
2251 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2252 (void) sd_bus_creds_get_tty(creds, &tty);
2254 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2256 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2261 r = manager_setup_wall_message_timer(m);
2265 r = update_schedule_file(m);
2269 return sd_bus_reply_method_return(message, NULL);
2272 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2273 Manager *m = userdata;
2275 #if 1 /// elogind needs to construct the message to allow extra wall messages
2276 _cleanup_free_ char *l = NULL;
2282 log_debug_elogind("%s called", __FUNCTION__);
2283 cancelled = m->scheduled_shutdown_type != NULL;
2284 reset_scheduled_shutdown(m);
2286 if (cancelled && m->enable_wall_messages) {
2287 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2288 const char *tty = NULL;
2292 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2294 (void) sd_bus_creds_get_uid(creds, &uid);
2295 (void) sd_bus_creds_get_tty(creds, &tty);
2298 #if 0 /// elogind wants to allow extra cancellation messages
2299 utmp_wall("The system shutdown has been cancelled",
2300 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2302 r = asprintf(&l, "%s%sThe system shutdown has been cancelled!",
2303 strempty(m->wall_message),
2304 isempty(m->wall_message) ? "" : "\n");
2310 utmp_wall(l, uid_to_name(uid), tty, logind_wall_tty_filter, m);
2314 return sd_bus_reply_method_return(message, "b", cancelled);
2317 static int method_can_shutdown_or_sleep(
2319 sd_bus_message *message,
2322 const char *action_multiple_sessions,
2323 const char *action_ignore_inhibit,
2324 const char *sleep_verb,
2325 sd_bus_error *error) {
2327 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2328 HandleAction handle;
2329 bool multiple_sessions, challenge, blocked;
2330 const char *result = NULL;
2337 assert(w <= _INHIBIT_WHAT_MAX);
2339 assert(action_multiple_sessions);
2340 assert(action_ignore_inhibit);
2343 #if 0 /// elogind needs to have the manager being passed
2344 r = can_sleep(sleep_verb);
2346 r = can_sleep(m, sleep_verb);
2348 if (IN_SET(r, 0, -ENOSPC))
2349 return sd_bus_reply_method_return(message, "s", "na");
2354 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2358 r = sd_bus_creds_get_euid(creds, &uid);
2362 r = have_multiple_sessions(m, uid);
2366 multiple_sessions = r > 0;
2367 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2369 handle = handle_action_from_string(sleep_verb);
2373 target = manager_target_for_action(handle);
2374 #if 0 /// elogind does not support systemd units units. A valid handle is enough
2376 _cleanup_free_ char *load_state = NULL;
2378 r = unit_load_state(m->bus, target, &load_state);
2382 if (!streq(load_state, "loaded")) {
2384 if (NULL == target) {
2388 #if 0 /// one less with elogind...
2393 if ( (handle <= HANDLE_IGNORE) || (handle >= _HANDLE_ACTION_MAX) ) {
2398 if (multiple_sessions) {
2399 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2406 result = "challenge";
2412 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2416 if (r > 0 && !result)
2418 else if (challenge && (!result || streq(result, "yes")))
2419 result = "challenge";
2424 if (!multiple_sessions && !blocked) {
2425 /* If neither inhibit nor multiple sessions
2426 * apply then just check the normal policy */
2428 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2435 result = "challenge";
2442 return sd_bus_reply_method_return(message, "s", result);
2445 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2446 Manager *m = userdata;
2448 return method_can_shutdown_or_sleep(
2451 "org.freedesktop.login1.power-off",
2452 "org.freedesktop.login1.power-off-multiple-sessions",
2453 "org.freedesktop.login1.power-off-ignore-inhibit",
2458 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2459 Manager *m = userdata;
2461 return method_can_shutdown_or_sleep(
2464 "org.freedesktop.login1.reboot",
2465 "org.freedesktop.login1.reboot-multiple-sessions",
2466 "org.freedesktop.login1.reboot-ignore-inhibit",
2471 static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2472 Manager *m = userdata;
2474 return method_can_shutdown_or_sleep(
2477 "org.freedesktop.login1.halt",
2478 "org.freedesktop.login1.halt-multiple-sessions",
2479 "org.freedesktop.login1.halt-ignore-inhibit",
2484 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2485 Manager *m = userdata;
2487 return method_can_shutdown_or_sleep(
2490 "org.freedesktop.login1.suspend",
2491 "org.freedesktop.login1.suspend-multiple-sessions",
2492 "org.freedesktop.login1.suspend-ignore-inhibit",
2497 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2498 Manager *m = userdata;
2500 return method_can_shutdown_or_sleep(
2503 "org.freedesktop.login1.hibernate",
2504 "org.freedesktop.login1.hibernate-multiple-sessions",
2505 "org.freedesktop.login1.hibernate-ignore-inhibit",
2510 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2511 Manager *m = userdata;
2513 return method_can_shutdown_or_sleep(
2516 "org.freedesktop.login1.hibernate",
2517 "org.freedesktop.login1.hibernate-multiple-sessions",
2518 "org.freedesktop.login1.hibernate-ignore-inhibit",
2523 static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2524 Manager *m = userdata;
2526 return method_can_shutdown_or_sleep(
2529 "org.freedesktop.login1.hibernate",
2530 "org.freedesktop.login1.hibernate-multiple-sessions",
2531 "org.freedesktop.login1.hibernate-ignore-inhibit",
2532 "suspend-then-hibernate",
2536 static int property_get_reboot_to_firmware_setup(
2539 const char *interface,
2540 const char *property,
2541 sd_bus_message *reply,
2543 sd_bus_error *error) {
2544 #if 0 /// elogind does not support EFI
2551 r = efi_get_reboot_to_firmware();
2552 if (r < 0 && r != -EOPNOTSUPP)
2553 log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m");
2555 return sd_bus_message_append(reply, "b", r > 0);
2557 return sd_bus_message_append(reply, "b", false);
2561 static int method_set_reboot_to_firmware_setup(
2562 sd_bus_message *message,
2564 sd_bus_error *error) {
2567 Manager *m = userdata;
2572 r = sd_bus_message_read(message, "b", &b);
2576 r = bus_verify_polkit_async(message,
2578 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2582 &m->polkit_registry,
2587 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2589 #if 0 /// elogind does not support EFI
2590 r = efi_set_reboot_to_firmware(b);
2595 return sd_bus_reply_method_return(message, NULL);
2598 static int method_can_reboot_to_firmware_setup(
2599 sd_bus_message *message,
2601 sd_bus_error *error) {
2603 #if 0 /// elogind does not support EFI
2607 Manager *m = userdata;
2612 r = efi_reboot_to_firmware_supported();
2614 if (r != -EOPNOTSUPP)
2615 log_warning_errno(errno, "Failed to determine whether reboot to firmware is supported: %m");
2617 return sd_bus_reply_method_return(message, "s", "na");
2620 r = bus_test_polkit(message,
2622 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2633 result = "challenge";
2637 return sd_bus_reply_method_return(message, "s", result);
2639 return sd_bus_reply_method_return(message, "s", "no");
2643 static int method_set_wall_message(
2644 sd_bus_message *message,
2646 sd_bus_error *error) {
2649 Manager *m = userdata;
2651 unsigned enable_wall_messages;
2656 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2660 #if 0 /// elogind only calls this for shutdown/reboot, which already needs authorization.
2661 r = bus_verify_polkit_async(message,
2663 "org.freedesktop.login1.set-wall-message",
2667 &m->polkit_registry,
2672 return 1; /* Will call us back */
2675 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
2679 m->enable_wall_messages = enable_wall_messages;
2681 return sd_bus_reply_method_return(message, NULL);
2684 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2685 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2686 const char *who, *why, *what, *mode;
2687 _cleanup_free_ char *id = NULL;
2688 _cleanup_close_ int fifo_fd = -1;
2689 Manager *m = userdata;
2690 Inhibitor *i = NULL;
2700 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2704 w = inhibit_what_from_string(what);
2706 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2708 mm = inhibit_mode_from_string(mode);
2710 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2712 /* Delay is only supported for shutdown/sleep */
2713 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2714 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2716 /* Don't allow taking delay locks while we are already
2717 * executing the operation. We shouldn't create the impression
2718 * that the lock was successful if the machine is about to go
2719 * down/suspend any moment. */
2720 if (m->action_what & w)
2721 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2723 r = bus_verify_polkit_async(
2726 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2727 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2728 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2729 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2730 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2731 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2732 "org.freedesktop.login1.inhibit-handle-lid-switch",
2736 &m->polkit_registry,
2741 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2743 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2747 r = sd_bus_creds_get_euid(creds, &uid);
2751 r = sd_bus_creds_get_pid(creds, &pid);
2755 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2756 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2761 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2764 } while (hashmap_get(m->inhibitors, id));
2766 r = manager_add_inhibitor(m, id, &i);
2774 i->why = strdup(why);
2775 i->who = strdup(who);
2777 if (!i->why || !i->who) {
2782 fifo_fd = inhibitor_create_fifo(i);
2790 return sd_bus_reply_method_return(message, "h", fifo_fd);
2799 const sd_bus_vtable manager_vtable[] = {
2800 SD_BUS_VTABLE_START(0),
2802 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2803 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2805 #if 0 /// UNNEEDED by elogind
2806 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2808 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2809 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2810 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2811 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2812 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2813 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2814 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2815 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2816 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2817 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2818 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2819 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2820 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2821 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2822 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
2823 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2824 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2825 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2826 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2827 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2828 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2829 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2830 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2831 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2832 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2833 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2834 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0),
2835 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2836 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
2837 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
2839 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2840 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2841 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2842 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2843 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2844 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2845 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2846 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2847 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2848 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2849 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2850 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2851 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2852 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2853 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2854 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2855 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2856 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2857 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2858 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2859 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2860 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2861 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2862 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2863 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2864 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2865 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2866 SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2867 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2868 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2869 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2870 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2871 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2872 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2873 SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2874 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2875 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2876 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2877 SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2878 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2879 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2880 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2881 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2882 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2883 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2885 SD_BUS_SIGNAL("SessionNew", "so", 0),
2886 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2887 SD_BUS_SIGNAL("UserNew", "uo", 0),
2888 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2889 SD_BUS_SIGNAL("SeatNew", "so", 0),
2890 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2891 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2892 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2897 #if 0 /// UNNEEDED by elogind
2898 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2907 if (streq(result, "done"))
2908 r = session_send_create_reply(s, NULL);
2910 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2912 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2913 r = session_send_create_reply(s, &e);
2919 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2920 const char *path, *result, *unit;
2921 Manager *m = userdata;
2930 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2932 bus_log_parse_error(r);
2936 if (m->action_job && streq(m->action_job, path)) {
2937 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2939 /* Tell people that they now may take a lock again */
2940 (void) send_prepare_for(m, m->action_what, false);
2942 m->action_job = mfree(m->action_job);
2943 m->action_unit = NULL;
2948 session = hashmap_get(m->session_units, unit);
2949 if (session && streq_ptr(path, session->scope_job)) {
2950 session->scope_job = mfree(session->scope_job);
2951 session_jobs_reply(session, unit, result);
2953 session_save(session);
2954 user_save(session->user);
2955 session_add_to_gc_queue(session);
2958 user = hashmap_get(m->user_units, unit);
2960 (streq_ptr(path, user->service_job) ||
2961 streq_ptr(path, user->slice_job))) {
2963 if (streq_ptr(path, user->service_job))
2964 user->service_job = mfree(user->service_job);
2966 if (streq_ptr(path, user->slice_job))
2967 user->slice_job = mfree(user->slice_job);
2969 LIST_FOREACH(sessions_by_user, session, user->sessions)
2970 session_jobs_reply(session, unit, result);
2973 user_add_to_gc_queue(user);
2979 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2980 const char *path, *unit;
2981 Manager *m = userdata;
2989 r = sd_bus_message_read(message, "so", &unit, &path);
2991 bus_log_parse_error(r);
2995 session = hashmap_get(m->session_units, unit);
2997 session_add_to_gc_queue(session);
2999 user = hashmap_get(m->user_units, unit);
3001 user_add_to_gc_queue(user);
3006 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3007 _cleanup_free_ char *unit = NULL;
3008 Manager *m = userdata;
3017 path = sd_bus_message_get_path(message);
3021 r = unit_name_from_dbus_path(path, &unit);
3022 if (r == -EINVAL) /* not a unit */
3029 session = hashmap_get(m->session_units, unit);
3031 session_add_to_gc_queue(session);
3033 user = hashmap_get(m->user_units, unit);
3035 user_add_to_gc_queue(user);
3040 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3041 Manager *m = userdata;
3049 r = sd_bus_message_read(message, "b", &b);
3051 bus_log_parse_error(r);
3058 /* systemd finished reloading, let's recheck all our sessions */
3059 log_debug("System manager has been reloaded, rechecking sessions...");
3061 HASHMAP_FOREACH(session, m->sessions, i)
3062 session_add_to_gc_queue(session);
3068 int manager_send_changed(Manager *manager, const char *property, ...) {
3073 l = strv_from_stdarg_alloca(property);
3075 return sd_bus_emit_properties_changed_strv(
3077 "/org/freedesktop/login1",
3078 "org.freedesktop.login1.Manager",
3082 #if 0 /// UNNEEDED by elogind
3083 static int strdup_job(sd_bus_message *reply, char **job) {
3088 r = sd_bus_message_read(reply, "o", &j);
3100 int manager_start_scope(
3105 const char *description,
3108 sd_bus_message *more_properties,
3109 sd_bus_error *error,
3112 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
3120 r = sd_bus_message_new_method_call(
3123 "org.freedesktop.systemd1",
3124 "/org/freedesktop/systemd1",
3125 "org.freedesktop.systemd1.Manager",
3126 "StartTransientUnit");
3130 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
3134 r = sd_bus_message_open_container(m, 'a', "(sv)");
3138 if (!isempty(slice)) {
3139 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
3144 if (!isempty(description)) {
3145 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3150 if (!isempty(after)) {
3151 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3156 if (!isempty(after2)) {
3157 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3162 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3164 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
3168 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3172 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3173 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1);
3175 return bus_log_create_error(r);
3177 if (more_properties) {
3178 /* If TasksMax also appears here, it will overwrite the default value set above */
3179 r = sd_bus_message_copy(m, more_properties, true);
3184 r = sd_bus_message_close_container(m);
3188 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3192 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3196 return strdup_job(reply, job);
3199 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3200 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3207 r = sd_bus_call_method(
3209 "org.freedesktop.systemd1",
3210 "/org/freedesktop/systemd1",
3211 "org.freedesktop.systemd1.Manager",
3215 "ss", unit, "replace");
3219 return strdup_job(reply, job);
3222 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3223 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3230 r = sd_bus_call_method(
3232 "org.freedesktop.systemd1",
3233 "/org/freedesktop/systemd1",
3234 "org.freedesktop.systemd1.Manager",
3238 "ss", unit, "fail");
3240 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3241 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3244 sd_bus_error_free(error);
3251 return strdup_job(reply, job);
3254 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3255 _cleanup_free_ char *path = NULL;
3261 path = unit_dbus_path_from_name(scope);
3265 r = sd_bus_call_method(
3267 "org.freedesktop.systemd1",
3269 "org.freedesktop.systemd1.Scope",
3275 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3276 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3277 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3278 sd_bus_error_free(error);
3288 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3292 return sd_bus_call_method(
3294 "org.freedesktop.systemd1",
3295 "/org/freedesktop/systemd1",
3296 "org.freedesktop.systemd1.Manager",
3300 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3303 int manager_unit_is_active(Manager *manager, const char *unit) {
3304 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3305 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3306 _cleanup_free_ char *path = NULL;
3313 path = unit_dbus_path_from_name(unit);
3317 r = sd_bus_get_property(
3319 "org.freedesktop.systemd1",
3321 "org.freedesktop.systemd1.Unit",
3327 /* systemd might have droppped off momentarily, let's
3328 * not make this an error */
3329 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3330 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3333 /* If the unit is already unloaded then it's not
3335 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3336 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3342 r = sd_bus_message_read(reply, "s", &state);
3346 return !streq(state, "inactive") && !streq(state, "failed");
3349 int manager_job_is_active(Manager *manager, const char *path) {
3350 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3351 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3357 r = sd_bus_get_property(
3359 "org.freedesktop.systemd1",
3361 "org.freedesktop.systemd1.Job",
3367 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3368 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3371 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3377 /* We don't actually care about the state really. The fact
3378 * that we could read the job state is enough for us */