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 <sys/epoll.h>
23 #include <sys/timerfd.h>
26 #include <dbus/dbus.h>
32 #include "dbus-unit.h"
34 #include "dbus-manager.h"
36 static void api_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
45 assert(m->api_bus == bus);
47 m->request_api_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
50 static void system_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) {
59 assert(m->system_bus == bus);
61 m->request_system_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
64 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
70 /* no watch flags for disabled watches */
71 if (!dbus_watch_get_enabled(bus_watch))
74 flags = dbus_watch_get_flags(bus_watch);
76 if (flags & DBUS_WATCH_READABLE)
78 if (flags & DBUS_WATCH_WRITABLE)
81 return events | EPOLLHUP | EPOLLERR;
84 static unsigned events_to_bus_flags(uint32_t events) {
88 flags |= DBUS_WATCH_READABLE;
89 if (events & EPOLLOUT)
90 flags |= DBUS_WATCH_WRITABLE;
91 if (events & EPOLLHUP)
92 flags |= DBUS_WATCH_HANGUP;
93 if (events & EPOLLERR)
94 flags |= DBUS_WATCH_ERROR;
99 void bus_watch_event(Manager *m, Watch *w, int events) {
103 /* This is called by the event loop whenever there is
104 * something happening on D-Bus' file handles. */
106 if (!dbus_watch_get_enabled(w->data.bus_watch))
109 dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
112 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
115 struct epoll_event ev;
120 if (!(w = new0(Watch, 1)))
123 w->fd = dbus_watch_get_unix_fd(bus_watch);
124 w->type = WATCH_DBUS_WATCH;
125 w->data.bus_watch = bus_watch;
128 ev.events = bus_flags_to_events(bus_watch);
131 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
133 if (errno != EEXIST) {
138 /* Hmm, bloody D-Bus creates multiple watches on the
139 * same fd. epoll() does not like that. As a dirty
140 * hack we simply dup() the fd and hence get a second
141 * one we can safely add to the epoll(). */
143 if ((w->fd = dup(w->fd)) < 0) {
148 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
150 close_nointr_nofail(w->fd);
154 w->fd_is_dupped = true;
157 dbus_watch_set_data(bus_watch, w, NULL);
162 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
169 if (!(w = dbus_watch_get_data(bus_watch)))
172 assert(w->type == WATCH_DBUS_WATCH);
173 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
176 close_nointr_nofail(w->fd);
181 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
184 struct epoll_event ev;
189 assert_se(w = dbus_watch_get_data(bus_watch));
190 assert(w->type == WATCH_DBUS_WATCH);
193 ev.events = bus_flags_to_events(bus_watch);
196 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
199 static int bus_timeout_arm(Manager *m, Watch *w) {
200 struct itimerspec its;
207 if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
208 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
209 its.it_interval = its.it_interval;
212 if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
218 void bus_timeout_event(Manager *m, Watch *w, int events) {
222 /* This is called by the event loop whenever there is
223 * something happening on D-Bus' file handles. */
225 if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
228 dbus_timeout_handle(w->data.bus_timeout);
231 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
234 struct epoll_event ev;
239 if (!(w = new0(Watch, 1)))
242 if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
245 w->type = WATCH_DBUS_TIMEOUT;
246 w->data.bus_timeout = timeout;
248 if (bus_timeout_arm(m, w) < 0)
255 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
258 dbus_timeout_set_data(timeout, w, NULL);
264 close_nointr_nofail(w->fd);
270 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
277 if (!(w = dbus_timeout_get_data(timeout)))
280 assert(w->type == WATCH_DBUS_TIMEOUT);
281 assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
282 close_nointr_nofail(w->fd);
286 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
294 assert_se(w = dbus_timeout_get_data(timeout));
295 assert(w->type == WATCH_DBUS_TIMEOUT);
297 if ((r = bus_timeout_arm(m, w)) < 0)
298 log_error("Failed to rearm timer: %s", strerror(-r));
301 static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
309 dbus_error_init(&error);
311 /* log_debug("Got D-Bus request: %s.%s() on %s", */
312 /* dbus_message_get_interface(message), */
313 /* dbus_message_get_member(message), */
314 /* dbus_message_get_path(message)); */
316 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
317 log_error("Warning! API D-Bus connection terminated.");
320 } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
321 const char *name, *old_owner, *new_owner;
323 if (!dbus_message_get_args(message, &error,
324 DBUS_TYPE_STRING, &name,
325 DBUS_TYPE_STRING, &old_owner,
326 DBUS_TYPE_STRING, &new_owner,
328 log_error("Failed to parse NameOwnerChanged message: %s", error.message);
330 if (set_remove(m->subscribed, (char*) name))
331 log_debug("Subscription client vanished: %s (left: %u)", name, set_size(m->subscribed));
333 if (old_owner[0] == 0)
336 if (new_owner[0] == 0)
339 manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner);
343 dbus_error_free(&error);
344 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
347 static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) {
355 dbus_error_init(&error);
357 /* log_debug("Got D-Bus request: %s.%s() on %s", */
358 /* dbus_message_get_interface(message), */
359 /* dbus_message_get_member(message), */
360 /* dbus_message_get_path(message)); */
362 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
363 log_error("Warning! System D-Bus connection terminated.");
366 } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
369 if (!dbus_message_get_args(message, &error,
370 DBUS_TYPE_STRING, &cgroup,
372 log_error("Failed to parse Released message: %s", error.message);
374 cgroup_notify_empty(m, cgroup);
377 dbus_error_free(&error);
378 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
381 unsigned bus_dispatch(Manager *m) {
384 if (m->request_api_bus_dispatch) {
385 if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE)
386 m->request_api_bus_dispatch = false;
391 if (m->request_system_bus_dispatch) {
392 if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE)
393 m->request_system_bus_dispatch = false;
401 static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) {
405 dbus_error_init(&error);
407 assert_se(reply = dbus_pending_call_steal_reply(pending));
409 switch (dbus_message_get_type(reply)) {
411 case DBUS_MESSAGE_TYPE_ERROR:
413 assert_se(dbus_set_error_from_message(&error, reply));
414 log_warning("RequestName() failed: %s", error.message);
417 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
420 if (!dbus_message_get_args(reply,
422 DBUS_TYPE_UINT32, &r,
423 DBUS_TYPE_INVALID)) {
424 log_error("Failed to parse RequestName() reply: %s", error.message);
429 log_debug("Successfully acquired name.");
431 log_error("Name already owned.");
437 assert_not_reached("Invalid reply message");
440 dbus_message_unref(reply);
441 dbus_error_free(&error);
444 static int request_name(Manager *m) {
445 const char *name = "org.freedesktop.systemd1";
447 DBusMessage *message = NULL;
448 DBusPendingCall *pending = NULL;
450 if (!(message = dbus_message_new_method_call(
457 if (!dbus_message_append_args(
459 DBUS_TYPE_STRING, &name,
460 DBUS_TYPE_UINT32, &flags,
464 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
467 if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL))
470 dbus_message_unref(message);
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. */
480 dbus_pending_call_cancel(pending);
481 dbus_pending_call_unref(pending);
485 dbus_message_unref(message);
490 static int bus_setup_loop(Manager *m, DBusConnection *bus) {
494 dbus_connection_set_exit_on_disconnect(bus, FALSE);
496 if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
497 !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL))
503 int bus_init_system(Manager *m) {
510 dbus_error_init(&error);
515 if (m->running_as != MANAGER_SESSION && m->api_bus)
516 m->system_bus = m->api_bus;
518 if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) {
519 log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message);
520 dbus_error_free(&error);
524 dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL);
525 m->request_system_bus_dispatch = true;
527 if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
533 if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
538 dbus_bus_add_match(m->system_bus,
540 "interface='org.freedesktop.systemd1.Agent',"
541 "path='/org/freedesktop/systemd1/agent'",
544 if (dbus_error_is_set(&error)) {
545 log_error("Failed to register match: %s", error.message);
546 dbus_error_free(&error);
551 log_debug("Successfully connected to system D-Bus bus %s as %s",
552 strnull((id = dbus_connection_get_server_id(m->system_bus))),
553 strnull(dbus_bus_get_unique_name(m->system_bus)));
559 int bus_init_api(Manager *m) {
566 dbus_error_init(&error);
571 if (m->name_data_slot < 0)
572 if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot))
575 if (m->running_as != MANAGER_SESSION && m->system_bus)
576 m->api_bus = m->system_bus;
578 if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) {
579 log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message);
580 dbus_error_free(&error);
584 dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL);
585 m->request_api_bus_dispatch = true;
587 if ((r = bus_setup_loop(m, m->api_bus)) < 0) {
593 if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
594 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
595 !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
596 !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) {
601 dbus_bus_add_match(m->api_bus,
603 "sender='"DBUS_SERVICE_DBUS"',"
604 "interface='"DBUS_INTERFACE_DBUS"',"
605 "path='"DBUS_PATH_DBUS"'",
608 if (dbus_error_is_set(&error)) {
609 log_error("Failed to register match: %s", error.message);
610 dbus_error_free(&error);
615 if ((r = request_name(m)) < 0) {
620 log_debug("Successfully connected to API D-Bus bus %s as %s",
621 strnull((id = dbus_connection_get_server_id(m->api_bus))),
622 strnull(dbus_bus_get_unique_name(m->api_bus)));
626 if (!(m->subscribed = set_new(string_hash_func, string_compare_func)))
632 void bus_done_api(Manager *m) {
636 if (m->system_bus == m->api_bus)
637 m->system_bus = NULL;
639 dbus_connection_set_dispatch_status_function(m->api_bus, NULL, NULL, NULL);
640 dbus_connection_flush(m->api_bus);
641 dbus_connection_close(m->api_bus);
642 dbus_connection_unref(m->api_bus);
649 while ((c = set_steal_first(m->subscribed)))
652 set_free(m->subscribed);
653 m->subscribed = NULL;
656 if (m->name_data_slot >= 0)
657 dbus_pending_call_free_data_slot(&m->name_data_slot);
660 void bus_done_system(Manager *m) {
663 if (m->system_bus == m->api_bus)
667 dbus_connection_set_dispatch_status_function(m->system_bus, NULL, NULL, NULL);
668 dbus_connection_flush(m->system_bus);
669 dbus_connection_close(m->system_bus);
670 dbus_connection_unref(m->system_bus);
671 m->system_bus = NULL;
675 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
676 Manager *m = userdata;
681 dbus_error_init(&error);
683 assert_se(name = dbus_pending_call_get_data(pending, m->name_data_slot));
684 assert_se(reply = dbus_pending_call_steal_reply(pending));
686 switch (dbus_message_get_type(reply)) {
688 case DBUS_MESSAGE_TYPE_ERROR:
690 assert_se(dbus_set_error_from_message(&error, reply));
691 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
694 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
697 if (!dbus_message_get_args(reply,
699 DBUS_TYPE_UINT32, &r,
700 DBUS_TYPE_INVALID)) {
701 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
705 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
710 assert_not_reached("Invalid reply message");
713 dbus_message_unref(reply);
714 dbus_error_free(&error);
717 int bus_query_pid(Manager *m, const char *name) {
718 DBusMessage *message = NULL;
719 DBusPendingCall *pending = NULL;
725 if (!(message = dbus_message_new_method_call(
729 "GetConnectionUnixProcessID")))
732 if (!(dbus_message_append_args(
734 DBUS_TYPE_STRING, &name,
738 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
741 if (!(n = strdup(name)))
744 if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
749 if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
752 dbus_message_unref(message);
753 dbus_pending_call_unref(pending);
761 dbus_pending_call_cancel(pending);
762 dbus_pending_call_unref(pending);
766 dbus_message_unref(message);
771 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
773 DBusMessage *reply = NULL;
779 dbus_error_init(&error);
781 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
783 if (!(reply = dbus_message_new_method_return(message)))
786 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
789 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
790 const char *interface, *property;
791 const BusProperty *p;
793 if (!dbus_message_get_args(
796 DBUS_TYPE_STRING, &interface,
797 DBUS_TYPE_STRING, &property,
799 return bus_send_error_reply(m, message, &error, -EINVAL);
801 for (p = properties; p->property; p++)
802 if (streq(p->interface, interface) && streq(p->property, property))
806 DBusMessageIter iter, sub;
808 if (!(reply = dbus_message_new_method_return(message)))
811 dbus_message_iter_init_append(reply, &iter);
813 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
816 if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
821 dbus_message_unref(reply);
822 return bus_send_error_reply(m, message, NULL, r);
825 if (!dbus_message_iter_close_container(&iter, &sub))
828 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
829 const char *interface;
830 const BusProperty *p;
831 DBusMessageIter iter, sub, sub2, sub3;
834 if (!dbus_message_get_args(
837 DBUS_TYPE_STRING, &interface,
839 return bus_send_error_reply(m, message, &error, -EINVAL);
841 if (!(reply = dbus_message_new_method_return(message)))
844 dbus_message_iter_init_append(reply, &iter);
846 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
849 for (p = properties; p->property; p++) {
850 if (!streq(p->interface, interface))
853 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
854 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
855 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
858 if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
863 dbus_message_unref(reply);
864 return bus_send_error_reply(m, message, NULL, r);
867 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
868 !dbus_message_iter_close_container(&sub, &sub2))
874 if (!dbus_message_iter_close_container(&iter, &sub))
879 if (!dbus_connection_send(m->api_bus, reply, NULL))
882 dbus_message_unref(reply);
883 return DBUS_HANDLER_RESULT_HANDLED;
886 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
890 dbus_message_unref(reply);
892 dbus_error_free(&error);
894 return DBUS_HANDLER_RESULT_NEED_MEMORY;
897 static const char *error_to_dbus(int error) {
902 return DBUS_ERROR_INVALID_ARGS;
905 return DBUS_ERROR_NO_MEMORY;
909 return DBUS_ERROR_ACCESS_DENIED;
912 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
915 return DBUS_ERROR_FILE_NOT_FOUND;
918 return DBUS_ERROR_FILE_EXISTS;
921 return DBUS_ERROR_TIMEOUT;
924 return DBUS_ERROR_IO_ERROR;
929 return DBUS_ERROR_DISCONNECTED;
932 return DBUS_ERROR_FAILED;
935 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
936 DBusMessage *reply = NULL;
937 const char *name, *text;
939 if (bus_error && dbus_error_is_set(bus_error)) {
940 name = bus_error->name;
941 text = bus_error->message;
943 name = error_to_dbus(error);
944 text = strerror(-error);
947 if (!(reply = dbus_message_new_error(message, name, text)))
950 if (!dbus_connection_send(m->api_bus, reply, NULL))
953 dbus_message_unref(reply);
956 dbus_error_free(bus_error);
958 return DBUS_HANDLER_RESULT_HANDLED;
962 dbus_message_unref(reply);
965 dbus_error_free(bus_error);
967 return DBUS_HANDLER_RESULT_NEED_MEMORY;
970 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
971 const char *t = data;
980 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
986 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
994 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
998 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
1001 if (!dbus_message_iter_close_container(i, &sub))
1007 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1018 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1024 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1030 /* Let's ensure that pid_t is actually 64bit, and hence this
1031 * function can be used for usec_t */
1032 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
1034 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1040 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1046 /* Let's ensure that pid_t and mode_t is actually 32bit, and
1047 * hence this function can be used for pid_t/mode_t */
1048 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
1049 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
1050 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1052 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
1058 int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1064 assert_cc(sizeof(int32_t) == sizeof(int));
1066 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))