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