]> git.ipfire.org Git - people/ms/systemd.git/blame - dbus.c
dbus: connect to bus as soon as the special dbus service is around
[people/ms/systemd.git] / dbus.c
CommitLineData
ea430986
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
a7334b09
LP
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
ea430986
LP
22#include <dbus/dbus.h>
23
24#include <sys/epoll.h>
25#include <sys/timerfd.h>
26#include <errno.h>
27#include <unistd.h>
28
29#include "dbus.h"
30#include "log.h"
31#include "strv.h"
8e274523 32#include "cgroup.h"
ea430986 33
f278026d 34static void api_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
ea430986
LP
35 Manager *m = data;
36
37 assert(bus);
38 assert(m);
f278026d 39 assert(m->api_bus == bus);
ea430986 40
f278026d 41 m->request_api_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
ea430986
LP
42}
43
8e274523
LP
44static void system_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
45 Manager *m = data;
46
47 assert(bus);
48 assert(m);
49 assert(m->system_bus == bus);
50
51 m->request_system_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
52}
53
ea430986
LP
54static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
55 unsigned flags;
56 uint32_t events = 0;
57
58 assert(bus_watch);
59
60 /* no watch flags for disabled watches */
61 if (!dbus_watch_get_enabled(bus_watch))
62 return 0;
63
64 flags = dbus_watch_get_flags(bus_watch);
65
66 if (flags & DBUS_WATCH_READABLE)
67 events |= EPOLLIN;
68 if (flags & DBUS_WATCH_WRITABLE)
69 events |= EPOLLOUT;
70
71 return events | EPOLLHUP | EPOLLERR;
72}
73
74static unsigned events_to_bus_flags(uint32_t events) {
75 unsigned flags = 0;
76
77 if (events & EPOLLIN)
78 flags |= DBUS_WATCH_READABLE;
79 if (events & EPOLLOUT)
80 flags |= DBUS_WATCH_WRITABLE;
81 if (events & EPOLLHUP)
82 flags |= DBUS_WATCH_HANGUP;
83 if (events & EPOLLERR)
84 flags |= DBUS_WATCH_ERROR;
85
86 return flags;
87}
88
89void bus_watch_event(Manager *m, Watch *w, int events) {
90 assert(m);
91 assert(w);
92
93 /* This is called by the event loop whenever there is
94 * something happening on D-Bus' file handles. */
95
8e274523 96 if (!dbus_watch_get_enabled(w->data.bus_watch))
ea430986
LP
97 return;
98
99 dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
100}
101
102static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
103 Manager *m = data;
104 Watch *w;
105 struct epoll_event ev;
106
107 assert(bus_watch);
108 assert(m);
109
110 if (!(w = new0(Watch, 1)))
111 return FALSE;
112
113 w->fd = dbus_watch_get_unix_fd(bus_watch);
114 w->type = WATCH_DBUS_WATCH;
115 w->data.bus_watch = bus_watch;
116
117 zero(ev);
118 ev.events = bus_flags_to_events(bus_watch);
119 ev.data.ptr = w;
120
121 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
122
123 if (errno != EEXIST) {
124 free(w);
125 return FALSE;
126 }
127
128 /* Hmm, bloody D-Bus creates multiple watches on the
129 * same fd. epoll() does not like that. As a dirty
130 * hack we simply dup() the fd and hence get a second
131 * one we can safely add to the epoll(). */
132
133 if ((w->fd = dup(w->fd)) < 0) {
134 free(w);
135 return FALSE;
136 }
137
138 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
139 free(w);
140 close_nointr_nofail(w->fd);
141 return FALSE;
142 }
143
144 w->fd_is_dupped = true;
145 }
146
147 dbus_watch_set_data(bus_watch, w, NULL);
148
149 return TRUE;
150}
151
152static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
153 Manager *m = data;
154 Watch *w;
155
156 assert(bus_watch);
157 assert(m);
158
159 if (!(w = dbus_watch_get_data(bus_watch)))
160 return;
161
162 assert(w->type == WATCH_DBUS_WATCH);
163 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
164
165 if (w->fd_is_dupped)
166 close_nointr_nofail(w->fd);
167
168 free(w);
169}
170
171static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
172 Manager *m = data;
173 Watch *w;
174 struct epoll_event ev;
175
176 assert(bus_watch);
177 assert(m);
178
179 assert_se(w = dbus_watch_get_data(bus_watch));
180 assert(w->type == WATCH_DBUS_WATCH);
181
182 zero(ev);
183 ev.events = bus_flags_to_events(bus_watch);
184 ev.data.ptr = w;
185
186 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
187}
188
189static int bus_timeout_arm(Manager *m, Watch *w) {
190 struct itimerspec its;
191
192 assert(m);
193 assert(w);
194
195 zero(its);
196
197 if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
198 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
199 its.it_interval = its.it_interval;
200 }
201
202 if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
203 return -errno;
204
205 return 0;
206}
207
208void bus_timeout_event(Manager *m, Watch *w, int events) {
209 assert(m);
210 assert(w);
211
212 /* This is called by the event loop whenever there is
213 * something happening on D-Bus' file handles. */
214
215 if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
216 return;
217
218 dbus_timeout_handle(w->data.bus_timeout);
219}
220
221static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
222 Manager *m = data;
223 Watch *w;
224 struct epoll_event ev;
225
226 assert(timeout);
227 assert(m);
228
229 if (!(w = new0(Watch, 1)))
230 return FALSE;
231
232 if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
233 goto fail;
234
235 w->type = WATCH_DBUS_TIMEOUT;
236 w->data.bus_timeout = timeout;
237
238 if (bus_timeout_arm(m, w) < 0)
239 goto fail;
240
241 zero(ev);
242 ev.events = EPOLLIN;
243 ev.data.ptr = w;
244
245 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
246 goto fail;
247
248 dbus_timeout_set_data(timeout, w, NULL);
249
250 return TRUE;
251
252fail:
253 if (w->fd >= 0)
254 close_nointr_nofail(w->fd);
255
256 free(w);
257 return FALSE;
258}
259
260static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
261 Manager *m = data;
262 Watch *w;
263
264 assert(timeout);
265 assert(m);
266
267 if (!(w = dbus_timeout_get_data(timeout)))
268 return;
269
270 assert(w->type == WATCH_DBUS_TIMEOUT);
271 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
272 close_nointr_nofail(w->fd);
273 free(w);
274}
275
276static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
277 Manager *m = data;
278 Watch *w;
279 int r;
280
281 assert(timeout);
282 assert(m);
283
284 assert_se(w = dbus_timeout_get_data(timeout));
285 assert(w->type == WATCH_DBUS_TIMEOUT);
286
287 if ((r = bus_timeout_arm(m, w)) < 0)
288 log_error("Failed to rearm timer: %s", strerror(-r));
289}
290
f278026d 291static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
c1e1601e
LP
292 Manager *m = data;
293 DBusError error;
294
295 assert(connection);
296 assert(message);
297 assert(m);
298
299 dbus_error_init(&error);
300
301 /* log_debug("Got D-Bus request: %s.%s() on %s", */
302 /* dbus_message_get_interface(message), */
303 /* dbus_message_get_member(message), */
304 /* dbus_message_get_path(message)); */
305
306 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
307 log_error("Warning! D-Bus connection terminated.");
f278026d 308 bus_done_api(m);
c1e1601e
LP
309
310 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
311 const char *name, *old, *new;
312
313 if (!dbus_message_get_args(message, &error,
314 DBUS_TYPE_STRING, &name,
315 DBUS_TYPE_STRING, &old,
316 DBUS_TYPE_STRING, &new,
317 DBUS_TYPE_INVALID))
318 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
319 else {
320 if (set_remove(m->subscribed, (char*) name))
321 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(m->subscribed));
322 }
323 }
324
325 dbus_error_free(&error);
326 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
327}
328
8e274523
LP
329static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
330 Manager *m = data;
331 DBusError error;
332
333 assert(connection);
334 assert(message);
335 assert(m);
336
337 dbus_error_init(&error);
338
339 /* log_debug("Got D-Bus request: %s.%s() on %s", */
340 /* dbus_message_get_interface(message), */
341 /* dbus_message_get_member(message), */
342 /* dbus_message_get_path(message)); */
343
344 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
345 log_error("Warning! D-Bus connection terminated.");
f278026d 346 bus_done_system(m);
8e274523
LP
347
348 } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
349 const char *cgroup;
350
351 if (!dbus_message_get_args(message, &error,
352 DBUS_TYPE_STRING, &cgroup,
353 DBUS_TYPE_INVALID))
354 log_error("Failed to parse Released message: %s", error.message);
355 else
356 cgroup_notify_empty(m, cgroup);
357 }
358
359 dbus_error_free(&error);
360 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
361}
362
c1e1601e 363unsigned bus_dispatch(Manager *m) {
ea430986
LP
364 assert(m);
365
f278026d
LP
366 if (m->request_api_bus_dispatch)
367 if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE) {
368 m->request_api_bus_dispatch = false;
8e274523
LP
369 return 1;
370 }
c1e1601e 371
8e274523
LP
372 if (m->request_system_bus_dispatch)
373 if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE) {
374 m->request_system_bus_dispatch = false;
375 return 1;
376 }
c1e1601e 377
8e274523 378 return 0;
ea430986
LP
379}
380
61902ea3
LP
381static void pending_cb(DBusPendingCall *pending, void *userdata) {
382 DBusMessage *reply;
383 DBusError error;
384
385 dbus_error_init(&error);
386
387 assert_se(reply = dbus_pending_call_steal_reply(pending));
388
389 switch (dbus_message_get_type(reply)) {
390
391 case DBUS_MESSAGE_TYPE_ERROR:
392
393 assert_se(dbus_set_error_from_message(&error, reply));
394 log_warning("RequestName() failed: %s", error.message);
395 break;
396
397 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
398 uint32_t r;
399
400 if (!dbus_message_get_args(reply,
401 &error,
402 DBUS_TYPE_UINT32, &r,
403 DBUS_TYPE_INVALID)) {
404 log_error("Failed to parse RequestName() reply: %s", error.message);
405 break;
406 }
407
408 if (r == 1)
409 log_debug("Successfully acquired name.");
410 else
411 log_error("Name already owned.");
412
413 break;
414 }
415
416 default:
417 assert_not_reached("Invalid reply message");
418 }
419
420 dbus_message_unref(reply);
421 dbus_error_free(&error);
422}
423
ea430986
LP
424static int request_name(Manager *m) {
425 DBusMessage *message;
426 const char *name = "org.freedesktop.systemd1";
427 uint32_t flags = 0;
61902ea3 428 DBusPendingCall *pending;
ea430986
LP
429
430 if (!(message = dbus_message_new_method_call(
431 DBUS_SERVICE_DBUS,
432 DBUS_PATH_DBUS,
433 DBUS_INTERFACE_DBUS,
434 "RequestName")))
435 return -ENOMEM;
436
437 if (!dbus_message_append_args(
438 message,
439 DBUS_TYPE_STRING, &name,
440 DBUS_TYPE_UINT32, &flags,
441 DBUS_TYPE_INVALID)) {
442 dbus_message_unref(message);
443 return -ENOMEM;
444 }
445
f278026d 446 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1)) {
ea430986
LP
447 dbus_message_unref(message);
448 return -ENOMEM;
449 }
450
ea430986
LP
451
452 dbus_message_unref(message);
453
61902ea3
LP
454 if (!dbus_pending_call_set_notify(pending, pending_cb, NULL, NULL)) {
455 dbus_pending_call_cancel(pending);
456 dbus_pending_call_unref(pending);
457 return -ENOMEM;
458 }
459
460
461 dbus_pending_call_unref(pending);
462
463 /* We simple ask for the name and don't wait for it. Sooner or
464 * later we'll have it. */
465
ea430986
LP
466 return 0;
467}
468
8e274523
LP
469static int bus_setup_loop(Manager *m, DBusConnection *bus) {
470 assert(m);
471 assert(bus);
472
473 dbus_connection_set_exit_on_disconnect(bus, FALSE);
474 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
475 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL))
476 return -ENOMEM;
477
478 return 0;
479}
480
f278026d 481int bus_init_system(Manager *m) {
ea430986
LP
482 DBusError error;
483 char *id;
484 int r;
485
486 assert(m);
487
ea430986 488 dbus_error_init(&error);
ea430986 489
f278026d
LP
490 if (m->system_bus)
491 return 0;
8e274523 492
f278026d
LP
493 if (m->running_as != MANAGER_SESSION && m->api_bus)
494 m->system_bus = m->api_bus;
495 else {
8e274523 496 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
f278026d 497 log_error("Failed to get D-Bus connection, retrying later: %s", error.message);
8e274523 498 dbus_error_free(&error);
f278026d 499 return 0;
8e274523
LP
500 }
501
502 if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
f278026d 503 bus_done_system(m);
8e274523
LP
504 return r;
505 }
506
507 dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL);
f278026d
LP
508 }
509
510 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
511 bus_done_system(m);
c1e1601e
LP
512 return -ENOMEM;
513 }
514
f278026d 515 dbus_bus_add_match(m->system_bus,
c1e1601e 516 "type='signal',"
f278026d
LP
517 "interface='org.freedesktop.systemd1.Agent',"
518 "path='/org/freedesktop/systemd1/agent'",
c1e1601e
LP
519 &error);
520
521 if (dbus_error_is_set(&error)) {
522 log_error("Failed to register match: %s", error.message);
523 dbus_error_free(&error);
f278026d 524 bus_done_system(m);
ea430986
LP
525 return -ENOMEM;
526 }
527
f278026d
LP
528 log_debug("Successfully connected to system D-Bus bus %s as %s",
529 strnull((id = dbus_connection_get_server_id(m->system_bus))),
530 strnull(dbus_bus_get_unique_name(m->system_bus)));
531 dbus_free(id);
532
533 m->request_system_bus_dispatch = true;
534
535 return 0;
536}
537
538int bus_init_api(Manager *m) {
539 DBusError error;
540 char *id;
541 int r;
542
543 assert(m);
544
545 dbus_error_init(&error);
546
547 if (m->api_bus)
548 return 0;
549
550 if (m->running_as != MANAGER_SESSION && m->system_bus)
551 m->api_bus = m->system_bus;
552 else {
553 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
554 log_debug("Failed to get D-Bus connection, retrying later: %s", error.message);
555 dbus_error_free(&error);
556 return 0;
557 }
558
559 if ((r = bus_setup_loop(m, m->api_bus)) < 0) {
560 bus_done_api(m);
561 return r;
562 }
563
564 dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL);
ea430986
LP
565 }
566
f278026d
LP
567 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
568 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
569 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
570 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
571 bus_done_api(m);
572 return -ENOMEM;
573 }
574
575 dbus_bus_add_match(m->api_bus,
8e274523 576 "type='signal',"
f278026d
LP
577 "sender='"DBUS_SERVICE_DBUS"',"
578 "interface='"DBUS_INTERFACE_DBUS"',"
579 "path='"DBUS_PATH_DBUS"'",
8e274523
LP
580 &error);
581
582 if (dbus_error_is_set(&error)) {
583 log_error("Failed to register match: %s", error.message);
584 dbus_error_free(&error);
f278026d 585 bus_done_api(m);
8e274523
LP
586 return -ENOMEM;
587 }
588
f278026d
LP
589 if ((r = request_name(m)) < 0) {
590 bus_done_api(m);
591 return r;
592 }
ea430986 593
f278026d
LP
594 log_debug("Successfully connected to API D-Bus bus %s as %s",
595 strnull((id = dbus_connection_get_server_id(m->api_bus))),
596 strnull(dbus_bus_get_unique_name(m->api_bus)));
8e274523
LP
597 dbus_free(id);
598
f278026d
LP
599 if (!m->subscribed)
600 if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
601 return -ENOMEM;
602
603 m->request_api_bus_dispatch = true;
ea430986
LP
604
605 return 0;
606}
607
f278026d 608void bus_done_api(Manager *m) {
ea430986
LP
609 assert(m);
610
f278026d
LP
611 if (m->api_bus) {
612 if (m->system_bus == m->api_bus)
613 m->system_bus = NULL;
614
615 dbus_connection_close(m->api_bus);
616 dbus_connection_unref(m->api_bus);
617 m->api_bus = NULL;
8e274523 618
ea430986 619 }
c1e1601e
LP
620
621 if (m->subscribed) {
622 char *c;
623
624 while ((c = set_steal_first(m->subscribed)))
625 free(c);
626
627 set_free(m->subscribed);
628 m->subscribed = NULL;
629 }
ea430986
LP
630}
631
f278026d
LP
632void bus_done_system(Manager *m) {
633 assert(m);
634
635 if (m->system_bus == m->api_bus)
636 bus_done_api(m);
637
638 if (m->system_bus) {
639 dbus_connection_close(m->system_bus);
640 dbus_connection_unref(m->system_bus);
641 m->system_bus = NULL;
642 }
643}
644
ea430986
LP
645DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
646 DBusError error;
647 DBusMessage *reply = NULL;
648 int r;
649
650 assert(m);
651 assert(message);
652
653 dbus_error_init(&error);
654
655 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
656
657 if (!(reply = dbus_message_new_method_return(message)))
658 goto oom;
659
660 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
661 goto oom;
662
663 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
664 const char *interface, *property;
665 const BusProperty *p;
666
667 if (!dbus_message_get_args(
668 message,
669 &error,
670 DBUS_TYPE_STRING, &interface,
671 DBUS_TYPE_STRING, &property,
672 DBUS_TYPE_INVALID))
673 return bus_send_error_reply(m, message, &error, -EINVAL);
674
675 for (p = properties; p->property; p++)
676 if (streq(p->interface, interface) && streq(p->property, property))
677 break;
678
679 if (p->property) {
680 DBusMessageIter iter, sub;
681
682 if (!(reply = dbus_message_new_method_return(message)))
683 goto oom;
684
685 dbus_message_iter_init_append(reply, &iter);
686
687 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
688 goto oom;
689
690 if ((r = p->append(m, &sub, property, p->data)) < 0) {
691
692 if (r == -ENOMEM)
693 goto oom;
694
695 dbus_message_unref(reply);
696 return bus_send_error_reply(m, message, NULL, r);
697 }
698
699 if (!dbus_message_iter_close_container(&iter, &sub))
700 goto oom;
701 }
702 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
703 const char *interface;
704 const BusProperty *p;
705 DBusMessageIter iter, sub, sub2, sub3;
706 bool any = false;
707
708 if (!dbus_message_get_args(
709 message,
710 &error,
711 DBUS_TYPE_STRING, &interface,
712 DBUS_TYPE_INVALID))
713 return bus_send_error_reply(m, message, &error, -EINVAL);
714
715 if (!(reply = dbus_message_new_method_return(message)))
716 goto oom;
717
718 dbus_message_iter_init_append(reply, &iter);
719
720 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
721 goto oom;
722
723 for (p = properties; p->property; p++) {
724 if (!streq(p->interface, interface))
725 continue;
726
c401a1e0 727 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
ea430986
LP
728 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
729 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
730 goto oom;
731
732 if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
733
734 if (r == -ENOMEM)
735 goto oom;
736
737 dbus_message_unref(reply);
738 return bus_send_error_reply(m, message, NULL, r);
739 }
740
741 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
742 !dbus_message_iter_close_container(&sub, &sub2))
743 goto oom;
744
745 any = true;
746 }
747
748 if (!dbus_message_iter_close_container(&iter, &sub))
749 goto oom;
750 }
751
752 if (reply) {
f278026d 753 if (!dbus_connection_send(m->api_bus, reply, NULL))
ea430986
LP
754 goto oom;
755
756 dbus_message_unref(reply);
757 return DBUS_HANDLER_RESULT_HANDLED;
758 }
759
760 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
761
762oom:
763 if (reply)
764 dbus_message_unref(reply);
765
766 dbus_error_free(&error);
767
768 return DBUS_HANDLER_RESULT_NEED_MEMORY;
769}
770
ea430986
LP
771static const char *error_to_dbus(int error) {
772
773 switch(error) {
774
775 case -EINVAL:
776 return DBUS_ERROR_INVALID_ARGS;
777
778 case -ENOMEM:
779 return DBUS_ERROR_NO_MEMORY;
780
781 case -EPERM:
782 case -EACCES:
783 return DBUS_ERROR_ACCESS_DENIED;
784
785 case -ESRCH:
786 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
787
788 case -ENOENT:
789 return DBUS_ERROR_FILE_NOT_FOUND;
790
791 case -EEXIST:
792 return DBUS_ERROR_FILE_EXISTS;
793
794 case -ETIMEDOUT:
795 return DBUS_ERROR_TIMEOUT;
796
797 case -EIO:
798 return DBUS_ERROR_IO_ERROR;
799
800 case -ENETRESET:
801 case -ECONNABORTED:
802 case -ECONNRESET:
803 return DBUS_ERROR_DISCONNECTED;
804 }
805
806 return DBUS_ERROR_FAILED;
807}
808
809DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
810 DBusMessage *reply = NULL;
811 const char *name, *text;
812
813 if (bus_error && dbus_error_is_set(bus_error)) {
814 name = bus_error->name;
815 text = bus_error->message;
816 } else {
817 name = error_to_dbus(error);
818 text = strerror(-error);
819 }
820
821 if (!(reply = dbus_message_new_error(message, name, text)))
822 goto oom;
823
f278026d 824 if (!dbus_connection_send(m->api_bus, reply, NULL))
ea430986
LP
825 goto oom;
826
827 dbus_message_unref(reply);
828
829 if (bus_error)
830 dbus_error_free(bus_error);
831
832 return DBUS_HANDLER_RESULT_HANDLED;
833
834oom:
835 if (reply)
836 dbus_message_unref(reply);
837
838 if (bus_error)
839 dbus_error_free(bus_error);
840
841 return DBUS_HANDLER_RESULT_NEED_MEMORY;
842}
843
844int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
845 const char *t = data;
846
847 assert(m);
848 assert(i);
849 assert(property);
38131695
LP
850
851 if (!t)
852 t = "";
ea430986
LP
853
854 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
855 return -ENOMEM;
856
857 return 0;
858}
859
860int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
861 DBusMessageIter sub;
862 char **t = data;
863
864 assert(m);
865 assert(i);
866 assert(property);
867
868 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
869 return -ENOMEM;
870
871 STRV_FOREACH(t, t)
872 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
873 return -ENOMEM;
874
875 if (!dbus_message_iter_close_container(i, &sub))
876 return -ENOMEM;
877
878 return 0;
879}
880
881int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
882 bool *b = data;
883 dbus_bool_t db;
884
885 assert(m);
886 assert(i);
887 assert(property);
888 assert(b);
889
890 db = *b;
891
892 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
893 return -ENOMEM;
894
895 return 0;
896}
38131695
LP
897
898int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
899 assert(m);
900 assert(i);
901 assert(property);
902 assert(data);
903
904 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
905 return -ENOMEM;
906
907 return 0;
908}
909
910int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
911 assert(m);
912 assert(i);
913 assert(property);
914 assert(data);
915
916 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
917 return -ENOMEM;
918
919 return 0;
920}