logind-seat-dbus.c revision 309a29dfd24f4175de334ca1593e3fe2436ab082
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen This file is part of systemd.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Copyright 2011 Lennart Poettering
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is free software; you can redistribute it and/or modify it
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen under the terms of the GNU Lesser General Public License as published by
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen the Free Software Foundation; either version 2.1 of the License, or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen (at your option) any later version.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is distributed in the hope that it will be useful, but
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen WITHOUT ANY WARRANTY; without even the implied warranty of
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Lesser General Public License for more details.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen You should have received a copy of the GNU Lesser General Public License
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen#include "bus-common-errors.h"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int property_get_active_session(
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *p = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen p = s->active ? session_bus_path(s->active) : strdup("/");
d5d8429a12c4b1ef0dcd226c0904f00f4fa4898aLennart Poettering return sd_bus_message_append(reply, "(so)", s->active ? s->active->id : "", p);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int property_get_can_multi_session(
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_message_append(reply, "b", seat_can_multi_session(s));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int property_get_can_tty(
8fba1c8d4e3d05d2af2848b6570bdc09e725d06eZbigniew Jędrzejewski-Szmek return sd_bus_message_append(reply, "b", seat_can_tty(s));
8fba1c8d4e3d05d2af2848b6570bdc09e725d06eZbigniew Jędrzejewski-Szmekstatic int property_get_can_graphical(
8fba1c8d4e3d05d2af2848b6570bdc09e725d06eZbigniew Jędrzejewski-Szmek return sd_bus_message_append(reply, "b", seat_can_graphical(s));
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int property_get_sessions(
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(reply, 'a', "(so)");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen LIST_FOREACH(sessions_by_seat, session, s->sessions) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *p = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen p = session_bus_path(session);
b7e7184634d573fb73143210962acce205f37f61Michael Biebl r = sd_bus_message_append(reply, "(so)", session->id, p);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_message_append(reply, "b", seat_get_idle_hint(s, NULL) > 0);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int property_get_idle_since_hint(
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = seat_get_idle_hint(s, &t);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_message_append(reply, "t", u);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int method_terminate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = seat_stop_sessions(s, true);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_reply_method_return(message, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_read(message, "s", &name);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen session = hashmap_get(s->manager->sessions, name);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", name, s->id);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = session_activate(session);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_reply_method_return(message, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int method_switch_to(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_read(message, "u", &to);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_reply_method_return(message, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int method_switch_to_next(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_reply_method_return(message, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int method_switch_to_previous(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = seat_switch_to_previous(s);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_reply_method_return(message, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenconst sd_bus_vtable seat_vtable[] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SD_BUS_PROPERTY("Id", "s", NULL, offsetof(Seat, id), SD_BUS_VTABLE_PROPERTY_CONST),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SD_BUS_PROPERTY("ActiveSession", "(so)", property_get_active_session, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SD_BUS_PROPERTY("CanMultiSession", "b", property_get_can_multi_session, 0, SD_BUS_VTABLE_PROPERTY_CONST),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SD_BUS_PROPERTY("CanTTY", "b", property_get_can_tty, 0, SD_BUS_VTABLE_PROPERTY_CONST),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SD_BUS_PROPERTY("CanGraphical", "b", property_get_can_graphical, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
4e4885553447f6f4c014bfa3e5b5837a76a0e612Lennart Poettering SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
3315f085178f46155fda345d9526c09083b45946Lennart Poettering SD_BUS_METHOD("Terminate", NULL, NULL, method_terminate, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
4e4885553447f6f4c014bfa3e5b5837a76a0e612Lennart Poettering SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED),
3315f085178f46155fda345d9526c09083b45946Lennart Poettering SD_BUS_METHOD("SwitchTo", "u", NULL, method_switch_to, SD_BUS_VTABLE_UNPRIVILEGED),
3315f085178f46155fda345d9526c09083b45946Lennart Poettering SD_BUS_METHOD("SwitchToNext", NULL, NULL, method_switch_to_next, SD_BUS_VTABLE_UNPRIVILEGED),
29e0e6d8c1f7f648b7c998880d034eaa3e58c53aMartin Pitt SD_BUS_METHOD("SwitchToPrevious", NULL, NULL, method_switch_to_previous, SD_BUS_VTABLE_UNPRIVILEGED),
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poetteringint seat_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (streq(path, "/org/freedesktop/login1/seat/self")) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen message = sd_bus_get_current_message(bus);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds);
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmek r = sd_bus_creds_get_session(creds, &name);
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmek session = hashmap_get(m->sessions, name);
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmek p = startswith(path, "/org/freedesktop/login1/seat/");
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmek seat = hashmap_get(m->seats, e);
f2341e0a87cab1558c84c933956e9181d5fb6c52Lennart Poettering return strappend("/org/freedesktop/login1/seat/", t);
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmekint seat_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) {
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmek _cleanup_strv_free_ char **l = NULL;
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmek HASHMAP_FOREACH(seat, m->seats, i) {
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmek r = strv_extend(&l, "/org/freedesktop/login1/seat/self");
1e2fee5f757c0bb3a6fea216b1e10ee7b3c0e12eZbigniew Jędrzejewski-Szmekint seat_send_signal(Seat *s, bool new_seat) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "org.freedesktop.login1.Manager",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen new_seat ? "SeatNew" : "SeatRemoved",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint seat_send_changed(Seat *s, const char *properties, ...) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *p = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_emit_properties_changed_strv(s->manager->bus, p, "org.freedesktop.login1.Seat", l);