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