chiark / gitweb /
2bad549fc528a60e49eda77ec1027cfda8d4e32e
[elogind.git] / src / logind-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 #include <unistd.h>
25
26 #include "logind.h"
27 #include "dbus-common.h"
28 #include "strv.h"
29
30 #define BUS_MANAGER_INTERFACE                                           \
31         " <interface name=\"org.freedesktop.login1.Manager\">\n"        \
32         "  <method name=\"GetSession\">\n"                              \
33         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
34         "   <arg name=\"session\" type=\"o\" direction=\"out\"/>\n"     \
35         "  </method>\n"                                                 \
36         "  <method name=\"GetUser\">\n"                                 \
37         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
38         "   <arg name=\"user\" type=\"o\" direction=\"out\"/>\n"        \
39         "  </method>\n"                                                 \
40         "  <method name=\"GetSeat\">\n"                                 \
41         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
42         "   <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n"        \
43         "  </method>\n"                                                 \
44         "  <method name=\"ListSessions\">\n"                            \
45         "   <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
46         "  </method>\n"                                                 \
47         "  <method name=\"ListUsers\">\n"                               \
48         "   <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n"  \
49         "  </method>\n"                                                 \
50         "  <method name=\"ListSeats\">\n"                               \
51         "   <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n"   \
52         "  </method>\n"                                                 \
53         "  <method name=\"CreateSession\">\n"                           \
54         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
55         "   <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n"       \
56         "   <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n"         \
57         "   <arg name=\"type\" type=\"s\" direction=\"in\"/>\n"         \
58         "   <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n"         \
59         "   <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n"          \
60         "   <arg name=\"display\" type=\"s\" direction=\"in\"/>\n"      \
61         "   <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n"       \
62         "   <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n"  \
63         "   <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n"  \
64         "   <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
65         "   <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
66         "   <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
67         "   <arg name=\"id\" type=\"s\" direction=\"out\"/>\n"          \
68         "   <arg name=\"path\" type=\"o\" direction=\"out\"/>\n"        \
69         "   <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
70         "   <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n"          \
71         "  </method>\n"                                                 \
72         "  <method name=\"ActivateSession\">\n"                         \
73         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
74         "  </method>\n"                                                 \
75         "  <method name=\"TerminateSession\">\n"                        \
76         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
77         "  </method>\n"                                                 \
78         "  <method name=\"TerminateUser\">\n"                           \
79         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
80         "  </method>\n"                                                 \
81         "  <method name=\"TerminateSeat\">\n"                           \
82         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
83         "  </method>\n"                                                 \
84         "  <signal name=\"SessionNew\">\n"                              \
85         "   <arg name=\"id\" type=\"s\"/>\n"                            \
86         "   <arg name=\"path\" type=\"o\"/>\n"                          \
87         "  </signal>\n"                                                 \
88         "  <signal name=\"SessionRemoved\">\n"                          \
89         "   <arg name=\"id\" type=\"s\"/>\n"                            \
90         "   <arg name=\"path\" type=\"o\"/>\n"                          \
91         "  </signal>\n"                                                 \
92         "  <signal name=\"UserNew\">\n"                                 \
93         "   <arg name=\"uid\" type=\"u\"/>\n"                           \
94         "   <arg name=\"path\" type=\"o\"/>\n"                          \
95         "  </signal>\n"                                                 \
96         "  <signal name=\"UserRemoved\">\n"                             \
97         "   <arg name=\"uid\" type=\"u\"/>\n"                           \
98         "   <arg name=\"path\" type=\"o\"/>\n"                          \
99         "  </signal>\n"                                                 \
100         "  <signal name=\"SeatNew\">\n"                                 \
101         "   <arg name=\"id\" type=\"s\"/>\n"                            \
102         "   <arg name=\"path\" type=\"o\"/>\n"                          \
103         "  </signal>\n"                                                 \
104         "  <signal name=\"SeatRemoved\">\n"                             \
105         "   <arg name=\"id\" type=\"s\"/>\n"                            \
106         "   <arg name=\"path\" type=\"o\"/>\n"                          \
107         "  </signal>\n"                                                 \
108         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
109         "  <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
110         "  <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
111         "  <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
112         "  <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
113         "  <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
114         "  <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
115         "  <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n"  \
116         "  <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
117         "  <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
118         " </interface>\n"
119
120 #define INTROSPECTION_BEGIN                                             \
121         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
122         "<node>\n"                                                      \
123         BUS_MANAGER_INTERFACE                                           \
124         BUS_PROPERTIES_INTERFACE                                        \
125         BUS_PEER_INTERFACE                                              \
126         BUS_INTROSPECTABLE_INTERFACE
127
128 #define INTROSPECTION_END                                               \
129         "</node>\n"
130
131 #define INTERFACES_LIST                              \
132         BUS_GENERIC_INTERFACES_LIST                  \
133         "org.freedesktop.login1.Manager\0"
134
135 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
136         Manager *m = data;
137         dbus_bool_t b;
138
139         assert(i);
140         assert(property);
141         assert(m);
142
143         b = manager_get_idle_hint(m, NULL) > 0;
144         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
145                 return -ENOMEM;
146
147         return 0;
148 }
149
150 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
151         Manager *m = data;
152         dual_timestamp t;
153         uint64_t u;
154
155         assert(i);
156         assert(property);
157         assert(m);
158
159         manager_get_idle_hint(m, &t);
160         u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
161
162         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
163                 return -ENOMEM;
164
165         return 0;
166 }
167
168 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
169         Session *session = NULL;
170         User *user = NULL;
171         const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
172         uint32_t uid, leader, audit_id = 0;
173         dbus_bool_t remote, kill_processes;
174         char **controllers = NULL, **reset_controllers = NULL;
175         SessionType t;
176         Seat *s;
177         DBusMessageIter iter;
178         int r;
179         char *id = NULL, *p;
180         int vtnr = -1;
181         int pipe_fds[2] = { -1, -1 };
182         DBusMessage *reply = NULL;
183         bool b;
184
185         assert(m);
186         assert(message);
187         assert(_reply);
188
189         if (!dbus_message_iter_init(message, &iter) ||
190             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
191                 return -EINVAL;
192
193         dbus_message_iter_get_basic(&iter, &uid);
194
195         if (!dbus_message_iter_next(&iter) ||
196             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
197                 return -EINVAL;
198
199         dbus_message_iter_get_basic(&iter, &leader);
200
201         if (leader <= 0 ||
202             !dbus_message_iter_next(&iter) ||
203             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
204                 return -EINVAL;
205
206         dbus_message_iter_get_basic(&iter, &service);
207
208         if (!dbus_message_iter_next(&iter) ||
209             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
210                 return -EINVAL;
211
212         dbus_message_iter_get_basic(&iter, &type);
213         t = session_type_from_string(type);
214
215         if (t < 0 ||
216             !dbus_message_iter_next(&iter) ||
217             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
218                 return -EINVAL;
219
220         dbus_message_iter_get_basic(&iter, &seat);
221
222         if (isempty(seat))
223                 s = NULL;
224         else {
225                 s = hashmap_get(m->seats, seat);
226                 if (!s)
227                         return -ENOENT;
228         }
229
230         if (!dbus_message_iter_next(&iter) ||
231             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
232                 return -EINVAL;
233
234         dbus_message_iter_get_basic(&iter, &tty);
235
236         if (tty_is_vc(tty)) {
237
238                 if (!s)
239                         s = m->vtconsole;
240                 else if (s != m->vtconsole)
241                         return -EINVAL;
242
243                 vtnr = vtnr_from_tty(tty);
244
245                 if (vtnr <= 0)
246                         return vtnr < 0 ? vtnr : -EINVAL;
247
248         } else if (s == m->vtconsole)
249                 return -EINVAL;
250
251         if (!dbus_message_iter_next(&iter) ||
252             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
253                 return -EINVAL;
254
255         dbus_message_iter_get_basic(&iter, &display);
256
257         if (!dbus_message_iter_next(&iter) ||
258             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
259                 return -EINVAL;
260
261         dbus_message_iter_get_basic(&iter, &remote);
262
263         if (!dbus_message_iter_next(&iter) ||
264             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
265                 return -EINVAL;
266
267         dbus_message_iter_get_basic(&iter, &remote_user);
268
269         if (!dbus_message_iter_next(&iter) ||
270             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
271                 return -EINVAL;
272
273         dbus_message_iter_get_basic(&iter, &remote_host);
274
275         if (!dbus_message_iter_next(&iter) ||
276             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
277             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
278                 return -EINVAL;
279
280         r = bus_parse_strv_iter(&iter, &controllers);
281         if (r < 0)
282                 return -EINVAL;
283
284         if (!dbus_message_iter_next(&iter) ||
285             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
286             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
287                 r = -EINVAL;
288                 goto fail;
289         }
290
291         r = bus_parse_strv_iter(&iter, &reset_controllers);
292         if (r < 0)
293                 goto fail;
294
295         if (!dbus_message_iter_next(&iter) ||
296             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
297                 r = -EINVAL;
298                 goto fail;
299         }
300
301         dbus_message_iter_get_basic(&iter, &kill_processes);
302
303         r = manager_add_user_by_uid(m, uid, &user);
304         if (r < 0)
305                 goto fail;
306
307         audit_session_from_pid(leader, &audit_id);
308
309         if (audit_id > 0) {
310                 asprintf(&id, "%lu", (unsigned long) audit_id);
311
312                 if (!id) {
313                         r = -ENOMEM;
314                         goto fail;
315                 }
316
317                 if (hashmap_get(m->sessions, id)) {
318                         r = -EEXIST;
319                         goto fail;
320                 }
321
322         } else {
323                 do {
324                         free(id);
325                         asprintf(&id, "c%lu", ++m->session_counter);
326
327                         if (!id) {
328                                 r = -ENOMEM;
329                                 goto fail;
330                         }
331
332                 } while (hashmap_get(m->sessions, id));
333         }
334
335         r = manager_add_session(m, user, id, &session);
336         free(id);
337         if (r < 0)
338                 goto fail;
339
340         session->leader = leader;
341         session->audit_id = audit_id;
342         session->type = t;
343         session->remote = remote;
344         session->controllers = controllers;
345         session->reset_controllers = reset_controllers;
346         session->kill_processes = kill_processes;
347         session->vtnr = vtnr;
348
349         controllers = reset_controllers = NULL;
350
351         if (!isempty(tty)) {
352                 session->tty = strdup(tty);
353                 if (!session->tty) {
354                         r = -ENOMEM;
355                         goto fail;
356                 }
357         }
358
359         if (!isempty(display)) {
360                 session->display = strdup(display);
361                 if (!session->display) {
362                         r = -ENOMEM;
363                         goto fail;
364                 }
365         }
366
367         if (!isempty(remote_user)) {
368                 session->remote_user = strdup(remote_user);
369                 if (!session->remote_user) {
370                         r = -ENOMEM;
371                         goto fail;
372                 }
373         }
374
375         if (!isempty(remote_host)) {
376                 session->remote_host = strdup(remote_host);
377                 if (!session->remote_host) {
378                         r = -ENOMEM;
379                         goto fail;
380                 }
381         }
382
383         if (!isempty(service)) {
384                 session->service = strdup(service);
385                 if (!session->service) {
386                         r = -ENOMEM;
387                         goto fail;
388                 }
389         }
390
391         if (pipe(pipe_fds) < 0) {
392                 r = -errno;
393                 goto fail;
394         }
395
396         session->pipe_fd = pipe_fds[0];
397         pipe_fds[0] = -1;
398
399         if (s) {
400                 r = seat_attach_session(s, session);
401                 if (r < 0)
402                         goto fail;
403         }
404
405         r = session_start(session);
406         if (r < 0)
407                 goto fail;
408
409         reply = dbus_message_new_method_return(message);
410         if (!reply) {
411                 r = -ENOMEM;
412                 goto fail;
413         }
414
415         p = session_bus_path(session);
416         if (!p) {
417                 r = -ENOMEM;
418                 goto fail;
419         }
420
421         b = dbus_message_append_args(
422                         reply,
423                         DBUS_TYPE_STRING, &session->id,
424                         DBUS_TYPE_OBJECT_PATH, &p,
425                         DBUS_TYPE_STRING, &session->user->runtime_path,
426                         DBUS_TYPE_UNIX_FD, &pipe_fds[1],
427                         DBUS_TYPE_INVALID);
428         free(p);
429
430         if (!b) {
431                 r = -ENOMEM;
432                 goto fail;
433         }
434
435         close_nointr_nofail(pipe_fds[1]);
436         *_reply = reply;
437
438         return 0;
439
440 fail:
441         strv_free(controllers);
442         strv_free(reset_controllers);
443
444         if (session)
445                 session_add_to_gc_queue(session);
446
447         if (user)
448                 user_add_to_gc_queue(user);
449
450         close_pipe(pipe_fds);
451
452         if (reply)
453                 dbus_message_unref(reply);
454
455         return r;
456 }
457
458 static DBusHandlerResult manager_message_handler(
459                 DBusConnection *connection,
460                 DBusMessage *message,
461                 void *userdata) {
462
463         Manager *m = userdata;
464
465         const BusProperty properties[] = {
466                 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy",  bus_property_append_string,   "s",  m->cgroup_path          },
467                 { "org.freedesktop.login1.Manager", "Controllers",            bus_property_append_strv,     "as", m->controllers          },
468                 { "org.freedesktop.login1.Manager", "NAutoVTs",               bus_property_append_unsigned, "u",  &m->n_autovts           },
469                 { "org.freedesktop.login1.Manager", "KillOnlyUsers",          bus_property_append_strv,     "as", m->kill_only_users      },
470                 { "org.freedesktop.login1.Manager", "KillExcludeUsers",       bus_property_append_strv,     "as", m->kill_exclude_users   },
471                 { "org.freedesktop.login1.Manager", "KillUserProcesses",      bus_property_append_bool,     "b",  &m->kill_user_processes },
472                 { "org.freedesktop.login1.Manager", "IdleHint",               bus_manager_append_idle_hint, "b",  m                       },
473                 { "org.freedesktop.login1.Manager", "IdleSinceHint",          bus_manager_append_idle_hint_since, "t", m                  },
474                 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m                  },
475                 { NULL, NULL, NULL, NULL, NULL }
476         };
477
478         DBusError error;
479         DBusMessage *reply = NULL;
480         int r;
481
482         assert(connection);
483         assert(message);
484         assert(m);
485
486         dbus_error_init(&error);
487
488         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
489                 const char *name;
490                 char *p;
491                 Session *session;
492                 bool b;
493
494                 if (!dbus_message_get_args(
495                                     message,
496                                     &error,
497                                     DBUS_TYPE_STRING, &name,
498                                     DBUS_TYPE_INVALID))
499                         return bus_send_error_reply(connection, message, &error, -EINVAL);
500
501                 session = hashmap_get(m->sessions, name);
502                 if (!session)
503                         return bus_send_error_reply(connection, message, &error, -ENOENT);
504
505                 reply = dbus_message_new_method_return(message);
506                 if (!reply)
507                         goto oom;
508
509                 p = session_bus_path(session);
510                 if (!p)
511                         goto oom;
512
513                 b = dbus_message_append_args(
514                                 reply,
515                                 DBUS_TYPE_OBJECT_PATH, &p,
516                                 DBUS_TYPE_INVALID);
517                 free(p);
518
519                 if (!b)
520                         goto oom;
521
522         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
523                 uint32_t uid;
524                 char *p;
525                 User *user;
526                 bool b;
527
528                 if (!dbus_message_get_args(
529                                     message,
530                                     &error,
531                                     DBUS_TYPE_UINT32, &uid,
532                                     DBUS_TYPE_INVALID))
533                         return bus_send_error_reply(connection, message, &error, -EINVAL);
534
535                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
536                 if (!user)
537                         return bus_send_error_reply(connection, message, &error, -ENOENT);
538
539                 reply = dbus_message_new_method_return(message);
540                 if (!reply)
541                         goto oom;
542
543                 p = user_bus_path(user);
544                 if (!p)
545                         goto oom;
546
547                 b = dbus_message_append_args(
548                                 reply,
549                                 DBUS_TYPE_OBJECT_PATH, &p,
550                                 DBUS_TYPE_INVALID);
551                 free(p);
552
553                 if (!b)
554                         goto oom;
555
556         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
557                 const char *name;
558                 char *p;
559                 Seat *seat;
560                 bool b;
561
562                 if (!dbus_message_get_args(
563                                     message,
564                                     &error,
565                                     DBUS_TYPE_STRING, &name,
566                                     DBUS_TYPE_INVALID))
567                         return bus_send_error_reply(connection, message, &error, -EINVAL);
568
569                 seat = hashmap_get(m->seats, name);
570                 if (!seat)
571                         return bus_send_error_reply(connection, message, &error, -ENOENT);
572
573                 reply = dbus_message_new_method_return(message);
574                 if (!reply)
575                         goto oom;
576
577                 p = seat_bus_path(seat);
578                 if (!p)
579                         goto oom;
580
581                 b = dbus_message_append_args(
582                                 reply,
583                                 DBUS_TYPE_OBJECT_PATH, &p,
584                                 DBUS_TYPE_INVALID);
585                 free(p);
586
587                 if (!b)
588                         goto oom;
589
590         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
591                 char *p;
592                 Session *session;
593                 Iterator i;
594                 DBusMessageIter iter, sub;
595                 const char *empty = "";
596
597                 reply = dbus_message_new_method_return(message);
598                 if (!reply)
599                         goto oom;
600
601                 dbus_message_iter_init_append(reply, &iter);
602
603                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
604                         goto oom;
605
606                 HASHMAP_FOREACH(session, m->sessions, i) {
607                         DBusMessageIter sub2;
608                         uint32_t uid;
609
610                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
611                                 goto oom;
612
613                         uid = session->user->uid;
614
615                         p = session_bus_path(session);
616                         if (!p)
617                                 goto oom;
618
619                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
620                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
621                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
622                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
623                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
624                                 free(p);
625                                 goto oom;
626                         }
627
628                         free(p);
629
630                         if (!dbus_message_iter_close_container(&sub, &sub2))
631                                 goto oom;
632                 }
633
634                 if (!dbus_message_iter_close_container(&iter, &sub))
635                         goto oom;
636
637         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
638                 char *p;
639                 User *user;
640                 Iterator i;
641                 DBusMessageIter iter, sub;
642
643                 reply = dbus_message_new_method_return(message);
644                 if (!reply)
645                         goto oom;
646
647                 dbus_message_iter_init_append(reply, &iter);
648
649                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
650                         goto oom;
651
652                 HASHMAP_FOREACH(user, m->users, i) {
653                         DBusMessageIter sub2;
654                         uint32_t uid;
655
656                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
657                                 goto oom;
658
659                         uid = user->uid;
660
661                         p = user_bus_path(user);
662                         if (!p)
663                                 goto oom;
664
665                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
666                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
667                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
668                                 free(p);
669                                 goto oom;
670                         }
671
672                         free(p);
673
674                         if (!dbus_message_iter_close_container(&sub, &sub2))
675                                 goto oom;
676                 }
677
678                 if (!dbus_message_iter_close_container(&iter, &sub))
679                         goto oom;
680
681         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
682                 char *p;
683                 Seat *seat;
684                 Iterator i;
685                 DBusMessageIter iter, sub;
686
687                 reply = dbus_message_new_method_return(message);
688                 if (!reply)
689                         goto oom;
690
691                 dbus_message_iter_init_append(reply, &iter);
692
693                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
694                         goto oom;
695
696                 HASHMAP_FOREACH(seat, m->seats, i) {
697                         DBusMessageIter sub2;
698
699                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
700                                 goto oom;
701
702                         p = seat_bus_path(seat);
703                         if (!p)
704                                 goto oom;
705
706                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
707                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
708                                 free(p);
709                                 goto oom;
710                         }
711
712                         free(p);
713
714                         if (!dbus_message_iter_close_container(&sub, &sub2))
715                                 goto oom;
716                 }
717
718                 if (!dbus_message_iter_close_container(&iter, &sub))
719                         goto oom;
720
721         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
722
723                 r = bus_manager_create_session(m, message, &reply);
724                 if (r == -ENOMEM)
725                         goto oom;
726
727                 if (r < 0)
728                         return bus_send_error_reply(connection, message, &error, r);
729
730         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
731                 const char *name;
732                 Session *session;
733
734                 if (!dbus_message_get_args(
735                                     message,
736                                     &error,
737                                     DBUS_TYPE_STRING, &name,
738                                     DBUS_TYPE_INVALID))
739                         return bus_send_error_reply(connection, message, &error, -EINVAL);
740
741                 session = hashmap_get(m->sessions, name);
742                 if (!session)
743                         return bus_send_error_reply(connection, message, &error, -ENOENT);
744
745                 r = session_activate(session);
746                 if (r < 0)
747                         return bus_send_error_reply(connection, message, NULL, r);
748
749                 reply = dbus_message_new_method_return(message);
750                 if (!reply)
751                         goto oom;
752
753         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
754                 const char *name;
755                 Session *session;
756
757                 if (!dbus_message_get_args(
758                                     message,
759                                     &error,
760                                     DBUS_TYPE_STRING, &name,
761                                     DBUS_TYPE_INVALID))
762                         return bus_send_error_reply(connection, message, &error, -EINVAL);
763
764                 session = hashmap_get(m->sessions, name);
765                 if (!session)
766                         return bus_send_error_reply(connection, message, &error, -ENOENT);
767
768                 r = session_stop(session);
769                 if (r < 0)
770                         return bus_send_error_reply(connection, message, NULL, r);
771
772                 reply = dbus_message_new_method_return(message);
773                 if (!reply)
774                         goto oom;
775
776         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
777                 uint32_t uid;
778                 User *user;
779
780                 if (!dbus_message_get_args(
781                                     message,
782                                     &error,
783                                     DBUS_TYPE_UINT32, &uid,
784                                     DBUS_TYPE_INVALID))
785                         return bus_send_error_reply(connection, message, &error, -EINVAL);
786
787                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
788                 if (!user)
789                         return bus_send_error_reply(connection, message, &error, -ENOENT);
790
791                 r = user_stop(user);
792                 if (r < 0)
793                         return bus_send_error_reply(connection, message, NULL, r);
794
795                 reply = dbus_message_new_method_return(message);
796                 if (!reply)
797                         goto oom;
798
799         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
800                 const char *name;
801                 Seat *seat;
802
803                 if (!dbus_message_get_args(
804                                     message,
805                                     &error,
806                                     DBUS_TYPE_STRING, &name,
807                                     DBUS_TYPE_INVALID))
808                         return bus_send_error_reply(connection, message, &error, -EINVAL);
809
810                 seat = hashmap_get(m->seats, name);
811                 if (!seat)
812                         return bus_send_error_reply(connection, message, &error, -ENOENT);
813
814                 r = seat_stop_sessions(seat);
815                 if (r < 0)
816                         return bus_send_error_reply(connection, message, NULL, r);
817
818                 reply = dbus_message_new_method_return(message);
819                 if (!reply)
820                         goto oom;
821
822         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
823                 char *introspection = NULL;
824                 FILE *f;
825                 Iterator i;
826                 Session *session;
827                 Seat *seat;
828                 User *user;
829                 size_t size;
830                 char *p;
831
832                 if (!(reply = dbus_message_new_method_return(message)))
833                         goto oom;
834
835                 /* We roll our own introspection code here, instead of
836                  * relying on bus_default_message_handler() because we
837                  * need to generate our introspection string
838                  * dynamically. */
839
840                 if (!(f = open_memstream(&introspection, &size)))
841                         goto oom;
842
843                 fputs(INTROSPECTION_BEGIN, f);
844
845                 HASHMAP_FOREACH(seat, m->seats, i) {
846                         p = bus_path_escape(seat->id);
847
848                         if (p) {
849                                 fprintf(f, "<node name=\"seat/%s\"/>", p);
850                                 free(p);
851                         }
852                 }
853
854                 HASHMAP_FOREACH(user, m->users, i)
855                         fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
856
857                 HASHMAP_FOREACH(session, m->sessions, i) {
858                         p = bus_path_escape(session->id);
859
860                         if (p) {
861                                 fprintf(f, "<node name=\"session/%s\"/>", p);
862                                 free(p);
863                         }
864                 }
865
866                 fputs(INTROSPECTION_END, f);
867
868                 if (ferror(f)) {
869                         fclose(f);
870                         free(introspection);
871                         goto oom;
872                 }
873
874                 fclose(f);
875
876                 if (!introspection)
877                         goto oom;
878
879                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
880                         free(introspection);
881                         goto oom;
882                 }
883
884                 free(introspection);
885         } else
886                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
887
888         if (reply) {
889                 if (!dbus_connection_send(connection, reply, NULL))
890                         goto oom;
891
892                 dbus_message_unref(reply);
893         }
894
895         return DBUS_HANDLER_RESULT_HANDLED;
896
897 oom:
898         if (reply)
899                 dbus_message_unref(reply);
900
901         dbus_error_free(&error);
902
903         return DBUS_HANDLER_RESULT_NEED_MEMORY;
904 }
905
906 const DBusObjectPathVTable bus_manager_vtable = {
907         .message_function = manager_message_handler
908 };
909
910 DBusHandlerResult bus_message_filter(
911                 DBusConnection *connection,
912                 DBusMessage *message,
913                 void *userdata) {
914
915         Manager *m = userdata;
916         DBusError error;
917
918         assert(m);
919         assert(connection);
920         assert(message);
921
922         dbus_error_init(&error);
923
924         if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
925                 const char *cgroup;
926
927                 if (!dbus_message_get_args(message, &error,
928                                            DBUS_TYPE_STRING, &cgroup,
929                                            DBUS_TYPE_INVALID))
930                         log_error("Failed to parse Released message: %s", bus_error_message(&error));
931                 else
932                         manager_cgroup_notify_empty(m, cgroup);
933         }
934
935         dbus_error_free(&error);
936
937         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
938 }
939
940 int manager_send_changed(Manager *manager, const char *properties) {
941         DBusMessage *m;
942         int r = -ENOMEM;
943
944         assert(manager);
945
946         m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
947         if (!m)
948                 goto finish;
949
950         if (!dbus_connection_send(manager->bus, m, NULL))
951                 goto finish;
952
953         r = 0;
954
955 finish:
956         if (m)
957                 dbus_message_unref(m);
958
959         return r;
960 }