1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011 Lennart Poettering
11 #include "sd-messages.h"
13 #include "alloc-util.h"
14 #include "audit-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-error.h"
17 //#include "bus-unit-util.h"
19 #include "dirent-util.h"
20 //#include "efivars.h"
23 #include "fileio-label.h"
24 #include "format-util.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 //#include "cgroup-util.h"
31 #include "selinux-util.h"
32 #include "sleep-config.h"
33 //#include "special.h"
35 #include "terminal-util.h"
36 #include "udev-util.h"
37 #include "unit-name.h"
38 #include "user-util.h"
39 #include "utmp-wtmp.h"
41 /// Additional includes needed by elogind
42 #include "elogind-dbus.h"
44 static int get_sender_session(Manager *m, sd_bus_message *message, sd_bus_error *error, Session **ret) {
46 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
51 /* Get client login session. This is not what you are looking for these days,
52 * as apps may instead belong to a user service unit. This includes terminal
53 * emulators and hence command-line apps. */
54 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
58 r = sd_bus_creds_get_session(creds, &name);
64 session = hashmap_get(m->sessions, name);
72 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID,
73 "Caller does not belong to any known session");
76 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
84 return get_sender_session(m, message, error, ret);
86 session = hashmap_get(m->sessions, name);
88 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
94 static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) {
96 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
101 /* Note that we get the owner UID of the session, not the actual client UID here! */
102 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
106 r = sd_bus_creds_get_owner_uid(creds, &uid);
112 user = hashmap_get(m->users, UID_TO_PTR(uid));
120 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "Caller does not belong to any logged in user or lingering user");
123 int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
130 if (!uid_is_valid(uid))
131 return get_sender_user(m, message, error, ret);
133 user = hashmap_get(m->users, UID_TO_PTR(uid));
135 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "User ID "UID_FMT" is not logged in or lingering", uid);
141 int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) {
152 r = manager_get_session_from_creds(m, message, NULL, error, &session);
156 seat = session->seat;
158 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat.");
160 seat = hashmap_get(m->seats, name);
162 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
169 static int property_get_idle_hint(
172 const char *interface,
173 const char *property,
174 sd_bus_message *reply,
176 sd_bus_error *error) {
178 Manager *m = userdata;
184 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
187 static int property_get_idle_since_hint(
190 const char *interface,
191 const char *property,
192 sd_bus_message *reply,
194 sd_bus_error *error) {
196 Manager *m = userdata;
197 dual_timestamp t = DUAL_TIMESTAMP_NULL;
203 manager_get_idle_hint(m, &t);
205 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
208 static int property_get_inhibited(
211 const char *interface,
212 const char *property,
213 sd_bus_message *reply,
215 sd_bus_error *error) {
217 Manager *m = userdata;
224 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
226 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
229 static int property_get_preparing(
232 const char *interface,
233 const char *property,
234 sd_bus_message *reply,
236 sd_bus_error *error) {
238 Manager *m = userdata;
245 if (streq(property, "PreparingForShutdown"))
246 b = m->action_what & INHIBIT_SHUTDOWN;
248 b = m->action_what & INHIBIT_SLEEP;
250 return sd_bus_message_append(reply, "b", b);
253 static int property_get_scheduled_shutdown(
256 const char *interface,
257 const char *property,
258 sd_bus_message *reply,
260 sd_bus_error *error) {
262 Manager *m = userdata;
269 r = sd_bus_message_open_container(reply, 'r', "st");
273 r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
277 return sd_bus_message_close_container(reply);
280 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
281 static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays);
282 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX);
283 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size);
285 static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
286 _cleanup_free_ char *p = NULL;
287 Manager *m = userdata;
295 r = sd_bus_message_read(message, "s", &name);
299 r = manager_get_session_from_creds(m, message, name, error, &session);
303 p = session_bus_path(session);
307 return sd_bus_reply_method_return(message, "o", p);
310 /* Get login session of a process. This is not what you are looking for these days,
311 * as apps may instead belong to a user service unit. This includes terminal
312 * emulators and hence command-line apps. */
313 static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
314 _cleanup_free_ char *p = NULL;
315 Session *session = NULL;
316 Manager *m = userdata;
323 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
325 r = sd_bus_message_read(message, "u", &pid);
332 r = manager_get_session_from_creds(m, message, NULL, error, &session);
336 r = manager_get_session_by_pid(m, pid, &session);
341 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
344 p = session_bus_path(session);
348 return sd_bus_reply_method_return(message, "o", p);
351 static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
352 _cleanup_free_ char *p = NULL;
353 Manager *m = userdata;
361 r = sd_bus_message_read(message, "u", &uid);
365 r = manager_get_user_from_creds(m, message, uid, error, &user);
369 p = user_bus_path(user);
373 return sd_bus_reply_method_return(message, "o", p);
376 static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
377 _cleanup_free_ char *p = NULL;
378 Manager *m = userdata;
386 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
388 r = sd_bus_message_read(message, "u", &pid);
395 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
399 r = manager_get_user_by_pid(m, pid, &user);
403 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID,
404 "PID "PID_FMT" does not belong to any logged in user or lingering user",
408 p = user_bus_path(user);
412 return sd_bus_reply_method_return(message, "o", p);
415 static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
416 _cleanup_free_ char *p = NULL;
417 Manager *m = userdata;
425 r = sd_bus_message_read(message, "s", &name);
429 r = manager_get_seat_from_creds(m, message, name, error, &seat);
433 p = seat_bus_path(seat);
437 return sd_bus_reply_method_return(message, "o", p);
440 static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
441 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
442 Manager *m = userdata;
450 r = sd_bus_message_new_method_return(message, &reply);
454 r = sd_bus_message_open_container(reply, 'a', "(susso)");
458 HASHMAP_FOREACH(session, m->sessions, i) {
459 _cleanup_free_ char *p = NULL;
461 p = session_bus_path(session);
465 r = sd_bus_message_append(reply, "(susso)",
467 (uint32_t) session->user->uid,
469 session->seat ? session->seat->id : "",
475 r = sd_bus_message_close_container(reply);
479 return sd_bus_send(NULL, reply, NULL);
482 static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
483 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
484 Manager *m = userdata;
492 r = sd_bus_message_new_method_return(message, &reply);
496 r = sd_bus_message_open_container(reply, 'a', "(uso)");
500 HASHMAP_FOREACH(user, m->users, i) {
501 _cleanup_free_ char *p = NULL;
503 p = user_bus_path(user);
507 r = sd_bus_message_append(reply, "(uso)",
508 (uint32_t) user->uid,
515 r = sd_bus_message_close_container(reply);
519 return sd_bus_send(NULL, reply, NULL);
522 static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
523 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
524 Manager *m = userdata;
532 r = sd_bus_message_new_method_return(message, &reply);
536 r = sd_bus_message_open_container(reply, 'a', "(so)");
540 HASHMAP_FOREACH(seat, m->seats, i) {
541 _cleanup_free_ char *p = NULL;
543 p = seat_bus_path(seat);
547 r = sd_bus_message_append(reply, "(so)", seat->id, p);
552 r = sd_bus_message_close_container(reply);
556 return sd_bus_send(NULL, reply, NULL);
559 static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
560 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
561 Manager *m = userdata;
562 Inhibitor *inhibitor;
569 r = sd_bus_message_new_method_return(message, &reply);
573 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
577 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
579 r = sd_bus_message_append(reply, "(ssssuu)",
580 strempty(inhibit_what_to_string(inhibitor->what)),
581 strempty(inhibitor->who),
582 strempty(inhibitor->why),
583 strempty(inhibit_mode_to_string(inhibitor->mode)),
584 (uint32_t) inhibitor->uid,
585 (uint32_t) inhibitor->pid);
590 r = sd_bus_message_close_container(reply);
594 return sd_bus_send(NULL, reply, NULL);
597 static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
598 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
599 _cleanup_free_ char *id = NULL;
600 Session *session = NULL;
601 uint32_t audit_id = 0;
602 Manager *m = userdata;
616 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
617 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
619 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
623 if (!uid_is_valid(uid))
624 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
625 if (leader < 0 || leader == 1 || leader == getpid_cached())
626 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
629 t = _SESSION_TYPE_INVALID;
631 t = session_type_from_string(type);
633 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
637 c = _SESSION_CLASS_INVALID;
639 c = session_class_from_string(class);
641 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
644 if (isempty(desktop))
647 if (!string_is_safe(desktop))
648 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
654 seat = hashmap_get(m->seats, cseat);
656 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
659 if (tty_is_vc(tty)) {
664 else if (seat != m->seat0)
665 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TTY %s is virtual console but seat %s is not seat0", tty, seat->id);
667 v = vtnr_from_tty(tty);
669 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
673 else if (vtnr != (uint32_t) v)
674 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
676 } else if (tty_is_console(tty)) {
680 else if (seat != m->seat0)
681 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
684 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
688 if (seat_has_vts(seat)) {
689 if (vtnr <= 0 || vtnr > 63)
690 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
693 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
697 if (t == _SESSION_TYPE_INVALID) {
698 if (!isempty(display))
700 else if (!isempty(tty))
703 t = SESSION_UNSPECIFIED;
706 if (c == _SESSION_CLASS_INVALID) {
707 if (t == SESSION_UNSPECIFIED)
708 c = SESSION_BACKGROUND;
714 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
716 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
720 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
725 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
726 * that avoids creating a logind session. */
727 r = manager_get_user_by_pid(m, leader, NULL);
731 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice");
734 * Old gdm and lightdm start the user-session on the same VT as
735 * the greeter session. But they destroy the greeter session
736 * after the user-session and want the user-session to take
737 * over the VT. We need to support this for
738 * backwards-compatibility, so make sure we allow new sessions
739 * on a VT that a greeter is running on. Furthermore, to allow
740 * re-logins, we have to allow a greeter to take over a used VT for
741 * the exact same reasons.
743 if (c != SESSION_GREETER &&
745 vtnr < m->seat0->position_count &&
746 m->seat0->positions[vtnr] &&
747 m->seat0->positions[vtnr]->class != SESSION_GREETER)
748 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
750 if (hashmap_size(m->sessions) >= m->sessions_max)
751 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
753 (void) audit_session_from_pid(leader, &audit_id);
754 if (audit_session_is_valid(audit_id)) {
755 /* Keep our session IDs and the audit session IDs in sync */
757 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
760 /* Wut? There's already a session by this name and we
761 * didn't find it above? Weird, then let's not trust
762 * the audit data and let's better register a new
764 if (hashmap_get(m->sessions, id)) {
765 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
766 audit_id = AUDIT_SESSION_INVALID;
775 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
778 } while (hashmap_get(m->sessions, id));
781 r = manager_add_user_by_uid(m, uid, &user);
785 r = manager_add_session(m, id, &session);
789 session_set_user(session, user);
791 session->leader = leader;
792 session->audit_id = audit_id;
795 session->remote = remote;
796 session->vtnr = vtnr;
799 session->tty = strdup(tty);
806 if (!isempty(display)) {
807 session->display = strdup(display);
808 if (!session->display) {
814 if (!isempty(remote_user)) {
815 session->remote_user = strdup(remote_user);
816 if (!session->remote_user) {
822 if (!isempty(remote_host)) {
823 session->remote_host = strdup(remote_host);
824 if (!session->remote_host) {
830 if (!isempty(service)) {
831 session->service = strdup(service);
832 if (!session->service) {
838 if (!isempty(desktop)) {
839 session->desktop = strdup(desktop);
840 if (!session->desktop) {
847 r = seat_attach_session(seat, session);
852 r = sd_bus_message_enter_container(message, 'a', "(sv)");
856 r = session_start(session, message);
860 r = sd_bus_message_exit_container(message);
864 session->create_message = sd_bus_message_ref(message);
866 #if 0 /// UNNEEDED by elogind
867 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
868 * session_send_create_reply(). */
870 /* We reply directly. */
872 r = session_send_create_reply(session, NULL);
881 session_add_to_gc_queue(session);
884 user_add_to_gc_queue(user);
889 static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
890 Manager *m = userdata;
898 r = sd_bus_message_read(message, "s", &name);
902 r = manager_get_session_from_creds(m, message, name, error, &session);
906 r = session_release(session);
910 #if 1 /// elogind must queue this session
911 session_add_to_gc_queue(session);
913 return sd_bus_reply_method_return(message, NULL);
916 static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
917 Manager *m = userdata;
925 r = sd_bus_message_read(message, "s", &name);
929 r = manager_get_session_from_creds(m, message, name, error, &session);
933 return bus_session_method_activate(message, session, error);
936 static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
937 const char *session_name, *seat_name;
938 Manager *m = userdata;
946 /* Same as ActivateSession() but refuses to work if
947 * the seat doesn't match */
949 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
953 r = manager_get_session_from_creds(m, message, session_name, error, &session);
957 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
961 if (session->seat != seat)
962 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
964 r = session_activate(session);
968 return sd_bus_reply_method_return(message, NULL);
971 static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
972 Manager *m = userdata;
980 r = sd_bus_message_read(message, "s", &name);
984 r = manager_get_session_from_creds(m, message, name, error, &session);
988 return bus_session_method_lock(message, session, error);
991 static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
992 Manager *m = userdata;
998 r = bus_verify_polkit_async(
1001 "org.freedesktop.login1.lock-sessions",
1005 &m->polkit_registry,
1010 return 1; /* Will call us back */
1012 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
1016 return sd_bus_reply_method_return(message, NULL);
1019 static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1021 Manager *m = userdata;
1028 r = sd_bus_message_read(message, "s", &name);
1032 r = manager_get_session_from_creds(m, message, name, error, &session);
1036 return bus_session_method_kill(message, session, error);
1039 static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1040 Manager *m = userdata;
1048 r = sd_bus_message_read(message, "u", &uid);
1052 r = manager_get_user_from_creds(m, message, uid, error, &user);
1056 return bus_user_method_kill(message, user, error);
1059 static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1060 Manager *m = userdata;
1068 r = sd_bus_message_read(message, "s", &name);
1072 r = manager_get_session_from_creds(m, message, name, error, &session);
1076 return bus_session_method_terminate(message, session, error);
1079 static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1080 Manager *m = userdata;
1088 r = sd_bus_message_read(message, "u", &uid);
1092 r = manager_get_user_from_creds(m, message, uid, error, &user);
1096 return bus_user_method_terminate(message, user, error);
1099 static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1100 Manager *m = userdata;
1108 r = sd_bus_message_read(message, "s", &name);
1112 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1116 return bus_seat_method_terminate(message, seat, error);
1119 static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1120 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1121 _cleanup_free_ char *cc = NULL;
1122 Manager *m = userdata;
1123 int r, b, interactive;
1126 uint32_t uid, auth_uid;
1131 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1135 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID |
1136 SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1140 if (!uid_is_valid(uid)) {
1141 /* Note that we get the owner UID of the session or user unit,
1142 * not the actual client UID here! */
1143 r = sd_bus_creds_get_owner_uid(creds, &uid);
1148 /* owner_uid is racy, so for authorization we must use euid */
1149 r = sd_bus_creds_get_euid(creds, &auth_uid);
1156 return errno > 0 ? -errno : -ENOENT;
1158 r = bus_verify_polkit_async(
1161 uid == auth_uid ? "org.freedesktop.login1.set-self-linger" :
1162 "org.freedesktop.login1.set-user-linger",
1166 &m->polkit_registry,
1171 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1173 mkdir_p_label("/var/lib/elogind", 0755);
1175 r = mkdir_safe_label("/var/lib/elogind/linger", 0755, 0, 0, MKDIR_WARN_MODE);
1179 cc = cescape(pw->pw_name);
1183 path = strjoina("/var/lib/elogind/linger/", cc);
1191 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1198 if (r < 0 && errno != ENOENT)
1201 u = hashmap_get(m->users, UID_TO_PTR(uid));
1203 user_add_to_gc_queue(u);
1206 return sd_bus_reply_method_return(message, NULL);
1209 static int trigger_device(Manager *m, struct udev_device *d) {
1210 _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
1211 struct udev_list_entry *first, *item;
1216 e = udev_enumerate_new(m->udev);
1221 r = udev_enumerate_add_match_parent(e, d);
1226 r = udev_enumerate_scan_devices(e);
1230 first = udev_enumerate_get_list_entry(e);
1231 udev_list_entry_foreach(item, first) {
1232 _cleanup_free_ char *t = NULL;
1235 p = udev_list_entry_get_name(item);
1237 t = strappend(p, "/uevent");
1241 (void) write_string_file(t, "change", 0);
1247 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1248 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
1249 _cleanup_free_ char *rule = NULL, *file = NULL;
1250 const char *id_for_seat;
1257 d = udev_device_new_from_syspath(m->udev, sysfs);
1261 if (!udev_device_has_tag(d, "seat"))
1264 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
1268 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1271 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1274 mkdir_p_label("/etc/udev/rules.d", 0755);
1275 r = write_string_file_atomic_label(file, rule);
1279 return trigger_device(m, d);
1282 static int flush_devices(Manager *m) {
1283 _cleanup_closedir_ DIR *d;
1287 d = opendir("/etc/udev/rules.d");
1289 if (errno != ENOENT)
1290 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1294 FOREACH_DIRENT_ALL(de, d, break) {
1295 if (!dirent_is_file(de))
1298 if (!startswith(de->d_name, "72-seat-"))
1301 if (!endswith(de->d_name, ".rules"))
1304 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1305 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1309 return trigger_device(m, NULL);
1312 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1313 const char *sysfs, *seat;
1314 Manager *m = userdata;
1320 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1324 if (!path_startswith(sysfs, "/sys"))
1325 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1327 if (!seat_name_is_valid(seat))
1328 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1330 r = bus_verify_polkit_async(
1333 "org.freedesktop.login1.attach-device",
1337 &m->polkit_registry,
1342 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1344 r = attach_device(m, seat, sysfs);
1348 return sd_bus_reply_method_return(message, NULL);
1351 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1352 Manager *m = userdata;
1358 r = sd_bus_message_read(message, "b", &interactive);
1362 r = bus_verify_polkit_async(
1365 "org.freedesktop.login1.flush-devices",
1369 &m->polkit_registry,
1374 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1376 r = flush_devices(m);
1380 return sd_bus_reply_method_return(message, NULL);
1383 static int have_multiple_sessions(
1392 /* Check for other users' sessions. Greeter sessions do not
1393 * count, and non-login sessions do not count either. */
1394 HASHMAP_FOREACH(session, m->sessions, i)
1395 if (session->class == SESSION_USER &&
1396 session->user->uid != uid)
1402 #if 0 /// elogind has its own variant in elogind-dbus.c
1403 static int bus_manager_log_shutdown(
1405 const char *unit_name) {
1412 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1413 p = "MESSAGE=System is powering down";
1414 q = "SHUTDOWN=power-off";
1415 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1416 p = "MESSAGE=System is rebooting";
1417 q = "SHUTDOWN=reboot";
1418 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1419 p = "MESSAGE=System is halting";
1420 q = "SHUTDOWN=halt";
1421 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1422 p = "MESSAGE=System is rebooting with kexec";
1423 q = "SHUTDOWN=kexec";
1425 p = "MESSAGE=System is shutting down";
1429 if (isempty(m->wall_message))
1430 p = strjoina(p, ".");
1432 p = strjoina(p, " (", m->wall_message, ").");
1434 return log_struct(LOG_NOTICE,
1435 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
1441 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1442 Manager *m = userdata;
1447 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1451 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1456 if (until <= now(CLOCK_MONOTONIC))
1459 /* We want to ignore the lid switch for a while after each
1460 * suspend, and after boot-up. Hence let's install a timer for
1461 * this. As long as the event source exists we ignore the lid
1464 if (m->lid_switch_ignore_event_source) {
1467 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1474 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1476 r = sd_event_add_time(
1478 &m->lid_switch_ignore_event_source,
1481 lid_switch_ignore_handler, m);
1486 #if 0 /// elogind-dbus.c needs to access this
1487 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1489 int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1492 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1493 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1494 [INHIBIT_SLEEP] = "PrepareForSleep"
1497 int active = _active;
1501 assert(w < _INHIBIT_WHAT_MAX);
1502 assert(signal_name[w]);
1504 return sd_bus_emit_signal(m->bus,
1505 "/org/freedesktop/login1",
1506 "org.freedesktop.login1.Manager",
1512 #if 0 /// elogind has its own variant in elogind-dbus.c
1513 static int execute_shutdown_or_sleep(
1516 const char *unit_name,
1517 sd_bus_error *error) {
1519 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1526 assert(w < _INHIBIT_WHAT_MAX);
1529 if (w == INHIBIT_SHUTDOWN)
1530 bus_manager_log_shutdown(m, unit_name);
1532 r = sd_bus_call_method(
1534 "org.freedesktop.systemd1",
1535 "/org/freedesktop/systemd1",
1536 "org.freedesktop.systemd1.Manager",
1540 "ss", unit_name, "replace-irreversibly");
1544 r = sd_bus_message_read(reply, "o", &p);
1554 m->action_unit = unit_name;
1555 free(m->action_job);
1559 /* Make sure the lid switch is ignored for a while */
1560 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1565 /* Tell people that they now may take a lock again */
1566 (void) send_prepare_for(m, w, false);
1572 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1574 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1575 Inhibitor *offending = NULL;
1580 #if 0 /// elogind has no action_job, but a pending_action
1581 if (manager->action_what == 0 || manager->action_job)
1583 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
1587 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1588 _cleanup_free_ char *comm = NULL, *u = NULL;
1593 (void) get_process_comm(offending->pid, &comm);
1594 u = uid_to_name(offending->uid);
1596 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1597 offending->uid, strna(u),
1598 offending->pid, strna(comm));
1601 /* Actually do the operation */
1602 #if 0 /// elogind has no action_unit but a pending_action
1603 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1605 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
1608 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1609 bus_error_message(&error, r));
1612 #if 0 /// elogind has no action_unit but a pending_action
1613 manager->action_unit = NULL;
1614 manager->action_what = 0;
1617 manager->pending_action = HANDLE_IGNORE;
1618 manager->action_what = 0;
1619 /* It is not a critical error for elogind if suspending fails */
1626 static int manager_inhibit_timeout_handler(
1631 Manager *manager = userdata;
1635 assert(manager->inhibit_timeout_source == s);
1637 r = manager_dispatch_delayed(manager, true);
1638 return (r < 0) ? r : 0;
1641 #if 0 /// elogind does not have unit_name but action
1642 static int delay_shutdown_or_sleep(
1645 const char *unit_name) {
1648 int delay_shutdown_or_sleep(
1651 HandleAction action) {
1658 assert(w < _INHIBIT_WHAT_MAX);
1659 #if 0 /// UNNEEDED by elogind
1663 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1665 if (m->inhibit_timeout_source) {
1666 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1668 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1670 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1672 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1674 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1675 timeout_val, 0, manager_inhibit_timeout_handler, m);
1680 #if 0 /// elogind does not have unit_name but pendig_action
1681 m->action_unit = unit_name;
1683 m->pending_action = action;
1690 int bus_manager_shutdown_or_sleep_now_or_later(
1692 #if 0 /// elogind has HandleAction instead of const char* unit_name
1693 const char *unit_name,
1695 _cleanup_free_ char *load_state = NULL;
1697 HandleAction unit_name,
1700 sd_bus_error *error) {
1705 #if 0 /// for elogind only w has to be checked.
1708 assert(w <= _INHIBIT_WHAT_MAX);
1709 assert(!m->action_job);
1711 r = unit_load_state(m->bus, unit_name, &load_state);
1715 if (!streq(load_state, "loaded")) {
1716 log_notice("Unit %s is %s, refusing operation.", unit_name, load_state);
1721 assert(w <= _INHIBIT_WHAT_MAX);
1724 /* Tell everybody to prepare for shutdown/sleep */
1725 (void) send_prepare_for(m, w, true);
1728 m->inhibit_delay_max > 0 &&
1729 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1731 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
1732 handle_action_to_string(unit_name),
1733 delayed ? "" : "NOT ");
1735 /* Shutdown is delayed, keep in mind what we
1736 * want to do, and start a timeout */
1737 r = delay_shutdown_or_sleep(m, w, unit_name);
1739 /* Shutdown is not delayed, execute it
1741 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1746 static int verify_shutdown_creds(
1748 sd_bus_message *message,
1752 const char *action_multiple_sessions,
1753 const char *action_ignore_inhibit,
1754 sd_bus_error *error) {
1756 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1757 bool multiple_sessions, blocked;
1764 assert(w <= _INHIBIT_WHAT_MAX);
1766 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1770 r = sd_bus_creds_get_euid(creds, &uid);
1774 r = have_multiple_sessions(m, uid);
1778 multiple_sessions = r > 0;
1779 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1781 if (multiple_sessions && action_multiple_sessions) {
1782 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1786 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1789 if (blocked && action_ignore_inhibit) {
1790 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1794 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1797 if (!multiple_sessions && !blocked && action) {
1798 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1802 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1808 static int method_do_shutdown_or_sleep(
1810 sd_bus_message *message,
1811 #if 0 /// elogind has HandleAction instead of const char* unit_name
1812 const char *unit_name,
1814 HandleAction unit_name,
1818 const char *action_multiple_sessions,
1819 const char *action_ignore_inhibit,
1820 const char *sleep_verb,
1821 sd_bus_error *error) {
1827 #if 0 /// elogind does not need this to be checked
1831 assert(w <= _INHIBIT_WHAT_MAX);
1833 r = sd_bus_message_read(message, "b", &interactive);
1837 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
1838 __FUNCTION__, action, sleep_verb,
1839 interactive ? "" : "NOT ");
1840 /* Don't allow multiple jobs being executed at the same time */
1842 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1845 #if 0 /// Within elogind the manager m must be provided, too
1846 r = can_sleep(sleep_verb);
1848 return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1849 "Not enough swap space for hibernation");
1851 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED,
1852 "Sleep verb \"%s\" not supported", sleep_verb);
1854 r = can_sleep(m, sleep_verb);
1860 #if 0 /// Within elogind it does not make sense to verify shutdown creds when suspending
1861 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1862 action_ignore_inhibit, error);
1866 if (IN_SET(unit_name, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
1867 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1868 action_ignore_inhibit, error);
1869 log_debug_elogind("verify_shutdown_creds() returned %d", r);
1875 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1879 return sd_bus_reply_method_return(message, NULL);
1882 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1883 Manager *m = userdata;
1885 log_debug_elogind("%s called", __FUNCTION__);
1886 return method_do_shutdown_or_sleep(
1888 #if 0 /// elogind uses HandleAction instead of const char* unti names
1889 SPECIAL_POWEROFF_TARGET,
1894 "org.freedesktop.login1.power-off",
1895 "org.freedesktop.login1.power-off-multiple-sessions",
1896 "org.freedesktop.login1.power-off-ignore-inhibit",
1901 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1902 Manager *m = userdata;
1904 log_debug_elogind("%s called", __FUNCTION__);
1905 return method_do_shutdown_or_sleep(
1907 #if 0 /// elogind uses HandleAction instead of const char* unti names
1908 SPECIAL_REBOOT_TARGET,
1913 "org.freedesktop.login1.reboot",
1914 "org.freedesktop.login1.reboot-multiple-sessions",
1915 "org.freedesktop.login1.reboot-ignore-inhibit",
1920 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1921 Manager *m = userdata;
1923 log_debug_elogind("%s called", __FUNCTION__);
1924 return method_do_shutdown_or_sleep(
1926 #if 0 /// elogind uses HandleAction instead of const char* unti names
1927 SPECIAL_HALT_TARGET,
1932 "org.freedesktop.login1.halt",
1933 "org.freedesktop.login1.halt-multiple-sessions",
1934 "org.freedesktop.login1.halt-ignore-inhibit",
1939 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1940 Manager *m = userdata;
1942 log_debug_elogind("%s called", __FUNCTION__);
1943 return method_do_shutdown_or_sleep(
1945 #if 0 /// elogind uses HandleAction instead of const char* unti names
1946 SPECIAL_SUSPEND_TARGET,
1951 "org.freedesktop.login1.suspend",
1952 "org.freedesktop.login1.suspend-multiple-sessions",
1953 "org.freedesktop.login1.suspend-ignore-inhibit",
1958 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1959 Manager *m = userdata;
1961 log_debug_elogind("%s called", __FUNCTION__);
1962 return method_do_shutdown_or_sleep(
1964 #if 0 /// elogind uses HandleAction instead of const char* unti names
1965 SPECIAL_HIBERNATE_TARGET,
1970 "org.freedesktop.login1.hibernate",
1971 "org.freedesktop.login1.hibernate-multiple-sessions",
1972 "org.freedesktop.login1.hibernate-ignore-inhibit",
1977 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1978 Manager *m = userdata;
1980 log_debug_elogind("%s called", __FUNCTION__);
1981 return method_do_shutdown_or_sleep(
1983 #if 0 /// elogind uses HandleAction instead of const char* unti names
1984 SPECIAL_HYBRID_SLEEP_TARGET,
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 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
2000 HANDLE_HYBRID_SLEEP,
2003 "org.freedesktop.login1.hibernate",
2004 "org.freedesktop.login1.hibernate-multiple-sessions",
2005 "org.freedesktop.login1.hibernate-ignore-inhibit",
2010 static int nologin_timeout_handler(
2015 Manager *m = userdata;
2017 log_info("Creating /run/nologin, blocking further logins...");
2020 create_shutdown_run_nologin_or_warn() >= 0;
2025 static int update_schedule_file(Manager *m) {
2026 _cleanup_free_ char *temp_path = NULL;
2027 _cleanup_fclose_ FILE *f = NULL;
2032 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
2034 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
2036 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
2038 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
2040 (void) fchmod(fileno(f), 0644);
2046 m->scheduled_shutdown_timeout,
2047 m->enable_wall_messages,
2048 m->scheduled_shutdown_type);
2050 if (!isempty(m->wall_message)) {
2051 _cleanup_free_ char *t;
2053 t = cescape(m->wall_message);
2059 fprintf(f, "WALL_MESSAGE=%s\n", t);
2062 r = fflush_and_check(f);
2066 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
2074 (void) unlink(temp_path);
2075 (void) unlink("/run/systemd/shutdown/scheduled");
2077 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
2080 #if 0 /// elogind must access this from elogind-dbus.c
2081 static void reset_scheduled_shutdown(Manager *m) {
2083 void reset_scheduled_shutdown(Manager *m) {
2087 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2088 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
2089 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
2091 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
2092 m->scheduled_shutdown_timeout = 0;
2093 m->shutdown_dry_run = false;
2095 if (m->unlink_nologin) {
2096 (void) unlink_or_warn("/run/nologin");
2097 m->unlink_nologin = false;
2100 (void) unlink("/run/systemd/shutdown/scheduled");
2103 #if 0 /// elogind has its own variant in elogind-dbus.c
2104 static int manager_scheduled_shutdown_handler(
2109 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2110 Manager *m = userdata;
2116 if (isempty(m->scheduled_shutdown_type))
2119 if (streq(m->scheduled_shutdown_type, "poweroff"))
2120 target = SPECIAL_POWEROFF_TARGET;
2121 else if (streq(m->scheduled_shutdown_type, "reboot"))
2122 target = SPECIAL_REBOOT_TARGET;
2123 else if (streq(m->scheduled_shutdown_type, "halt"))
2124 target = SPECIAL_HALT_TARGET;
2126 assert_not_reached("unexpected shutdown type");
2128 /* Don't allow multiple jobs being executed at the same time */
2129 if (m->action_what) {
2131 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
2135 if (m->shutdown_dry_run) {
2136 /* We do not process delay inhibitors here. Otherwise, we
2137 * would have to be considered "in progress" (like the check
2138 * above) for some seconds after our admin has seen the final
2141 bus_manager_log_shutdown(m, target);
2142 log_info("Running in dry run, suppressing action.");
2143 reset_scheduled_shutdown(m);
2148 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
2150 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2157 reset_scheduled_shutdown(m);
2162 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2163 Manager *m = userdata;
2164 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2165 const char *action_multiple_sessions = NULL;
2166 const char *action_ignore_inhibit = NULL;
2167 const char *action = NULL;
2171 bool dry_run = false;
2176 log_debug_elogind("%s called", __FUNCTION__);
2177 r = sd_bus_message_read(message, "st", &type, &elapse);
2181 if (startswith(type, "dry-")) {
2186 if (streq(type, "poweroff")) {
2187 action = "org.freedesktop.login1.power-off";
2188 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2189 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2190 } else if (streq(type, "reboot")) {
2191 action = "org.freedesktop.login1.reboot";
2192 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2193 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2194 } else if (streq(type, "halt")) {
2195 action = "org.freedesktop.login1.halt";
2196 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2197 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2199 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2201 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2202 action, action_multiple_sessions, action_ignore_inhibit, error);
2206 if (m->scheduled_shutdown_timeout_source) {
2207 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2209 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2211 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2213 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2215 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2216 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2218 return log_error_errno(r, "sd_event_add_time() failed: %m");
2221 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2223 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2227 m->shutdown_dry_run = dry_run;
2229 if (m->nologin_timeout_source) {
2230 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2232 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2234 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2236 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2238 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2239 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2241 return log_error_errno(r, "sd_event_add_time() failed: %m");
2244 m->scheduled_shutdown_timeout = elapse;
2246 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2248 const char *tty = NULL;
2250 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2251 (void) sd_bus_creds_get_tty(creds, &tty);
2253 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2255 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2260 r = manager_setup_wall_message_timer(m);
2264 r = update_schedule_file(m);
2268 return sd_bus_reply_method_return(message, NULL);
2271 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2272 Manager *m = userdata;
2274 #if 1 /// elogind needs to construct the message to allow extra wall messages
2275 _cleanup_free_ char *l = NULL;
2281 log_debug_elogind("%s called", __FUNCTION__);
2282 cancelled = m->scheduled_shutdown_type != NULL;
2283 reset_scheduled_shutdown(m);
2285 if (cancelled && m->enable_wall_messages) {
2286 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2287 const char *tty = NULL;
2291 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2293 (void) sd_bus_creds_get_uid(creds, &uid);
2294 (void) sd_bus_creds_get_tty(creds, &tty);
2297 #if 0 /// elogind wants to allow extra cancellation messages
2298 utmp_wall("The system shutdown has been cancelled",
2299 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2301 r = asprintf(&l, "%s%sThe system shutdown has been cancelled!",
2302 strempty(m->wall_message),
2303 isempty(m->wall_message) ? "" : "\n");
2309 utmp_wall(l, uid_to_name(uid), tty, logind_wall_tty_filter, m);
2313 return sd_bus_reply_method_return(message, "b", cancelled);
2316 static int method_can_shutdown_or_sleep(
2318 sd_bus_message *message,
2321 const char *action_multiple_sessions,
2322 const char *action_ignore_inhibit,
2323 const char *sleep_verb,
2324 sd_bus_error *error) {
2326 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2327 HandleAction handle;
2328 bool multiple_sessions, challenge, blocked;
2329 const char *result = NULL;
2336 assert(w <= _INHIBIT_WHAT_MAX);
2338 assert(action_multiple_sessions);
2339 assert(action_ignore_inhibit);
2342 #if 0 /// elogind needs to have the manager being passed
2343 r = can_sleep(sleep_verb);
2344 if (IN_SET(r, 0, -ENOSPC))
2345 return sd_bus_reply_method_return(message, "s", "na");
2347 r = can_sleep(m, sleep_verb);
2353 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2357 r = sd_bus_creds_get_euid(creds, &uid);
2361 r = have_multiple_sessions(m, uid);
2365 multiple_sessions = r > 0;
2366 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2368 handle = handle_action_from_string(sleep_verb);
2372 target = manager_target_for_action(handle);
2374 _cleanup_free_ char *load_state = NULL;
2376 r = unit_load_state(m->bus, target, &load_state);
2380 if (!streq(load_state, "loaded")) {
2387 if (multiple_sessions) {
2388 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2395 result = "challenge";
2401 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2405 if (r > 0 && !result)
2407 else if (challenge && (!result || streq(result, "yes")))
2408 result = "challenge";
2413 if (!multiple_sessions && !blocked) {
2414 /* If neither inhibit nor multiple sessions
2415 * apply then just check the normal policy */
2417 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2424 result = "challenge";
2430 return sd_bus_reply_method_return(message, "s", result);
2433 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2434 Manager *m = userdata;
2436 return method_can_shutdown_or_sleep(
2439 "org.freedesktop.login1.power-off",
2440 "org.freedesktop.login1.power-off-multiple-sessions",
2441 "org.freedesktop.login1.power-off-ignore-inhibit",
2446 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2447 Manager *m = userdata;
2449 return method_can_shutdown_or_sleep(
2452 "org.freedesktop.login1.reboot",
2453 "org.freedesktop.login1.reboot-multiple-sessions",
2454 "org.freedesktop.login1.reboot-ignore-inhibit",
2459 static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2460 Manager *m = userdata;
2462 return method_can_shutdown_or_sleep(
2465 "org.freedesktop.login1.halt",
2466 "org.freedesktop.login1.halt-multiple-sessions",
2467 "org.freedesktop.login1.halt-ignore-inhibit",
2472 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2473 Manager *m = userdata;
2475 return method_can_shutdown_or_sleep(
2478 "org.freedesktop.login1.suspend",
2479 "org.freedesktop.login1.suspend-multiple-sessions",
2480 "org.freedesktop.login1.suspend-ignore-inhibit",
2485 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2486 Manager *m = userdata;
2488 return method_can_shutdown_or_sleep(
2491 "org.freedesktop.login1.hibernate",
2492 "org.freedesktop.login1.hibernate-multiple-sessions",
2493 "org.freedesktop.login1.hibernate-ignore-inhibit",
2498 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2499 Manager *m = userdata;
2501 return method_can_shutdown_or_sleep(
2504 "org.freedesktop.login1.hibernate",
2505 "org.freedesktop.login1.hibernate-multiple-sessions",
2506 "org.freedesktop.login1.hibernate-ignore-inhibit",
2511 static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2512 Manager *m = userdata;
2514 return method_can_shutdown_or_sleep(
2517 "org.freedesktop.login1.hibernate",
2518 "org.freedesktop.login1.hibernate-multiple-sessions",
2519 "org.freedesktop.login1.hibernate-ignore-inhibit",
2520 "suspend-then-hibernate",
2524 static int property_get_reboot_to_firmware_setup(
2527 const char *interface,
2528 const char *property,
2529 sd_bus_message *reply,
2531 sd_bus_error *error) {
2532 #if 0 /// elogind does not support EFI
2539 r = efi_get_reboot_to_firmware();
2540 if (r < 0 && r != -EOPNOTSUPP)
2541 log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m");
2543 return sd_bus_message_append(reply, "b", r > 0);
2545 return sd_bus_message_append(reply, "b", false);
2549 static int method_set_reboot_to_firmware_setup(
2550 sd_bus_message *message,
2552 sd_bus_error *error) {
2555 Manager *m = userdata;
2560 r = sd_bus_message_read(message, "b", &b);
2564 r = bus_verify_polkit_async(message,
2566 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2570 &m->polkit_registry,
2575 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2577 #if 0 /// elogind does not support EFI
2578 r = efi_set_reboot_to_firmware(b);
2583 return sd_bus_reply_method_return(message, NULL);
2586 static int method_can_reboot_to_firmware_setup(
2587 sd_bus_message *message,
2589 sd_bus_error *error) {
2591 #if 0 /// elogind does not support EFI
2595 Manager *m = userdata;
2600 r = efi_reboot_to_firmware_supported();
2602 if (r != -EOPNOTSUPP)
2603 log_warning_errno(errno, "Failed to determine whether reboot to firmware is supported: %m");
2605 return sd_bus_reply_method_return(message, "s", "na");
2608 r = bus_test_polkit(message,
2610 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2621 result = "challenge";
2625 return sd_bus_reply_method_return(message, "s", result);
2627 return sd_bus_reply_method_return(message, "s", "no");
2631 static int method_set_wall_message(
2632 sd_bus_message *message,
2634 sd_bus_error *error) {
2637 Manager *m = userdata;
2639 unsigned enable_wall_messages;
2644 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2648 #if 0 /// elogind only calls this for shutdown/reboot, which already needs authorization.
2649 r = bus_verify_polkit_async(message,
2651 "org.freedesktop.login1.set-wall-message",
2655 &m->polkit_registry,
2660 return 1; /* Will call us back */
2663 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
2667 m->enable_wall_messages = enable_wall_messages;
2669 return sd_bus_reply_method_return(message, NULL);
2672 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2673 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2674 const char *who, *why, *what, *mode;
2675 _cleanup_free_ char *id = NULL;
2676 _cleanup_close_ int fifo_fd = -1;
2677 Manager *m = userdata;
2678 Inhibitor *i = NULL;
2688 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2692 w = inhibit_what_from_string(what);
2694 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2696 mm = inhibit_mode_from_string(mode);
2698 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2700 /* Delay is only supported for shutdown/sleep */
2701 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2702 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2704 /* Don't allow taking delay locks while we are already
2705 * executing the operation. We shouldn't create the impression
2706 * that the lock was successful if the machine is about to go
2707 * down/suspend any moment. */
2708 if (m->action_what & w)
2709 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2711 r = bus_verify_polkit_async(
2714 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2715 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2716 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2717 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2718 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2719 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2720 "org.freedesktop.login1.inhibit-handle-lid-switch",
2724 &m->polkit_registry,
2729 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2731 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2735 r = sd_bus_creds_get_euid(creds, &uid);
2739 r = sd_bus_creds_get_pid(creds, &pid);
2743 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2744 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2749 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2752 } while (hashmap_get(m->inhibitors, id));
2754 r = manager_add_inhibitor(m, id, &i);
2762 i->why = strdup(why);
2763 i->who = strdup(who);
2765 if (!i->why || !i->who) {
2770 fifo_fd = inhibitor_create_fifo(i);
2778 return sd_bus_reply_method_return(message, "h", fifo_fd);
2787 const sd_bus_vtable manager_vtable[] = {
2788 SD_BUS_VTABLE_START(0),
2790 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2791 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2793 #if 0 /// UNNEEDED by elogind
2794 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2796 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2797 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2798 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2799 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2800 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2801 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2802 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2803 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2804 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2805 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2806 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2807 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2808 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2809 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2810 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
2811 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2812 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2813 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2814 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2815 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2816 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2817 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2818 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2819 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2820 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2821 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2822 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0),
2823 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2824 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0),
2825 SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
2827 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2828 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2829 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2830 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2831 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2832 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2833 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2834 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2835 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2836 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2837 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2838 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2839 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2840 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2841 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2842 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2843 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2844 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2845 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2846 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2847 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2848 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2849 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2850 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2851 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2852 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2853 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2854 SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2855 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2856 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2857 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2858 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2859 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2860 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2861 SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2862 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2863 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2864 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2865 SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2866 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2867 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2868 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2869 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2870 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2871 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2873 SD_BUS_SIGNAL("SessionNew", "so", 0),
2874 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2875 SD_BUS_SIGNAL("UserNew", "uo", 0),
2876 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2877 SD_BUS_SIGNAL("SeatNew", "so", 0),
2878 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2879 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2880 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2885 #if 0 /// UNNEEDED by elogind
2886 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2895 if (streq(result, "done"))
2896 r = session_send_create_reply(s, NULL);
2898 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2900 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2901 r = session_send_create_reply(s, &e);
2907 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2908 const char *path, *result, *unit;
2909 Manager *m = userdata;
2918 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2920 bus_log_parse_error(r);
2924 if (m->action_job && streq(m->action_job, path)) {
2925 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2927 /* Tell people that they now may take a lock again */
2928 (void) send_prepare_for(m, m->action_what, false);
2930 m->action_job = mfree(m->action_job);
2931 m->action_unit = NULL;
2936 session = hashmap_get(m->session_units, unit);
2937 if (session && streq_ptr(path, session->scope_job)) {
2938 session->scope_job = mfree(session->scope_job);
2939 session_jobs_reply(session, unit, result);
2941 session_save(session);
2942 user_save(session->user);
2943 session_add_to_gc_queue(session);
2946 user = hashmap_get(m->user_units, unit);
2948 (streq_ptr(path, user->service_job) ||
2949 streq_ptr(path, user->slice_job))) {
2951 if (streq_ptr(path, user->service_job))
2952 user->service_job = mfree(user->service_job);
2954 if (streq_ptr(path, user->slice_job))
2955 user->slice_job = mfree(user->slice_job);
2957 LIST_FOREACH(sessions_by_user, session, user->sessions)
2958 session_jobs_reply(session, unit, result);
2961 user_add_to_gc_queue(user);
2967 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2968 const char *path, *unit;
2969 Manager *m = userdata;
2977 r = sd_bus_message_read(message, "so", &unit, &path);
2979 bus_log_parse_error(r);
2983 session = hashmap_get(m->session_units, unit);
2985 session_add_to_gc_queue(session);
2987 user = hashmap_get(m->user_units, unit);
2989 user_add_to_gc_queue(user);
2994 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2995 _cleanup_free_ char *unit = NULL;
2996 Manager *m = userdata;
3005 path = sd_bus_message_get_path(message);
3009 r = unit_name_from_dbus_path(path, &unit);
3010 if (r == -EINVAL) /* not a unit */
3017 session = hashmap_get(m->session_units, unit);
3019 session_add_to_gc_queue(session);
3021 user = hashmap_get(m->user_units, unit);
3023 user_add_to_gc_queue(user);
3028 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3029 Manager *m = userdata;
3037 r = sd_bus_message_read(message, "b", &b);
3039 bus_log_parse_error(r);
3046 /* systemd finished reloading, let's recheck all our sessions */
3047 log_debug("System manager has been reloaded, rechecking sessions...");
3049 HASHMAP_FOREACH(session, m->sessions, i)
3050 session_add_to_gc_queue(session);
3056 int manager_send_changed(Manager *manager, const char *property, ...) {
3061 l = strv_from_stdarg_alloca(property);
3063 return sd_bus_emit_properties_changed_strv(
3065 "/org/freedesktop/login1",
3066 "org.freedesktop.login1.Manager",
3070 #if 0 /// UNNEEDED by elogind
3071 static int strdup_job(sd_bus_message *reply, char **job) {
3076 r = sd_bus_message_read(reply, "o", &j);
3088 int manager_start_scope(
3093 const char *description,
3096 sd_bus_message *more_properties,
3097 sd_bus_error *error,
3100 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
3108 r = sd_bus_message_new_method_call(
3111 "org.freedesktop.systemd1",
3112 "/org/freedesktop/systemd1",
3113 "org.freedesktop.systemd1.Manager",
3114 "StartTransientUnit");
3118 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
3122 r = sd_bus_message_open_container(m, 'a', "(sv)");
3126 if (!isempty(slice)) {
3127 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
3132 if (!isempty(description)) {
3133 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3138 if (!isempty(after)) {
3139 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3144 if (!isempty(after2)) {
3145 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3150 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3152 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
3156 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3160 /* disable TasksMax= for the session scope, rely on the slice setting for it */
3161 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1);
3163 return bus_log_create_error(r);
3165 if (more_properties) {
3166 /* If TasksMax also appears here, it will overwrite the default value set above */
3167 r = sd_bus_message_copy(m, more_properties, true);
3172 r = sd_bus_message_close_container(m);
3176 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3180 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3184 return strdup_job(reply, job);
3187 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3188 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3195 r = sd_bus_call_method(
3197 "org.freedesktop.systemd1",
3198 "/org/freedesktop/systemd1",
3199 "org.freedesktop.systemd1.Manager",
3203 "ss", unit, "replace");
3207 return strdup_job(reply, job);
3210 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3211 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3218 r = sd_bus_call_method(
3220 "org.freedesktop.systemd1",
3221 "/org/freedesktop/systemd1",
3222 "org.freedesktop.systemd1.Manager",
3226 "ss", unit, "fail");
3228 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3229 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3232 sd_bus_error_free(error);
3239 return strdup_job(reply, job);
3242 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3243 _cleanup_free_ char *path = NULL;
3249 path = unit_dbus_path_from_name(scope);
3253 r = sd_bus_call_method(
3255 "org.freedesktop.systemd1",
3257 "org.freedesktop.systemd1.Scope",
3263 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3264 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3265 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3266 sd_bus_error_free(error);
3276 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3280 return sd_bus_call_method(
3282 "org.freedesktop.systemd1",
3283 "/org/freedesktop/systemd1",
3284 "org.freedesktop.systemd1.Manager",
3288 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3291 int manager_unit_is_active(Manager *manager, const char *unit) {
3292 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3293 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3294 _cleanup_free_ char *path = NULL;
3301 path = unit_dbus_path_from_name(unit);
3305 r = sd_bus_get_property(
3307 "org.freedesktop.systemd1",
3309 "org.freedesktop.systemd1.Unit",
3315 /* systemd might have droppped off momentarily, let's
3316 * not make this an error */
3317 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3318 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3321 /* If the unit is already unloaded then it's not
3323 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3324 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3330 r = sd_bus_message_read(reply, "s", &state);
3334 return !streq(state, "inactive") && !streq(state, "failed");
3337 int manager_job_is_active(Manager *manager, const char *path) {
3338 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3339 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3345 r = sd_bus_get_property(
3347 "org.freedesktop.systemd1",
3349 "org.freedesktop.systemd1.Job",
3355 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3356 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3359 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3365 /* We don't actually care about the state really. The fact
3366 * that we could read the job state is enough for us */