chiark / gitweb /
seat: only mark main input device for seat assignments
[elogind.git] / src / 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         "  <property name=\"Id\" type=\"u\" access=\"read\"/>\n"        \
40         "  <property name=\"User\" type=\"(uo)\" access=\"read\"/>\n"   \
41         "  <property name=\"Name\" type=\"s\" access=\"read\"/>\n"      \
42         "  <property name=\"Timestamp\" type=\"t\" access=\"read\"/>\n" \
43         "  <property name=\"TimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
44         "  <property name=\"ControlGroupPath\" type=\"s\" access=\"read\"/>\n" \
45         "  <property name=\"VTNr\" type=\"u\" access=\"read\"/>\n"      \
46         "  <property name=\"Seat\" type=\"(so)\" access=\"read\"/>\n"   \
47         "  <property name=\"TTY\" type=\"s\" access=\"read\"/>\n"       \
48         "  <property name=\"Display\" type=\"s\" access=\"read\"/>\n"   \
49         "  <property name=\"Remote\" type=\"b\" access=\"read\"/>\n"    \
50         "  <property name=\"RemoteHost\" type=\"s\" access=\"read\"/>\n" \
51         "  <property name=\"RemoteUser\" type=\"s\" access=\"read\"/>\n" \
52         "  <property name=\"Service\" type=\"s\" access=\"read\"/>\n" \
53         "  <property name=\"Leader\" type=\"u\" access=\"read\"/>\n"    \
54         "  <property name=\"Audit\" type=\"u\" access=\"read\"/>\n"     \
55         "  <property name=\"Type\" type=\"s\" access=\"read\"/>\n"      \
56         "  <property name=\"Active\" type=\"b\" access=\"read\"/>\n"    \
57         "  <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
58         "  <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
59         "  <property name=\"KillProcesses\" type=\"b\" access=\"read\"/>\n" \
60         "  <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n"  \
61         "  <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
62         "  <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
63         " </interface>\n"
64
65 #define INTROSPECTION                                                   \
66         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
67         "<node>\n"                                                      \
68         BUS_SESSION_INTERFACE                                           \
69         BUS_PROPERTIES_INTERFACE                                        \
70         BUS_PEER_INTERFACE                                              \
71         BUS_INTROSPECTABLE_INTERFACE                                    \
72         "</node>\n"
73
74 #define INTERFACES_LIST                              \
75         BUS_GENERIC_INTERFACES_LIST                  \
76         "org.freedesktop.login1.Session\0"
77
78 static int bus_session_append_seat(DBusMessageIter *i, const char *property, void *data) {
79         DBusMessageIter sub;
80         Session *s = data;
81         const char *id, *path;
82         char *p = NULL;
83
84         assert(i);
85         assert(property);
86         assert(s);
87
88         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
89                 return -ENOMEM;
90
91         if (s->seat) {
92                 id = s->seat->id;
93                 path = p = seat_bus_path(s->seat);
94
95                 if (!p)
96                         return -ENOMEM;
97         } else {
98                 id = "";
99                 path = "/";
100         }
101
102         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &id) ||
103             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &path)) {
104                 free(p);
105                 return -ENOMEM;
106         }
107
108         free(p);
109
110         if (!dbus_message_iter_close_container(i, &sub))
111                 return -ENOMEM;
112
113         return 0;
114 }
115
116 static int bus_session_append_user(DBusMessageIter *i, const char *property, void *data) {
117         DBusMessageIter sub;
118         Session *s = data;
119         char *p = NULL;
120
121         assert(i);
122         assert(property);
123         assert(s);
124
125         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
126                 return -ENOMEM;
127
128         p = user_bus_path(s->user);
129         if (!p)
130                 return -ENOMEM;
131
132         if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &s->user->uid) ||
133             !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
134                 free(p);
135                 return -ENOMEM;
136         }
137
138         free(p);
139
140         if (!dbus_message_iter_close_container(i, &sub))
141                 return -ENOMEM;
142
143         return 0;
144 }
145
146 static int bus_session_append_active(DBusMessageIter *i, const char *property, void *data) {
147         Session *s = data;
148         dbus_bool_t b;
149
150         assert(i);
151         assert(property);
152         assert(s);
153
154         b = session_is_active(s);
155         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
156                 return -ENOMEM;
157
158         return 0;
159 }
160
161 static int bus_session_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
162         Session *s = data;
163         int b;
164
165         assert(i);
166         assert(property);
167         assert(s);
168
169         b = session_get_idle_hint(s, NULL) > 0;
170         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
171                 return -ENOMEM;
172
173         return 0;
174 }
175
176 static int bus_session_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
177         Session *s = data;
178         dual_timestamp t;
179         uint64_t u;
180
181         assert(i);
182         assert(property);
183         assert(s);
184
185         session_get_idle_hint(s, &t);
186         u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
187
188         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
189                 return -ENOMEM;
190
191         return 0;
192 }
193
194 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_session_append_type, session_type, SessionType);
195
196 static int get_session_for_path(Manager *m, const char *path, Session **_s) {
197         Session *s;
198         char *id;
199
200         assert(m);
201         assert(path);
202         assert(_s);
203
204         if (!startswith(path, "/org/freedesktop/login1/session/"))
205                 return -EINVAL;
206
207         id = bus_path_unescape(path + 32);
208         if (!id)
209                 return -ENOMEM;
210
211         s = hashmap_get(m->sessions, id);
212         free(id);
213
214         if (!s)
215                 return -ENOENT;
216
217         *_s = s;
218         return 0;
219 }
220
221 static DBusHandlerResult session_message_dispatch(
222                 Session *s,
223                 DBusConnection *connection,
224                 DBusMessage *message) {
225
226         const BusProperty properties[] = {
227                 { "org.freedesktop.login1.Session", "Id",                 bus_property_append_string,   "s",    s->id                   },
228                 { "org.freedesktop.login1.Session", "User",               bus_session_append_user,      "(uo)", s                       },
229                 { "org.freedesktop.login1.Session", "Name",               bus_property_append_string,   "s",    s->user->name           },
230                 { "org.freedesktop.login1.Session", "Timestamp",          bus_property_append_usec,     "t",    &s->timestamp.realtime  },
231                 { "org.freedesktop.login1.Session", "TimestampMonotonic", bus_property_append_usec,     "t",    &s->timestamp.monotonic },
232                 { "org.freedesktop.login1.Session", "ControlGroupPath",   bus_property_append_string,   "s",    s->cgroup_path          },
233                 { "org.freedesktop.login1.Session", "VTNr",               bus_property_append_uint32,   "u",    &s->vtnr                },
234                 { "org.freedesktop.login1.Session", "Seat",               bus_session_append_seat,      "(so)", s                       },
235                 { "org.freedesktop.login1.Session", "TTY",                bus_property_append_string,   "s",    s->tty                  },
236                 { "org.freedesktop.login1.Session", "Display",            bus_property_append_string,   "s",    s->display              },
237                 { "org.freedesktop.login1.Session", "Remote",             bus_property_append_bool,     "b",    &s->remote              },
238                 { "org.freedesktop.login1.Session", "RemoteUser",         bus_property_append_string,   "s",    s->remote_user          },
239                 { "org.freedesktop.login1.Session", "RemoteHost",         bus_property_append_string,   "s",    s->remote_host          },
240                 { "org.freedesktop.login1.Session", "Service",            bus_property_append_string,   "s",    s->service              },
241                 { "org.freedesktop.login1.Session", "Leader",             bus_property_append_pid,      "u",    &s->leader              },
242                 { "org.freedesktop.login1.Session", "Audit",              bus_property_append_uint32,   "u",    &s->audit_id            },
243                 { "org.freedesktop.login1.Session", "Type",               bus_session_append_type,      "s",    &s->type                },
244                 { "org.freedesktop.login1.Session", "Active",             bus_session_append_active,    "b",    s                       },
245                 { "org.freedesktop.login1.Session", "Controllers",        bus_property_append_strv,     "as",   s->controllers          },
246                 { "org.freedesktop.login1.Session", "ResetControllers",   bus_property_append_strv,     "as",   s->reset_controllers    },
247                 { "org.freedesktop.login1.Session", "KillProcesses",      bus_property_append_bool,     "b",    &s->kill_processes      },
248                 { "org.freedesktop.login1.Session", "IdleHint",           bus_session_append_idle_hint, "b",    s                       },
249                 { "org.freedesktop.login1.Session", "IdleSinceHint",          bus_session_append_idle_hint_since, "t", s                },
250                 { "org.freedesktop.login1.Session", "IdleSinceHintMonotonic", bus_session_append_idle_hint_since, "t", s                },
251                 { NULL, NULL, NULL, NULL, NULL }
252         };
253
254         DBusError error;
255         DBusMessage *reply = NULL;
256         int r;
257
258         assert(s);
259         assert(connection);
260         assert(message);
261
262         dbus_error_init(&error);
263
264         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Terminate")) {
265
266                 r = session_stop(s);
267                 if (r < 0)
268                         return bus_send_error_reply(connection, message, NULL, r);
269
270                 reply = dbus_message_new_method_return(message);
271                 if (!reply)
272                         goto oom;
273
274         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Activate")) {
275
276                 r = session_activate(s);
277                 if (r < 0)
278                         return bus_send_error_reply(connection, message, NULL, r);
279
280                 reply = dbus_message_new_method_return(message);
281                 if (!reply)
282                         goto oom;
283
284         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Lock") ||
285                    dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "Unlock")) {
286                 bool b;
287                 DBusMessage *sig;
288
289                 sig = dbus_message_new_signal(dbus_message_get_path(message), "org.freedesktop.login1.Session", dbus_message_get_member(message));
290                 if (!sig)
291                         goto oom;
292
293                 b = dbus_connection_send(connection, sig, NULL);
294                 dbus_message_unref(sig);
295
296                 if (!b)
297                         goto oom;
298
299                 reply = dbus_message_new_method_return(message);
300                 if (!reply)
301                         goto oom;
302
303         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Session", "SetIdleHint")) {
304                 dbus_bool_t b;
305                 unsigned long ul;
306
307                 if (!dbus_message_get_args(
308                                     message,
309                                     &error,
310                                     DBUS_TYPE_BOOLEAN, &b,
311                                     DBUS_TYPE_INVALID))
312                         return bus_send_error_reply(connection, message, &error, -EINVAL);
313
314                 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
315                 if (ul == (unsigned long) -1)
316                         return bus_send_error_reply(connection, message, &error, -EIO);
317
318                 if (ul != 0 && ul != s->user->uid)
319                         return bus_send_error_reply(connection, message, NULL, -EPERM);
320
321                 session_set_idle_hint(s, b);
322
323                 reply = dbus_message_new_method_return(message);
324                 if (!reply)
325                         goto oom;
326
327         } else
328                 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
329
330         if (reply) {
331                 if (!dbus_connection_send(connection, reply, NULL))
332                         goto oom;
333
334                 dbus_message_unref(reply);
335         }
336
337         return DBUS_HANDLER_RESULT_HANDLED;
338
339 oom:
340         if (reply)
341                 dbus_message_unref(reply);
342
343         dbus_error_free(&error);
344
345         return DBUS_HANDLER_RESULT_NEED_MEMORY;
346 }
347
348 static DBusHandlerResult session_message_handler(
349                 DBusConnection *connection,
350                 DBusMessage *message,
351                 void *userdata) {
352
353         Manager *m = userdata;
354         Session *s;
355         int r;
356
357         r = get_session_for_path(m, dbus_message_get_path(message), &s);
358         if (r < 0) {
359
360                 if (r == -ENOMEM)
361                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
362
363                 if (r == -ENOENT) {
364                         DBusError e;
365
366                         dbus_error_init(&e);
367                         dbus_set_error_const(&e, DBUS_ERROR_UNKNOWN_OBJECT, "Unknown session");
368                         return bus_send_error_reply(connection, message, &e, r);
369                 }
370
371                 return bus_send_error_reply(connection, message, NULL, r);
372         }
373
374         return session_message_dispatch(s, connection, message);
375 }
376
377 const DBusObjectPathVTable bus_session_vtable = {
378         .message_function = session_message_handler
379 };
380
381 char *session_bus_path(Session *s) {
382         char *t, *r;
383
384         assert(s);
385
386         t = bus_path_escape(s->id);
387         if (!t)
388                 return NULL;
389
390         r = strappend("/org/freedesktop/login1/session/", t);
391         free(t);
392
393         return r;
394 }
395
396 int session_send_signal(Session *s, bool new_session) {
397         DBusMessage *m;
398         int r = -ENOMEM;
399         char *p = NULL;
400
401         assert(s);
402
403         m = dbus_message_new_signal("/org/freedesktop/login1",
404                                     "org.freedesktop.login1.Manager",
405                                     new_session ? "SessionNew" : "SessionRemoved");
406
407         if (!m)
408                 return -ENOMEM;
409
410         p = session_bus_path(s);
411         if (!p)
412                 goto finish;
413
414         if (!dbus_message_append_args(
415                             m,
416                             DBUS_TYPE_STRING, &s->id,
417                             DBUS_TYPE_OBJECT_PATH, &p,
418                             DBUS_TYPE_INVALID))
419                 goto finish;
420
421         if (!dbus_connection_send(s->manager->bus, m, NULL))
422                 goto finish;
423
424         r = 0;
425
426 finish:
427         dbus_message_unref(m);
428         free(p);
429
430         return r;
431 }
432
433 int session_send_changed(Session *s, const char *properties) {
434         DBusMessage *m;
435         int r = -ENOMEM;
436         char *p = NULL;
437
438         assert(s);
439
440         if (!s->started)
441                 return 0;
442
443         p = session_bus_path(s);
444         if (!p)
445                 return -ENOMEM;
446
447         m = bus_properties_changed_new(p, "org.freedesktop.login1.Session", properties);
448         if (!m)
449                 goto finish;
450
451         if (!dbus_connection_send(s->manager->bus, m, NULL))
452                 goto finish;
453
454         r = 0;
455
456 finish:
457         if (m)
458                 dbus_message_unref(m);
459         free(p);
460
461         return r;
462 }