chiark / gitweb /
systemctl: don't try rebooting with /dev/initcl if Upstart worked
[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, struct udev_device *d) {
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 (d) {
567                 if (udev_enumerate_add_match_parent(e, d) < 0) {
568                         r = -EIO;
569                         goto finish;
570                 }
571         }
572
573
574         if (udev_enumerate_scan_devices(e) < 0) {
575                 r = -EIO;
576                 goto finish;
577         }
578
579         first = udev_enumerate_get_list_entry(e);
580         udev_list_entry_foreach(item, first) {
581                 char *t;
582                 const char *p;
583
584                 p = udev_list_entry_get_name(item);
585
586                 t = strappend(p, "/uevent");
587                 if (!t) {
588                         r = -ENOMEM;
589                         goto finish;
590                 }
591
592                 write_one_line_file(t, "change");
593                 free(t);
594         }
595
596         r = 0;
597
598 finish:
599         if (e)
600                 udev_enumerate_unref(e);
601
602         return r;
603 }
604
605 static int attach_device(Manager *m, const char *seat, const char *sysfs) {
606         struct udev_device *d;
607         char *rule = NULL, *file = NULL;
608         const char *id_for_seat;
609         int r;
610
611         assert(m);
612         assert(seat);
613         assert(sysfs);
614
615         d = udev_device_new_from_syspath(m->udev, sysfs);
616         if (!d)
617                 return -ENODEV;
618
619         if (!udev_device_has_tag(d, "seat")) {
620                 r = -ENODEV;
621                 goto finish;
622         }
623
624         id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT");
625         if (!id_for_seat) {
626                 r = -ENODEV;
627                 goto finish;
628         }
629
630         if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) {
631                 r = -ENOMEM;
632                 goto finish;
633         }
634
635         if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) {
636                 r = -ENOMEM;
637                 goto finish;
638         }
639
640         mkdir_p("/etc/udev/rules.d", 0755);
641         r = write_one_line_file_atomic(file, rule);
642         if (r < 0)
643                 goto finish;
644
645         r = trigger_device(m, d);
646
647 finish:
648         free(rule);
649         free(file);
650
651         if (d)
652                 udev_device_unref(d);
653
654         return r;
655 }
656
657 static int flush_devices(Manager *m) {
658         DIR *d;
659
660         assert(m);
661
662         d = opendir("/etc/udev/rules.d");
663         if (!d) {
664                 if (errno != ENOENT)
665                         log_warning("Failed to open /etc/udev/rules.d: %m");
666         } else {
667                 struct dirent *de;
668
669                 while ((de = readdir(d))) {
670
671                         if (!dirent_is_file(de))
672                                 continue;
673
674                         if (!startswith(de->d_name, "72-seat-"))
675                                 continue;
676
677                         if (!endswith(de->d_name, ".rules"))
678                                 continue;
679
680                         if (unlinkat(dirfd(d), de->d_name, 0) < 0)
681                                 log_warning("Failed to unlink %s: %m", de->d_name);
682                 }
683
684                 closedir(d);
685         }
686
687         return trigger_device(m, NULL);
688 }
689
690 static DBusHandlerResult manager_message_handler(
691                 DBusConnection *connection,
692                 DBusMessage *message,
693                 void *userdata) {
694
695         Manager *m = userdata;
696
697         const BusProperty properties[] = {
698                 { "org.freedesktop.login1.Manager", "ControlGroupHierarchy",  bus_property_append_string,   "s",  m->cgroup_path          },
699                 { "org.freedesktop.login1.Manager", "Controllers",            bus_property_append_strv,     "as", m->controllers          },
700                 { "org.freedesktop.login1.Manager", "ResetControllers",       bus_property_append_strv,     "as", m->reset_controllers    },
701                 { "org.freedesktop.login1.Manager", "NAutoVTs",               bus_property_append_unsigned, "u",  &m->n_autovts           },
702                 { "org.freedesktop.login1.Manager", "KillOnlyUsers",          bus_property_append_strv,     "as", m->kill_only_users      },
703                 { "org.freedesktop.login1.Manager", "KillExcludeUsers",       bus_property_append_strv,     "as", m->kill_exclude_users   },
704                 { "org.freedesktop.login1.Manager", "KillUserProcesses",      bus_property_append_bool,     "b",  &m->kill_user_processes },
705                 { "org.freedesktop.login1.Manager", "IdleHint",               bus_manager_append_idle_hint, "b",  m                       },
706                 { "org.freedesktop.login1.Manager", "IdleSinceHint",          bus_manager_append_idle_hint_since, "t", m                  },
707                 { "org.freedesktop.login1.Manager", "IdleSinceHintMonotonic", bus_manager_append_idle_hint_since, "t", m                  },
708                 { NULL, NULL, NULL, NULL, NULL }
709         };
710
711         DBusError error;
712         DBusMessage *reply = NULL;
713         int r;
714
715         assert(connection);
716         assert(message);
717         assert(m);
718
719         dbus_error_init(&error);
720
721         if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSession")) {
722                 const char *name;
723                 char *p;
724                 Session *session;
725                 bool b;
726
727                 if (!dbus_message_get_args(
728                                     message,
729                                     &error,
730                                     DBUS_TYPE_STRING, &name,
731                                     DBUS_TYPE_INVALID))
732                         return bus_send_error_reply(connection, message, &error, -EINVAL);
733
734                 session = hashmap_get(m->sessions, name);
735                 if (!session)
736                         return bus_send_error_reply(connection, message, &error, -ENOENT);
737
738                 reply = dbus_message_new_method_return(message);
739                 if (!reply)
740                         goto oom;
741
742                 p = session_bus_path(session);
743                 if (!p)
744                         goto oom;
745
746                 b = dbus_message_append_args(
747                                 reply,
748                                 DBUS_TYPE_OBJECT_PATH, &p,
749                                 DBUS_TYPE_INVALID);
750                 free(p);
751
752                 if (!b)
753                         goto oom;
754
755         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetUser")) {
756                 uint32_t uid;
757                 char *p;
758                 User *user;
759                 bool b;
760
761                 if (!dbus_message_get_args(
762                                     message,
763                                     &error,
764                                     DBUS_TYPE_UINT32, &uid,
765                                     DBUS_TYPE_INVALID))
766                         return bus_send_error_reply(connection, message, &error, -EINVAL);
767
768                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
769                 if (!user)
770                         return bus_send_error_reply(connection, message, &error, -ENOENT);
771
772                 reply = dbus_message_new_method_return(message);
773                 if (!reply)
774                         goto oom;
775
776                 p = user_bus_path(user);
777                 if (!p)
778                         goto oom;
779
780                 b = dbus_message_append_args(
781                                 reply,
782                                 DBUS_TYPE_OBJECT_PATH, &p,
783                                 DBUS_TYPE_INVALID);
784                 free(p);
785
786                 if (!b)
787                         goto oom;
788
789         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "GetSeat")) {
790                 const char *name;
791                 char *p;
792                 Seat *seat;
793                 bool b;
794
795                 if (!dbus_message_get_args(
796                                     message,
797                                     &error,
798                                     DBUS_TYPE_STRING, &name,
799                                     DBUS_TYPE_INVALID))
800                         return bus_send_error_reply(connection, message, &error, -EINVAL);
801
802                 seat = hashmap_get(m->seats, name);
803                 if (!seat)
804                         return bus_send_error_reply(connection, message, &error, -ENOENT);
805
806                 reply = dbus_message_new_method_return(message);
807                 if (!reply)
808                         goto oom;
809
810                 p = seat_bus_path(seat);
811                 if (!p)
812                         goto oom;
813
814                 b = dbus_message_append_args(
815                                 reply,
816                                 DBUS_TYPE_OBJECT_PATH, &p,
817                                 DBUS_TYPE_INVALID);
818                 free(p);
819
820                 if (!b)
821                         goto oom;
822
823         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSessions")) {
824                 char *p;
825                 Session *session;
826                 Iterator i;
827                 DBusMessageIter iter, sub;
828                 const char *empty = "";
829
830                 reply = dbus_message_new_method_return(message);
831                 if (!reply)
832                         goto oom;
833
834                 dbus_message_iter_init_append(reply, &iter);
835
836                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(susso)", &sub))
837                         goto oom;
838
839                 HASHMAP_FOREACH(session, m->sessions, i) {
840                         DBusMessageIter sub2;
841                         uint32_t uid;
842
843                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
844                                 goto oom;
845
846                         uid = session->user->uid;
847
848                         p = session_bus_path(session);
849                         if (!p)
850                                 goto oom;
851
852                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->id) ||
853                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
854                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &session->user->name) ||
855                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, session->seat ? (const char**) &session->seat->id : &empty) ||
856                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
857                                 free(p);
858                                 goto oom;
859                         }
860
861                         free(p);
862
863                         if (!dbus_message_iter_close_container(&sub, &sub2))
864                                 goto oom;
865                 }
866
867                 if (!dbus_message_iter_close_container(&iter, &sub))
868                         goto oom;
869
870         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListUsers")) {
871                 char *p;
872                 User *user;
873                 Iterator i;
874                 DBusMessageIter iter, sub;
875
876                 reply = dbus_message_new_method_return(message);
877                 if (!reply)
878                         goto oom;
879
880                 dbus_message_iter_init_append(reply, &iter);
881
882                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(uso)", &sub))
883                         goto oom;
884
885                 HASHMAP_FOREACH(user, m->users, i) {
886                         DBusMessageIter sub2;
887                         uint32_t uid;
888
889                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
890                                 goto oom;
891
892                         uid = user->uid;
893
894                         p = user_bus_path(user);
895                         if (!p)
896                                 goto oom;
897
898                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &uid) ||
899                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &user->name) ||
900                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
901                                 free(p);
902                                 goto oom;
903                         }
904
905                         free(p);
906
907                         if (!dbus_message_iter_close_container(&sub, &sub2))
908                                 goto oom;
909                 }
910
911                 if (!dbus_message_iter_close_container(&iter, &sub))
912                         goto oom;
913
914         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ListSeats")) {
915                 char *p;
916                 Seat *seat;
917                 Iterator i;
918                 DBusMessageIter iter, sub;
919
920                 reply = dbus_message_new_method_return(message);
921                 if (!reply)
922                         goto oom;
923
924                 dbus_message_iter_init_append(reply, &iter);
925
926                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(so)", &sub))
927                         goto oom;
928
929                 HASHMAP_FOREACH(seat, m->seats, i) {
930                         DBusMessageIter sub2;
931
932                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
933                                 goto oom;
934
935                         p = seat_bus_path(seat);
936                         if (!p)
937                                 goto oom;
938
939                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &seat->id) ||
940                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &p)) {
941                                 free(p);
942                                 goto oom;
943                         }
944
945                         free(p);
946
947                         if (!dbus_message_iter_close_container(&sub, &sub2))
948                                 goto oom;
949                 }
950
951                 if (!dbus_message_iter_close_container(&iter, &sub))
952                         goto oom;
953
954         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "CreateSession")) {
955
956                 r = bus_manager_create_session(m, message, &reply);
957                 if (r == -ENOMEM)
958                         goto oom;
959
960                 if (r < 0)
961                         return bus_send_error_reply(connection, message, &error, r);
962
963         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "ActivateSession")) {
964                 const char *name;
965                 Session *session;
966
967                 if (!dbus_message_get_args(
968                                     message,
969                                     &error,
970                                     DBUS_TYPE_STRING, &name,
971                                     DBUS_TYPE_INVALID))
972                         return bus_send_error_reply(connection, message, &error, -EINVAL);
973
974                 session = hashmap_get(m->sessions, name);
975                 if (!session)
976                         return bus_send_error_reply(connection, message, &error, -ENOENT);
977
978                 r = session_activate(session);
979                 if (r < 0)
980                         return bus_send_error_reply(connection, message, NULL, r);
981
982                 reply = dbus_message_new_method_return(message);
983                 if (!reply)
984                         goto oom;
985
986         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "LockSession") ||
987                    dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "UnlockSession")) {
988                 const char *name;
989                 Session *session;
990
991                 if (!dbus_message_get_args(
992                                     message,
993                                     &error,
994                                     DBUS_TYPE_STRING, &name,
995                                     DBUS_TYPE_INVALID))
996                         return bus_send_error_reply(connection, message, &error, -EINVAL);
997
998                 session = hashmap_get(m->sessions, name);
999                 if (!session)
1000                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1001
1002                 if (session_send_lock(session, streq(dbus_message_get_member(message), "LockSession")) < 0)
1003                         goto oom;
1004
1005                 reply = dbus_message_new_method_return(message);
1006                 if (!reply)
1007                         goto oom;
1008
1009         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillSession")) {
1010                 const char *swho;
1011                 int32_t signo;
1012                 KillWho who;
1013                 const char *name;
1014                 Session *session;
1015
1016                 if (!dbus_message_get_args(
1017                                     message,
1018                                     &error,
1019                                     DBUS_TYPE_STRING, &name,
1020                                     DBUS_TYPE_STRING, &swho,
1021                                     DBUS_TYPE_INT32, &signo,
1022                                     DBUS_TYPE_INVALID))
1023                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1024
1025                 if (isempty(swho))
1026                         who = KILL_ALL;
1027                 else {
1028                         who = kill_who_from_string(swho);
1029                         if (who < 0)
1030                                 return bus_send_error_reply(connection, message, &error, -EINVAL);
1031                 }
1032
1033                 if (signo <= 0 || signo >= _NSIG)
1034                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1035
1036                 session = hashmap_get(m->sessions, name);
1037                 if (!session)
1038                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1039
1040                 r = session_kill(session, who, signo);
1041                 if (r < 0)
1042                         return bus_send_error_reply(connection, message, NULL, r);
1043
1044                 reply = dbus_message_new_method_return(message);
1045                 if (!reply)
1046                         goto oom;
1047
1048         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "KillUser")) {
1049                 uint32_t uid;
1050                 User *user;
1051                 int32_t signo;
1052
1053                 if (!dbus_message_get_args(
1054                                     message,
1055                                     &error,
1056                                     DBUS_TYPE_UINT32, &uid,
1057                                     DBUS_TYPE_INT32, &signo,
1058                                     DBUS_TYPE_INVALID))
1059                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1060
1061                 if (signo <= 0 || signo >= _NSIG)
1062                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1063
1064                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1065                 if (!user)
1066                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1067
1068                 r = user_kill(user, signo);
1069                 if (r < 0)
1070                         return bus_send_error_reply(connection, message, NULL, r);
1071
1072                 reply = dbus_message_new_method_return(message);
1073                 if (!reply)
1074                         goto oom;
1075
1076         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSession")) {
1077                 const char *name;
1078                 Session *session;
1079
1080                 if (!dbus_message_get_args(
1081                                     message,
1082                                     &error,
1083                                     DBUS_TYPE_STRING, &name,
1084                                     DBUS_TYPE_INVALID))
1085                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1086
1087                 session = hashmap_get(m->sessions, name);
1088                 if (!session)
1089                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1090
1091                 r = session_stop(session);
1092                 if (r < 0)
1093                         return bus_send_error_reply(connection, message, NULL, r);
1094
1095                 reply = dbus_message_new_method_return(message);
1096                 if (!reply)
1097                         goto oom;
1098
1099         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateUser")) {
1100                 uint32_t uid;
1101                 User *user;
1102
1103                 if (!dbus_message_get_args(
1104                                     message,
1105                                     &error,
1106                                     DBUS_TYPE_UINT32, &uid,
1107                                     DBUS_TYPE_INVALID))
1108                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1109
1110                 user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1111                 if (!user)
1112                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1113
1114                 r = user_stop(user);
1115                 if (r < 0)
1116                         return bus_send_error_reply(connection, message, NULL, r);
1117
1118                 reply = dbus_message_new_method_return(message);
1119                 if (!reply)
1120                         goto oom;
1121
1122         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "TerminateSeat")) {
1123                 const char *name;
1124                 Seat *seat;
1125
1126                 if (!dbus_message_get_args(
1127                                     message,
1128                                     &error,
1129                                     DBUS_TYPE_STRING, &name,
1130                                     DBUS_TYPE_INVALID))
1131                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1132
1133                 seat = hashmap_get(m->seats, name);
1134                 if (!seat)
1135                         return bus_send_error_reply(connection, message, &error, -ENOENT);
1136
1137                 r = seat_stop_sessions(seat);
1138                 if (r < 0)
1139                         return bus_send_error_reply(connection, message, NULL, r);
1140
1141                 reply = dbus_message_new_method_return(message);
1142                 if (!reply)
1143                         goto oom;
1144
1145         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "SetUserLinger")) {
1146                 uint32_t uid;
1147                 struct passwd *pw;
1148                 dbus_bool_t b, interactive;
1149                 char *path;
1150
1151                 if (!dbus_message_get_args(
1152                                     message,
1153                                     &error,
1154                                     DBUS_TYPE_UINT32, &uid,
1155                                     DBUS_TYPE_BOOLEAN, &b,
1156                                     DBUS_TYPE_BOOLEAN, &interactive,
1157                                     DBUS_TYPE_INVALID))
1158                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1159
1160                 errno = 0;
1161                 pw = getpwuid(uid);
1162                 if (!pw)
1163                         return bus_send_error_reply(connection, message, NULL, errno ? -errno : -EINVAL);
1164
1165                 r = verify_polkit(connection, message, "org.freedesktop.login1.set-user-linger", interactive, &error);
1166                 if (r < 0)
1167                         return bus_send_error_reply(connection, message, &error, r);
1168
1169                 r = safe_mkdir("/var/lib/systemd/linger", 0755, 0, 0);
1170                 if (r < 0)
1171                         return bus_send_error_reply(connection, message, &error, r);
1172
1173                 path = strappend("/var/lib/systemd/linger/", pw->pw_name);
1174                 if (!path)
1175                         goto oom;
1176
1177                 if (b) {
1178                         User *u;
1179
1180                         r = touch(path);
1181                         free(path);
1182
1183                         if (r < 0)
1184                                 return bus_send_error_reply(connection, message, &error, r);
1185
1186                         if (manager_add_user_by_uid(m, uid, &u) >= 0)
1187                                 user_start(u);
1188
1189                 } else {
1190                         User *u;
1191
1192                         r = unlink(path);
1193                         free(path);
1194
1195                         if (r < 0 && errno != ENOENT)
1196                                 return bus_send_error_reply(connection, message, &error, -errno);
1197
1198                         u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid));
1199                         if (u)
1200                                 user_add_to_gc_queue(u);
1201                 }
1202
1203                 reply = dbus_message_new_method_return(message);
1204                 if (!reply)
1205                         goto oom;
1206
1207         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "AttachDevice")) {
1208                 const char *sysfs, *seat;
1209                 dbus_bool_t interactive;
1210
1211                 if (!dbus_message_get_args(
1212                                     message,
1213                                     &error,
1214                                     DBUS_TYPE_STRING, &seat,
1215                                     DBUS_TYPE_STRING, &sysfs,
1216                                     DBUS_TYPE_BOOLEAN, &interactive,
1217                                     DBUS_TYPE_INVALID))
1218                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1219
1220                 if (!path_startswith(sysfs, "/sys") || !seat_name_is_valid(seat))
1221                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1222
1223                 r = verify_polkit(connection, message, "org.freedesktop.login1.attach-device", interactive, &error);
1224                 if (r < 0)
1225                         return bus_send_error_reply(connection, message, &error, r);
1226
1227                 r = attach_device(m, seat, sysfs);
1228                 if (r < 0)
1229                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1230
1231                 reply = dbus_message_new_method_return(message);
1232                 if (!reply)
1233                         goto oom;
1234
1235
1236         } else if (dbus_message_is_method_call(message, "org.freedesktop.login1.Manager", "FlushDevices")) {
1237                 dbus_bool_t interactive;
1238
1239                 if (!dbus_message_get_args(
1240                                     message,
1241                                     &error,
1242                                     DBUS_TYPE_BOOLEAN, &interactive,
1243                                     DBUS_TYPE_INVALID))
1244                         return bus_send_error_reply(connection, message, &error, -EINVAL);
1245
1246                 r = verify_polkit(connection, message, "org.freedesktop.login1.flush-devices", interactive, &error);
1247                 if (r < 0)
1248                         return bus_send_error_reply(connection, message, &error, r);
1249
1250                 r = flush_devices(m);
1251                 if (r < 0)
1252                         return bus_send_error_reply(connection, message, NULL, -EINVAL);
1253
1254                 reply = dbus_message_new_method_return(message);
1255                 if (!reply)
1256                         goto oom;
1257
1258         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1259                 char *introspection = NULL;
1260                 FILE *f;
1261                 Iterator i;
1262                 Session *session;
1263                 Seat *seat;
1264                 User *user;
1265                 size_t size;
1266                 char *p;
1267
1268                 if (!(reply = dbus_message_new_method_return(message)))
1269                         goto oom;
1270
1271                 /* We roll our own introspection code here, instead of
1272                  * relying on bus_default_message_handler() because we
1273                  * need to generate our introspection string
1274                  * dynamically. */
1275
1276                 if (!(f = open_memstream(&introspection, &size)))
1277                         goto oom;
1278
1279                 fputs(INTROSPECTION_BEGIN, f);
1280
1281                 HASHMAP_FOREACH(seat, m->seats, i) {
1282                         p = bus_path_escape(seat->id);
1283
1284                         if (p) {
1285                                 fprintf(f, "<node name=\"seat/%s\"/>", p);
1286                                 free(p);
1287                         }
1288                 }
1289
1290                 HASHMAP_FOREACH(user, m->users, i)
1291                         fprintf(f, "<node name=\"user/%llu\"/>", (unsigned long long) user->uid);
1292
1293                 HASHMAP_FOREACH(session, m->sessions, i) {
1294                         p = bus_path_escape(session->id);
1295
1296                         if (p) {
1297                                 fprintf(f, "<node name=\"session/%s\"/>", p);
1298                                 free(p);
1299                         }
1300                 }
1301
1302                 fputs(INTROSPECTION_END, f);
1303
1304                 if (ferror(f)) {
1305                         fclose(f);
1306                         free(introspection);
1307                         goto oom;
1308                 }
1309
1310                 fclose(f);
1311
1312                 if (!introspection)
1313                         goto oom;
1314
1315                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1316                         free(introspection);
1317                         goto oom;
1318                 }
1319
1320                 free(introspection);
1321         } else
1322                 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, properties);
1323
1324         if (reply) {
1325                 if (!dbus_connection_send(connection, reply, NULL))
1326                         goto oom;
1327
1328                 dbus_message_unref(reply);
1329         }
1330
1331         return DBUS_HANDLER_RESULT_HANDLED;
1332
1333 oom:
1334         if (reply)
1335                 dbus_message_unref(reply);
1336
1337         dbus_error_free(&error);
1338
1339         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1340 }
1341
1342 const DBusObjectPathVTable bus_manager_vtable = {
1343         .message_function = manager_message_handler
1344 };
1345
1346 DBusHandlerResult bus_message_filter(
1347                 DBusConnection *connection,
1348                 DBusMessage *message,
1349                 void *userdata) {
1350
1351         Manager *m = userdata;
1352         DBusError error;
1353
1354         assert(m);
1355         assert(connection);
1356         assert(message);
1357
1358         dbus_error_init(&error);
1359
1360         if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
1361                 const char *cgroup;
1362
1363                 if (!dbus_message_get_args(message, &error,
1364                                            DBUS_TYPE_STRING, &cgroup,
1365                                            DBUS_TYPE_INVALID))
1366                         log_error("Failed to parse Released message: %s", bus_error_message(&error));
1367                 else
1368                         manager_cgroup_notify_empty(m, cgroup);
1369         }
1370
1371         dbus_error_free(&error);
1372
1373         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1374 }
1375
1376 int manager_send_changed(Manager *manager, const char *properties) {
1377         DBusMessage *m;
1378         int r = -ENOMEM;
1379
1380         assert(manager);
1381
1382         m = bus_properties_changed_new("/org/freedesktop/login1", "org.freedesktop.login1.Manager", properties);
1383         if (!m)
1384                 goto finish;
1385
1386         if (!dbus_connection_send(manager->bus, m, NULL))
1387                 goto finish;
1388
1389         r = 0;
1390
1391 finish:
1392         if (m)
1393                 dbus_message_unref(m);
1394
1395         return r;
1396 }