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>
34 static void api_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
39 assert(m->api_bus == bus);
41 m->request_api_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
44 static void system_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
49 assert(m->system_bus == bus);
51 m->request_system_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
54 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
60 /* no watch flags for disabled watches */
61 if (!dbus_watch_get_enabled(bus_watch))
64 flags = dbus_watch_get_flags(bus_watch);
66 if (flags & DBUS_WATCH_READABLE)
68 if (flags & DBUS_WATCH_WRITABLE)
71 return events | EPOLLHUP | EPOLLERR;
74 static unsigned events_to_bus_flags(uint32_t events) {
78 flags |= DBUS_WATCH_READABLE;
79 if (events & EPOLLOUT)
80 flags |= DBUS_WATCH_WRITABLE;
81 if (events & EPOLLHUP)
82 flags |= DBUS_WATCH_HANGUP;
83 if (events & EPOLLERR)
84 flags |= DBUS_WATCH_ERROR;
89 void bus_watch_event(Manager *m, Watch *w, int events) {
93 /* This is called by the event loop whenever there is
94 * something happening on D-Bus' file handles. */
96 if (!dbus_watch_get_enabled(w->data.bus_watch))
99 dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
102 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
105 struct epoll_event ev;
110 if (!(w = new0(Watch, 1)))
113 w->fd = dbus_watch_get_unix_fd(bus_watch);
114 w->type = WATCH_DBUS_WATCH;
115 w->data.bus_watch = bus_watch;
118 ev.events = bus_flags_to_events(bus_watch);
121 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
123 if (errno != EEXIST) {
128 /* Hmm, bloody D-Bus creates multiple watches on the
129 * same fd. epoll() does not like that. As a dirty
130 * hack we simply dup() the fd and hence get a second
131 * one we can safely add to the epoll(). */
133 if ((w->fd = dup(w->fd)) < 0) {
138 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
140 close_nointr_nofail(w->fd);
144 w->fd_is_dupped = true;
147 dbus_watch_set_data(bus_watch, w, NULL);
152 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
159 if (!(w = dbus_watch_get_data(bus_watch)))
162 assert(w->type == WATCH_DBUS_WATCH);
163 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
166 close_nointr_nofail(w->fd);
171 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
174 struct epoll_event ev;
179 assert_se(w = dbus_watch_get_data(bus_watch));
180 assert(w->type == WATCH_DBUS_WATCH);
183 ev.events = bus_flags_to_events(bus_watch);
186 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
189 static int bus_timeout_arm(Manager *m, Watch *w) {
190 struct itimerspec its;
197 if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
198 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
199 its.it_interval = its.it_interval;
202 if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
208 void bus_timeout_event(Manager *m, Watch *w, int events) {
212 /* This is called by the event loop whenever there is
213 * something happening on D-Bus' file handles. */
215 if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
218 dbus_timeout_handle(w->data.bus_timeout);
221 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
224 struct epoll_event ev;
229 if (!(w = new0(Watch, 1)))
232 if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
235 w->type = WATCH_DBUS_TIMEOUT;
236 w->data.bus_timeout = timeout;
238 if (bus_timeout_arm(m, w) < 0)
245 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
248 dbus_timeout_set_data(timeout, w, NULL);
254 close_nointr_nofail(w->fd);
260 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
267 if (!(w = dbus_timeout_get_data(timeout)))
270 assert(w->type == WATCH_DBUS_TIMEOUT);
271 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
272 close_nointr_nofail(w->fd);
276 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
284 assert_se(w = dbus_timeout_get_data(timeout));
285 assert(w->type == WATCH_DBUS_TIMEOUT);
287 if ((r = bus_timeout_arm(m, w)) < 0)
288 log_error("Failed to rearm timer: %s", strerror(-r));
291 static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
299 dbus_error_init(&error);
301 /* log_debug("Got D-Bus request: %s.%s() on %s", */
302 /* dbus_message_get_interface(message), */
303 /* dbus_message_get_member(message), */
304 /* dbus_message_get_path(message)); */
306 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
307 log_error("Warning! D-Bus connection terminated.");
310 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
311 const char *name, *old, *new;
313 if (!dbus_message_get_args(message, &error,
314 DBUS_TYPE_STRING, &name,
315 DBUS_TYPE_STRING, &old,
316 DBUS_TYPE_STRING, &new,
318 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
320 if (set_remove(m->subscribed, (char*) name))
321 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(m->subscribed));
325 dbus_error_free(&error);
326 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
329 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
337 dbus_error_init(&error);
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)); */
344 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
345 log_error("Warning! D-Bus connection terminated.");
348 } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
351 if (!dbus_message_get_args(message, &error,
352 DBUS_TYPE_STRING, &cgroup,
354 log_error("Failed to parse Released message: %s", error.message);
356 cgroup_notify_empty(m, cgroup);
359 dbus_error_free(&error);
360 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
363 unsigned bus_dispatch(Manager *m) {
366 if (m->request_api_bus_dispatch)
367 if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE) {
368 m->request_api_bus_dispatch = false;
372 if (m->request_system_bus_dispatch)
373 if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE) {
374 m->request_system_bus_dispatch = false;
381 static void pending_cb(DBusPendingCall *pending, void *userdata) {
385 dbus_error_init(&error);
387 assert_se(reply = dbus_pending_call_steal_reply(pending));
389 switch (dbus_message_get_type(reply)) {
391 case DBUS_MESSAGE_TYPE_ERROR:
393 assert_se(dbus_set_error_from_message(&error, reply));
394 log_warning("RequestName() failed: %s", error.message);
397 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
400 if (!dbus_message_get_args(reply,
402 DBUS_TYPE_UINT32, &r,
403 DBUS_TYPE_INVALID)) {
404 log_error("Failed to parse RequestName() reply: %s", error.message);
409 log_debug("Successfully acquired name.");
411 log_error("Name already owned.");
417 assert_not_reached("Invalid reply message");
420 dbus_message_unref(reply);
421 dbus_error_free(&error);
424 static int request_name(Manager *m) {
425 DBusMessage *message;
426 const char *name = "org.freedesktop.systemd1";
428 DBusPendingCall *pending;
430 if (!(message = dbus_message_new_method_call(
437 if (!dbus_message_append_args(
439 DBUS_TYPE_STRING, &name,
440 DBUS_TYPE_UINT32, &flags,
441 DBUS_TYPE_INVALID)) {
442 dbus_message_unref(message);
446 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1)) {
447 dbus_message_unref(message);
452 dbus_message_unref(message);
454 if (!dbus_pending_call_set_notify(pending, pending_cb, NULL, NULL)) {
455 dbus_pending_call_cancel(pending);
456 dbus_pending_call_unref(pending);
461 dbus_pending_call_unref(pending);
463 /* We simple ask for the name and don't wait for it. Sooner or
464 * later we'll have it. */
469 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
473 dbus_connection_set_exit_on_disconnect(bus, FALSE);
474 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
475 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL))
481 int bus_init_system(Manager *m) {
488 dbus_error_init(&error);
493 if (m->running_as != MANAGER_SESSION && m->api_bus)
494 m->system_bus = m->api_bus;
496 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
497 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
498 dbus_error_free(&error);
502 if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
507 dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL);
510 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
515 dbus_bus_add_match(m->system_bus,
517 "interface='org.freedesktop.systemd1.Agent',"
518 "path='/org/freedesktop/systemd1/agent'",
521 if (dbus_error_is_set(&error)) {
522 log_error("Failed to register match: %s", error.message);
523 dbus_error_free(&error);
528 log_debug("Successfully connected to system D-Bus bus %s as %s",
529 strnull((id = dbus_connection_get_server_id(m->system_bus))),
530 strnull(dbus_bus_get_unique_name(m->system_bus)));
533 m->request_system_bus_dispatch = true;
538 int bus_init_api(Manager *m) {
545 dbus_error_init(&error);
550 if (m->running_as != MANAGER_SESSION && m->system_bus)
551 m->api_bus = m->system_bus;
553 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
554 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
555 dbus_error_free(&error);
559 if ((r = bus_setup_loop(m, m->api_bus)) < 0) {
564 dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL);
567 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
568 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
569 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
570 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
575 dbus_bus_add_match(m->api_bus,
577 "sender='"DBUS_SERVICE_DBUS"',"
578 "interface='"DBUS_INTERFACE_DBUS"',"
579 "path='"DBUS_PATH_DBUS"'",
582 if (dbus_error_is_set(&error)) {
583 log_error("Failed to register match: %s", error.message);
584 dbus_error_free(&error);
589 if ((r = request_name(m)) < 0) {
594 log_debug("Successfully connected to API D-Bus bus %s as %s",
595 strnull((id = dbus_connection_get_server_id(m->api_bus))),
596 strnull(dbus_bus_get_unique_name(m->api_bus)));
600 if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
603 m->request_api_bus_dispatch = true;
608 void bus_done_api(Manager *m) {
612 if (m->system_bus == m->api_bus)
613 m->system_bus = NULL;
615 dbus_connection_close(m->api_bus);
616 dbus_connection_unref(m->api_bus);
624 while ((c = set_steal_first(m->subscribed)))
627 set_free(m->subscribed);
628 m->subscribed = NULL;
632 void bus_done_system(Manager *m) {
635 if (m->system_bus == m->api_bus)
639 dbus_connection_close(m->system_bus);
640 dbus_connection_unref(m->system_bus);
641 m->system_bus = NULL;
645 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
647 DBusMessage *reply = NULL;
653 dbus_error_init(&error);
655 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
657 if (!(reply = dbus_message_new_method_return(message)))
660 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
663 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
664 const char *interface, *property;
665 const BusProperty *p;
667 if (!dbus_message_get_args(
670 DBUS_TYPE_STRING, &interface,
671 DBUS_TYPE_STRING, &property,
673 return bus_send_error_reply(m, message, &error, -EINVAL);
675 for (p = properties; p->property; p++)
676 if (streq(p->interface, interface) && streq(p->property, property))
680 DBusMessageIter iter, sub;
682 if (!(reply = dbus_message_new_method_return(message)))
685 dbus_message_iter_init_append(reply, &iter);
687 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
690 if ((r = p->append(m, &sub, property, p->data)) < 0) {
695 dbus_message_unref(reply);
696 return bus_send_error_reply(m, message, NULL, r);
699 if (!dbus_message_iter_close_container(&iter, &sub))
702 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
703 const char *interface;
704 const BusProperty *p;
705 DBusMessageIter iter, sub, sub2, sub3;
708 if (!dbus_message_get_args(
711 DBUS_TYPE_STRING, &interface,
713 return bus_send_error_reply(m, message, &error, -EINVAL);
715 if (!(reply = dbus_message_new_method_return(message)))
718 dbus_message_iter_init_append(reply, &iter);
720 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
723 for (p = properties; p->property; p++) {
724 if (!streq(p->interface, interface))
727 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
728 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
729 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
732 if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
737 dbus_message_unref(reply);
738 return bus_send_error_reply(m, message, NULL, r);
741 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
742 !dbus_message_iter_close_container(&sub, &sub2))
748 if (!dbus_message_iter_close_container(&iter, &sub))
753 if (!dbus_connection_send(m->api_bus, reply, NULL))
756 dbus_message_unref(reply);
757 return DBUS_HANDLER_RESULT_HANDLED;
760 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
764 dbus_message_unref(reply);
766 dbus_error_free(&error);
768 return DBUS_HANDLER_RESULT_NEED_MEMORY;
771 static const char *error_to_dbus(int error) {
776 return DBUS_ERROR_INVALID_ARGS;
779 return DBUS_ERROR_NO_MEMORY;
783 return DBUS_ERROR_ACCESS_DENIED;
786 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
789 return DBUS_ERROR_FILE_NOT_FOUND;
792 return DBUS_ERROR_FILE_EXISTS;
795 return DBUS_ERROR_TIMEOUT;
798 return DBUS_ERROR_IO_ERROR;
803 return DBUS_ERROR_DISCONNECTED;
806 return DBUS_ERROR_FAILED;
809 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
810 DBusMessage *reply = NULL;
811 const char *name, *text;
813 if (bus_error && dbus_error_is_set(bus_error)) {
814 name = bus_error->name;
815 text = bus_error->message;
817 name = error_to_dbus(error);
818 text = strerror(-error);
821 if (!(reply = dbus_message_new_error(message, name, text)))
824 if (!dbus_connection_send(m->api_bus, reply, NULL))
827 dbus_message_unref(reply);
830 dbus_error_free(bus_error);
832 return DBUS_HANDLER_RESULT_HANDLED;
836 dbus_message_unref(reply);
839 dbus_error_free(bus_error);
841 return DBUS_HANDLER_RESULT_NEED_MEMORY;
844 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
845 const char *t = data;
854 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
860 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
868 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
872 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
875 if (!dbus_message_iter_close_container(i, &sub))
881 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
892 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
898 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
904 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
910 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
916 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))