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 " <method name=\"Kill\">\n" \
40 " <arg name=\"who\" type=\"s\"/>\n" \
41 " <arg name=\"signal\" type=\"s\"/>\n" \
43 " <property name=\"Id\" type=\"s\" access=\"read\"/>\n" \
44 " <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n" \
45 " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \
46 " <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \
47 " <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
48 " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
49 " <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n" \
50 " <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n" \
51 " <property name=\"TTY\" type=\"s\" access=\"read\"/>\n" \
52 " <property name=\"Display\" type=\"s\" access=\"read\"/>\n" \
53 " <property name=\"Remote\" type=\"b\" access=\"read\"/>\n" \
54 " <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
55 " <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
56 " <property name=\"Service\" type=\"s\" access=\"read\"/>\n" \
57 " <property name=\"Leader\" type=\"u\" access=\"read\"/>\n" \
58 " <property name=\"Audit\" type=\"u\" access=\"read\"/>\n" \
59 " <property name=\"Type\" type=\"s\" access=\"read\"/>\n" \
60 " <property name=\"Active\" type=\"b\" access=\"read\"/>\n" \
61 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
62 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
63 " <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
64 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
65 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
66 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
69 #define INTROSPECTION \
70 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
72 BUS_SESSION_INTERFACE \
73 BUS_PROPERTIES_INTERFACE \
75 BUS_INTROSPECTABLE_INTERFACE \
78 #define INTERFACES_LIST \
79 BUS_GENERIC_INTERFACES_LIST \
80 "org.freedesktop.login1.Session\0"
82 static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
85 const char *id, *path;
92 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
97 path = p = seat_bus_path(s->seat);
106 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
107 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
114 if (!dbus_message_iter_close_container(i, &sub))
120 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
129 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
132 p = user_bus_path(u);
136 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->uid) ||
137 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
144 if (!dbus_message_iter_close_container(i, &sub))
150 static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
158 b = session_is_active(s);
159 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
165 static int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
173 b = session_get_idle_hint(s, NULL) > 0;
174 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
180 static int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
189 session_get_idle_hint(s, &t);
190 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
192 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
198 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
200 static int get_session_for_path(Manager *m, const char *path, Session **_s) {
208 if (!startswith(path, "/org/freedesktop/login1/session/"))
211 id = bus_path_unescape(path + 32);
215 s = hashmap_get(m->sessions, id);
225 static const BusProperty bus_login_session_properties[] = {
226 { "Id", bus_property_append_string, "s", offsetof(Session, id), true },
227 { "Timestamp", bus_property_append_usec, "t", offsetof(Session, timestamp.realtime) },
228 { "TimestampMonotonic", bus_property_append_usec, "t", offsetof(Session, timestamp.monotonic) },
229 { "ControlGroupPath", bus_property_append_string, "s", offsetof(Session, cgroup_path), true },
230 { "VTNr", bus_property_append_uint32, "u", offsetof(Session, vtnr) },
231 { "Seat", bus_session_append_seat, "(so)", 0 },
232 { "TTY", bus_property_append_string, "s", offsetof(Session, tty), true },
233 { "Display", bus_property_append_string, "s", offsetof(Session, display), true },
234 { "Remote", bus_property_append_bool, "b", offsetof(Session, remote) },
235 { "RemoteUser", bus_property_append_string, "s", offsetof(Session, remote_user), true },
236 { "RemoteHost", bus_property_append_string, "s", offsetof(Session, remote_host), true },
237 { "Service", bus_property_append_string, "s", offsetof(Session, service), true },
238 { "Leader", bus_property_append_pid, "u", offsetof(Session, leader) },
239 { "Audit", bus_property_append_uint32, "u", offsetof(Session, audit_id) },
240 { "Type", bus_session_append_type, "s", offsetof(Session, type) },
241 { "Active", bus_session_append_active, "b", 0 },
242 { "Controllers", bus_property_append_strv, "as", offsetof(Session, controllers), true },
243 { "ResetControllers", bus_property_append_strv, "as", offsetof(Session, reset_controllers), true },
244 { "KillProcesses", bus_property_append_bool, "b", offsetof(Session, kill_processes) },
245 { "IdleHint", bus_session_append_idle_hint, "b", 0 },
246 { "IdleSinceHint", bus_session_append_idle_hint_since, "t", 0 },
247 { "IdleSinceHintMonotonic", bus_session_append_idle_hint_since, "t", 0 },
251 static const BusProperty bus_login_session_user_properties[] = {
252 { "User", bus_session_append_user, "(uo)", 0 },
253 { "Name", bus_property_append_string, "s", offsetof(User, name), true },
256 static DBusHandlerResult session_message_dispatch(
258 DBusConnection *connection,
259 DBusMessage *message) {
262 DBusMessage *reply = NULL;
269 dbus_error_init(&error);
271 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) {
275 return bus_send_error_reply(connection, message, NULL, r);
277 reply = dbus_message_new_method_return(message);
281 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) {
283 r = session_activate(s);
285 return bus_send_error_reply(connection, message, NULL, r);
287 reply = dbus_message_new_method_return(message);
291 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") ||
292 dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) {
294 if (session_send_lock(s, streq(dbus_message_get_member(message), "Lock")) < 0)
297 reply = dbus_message_new_method_return(message);
301 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) {
305 if (!dbus_message_get_args(
308 DBUS_TYPE_BOOLEAN, &b,
310 return bus_send_error_reply(connection, message, &error, -EINVAL);
312 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
313 if (ul == (unsigned long) -1)
314 return bus_send_error_reply(connection, message, &error, -EIO);
316 if (ul != 0 && ul != s->user->uid)
317 return bus_send_error_reply(connection, message, NULL, -EPERM);
319 session_set_idle_hint(s, b);
321 reply = dbus_message_new_method_return(message);
325 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Kill")) {
330 if (!dbus_message_get_args(
333 DBUS_TYPE_STRING, &swho,
334 DBUS_TYPE_INT32, &signo,
336 return bus_send_error_reply(connection, message, &error, -EINVAL);
341 who = kill_who_from_string(swho);
343 return bus_send_error_reply(connection, message, &error, -EINVAL);
346 if (signo <= 0 || signo >= _NSIG)
347 return bus_send_error_reply(connection, message, &error, -EINVAL);
349 r = session_kill(s, who, signo);
351 return bus_send_error_reply(connection, message, NULL, r);
353 reply = dbus_message_new_method_return(message);
358 const BusBoundProperties bps[] = {
359 { "org.freedesktop.login1.Session", bus_login_session_properties, s },
360 { "org.freedesktop.login1.Session", bus_login_session_user_properties, s->user },
363 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, bps);
367 if (!dbus_connection_send(connection, reply, NULL))
370 dbus_message_unref(reply);
373 return DBUS_HANDLER_RESULT_HANDLED;
377 dbus_message_unref(reply);
379 dbus_error_free(&error);
381 return DBUS_HANDLER_RESULT_NEED_MEMORY;
384 static DBusHandlerResult session_message_handler(
385 DBusConnection *connection,
386 DBusMessage *message,
389 Manager *m = userdata;
393 r = get_session_for_path(m, dbus_message_get_path(message), &s);
397 return DBUS_HANDLER_RESULT_NEED_MEMORY;
403 dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
404 return bus_send_error_reply(connection, message, &e, r);
407 return bus_send_error_reply(connection, message, NULL, r);
410 return session_message_dispatch(s, connection, message);
413 const DBusObjectPathVTable bus_session_vtable = {
414 .message_function = session_message_handler
417 char *session_bus_path(Session *s) {
422 t = bus_path_escape(s->id);
426 r = strappend("/org/freedesktop/login1/session/", t);
432 int session_send_signal(Session *s, bool new_session) {
439 m = dbus_message_new_signal("/org/freedesktop/login1",
440 "org.freedesktop.login1.Manager",
441 new_session ? "SessionNew" : "SessionRemoved");
446 p = session_bus_path(s);
450 if (!dbus_message_append_args(
452 DBUS_TYPE_STRING, &s->id,
453 DBUS_TYPE_OBJECT_PATH, &p,
457 if (!dbus_connection_send(s->manager->bus, m, NULL))
463 dbus_message_unref(m);
469 int session_send_changed(Session *s, const char *properties) {
479 p = session_bus_path(s);
483 m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties);
487 if (!dbus_connection_send(s->manager->bus, m, NULL))
494 dbus_message_unref(m);
500 int session_send_lock(Session *s, bool lock) {
507 p = session_bus_path(s);
511 m = dbus_message_new_signal(p, "org.freedesktop.login1.Session", lock ? "Lock" : "Unlock");
517 b = dbus_connection_send(s->manager->bus, m, NULL);
518 dbus_message_unref(m);