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_close(m->api_bus);
641 dbus_connection_unref(m->api_bus);
648 while ((c = set_steal_first(m->subscribed)))
651 set_free(m->subscribed);
652 m->subscribed = NULL;
655 if (m->name_data_slot >= 0)
656 dbus_pending_call_free_data_slot(&m->name_data_slot);
659 void bus_done_system(Manager *m) {
662 if (m->system_bus == m->api_bus)
666 dbus_connection_set_dispatch_status_function(m->system_bus, NULL, NULL, NULL);
667 dbus_connection_close(m->system_bus);
668 dbus_connection_unref(m->system_bus);
669 m->system_bus = NULL;
673 static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) {
674 Manager *m = userdata;
679 dbus_error_init(&error);
681 assert_se(name = dbus_pending_call_get_data(pending, m->name_data_slot));
682 assert_se(reply = dbus_pending_call_steal_reply(pending));
684 switch (dbus_message_get_type(reply)) {
686 case DBUS_MESSAGE_TYPE_ERROR:
688 assert_se(dbus_set_error_from_message(&error, reply));
689 log_warning("GetConnectionUnixProcessID() failed: %s", error.message);
692 case DBUS_MESSAGE_TYPE_METHOD_RETURN: {
695 if (!dbus_message_get_args(reply,
697 DBUS_TYPE_UINT32, &r,
698 DBUS_TYPE_INVALID)) {
699 log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message);
703 manager_dispatch_bus_query_pid_done(m, name, (pid_t) r);
708 assert_not_reached("Invalid reply message");
711 dbus_message_unref(reply);
712 dbus_error_free(&error);
715 int bus_query_pid(Manager *m, const char *name) {
716 DBusMessage *message = NULL;
717 DBusPendingCall *pending = NULL;
723 if (!(message = dbus_message_new_method_call(
727 "GetConnectionUnixProcessID")))
730 if (!(dbus_message_append_args(
732 DBUS_TYPE_STRING, &name,
736 if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1))
739 if (!(n = strdup(name)))
742 if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free))
747 if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL))
750 dbus_message_unref(message);
751 dbus_pending_call_unref(pending);
759 dbus_pending_call_cancel(pending);
760 dbus_pending_call_unref(pending);
764 dbus_message_unref(message);
769 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
771 DBusMessage *reply = NULL;
777 dbus_error_init(&error);
779 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
781 if (!(reply = dbus_message_new_method_return(message)))
784 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
787 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
788 const char *interface, *property;
789 const BusProperty *p;
791 if (!dbus_message_get_args(
794 DBUS_TYPE_STRING, &interface,
795 DBUS_TYPE_STRING, &property,
797 return bus_send_error_reply(m, message, &error, -EINVAL);
799 for (p = properties; p->property; p++)
800 if (streq(p->interface, interface) && streq(p->property, property))
804 DBusMessageIter iter, sub;
806 if (!(reply = dbus_message_new_method_return(message)))
809 dbus_message_iter_init_append(reply, &iter);
811 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
814 if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) {
819 dbus_message_unref(reply);
820 return bus_send_error_reply(m, message, NULL, r);
823 if (!dbus_message_iter_close_container(&iter, &sub))
826 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
827 const char *interface;
828 const BusProperty *p;
829 DBusMessageIter iter, sub, sub2, sub3;
832 if (!dbus_message_get_args(
835 DBUS_TYPE_STRING, &interface,
837 return bus_send_error_reply(m, message, &error, -EINVAL);
839 if (!(reply = dbus_message_new_method_return(message)))
842 dbus_message_iter_init_append(reply, &iter);
844 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
847 for (p = properties; p->property; p++) {
848 if (!streq(p->interface, interface))
851 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
852 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
853 !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
856 if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) {
861 dbus_message_unref(reply);
862 return bus_send_error_reply(m, message, NULL, r);
865 if (!dbus_message_iter_close_container(&sub2, &sub3) ||
866 !dbus_message_iter_close_container(&sub, &sub2))
872 if (!dbus_message_iter_close_container(&iter, &sub))
877 if (!dbus_connection_send(m->api_bus, reply, NULL))
880 dbus_message_unref(reply);
881 return DBUS_HANDLER_RESULT_HANDLED;
884 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
888 dbus_message_unref(reply);
890 dbus_error_free(&error);
892 return DBUS_HANDLER_RESULT_NEED_MEMORY;
895 static const char *error_to_dbus(int error) {
900 return DBUS_ERROR_INVALID_ARGS;
903 return DBUS_ERROR_NO_MEMORY;
907 return DBUS_ERROR_ACCESS_DENIED;
910 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
913 return DBUS_ERROR_FILE_NOT_FOUND;
916 return DBUS_ERROR_FILE_EXISTS;
919 return DBUS_ERROR_TIMEOUT;
922 return DBUS_ERROR_IO_ERROR;
927 return DBUS_ERROR_DISCONNECTED;
930 return DBUS_ERROR_FAILED;
933 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
934 DBusMessage *reply = NULL;
935 const char *name, *text;
937 if (bus_error && dbus_error_is_set(bus_error)) {
938 name = bus_error->name;
939 text = bus_error->message;
941 name = error_to_dbus(error);
942 text = strerror(-error);
945 if (!(reply = dbus_message_new_error(message, name, text)))
948 if (!dbus_connection_send(m->api_bus, reply, NULL))
951 dbus_message_unref(reply);
954 dbus_error_free(bus_error);
956 return DBUS_HANDLER_RESULT_HANDLED;
960 dbus_message_unref(reply);
963 dbus_error_free(bus_error);
965 return DBUS_HANDLER_RESULT_NEED_MEMORY;
968 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
969 const char *t = data;
978 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
984 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
992 if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
996 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
999 if (!dbus_message_iter_close_container(i, &sub))
1005 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1016 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
1022 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1028 /* Let's ensure that pid_t is actually 64bit, and hence this
1029 * function can be used for usec_t */
1030 assert_cc(sizeof(uint64_t) == sizeof(usec_t));
1032 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
1038 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1044 /* Let's ensure that pid_t and mode_t is actually 32bit, and
1045 * hence this function can be used for pid_t/mode_t */
1046 assert_cc(sizeof(uint32_t) == sizeof(pid_t));
1047 assert_cc(sizeof(uint32_t) == sizeof(mode_t));
1048 assert_cc(sizeof(uint32_t) == sizeof(unsigned));
1050 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
1056 int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
1062 assert_cc(sizeof(int32_t) == sizeof(int));
1064 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data))