]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/logind-dbus.c
logind: remove a session when its cgroup is gone
[thirdparty/systemd.git] / src / logind-dbus.c
CommitLineData
3f49d45a
LP
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
a185c5aa
LP
22#include <errno.h>
23#include <string.h>
98a28fef 24#include <unistd.h>
a185c5aa 25
3f49d45a
LP
26#include "logind.h"
27#include "dbus-common.h"
98a28fef 28#include "strv.h"
3f49d45a
LP
29
30#define BUS_MANAGER_INTERFACE \
31 " <interface name=\"org.freedesktop.login1.Manager\">\n" \
bef422ae 32 " <method name=\"GetSession\">\n" \
3f49d45a 33 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
bef422ae 34 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
3f49d45a
LP
35 " </method>\n" \
36 " <method name=\"GetUser\">\n" \
bef422ae 37 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
3f49d45a
LP
38 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
39 " </method>\n" \
bef422ae 40 " <method name=\"GetSeat\">\n" \
3f49d45a 41 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
bef422ae 42 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
3f49d45a 43 " </method>\n" \
bef422ae 44 " <method name=\"ListSessions\">\n" \
e1c9c2d5 45 " <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
3f49d45a
LP
46 " </method>\n" \
47 " <method name=\"ListUsers\">\n" \
48 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
49 " </method>\n" \
bef422ae
LP
50 " <method name=\"ListSeats\">\n" \
51 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
3f49d45a
LP
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" \
98a28fef 56 " <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n" \
3f49d45a
LP
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" \
98a28fef 66 " <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
3f49d45a
LP
67 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
68 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
98a28fef 69 " <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
3f49d45a
LP
70 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
71 " </method>\n" \
a185c5aa
LP
72 " <method name=\"ActivateSession\">\n" \
73 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
74 " </method>\n" \
3f49d45a
LP
75 " <method name=\"TerminateSession\">\n" \
76 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
77 " </method>\n" \
78 " <method name=\"TerminateUser\">\n" \
bef422ae 79 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
3f49d45a
LP
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" \
a185c5aa
LP
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" \
3f49d45a
LP
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
a185c5aa
LP
135static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
136 Manager *m = data;
77527da0 137 dbus_bool_t b;
a185c5aa
LP
138
139 assert(i);
140 assert(property);
141 assert(m);
142
77527da0 143 b = manager_get_idle_hint(m, NULL) > 0;
a185c5aa
LP
144 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
145 return -ENOMEM;
146
147 return 0;
148}
149
150static 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
98a28fef
LP
168static 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, *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 else
312 asprintf(&id, "c%lu", ++m->session_counter);
313
314 if (!id) {
315 r = -ENOMEM;
316 goto fail;
317 }
318
319 if (hashmap_get(m->sessions, id)) {
320 r = -EEXIST;
321 goto fail;
322 }
323
324 r = manager_add_session(m, user, id, &session);
325 free(id);
326 if (r < 0)
327 goto fail;
328
329 session->leader = leader;
330 session->audit_id = audit_id;
331 session->type = t;
332 session->remote = remote;
333 session->controllers = controllers;
334 session->reset_controllers = reset_controllers;
335 session->kill_processes = kill_processes;
336 session->vtnr = vtnr;
337
338 controllers = reset_controllers = NULL;
339
340 if (!isempty(tty)) {
341 session->tty = strdup(tty);
342 if (!session->tty) {
343 r = -ENOMEM;
344 goto fail;
345 }
346 }
347
348 if (!isempty(display)) {
349 session->display = strdup(display);
350 if (!session->display) {
351 r = -ENOMEM;
352 goto fail;
353 }
354 }
355
356 if (!isempty(remote_user)) {
357 session->remote_user = strdup(remote_user);
358 if (!session->remote_user) {
359 r = -ENOMEM;
360 goto fail;
361 }
362 }
363
364 if (!isempty(remote_host)) {
365 session->remote_host = strdup(remote_host);
366 if (!session->remote_host) {
367 r = -ENOMEM;
368 goto fail;
369 }
370 }
371
372 if (!isempty(service)) {
373 session->service = strdup(service);
374 if (!session->service) {
375 r = -ENOMEM;
376 goto fail;
377 }
378 }
379
380 if (pipe(pipe_fds) < 0) {
381 r = -errno;
382 goto fail;
383 }
384
19bc719e
LP
385 session->pipe_fd = pipe_fds[0];
386 pipe_fds[0] = -1;
98a28fef
LP
387
388 if (s) {
389 r = seat_attach_session(s, session);
390 if (r < 0)
391 goto fail;
392 }
393
394 r = session_start(session);
395 if (r < 0)
396 goto fail;
397
398 reply = dbus_message_new_method_return(message);
399 if (!reply) {
400 r = -ENOMEM;
401 goto fail;
402 }
403
404 p = session_bus_path(session);
405 if (!p) {
406 r = -ENOMEM;
407 goto fail;
408 }
409
410 b = dbus_message_append_args(
411 reply,
412 DBUS_TYPE_STRING, &session->id,
413 DBUS_TYPE_OBJECT_PATH, &p,
414 DBUS_TYPE_STRING, &session->user->runtime_path,
19bc719e 415 DBUS_TYPE_UNIX_FD, &pipe_fds[1],
98a28fef
LP
416 DBUS_TYPE_INVALID);
417 free(p);
418
419 if (!b) {
420 r = -ENOMEM;
421 goto fail;
422 }
423
19bc719e 424 close_nointr_nofail(pipe_fds[1]);
98a28fef
LP
425 *_reply = reply;
426
427 return 0;
428
429fail:
430 strv_free(controllers);
431 strv_free(reset_controllers);
432
433 if (session)
434 session_add_to_gc_queue(session);
435
436 if (user)
437 user_add_to_gc_queue(user);
438
439 close_pipe(pipe_fds);
440
441 if (reply)
442 dbus_message_unref(reply);
443
444 return r;
445}
446
3f49d45a
LP
447static DBusHandlerResult manager_message_handler(
448 DBusConnection *connection,
449 DBusMessage *message,
450 void *userdata) {
451
452 Manager *m = userdata;
453
454 const BusProperty properties[] = {
a185c5aa
LP
455 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
456 { "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
a185c5aa
LP
457 { "org.freedesktop.login1.Manager", "NAutoVTs", bus_property_append_unsigned, "u", &m->n_autovts },
458 { "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
459 { "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
460 { "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
461 { "org.freedesktop.login1.Manager", "IdleHint", bus_manager_append_idle_hint, "b", m },
462 { "org.freedesktop.login1.Manager", "IdleSinceHint", bus_manager_append_idle_hint_since, "t", m },
463 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
3f49d45a
LP
464 { NULL, NULL, NULL, NULL, NULL }
465 };
466
467 DBusError error;
468 DBusMessage *reply = NULL;
bef422ae 469 int r;
3f49d45a
LP
470
471 assert(connection);
472 assert(message);
473 assert(m);
474
475 dbus_error_init(&error);
476
bef422ae
LP
477 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
478 const char *name;
479 char *p;
480 Session *session;
481 bool b;
482
483 if (!dbus_message_get_args(
484 message,
485 &error,
486 DBUS_TYPE_STRING, &name,
487 DBUS_TYPE_INVALID))
488 return bus_send_error_reply(connection, message, &error, -EINVAL);
489
490 session = hashmap_get(m->sessions, name);
491 if (!session)
492 return bus_send_error_reply(connection, message, &error, -ENOENT);
493
494 reply = dbus_message_new_method_return(message);
495 if (!reply)
496 goto oom;
497
498 p = session_bus_path(session);
499 if (!p)
500 goto oom;
501
502 b = dbus_message_append_args(
503 reply,
504 DBUS_TYPE_OBJECT_PATH, &p,
505 DBUS_TYPE_INVALID);
506 free(p);
507
508 if (!b)
509 goto oom;
510
511 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
512 uint32_t uid;
513 char *p;
514 User *user;
515 bool b;
516
517 if (!dbus_message_get_args(
518 message,
519 &error,
520 DBUS_TYPE_UINT32, &uid,
521 DBUS_TYPE_INVALID))
522 return bus_send_error_reply(connection, message, &error, -EINVAL);
523
524 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
525 if (!user)
526 return bus_send_error_reply(connection, message, &error, -ENOENT);
527
528 reply = dbus_message_new_method_return(message);
529 if (!reply)
530 goto oom;
531
532 p = user_bus_path(user);
533 if (!p)
534 goto oom;
535
536 b = dbus_message_append_args(
537 reply,
538 DBUS_TYPE_OBJECT_PATH, &p,
539 DBUS_TYPE_INVALID);
540 free(p);
541
542 if (!b)
543 goto oom;
544
545 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
546 const char *name;
547 char *p;
548 Seat *seat;
549 bool b;
550
551 if (!dbus_message_get_args(
552 message,
553 &error,
554 DBUS_TYPE_STRING, &name,
555 DBUS_TYPE_INVALID))
556 return bus_send_error_reply(connection, message, &error, -EINVAL);
557
558 seat = hashmap_get(m->seats, name);
559 if (!seat)
560 return bus_send_error_reply(connection, message, &error, -ENOENT);
561
562 reply = dbus_message_new_method_return(message);
563 if (!reply)
564 goto oom;
565
566 p = seat_bus_path(seat);
567 if (!p)
568 goto oom;
569
570 b = dbus_message_append_args(
571 reply,
572 DBUS_TYPE_OBJECT_PATH, &p,
573 DBUS_TYPE_INVALID);
574 free(p);
575
576 if (!b)
577 goto oom;
578
e1c9c2d5
LP
579 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
580 char *p;
581 Session *session;
582 Iterator i;
583 DBusMessageIter iter, sub;
584 const char *empty = "";
585
586 reply = dbus_message_new_method_return(message);
587 if (!reply)
588 goto oom;
589
590 dbus_message_iter_init_append(reply, &iter);
591
dec15e92 592 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
e1c9c2d5
LP
593 goto oom;
594
595 HASHMAP_FOREACH(session, m->sessions, i) {
596 DBusMessageIter sub2;
597 uint32_t uid;
598
599 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
600 goto oom;
601
602 uid = session->user->uid;
603
604 p = session_bus_path(session);
605 if (!p)
606 goto oom;
607
608 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
609 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
610 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
611 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
612 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
613 free(p);
614 goto oom;
615 }
616
617 free(p);
618
619 if (!dbus_message_iter_close_container(&sub, &sub2))
620 goto oom;
621 }
622
623 if (!dbus_message_iter_close_container(&iter, &sub))
624 goto oom;
625
626 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
627 char *p;
628 User *user;
629 Iterator i;
630 DBusMessageIter iter, sub;
631
632 reply = dbus_message_new_method_return(message);
633 if (!reply)
634 goto oom;
635
636 dbus_message_iter_init_append(reply, &iter);
637
dec15e92 638 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
e1c9c2d5
LP
639 goto oom;
640
641 HASHMAP_FOREACH(user, m->users, i) {
642 DBusMessageIter sub2;
643 uint32_t uid;
644
645 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
646 goto oom;
647
648 uid = user->uid;
649
650 p = user_bus_path(user);
651 if (!p)
652 goto oom;
653
654 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
655 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
656 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
657 free(p);
658 goto oom;
659 }
660
661 free(p);
662
663 if (!dbus_message_iter_close_container(&sub, &sub2))
664 goto oom;
665 }
666
667 if (!dbus_message_iter_close_container(&iter, &sub))
668 goto oom;
669
670 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
671 char *p;
672 Seat *seat;
673 Iterator i;
674 DBusMessageIter iter, sub;
675
676 reply = dbus_message_new_method_return(message);
677 if (!reply)
678 goto oom;
679
680 dbus_message_iter_init_append(reply, &iter);
681
dec15e92 682 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
e1c9c2d5
LP
683 goto oom;
684
685 HASHMAP_FOREACH(seat, m->seats, i) {
686 DBusMessageIter sub2;
687
688 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
689 goto oom;
690
691 p = seat_bus_path(seat);
692 if (!p)
693 goto oom;
694
695 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
696 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
697 free(p);
698 goto oom;
699 }
700
701 free(p);
702
703 if (!dbus_message_iter_close_container(&sub, &sub2))
704 goto oom;
705 }
706
707 if (!dbus_message_iter_close_container(&iter, &sub))
708 goto oom;
709
98a28fef
LP
710 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
711
712 r = bus_manager_create_session(m, message, &reply);
713 if (r == -ENOMEM)
714 goto oom;
715
716 if (r < 0)
717 return bus_send_error_reply(connection, message, &error, r);
718
bef422ae
LP
719 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
720 const char *name;
721 Session *session;
722
723 if (!dbus_message_get_args(
724 message,
725 &error,
726 DBUS_TYPE_STRING, &name,
727 DBUS_TYPE_INVALID))
728 return bus_send_error_reply(connection, message, &error, -EINVAL);
729
730 session = hashmap_get(m->sessions, name);
731 if (!session)
732 return bus_send_error_reply(connection, message, &error, -ENOENT);
733
734 r = session_activate(session);
735 if (r < 0)
736 return bus_send_error_reply(connection, message, NULL, r);
737
738 reply = dbus_message_new_method_return(message);
739 if (!reply)
740 goto oom;
741
742 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
743 const char *name;
744 Session *session;
745
746 if (!dbus_message_get_args(
747 message,
748 &error,
749 DBUS_TYPE_STRING, &name,
750 DBUS_TYPE_INVALID))
751 return bus_send_error_reply(connection, message, &error, -EINVAL);
752
753 session = hashmap_get(m->sessions, name);
754 if (!session)
755 return bus_send_error_reply(connection, message, &error, -ENOENT);
756
757 r = session_stop(session);
758 if (r < 0)
759 return bus_send_error_reply(connection, message, NULL, r);
760
761 reply = dbus_message_new_method_return(message);
762 if (!reply)
763 goto oom;
764
765 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
766 uint32_t uid;
767 User *user;
768
769 if (!dbus_message_get_args(
770 message,
771 &error,
772 DBUS_TYPE_UINT32, &uid,
773 DBUS_TYPE_INVALID))
774 return bus_send_error_reply(connection, message, &error, -EINVAL);
775
776 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
777 if (!user)
778 return bus_send_error_reply(connection, message, &error, -ENOENT);
779
780 r = user_stop(user);
781 if (r < 0)
782 return bus_send_error_reply(connection, message, NULL, r);
783
784 reply = dbus_message_new_method_return(message);
785 if (!reply)
786 goto oom;
787
788 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
789 const char *name;
790 Seat *seat;
791
792 if (!dbus_message_get_args(
793 message,
794 &error,
795 DBUS_TYPE_STRING, &name,
796 DBUS_TYPE_INVALID))
797 return bus_send_error_reply(connection, message, &error, -EINVAL);
798
799 seat = hashmap_get(m->seats, name);
800 if (!seat)
801 return bus_send_error_reply(connection, message, &error, -ENOENT);
802
803 r = seat_stop_sessions(seat);
804 if (r < 0)
805 return bus_send_error_reply(connection, message, NULL, r);
806
807 reply = dbus_message_new_method_return(message);
808 if (!reply)
809 goto oom;
810
811 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
3f49d45a
LP
812 char *introspection = NULL;
813 FILE *f;
814 Iterator i;
815 Session *session;
816 Seat *seat;
817 User *user;
818 size_t size;
819 char *p;
820
821 if (!(reply = dbus_message_new_method_return(message)))
822 goto oom;
823
824 /* We roll our own introspection code here, instead of
825 * relying on bus_default_message_handler() because we
826 * need to generate our introspection string
827 * dynamically. */
828
829 if (!(f = open_memstream(&introspection, &size)))
830 goto oom;
831
832 fputs(INTROSPECTION_BEGIN, f);
833
834 HASHMAP_FOREACH(seat, m->seats, i) {
835 p = bus_path_escape(seat->id);
836
837 if (p) {
838 fprintf(f, "<node name=\"seat/%s\"/>", p);
839 free(p);
840 }
841 }
842
843 HASHMAP_FOREACH(user, m->users, i)
844 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
845
846 HASHMAP_FOREACH(session, m->sessions, i) {
847 p = bus_path_escape(session->id);
848
849 if (p) {
850 fprintf(f, "<node name=\"session/%s\"/>", p);
851 free(p);
852 }
853 }
854
855 fputs(INTROSPECTION_END, f);
856
857 if (ferror(f)) {
858 fclose(f);
859 free(introspection);
860 goto oom;
861 }
862
863 fclose(f);
864
865 if (!introspection)
866 goto oom;
867
868 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
869 free(introspection);
870 goto oom;
871 }
872
873 free(introspection);
874 } else
875 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
876
877 if (reply) {
878 if (!dbus_connection_send(connection, reply, NULL))
879 goto oom;
880
881 dbus_message_unref(reply);
882 }
883
884 return DBUS_HANDLER_RESULT_HANDLED;
885
886oom:
887 if (reply)
888 dbus_message_unref(reply);
889
890 dbus_error_free(&error);
891
892 return DBUS_HANDLER_RESULT_NEED_MEMORY;
893}
894
895const DBusObjectPathVTable bus_manager_vtable = {
896 .message_function = manager_message_handler
897};
9418f147 898
1713813d
LP
899DBusHandlerResult bus_message_filter(
900 DBusConnection *connection,
901 DBusMessage *message,
902 void *userdata) {
903
904 Manager *m = userdata;
905 DBusError error;
906
907 assert(m);
908 assert(connection);
909 assert(message);
910
911 dbus_error_init(&error);
912
913 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
914 const char *cgroup;
915
916 if (!dbus_message_get_args(message, &error,
917 DBUS_TYPE_STRING, &cgroup,
918 DBUS_TYPE_INVALID))
919 log_error("Failed to parse Released message: %s", bus_error_message(&error));
920 else
921 manager_cgroup_notify_empty(m, cgroup);
922 }
923
924 dbus_error_free(&error);
925
926 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
927}
928
9418f147
LP
929int manager_send_changed(Manager *manager, const char *properties) {
930 DBusMessage *m;
931 int r = -ENOMEM;
932
933 assert(manager);
934
935 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
936 if (!m)
937 goto finish;
938
939 if (!dbus_connection_send(manager->bus, m, NULL))
940 goto finish;
941
942 r = 0;
943
944finish:
945 if (m)
946 dbus_message_unref(m);
947
948 return r;
949}