chiark / gitweb /
shutdown: initialize the controlling terminal
[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 #include <pwd.h>
26
27 #include "logind.h"
28 #include "dbus-common.h"
29 #include "strv.h"
30 #include "polkit.h"
31 #include "special.h"
32
33 #define BUS_MANAGER_INTERFACE                                           \
34         " <interface name=\"org.freedesktop.login1.Manager\">\n"        \
35         "  <method name=\"GetSession\">\n"                              \
36         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
37         "   <arg name=\"session\" type=\"o\" direction=\"out\"/>\n"     \
38         "  </method>\n"                                                 \
39         "  <method name=\"GetUser\">\n"                                 \
40         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
41         "   <arg name=\"user\" type=\"o\" direction=\"out\"/>\n"        \
42         "  </method>\n"                                                 \
43         "  <method name=\"GetSeat\">\n"                                 \
44         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
45         "   <arg name=\"seat\" type=\"o\" direction=\"out\"/>\n"        \
46         "  </method>\n"                                                 \
47         "  <method name=\"ListSessions\">\n"                            \
48         "   <arg name=\"sessions\" type=\"a(susso)\" direction=\"out\"/>\n" \
49         "  </method>\n"                                                 \
50         "  <method name=\"ListUsers\">\n"                               \
51         "   <arg name=\"users\" type=\"a(uso)\" direction=\"out\"/>\n"  \
52         "  </method>\n"                                                 \
53         "  <method name=\"ListSeats\">\n"                               \
54         "   <arg name=\"seats\" type=\"a(so)\" direction=\"out\"/>\n"   \
55         "  </method>\n"                                                 \
56         "  <method name=\"CreateSession\">\n"                           \
57         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
58         "   <arg name=\"leader\" type=\"u\" direction=\"in\"/>\n"       \
59         "   <arg name=\"sevice\" type=\"s\" direction=\"in\"/>\n"       \
60         "   <arg name=\"type\" type=\"s\" direction=\"in\"/>\n"         \
61         "   <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n"         \
62         "   <arg name=\"vtnr\" type=\"u\" direction=\"in\"/>\n"         \
63         "   <arg name=\"tty\" type=\"s\" direction=\"in\"/>\n"          \
64         "   <arg name=\"display\" type=\"s\" direction=\"in\"/>\n"      \
65         "   <arg name=\"remote\" type=\"b\" direction=\"in\"/>\n"       \
66         "   <arg name=\"remote_user\" type=\"s\" direction=\"in\"/>\n"  \
67         "   <arg name=\"remote_host\" type=\"s\" direction=\"in\"/>\n"  \
68         "   <arg name=\"controllers\" type=\"as\" direction=\"in\"/>\n" \
69         "   <arg name=\"reset_controllers\" type=\"as\" direction=\"in\"/>\n" \
70         "   <arg name=\"kill_processes\" type=\"b\" direction=\"in\"/>\n" \
71         "   <arg name=\"id\" type=\"s\" direction=\"out\"/>\n"          \
72         "   <arg name=\"path\" type=\"o\" direction=\"out\"/>\n"        \
73         "   <arg name=\"runtime_path\" type=\"o\" direction=\"out\"/>\n" \
74         "   <arg name=\"fd\" type=\"h\" direction=\"out\"/>\n"          \
75         "  </method>\n"                                                 \
76         "  <method name=\"ActivateSession\">\n"                         \
77         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
78         "  </method>\n"                                                 \
79         "  <method name=\"LockSession\">\n"                             \
80         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
81         "  </method>\n"                                                 \
82         "  <method name=\"UnlockSession\">\n"                           \
83         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
84         "  </method>\n"                                                 \
85         "  <method name=\"KillSession\">\n"                             \
86         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
87         "   <arg name=\"who\" type=\"s\"/>\n"                           \
88         "   <arg name=\"signal\" type=\"s\"/>\n"                        \
89         "  </method>\n"                                                 \
90         "  <method name=\"KillUser\">\n"                                \
91         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
92         "   <arg name=\"signal\" type=\"s\"/>\n"                        \
93         "  </method>\n"                                                 \
94         "  <method name=\"TerminateSession\">\n"                        \
95         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
96         "  </method>\n"                                                 \
97         "  <method name=\"TerminateUser\">\n"                           \
98         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
99         "  </method>\n"                                                 \
100         "  <method name=\"TerminateSeat\">\n"                           \
101         "   <arg name=\"id\" type=\"s\" direction=\"in\"/>\n"           \
102         "  </method>\n"                                                 \
103         "  <method name=\"SetUserLinger\">\n"                           \
104         "   <arg name=\"uid\" type=\"u\" direction=\"in\"/>\n"          \
105         "   <arg name=\"b\" type=\"b\" direction=\"in\"/>\n"            \
106         "   <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n"  \
107         "  </method>\n"                                                 \
108         "  <method name=\"AttachDevice\">\n"                            \
109         "   <arg name=\"seat\" type=\"s\" direction=\"in\"/>\n"         \
110         "   <arg name=\"sysfs\" type=\"s\" direction=\"in\"/>\n"        \
111         "   <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n"  \
112         "  </method>\n"                                                 \
113         "  <method name=\"FlushDevices\">\n"                            \
114         "   <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n"  \
115         "  </method>\n"                                                 \
116         "  <method name=\"PowerOff\">\n"                                \
117         "   <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n"  \
118         "  </method>\n"                                                 \
119         "  <method name=\"Reboot\">\n"                                  \
120         "   <arg name=\"interactive\" type=\"b\" direction=\"in\"/>\n"  \
121         "  </method>\n"                                                 \
122         "  <signal name=\"SessionNew\">\n"                              \
123         "   <arg name=\"id\" type=\"s\"/>\n"                            \
124         "   <arg name=\"path\" type=\"o\"/>\n"                          \
125         "  </signal>\n"                                                 \
126         "  <signal name=\"SessionRemoved\">\n"                          \
127         "   <arg name=\"id\" type=\"s\"/>\n"                            \
128         "   <arg name=\"path\" type=\"o\"/>\n"                          \
129         "  </signal>\n"                                                 \
130         "  <signal name=\"UserNew\">\n"                                 \
131         "   <arg name=\"uid\" type=\"u\"/>\n"                           \
132         "   <arg name=\"path\" type=\"o\"/>\n"                          \
133         "  </signal>\n"                                                 \
134         "  <signal name=\"UserRemoved\">\n"                             \
135         "   <arg name=\"uid\" type=\"u\"/>\n"                           \
136         "   <arg name=\"path\" type=\"o\"/>\n"                          \
137         "  </signal>\n"                                                 \
138         "  <signal name=\"SeatNew\">\n"                                 \
139         "   <arg name=\"id\" type=\"s\"/>\n"                            \
140         "   <arg name=\"path\" type=\"o\"/>\n"                          \
141         "  </signal>\n"                                                 \
142         "  <signal name=\"SeatRemoved\">\n"                             \
143         "   <arg name=\"id\" type=\"s\"/>\n"                            \
144         "   <arg name=\"path\" type=\"o\"/>\n"                          \
145         "  </signal>\n"                                                 \
146         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
147         "  <property name=\"Controllers\" type=\"as\" access=\"read\"/>\n" \
148         "  <property name=\"ResetControllers\" type=\"as\" access=\"read\"/>\n" \
149         "  <property name=\"NAutoVTs\" type=\"u\" access=\"read\"/>\n" \
150         "  <property name=\"KillOnlyUsers\" type=\"as\" access=\"read\"/>\n" \
151         "  <property name=\"KillExcludeUsers\" type=\"as\" access=\"read\"/>\n" \
152         "  <property name=\"KillUserProcesses\" type=\"b\" access=\"read\"/>\n" \
153         "  <property name=\"IdleHint\" type=\"b\" access=\"read\"/>\n"  \
154         "  <property name=\"IdleSinceHint\" type=\"t\" access=\"read\"/>\n" \
155         "  <property name=\"IdleSinceHintMonotonic\" type=\"t\" access=\"read\"/>\n" \
156         " </interface>\n"
157
158 #define INTROSPECTION_BEGIN                                             \
159         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
160         "<node>\n"                                                      \
161         BUS_MANAGER_INTERFACE                                           \
162         BUS_PROPERTIES_INTERFACE                                        \
163         BUS_PEER_INTERFACE                                              \
164         BUS_INTROSPECTABLE_INTERFACE
165
166 #define INTROSPECTION_END                                               \
167         "</node>\n"
168
169 #define INTERFACES_LIST                              \
170         BUS_GENERIC_INTERFACES_LIST                  \
171         "org.freedesktop.login1.Manager\0"
172
173 static int bus_manager_append_idle_hint(DBusMessageIter *i, const char *property, void *data) {
174         Manager *m = data;
175         dbus_bool_t b;
176
177         assert(i);
178         assert(property);
179         assert(m);
180
181         b = manager_get_idle_hint(m, NULL) > 0;
182         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
183                 return -ENOMEM;
184
185         return 0;
186 }
187
188 static int bus_manager_append_idle_hint_since(DBusMessageIter *i, const char *property, void *data) {
189         Manager *m = data;
190         dual_timestamp t;
191         uint64_t u;
192
193         assert(i);
194         assert(property);
195         assert(m);
196
197         manager_get_idle_hint(m, &t);
198         u = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic;
199
200         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
201                 return -ENOMEM;
202
203         return 0;
204 }
205
206 static int bus_manager_create_session(Manager *m, DBusMessage *message, DBusMessage **_reply) {
207         Session *session = NULL;
208         User *user = NULL;
209         const char *type, *seat, *tty, *display, *remote_user, *remote_host, *service;
210         uint32_t uid, leader, audit_id = 0;
211         dbus_bool_t remote, kill_processes;
212         char **controllers = NULL, **reset_controllers = NULL;
213         SessionType t;
214         Seat *s;
215         DBusMessageIter iter;
216         int r;
217         char *id = NULL, *p;
218         uint32_t vtnr = 0;
219         int fifo_fd = -1;
220         DBusMessage *reply = NULL;
221         bool b;
222
223         assert(m);
224         assert(message);
225         assert(_reply);
226
227         if (!dbus_message_iter_init(message, &iter) ||
228             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
229                 return -EINVAL;
230
231         dbus_message_iter_get_basic(&iter, &uid);
232
233         if (!dbus_message_iter_next(&iter) ||
234             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
235                 return -EINVAL;
236
237         dbus_message_iter_get_basic(&iter, &leader);
238
239         if (leader <= 0 ||
240             !dbus_message_iter_next(&iter) ||
241             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
242                 return -EINVAL;
243
244         dbus_message_iter_get_basic(&iter, &service);
245
246         if (!dbus_message_iter_next(&iter) ||
247             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
248                 return -EINVAL;
249
250         dbus_message_iter_get_basic(&iter, &type);
251         t = session_type_from_string(type);
252
253         if (t < 0 ||
254             !dbus_message_iter_next(&iter) ||
255             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
256                 return -EINVAL;
257
258         dbus_message_iter_get_basic(&iter, &seat);
259
260         if (isempty(seat))
261                 s = NULL;
262         else {
263                 s = hashmap_get(m->seats, seat);
264                 if (!s)
265                         return -ENOENT;
266         }
267
268         if (!dbus_message_iter_next(&iter) ||
269             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32)
270                 return -EINVAL;
271
272         dbus_message_iter_get_basic(&iter, &vtnr);
273
274         if (!dbus_message_iter_next(&iter) ||
275             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
276                 return -EINVAL;
277
278         dbus_message_iter_get_basic(&iter, &tty);
279
280         if (tty_is_vc(tty)) {
281                 int v;
282
283                 if (!s)
284                         s = m->vtconsole;
285                 else if (s != m->vtconsole)
286                         return -EINVAL;
287
288                 v = vtnr_from_tty(tty);
289
290                 if (v <= 0)
291                         return v < 0 ? v : -EINVAL;
292
293                 if (vtnr <= 0)
294                         vtnr = (uint32_t) v;
295                 else if (vtnr != (uint32_t) v)
296                         return -EINVAL;
297
298         } else if (!isempty(tty) && s && seat_is_vtconsole(s))
299                 return -EINVAL;
300
301         if (s) {
302                 if (seat_is_vtconsole(s)) {
303                         if (vtnr <= 0 || vtnr > 63)
304                                 return -EINVAL;
305                 } else {
306                         if (vtnr > 0)
307                                 return -EINVAL;
308                 }
309         }
310
311         if (!dbus_message_iter_next(&iter) ||
312             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
313                 return -EINVAL;
314
315         dbus_message_iter_get_basic(&iter, &display);
316
317         if (!dbus_message_iter_next(&iter) ||
318             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN)
319                 return -EINVAL;
320
321         dbus_message_iter_get_basic(&iter, &remote);
322
323         if (!dbus_message_iter_next(&iter) ||
324             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
325                 return -EINVAL;
326
327         dbus_message_iter_get_basic(&iter, &remote_user);
328
329         if (!dbus_message_iter_next(&iter) ||
330             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
331                 return -EINVAL;
332
333         dbus_message_iter_get_basic(&iter, &remote_host);
334
335         if (!dbus_message_iter_next(&iter) ||
336             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
337             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
338                 return -EINVAL;
339
340         r = bus_parse_strv_iter(&iter, &controllers);
341         if (r < 0)
342                 return -EINVAL;
343
344         if (strv_contains(controllers, "systemd") ||
345             !dbus_message_iter_next(&iter) ||
346             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
347             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) {
348                 r = -EINVAL;
349                 goto fail;
350         }
351
352         r = bus_parse_strv_iter(&iter, &reset_controllers);
353         if (r < 0)
354                 goto fail;
355
356         if (strv_contains(reset_controllers, "systemd") ||
357             !dbus_message_iter_next(&iter) ||
358             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BOOLEAN) {
359                 r = -EINVAL;
360                 goto fail;
361         }
362
363         dbus_message_iter_get_basic(&iter, &kill_processes);
364
365         r = manager_add_user_by_uid(m, uid, &user);
366         if (r < 0)
367                 goto fail;
368
369         audit_session_from_pid(leader, &audit_id);
370
371         if (audit_id > 0) {
372                 asprintf(&id, "%lu", (unsigned long) audit_id);
373
374                 if (!id) {
375                         r = -ENOMEM;
376                         goto fail;
377                 }
378
379                 session = hashmap_get(m->sessions, id);
380
381                 if (session) {
382
383                         fifo_fd = session_create_fifo(session);
384                         if (fifo_fd < 0) {
385                                 r = fifo_fd;
386                                 goto fail;
387                         }
388
389                         /* Session already exists, client is probably
390                          * something like "su" which changes uid but
391                          * is still the same audit session */
392
393                         reply = dbus_message_new_method_return(message);
394                         if (!reply) {
395                                 r = -ENOMEM;
396                                 goto fail;
397                         }
398
399                         p = session_bus_path(session);
400                         if (!p) {
401                                 r = -ENOMEM;
402                                 goto fail;
403                         }
404
405                         b = dbus_message_append_args(
406                                         reply,
407                                         DBUS_TYPE_STRING, &session->id,
408                                         DBUS_TYPE_OBJECT_PATH, &p,
409                                         DBUS_TYPE_STRING, &session->user->runtime_path,
410                                         DBUS_TYPE_UNIX_FD, &fifo_fd,
411                                         DBUS_TYPE_INVALID);
412                         free(p);
413
414                         if (!b) {
415                                 r = -ENOMEM;
416                                 goto fail;
417                         }
418
419                         close_nointr_nofail(fifo_fd);
420                         *_reply = reply;
421
422                         return 0;
423                 }
424
425         } else {
426                 do {
427                         free(id);
428                         asprintf(&id, "c%lu", ++m->session_counter);
429
430                         if (!id) {
431                                 r = -ENOMEM;
432                                 goto fail;
433                         }
434
435                 } while (hashmap_get(m->sessions, id));
436         }
437
438         r = manager_add_session(m, user, id, &session);
439         free(id);
440         if (r < 0)
441                 goto fail;
442
443         session->leader = leader;
444         session->audit_id = audit_id;
445         session->type = t;
446         session->remote = remote;
447         session->controllers = controllers;
448         session->reset_controllers = reset_controllers;
449         session->kill_processes = kill_processes;
450         session->vtnr = vtnr;
451
452         controllers = reset_controllers = NULL;
453
454         if (!isempty(tty)) {
455                 session->tty = strdup(tty);
456                 if (!session->tty) {
457                         r = -ENOMEM;
458                         goto fail;
459                 }
460         }
461
462         if (!isempty(display)) {
463                 session->display = strdup(display);
464                 if (!session->display) {
465                         r = -ENOMEM;
466                         goto fail;
467                 }
468         }
469
470         if (!isempty(remote_user)) {
471                 session->remote_user = strdup(remote_user);
472                 if (!session->remote_user) {
473                         r = -ENOMEM;
474                         goto fail;
475                 }
476         }
477
478         if (!isempty(remote_host)) {
479                 session->remote_host = strdup(remote_host);
480                 if (!session->remote_host) {
481                         r = -ENOMEM;
482                         goto fail;
483                 }
484         }
485
486         if (!isempty(service)) {
487                 session->service = strdup(service);
488                 if (!session->service) {
489                         r = -ENOMEM;
490                         goto fail;
491                 }
492         }
493
494         fifo_fd = session_create_fifo(session);
495         if (fifo_fd < 0) {
496                 r = fifo_fd;
497                 goto fail;
498         }
499
500         if (s) {
501                 r = seat_attach_session(s, session);
502                 if (r < 0)
503                         goto fail;
504         }
505
506         r = session_start(session);
507         if (r < 0)
508                 goto fail;
509
510         reply = dbus_message_new_method_return(message);
511         if (!reply) {
512                 r = -ENOMEM;
513                 goto fail;
514         }
515
516         p = session_bus_path(session);
517         if (!p) {
518                 r = -ENOMEM;
519                 goto fail;
520         }
521
522         b = dbus_message_append_args(
523                         reply,
524                         DBUS_TYPE_STRING, &session->id,
525                         DBUS_TYPE_OBJECT_PATH, &p,
526                         DBUS_TYPE_STRING, &session->user->runtime_path,
527                         DBUS_TYPE_UNIX_FD, &fifo_fd,
528                         DBUS_TYPE_INVALID);
529         free(p);
530
531         if (!b) {
532                 r = -ENOMEM;
533                 goto fail;
534         }
535
536         close_nointr_nofail(fifo_fd);
537         *_reply = reply;
538
539         return 0;
540
541 fail:
542         strv_free(controllers);
543         strv_free(reset_controllers);
544
545         if (session)
546                 session_add_to_gc_queue(session);
547
548         if (user)
549                 user_add_to_gc_queue(user);
550
551         if (fifo_fd >= 0)
552                 close_nointr_nofail(fifo_fd);
553
554         if (reply)
555                 dbus_message_unref(reply);
556
557         return r;
558 }
559
560 static int trigger_device(Manager *m, struct udev_device *d) {
561         struct udev_enumerate *e;
562         struct udev_list_entry *first, *item;
563         int r;
564
565         assert(m);
566
567         e = udev_enumerate_new(m->udev);
568         if (!e) {
569                 r = -ENOMEM;
570                 goto finish;
571         }
572
573         if (d) {
574                 if (udev_enumerate_add_match_parent(e, d) < 0) {
575                         r = -EIO;
576                         goto finish;
577                 }
578         }
579
580
581         if (udev_enumerate_scan_devices(e) < 0) {
582                 r = -EIO;
583                 goto finish;
584         }
585
586         first = udev_enumerate_get_list_entry(e);
587         udev_list_entry_foreach(item, first) {
588                 char *t;
589                 const char *p;
590
591                 p = udev_list_entry_get_name(item);
592
593                 t = strappend(p, "/uevent");
594                 if (!t) {
595                         r = -ENOMEM;
596                         goto finish;
597                 }
598
599                 write_one_line_file(t, "change");
600                 free(t);
601         }
602
603         r = 0;
604
605 finish:
606         if (e)
607                 udev_enumerate_unref(e);
608
609         return r;
610 }
611
612 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
613         struct udev_device *d;
614         char *rule = NULL, *file = NULL;
615         const char *id_for_seat;
616         int r;
617
618         assert(m);
619         assert(seat);
620         assert(sysfs);
621
622         d = udev_device_new_from_syspath(m->udev, sysfs);
623         if (!d)
624                 return -ENODEV;
625
626         if (!udev_device_has_tag(d, "seat")) {
627                 r = -ENODEV;
628                 goto finish;
629         }
630
631         id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
632         if (!id_for_seat) {
633                 r = -ENODEV;
634                 goto finish;
635         }
636
637         if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
638                 r = -ENOMEM;
639                 goto finish;
640         }
641
642         if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
643                 r = -ENOMEM;
644                 goto finish;
645         }
646
647         mkdir_p("/etc/udev/rules.d", 0755);
648         r = write_one_line_file_atomic(file, rule);
649         if (r < 0)
650                 goto finish;
651
652         r = trigger_device(m, d);
653
654 finish:
655         free(rule);
656         free(file);
657
658         if (d)
659                 udev_device_unref(d);
660
661         return r;
662 }
663
664 static int flush_devices(Manager *m) {
665         DIR *d;
666
667         assert(m);
668
669         d = opendir("/etc/udev/rules.d");
670         if (!d) {
671                 if (errno != ENOENT)
672                         log_warning("Failed to open /etc/udev/rules.d: %m");
673         } else {
674                 struct dirent *de;
675
676                 while ((de = readdir(d))) {
677
678                         if (!dirent_is_file(de))
679                                 continue;
680
681                         if (!startswith(de->d_name, "72-seat-"))
682                                 continue;
683
684                         if (!endswith(de->d_name, ".rules"))
685                                 continue;
686
687                         if (unlinkat(dirfd(d), de->d_name, 0) < 0)
688                                 log_warning("Failed to unlink %s: %m", de->d_name);
689                 }
690
691                 closedir(d);
692         }
693
694         return trigger_device(m, NULL);
695 }
696
697 static DBusHandlerResult manager_message_handler(
698                 DBusConnection *connection,
699                 DBusMessage *message,
700                 void *userdata) {
701
702         Manager *m = userdata;
703
704         const BusProperty properties[] = {
705                 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy",  bus_property_append_string,   "s",  m->cgroup_path          },
706                 { "org.freedesktop.login1.Manager", "Controllers",            bus_property_append_strv,     "as", m->controllers          },
707                 { "org.freedesktop.login1.Manager", "ResetControllers",       bus_property_append_strv,     "as", m->reset_controllers    },
708                 { "org.freedesktop.login1.Manager", "NAutoVTs",               bus_property_append_unsigned, "u",  &m->n_autovts           },
709                 { "org.freedesktop.login1.Manager", "KillOnlyUsers",          bus_property_append_strv,     "as", m->kill_only_users      },
710                 { "org.freedesktop.login1.Manager", "KillExcludeUsers",       bus_property_append_strv,     "as", m->kill_exclude_users   },
711                 { "org.freedesktop.login1.Manager", "KillUserProcesses",      bus_property_append_bool,     "b",  &m->kill_user_processes },
712                 { "org.freedesktop.login1.Manager", "IdleHint",               bus_manager_append_idle_hint, "b",  m                       },
713                 { "org.freedesktop.login1.Manager", "IdleSinceHint",          bus_manager_append_idle_hint_since, "t", m                  },
714                 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m                  },
715                 { NULL, NULL, NULL, NULL, NULL }
716         };
717
718         DBusError error;
719         DBusMessage *reply = NULL;
720         int r;
721
722         assert(connection);
723         assert(message);
724         assert(m);
725
726         dbus_error_init(&error);
727
728         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
729                 const char *name;
730                 char *p;
731                 Session *session;
732                 bool b;
733
734                 if (!dbus_message_get_args(
735                                     message,
736                                     &error,
737                                     DBUS_TYPE_STRING, &name,
738                                     DBUS_TYPE_INVALID))
739                         return bus_send_error_reply(connection, message, &error, -EINVAL);
740
741                 session = hashmap_get(m->sessions, name);
742                 if (!session)
743                         return bus_send_error_reply(connection, message, &error, -ENOENT);
744
745                 reply = dbus_message_new_method_return(message);
746                 if (!reply)
747                         goto oom;
748
749                 p = session_bus_path(session);
750                 if (!p)
751                         goto oom;
752
753                 b = dbus_message_append_args(
754                                 reply,
755                                 DBUS_TYPE_OBJECT_PATH, &p,
756                                 DBUS_TYPE_INVALID);
757                 free(p);
758
759                 if (!b)
760                         goto oom;
761
762         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
763                 uint32_t uid;
764                 char *p;
765                 User *user;
766                 bool b;
767
768                 if (!dbus_message_get_args(
769                                     message,
770                                     &error,
771                                     DBUS_TYPE_UINT32, &uid,
772                                     DBUS_TYPE_INVALID))
773                         return bus_send_error_reply(connection, message, &error, -EINVAL);
774
775                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
776                 if (!user)
777                         return bus_send_error_reply(connection, message, &error, -ENOENT);
778
779                 reply = dbus_message_new_method_return(message);
780                 if (!reply)
781                         goto oom;
782
783                 p = user_bus_path(user);
784                 if (!p)
785                         goto oom;
786
787                 b = dbus_message_append_args(
788                                 reply,
789                                 DBUS_TYPE_OBJECT_PATH, &p,
790                                 DBUS_TYPE_INVALID);
791                 free(p);
792
793                 if (!b)
794                         goto oom;
795
796         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
797                 const char *name;
798                 char *p;
799                 Seat *seat;
800                 bool b;
801
802                 if (!dbus_message_get_args(
803                                     message,
804                                     &error,
805                                     DBUS_TYPE_STRING, &name,
806                                     DBUS_TYPE_INVALID))
807                         return bus_send_error_reply(connection, message, &error, -EINVAL);
808
809                 seat = hashmap_get(m->seats, name);
810                 if (!seat)
811                         return bus_send_error_reply(connection, message, &error, -ENOENT);
812
813                 reply = dbus_message_new_method_return(message);
814                 if (!reply)
815                         goto oom;
816
817                 p = seat_bus_path(seat);
818                 if (!p)
819                         goto oom;
820
821                 b = dbus_message_append_args(
822                                 reply,
823                                 DBUS_TYPE_OBJECT_PATH, &p,
824                                 DBUS_TYPE_INVALID);
825                 free(p);
826
827                 if (!b)
828                         goto oom;
829
830         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
831                 char *p;
832                 Session *session;
833                 Iterator i;
834                 DBusMessageIter iter, sub;
835                 const char *empty = "";
836
837                 reply = dbus_message_new_method_return(message);
838                 if (!reply)
839                         goto oom;
840
841                 dbus_message_iter_init_append(reply, &iter);
842
843                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
844                         goto oom;
845
846                 HASHMAP_FOREACH(session, m->sessions, i) {
847                         DBusMessageIter sub2;
848                         uint32_t uid;
849
850                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
851                                 goto oom;
852
853                         uid = session->user->uid;
854
855                         p = session_bus_path(session);
856                         if (!p)
857                                 goto oom;
858
859                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
860                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
861                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
862                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
863                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
864                                 free(p);
865                                 goto oom;
866                         }
867
868                         free(p);
869
870                         if (!dbus_message_iter_close_container(&sub, &sub2))
871                                 goto oom;
872                 }
873
874                 if (!dbus_message_iter_close_container(&iter, &sub))
875                         goto oom;
876
877         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
878                 char *p;
879                 User *user;
880                 Iterator i;
881                 DBusMessageIter iter, sub;
882
883                 reply = dbus_message_new_method_return(message);
884                 if (!reply)
885                         goto oom;
886
887                 dbus_message_iter_init_append(reply, &iter);
888
889                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
890                         goto oom;
891
892                 HASHMAP_FOREACH(user, m->users, i) {
893                         DBusMessageIter sub2;
894                         uint32_t uid;
895
896                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
897                                 goto oom;
898
899                         uid = user->uid;
900
901                         p = user_bus_path(user);
902                         if (!p)
903                                 goto oom;
904
905                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
906                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
907                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
908                                 free(p);
909                                 goto oom;
910                         }
911
912                         free(p);
913
914                         if (!dbus_message_iter_close_container(&sub, &sub2))
915                                 goto oom;
916                 }
917
918                 if (!dbus_message_iter_close_container(&iter, &sub))
919                         goto oom;
920
921         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
922                 char *p;
923                 Seat *seat;
924                 Iterator i;
925                 DBusMessageIter iter, sub;
926
927                 reply = dbus_message_new_method_return(message);
928                 if (!reply)
929                         goto oom;
930
931                 dbus_message_iter_init_append(reply, &iter);
932
933                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
934                         goto oom;
935
936                 HASHMAP_FOREACH(seat, m->seats, i) {
937                         DBusMessageIter sub2;
938
939                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
940                                 goto oom;
941
942                         p = seat_bus_path(seat);
943                         if (!p)
944                                 goto oom;
945
946                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
947                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
948                                 free(p);
949                                 goto oom;
950                         }
951
952                         free(p);
953
954                         if (!dbus_message_iter_close_container(&sub, &sub2))
955                                 goto oom;
956                 }
957
958                 if (!dbus_message_iter_close_container(&iter, &sub))
959                         goto oom;
960
961         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
962
963                 r = bus_manager_create_session(m, message, &reply);
964                 if (r == -ENOMEM)
965                         goto oom;
966
967                 if (r < 0)
968                         return bus_send_error_reply(connection, message, &error, r);
969
970         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
971                 const char *name;
972                 Session *session;
973
974                 if (!dbus_message_get_args(
975                                     message,
976                                     &error,
977                                     DBUS_TYPE_STRING, &name,
978                                     DBUS_TYPE_INVALID))
979                         return bus_send_error_reply(connection, message, &error, -EINVAL);
980
981                 session = hashmap_get(m->sessions, name);
982                 if (!session)
983                         return bus_send_error_reply(connection, message, &error, -ENOENT);
984
985                 r = session_activate(session);
986                 if (r < 0)
987                         return bus_send_error_reply(connection, message, NULL, r);
988
989                 reply = dbus_message_new_method_return(message);
990                 if (!reply)
991                         goto oom;
992
993         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
994                    dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
995                 const char *name;
996                 Session *session;
997
998                 if (!dbus_message_get_args(
999                                     message,
1000                                     &error,
1001                                     DBUS_TYPE_STRING, &name,
1002                                     DBUS_TYPE_INVALID))
1003                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1004
1005                 session = hashmap_get(m->sessions, name);
1006                 if (!session)
1007                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1008
1009                 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1010                         goto oom;
1011
1012                 reply = dbus_message_new_method_return(message);
1013                 if (!reply)
1014                         goto oom;
1015
1016         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1017                 const char *swho;
1018                 int32_t signo;
1019                 KillWho who;
1020                 const char *name;
1021                 Session *session;
1022
1023                 if (!dbus_message_get_args(
1024                                     message,
1025                                     &error,
1026                                     DBUS_TYPE_STRING, &name,
1027                                     DBUS_TYPE_STRING, &swho,
1028                                     DBUS_TYPE_INT32, &signo,
1029                                     DBUS_TYPE_INVALID))
1030                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1031
1032                 if (isempty(swho))
1033                         who = KILL_ALL;
1034                 else {
1035                         who = kill_who_from_string(swho);
1036                         if (who < 0)
1037                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
1038                 }
1039
1040                 if (signo <= 0 || signo >= _NSIG)
1041                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1042
1043                 session = hashmap_get(m->sessions, name);
1044                 if (!session)
1045                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1046
1047                 r = session_kill(session, who, signo);
1048                 if (r < 0)
1049                         return bus_send_error_reply(connection, message, NULL, r);
1050
1051                 reply = dbus_message_new_method_return(message);
1052                 if (!reply)
1053                         goto oom;
1054
1055         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1056                 uint32_t uid;
1057                 User *user;
1058                 int32_t signo;
1059
1060                 if (!dbus_message_get_args(
1061                                     message,
1062                                     &error,
1063                                     DBUS_TYPE_UINT32, &uid,
1064                                     DBUS_TYPE_INT32, &signo,
1065                                     DBUS_TYPE_INVALID))
1066                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1067
1068                 if (signo <= 0 || signo >= _NSIG)
1069                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1070
1071                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1072                 if (!user)
1073                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1074
1075                 r = user_kill(user, signo);
1076                 if (r < 0)
1077                         return bus_send_error_reply(connection, message, NULL, r);
1078
1079                 reply = dbus_message_new_method_return(message);
1080                 if (!reply)
1081                         goto oom;
1082
1083         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1084                 const char *name;
1085                 Session *session;
1086
1087                 if (!dbus_message_get_args(
1088                                     message,
1089                                     &error,
1090                                     DBUS_TYPE_STRING, &name,
1091                                     DBUS_TYPE_INVALID))
1092                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1093
1094                 session = hashmap_get(m->sessions, name);
1095                 if (!session)
1096                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1097
1098                 r = session_stop(session);
1099                 if (r < 0)
1100                         return bus_send_error_reply(connection, message, NULL, r);
1101
1102                 reply = dbus_message_new_method_return(message);
1103                 if (!reply)
1104                         goto oom;
1105
1106         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1107                 uint32_t uid;
1108                 User *user;
1109
1110                 if (!dbus_message_get_args(
1111                                     message,
1112                                     &error,
1113                                     DBUS_TYPE_UINT32, &uid,
1114                                     DBUS_TYPE_INVALID))
1115                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1116
1117                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1118                 if (!user)
1119                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1120
1121                 r = user_stop(user);
1122                 if (r < 0)
1123                         return bus_send_error_reply(connection, message, NULL, r);
1124
1125                 reply = dbus_message_new_method_return(message);
1126                 if (!reply)
1127                         goto oom;
1128
1129         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1130                 const char *name;
1131                 Seat *seat;
1132
1133                 if (!dbus_message_get_args(
1134                                     message,
1135                                     &error,
1136                                     DBUS_TYPE_STRING, &name,
1137                                     DBUS_TYPE_INVALID))
1138                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1139
1140                 seat = hashmap_get(m->seats, name);
1141                 if (!seat)
1142                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1143
1144                 r = seat_stop_sessions(seat);
1145                 if (r < 0)
1146                         return bus_send_error_reply(connection, message, NULL, r);
1147
1148                 reply = dbus_message_new_method_return(message);
1149                 if (!reply)
1150                         goto oom;
1151
1152         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1153                 uint32_t uid;
1154                 struct passwd *pw;
1155                 dbus_bool_t b, interactive;
1156                 char *path;
1157
1158                 if (!dbus_message_get_args(
1159                                     message,
1160                                     &error,
1161                                     DBUS_TYPE_UINT32, &uid,
1162                                     DBUS_TYPE_BOOLEAN, &b,
1163                                     DBUS_TYPE_BOOLEAN, &interactive,
1164                                     DBUS_TYPE_INVALID))
1165                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1166
1167                 errno = 0;
1168                 pw = getpwuid(uid);
1169                 if (!pw)
1170                         return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1171
1172                 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1173                 if (r < 0)
1174                         return bus_send_error_reply(connection, message, &error, r);
1175
1176                 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1177                 if (r < 0)
1178                         return bus_send_error_reply(connection, message, &error, r);
1179
1180                 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1181                 if (!path)
1182                         goto oom;
1183
1184                 if (b) {
1185                         User *u;
1186
1187                         r = touch(path);
1188                         free(path);
1189
1190                         if (r < 0)
1191                                 return bus_send_error_reply(connection, message, &error, r);
1192
1193                         if (manager_add_user_by_uid(m, uid, &u) >= 0)
1194                                 user_start(u);
1195
1196                 } else {
1197                         User *u;
1198
1199                         r = unlink(path);
1200                         free(path);
1201
1202                         if (r < 0 && errno != ENOENT)
1203                                 return bus_send_error_reply(connection, message, &error, -errno);
1204
1205                         u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1206                         if (u)
1207                                 user_add_to_gc_queue(u);
1208                 }
1209
1210                 reply = dbus_message_new_method_return(message);
1211                 if (!reply)
1212                         goto oom;
1213
1214         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1215                 const char *sysfs, *seat;
1216                 dbus_bool_t interactive;
1217
1218                 if (!dbus_message_get_args(
1219                                     message,
1220                                     &error,
1221                                     DBUS_TYPE_STRING, &seat,
1222                                     DBUS_TYPE_STRING, &sysfs,
1223                                     DBUS_TYPE_BOOLEAN, &interactive,
1224                                     DBUS_TYPE_INVALID))
1225                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1226
1227                 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1228                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1229
1230                 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1231                 if (r < 0)
1232                         return bus_send_error_reply(connection, message, &error, r);
1233
1234                 r = attach_device(m, seat, sysfs);
1235                 if (r < 0)
1236                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1237
1238                 reply = dbus_message_new_method_return(message);
1239                 if (!reply)
1240                         goto oom;
1241
1242
1243         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1244                 dbus_bool_t interactive;
1245
1246                 if (!dbus_message_get_args(
1247                                     message,
1248                                     &error,
1249                                     DBUS_TYPE_BOOLEAN, &interactive,
1250                                     DBUS_TYPE_INVALID))
1251                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1252
1253                 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1254                 if (r < 0)
1255                         return bus_send_error_reply(connection, message, &error, r);
1256
1257                 r = flush_devices(m);
1258                 if (r < 0)
1259                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1260
1261                 reply = dbus_message_new_method_return(message);
1262                 if (!reply)
1263                         goto oom;
1264
1265         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "PowerOff") ||
1266                    dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "Reboot")) {
1267                 dbus_bool_t interactive;
1268                 bool multiple_sessions;
1269                 DBusMessage *forward, *freply;
1270                 const char *name;
1271                 const char *mode = "replace";
1272                 const char *action;
1273
1274                 if (!dbus_message_get_args(
1275                                     message,
1276                                     &error,
1277                                     DBUS_TYPE_BOOLEAN, &interactive,
1278                                     DBUS_TYPE_INVALID))
1279                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1280
1281                 multiple_sessions = hashmap_size(m->sessions) > 1;
1282
1283                 if (!multiple_sessions) {
1284                         Session *s;
1285
1286                         /* Hmm, there's only one session, but let's
1287                          * make sure it actually belongs to the user
1288                          * who is asking. If not, better be safe than
1289                          * sorry. */
1290
1291                         s = hashmap_first(m->sessions);
1292                         if (s) {
1293                                 unsigned long ul;
1294
1295                                 ul = dbus_bus_get_unix_user(connection, dbus_message_get_sender(message), &error);
1296                                 if (ul == (unsigned long) -1)
1297                                         return bus_send_error_reply(connection, message, &error, -EIO);
1298
1299                                 multiple_sessions = s->user->uid != ul;
1300                         }
1301                 }
1302
1303                 if (streq(dbus_message_get_member(message), "PowerOff")) {
1304                         if (multiple_sessions)
1305                                 action = "org.freedesktop.login1.power-off-multiple-sessions";
1306                         else
1307                                 action = "org.freedesktop.login1.power-off";
1308
1309                         name = SPECIAL_POWEROFF_TARGET;
1310                 } else {
1311                         if (multiple_sessions)
1312                                 action = "org.freedesktop.login1.reboot-multiple-sessions";
1313                         else
1314                                 action = "org.freedesktop.login1.reboot";
1315
1316                         name = SPECIAL_REBOOT_TARGET;
1317                 }
1318
1319                 r = verify_polkit(connection, message, action, interactive, &error);
1320                 if (r < 0)
1321                         return bus_send_error_reply(connection, message, &error, r);
1322
1323                 forward = dbus_message_new_method_call(
1324                               "org.freedesktop.systemd1",
1325                               "/org/freedesktop/systemd1",
1326                               "org.freedesktop.systemd1.Manager",
1327                               "StartUnit");
1328                 if (!forward)
1329                         return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1330
1331                 if (!dbus_message_append_args(forward,
1332                                               DBUS_TYPE_STRING, &name,
1333                                               DBUS_TYPE_STRING, &mode,
1334                                               DBUS_TYPE_INVALID)) {
1335                         dbus_message_unref(forward);
1336                         return bus_send_error_reply(connection, message, NULL, -ENOMEM);
1337                 }
1338
1339                 freply = dbus_connection_send_with_reply_and_block(connection, forward, -1, &error);
1340                 dbus_message_unref(forward);
1341
1342                 if (!freply)
1343                         return bus_send_error_reply(connection, message, &error, -EIO);
1344
1345                 dbus_message_unref(freply);
1346
1347                 reply = dbus_message_new_method_return(message);
1348                 if (!reply)
1349                         goto oom;
1350
1351         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1352                 char *introspection = NULL;
1353                 FILE *f;
1354                 Iterator i;
1355                 Session *session;
1356                 Seat *seat;
1357                 User *user;
1358                 size_t size;
1359                 char *p;
1360
1361                 if (!(reply = dbus_message_new_method_return(message)))
1362                         goto oom;
1363
1364                 /* We roll our own introspection code here, instead of
1365                  * relying on bus_default_message_handler() because we
1366                  * need to generate our introspection string
1367                  * dynamically. */
1368
1369                 if (!(f = open_memstream(&introspection, &size)))
1370                         goto oom;
1371
1372                 fputs(INTROSPECTION_BEGIN, f);
1373
1374                 HASHMAP_FOREACH(seat, m->seats, i) {
1375                         p = bus_path_escape(seat->id);
1376
1377                         if (p) {
1378                                 fprintf(f, "<node name=\"seat/%s\"/>", p);
1379                                 free(p);
1380                         }
1381                 }
1382
1383                 HASHMAP_FOREACH(user, m->users, i)
1384                         fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1385
1386                 HASHMAP_FOREACH(session, m->sessions, i) {
1387                         p = bus_path_escape(session->id);
1388
1389                         if (p) {
1390                                 fprintf(f, "<node name=\"session/%s\"/>", p);
1391                                 free(p);
1392                         }
1393                 }
1394
1395                 fputs(INTROSPECTION_END, f);
1396
1397                 if (ferror(f)) {
1398                         fclose(f);
1399                         free(introspection);
1400                         goto oom;
1401                 }
1402
1403                 fclose(f);
1404
1405                 if (!introspection)
1406                         goto oom;
1407
1408                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1409                         free(introspection);
1410                         goto oom;
1411                 }
1412
1413                 free(introspection);
1414         } else
1415                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1416
1417         if (reply) {
1418                 if (!dbus_connection_send(connection, reply, NULL))
1419                         goto oom;
1420
1421                 dbus_message_unref(reply);
1422         }
1423
1424         return DBUS_HANDLER_RESULT_HANDLED;
1425
1426 oom:
1427         if (reply)
1428                 dbus_message_unref(reply);
1429
1430         dbus_error_free(&error);
1431
1432         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1433 }
1434
1435 const DBusObjectPathVTable bus_manager_vtable = {
1436         .message_function = manager_message_handler
1437 };
1438
1439 DBusHandlerResult bus_message_filter(
1440                 DBusConnection *connection,
1441                 DBusMessage *message,
1442                 void *userdata) {
1443
1444         Manager *m = userdata;
1445         DBusError error;
1446
1447         assert(m);
1448         assert(connection);
1449         assert(message);
1450
1451         dbus_error_init(&error);
1452
1453         if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1454                 const char *cgroup;
1455
1456                 if (!dbus_message_get_args(message, &error,
1457                                            DBUS_TYPE_STRING, &cgroup,
1458                                            DBUS_TYPE_INVALID))
1459                         log_error("Failed to parse Released message: %s", bus_error_message(&error));
1460                 else
1461                         manager_cgroup_notify_empty(m, cgroup);
1462         }
1463
1464         dbus_error_free(&error);
1465
1466         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1467 }
1468
1469 int manager_send_changed(Manager *manager, const char *properties) {
1470         DBusMessage *m;
1471         int r = -ENOMEM;
1472
1473         assert(manager);
1474
1475         m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1476         if (!m)
1477                 goto finish;
1478
1479         if (!dbus_connection_send(manager->bus, m, NULL))
1480                 goto finish;
1481
1482         r = 0;
1483
1484 finish:
1485         if (m)
1486                 dbus_message_unref(m);
1487
1488         return r;
1489 }