chiark / gitweb /
dbus: send signals about jobs to the clients having created them unconditionally...
[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
46 #define CONNECTIONS_MAX 52
47
48 static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE;
49 static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE;
50
51 const char *const bus_interface_table[] = {
52         "org.freedesktop.DBus.Properties",     bus_properties_interface,
53         "org.freedesktop.DBus.Introspectable", bus_introspectable_interface,
54         "org.freedesktop.systemd1.Manager",    bus_manager_interface,
55         "org.freedesktop.systemd1.Job",        bus_job_interface,
56         "org.freedesktop.systemd1.Unit",       bus_unit_interface,
57         "org.freedesktop.systemd1.Service",    bus_service_interface,
58         "org.freedesktop.systemd1.Socket",     bus_socket_interface,
59         "org.freedesktop.systemd1.Target",     bus_target_interface,
60         "org.freedesktop.systemd1.Device",     bus_device_interface,
61         "org.freedesktop.systemd1.Mount",      bus_mount_interface,
62         "org.freedesktop.systemd1.Automount",  bus_automount_interface,
63         "org.freedesktop.systemd1.Snapshot",   bus_snapshot_interface,
64         "org.freedesktop.systemd1.Swap",       bus_swap_interface,
65         "org.freedesktop.systemd1.Timer",      bus_timer_interface,
66         "org.freedesktop.systemd1.Path",       bus_path_interface,
67         NULL
68 };
69
70 static const char *error_to_dbus(int error);
71 static void bus_done_api(Manager *m);
72 static void bus_done_system(Manager *m);
73 static void bus_done_private(Manager *m);
74
75 static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data)  {
76         Manager *m = data;
77
78         assert(bus);
79         assert(m);
80
81         /* We maintain two sets, one for those connections where we
82          * requested a dispatch, and another where we didn't. And then,
83          * we move the connections between the two sets. */
84
85         if (status == DBUS_DISPATCH_COMPLETE)
86                 set_move_one(m->bus_connections, m->bus_connections_for_dispatch, bus);
87         else
88                 set_move_one(m->bus_connections_for_dispatch, m->bus_connections, bus);
89 }
90
91 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
92         unsigned flags;
93         uint32_t events = 0;
94
95         assert(bus_watch);
96
97         /* no watch flags for disabled watches */
98         if (!dbus_watch_get_enabled(bus_watch))
99                 return 0;
100
101         flags = dbus_watch_get_flags(bus_watch);
102
103         if (flags & DBUS_WATCH_READABLE)
104                 events |= EPOLLIN;
105         if (flags & DBUS_WATCH_WRITABLE)
106                 events |= EPOLLOUT;
107
108         return events | EPOLLHUP | EPOLLERR;
109 }
110
111 static unsigned events_to_bus_flags(uint32_t events) {
112         unsigned flags = 0;
113
114         if (events & EPOLLIN)
115                 flags |= DBUS_WATCH_READABLE;
116         if (events & EPOLLOUT)
117                 flags |= DBUS_WATCH_WRITABLE;
118         if (events & EPOLLHUP)
119                 flags |= DBUS_WATCH_HANGUP;
120         if (events & EPOLLERR)
121                 flags |= DBUS_WATCH_ERROR;
122
123         return flags;
124 }
125
126 void bus_watch_event(Manager *m, Watch *w, int events) {
127         assert(m);
128         assert(w);
129
130         /* This is called by the event loop whenever there is
131          * something happening on D-Bus' file handles. */
132
133         if (!dbus_watch_get_enabled(w->data.bus_watch))
134                 return;
135
136         dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
137 }
138
139 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
140         Manager *m = data;
141         Watch *w;
142         struct epoll_event ev;
143
144         assert(bus_watch);
145         assert(m);
146
147         if (!(w = new0(Watch, 1)))
148                 return FALSE;
149
150         w->fd = dbus_watch_get_unix_fd(bus_watch);
151         w->type = WATCH_DBUS_WATCH;
152         w->data.bus_watch = bus_watch;
153
154         zero(ev);
155         ev.events = bus_flags_to_events(bus_watch);
156         ev.data.ptr = w;
157
158         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
159
160                 if (errno != EEXIST) {
161                         free(w);
162                         return FALSE;
163                 }
164
165                 /* Hmm, bloody D-Bus creates multiple watches on the
166                  * same fd. epoll() does not like that. As a dirty
167                  * hack we simply dup() the fd and hence get a second
168                  * one we can safely add to the epoll(). */
169
170                 if ((w->fd = dup(w->fd)) < 0) {
171                         free(w);
172                         return FALSE;
173                 }
174
175                 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
176                         free(w);
177                         close_nointr_nofail(w->fd);
178                         return FALSE;
179                 }
180
181                 w->fd_is_dupped = true;
182         }
183
184         dbus_watch_set_data(bus_watch, w, NULL);
185
186         return TRUE;
187 }
188
189 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
190         Manager *m = data;
191         Watch *w;
192
193         assert(bus_watch);
194         assert(m);
195
196         if (!(w = dbus_watch_get_data(bus_watch)))
197                 return;
198
199         assert(w->type == WATCH_DBUS_WATCH);
200         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
201
202         if (w->fd_is_dupped)
203                 close_nointr_nofail(w->fd);
204
205         free(w);
206 }
207
208 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
209         Manager *m = data;
210         Watch *w;
211         struct epoll_event ev;
212
213         assert(bus_watch);
214         assert(m);
215
216         assert_se(w = dbus_watch_get_data(bus_watch));
217         assert(w->type == WATCH_DBUS_WATCH);
218
219         zero(ev);
220         ev.events = bus_flags_to_events(bus_watch);
221         ev.data.ptr = w;
222
223         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
224 }
225
226 static int bus_timeout_arm(Manager *m, Watch *w) {
227         struct itimerspec its;
228
229         assert(m);
230         assert(w);
231
232         zero(its);
233
234         if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
235                 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
236                 its.it_interval = its.it_interval;
237         }
238
239         if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
240                 return -errno;
241
242         return 0;
243 }
244
245 void bus_timeout_event(Manager *m, Watch *w, int events) {
246         assert(m);
247         assert(w);
248
249         /* This is called by the event loop whenever there is
250          * something happening on D-Bus' file handles. */
251
252         if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
253                 return;
254
255         dbus_timeout_handle(w->data.bus_timeout);
256 }
257
258 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
259         Manager *m = data;
260         Watch *w;
261         struct epoll_event ev;
262
263         assert(timeout);
264         assert(m);
265
266         if (!(w = new0(Watch, 1)))
267                 return FALSE;
268
269         if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
270                 goto fail;
271
272         w->type = WATCH_DBUS_TIMEOUT;
273         w->data.bus_timeout = timeout;
274
275         if (bus_timeout_arm(m, w) < 0)
276                 goto fail;
277
278         zero(ev);
279         ev.events = EPOLLIN;
280         ev.data.ptr = w;
281
282         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
283                 goto fail;
284
285         dbus_timeout_set_data(timeout, w, NULL);
286
287         return TRUE;
288
289 fail:
290         if (w->fd >= 0)
291                 close_nointr_nofail(w->fd);
292
293         free(w);
294         return FALSE;
295 }
296
297 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
298         Manager *m = data;
299         Watch *w;
300
301         assert(timeout);
302         assert(m);
303
304         if (!(w = dbus_timeout_get_data(timeout)))
305                 return;
306
307         assert(w->type == WATCH_DBUS_TIMEOUT);
308         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
309         close_nointr_nofail(w->fd);
310         free(w);
311 }
312
313 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
314         Manager *m = data;
315         Watch *w;
316         int r;
317
318         assert(timeout);
319         assert(m);
320
321         assert_se(w = dbus_timeout_get_data(timeout));
322         assert(w->type == WATCH_DBUS_TIMEOUT);
323
324         if ((r = bus_timeout_arm(m, w)) < 0)
325                 log_error("Failed to rearm timer: %s", strerror(-r));
326 }
327
328 static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
329         Manager *m = data;
330         DBusError error;
331         DBusMessage *reply = NULL;
332
333         assert(connection);
334         assert(message);
335         assert(m);
336
337         dbus_error_init(&error);
338
339         /* log_debug("Got D-Bus request: %s.%s() on %s", */
340         /*           dbus_message_get_interface(message), */
341         /*           dbus_message_get_member(message), */
342         /*           dbus_message_get_path(message)); */
343
344         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
345                 log_error("Warning! API D-Bus connection terminated.");
346                 bus_done_api(m);
347
348         } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
349                 const char *name, *old_owner, *new_owner;
350
351                 if (!dbus_message_get_args(message, &error,
352                                            DBUS_TYPE_STRING, &name,
353                                            DBUS_TYPE_STRING, &old_owner,
354                                            DBUS_TYPE_STRING, &new_owner,
355                                            DBUS_TYPE_INVALID))
356                         log_error("Failed to parse NameOwnerChanged message: %s", error.message);
357                 else  {
358                         if (set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) name))
359                                 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(BUS_CONNECTION_SUBSCRIBED(m, connection)));
360
361                         if (old_owner[0] == 0)
362                                 old_owner = NULL;
363
364                         if (new_owner[0] == 0)
365                                 new_owner = NULL;
366
367                         manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
368                 }
369         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
370                 const char *name;
371
372                 if (!dbus_message_get_args(message, &error,
373                                            DBUS_TYPE_STRING, &name,
374                                            DBUS_TYPE_INVALID))
375                         log_error("Failed to parse ActivationRequest message: %s", error.message);
376                 else  {
377                         int r;
378                         Unit *u;
379
380                         log_debug("Got D-Bus activation request for %s", name);
381
382                         r = manager_load_unit(m, name, NULL, &u);
383
384                         if (r >= 0 && u->meta.only_by_dependency)
385                                 r = -EPERM;
386
387                         if (r >= 0)
388                                 r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, NULL);
389
390                         if (r < 0) {
391                                 const char *id, *text;
392
393                                 log_warning("D-Bus activation failed for %s: %s", name, strerror(-r));
394
395                                 if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
396                                         goto oom;
397
398                                 id = error_to_dbus(r);
399                                 text = strerror(-r);
400
401                                 if (!dbus_message_set_destination(reply, DBUS_SERVICE_DBUS) ||
402                                     !dbus_message_append_args(reply,
403                                                               DBUS_TYPE_STRING, &name,
404                                                               DBUS_TYPE_STRING, &id,
405                                                               DBUS_TYPE_STRING, &text,
406                                                               DBUS_TYPE_INVALID))
407                                         goto oom;
408                         }
409
410                         /* On success we don't do anything, the service will be spwaned now */
411                 }
412         }
413
414         dbus_error_free(&error);
415
416         if (reply) {
417                 if (!dbus_connection_send(connection, reply, NULL))
418                         goto oom;
419
420                 dbus_message_unref(reply);
421         }
422
423         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
424
425 oom:
426         if (reply)
427                 dbus_message_unref(reply);
428
429         dbus_error_free(&error);
430
431         return DBUS_HANDLER_RESULT_NEED_MEMORY;
432 }
433
434 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
435         Manager *m = data;
436         DBusError error;
437
438         assert(connection);
439         assert(message);
440         assert(m);
441
442         dbus_error_init(&error);
443
444         /* log_debug("Got D-Bus request: %s.%s() on %s", */
445         /*           dbus_message_get_interface(message), */
446         /*           dbus_message_get_member(message), */
447         /*           dbus_message_get_path(message)); */
448
449         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
450                 log_error("Warning! System D-Bus connection terminated.");
451                 bus_done_system(m);
452
453         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
454                 const char *cgroup;
455
456                 if (!dbus_message_get_args(message, &error,
457                                            DBUS_TYPE_STRING, &cgroup,
458                                            DBUS_TYPE_INVALID))
459                         log_error("Failed to parse Released message: %s", error.message);
460                 else
461                         cgroup_notify_empty(m, cgroup);
462         }
463
464         dbus_error_free(&error);
465         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
466 }
467
468 static DBusHandlerResult private_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
469         Manager *m = data;
470
471         assert(connection);
472         assert(message);
473         assert(m);
474
475         /* log_debug("Got D-Bus request: %s.%s() on %s", */
476         /*           dbus_message_get_interface(message), */
477         /*           dbus_message_get_member(message), */
478         /*           dbus_message_get_path(message)); */
479
480         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
481                 set_remove(m->bus_connections, connection);
482                 set_remove(m->bus_connections_for_dispatch, connection);
483                 dbus_connection_unref(connection);
484         }
485
486         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
487 }
488
489 unsigned bus_dispatch(Manager *m) {
490         DBusConnection *c;
491
492         assert(m);
493
494         if (m->queued_message) {
495                 /* If we cannot get rid of this message we won't
496                  * dispatch any D-Bus messages, so that we won't end
497                  * up wanting to queue another message. */
498
499                 if (m->api_bus)
500                         if (!dbus_connection_send(m->api_bus, m->queued_message, NULL))
501                                 return 0;
502
503                 dbus_message_unref(m->queued_message);
504                 m->queued_message = 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_debug("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_debug("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 create 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         dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
995         dbus_connection_flush(c);
996         dbus_connection_close(c);
997         dbus_connection_unref(c);
998 }
999
1000 static void bus_done_api(Manager *m) {
1001         assert(m);
1002
1003         if (m->api_bus) {
1004                 if (m->system_bus == m->api_bus)
1005                         m->system_bus = NULL;
1006
1007                 shutdown_connection(m, m->api_bus);
1008                 m->api_bus = NULL;
1009         }
1010
1011
1012        if (m->queued_message) {
1013                dbus_message_unref(m->queued_message);
1014                m->queued_message = NULL;
1015        }
1016 }
1017
1018 static void bus_done_system(Manager *m) {
1019         assert(m);
1020
1021         if (m->system_bus == m->api_bus)
1022                 bus_done_api(m);
1023
1024         if (m->system_bus) {
1025                 shutdown_connection(m, m->system_bus);
1026                 m->system_bus = NULL;
1027         }
1028 }
1029
1030 static void bus_done_private(Manager *m) {
1031
1032         if (m->private_bus) {
1033                 dbus_server_disconnect(m->private_bus);
1034                 dbus_server_unref(m->private_bus);
1035                 m->private_bus = NULL;
1036         }
1037 }
1038
1039 void bus_done(Manager *m) {
1040         DBusConnection *c;
1041
1042         bus_done_api(m);
1043         bus_done_system(m);
1044         bus_done_private(m);
1045
1046         while ((c = set_steal_first(m->bus_connections)))
1047                 shutdown_connection(m, c);
1048
1049         while ((c = set_steal_first(m->bus_connections_for_dispatch)))
1050                 shutdown_connection(m, c);
1051
1052         set_free(m->bus_connections);
1053         set_free(m->bus_connections_for_dispatch);
1054
1055         if (m->name_data_slot >= 0)
1056                dbus_pending_call_free_data_slot(&m->name_data_slot);
1057
1058         if (m->subscribed_data_slot >= 0)
1059                 dbus_pending_call_free_data_slot(&m->subscribed_data_slot);
1060 }
1061
1062 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
1063         Manager *m = userdata;
1064         DBusMessage *reply;
1065         DBusError error;
1066         const char *name;
1067
1068         dbus_error_init(&error);
1069
1070         assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
1071         assert_se(reply = dbus_pending_call_steal_reply(pending));
1072
1073         switch (dbus_message_get_type(reply)) {
1074
1075         case DBUS_MESSAGE_TYPE_ERROR:
1076
1077                 assert_se(dbus_set_error_from_message(&error, reply));
1078                 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
1079                 break;
1080
1081         case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
1082                 uint32_t r;
1083
1084                 if (!dbus_message_get_args(reply,
1085                                            &error,
1086                                            DBUS_TYPE_UINT32, &r,
1087                                            DBUS_TYPE_INVALID)) {
1088                         log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
1089                         break;
1090                 }
1091
1092                 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
1093                 break;
1094         }
1095
1096         default:
1097                 assert_not_reached("Invalid reply message");
1098         }
1099
1100         dbus_message_unref(reply);
1101         dbus_error_free(&error);
1102 }
1103
1104 int bus_query_pid(Manager *m, const char *name) {
1105         DBusMessage *message = NULL;
1106         DBusPendingCall *pending = NULL;
1107         char *n = NULL;
1108
1109         assert(m);
1110         assert(name);
1111
1112         if (!(message = dbus_message_new_method_call(
1113                               DBUS_SERVICE_DBUS,
1114                               DBUS_PATH_DBUS,
1115                               DBUS_INTERFACE_DBUS,
1116                               "GetConnectionUnixProcessID")))
1117                 goto oom;
1118
1119         if (!(dbus_message_append_args(
1120                               message,
1121                               DBUS_TYPE_STRING, &name,
1122                               DBUS_TYPE_INVALID)))
1123                 goto oom;
1124
1125         if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
1126                 goto oom;
1127
1128         if (!(n = strdup(name)))
1129                 goto oom;
1130
1131         if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
1132                 goto oom;
1133
1134         n = NULL;
1135
1136         if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
1137                 goto oom;
1138
1139         dbus_message_unref(message);
1140         dbus_pending_call_unref(pending);
1141
1142         return 0;
1143
1144 oom:
1145         free(n);
1146
1147         if (pending) {
1148                 dbus_pending_call_cancel(pending);
1149                 dbus_pending_call_unref(pending);
1150         }
1151
1152         if (message)
1153                 dbus_message_unref(message);
1154
1155         return -ENOMEM;
1156 }
1157
1158 DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char*introspection, const BusProperty *properties) {
1159         DBusError error;
1160         DBusMessage *reply = NULL;
1161         int r;
1162
1163         assert(m);
1164         assert(message);
1165
1166         dbus_error_init(&error);
1167
1168         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
1169
1170                 if (!(reply = dbus_message_new_method_return(message)))
1171                         goto oom;
1172
1173                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
1174                         goto oom;
1175
1176         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
1177                 const char *interface, *property;
1178                 const BusProperty *p;
1179
1180                 if (!dbus_message_get_args(
1181                             message,
1182                             &error,
1183                             DBUS_TYPE_STRING, &interface,
1184                             DBUS_TYPE_STRING, &property,
1185                             DBUS_TYPE_INVALID))
1186                         return bus_send_error_reply(m, c, message, &error, -EINVAL);
1187
1188                 for (p = properties; p->property; p++)
1189                         if (streq(p->interface, interface) && streq(p->property, property))
1190                                 break;
1191
1192                 if (p->property) {
1193                         DBusMessageIter iter, sub;
1194
1195                         if (!(reply = dbus_message_new_method_return(message)))
1196                                 goto oom;
1197
1198                         dbus_message_iter_init_append(reply, &iter);
1199
1200                         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
1201                                 goto oom;
1202
1203                         if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
1204
1205                                 if (r == -ENOMEM)
1206                                         goto oom;
1207
1208                                 dbus_message_unref(reply);
1209                                 return bus_send_error_reply(m, c, message, NULL, r);
1210                         }
1211
1212                         if (!dbus_message_iter_close_container(&iter, &sub))
1213                                 goto oom;
1214                 }
1215         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
1216                 const char *interface;
1217                 const BusProperty *p;
1218                 DBusMessageIter iter, sub, sub2, sub3;
1219
1220                 if (!dbus_message_get_args(
1221                             message,
1222                             &error,
1223                             DBUS_TYPE_STRING, &interface,
1224                             DBUS_TYPE_INVALID))
1225                         return bus_send_error_reply(m, c, message, &error, -EINVAL);
1226
1227                 if (!(reply = dbus_message_new_method_return(message)))
1228                         goto oom;
1229
1230                 dbus_message_iter_init_append(reply, &iter);
1231
1232                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
1233                         goto oom;
1234
1235                 for (p = properties; p->property; p++) {
1236                         if (interface[0] && !streq(p->interface, interface))
1237                                 continue;
1238
1239                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
1240                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
1241                             !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
1242                                 goto oom;
1243
1244                         if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
1245
1246                                 if (r == -ENOMEM)
1247                                         goto oom;
1248
1249                                 dbus_message_unref(reply);
1250                                 return bus_send_error_reply(m, c, message, NULL, r);
1251                         }
1252
1253                         if (!dbus_message_iter_close_container(&sub2, &sub3) ||
1254                             !dbus_message_iter_close_container(&sub, &sub2))
1255                                 goto oom;
1256                 }
1257
1258                 if (!dbus_message_iter_close_container(&iter, &sub))
1259                         goto oom;
1260         }
1261
1262         if (reply) {
1263                 if (!dbus_connection_send(c, reply, NULL))
1264                         goto oom;
1265
1266                 dbus_message_unref(reply);
1267                 return DBUS_HANDLER_RESULT_HANDLED;
1268         }
1269
1270         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1271
1272 oom:
1273         if (reply)
1274                 dbus_message_unref(reply);
1275
1276         dbus_error_free(&error);
1277
1278         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1279 }
1280
1281 static const char *error_to_dbus(int error) {
1282
1283         switch(error) {
1284
1285         case -EINVAL:
1286                 return DBUS_ERROR_INVALID_ARGS;
1287
1288         case -ENOMEM:
1289                 return DBUS_ERROR_NO_MEMORY;
1290
1291         case -EPERM:
1292         case -EACCES:
1293                 return DBUS_ERROR_ACCESS_DENIED;
1294
1295         case -ESRCH:
1296                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
1297
1298         case -ENOENT:
1299                 return DBUS_ERROR_FILE_NOT_FOUND;
1300
1301         case -EEXIST:
1302                 return DBUS_ERROR_FILE_EXISTS;
1303
1304         case -ETIMEDOUT:
1305                 return DBUS_ERROR_TIMEOUT;
1306
1307         case -EIO:
1308                 return DBUS_ERROR_IO_ERROR;
1309
1310         case -ENETRESET:
1311         case -ECONNABORTED:
1312         case -ECONNRESET:
1313                 return DBUS_ERROR_DISCONNECTED;
1314         }
1315
1316         return DBUS_ERROR_FAILED;
1317 }
1318
1319 DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *bus_error, int error) {
1320         DBusMessage *reply = NULL;
1321         const char *name, *text;
1322
1323         if (bus_error && dbus_error_is_set(bus_error)) {
1324                 name = bus_error->name;
1325                 text = bus_error->message;
1326         } else {
1327                 name = error_to_dbus(error);
1328                 text = strerror(-error);
1329         }
1330
1331         if (!(reply = dbus_message_new_error(message, name, text)))
1332                 goto oom;
1333
1334         if (!dbus_connection_send(c, reply, NULL))
1335                 goto oom;
1336
1337         dbus_message_unref(reply);
1338
1339         if (bus_error)
1340                 dbus_error_free(bus_error);
1341
1342         return DBUS_HANDLER_RESULT_HANDLED;
1343
1344 oom:
1345         if (reply)
1346                 dbus_message_unref(reply);
1347
1348         if (bus_error)
1349                 dbus_error_free(bus_error);
1350
1351         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1352 }
1353
1354 int bus_broadcast(Manager *m, DBusMessage *message) {
1355         bool oom = false;
1356         Iterator i;
1357         DBusConnection *c;
1358
1359         assert(m);
1360         assert(message);
1361
1362         SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1363                 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1364                         oom = !dbus_connection_send(c, message, NULL);
1365
1366         SET_FOREACH(c, m->bus_connections, i)
1367                 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1368                         oom = !dbus_connection_send(c, message, NULL);
1369
1370         return oom ? -ENOMEM : 0;
1371 }
1372
1373 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1374         const char *t = data;
1375
1376         assert(m);
1377         assert(i);
1378         assert(property);
1379
1380         if (!t)
1381                 t = "";
1382
1383         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
1384                 return -ENOMEM;
1385
1386         return 0;
1387 }
1388
1389 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1390         DBusMessageIter sub;
1391         char **t = data;
1392
1393         assert(m);
1394         assert(i);
1395         assert(property);
1396
1397         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
1398                 return -ENOMEM;
1399
1400         STRV_FOREACH(t, t)
1401                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
1402                         return -ENOMEM;
1403
1404         if (!dbus_message_iter_close_container(i, &sub))
1405                 return -ENOMEM;
1406
1407         return 0;
1408 }
1409
1410 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1411         bool *b = data;
1412         dbus_bool_t db;
1413
1414         assert(m);
1415         assert(i);
1416         assert(property);
1417         assert(b);
1418
1419         db = *b;
1420
1421         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1422                 return -ENOMEM;
1423
1424         return 0;
1425 }
1426
1427 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1428         assert(m);
1429         assert(i);
1430         assert(property);
1431         assert(data);
1432
1433         /* Let's ensure that pid_t is actually 64bit, and hence this
1434          * function can be used for usec_t */
1435         assert_cc(sizeof(uint64_t) == sizeof(usec_t));
1436
1437         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1438                 return -ENOMEM;
1439
1440         return 0;
1441 }
1442
1443 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1444         assert(m);
1445         assert(i);
1446         assert(property);
1447         assert(data);
1448
1449         /* Let's ensure that pid_t and mode_t is actually 32bit, and
1450          * hence this function can be used for pid_t/mode_t */
1451         assert_cc(sizeof(uint32_t) == sizeof(pid_t));
1452         assert_cc(sizeof(uint32_t) == sizeof(mode_t));
1453         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1454
1455         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
1456                 return -ENOMEM;
1457
1458         return 0;
1459 }
1460
1461 int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1462         assert(m);
1463         assert(i);
1464         assert(property);
1465         assert(data);
1466
1467         assert_cc(sizeof(int32_t) == sizeof(int));
1468
1469         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
1470                 return -ENOMEM;
1471
1472         return 0;
1473 }
1474
1475 int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1476         uint64_t u;
1477
1478         assert(m);
1479         assert(i);
1480         assert(property);
1481         assert(data);
1482
1483         u = (uint64_t) *(size_t*) data;
1484
1485         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1486                 return -ENOMEM;
1487
1488         return 0;
1489 }
1490
1491 int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1492         uint64_t u;
1493
1494         assert(m);
1495         assert(i);
1496         assert(property);
1497         assert(data);
1498
1499         u = (uint64_t) *(unsigned long*) data;
1500
1501         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1502                 return -ENOMEM;
1503
1504         return 0;
1505 }
1506
1507 int bus_parse_strv(DBusMessage *m, char ***_l) {
1508         DBusMessageIter iter, sub;
1509         unsigned n = 0, i = 0;
1510         char **l;
1511
1512         assert(m);
1513         assert(_l);
1514
1515         if (!dbus_message_iter_init(m, &iter) ||
1516             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1517             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1518             return -EINVAL;
1519
1520         dbus_message_iter_recurse(&iter, &sub);
1521
1522         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1523                 n++;
1524                 dbus_message_iter_next(&sub);
1525         }
1526
1527         if (!(l = new(char*, n+1)))
1528                 return -ENOMEM;
1529
1530         assert_se(dbus_message_iter_init(m, &iter));
1531         dbus_message_iter_recurse(&iter, &sub);
1532
1533         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1534                 const char *s;
1535
1536                 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1537                 dbus_message_iter_get_basic(&sub, &s);
1538
1539                 if (!(l[i++] = strdup(s))) {
1540                         strv_free(l);
1541                         return -ENOMEM;
1542                 }
1543
1544                 dbus_message_iter_next(&sub);
1545         }
1546
1547         assert(i == n);
1548         l[i] = NULL;
1549
1550         if (_l)
1551                 *_l = l;
1552
1553         return 0;
1554 }
1555
1556 bool bus_has_subscriber(Manager *m) {
1557         Iterator i;
1558         DBusConnection *c;
1559
1560         assert(m);
1561
1562         SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1563                 if (bus_connection_has_subscriber(m, c))
1564                         return true;
1565
1566         SET_FOREACH(c, m->bus_connections, i)
1567                 if (bus_connection_has_subscriber(m, c))
1568                         return true;
1569
1570         return false;
1571 }
1572
1573 bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) {
1574         assert(m);
1575         assert(c);
1576
1577         return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c));
1578 }