]> git.ipfire.org Git - people/ms/systemd.git/blame - dbus.c
udev: introduce SYSTEMD_EXPOSE as binary udev property for hiding/showing devices...
[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);
617 dbus_connection_unref(m->api_bus);
618 m->api_bus = NULL;
8e274523 619
ea430986 620 }
c1e1601e
LP
621
622 if (m->subscribed) {
623 char *c;
624
625 while ((c = set_steal_first(m->subscribed)))
626 free(c);
627
628 set_free(m->subscribed);
629 m->subscribed = NULL;
630 }
ea430986
LP
631}
632
f278026d
LP
633void bus_done_system(Manager *m) {
634 assert(m);
635
636 if (m->system_bus == m->api_bus)
637 bus_done_api(m);
638
639 if (m->system_bus) {
640 dbus_connection_close(m->system_bus);
641 dbus_connection_unref(m->system_bus);
642 m->system_bus = NULL;
643 }
644}
645
ea430986
LP
646DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
647 DBusError error;
648 DBusMessage *reply = NULL;
649 int r;
650
651 assert(m);
652 assert(message);
653
654 dbus_error_init(&error);
655
656 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
657
658 if (!(reply = dbus_message_new_method_return(message)))
659 goto oom;
660
661 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
662 goto oom;
663
664 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
665 const char *interface, *property;
666 const BusProperty *p;
667
668 if (!dbus_message_get_args(
669 message,
670 &error,
671 DBUS_TYPE_STRING, &interface,
672 DBUS_TYPE_STRING, &property,
673 DBUS_TYPE_INVALID))
674 return bus_send_error_reply(m, message, &error, -EINVAL);
675
676 for (p = properties; p->property; p++)
677 if (streq(p->interface, interface) && streq(p->property, property))
678 break;
679
680 if (p->property) {
681 DBusMessageIter iter, sub;
682
683 if (!(reply = dbus_message_new_method_return(message)))
684 goto oom;
685
686 dbus_message_iter_init_append(reply, &iter);
687
688 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
689 goto oom;
690
691 if ((r = p->append(m, &sub, property, p->data)) < 0) {
692
693 if (r == -ENOMEM)
694 goto oom;
695
696 dbus_message_unref(reply);
697 return bus_send_error_reply(m, message, NULL, r);
698 }
699
700 if (!dbus_message_iter_close_container(&iter, &sub))
701 goto oom;
702 }
703 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
704 const char *interface;
705 const BusProperty *p;
706 DBusMessageIter iter, sub, sub2, sub3;
707 bool any = false;
708
709 if (!dbus_message_get_args(
710 message,
711 &error,
712 DBUS_TYPE_STRING, &interface,
713 DBUS_TYPE_INVALID))
714 return bus_send_error_reply(m, message, &error, -EINVAL);
715
716 if (!(reply = dbus_message_new_method_return(message)))
717 goto oom;
718
719 dbus_message_iter_init_append(reply, &iter);
720
721 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
722 goto oom;
723
724 for (p = properties; p->property; p++) {
725 if (!streq(p->interface, interface))
726 continue;
727
c401a1e0 728 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
ea430986
LP
729 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
730 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
731 goto oom;
732
733 if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
734
735 if (r == -ENOMEM)
736 goto oom;
737
738 dbus_message_unref(reply);
739 return bus_send_error_reply(m, message, NULL, r);
740 }
741
742 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
743 !dbus_message_iter_close_container(&sub, &sub2))
744 goto oom;
745
746 any = true;
747 }
748
749 if (!dbus_message_iter_close_container(&iter, &sub))
750 goto oom;
751 }
752
753 if (reply) {
f278026d 754 if (!dbus_connection_send(m->api_bus, reply, NULL))
ea430986
LP
755 goto oom;
756
757 dbus_message_unref(reply);
758 return DBUS_HANDLER_RESULT_HANDLED;
759 }
760
761 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
762
763oom:
764 if (reply)
765 dbus_message_unref(reply);
766
767 dbus_error_free(&error);
768
769 return DBUS_HANDLER_RESULT_NEED_MEMORY;
770}
771
ea430986
LP
772static const char *error_to_dbus(int error) {
773
774 switch(error) {
775
776 case -EINVAL:
777 return DBUS_ERROR_INVALID_ARGS;
778
779 case -ENOMEM:
780 return DBUS_ERROR_NO_MEMORY;
781
782 case -EPERM:
783 case -EACCES:
784 return DBUS_ERROR_ACCESS_DENIED;
785
786 case -ESRCH:
787 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
788
789 case -ENOENT:
790 return DBUS_ERROR_FILE_NOT_FOUND;
791
792 case -EEXIST:
793 return DBUS_ERROR_FILE_EXISTS;
794
795 case -ETIMEDOUT:
796 return DBUS_ERROR_TIMEOUT;
797
798 case -EIO:
799 return DBUS_ERROR_IO_ERROR;
800
801 case -ENETRESET:
802 case -ECONNABORTED:
803 case -ECONNRESET:
804 return DBUS_ERROR_DISCONNECTED;
805 }
806
807 return DBUS_ERROR_FAILED;
808}
809
810DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
811 DBusMessage *reply = NULL;
812 const char *name, *text;
813
814 if (bus_error && dbus_error_is_set(bus_error)) {
815 name = bus_error->name;
816 text = bus_error->message;
817 } else {
818 name = error_to_dbus(error);
819 text = strerror(-error);
820 }
821
822 if (!(reply = dbus_message_new_error(message, name, text)))
823 goto oom;
824
f278026d 825 if (!dbus_connection_send(m->api_bus, reply, NULL))
ea430986
LP
826 goto oom;
827
828 dbus_message_unref(reply);
829
830 if (bus_error)
831 dbus_error_free(bus_error);
832
833 return DBUS_HANDLER_RESULT_HANDLED;
834
835oom:
836 if (reply)
837 dbus_message_unref(reply);
838
839 if (bus_error)
840 dbus_error_free(bus_error);
841
842 return DBUS_HANDLER_RESULT_NEED_MEMORY;
843}
844
845int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
846 const char *t = data;
847
848 assert(m);
849 assert(i);
850 assert(property);
38131695
LP
851
852 if (!t)
853 t = "";
ea430986
LP
854
855 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
856 return -ENOMEM;
857
858 return 0;
859}
860
861int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
862 DBusMessageIter sub;
863 char **t = data;
864
865 assert(m);
866 assert(i);
867 assert(property);
868
869 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
870 return -ENOMEM;
871
872 STRV_FOREACH(t, t)
873 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
874 return -ENOMEM;
875
876 if (!dbus_message_iter_close_container(i, &sub))
877 return -ENOMEM;
878
879 return 0;
880}
881
882int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
883 bool *b = data;
884 dbus_bool_t db;
885
886 assert(m);
887 assert(i);
888 assert(property);
889 assert(b);
890
891 db = *b;
892
893 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
894 return -ENOMEM;
895
896 return 0;
897}
38131695
LP
898
899int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
900 assert(m);
901 assert(i);
902 assert(property);
903 assert(data);
904
905 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
906 return -ENOMEM;
907
908 return 0;
909}
910
911int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
912 assert(m);
913 assert(i);
914 assert(property);
915 assert(data);
916
917 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
918 return -ENOMEM;
919
920 return 0;
921}