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