chiark / gitweb /
dbus: more efficient implementation of properties
[elogind.git] / src / login / logind-session-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-session.h"
27 #include "dbus-common.h"
28 #include "util.h"
29
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"                             \
38         "  </method>\n"                                                 \
39         "  <method name=\"Kill\">\n"                                    \
40         "   <arg name=\"who\" type=\"s\"/>\n"                           \
41         "   <arg name=\"signal\" type=\"s\"/>\n"                        \
42         "  </method>\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" \
67         " </interface>\n"
68
69 #define INTROSPECTION                                                   \
70         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
71         "<node>\n"                                                      \
72         BUS_SESSION_INTERFACE                                           \
73         BUS_PROPERTIES_INTERFACE                                        \
74         BUS_PEER_INTERFACE                                              \
75         BUS_INTROSPECTABLE_INTERFACE                                    \
76         "</node>\n"
77
78 #define INTERFACES_LIST                              \
79         BUS_GENERIC_INTERFACES_LIST                  \
80         "org.freedesktop.login1.Session\0"
81
82 static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
83         DBusMessageIter sub;
84         Session *s = data;
85         const char *id, *path;
86         char *p = NULL;
87
88         assert(i);
89         assert(property);
90         assert(s);
91
92         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
93                 return -ENOMEM;
94
95         if (s->seat) {
96                 id = s->seat->id;
97                 path = p = seat_bus_path(s->seat);
98
99                 if (!p)
100                         return -ENOMEM;
101         } else {
102                 id = "";
103                 path = "/";
104         }
105
106         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
107             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
108                 free(p);
109                 return -ENOMEM;
110         }
111
112         free(p);
113
114         if (!dbus_message_iter_close_container(i, &sub))
115                 return -ENOMEM;
116
117         return 0;
118 }
119
120 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
121         DBusMessageIter sub;
122         User *u = data;
123         char *p = NULL;
124
125         assert(i);
126         assert(property);
127         assert(u);
128
129         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
130                 return -ENOMEM;
131
132         p = user_bus_path(u);
133         if (!p)
134                 return -ENOMEM;
135
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)) {
138                 free(p);
139                 return -ENOMEM;
140         }
141
142         free(p);
143
144         if (!dbus_message_iter_close_container(i, &sub))
145                 return -ENOMEM;
146
147         return 0;
148 }
149
150 static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
151         Session *s = data;
152         dbus_bool_t b;
153
154         assert(i);
155         assert(property);
156         assert(s);
157
158         b = session_is_active(s);
159         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
160                 return -ENOMEM;
161
162         return 0;
163 }
164
165 static int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
166         Session *s = data;
167         int b;
168
169         assert(i);
170         assert(property);
171         assert(s);
172
173         b = session_get_idle_hint(s, NULL) > 0;
174         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
175                 return -ENOMEM;
176
177         return 0;
178 }
179
180 static int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
181         Session *s = data;
182         dual_timestamp t;
183         uint64_t u;
184
185         assert(i);
186         assert(property);
187         assert(s);
188
189         session_get_idle_hint(s, &t);
190         u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
191
192         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
193                 return -ENOMEM;
194
195         return 0;
196 }
197
198 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
199
200 static int get_session_for_path(Manager *m, const char *path, Session **_s) {
201         Session *s;
202         char *id;
203
204         assert(m);
205         assert(path);
206         assert(_s);
207
208         if (!startswith(path, "/org/freedesktop/login1/session/"))
209                 return -EINVAL;
210
211         id = bus_path_unescape(path + 32);
212         if (!id)
213                 return -ENOMEM;
214
215         s = hashmap_get(m->sessions, id);
216         free(id);
217
218         if (!s)
219                 return -ENOENT;
220
221         *_s = s;
222         return 0;
223 }
224
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 },
248         { NULL, }
249 };
250
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 },
254 };
255
256 static DBusHandlerResult session_message_dispatch(
257                 Session *s,
258                 DBusConnection *connection,
259                 DBusMessage *message) {
260
261         DBusError error;
262         DBusMessage *reply = NULL;
263         int r;
264
265         assert(s);
266         assert(connection);
267         assert(message);
268
269         dbus_error_init(&error);
270
271         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) {
272
273                 r = session_stop(s);
274                 if (r < 0)
275                         return bus_send_error_reply(connection, message, NULL, r);
276
277                 reply = dbus_message_new_method_return(message);
278                 if (!reply)
279                         goto oom;
280
281         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) {
282
283                 r = session_activate(s);
284                 if (r < 0)
285                         return bus_send_error_reply(connection, message, NULL, r);
286
287                 reply = dbus_message_new_method_return(message);
288                 if (!reply)
289                         goto oom;
290
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")) {
293
294                 if (session_send_lock(s, streq(dbus_message_get_member(message), "Lock")) < 0)
295                         goto oom;
296
297                 reply = dbus_message_new_method_return(message);
298                 if (!reply)
299                         goto oom;
300
301         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) {
302                 dbus_bool_t b;
303                 unsigned long ul;
304
305                 if (!dbus_message_get_args(
306                                     message,
307                                     &error,
308                                     DBUS_TYPE_BOOLEAN, &b,
309                                     DBUS_TYPE_INVALID))
310                         return bus_send_error_reply(connection, message, &error, -EINVAL);
311
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);
315
316                 if (ul != 0 && ul != s->user->uid)
317                         return bus_send_error_reply(connection, message, NULL, -EPERM);
318
319                 session_set_idle_hint(s, b);
320
321                 reply = dbus_message_new_method_return(message);
322                 if (!reply)
323                         goto oom;
324
325         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Kill")) {
326                 const char *swho;
327                 int32_t signo;
328                 KillWho who;
329
330                 if (!dbus_message_get_args(
331                                     message,
332                                     &error,
333                                     DBUS_TYPE_STRING, &swho,
334                                     DBUS_TYPE_INT32, &signo,
335                                     DBUS_TYPE_INVALID))
336                         return bus_send_error_reply(connection, message, &error, -EINVAL);
337
338                 if (isempty(swho))
339                         who = KILL_ALL;
340                 else {
341                         who = kill_who_from_string(swho);
342                         if (who < 0)
343                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
344                 }
345
346                 if (signo <= 0 || signo >= _NSIG)
347                         return bus_send_error_reply(connection, message, &error, -EINVAL);
348
349                 r = session_kill(s, who, signo);
350                 if (r < 0)
351                         return bus_send_error_reply(connection, message, NULL, r);
352
353                 reply = dbus_message_new_method_return(message);
354                 if (!reply)
355                         goto oom;
356
357         } else {
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 },
361                         { NULL, }
362                 };
363                 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, bps);
364         }
365
366         if (reply) {
367                 if (!dbus_connection_send(connection, reply, NULL))
368                         goto oom;
369
370                 dbus_message_unref(reply);
371         }
372
373         return DBUS_HANDLER_RESULT_HANDLED;
374
375 oom:
376         if (reply)
377                 dbus_message_unref(reply);
378
379         dbus_error_free(&error);
380
381         return DBUS_HANDLER_RESULT_NEED_MEMORY;
382 }
383
384 static DBusHandlerResult session_message_handler(
385                 DBusConnection *connection,
386                 DBusMessage *message,
387                 void *userdata) {
388
389         Manager *m = userdata;
390         Session *s;
391         int r;
392
393         r = get_session_for_path(m, dbus_message_get_path(message), &s);
394         if (r < 0) {
395
396                 if (r == -ENOMEM)
397                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
398
399                 if (r == -ENOENT) {
400                         DBusError e;
401
402                         dbus_error_init(&e);
403                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
404                         return bus_send_error_reply(connection, message, &e, r);
405                 }
406
407                 return bus_send_error_reply(connection, message, NULL, r);
408         }
409
410         return session_message_dispatch(s, connection, message);
411 }
412
413 const DBusObjectPathVTable bus_session_vtable = {
414         .message_function = session_message_handler
415 };
416
417 char *session_bus_path(Session *s) {
418         char *t, *r;
419
420         assert(s);
421
422         t = bus_path_escape(s->id);
423         if (!t)
424                 return NULL;
425
426         r = strappend("/org/freedesktop/login1/session/", t);
427         free(t);
428
429         return r;
430 }
431
432 int session_send_signal(Session *s, bool new_session) {
433         DBusMessage *m;
434         int r = -ENOMEM;
435         char *p = NULL;
436
437         assert(s);
438
439         m = dbus_message_new_signal("/org/freedesktop/login1",
440                                     "org.freedesktop.login1.Manager",
441                                     new_session ? "SessionNew" : "SessionRemoved");
442
443         if (!m)
444                 return -ENOMEM;
445
446         p = session_bus_path(s);
447         if (!p)
448                 goto finish;
449
450         if (!dbus_message_append_args(
451                             m,
452                             DBUS_TYPE_STRING, &s->id,
453                             DBUS_TYPE_OBJECT_PATH, &p,
454                             DBUS_TYPE_INVALID))
455                 goto finish;
456
457         if (!dbus_connection_send(s->manager->bus, m, NULL))
458                 goto finish;
459
460         r = 0;
461
462 finish:
463         dbus_message_unref(m);
464         free(p);
465
466         return r;
467 }
468
469 int session_send_changed(Session *s, const char *properties) {
470         DBusMessage *m;
471         int r = -ENOMEM;
472         char *p = NULL;
473
474         assert(s);
475
476         if (!s->started)
477                 return 0;
478
479         p = session_bus_path(s);
480         if (!p)
481                 return -ENOMEM;
482
483         m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties);
484         if (!m)
485                 goto finish;
486
487         if (!dbus_connection_send(s->manager->bus, m, NULL))
488                 goto finish;
489
490         r = 0;
491
492 finish:
493         if (m)
494                 dbus_message_unref(m);
495         free(p);
496
497         return r;
498 }
499
500 int session_send_lock(Session *s, bool lock) {
501         DBusMessage *m;
502         bool b;
503         char *p;
504
505         assert(s);
506
507         p = session_bus_path(s);
508         if (!p)
509                 return -ENOMEM;
510
511         m = dbus_message_new_signal(p, "org.freedesktop.login1.Session", lock ? "Lock" : "Unlock");
512         free(p);
513
514         if (!m)
515                 return -ENOMEM;
516
517         b = dbus_connection_send(s->manager->bus, m, NULL);
518         dbus_message_unref(m);
519
520         if (!b)
521                 return -ENOMEM;
522
523         return 0;
524 }