1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
22 #include <dbus/dbus.h>
24 #include <sys/epoll.h>
25 #include <sys/timerfd.h>
33 static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
39 m->request_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
42 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
48 /* no watch flags for disabled watches */
49 if (!dbus_watch_get_enabled(bus_watch))
52 flags = dbus_watch_get_flags(bus_watch);
54 if (flags & DBUS_WATCH_READABLE)
56 if (flags & DBUS_WATCH_WRITABLE)
59 return events | EPOLLHUP | EPOLLERR;
62 static unsigned events_to_bus_flags(uint32_t events) {
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;
77 void bus_watch_event(Manager *m, Watch *w, int events) {
81 /* This is called by the event loop whenever there is
82 * something happening on D-Bus' file handles. */
84 if (!(dbus_watch_get_enabled(w->data.bus_watch)))
87 dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
90 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
93 struct epoll_event ev;
98 if (!(w = new0(Watch, 1)))
101 w->fd = dbus_watch_get_unix_fd(bus_watch);
102 w->type = WATCH_DBUS_WATCH;
103 w->data.bus_watch = bus_watch;
106 ev.events = bus_flags_to_events(bus_watch);
109 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
111 if (errno != EEXIST) {
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(). */
121 if ((w->fd = dup(w->fd)) < 0) {
126 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
128 close_nointr_nofail(w->fd);
132 w->fd_is_dupped = true;
135 dbus_watch_set_data(bus_watch, w, NULL);
140 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
147 if (!(w = dbus_watch_get_data(bus_watch)))
150 assert(w->type == WATCH_DBUS_WATCH);
151 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
154 close_nointr_nofail(w->fd);
159 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
162 struct epoll_event ev;
167 assert_se(w = dbus_watch_get_data(bus_watch));
168 assert(w->type == WATCH_DBUS_WATCH);
171 ev.events = bus_flags_to_events(bus_watch);
174 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
177 static int bus_timeout_arm(Manager *m, Watch *w) {
178 struct itimerspec its;
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;
190 if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
196 void bus_timeout_event(Manager *m, Watch *w, int events) {
200 /* This is called by the event loop whenever there is
201 * something happening on D-Bus' file handles. */
203 if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
206 dbus_timeout_handle(w->data.bus_timeout);
209 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
212 struct epoll_event ev;
217 if (!(w = new0(Watch, 1)))
220 if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
223 w->type = WATCH_DBUS_TIMEOUT;
224 w->data.bus_timeout = timeout;
226 if (bus_timeout_arm(m, w) < 0)
233 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
236 dbus_timeout_set_data(timeout, w, NULL);
242 close_nointr_nofail(w->fd);
248 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
255 if (!(w = dbus_timeout_get_data(timeout)))
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);
264 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
272 assert_se(w = dbus_timeout_get_data(timeout));
273 assert(w->type == WATCH_DBUS_TIMEOUT);
275 if ((r = bus_timeout_arm(m, w)) < 0)
276 log_error("Failed to rearm timer: %s", strerror(-r));
279 static DBusHandlerResult bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
287 dbus_error_init(&error);
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)); */
294 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
295 log_error("Warning! D-Bus connection terminated.");
297 /* FIXME: we probably should restart D-Bus here */
299 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
300 const char *name, *old, *new;
302 if (!dbus_message_get_args(message, &error,
303 DBUS_TYPE_STRING, &name,
304 DBUS_TYPE_STRING, &old,
305 DBUS_TYPE_STRING, &new,
307 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
309 if (set_remove(m->subscribed, (char*) name))
310 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(m->subscribed));
314 dbus_error_free(&error);
315 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
318 unsigned bus_dispatch(Manager *m) {
321 if (!m->request_bus_dispatch)
324 if (dbus_connection_dispatch(m->bus) == DBUS_DISPATCH_COMPLETE)
325 m->request_bus_dispatch = false;
330 static int request_name(Manager *m) {
331 DBusMessage *message;
332 const char *name = "org.freedesktop.systemd1";
335 if (!(message = dbus_message_new_method_call(
342 if (!dbus_message_append_args(
344 DBUS_TYPE_STRING, &name,
345 DBUS_TYPE_UINT32, &flags,
346 DBUS_TYPE_INVALID)) {
347 dbus_message_unref(message);
351 if (!dbus_connection_send(m->bus, message, NULL)) {
352 dbus_message_unref(message);
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
360 dbus_message_unref(message);
365 int bus_init(Manager *m) {
375 if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
378 dbus_connection_set_change_sigpipe(FALSE);
380 dbus_error_init(&error);
381 if (!(m->bus = dbus_bus_get_private(m->running_as == MANAGER_USER ? 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;
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)) {
399 dbus_bus_add_match(m->bus,
401 "sender='"DBUS_SERVICE_DBUS"',"
402 "interface='"DBUS_INTERFACE_DBUS"',"
403 "path='"DBUS_PATH_DBUS"'",
406 if (dbus_error_is_set(&error)) {
407 log_error("Failed to register match: %s", error.message);
408 dbus_error_free(&error);
413 if ((r = request_name(m)) < 0) {
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)));
423 m->request_bus_dispatch = true;
428 void bus_done(Manager *m) {
432 dbus_connection_close(m->bus);
433 dbus_connection_unref(m->bus);
440 while ((c = set_steal_first(m->subscribed)))
443 set_free(m->subscribed);
444 m->subscribed = NULL;
448 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
450 DBusMessage *reply = NULL;
456 dbus_error_init(&error);
458 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
460 if (!(reply = dbus_message_new_method_return(message)))
463 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
466 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
467 const char *interface, *property;
468 const BusProperty *p;
470 if (!dbus_message_get_args(
473 DBUS_TYPE_STRING, &interface,
474 DBUS_TYPE_STRING, &property,
476 return bus_send_error_reply(m, message, &error, -EINVAL);
478 for (p = properties; p->property; p++)
479 if (streq(p->interface, interface) && streq(p->property, property))
483 DBusMessageIter iter, sub;
485 if (!(reply = dbus_message_new_method_return(message)))
488 dbus_message_iter_init_append(reply, &iter);
490 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
493 if ((r = p->append(m, &sub, property, p->data)) < 0) {
498 dbus_message_unref(reply);
499 return bus_send_error_reply(m, message, NULL, r);
502 if (!dbus_message_iter_close_container(&iter, &sub))
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;
511 if (!dbus_message_get_args(
514 DBUS_TYPE_STRING, &interface,
516 return bus_send_error_reply(m, message, &error, -EINVAL);
518 if (!(reply = dbus_message_new_method_return(message)))
521 dbus_message_iter_init_append(reply, &iter);
523 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
526 for (p = properties; p->property; p++) {
527 if (!streq(p->interface, interface))
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))
535 if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
540 dbus_message_unref(reply);
541 return bus_send_error_reply(m, message, NULL, r);
544 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
545 !dbus_message_iter_close_container(&sub, &sub2))
551 if (!dbus_message_iter_close_container(&iter, &sub))
556 if (!dbus_connection_send(m->bus, reply, NULL))
559 dbus_message_unref(reply);
560 return DBUS_HANDLER_RESULT_HANDLED;
563 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
567 dbus_message_unref(reply);
569 dbus_error_free(&error);
571 return DBUS_HANDLER_RESULT_NEED_MEMORY;
576 static const char *error_to_dbus(int error) {
581 return DBUS_ERROR_INVALID_ARGS;
584 return DBUS_ERROR_NO_MEMORY;
588 return DBUS_ERROR_ACCESS_DENIED;
591 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
594 return DBUS_ERROR_FILE_NOT_FOUND;
597 return DBUS_ERROR_FILE_EXISTS;
600 return DBUS_ERROR_TIMEOUT;
603 return DBUS_ERROR_IO_ERROR;
608 return DBUS_ERROR_DISCONNECTED;
611 return DBUS_ERROR_FAILED;
614 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
615 DBusMessage *reply = NULL;
616 const char *name, *text;
618 if (bus_error && dbus_error_is_set(bus_error)) {
619 name = bus_error->name;
620 text = bus_error->message;
622 name = error_to_dbus(error);
623 text = strerror(-error);
626 if (!(reply = dbus_message_new_error(message, name, text)))
629 if (!dbus_connection_send(m->bus, reply, NULL))
632 dbus_message_unref(reply);
635 dbus_error_free(bus_error);
637 return DBUS_HANDLER_RESULT_HANDLED;
641 dbus_message_unref(reply);
644 dbus_error_free(bus_error);
646 return DBUS_HANDLER_RESULT_NEED_MEMORY;
649 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
650 const char *t = data;
659 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
665 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
673 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
677 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
680 if (!dbus_message_iter_close_container(i, &sub))
686 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
697 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
703 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
709 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
715 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
721 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))