1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "bus-common-errors.h"
11 #include "logind-session-device.h"
12 #include "logind-session.h"
14 #include "signal-util.h"
18 static int property_get_user(
21 const char *interface,
23 sd_bus_message *reply,
25 sd_bus_error *error) {
27 _cleanup_free_ char *p = NULL;
28 Session *s = userdata;
34 p = user_bus_path(s->user);
38 return sd_bus_message_append(reply, "(uo)", (uint32_t) s->user->uid, p);
41 static int property_get_name(
44 const char *interface,
46 sd_bus_message *reply,
48 sd_bus_error *error) {
50 Session *s = userdata;
56 return sd_bus_message_append(reply, "s", s->user->name);
59 static int property_get_seat(
62 const char *interface,
64 sd_bus_message *reply,
66 sd_bus_error *error) {
68 _cleanup_free_ char *p = NULL;
69 Session *s = userdata;
75 p = s->seat ? seat_bus_path(s->seat) : strdup("/");
79 return sd_bus_message_append(reply, "(so)", s->seat ? s->seat->id : "", p);
82 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, session_type, SessionType);
83 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class, session_class, SessionClass);
84 static BUS_DEFINE_PROPERTY_GET(property_get_active, "b", Session, session_is_active);
85 static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", Session, session_get_state, session_state_to_string);
87 static int property_get_idle_hint(
90 const char *interface,
92 sd_bus_message *reply,
94 sd_bus_error *error) {
96 Session *s = userdata;
102 return sd_bus_message_append(reply, "b", session_get_idle_hint(s, NULL) > 0);
105 static int property_get_idle_since_hint(
108 const char *interface,
109 const char *property,
110 sd_bus_message *reply,
112 sd_bus_error *error) {
114 Session *s = userdata;
115 dual_timestamp t = DUAL_TIMESTAMP_NULL;
123 r = session_get_idle_hint(s, &t);
127 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
129 return sd_bus_message_append(reply, "t", u);
132 static int property_get_locked_hint(
135 const char *interface,
136 const char *property,
137 sd_bus_message *reply,
139 sd_bus_error *error) {
141 Session *s = userdata;
147 return sd_bus_message_append(reply, "b", session_get_locked_hint(s) > 0);
150 int bus_session_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
151 Session *s = userdata;
157 r = bus_verify_polkit_async(
160 "org.freedesktop.login1.manage",
164 &s->manager->polkit_registry,
169 return 1; /* Will call us back */
171 r = session_stop(s, true);
175 return sd_bus_reply_method_return(message, NULL);
178 int bus_session_method_activate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
179 Session *s = userdata;
185 r = session_activate(s);
189 return sd_bus_reply_method_return(message, NULL);
192 int bus_session_method_lock(sd_bus_message *message, void *userdata, sd_bus_error *error) {
193 Session *s = userdata;
199 r = bus_verify_polkit_async(
202 "org.freedesktop.login1.lock-sessions",
206 &s->manager->polkit_registry,
211 return 1; /* Will call us back */
213 r = session_send_lock(s, strstr(sd_bus_message_get_member(message), "Lock"));
217 return sd_bus_reply_method_return(message, NULL);
220 static int method_set_idle_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) {
221 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
222 Session *s = userdata;
229 r = sd_bus_message_read(message, "b", &b);
233 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
237 r = sd_bus_creds_get_euid(creds, &uid);
241 if (uid != 0 && uid != s->user->uid)
242 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set idle hint");
244 session_set_idle_hint(s, b);
246 return sd_bus_reply_method_return(message, NULL);
249 static int method_set_locked_hint(sd_bus_message *message, void *userdata, sd_bus_error *error) {
250 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
251 Session *s = userdata;
258 r = sd_bus_message_read(message, "b", &b);
262 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
266 r = sd_bus_creds_get_euid(creds, &uid);
270 if (uid != 0 && uid != s->user->uid)
271 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may set locked hint");
273 session_set_locked_hint(s, b);
275 return sd_bus_reply_method_return(message, NULL);
278 int bus_session_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) {
279 Session *s = userdata;
288 r = sd_bus_message_read(message, "si", &swho, &signo);
295 who = kill_who_from_string(swho);
297 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
300 if (!SIGNAL_VALID(signo))
301 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
303 r = bus_verify_polkit_async(
306 "org.freedesktop.login1.manage",
310 &s->manager->polkit_registry,
315 return 1; /* Will call us back */
317 r = session_kill(s, who, signo);
321 return sd_bus_reply_method_return(message, NULL);
324 static int method_take_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
325 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
326 Session *s = userdata;
333 r = sd_bus_message_read(message, "b", &force);
337 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
341 r = sd_bus_creds_get_euid(creds, &uid);
345 if (uid != 0 && (force || uid != s->user->uid))
346 return sd_bus_error_setf(error, SD_BUS_ERROR_ACCESS_DENIED, "Only owner of session may take control");
348 r = session_set_controller(s, sd_bus_message_get_sender(message), force, true);
352 return sd_bus_reply_method_return(message, NULL);
355 static int method_release_control(sd_bus_message *message, void *userdata, sd_bus_error *error) {
356 Session *s = userdata;
361 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
362 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
364 session_drop_controller(s);
366 return sd_bus_reply_method_return(message, NULL);
369 static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
370 Session *s = userdata;
371 uint32_t major, minor;
379 r = sd_bus_message_read(message, "uu", &major, &minor);
383 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
384 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
386 dev = makedev(major, minor);
387 sd = hashmap_get(s->devices, &dev);
389 /* We don't allow retrieving a device multiple times.
390 * The related ReleaseDevice call is not ref-counted.
391 * The caller should use dup() if it requires more
392 * than one fd (it would be functionally
394 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_IS_TAKEN, "Device already taken");
396 r = session_device_new(s, dev, true, &sd);
400 r = session_device_save(sd);
404 r = sd_bus_reply_method_return(message, "hb", sd->fd, !sd->active);
412 session_device_free(sd);
416 static int method_release_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
417 Session *s = userdata;
418 uint32_t major, minor;
426 r = sd_bus_message_read(message, "uu", &major, &minor);
430 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
431 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
433 dev = makedev(major, minor);
434 sd = hashmap_get(s->devices, &dev);
436 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
438 session_device_free(sd);
441 return sd_bus_reply_method_return(message, NULL);
444 static int method_pause_device_complete(sd_bus_message *message, void *userdata, sd_bus_error *error) {
445 Session *s = userdata;
446 uint32_t major, minor;
454 r = sd_bus_message_read(message, "uu", &major, &minor);
458 if (!session_is_controller(s, sd_bus_message_get_sender(message)))
459 return sd_bus_error_setf(error, BUS_ERROR_NOT_IN_CONTROL, "You are not in control of this session");
461 dev = makedev(major, minor);
462 sd = hashmap_get(s->devices, &dev);
464 return sd_bus_error_setf(error, BUS_ERROR_DEVICE_NOT_TAKEN, "Device not taken");
466 session_device_complete_pause(sd);
468 return sd_bus_reply_method_return(message, NULL);
471 const sd_bus_vtable session_vtable[] = {
472 SD_BUS_VTABLE_START(0),
474 SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Session, id), SD_BUS_VTABLE_PROPERTY_CONST),
475 SD_BUS_PROPERTY("User", "(uo)", property_get_user, 0, SD_BUS_VTABLE_PROPERTY_CONST),
476 SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST),
477 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Session, timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
478 SD_BUS_PROPERTY("VTNr", "u", NULL, offsetof(Session, vtnr), SD_BUS_VTABLE_PROPERTY_CONST),
479 SD_BUS_PROPERTY("Seat", "(so)", property_get_seat, 0, SD_BUS_VTABLE_PROPERTY_CONST),
480 SD_BUS_PROPERTY("TTY", "s", NULL, offsetof(Session, tty), SD_BUS_VTABLE_PROPERTY_CONST),
481 SD_BUS_PROPERTY("Display", "s", NULL, offsetof(Session, display), SD_BUS_VTABLE_PROPERTY_CONST),
482 SD_BUS_PROPERTY("Remote", "b", bus_property_get_bool, offsetof(Session, remote), SD_BUS_VTABLE_PROPERTY_CONST),
483 SD_BUS_PROPERTY("RemoteHost", "s", NULL, offsetof(Session, remote_host), SD_BUS_VTABLE_PROPERTY_CONST),
484 SD_BUS_PROPERTY("RemoteUser", "s", NULL, offsetof(Session, remote_user), SD_BUS_VTABLE_PROPERTY_CONST),
485 SD_BUS_PROPERTY("Service", "s", NULL, offsetof(Session, service), SD_BUS_VTABLE_PROPERTY_CONST),
486 SD_BUS_PROPERTY("Desktop", "s", NULL, offsetof(Session, desktop), SD_BUS_VTABLE_PROPERTY_CONST),
487 SD_BUS_PROPERTY("Scope", "s", NULL, offsetof(Session, scope), SD_BUS_VTABLE_PROPERTY_CONST),
488 SD_BUS_PROPERTY("Leader", "u", bus_property_get_pid, offsetof(Session, leader), SD_BUS_VTABLE_PROPERTY_CONST),
489 SD_BUS_PROPERTY("Audit", "u", NULL, offsetof(Session, audit_id), SD_BUS_VTABLE_PROPERTY_CONST),
490 SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Session, type), SD_BUS_VTABLE_PROPERTY_CONST),
491 SD_BUS_PROPERTY("Class", "s", property_get_class, offsetof(Session, class), SD_BUS_VTABLE_PROPERTY_CONST),
492 SD_BUS_PROPERTY("Active", "b", property_get_active, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
493 SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0),
494 SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
495 SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
496 SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
497 SD_BUS_PROPERTY("LockedHint", "b", property_get_locked_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
499 SD_BUS_METHOD("Terminate", NULL, NULL, bus_session_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED),
500 SD_BUS_METHOD("Activate", NULL, NULL, bus_session_method_activate, SD_BUS_VTABLE_UNPRIVILEGED),
501 SD_BUS_METHOD("Lock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED),
502 SD_BUS_METHOD("Unlock", NULL, NULL, bus_session_method_lock, SD_BUS_VTABLE_UNPRIVILEGED),
503 SD_BUS_METHOD("SetIdleHint", "b", NULL, method_set_idle_hint, SD_BUS_VTABLE_UNPRIVILEGED),
504 SD_BUS_METHOD("SetLockedHint", "b", NULL, method_set_locked_hint, SD_BUS_VTABLE_UNPRIVILEGED),
505 SD_BUS_METHOD("Kill", "si", NULL, bus_session_method_kill, SD_BUS_VTABLE_UNPRIVILEGED),
506 SD_BUS_METHOD("TakeControl", "b", NULL, method_take_control, SD_BUS_VTABLE_UNPRIVILEGED),
507 SD_BUS_METHOD("ReleaseControl", NULL, NULL, method_release_control, SD_BUS_VTABLE_UNPRIVILEGED),
508 SD_BUS_METHOD("TakeDevice", "uu", "hb", method_take_device, SD_BUS_VTABLE_UNPRIVILEGED),
509 SD_BUS_METHOD("ReleaseDevice", "uu", NULL, method_release_device, SD_BUS_VTABLE_UNPRIVILEGED),
510 SD_BUS_METHOD("PauseDeviceComplete", "uu", NULL, method_pause_device_complete, SD_BUS_VTABLE_UNPRIVILEGED),
512 SD_BUS_SIGNAL("PauseDevice", "uus", 0),
513 SD_BUS_SIGNAL("ResumeDevice", "uuh", 0),
514 SD_BUS_SIGNAL("Lock", NULL, 0),
515 SD_BUS_SIGNAL("Unlock", NULL, 0),
520 int session_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
521 Manager *m = userdata;
531 if (streq(path, "/org/freedesktop/login1/session/self")) {
532 sd_bus_message *message;
534 message = sd_bus_get_current_message(bus);
538 r = manager_get_session_from_creds(m, message, NULL, error, &session);
542 _cleanup_free_ char *e = NULL;
545 p = startswith(path, "/org/freedesktop/login1/session/");
549 e = bus_label_unescape(p);
553 session = hashmap_get(m->sessions, e);
562 char *session_bus_path(Session *s) {
563 _cleanup_free_ char *t = NULL;
567 t = bus_label_escape(s->id);
571 return strappend("/org/freedesktop/login1/session/", t);
574 int session_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
575 _cleanup_strv_free_ char **l = NULL;
576 sd_bus_message *message;
577 Manager *m = userdata;
586 HASHMAP_FOREACH(session, m->sessions, i) {
589 p = session_bus_path(session);
593 r = strv_consume(&l, p);
598 message = sd_bus_get_current_message(bus);
600 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
603 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
605 r = sd_bus_creds_get_session(creds, &name);
607 session = hashmap_get(m->sessions, name);
609 r = strv_extend(&l, "/org/freedesktop/login1/session/self");
617 *nodes = TAKE_PTR(l);
622 int session_send_signal(Session *s, bool new_session) {
623 _cleanup_free_ char *p = NULL;
627 p = session_bus_path(s);
631 return sd_bus_emit_signal(
633 "/org/freedesktop/login1",
634 "org.freedesktop.login1.Manager",
635 new_session ? "SessionNew" : "SessionRemoved",
639 int session_send_changed(Session *s, const char *properties, ...) {
640 _cleanup_free_ char *p = NULL;
648 p = session_bus_path(s);
652 l = strv_from_stdarg_alloca(properties);
654 return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Session", l);
657 int session_send_lock(Session *s, bool lock) {
658 _cleanup_free_ char *p = NULL;
662 p = session_bus_path(s);
666 return sd_bus_emit_signal(
669 "org.freedesktop.login1.Session",
670 lock ? "Lock" : "Unlock",
674 int session_send_lock_all(Manager *m, bool lock) {
681 HASHMAP_FOREACH(session, m->sessions, i) {
684 k = session_send_lock(session, lock);
692 int session_send_create_reply(Session *s, sd_bus_error *error) {
693 _cleanup_(sd_bus_message_unrefp) sd_bus_message *c = NULL;
694 _cleanup_close_ int fifo_fd = -1;
695 _cleanup_free_ char *p = NULL;
699 /* This is called after the session scope and the user service
700 * were successfully created, and finishes where
701 * bus_manager_create_session() left off. */
703 if (!s->create_message)
706 #if 0 /// elogind does not support scope and service jobs
707 if (!sd_bus_error_is_set(error) && (s->scope_job || s->user->service_job))
711 c = s->create_message;
712 s->create_message = NULL;
715 return sd_bus_reply_method_error(c, error);
717 fifo_fd = session_create_fifo(s);
721 /* Update the session state file before we notify the client
722 * about the result. */
725 #if 1 /// Additionally elogind saves the user state file
728 p = session_bus_path(s);
732 log_debug("Sending reply about created session: "
733 "id=%s object_path=%s uid=%u runtime_path=%s "
734 "session_fd=%d seat=%s vtnr=%u",
737 (uint32_t) s->user->uid,
738 s->user->runtime_path,
740 s->seat ? s->seat->id : "",
743 return sd_bus_reply_method_return(
747 s->user->runtime_path,
749 (uint32_t) s->user->uid,
750 s->seat ? s->seat->id : "",