chiark / gitweb /
logind: implement D-Bus properties
[elogind.git] / src / logind-user-dbus.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "logind.h"
25 #include "logind-user.h"
26 #include "dbus-common.h"
27
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" \
40         " </interface>\n"                                               \
41
42 #define INTROSPECTION                                                   \
43         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
44         "<node>\n"                                                      \
45         BUS_USER_INTERFACE                                              \
46         BUS_PROPERTIES_INTERFACE                                        \
47         BUS_PEER_INTERFACE                                              \
48         BUS_INTROSPECTABLE_INTERFACE                                    \
49         "</node>\n"
50
51 #define INTERFACES_LIST                              \
52         BUS_GENERIC_INTERFACES_LIST                  \
53         "org.freedesktop.login1.User\0"
54
55 static int bus_user_append_display(DBusMessageIter *i, const char *property, void *data) {
56         DBusMessageIter sub;
57         User *u = data;
58         const char *id, *path;
59         char *p = NULL;
60
61         assert(i);
62         assert(property);
63         assert(u);
64
65         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
66                 return -ENOMEM;
67
68         if (u->display) {
69                 id = u->display->id;
70                 path = p = session_bus_path(u->display);
71
72                 if (!p)
73                         return -ENOMEM;
74         } else {
75                 id = "";
76                 path = "/";
77         }
78
79         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
80             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
81                 free(p);
82                 return -ENOMEM;
83         }
84
85         free(p);
86
87         if (!dbus_message_iter_close_container(i, &sub))
88                 return -ENOMEM;
89
90         return 0;
91 }
92
93 static int bus_user_append_state(DBusMessageIter *i, const char *property, void *data) {
94         User *u = data;
95         const char *state;
96
97         assert(i);
98         assert(property);
99         assert(u);
100
101         state = user_state_to_string(user_get_state(u));
102
103         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
104                 return -ENOMEM;
105
106         return 0;
107 }
108
109 static int bus_user_append_sessions(DBusMessageIter *i, const char *property, void *data) {
110         DBusMessageIter sub, sub2;
111         User *u = data;
112         Session *session;
113
114         assert(i);
115         assert(property);
116         assert(u);
117
118         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "so", &sub))
119                 return -ENOMEM;
120
121         LIST_FOREACH(sessions_by_user, session, u->sessions) {
122                 char *p;
123
124                 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
125                         return -ENOMEM;
126
127                 p = session_bus_path(session);
128                 if (!p)
129                         return -ENOMEM;
130
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)) {
133                         free(p);
134                         return -ENOMEM;
135                 }
136
137                 free(p);
138
139                 if (!dbus_message_iter_close_container(&sub, &sub2))
140                         return -ENOMEM;
141         }
142
143         if (!dbus_message_iter_close_container(i, &sub))
144                 return -ENOMEM;
145
146         return 0;
147 }
148
149 static int get_user_for_path(Manager *m, const char *path, User **_u) {
150         User *u;
151         unsigned long lu;
152         int r;
153
154         assert(m);
155         assert(path);
156         assert(_u);
157
158         if (!startswith(path, "/org/freedesktop/login1/user/"))
159                 return -EINVAL;
160
161         r = safe_atolu(path + 29, &lu);
162         if (r < 0)
163                 return r;
164
165         u = hashmap_get(m->users, ULONG_TO_PTR(lu));
166         if (!u)
167                 return -ENOENT;
168
169         *_u = u;
170         return 0;
171 }
172
173 static DBusHandlerResult user_message_dispatch(
174                 User *u,
175                 DBusConnection *connection,
176                 DBusMessage *message) {
177
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 }
189         };
190
191         assert(u);
192         assert(connection);
193         assert(message);
194
195         return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
196 }
197
198 static DBusHandlerResult user_message_handler(
199                 DBusConnection *connection,
200                 DBusMessage *message,
201                 void *userdata) {
202
203         Manager *m = userdata;
204         User *u;
205         int r;
206
207         r = get_user_for_path(m, dbus_message_get_path(message), &u);
208         if (r < 0) {
209
210                 if (r == -ENOMEM)
211                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
212
213                 if (r == -ENOENT) {
214                         DBusError e;
215
216                         dbus_error_init(&e);
217                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown user");
218                         return bus_send_error_reply(connection, message, &e, r);
219                 }
220
221                 return bus_send_error_reply(connection, message, NULL, r);
222         }
223
224         return user_message_dispatch(u, connection, message);
225 }
226
227 const DBusObjectPathVTable bus_user_vtable = {
228         .message_function = user_message_handler
229 };
230
231 char *user_bus_path(User *u) {
232         char *s;
233
234         assert(u);
235
236         if (asprintf(&s, "/org/freedesktop/login1/user/%llu", (unsigned long long) u->uid) < 0)
237                 return NULL;
238
239         return s;
240 }