chiark / gitweb /
d48d68c2bb70b4b64d5bdd3fc63c7a54dc43e134
[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                 session = hashmap_get(m->sessions, id);
318
319                 if (session) {
320
321                         /* Session already exists, client is probably
322                          * something like "su" which changes uid but
323                          * is still the same audit session */
324
325                         reply = dbus_message_new_method_return(message);
326                         if (!reply) {
327                                 r = -ENOMEM;
328                                 goto fail;
329                         }
330
331                         /* Create a throw-away fd */
332                         if (pipe(pipe_fds) < 0) {
333                                 r = -errno;
334                                 goto fail;
335                         }
336
337                         close_nointr_nofail(pipe_fds[0]);
338                         pipe_fds[0] = -1;
339
340                         p = session_bus_path(session);
341                         if (!p) {
342                                 r = -ENOMEM;
343                                 goto fail;
344                         }
345
346                         b = dbus_message_append_args(
347                                         reply,
348                                         DBUS_TYPE_STRING, &session->id,
349                                         DBUS_TYPE_OBJECT_PATH, &p,
350                                         DBUS_TYPE_STRING, &session->user->runtime_path,
351                                         DBUS_TYPE_UNIX_FD, &pipe_fds[1],
352                                         DBUS_TYPE_INVALID);
353                         free(p);
354
355                         if (!b) {
356                                 r = -ENOMEM;
357                                 goto fail;
358                         }
359
360                         close_nointr_nofail(pipe_fds[1]);
361                         *_reply = reply;
362
363                         return 0;
364                 }
365
366         } else {
367                 do {
368                         free(id);
369                         asprintf(&id, "c%lu", ++m->session_counter);
370
371                         if (!id) {
372                                 r = -ENOMEM;
373                                 goto fail;
374                         }
375
376                 } while (hashmap_get(m->sessions, id));
377         }
378
379         r = manager_add_session(m, user, id, &session);
380         free(id);
381         if (r < 0)
382                 goto fail;
383
384         session->leader = leader;
385         session->audit_id = audit_id;
386         session->type = t;
387         session->remote = remote;
388         session->controllers = controllers;
389         session->reset_controllers = reset_controllers;
390         session->kill_processes = kill_processes;
391         session->vtnr = vtnr;
392
393         controllers = reset_controllers = NULL;
394
395         if (!isempty(tty)) {
396                 session->tty = strdup(tty);
397                 if (!session->tty) {
398                         r = -ENOMEM;
399                         goto fail;
400                 }
401         }
402
403         if (!isempty(display)) {
404                 session->display = strdup(display);
405                 if (!session->display) {
406                         r = -ENOMEM;
407                         goto fail;
408                 }
409         }
410
411         if (!isempty(remote_user)) {
412                 session->remote_user = strdup(remote_user);
413                 if (!session->remote_user) {
414                         r = -ENOMEM;
415                         goto fail;
416                 }
417         }
418
419         if (!isempty(remote_host)) {
420                 session->remote_host = strdup(remote_host);
421                 if (!session->remote_host) {
422                         r = -ENOMEM;
423                         goto fail;
424                 }
425         }
426
427         if (!isempty(service)) {
428                 session->service = strdup(service);
429                 if (!session->service) {
430                         r = -ENOMEM;
431                         goto fail;
432                 }
433         }
434
435         if (pipe(pipe_fds) < 0) {
436                 r = -errno;
437                 goto fail;
438         }
439
440         session->pipe_fd = pipe_fds[0];
441         pipe_fds[0] = -1;
442
443         if (s) {
444                 r = seat_attach_session(s, session);
445                 if (r < 0)
446                         goto fail;
447         }
448
449         r = session_start(session);
450         if (r < 0)
451                 goto fail;
452
453         reply = dbus_message_new_method_return(message);
454         if (!reply) {
455                 r = -ENOMEM;
456                 goto fail;
457         }
458
459         p = session_bus_path(session);
460         if (!p) {
461                 r = -ENOMEM;
462                 goto fail;
463         }
464
465         b = dbus_message_append_args(
466                         reply,
467                         DBUS_TYPE_STRING, &session->id,
468                         DBUS_TYPE_OBJECT_PATH, &p,
469                         DBUS_TYPE_STRING, &session->user->runtime_path,
470                         DBUS_TYPE_UNIX_FD, &pipe_fds[1],
471                         DBUS_TYPE_INVALID);
472         free(p);
473
474         if (!b) {
475                 r = -ENOMEM;
476                 goto fail;
477         }
478
479         close_nointr_nofail(pipe_fds[1]);
480         *_reply = reply;
481
482         return 0;
483
484 fail:
485         strv_free(controllers);
486         strv_free(reset_controllers);
487
488         if (session)
489                 session_add_to_gc_queue(session);
490
491         if (user)
492                 user_add_to_gc_queue(user);
493
494         close_pipe(pipe_fds);
495
496         if (reply)
497                 dbus_message_unref(reply);
498
499         return r;
500 }
501
502 static DBusHandlerResult manager_message_handler(
503                 DBusConnection *connection,
504                 DBusMessage *message,
505                 void *userdata) {
506
507         Manager *m = userdata;
508
509         const BusProperty properties[] = {
510                 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy",  bus_property_append_string,   "s",  m->cgroup_path          },
511                 { "org.freedesktop.login1.Manager", "Controllers",            bus_property_append_strv,     "as", m->controllers          },
512                 { "org.freedesktop.login1.Manager", "NAutoVTs",               bus_property_append_unsigned, "u",  &m->n_autovts           },
513                 { "org.freedesktop.login1.Manager", "KillOnlyUsers",          bus_property_append_strv,     "as", m->kill_only_users      },
514                 { "org.freedesktop.login1.Manager", "KillExcludeUsers",       bus_property_append_strv,     "as", m->kill_exclude_users   },
515                 { "org.freedesktop.login1.Manager", "KillUserProcesses",      bus_property_append_bool,     "b",  &m->kill_user_processes },
516                 { "org.freedesktop.login1.Manager", "IdleHint",               bus_manager_append_idle_hint, "b",  m                       },
517                 { "org.freedesktop.login1.Manager", "IdleSinceHint",          bus_manager_append_idle_hint_since, "t", m                  },
518                 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m                  },
519                 { NULL, NULL, NULL, NULL, NULL }
520         };
521
522         DBusError error;
523         DBusMessage *reply = NULL;
524         int r;
525
526         assert(connection);
527         assert(message);
528         assert(m);
529
530         dbus_error_init(&error);
531
532         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
533                 const char *name;
534                 char *p;
535                 Session *session;
536                 bool b;
537
538                 if (!dbus_message_get_args(
539                                     message,
540                                     &error,
541                                     DBUS_TYPE_STRING, &name,
542                                     DBUS_TYPE_INVALID))
543                         return bus_send_error_reply(connection, message, &error, -EINVAL);
544
545                 session = hashmap_get(m->sessions, name);
546                 if (!session)
547                         return bus_send_error_reply(connection, message, &error, -ENOENT);
548
549                 reply = dbus_message_new_method_return(message);
550                 if (!reply)
551                         goto oom;
552
553                 p = session_bus_path(session);
554                 if (!p)
555                         goto oom;
556
557                 b = dbus_message_append_args(
558                                 reply,
559                                 DBUS_TYPE_OBJECT_PATH, &p,
560                                 DBUS_TYPE_INVALID);
561                 free(p);
562
563                 if (!b)
564                         goto oom;
565
566         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
567                 uint32_t uid;
568                 char *p;
569                 User *user;
570                 bool b;
571
572                 if (!dbus_message_get_args(
573                                     message,
574                                     &error,
575                                     DBUS_TYPE_UINT32, &uid,
576                                     DBUS_TYPE_INVALID))
577                         return bus_send_error_reply(connection, message, &error, -EINVAL);
578
579                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
580                 if (!user)
581                         return bus_send_error_reply(connection, message, &error, -ENOENT);
582
583                 reply = dbus_message_new_method_return(message);
584                 if (!reply)
585                         goto oom;
586
587                 p = user_bus_path(user);
588                 if (!p)
589                         goto oom;
590
591                 b = dbus_message_append_args(
592                                 reply,
593                                 DBUS_TYPE_OBJECT_PATH, &p,
594                                 DBUS_TYPE_INVALID);
595                 free(p);
596
597                 if (!b)
598                         goto oom;
599
600         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
601                 const char *name;
602                 char *p;
603                 Seat *seat;
604                 bool b;
605
606                 if (!dbus_message_get_args(
607                                     message,
608                                     &error,
609                                     DBUS_TYPE_STRING, &name,
610                                     DBUS_TYPE_INVALID))
611                         return bus_send_error_reply(connection, message, &error, -EINVAL);
612
613                 seat = hashmap_get(m->seats, name);
614                 if (!seat)
615                         return bus_send_error_reply(connection, message, &error, -ENOENT);
616
617                 reply = dbus_message_new_method_return(message);
618                 if (!reply)
619                         goto oom;
620
621                 p = seat_bus_path(seat);
622                 if (!p)
623                         goto oom;
624
625                 b = dbus_message_append_args(
626                                 reply,
627                                 DBUS_TYPE_OBJECT_PATH, &p,
628                                 DBUS_TYPE_INVALID);
629                 free(p);
630
631                 if (!b)
632                         goto oom;
633
634         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
635                 char *p;
636                 Session *session;
637                 Iterator i;
638                 DBusMessageIter iter, sub;
639                 const char *empty = "";
640
641                 reply = dbus_message_new_method_return(message);
642                 if (!reply)
643                         goto oom;
644
645                 dbus_message_iter_init_append(reply, &iter);
646
647                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
648                         goto oom;
649
650                 HASHMAP_FOREACH(session, m->sessions, i) {
651                         DBusMessageIter sub2;
652                         uint32_t uid;
653
654                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
655                                 goto oom;
656
657                         uid = session->user->uid;
658
659                         p = session_bus_path(session);
660                         if (!p)
661                                 goto oom;
662
663                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
664                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
665                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
666                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
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", "ListUsers")) {
682                 char *p;
683                 User *user;
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, "(uso)", &sub))
694                         goto oom;
695
696                 HASHMAP_FOREACH(user, m->users, i) {
697                         DBusMessageIter sub2;
698                         uint32_t uid;
699
700                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
701                                 goto oom;
702
703                         uid = user->uid;
704
705                         p = user_bus_path(user);
706                         if (!p)
707                                 goto oom;
708
709                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
710                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
711                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
712                                 free(p);
713                                 goto oom;
714                         }
715
716                         free(p);
717
718                         if (!dbus_message_iter_close_container(&sub, &sub2))
719                                 goto oom;
720                 }
721
722                 if (!dbus_message_iter_close_container(&iter, &sub))
723                         goto oom;
724
725         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
726                 char *p;
727                 Seat *seat;
728                 Iterator i;
729                 DBusMessageIter iter, sub;
730
731                 reply = dbus_message_new_method_return(message);
732                 if (!reply)
733                         goto oom;
734
735                 dbus_message_iter_init_append(reply, &iter);
736
737                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
738                         goto oom;
739
740                 HASHMAP_FOREACH(seat, m->seats, i) {
741                         DBusMessageIter sub2;
742
743                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
744                                 goto oom;
745
746                         p = seat_bus_path(seat);
747                         if (!p)
748                                 goto oom;
749
750                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
751                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
752                                 free(p);
753                                 goto oom;
754                         }
755
756                         free(p);
757
758                         if (!dbus_message_iter_close_container(&sub, &sub2))
759                                 goto oom;
760                 }
761
762                 if (!dbus_message_iter_close_container(&iter, &sub))
763                         goto oom;
764
765         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
766
767                 r = bus_manager_create_session(m, message, &reply);
768                 if (r == -ENOMEM)
769                         goto oom;
770
771                 if (r < 0)
772                         return bus_send_error_reply(connection, message, &error, r);
773
774         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
775                 const char *name;
776                 Session *session;
777
778                 if (!dbus_message_get_args(
779                                     message,
780                                     &error,
781                                     DBUS_TYPE_STRING, &name,
782                                     DBUS_TYPE_INVALID))
783                         return bus_send_error_reply(connection, message, &error, -EINVAL);
784
785                 session = hashmap_get(m->sessions, name);
786                 if (!session)
787                         return bus_send_error_reply(connection, message, &error, -ENOENT);
788
789                 r = session_activate(session);
790                 if (r < 0)
791                         return bus_send_error_reply(connection, message, NULL, r);
792
793                 reply = dbus_message_new_method_return(message);
794                 if (!reply)
795                         goto oom;
796
797         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
798                 const char *name;
799                 Session *session;
800
801                 if (!dbus_message_get_args(
802                                     message,
803                                     &error,
804                                     DBUS_TYPE_STRING, &name,
805                                     DBUS_TYPE_INVALID))
806                         return bus_send_error_reply(connection, message, &error, -EINVAL);
807
808                 session = hashmap_get(m->sessions, name);
809                 if (!session)
810                         return bus_send_error_reply(connection, message, &error, -ENOENT);
811
812                 r = session_stop(session);
813                 if (r < 0)
814                         return bus_send_error_reply(connection, message, NULL, r);
815
816                 reply = dbus_message_new_method_return(message);
817                 if (!reply)
818                         goto oom;
819
820         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
821                 uint32_t uid;
822                 User *user;
823
824                 if (!dbus_message_get_args(
825                                     message,
826                                     &error,
827                                     DBUS_TYPE_UINT32, &uid,
828                                     DBUS_TYPE_INVALID))
829                         return bus_send_error_reply(connection, message, &error, -EINVAL);
830
831                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
832                 if (!user)
833                         return bus_send_error_reply(connection, message, &error, -ENOENT);
834
835                 r = user_stop(user);
836                 if (r < 0)
837                         return bus_send_error_reply(connection, message, NULL, r);
838
839                 reply = dbus_message_new_method_return(message);
840                 if (!reply)
841                         goto oom;
842
843         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
844                 const char *name;
845                 Seat *seat;
846
847                 if (!dbus_message_get_args(
848                                     message,
849                                     &error,
850                                     DBUS_TYPE_STRING, &name,
851                                     DBUS_TYPE_INVALID))
852                         return bus_send_error_reply(connection, message, &error, -EINVAL);
853
854                 seat = hashmap_get(m->seats, name);
855                 if (!seat)
856                         return bus_send_error_reply(connection, message, &error, -ENOENT);
857
858                 r = seat_stop_sessions(seat);
859                 if (r < 0)
860                         return bus_send_error_reply(connection, message, NULL, r);
861
862                 reply = dbus_message_new_method_return(message);
863                 if (!reply)
864                         goto oom;
865
866         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
867                 char *introspection = NULL;
868                 FILE *f;
869                 Iterator i;
870                 Session *session;
871                 Seat *seat;
872                 User *user;
873                 size_t size;
874                 char *p;
875
876                 if (!(reply = dbus_message_new_method_return(message)))
877                         goto oom;
878
879                 /* We roll our own introspection code here, instead of
880                  * relying on bus_default_message_handler() because we
881                  * need to generate our introspection string
882                  * dynamically. */
883
884                 if (!(f = open_memstream(&introspection, &size)))
885                         goto oom;
886
887                 fputs(INTROSPECTION_BEGIN, f);
888
889                 HASHMAP_FOREACH(seat, m->seats, i) {
890                         p = bus_path_escape(seat->id);
891
892                         if (p) {
893                                 fprintf(f, "<node name=\"seat/%s\"/>", p);
894                                 free(p);
895                         }
896                 }
897
898                 HASHMAP_FOREACH(user, m->users, i)
899                         fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
900
901                 HASHMAP_FOREACH(session, m->sessions, i) {
902                         p = bus_path_escape(session->id);
903
904                         if (p) {
905                                 fprintf(f, "<node name=\"session/%s\"/>", p);
906                                 free(p);
907                         }
908                 }
909
910                 fputs(INTROSPECTION_END, f);
911
912                 if (ferror(f)) {
913                         fclose(f);
914                         free(introspection);
915                         goto oom;
916                 }
917
918                 fclose(f);
919
920                 if (!introspection)
921                         goto oom;
922
923                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
924                         free(introspection);
925                         goto oom;
926                 }
927
928                 free(introspection);
929         } else
930                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
931
932         if (reply) {
933                 if (!dbus_connection_send(connection, reply, NULL))
934                         goto oom;
935
936                 dbus_message_unref(reply);
937         }
938
939         return DBUS_HANDLER_RESULT_HANDLED;
940
941 oom:
942         if (reply)
943                 dbus_message_unref(reply);
944
945         dbus_error_free(&error);
946
947         return DBUS_HANDLER_RESULT_NEED_MEMORY;
948 }
949
950 const DBusObjectPathVTable bus_manager_vtable = {
951         .message_function = manager_message_handler
952 };
953
954 DBusHandlerResult bus_message_filter(
955                 DBusConnection *connection,
956                 DBusMessage *message,
957                 void *userdata) {
958
959         Manager *m = userdata;
960         DBusError error;
961
962         assert(m);
963         assert(connection);
964         assert(message);
965
966         dbus_error_init(&error);
967
968         if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
969                 const char *cgroup;
970
971                 if (!dbus_message_get_args(message, &error,
972                                            DBUS_TYPE_STRING, &cgroup,
973                                            DBUS_TYPE_INVALID))
974                         log_error("Failed to parse Released message: %s", bus_error_message(&error));
975                 else
976                         manager_cgroup_notify_empty(m, cgroup);
977         }
978
979         dbus_error_free(&error);
980
981         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
982 }
983
984 int manager_send_changed(Manager *manager, const char *properties) {
985         DBusMessage *m;
986         int r = -ENOMEM;
987
988         assert(manager);
989
990         m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
991         if (!m)
992                 goto finish;
993
994         if (!dbus_connection_send(manager->bus, m, NULL))
995                 goto finish;
996
997         r = 0;
998
999 finish:
1000         if (m)
1001                 dbus_message_unref(m);
1002
1003         return r;
1004 }