2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "sd-messages.h"
27 #include "alloc-util.h"
28 #include "audit-util.h"
29 #include "bus-common-errors.h"
30 #include "bus-error.h"
32 #include "dirent-util.h"
33 //#include "efivars.h"
36 #include "fileio-label.h"
37 #include "formats-util.h"
41 #include "path-util.h"
42 #include "process-util.h"
43 #include "selinux-util.h"
44 //#include "sleep-config.h"
45 //#include "special.h"
47 #include "terminal-util.h"
48 #include "udev-util.h"
49 #include "unit-name.h"
50 #include "user-util.h"
51 //#include "utmp-wtmp.h"
53 int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
54 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
63 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
67 r = sd_bus_creds_get_session(creds, &name);
72 session = hashmap_get(m->sessions, name);
74 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
80 int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) {
88 if (uid == UID_INVALID) {
89 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
91 /* Note that we get the owner UID of the session, not the actual client UID here! */
92 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
96 r = sd_bus_creds_get_owner_uid(creds, &uid);
101 user = hashmap_get(m->users, UID_TO_PTR(uid));
103 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user "UID_FMT" known or logged in", uid);
109 int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) {
120 r = manager_get_session_from_creds(m, message, NULL, error, &session);
124 seat = session->seat;
126 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat.");
128 seat = hashmap_get(m->seats, name);
130 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name);
137 static int property_get_idle_hint(
140 const char *interface,
141 const char *property,
142 sd_bus_message *reply,
144 sd_bus_error *error) {
146 Manager *m = userdata;
152 return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0);
155 static int property_get_idle_since_hint(
158 const char *interface,
159 const char *property,
160 sd_bus_message *reply,
162 sd_bus_error *error) {
164 Manager *m = userdata;
165 dual_timestamp t = DUAL_TIMESTAMP_NULL;
171 manager_get_idle_hint(m, &t);
173 return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic);
176 static int property_get_inhibited(
179 const char *interface,
180 const char *property,
181 sd_bus_message *reply,
183 sd_bus_error *error) {
185 Manager *m = userdata;
192 w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY);
194 return sd_bus_message_append(reply, "s", inhibit_what_to_string(w));
197 static int property_get_preparing(
200 const char *interface,
201 const char *property,
202 sd_bus_message *reply,
204 sd_bus_error *error) {
206 Manager *m = userdata;
213 if (streq(property, "PreparingForShutdown"))
214 b = !!(m->action_what & INHIBIT_SHUTDOWN);
216 b = !!(m->action_what & INHIBIT_SLEEP);
218 return sd_bus_message_append(reply, "b", b);
221 static int property_get_scheduled_shutdown(
224 const char *interface,
225 const char *property,
226 sd_bus_message *reply,
228 sd_bus_error *error) {
230 Manager *m = userdata;
237 r = sd_bus_message_open_container(reply, 'r', "st");
241 r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout);
245 return sd_bus_message_close_container(reply);
248 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction);
250 static int property_get_docked(
253 const char *interface,
254 const char *property,
255 sd_bus_message *reply,
257 sd_bus_error *error) {
259 Manager *m = userdata;
265 return sd_bus_message_append(reply, "b", manager_is_docked_or_external_displays(m));
268 static int property_get_current_sessions(
271 const char *interface,
272 const char *property,
273 sd_bus_message *reply,
275 sd_bus_error *error) {
277 Manager *m = userdata;
283 return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->sessions));
286 static int property_get_current_inhibitors(
289 const char *interface,
290 const char *property,
291 sd_bus_message *reply,
293 sd_bus_error *error) {
295 Manager *m = userdata;
301 return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->inhibitors));
304 static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
305 _cleanup_free_ char *p = NULL;
306 Manager *m = userdata;
314 r = sd_bus_message_read(message, "s", &name);
318 r = manager_get_session_from_creds(m, message, name, error, &session);
322 p = session_bus_path(session);
326 return sd_bus_reply_method_return(message, "o", p);
329 static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
330 _cleanup_free_ char *p = NULL;
331 Session *session = NULL;
332 Manager *m = userdata;
339 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
341 r = sd_bus_message_read(message, "u", &pid);
348 r = manager_get_session_from_creds(m, message, NULL, error, &session);
352 r = manager_get_session_by_pid(m, pid, &session);
357 return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid);
360 p = session_bus_path(session);
364 return sd_bus_reply_method_return(message, "o", p);
367 static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
368 _cleanup_free_ char *p = NULL;
369 Manager *m = userdata;
377 r = sd_bus_message_read(message, "u", &uid);
381 r = manager_get_user_from_creds(m, message, uid, error, &user);
385 p = user_bus_path(user);
389 return sd_bus_reply_method_return(message, "o", p);
392 static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
393 _cleanup_free_ char *p = NULL;
394 Manager *m = userdata;
402 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
404 r = sd_bus_message_read(message, "u", &pid);
411 r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user);
415 r = manager_get_user_by_pid(m, pid, &user);
419 return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "PID "PID_FMT" does not belong to any known or logged in user", pid);
422 p = user_bus_path(user);
426 return sd_bus_reply_method_return(message, "o", p);
429 static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
430 _cleanup_free_ char *p = NULL;
431 Manager *m = userdata;
439 r = sd_bus_message_read(message, "s", &name);
443 r = manager_get_seat_from_creds(m, message, name, error, &seat);
447 p = seat_bus_path(seat);
451 return sd_bus_reply_method_return(message, "o", p);
454 static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
455 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
456 Manager *m = userdata;
464 r = sd_bus_message_new_method_return(message, &reply);
468 r = sd_bus_message_open_container(reply, 'a', "(susso)");
472 HASHMAP_FOREACH(session, m->sessions, i) {
473 _cleanup_free_ char *p = NULL;
475 p = session_bus_path(session);
479 r = sd_bus_message_append(reply, "(susso)",
481 (uint32_t) session->user->uid,
483 session->seat ? session->seat->id : "",
489 r = sd_bus_message_close_container(reply);
493 return sd_bus_send(NULL, reply, NULL);
496 static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
497 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
498 Manager *m = userdata;
506 r = sd_bus_message_new_method_return(message, &reply);
510 r = sd_bus_message_open_container(reply, 'a', "(uso)");
514 HASHMAP_FOREACH(user, m->users, i) {
515 _cleanup_free_ char *p = NULL;
517 p = user_bus_path(user);
521 r = sd_bus_message_append(reply, "(uso)",
522 (uint32_t) user->uid,
529 r = sd_bus_message_close_container(reply);
533 return sd_bus_send(NULL, reply, NULL);
536 static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) {
537 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
538 Manager *m = userdata;
546 r = sd_bus_message_new_method_return(message, &reply);
550 r = sd_bus_message_open_container(reply, 'a', "(so)");
554 HASHMAP_FOREACH(seat, m->seats, i) {
555 _cleanup_free_ char *p = NULL;
557 p = seat_bus_path(seat);
561 r = sd_bus_message_append(reply, "(so)", seat->id, p);
566 r = sd_bus_message_close_container(reply);
570 return sd_bus_send(NULL, reply, NULL);
573 static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) {
574 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
575 Manager *m = userdata;
576 Inhibitor *inhibitor;
583 r = sd_bus_message_new_method_return(message, &reply);
587 r = sd_bus_message_open_container(reply, 'a', "(ssssuu)");
591 HASHMAP_FOREACH(inhibitor, m->inhibitors, i) {
593 r = sd_bus_message_append(reply, "(ssssuu)",
594 strempty(inhibit_what_to_string(inhibitor->what)),
595 strempty(inhibitor->who),
596 strempty(inhibitor->why),
597 strempty(inhibit_mode_to_string(inhibitor->mode)),
598 (uint32_t) inhibitor->uid,
599 (uint32_t) inhibitor->pid);
604 r = sd_bus_message_close_container(reply);
608 return sd_bus_send(NULL, reply, NULL);
611 static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
612 const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop;
613 uint32_t audit_id = 0;
614 _cleanup_free_ char *id = NULL;
615 Session *session = NULL;
616 Manager *m = userdata;
630 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
631 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
633 r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host);
637 if (!uid_is_valid(uid))
638 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID");
639 if (leader < 0 || leader == 1)
640 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID");
643 t = _SESSION_TYPE_INVALID;
645 t = session_type_from_string(type);
647 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type);
651 c = _SESSION_CLASS_INVALID;
653 c = session_class_from_string(class);
655 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class);
658 if (isempty(desktop))
661 if (!string_is_safe(desktop))
662 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop);
668 seat = hashmap_get(m->seats, cseat);
670 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat);
673 if (tty_is_vc(tty)) {
678 else if (seat != m->seat0)
679 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);
681 v = vtnr_from_tty(tty);
683 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty);
687 else if (vtnr != (uint32_t) v)
688 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match");
690 } else if (tty_is_console(tty)) {
694 else if (seat != m->seat0)
695 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0");
698 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0");
702 if (seat_has_vts(seat)) {
703 if (!vtnr || vtnr > 63)
704 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range");
707 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0");
711 r = sd_bus_message_enter_container(message, 'a', "(sv)");
715 if (t == _SESSION_TYPE_INVALID) {
716 if (!isempty(display))
718 else if (!isempty(tty))
721 t = SESSION_UNSPECIFIED;
724 if (c == _SESSION_CLASS_INVALID) {
725 if (t == SESSION_UNSPECIFIED)
726 c = SESSION_BACKGROUND;
732 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
734 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
738 r = sd_bus_creds_get_pid(creds, (pid_t*) &leader);
743 r = manager_get_session_by_pid(m, leader, NULL);
745 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session");
748 * Old gdm and lightdm start the user-session on the same VT as
749 * the greeter session. But they destroy the greeter session
750 * after the user-session and want the user-session to take
751 * over the VT. We need to support this for
752 * backwards-compatibility, so make sure we allow new sessions
753 * on a VT that a greeter is running on. Furthermore, to allow
754 * re-logins, we have to allow a greeter to take over a used VT for
755 * the exact same reasons.
757 if (c != SESSION_GREETER &&
759 vtnr < m->seat0->position_count &&
760 m->seat0->positions[vtnr] &&
761 m->seat0->positions[vtnr]->class != SESSION_GREETER)
762 return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session");
764 if (hashmap_size(m->sessions) >= m->sessions_max)
765 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max);
767 audit_session_from_pid(leader, &audit_id);
769 /* Keep our session IDs and the audit session IDs in sync */
771 if (asprintf(&id, "%"PRIu32, audit_id) < 0)
774 /* Wut? There's already a session by this name and we
775 * didn't find it above? Weird, then let's not trust
776 * the audit data and let's better register a new
778 if (hashmap_get(m->sessions, id)) {
779 log_warning("Existing logind session ID %s used by new audit session, ignoring", id);
790 if (asprintf(&id, "c%lu", ++m->session_counter) < 0)
793 } while (hashmap_get(m->sessions, id));
796 r = manager_add_user_by_uid(m, uid, &user);
800 r = manager_add_session(m, id, &session);
804 session_set_user(session, user);
806 session->leader = leader;
807 session->audit_id = audit_id;
810 session->remote = remote;
811 session->vtnr = vtnr;
814 session->tty = strdup(tty);
821 if (!isempty(display)) {
822 session->display = strdup(display);
823 if (!session->display) {
829 if (!isempty(remote_user)) {
830 session->remote_user = strdup(remote_user);
831 if (!session->remote_user) {
837 if (!isempty(remote_host)) {
838 session->remote_host = strdup(remote_host);
839 if (!session->remote_host) {
845 if (!isempty(service)) {
846 session->service = strdup(service);
847 if (!session->service) {
853 if (!isempty(desktop)) {
854 session->desktop = strdup(desktop);
855 if (!session->desktop) {
862 r = seat_attach_session(seat, session);
867 r = session_start(session);
871 session->create_message = sd_bus_message_ref(message);
873 #if 0 /// UNNEEDED by elogind
874 /* Now, let's wait until the slice unit and stuff got
875 * created. We send the reply back from
876 * session_send_create_reply(). */
878 /* We reply directly. */
880 r = session_send_create_reply(session, NULL);
889 session_add_to_gc_queue(session);
892 user_add_to_gc_queue(user);
897 static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
898 Manager *m = userdata;
906 r = sd_bus_message_read(message, "s", &name);
910 r = manager_get_session_from_creds(m, message, name, error, &session);
914 r = session_release(session);
918 session_add_to_gc_queue(session);
920 return sd_bus_reply_method_return(message, NULL);
923 static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
924 Manager *m = userdata;
932 r = sd_bus_message_read(message, "s", &name);
936 r = manager_get_session_from_creds(m, message, name, error, &session);
940 return bus_session_method_activate(message, session, error);
943 static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
944 const char *session_name, *seat_name;
945 Manager *m = userdata;
953 /* Same as ActivateSession() but refuses to work if
954 * the seat doesn't match */
956 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
960 r = manager_get_session_from_creds(m, message, session_name, error, &session);
964 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
968 if (session->seat != seat)
969 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
971 r = session_activate(session);
975 return sd_bus_reply_method_return(message, NULL);
978 static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
979 Manager *m = userdata;
987 r = sd_bus_message_read(message, "s", &name);
991 r = manager_get_session_from_creds(m, message, name, error, &session);
995 return bus_session_method_lock(message, session, error);
998 static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
999 Manager *m = userdata;
1005 r = bus_verify_polkit_async(
1008 "org.freedesktop.login1.lock-sessions",
1012 &m->polkit_registry,
1017 return 1; /* Will call us back */
1019 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
1023 return sd_bus_reply_method_return(message, NULL);
1026 static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1028 Manager *m = userdata;
1035 r = sd_bus_message_read(message, "s", &name);
1039 r = manager_get_session_from_creds(m, message, name, error, &session);
1043 return bus_session_method_kill(message, session, error);
1046 static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1047 Manager *m = userdata;
1055 r = sd_bus_message_read(message, "u", &uid);
1059 r = manager_get_user_from_creds(m, message, uid, error, &user);
1063 return bus_user_method_kill(message, user, error);
1066 static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1067 Manager *m = userdata;
1075 r = sd_bus_message_read(message, "s", &name);
1079 r = manager_get_session_from_creds(m, message, name, error, &session);
1083 return bus_session_method_terminate(message, session, error);
1086 static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1087 Manager *m = userdata;
1095 r = sd_bus_message_read(message, "u", &uid);
1099 r = manager_get_user_from_creds(m, message, uid, error, &user);
1103 return bus_user_method_terminate(message, user, error);
1106 static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1107 Manager *m = userdata;
1115 r = sd_bus_message_read(message, "s", &name);
1119 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1123 return bus_seat_method_terminate(message, seat, error);
1126 static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1127 _cleanup_free_ char *cc = NULL;
1128 Manager *m = userdata;
1129 int r, b, interactive;
1138 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1142 if (uid == UID_INVALID) {
1143 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1145 /* Note that we get the owner UID of the session, not the actual client UID here! */
1146 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1150 r = sd_bus_creds_get_owner_uid(creds, &uid);
1156 } else if (!uid_is_valid(uid))
1162 return errno > 0 ? -errno : -ENOENT;
1164 r = bus_verify_polkit_async(
1167 self ? "org.freedesktop.login1.set-self-linger" : "org.freedesktop.login1.set-user-linger",
1171 &m->polkit_registry,
1176 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1178 mkdir_p_label("/var/lib/systemd", 0755);
1180 r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0);
1184 cc = cescape(pw->pw_name);
1188 path = strjoina("/var/lib/systemd/linger/", cc);
1196 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1203 if (r < 0 && errno != ENOENT)
1206 u = hashmap_get(m->users, UID_TO_PTR(uid));
1208 user_add_to_gc_queue(u);
1211 return sd_bus_reply_method_return(message, NULL);
1214 static int trigger_device(Manager *m, struct udev_device *d) {
1215 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
1216 struct udev_list_entry *first, *item;
1221 e = udev_enumerate_new(m->udev);
1226 r = udev_enumerate_add_match_parent(e, d);
1231 r = udev_enumerate_scan_devices(e);
1235 first = udev_enumerate_get_list_entry(e);
1236 udev_list_entry_foreach(item, first) {
1237 _cleanup_free_ char *t = NULL;
1240 p = udev_list_entry_get_name(item);
1242 t = strappend(p, "/uevent");
1246 write_string_file(t, "change", WRITE_STRING_FILE_CREATE);
1252 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1253 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
1254 _cleanup_free_ char *rule = NULL, *file = NULL;
1255 const char *id_for_seat;
1262 d = udev_device_new_from_syspath(m->udev, sysfs);
1266 if (!udev_device_has_tag(d, "seat"))
1269 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
1273 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1276 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1279 mkdir_p_label("/etc/udev/rules.d", 0755);
1280 r = write_string_file_atomic_label(file, rule);
1284 return trigger_device(m, d);
1287 static int flush_devices(Manager *m) {
1288 _cleanup_closedir_ DIR *d;
1292 d = opendir("/etc/udev/rules.d");
1294 if (errno != ENOENT)
1295 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1299 while ((de = readdir(d))) {
1301 if (!dirent_is_file(de))
1304 if (!startswith(de->d_name, "72-seat-"))
1307 if (!endswith(de->d_name, ".rules"))
1310 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1311 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1315 return trigger_device(m, NULL);
1318 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1319 const char *sysfs, *seat;
1320 Manager *m = userdata;
1326 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1330 if (!path_startswith(sysfs, "/sys"))
1331 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1333 if (!seat_name_is_valid(seat))
1334 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1336 r = bus_verify_polkit_async(
1339 "org.freedesktop.login1.attach-device",
1343 &m->polkit_registry,
1348 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1350 r = attach_device(m, seat, sysfs);
1354 return sd_bus_reply_method_return(message, NULL);
1357 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1358 Manager *m = userdata;
1364 r = sd_bus_message_read(message, "b", &interactive);
1368 r = bus_verify_polkit_async(
1371 "org.freedesktop.login1.flush-devices",
1375 &m->polkit_registry,
1380 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1382 r = flush_devices(m);
1386 return sd_bus_reply_method_return(message, NULL);
1389 static int have_multiple_sessions(
1398 /* Check for other users' sessions. Greeter sessions do not
1399 * count, and non-login sessions do not count either. */
1400 HASHMAP_FOREACH(session, m->sessions, i)
1401 if (session->class == SESSION_USER &&
1402 session->user->uid != uid)
1408 static int bus_manager_log_shutdown(
1411 #if 0 /// elogind does not support systemd units
1412 const char *unit_name) {
1419 if (w != INHIBIT_SHUTDOWN)
1422 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1423 p = "MESSAGE=System is powering down";
1424 q = "SHUTDOWN=power-off";
1425 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1426 p = "MESSAGE=System is halting";
1427 q = "SHUTDOWN=halt";
1428 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1429 p = "MESSAGE=System is rebooting";
1430 q = "SHUTDOWN=reboot";
1431 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1432 p = "MESSAGE=System is rebooting with kexec";
1433 q = "SHUTDOWN=kexec";
1435 p = "MESSAGE=System is shutting down";
1439 HandleAction action) {
1445 if (w != INHIBIT_SHUTDOWN)
1449 case HANDLE_POWEROFF:
1450 p = "MESSAGE=System is powering down.";
1451 q = "SHUTDOWN=power-off";
1454 p = "MESSAGE=System is halting.";
1455 q = "SHUTDOWN=halt";
1458 p = "MESSAGE=System is rebooting.";
1459 q = "SHUTDOWN=reboot";
1462 p = "MESSAGE=System is rebooting with kexec.";
1463 q = "SHUTDOWN=kexec";
1466 p = "MESSAGE=System is shutting down.";
1470 if (isempty(m->wall_message))
1471 p = strjoina(p, ".");
1473 p = strjoina(p, " (", m->wall_message, ").");
1475 return log_struct(LOG_NOTICE,
1476 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
1482 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1483 Manager *m = userdata;
1488 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1492 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1497 if (until <= now(CLOCK_MONOTONIC))
1500 /* We want to ignore the lid switch for a while after each
1501 * suspend, and after boot-up. Hence let's install a timer for
1502 * this. As long as the event source exists we ignore the lid
1505 if (m->lid_switch_ignore_event_source) {
1508 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1515 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1517 r = sd_event_add_time(
1519 &m->lid_switch_ignore_event_source,
1522 lid_switch_ignore_handler, m);
1527 static void reset_scheduled_shutdown(Manager *m) {
1528 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
1529 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
1530 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
1531 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
1532 m->scheduled_shutdown_timeout = 0;
1533 m->shutdown_dry_run = false;
1535 if (m->unlink_nologin) {
1536 (void) unlink("/run/nologin");
1537 m->unlink_nologin = false;
1541 static int execute_shutdown_or_sleep(
1544 HandleAction action,
1545 sd_bus_error *error) {
1547 #if 0 /// elogind does not need these, we do it ourselves
1548 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1556 assert(w < _INHIBIT_WHAT_MAX);
1558 bus_manager_log_shutdown(m, w, action);
1560 #if 0 /// elogind does it directly without depending on systemd running the system
1561 if (m->shutdown_dry_run) {
1562 log_info("Running in dry run, suppressing action.");
1563 reset_scheduled_shutdown(m);
1565 r = sd_bus_call_method(
1567 "org.freedesktop.systemd1",
1568 "/org/freedesktop/systemd1",
1569 "org.freedesktop.systemd1.Manager",
1573 "ss", unit_name, "replace-irreversibly");
1577 r = sd_bus_message_read(reply, "o", &p);
1585 m->action_unit = unit_name;
1586 free(m->action_job);
1591 r = shutdown_or_sleep(m, action);
1593 /* no more pending actions, whether this failed or not */
1594 m->pending_action = HANDLE_IGNORE;
1600 /* Make sure the lid switch is ignored for a while */
1601 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1606 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1608 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1609 Inhibitor *offending = NULL;
1614 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
1617 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1618 _cleanup_free_ char *comm = NULL, *u = NULL;
1623 (void) get_process_comm(offending->pid, &comm);
1624 u = uid_to_name(offending->uid);
1626 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1627 offending->uid, strna(u),
1628 offending->pid, strna(comm));
1631 /* Actually do the operation */
1632 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
1634 log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
1636 manager->pending_action = HANDLE_IGNORE;
1637 manager->action_what = 0;
1644 static int manager_inhibit_timeout_handler(
1649 Manager *manager = userdata;
1653 assert(manager->inhibit_timeout_source == s);
1655 r = manager_dispatch_delayed(manager, true);
1656 return (r < 0) ? r : 0;
1659 static int delay_shutdown_or_sleep(
1662 HandleAction action) {
1669 assert(w < _INHIBIT_WHAT_MAX);
1671 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1673 if (m->inhibit_timeout_source) {
1674 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1676 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1678 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1680 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1682 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1683 timeout_val, 0, manager_inhibit_timeout_handler, m);
1688 m->pending_action = action;
1694 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1696 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1697 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1698 [INHIBIT_SLEEP] = "PrepareForSleep"
1701 int active = _active;
1705 assert(w < _INHIBIT_WHAT_MAX);
1706 assert(signal_name[w]);
1708 return sd_bus_emit_signal(m->bus,
1709 "/org/freedesktop/login1",
1710 "org.freedesktop.login1.Manager",
1716 int bus_manager_shutdown_or_sleep_now_or_later(
1718 HandleAction action,
1720 sd_bus_error *error) {
1727 assert(w <= _INHIBIT_WHAT_MAX);
1729 /* Tell everybody to prepare for shutdown/sleep */
1730 send_prepare_for(m, w, true);
1733 m->inhibit_delay_max > 0 &&
1734 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1737 /* Shutdown is delayed, keep in mind what we
1738 * want to do, and start a timeout */
1739 r = delay_shutdown_or_sleep(m, w, action);
1741 /* Shutdown is not delayed, execute it
1743 r = execute_shutdown_or_sleep(m, w, action, error);
1748 static int verify_shutdown_creds(
1750 sd_bus_message *message,
1754 const char *action_multiple_sessions,
1755 const char *action_ignore_inhibit,
1756 sd_bus_error *error) {
1758 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1759 bool multiple_sessions, blocked;
1766 assert(w <= _INHIBIT_WHAT_MAX);
1768 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1772 r = sd_bus_creds_get_euid(creds, &uid);
1776 r = have_multiple_sessions(m, uid);
1780 multiple_sessions = r > 0;
1781 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1783 if (multiple_sessions && action_multiple_sessions) {
1784 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1788 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1791 if (blocked && action_ignore_inhibit) {
1792 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1796 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1799 if (!multiple_sessions && !blocked && action) {
1800 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1804 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1810 static int method_do_shutdown_or_sleep(
1812 sd_bus_message *message,
1813 HandleAction sleep_action,
1816 const char *action_multiple_sessions,
1817 const char *action_ignore_inhibit,
1818 const char *sleep_verb,
1819 sd_bus_error *error) {
1826 assert(w <= _INHIBIT_WHAT_MAX);
1828 r = sd_bus_message_read(message, "b", &interactive);
1832 /* Don't allow multiple jobs being executed at the same time */
1834 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1837 r = can_sleep(m, sleep_verb);
1842 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
1845 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1846 action_ignore_inhibit, error);
1850 r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
1854 return sd_bus_reply_method_return(message, NULL);
1857 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1858 Manager *m = userdata;
1860 return method_do_shutdown_or_sleep(
1864 "org.freedesktop.login1.power-off",
1865 "org.freedesktop.login1.power-off-multiple-sessions",
1866 "org.freedesktop.login1.power-off-ignore-inhibit",
1871 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1872 Manager *m = userdata;
1874 return method_do_shutdown_or_sleep(
1878 "org.freedesktop.login1.reboot",
1879 "org.freedesktop.login1.reboot-multiple-sessions",
1880 "org.freedesktop.login1.reboot-ignore-inhibit",
1885 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1886 Manager *m = userdata;
1888 return method_do_shutdown_or_sleep(
1892 "org.freedesktop.login1.suspend",
1893 "org.freedesktop.login1.suspend-multiple-sessions",
1894 "org.freedesktop.login1.suspend-ignore-inhibit",
1899 static int nologin_timeout_handler(
1904 Manager *m = userdata;
1907 log_info("Creating /run/nologin, blocking further logins...");
1909 r = write_string_file_atomic_label("/run/nologin", "System is going down.");
1911 log_error_errno(r, "Failed to create /run/nologin: %m");
1913 m->unlink_nologin = true;
1918 static int update_schedule_file(Manager *m) {
1919 _cleanup_free_ char *temp_path = NULL;
1920 _cleanup_fclose_ FILE *f = NULL;
1925 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0);
1927 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
1929 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
1931 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
1933 (void) fchmod(fileno(f), 0644);
1939 m->scheduled_shutdown_timeout,
1940 m->enable_wall_messages,
1941 m->scheduled_shutdown_type);
1943 if (!isempty(m->wall_message)) {
1944 _cleanup_free_ char *t;
1946 t = cescape(m->wall_message);
1952 fprintf(f, "WALL_MESSAGE=%s\n", t);
1955 r = fflush_and_check(f);
1959 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
1967 (void) unlink(temp_path);
1968 (void) unlink("/run/systemd/shutdown/scheduled");
1970 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
1973 static int manager_scheduled_shutdown_handler(
1978 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1979 Manager *m = userdata;
1980 HandleAction action;
1985 if (isempty(m->scheduled_shutdown_type))
1988 if (streq(m->scheduled_shutdown_type, "halt"))
1989 action = HANDLE_HALT;
1990 else if (streq(m->scheduled_shutdown_type, "poweroff"))
1991 action = HANDLE_POWEROFF;
1993 action = HANDLE_REBOOT;
1995 r = execute_shutdown_or_sleep(m, 0, action, &error);
1997 return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type);
2002 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2003 Manager *m = userdata;
2004 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2005 const char *action_multiple_sessions = NULL;
2006 const char *action_ignore_inhibit = NULL;
2007 const char *action = NULL;
2015 r = sd_bus_message_read(message, "st", &type, &elapse);
2019 if (startswith(type, "dry-")) {
2021 m->shutdown_dry_run = true;
2024 if (streq(type, "reboot")) {
2025 action = "org.freedesktop.login1.reboot";
2026 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2027 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2028 } else if (streq(type, "halt")) {
2029 action = "org.freedesktop.login1.halt";
2030 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2031 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2032 } else if (streq(type, "poweroff")) {
2033 action = "org.freedesktop.login1.power-off";
2034 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2035 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2037 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2039 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2040 action, action_multiple_sessions, action_ignore_inhibit, error);
2044 if (m->scheduled_shutdown_timeout_source) {
2045 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2047 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2049 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2051 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2053 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2054 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2056 return log_error_errno(r, "sd_event_add_time() failed: %m");
2059 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2061 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2065 if (m->nologin_timeout_source) {
2066 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2068 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2070 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2072 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2074 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2075 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2077 return log_error_errno(r, "sd_event_add_time() failed: %m");
2080 m->scheduled_shutdown_timeout = elapse;
2082 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2084 const char *tty = NULL;
2086 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2087 (void) sd_bus_creds_get_tty(creds, &tty);
2089 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2091 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2096 #if 0 /// elogind does not support utmp-wtmp
2097 r = manager_setup_wall_message_timer(m);
2102 if (!isempty(type)) {
2103 r = update_schedule_file(m);
2107 (void) unlink("/run/systemd/shutdown/scheduled");
2109 return sd_bus_reply_method_return(message, NULL);
2112 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2113 Manager *m = userdata;
2119 cancelled = m->scheduled_shutdown_type != NULL;
2120 reset_scheduled_shutdown(m);
2122 #if 0 /// elogind does not support utmp-wtmp
2124 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2125 const char *tty = NULL;
2129 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2131 (void) sd_bus_creds_get_uid(creds, &uid);
2132 (void) sd_bus_creds_get_tty(creds, &tty);
2135 utmp_wall("The system shutdown has been cancelled",
2136 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2140 return sd_bus_reply_method_return(message, "b", cancelled);
2143 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2144 Manager *m = userdata;
2146 return method_do_shutdown_or_sleep(
2150 "org.freedesktop.login1.hibernate",
2151 "org.freedesktop.login1.hibernate-multiple-sessions",
2152 "org.freedesktop.login1.hibernate-ignore-inhibit",
2157 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2158 Manager *m = userdata;
2160 return method_do_shutdown_or_sleep(
2162 HANDLE_HYBRID_SLEEP,
2164 "org.freedesktop.login1.hibernate",
2165 "org.freedesktop.login1.hibernate-multiple-sessions",
2166 "org.freedesktop.login1.hibernate-ignore-inhibit",
2171 static int method_can_shutdown_or_sleep(
2173 sd_bus_message *message,
2176 const char *action_multiple_sessions,
2177 const char *action_ignore_inhibit,
2178 const char *sleep_verb,
2179 sd_bus_error *error) {
2181 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2182 bool multiple_sessions, challenge, blocked;
2183 const char *result = NULL;
2190 assert(w <= _INHIBIT_WHAT_MAX);
2192 assert(action_multiple_sessions);
2193 assert(action_ignore_inhibit);
2196 r = can_sleep(m, sleep_verb);
2200 return sd_bus_reply_method_return(message, "s", "na");
2203 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2207 r = sd_bus_creds_get_euid(creds, &uid);
2211 r = have_multiple_sessions(m, uid);
2215 multiple_sessions = r > 0;
2216 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2218 if (multiple_sessions) {
2219 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2226 result = "challenge";
2232 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2236 if (r > 0 && !result)
2238 else if (challenge && (!result || streq(result, "yes")))
2239 result = "challenge";
2244 if (!multiple_sessions && !blocked) {
2245 /* If neither inhibit nor multiple sessions
2246 * apply then just check the normal policy */
2248 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2255 result = "challenge";
2260 return sd_bus_reply_method_return(message, "s", result);
2263 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2264 Manager *m = userdata;
2266 return method_can_shutdown_or_sleep(
2269 "org.freedesktop.login1.power-off",
2270 "org.freedesktop.login1.power-off-multiple-sessions",
2271 "org.freedesktop.login1.power-off-ignore-inhibit",
2276 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2277 Manager *m = userdata;
2279 return method_can_shutdown_or_sleep(
2282 "org.freedesktop.login1.reboot",
2283 "org.freedesktop.login1.reboot-multiple-sessions",
2284 "org.freedesktop.login1.reboot-ignore-inhibit",
2289 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2290 Manager *m = userdata;
2292 return method_can_shutdown_or_sleep(
2295 "org.freedesktop.login1.suspend",
2296 "org.freedesktop.login1.suspend-multiple-sessions",
2297 "org.freedesktop.login1.suspend-ignore-inhibit",
2302 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2303 Manager *m = userdata;
2305 return method_can_shutdown_or_sleep(
2308 "org.freedesktop.login1.hibernate",
2309 "org.freedesktop.login1.hibernate-multiple-sessions",
2310 "org.freedesktop.login1.hibernate-ignore-inhibit",
2315 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2316 Manager *m = userdata;
2318 return method_can_shutdown_or_sleep(
2321 "org.freedesktop.login1.hibernate",
2322 "org.freedesktop.login1.hibernate-multiple-sessions",
2323 "org.freedesktop.login1.hibernate-ignore-inhibit",
2328 static int property_get_reboot_to_firmware_setup(
2331 const char *interface,
2332 const char *property,
2333 sd_bus_message *reply,
2335 sd_bus_error *error) {
2336 #if 0 /// elogind does not support EFI
2343 r = efi_get_reboot_to_firmware();
2344 if (r < 0 && r != -EOPNOTSUPP)
2347 return sd_bus_message_append(reply, "b", r > 0);
2349 return sd_bus_message_append(reply, "b", false);
2353 static int method_set_reboot_to_firmware_setup(
2354 sd_bus_message *message,
2356 sd_bus_error *error) {
2359 Manager *m = userdata;
2364 r = sd_bus_message_read(message, "b", &b);
2368 r = bus_verify_polkit_async(message,
2370 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2374 &m->polkit_registry,
2379 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2381 #if 0 /// elogind does not support EFI
2382 r = efi_set_reboot_to_firmware(b);
2387 return sd_bus_reply_method_return(message, NULL);
2390 static int method_can_reboot_to_firmware_setup(
2391 sd_bus_message *message,
2393 sd_bus_error *error) {
2395 #if 0 /// elogind does not support EFI
2399 Manager *m = userdata;
2404 r = efi_reboot_to_firmware_supported();
2405 if (r == -EOPNOTSUPP)
2406 return sd_bus_reply_method_return(message, "s", "na");
2410 r = bus_test_polkit(message,
2412 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2423 result = "challenge";
2427 return sd_bus_reply_method_return(message, "s", result);
2429 return sd_bus_reply_method_return(message, "s", "no");
2433 static int method_set_wall_message(
2434 sd_bus_message *message,
2436 sd_bus_error *error) {
2439 Manager *m = userdata;
2441 int enable_wall_messages;
2446 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2450 r = bus_verify_polkit_async(message,
2452 "org.freedesktop.login1.set-wall-message",
2456 &m->polkit_registry,
2461 return 1; /* Will call us back */
2463 if (isempty(wall_message))
2464 m->wall_message = mfree(m->wall_message);
2466 r = free_and_strdup(&m->wall_message, wall_message);
2471 m->enable_wall_messages = enable_wall_messages;
2473 return sd_bus_reply_method_return(message, NULL);
2476 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2477 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2478 const char *who, *why, *what, *mode;
2479 _cleanup_free_ char *id = NULL;
2480 _cleanup_close_ int fifo_fd = -1;
2481 Manager *m = userdata;
2482 Inhibitor *i = NULL;
2492 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2496 w = inhibit_what_from_string(what);
2498 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2500 mm = inhibit_mode_from_string(mode);
2502 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2504 /* Delay is only supported for shutdown/sleep */
2505 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2506 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2508 /* Don't allow taking delay locks while we are already
2509 * executing the operation. We shouldn't create the impression
2510 * that the lock was successful if the machine is about to go
2511 * down/suspend any moment. */
2512 if (m->action_what & w)
2513 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2515 r = bus_verify_polkit_async(
2518 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2519 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2520 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2521 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2522 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2523 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2524 "org.freedesktop.login1.inhibit-handle-lid-switch",
2528 &m->polkit_registry,
2533 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2535 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2539 r = sd_bus_creds_get_euid(creds, &uid);
2543 r = sd_bus_creds_get_pid(creds, &pid);
2547 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2548 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2553 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2556 } while (hashmap_get(m->inhibitors, id));
2558 r = manager_add_inhibitor(m, id, &i);
2566 i->why = strdup(why);
2567 i->who = strdup(who);
2569 if (!i->why || !i->who) {
2574 fifo_fd = inhibitor_create_fifo(i);
2582 return sd_bus_reply_method_return(message, "h", fifo_fd);
2591 const sd_bus_vtable manager_vtable[] = {
2592 SD_BUS_VTABLE_START(0),
2594 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2595 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2597 // SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2598 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2599 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2600 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2601 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2602 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2603 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2604 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2605 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2606 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2607 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2608 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2609 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2610 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2611 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2612 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2613 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2614 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2615 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2616 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2617 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2618 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2619 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2620 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2621 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", bus_property_get_size, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2622 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2623 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_current_inhibitors, 0, 0),
2624 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2625 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_current_sessions, 0, 0),
2626 SD_BUS_PROPERTY("UserTasksMax", "t", NULL, offsetof(Manager, user_tasks_max), SD_BUS_VTABLE_PROPERTY_CONST),
2628 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2629 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2630 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2631 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2632 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2633 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2634 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2635 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2636 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2637 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2638 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2639 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2640 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2641 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2642 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2643 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2644 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2645 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2646 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2647 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2648 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2649 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2650 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2651 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2652 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2653 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2654 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2655 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2656 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2657 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2658 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2659 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2660 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2661 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2662 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2663 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2664 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2665 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2666 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2667 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2668 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2670 SD_BUS_SIGNAL("SessionNew", "so", 0),
2671 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2672 SD_BUS_SIGNAL("UserNew", "uo", 0),
2673 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2674 SD_BUS_SIGNAL("SeatNew", "so", 0),
2675 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2676 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2677 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2682 #if 0 /// UNNEEDED by elogind
2683 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2692 if (streq(result, "done"))
2693 r = session_send_create_reply(s, NULL);
2695 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2697 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2698 r = session_send_create_reply(s, &e);
2704 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2705 const char *path, *result, *unit;
2706 Manager *m = userdata;
2715 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2717 bus_log_parse_error(r);
2721 if (m->action_job && streq(m->action_job, path)) {
2722 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2724 /* Tell people that they now may take a lock again */
2725 send_prepare_for(m, m->action_what, false);
2727 m->action_job = mfree(m->action_job);
2728 m->action_unit = NULL;
2733 session = hashmap_get(m->session_units, unit);
2734 if (session && streq_ptr(path, session->scope_job)) {
2735 session->scope_job = mfree(session->scope_job);
2736 session_jobs_reply(session, unit, result);
2738 session_save(session);
2739 user_save(session->user);
2740 session_add_to_gc_queue(session);
2743 user = hashmap_get(m->user_units, unit);
2745 (streq_ptr(path, user->service_job) ||
2746 streq_ptr(path, user->slice_job))) {
2748 if (streq_ptr(path, user->service_job))
2749 user->service_job = mfree(user->service_job);
2751 if (streq_ptr(path, user->slice_job))
2752 user->slice_job = mfree(user->slice_job);
2754 LIST_FOREACH(sessions_by_user, session, user->sessions)
2755 session_jobs_reply(session, unit, result);
2758 user_add_to_gc_queue(user);
2764 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2765 const char *path, *unit;
2766 Manager *m = userdata;
2774 r = sd_bus_message_read(message, "so", &unit, &path);
2776 bus_log_parse_error(r);
2780 session = hashmap_get(m->session_units, unit);
2782 session_add_to_gc_queue(session);
2784 user = hashmap_get(m->user_units, unit);
2786 user_add_to_gc_queue(user);
2791 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2792 _cleanup_free_ char *unit = NULL;
2793 Manager *m = userdata;
2802 path = sd_bus_message_get_path(message);
2806 r = unit_name_from_dbus_path(path, &unit);
2807 if (r == -EINVAL) /* not a unit */
2814 session = hashmap_get(m->session_units, unit);
2816 session_add_to_gc_queue(session);
2818 user = hashmap_get(m->user_units, unit);
2820 user_add_to_gc_queue(user);
2825 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2826 Manager *m = userdata;
2834 r = sd_bus_message_read(message, "b", &b);
2836 bus_log_parse_error(r);
2843 /* systemd finished reloading, let's recheck all our sessions */
2844 log_debug("System manager has been reloaded, rechecking sessions...");
2846 HASHMAP_FOREACH(session, m->sessions, i)
2847 session_add_to_gc_queue(session);
2853 int manager_send_changed(Manager *manager, const char *property, ...) {
2858 l = strv_from_stdarg_alloca(property);
2860 return sd_bus_emit_properties_changed_strv(
2862 "/org/freedesktop/login1",
2863 "org.freedesktop.login1.Manager",
2867 #if 0 /// UNNEEDED by elogind
2868 static int strdup_job(sd_bus_message *reply, char **job) {
2873 r = sd_bus_message_read(reply, "o", &j);
2885 int manager_start_slice(
2888 const char *description,
2892 sd_bus_error *error,
2895 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
2901 r = sd_bus_message_new_method_call(
2904 "org.freedesktop.systemd1",
2905 "/org/freedesktop/systemd1",
2906 "org.freedesktop.systemd1.Manager",
2907 "StartTransientUnit");
2911 r = sd_bus_message_append(m, "ss", strempty(slice), "fail");
2915 r = sd_bus_message_open_container(m, 'a', "(sv)");
2919 if (!isempty(description)) {
2920 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
2925 if (!isempty(after)) {
2926 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
2931 if (!isempty(after2)) {
2932 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
2937 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", tasks_max);
2941 r = sd_bus_message_close_container(m);
2945 r = sd_bus_message_append(m, "a(sa(sv))", 0);
2949 r = sd_bus_call(manager->bus, m, 0, error, &reply);
2954 return strdup_job(reply, job);
2958 int manager_start_scope(
2963 const char *description,
2967 sd_bus_error *error,
2970 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
2977 r = sd_bus_message_new_method_call(
2980 "org.freedesktop.systemd1",
2981 "/org/freedesktop/systemd1",
2982 "org.freedesktop.systemd1.Manager",
2983 "StartTransientUnit");
2987 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
2991 r = sd_bus_message_open_container(m, 'a', "(sv)");
2995 if (!isempty(slice)) {
2996 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
3001 if (!isempty(description)) {
3002 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3007 if (!isempty(after)) {
3008 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3013 if (!isempty(after2)) {
3014 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3019 /* cgroup empty notification is not available in containers
3020 * currently. To make this less problematic, let's shorten the
3021 * stop timeout for sessions, so that we don't wait
3024 /* Make sure that the session shells are terminated with
3025 * SIGHUP since bash and friends tend to ignore SIGTERM */
3026 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
3030 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3034 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", tasks_max);
3038 r = sd_bus_message_close_container(m);
3042 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3046 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3051 return strdup_job(reply, job);
3055 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3056 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3062 r = sd_bus_call_method(
3064 "org.freedesktop.systemd1",
3065 "/org/freedesktop/systemd1",
3066 "org.freedesktop.systemd1.Manager",
3070 "ss", unit, "replace");
3075 return strdup_job(reply, job);
3079 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3080 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3086 r = sd_bus_call_method(
3088 "org.freedesktop.systemd1",
3089 "/org/freedesktop/systemd1",
3090 "org.freedesktop.systemd1.Manager",
3094 "ss", unit, "fail");
3096 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3097 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3102 sd_bus_error_free(error);
3110 return strdup_job(reply, job);
3114 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3115 _cleanup_free_ char *path = NULL;
3121 path = unit_dbus_path_from_name(scope);
3125 r = sd_bus_call_method(
3127 "org.freedesktop.systemd1",
3129 "org.freedesktop.systemd1.Scope",
3135 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3136 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3137 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3138 sd_bus_error_free(error);
3148 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3152 return sd_bus_call_method(
3154 "org.freedesktop.systemd1",
3155 "/org/freedesktop/systemd1",
3156 "org.freedesktop.systemd1.Manager",
3160 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3163 int manager_unit_is_active(Manager *manager, const char *unit) {
3164 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3165 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3166 _cleanup_free_ char *path = NULL;
3173 path = unit_dbus_path_from_name(unit);
3177 r = sd_bus_get_property(
3179 "org.freedesktop.systemd1",
3181 "org.freedesktop.systemd1.Unit",
3187 /* systemd might have droppped off momentarily, let's
3188 * not make this an error */
3189 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3190 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3193 /* If the unit is already unloaded then it's not
3195 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3196 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3202 r = sd_bus_message_read(reply, "s", &state);
3206 return !streq(state, "inactive") && !streq(state, "failed");
3209 int manager_job_is_active(Manager *manager, const char *path) {
3210 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3211 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3217 r = sd_bus_get_property(
3219 "org.freedesktop.systemd1",
3221 "org.freedesktop.systemd1.Job",
3227 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3228 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3231 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3237 /* We don't actually care about the state really. The fact
3238 * that we could read the job state is enough for us */