chiark / gitweb /
job: make sure restart jobs are readded to the run queue after conversion to start...
[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         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
342             dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
343                 log_debug("Got D-Bus request: %s.%s() on %s",
344                           dbus_message_get_interface(message),
345                           dbus_message_get_member(message),
346                           dbus_message_get_path(message));
347
348         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
349                 log_error("Warning! API D-Bus connection terminated.");
350                 bus_done_api(m);
351
352         } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
353                 const char *name, *old_owner, *new_owner;
354
355                 if (!dbus_message_get_args(message, &error,
356                                            DBUS_TYPE_STRING, &name,
357                                            DBUS_TYPE_STRING, &old_owner,
358                                            DBUS_TYPE_STRING, &new_owner,
359                                            DBUS_TYPE_INVALID))
360                         log_error("Failed to parse NameOwnerChanged message: %s", error.message);
361                 else  {
362                         if (set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) name))
363                                 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(BUS_CONNECTION_SUBSCRIBED(m, connection)));
364
365                         if (old_owner[0] == 0)
366                                 old_owner = NULL;
367
368                         if (new_owner[0] == 0)
369                                 new_owner = NULL;
370
371                         manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
372                 }
373         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Activator", "ActivationRequest")) {
374                 const char *name;
375
376                 if (!dbus_message_get_args(message, &error,
377                                            DBUS_TYPE_STRING, &name,
378                                            DBUS_TYPE_INVALID))
379                         log_error("Failed to parse ActivationRequest message: %s", error.message);
380                 else  {
381                         int r;
382                         Unit *u;
383
384                         log_debug("Got D-Bus activation request for %s", name);
385
386                         r = manager_load_unit(m, name, NULL, &error, &u);
387
388                         if (r >= 0 && u->meta.only_by_dependency)
389                                 r = -EPERM;
390
391                         if (r >= 0)
392                                 r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, &error, NULL);
393
394                         if (r < 0) {
395                                 const char *id, *text;
396
397                                 log_warning("D-Bus activation failed for %s: %s", name, strerror(-r));
398
399                                 if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure")))
400                                         goto oom;
401
402                                 id = error.name ? error.name : error_to_dbus(r);
403                                 text = bus_error(&error, r);
404
405                                 if (!dbus_message_set_destination(reply, DBUS_SERVICE_DBUS) ||
406                                     !dbus_message_append_args(reply,
407                                                               DBUS_TYPE_STRING, &name,
408                                                               DBUS_TYPE_STRING, &id,
409                                                               DBUS_TYPE_STRING, &text,
410                                                               DBUS_TYPE_INVALID))
411                                         goto oom;
412                         }
413
414                         /* On success we don't do anything, the service will be spwaned now */
415                 }
416         }
417
418         dbus_error_free(&error);
419
420         if (reply) {
421                 if (!dbus_connection_send(connection, reply, NULL))
422                         goto oom;
423
424                 dbus_message_unref(reply);
425         }
426
427         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
428
429 oom:
430         if (reply)
431                 dbus_message_unref(reply);
432
433         dbus_error_free(&error);
434
435         return DBUS_HANDLER_RESULT_NEED_MEMORY;
436 }
437
438 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
439         Manager *m = data;
440         DBusError error;
441
442         assert(connection);
443         assert(message);
444         assert(m);
445
446         dbus_error_init(&error);
447
448         if (m->api_bus != m->system_bus &&
449             (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
450              dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL))
451                 log_debug("Got D-Bus request on system bus: %s.%s() on %s",
452                           dbus_message_get_interface(message),
453                           dbus_message_get_member(message),
454                           dbus_message_get_path(message));
455
456         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
457                 log_error("Warning! System D-Bus connection terminated.");
458                 bus_done_system(m);
459
460         } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
461                 const char *cgroup;
462
463                 if (!dbus_message_get_args(message, &error,
464                                            DBUS_TYPE_STRING, &cgroup,
465                                            DBUS_TYPE_INVALID))
466                         log_error("Failed to parse Released message: %s", error.message);
467                 else
468                         cgroup_notify_empty(m, cgroup);
469         }
470
471         dbus_error_free(&error);
472         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
473 }
474
475 static DBusHandlerResult private_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
476         Manager *m = data;
477
478         assert(connection);
479         assert(message);
480         assert(m);
481
482         if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL ||
483             dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_SIGNAL)
484                 log_debug("Got D-Bus request: %s.%s() on %s",
485                           dbus_message_get_interface(message),
486                           dbus_message_get_member(message),
487                           dbus_message_get_path(message));
488
489         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
490                 shutdown_connection(m, connection);
491
492         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
493 }
494
495 unsigned bus_dispatch(Manager *m) {
496         DBusConnection *c;
497
498         assert(m);
499
500         if (m->queued_message) {
501                 /* If we cannot get rid of this message we won't
502                  * dispatch any D-Bus messages, so that we won't end
503                  * up wanting to queue another message. */
504
505                 if (m->queued_message_connection)
506                         if (!dbus_connection_send(m->queued_message_connection, m->queued_message, NULL))
507                                 return 0;
508
509                 dbus_message_unref(m->queued_message);
510                 m->queued_message = NULL;
511                 m->queued_message_connection = NULL;
512         }
513
514         if ((c = set_first(m->bus_connections_for_dispatch))) {
515                 if (dbus_connection_dispatch(c) == DBUS_DISPATCH_COMPLETE)
516                         set_move_one(m->bus_connections, m->bus_connections_for_dispatch, c);
517
518                 return 1;
519         }
520
521         return 0;
522 }
523
524 static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
525         DBusMessage *reply;
526         DBusError error;
527
528         dbus_error_init(&error);
529
530         assert_se(reply = dbus_pending_call_steal_reply(pending));
531
532         switch (dbus_message_get_type(reply)) {
533
534         case DBUS_MESSAGE_TYPE_ERROR:
535
536                 assert_se(dbus_set_error_from_message(&error, reply));
537                 log_warning("RequestName() failed: %s", error.message);
538                 break;
539
540         case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
541                 uint32_t r;
542
543                 if (!dbus_message_get_args(reply,
544                                            &error,
545                                            DBUS_TYPE_UINT32, &r,
546                                            DBUS_TYPE_INVALID)) {
547                         log_error("Failed to parse RequestName() reply: %s", error.message);
548                         break;
549                 }
550
551                 if (r == 1)
552                         log_debug("Successfully acquired name.");
553                 else
554                         log_error("Name already owned.");
555
556                 break;
557         }
558
559         default:
560                 assert_not_reached("Invalid reply message");
561         }
562
563         dbus_message_unref(reply);
564         dbus_error_free(&error);
565 }
566
567 static int request_name(Manager *m) {
568         const char *name = "org.freedesktop.systemd1";
569         uint32_t flags = 0;
570         DBusMessage *message = NULL;
571         DBusPendingCall *pending = NULL;
572
573         if (!(message = dbus_message_new_method_call(
574                               DBUS_SERVICE_DBUS,
575                               DBUS_PATH_DBUS,
576                               DBUS_INTERFACE_DBUS,
577                               "RequestName")))
578                 goto oom;
579
580         if (!dbus_message_append_args(
581                             message,
582                             DBUS_TYPE_STRING, &name,
583                             DBUS_TYPE_UINT32, &flags,
584                             DBUS_TYPE_INVALID))
585                 goto oom;
586
587         if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
588                 goto oom;
589
590         if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
591                 goto oom;
592
593         dbus_message_unref(message);
594         dbus_pending_call_unref(pending);
595
596         /* We simple ask for the name and don't wait for it. Sooner or
597          * later we'll have it. */
598
599         return 0;
600
601 oom:
602         if (pending) {
603                 dbus_pending_call_cancel(pending);
604                 dbus_pending_call_unref(pending);
605         }
606
607         if (message)
608                 dbus_message_unref(message);
609
610         return -ENOMEM;
611 }
612
613 static void query_name_list_pending_cb(DBusPendingCall *pending, void *userdata) {
614         DBusMessage *reply;
615         DBusError error;
616         Manager *m = userdata;
617
618         assert(m);
619
620         dbus_error_init(&error);
621
622         assert_se(reply = dbus_pending_call_steal_reply(pending));
623
624         switch (dbus_message_get_type(reply)) {
625
626         case DBUS_MESSAGE_TYPE_ERROR:
627
628                 assert_se(dbus_set_error_from_message(&error, reply));
629                 log_warning("ListNames() failed: %s", error.message);
630                 break;
631
632         case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
633                 int r;
634                 char **l;
635
636                 if ((r = bus_parse_strv(reply, &l)) < 0)
637                         log_warning("Failed to parse ListNames() reply: %s", strerror(-r));
638                 else {
639                         char **t;
640
641                         STRV_FOREACH(t, l)
642                                 /* This is a bit hacky, we say the
643                                  * owner of the name is the name
644                                  * itself, because we don't want the
645                                  * extra traffic to figure out the
646                                  * real owner. */
647                                 manager_dispatch_bus_name_owner_changed(m, *t, NULL, *t);
648
649                         strv_free(l);
650                 }
651
652                 break;
653         }
654
655         default:
656                 assert_not_reached("Invalid reply message");
657         }
658
659         dbus_message_unref(reply);
660         dbus_error_free(&error);
661 }
662
663 static int query_name_list(Manager *m) {
664         DBusMessage *message = NULL;
665         DBusPendingCall *pending = NULL;
666
667         /* Asks for the currently installed bus names */
668
669         if (!(message = dbus_message_new_method_call(
670                               DBUS_SERVICE_DBUS,
671                               DBUS_PATH_DBUS,
672                               DBUS_INTERFACE_DBUS,
673                               "ListNames")))
674                 goto oom;
675
676         if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
677                 goto oom;
678
679         if (!dbus_pending_call_set_notify(pending, query_name_list_pending_cb, m, NULL))
680                 goto oom;
681
682         dbus_message_unref(message);
683         dbus_pending_call_unref(pending);
684
685         /* We simple ask for the list and don't wait for it. Sooner or
686          * later we'll get it. */
687
688         return 0;
689
690 oom:
691         if (pending) {
692                 dbus_pending_call_cancel(pending);
693                 dbus_pending_call_unref(pending);
694         }
695
696         if (message)
697                 dbus_message_unref(message);
698
699         return -ENOMEM;
700 }
701
702 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
703         assert(m);
704         assert(bus);
705
706         dbus_connection_set_exit_on_disconnect(bus, FALSE);
707
708         if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
709             !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
710                 log_error("Not enough memory");
711                 return -ENOMEM;
712         }
713
714         if (set_put(m->bus_connections_for_dispatch, bus) < 0) {
715                 log_error("Not enough memory");
716                 return -ENOMEM;
717         }
718
719         dbus_connection_set_dispatch_status_function(bus, bus_dispatch_status, m, NULL);
720         return 0;
721 }
722
723 static dbus_bool_t allow_only_root(DBusConnection *connection, unsigned long uid, void *data) {
724         return uid == 0;
725 }
726
727 static void bus_new_connection(
728                 DBusServer *server,
729                 DBusConnection *new_connection,
730                 void *data) {
731
732         Manager *m = data;
733
734         assert(m);
735
736         if (set_size(m->bus_connections) >= CONNECTIONS_MAX) {
737                 log_error("Too many concurrent connections.");
738                 return;
739         }
740
741         dbus_connection_set_unix_user_function(new_connection, allow_only_root, NULL, NULL);
742
743         if (bus_setup_loop(m, new_connection) < 0)
744                 return;
745
746         if (!dbus_connection_register_object_path(new_connection, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
747             !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
748             !dbus_connection_register_fallback(new_connection, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
749             !dbus_connection_add_filter(new_connection, private_bus_message_filter, m, NULL)) {
750                 log_error("Not enough memory.");
751                 return;
752         }
753
754         log_debug("Accepted connection on private bus.");
755
756         dbus_connection_ref(new_connection);
757 }
758
759 static int bus_init_system(Manager *m) {
760         DBusError error;
761         int r;
762
763         assert(m);
764
765         dbus_error_init(&error);
766
767         if (m->system_bus)
768                 return 0;
769
770         if (m->running_as == MANAGER_SYSTEM && m->api_bus)
771                 m->system_bus = m->api_bus;
772         else {
773                 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
774                         log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
775                         r = 0;
776                         goto fail;
777                 }
778
779                 if ((r = bus_setup_loop(m, m->system_bus)) < 0)
780                         goto fail;
781         }
782
783         if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
784                 log_error("Not enough memory");
785                 r = -EIO;
786                 goto fail;
787         }
788
789         dbus_bus_add_match(m->system_bus,
790                            "type='signal',"
791                            "interface='org.freedesktop.systemd1.Agent',"
792                            "member='Released',"
793                            "path='/org/freedesktop/systemd1/agent'",
794                            &error);
795
796         if (dbus_error_is_set(&error)) {
797                 log_error("Failed to register match: %s", error.message);
798                 r = -EIO;
799                 goto fail;
800         }
801
802         if (m->api_bus != m->system_bus) {
803                 char *id;
804                 log_info("Successfully connected to system D-Bus bus %s as %s",
805                          strnull((id = dbus_connection_get_server_id(m->system_bus))),
806                          strnull(dbus_bus_get_unique_name(m->system_bus)));
807                 dbus_free(id);
808         }
809
810         return 0;
811
812 fail:
813         bus_done_system(m);
814         dbus_error_free(&error);
815
816         return r;
817 }
818
819 static int bus_init_api(Manager *m) {
820         DBusError error;
821         int r;
822
823         assert(m);
824
825         dbus_error_init(&error);
826
827         if (m->api_bus)
828                 return 0;
829
830         if (m->running_as == MANAGER_SYSTEM && m->system_bus)
831                 m->api_bus = m->system_bus;
832         else {
833                 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
834                         log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
835                         r = 0;
836                         goto fail;
837                 }
838
839                 if ((r = bus_setup_loop(m, m->api_bus)) < 0)
840                         goto fail;
841         }
842
843         if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
844             !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
845             !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
846             !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
847                 log_error("Not enough memory");
848                 r = -ENOMEM;
849                 goto fail;
850         }
851
852         /* Get NameOwnerChange messages */
853         dbus_bus_add_match(m->api_bus,
854                            "type='signal',"
855                            "sender='"DBUS_SERVICE_DBUS"',"
856                            "interface='"DBUS_INTERFACE_DBUS"',"
857                            "member='NameOwnerChanged',"
858                            "path='"DBUS_PATH_DBUS"'",
859                            &error);
860
861         if (dbus_error_is_set(&error)) {
862                 log_error("Failed to register match: %s", error.message);
863                 r = -EIO;
864                 goto fail;
865         }
866
867         /* Get activation requests */
868         dbus_bus_add_match(m->api_bus,
869                            "type='signal',"
870                            "sender='"DBUS_SERVICE_DBUS"',"
871                            "interface='org.freedesktop.systemd1.Activator',"
872                            "member='ActivationRequest',"
873                            "path='"DBUS_PATH_DBUS"'",
874                            &error);
875
876         if (dbus_error_is_set(&error)) {
877                 log_error("Failed to register match: %s", error.message);
878                 r = -EIO;
879                 goto fail;
880         }
881
882         if ((r = request_name(m)) < 0)
883                 goto fail;
884
885         if ((r = query_name_list(m)) < 0)
886                 goto fail;
887
888         if (m->api_bus != m->system_bus) {
889                 char *id;
890                 log_info("Successfully connected to API D-Bus bus %s as %s",
891                          strnull((id = dbus_connection_get_server_id(m->api_bus))),
892                          strnull(dbus_bus_get_unique_name(m->api_bus)));
893                 dbus_free(id);
894         }
895
896         return 0;
897
898 fail:
899         bus_done_api(m);
900         dbus_error_free(&error);
901
902         return r;
903 }
904
905 static int bus_init_private(Manager *m) {
906         DBusError error;
907         int r;
908         const char *const external_only[] = {
909                 "EXTERNAL",
910                 NULL
911         };
912
913         assert(m);
914
915         dbus_error_init(&error);
916
917         if (m->private_bus)
918                 return 0;
919
920         /* We want the private bus only when running as init */
921         if (getpid() != 1)
922                 return 0;
923
924         if (!(m->private_bus = dbus_server_listen("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
925                 log_error("Failed to create private D-Bus server: %s", error.message);
926                 r = -EIO;
927                 goto fail;
928         }
929
930         if (!dbus_server_set_auth_mechanisms(m->private_bus, (const char**) external_only) ||
931             !dbus_server_set_watch_functions(m->private_bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
932             !dbus_server_set_timeout_functions(m->private_bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) {
933                 log_error("Not enough memory");
934                 r = -ENOMEM;
935                 goto fail;
936         }
937
938         dbus_server_set_new_connection_function(m->private_bus, bus_new_connection, m, NULL);
939
940         log_debug("Successfully created private D-Bus server.");
941
942         return 0;
943
944 fail:
945         bus_done_private(m);
946         dbus_error_free(&error);
947
948         return r;
949 }
950
951 int bus_init(Manager *m) {
952         int r;
953
954         if (set_ensure_allocated(&m->bus_connections, trivial_hash_func, trivial_compare_func) < 0 ||
955             set_ensure_allocated(&m->bus_connections_for_dispatch, trivial_hash_func, trivial_compare_func) < 0) {
956                 log_error("Not enough memory");
957                 return -ENOMEM;
958         }
959
960         if (m->name_data_slot < 0)
961                 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) {
962                         log_error("Not enough memory");
963                         return -ENOMEM;
964                 }
965
966         if (m->subscribed_data_slot < 0)
967                 if (!dbus_pending_call_allocate_data_slot(&m->subscribed_data_slot)) {
968                         log_error("Not enough memory");
969                         return -ENOMEM;
970                 }
971
972         if ((r = bus_init_system(m)) < 0 ||
973             (r = bus_init_api(m)) < 0 ||
974             (r = bus_init_private(m)) < 0)
975                 return r;
976
977         return 0;
978 }
979
980 static void shutdown_connection(Manager *m, DBusConnection *c) {
981         Set *s;
982         Job *j;
983         Iterator i;
984
985         HASHMAP_FOREACH(j, m->jobs, i)
986                 if (j->bus == c) {
987                         free(j->bus_client);
988                         j->bus_client = NULL;
989
990                         j->bus = NULL;
991                 }
992
993         set_remove(m->bus_connections, c);
994         set_remove(m->bus_connections_for_dispatch, c);
995
996         if ((s = BUS_CONNECTION_SUBSCRIBED(m, c))) {
997                 char *t;
998
999                 while ((t = set_steal_first(s)))
1000                         free(t);
1001
1002                 set_free(s);
1003         }
1004
1005         if (m->queued_message_connection == c) {
1006                 m->queued_message_connection = NULL;
1007
1008                 if (m->queued_message) {
1009                         dbus_message_unref(m->queued_message);
1010                         m->queued_message = NULL;
1011                 }
1012         }
1013
1014         dbus_connection_set_dispatch_status_function(c, NULL, NULL, NULL);
1015         dbus_connection_flush(c);
1016         dbus_connection_close(c);
1017         dbus_connection_unref(c);
1018 }
1019
1020 static void bus_done_api(Manager *m) {
1021         assert(m);
1022
1023         if (m->api_bus) {
1024                 if (m->system_bus == m->api_bus)
1025                         m->system_bus = NULL;
1026
1027                 shutdown_connection(m, m->api_bus);
1028                 m->api_bus = NULL;
1029         }
1030
1031
1032        if (m->queued_message) {
1033                dbus_message_unref(m->queued_message);
1034                m->queued_message = NULL;
1035        }
1036 }
1037
1038 static void bus_done_system(Manager *m) {
1039         assert(m);
1040
1041         if (m->system_bus == m->api_bus)
1042                 bus_done_api(m);
1043
1044         if (m->system_bus) {
1045                 shutdown_connection(m, m->system_bus);
1046                 m->system_bus = NULL;
1047         }
1048 }
1049
1050 static void bus_done_private(Manager *m) {
1051
1052         if (m->private_bus) {
1053                 dbus_server_disconnect(m->private_bus);
1054                 dbus_server_unref(m->private_bus);
1055                 m->private_bus = NULL;
1056         }
1057 }
1058
1059 void bus_done(Manager *m) {
1060         DBusConnection *c;
1061
1062         bus_done_api(m);
1063         bus_done_system(m);
1064         bus_done_private(m);
1065
1066         while ((c = set_steal_first(m->bus_connections)))
1067                 shutdown_connection(m, c);
1068
1069         while ((c = set_steal_first(m->bus_connections_for_dispatch)))
1070                 shutdown_connection(m, c);
1071
1072         set_free(m->bus_connections);
1073         set_free(m->bus_connections_for_dispatch);
1074
1075         if (m->name_data_slot >= 0)
1076                dbus_pending_call_free_data_slot(&m->name_data_slot);
1077
1078         if (m->subscribed_data_slot >= 0)
1079                 dbus_pending_call_free_data_slot(&m->subscribed_data_slot);
1080 }
1081
1082 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
1083         Manager *m = userdata;
1084         DBusMessage *reply;
1085         DBusError error;
1086         const char *name;
1087
1088         dbus_error_init(&error);
1089
1090         assert_se(name = BUS_PENDING_CALL_NAME(m, pending));
1091         assert_se(reply = dbus_pending_call_steal_reply(pending));
1092
1093         switch (dbus_message_get_type(reply)) {
1094
1095         case DBUS_MESSAGE_TYPE_ERROR:
1096
1097                 assert_se(dbus_set_error_from_message(&error, reply));
1098                 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
1099                 break;
1100
1101         case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
1102                 uint32_t r;
1103
1104                 if (!dbus_message_get_args(reply,
1105                                            &error,
1106                                            DBUS_TYPE_UINT32, &r,
1107                                            DBUS_TYPE_INVALID)) {
1108                         log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
1109                         break;
1110                 }
1111
1112                 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
1113                 break;
1114         }
1115
1116         default:
1117                 assert_not_reached("Invalid reply message");
1118         }
1119
1120         dbus_message_unref(reply);
1121         dbus_error_free(&error);
1122 }
1123
1124 int bus_query_pid(Manager *m, const char *name) {
1125         DBusMessage *message = NULL;
1126         DBusPendingCall *pending = NULL;
1127         char *n = NULL;
1128
1129         assert(m);
1130         assert(name);
1131
1132         if (!(message = dbus_message_new_method_call(
1133                               DBUS_SERVICE_DBUS,
1134                               DBUS_PATH_DBUS,
1135                               DBUS_INTERFACE_DBUS,
1136                               "GetConnectionUnixProcessID")))
1137                 goto oom;
1138
1139         if (!(dbus_message_append_args(
1140                               message,
1141                               DBUS_TYPE_STRING, &name,
1142                               DBUS_TYPE_INVALID)))
1143                 goto oom;
1144
1145         if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
1146                 goto oom;
1147
1148         if (!(n = strdup(name)))
1149                 goto oom;
1150
1151         if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
1152                 goto oom;
1153
1154         n = NULL;
1155
1156         if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
1157                 goto oom;
1158
1159         dbus_message_unref(message);
1160         dbus_pending_call_unref(pending);
1161
1162         return 0;
1163
1164 oom:
1165         free(n);
1166
1167         if (pending) {
1168                 dbus_pending_call_cancel(pending);
1169                 dbus_pending_call_unref(pending);
1170         }
1171
1172         if (message)
1173                 dbus_message_unref(message);
1174
1175         return -ENOMEM;
1176 }
1177
1178 DBusHandlerResult bus_default_message_handler(Manager *m, DBusConnection *c, DBusMessage *message, const char*introspection, const BusProperty *properties) {
1179         DBusError error;
1180         DBusMessage *reply = NULL;
1181         int r;
1182
1183         assert(m);
1184         assert(message);
1185
1186         dbus_error_init(&error);
1187
1188         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
1189
1190                 if (!(reply = dbus_message_new_method_return(message)))
1191                         goto oom;
1192
1193                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
1194                         goto oom;
1195
1196         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
1197                 const char *interface, *property;
1198                 const BusProperty *p;
1199
1200                 if (!dbus_message_get_args(
1201                             message,
1202                             &error,
1203                             DBUS_TYPE_STRING, &interface,
1204                             DBUS_TYPE_STRING, &property,
1205                             DBUS_TYPE_INVALID))
1206                         return bus_send_error_reply(m, c, message, &error, -EINVAL);
1207
1208                 for (p = properties; p->property; p++)
1209                         if (streq(p->interface, interface) && streq(p->property, property))
1210                                 break;
1211
1212                 if (p->property) {
1213                         DBusMessageIter iter, sub;
1214
1215                         if (!(reply = dbus_message_new_method_return(message)))
1216                                 goto oom;
1217
1218                         dbus_message_iter_init_append(reply, &iter);
1219
1220                         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
1221                                 goto oom;
1222
1223                         if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
1224
1225                                 if (r == -ENOMEM)
1226                                         goto oom;
1227
1228                                 dbus_message_unref(reply);
1229                                 return bus_send_error_reply(m, c, message, NULL, r);
1230                         }
1231
1232                         if (!dbus_message_iter_close_container(&iter, &sub))
1233                                 goto oom;
1234                 }
1235         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
1236                 const char *interface;
1237                 const BusProperty *p;
1238                 DBusMessageIter iter, sub, sub2, sub3;
1239
1240                 if (!dbus_message_get_args(
1241                             message,
1242                             &error,
1243                             DBUS_TYPE_STRING, &interface,
1244                             DBUS_TYPE_INVALID))
1245                         return bus_send_error_reply(m, c, message, &error, -EINVAL);
1246
1247                 if (!(reply = dbus_message_new_method_return(message)))
1248                         goto oom;
1249
1250                 dbus_message_iter_init_append(reply, &iter);
1251
1252                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
1253                         goto oom;
1254
1255                 for (p = properties; p->property; p++) {
1256                         if (interface[0] && !streq(p->interface, interface))
1257                                 continue;
1258
1259                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
1260                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
1261                             !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
1262                                 goto oom;
1263
1264                         if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
1265
1266                                 if (r == -ENOMEM)
1267                                         goto oom;
1268
1269                                 dbus_message_unref(reply);
1270                                 return bus_send_error_reply(m, c, message, NULL, r);
1271                         }
1272
1273                         if (!dbus_message_iter_close_container(&sub2, &sub3) ||
1274                             !dbus_message_iter_close_container(&sub, &sub2))
1275                                 goto oom;
1276                 }
1277
1278                 if (!dbus_message_iter_close_container(&iter, &sub))
1279                         goto oom;
1280         }
1281
1282         if (reply) {
1283                 if (!dbus_connection_send(c, reply, NULL))
1284                         goto oom;
1285
1286                 dbus_message_unref(reply);
1287                 return DBUS_HANDLER_RESULT_HANDLED;
1288         }
1289
1290         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1291
1292 oom:
1293         if (reply)
1294                 dbus_message_unref(reply);
1295
1296         dbus_error_free(&error);
1297
1298         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1299 }
1300
1301 static const char *error_to_dbus(int error) {
1302
1303         switch(error) {
1304
1305         case -EINVAL:
1306                 return DBUS_ERROR_INVALID_ARGS;
1307
1308         case -ENOMEM:
1309                 return DBUS_ERROR_NO_MEMORY;
1310
1311         case -EPERM:
1312         case -EACCES:
1313                 return DBUS_ERROR_ACCESS_DENIED;
1314
1315         case -ESRCH:
1316                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
1317
1318         case -ENOENT:
1319                 return DBUS_ERROR_FILE_NOT_FOUND;
1320
1321         case -EEXIST:
1322                 return DBUS_ERROR_FILE_EXISTS;
1323
1324         case -ETIMEDOUT:
1325                 return DBUS_ERROR_TIMEOUT;
1326
1327         case -EIO:
1328                 return DBUS_ERROR_IO_ERROR;
1329
1330         case -ENETRESET:
1331         case -ECONNABORTED:
1332         case -ECONNRESET:
1333                 return DBUS_ERROR_DISCONNECTED;
1334         }
1335
1336         return DBUS_ERROR_FAILED;
1337 }
1338
1339 DBusHandlerResult bus_send_error_reply(Manager *m, DBusConnection *c, DBusMessage *message, DBusError *berror, int error) {
1340         DBusMessage *reply = NULL;
1341         const char *name, *text;
1342
1343         if (berror && dbus_error_is_set(berror)) {
1344                 name = berror->name;
1345                 text = berror->message;
1346         } else {
1347                 name = error_to_dbus(error);
1348                 text = strerror(-error);
1349         }
1350
1351         if (!(reply = dbus_message_new_error(message, name, text)))
1352                 goto oom;
1353
1354         if (!dbus_connection_send(c, reply, NULL))
1355                 goto oom;
1356
1357         dbus_message_unref(reply);
1358
1359         if (berror)
1360                 dbus_error_free(berror);
1361
1362         return DBUS_HANDLER_RESULT_HANDLED;
1363
1364 oom:
1365         if (reply)
1366                 dbus_message_unref(reply);
1367
1368         if (berror)
1369                 dbus_error_free(berror);
1370
1371         return DBUS_HANDLER_RESULT_NEED_MEMORY;
1372 }
1373
1374 int bus_broadcast(Manager *m, DBusMessage *message) {
1375         bool oom = false;
1376         Iterator i;
1377         DBusConnection *c;
1378
1379         assert(m);
1380         assert(message);
1381
1382         SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1383                 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1384                         oom = !dbus_connection_send(c, message, NULL);
1385
1386         SET_FOREACH(c, m->bus_connections, i)
1387                 if (c != m->system_bus || m->running_as == MANAGER_SYSTEM)
1388                         oom = !dbus_connection_send(c, message, NULL);
1389
1390         return oom ? -ENOMEM : 0;
1391 }
1392
1393 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1394         const char *t = data;
1395
1396         assert(m);
1397         assert(i);
1398         assert(property);
1399
1400         if (!t)
1401                 t = "";
1402
1403         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
1404                 return -ENOMEM;
1405
1406         return 0;
1407 }
1408
1409 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1410         DBusMessageIter sub;
1411         char **t = data;
1412
1413         assert(m);
1414         assert(i);
1415         assert(property);
1416
1417         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
1418                 return -ENOMEM;
1419
1420         STRV_FOREACH(t, t)
1421                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
1422                         return -ENOMEM;
1423
1424         if (!dbus_message_iter_close_container(i, &sub))
1425                 return -ENOMEM;
1426
1427         return 0;
1428 }
1429
1430 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1431         bool *b = data;
1432         dbus_bool_t db;
1433
1434         assert(m);
1435         assert(i);
1436         assert(property);
1437         assert(b);
1438
1439         db = *b;
1440
1441         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1442                 return -ENOMEM;
1443
1444         return 0;
1445 }
1446
1447 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1448         assert(m);
1449         assert(i);
1450         assert(property);
1451         assert(data);
1452
1453         /* Let's ensure that pid_t is actually 64bit, and hence this
1454          * function can be used for usec_t */
1455         assert_cc(sizeof(uint64_t) == sizeof(usec_t));
1456
1457         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1458                 return -ENOMEM;
1459
1460         return 0;
1461 }
1462
1463 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1464         assert(m);
1465         assert(i);
1466         assert(property);
1467         assert(data);
1468
1469         /* Let's ensure that pid_t and mode_t is actually 32bit, and
1470          * hence this function can be used for pid_t/mode_t */
1471         assert_cc(sizeof(uint32_t) == sizeof(pid_t));
1472         assert_cc(sizeof(uint32_t) == sizeof(mode_t));
1473         assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1474
1475         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
1476                 return -ENOMEM;
1477
1478         return 0;
1479 }
1480
1481 int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1482         assert(m);
1483         assert(i);
1484         assert(property);
1485         assert(data);
1486
1487         assert_cc(sizeof(int32_t) == sizeof(int));
1488
1489         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))
1490                 return -ENOMEM;
1491
1492         return 0;
1493 }
1494
1495 int bus_property_append_size(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1496         uint64_t u;
1497
1498         assert(m);
1499         assert(i);
1500         assert(property);
1501         assert(data);
1502
1503         u = (uint64_t) *(size_t*) data;
1504
1505         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1506                 return -ENOMEM;
1507
1508         return 0;
1509 }
1510
1511 int bus_property_append_ul(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1512         uint64_t u;
1513
1514         assert(m);
1515         assert(i);
1516         assert(property);
1517         assert(data);
1518
1519         u = (uint64_t) *(unsigned long*) data;
1520
1521         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, &u))
1522                 return -ENOMEM;
1523
1524         return 0;
1525 }
1526
1527 int bus_parse_strv(DBusMessage *m, char ***_l) {
1528         DBusMessageIter iter, sub;
1529         unsigned n = 0, i = 0;
1530         char **l;
1531
1532         assert(m);
1533         assert(_l);
1534
1535         if (!dbus_message_iter_init(m, &iter) ||
1536             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
1537             dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING)
1538             return -EINVAL;
1539
1540         dbus_message_iter_recurse(&iter, &sub);
1541
1542         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1543                 n++;
1544                 dbus_message_iter_next(&sub);
1545         }
1546
1547         if (!(l = new(char*, n+1)))
1548                 return -ENOMEM;
1549
1550         assert_se(dbus_message_iter_init(m, &iter));
1551         dbus_message_iter_recurse(&iter, &sub);
1552
1553         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
1554                 const char *s;
1555
1556                 assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING);
1557                 dbus_message_iter_get_basic(&sub, &s);
1558
1559                 if (!(l[i++] = strdup(s))) {
1560                         strv_free(l);
1561                         return -ENOMEM;
1562                 }
1563
1564                 dbus_message_iter_next(&sub);
1565         }
1566
1567         assert(i == n);
1568         l[i] = NULL;
1569
1570         if (_l)
1571                 *_l = l;
1572
1573         return 0;
1574 }
1575
1576 bool bus_has_subscriber(Manager *m) {
1577         Iterator i;
1578         DBusConnection *c;
1579
1580         assert(m);
1581
1582         SET_FOREACH(c, m->bus_connections_for_dispatch, i)
1583                 if (bus_connection_has_subscriber(m, c))
1584                         return true;
1585
1586         SET_FOREACH(c, m->bus_connections, i)
1587                 if (bus_connection_has_subscriber(m, c))
1588                         return true;
1589
1590         return false;
1591 }
1592
1593 bool bus_connection_has_subscriber(Manager *m, DBusConnection *c) {
1594         assert(m);
1595         assert(c);
1596
1597         return !set_isempty(BUS_CONNECTION_SUBSCRIBED(m, c));
1598 }