chiark / gitweb /
build-sys: add a number of missing header files to EXTRA_DIST
[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 #include <string.h>
24
25 #include "logind.h"
26 #include "logind-user.h"
27 #include "dbus-common.h"
28
29 #define BUS_USER_INTERFACE \
30         " <interface name=\"org.freedesktop.login1.User\">\n"           \
31         "  <method name=\"Terminate\"/>\n"                              \
32         "  <property name=\"UID\" type=\"u\" access=\"read\"/>\n"       \
33         "  <property name=\"GID\" type=\"u\" access=\"read\"/>\n"       \
34         "  <property name=\"Name\" type=\"s\" access=\"read\"/>\n"      \
35         "  <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \
36         "  <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
37         "  <property name=\"RuntimePath\" type=\"s\" access=\"read\"/>\n" \
38         "  <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
39         "  <property name=\"Service\" type=\"s\" access=\"read\"/>\n"   \
40         "  <property name=\"Display\" type=\"(so)\" access=\"read\"/>\n" \
41         "  <property name=\"State\" type=\"s\" access=\"read\"/>\n"     \
42         "  <property name=\"Sessions\" type=\"a(so)\" access=\"read\"/>\n" \
43         "  <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n"  \
44         "  <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
45         "  <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
46         " </interface>\n"                                               \
47
48 #define INTROSPECTION                                                   \
49         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
50         "<node>\n"                                                      \
51         BUS_USER_INTERFACE                                              \
52         BUS_PROPERTIES_INTERFACE                                        \
53         BUS_PEER_INTERFACE                                              \
54         BUS_INTROSPECTABLE_INTERFACE                                    \
55         "</node>\n"
56
57 #define INTERFACES_LIST                              \
58         BUS_GENERIC_INTERFACES_LIST                  \
59         "org.freedesktop.login1.User\0"
60
61 static int bus_user_append_display(DBusMessageIter *i, const char *property, void *data) {
62         DBusMessageIter sub;
63         User *u = data;
64         const char *id, *path;
65         char *p = NULL;
66
67         assert(i);
68         assert(property);
69         assert(u);
70
71         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
72                 return -ENOMEM;
73
74         if (u->display) {
75                 id = u->display->id;
76                 path = p = session_bus_path(u->display);
77
78                 if (!p)
79                         return -ENOMEM;
80         } else {
81                 id = "";
82                 path = "/";
83         }
84
85         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
86             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
87                 free(p);
88                 return -ENOMEM;
89         }
90
91         free(p);
92
93         if (!dbus_message_iter_close_container(i, &sub))
94                 return -ENOMEM;
95
96         return 0;
97 }
98
99 static int bus_user_append_state(DBusMessageIter *i, const char *property, void *data) {
100         User *u = data;
101         const char *state;
102
103         assert(i);
104         assert(property);
105         assert(u);
106
107         state = user_state_to_string(user_get_state(u));
108
109         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
110                 return -ENOMEM;
111
112         return 0;
113 }
114
115 static int bus_user_append_sessions(DBusMessageIter *i, const char *property, void *data) {
116         DBusMessageIter sub, sub2;
117         User *u = data;
118         Session *session;
119
120         assert(i);
121         assert(property);
122         assert(u);
123
124         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "(so)", &sub))
125                 return -ENOMEM;
126
127         LIST_FOREACH(sessions_by_user, session, u->sessions) {
128                 char *p;
129
130                 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
131                         return -ENOMEM;
132
133                 p = session_bus_path(session);
134                 if (!p)
135                         return -ENOMEM;
136
137                 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
138                     !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
139                         free(p);
140                         return -ENOMEM;
141                 }
142
143                 free(p);
144
145                 if (!dbus_message_iter_close_container(&sub, &sub2))
146                         return -ENOMEM;
147         }
148
149         if (!dbus_message_iter_close_container(i, &sub))
150                 return -ENOMEM;
151
152         return 0;
153 }
154
155 static int bus_user_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
156         User *u = data;
157         dbus_bool_t b;
158
159         assert(i);
160         assert(property);
161         assert(u);
162
163         b = user_get_idle_hint(u, NULL) > 0;
164
165         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
166                 return -ENOMEM;
167
168         return 0;
169 }
170
171 static int bus_user_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
172         User *u = data;
173         dual_timestamp t;
174         uint64_t k;
175
176         assert(i);
177         assert(property);
178         assert(u);
179
180         user_get_idle_hint(u, &t);
181         k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
182
183         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &k))
184                 return -ENOMEM;
185
186         return 0;
187 }
188
189 static int get_user_for_path(Manager *m, const char *path, User **_u) {
190         User *u;
191         unsigned long lu;
192         int r;
193
194         assert(m);
195         assert(path);
196         assert(_u);
197
198         if (!startswith(path, "/org/freedesktop/login1/user/"))
199                 return -EINVAL;
200
201         r = safe_atolu(path + 29, &lu);
202         if (r < 0)
203                 return r;
204
205         u = hashmap_get(m->users, ULONG_TO_PTR(lu));
206         if (!u)
207                 return -ENOENT;
208
209         *_u = u;
210         return 0;
211 }
212
213 static DBusHandlerResult user_message_dispatch(
214                 User *u,
215                 DBusConnection *connection,
216                 DBusMessage *message) {
217
218         const BusProperty properties[] = {
219                 { "org.freedesktop.login1.User", "UID",                bus_property_append_uid,    "u",     &u->uid                 },
220                 { "org.freedesktop.login1.User", "GID",                bus_property_append_gid,    "u",     &u->gid                 },
221                 { "org.freedesktop.login1.User", "Name",               bus_property_append_string, "s",     u->name                 },
222                 { "org.freedesktop.login1.User", "Timestamp",          bus_property_append_usec,   "t",     &u->timestamp.realtime  },
223                 { "org.freedesktop.login1.User", "TimestampMonotonic", bus_property_append_usec,   "t",     &u->timestamp.monotonic },
224                 { "org.freedesktop.login1.User", "RuntimePath",        bus_property_append_string, "s",     u->runtime_path         },
225                 { "org.freedesktop.login1.User", "ControlGroupPath",   bus_property_append_string, "s",     u->cgroup_path          },
226                 { "org.freedesktop.login1.User", "Service",            bus_property_append_string, "s",     u->service              },
227                 { "org.freedesktop.login1.User", "Display",            bus_user_append_display,    "(so)",  u                       },
228                 { "org.freedesktop.login1.User", "State",              bus_user_append_state,      "s",     u                       },
229                 { "org.freedesktop.login1.User", "Sessions",           bus_user_append_sessions,   "a(so)", u                       },
230                 { "org.freedesktop.login1.User", "IdleHint",           bus_user_append_idle_hint,  "b",     u                       },
231                 { "org.freedesktop.login1.User", "IdleSinceHint",          bus_user_append_idle_hint_since, "t", u                  },
232                 { "org.freedesktop.login1.User", "IdleSinceHintMonotonic", bus_user_append_idle_hint_since, "t", u                  },
233                 { NULL, NULL, NULL, NULL, NULL }
234         };
235
236         DBusError error;
237         DBusMessage *reply = NULL;
238         int r;
239
240         assert(u);
241         assert(connection);
242         assert(message);
243
244         if (dbus_message_is_method_call(message, "org.freedesktop.login1.User", "Terminate")) {
245
246                 r = user_stop(u);
247                 if (r < 0)
248                         return bus_send_error_reply(connection, message, NULL, r);
249
250                 reply = dbus_message_new_method_return(message);
251                 if (!reply)
252                         goto oom;
253         } else
254                 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
255
256         if (reply) {
257                 if (!dbus_connection_send(connection, reply, NULL))
258                         goto oom;
259
260                 dbus_message_unref(reply);
261         }
262
263         return DBUS_HANDLER_RESULT_HANDLED;
264
265 oom:
266         if (reply)
267                 dbus_message_unref(reply);
268
269         dbus_error_free(&error);
270
271         return DBUS_HANDLER_RESULT_NEED_MEMORY;
272 }
273
274 static DBusHandlerResult user_message_handler(
275                 DBusConnection *connection,
276                 DBusMessage *message,
277                 void *userdata) {
278
279         Manager *m = userdata;
280         User *u;
281         int r;
282
283         r = get_user_for_path(m, dbus_message_get_path(message), &u);
284         if (r < 0) {
285
286                 if (r == -ENOMEM)
287                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
288
289                 if (r == -ENOENT) {
290                         DBusError e;
291
292                         dbus_error_init(&e);
293                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown user");
294                         return bus_send_error_reply(connection, message, &e, r);
295                 }
296
297                 return bus_send_error_reply(connection, message, NULL, r);
298         }
299
300         return user_message_dispatch(u, connection, message);
301 }
302
303 const DBusObjectPathVTable bus_user_vtable = {
304         .message_function = user_message_handler
305 };
306
307 char *user_bus_path(User *u) {
308         char *s;
309
310         assert(u);
311
312         if (asprintf(&s, "/org/freedesktop/login1/user/%llu", (unsigned long long) u->uid) < 0)
313                 return NULL;
314
315         return s;
316 }
317
318 int user_send_signal(User *u, bool new_user) {
319         DBusMessage *m;
320         int r = -ENOMEM;
321         char *p = NULL;
322         uint32_t uid;
323
324         assert(u);
325
326         m = dbus_message_new_signal("/org/freedesktop/login1",
327                                     "org.freedesktop.login1.Manager",
328                                     new_user ? "UserNew" : "UserRemoved");
329
330         if (!m)
331                 return -ENOMEM;
332
333         p = user_bus_path(u);
334         if (!p)
335                 goto finish;
336
337         uid = u->uid;
338
339         if (!dbus_message_append_args(
340                             m,
341                             DBUS_TYPE_UINT32, &uid,
342                             DBUS_TYPE_OBJECT_PATH, &p,
343                             DBUS_TYPE_INVALID))
344                 goto finish;
345
346         if (!dbus_connection_send(u->manager->bus, m, NULL))
347                 goto finish;
348
349         r = 0;
350
351 finish:
352         dbus_message_unref(m);
353         free(p);
354
355         return r;
356 }
357
358 int user_send_changed(User *u, const char *properties) {
359         DBusMessage *m;
360         int r = -ENOMEM;
361         char *p = NULL;
362
363         assert(u);
364
365         if (!u->started)
366                 return 0;
367
368         p = user_bus_path(u);
369         if (!p)
370                 return -ENOMEM;
371
372         m = bus_properties_changed_new(p, "org.freedesktop.login1.User", properties);
373         if (!m)
374                 goto finish;
375
376         if (!dbus_connection_send(u->manager->bus, m, NULL))
377                 goto finish;
378
379         r = 0;
380
381 finish:
382         if (m)
383                 dbus_message_unref(m);
384         free(p);
385
386         return r;
387 }