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-user.h"
26 #include "dbus-common.h"
28 #define BUS_USER_INTERFACE \
29 " <interface name=\"org.freedesktop.login1.User\">\n" \
30 " <method name=\"Terminate\"/>\n" \
31 " <property name=\"UID\" type=\"u\" access=\"read\"/>\n" \
32 " <property name=\"GID\" type=\"u\" access=\"read\"/>\n" \
33 " <property name=\"Name\" type=\"s\" access=\"read\"/>\n" \
34 " <property name=\"RuntimePath\" type=\"s\" access=\"read\"/>\n" \
35 " <property name=\"Service\" type=\"s\" access=\"read\"/>\n" \
36 " <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
37 " <property name=\"Display\" type=\"(so)\" access=\"read\"/>\n" \
38 " <property name=\"State\" type=\"s\" access=\"read\"/>\n" \
39 " <property name=\"Sessions\" type=\"a(so)\" access=\"read\"/>\n" \
42 #define INTROSPECTION \
43 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
46 BUS_PROPERTIES_INTERFACE \
48 BUS_INTROSPECTABLE_INTERFACE \
51 #define INTERFACES_LIST \
52 BUS_GENERIC_INTERFACES_LIST \
53 "org.freedesktop.login1.User\0"
55 static int bus_user_append_display(DBusMessageIter *i, const char *property, void *data) {
58 const char *id, *path;
65 if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
70 path = p = session_bus_path(u->display);
79 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
80 !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
87 if (!dbus_message_iter_close_container(i, &sub))
93 static int bus_user_append_state(DBusMessageIter *i, const char *property, void *data) {
101 state = user_state_to_string(user_get_state(u));
103 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
109 static int bus_user_append_sessions(DBusMessageIter *i, const char *property, void *data) {
110 DBusMessageIter sub, sub2;
118 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "so", &sub))
121 LIST_FOREACH(sessions_by_user, session, u->sessions) {
124 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
127 p = session_bus_path(session);
131 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
132 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
139 if (!dbus_message_iter_close_container(&sub, &sub2))
143 if (!dbus_message_iter_close_container(i, &sub))
149 static int get_user_for_path(Manager *m, const char *path, User **_u) {
158 if (!startswith(path, "/org/freedesktop/login1/user/"))
161 r = safe_atolu(path + 29, &lu);
165 u = hashmap_get(m->users, ULONG_TO_PTR(lu));
173 static DBusHandlerResult user_message_dispatch(
175 DBusConnection *connection,
176 DBusMessage *message) {
178 const BusProperty properties[] = {
179 { "org.freedesktop.login1.User", "UID", bus_property_append_uid, "u", &u->uid },
180 { "org.freedesktop.login1.User", "GID", bus_property_append_gid, "u", &u->gid },
181 { "org.freedesktop.login1.User", "Name", bus_property_append_string, "s", u->name },
182 { "org.freedesktop.login1.User", "RuntimePath", bus_property_append_string, "s", u->runtime_path },
183 { "org.freedesktop.login1.User", "ControlGroupPath", bus_property_append_string, "s", u->cgroup_path },
184 { "org.freedesktop.login1.User", "Service", bus_property_append_string, "s", u->service },
185 { "org.freedesktop.login1.User", "Display", bus_user_append_display, "(so)", u },
186 { "org.freedesktop.login1.User", "State", bus_user_append_state, "s", u },
187 { "org.freedesktop.login1.User", "Sessions", bus_user_append_sessions, "a(so)", u },
188 { NULL, NULL, NULL, NULL, NULL }
195 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
198 static DBusHandlerResult user_message_handler(
199 DBusConnection *connection,
200 DBusMessage *message,
203 Manager *m = userdata;
207 r = get_user_for_path(m, dbus_message_get_path(message), &u);
211 return DBUS_HANDLER_RESULT_NEED_MEMORY;
217 dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown user");
218 return bus_send_error_reply(connection, message, &e, r);
221 return bus_send_error_reply(connection, message, NULL, r);
224 return user_message_dispatch(u, connection, message);
227 const DBusObjectPathVTable bus_user_vtable = {
228 .message_function = user_message_handler
231 char *user_bus_path(User *u) {
236 if (asprintf(&s, "/org/freedesktop/login1/user/%llu", (unsigned long long) u->uid) < 0)