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