From f278026d21e2ded46f7f91152d60842525e74956 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 6 Apr 2010 16:32:07 +0200 Subject: [PATCH] dbus: connect to bus as soon as the special dbus service is around --- dbus-job.c | 6 +- dbus-unit.c | 6 +- dbus.c | 191 ++++++++++++++++++++++++++++++---------------------- dbus.h | 6 +- manager.c | 10 ++- manager.h | 4 +- service.c | 1 - unit.c | 29 ++++++++ unit.h | 2 + 9 files changed, 162 insertions(+), 93 deletions(-) diff --git a/dbus-job.c b/dbus-job.c index e0c1153f..d73b1258 100644 --- a/dbus-job.c +++ b/dbus-job.c @@ -128,7 +128,7 @@ static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message) return bus_default_message_handler(j->manager, message, introspection, properties); if (reply) { - if (!dbus_connection_send(m->bus, reply, NULL)) + if (!dbus_connection_send(m->api_bus, reply, NULL)) goto oom; dbus_message_unref(reply); @@ -209,7 +209,7 @@ void bus_job_send_change_signal(Job *j) { goto oom; } - if (!dbus_connection_send(j->manager->bus, m, NULL)) + if (!dbus_connection_send(j->manager->api_bus, m, NULL)) goto oom; free(p); @@ -249,7 +249,7 @@ void bus_job_send_removed_signal(Job *j) { DBUS_TYPE_INVALID)) goto oom; - if (!dbus_connection_send(j->manager->bus, m, NULL)) + if (!dbus_connection_send(j->manager->api_bus, m, NULL)) goto oom; free(p); diff --git a/dbus-unit.c b/dbus-unit.c index 00374e7f..4ce94e2f 100644 --- a/dbus-unit.c +++ b/dbus-unit.c @@ -278,7 +278,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message } if (reply) { - if (!dbus_connection_send(m->bus, reply, NULL)) + if (!dbus_connection_send(m->api_bus, reply, NULL)) goto oom; dbus_message_unref(reply); @@ -363,7 +363,7 @@ void bus_unit_send_change_signal(Unit *u) { goto oom; } - if (!dbus_connection_send(u->meta.manager->bus, m, NULL)) + if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL)) goto oom; free(p); @@ -405,7 +405,7 @@ void bus_unit_send_removed_signal(Unit *u) { DBUS_TYPE_INVALID)) goto oom; - if (!dbus_connection_send(u->meta.manager->bus, m, NULL)) + if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL)) goto oom; free(p); diff --git a/dbus.c b/dbus.c index 75adba10..5829a54f 100644 --- a/dbus.c +++ b/dbus.c @@ -31,14 +31,14 @@ #include "strv.h" #include "cgroup.h" -static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) { +static void api_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) { Manager *m = data; assert(bus); assert(m); - assert(m->bus == bus); + assert(m->api_bus == bus); - m->request_bus_dispatch = status != DBUS_DISPATCH_COMPLETE; + m->request_api_bus_dispatch = status != DBUS_DISPATCH_COMPLETE; } static void system_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) { @@ -288,7 +288,7 @@ static void bus_toggle_timeout(DBusTimeout *timeout, void *data) { log_error("Failed to rearm timer: %s", strerror(-r)); } -static DBusHandlerResult bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) { +static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) { Manager *m = data; DBusError error; @@ -305,8 +305,7 @@ static DBusHandlerResult bus_message_filter(DBusConnection *connection, DBusMes if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { log_error("Warning! D-Bus connection terminated."); - - /* FIXME: we probably should restart D-Bus here */ + bus_done_api(m); } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) { const char *name, *old, *new; @@ -344,8 +343,7 @@ static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { log_error("Warning! D-Bus connection terminated."); - - /* FIXME: we probably should restart D-Bus here */ + bus_done_system(m); } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) { const char *cgroup; @@ -365,9 +363,9 @@ static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, unsigned bus_dispatch(Manager *m) { assert(m); - if (m->request_bus_dispatch) - if (dbus_connection_dispatch(m->bus) == DBUS_DISPATCH_COMPLETE) { - m->request_bus_dispatch = false; + if (m->request_api_bus_dispatch) + if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE) { + m->request_api_bus_dispatch = false; return 1; } @@ -445,7 +443,7 @@ static int request_name(Manager *m) { return -ENOMEM; } - if (!dbus_connection_send_with_reply(m->bus, message, &pending, -1)) { + if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1)) { dbus_message_unref(message); return -ENOMEM; } @@ -480,122 +478,144 @@ static int bus_setup_loop(Manager *m, DBusConnection *bus) { return 0; } -int bus_init(Manager *m) { +int bus_init_system(Manager *m) { DBusError error; char *id; int r; assert(m); - if (m->bus) - return 0; - - if (!(m->subscribed = set_new(string_hash_func, string_compare_func))) - return -ENOMEM; - - dbus_connection_set_change_sigpipe(FALSE); - dbus_error_init(&error); - if (!(m->bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) { - log_error("Failed to get D-Bus connection: %s", error.message); - dbus_error_free(&error); - bus_done(m); - return -ECONNREFUSED; - } - if ((r = bus_setup_loop(m, m->bus)) < 0) { - bus_done(m); - return r; - } - - dbus_connection_set_dispatch_status_function(m->bus, bus_dispatch_status, m, NULL); + if (m->system_bus) + return 0; - if (m->running_as == MANAGER_SESSION) { + if (m->running_as != MANAGER_SESSION && m->api_bus) + m->system_bus = m->api_bus; + else { if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) { - log_error("Failed to get D-Bus connection: %s", error.message); + log_error("Failed to get D-Bus connection, retrying later: %s", error.message); dbus_error_free(&error); - bus_done(m); - return -ECONNREFUSED; + return 0; } if ((r = bus_setup_loop(m, m->system_bus)) < 0) { - bus_done(m); + bus_done_system(m); return r; } dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL); - } else - m->system_bus = m->bus; - - if (!dbus_connection_register_object_path(m->bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) || - !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) || - !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) || - !dbus_connection_add_filter(m->bus, bus_message_filter, m, NULL) || - !dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) { - bus_done(m); + } + + if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) { + bus_done_system(m); return -ENOMEM; } - dbus_bus_add_match(m->bus, + dbus_bus_add_match(m->system_bus, "type='signal'," - "sender='"DBUS_SERVICE_DBUS"'," - "interface='"DBUS_INTERFACE_DBUS"'," - "path='"DBUS_PATH_DBUS"'", + "interface='org.freedesktop.systemd1.Agent'," + "path='/org/freedesktop/systemd1/agent'", &error); if (dbus_error_is_set(&error)) { log_error("Failed to register match: %s", error.message); dbus_error_free(&error); + bus_done_system(m); return -ENOMEM; } - if ((r = request_name(m)) < 0) { - bus_done(m); - return r; + log_debug("Successfully connected to system D-Bus bus %s as %s", + strnull((id = dbus_connection_get_server_id(m->system_bus))), + strnull(dbus_bus_get_unique_name(m->system_bus))); + dbus_free(id); + + m->request_system_bus_dispatch = true; + + return 0; +} + +int bus_init_api(Manager *m) { + DBusError error; + char *id; + int r; + + assert(m); + + dbus_error_init(&error); + + if (m->api_bus) + return 0; + + if (m->running_as != MANAGER_SESSION && m->system_bus) + m->api_bus = m->system_bus; + else { + if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) { + log_debug("Failed to get D-Bus connection, retrying later: %s", error.message); + dbus_error_free(&error); + return 0; + } + + if ((r = bus_setup_loop(m, m->api_bus)) < 0) { + bus_done_api(m); + return r; + } + + dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL); } - dbus_bus_add_match(m->system_bus, + if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) || + !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) || + !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) || + !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) { + bus_done_api(m); + return -ENOMEM; + } + + dbus_bus_add_match(m->api_bus, "type='signal'," - "interface='org.freedesktop.systemd1.Agent'," - "path='/org/freedesktop/systemd1/agent'", + "sender='"DBUS_SERVICE_DBUS"'," + "interface='"DBUS_INTERFACE_DBUS"'," + "path='"DBUS_PATH_DBUS"'", &error); if (dbus_error_is_set(&error)) { log_error("Failed to register match: %s", error.message); dbus_error_free(&error); - bus_done(m); + bus_done_api(m); return -ENOMEM; } - log_debug("Successfully connected to D-Bus bus %s as %s", - strnull((id = dbus_connection_get_server_id(m->bus))), - strnull(dbus_bus_get_unique_name(m->bus))); - dbus_free(id); + if ((r = request_name(m)) < 0) { + bus_done_api(m); + return r; + } - log_debug("Successfully connected to system D-Bus bus %s as %s", - strnull((id = dbus_connection_get_server_id(m->system_bus))), - strnull(dbus_bus_get_unique_name(m->system_bus))); + log_debug("Successfully connected to API D-Bus bus %s as %s", + strnull((id = dbus_connection_get_server_id(m->api_bus))), + strnull(dbus_bus_get_unique_name(m->api_bus))); dbus_free(id); - m->request_bus_dispatch = true; - m->request_system_bus_dispatch = true; + if (!m->subscribed) + if (!(m->subscribed = set_new(string_hash_func, string_compare_func))) + return -ENOMEM; + + m->request_api_bus_dispatch = true; return 0; } -void bus_done(Manager *m) { +void bus_done_api(Manager *m) { assert(m); - if (m->system_bus && m->system_bus != m->bus) { - dbus_connection_close(m->system_bus); - dbus_connection_unref(m->system_bus); - m->system_bus = NULL; - } + if (m->api_bus) { + if (m->system_bus == m->api_bus) + m->system_bus = NULL; + + dbus_connection_close(m->api_bus); + dbus_connection_unref(m->api_bus); + m->api_bus = NULL; - if (m->bus) { - dbus_connection_close(m->bus); - dbus_connection_unref(m->bus); - m->bus = NULL; } if (m->subscribed) { @@ -609,6 +629,19 @@ void bus_done(Manager *m) { } } +void bus_done_system(Manager *m) { + assert(m); + + if (m->system_bus == m->api_bus) + bus_done_api(m); + + if (m->system_bus) { + dbus_connection_close(m->system_bus); + dbus_connection_unref(m->system_bus); + m->system_bus = NULL; + } +} + DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) { DBusError error; DBusMessage *reply = NULL; @@ -717,7 +750,7 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, } if (reply) { - if (!dbus_connection_send(m->bus, reply, NULL)) + if (!dbus_connection_send(m->api_bus, reply, NULL)) goto oom; dbus_message_unref(reply); @@ -788,7 +821,7 @@ DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusErr if (!(reply = dbus_message_new_error(message, name, text))) goto oom; - if (!dbus_connection_send(m->bus, reply, NULL)) + if (!dbus_connection_send(m->api_bus, reply, NULL)) goto oom; dbus_message_unref(reply); diff --git a/dbus.h b/dbus.h index aaae89de..1e71971f 100644 --- a/dbus.h +++ b/dbus.h @@ -56,8 +56,10 @@ typedef struct BusProperty { " " \ " " -int bus_init(Manager *m); -void bus_done(Manager *m); +int bus_init_system(Manager *m); +int bus_init_api(Manager *m); +void bus_done_system(Manager *m); +void bus_done_api(Manager *m); unsigned bus_dispatch(Manager *m); diff --git a/manager.c b/manager.c index 87941e3f..234fb93c 100644 --- a/manager.c +++ b/manager.c @@ -315,8 +315,11 @@ int manager_new(Manager **_m) { if ((r = manager_setup_cgroup(m)) < 0) goto fail; - /* FIXME: this should be called only when the D-Bus bus daemon is running */ - if ((r = bus_init(m)) < 0) + dbus_connection_set_change_sigpipe(FALSE); + + /* Try to connect to the busses, if possible. */ + if ((r = bus_init_system(m)) < 0 || + (r = bus_init_api(m)) < 0) goto fail; *_m = m; @@ -364,7 +367,8 @@ void manager_free(Manager *m) { manager_shutdown_cgroup(m); - bus_done(m); + bus_done_api(m); + bus_done_system(m); hashmap_free(m->units); hashmap_free(m->jobs); diff --git a/manager.h b/manager.h index 312ed879..3dbac34e 100644 --- a/manager.h +++ b/manager.h @@ -135,7 +135,7 @@ struct Manager { bool dispatching_run_queue:1; bool dispatching_dbus_queue:1; - bool request_bus_dispatch:1; + bool request_api_bus_dispatch:1; bool request_system_bus_dispatch:1; Hashmap *watch_pids; /* pid => Unit object n:1 */ @@ -157,7 +157,7 @@ struct Manager { Watch mount_watch; /* Data specific to the D-Bus subsystem */ - DBusConnection *bus, *system_bus; + DBusConnection *api_bus, *system_bus; Set *subscribed; /* Data specific to the cgroup subsystem */ diff --git a/service.c b/service.c index caef8440..fda553d2 100644 --- a/service.c +++ b/service.c @@ -732,7 +732,6 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) { "%sPIDFile: %s\n", prefix, s->pid_file); - exec_context_dump(&s->exec_context, f, prefix); for (c = 0; c < _SERVICE_EXEC_MAX; c++) { diff --git a/unit.c b/unit.c index 10efd14c..1a0225a6 100644 --- a/unit.c +++ b/unit.c @@ -131,6 +131,13 @@ Unit *unit_new(Manager *m) { return u; } +bool unit_has_name(Unit *u, const char *name) { + assert(u); + assert(name); + + return !!set_get(u->meta.names, (char*) name); +} + int unit_add_name(Unit *u, const char *text) { UnitType t; char *s; @@ -906,6 +913,28 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) { else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns)) retroactively_stop_dependencies(u); + if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns)) { + + if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) { + /* The bus just got started, hence try to connect to it. */ + bus_init_system(u->meta.manager); + bus_init_api(u->meta.manager); + } + + if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE)) + /* The syslog daemon just got started, hence try to connect to it. */ + log_info("Syslog now available, this is where we should start logging to it."); + + } else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns)) { + + if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE)) + /* The syslog daemon just got terminated, hence try to disconnect from it. */ + log_info("Syslog now gone, this is where we should stio logging to it."); + + /* We don't care about D-Bus here, since we'll get an + * asynchronous notification for it anyway. */ + } + /* Maybe we finished startup and are now ready for being * stopped because unneeded? */ unit_check_uneeded(u); diff --git a/unit.h b/unit.h index b05efe01..942de5f3 100644 --- a/unit.h +++ b/unit.h @@ -284,6 +284,8 @@ int unit_load(Unit *unit); const char* unit_id(Unit *u); const char *unit_description(Unit *u); +bool unit_has_name(Unit *u, const char *name); + UnitActiveState unit_active_state(Unit *u); void unit_dump(Unit *u, FILE *f, const char *prefix); -- 2.39.5