1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
13 #include "sd-messages.h"
15 #include "alloc-util.h"
16 #include "audit-util.h"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
20 #include "dirent-util.h"
21 //#include "efivars.h"
24 #include "fileio-label.h"
25 #include "format-util.h"
29 #include "path-util.h"
30 #include "process-util.h"
31 //#include "cgroup-util.h"
32 #include "selinux-util.h"
33 #include "sleep-config.h"
34 //#include "special.h"
36 #include "terminal-util.h"
37 #include "udev-util.h"
38 #include "unit-name.h"
39 #include "user-util.h"
40 #include "utmp-wtmp.h"
42 /// Additional includes needed by elogind
43 #include "elogind-dbus.h"
45 static int get_sender_session(Manager *m, sd_bus_message *message, sd_bus_error *error, Session **ret) {
47 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
52 /* Get client login session. This is not what you are looking for these days,
53 * as apps may instead belong to a user service unit. This includes terminal
54 * emulators and hence command-line apps. */
55 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
59 r = sd_bus_creds_get_session(creds, &name);
65 session = hashmap_get(m->sessions, name);
73 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID,
74 "Caller does not belong to any known session");
77 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
85 return get_sender_session(m, message, error, ret);
87 session = hashmap_get(m->sessions, name);
89 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
95 static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) {
97 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
102 /* Note that we get the owner UID of the session, not the actual client UID here! */
103 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
107 r = sd_bus_creds_get_owner_uid(creds, &uid);
113 user = hashmap_get(m->users, UID_TO_PTR(uid));
121 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "Caller does not belong to any logged in user or lingering user");
124 int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
131 if (!uid_is_valid(uid))
132 return get_sender_user(m, message, error, ret);
134 user = hashmap_get(m->users, UID_TO_PTR(uid));
136 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "User ID "UID_FMT" is not logged in or lingering", uid);
142 int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) {
153 r = manager_get_session_from_creds(m, message, NULL, error, &session);
157 seat = session->seat;
159 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat.");
161 seat = hashmap_get(m->seats, name);
163 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
170 static int property_get_idle_hint(
173 const char *interface,
174 const char *property,
175 sd_bus_message *reply,
177 sd_bus_error *error) {
179 Manager *m = userdata;
185 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
188 static int property_get_idle_since_hint(
191 const char *interface,
192 const char *property,
193 sd_bus_message *reply,
195 sd_bus_error *error) {
197 Manager *m = userdata;
198 dual_timestamp t = DUAL_TIMESTAMP_NULL;
204 manager_get_idle_hint(m, &t);
206 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
209 static int property_get_inhibited(
212 const char *interface,
213 const char *property,
214 sd_bus_message *reply,
216 sd_bus_error *error) {
218 Manager *m = userdata;
225 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
227 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
230 static int property_get_preparing(
233 const char *interface,
234 const char *property,
235 sd_bus_message *reply,
237 sd_bus_error *error) {
239 Manager *m = userdata;
246 if (streq(property, "PreparingForShutdown"))
247 b = !!(m->action_what & INHIBIT_SHUTDOWN);
249 b = !!(m->action_what & INHIBIT_SLEEP);
251 return sd_bus_message_append(reply, "b", b);
254 static int property_get_scheduled_shutdown(
257 const char *interface,
258 const char *property,
259 sd_bus_message *reply,
261 sd_bus_error *error) {
263 Manager *m = userdata;
270 r = sd_bus_message_open_container(reply, 'r', "st");
274 r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
278 return sd_bus_message_close_container(reply);
281 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
283 static int property_get_docked(
286 const char *interface,
287 const char *property,
288 sd_bus_message *reply,
290 sd_bus_error *error) {
292 Manager *m = userdata;
298 return sd_bus_message_append(reply, "b", manager_is_docked_or_external_displays(m));
301 static int property_get_current_sessions(
304 const char *interface,
305 const char *property,
306 sd_bus_message *reply,
308 sd_bus_error *error) {
310 Manager *m = userdata;
316 return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->sessions));
319 static int property_get_current_inhibitors(
322 const char *interface,
323 const char *property,
324 sd_bus_message *reply,
326 sd_bus_error *error) {
328 Manager *m = userdata;
334 return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->inhibitors));
337 static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
338 _cleanup_free_ char *p = NULL;
339 Manager *m = userdata;
347 r = sd_bus_message_read(message, "s", &name);
351 r = manager_get_session_from_creds(m, message, name, error, &session);
355 p = session_bus_path(session);
359 return sd_bus_reply_method_return(message, "o", p);
362 /* Get login session of a process. This is not what you are looking for these days,
363 * as apps may instead belong to a user service unit. This includes terminal
364 * emulators and hence command-line apps. */
365 static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
366 _cleanup_free_ char *p = NULL;
367 Session *session = NULL;
368 Manager *m = userdata;
375 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
377 r = sd_bus_message_read(message, "u", &pid);
384 r = manager_get_session_from_creds(m, message, NULL, error, &session);
388 r = manager_get_session_by_pid(m, pid, &session);
393 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
396 p = session_bus_path(session);
400 return sd_bus_reply_method_return(message, "o", p);
403 static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
404 _cleanup_free_ char *p = NULL;
405 Manager *m = userdata;
413 r = sd_bus_message_read(message, "u", &uid);
417 r = manager_get_user_from_creds(m, message, uid, error, &user);
421 p = user_bus_path(user);
425 return sd_bus_reply_method_return(message, "o", p);
428 static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
429 _cleanup_free_ char *p = NULL;
430 Manager *m = userdata;
438 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
440 r = sd_bus_message_read(message, "u", &pid);
447 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
451 r = manager_get_user_by_pid(m, pid, &user);
455 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID,
456 "PID "PID_FMT" does not belong to any logged in user or lingering user",
460 p = user_bus_path(user);
464 return sd_bus_reply_method_return(message, "o", p);
467 static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
468 _cleanup_free_ char *p = NULL;
469 Manager *m = userdata;
477 r = sd_bus_message_read(message, "s", &name);
481 r = manager_get_seat_from_creds(m, message, name, error, &seat);
485 p = seat_bus_path(seat);
489 return sd_bus_reply_method_return(message, "o", p);
492 static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
493 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
494 Manager *m = userdata;
502 r = sd_bus_message_new_method_return(message, &reply);
506 r = sd_bus_message_open_container(reply, 'a', "(susso)");
510 HASHMAP_FOREACH(session, m->sessions, i) {
511 _cleanup_free_ char *p = NULL;
513 p = session_bus_path(session);
517 r = sd_bus_message_append(reply, "(susso)",
519 (uint32_t) session->user->uid,
521 session->seat ? session->seat->id : "",
527 r = sd_bus_message_close_container(reply);
531 return sd_bus_send(NULL, reply, NULL);
534 static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
535 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
536 Manager *m = userdata;
544 r = sd_bus_message_new_method_return(message, &reply);
548 r = sd_bus_message_open_container(reply, 'a', "(uso)");
552 HASHMAP_FOREACH(user, m->users, i) {
553 _cleanup_free_ char *p = NULL;
555 p = user_bus_path(user);
559 r = sd_bus_message_append(reply, "(uso)",
560 (uint32_t) user->uid,
567 r = sd_bus_message_close_container(reply);
571 return sd_bus_send(NULL, reply, NULL);
574 static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
575 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
576 Manager *m = userdata;
584 r = sd_bus_message_new_method_return(message, &reply);
588 r = sd_bus_message_open_container(reply, 'a', "(so)");
592 HASHMAP_FOREACH(seat, m->seats, i) {
593 _cleanup_free_ char *p = NULL;
595 p = seat_bus_path(seat);
599 r = sd_bus_message_append(reply, "(so)", seat->id, p);
604 r = sd_bus_message_close_container(reply);
608 return sd_bus_send(NULL, reply, NULL);
611 static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
612 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
613 Manager *m = userdata;
614 Inhibitor *inhibitor;
621 r = sd_bus_message_new_method_return(message, &reply);
625 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
629 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
631 r = sd_bus_message_append(reply, "(ssssuu)",
632 strempty(inhibit_what_to_string(inhibitor->what)),
633 strempty(inhibitor->who),
634 strempty(inhibitor->why),
635 strempty(inhibit_mode_to_string(inhibitor->mode)),
636 (uint32_t) inhibitor->uid,
637 (uint32_t) inhibitor->pid);
642 r = sd_bus_message_close_container(reply);
646 return sd_bus_send(NULL, reply, NULL);
649 static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
650 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
651 _cleanup_free_ char *id = NULL;
652 Session *session = NULL;
653 uint32_t audit_id = 0;
654 Manager *m = userdata;
668 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
669 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
671 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
675 if (!uid_is_valid(uid))
676 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
677 if (leader < 0 || leader == 1 || leader == getpid_cached())
678 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
681 t = _SESSION_TYPE_INVALID;
683 t = session_type_from_string(type);
685 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
689 c = _SESSION_CLASS_INVALID;
691 c = session_class_from_string(class);
693 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
696 if (isempty(desktop))
699 if (!string_is_safe(desktop))
700 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
706 seat = hashmap_get(m->seats, cseat);
708 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
711 if (tty_is_vc(tty)) {
716 else if (seat != m->seat0)
717 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);
719 v = vtnr_from_tty(tty);
721 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
725 else if (vtnr != (uint32_t) v)
726 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
728 } else if (tty_is_console(tty)) {
732 else if (seat != m->seat0)
733 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
736 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
740 if (seat_has_vts(seat)) {
741 if (vtnr <= 0 || vtnr > 63)
742 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
745 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
749 r = sd_bus_message_enter_container(message, 'a', "(sv)");
753 if (t == _SESSION_TYPE_INVALID) {
754 if (!isempty(display))
756 else if (!isempty(tty))
759 t = SESSION_UNSPECIFIED;
762 if (c == _SESSION_CLASS_INVALID) {
763 if (t == SESSION_UNSPECIFIED)
764 c = SESSION_BACKGROUND;
770 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
772 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
776 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
781 /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session
782 * that avoids creating a logind session. */
783 r = manager_get_user_by_pid(m, leader, NULL);
787 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice");
790 * Old gdm and lightdm start the user-session on the same VT as
791 * the greeter session. But they destroy the greeter session
792 * after the user-session and want the user-session to take
793 * over the VT. We need to support this for
794 * backwards-compatibility, so make sure we allow new sessions
795 * on a VT that a greeter is running on. Furthermore, to allow
796 * re-logins, we have to allow a greeter to take over a used VT for
797 * the exact same reasons.
799 if (c != SESSION_GREETER &&
801 vtnr < m->seat0->position_count &&
802 m->seat0->positions[vtnr] &&
803 m->seat0->positions[vtnr]->class != SESSION_GREETER)
804 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
806 if (hashmap_size(m->sessions) >= m->sessions_max)
807 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
809 (void) audit_session_from_pid(leader, &audit_id);
810 if (audit_session_is_valid(audit_id)) {
811 /* Keep our session IDs and the audit session IDs in sync */
813 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
816 /* Wut? There's already a session by this name and we
817 * didn't find it above? Weird, then let's not trust
818 * the audit data and let's better register a new
820 if (hashmap_get(m->sessions, id)) {
821 log_warning("Existing logind session ID %s used by new audit session, ignoring.", id);
822 audit_id = AUDIT_SESSION_INVALID;
831 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
834 } while (hashmap_get(m->sessions, id));
837 r = manager_add_user_by_uid(m, uid, &user);
841 r = manager_add_session(m, id, &session);
845 session_set_user(session, user);
847 session->leader = leader;
848 session->audit_id = audit_id;
851 session->remote = remote;
852 session->vtnr = vtnr;
855 session->tty = strdup(tty);
862 if (!isempty(display)) {
863 session->display = strdup(display);
864 if (!session->display) {
870 if (!isempty(remote_user)) {
871 session->remote_user = strdup(remote_user);
872 if (!session->remote_user) {
878 if (!isempty(remote_host)) {
879 session->remote_host = strdup(remote_host);
880 if (!session->remote_host) {
886 if (!isempty(service)) {
887 session->service = strdup(service);
888 if (!session->service) {
894 if (!isempty(desktop)) {
895 session->desktop = strdup(desktop);
896 if (!session->desktop) {
903 r = seat_attach_session(seat, session);
908 r = session_start(session);
912 session->create_message = sd_bus_message_ref(message);
914 #if 0 /// UNNEEDED by elogind
915 /* Now, let's wait until the slice unit and stuff got created. We send the reply back from
916 * session_send_create_reply(). */
918 /* We reply directly. */
920 r = session_send_create_reply(session, NULL);
929 session_add_to_gc_queue(session);
932 user_add_to_gc_queue(user);
937 static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
938 Manager *m = userdata;
946 r = sd_bus_message_read(message, "s", &name);
950 r = manager_get_session_from_creds(m, message, name, error, &session);
954 r = session_release(session);
958 #if 1 /// elogind must queue this session
959 session_add_to_gc_queue(session);
961 return sd_bus_reply_method_return(message, NULL);
964 static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
965 Manager *m = userdata;
973 r = sd_bus_message_read(message, "s", &name);
977 r = manager_get_session_from_creds(m, message, name, error, &session);
981 return bus_session_method_activate(message, session, error);
984 static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
985 const char *session_name, *seat_name;
986 Manager *m = userdata;
994 /* Same as ActivateSession() but refuses to work if
995 * the seat doesn't match */
997 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
1001 r = manager_get_session_from_creds(m, message, session_name, error, &session);
1005 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
1009 if (session->seat != seat)
1010 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
1012 r = session_activate(session);
1016 return sd_bus_reply_method_return(message, NULL);
1019 static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1020 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_lock(message, session, error);
1039 static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1040 Manager *m = userdata;
1046 r = bus_verify_polkit_async(
1049 "org.freedesktop.login1.lock-sessions",
1053 &m->polkit_registry,
1058 return 1; /* Will call us back */
1060 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
1064 return sd_bus_reply_method_return(message, NULL);
1067 static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1069 Manager *m = userdata;
1076 r = sd_bus_message_read(message, "s", &name);
1080 r = manager_get_session_from_creds(m, message, name, error, &session);
1084 return bus_session_method_kill(message, session, error);
1087 static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1088 Manager *m = userdata;
1096 r = sd_bus_message_read(message, "u", &uid);
1100 r = manager_get_user_from_creds(m, message, uid, error, &user);
1104 return bus_user_method_kill(message, user, error);
1107 static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1108 Manager *m = userdata;
1116 r = sd_bus_message_read(message, "s", &name);
1120 r = manager_get_session_from_creds(m, message, name, error, &session);
1124 return bus_session_method_terminate(message, session, error);
1127 static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1128 Manager *m = userdata;
1136 r = sd_bus_message_read(message, "u", &uid);
1140 r = manager_get_user_from_creds(m, message, uid, error, &user);
1144 return bus_user_method_terminate(message, user, error);
1147 static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1148 Manager *m = userdata;
1156 r = sd_bus_message_read(message, "s", &name);
1160 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1164 return bus_seat_method_terminate(message, seat, error);
1167 static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1168 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1169 _cleanup_free_ char *cc = NULL;
1170 Manager *m = userdata;
1171 int r, b, interactive;
1174 uint32_t uid, auth_uid;
1179 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1183 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID |
1184 SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1188 if (!uid_is_valid(uid)) {
1189 /* Note that we get the owner UID of the session or user unit,
1190 * not the actual client UID here! */
1191 r = sd_bus_creds_get_owner_uid(creds, &uid);
1196 /* owner_uid is racy, so for authorization we must use euid */
1197 r = sd_bus_creds_get_euid(creds, &auth_uid);
1204 return errno > 0 ? -errno : -ENOENT;
1206 r = bus_verify_polkit_async(
1209 uid == auth_uid ? "org.freedesktop.login1.set-self-linger" :
1210 "org.freedesktop.login1.set-user-linger",
1214 &m->polkit_registry,
1219 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1221 mkdir_p_label("/var/lib/elogind", 0755);
1223 r = mkdir_safe_label("/var/lib/elogind/linger", 0755, 0, 0, MKDIR_WARN_MODE);
1227 cc = cescape(pw->pw_name);
1231 path = strjoina("/var/lib/elogind/linger/", cc);
1239 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1246 if (r < 0 && errno != ENOENT)
1249 u = hashmap_get(m->users, UID_TO_PTR(uid));
1251 user_add_to_gc_queue(u);
1254 return sd_bus_reply_method_return(message, NULL);
1257 static int trigger_device(Manager *m, struct udev_device *d) {
1258 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
1259 struct udev_list_entry *first, *item;
1264 e = udev_enumerate_new(m->udev);
1269 r = udev_enumerate_add_match_parent(e, d);
1274 r = udev_enumerate_scan_devices(e);
1278 first = udev_enumerate_get_list_entry(e);
1279 udev_list_entry_foreach(item, first) {
1280 _cleanup_free_ char *t = NULL;
1283 p = udev_list_entry_get_name(item);
1285 t = strappend(p, "/uevent");
1289 (void) write_string_file(t, "change", 0);
1295 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1296 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
1297 _cleanup_free_ char *rule = NULL, *file = NULL;
1298 const char *id_for_seat;
1305 d = udev_device_new_from_syspath(m->udev, sysfs);
1309 if (!udev_device_has_tag(d, "seat"))
1312 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
1316 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1319 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1322 mkdir_p_label("/etc/udev/rules.d", 0755);
1323 r = write_string_file_atomic_label(file, rule);
1327 return trigger_device(m, d);
1330 static int flush_devices(Manager *m) {
1331 _cleanup_closedir_ DIR *d;
1335 d = opendir("/etc/udev/rules.d");
1337 if (errno != ENOENT)
1338 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1342 FOREACH_DIRENT_ALL(de, d, break) {
1343 if (!dirent_is_file(de))
1346 if (!startswith(de->d_name, "72-seat-"))
1349 if (!endswith(de->d_name, ".rules"))
1352 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1353 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1357 return trigger_device(m, NULL);
1360 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1361 const char *sysfs, *seat;
1362 Manager *m = userdata;
1368 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1372 if (!path_startswith(sysfs, "/sys"))
1373 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1375 if (!seat_name_is_valid(seat))
1376 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1378 r = bus_verify_polkit_async(
1381 "org.freedesktop.login1.attach-device",
1385 &m->polkit_registry,
1390 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1392 r = attach_device(m, seat, sysfs);
1396 return sd_bus_reply_method_return(message, NULL);
1399 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1400 Manager *m = userdata;
1406 r = sd_bus_message_read(message, "b", &interactive);
1410 r = bus_verify_polkit_async(
1413 "org.freedesktop.login1.flush-devices",
1417 &m->polkit_registry,
1422 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1424 r = flush_devices(m);
1428 return sd_bus_reply_method_return(message, NULL);
1431 static int have_multiple_sessions(
1440 /* Check for other users' sessions. Greeter sessions do not
1441 * count, and non-login sessions do not count either. */
1442 HASHMAP_FOREACH(session, m->sessions, i)
1443 if (session->class == SESSION_USER &&
1444 session->user->uid != uid)
1450 #if 0 /// elogind has its own variant in elogind-dbus.c
1451 static int bus_manager_log_shutdown(
1453 const char *unit_name) {
1460 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1461 p = "MESSAGE=System is powering down";
1462 q = "SHUTDOWN=power-off";
1463 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1464 p = "MESSAGE=System is rebooting";
1465 q = "SHUTDOWN=reboot";
1466 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1467 p = "MESSAGE=System is halting";
1468 q = "SHUTDOWN=halt";
1469 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1470 p = "MESSAGE=System is rebooting with kexec";
1471 q = "SHUTDOWN=kexec";
1473 p = "MESSAGE=System is shutting down";
1477 if (isempty(m->wall_message))
1478 p = strjoina(p, ".");
1480 p = strjoina(p, " (", m->wall_message, ").");
1482 return log_struct(LOG_NOTICE,
1483 "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
1490 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1491 Manager *m = userdata;
1496 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1500 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1505 if (until <= now(CLOCK_MONOTONIC))
1508 /* We want to ignore the lid switch for a while after each
1509 * suspend, and after boot-up. Hence let's install a timer for
1510 * this. As long as the event source exists we ignore the lid
1513 if (m->lid_switch_ignore_event_source) {
1516 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1523 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1525 r = sd_event_add_time(
1527 &m->lid_switch_ignore_event_source,
1530 lid_switch_ignore_handler, m);
1535 #if 0 /// elogind-dbus.c needs to access this
1536 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1538 int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1541 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1542 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1543 [INHIBIT_SLEEP] = "PrepareForSleep"
1546 int active = _active;
1550 assert(w < _INHIBIT_WHAT_MAX);
1551 assert(signal_name[w]);
1553 return sd_bus_emit_signal(m->bus,
1554 "/org/freedesktop/login1",
1555 "org.freedesktop.login1.Manager",
1561 #if 0 /// elogind has its own variant in elogind-dbus.c
1562 static int execute_shutdown_or_sleep(
1565 const char *unit_name,
1566 sd_bus_error *error) {
1568 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1575 assert(w < _INHIBIT_WHAT_MAX);
1578 if (w == INHIBIT_SHUTDOWN)
1579 bus_manager_log_shutdown(m, unit_name);
1581 r = sd_bus_call_method(
1583 "org.freedesktop.systemd1",
1584 "/org/freedesktop/systemd1",
1585 "org.freedesktop.systemd1.Manager",
1589 "ss", unit_name, "replace-irreversibly");
1593 r = sd_bus_message_read(reply, "o", &p);
1603 m->action_unit = unit_name;
1604 free(m->action_job);
1608 /* Make sure the lid switch is ignored for a while */
1609 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1614 /* Tell people that they now may take a lock again */
1615 (void) send_prepare_for(m, w, false);
1621 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1623 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1624 Inhibitor *offending = NULL;
1629 #if 0 /// elogind has no action_job, but a pending_action
1630 if (manager->action_what == 0 || manager->action_job)
1632 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
1636 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1637 _cleanup_free_ char *comm = NULL, *u = NULL;
1642 (void) get_process_comm(offending->pid, &comm);
1643 u = uid_to_name(offending->uid);
1645 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1646 offending->uid, strna(u),
1647 offending->pid, strna(comm));
1650 /* Actually do the operation */
1651 #if 0 /// elogind has no action_unit but a pending_action
1652 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
1654 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
1657 log_warning("Error during inhibitor-delayed operation (already returned success to client): %s",
1658 bus_error_message(&error, r));
1661 #if 0 /// elogind has no action_unit but a pending_action
1662 manager->action_unit = NULL;
1663 manager->action_what = 0;
1666 manager->pending_action = HANDLE_IGNORE;
1667 manager->action_what = 0;
1668 /* It is not a critical error for elogind if suspending fails */
1675 static int manager_inhibit_timeout_handler(
1680 Manager *manager = userdata;
1684 assert(manager->inhibit_timeout_source == s);
1686 r = manager_dispatch_delayed(manager, true);
1687 return (r < 0) ? r : 0;
1690 #if 0 /// elogind does not have unit_name but action
1691 static int delay_shutdown_or_sleep(
1694 const char *unit_name) {
1697 int delay_shutdown_or_sleep(
1700 HandleAction action) {
1707 assert(w < _INHIBIT_WHAT_MAX);
1708 #if 0 /// UNNEEDED by elogind
1712 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1714 if (m->inhibit_timeout_source) {
1715 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1717 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1719 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1721 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1723 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1724 timeout_val, 0, manager_inhibit_timeout_handler, m);
1729 #if 0 /// elogind does not have unit_name but pendig_action
1730 m->action_unit = unit_name;
1732 m->pending_action = action;
1739 int bus_manager_shutdown_or_sleep_now_or_later(
1741 #if 0 /// elogind has HandleAction instead of const char* unit_name
1742 const char *unit_name,
1745 HandleAction unit_name,
1748 sd_bus_error *error) {
1753 #if 0 /// for elogind only w has to be checked.
1756 assert(w <= _INHIBIT_WHAT_MAX);
1757 assert(!m->action_job);
1760 assert(w <= _INHIBIT_WHAT_MAX);
1763 /* Tell everybody to prepare for shutdown/sleep */
1764 (void) send_prepare_for(m, w, true);
1767 m->inhibit_delay_max > 0 &&
1768 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1770 log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
1771 handle_action_to_string(unit_name),
1772 delayed ? "" : "NOT ");
1774 /* Shutdown is delayed, keep in mind what we
1775 * want to do, and start a timeout */
1776 r = delay_shutdown_or_sleep(m, w, unit_name);
1778 /* Shutdown is not delayed, execute it
1780 r = execute_shutdown_or_sleep(m, w, unit_name, error);
1785 static int verify_shutdown_creds(
1787 sd_bus_message *message,
1791 const char *action_multiple_sessions,
1792 const char *action_ignore_inhibit,
1793 sd_bus_error *error) {
1795 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1796 bool multiple_sessions, blocked;
1803 assert(w <= _INHIBIT_WHAT_MAX);
1805 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1809 r = sd_bus_creds_get_euid(creds, &uid);
1813 r = have_multiple_sessions(m, uid);
1817 multiple_sessions = r > 0;
1818 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1820 if (multiple_sessions && action_multiple_sessions) {
1821 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1825 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1828 if (blocked && action_ignore_inhibit) {
1829 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1833 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1836 if (!multiple_sessions && !blocked && action) {
1837 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1841 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1847 static int method_do_shutdown_or_sleep(
1849 sd_bus_message *message,
1850 #if 0 /// elogind has HandleAction instead of const char* unit_name
1851 const char *unit_name,
1853 HandleAction unit_name,
1857 const char *action_multiple_sessions,
1858 const char *action_ignore_inhibit,
1859 const char *sleep_verb,
1860 sd_bus_error *error) {
1866 #if 0 /// elogind does not need this to be checked
1870 assert(w <= _INHIBIT_WHAT_MAX);
1872 r = sd_bus_message_read(message, "b", &interactive);
1876 log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
1877 __FUNCTION__, action, sleep_verb,
1878 interactive ? "" : "NOT ");
1879 /* Don't allow multiple jobs being executed at the same time */
1881 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1884 #if 0 /// Within elogind the manager m must be provided, too
1885 r = can_sleep(sleep_verb);
1887 r = can_sleep(m, sleep_verb);
1893 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
1896 #if 0 /// Within elogind it does not make sense to verify shutdown creds when suspending
1897 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1898 action_ignore_inhibit, error);
1902 if (IN_SET(unit_name, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
1903 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1904 action_ignore_inhibit, error);
1905 log_debug_elogind("verify_shutdown_creds() returned %d", r);
1911 r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
1915 return sd_bus_reply_method_return(message, NULL);
1918 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1919 Manager *m = userdata;
1921 log_debug_elogind("%s called", __FUNCTION__);
1922 return method_do_shutdown_or_sleep(
1924 #if 0 /// elogind uses HandleAction instead of const char* unti names
1925 SPECIAL_POWEROFF_TARGET,
1930 "org.freedesktop.login1.power-off",
1931 "org.freedesktop.login1.power-off-multiple-sessions",
1932 "org.freedesktop.login1.power-off-ignore-inhibit",
1937 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1938 Manager *m = userdata;
1940 log_debug_elogind("%s called", __FUNCTION__);
1941 return method_do_shutdown_or_sleep(
1943 #if 0 /// elogind uses HandleAction instead of const char* unti names
1944 SPECIAL_REBOOT_TARGET,
1949 "org.freedesktop.login1.reboot",
1950 "org.freedesktop.login1.reboot-multiple-sessions",
1951 "org.freedesktop.login1.reboot-ignore-inhibit",
1956 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1957 Manager *m = userdata;
1959 log_debug_elogind("%s called", __FUNCTION__);
1960 return method_do_shutdown_or_sleep(
1962 #if 0 /// elogind uses HandleAction instead of const char* unti names
1963 SPECIAL_HALT_TARGET,
1968 "org.freedesktop.login1.halt",
1969 "org.freedesktop.login1.halt-multiple-sessions",
1970 "org.freedesktop.login1.halt-ignore-inhibit",
1975 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1976 Manager *m = userdata;
1978 log_debug_elogind("%s called", __FUNCTION__);
1979 return method_do_shutdown_or_sleep(
1981 #if 0 /// elogind uses HandleAction instead of const char* unti names
1982 SPECIAL_SUSPEND_TARGET,
1987 "org.freedesktop.login1.suspend",
1988 "org.freedesktop.login1.suspend-multiple-sessions",
1989 "org.freedesktop.login1.suspend-ignore-inhibit",
1994 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1995 Manager *m = userdata;
1997 log_debug_elogind("%s called", __FUNCTION__);
1998 return method_do_shutdown_or_sleep(
2000 #if 0 /// elogind uses HandleAction instead of const char* unti names
2001 SPECIAL_HIBERNATE_TARGET,
2006 "org.freedesktop.login1.hibernate",
2007 "org.freedesktop.login1.hibernate-multiple-sessions",
2008 "org.freedesktop.login1.hibernate-ignore-inhibit",
2013 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2014 Manager *m = userdata;
2016 log_debug_elogind("%s called", __FUNCTION__);
2017 return method_do_shutdown_or_sleep(
2019 #if 0 /// elogind uses HandleAction instead of const char* unti names
2020 SPECIAL_HYBRID_SLEEP_TARGET,
2022 "org.freedesktop.login1.hibernate",
2023 "org.freedesktop.login1.hibernate-multiple-sessions",
2024 "org.freedesktop.login1.hibernate-ignore-inhibit",
2029 static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2030 Manager *m = userdata;
2032 return method_do_shutdown_or_sleep(
2034 SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
2036 HANDLE_HYBRID_SLEEP,
2039 "org.freedesktop.login1.hibernate",
2040 "org.freedesktop.login1.hibernate-multiple-sessions",
2041 "org.freedesktop.login1.hibernate-ignore-inhibit",
2046 static int nologin_timeout_handler(
2051 Manager *m = userdata;
2053 log_info("Creating /run/nologin, blocking further logins...");
2056 create_shutdown_run_nologin_or_warn() >= 0;
2061 static int update_schedule_file(Manager *m) {
2062 _cleanup_free_ char *temp_path = NULL;
2063 _cleanup_fclose_ FILE *f = NULL;
2068 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE);
2070 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
2072 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
2074 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
2076 (void) fchmod(fileno(f), 0644);
2082 m->scheduled_shutdown_timeout,
2083 m->enable_wall_messages,
2084 m->scheduled_shutdown_type);
2086 if (!isempty(m->wall_message)) {
2087 _cleanup_free_ char *t;
2089 t = cescape(m->wall_message);
2095 fprintf(f, "WALL_MESSAGE=%s\n", t);
2098 r = fflush_and_check(f);
2102 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
2110 (void) unlink(temp_path);
2111 (void) unlink("/run/systemd/shutdown/scheduled");
2113 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
2116 #if 0 /// elogind must access this from elogind-dbus.c
2117 static void reset_scheduled_shutdown(Manager *m) {
2119 void reset_scheduled_shutdown(Manager *m) {
2123 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2124 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
2125 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
2127 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
2128 m->scheduled_shutdown_timeout = 0;
2129 m->shutdown_dry_run = false;
2131 if (m->unlink_nologin) {
2132 (void) unlink_or_warn("/run/nologin");
2133 m->unlink_nologin = false;
2136 (void) unlink("/run/systemd/shutdown/scheduled");
2139 #if 0 /// elogind has its own variant in elogind-dbus.c
2140 static int manager_scheduled_shutdown_handler(
2145 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2146 Manager *m = userdata;
2152 if (isempty(m->scheduled_shutdown_type))
2155 if (streq(m->scheduled_shutdown_type, "poweroff"))
2156 target = SPECIAL_POWEROFF_TARGET;
2157 else if (streq(m->scheduled_shutdown_type, "reboot"))
2158 target = SPECIAL_REBOOT_TARGET;
2159 else if (streq(m->scheduled_shutdown_type, "halt"))
2160 target = SPECIAL_HALT_TARGET;
2162 assert_not_reached("unexpected shutdown type");
2164 /* Don't allow multiple jobs being executed at the same time */
2165 if (m->action_what) {
2167 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target);
2171 if (m->shutdown_dry_run) {
2172 /* We do not process delay inhibitors here. Otherwise, we
2173 * would have to be considered "in progress" (like the check
2174 * above) for some seconds after our admin has seen the final
2177 bus_manager_log_shutdown(m, target);
2178 log_info("Running in dry run, suppressing action.");
2179 reset_scheduled_shutdown(m);
2184 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error);
2186 log_error_errno(r, "Scheduled shutdown to %s failed: %m", target);
2193 reset_scheduled_shutdown(m);
2198 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2199 Manager *m = userdata;
2200 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2201 const char *action_multiple_sessions = NULL;
2202 const char *action_ignore_inhibit = NULL;
2203 const char *action = NULL;
2207 bool dry_run = false;
2212 log_debug_elogind("%s called", __FUNCTION__);
2213 r = sd_bus_message_read(message, "st", &type, &elapse);
2217 if (startswith(type, "dry-")) {
2222 if (streq(type, "poweroff")) {
2223 action = "org.freedesktop.login1.power-off";
2224 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2225 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2226 } else if (streq(type, "reboot")) {
2227 action = "org.freedesktop.login1.reboot";
2228 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2229 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2230 } else if (streq(type, "halt")) {
2231 action = "org.freedesktop.login1.halt";
2232 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2233 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2235 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2237 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2238 action, action_multiple_sessions, action_ignore_inhibit, error);
2242 if (m->scheduled_shutdown_timeout_source) {
2243 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2245 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2247 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2249 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2251 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2252 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2254 return log_error_errno(r, "sd_event_add_time() failed: %m");
2257 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2259 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2263 m->shutdown_dry_run = dry_run;
2265 if (m->nologin_timeout_source) {
2266 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2268 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2270 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2272 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2274 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2275 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2277 return log_error_errno(r, "sd_event_add_time() failed: %m");
2280 m->scheduled_shutdown_timeout = elapse;
2282 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2284 const char *tty = NULL;
2286 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2287 (void) sd_bus_creds_get_tty(creds, &tty);
2289 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2291 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2296 r = manager_setup_wall_message_timer(m);
2300 r = update_schedule_file(m);
2304 return sd_bus_reply_method_return(message, NULL);
2307 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2308 Manager *m = userdata;
2310 #if 1 /// elogind needs to construct the message to allow extra wall messages
2311 _cleanup_free_ char *l = NULL;
2317 log_debug_elogind("%s called", __FUNCTION__);
2318 cancelled = m->scheduled_shutdown_type != NULL;
2319 reset_scheduled_shutdown(m);
2322 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2323 const char *tty = NULL;
2327 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2329 (void) sd_bus_creds_get_uid(creds, &uid);
2330 (void) sd_bus_creds_get_tty(creds, &tty);
2333 #if 0 /// elogind wants to allow extra cancellation messages
2334 utmp_wall("The system shutdown has been cancelled",
2335 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2337 r = asprintf(&l, "%s%sThe system shutdown has been cancelled!",
2338 strempty(m->wall_message),
2339 isempty(m->wall_message) ? "" : "\n");
2345 utmp_wall(l, uid_to_name(uid), tty, logind_wall_tty_filter, m);
2349 return sd_bus_reply_method_return(message, "b", cancelled);
2352 static int method_can_shutdown_or_sleep(
2354 sd_bus_message *message,
2357 const char *action_multiple_sessions,
2358 const char *action_ignore_inhibit,
2359 const char *sleep_verb,
2360 sd_bus_error *error) {
2362 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2363 bool multiple_sessions, challenge, blocked;
2364 const char *result = NULL;
2371 assert(w <= _INHIBIT_WHAT_MAX);
2373 assert(action_multiple_sessions);
2374 assert(action_ignore_inhibit);
2377 #if 0 /// elogind needs to have the manager being passed
2378 r = can_sleep(sleep_verb);
2380 r = can_sleep(m, sleep_verb);
2385 return sd_bus_reply_method_return(message, "s", "na");
2388 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2392 r = sd_bus_creds_get_euid(creds, &uid);
2396 r = have_multiple_sessions(m, uid);
2400 multiple_sessions = r > 0;
2401 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2403 if (multiple_sessions) {
2404 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2411 result = "challenge";
2417 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2421 if (r > 0 && !result)
2423 else if (challenge && (!result || streq(result, "yes")))
2424 result = "challenge";
2429 if (!multiple_sessions && !blocked) {
2430 /* If neither inhibit nor multiple sessions
2431 * apply then just check the normal policy */
2433 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2440 result = "challenge";
2445 return sd_bus_reply_method_return(message, "s", result);
2448 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2449 Manager *m = userdata;
2451 return method_can_shutdown_or_sleep(
2454 "org.freedesktop.login1.power-off",
2455 "org.freedesktop.login1.power-off-multiple-sessions",
2456 "org.freedesktop.login1.power-off-ignore-inhibit",
2461 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2462 Manager *m = userdata;
2464 return method_can_shutdown_or_sleep(
2467 "org.freedesktop.login1.reboot",
2468 "org.freedesktop.login1.reboot-multiple-sessions",
2469 "org.freedesktop.login1.reboot-ignore-inhibit",
2474 static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2475 Manager *m = userdata;
2477 return method_can_shutdown_or_sleep(
2480 "org.freedesktop.login1.halt",
2481 "org.freedesktop.login1.halt-multiple-sessions",
2482 "org.freedesktop.login1.halt-ignore-inhibit",
2487 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2488 Manager *m = userdata;
2490 return method_can_shutdown_or_sleep(
2493 "org.freedesktop.login1.suspend",
2494 "org.freedesktop.login1.suspend-multiple-sessions",
2495 "org.freedesktop.login1.suspend-ignore-inhibit",
2500 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2501 Manager *m = userdata;
2503 return method_can_shutdown_or_sleep(
2506 "org.freedesktop.login1.hibernate",
2507 "org.freedesktop.login1.hibernate-multiple-sessions",
2508 "org.freedesktop.login1.hibernate-ignore-inhibit",
2513 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2514 Manager *m = userdata;
2516 return method_can_shutdown_or_sleep(
2519 "org.freedesktop.login1.hibernate",
2520 "org.freedesktop.login1.hibernate-multiple-sessions",
2521 "org.freedesktop.login1.hibernate-ignore-inhibit",
2526 static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2527 Manager *m = userdata;
2529 return method_can_shutdown_or_sleep(
2532 "org.freedesktop.login1.hibernate",
2533 "org.freedesktop.login1.hibernate-multiple-sessions",
2534 "org.freedesktop.login1.hibernate-ignore-inhibit",
2535 "suspend-then-hibernate",
2539 static int property_get_reboot_to_firmware_setup(
2542 const char *interface,
2543 const char *property,
2544 sd_bus_message *reply,
2546 sd_bus_error *error) {
2547 #if 0 /// elogind does not support EFI
2554 r = efi_get_reboot_to_firmware();
2555 if (r < 0 && r != -EOPNOTSUPP)
2556 log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m");
2558 return sd_bus_message_append(reply, "b", r > 0);
2560 return sd_bus_message_append(reply, "b", false);
2564 static int method_set_reboot_to_firmware_setup(
2565 sd_bus_message *message,
2567 sd_bus_error *error) {
2570 Manager *m = userdata;
2575 r = sd_bus_message_read(message, "b", &b);
2579 r = bus_verify_polkit_async(message,
2581 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2585 &m->polkit_registry,
2590 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2592 #if 0 /// elogind does not support EFI
2593 r = efi_set_reboot_to_firmware(b);
2598 return sd_bus_reply_method_return(message, NULL);
2601 static int method_can_reboot_to_firmware_setup(
2602 sd_bus_message *message,
2604 sd_bus_error *error) {
2606 #if 0 /// elogind does not support EFI
2610 Manager *m = userdata;
2615 r = efi_reboot_to_firmware_supported();
2617 if (r != -EOPNOTSUPP)
2618 log_warning_errno(errno, "Failed to determine whether reboot to firmware is supported: %m");
2620 return sd_bus_reply_method_return(message, "s", "na");
2623 r = bus_test_polkit(message,
2625 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2636 result = "challenge";
2640 return sd_bus_reply_method_return(message, "s", result);
2642 return sd_bus_reply_method_return(message, "s", "no");
2646 static int method_set_wall_message(
2647 sd_bus_message *message,
2649 sd_bus_error *error) {
2652 Manager *m = userdata;
2654 int enable_wall_messages;
2659 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2663 #if 0 /// elogind only calls this for shutdown/reboot, which already needs authorization.
2664 r = bus_verify_polkit_async(message,
2666 "org.freedesktop.login1.set-wall-message",
2670 &m->polkit_registry,
2675 return 1; /* Will call us back */
2678 r = free_and_strdup(&m->wall_message, empty_to_null(wall_message));
2682 m->enable_wall_messages = enable_wall_messages;
2684 return sd_bus_reply_method_return(message, NULL);
2687 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2688 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2689 const char *who, *why, *what, *mode;
2690 _cleanup_free_ char *id = NULL;
2691 _cleanup_close_ int fifo_fd = -1;
2692 Manager *m = userdata;
2693 Inhibitor *i = NULL;
2703 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2707 w = inhibit_what_from_string(what);
2709 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2711 mm = inhibit_mode_from_string(mode);
2713 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2715 /* Delay is only supported for shutdown/sleep */
2716 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2717 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2719 /* Don't allow taking delay locks while we are already
2720 * executing the operation. We shouldn't create the impression
2721 * that the lock was successful if the machine is about to go
2722 * down/suspend any moment. */
2723 if (m->action_what & w)
2724 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2726 r = bus_verify_polkit_async(
2729 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2730 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2731 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2732 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2733 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2734 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2735 "org.freedesktop.login1.inhibit-handle-lid-switch",
2739 &m->polkit_registry,
2744 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2746 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2750 r = sd_bus_creds_get_euid(creds, &uid);
2754 r = sd_bus_creds_get_pid(creds, &pid);
2758 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2759 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2764 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2767 } while (hashmap_get(m->inhibitors, id));
2769 r = manager_add_inhibitor(m, id, &i);
2777 i->why = strdup(why);
2778 i->who = strdup(who);
2780 if (!i->why || !i->who) {
2785 fifo_fd = inhibitor_create_fifo(i);
2793 return sd_bus_reply_method_return(message, "h", fifo_fd);
2802 const sd_bus_vtable manager_vtable[] = {
2803 SD_BUS_VTABLE_START(0),
2805 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2806 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2808 #if 0 /// UNNEEDED by elogind
2809 SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2811 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2812 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2813 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2814 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2815 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2816 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2817 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2818 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2819 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2820 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2821 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2822 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2823 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2824 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2825 SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST),
2826 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2827 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2828 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2829 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2830 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2831 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2832 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2833 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2834 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2835 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", bus_property_get_size, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2836 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2837 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_current_inhibitors, 0, 0),
2838 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2839 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_current_sessions, 0, 0),
2840 SD_BUS_PROPERTY("UserTasksMax", "t", NULL, offsetof(Manager, user_tasks_max), SD_BUS_VTABLE_PROPERTY_CONST),
2842 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2843 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2844 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2845 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2846 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2847 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2848 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2849 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2850 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2851 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2852 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2853 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2854 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2855 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2856 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2857 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2858 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2859 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2860 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2861 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2862 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2863 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2864 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2865 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2866 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2867 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2868 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2869 SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2870 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2871 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2872 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2873 SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2874 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2875 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2876 SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED),
2877 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2878 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2879 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2880 SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2881 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2882 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2883 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2884 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2885 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2886 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2888 SD_BUS_SIGNAL("SessionNew", "so", 0),
2889 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2890 SD_BUS_SIGNAL("UserNew", "uo", 0),
2891 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2892 SD_BUS_SIGNAL("SeatNew", "so", 0),
2893 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2894 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2895 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2900 #if 0 /// UNNEEDED by elogind
2901 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2910 if (streq(result, "done"))
2911 r = session_send_create_reply(s, NULL);
2913 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2915 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2916 r = session_send_create_reply(s, &e);
2922 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2923 const char *path, *result, *unit;
2924 Manager *m = userdata;
2933 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2935 bus_log_parse_error(r);
2939 if (m->action_job && streq(m->action_job, path)) {
2940 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2942 /* Tell people that they now may take a lock again */
2943 (void) send_prepare_for(m, m->action_what, false);
2945 m->action_job = mfree(m->action_job);
2946 m->action_unit = NULL;
2951 session = hashmap_get(m->session_units, unit);
2952 if (session && streq_ptr(path, session->scope_job)) {
2953 session->scope_job = mfree(session->scope_job);
2954 session_jobs_reply(session, unit, result);
2956 session_save(session);
2957 user_save(session->user);
2958 session_add_to_gc_queue(session);
2961 user = hashmap_get(m->user_units, unit);
2963 (streq_ptr(path, user->service_job) ||
2964 streq_ptr(path, user->slice_job))) {
2966 if (streq_ptr(path, user->service_job))
2967 user->service_job = mfree(user->service_job);
2969 if (streq_ptr(path, user->slice_job))
2970 user->slice_job = mfree(user->slice_job);
2972 LIST_FOREACH(sessions_by_user, session, user->sessions)
2973 session_jobs_reply(session, unit, result);
2976 user_add_to_gc_queue(user);
2982 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2983 const char *path, *unit;
2984 Manager *m = userdata;
2992 r = sd_bus_message_read(message, "so", &unit, &path);
2994 bus_log_parse_error(r);
2998 session = hashmap_get(m->session_units, unit);
3000 session_add_to_gc_queue(session);
3002 user = hashmap_get(m->user_units, unit);
3004 user_add_to_gc_queue(user);
3009 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3010 _cleanup_free_ char *unit = NULL;
3011 Manager *m = userdata;
3020 path = sd_bus_message_get_path(message);
3024 r = unit_name_from_dbus_path(path, &unit);
3025 if (r == -EINVAL) /* not a unit */
3032 session = hashmap_get(m->session_units, unit);
3034 session_add_to_gc_queue(session);
3036 user = hashmap_get(m->user_units, unit);
3038 user_add_to_gc_queue(user);
3043 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
3044 Manager *m = userdata;
3052 r = sd_bus_message_read(message, "b", &b);
3054 bus_log_parse_error(r);
3061 /* systemd finished reloading, let's recheck all our sessions */
3062 log_debug("System manager has been reloaded, rechecking sessions...");
3064 HASHMAP_FOREACH(session, m->sessions, i)
3065 session_add_to_gc_queue(session);
3071 int manager_send_changed(Manager *manager, const char *property, ...) {
3076 l = strv_from_stdarg_alloca(property);
3078 return sd_bus_emit_properties_changed_strv(
3080 "/org/freedesktop/login1",
3081 "org.freedesktop.login1.Manager",
3085 #if 0 /// UNNEEDED by elogind
3086 static int strdup_job(sd_bus_message *reply, char **job) {
3091 r = sd_bus_message_read(reply, "o", &j);
3103 int manager_start_slice(
3106 const char *description,
3110 sd_bus_error *error,
3113 _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(slice), "fail");
3134 r = sd_bus_message_open_container(m, 'a', "(sv)");
3138 if (!isempty(description)) {
3139 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3144 if (!isempty(after)) {
3145 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3150 if (!isempty(after2)) {
3151 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3156 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", tasks_max);
3160 r = sd_bus_message_close_container(m);
3164 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3168 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3172 return strdup_job(reply, job);
3175 int manager_start_scope(
3180 const char *description,
3184 sd_bus_error *error,
3187 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
3195 r = sd_bus_message_new_method_call(
3198 "org.freedesktop.systemd1",
3199 "/org/freedesktop/systemd1",
3200 "org.freedesktop.systemd1.Manager",
3201 "StartTransientUnit");
3205 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
3209 r = sd_bus_message_open_container(m, 'a', "(sv)");
3213 if (!isempty(slice)) {
3214 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
3219 if (!isempty(description)) {
3220 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3225 if (!isempty(after)) {
3226 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3231 if (!isempty(after2)) {
3232 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3237 /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore
3239 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
3243 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3247 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", tasks_max);
3251 r = sd_bus_message_close_container(m);
3255 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3259 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3263 return strdup_job(reply, job);
3266 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3267 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3274 r = sd_bus_call_method(
3276 "org.freedesktop.systemd1",
3277 "/org/freedesktop/systemd1",
3278 "org.freedesktop.systemd1.Manager",
3282 "ss", unit, "replace");
3286 return strdup_job(reply, job);
3289 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3290 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3297 r = sd_bus_call_method(
3299 "org.freedesktop.systemd1",
3300 "/org/freedesktop/systemd1",
3301 "org.freedesktop.systemd1.Manager",
3305 "ss", unit, "fail");
3307 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3308 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3311 sd_bus_error_free(error);
3318 return strdup_job(reply, job);
3321 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3322 _cleanup_free_ char *path = NULL;
3328 path = unit_dbus_path_from_name(scope);
3332 r = sd_bus_call_method(
3334 "org.freedesktop.systemd1",
3336 "org.freedesktop.systemd1.Scope",
3342 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3343 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3344 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3345 sd_bus_error_free(error);
3355 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3359 return sd_bus_call_method(
3361 "org.freedesktop.systemd1",
3362 "/org/freedesktop/systemd1",
3363 "org.freedesktop.systemd1.Manager",
3367 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3370 int manager_unit_is_active(Manager *manager, const char *unit) {
3371 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3372 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3373 _cleanup_free_ char *path = NULL;
3380 path = unit_dbus_path_from_name(unit);
3384 r = sd_bus_get_property(
3386 "org.freedesktop.systemd1",
3388 "org.freedesktop.systemd1.Unit",
3394 /* systemd might have droppped off momentarily, let's
3395 * not make this an error */
3396 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3397 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3400 /* If the unit is already unloaded then it's not
3402 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3403 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3409 r = sd_bus_message_read(reply, "s", &state);
3413 return !streq(state, "inactive") && !streq(state, "failed");
3416 int manager_job_is_active(Manager *manager, const char *path) {
3417 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3418 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3424 r = sd_bus_get_property(
3426 "org.freedesktop.systemd1",
3428 "org.freedesktop.systemd1.Job",
3434 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3435 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3438 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3444 /* We don't actually care about the state really. The fact
3445 * that we could read the job state is enough for us */