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