logind-session-dbus.c revision 4bba9156da3e1df2cee24d10d7cd88c776ef4179
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering/***
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering This file is part of systemd.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Copyright 2011 Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering systemd is free software; you can redistribute it and/or modify it
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering under the terms of the GNU General Public License as published by
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering the Free Software Foundation; either version 2 of the License, or
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering (at your option) any later version.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering systemd is distributed in the hope that it will be useful, but
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering General Public License for more details.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering You should have received a copy of the GNU General Public License
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering***/
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <errno.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <string.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "logind.h"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include "logind-session.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "dbus-common.h"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#include "util.h"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering#define BUS_SESSION_INTERFACE \
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering " <interface name=\"org.freedesktop.login1.Session\">\n" \
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering " <method name=\"Terminate\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <method name=\"Activate\"/>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering " <method name=\"Lock\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <method name=\"Unlock\"/>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering " <method name=\"SetIdleHint\">\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <arg name=\"b\" type=\"b\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " </method>\n" \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering " <method name=\"Kill\">\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <arg name=\"who\" type=\"s\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <arg name=\"signal\" type=\"s\"/>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering " </method>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"Id\" type=\"s\" access=\"read\"/>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering " <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering " <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering " <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering " <property name=\"TTY\" type=\"s\" access=\"read\"/>\n" \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering " <property name=\"Display\" type=\"s\" access=\"read\"/>\n" \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering " <property name=\"Remote\" type=\"b\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"Service\" type=\"s\" access=\"read\"/>\n" \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering " <property name=\"Leader\" type=\"u\" access=\"read\"/>\n" \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering " </interface>\n"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
115cbf7e209a86cf5317fa12d44566247b4af332Evgeny Vereshchagin#define INTROSPECTION \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering "<node>\n" \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering BUS_SESSION_INTERFACE \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering BUS_PROPERTIES_INTERFACE \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering BUS_PEER_INTERFACE \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering BUS_INTROSPECTABLE_INTERFACE \
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering "</node>\n"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering#define INTERFACES_LIST \
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering BUS_GENERIC_INTERFACES_LIST \
7410616cd9dbbec97cf98d75324da5cda2b2f7a2Lennart Poettering "org.freedesktop.login1.Session\0"
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering DBusMessageIter sub;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Session *s = data;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering const char *id, *path;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering char *p = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert(i);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering assert(property);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering assert(s);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return -ENOMEM;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (s->seat) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering id = s->seat->id;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering path = p = seat_bus_path(s->seat);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!p)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return -ENOMEM;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering } else {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering id = "";
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering path = "/";
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering }
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering free(p);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENOMEM;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering }
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering free(p);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering if (!dbus_message_iter_close_container(i, &sub))
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return -ENOMEM;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return 0;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering}
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poetteringstatic int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering DBusMessageIter sub;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering Session *s = data;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering char *p = NULL;
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering assert(i);
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering assert(property);
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering assert(s);
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering return -ENOMEM;
115cbf7e209a86cf5317fa12d44566247b4af332Evgeny Vereshchagin
115cbf7e209a86cf5317fa12d44566247b4af332Evgeny Vereshchagin p = user_bus_path(s->user);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!p)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering return -ENOMEM;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &s->user->uid) ||
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering free(p);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENOMEM;
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering }
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering free(p);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!dbus_message_iter_close_container(i, &sub))
b2c23da8cea1987a1a329f5a964d3299b7ca7890Lennart Poettering return -ENOMEM;
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering return 0;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
15411c0cb1192799b37ec8f25d6f30e8d7292fc6David Herrmannstatic int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Session *s = data;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering dbus_bool_t b;
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering assert(i);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering assert(property);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering assert(s);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering b = session_is_active(s);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENOMEM;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return 0;
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering}
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poetteringstatic int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering Session *s = data;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering int b;
067d851d30386c553e3a84f59d81d003ff638b91Daniel Wallace
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering assert(i);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering assert(property);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering assert(s);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering b = session_get_idle_hint(s, NULL) > 0;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENOMEM;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return 0;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering}
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringstatic int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering Session *s = data;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering dual_timestamp t;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering uint64_t u;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering assert(i);
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering assert(property);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering assert(s);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering session_get_idle_hint(s, &t);
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return -ENOMEM;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering return 0;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poetteringstatic DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poetteringstatic int get_session_for_path(Manager *m, const char *path, Session **_s) {
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering Session *s;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering char *id;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert(m);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert(path);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert(_s);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!startswith(path, "/org/freedesktop/login1/session/"))
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -EINVAL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering id = bus_path_unescape(path + 32);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!id)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENOMEM;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering s = hashmap_get(m->sessions, id);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering free(id);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!s)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return -ENOENT;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering *_s = s;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return 0;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering}
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poetteringstatic DBusHandlerResult session_message_dispatch(
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering Session *s,
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering DBusConnection *connection,
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering DBusMessage *message) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering const BusProperty properties[] = {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "Id", bus_property_append_string, "s", s->id },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "User", bus_session_append_user, "(uo)", s },
dca348bcbb462305864526c587495a14a76bfcdeJan Engelhardt { "org.freedesktop.login1.Session", "Name", bus_property_append_string, "s", s->user->name },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "Timestamp", bus_property_append_usec, "t", &s->timestamp.realtime },
2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2Lennart Poettering { "org.freedesktop.login1.Session", "TimestampMonotonic", bus_property_append_usec, "t", &s->timestamp.monotonic },
9444b1f20e311f073864d81e913bd4f32fe95cfdLennart Poettering { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string, "s", s->cgroup_path },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32, "u", &s->vtnr },
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat, "(so)", s },
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering { "org.freedesktop.login1.Session", "TTY", bus_property_append_string, "s", s->tty },
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering { "org.freedesktop.login1.Session", "Display", bus_property_append_string, "s", s->display },
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool, "b", &s->remote },
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string, "s", s->remote_user },
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string, "s", s->remote_host },
7584d236eac91f9b7128b1eb08bddf18be2bce9fZbigniew Jędrzejewski-Szmek { "org.freedesktop.login1.Session", "Service", bus_property_append_string, "s", s->service },
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid, "u", &s->leader },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32, "u", &s->audit_id },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "Type", bus_session_append_type, "s", &s->type },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "Active", bus_session_append_active, "b", s },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv, "as", s->controllers },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv, "as", s->reset_controllers },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool, "b", &s->kill_processes },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "IdleHint", bus_session_append_idle_hint, "b", s },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "IdleSinceHint", bus_session_append_idle_hint_since, "t", s },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { "org.freedesktop.login1.Session", "IdleSinceHintMonotonic", bus_session_append_idle_hint_since, "t", s },
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering { NULL, NULL, NULL, NULL, NULL }
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering };
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
696fd1ef4f2f8574e349332a16987c6772641eddLennart Poettering DBusError error;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering DBusMessage *reply = NULL;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering int r;
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering
79413b673b45adc98dfeaec882bbdda2343cb2f9Lennart Poettering assert(s);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert(connection);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering assert(message);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering dbus_error_init(&error);
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) {
6569cae18ed640a4e9f52f73e2a3ec54b07d0406Lennart Poettering
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek r = session_stop(s);
6aaa8c2f783cd1b3ac27c5ce40625d032e7e3d71Zbigniew Jędrzejewski-Szmek if (r < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return bus_send_error_reply(connection, message, NULL, r);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering reply = dbus_message_new_method_return(message);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!reply)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering goto oom;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering r = session_activate(s);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (r < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return bus_send_error_reply(connection, message, NULL, r);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering reply = dbus_message_new_method_return(message);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!reply)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering goto oom;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") ||
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (session_send_signal(s, streq(dbus_message_get_member(message), "Lock")) < 0)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering goto oom;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering reply = dbus_message_new_method_return(message);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering if (!reply)
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering goto oom;
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) {
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering dbus_bool_t b;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering unsigned long ul;
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (!dbus_message_get_args(
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering message,
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering &error,
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering DBUS_TYPE_BOOLEAN, &b,
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering DBUS_TYPE_INVALID))
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return bus_send_error_reply(connection, message, &error, -EINVAL);
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
19f6d710772305610b928bc2678b9d77fe11e770Lennart Poettering if (ul == (unsigned long) -1)
41f9172f427bdbb8221c64029f78364b8dd4e527Lennart Poettering return bus_send_error_reply(connection, message, &error, -EIO);
if (ul != 0 && ul != s->user->uid)
return bus_send_error_reply(connection, message, NULL, -EPERM);
session_set_idle_hint(s, b);
reply = dbus_message_new_method_return(message);
if (!reply)
goto oom;
} else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Kill")) {
const char *swho;
int32_t signo;
KillWho who;
if (!dbus_message_get_args(
message,
&error,
DBUS_TYPE_STRING, &swho,
DBUS_TYPE_INT32, &signo,
DBUS_TYPE_INVALID))
return bus_send_error_reply(connection, message, &error, -EINVAL);
if (isempty(swho))
who = KILL_ALL;
else {
who = kill_who_from_string(swho);
if (who < 0)
return bus_send_error_reply(connection, message, &error, -EINVAL);
}
if (signo <= 0 || signo >= _NSIG)
return bus_send_error_reply(connection, message, &error, -EINVAL);
r = session_kill(s, who, signo);
if (r < 0)
return bus_send_error_reply(connection, message, NULL, r);
reply = dbus_message_new_method_return(message);
if (!reply)
goto oom;
} else
return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
if (reply) {
if (!dbus_connection_send(connection, reply, NULL))
goto oom;
dbus_message_unref(reply);
}
return DBUS_HANDLER_RESULT_HANDLED;
oom:
if (reply)
dbus_message_unref(reply);
dbus_error_free(&error);
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
static DBusHandlerResult session_message_handler(
DBusConnection *connection,
DBusMessage *message,
void *userdata) {
Manager *m = userdata;
Session *s;
int r;
r = get_session_for_path(m, dbus_message_get_path(message), &s);
if (r < 0) {
if (r == -ENOMEM)
return DBUS_HANDLER_RESULT_NEED_MEMORY;
if (r == -ENOENT) {
DBusError e;
dbus_error_init(&e);
dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
return bus_send_error_reply(connection, message, &e, r);
}
return bus_send_error_reply(connection, message, NULL, r);
}
return session_message_dispatch(s, connection, message);
}
const DBusObjectPathVTable bus_session_vtable = {
.message_function = session_message_handler
};
char *session_bus_path(Session *s) {
char *t, *r;
assert(s);
t = bus_path_escape(s->id);
if (!t)
return NULL;
r = strappend("/org/freedesktop/login1/session/", t);
free(t);
return r;
}
int session_send_signal(Session *s, bool new_session) {
DBusMessage *m;
int r = -ENOMEM;
char *p = NULL;
assert(s);
m = dbus_message_new_signal("/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
new_session ? "SessionNew" : "SessionRemoved");
if (!m)
return -ENOMEM;
p = session_bus_path(s);
if (!p)
goto finish;
if (!dbus_message_append_args(
m,
DBUS_TYPE_STRING, &s->id,
DBUS_TYPE_OBJECT_PATH, &p,
DBUS_TYPE_INVALID))
goto finish;
if (!dbus_connection_send(s->manager->bus, m, NULL))
goto finish;
r = 0;
finish:
dbus_message_unref(m);
free(p);
return r;
}
int session_send_changed(Session *s, const char *properties) {
DBusMessage *m;
int r = -ENOMEM;
char *p = NULL;
assert(s);
if (!s->started)
return 0;
p = session_bus_path(s);
if (!p)
return -ENOMEM;
m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties);
if (!m)
goto finish;
if (!dbus_connection_send(s->manager->bus, m, NULL))
goto finish;
r = 0;
finish:
if (m)
dbus_message_unref(m);
free(p);
return r;
}
int session_send_lock(Session *s, bool lock) {
DBusMessage *m;
bool b;
char *p;
assert(s);
p = session_bus_path(s);
if (!p)
return -ENOMEM;
m = dbus_message_new_signal(p, "org.freedesktop.login1.Session", lock ? "Lock" : "Unlock");
free(p);
if (!m)
return -ENOMEM;
b = dbus_connection_send(s->manager->bus, m, NULL);
dbus_message_unref(m);
if (!b)
return -ENOMEM;
return 0;
}