]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/logind-dbus.c
logind: call udev_device_get_is_initialized() to trigger lazy loading, as a temporary...
[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>
7f7bb946 25#include <pwd.h>
a185c5aa 26
3f49d45a
LP
27#include "logind.h"
28#include "dbus-common.h"
98a28fef 29#include "strv.h"
7f7bb946 30#include "polkit.h"
3f49d45a
LP
31
32#define BUS_MANAGER_INTERFACE \
33 " <interface name=\"org.freedesktop.login1.Manager\">\n" \
bef422ae 34 " <method name=\"GetSession\">\n" \
3f49d45a 35 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
bef422ae 36 " <arg name=\"session\" type=\"o\" direction=\"out\"/>\n" \
3f49d45a
LP
37 " </method>\n" \
38 " <method name=\"GetUser\">\n" \
bef422ae 39 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
3f49d45a
LP
40 " <arg name=\"user\" type=\"o\" direction=\"out\"/>\n" \
41 " </method>\n" \
bef422ae 42 " <method name=\"GetSeat\">\n" \
3f49d45a 43 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
bef422ae 44 " <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n" \
3f49d45a 45 " </method>\n" \
bef422ae 46 " <method name=\"ListSessions\">\n" \
e1c9c2d5 47 " <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
3f49d45a
LP
48 " </method>\n" \
49 " <method name=\"ListUsers\">\n" \
50 " <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n" \
51 " </method>\n" \
bef422ae
LP
52 " <method name=\"ListSeats\">\n" \
53 " <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n" \
3f49d45a
LP
54 " </method>\n" \
55 " <method name=\"CreateSession\">\n" \
56 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
57 " <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n" \
4d6d6518 58 " <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n" \
3f49d45a
LP
59 " <arg name=\"type\" type=\"s\" direction=\"in\"/>\n" \
60 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
4d6d6518 61 " <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n" \
3f49d45a
LP
62 " <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n" \
63 " <arg name=\"display\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n" \
65 " <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
68 " <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
98a28fef 69 " <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
3f49d45a
LP
70 " <arg name=\"id\" type=\"s\" direction=\"out\"/>\n" \
71 " <arg name=\"path\" type=\"o\" direction=\"out\"/>\n" \
98a28fef 72 " <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
3f49d45a
LP
73 " <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n" \
74 " </method>\n" \
a185c5aa
LP
75 " <method name=\"ActivateSession\">\n" \
76 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
77 " </method>\n" \
3f49d45a
LP
78 " <method name=\"TerminateSession\">\n" \
79 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
80 " </method>\n" \
81 " <method name=\"TerminateUser\">\n" \
bef422ae 82 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
3f49d45a
LP
83 " </method>\n" \
84 " <method name=\"TerminateSeat\">\n" \
85 " <arg name=\"id\" type=\"s\" direction=\"in\"/>\n" \
86 " </method>\n" \
7f7bb946
LP
87 " <method name=\"SetUserLinger\">\n" \
88 " <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n" \
89 " <arg name=\"b\" type=\"b\" direction=\"in\"/>\n" \
90 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
91 " </method>\n" \
47a26690
LP
92 " <method name=\"AttachDevice\">\n" \
93 " <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n" \
94 " <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n" \
95 " <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n" \
96 " </method>\n" \
3f49d45a
LP
97 " <signal name=\"SessionNew\">\n" \
98 " <arg name=\"id\" type=\"s\"/>\n" \
99 " <arg name=\"path\" type=\"o\"/>\n" \
100 " </signal>\n" \
101 " <signal name=\"SessionRemoved\">\n" \
102 " <arg name=\"id\" type=\"s\"/>\n" \
103 " <arg name=\"path\" type=\"o\"/>\n" \
104 " </signal>\n" \
105 " <signal name=\"UserNew\">\n" \
106 " <arg name=\"uid\" type=\"u\"/>\n" \
107 " <arg name=\"path\" type=\"o\"/>\n" \
108 " </signal>\n" \
109 " <signal name=\"UserRemoved\">\n" \
110 " <arg name=\"uid\" type=\"u\"/>\n" \
111 " <arg name=\"path\" type=\"o\"/>\n" \
112 " </signal>\n" \
113 " <signal name=\"SeatNew\">\n" \
114 " <arg name=\"id\" type=\"s\"/>\n" \
115 " <arg name=\"path\" type=\"o\"/>\n" \
116 " </signal>\n" \
117 " <signal name=\"SeatRemoved\">\n" \
118 " <arg name=\"id\" type=\"s\"/>\n" \
119 " <arg name=\"path\" type=\"o\"/>\n" \
120 " </signal>\n" \
121 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
122 " <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
123 " <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
124 " <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
125 " <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
126 " <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
127 " <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
a185c5aa
LP
128 " <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n" \
129 " <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
130 " <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
3f49d45a
LP
131 " </interface>\n"
132
133#define INTROSPECTION_BEGIN \
134 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
135 "<node>\n" \
136 BUS_MANAGER_INTERFACE \
137 BUS_PROPERTIES_INTERFACE \
138 BUS_PEER_INTERFACE \
139 BUS_INTROSPECTABLE_INTERFACE
140
141#define INTROSPECTION_END \
142 "</node>\n"
143
144#define INTERFACES_LIST \
145 BUS_GENERIC_INTERFACES_LIST \
146 "org.freedesktop.login1.Manager\0"
147
a185c5aa
LP
148static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
149 Manager *m = data;
77527da0 150 dbus_bool_t b;
a185c5aa
LP
151
152 assert(i);
153 assert(property);
154 assert(m);
155
77527da0 156 b = manager_get_idle_hint(m, NULL) > 0;
a185c5aa
LP
157 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
158 return -ENOMEM;
159
160 return 0;
161}
162
163static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
164 Manager *m = data;
165 dual_timestamp t;
166 uint64_t u;
167
168 assert(i);
169 assert(property);
170 assert(m);
171
172 manager_get_idle_hint(m, &t);
173 u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
174
175 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
176 return -ENOMEM;
177
178 return 0;
179}
180
98a28fef
LP
181static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
182 Session *session = NULL;
183 User *user = NULL;
184 const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
185 uint32_t uid, leader, audit_id = 0;
186 dbus_bool_t remote, kill_processes;
187 char **controllers = NULL, **reset_controllers = NULL;
188 SessionType t;
189 Seat *s;
190 DBusMessageIter iter;
191 int r;
07714753 192 char *id = NULL, *p;
4d6d6518 193 uint32_t vtnr = 0;
98a28fef
LP
194 int pipe_fds[2] = { -1, -1 };
195 DBusMessage *reply = NULL;
196 bool b;
197
198 assert(m);
199 assert(message);
200 assert(_reply);
201
202 if (!dbus_message_iter_init(message, &iter) ||
203 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
204 return -EINVAL;
205
206 dbus_message_iter_get_basic(&iter, &uid);
207
208 if (!dbus_message_iter_next(&iter) ||
209 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
210 return -EINVAL;
211
212 dbus_message_iter_get_basic(&iter, &leader);
213
214 if (leader <= 0 ||
215 !dbus_message_iter_next(&iter) ||
216 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
217 return -EINVAL;
218
219 dbus_message_iter_get_basic(&iter, &service);
220
221 if (!dbus_message_iter_next(&iter) ||
222 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
223 return -EINVAL;
224
225 dbus_message_iter_get_basic(&iter, &type);
226 t = session_type_from_string(type);
227
228 if (t < 0 ||
229 !dbus_message_iter_next(&iter) ||
230 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
231 return -EINVAL;
232
233 dbus_message_iter_get_basic(&iter, &seat);
234
235 if (isempty(seat))
236 s = NULL;
237 else {
238 s = hashmap_get(m->seats, seat);
239 if (!s)
240 return -ENOENT;
241 }
242
4d6d6518
LP
243 if (!dbus_message_iter_next(&iter) ||
244 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
245 return -EINVAL;
246
247 dbus_message_iter_get_basic(&iter, &vtnr);
248
98a28fef
LP
249 if (!dbus_message_iter_next(&iter) ||
250 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
251 return -EINVAL;
252
253 dbus_message_iter_get_basic(&iter, &tty);
254
255 if (tty_is_vc(tty)) {
4d6d6518 256 int v;
98a28fef
LP
257
258 if (!s)
259 s = m->vtconsole;
260 else if (s != m->vtconsole)
261 return -EINVAL;
262
4d6d6518
LP
263 v = vtnr_from_tty(tty);
264
265 if (v <= 0)
266 return v < 0 ? v : -EINVAL;
98a28fef
LP
267
268 if (vtnr <= 0)
4d6d6518
LP
269 vtnr = (uint32_t) v;
270 else if (vtnr != (uint32_t) v)
271 return -EINVAL;
98a28fef 272
bf100920 273 } else if (!isempty(tty) && s && seat_is_vtconsole(s))
98a28fef
LP
274 return -EINVAL;
275
4d6d6518
LP
276 if (s) {
277 if (seat_is_vtconsole(s)) {
278 if (vtnr <= 0 || vtnr > 63)
279 return -EINVAL;
280 } else {
281 if (vtnr > 0)
282 return -EINVAL;
283 }
284 }
285
98a28fef
LP
286 if (!dbus_message_iter_next(&iter) ||
287 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
288 return -EINVAL;
289
290 dbus_message_iter_get_basic(&iter, &display);
291
292 if (!dbus_message_iter_next(&iter) ||
293 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
294 return -EINVAL;
295
296 dbus_message_iter_get_basic(&iter, &remote);
297
298 if (!dbus_message_iter_next(&iter) ||
299 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
300 return -EINVAL;
301
302 dbus_message_iter_get_basic(&iter, &remote_user);
303
304 if (!dbus_message_iter_next(&iter) ||
305 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
306 return -EINVAL;
307
308 dbus_message_iter_get_basic(&iter, &remote_host);
309
310 if (!dbus_message_iter_next(&iter) ||
311 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
312 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
313 return -EINVAL;
314
315 r = bus_parse_strv_iter(&iter, &controllers);
316 if (r < 0)
317 return -EINVAL;
318
319 if (!dbus_message_iter_next(&iter) ||
320 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
321 dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
322 r = -EINVAL;
323 goto fail;
324 }
325
326 r = bus_parse_strv_iter(&iter, &reset_controllers);
327 if (r < 0)
328 goto fail;
329
330 if (!dbus_message_iter_next(&iter) ||
331 dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
332 r = -EINVAL;
333 goto fail;
334 }
335
336 dbus_message_iter_get_basic(&iter, &kill_processes);
337
338 r = manager_add_user_by_uid(m, uid, &user);
339 if (r < 0)
340 goto fail;
341
342 audit_session_from_pid(leader, &audit_id);
343
07714753 344 if (audit_id > 0) {
98a28fef 345 asprintf(&id, "%lu", (unsigned long) audit_id);
98a28fef 346
07714753
LP
347 if (!id) {
348 r = -ENOMEM;
349 goto fail;
350 }
98a28fef 351
21c390cc
LP
352 session = hashmap_get(m->sessions, id);
353
354 if (session) {
355
356 /* Session already exists, client is probably
357 * something like "su" which changes uid but
358 * is still the same audit session */
359
360 reply = dbus_message_new_method_return(message);
361 if (!reply) {
362 r = -ENOMEM;
363 goto fail;
364 }
365
366 /* Create a throw-away fd */
367 if (pipe(pipe_fds) < 0) {
368 r = -errno;
369 goto fail;
370 }
371
372 close_nointr_nofail(pipe_fds[0]);
373 pipe_fds[0] = -1;
374
375 p = session_bus_path(session);
376 if (!p) {
377 r = -ENOMEM;
378 goto fail;
379 }
380
381 b = dbus_message_append_args(
382 reply,
383 DBUS_TYPE_STRING, &session->id,
384 DBUS_TYPE_OBJECT_PATH, &p,
385 DBUS_TYPE_STRING, &session->user->runtime_path,
386 DBUS_TYPE_UNIX_FD, &pipe_fds[1],
387 DBUS_TYPE_INVALID);
388 free(p);
389
390 if (!b) {
391 r = -ENOMEM;
392 goto fail;
393 }
394
395 close_nointr_nofail(pipe_fds[1]);
396 *_reply = reply;
397
398 return 0;
07714753
LP
399 }
400
401 } else {
402 do {
403 free(id);
404 asprintf(&id, "c%lu", ++m->session_counter);
405
406 if (!id) {
407 r = -ENOMEM;
408 goto fail;
409 }
410
411 } while (hashmap_get(m->sessions, id));
98a28fef
LP
412 }
413
414 r = manager_add_session(m, user, id, &session);
415 free(id);
416 if (r < 0)
417 goto fail;
418
419 session->leader = leader;
420 session->audit_id = audit_id;
421 session->type = t;
422 session->remote = remote;
423 session->controllers = controllers;
424 session->reset_controllers = reset_controllers;
425 session->kill_processes = kill_processes;
426 session->vtnr = vtnr;
427
428 controllers = reset_controllers = NULL;
429
430 if (!isempty(tty)) {
431 session->tty = strdup(tty);
432 if (!session->tty) {
433 r = -ENOMEM;
434 goto fail;
435 }
436 }
437
438 if (!isempty(display)) {
439 session->display = strdup(display);
440 if (!session->display) {
441 r = -ENOMEM;
442 goto fail;
443 }
444 }
445
446 if (!isempty(remote_user)) {
447 session->remote_user = strdup(remote_user);
448 if (!session->remote_user) {
449 r = -ENOMEM;
450 goto fail;
451 }
452 }
453
454 if (!isempty(remote_host)) {
455 session->remote_host = strdup(remote_host);
456 if (!session->remote_host) {
457 r = -ENOMEM;
458 goto fail;
459 }
460 }
461
462 if (!isempty(service)) {
463 session->service = strdup(service);
464 if (!session->service) {
465 r = -ENOMEM;
466 goto fail;
467 }
468 }
469
470 if (pipe(pipe_fds) < 0) {
471 r = -errno;
472 goto fail;
473 }
474
31b79c2b
LP
475 r = session_set_pipe_fd(session, pipe_fds[0]);
476 if (r < 0)
477 goto fail;
19bc719e 478 pipe_fds[0] = -1;
98a28fef
LP
479
480 if (s) {
481 r = seat_attach_session(s, session);
482 if (r < 0)
483 goto fail;
484 }
485
486 r = session_start(session);
487 if (r < 0)
488 goto fail;
489
490 reply = dbus_message_new_method_return(message);
491 if (!reply) {
492 r = -ENOMEM;
493 goto fail;
494 }
495
496 p = session_bus_path(session);
497 if (!p) {
498 r = -ENOMEM;
499 goto fail;
500 }
501
502 b = dbus_message_append_args(
503 reply,
504 DBUS_TYPE_STRING, &session->id,
505 DBUS_TYPE_OBJECT_PATH, &p,
506 DBUS_TYPE_STRING, &session->user->runtime_path,
19bc719e 507 DBUS_TYPE_UNIX_FD, &pipe_fds[1],
98a28fef
LP
508 DBUS_TYPE_INVALID);
509 free(p);
510
511 if (!b) {
512 r = -ENOMEM;
513 goto fail;
514 }
515
19bc719e 516 close_nointr_nofail(pipe_fds[1]);
98a28fef
LP
517 *_reply = reply;
518
519 return 0;
520
521fail:
522 strv_free(controllers);
523 strv_free(reset_controllers);
524
525 if (session)
526 session_add_to_gc_queue(session);
527
528 if (user)
529 user_add_to_gc_queue(user);
530
531 close_pipe(pipe_fds);
532
533 if (reply)
534 dbus_message_unref(reply);
535
536 return r;
537}
538
47a26690
LP
539static bool device_has_tag(struct udev_device *d, const char *tag) {
540 struct udev_list_entry *first, *item;
541
542 assert(d);
543 assert(tag);
544
88bb8d21
LP
545 udev_device_get_is_initialized(d);
546
47a26690
LP
547 first = udev_device_get_tags_list_entry(d);
548 udev_list_entry_foreach(item, first)
549 if (streq(udev_list_entry_get_name(item), tag))
550 return true;
551
552 return false;
553}
554
555static int attach_device(Manager *m, const char *seat, const char *sysfs) {
556 struct udev_device *d;
557 char *rule = NULL, *file = NULL;
c28fa3d3 558 const char *id_for_seat;
47a26690
LP
559 int r;
560
561 assert(m);
562 assert(seat);
563 assert(sysfs);
564
565 d = udev_device_new_from_syspath(m->udev, sysfs);
566 if (!d)
567 return -ENODEV;
568
569 if (!device_has_tag(d, "seat")) {
570 r = -ENODEV;
571 goto finish;
572 }
573
c28fa3d3
LP
574 id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
575 if (!id_for_seat) {
47a26690
LP
576 r = -ENODEV;
577 goto finish;
578 }
579
c28fa3d3 580 if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
47a26690
LP
581 r = -ENOMEM;
582 goto finish;
583 }
584
c28fa3d3 585 if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
47a26690
LP
586 r = -ENOMEM;
587 goto finish;
588 }
589
c28fa3d3 590 mkdir_p("/etc/udev/rules.d", 0755);
47a26690
LP
591 r = write_one_line_file(file, rule);
592
593finish:
594 free(rule);
595 free(file);
596
597 if (d)
598 udev_device_unref(d);
599
600 return r;
601}
602
3f49d45a
LP
603static DBusHandlerResult manager_message_handler(
604 DBusConnection *connection,
605 DBusMessage *message,
606 void *userdata) {
607
608 Manager *m = userdata;
609
610 const BusProperty properties[] = {
a185c5aa
LP
611 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_path },
612 { "org.freedesktop.login1.Manager", "Controllers", bus_property_append_strv, "as", m->controllers },
a185c5aa
LP
613 { "org.freedesktop.login1.Manager", "NAutoVTs", bus_property_append_unsigned, "u", &m->n_autovts },
614 { "org.freedesktop.login1.Manager", "KillOnlyUsers", bus_property_append_strv, "as", m->kill_only_users },
615 { "org.freedesktop.login1.Manager", "KillExcludeUsers", bus_property_append_strv, "as", m->kill_exclude_users },
616 { "org.freedesktop.login1.Manager", "KillUserProcesses", bus_property_append_bool, "b", &m->kill_user_processes },
617 { "org.freedesktop.login1.Manager", "IdleHint", bus_manager_append_idle_hint, "b", m },
618 { "org.freedesktop.login1.Manager", "IdleSinceHint", bus_manager_append_idle_hint_since, "t", m },
619 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m },
3f49d45a
LP
620 { NULL, NULL, NULL, NULL, NULL }
621 };
622
623 DBusError error;
624 DBusMessage *reply = NULL;
bef422ae 625 int r;
3f49d45a
LP
626
627 assert(connection);
628 assert(message);
629 assert(m);
630
631 dbus_error_init(&error);
632
bef422ae
LP
633 if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
634 const char *name;
635 char *p;
636 Session *session;
637 bool b;
638
639 if (!dbus_message_get_args(
640 message,
641 &error,
642 DBUS_TYPE_STRING, &name,
643 DBUS_TYPE_INVALID))
644 return bus_send_error_reply(connection, message, &error, -EINVAL);
645
646 session = hashmap_get(m->sessions, name);
647 if (!session)
648 return bus_send_error_reply(connection, message, &error, -ENOENT);
649
650 reply = dbus_message_new_method_return(message);
651 if (!reply)
652 goto oom;
653
654 p = session_bus_path(session);
655 if (!p)
656 goto oom;
657
658 b = dbus_message_append_args(
659 reply,
660 DBUS_TYPE_OBJECT_PATH, &p,
661 DBUS_TYPE_INVALID);
662 free(p);
663
664 if (!b)
665 goto oom;
666
667 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
668 uint32_t uid;
669 char *p;
670 User *user;
671 bool b;
672
673 if (!dbus_message_get_args(
674 message,
675 &error,
676 DBUS_TYPE_UINT32, &uid,
677 DBUS_TYPE_INVALID))
678 return bus_send_error_reply(connection, message, &error, -EINVAL);
679
680 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
681 if (!user)
682 return bus_send_error_reply(connection, message, &error, -ENOENT);
683
684 reply = dbus_message_new_method_return(message);
685 if (!reply)
686 goto oom;
687
688 p = user_bus_path(user);
689 if (!p)
690 goto oom;
691
692 b = dbus_message_append_args(
693 reply,
694 DBUS_TYPE_OBJECT_PATH, &p,
695 DBUS_TYPE_INVALID);
696 free(p);
697
698 if (!b)
699 goto oom;
700
701 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
702 const char *name;
703 char *p;
704 Seat *seat;
705 bool b;
706
707 if (!dbus_message_get_args(
708 message,
709 &error,
710 DBUS_TYPE_STRING, &name,
711 DBUS_TYPE_INVALID))
712 return bus_send_error_reply(connection, message, &error, -EINVAL);
713
714 seat = hashmap_get(m->seats, name);
715 if (!seat)
716 return bus_send_error_reply(connection, message, &error, -ENOENT);
717
718 reply = dbus_message_new_method_return(message);
719 if (!reply)
720 goto oom;
721
722 p = seat_bus_path(seat);
723 if (!p)
724 goto oom;
725
726 b = dbus_message_append_args(
727 reply,
728 DBUS_TYPE_OBJECT_PATH, &p,
729 DBUS_TYPE_INVALID);
730 free(p);
731
732 if (!b)
733 goto oom;
734
e1c9c2d5
LP
735 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
736 char *p;
737 Session *session;
738 Iterator i;
739 DBusMessageIter iter, sub;
740 const char *empty = "";
741
742 reply = dbus_message_new_method_return(message);
743 if (!reply)
744 goto oom;
745
746 dbus_message_iter_init_append(reply, &iter);
747
dec15e92 748 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
e1c9c2d5
LP
749 goto oom;
750
751 HASHMAP_FOREACH(session, m->sessions, i) {
752 DBusMessageIter sub2;
753 uint32_t uid;
754
755 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
756 goto oom;
757
758 uid = session->user->uid;
759
760 p = session_bus_path(session);
761 if (!p)
762 goto oom;
763
764 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
765 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
766 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
767 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
768 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
769 free(p);
770 goto oom;
771 }
772
773 free(p);
774
775 if (!dbus_message_iter_close_container(&sub, &sub2))
776 goto oom;
777 }
778
779 if (!dbus_message_iter_close_container(&iter, &sub))
780 goto oom;
781
782 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
783 char *p;
784 User *user;
785 Iterator i;
786 DBusMessageIter iter, sub;
787
788 reply = dbus_message_new_method_return(message);
789 if (!reply)
790 goto oom;
791
792 dbus_message_iter_init_append(reply, &iter);
793
dec15e92 794 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
e1c9c2d5
LP
795 goto oom;
796
797 HASHMAP_FOREACH(user, m->users, i) {
798 DBusMessageIter sub2;
799 uint32_t uid;
800
801 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
802 goto oom;
803
804 uid = user->uid;
805
806 p = user_bus_path(user);
807 if (!p)
808 goto oom;
809
810 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
811 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
812 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
813 free(p);
814 goto oom;
815 }
816
817 free(p);
818
819 if (!dbus_message_iter_close_container(&sub, &sub2))
820 goto oom;
821 }
822
823 if (!dbus_message_iter_close_container(&iter, &sub))
824 goto oom;
825
826 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
827 char *p;
828 Seat *seat;
829 Iterator i;
830 DBusMessageIter iter, sub;
831
832 reply = dbus_message_new_method_return(message);
833 if (!reply)
834 goto oom;
835
836 dbus_message_iter_init_append(reply, &iter);
837
dec15e92 838 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
e1c9c2d5
LP
839 goto oom;
840
841 HASHMAP_FOREACH(seat, m->seats, i) {
842 DBusMessageIter sub2;
843
844 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
845 goto oom;
846
847 p = seat_bus_path(seat);
848 if (!p)
849 goto oom;
850
851 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
852 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
853 free(p);
854 goto oom;
855 }
856
857 free(p);
858
859 if (!dbus_message_iter_close_container(&sub, &sub2))
860 goto oom;
861 }
862
863 if (!dbus_message_iter_close_container(&iter, &sub))
864 goto oom;
865
98a28fef
LP
866 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
867
868 r = bus_manager_create_session(m, message, &reply);
869 if (r == -ENOMEM)
870 goto oom;
871
872 if (r < 0)
873 return bus_send_error_reply(connection, message, &error, r);
874
bef422ae
LP
875 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
876 const char *name;
877 Session *session;
878
879 if (!dbus_message_get_args(
880 message,
881 &error,
882 DBUS_TYPE_STRING, &name,
883 DBUS_TYPE_INVALID))
884 return bus_send_error_reply(connection, message, &error, -EINVAL);
885
886 session = hashmap_get(m->sessions, name);
887 if (!session)
888 return bus_send_error_reply(connection, message, &error, -ENOENT);
889
890 r = session_activate(session);
891 if (r < 0)
892 return bus_send_error_reply(connection, message, NULL, r);
893
894 reply = dbus_message_new_method_return(message);
895 if (!reply)
896 goto oom;
897
898 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
899 const char *name;
900 Session *session;
901
902 if (!dbus_message_get_args(
903 message,
904 &error,
905 DBUS_TYPE_STRING, &name,
906 DBUS_TYPE_INVALID))
907 return bus_send_error_reply(connection, message, &error, -EINVAL);
908
909 session = hashmap_get(m->sessions, name);
910 if (!session)
911 return bus_send_error_reply(connection, message, &error, -ENOENT);
912
913 r = session_stop(session);
914 if (r < 0)
915 return bus_send_error_reply(connection, message, NULL, r);
916
917 reply = dbus_message_new_method_return(message);
918 if (!reply)
919 goto oom;
920
921 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
922 uint32_t uid;
923 User *user;
924
925 if (!dbus_message_get_args(
926 message,
927 &error,
928 DBUS_TYPE_UINT32, &uid,
929 DBUS_TYPE_INVALID))
930 return bus_send_error_reply(connection, message, &error, -EINVAL);
931
932 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
933 if (!user)
934 return bus_send_error_reply(connection, message, &error, -ENOENT);
935
936 r = user_stop(user);
937 if (r < 0)
938 return bus_send_error_reply(connection, message, NULL, r);
939
940 reply = dbus_message_new_method_return(message);
941 if (!reply)
942 goto oom;
943
944 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
945 const char *name;
946 Seat *seat;
947
948 if (!dbus_message_get_args(
949 message,
950 &error,
951 DBUS_TYPE_STRING, &name,
952 DBUS_TYPE_INVALID))
953 return bus_send_error_reply(connection, message, &error, -EINVAL);
954
955 seat = hashmap_get(m->seats, name);
956 if (!seat)
957 return bus_send_error_reply(connection, message, &error, -ENOENT);
958
959 r = seat_stop_sessions(seat);
960 if (r < 0)
961 return bus_send_error_reply(connection, message, NULL, r);
962
963 reply = dbus_message_new_method_return(message);
964 if (!reply)
965 goto oom;
966
7f7bb946
LP
967 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
968 uint32_t uid;
969 struct passwd *pw;
970 dbus_bool_t b, interactive;
971 char *path;
972
973 if (!dbus_message_get_args(
974 message,
975 &error,
976 DBUS_TYPE_UINT32, &uid,
977 DBUS_TYPE_BOOLEAN, &b,
978 DBUS_TYPE_BOOLEAN, &interactive,
979 DBUS_TYPE_INVALID))
980 return bus_send_error_reply(connection, message, &error, -EINVAL);
981
982 errno = 0;
983 pw = getpwuid(uid);
984 if (!pw)
985 return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
986
987 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
988 if (r < 0)
989 return bus_send_error_reply(connection, message, &error, r);
990
991 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
992 if (r < 0)
993 return bus_send_error_reply(connection, message, &error, r);
994
995 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
996 if (!path)
997 goto oom;
998
999 if (b) {
38f3fc7d
LP
1000 User *u;
1001
7f7bb946
LP
1002 r = touch(path);
1003 free(path);
1004
1005 if (r < 0)
1006 return bus_send_error_reply(connection, message, &error, r);
38f3fc7d
LP
1007
1008 if (manager_add_user_by_uid(m, uid, &u) >= 0)
1009 user_start(u);
1010
7f7bb946 1011 } else {
38f3fc7d
LP
1012 User *u;
1013
7f7bb946
LP
1014 r = unlink(path);
1015 free(path);
1016
1017 if (r < 0 && errno != ENOENT)
1018 return bus_send_error_reply(connection, message, &error, -errno);
38f3fc7d
LP
1019
1020 u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1021 if (u)
1022 user_add_to_gc_queue(u);
7f7bb946
LP
1023 }
1024
1025 reply = dbus_message_new_method_return(message);
1026 if (!reply)
1027 goto oom;
1028
47a26690
LP
1029 } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1030 const char *sysfs, *seat;
1031 dbus_bool_t interactive;
1032
1033 if (!dbus_message_get_args(
1034 message,
1035 &error,
1036 DBUS_TYPE_STRING, &seat,
1037 DBUS_TYPE_STRING, &sysfs,
1038 DBUS_TYPE_BOOLEAN, &interactive,
1039 DBUS_TYPE_INVALID))
1040 return bus_send_error_reply(connection, message, &error, -EINVAL);
1041
1042 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1043 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1044
1045 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1046 if (r < 0)
1047 return bus_send_error_reply(connection, message, &error, r);
1048
1049 r = attach_device(m, seat, sysfs);
1050 if (r < 0)
1051 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1052
1053 reply = dbus_message_new_method_return(message);
1054 if (!reply)
1055 goto oom;
7f7bb946 1056
bef422ae 1057 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
3f49d45a
LP
1058 char *introspection = NULL;
1059 FILE *f;
1060 Iterator i;
1061 Session *session;
1062 Seat *seat;
1063 User *user;
1064 size_t size;
1065 char *p;
1066
1067 if (!(reply = dbus_message_new_method_return(message)))
1068 goto oom;
1069
1070 /* We roll our own introspection code here, instead of
1071 * relying on bus_default_message_handler() because we
1072 * need to generate our introspection string
1073 * dynamically. */
1074
1075 if (!(f = open_memstream(&introspection, &size)))
1076 goto oom;
1077
1078 fputs(INTROSPECTION_BEGIN, f);
1079
1080 HASHMAP_FOREACH(seat, m->seats, i) {
1081 p = bus_path_escape(seat->id);
1082
1083 if (p) {
1084 fprintf(f, "<node name=\"seat/%s\"/>", p);
1085 free(p);
1086 }
1087 }
1088
1089 HASHMAP_FOREACH(user, m->users, i)
1090 fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1091
1092 HASHMAP_FOREACH(session, m->sessions, i) {
1093 p = bus_path_escape(session->id);
1094
1095 if (p) {
1096 fprintf(f, "<node name=\"session/%s\"/>", p);
1097 free(p);
1098 }
1099 }
1100
1101 fputs(INTROSPECTION_END, f);
1102
1103 if (ferror(f)) {
1104 fclose(f);
1105 free(introspection);
1106 goto oom;
1107 }
1108
1109 fclose(f);
1110
1111 if (!introspection)
1112 goto oom;
1113
1114 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1115 free(introspection);
1116 goto oom;
1117 }
1118
1119 free(introspection);
1120 } else
1121 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1122
1123 if (reply) {
1124 if (!dbus_connection_send(connection, reply, NULL))
1125 goto oom;
1126
1127 dbus_message_unref(reply);
1128 }
1129
1130 return DBUS_HANDLER_RESULT_HANDLED;
1131
1132oom:
1133 if (reply)
1134 dbus_message_unref(reply);
1135
1136 dbus_error_free(&error);
1137
1138 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1139}
1140
1141const DBusObjectPathVTable bus_manager_vtable = {
1142 .message_function = manager_message_handler
1143};
9418f147 1144
1713813d
LP
1145DBusHandlerResult bus_message_filter(
1146 DBusConnection *connection,
1147 DBusMessage *message,
1148 void *userdata) {
1149
1150 Manager *m = userdata;
1151 DBusError error;
1152
1153 assert(m);
1154 assert(connection);
1155 assert(message);
1156
1157 dbus_error_init(&error);
1158
1159 if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1160 const char *cgroup;
1161
1162 if (!dbus_message_get_args(message, &error,
1163 DBUS_TYPE_STRING, &cgroup,
1164 DBUS_TYPE_INVALID))
1165 log_error("Failed to parse Released message: %s", bus_error_message(&error));
1166 else
1167 manager_cgroup_notify_empty(m, cgroup);
1168 }
1169
1170 dbus_error_free(&error);
1171
1172 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1173}
1174
9418f147
LP
1175int manager_send_changed(Manager *manager, const char *properties) {
1176 DBusMessage *m;
1177 int r = -ENOMEM;
1178
1179 assert(manager);
1180
1181 m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1182 if (!m)
1183 goto finish;
1184
1185 if (!dbus_connection_send(manager->bus, m, NULL))
1186 goto finish;
1187
1188 r = 0;
1189
1190finish:
1191 if (m)
1192 dbus_message_unref(m);
1193
1194 return r;
1195}