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 " <method name=\"Lock\"/>\n" \
34 " <method name=\"Unlock\"/>\n" \
35 " <method name=\"SetIdleHint\">\n" \
36 " <arg name=\"b\" type=\"b\"/>\n" \
38 " <property name=\"Id\" type=\"u\" access=\"read\"/>\n" \
39 " <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n" \
40 " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \
41 " <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \
42 " <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
43 " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
44 " <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n" \
45 " <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n" \
46 " <property name=\"TTY\" type=\"s\" access=\"read\"/>\n" \
47 " <property name=\"Display\" type=\"s\" access=\"read\"/>\n" \
48 " <property name=\"Remote\" type=\"b\" access=\"read\"/>\n" \
49 " <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
50 " <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
51 " <property name=\"Leader\" type=\"u\" access=\"read\"/>\n" \
52 " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \
53 " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \
54 " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \
55 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
56 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
57 " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
58 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
59 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
60 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
63 #define INTROSPECTION \
64 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
66 BUS_SESSION_INTERFACE \
67 BUS_PROPERTIES_INTERFACE \
69 BUS_INTROSPECTABLE_INTERFACE \
72 #define INTERFACES_LIST \
73 BUS_GENERIC_INTERFACES_LIST \
74 "org.freedesktop.login1.Session\0"
76 static int bus_session_append_seat(DBusMessageIter
*i
, const char *property
, void *data
) {
79 const char *id
, *path
;
86 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_STRUCT
, NULL
, &sub
))
91 path
= p
= seat_bus_path(s
->seat
);
100 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &id
) ||
101 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &path
)) {
108 if (!dbus_message_iter_close_container(i
, &sub
))
114 static int bus_session_append_user(DBusMessageIter
*i
, const char *property
, void *data
) {
123 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_STRUCT
, NULL
, &sub
))
126 p
= user_bus_path(s
->user
);
130 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_UINT32
, &s
->user
->uid
) ||
131 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
138 if (!dbus_message_iter_close_container(i
, &sub
))
144 static int bus_session_append_active(DBusMessageIter
*i
, const char *property
, void *data
) {
152 b
= session_is_active(s
);
153 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
159 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type
, session_type
, SessionType
);
161 static int get_session_for_path(Manager
*m
, const char *path
, Session
**_s
) {
169 if (!startswith(path
, "/org/freedesktop/login1/session/"))
172 id
= bus_path_unescape(path
+ 32);
176 s
= hashmap_get(m
->sessions
, id
);
186 static DBusHandlerResult
session_message_dispatch(
188 DBusConnection
*connection
,
189 DBusMessage
*message
) {
191 const BusProperty properties
[] = {
192 { "org.freedesktop.login1.Session", "Id", bus_property_append_string
, "s", s
->id
},
193 { "org.freedesktop.login1.Session", "User", bus_session_append_user
, "(uo)", s
},
194 { "org.freedesktop.login1.Session", "Name", bus_property_append_string
, "s", s
->user
->name
},
195 { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string
, "s", s
->cgroup_path
},
196 { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32
, "u", &s
->vtnr
},
197 { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat
, "(so)", s
},
198 { "org.freedesktop.login1.Session", "TTY", bus_property_append_string
, "s", s
->tty
},
199 { "org.freedesktop.login1.Session", "Display", bus_property_append_string
, "s", s
->display
},
200 { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool
, "b", &s
->remote
},
201 { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string
, "s", s
->remote_user
},
202 { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string
, "s", s
->remote_host
},
203 { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid
, "u", &s
->leader
},
204 { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32
, "u", &s
->audit_id
},
205 { "org.freedesktop.login1.Session", "Type", bus_session_append_type
, "s", &s
->type
},
206 { "org.freedesktop.login1.Session", "Active", bus_session_append_active
, "b", s
},
207 { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv
, "as", s
->controllers
},
208 { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv
, "as", s
->reset_controllers
},
209 { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool
, "b", &s
->kill_processes
},
210 { NULL
, NULL
, NULL
, NULL
, NULL
}
217 return bus_default_message_handler(connection
, message
, INTROSPECTION
, INTERFACES_LIST
, properties
);
220 static DBusHandlerResult
session_message_handler(
221 DBusConnection
*connection
,
222 DBusMessage
*message
,
225 Manager
*m
= userdata
;
229 r
= get_session_for_path(m
, dbus_message_get_path(message
), &s
);
233 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
239 dbus_set_error_const(&e
, DBUS_ERROR_UNKNOWN_OBJECT
, "Unknown session");
240 return bus_send_error_reply(connection
, message
, &e
, r
);
243 return bus_send_error_reply(connection
, message
, NULL
, r
);
246 return session_message_dispatch(s
, connection
, message
);
249 const DBusObjectPathVTable bus_session_vtable
= {
250 .message_function
= session_message_handler
253 char *session_bus_path(Session
*s
) {
258 t
= bus_path_escape(s
->id
);
262 r
= strappend("/org/freedesktop/login1/session/", t
);