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