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/>.
26 #include "logind-session.h"
27 #include "dbus-common.h"
30 #define BUS_SESSION_INTERFACE \
31 " <interface name=\"org.freedesktop.login1.Session\">\n" \
32 " <method name=\"Terminate\"/>\n" \
33 " <method name=\"Activate\"/>\n" \
34 " <method name=\"Lock\"/>\n" \
35 " <method name=\"Unlock\"/>\n" \
36 " <method name=\"SetIdleHint\">\n" \
37 " <arg name=\"b\" type=\"b\"/>\n" \
39 " <property name=\"Id\" type=\"u\" access=\"read\"/>\n" \
40 " <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n" \
41 " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \
42 " <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \
43 " <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
44 " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
45 " <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n" \
46 " <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n" \
47 " <property name=\"TTY\" type=\"s\" access=\"read\"/>\n" \
48 " <property name=\"Display\" type=\"s\" access=\"read\"/>\n" \
49 " <property name=\"Remote\" type=\"b\" access=\"read\"/>\n" \
50 " <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
51 " <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
52 " <property name=\"Service\" type=\"s\" access=\"read\"/>\n" \
53 " <property name=\"Leader\" type=\"u\" access=\"read\"/>\n" \
54 " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \
55 " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \
56 " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \
57 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
58 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
59 " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
60 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
61 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
62 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
65 #define INTROSPECTION \
66 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
68 BUS_SESSION_INTERFACE \
69 BUS_PROPERTIES_INTERFACE \
71 BUS_INTROSPECTABLE_INTERFACE \
74 #define INTERFACES_LIST \
75 BUS_GENERIC_INTERFACES_LIST \
76 "org.freedesktop.login1.Session\0"
78 static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
81 const char *id, *path;
88 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
93 path = p = seat_bus_path(s->seat);
102 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
103 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
110 if (!dbus_message_iter_close_container(i, &sub))
116 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
125 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
128 p = user_bus_path(s->user);
132 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &s->user->uid) ||
133 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
140 if (!dbus_message_iter_close_container(i, &sub))
146 static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
154 b = session_is_active(s);
155 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
161 static int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
169 b = session_get_idle_hint(s, NULL) > 0;
170 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
176 static int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
185 session_get_idle_hint(s, &t);
186 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
188 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
194 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
196 static int get_session_for_path(Manager *m, const char *path, Session **_s) {
204 if (!startswith(path, "/org/freedesktop/login1/session/"))
207 id = bus_path_unescape(path + 32);
211 s = hashmap_get(m->sessions, id);
221 static DBusHandlerResult session_message_dispatch(
223 DBusConnection *connection,
224 DBusMessage *message) {
226 const BusProperty properties[] = {
227 { "org.freedesktop.login1.Session", "Id", bus_property_append_string, "s", s->id },
228 { "org.freedesktop.login1.Session", "User", bus_session_append_user, "(uo)", s },
229 { "org.freedesktop.login1.Session", "Name", bus_property_append_string, "s", s->user->name },
230 { "org.freedesktop.login1.Session", "Timestamp", bus_property_append_usec, "t", &s->timestamp.realtime },
231 { "org.freedesktop.login1.Session", "TimestampMonotonic", bus_property_append_usec, "t", &s->timestamp.monotonic },
232 { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string, "s", s->cgroup_path },
233 { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32, "u", &s->vtnr },
234 { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat, "(so)", s },
235 { "org.freedesktop.login1.Session", "TTY", bus_property_append_string, "s", s->tty },
236 { "org.freedesktop.login1.Session", "Display", bus_property_append_string, "s", s->display },
237 { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool, "b", &s->remote },
238 { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string, "s", s->remote_user },
239 { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string, "s", s->remote_host },
240 { "org.freedesktop.login1.Session", "Service", bus_property_append_string, "s", s->service },
241 { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid, "u", &s->leader },
242 { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32, "u", &s->audit_id },
243 { "org.freedesktop.login1.Session", "Type", bus_session_append_type, "s", &s->type },
244 { "org.freedesktop.login1.Session", "Active", bus_session_append_active, "b", s },
245 { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv, "as", s->controllers },
246 { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv, "as", s->reset_controllers },
247 { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool, "b", &s->kill_processes },
248 { "org.freedesktop.login1.Session", "IdleHint", bus_session_append_idle_hint, "b", s },
249 { "org.freedesktop.login1.Session", "IdleSinceHint", bus_session_append_idle_hint_since, "t", s },
250 { "org.freedesktop.login1.Session", "IdleSinceHintMonotonic", bus_session_append_idle_hint_since, "t", s },
251 { NULL, NULL, NULL, NULL, NULL }
255 DBusMessage *reply = NULL;
262 dbus_error_init(&error);
264 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) {
268 return bus_send_error_reply(connection, message, NULL, r);
270 reply = dbus_message_new_method_return(message);
274 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) {
276 r = session_activate(s);
278 return bus_send_error_reply(connection, message, NULL, r);
280 reply = dbus_message_new_method_return(message);
284 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") ||
285 dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) {
289 sig = dbus_message_new_signal(dbus_message_get_path(message), "org.freedesktop.login1.Session", dbus_message_get_member(message));
293 b = dbus_connection_send(connection, sig, NULL);
294 dbus_message_unref(sig);
299 reply = dbus_message_new_method_return(message);
303 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) {
306 if (!dbus_message_get_args(
309 DBUS_TYPE_BOOLEAN, &b,
311 return bus_send_error_reply(connection, message, &error, -EINVAL);
313 session_set_idle_hint(s, b);
315 reply = dbus_message_new_method_return(message);
320 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
323 if (!dbus_connection_send(connection, reply, NULL))
326 dbus_message_unref(reply);
329 return DBUS_HANDLER_RESULT_HANDLED;
333 dbus_message_unref(reply);
335 dbus_error_free(&error);
337 return DBUS_HANDLER_RESULT_NEED_MEMORY;
340 static DBusHandlerResult session_message_handler(
341 DBusConnection *connection,
342 DBusMessage *message,
345 Manager *m = userdata;
349 r = get_session_for_path(m, dbus_message_get_path(message), &s);
353 return DBUS_HANDLER_RESULT_NEED_MEMORY;
359 dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
360 return bus_send_error_reply(connection, message, &e, r);
363 return bus_send_error_reply(connection, message, NULL, r);
366 return session_message_dispatch(s, connection, message);
369 const DBusObjectPathVTable bus_session_vtable = {
370 .message_function = session_message_handler
373 char *session_bus_path(Session *s) {
378 t = bus_path_escape(s->id);
382 r = strappend("/org/freedesktop/login1/session/", t);
388 int session_send_signal(Session *s, bool new_session) {
395 m = dbus_message_new_signal("/org/freedesktop/login1",
396 "org.freedesktop.login1.Manager",
397 new_session ? "SessionNew" : "SessionRemoved");
402 p = session_bus_path(s);
406 if (!dbus_message_append_args(
408 DBUS_TYPE_STRING, &s->id,
409 DBUS_TYPE_OBJECT_PATH, &p,
413 if (!dbus_connection_send(s->manager->bus, m, NULL))
419 dbus_message_unref(m);
425 int session_send_changed(Session *s, const char *properties) {
435 p = session_bus_path(s);
439 m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties);
443 if (!dbus_connection_send(s->manager->bus, m, NULL))
450 dbus_message_unref(m);