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_owner, *new_owner;
321 if (!dbus_message_get_args(message, &error,
322 DBUS_TYPE_STRING, &name,
323 DBUS_TYPE_STRING, &old_owner,
324 DBUS_TYPE_STRING, &new_owner,
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));
331 if (old_owner[0] == 0)
334 if (new_owner[0] == 0)
337 manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
341 dbus_error_free(&error);
342 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
345 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
353 dbus_error_init(&error);
355 /* log_debug("Got D-Bus request: %s.%s() on %s", */
356 /* dbus_message_get_interface(message), */
357 /* dbus_message_get_member(message), */
358 /* dbus_message_get_path(message)); */
360 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
361 log_error("Warning! System D-Bus connection terminated.");
364 } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
367 if (!dbus_message_get_args(message, &error,
368 DBUS_TYPE_STRING, &cgroup,
370 log_error("Failed to parse Released message: %s", error.message);
372 cgroup_notify_empty(m, cgroup);
375 dbus_error_free(&error);
376 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
379 unsigned bus_dispatch(Manager *m) {
382 if (m->request_api_bus_dispatch) {
383 if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE)
384 m->request_api_bus_dispatch = false;
389 if (m->request_system_bus_dispatch) {
390 if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE)
391 m->request_system_bus_dispatch = false;
399 static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
403 dbus_error_init(&error);
405 assert_se(reply = dbus_pending_call_steal_reply(pending));
407 switch (dbus_message_get_type(reply)) {
409 case DBUS_MESSAGE_TYPE_ERROR:
411 assert_se(dbus_set_error_from_message(&error, reply));
412 log_warning("RequestName() failed: %s", error.message);
415 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
418 if (!dbus_message_get_args(reply,
420 DBUS_TYPE_UINT32, &r,
421 DBUS_TYPE_INVALID)) {
422 log_error("Failed to parse RequestName() reply: %s", error.message);
427 log_debug("Successfully acquired name.");
429 log_error("Name already owned.");
435 assert_not_reached("Invalid reply message");
438 dbus_message_unref(reply);
439 dbus_error_free(&error);
442 static int request_name(Manager *m) {
443 const char *name = "org.freedesktop.systemd1";
445 DBusMessage *message = NULL;
446 DBusPendingCall *pending = NULL;
448 if (!(message = dbus_message_new_method_call(
455 if (!dbus_message_append_args(
457 DBUS_TYPE_STRING, &name,
458 DBUS_TYPE_UINT32, &flags,
462 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
465 if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
468 dbus_message_unref(message);
469 dbus_pending_call_unref(pending);
471 /* We simple ask for the name and don't wait for it. Sooner or
472 * later we'll have it. */
478 dbus_pending_call_cancel(pending);
479 dbus_pending_call_unref(pending);
483 dbus_message_unref(message);
488 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
492 dbus_connection_set_exit_on_disconnect(bus, FALSE);
494 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
495 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL))
501 int bus_init_system(Manager *m) {
508 dbus_error_init(&error);
513 if (m->running_as != MANAGER_SESSION && m->api_bus)
514 m->system_bus = m->api_bus;
516 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
517 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
518 dbus_error_free(&error);
522 dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL);
523 m->request_system_bus_dispatch = true;
525 if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
531 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
536 dbus_bus_add_match(m->system_bus,
538 "interface='org.freedesktop.systemd1.Agent',"
539 "path='/org/freedesktop/systemd1/agent'",
542 if (dbus_error_is_set(&error)) {
543 log_error("Failed to register match: %s", error.message);
544 dbus_error_free(&error);
549 log_debug("Successfully connected to system D-Bus bus %s as %s",
550 strnull((id = dbus_connection_get_server_id(m->system_bus))),
551 strnull(dbus_bus_get_unique_name(m->system_bus)));
557 int bus_init_api(Manager *m) {
564 dbus_error_init(&error);
569 if (m->name_data_slot < 0)
570 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot))
573 if (m->running_as != MANAGER_SESSION && m->system_bus)
574 m->api_bus = m->system_bus;
576 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
577 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
578 dbus_error_free(&error);
582 dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL);
583 m->request_api_bus_dispatch = true;
585 if ((r = bus_setup_loop(m, m->api_bus)) < 0) {
591 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
592 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
593 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
594 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
599 dbus_bus_add_match(m->api_bus,
601 "sender='"DBUS_SERVICE_DBUS"',"
602 "interface='"DBUS_INTERFACE_DBUS"',"
603 "path='"DBUS_PATH_DBUS"'",
606 if (dbus_error_is_set(&error)) {
607 log_error("Failed to register match: %s", error.message);
608 dbus_error_free(&error);
613 if ((r = request_name(m)) < 0) {
618 log_debug("Successfully connected to API D-Bus bus %s as %s",
619 strnull((id = dbus_connection_get_server_id(m->api_bus))),
620 strnull(dbus_bus_get_unique_name(m->api_bus)));
624 if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
630 void bus_done_api(Manager *m) {
634 if (m->system_bus == m->api_bus)
635 m->system_bus = NULL;
637 dbus_connection_set_dispatch_status_function(m->api_bus, NULL, NULL, NULL);
638 dbus_connection_close(m->api_bus);
639 dbus_connection_unref(m->api_bus);
646 while ((c = set_steal_first(m->subscribed)))
649 set_free(m->subscribed);
650 m->subscribed = NULL;
653 if (m->name_data_slot >= 0)
654 dbus_pending_call_free_data_slot(&m->name_data_slot);
657 void bus_done_system(Manager *m) {
660 if (m->system_bus == m->api_bus)
664 dbus_connection_set_dispatch_status_function(m->system_bus, NULL, NULL, NULL);
665 dbus_connection_close(m->system_bus);
666 dbus_connection_unref(m->system_bus);
667 m->system_bus = NULL;
671 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
672 Manager *m = userdata;
677 dbus_error_init(&error);
679 assert_se(name = dbus_pending_call_get_data(pending, m->name_data_slot));
680 assert_se(reply = dbus_pending_call_steal_reply(pending));
682 switch (dbus_message_get_type(reply)) {
684 case DBUS_MESSAGE_TYPE_ERROR:
686 assert_se(dbus_set_error_from_message(&error, reply));
687 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
690 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
693 if (!dbus_message_get_args(reply,
695 DBUS_TYPE_UINT32, &r,
696 DBUS_TYPE_INVALID)) {
697 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
701 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
706 assert_not_reached("Invalid reply message");
709 dbus_message_unref(reply);
710 dbus_error_free(&error);
713 int bus_query_pid(Manager *m, const char *name) {
714 DBusMessage *message = NULL;
715 DBusPendingCall *pending = NULL;
721 if (!(message = dbus_message_new_method_call(
725 "GetConnectionUnixProcessID")))
728 if (!(dbus_message_append_args(
730 DBUS_TYPE_STRING, &name,
734 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
737 if (!(n = strdup(name)))
740 if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
745 if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
748 dbus_message_unref(message);
749 dbus_pending_call_unref(pending);
757 dbus_pending_call_cancel(pending);
758 dbus_pending_call_unref(pending);
762 dbus_message_unref(message);
767 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
769 DBusMessage *reply = NULL;
775 dbus_error_init(&error);
777 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
779 if (!(reply = dbus_message_new_method_return(message)))
782 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
785 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
786 const char *interface, *property;
787 const BusProperty *p;
789 if (!dbus_message_get_args(
792 DBUS_TYPE_STRING, &interface,
793 DBUS_TYPE_STRING, &property,
795 return bus_send_error_reply(m, message, &error, -EINVAL);
797 for (p = properties; p->property; p++)
798 if (streq(p->interface, interface) && streq(p->property, property))
802 DBusMessageIter iter, sub;
804 if (!(reply = dbus_message_new_method_return(message)))
807 dbus_message_iter_init_append(reply, &iter);
809 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
812 if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
817 dbus_message_unref(reply);
818 return bus_send_error_reply(m, message, NULL, r);
821 if (!dbus_message_iter_close_container(&iter, &sub))
824 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
825 const char *interface;
826 const BusProperty *p;
827 DBusMessageIter iter, sub, sub2, sub3;
830 if (!dbus_message_get_args(
833 DBUS_TYPE_STRING, &interface,
835 return bus_send_error_reply(m, message, &error, -EINVAL);
837 if (!(reply = dbus_message_new_method_return(message)))
840 dbus_message_iter_init_append(reply, &iter);
842 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
845 for (p = properties; p->property; p++) {
846 if (!streq(p->interface, interface))
849 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
850 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
851 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
854 if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
859 dbus_message_unref(reply);
860 return bus_send_error_reply(m, message, NULL, r);
863 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
864 !dbus_message_iter_close_container(&sub, &sub2))
870 if (!dbus_message_iter_close_container(&iter, &sub))
875 if (!dbus_connection_send(m->api_bus, reply, NULL))
878 dbus_message_unref(reply);
879 return DBUS_HANDLER_RESULT_HANDLED;
882 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
886 dbus_message_unref(reply);
888 dbus_error_free(&error);
890 return DBUS_HANDLER_RESULT_NEED_MEMORY;
893 static const char *error_to_dbus(int error) {
898 return DBUS_ERROR_INVALID_ARGS;
901 return DBUS_ERROR_NO_MEMORY;
905 return DBUS_ERROR_ACCESS_DENIED;
908 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
911 return DBUS_ERROR_FILE_NOT_FOUND;
914 return DBUS_ERROR_FILE_EXISTS;
917 return DBUS_ERROR_TIMEOUT;
920 return DBUS_ERROR_IO_ERROR;
925 return DBUS_ERROR_DISCONNECTED;
928 return DBUS_ERROR_FAILED;
931 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
932 DBusMessage *reply = NULL;
933 const char *name, *text;
935 if (bus_error && dbus_error_is_set(bus_error)) {
936 name = bus_error->name;
937 text = bus_error->message;
939 name = error_to_dbus(error);
940 text = strerror(-error);
943 if (!(reply = dbus_message_new_error(message, name, text)))
946 if (!dbus_connection_send(m->api_bus, reply, NULL))
949 dbus_message_unref(reply);
952 dbus_error_free(bus_error);
954 return DBUS_HANDLER_RESULT_HANDLED;
958 dbus_message_unref(reply);
961 dbus_error_free(bus_error);
963 return DBUS_HANDLER_RESULT_NEED_MEMORY;
966 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
967 const char *t = data;
976 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
982 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
990 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
994 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
997 if (!dbus_message_iter_close_container(i, &sub))
1003 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1014 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1020 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1026 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1032 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1038 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))