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