1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "logind-session.h"
26 #include "dbus-common.h"
29 #define BUS_SESSION_INTERFACE \
30 " <interface name=\"org.freedesktop.login1.Session\">\n" \
31 " <method name=\"Terminate\"/>\n" \
32 " <method name=\"Activate\"/>\n" \
33 " <property name=\"Id\" type=\"u\" access=\"read\"/>\n" \
34 " <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n" \
35 " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \
36 " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
37 " <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n" \
38 " <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n" \
39 " <property name=\"TTY\" type=\"s\" access=\"read\"/>\n" \
40 " <property name=\"Display\" type=\"s\" access=\"read\"/>\n" \
41 " <property name=\"Remote\" type=\"b\" access=\"read\"/>\n" \
42 " <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
43 " <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
44 " <property name=\"Leader\" type=\"u\" access=\"read\"/>\n" \
45 " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \
46 " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \
47 " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \
48 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
49 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
50 " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
53 #define INTROSPECTION \
54 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
56 BUS_SESSION_INTERFACE \
57 BUS_PROPERTIES_INTERFACE \
59 BUS_INTROSPECTABLE_INTERFACE \
62 #define INTERFACES_LIST \
63 BUS_GENERIC_INTERFACES_LIST \
64 "org.freedesktop.login1.Session\0"
66 static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
69 const char *id, *path;
76 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
81 path = p = seat_bus_path(s->seat);
90 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
91 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
98 if (!dbus_message_iter_close_container(i, &sub))
104 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
113 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
116 p = user_bus_path(s->user);
120 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &s->user->uid) ||
121 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
128 if (!dbus_message_iter_close_container(i, &sub))
134 static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
142 b = session_is_active(s);
143 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
149 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
151 static int get_session_for_path(Manager *m, const char *path, Session **_s) {
159 if (!startswith(path, "/org/freedesktop/login1/session/"))
162 id = bus_path_unescape(path + 32);
166 s = hashmap_get(m->sessions, id);
176 static DBusHandlerResult session_message_dispatch(
178 DBusConnection *connection,
179 DBusMessage *message) {
181 const BusProperty properties[] = {
182 { "org.freedesktop.login1.Session", "Id", bus_property_append_string, "s", s->id },
183 { "org.freedesktop.login1.Session", "User", bus_session_append_user, "(uo)", s },
184 { "org.freedesktop.login1.Session", "Name", bus_property_append_string, "s", s->user->name },
185 { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string, "s", s->cgroup_path },
186 { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32, "u", &s->vtnr },
187 { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat, "(so)", s },
188 { "org.freedesktop.login1.Session", "TTY", bus_property_append_string, "s", s->tty },
189 { "org.freedesktop.login1.Session", "Display", bus_property_append_string, "s", s->display },
190 { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool, "b", &s->remote },
191 { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string, "s", s->remote_user },
192 { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string, "s", s->remote_host },
193 { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid, "u", &s->leader },
194 { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32, "u", &s->audit_id },
195 { "org.freedesktop.login1.Session", "Type", bus_session_append_type, "s", &s->type },
196 { "org.freedesktop.login1.Session", "Active", bus_session_append_active, "b", s },
197 { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv, "as", s->controllers },
198 { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv, "as", s->reset_controllers },
199 { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool, "b", &s->kill_processes },
200 { NULL, NULL, NULL, NULL, NULL }
207 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
210 static DBusHandlerResult session_message_handler(
211 DBusConnection *connection,
212 DBusMessage *message,
215 Manager *m = userdata;
219 r = get_session_for_path(m, dbus_message_get_path(message), &s);
223 return DBUS_HANDLER_RESULT_NEED_MEMORY;
229 dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
230 return bus_send_error_reply(connection, message, &e, r);
233 return bus_send_error_reply(connection, message, NULL, r);
236 return session_message_dispatch(s, connection, message);
239 const DBusObjectPathVTable bus_session_vtable = {
240 .message_function = session_message_handler
243 char *session_bus_path(Session *s) {
248 t = bus_path_escape(s->id);
252 r = strappend("/org/freedesktop/login1/session/", t);