]> git.ipfire.org Git - people/ms/systemd.git/blame - dbus.c
dbus: make sure dbus doesn't call us anymore when we unref'ed it
[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
fd18e1f4
LP
366 if (m->request_api_bus_dispatch) {
367 if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE)
f278026d 368 m->request_api_bus_dispatch = false;
c1e1601e 369
fd18e1f4
LP
370 return 1;
371 }
372
373 if (m->request_system_bus_dispatch) {
374 if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE)
8e274523 375 m->request_system_bus_dispatch = false;
fd18e1f4
LP
376
377 return 1;
378 }
c1e1601e 379
8e274523 380 return 0;
ea430986
LP
381}
382
61902ea3
LP
383static void pending_cb(DBusPendingCall *pending, void *userdata) {
384 DBusMessage *reply;
385 DBusError error;
386
387 dbus_error_init(&error);
388
389 assert_se(reply = dbus_pending_call_steal_reply(pending));
390
391 switch (dbus_message_get_type(reply)) {
392
393 case DBUS_MESSAGE_TYPE_ERROR:
394
395 assert_se(dbus_set_error_from_message(&error, reply));
396 log_warning("RequestName() failed: %s", error.message);
397 break;
398
399 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
400 uint32_t r;
401
402 if (!dbus_message_get_args(reply,
403 &error,
404 DBUS_TYPE_UINT32, &r,
405 DBUS_TYPE_INVALID)) {
406 log_error("Failed to parse RequestName() reply: %s", error.message);
407 break;
408 }
409
410 if (r == 1)
411 log_debug("Successfully acquired name.");
412 else
413 log_error("Name already owned.");
414
415 break;
416 }
417
418 default:
419 assert_not_reached("Invalid reply message");
420 }
421
422 dbus_message_unref(reply);
423 dbus_error_free(&error);
424}
425
ea430986
LP
426static int request_name(Manager *m) {
427 DBusMessage *message;
428 const char *name = "org.freedesktop.systemd1";
429 uint32_t flags = 0;
61902ea3 430 DBusPendingCall *pending;
ea430986
LP
431
432 if (!(message = dbus_message_new_method_call(
433 DBUS_SERVICE_DBUS,
434 DBUS_PATH_DBUS,
435 DBUS_INTERFACE_DBUS,
436 "RequestName")))
437 return -ENOMEM;
438
439 if (!dbus_message_append_args(
440 message,
441 DBUS_TYPE_STRING, &name,
442 DBUS_TYPE_UINT32, &flags,
443 DBUS_TYPE_INVALID)) {
444 dbus_message_unref(message);
445 return -ENOMEM;
446 }
447
f278026d 448 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1)) {
ea430986
LP
449 dbus_message_unref(message);
450 return -ENOMEM;
451 }
452
ea430986
LP
453
454 dbus_message_unref(message);
455
61902ea3
LP
456 if (!dbus_pending_call_set_notify(pending, pending_cb, NULL, NULL)) {
457 dbus_pending_call_cancel(pending);
458 dbus_pending_call_unref(pending);
459 return -ENOMEM;
460 }
461
462
463 dbus_pending_call_unref(pending);
464
465 /* We simple ask for the name and don't wait for it. Sooner or
466 * later we'll have it. */
467
ea430986
LP
468 return 0;
469}
470
8e274523
LP
471static int bus_setup_loop(Manager *m, DBusConnection *bus) {
472 assert(m);
473 assert(bus);
474
475 dbus_connection_set_exit_on_disconnect(bus, FALSE);
fd18e1f4 476
8e274523
LP
477 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
478 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL))
479 return -ENOMEM;
480
481 return 0;
482}
483
f278026d 484int bus_init_system(Manager *m) {
ea430986
LP
485 DBusError error;
486 char *id;
487 int r;
488
489 assert(m);
490
ea430986 491 dbus_error_init(&error);
ea430986 492
f278026d
LP
493 if (m->system_bus)
494 return 0;
8e274523 495
f278026d
LP
496 if (m->running_as != MANAGER_SESSION && m->api_bus)
497 m->system_bus = m->api_bus;
498 else {
8e274523 499 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
266e525c 500 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
8e274523 501 dbus_error_free(&error);
f278026d 502 return 0;
8e274523
LP
503 }
504
fd18e1f4
LP
505 dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL);
506 m->request_system_bus_dispatch = true;
507
8e274523 508 if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
f278026d 509 bus_done_system(m);
8e274523
LP
510 return r;
511 }
f278026d
LP
512 }
513
514 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
515 bus_done_system(m);
c1e1601e
LP
516 return -ENOMEM;
517 }
518
f278026d 519 dbus_bus_add_match(m->system_bus,
c1e1601e 520 "type='signal',"
f278026d
LP
521 "interface='org.freedesktop.systemd1.Agent',"
522 "path='/org/freedesktop/systemd1/agent'",
c1e1601e
LP
523 &error);
524
525 if (dbus_error_is_set(&error)) {
526 log_error("Failed to register match: %s", error.message);
527 dbus_error_free(&error);
f278026d 528 bus_done_system(m);
ea430986
LP
529 return -ENOMEM;
530 }
531
f278026d
LP
532 log_debug("Successfully connected to system D-Bus bus %s as %s",
533 strnull((id = dbus_connection_get_server_id(m->system_bus))),
534 strnull(dbus_bus_get_unique_name(m->system_bus)));
535 dbus_free(id);
536
f278026d
LP
537 return 0;
538}
539
540int bus_init_api(Manager *m) {
541 DBusError error;
542 char *id;
543 int r;
544
545 assert(m);
546
547 dbus_error_init(&error);
548
549 if (m->api_bus)
550 return 0;
551
552 if (m->running_as != MANAGER_SESSION && m->system_bus)
553 m->api_bus = m->system_bus;
554 else {
555 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
266e525c 556 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
f278026d
LP
557 dbus_error_free(&error);
558 return 0;
559 }
560
fd18e1f4
LP
561 dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL);
562 m->request_api_bus_dispatch = true;
563
f278026d
LP
564 if ((r = bus_setup_loop(m, m->api_bus)) < 0) {
565 bus_done_api(m);
566 return r;
567 }
ea430986
LP
568 }
569
f278026d
LP
570 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
571 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
572 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
573 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
574 bus_done_api(m);
575 return -ENOMEM;
576 }
577
578 dbus_bus_add_match(m->api_bus,
8e274523 579 "type='signal',"
f278026d
LP
580 "sender='"DBUS_SERVICE_DBUS"',"
581 "interface='"DBUS_INTERFACE_DBUS"',"
582 "path='"DBUS_PATH_DBUS"'",
8e274523
LP
583 &error);
584
585 if (dbus_error_is_set(&error)) {
586 log_error("Failed to register match: %s", error.message);
587 dbus_error_free(&error);
f278026d 588 bus_done_api(m);
8e274523
LP
589 return -ENOMEM;
590 }
591
f278026d
LP
592 if ((r = request_name(m)) < 0) {
593 bus_done_api(m);
594 return r;
595 }
ea430986 596
f278026d
LP
597 log_debug("Successfully connected to API D-Bus bus %s as %s",
598 strnull((id = dbus_connection_get_server_id(m->api_bus))),
599 strnull(dbus_bus_get_unique_name(m->api_bus)));
8e274523
LP
600 dbus_free(id);
601
f278026d
LP
602 if (!m->subscribed)
603 if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
604 return -ENOMEM;
605
ea430986
LP
606 return 0;
607}
608
f278026d 609void bus_done_api(Manager *m) {
ea430986
LP
610 assert(m);
611
f278026d
LP
612 if (m->api_bus) {
613 if (m->system_bus == m->api_bus)
614 m->system_bus = NULL;
615
616 dbus_connection_close(m->api_bus);
90d3f30b 617 dbus_connection_set_dispatch_status_function(m->api_bus, NULL, NULL, NULL);
f278026d
LP
618 dbus_connection_unref(m->api_bus);
619 m->api_bus = NULL;
8e274523 620
ea430986 621 }
c1e1601e
LP
622
623 if (m->subscribed) {
624 char *c;
625
626 while ((c = set_steal_first(m->subscribed)))
627 free(c);
628
629 set_free(m->subscribed);
630 m->subscribed = NULL;
631 }
ea430986
LP
632}
633
f278026d
LP
634void bus_done_system(Manager *m) {
635 assert(m);
636
637 if (m->system_bus == m->api_bus)
638 bus_done_api(m);
639
640 if (m->system_bus) {
641 dbus_connection_close(m->system_bus);
90d3f30b 642 dbus_connection_set_dispatch_status_function(m->system_bus, NULL, NULL, NULL);
f278026d
LP
643 dbus_connection_unref(m->system_bus);
644 m->system_bus = NULL;
645 }
646}
647
ea430986
LP
648DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
649 DBusError error;
650 DBusMessage *reply = NULL;
651 int r;
652
653 assert(m);
654 assert(message);
655
656 dbus_error_init(&error);
657
658 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
659
660 if (!(reply = dbus_message_new_method_return(message)))
661 goto oom;
662
663 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
664 goto oom;
665
666 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
667 const char *interface, *property;
668 const BusProperty *p;
669
670 if (!dbus_message_get_args(
671 message,
672 &error,
673 DBUS_TYPE_STRING, &interface,
674 DBUS_TYPE_STRING, &property,
675 DBUS_TYPE_INVALID))
676 return bus_send_error_reply(m, message, &error, -EINVAL);
677
678 for (p = properties; p->property; p++)
679 if (streq(p->interface, interface) && streq(p->property, property))
680 break;
681
682 if (p->property) {
683 DBusMessageIter iter, sub;
684
685 if (!(reply = dbus_message_new_method_return(message)))
686 goto oom;
687
688 dbus_message_iter_init_append(reply, &iter);
689
690 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
691 goto oom;
692
693 if ((r = p->append(m, &sub, property, p->data)) < 0) {
694
695 if (r == -ENOMEM)
696 goto oom;
697
698 dbus_message_unref(reply);
699 return bus_send_error_reply(m, message, NULL, r);
700 }
701
702 if (!dbus_message_iter_close_container(&iter, &sub))
703 goto oom;
704 }
705 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
706 const char *interface;
707 const BusProperty *p;
708 DBusMessageIter iter, sub, sub2, sub3;
709 bool any = false;
710
711 if (!dbus_message_get_args(
712 message,
713 &error,
714 DBUS_TYPE_STRING, &interface,
715 DBUS_TYPE_INVALID))
716 return bus_send_error_reply(m, message, &error, -EINVAL);
717
718 if (!(reply = dbus_message_new_method_return(message)))
719 goto oom;
720
721 dbus_message_iter_init_append(reply, &iter);
722
723 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
724 goto oom;
725
726 for (p = properties; p->property; p++) {
727 if (!streq(p->interface, interface))
728 continue;
729
c401a1e0 730 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
ea430986
LP
731 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
732 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
733 goto oom;
734
735 if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
736
737 if (r == -ENOMEM)
738 goto oom;
739
740 dbus_message_unref(reply);
741 return bus_send_error_reply(m, message, NULL, r);
742 }
743
744 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
745 !dbus_message_iter_close_container(&sub, &sub2))
746 goto oom;
747
748 any = true;
749 }
750
751 if (!dbus_message_iter_close_container(&iter, &sub))
752 goto oom;
753 }
754
755 if (reply) {
f278026d 756 if (!dbus_connection_send(m->api_bus, reply, NULL))
ea430986
LP
757 goto oom;
758
759 dbus_message_unref(reply);
760 return DBUS_HANDLER_RESULT_HANDLED;
761 }
762
763 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
764
765oom:
766 if (reply)
767 dbus_message_unref(reply);
768
769 dbus_error_free(&error);
770
771 return DBUS_HANDLER_RESULT_NEED_MEMORY;
772}
773
ea430986
LP
774static const char *error_to_dbus(int error) {
775
776 switch(error) {
777
778 case -EINVAL:
779 return DBUS_ERROR_INVALID_ARGS;
780
781 case -ENOMEM:
782 return DBUS_ERROR_NO_MEMORY;
783
784 case -EPERM:
785 case -EACCES:
786 return DBUS_ERROR_ACCESS_DENIED;
787
788 case -ESRCH:
789 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
790
791 case -ENOENT:
792 return DBUS_ERROR_FILE_NOT_FOUND;
793
794 case -EEXIST:
795 return DBUS_ERROR_FILE_EXISTS;
796
797 case -ETIMEDOUT:
798 return DBUS_ERROR_TIMEOUT;
799
800 case -EIO:
801 return DBUS_ERROR_IO_ERROR;
802
803 case -ENETRESET:
804 case -ECONNABORTED:
805 case -ECONNRESET:
806 return DBUS_ERROR_DISCONNECTED;
807 }
808
809 return DBUS_ERROR_FAILED;
810}
811
812DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
813 DBusMessage *reply = NULL;
814 const char *name, *text;
815
816 if (bus_error && dbus_error_is_set(bus_error)) {
817 name = bus_error->name;
818 text = bus_error->message;
819 } else {
820 name = error_to_dbus(error);
821 text = strerror(-error);
822 }
823
824 if (!(reply = dbus_message_new_error(message, name, text)))
825 goto oom;
826
f278026d 827 if (!dbus_connection_send(m->api_bus, reply, NULL))
ea430986
LP
828 goto oom;
829
830 dbus_message_unref(reply);
831
832 if (bus_error)
833 dbus_error_free(bus_error);
834
835 return DBUS_HANDLER_RESULT_HANDLED;
836
837oom:
838 if (reply)
839 dbus_message_unref(reply);
840
841 if (bus_error)
842 dbus_error_free(bus_error);
843
844 return DBUS_HANDLER_RESULT_NEED_MEMORY;
845}
846
847int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
848 const char *t = data;
849
850 assert(m);
851 assert(i);
852 assert(property);
38131695
LP
853
854 if (!t)
855 t = "";
ea430986
LP
856
857 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
858 return -ENOMEM;
859
860 return 0;
861}
862
863int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
864 DBusMessageIter sub;
865 char **t = data;
866
867 assert(m);
868 assert(i);
869 assert(property);
870
871 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
872 return -ENOMEM;
873
874 STRV_FOREACH(t, t)
875 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
876 return -ENOMEM;
877
878 if (!dbus_message_iter_close_container(i, &sub))
879 return -ENOMEM;
880
881 return 0;
882}
883
884int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
885 bool *b = data;
886 dbus_bool_t db;
887
888 assert(m);
889 assert(i);
890 assert(property);
891 assert(b);
892
893 db = *b;
894
895 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
896 return -ENOMEM;
897
898 return 0;
899}
38131695
LP
900
901int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
902 assert(m);
903 assert(i);
904 assert(property);
905 assert(data);
906
907 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
908 return -ENOMEM;
909
910 return 0;
911}
912
913int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
914 assert(m);
915 assert(i);
916 assert(property);
917 assert(data);
918
919 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
920 return -ENOMEM;
921
922 return 0;
923}