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