chiark / gitweb /
2998c5817833f88357da3b8c61ae3d8e435d5c9e
[elogind.git] / src / dbus.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 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 <sys/epoll.h>
23 #include <sys/timerfd.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <dbus/dbus.h>
27
28 #include "dbus.h"
29 #include "log.h"
30 #include "strv.h"
31 #include "cgroup.h"
32 #include "dbus-unit.h"
33 #include "dbus-job.h"
34 #include "dbus-manager.h"
35 #include "dbus-service.h"
36 #include "dbus-socket.h"
37 #include "dbus-target.h"
38 #include "dbus-device.h"
39 #include "dbus-mount.h"
40 #include "dbus-automount.h"
41 #include "dbus-snapshot.h"
42 #include "dbus-swap.h"
43 #include "dbus-timer.h"
44 #include "dbus-path.h"
45 #include "bus-errors.h"
46
47 #define CONNECTIONS_MAX 52
48
49 static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
50 static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
51
52 const char *const bus_interface_table[] = {
53         "org.freedesktop.DBus.Properties",     bus_properties_interface,
54         "org.freedesktop.DBus.Introspectable", bus_introspectable_interface,
55         "org.freedesktop.systemd1.Manager",    bus_manager_interface,
56         "org.freedesktop.systemd1.Job",        bus_job_interface,
57         "org.freedesktop.systemd1.Unit",       bus_unit_interface,
58         "org.freedesktop.systemd1.Service",    bus_service_interface,
59         "org.freedesktop.systemd1.Socket",     bus_socket_interface,
60         "org.freedesktop.systemd1.Target",     bus_target_interface,
61         "org.freedesktop.systemd1.Device",     bus_device_interface,
62         "org.freedesktop.systemd1.Mount",      bus_mount_interface,
63         "org.freedesktop.systemd1.Automount",  bus_automount_interface,
64         "org.freedesktop.systemd1.Snapshot",   bus_snapshot_interface,
65         "org.freedesktop.systemd1.Swap",       bus_swap_interface,
66         "org.freedesktop.systemd1.Timer",      bus_timer_interface,
67         "org.freedesktop.systemd1.Path",       bus_path_interface,
68         NULL
69 };
70
71 static const char *error_to_dbus(int error);
72 static void bus_done_api(Manager *m);
73 static void bus_done_system(Manager *m);
74 static void bus_done_private(Manager *m);
75 static void shutdown_connection(Manager *m, DBusConnection *c);
76
77 static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data)  {
78         Manager *m = data;
79
80         assert(bus);
81         assert(m);
82
83         /* We maintain two sets, one for those connections where we
84          * requested a dispatch, and another where we didn't. And then,
85          * we move the connections between the two sets. */
86
87         if (status == DBUS_DISPATCH_COMPLETE)
88                 set_move_one(m->bus_connections, m->bus_connections_for_dispatch, bus);
89         else
90                 set_move_one(m->bus_connections_for_dispatch, m->bus_connections, bus);
91 }
92
93 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
94         unsigned flags;
95         uint32_t events = 0;
96
97         assert(bus_watch);
98
99         /* no watch flags for disabled watches */
100         if (!dbus_watch_get_enabled(bus_watch))
101                 return 0;
102
103         flags = dbus_watch_get_flags(bus_watch);
104
105         if (flags & DBUS_WATCH_READABLE)
106                 events |= EPOLLIN;
107         if (flags & DBUS_WATCH_WRITABLE)
108                 events |= EPOLLOUT;
109
110         return events | EPOLLHUP | EPOLLERR;
111 }
112
113 static unsigned events_to_bus_flags(uint32_t events) {
114         unsigned flags = 0;
115
116         if (events & EPOLLIN)
117                 flags |= DBUS_WATCH_READABLE;
118         if (events & EPOLLOUT)
119                 flags |= DBUS_WATCH_WRITABLE;
120         if (events & EPOLLHUP)
121                 flags |= DBUS_WATCH_HANGUP;
122         if (events & EPOLLERR)
123                 flags |= DBUS_WATCH_ERROR;
124
125         return flags;
126 }
127
128 void bus_watch_event(Manager *m, Watch *w, int events) {
129         assert(m);
130         assert(w);
131
132         /* This is called by the event loop whenever there is
133          * something happening on D-Bus' file handles. */
134
135         if (!dbus_watch_get_enabled(w->data.bus_watch))
136                 return;
137
138         dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
139 }
140
141 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
142         Manager *m = data;
143         Watch *w;
144         struct epoll_event ev;
145
146         assert(bus_watch);
147         assert(m);
148
149         if (!(w = new0(Watch, 1)))
150                 return FALSE;
151
152         w->fd = dbus_watch_get_unix_fd(bus_watch);
153         w->type = WATCH_DBUS_WATCH;
154         w->data.bus_watch = bus_watch;
155
156         zero(ev);
157         ev.events = bus_flags_to_events(bus_watch);
158         ev.data.ptr = w;
159
160         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
161
162                 if (errno != EEXIST) {
163                         free(w);
164                         return FALSE;
165                 }
166
167                 /* Hmm, bloody D-Bus creates multiple watches on the
168                  * same fd. epoll() does not like that. As a dirty
169                  * hack we simply dup() the fd and hence get a second
170                  * one we can safely add to the epoll(). */
171
172                 if ((w->fd = dup(w->fd)) < 0) {
173                         free(w);
174                         return FALSE;
175                 }
176
177                 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
178                         free(w);
179                         close_nointr_nofail(w->fd);
180                         return FALSE;
181                 }
182
183                 w->fd_is_dupped = true;
184         }
185
186         dbus_watch_set_data(bus_watch, w, NULL);
187
188         return TRUE;
189 }
190
191 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
192         Manager *m = data;
193         Watch *w;
194
195         assert(bus_watch);
196         assert(m);
197
198         if (!(w = dbus_watch_get_data(bus_watch)))
199                 return;
200
201         assert(w->type == WATCH_DBUS_WATCH);
202         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
203
204         if (w->fd_is_dupped)
205                 close_nointr_nofail(w->fd);
206
207         free(w);
208 }
209
210 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
211         Manager *m = data;
212         Watch *w;
213         struct epoll_event ev;
214
215         assert(bus_watch);
216         assert(m);
217
218         assert_se(w = dbus_watch_get_data(bus_watch));
219         assert(w->type == WATCH_DBUS_WATCH);
220
221         zero(ev);
222         ev.events = bus_flags_to_events(bus_watch);
223         ev.data.ptr = w;
224
225         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
226 }
227
228 static int bus_timeout_arm(Manager *m, Watch *w) {
229         struct itimerspec its;
230
231         assert(m);
232         assert(w);
233
234         zero(its);
235
236         if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
237                 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
238                 its.it_interval = its.it_interval;
239         }
240
241         if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
242                 return -errno;
243
244         return 0;
245 }
246
247 void bus_timeout_event(Manager *m, Watch *w, int events) {
248         assert(m);
249         assert(w);
250
251         /* This is called by the event loop whenever there is
252          * something happening on D-Bus' file handles. */
253
254         if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
255                 return;
256
257         dbus_timeout_handle(w->data.bus_timeout);
258 }
259
260 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
261         Manager *m = data;
262         Watch *w;
263         struct epoll_event ev;
264
265         assert(timeout);
266         assert(m);
267
268         if (!(w = new0(Watch, 1)))
269                 return FALSE;
270
271         if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
272                 goto fail;
273
274         w->type = WATCH_DBUS_TIMEOUT;
275         w->data.bus_timeout = timeout;
276
277         if (bus_timeout_arm(m, w) < 0)
278                 goto fail;
279
280         zero(ev);
281         ev.events = EPOLLIN;
282         ev.data.ptr = w;
283
284         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
285                 goto fail;
286
287         dbus_timeout_set_data(timeout, w, NULL);
288
289         return TRUE;
290
291 fail:
292         if (w->fd >= 0)
293                 close_nointr_nofail(w->fd);
294
295         free(w);
296         return FALSE;
297 }
298
299 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
300         Manager *m = data;
301         Watch *w;
302
303         assert(timeout);
304         assert(m);
305
306         if (!(w = dbus_timeout_get_data(timeout)))
307                 return;
308
309         assert(w->type == WATCH_DBUS_TIMEOUT);
310         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
311         close_nointr_nofail(w->fd);
312         free(w);
313 }
314
315 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
316         Manager *m = data;
317         Watch *w;
318         int r;
319
320         assert(timeout);
321         assert(m);
322
323         assert_se(w = dbus_timeout_get_data(timeout));
324         assert(w->type == WATCH_DBUS_TIMEOUT);
325
326         if ((r = bus_timeout_arm(m, w)) < 0)
327                 log_error("Failed to rearm timer: %s", strerror(-r));
328 }
329
330 static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
331         Manager *m = data;
332         DBusError error;
333         DBusMessage *reply = NULL;
334
335         assert(connection);
336         assert(message);
337         assert(m);
338
339         dbus_error_init(&error);
340
341         log_debug("Got D-Bus request: %s.%s() on %s",
342                   dbus_message_get_interface(message),
343                   dbus_message_get_member(message),
344                   dbus_message_get_path(message));
345
346         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
347                 log_error("Warning! API D-Bus connection terminated.");
348                 bus_done_api(m);
349
350         } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
351                 const char *name, *old_owner, *new_owner;
352
353                 if (!dbus_message_get_args(message, &error,
354                                            DBUS_TYPE_STRING, &name,
355                                            DBUS_TYPE_STRING, &old_owner,
356                                            DBUS_TYPE_STRING, &new_owner,
357                                            DBUS_TYPE_INVALID))
358                         log_error("Failed to parse NameOwnerChanged message: %s", error.message);
359                 else  {
360                         if (set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) name))
361                                 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(BUS_CONNECTION_SUBSCRIBED(m, connection)));
362
363                         if (old_owner[0] == 0)
364                                 old_owner = NULL;
365
366                         if (new_owner[0] == 0)
367                                 new_owner = NULL;
368
369                         manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
370                 }
371         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
372                 const char *name;
373
374                 if (!dbus_message_get_args(message, &error,
375                                            DBUS_TYPE_STRING, &name,
376                                            DBUS_TYPE_INVALID))
377                         log_error("Failed to parse ActivationRequest message: %s", error.message);
378                 else  {
379                         int r;
380                         Unit *u;
381
382                         log_debug("Got D-Bus activation request for %s", name);
383
384                         r = manager_load_unit(m, name, NULL, &error, &u);
385
386                         if (r >= 0 && u->meta.only_by_dependency)
387                                 r = -EPERM;
388
389                         if (r >= 0)
390                                 r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, &error, NULL);
391
392                         if (r < 0) {
393                                 const char *id, *text;
394
395                                 log_warning("D-Bus activation failed for %s: %s", name, strerror(-r));
396
397                                 if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
398                                         goto oom;
399
400                                 id = error.name ? error.name : error_to_dbus(r);
401                                 text = bus_error(&error, r);
402
403                                 if (!dbus_message_set_destination(reply, DBUS_SERVICE_DBUS) ||
404                                     !dbus_message_append_args(reply,
405                                                               DBUS_TYPE_STRING, &name,
406                                                               DBUS_TYPE_STRING, &id,
407                                                               DBUS_TYPE_STRING, &text,
408                                                               DBUS_TYPE_INVALID))
409                                         goto oom;
410                         }
411
412                         /* On success we don't do anything, the service will be spwaned now */
413                 }
414         }
415
416         dbus_error_free(&error);
417
418         if (reply) {
419                 if (!dbus_connection_send(connection, reply, NULL))
420                         goto oom;
421
422                 dbus_message_unref(reply);
423         }
424
425         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
426
427 oom:
428         if (reply)
429                 dbus_message_unref(reply);
430
431         dbus_error_free(&error);
432
433         return DBUS_HANDLER_RESULT_NEED_MEMORY;
434 }
435
436 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
437         Manager *m = data;
438         DBusError error;
439
440         assert(connection);
441         assert(message);
442         assert(m);
443
444         dbus_error_init(&error);
445
446         log_debug("Got D-Bus request: %s.%s() on %s",
447                   dbus_message_get_interface(message),
448                   dbus_message_get_member(message),
449                   dbus_message_get_path(message));
450
451         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
452                 log_error("Warning! System D-Bus connection terminated.");
453                 bus_done_system(m);
454
455         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
456                 const char *cgroup;
457
458                 if (!dbus_message_get_args(message, &error,
459                                            DBUS_TYPE_STRING, &cgroup,
460                                            DBUS_TYPE_INVALID))
461                         log_error("Failed to parse Released message: %s", error.message);
462                 else
463                         cgroup_notify_empty(m, cgroup);
464         }
465
466         dbus_error_free(&error);
467         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
468 }
469
470 static DBusHandlerResult private_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
471         Manager *m = data;
472
473         assert(connection);
474         assert(message);
475         assert(m);
476
477         log_debug("Got D-Bus request: %s.%s() on %s",
478                   dbus_message_get_interface(message),
479                   dbus_message_get_member(message),
480                   dbus_message_get_path(message));
481
482         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
483                 shutdown_connection(m, connection);
484
485         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
486 }
487
488 unsigned bus_dispatch(Manager *m) {
489         DBusConnection *c;
490
491         assert(m);
492
493         if (m->queued_message) {
494                 /* If we cannot get rid of this message we won't
495                  * dispatch any D-Bus messages, so that we won't end
496                  * up wanting to queue another message. */
497
498                 if (m->queued_message_connection)
499                         if (!dbus_connection_send(m->queued_message_connection, m->queued_message, NULL))
500                                 return 0;
501
502                 dbus_message_unref(m->queued_message);
503                 m->queued_message = NULL;
504                 m->queued_message_connection = NULL;
505         }
506
507         if ((c = set_first(m->bus_connections_for_dispatch))) {
508                 if (dbus_connection_dispatch(c) == DBUS_DISPATCH_COMPLETE)
509                         set_move_one(m->bus_connections, m->bus_connections_for_dispatch, c);
510
511                 return 1;
512         }
513
514         return 0;
515 }
516
517 static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
518         DBusMessage *reply;
519         DBusError error;
520
521         dbus_error_init(&error);
522
523         assert_se(reply = dbus_pending_call_steal_reply(pending));
524
525         switch (dbus_message_get_type(reply)) {
526
527         case DBUS_MESSAGE_TYPE_ERROR:
528
529                 assert_se(dbus_set_error_from_message(&error, reply));
530                 log_warning("RequestName() failed: %s", error.message);
531                 break;
532
533         case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
534                 uint32_t r;
535
536                 if (!dbus_message_get_args(reply,
537                                            &error,
538                                            DBUS_TYPE_UINT32, &r,
539                                            DBUS_TYPE_INVALID)) {
540                         log_error("Failed to parse RequestName() reply: %s", error.message);
541                         break;
542                 }
543
544                 if (r == 1)
545                         log_debug("Successfully acquired name.");
546                 else
547                         log_error("Name already owned.");
548
549                 break;
550         }
551
552         default:
553                 assert_not_reached("Invalid reply message");
554         }
555
556         dbus_message_unref(reply);
557         dbus_error_free(&error);
558 }
559
560 static int request_name(Manager *m) {
561         const char *name = "org.freedesktop.systemd1";
562         uint32_t flags = 0;
563         DBusMessage *message = NULL;
564         DBusPendingCall *pending = NULL;
565
566         if (!(message = dbus_message_new_method_call(
567                               DBUS_SERVICE_DBUS,
568                               DBUS_PATH_DBUS,
569                               DBUS_INTERFACE_DBUS,
570                               "RequestName")))
571                 goto oom;
572
573         if (!dbus_message_append_args(
574                             message,
575                             DBUS_TYPE_STRING, &name,
576                             DBUS_TYPE_UINT32, &flags,
577                             DBUS_TYPE_INVALID))
578                 goto oom;
579
580         if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
581                 goto oom;
582
583         if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
584                 goto oom;
585
586         dbus_message_unref(message);
587         dbus_pending_call_unref(pending);
588
589         /* We simple ask for the name and don't wait for it. Sooner or
590          * later we'll have it. */
591
592         return 0;
593
594 oom:
595         if (pending) {
596                 dbus_pending_call_cancel(pending);
597                 dbus_pending_call_unref(pending);
598         }
599
600         if (message)
601                 dbus_message_unref(message);
602
603         return -ENOMEM;
604 }
605
606 static void query_name_list_pending_cb(DBusPendingCall *pending, void *userdata) {
607         DBusMessage *reply;
608         DBusError error;
609         Manager *m = userdata;
610
611         assert(m);
612
613         dbus_error_init(&error);
614
615         assert_se(reply = dbus_pending_call_steal_reply(pending));
616
617         switch (dbus_message_get_type(reply)) {
618
619         case DBUS_MESSAGE_TYPE_ERROR:
620
621                 assert_se(dbus_set_error_from_message(&error, reply));
622                 log_warning("ListNames() failed: %s", error.message);
623                 break;
624
625         case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
626                 int r;
627                 char **l;
628
629                 if ((r = bus_parse_strv(reply, &l)) < 0)
630                         log_warning("Failed to parse ListNames() reply: %s", strerror(-r));
631                 else {
632                         char **t;
633
634                         STRV_FOREACH(t, l)
635                                 /* This is a bit hacky, we say the
636                                  * owner of the name is the name
637                                  * itself, because we don't want the
638                                  * extra traffic to figure out the
639                                  * real owner. */
640                                 manager_dispatch_bus_name_owner_changed(m, *t, NULL, *t);
641
642                         strv_free(l);
643                 }
644
645                 break;
646         }
647
648         default:
649                 assert_not_reached("Invalid reply message");
650         }
651
652         dbus_message_unref(reply);
653         dbus_error_free(&error);
654 }
655
656 static int query_name_list(Manager *m) {
657         DBusMessage *message = NULL;
658         DBusPendingCall *pending = NULL;
659
660         /* Asks for the currently installed bus names */
661
662         if (!(message = dbus_message_new_method_call(
663                               DBUS_SERVICE_DBUS,
664                               DBUS_PATH_DBUS,
665                               DBUS_INTERFACE_DBUS,
666                               "ListNames")))
667                 goto oom;
668
669         if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
670                 goto oom;
671
672         if (!dbus_pending_call_set_notify(pending, query_name_list_pending_cb, m, NULL))
673                 goto oom;
674
675         dbus_message_unref(message);
676         dbus_pending_call_unref(pending);
677
678         /* We simple ask for the list and don't wait for it. Sooner or
679          * later we'll get it. */
680
681         return 0;
682
683 oom:
684         if (pending) {
685                 dbus_pending_call_cancel(pending);
686                 dbus_pending_call_unref(pending);
687         }
688
689         if (message)
690                 dbus_message_unref(message);
691
692         return -ENOMEM;
693 }
694
695 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
696         assert(m);
697         assert(bus);
698
699         dbus_connection_set_exit_on_disconnect(bus, FALSE);
700
701         if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
702             !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
703                 log_error("Not enough memory");
704                 return -ENOMEM;
705         }
706
707         if (set_put(m->bus_connections_for_dispatch, bus) < 0) {
708                 log_error("Not enough memory");
709                 return -ENOMEM;
710         }
711
712         dbus_connection_set_dispatch_status_function(bus, bus_dispatch_status, m, NULL);
713         return 0;
714 }
715
716 static dbus_bool_t allow_only_root(DBusConnection *connection, unsigned long uid, void *data) {
717         return uid == 0;
718 }
719
720 static void bus_new_connection(
721                 DBusServer *server,
722                 DBusConnection *new_connection,
723                 void *data) {
724
725         Manager *m = data;
726
727         assert(m);
728
729         if (set_size(m->bus_connections) >= CONNECTIONS_MAX) {
730                 log_error("Too many concurrent connections.");
731                 return;
732         }
733
734         dbus_connection_set_unix_user_function(new_connection, allow_only_root, NULL, NULL);
735
736         if (bus_setup_loop(m, new_connection) < 0)
737                 return;
738
739         if (!dbus_connection_register_object_path(new_connection, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
740             !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
741             !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
742             !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) {
743                 log_error("Not enough memory.");
744                 return;
745         }
746
747         log_debug("Accepted connection on private bus.");
748
749         dbus_connection_ref(new_connection);
750 }
751
752 static int bus_init_system(Manager *m) {
753         DBusError error;
754         char *id;
755         int r;
756
757         assert(m);
758
759         dbus_error_init(&error);
760
761         if (m->system_bus)
762                 return 0;
763
764         if (m->running_as == MANAGER_SYSTEM && m->api_bus)
765                 m->system_bus = m->api_bus;
766         else {
767                 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
768                         log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
769                         r = 0;
770                         goto fail;
771                 }
772
773                 if ((r = bus_setup_loop(m, m->system_bus)) < 0)
774                         goto fail;
775         }
776
777         if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
778                 log_error("Not enough memory");
779                 r = -EIO;
780                 goto fail;
781         }
782
783         dbus_bus_add_match(m->system_bus,
784                            "type='signal',"
785                            "interface='org.freedesktop.systemd1.Agent',"
786                            "member='Released',"
787                            "path='/org/freedesktop/systemd1/agent'",
788                            &error);
789
790         if (dbus_error_is_set(&error)) {
791                 log_error("Failed to register match: %s", error.message);
792                 r = -EIO;
793                 goto fail;
794         }
795
796         log_info("Successfully connected to system D-Bus bus %s as %s",
797                  strnull((id = dbus_connection_get_server_id(m->system_bus))),
798                  strnull(dbus_bus_get_unique_name(m->system_bus)));
799         dbus_free(id);
800
801         return 0;
802
803 fail:
804         bus_done_system(m);
805         dbus_error_free(&error);
806
807         return r;
808 }
809
810 static int bus_init_api(Manager *m) {
811         DBusError error;
812         char *id;
813         int r;
814
815         assert(m);
816
817         dbus_error_init(&error);
818
819         if (m->api_bus)
820                 return 0;
821
822         if (m->running_as == MANAGER_SYSTEM && m->system_bus)
823                 m->api_bus = m->system_bus;
824         else {
825                 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
826                         log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
827                         r = 0;
828                         goto fail;
829                 }
830
831                 if ((r = bus_setup_loop(m, m->api_bus)) < 0)
832                         goto fail;
833         }
834
835         if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
836             !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
837             !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
838             !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
839                 log_error("Not enough memory");
840                 r = -ENOMEM;
841                 goto fail;
842         }
843
844         /* Get NameOwnerChange messages */
845         dbus_bus_add_match(m->api_bus,
846                            "type='signal',"
847                            "sender='"DBUS_SERVICE_DBUS"',"
848                            "interface='"DBUS_INTERFACE_DBUS"',"
849                            "member='NameOwnerChanged',"
850                            "path='"DBUS_PATH_DBUS"'",
851                            &error);
852
853         if (dbus_error_is_set(&error)) {
854                 log_error("Failed to register match: %s", error.message);
855                 r = -EIO;
856                 goto fail;
857         }
858
859         /* Get activation requests */
860         dbus_bus_add_match(m->api_bus,
861                            "type='signal',"
862                            "sender='"DBUS_SERVICE_DBUS"',"
863                            "interface='org.freedesktop.systemd1.Activator',"
864                            "member='ActivationRequest',"
865                            "path='"DBUS_PATH_DBUS"'",
866                            &error);
867
868         if (dbus_error_is_set(&error)) {
869                 log_error("Failed to register match: %s", error.message);
870                 r = -EIO;
871                 goto fail;
872         }
873
874         if ((r = request_name(m)) < 0)
875                 goto fail;
876
877         if ((r = query_name_list(m)) < 0)
878                 goto fail;
879
880         log_info("Successfully connected to API D-Bus bus %s as %s",
881                  strnull((id = dbus_connection_get_server_id(m->api_bus))),
882                  strnull(dbus_bus_get_unique_name(m->api_bus)));
883         dbus_free(id);
884
885         return 0;
886
887 fail:
888         bus_done_api(m);
889         dbus_error_free(&error);
890
891         return r;
892 }
893
894 static int bus_init_private(Manager *m) {
895         DBusError error;
896         int r;
897         const char *const external_only[] = {
898                 "EXTERNAL",
899                 NULL
900         };
901
902         assert(m);
903
904         dbus_error_init(&error);
905
906         if (m->private_bus)
907                 return 0;
908
909         /* We want the private bus only when running as init */
910         if (m->running_as != MANAGER_SYSTEM)
911                 return 0;
912
913         if (!(m->private_bus = dbus_server_listen("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
914                 log_error("Failed to create private D-Bus server: %s", error.message);
915                 r = -EIO;
916                 goto fail;
917         }
918
919         if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) ||
920             !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
921             !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
922                 log_error("Not enough memory");
923                 r = -ENOMEM;
924                 goto fail;
925         }
926
927         dbus_server_set_new_connection_function(m->private_bus, bus_new_connection, m, NULL);
928
929         log_debug("Successfully created private D-Bus server.");
930
931         return 0;
932
933 fail:
934         bus_done_private(m);
935         dbus_error_free(&error);
936
937         return r;
938 }
939
940 int bus_init(Manager *m) {
941         int r;
942
943         if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
944             set_ensure_allocated(&m->bus_connections_for_dispatch, trivial_hash_func, trivial_compare_func) < 0) {
945                 log_error("Not enough memory");
946                 return -ENOMEM;
947         }
948
949         if (m->name_data_slot < 0)
950                 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) {
951                         log_error("Not enough memory");
952                         return -ENOMEM;
953                 }
954
955         if (m->subscribed_data_slot < 0)
956                 if (!dbus_pending_call_allocate_data_slot(&m->subscribed_data_slot)) {
957                         log_error("Not enough memory");
958                         return -ENOMEM;
959                 }
960
961         if ((r = bus_init_system(m)) < 0 ||
962             (r = bus_init_api(m)) < 0 ||
963             (r = bus_init_private(m)) < 0)
964                 return r;
965
966         return 0;
967 }
968
969 static void shutdown_connection(Manager *m, DBusConnection *c) {
970         Set *s;
971         Job *j;
972         Iterator i;
973
974         HASHMAP_FOREACH(j, m->jobs, i)
975                 if (j->bus == c) {
976                         free(j->bus_client);
977                         j->bus_client = NULL;
978
979                         j->bus = NULL;
980                 }
981
982         set_remove(m->bus_connections, c);
983         set_remove(m->bus_connections_for_dispatch, c);
984
985         if ((s = BUS_CONNECTION_SUBSCRIBED(m, c))) {
986                 char *t;
987
988                 while ((t = set_steal_first(s)))
989                         free(t);
990
991                 set_free(s);
992         }
993
994         if (m->queued_message_connection == c) {
995                 m->queued_message_connection = NULL;
996
997                 if (m->queued_message) {
998                         dbus_message_unref(m->queued_message);
999                         m->queued_message = NULL;
1000                 }
1001         }
1002
1003         dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
1004         dbus_connection_flush(c);
1005         dbus_connection_close(c);
1006         dbus_connection_unref(c);
1007 }
1008
1009 static void bus_done_api(Manager *m) {
1010         assert(m);
1011
1012         if (m->api_bus) {
1013                 if (m->system_bus == m->api_bus)
1014                         m->system_bus = NULL;
1015
1016                 shutdown_connection(m, m->api_bus);
1017                 m->api_bus = NULL;
1018         }
1019
1020
1021        if (m->queued_message) {
1022                dbus_message_unref(m->queued_message);
1023                m->queued_message = NULL;
1024        }
1025 }
1026
1027 static void bus_done_system(Manager *m) {
1028         assert(m);
1029
1030         if (m->system_bus == m->api_bus)
1031                 bus_done_api(m);
1032
1033         if (m->system_bus) {
1034                 shutdown_connection(m, m->system_bus);
1035                 m->system_bus = NULL;
1036         }
1037 }
1038
1039 static void bus_done_private(Manager *m) {
1040
1041         if (m->private_bus) {
1042                 dbus_server_disconnect(m->private_bus);
1043                 dbus_server_unref(m->private_bus);
1044                 m->private_bus = NULL;
1045         }
1046 }
1047
1048 void bus_done(Manager *m) {
1049         DBusConnection *c;
1050
1051         bus_done_api(m);
1052         bus_done_system(m);
1053         bus_done_private(m);
1054
1055         while ((c = set_steal_first(m->bus_connections)))
1056                 shutdown_connection(m, c);
1057
1058         while ((c = set_steal_first(m->bus_connections_for_dispatch)))
1059                 shutdown_connection(m, c);
1060
1061         set_free(m->bus_connections);
1062         set_free(m->bus_connections_for_dispatch);
1063
1064         if (m->name_data_slot >= 0)
1065                dbus_pending_call_free_data_slot(&m->name_data_slot);
1066
1067         if (m->subscribed_data_slot >= 0)
1068                 dbus_pending_call_free_data_slot(&m->subscribed_data_slot);
1069 }
1070
1071 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
1072         Manager *m = userdata;
1073         DBusMessage *reply;
1074         DBusError error;
1075         const char *name;
1076
1077         dbus_error_init(&error);
1078
1079         assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
1080         assert_se(reply = dbus_pending_call_steal_reply(pending));
1081
1082         switch (dbus_message_get_type(reply)) {
1083
1084         case DBUS_MESSAGE_TYPE_ERROR:
1085
1086                 assert_se(dbus_set_error_from_message(&error, reply));
1087                 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
1088                 break;
1089
1090         case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
1091                 uint32_t r;
1092
1093                 if (!dbus_message_get_args(reply,
1094                                            &error,
1095                                            DBUS_TYPE_UINT32, &r,
1096                                            DBUS_TYPE_INVALID)) {
1097                         log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
1098                         break;
1099                 }
1100
1101                 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
1102                 break;
1103         }
1104
1105         default:
1106                 assert_not_reached("Invalid reply message");
1107         }
1108
1109         dbus_message_unref(reply);
1110         dbus_error_free(&error);
1111 }
1112
1113 int bus_query_pid(Manager *m, const char *name) {
1114         DBusMessage *message = NULL;
1115         DBusPendingCall *pending = NULL;
1116         char *n = NULL;
1117
1118         assert(m);
1119         assert(name);
1120
1121         if (!(message = dbus_message_new_method_call(
1122                               DBUS_SERVICE_DBUS,
1123                               DBUS_PATH_DBUS,
1124                               DBUS_INTERFACE_DBUS,
1125                               "GetConnectionUnixProcessID")))
1126                 goto oom;
1127
1128         if (!(dbus_message_append_args(
1129                               message,
1130                               DBUS_TYPE_STRING, &name,
1131                               DBUS_TYPE_INVALID)))
1132                 goto oom;
1133
1134         if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
1135                 goto oom;
1136
1137         if (!(n = strdup(name)))
1138                 goto oom;
1139
1140         if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
1141                 goto oom;
1142
1143         n = NULL;
1144
1145         if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
1146                 goto oom;
1147
1148         dbus_message_unref(message);
1149         dbus_pending_call_unref(pending);
1150
1151         return 0;
1152
1153 oom:
1154         free(n);
1155
1156         if (pending) {
1157                 dbus_pending_call_cancel(pending);
1158                 dbus_pending_call_unref(pending);
1159         }
1160
1161         if (message)
1162                 dbus_message_unref(message);
1163
1164         return -ENOMEM;
1165 }
1166
1167 DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char*introspection, const BusProperty *properties) {
1168         DBusError error;
1169         DBusMessage *reply = NULL;
1170         int r;
1171
1172         assert(m);
1173         assert(message);
1174
1175         dbus_error_init(&error);
1176
1177         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
1178
1179                 if (!(reply = dbus_message_new_method_return(message)))
1180                         goto oom;
1181
1182                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
1183                         goto oom;
1184
1185         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
1186                 const char *interface, *property;
1187                 const BusProperty *p;
1188
1189                 if (!dbus_message_get_args(
1190                             message,
1191                             &error,
1192                             DBUS_TYPE_STRING, &interface,
1193                             DBUS_TYPE_STRING, &property,
1194                             DBUS_TYPE_INVALID))
1195                         return bus_send_error_reply(m, c, message, &error, -EINVAL);
1196
1197                 for (p = properties; p->property; p++)
1198                         if (streq(p->interface, interface) && streq(p->property, property))
1199                                 break;
1200
1201                 if (p->property) {
1202                         DBusMessageIter iter, sub;
1203
1204                         if (!(reply = dbus_message_new_method_return(message)))
1205                                 goto oom;
1206
1207                         dbus_message_iter_init_append(reply, &iter);
1208
1209                         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
1210                                 goto oom;
1211
1212                         if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
1213
1214                                 if (r == -ENOMEM)
1215                                         goto oom;
1216
1217                                 dbus_message_unref(reply);
1218                                 return bus_send_error_reply(m, c, message, NULL, r);
1219                         }
1220
1221                         if (!dbus_message_iter_close_container(&iter, &sub))
1222                                 goto oom;
1223                 }
1224         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
1225                 const char *interface;
1226                 const BusProperty *p;
1227                 DBusMessageIter iter, sub, sub2, sub3;
1228
1229                 if (!dbus_message_get_args(
1230                             message,
1231                             &error,
1232                             DBUS_TYPE_STRING, &interface,
1233                             DBUS_TYPE_INVALID))
1234                         return bus_send_error_reply(m, c, message, &error, -EINVAL);
1235
1236                 if (!(reply = dbus_message_new_method_return(message)))
1237                         goto oom;
1238
1239                 dbus_message_iter_init_append(reply, &iter);
1240
1241                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
1242                         goto oom;
1243
1244                 for (p = properties; p->property; p++) {
1245                         if (interface[0] && !streq(p->interface, interface))
1246                                 continue;
1247
1248                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
1249                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
1250                             !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
1251                                 goto oom;
1252
1253                         if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
1254
1255                                 if (r == -ENOMEM)
1256                                         goto oom;
1257
1258                                 dbus_message_unref(reply);
1259                                 return bus_send_error_reply(m, c, message, NULL, r);
1260                         }
1261
1262                         if (!dbus_message_iter_close_container(&sub2, &sub3) ||
1263                             !dbus_message_iter_close_container(&sub, &sub2))
1264                                 goto oom;
1265                 }
1266
1267                 if (!dbus_message_iter_close_container(&iter, &sub))
1268                         goto oom;
1269         }
1270
1271         if (reply) {
1272                 if (!dbus_connection_send(c, reply, NULL))
1273                         goto oom;
1274
1275                 dbus_message_unref(reply);
1276                 return DBUS_HANDLER_RESULT_HANDLED;
1277         }
1278
1279         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1280
1281 oom:
1282         if (reply)
1283                 dbus_message_unref(reply);
1284
1285         dbus_error_free(&error);
1286
1287         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1288 }
1289
1290 static const char *error_to_dbus(int error) {
1291
1292         switch(error) {
1293
1294         case -EINVAL:
1295                 return DBUS_ERROR_INVALID_ARGS;
1296
1297         case -ENOMEM:
1298                 return DBUS_ERROR_NO_MEMORY;
1299
1300         case -EPERM:
1301         case -EACCES:
1302                 return DBUS_ERROR_ACCESS_DENIED;
1303
1304         case -ESRCH:
1305                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
1306
1307         case -ENOENT:
1308                 return DBUS_ERROR_FILE_NOT_FOUND;
1309
1310         case -EEXIST:
1311                 return DBUS_ERROR_FILE_EXISTS;
1312
1313         case -ETIMEDOUT:
1314                 return DBUS_ERROR_TIMEOUT;
1315
1316         case -EIO:
1317                 return DBUS_ERROR_IO_ERROR;
1318
1319         case -ENETRESET:
1320         case -ECONNABORTED:
1321         case -ECONNRESET:
1322                 return DBUS_ERROR_DISCONNECTED;
1323         }
1324
1325         return DBUS_ERROR_FAILED;
1326 }
1327
1328 DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
1329         DBusMessage *reply = NULL;
1330         const char *name, *text;
1331
1332         if (berror && dbus_error_is_set(berror)) {
1333                 name = berror->name;
1334                 text = berror->message;
1335         } else {
1336                 name = error_to_dbus(error);
1337                 text = strerror(-error);
1338         }
1339
1340         if (!(reply = dbus_message_new_error(message, name, text)))
1341                 goto oom;
1342
1343         if (!dbus_connection_send(c, reply, NULL))
1344                 goto oom;
1345
1346         dbus_message_unref(reply);
1347
1348         if (berror)
1349                 dbus_error_free(berror);
1350
1351         return DBUS_HANDLER_RESULT_HANDLED;
1352
1353 oom:
1354         if (reply)
1355                 dbus_message_unref(reply);
1356
1357         if (berror)
1358                 dbus_error_free(berror);
1359
1360         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1361 }
1362
1363 int bus_broadcast(Manager *m, DBusMessage *message) {
1364         bool oom = false;
1365         Iterator i;
1366         DBusConnection *c;
1367
1368         assert(m);
1369         assert(message);
1370
1371         SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1372                 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1373                         oom = !dbus_connection_send(c, message, NULL);
1374
1375         SET_FOREACH(c, m->bus_connections, i)
1376                 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1377                         oom = !dbus_connection_send(c, message, NULL);
1378
1379         return oom ? -ENOMEM : 0;
1380 }
1381
1382 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1383         const char *t = data;
1384
1385         assert(m);
1386         assert(i);
1387         assert(property);
1388
1389         if (!t)
1390                 t = "";
1391
1392         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
1393                 return -ENOMEM;
1394
1395         return 0;
1396 }
1397
1398 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1399         DBusMessageIter sub;
1400         char **t = data;
1401
1402         assert(m);
1403         assert(i);
1404         assert(property);
1405
1406         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
1407                 return -ENOMEM;
1408
1409         STRV_FOREACH(t, t)
1410                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
1411                         return -ENOMEM;
1412
1413         if (!dbus_message_iter_close_container(i, &sub))
1414                 return -ENOMEM;
1415
1416         return 0;
1417 }
1418
1419 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1420         bool *b = data;
1421         dbus_bool_t db;
1422
1423         assert(m);
1424         assert(i);
1425         assert(property);
1426         assert(b);
1427
1428         db = *b;
1429
1430         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1431                 return -ENOMEM;
1432
1433         return 0;
1434 }
1435
1436 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1437         assert(m);
1438         assert(i);
1439         assert(property);
1440         assert(data);
1441
1442         /* Let's ensure that pid_t is actually 64bit, and hence this
1443          * function can be used for usec_t */
1444         assert_cc(sizeof(uint64_t) == sizeof(usec_t));
1445
1446         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1447                 return -ENOMEM;
1448
1449         return 0;
1450 }
1451
1452 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1453         assert(m);
1454         assert(i);
1455         assert(property);
1456         assert(data);
1457
1458         /* Let's ensure that pid_t and mode_t is actually 32bit, and
1459          * hence this function can be used for pid_t/mode_t */
1460         assert_cc(sizeof(uint32_t) == sizeof(pid_t));
1461         assert_cc(sizeof(uint32_t) == sizeof(mode_t));
1462         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1463
1464         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
1465                 return -ENOMEM;
1466
1467         return 0;
1468 }
1469
1470 int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1471         assert(m);
1472         assert(i);
1473         assert(property);
1474         assert(data);
1475
1476         assert_cc(sizeof(int32_t) == sizeof(int));
1477
1478         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
1479                 return -ENOMEM;
1480
1481         return 0;
1482 }
1483
1484 int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1485         uint64_t u;
1486
1487         assert(m);
1488         assert(i);
1489         assert(property);
1490         assert(data);
1491
1492         u = (uint64_t) *(size_t*) data;
1493
1494         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1495                 return -ENOMEM;
1496
1497         return 0;
1498 }
1499
1500 int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1501         uint64_t u;
1502
1503         assert(m);
1504         assert(i);
1505         assert(property);
1506         assert(data);
1507
1508         u = (uint64_t) *(unsigned long*) data;
1509
1510         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1511                 return -ENOMEM;
1512
1513         return 0;
1514 }
1515
1516 int bus_parse_strv(DBusMessage *m, char ***_l) {
1517         DBusMessageIter iter, sub;
1518         unsigned n = 0, i = 0;
1519         char **l;
1520
1521         assert(m);
1522         assert(_l);
1523
1524         if (!dbus_message_iter_init(m, &iter) ||
1525             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1526             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1527             return -EINVAL;
1528
1529         dbus_message_iter_recurse(&iter, &sub);
1530
1531         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1532                 n++;
1533                 dbus_message_iter_next(&sub);
1534         }
1535
1536         if (!(l = new(char*, n+1)))
1537                 return -ENOMEM;
1538
1539         assert_se(dbus_message_iter_init(m, &iter));
1540         dbus_message_iter_recurse(&iter, &sub);
1541
1542         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1543                 const char *s;
1544
1545                 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1546                 dbus_message_iter_get_basic(&sub, &s);
1547
1548                 if (!(l[i++] = strdup(s))) {
1549                         strv_free(l);
1550                         return -ENOMEM;
1551                 }
1552
1553                 dbus_message_iter_next(&sub);
1554         }
1555
1556         assert(i == n);
1557         l[i] = NULL;
1558
1559         if (_l)
1560                 *_l = l;
1561
1562         return 0;
1563 }
1564
1565 bool bus_has_subscriber(Manager *m) {
1566         Iterator i;
1567         DBusConnection *c;
1568
1569         assert(m);
1570
1571         SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1572                 if (bus_connection_has_subscriber(m, c))
1573                         return true;
1574
1575         SET_FOREACH(c, m->bus_connections, i)
1576                 if (bus_connection_has_subscriber(m, c))
1577                         return true;
1578
1579         return false;
1580 }
1581
1582 bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) {
1583         assert(m);
1584         assert(c);
1585
1586         return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c));
1587 }