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=\"Leader\" type=\"u\" access=\"read\"/>\n" \
53 " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \
54 " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \
55 " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \
56 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
57 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
58 " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
59 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
60 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
61 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
64 #define INTROSPECTION \
65 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
67 BUS_SESSION_INTERFACE \
68 BUS_PROPERTIES_INTERFACE \
70 BUS_INTROSPECTABLE_INTERFACE \
73 #define INTERFACES_LIST \
74 BUS_GENERIC_INTERFACES_LIST \
75 "org.freedesktop.login1.Session\0"
77 static int bus_session_append_seat(DBusMessageIter
*i
, const char *property
, void *data
) {
80 const char *id
, *path
;
87 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_STRUCT
, NULL
, &sub
))
92 path
= p
= seat_bus_path(s
->seat
);
101 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, &id
) ||
102 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &path
)) {
109 if (!dbus_message_iter_close_container(i
, &sub
))
115 static int bus_session_append_user(DBusMessageIter
*i
, const char *property
, void *data
) {
124 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_STRUCT
, NULL
, &sub
))
127 p
= user_bus_path(s
->user
);
131 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_UINT32
, &s
->user
->uid
) ||
132 !dbus_message_iter_append_basic(&sub
, DBUS_TYPE_OBJECT_PATH
, &p
)) {
139 if (!dbus_message_iter_close_container(i
, &sub
))
145 static int bus_session_append_active(DBusMessageIter
*i
, const char *property
, void *data
) {
153 b
= session_is_active(s
);
154 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
160 static int bus_session_append_idle_hint(DBusMessageIter
*i
, const char *property
, void *data
) {
168 b
= session_get_idle_hint(s
, NULL
) > 0;
169 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &b
))
175 static int bus_session_append_idle_hint_since(DBusMessageIter
*i
, const char *property
, void *data
) {
184 session_get_idle_hint(s
, &t
);
185 u
= streq(property
, "IdleSinceHint") ? t
.realtime
: t
.monotonic
;
187 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_UINT64
, &u
))
193 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type
, session_type
, SessionType
);
195 static int get_session_for_path(Manager
*m
, const char *path
, Session
**_s
) {
203 if (!startswith(path
, "/org/freedesktop/login1/session/"))
206 id
= bus_path_unescape(path
+ 32);
210 s
= hashmap_get(m
->sessions
, id
);
220 static DBusHandlerResult
session_message_dispatch(
222 DBusConnection
*connection
,
223 DBusMessage
*message
) {
225 const BusProperty properties
[] = {
226 { "org.freedesktop.login1.Session", "Id", bus_property_append_string
, "s", s
->id
},
227 { "org.freedesktop.login1.Session", "User", bus_session_append_user
, "(uo)", s
},
228 { "org.freedesktop.login1.Session", "Name", bus_property_append_string
, "s", s
->user
->name
},
229 { "org.freedesktop.login1.Session", "Timestamp", bus_property_append_usec
, "t", &s
->timestamp
.realtime
},
230 { "org.freedesktop.login1.Session", "TimestampMonotonic", bus_property_append_usec
, "t", &s
->timestamp
.monotonic
},
231 { "org.freedesktop.login1.Session", "ControlGroupPath", bus_property_append_string
, "s", s
->cgroup_path
},
232 { "org.freedesktop.login1.Session", "VTNr", bus_property_append_uint32
, "u", &s
->vtnr
},
233 { "org.freedesktop.login1.Session", "Seat", bus_session_append_seat
, "(so)", s
},
234 { "org.freedesktop.login1.Session", "TTY", bus_property_append_string
, "s", s
->tty
},
235 { "org.freedesktop.login1.Session", "Display", bus_property_append_string
, "s", s
->display
},
236 { "org.freedesktop.login1.Session", "Remote", bus_property_append_bool
, "b", &s
->remote
},
237 { "org.freedesktop.login1.Session", "RemoteUser", bus_property_append_string
, "s", s
->remote_user
},
238 { "org.freedesktop.login1.Session", "RemoteHost", bus_property_append_string
, "s", s
->remote_host
},
239 { "org.freedesktop.login1.Session", "Leader", bus_property_append_pid
, "u", &s
->leader
},
240 { "org.freedesktop.login1.Session", "Audit", bus_property_append_uint32
, "u", &s
->audit_id
},
241 { "org.freedesktop.login1.Session", "Type", bus_session_append_type
, "s", &s
->type
},
242 { "org.freedesktop.login1.Session", "Active", bus_session_append_active
, "b", s
},
243 { "org.freedesktop.login1.Session", "Controllers", bus_property_append_strv
, "as", s
->controllers
},
244 { "org.freedesktop.login1.Session", "ResetControllers", bus_property_append_strv
, "as", s
->reset_controllers
},
245 { "org.freedesktop.login1.Session", "KillProcesses", bus_property_append_bool
, "b", &s
->kill_processes
},
246 { "org.freedesktop.login1.Session", "IdleHint", bus_session_append_idle_hint
, "b", s
},
247 { "org.freedesktop.login1.Session", "IdleSinceHint", bus_session_append_idle_hint_since
, "t", s
},
248 { "org.freedesktop.login1.Session", "IdleSinceHintMonotonic", bus_session_append_idle_hint_since
, "t", s
},
249 { NULL
, NULL
, NULL
, NULL
, NULL
}
253 DBusMessage
*reply
= NULL
;
260 dbus_error_init(&error
);
262 if (dbus_message_is_method_call(message
, "org.freedesktop.login1.Session", "Terminate")) {
266 return bus_send_error_reply(connection
, message
, NULL
, r
);
268 reply
= dbus_message_new_method_return(message
);
272 } else if (dbus_message_is_method_call(message
, "org.freedesktop.login1.Session", "Activate")) {
274 r
= session_activate(s
);
276 return bus_send_error_reply(connection
, message
, NULL
, r
);
278 reply
= dbus_message_new_method_return(message
);
282 } else if (dbus_message_is_method_call(message
, "org.freedesktop.login1.Session", "Lock") ||
283 dbus_message_is_method_call(message
, "org.freedesktop.login1.Session", "Unlock")) {
287 sig
= dbus_message_new_signal(dbus_message_get_path(message
), "org.freedesktop.login1.Session", dbus_message_get_member(message
));
291 b
= dbus_connection_send(connection
, sig
, NULL
);
292 dbus_message_unref(sig
);
297 reply
= dbus_message_new_method_return(message
);
301 } else if (dbus_message_is_method_call(message
, "org.freedesktop.login1.Session", "SetIdleHint")) {
304 if (!dbus_message_get_args(
307 DBUS_TYPE_BOOLEAN
, &b
,
309 return bus_send_error_reply(connection
, message
, &error
, -EINVAL
);
311 session_set_idle_hint(s
, b
);
313 reply
= dbus_message_new_method_return(message
);
318 return bus_default_message_handler(connection
, message
, INTROSPECTION
, INTERFACES_LIST
, properties
);
321 if (!dbus_connection_send(connection
, reply
, NULL
))
324 dbus_message_unref(reply
);
327 return DBUS_HANDLER_RESULT_HANDLED
;
331 dbus_message_unref(reply
);
333 dbus_error_free(&error
);
335 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
338 static DBusHandlerResult
session_message_handler(
339 DBusConnection
*connection
,
340 DBusMessage
*message
,
343 Manager
*m
= userdata
;
347 r
= get_session_for_path(m
, dbus_message_get_path(message
), &s
);
351 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
357 dbus_set_error_const(&e
, DBUS_ERROR_UNKNOWN_OBJECT
, "Unknown session");
358 return bus_send_error_reply(connection
, message
, &e
, r
);
361 return bus_send_error_reply(connection
, message
, NULL
, r
);
364 return session_message_dispatch(s
, connection
, message
);
367 const DBusObjectPathVTable bus_session_vtable
= {
368 .message_function
= session_message_handler
371 char *session_bus_path(Session
*s
) {
376 t
= bus_path_escape(s
->id
);
380 r
= strappend("/org/freedesktop/login1/session/", t
);
386 int session_send_signal(Session
*s
, bool new_session
) {
393 m
= dbus_message_new_signal("/org/freedesktop/login1",
394 "org.freedesktop.login1.Manager",
395 new_session
? "SessionNew" : "SessionRemoved");
400 p
= session_bus_path(s
);
404 if (!dbus_message_append_args(
406 DBUS_TYPE_STRING
, &s
->id
,
407 DBUS_TYPE_OBJECT_PATH
, &p
,
411 if (!dbus_connection_send(s
->manager
->bus
, m
, NULL
))
417 dbus_message_unref(m
);
423 int session_send_changed(Session
*s
, const char *properties
) {
430 p
= session_bus_path(s
);
434 m
= bus_properties_changed_new(p
, "org.freedesktop.login1.Session", properties
);
438 if (!dbus_connection_send(s
->manager
->bus
, m
, NULL
))
445 dbus_message_unref(m
);