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 void bus_dispatch(Manager *m) {
282 if (dbus_connection_dispatch(m->bus) == DBUS_DISPATCH_COMPLETE)
283 m->request_bus_dispatch = false;
286 static int request_name(Manager *m) {
287 DBusMessage *message;
288 const char *name = "org.freedesktop.systemd1";
291 if (!(message = dbus_message_new_method_call(
298 if (!dbus_message_append_args(
300 DBUS_TYPE_STRING, &name,
301 DBUS_TYPE_UINT32, &flags,
302 DBUS_TYPE_INVALID)) {
303 dbus_message_unref(message);
307 if (!dbus_connection_send(m->bus, message, NULL)) {
308 dbus_message_unref(message);
312 /* We simple ask for the name and don't wait for it. Sooner or
313 * later we'll have it, and we wouldn't know what to do on
316 dbus_message_unref(message);
321 int bus_init(Manager *m) {
331 dbus_connection_set_change_sigpipe(FALSE);
333 dbus_error_init(&error);
334 if (!(m->bus = dbus_bus_get_private(m->is_init ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &error))) {
335 log_error("Failed to get D-Bus connection: %s", error.message);
336 dbus_error_free(&error);
337 return -ECONNREFUSED;
340 dbus_connection_set_exit_on_disconnect(m->bus, FALSE);
341 dbus_connection_set_dispatch_status_function(m->bus, bus_dispatch_status, m, NULL);
342 if (!dbus_connection_set_watch_functions(m->bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
343 !dbus_connection_set_timeout_functions(m->bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL) ||
344 !dbus_connection_register_object_path(m->bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
345 !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
346 !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m)) {
351 if ((r = request_name(m)) < 0) {
356 log_debug("Successfully connected to D-Bus bus %s as %s",
357 strnull((id = dbus_connection_get_server_id(m->bus))),
358 strnull(dbus_bus_get_unique_name(m->bus)));
361 m->request_bus_dispatch = true;
366 void bus_done(Manager *m) {
370 dbus_connection_close(m->bus);
371 dbus_connection_unref(m->bus);
376 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
378 DBusMessage *reply = NULL;
384 dbus_error_init(&error);
386 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
388 if (!(reply = dbus_message_new_method_return(message)))
391 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
394 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
395 const char *interface, *property;
396 const BusProperty *p;
398 if (!dbus_message_get_args(
401 DBUS_TYPE_STRING, &interface,
402 DBUS_TYPE_STRING, &property,
404 return bus_send_error_reply(m, message, &error, -EINVAL);
406 for (p = properties; p->property; p++)
407 if (streq(p->interface, interface) && streq(p->property, property))
411 DBusMessageIter iter, sub;
413 if (!(reply = dbus_message_new_method_return(message)))
416 dbus_message_iter_init_append(reply, &iter);
418 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
421 if ((r = p->append(m, &sub, property, p->data)) < 0) {
426 dbus_message_unref(reply);
427 return bus_send_error_reply(m, message, NULL, r);
430 if (!dbus_message_iter_close_container(&iter, &sub))
433 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
434 const char *interface;
435 const BusProperty *p;
436 DBusMessageIter iter, sub, sub2, sub3;
439 if (!dbus_message_get_args(
442 DBUS_TYPE_STRING, &interface,
444 return bus_send_error_reply(m, message, &error, -EINVAL);
446 if (!(reply = dbus_message_new_method_return(message)))
449 dbus_message_iter_init_append(reply, &iter);
451 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
454 for (p = properties; p->property; p++) {
455 if (!streq(p->interface, interface))
458 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
459 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
460 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
463 if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
468 dbus_message_unref(reply);
469 return bus_send_error_reply(m, message, NULL, r);
472 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
473 !dbus_message_iter_close_container(&sub, &sub2))
479 if (!dbus_message_iter_close_container(&iter, &sub))
484 if (!dbus_connection_send(m->bus, reply, NULL))
487 dbus_message_unref(reply);
488 return DBUS_HANDLER_RESULT_HANDLED;
491 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
495 dbus_message_unref(reply);
497 dbus_error_free(&error);
499 return DBUS_HANDLER_RESULT_NEED_MEMORY;
504 static const char *error_to_dbus(int error) {
509 return DBUS_ERROR_INVALID_ARGS;
512 return DBUS_ERROR_NO_MEMORY;
516 return DBUS_ERROR_ACCESS_DENIED;
519 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
522 return DBUS_ERROR_FILE_NOT_FOUND;
525 return DBUS_ERROR_FILE_EXISTS;
528 return DBUS_ERROR_TIMEOUT;
531 return DBUS_ERROR_IO_ERROR;
536 return DBUS_ERROR_DISCONNECTED;
539 return DBUS_ERROR_FAILED;
542 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
543 DBusMessage *reply = NULL;
544 const char *name, *text;
546 if (bus_error && dbus_error_is_set(bus_error)) {
547 name = bus_error->name;
548 text = bus_error->message;
550 name = error_to_dbus(error);
551 text = strerror(-error);
554 if (!(reply = dbus_message_new_error(message, name, text)))
557 if (!dbus_connection_send(m->bus, reply, NULL))
560 dbus_message_unref(reply);
563 dbus_error_free(bus_error);
565 return DBUS_HANDLER_RESULT_HANDLED;
569 dbus_message_unref(reply);
572 dbus_error_free(bus_error);
574 return DBUS_HANDLER_RESULT_NEED_MEMORY;
577 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
578 const char *t = data;
587 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
593 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
601 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
605 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
608 if (!dbus_message_iter_close_container(i, &sub))
614 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
625 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
631 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
637 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
643 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
649 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))