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) {
43 assert(m->api_bus == bus);
45 m->request_api_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
48 static void system_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
57 assert(m->system_bus == bus);
59 m->request_system_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
62 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
68 /* no watch flags for disabled watches */
69 if (!dbus_watch_get_enabled(bus_watch))
72 flags = dbus_watch_get_flags(bus_watch);
74 if (flags & DBUS_WATCH_READABLE)
76 if (flags & DBUS_WATCH_WRITABLE)
79 return events | EPOLLHUP | EPOLLERR;
82 static unsigned events_to_bus_flags(uint32_t events) {
86 flags |= DBUS_WATCH_READABLE;
87 if (events & EPOLLOUT)
88 flags |= DBUS_WATCH_WRITABLE;
89 if (events & EPOLLHUP)
90 flags |= DBUS_WATCH_HANGUP;
91 if (events & EPOLLERR)
92 flags |= DBUS_WATCH_ERROR;
97 void bus_watch_event(Manager *m, Watch *w, int events) {
101 /* This is called by the event loop whenever there is
102 * something happening on D-Bus' file handles. */
104 if (!dbus_watch_get_enabled(w->data.bus_watch))
107 dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
110 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
113 struct epoll_event ev;
118 if (!(w = new0(Watch, 1)))
121 w->fd = dbus_watch_get_unix_fd(bus_watch);
122 w->type = WATCH_DBUS_WATCH;
123 w->data.bus_watch = bus_watch;
126 ev.events = bus_flags_to_events(bus_watch);
129 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
131 if (errno != EEXIST) {
136 /* Hmm, bloody D-Bus creates multiple watches on the
137 * same fd. epoll() does not like that. As a dirty
138 * hack we simply dup() the fd and hence get a second
139 * one we can safely add to the epoll(). */
141 if ((w->fd = dup(w->fd)) < 0) {
146 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
148 close_nointr_nofail(w->fd);
152 w->fd_is_dupped = true;
155 dbus_watch_set_data(bus_watch, w, NULL);
160 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
167 if (!(w = dbus_watch_get_data(bus_watch)))
170 assert(w->type == WATCH_DBUS_WATCH);
171 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
174 close_nointr_nofail(w->fd);
179 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
182 struct epoll_event ev;
187 assert_se(w = dbus_watch_get_data(bus_watch));
188 assert(w->type == WATCH_DBUS_WATCH);
191 ev.events = bus_flags_to_events(bus_watch);
194 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
197 static int bus_timeout_arm(Manager *m, Watch *w) {
198 struct itimerspec its;
205 if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
206 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
207 its.it_interval = its.it_interval;
210 if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
216 void bus_timeout_event(Manager *m, Watch *w, int events) {
220 /* This is called by the event loop whenever there is
221 * something happening on D-Bus' file handles. */
223 if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
226 dbus_timeout_handle(w->data.bus_timeout);
229 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
232 struct epoll_event ev;
237 if (!(w = new0(Watch, 1)))
240 if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
243 w->type = WATCH_DBUS_TIMEOUT;
244 w->data.bus_timeout = timeout;
246 if (bus_timeout_arm(m, w) < 0)
253 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
256 dbus_timeout_set_data(timeout, w, NULL);
262 close_nointr_nofail(w->fd);
268 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
275 if (!(w = dbus_timeout_get_data(timeout)))
278 assert(w->type == WATCH_DBUS_TIMEOUT);
279 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
280 close_nointr_nofail(w->fd);
284 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
292 assert_se(w = dbus_timeout_get_data(timeout));
293 assert(w->type == WATCH_DBUS_TIMEOUT);
295 if ((r = bus_timeout_arm(m, w)) < 0)
296 log_error("Failed to rearm timer: %s", strerror(-r));
299 static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
307 dbus_error_init(&error);
309 /* log_debug("Got D-Bus request: %s.%s() on %s", */
310 /* dbus_message_get_interface(message), */
311 /* dbus_message_get_member(message), */
312 /* dbus_message_get_path(message)); */
314 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
315 log_error("Warning! API D-Bus connection terminated.");
318 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
319 const char *name, *old, *new;
321 if (!dbus_message_get_args(message, &error,
322 DBUS_TYPE_STRING, &name,
323 DBUS_TYPE_STRING, &old,
324 DBUS_TYPE_STRING, &new,
326 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
328 if (set_remove(m->subscribed, (char*) name))
329 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(m->subscribed));
333 dbus_error_free(&error);
334 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
337 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
345 dbus_error_init(&error);
347 /* log_debug("Got D-Bus request: %s.%s() on %s", */
348 /* dbus_message_get_interface(message), */
349 /* dbus_message_get_member(message), */
350 /* dbus_message_get_path(message)); */
352 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
353 log_error("Warning! System D-Bus connection terminated.");
356 } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
359 if (!dbus_message_get_args(message, &error,
360 DBUS_TYPE_STRING, &cgroup,
362 log_error("Failed to parse Released message: %s", error.message);
364 cgroup_notify_empty(m, cgroup);
367 dbus_error_free(&error);
368 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
371 unsigned bus_dispatch(Manager *m) {
374 if (m->request_api_bus_dispatch) {
375 if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE)
376 m->request_api_bus_dispatch = false;
381 if (m->request_system_bus_dispatch) {
382 if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE)
383 m->request_system_bus_dispatch = false;
391 static void pending_cb(DBusPendingCall *pending, void *userdata) {
395 dbus_error_init(&error);
397 assert_se(reply = dbus_pending_call_steal_reply(pending));
399 switch (dbus_message_get_type(reply)) {
401 case DBUS_MESSAGE_TYPE_ERROR:
403 assert_se(dbus_set_error_from_message(&error, reply));
404 log_warning("RequestName() failed: %s", error.message);
407 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
410 if (!dbus_message_get_args(reply,
412 DBUS_TYPE_UINT32, &r,
413 DBUS_TYPE_INVALID)) {
414 log_error("Failed to parse RequestName() reply: %s", error.message);
419 log_debug("Successfully acquired name.");
421 log_error("Name already owned.");
427 assert_not_reached("Invalid reply message");
430 dbus_message_unref(reply);
431 dbus_error_free(&error);
434 static int request_name(Manager *m) {
435 DBusMessage *message;
436 const char *name = "org.freedesktop.systemd1";
438 DBusPendingCall *pending;
440 if (!(message = dbus_message_new_method_call(
447 if (!dbus_message_append_args(
449 DBUS_TYPE_STRING, &name,
450 DBUS_TYPE_UINT32, &flags,
451 DBUS_TYPE_INVALID)) {
452 dbus_message_unref(message);
456 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1)) {
457 dbus_message_unref(message);
462 dbus_message_unref(message);
464 if (!dbus_pending_call_set_notify(pending, pending_cb, NULL, NULL)) {
465 dbus_pending_call_cancel(pending);
466 dbus_pending_call_unref(pending);
471 dbus_pending_call_unref(pending);
473 /* We simple ask for the name and don't wait for it. Sooner or
474 * later we'll have it. */
479 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
483 dbus_connection_set_exit_on_disconnect(bus, FALSE);
485 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
486 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL))
492 int bus_init_system(Manager *m) {
499 dbus_error_init(&error);
504 if (m->running_as != MANAGER_SESSION && m->api_bus)
505 m->system_bus = m->api_bus;
507 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
508 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
509 dbus_error_free(&error);
513 dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL);
514 m->request_system_bus_dispatch = true;
516 if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
522 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
527 dbus_bus_add_match(m->system_bus,
529 "interface='org.freedesktop.systemd1.Agent',"
530 "path='/org/freedesktop/systemd1/agent'",
533 if (dbus_error_is_set(&error)) {
534 log_error("Failed to register match: %s", error.message);
535 dbus_error_free(&error);
540 log_debug("Successfully connected to system D-Bus bus %s as %s",
541 strnull((id = dbus_connection_get_server_id(m->system_bus))),
542 strnull(dbus_bus_get_unique_name(m->system_bus)));
548 int bus_init_api(Manager *m) {
555 dbus_error_init(&error);
560 if (m->running_as != MANAGER_SESSION && m->system_bus)
561 m->api_bus = m->system_bus;
563 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
564 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
565 dbus_error_free(&error);
569 dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL);
570 m->request_api_bus_dispatch = true;
572 if ((r = bus_setup_loop(m, m->api_bus)) < 0) {
578 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
579 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
580 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
581 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
586 dbus_bus_add_match(m->api_bus,
588 "sender='"DBUS_SERVICE_DBUS"',"
589 "interface='"DBUS_INTERFACE_DBUS"',"
590 "path='"DBUS_PATH_DBUS"'",
593 if (dbus_error_is_set(&error)) {
594 log_error("Failed to register match: %s", error.message);
595 dbus_error_free(&error);
600 if ((r = request_name(m)) < 0) {
605 log_debug("Successfully connected to API D-Bus bus %s as %s",
606 strnull((id = dbus_connection_get_server_id(m->api_bus))),
607 strnull(dbus_bus_get_unique_name(m->api_bus)));
611 if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
617 void bus_done_api(Manager *m) {
621 if (m->system_bus == m->api_bus)
622 m->system_bus = NULL;
624 dbus_connection_set_dispatch_status_function(m->api_bus, NULL, NULL, NULL);
625 dbus_connection_close(m->api_bus);
626 dbus_connection_unref(m->api_bus);
633 while ((c = set_steal_first(m->subscribed)))
636 set_free(m->subscribed);
637 m->subscribed = NULL;
641 void bus_done_system(Manager *m) {
644 if (m->system_bus == m->api_bus)
648 dbus_connection_set_dispatch_status_function(m->system_bus, NULL, NULL, NULL);
649 dbus_connection_close(m->system_bus);
650 dbus_connection_unref(m->system_bus);
651 m->system_bus = NULL;
655 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
657 DBusMessage *reply = NULL;
663 dbus_error_init(&error);
665 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
667 if (!(reply = dbus_message_new_method_return(message)))
670 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
673 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
674 const char *interface, *property;
675 const BusProperty *p;
677 if (!dbus_message_get_args(
680 DBUS_TYPE_STRING, &interface,
681 DBUS_TYPE_STRING, &property,
683 return bus_send_error_reply(m, message, &error, -EINVAL);
685 for (p = properties; p->property; p++)
686 if (streq(p->interface, interface) && streq(p->property, property))
690 DBusMessageIter iter, sub;
692 if (!(reply = dbus_message_new_method_return(message)))
695 dbus_message_iter_init_append(reply, &iter);
697 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
700 if ((r = p->append(m, &sub, property, p->data)) < 0) {
705 dbus_message_unref(reply);
706 return bus_send_error_reply(m, message, NULL, r);
709 if (!dbus_message_iter_close_container(&iter, &sub))
712 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
713 const char *interface;
714 const BusProperty *p;
715 DBusMessageIter iter, sub, sub2, sub3;
718 if (!dbus_message_get_args(
721 DBUS_TYPE_STRING, &interface,
723 return bus_send_error_reply(m, message, &error, -EINVAL);
725 if (!(reply = dbus_message_new_method_return(message)))
728 dbus_message_iter_init_append(reply, &iter);
730 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
733 for (p = properties; p->property; p++) {
734 if (!streq(p->interface, interface))
737 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
738 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
739 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
742 if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
747 dbus_message_unref(reply);
748 return bus_send_error_reply(m, message, NULL, r);
751 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
752 !dbus_message_iter_close_container(&sub, &sub2))
758 if (!dbus_message_iter_close_container(&iter, &sub))
763 if (!dbus_connection_send(m->api_bus, reply, NULL))
766 dbus_message_unref(reply);
767 return DBUS_HANDLER_RESULT_HANDLED;
770 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
774 dbus_message_unref(reply);
776 dbus_error_free(&error);
778 return DBUS_HANDLER_RESULT_NEED_MEMORY;
781 static const char *error_to_dbus(int error) {
786 return DBUS_ERROR_INVALID_ARGS;
789 return DBUS_ERROR_NO_MEMORY;
793 return DBUS_ERROR_ACCESS_DENIED;
796 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
799 return DBUS_ERROR_FILE_NOT_FOUND;
802 return DBUS_ERROR_FILE_EXISTS;
805 return DBUS_ERROR_TIMEOUT;
808 return DBUS_ERROR_IO_ERROR;
813 return DBUS_ERROR_DISCONNECTED;
816 return DBUS_ERROR_FAILED;
819 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
820 DBusMessage *reply = NULL;
821 const char *name, *text;
823 if (bus_error && dbus_error_is_set(bus_error)) {
824 name = bus_error->name;
825 text = bus_error->message;
827 name = error_to_dbus(error);
828 text = strerror(-error);
831 if (!(reply = dbus_message_new_error(message, name, text)))
834 if (!dbus_connection_send(m->api_bus, reply, NULL))
837 dbus_message_unref(reply);
840 dbus_error_free(bus_error);
842 return DBUS_HANDLER_RESULT_HANDLED;
846 dbus_message_unref(reply);
849 dbus_error_free(bus_error);
851 return DBUS_HANDLER_RESULT_NEED_MEMORY;
854 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
855 const char *t = data;
864 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
870 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
878 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
882 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
885 if (!dbus_message_iter_close_container(i, &sub))
891 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
902 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
908 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
914 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
920 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
926 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))