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 /* Here upstream systemd starts cgroups and the user systemd,
874 and arranges to reply asynchronously. We reply
877 r = session_send_create_reply(session, NULL);
885 session_add_to_gc_queue(session);
888 user_add_to_gc_queue(user);
893 static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
894 Manager *m = userdata;
902 r = sd_bus_message_read(message, "s", &name);
906 r = manager_get_session_from_creds(m, message, name, error, &session);
910 r = session_release(session);
914 session_add_to_gc_queue(session);
916 return sd_bus_reply_method_return(message, NULL);
919 static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
920 Manager *m = userdata;
928 r = sd_bus_message_read(message, "s", &name);
932 r = manager_get_session_from_creds(m, message, name, error, &session);
936 return bus_session_method_activate(message, session, error);
939 static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
940 const char *session_name, *seat_name;
941 Manager *m = userdata;
949 /* Same as ActivateSession() but refuses to work if
950 * the seat doesn't match */
952 r = sd_bus_message_read(message, "ss", &session_name, &seat_name);
956 r = manager_get_session_from_creds(m, message, session_name, error, &session);
960 r = manager_get_seat_from_creds(m, message, seat_name, error, &seat);
964 if (session->seat != seat)
965 return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name);
967 r = session_activate(session);
971 return sd_bus_reply_method_return(message, NULL);
974 static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
975 Manager *m = userdata;
983 r = sd_bus_message_read(message, "s", &name);
987 r = manager_get_session_from_creds(m, message, name, error, &session);
991 return bus_session_method_lock(message, session, error);
994 static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) {
995 Manager *m = userdata;
1001 r = bus_verify_polkit_async(
1004 "org.freedesktop.login1.lock-sessions",
1008 &m->polkit_registry,
1013 return 1; /* Will call us back */
1015 r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions"));
1019 return sd_bus_reply_method_return(message, NULL);
1022 static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1024 Manager *m = userdata;
1031 r = sd_bus_message_read(message, "s", &name);
1035 r = manager_get_session_from_creds(m, message, name, error, &session);
1039 return bus_session_method_kill(message, session, error);
1042 static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1043 Manager *m = userdata;
1051 r = sd_bus_message_read(message, "u", &uid);
1055 r = manager_get_user_from_creds(m, message, uid, error, &user);
1059 return bus_user_method_kill(message, user, error);
1062 static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1063 Manager *m = userdata;
1071 r = sd_bus_message_read(message, "s", &name);
1075 r = manager_get_session_from_creds(m, message, name, error, &session);
1079 return bus_session_method_terminate(message, session, error);
1082 static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1083 Manager *m = userdata;
1091 r = sd_bus_message_read(message, "u", &uid);
1095 r = manager_get_user_from_creds(m, message, uid, error, &user);
1099 return bus_user_method_terminate(message, user, error);
1102 static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1103 Manager *m = userdata;
1111 r = sd_bus_message_read(message, "s", &name);
1115 r = manager_get_seat_from_creds(m, message, name, error, &seat);
1119 return bus_seat_method_terminate(message, seat, error);
1122 static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1123 _cleanup_free_ char *cc = NULL;
1124 Manager *m = userdata;
1125 int r, b, interactive;
1134 r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive);
1138 if (uid == UID_INVALID) {
1139 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1141 /* Note that we get the owner UID of the session, not the actual client UID here! */
1142 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds);
1146 r = sd_bus_creds_get_owner_uid(creds, &uid);
1152 } else if (!uid_is_valid(uid))
1158 return errno > 0 ? -errno : -ENOENT;
1160 r = bus_verify_polkit_async(
1163 self ? "org.freedesktop.login1.set-self-linger" : "org.freedesktop.login1.set-user-linger",
1167 &m->polkit_registry,
1172 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1174 mkdir_p_label("/var/lib/systemd", 0755);
1176 r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0);
1180 cc = cescape(pw->pw_name);
1184 path = strjoina("/var/lib/systemd/linger/", cc);
1192 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1199 if (r < 0 && errno != ENOENT)
1202 u = hashmap_get(m->users, UID_TO_PTR(uid));
1204 user_add_to_gc_queue(u);
1207 return sd_bus_reply_method_return(message, NULL);
1210 static int trigger_device(Manager *m, struct udev_device *d) {
1211 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
1212 struct udev_list_entry *first, *item;
1217 e = udev_enumerate_new(m->udev);
1222 r = udev_enumerate_add_match_parent(e, d);
1227 r = udev_enumerate_scan_devices(e);
1231 first = udev_enumerate_get_list_entry(e);
1232 udev_list_entry_foreach(item, first) {
1233 _cleanup_free_ char *t = NULL;
1236 p = udev_list_entry_get_name(item);
1238 t = strappend(p, "/uevent");
1242 write_string_file(t, "change", WRITE_STRING_FILE_CREATE);
1248 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
1249 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
1250 _cleanup_free_ char *rule = NULL, *file = NULL;
1251 const char *id_for_seat;
1258 d = udev_device_new_from_syspath(m->udev, sysfs);
1262 if (!udev_device_has_tag(d, "seat"))
1265 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
1269 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0)
1272 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0)
1275 mkdir_p_label("/etc/udev/rules.d", 0755);
1276 r = write_string_file_atomic_label(file, rule);
1280 return trigger_device(m, d);
1283 static int flush_devices(Manager *m) {
1284 _cleanup_closedir_ DIR *d;
1288 d = opendir("/etc/udev/rules.d");
1290 if (errno != ENOENT)
1291 log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m");
1295 while ((de = readdir(d))) {
1297 if (!dirent_is_file(de))
1300 if (!startswith(de->d_name, "72-seat-"))
1303 if (!endswith(de->d_name, ".rules"))
1306 if (unlinkat(dirfd(d), de->d_name, 0) < 0)
1307 log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name);
1311 return trigger_device(m, NULL);
1314 static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1315 const char *sysfs, *seat;
1316 Manager *m = userdata;
1322 r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive);
1326 if (!path_startswith(sysfs, "/sys"))
1327 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs);
1329 if (!seat_name_is_valid(seat))
1330 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat);
1332 r = bus_verify_polkit_async(
1335 "org.freedesktop.login1.attach-device",
1339 &m->polkit_registry,
1344 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1346 r = attach_device(m, seat, sysfs);
1350 return sd_bus_reply_method_return(message, NULL);
1353 static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1354 Manager *m = userdata;
1360 r = sd_bus_message_read(message, "b", &interactive);
1364 r = bus_verify_polkit_async(
1367 "org.freedesktop.login1.flush-devices",
1371 &m->polkit_registry,
1376 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1378 r = flush_devices(m);
1382 return sd_bus_reply_method_return(message, NULL);
1385 static int have_multiple_sessions(
1394 /* Check for other users' sessions. Greeter sessions do not
1395 * count, and non-login sessions do not count either. */
1396 HASHMAP_FOREACH(session, m->sessions, i)
1397 if (session->class == SESSION_USER &&
1398 session->user->uid != uid)
1404 static int bus_manager_log_shutdown(
1407 #if 0 /// elogind does not support systemd units
1408 const char *unit_name) {
1415 if (w != INHIBIT_SHUTDOWN)
1418 if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
1419 p = "MESSAGE=System is powering down";
1420 q = "SHUTDOWN=power-off";
1421 } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
1422 p = "MESSAGE=System is halting";
1423 q = "SHUTDOWN=halt";
1424 } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
1425 p = "MESSAGE=System is rebooting";
1426 q = "SHUTDOWN=reboot";
1427 } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
1428 p = "MESSAGE=System is rebooting with kexec";
1429 q = "SHUTDOWN=kexec";
1431 p = "MESSAGE=System is shutting down";
1435 HandleAction action) {
1441 if (w != INHIBIT_SHUTDOWN)
1445 case HANDLE_POWEROFF:
1446 p = "MESSAGE=System is powering down.";
1447 q = "SHUTDOWN=power-off";
1450 p = "MESSAGE=System is halting.";
1451 q = "SHUTDOWN=halt";
1454 p = "MESSAGE=System is rebooting.";
1455 q = "SHUTDOWN=reboot";
1458 p = "MESSAGE=System is rebooting with kexec.";
1459 q = "SHUTDOWN=kexec";
1462 p = "MESSAGE=System is shutting down.";
1466 if (isempty(m->wall_message))
1467 p = strjoina(p, ".");
1469 p = strjoina(p, " (", m->wall_message, ").");
1471 return log_struct(LOG_NOTICE,
1472 LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
1478 static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
1479 Manager *m = userdata;
1484 m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source);
1488 int manager_set_lid_switch_ignore(Manager *m, usec_t until) {
1493 if (until <= now(CLOCK_MONOTONIC))
1496 /* We want to ignore the lid switch for a while after each
1497 * suspend, and after boot-up. Hence let's install a timer for
1498 * this. As long as the event source exists we ignore the lid
1501 if (m->lid_switch_ignore_event_source) {
1504 r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u);
1511 r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until);
1513 r = sd_event_add_time(
1515 &m->lid_switch_ignore_event_source,
1518 lid_switch_ignore_handler, m);
1523 static void reset_scheduled_shutdown(Manager *m) {
1524 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
1525 m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source);
1526 m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source);
1527 m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type);
1528 m->scheduled_shutdown_timeout = 0;
1529 m->shutdown_dry_run = false;
1531 if (m->unlink_nologin) {
1532 (void) unlink("/run/nologin");
1533 m->unlink_nologin = false;
1537 static int execute_shutdown_or_sleep(
1540 HandleAction action,
1541 sd_bus_error *error) {
1543 #if 0 /// elogind does not need these, we do it ourselves
1544 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1552 assert(w < _INHIBIT_WHAT_MAX);
1554 bus_manager_log_shutdown(m, w, action);
1556 #if 0 /// elogind does it directly without depending on systemd running the system
1557 if (m->shutdown_dry_run) {
1558 log_info("Running in dry run, suppressing action.");
1559 reset_scheduled_shutdown(m);
1561 r = sd_bus_call_method(
1563 "org.freedesktop.systemd1",
1564 "/org/freedesktop/systemd1",
1565 "org.freedesktop.systemd1.Manager",
1569 "ss", unit_name, "replace-irreversibly");
1571 r = shutdown_or_sleep(m, action);
1573 /* no more pending actions, whether this failed or not */
1574 m->pending_action = HANDLE_IGNORE;
1580 #if 0 /// elogind neither needs a dbus reply, nor supports systemd action jobs
1581 r = sd_bus_message_read(reply, "o", &p);
1590 m->action_unit = unit_name;
1591 free(m->action_job);
1596 /* Make sure the lid switch is ignored for a while */
1597 manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
1602 int manager_dispatch_delayed(Manager *manager, bool timeout) {
1604 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1605 Inhibitor *offending = NULL;
1610 if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
1613 if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
1614 _cleanup_free_ char *comm = NULL, *u = NULL;
1619 (void) get_process_comm(offending->pid, &comm);
1620 u = uid_to_name(offending->uid);
1622 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
1623 offending->uid, strna(u),
1624 offending->pid, strna(comm));
1627 /* Actually do the operation */
1628 r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
1630 log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
1632 manager->pending_action = HANDLE_IGNORE;
1633 manager->action_what = 0;
1640 static int manager_inhibit_timeout_handler(
1645 Manager *manager = userdata;
1649 assert(manager->inhibit_timeout_source == s);
1651 r = manager_dispatch_delayed(manager, true);
1652 return (r < 0) ? r : 0;
1655 static int delay_shutdown_or_sleep(
1658 HandleAction action) {
1665 assert(w < _INHIBIT_WHAT_MAX);
1667 timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
1669 if (m->inhibit_timeout_source) {
1670 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
1672 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
1674 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
1676 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
1678 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
1679 timeout_val, 0, manager_inhibit_timeout_handler, m);
1684 m->pending_action = action;
1690 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
1692 static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
1693 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
1694 [INHIBIT_SLEEP] = "PrepareForSleep"
1697 int active = _active;
1701 assert(w < _INHIBIT_WHAT_MAX);
1702 assert(signal_name[w]);
1704 return sd_bus_emit_signal(m->bus,
1705 "/org/freedesktop/login1",
1706 "org.freedesktop.login1.Manager",
1712 int bus_manager_shutdown_or_sleep_now_or_later(
1714 HandleAction action,
1716 sd_bus_error *error) {
1723 assert(w <= _INHIBIT_WHAT_MAX);
1725 /* Tell everybody to prepare for shutdown/sleep */
1726 send_prepare_for(m, w, true);
1729 m->inhibit_delay_max > 0 &&
1730 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
1733 /* Shutdown is delayed, keep in mind what we
1734 * want to do, and start a timeout */
1735 r = delay_shutdown_or_sleep(m, w, action);
1737 /* Shutdown is not delayed, execute it
1739 r = execute_shutdown_or_sleep(m, w, action, error);
1744 static int verify_shutdown_creds(
1746 sd_bus_message *message,
1750 const char *action_multiple_sessions,
1751 const char *action_ignore_inhibit,
1752 sd_bus_error *error) {
1754 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
1755 bool multiple_sessions, blocked;
1762 assert(w <= _INHIBIT_WHAT_MAX);
1764 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
1768 r = sd_bus_creds_get_euid(creds, &uid);
1772 r = have_multiple_sessions(m, uid);
1776 multiple_sessions = r > 0;
1777 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
1779 if (multiple_sessions && action_multiple_sessions) {
1780 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1784 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1787 if (blocked && action_ignore_inhibit) {
1788 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1792 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1795 if (!multiple_sessions && !blocked && action) {
1796 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
1800 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1806 static int method_do_shutdown_or_sleep(
1808 sd_bus_message *message,
1809 HandleAction sleep_action,
1812 const char *action_multiple_sessions,
1813 const char *action_ignore_inhibit,
1814 const char *sleep_verb,
1815 sd_bus_error *error) {
1822 assert(w <= _INHIBIT_WHAT_MAX);
1824 r = sd_bus_message_read(message, "b", &interactive);
1828 /* Don't allow multiple jobs being executed at the same time */
1830 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
1833 r = can_sleep(m, sleep_verb);
1838 return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
1841 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
1842 action_ignore_inhibit, error);
1846 r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
1850 return sd_bus_reply_method_return(message, NULL);
1853 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1854 Manager *m = userdata;
1856 return method_do_shutdown_or_sleep(
1860 "org.freedesktop.login1.power-off",
1861 "org.freedesktop.login1.power-off-multiple-sessions",
1862 "org.freedesktop.login1.power-off-ignore-inhibit",
1867 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1868 Manager *m = userdata;
1870 return method_do_shutdown_or_sleep(
1874 "org.freedesktop.login1.reboot",
1875 "org.freedesktop.login1.reboot-multiple-sessions",
1876 "org.freedesktop.login1.reboot-ignore-inhibit",
1881 static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1882 Manager *m = userdata;
1884 return method_do_shutdown_or_sleep(
1888 "org.freedesktop.login1.suspend",
1889 "org.freedesktop.login1.suspend-multiple-sessions",
1890 "org.freedesktop.login1.suspend-ignore-inhibit",
1895 static int nologin_timeout_handler(
1900 Manager *m = userdata;
1903 log_info("Creating /run/nologin, blocking further logins...");
1905 r = write_string_file_atomic_label("/run/nologin", "System is going down.");
1907 log_error_errno(r, "Failed to create /run/nologin: %m");
1909 m->unlink_nologin = true;
1914 static int update_schedule_file(Manager *m) {
1915 _cleanup_free_ char *temp_path = NULL;
1916 _cleanup_fclose_ FILE *f = NULL;
1921 r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0);
1923 return log_error_errno(r, "Failed to create shutdown subdirectory: %m");
1925 r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path);
1927 return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m");
1929 (void) fchmod(fileno(f), 0644);
1935 m->scheduled_shutdown_timeout,
1936 m->enable_wall_messages,
1937 m->scheduled_shutdown_type);
1939 if (!isempty(m->wall_message)) {
1940 _cleanup_free_ char *t;
1942 t = cescape(m->wall_message);
1948 fprintf(f, "WALL_MESSAGE=%s\n", t);
1951 r = fflush_and_check(f);
1955 if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) {
1963 (void) unlink(temp_path);
1964 (void) unlink("/run/systemd/shutdown/scheduled");
1966 return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
1969 static int manager_scheduled_shutdown_handler(
1974 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1975 Manager *m = userdata;
1976 HandleAction action;
1981 if (isempty(m->scheduled_shutdown_type))
1984 if (streq(m->scheduled_shutdown_type, "halt"))
1985 action = HANDLE_HALT;
1986 else if (streq(m->scheduled_shutdown_type, "poweroff"))
1987 action = HANDLE_POWEROFF;
1989 action = HANDLE_REBOOT;
1991 r = execute_shutdown_or_sleep(m, 0, action, &error);
1993 return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type);
1998 static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1999 Manager *m = userdata;
2000 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2001 const char *action_multiple_sessions = NULL;
2002 const char *action_ignore_inhibit = NULL;
2003 const char *action = NULL;
2011 r = sd_bus_message_read(message, "st", &type, &elapse);
2015 if (startswith(type, "dry-")) {
2017 m->shutdown_dry_run = true;
2020 if (streq(type, "reboot")) {
2021 action = "org.freedesktop.login1.reboot";
2022 action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions";
2023 action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit";
2024 } else if (streq(type, "halt")) {
2025 action = "org.freedesktop.login1.halt";
2026 action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions";
2027 action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit";
2028 } else if (streq(type, "poweroff")) {
2029 action = "org.freedesktop.login1.power-off";
2030 action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions";
2031 action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit";
2033 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
2035 r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false,
2036 action, action_multiple_sessions, action_ignore_inhibit, error);
2040 if (m->scheduled_shutdown_timeout_source) {
2041 r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse);
2043 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2045 r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT);
2047 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2049 r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source,
2050 CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m);
2052 return log_error_errno(r, "sd_event_add_time() failed: %m");
2055 r = free_and_strdup(&m->scheduled_shutdown_type, type);
2057 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2061 if (m->nologin_timeout_source) {
2062 r = sd_event_source_set_time(m->nologin_timeout_source, elapse);
2064 return log_error_errno(r, "sd_event_source_set_time() failed: %m");
2066 r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT);
2068 return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
2070 r = sd_event_add_time(m->event, &m->nologin_timeout_source,
2071 CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m);
2073 return log_error_errno(r, "sd_event_add_time() failed: %m");
2076 m->scheduled_shutdown_timeout = elapse;
2078 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2080 const char *tty = NULL;
2082 (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid);
2083 (void) sd_bus_creds_get_tty(creds, &tty);
2085 r = free_and_strdup(&m->scheduled_shutdown_tty, tty);
2087 m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source);
2092 #if 0 /// elogind does not support utmp-wtmp
2093 r = manager_setup_wall_message_timer(m);
2098 if (!isempty(type)) {
2099 r = update_schedule_file(m);
2103 (void) unlink("/run/systemd/shutdown/scheduled");
2105 return sd_bus_reply_method_return(message, NULL);
2108 static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2109 Manager *m = userdata;
2115 cancelled = m->scheduled_shutdown_type != NULL;
2116 reset_scheduled_shutdown(m);
2118 #if 0 /// elogind does not support utmp-wtmp
2120 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2121 const char *tty = NULL;
2125 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
2127 (void) sd_bus_creds_get_uid(creds, &uid);
2128 (void) sd_bus_creds_get_tty(creds, &tty);
2131 utmp_wall("The system shutdown has been cancelled",
2132 uid_to_name(uid), tty, logind_wall_tty_filter, m);
2136 return sd_bus_reply_method_return(message, "b", cancelled);
2139 static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2140 Manager *m = userdata;
2142 return method_do_shutdown_or_sleep(
2146 "org.freedesktop.login1.hibernate",
2147 "org.freedesktop.login1.hibernate-multiple-sessions",
2148 "org.freedesktop.login1.hibernate-ignore-inhibit",
2153 static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2154 Manager *m = userdata;
2156 return method_do_shutdown_or_sleep(
2158 HANDLE_HYBRID_SLEEP,
2160 "org.freedesktop.login1.hibernate",
2161 "org.freedesktop.login1.hibernate-multiple-sessions",
2162 "org.freedesktop.login1.hibernate-ignore-inhibit",
2167 static int method_can_shutdown_or_sleep(
2169 sd_bus_message *message,
2172 const char *action_multiple_sessions,
2173 const char *action_ignore_inhibit,
2174 const char *sleep_verb,
2175 sd_bus_error *error) {
2177 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2178 bool multiple_sessions, challenge, blocked;
2179 const char *result = NULL;
2186 assert(w <= _INHIBIT_WHAT_MAX);
2188 assert(action_multiple_sessions);
2189 assert(action_ignore_inhibit);
2192 r = can_sleep(m, sleep_verb);
2196 return sd_bus_reply_method_return(message, "s", "na");
2199 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
2203 r = sd_bus_creds_get_euid(creds, &uid);
2207 r = have_multiple_sessions(m, uid);
2211 multiple_sessions = r > 0;
2212 blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
2214 if (multiple_sessions) {
2215 r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error);
2222 result = "challenge";
2228 r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error);
2232 if (r > 0 && !result)
2234 else if (challenge && (!result || streq(result, "yes")))
2235 result = "challenge";
2240 if (!multiple_sessions && !blocked) {
2241 /* If neither inhibit nor multiple sessions
2242 * apply then just check the normal policy */
2244 r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error);
2251 result = "challenge";
2256 return sd_bus_reply_method_return(message, "s", result);
2259 static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2260 Manager *m = userdata;
2262 return method_can_shutdown_or_sleep(
2265 "org.freedesktop.login1.power-off",
2266 "org.freedesktop.login1.power-off-multiple-sessions",
2267 "org.freedesktop.login1.power-off-ignore-inhibit",
2272 static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2273 Manager *m = userdata;
2275 return method_can_shutdown_or_sleep(
2278 "org.freedesktop.login1.reboot",
2279 "org.freedesktop.login1.reboot-multiple-sessions",
2280 "org.freedesktop.login1.reboot-ignore-inhibit",
2285 static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2286 Manager *m = userdata;
2288 return method_can_shutdown_or_sleep(
2291 "org.freedesktop.login1.suspend",
2292 "org.freedesktop.login1.suspend-multiple-sessions",
2293 "org.freedesktop.login1.suspend-ignore-inhibit",
2298 static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2299 Manager *m = userdata;
2301 return method_can_shutdown_or_sleep(
2304 "org.freedesktop.login1.hibernate",
2305 "org.freedesktop.login1.hibernate-multiple-sessions",
2306 "org.freedesktop.login1.hibernate-ignore-inhibit",
2311 static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2312 Manager *m = userdata;
2314 return method_can_shutdown_or_sleep(
2317 "org.freedesktop.login1.hibernate",
2318 "org.freedesktop.login1.hibernate-multiple-sessions",
2319 "org.freedesktop.login1.hibernate-ignore-inhibit",
2324 static int property_get_reboot_to_firmware_setup(
2327 const char *interface,
2328 const char *property,
2329 sd_bus_message *reply,
2331 sd_bus_error *error) {
2332 #if 0 /// elogind does not support EFI
2339 r = efi_get_reboot_to_firmware();
2340 if (r < 0 && r != -EOPNOTSUPP)
2343 return sd_bus_message_append(reply, "b", r > 0);
2345 return sd_bus_message_append(reply, "b", -EOPNOTSUPP);
2349 static int method_set_reboot_to_firmware_setup(
2350 sd_bus_message *message,
2352 sd_bus_error *error) {
2355 Manager *m = userdata;
2360 r = sd_bus_message_read(message, "b", &b);
2364 r = bus_verify_polkit_async(message,
2366 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2370 &m->polkit_registry,
2375 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2377 #if 0 /// elogind does not support EFI
2378 r = efi_set_reboot_to_firmware(b);
2383 return sd_bus_reply_method_return(message, NULL);
2386 static int method_can_reboot_to_firmware_setup(
2387 sd_bus_message *message,
2389 sd_bus_error *error) {
2391 #if 0 /// elogind does not support EFI
2395 Manager *m = userdata;
2400 r = efi_reboot_to_firmware_supported();
2401 if (r == -EOPNOTSUPP)
2402 return sd_bus_reply_method_return(message, "s", "na");
2406 r = bus_test_polkit(message,
2408 "org.freedesktop.login1.set-reboot-to-firmware-setup",
2419 result = "challenge";
2423 return sd_bus_reply_method_return(message, "s", result);
2425 return sd_bus_reply_method_return(message, "s", "na");
2429 static int method_set_wall_message(
2430 sd_bus_message *message,
2432 sd_bus_error *error) {
2435 Manager *m = userdata;
2437 int enable_wall_messages;
2442 r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages);
2446 r = bus_verify_polkit_async(message,
2448 "org.freedesktop.login1.set-wall-message",
2452 &m->polkit_registry,
2457 return 1; /* Will call us back */
2459 if (isempty(wall_message))
2460 m->wall_message = mfree(m->wall_message);
2462 r = free_and_strdup(&m->wall_message, wall_message);
2467 m->enable_wall_messages = enable_wall_messages;
2469 return sd_bus_reply_method_return(message, NULL);
2472 static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2473 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
2474 const char *who, *why, *what, *mode;
2475 _cleanup_free_ char *id = NULL;
2476 _cleanup_close_ int fifo_fd = -1;
2477 Manager *m = userdata;
2478 Inhibitor *i = NULL;
2488 r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode);
2492 w = inhibit_what_from_string(what);
2494 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what);
2496 mm = inhibit_mode_from_string(mode);
2498 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode);
2500 /* Delay is only supported for shutdown/sleep */
2501 if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP)))
2502 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep");
2504 /* Don't allow taking delay locks while we are already
2505 * executing the operation. We shouldn't create the impression
2506 * that the lock was successful if the machine is about to go
2507 * down/suspend any moment. */
2508 if (m->action_what & w)
2509 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running");
2511 r = bus_verify_polkit_async(
2514 w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") :
2515 w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") :
2516 w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" :
2517 w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" :
2518 w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" :
2519 w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" :
2520 "org.freedesktop.login1.inhibit-handle-lid-switch",
2524 &m->polkit_registry,
2529 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2531 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds);
2535 r = sd_bus_creds_get_euid(creds, &uid);
2539 r = sd_bus_creds_get_pid(creds, &pid);
2543 if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
2544 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
2549 if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0)
2552 } while (hashmap_get(m->inhibitors, id));
2554 r = manager_add_inhibitor(m, id, &i);
2562 i->why = strdup(why);
2563 i->who = strdup(who);
2565 if (!i->why || !i->who) {
2570 fifo_fd = inhibitor_create_fifo(i);
2578 return sd_bus_reply_method_return(message, "h", fifo_fd);
2587 const sd_bus_vtable manager_vtable[] = {
2588 SD_BUS_VTABLE_START(0),
2590 SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
2591 SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
2593 // SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
2594 SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
2595 SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
2596 SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
2597 SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2598 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2599 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2600 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2601 SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2602 SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2603 SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST),
2604 SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST),
2605 SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST),
2606 SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST),
2607 SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST),
2608 SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST),
2609 SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2610 SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST),
2611 SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2612 SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0),
2613 SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0),
2614 SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0),
2615 SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
2616 SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
2617 SD_BUS_PROPERTY("RuntimeDirectorySize", "t", bus_property_get_size, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
2618 SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
2619 SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_current_inhibitors, 0, 0),
2620 SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
2621 SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_current_sessions, 0, 0),
2622 SD_BUS_PROPERTY("UserTasksMax", "t", NULL, offsetof(Manager, user_tasks_max), SD_BUS_VTABLE_PROPERTY_CONST),
2624 SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED),
2625 SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2626 SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
2627 SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
2628 SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2629 SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2630 SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED),
2631 SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED),
2632 SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED),
2633 SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0),
2634 SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0),
2635 SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2636 SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2637 SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2638 SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED),
2639 SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2640 SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED),
2641 SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED),
2642 SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED),
2643 SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED),
2644 SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED),
2645 SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED),
2646 SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED),
2647 SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED),
2648 SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED),
2649 SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2650 SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2651 SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2652 SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2653 SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2654 SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED),
2655 SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED),
2656 SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED),
2657 SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED),
2658 SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED),
2659 SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2660 SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED),
2661 SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED),
2662 SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2663 SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED),
2664 SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED),
2666 SD_BUS_SIGNAL("SessionNew", "so", 0),
2667 SD_BUS_SIGNAL("SessionRemoved", "so", 0),
2668 SD_BUS_SIGNAL("UserNew", "uo", 0),
2669 SD_BUS_SIGNAL("UserRemoved", "uo", 0),
2670 SD_BUS_SIGNAL("SeatNew", "so", 0),
2671 SD_BUS_SIGNAL("SeatRemoved", "so", 0),
2672 SD_BUS_SIGNAL("PrepareForShutdown", "b", 0),
2673 SD_BUS_SIGNAL("PrepareForSleep", "b", 0),
2678 #if 0 /// UNNEEDED by elogind
2679 static int session_jobs_reply(Session *s, const char *unit, const char *result) {
2688 if (streq(result, "done"))
2689 r = session_send_create_reply(s, NULL);
2691 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
2693 sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
2694 r = session_send_create_reply(s, &e);
2700 int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2701 const char *path, *result, *unit;
2702 Manager *m = userdata;
2711 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
2713 bus_log_parse_error(r);
2717 if (m->action_job && streq(m->action_job, path)) {
2718 log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what));
2720 /* Tell people that they now may take a lock again */
2721 send_prepare_for(m, m->action_what, false);
2723 m->action_job = mfree(m->action_job);
2724 m->action_unit = NULL;
2729 session = hashmap_get(m->session_units, unit);
2730 if (session && streq_ptr(path, session->scope_job)) {
2731 session->scope_job = mfree(session->scope_job);
2732 session_jobs_reply(session, unit, result);
2734 session_save(session);
2735 user_save(session->user);
2736 session_add_to_gc_queue(session);
2739 user = hashmap_get(m->user_units, unit);
2741 (streq_ptr(path, user->service_job) ||
2742 streq_ptr(path, user->slice_job))) {
2744 if (streq_ptr(path, user->service_job))
2745 user->service_job = mfree(user->service_job);
2747 if (streq_ptr(path, user->slice_job))
2748 user->slice_job = mfree(user->slice_job);
2750 LIST_FOREACH(sessions_by_user, session, user->sessions)
2751 session_jobs_reply(session, unit, result);
2754 user_add_to_gc_queue(user);
2760 int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2761 const char *path, *unit;
2762 Manager *m = userdata;
2770 r = sd_bus_message_read(message, "so", &unit, &path);
2772 bus_log_parse_error(r);
2776 session = hashmap_get(m->session_units, unit);
2778 session_add_to_gc_queue(session);
2780 user = hashmap_get(m->user_units, unit);
2782 user_add_to_gc_queue(user);
2787 int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2788 _cleanup_free_ char *unit = NULL;
2789 Manager *m = userdata;
2798 path = sd_bus_message_get_path(message);
2802 r = unit_name_from_dbus_path(path, &unit);
2803 if (r == -EINVAL) /* not a unit */
2810 session = hashmap_get(m->session_units, unit);
2812 session_add_to_gc_queue(session);
2814 user = hashmap_get(m->user_units, unit);
2816 user_add_to_gc_queue(user);
2821 int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2822 Manager *m = userdata;
2830 r = sd_bus_message_read(message, "b", &b);
2832 bus_log_parse_error(r);
2839 /* systemd finished reloading, let's recheck all our sessions */
2840 log_debug("System manager has been reloaded, rechecking sessions...");
2842 HASHMAP_FOREACH(session, m->sessions, i)
2843 session_add_to_gc_queue(session);
2849 int manager_send_changed(Manager *manager, const char *property, ...) {
2854 l = strv_from_stdarg_alloca(property);
2856 return sd_bus_emit_properties_changed_strv(
2858 "/org/freedesktop/login1",
2859 "org.freedesktop.login1.Manager",
2863 #if 0 /// UNNEEDED by elogind
2864 static int strdup_job(sd_bus_message *reply, char **job) {
2869 r = sd_bus_message_read(reply, "o", &j);
2881 int manager_start_slice(
2884 const char *description,
2888 sd_bus_error *error,
2891 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
2897 r = sd_bus_message_new_method_call(
2900 "org.freedesktop.systemd1",
2901 "/org/freedesktop/systemd1",
2902 "org.freedesktop.systemd1.Manager",
2903 "StartTransientUnit");
2907 r = sd_bus_message_append(m, "ss", strempty(slice), "fail");
2911 r = sd_bus_message_open_container(m, 'a', "(sv)");
2915 if (!isempty(description)) {
2916 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
2921 if (!isempty(after)) {
2922 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
2927 if (!isempty(after2)) {
2928 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
2933 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", tasks_max);
2937 r = sd_bus_message_close_container(m);
2941 r = sd_bus_message_append(m, "a(sa(sv))", 0);
2945 r = sd_bus_call(manager->bus, m, 0, error, &reply);
2950 return strdup_job(reply, job);
2954 int manager_start_scope(
2959 const char *description,
2963 sd_bus_error *error,
2966 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
2973 r = sd_bus_message_new_method_call(
2976 "org.freedesktop.systemd1",
2977 "/org/freedesktop/systemd1",
2978 "org.freedesktop.systemd1.Manager",
2979 "StartTransientUnit");
2983 r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
2987 r = sd_bus_message_open_container(m, 'a', "(sv)");
2991 if (!isempty(slice)) {
2992 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
2997 if (!isempty(description)) {
2998 r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
3003 if (!isempty(after)) {
3004 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
3009 if (!isempty(after2)) {
3010 r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
3015 /* cgroup empty notification is not available in containers
3016 * currently. To make this less problematic, let's shorten the
3017 * stop timeout for sessions, so that we don't wait
3020 /* Make sure that the session shells are terminated with
3021 * SIGHUP since bash and friends tend to ignore SIGTERM */
3022 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
3026 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
3030 r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", tasks_max);
3034 r = sd_bus_message_close_container(m);
3038 r = sd_bus_message_append(m, "a(sa(sv))", 0);
3042 r = sd_bus_call(manager->bus, m, 0, error, &reply);
3047 return strdup_job(reply, job);
3051 int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3052 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3058 r = sd_bus_call_method(
3060 "org.freedesktop.systemd1",
3061 "/org/freedesktop/systemd1",
3062 "org.freedesktop.systemd1.Manager",
3066 "ss", unit, "replace");
3071 return strdup_job(reply, job);
3075 int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
3076 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3082 r = sd_bus_call_method(
3084 "org.freedesktop.systemd1",
3085 "/org/freedesktop/systemd1",
3086 "org.freedesktop.systemd1.Manager",
3090 "ss", unit, "fail");
3092 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3093 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
3098 sd_bus_error_free(error);
3106 return strdup_job(reply, job);
3110 int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
3111 _cleanup_free_ char *path = NULL;
3117 path = unit_dbus_path_from_name(scope);
3121 r = sd_bus_call_method(
3123 "org.freedesktop.systemd1",
3125 "org.freedesktop.systemd1.Scope",
3131 if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
3132 sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
3133 sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
3134 sd_bus_error_free(error);
3144 int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
3148 return sd_bus_call_method(
3150 "org.freedesktop.systemd1",
3151 "/org/freedesktop/systemd1",
3152 "org.freedesktop.systemd1.Manager",
3156 "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
3159 int manager_unit_is_active(Manager *manager, const char *unit) {
3160 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3161 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3162 _cleanup_free_ char *path = NULL;
3169 path = unit_dbus_path_from_name(unit);
3173 r = sd_bus_get_property(
3175 "org.freedesktop.systemd1",
3177 "org.freedesktop.systemd1.Unit",
3183 /* systemd might have droppped off momentarily, let's
3184 * not make this an error */
3185 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3186 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3189 /* If the unit is already unloaded then it's not
3191 if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
3192 sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
3198 r = sd_bus_message_read(reply, "s", &state);
3202 return !streq(state, "inactive") && !streq(state, "failed");
3205 int manager_job_is_active(Manager *manager, const char *path) {
3206 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3207 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3213 r = sd_bus_get_property(
3215 "org.freedesktop.systemd1",
3217 "org.freedesktop.systemd1.Job",
3223 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
3224 sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
3227 if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
3233 /* We don't actually care about the state really. The fact
3234 * that we could read the job state is enough for us */