chiark / gitweb /
config: implement search path logic
[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
33 static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data)  {
34         Manager *m = data;
35
36         assert(bus);
37         assert(m);
38
39         m->request_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
40 }
41
42 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
43         unsigned flags;
44         uint32_t events = 0;
45
46         assert(bus_watch);
47
48         /* no watch flags for disabled watches */
49         if (!dbus_watch_get_enabled(bus_watch))
50                 return 0;
51
52         flags = dbus_watch_get_flags(bus_watch);
53
54         if (flags & DBUS_WATCH_READABLE)
55                 events |= EPOLLIN;
56         if (flags & DBUS_WATCH_WRITABLE)
57                 events |= EPOLLOUT;
58
59         return events | EPOLLHUP | EPOLLERR;
60 }
61
62 static unsigned events_to_bus_flags(uint32_t events) {
63         unsigned flags = 0;
64
65         if (events & EPOLLIN)
66                 flags |= DBUS_WATCH_READABLE;
67         if (events & EPOLLOUT)
68                 flags |= DBUS_WATCH_WRITABLE;
69         if (events & EPOLLHUP)
70                 flags |= DBUS_WATCH_HANGUP;
71         if (events & EPOLLERR)
72                 flags |= DBUS_WATCH_ERROR;
73
74         return flags;
75 }
76
77 void bus_watch_event(Manager *m, Watch *w, int events) {
78         assert(m);
79         assert(w);
80
81         /* This is called by the event loop whenever there is
82          * something happening on D-Bus' file handles. */
83
84         if (!(dbus_watch_get_enabled(w->data.bus_watch)))
85                 return;
86
87         dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
88 }
89
90 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
91         Manager *m = data;
92         Watch *w;
93         struct epoll_event ev;
94
95         assert(bus_watch);
96         assert(m);
97
98         if (!(w = new0(Watch, 1)))
99                 return FALSE;
100
101         w->fd = dbus_watch_get_unix_fd(bus_watch);
102         w->type = WATCH_DBUS_WATCH;
103         w->data.bus_watch = bus_watch;
104
105         zero(ev);
106         ev.events = bus_flags_to_events(bus_watch);
107         ev.data.ptr = w;
108
109         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
110
111                 if (errno != EEXIST) {
112                         free(w);
113                         return FALSE;
114                 }
115
116                 /* Hmm, bloody D-Bus creates multiple watches on the
117                  * same fd. epoll() does not like that. As a dirty
118                  * hack we simply dup() the fd and hence get a second
119                  * one we can safely add to the epoll(). */
120
121                 if ((w->fd = dup(w->fd)) < 0) {
122                         free(w);
123                         return FALSE;
124                 }
125
126                 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
127                         free(w);
128                         close_nointr_nofail(w->fd);
129                         return FALSE;
130                 }
131
132                 w->fd_is_dupped = true;
133         }
134
135         dbus_watch_set_data(bus_watch, w, NULL);
136
137         return TRUE;
138 }
139
140 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
141         Manager *m = data;
142         Watch *w;
143
144         assert(bus_watch);
145         assert(m);
146
147         if (!(w = dbus_watch_get_data(bus_watch)))
148                 return;
149
150         assert(w->type == WATCH_DBUS_WATCH);
151         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
152
153         if (w->fd_is_dupped)
154                 close_nointr_nofail(w->fd);
155
156         free(w);
157 }
158
159 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
160         Manager *m = data;
161         Watch *w;
162         struct epoll_event ev;
163
164         assert(bus_watch);
165         assert(m);
166
167         assert_se(w = dbus_watch_get_data(bus_watch));
168         assert(w->type == WATCH_DBUS_WATCH);
169
170         zero(ev);
171         ev.events = bus_flags_to_events(bus_watch);
172         ev.data.ptr = w;
173
174         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
175 }
176
177 static int bus_timeout_arm(Manager *m, Watch *w) {
178         struct itimerspec its;
179
180         assert(m);
181         assert(w);
182
183         zero(its);
184
185         if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
186                 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
187                 its.it_interval = its.it_interval;
188         }
189
190         if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
191                 return -errno;
192
193         return 0;
194 }
195
196 void bus_timeout_event(Manager *m, Watch *w, int events) {
197         assert(m);
198         assert(w);
199
200         /* This is called by the event loop whenever there is
201          * something happening on D-Bus' file handles. */
202
203         if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
204                 return;
205
206         dbus_timeout_handle(w->data.bus_timeout);
207 }
208
209 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
210         Manager *m = data;
211         Watch *w;
212         struct epoll_event ev;
213
214         assert(timeout);
215         assert(m);
216
217         if (!(w = new0(Watch, 1)))
218                 return FALSE;
219
220         if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
221                 goto fail;
222
223         w->type = WATCH_DBUS_TIMEOUT;
224         w->data.bus_timeout = timeout;
225
226         if (bus_timeout_arm(m, w) < 0)
227                 goto fail;
228
229         zero(ev);
230         ev.events = EPOLLIN;
231         ev.data.ptr = w;
232
233         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
234                 goto fail;
235
236         dbus_timeout_set_data(timeout, w, NULL);
237
238         return TRUE;
239
240 fail:
241         if (w->fd >= 0)
242                 close_nointr_nofail(w->fd);
243
244         free(w);
245         return FALSE;
246 }
247
248 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
249         Manager *m = data;
250         Watch *w;
251
252         assert(timeout);
253         assert(m);
254
255         if (!(w = dbus_timeout_get_data(timeout)))
256                 return;
257
258         assert(w->type == WATCH_DBUS_TIMEOUT);
259         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
260         close_nointr_nofail(w->fd);
261         free(w);
262 }
263
264 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
265         Manager *m = data;
266         Watch *w;
267         int r;
268
269         assert(timeout);
270         assert(m);
271
272         assert_se(w = dbus_timeout_get_data(timeout));
273         assert(w->type == WATCH_DBUS_TIMEOUT);
274
275         if ((r = bus_timeout_arm(m, w)) < 0)
276                 log_error("Failed to rearm timer: %s", strerror(-r));
277 }
278
279 static DBusHandlerResult bus_message_filter(DBusConnection  *connection, DBusMessage  *message, void *data) {
280         Manager *m = data;
281         DBusError error;
282
283         assert(connection);
284         assert(message);
285         assert(m);
286
287         dbus_error_init(&error);
288
289         /* log_debug("Got D-Bus request: %s.%s() on %s", */
290         /*           dbus_message_get_interface(message), */
291         /*           dbus_message_get_member(message), */
292         /*           dbus_message_get_path(message)); */
293
294         if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
295                 log_error("Warning! D-Bus connection terminated.");
296
297                 /* FIXME: we probably should restart D-Bus here */
298
299         } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
300                 const char *name, *old, *new;
301
302                 if (!dbus_message_get_args(message, &error,
303                                            DBUS_TYPE_STRING, &name,
304                                            DBUS_TYPE_STRING, &old,
305                                            DBUS_TYPE_STRING, &new,
306                                            DBUS_TYPE_INVALID))
307                         log_error("Failed to parse NameOwnerChanged message: %s", error.message);
308                 else  {
309                         if (set_remove(m->subscribed, (char*) name))
310                                 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(m->subscribed));
311                 }
312         }
313
314         dbus_error_free(&error);
315         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
316 }
317
318 unsigned bus_dispatch(Manager *m) {
319         assert(m);
320
321         if (!m->request_bus_dispatch)
322                 return 0;
323
324         if (dbus_connection_dispatch(m->bus) == DBUS_DISPATCH_COMPLETE)
325                 m->request_bus_dispatch = false;
326
327         return 1;
328 }
329
330 static int request_name(Manager *m) {
331         DBusMessage *message;
332         const char *name = "org.freedesktop.systemd1";
333         uint32_t flags = 0;
334
335         if (!(message = dbus_message_new_method_call(
336                               DBUS_SERVICE_DBUS,
337                               DBUS_PATH_DBUS,
338                               DBUS_INTERFACE_DBUS,
339                               "RequestName")))
340                 return -ENOMEM;
341
342         if (!dbus_message_append_args(
343                             message,
344                             DBUS_TYPE_STRING, &name,
345                             DBUS_TYPE_UINT32, &flags,
346                             DBUS_TYPE_INVALID)) {
347                 dbus_message_unref(message);
348                 return -ENOMEM;
349         }
350
351         if (!dbus_connection_send(m->bus, message, NULL)) {
352                 dbus_message_unref(message);
353                 return -ENOMEM;
354         }
355
356         /* We simple ask for the name and don't wait for it. Sooner or
357          * later we'll have it, and we wouldn't know what to do on
358          * error anyway. */
359
360         dbus_message_unref(message);
361
362         return 0;
363 }
364
365 int bus_init(Manager *m) {
366         DBusError error;
367         char *id;
368         int r;
369
370         assert(m);
371
372         if (m->bus)
373                 return 0;
374
375         if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
376                 return -ENOMEM;
377
378         dbus_connection_set_change_sigpipe(FALSE);
379
380         dbus_error_init(&error);
381         if (!(m->bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
382                 log_error("Failed to get D-Bus connection: %s", error.message);
383                 dbus_error_free(&error);
384                 return -ECONNREFUSED;
385         }
386
387         dbus_connection_set_exit_on_disconnect(m->bus, FALSE);
388         dbus_connection_set_dispatch_status_function(m->bus, bus_dispatch_status, m, NULL);
389         if (!dbus_connection_set_watch_functions(m->bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
390             !dbus_connection_set_timeout_functions(m->bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL) ||
391             !dbus_connection_register_object_path(m->bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
392             !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
393             !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
394             !dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL)) {
395                 bus_done(m);
396                 return -ENOMEM;
397         }
398
399         dbus_bus_add_match(m->bus,
400                            "type='signal',"
401                            "sender='"DBUS_SERVICE_DBUS"',"
402                            "interface='"DBUS_INTERFACE_DBUS"',"
403                            "path='"DBUS_PATH_DBUS"'",
404                            &error);
405
406         if (dbus_error_is_set(&error)) {
407                 log_error("Failed to register match: %s", error.message);
408                 dbus_error_free(&error);
409                 bus_done(m);
410                 return -ENOMEM;
411         }
412
413         if ((r = request_name(m)) < 0) {
414                 bus_done(m);
415                 return r;
416         }
417
418         log_debug("Successfully connected to D-Bus bus %s as %s",
419                   strnull((id = dbus_connection_get_server_id(m->bus))),
420                   strnull(dbus_bus_get_unique_name(m->bus)));
421         dbus_free(id);
422
423         m->request_bus_dispatch = true;
424
425         return 0;
426 }
427
428 void bus_done(Manager *m) {
429         assert(m);
430
431         if (m->bus) {
432                 dbus_connection_close(m->bus);
433                 dbus_connection_unref(m->bus);
434                 m->bus = NULL;
435         }
436
437         if (m->subscribed) {
438                 char *c;
439
440                 while ((c = set_steal_first(m->subscribed)))
441                         free(c);
442
443                 set_free(m->subscribed);
444                 m->subscribed = NULL;
445         }
446 }
447
448 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
449         DBusError error;
450         DBusMessage *reply = NULL;
451         int r;
452
453         assert(m);
454         assert(message);
455
456         dbus_error_init(&error);
457
458         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
459
460                 if (!(reply = dbus_message_new_method_return(message)))
461                         goto oom;
462
463                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
464                         goto oom;
465
466         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
467                 const char *interface, *property;
468                 const BusProperty *p;
469
470                 if (!dbus_message_get_args(
471                             message,
472                             &error,
473                             DBUS_TYPE_STRING, &interface,
474                             DBUS_TYPE_STRING, &property,
475                             DBUS_TYPE_INVALID))
476                         return bus_send_error_reply(m, message, &error, -EINVAL);
477
478                 for (p = properties; p->property; p++)
479                         if (streq(p->interface, interface) && streq(p->property, property))
480                                 break;
481
482                 if (p->property) {
483                         DBusMessageIter iter, sub;
484
485                         if (!(reply = dbus_message_new_method_return(message)))
486                                 goto oom;
487
488                         dbus_message_iter_init_append(reply, &iter);
489
490                         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
491                                 goto oom;
492
493                         if ((r = p->append(m, &sub, property, p->data)) < 0) {
494
495                                 if (r == -ENOMEM)
496                                         goto oom;
497
498                                 dbus_message_unref(reply);
499                                 return bus_send_error_reply(m, message, NULL, r);
500                         }
501
502                         if (!dbus_message_iter_close_container(&iter, &sub))
503                                 goto oom;
504                 }
505         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
506                 const char *interface;
507                 const BusProperty *p;
508                 DBusMessageIter iter, sub, sub2, sub3;
509                 bool any = false;
510
511                 if (!dbus_message_get_args(
512                             message,
513                             &error,
514                             DBUS_TYPE_STRING, &interface,
515                             DBUS_TYPE_INVALID))
516                         return bus_send_error_reply(m, message, &error, -EINVAL);
517
518                 if (!(reply = dbus_message_new_method_return(message)))
519                         goto oom;
520
521                 dbus_message_iter_init_append(reply, &iter);
522
523                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
524                         goto oom;
525
526                 for (p = properties; p->property; p++) {
527                         if (!streq(p->interface, interface))
528                                 continue;
529
530                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
531                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
532                             !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
533                                 goto oom;
534
535                         if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
536
537                                 if (r == -ENOMEM)
538                                         goto oom;
539
540                                 dbus_message_unref(reply);
541                                 return bus_send_error_reply(m, message, NULL, r);
542                         }
543
544                         if (!dbus_message_iter_close_container(&sub2, &sub3) ||
545                             !dbus_message_iter_close_container(&sub, &sub2))
546                                 goto oom;
547
548                         any = true;
549                 }
550
551                 if (!dbus_message_iter_close_container(&iter, &sub))
552                         goto oom;
553         }
554
555         if (reply) {
556                 if (!dbus_connection_send(m->bus, reply, NULL))
557                         goto oom;
558
559                 dbus_message_unref(reply);
560                 return DBUS_HANDLER_RESULT_HANDLED;
561         }
562
563         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
564
565 oom:
566         if (reply)
567                 dbus_message_unref(reply);
568
569         dbus_error_free(&error);
570
571         return DBUS_HANDLER_RESULT_NEED_MEMORY;
572 }
573
574
575
576 static const char *error_to_dbus(int error) {
577
578         switch(error) {
579
580         case -EINVAL:
581                 return DBUS_ERROR_INVALID_ARGS;
582
583         case -ENOMEM:
584                 return DBUS_ERROR_NO_MEMORY;
585
586         case -EPERM:
587         case -EACCES:
588                 return DBUS_ERROR_ACCESS_DENIED;
589
590         case -ESRCH:
591                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
592
593         case -ENOENT:
594                 return DBUS_ERROR_FILE_NOT_FOUND;
595
596         case -EEXIST:
597                 return DBUS_ERROR_FILE_EXISTS;
598
599         case -ETIMEDOUT:
600                 return DBUS_ERROR_TIMEOUT;
601
602         case -EIO:
603                 return DBUS_ERROR_IO_ERROR;
604
605         case -ENETRESET:
606         case -ECONNABORTED:
607         case -ECONNRESET:
608                 return DBUS_ERROR_DISCONNECTED;
609         }
610
611         return DBUS_ERROR_FAILED;
612 }
613
614 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
615         DBusMessage *reply = NULL;
616         const char *name, *text;
617
618         if (bus_error && dbus_error_is_set(bus_error)) {
619                 name = bus_error->name;
620                 text = bus_error->message;
621         } else {
622                 name = error_to_dbus(error);
623                 text = strerror(-error);
624         }
625
626         if (!(reply = dbus_message_new_error(message, name, text)))
627                 goto oom;
628
629         if (!dbus_connection_send(m->bus, reply, NULL))
630                 goto oom;
631
632         dbus_message_unref(reply);
633
634         if (bus_error)
635                 dbus_error_free(bus_error);
636
637         return DBUS_HANDLER_RESULT_HANDLED;
638
639 oom:
640         if (reply)
641                 dbus_message_unref(reply);
642
643         if (bus_error)
644                 dbus_error_free(bus_error);
645
646         return DBUS_HANDLER_RESULT_NEED_MEMORY;
647 }
648
649 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
650         const char *t = data;
651
652         assert(m);
653         assert(i);
654         assert(property);
655
656         if (!t)
657                 t = "";
658
659         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
660                 return -ENOMEM;
661
662         return 0;
663 }
664
665 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
666         DBusMessageIter sub;
667         char **t = data;
668
669         assert(m);
670         assert(i);
671         assert(property);
672
673         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
674                 return -ENOMEM;
675
676         STRV_FOREACH(t, t)
677                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
678                         return -ENOMEM;
679
680         if (!dbus_message_iter_close_container(i, &sub))
681                 return -ENOMEM;
682
683         return 0;
684 }
685
686 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
687         bool *b = data;
688         dbus_bool_t db;
689
690         assert(m);
691         assert(i);
692         assert(property);
693         assert(b);
694
695         db = *b;
696
697         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
698                 return -ENOMEM;
699
700         return 0;
701 }
702
703 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
704         assert(m);
705         assert(i);
706         assert(property);
707         assert(data);
708
709         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
710                 return -ENOMEM;
711
712         return 0;
713 }
714
715 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
716         assert(m);
717         assert(i);
718         assert(property);
719         assert(data);
720
721         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
722                 return -ENOMEM;
723
724         return 0;
725 }