1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

dbus: connect to bus as soon as the special dbus service is around

This commit is contained in:
Lennart Poettering 2010-04-06 16:32:07 +02:00
parent 8b44eabff0
commit f278026d21
9 changed files with 165 additions and 96 deletions

View File

@ -128,7 +128,7 @@ static DBusHandlerResult bus_job_message_dispatch(Job *j, DBusMessage *message)
return bus_default_message_handler(j->manager, message, introspection, properties); return bus_default_message_handler(j->manager, message, introspection, properties);
if (reply) { if (reply) {
if (!dbus_connection_send(m->bus, reply, NULL)) if (!dbus_connection_send(m->api_bus, reply, NULL))
goto oom; goto oom;
dbus_message_unref(reply); dbus_message_unref(reply);
@ -209,7 +209,7 @@ void bus_job_send_change_signal(Job *j) {
goto oom; goto oom;
} }
if (!dbus_connection_send(j->manager->bus, m, NULL)) if (!dbus_connection_send(j->manager->api_bus, m, NULL))
goto oom; goto oom;
free(p); free(p);
@ -249,7 +249,7 @@ void bus_job_send_removed_signal(Job *j) {
DBUS_TYPE_INVALID)) DBUS_TYPE_INVALID))
goto oom; goto oom;
if (!dbus_connection_send(j->manager->bus, m, NULL)) if (!dbus_connection_send(j->manager->api_bus, m, NULL))
goto oom; goto oom;
free(p); free(p);

View File

@ -278,7 +278,7 @@ static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message
} }
if (reply) { if (reply) {
if (!dbus_connection_send(m->bus, reply, NULL)) if (!dbus_connection_send(m->api_bus, reply, NULL))
goto oom; goto oom;
dbus_message_unref(reply); dbus_message_unref(reply);
@ -363,7 +363,7 @@ void bus_unit_send_change_signal(Unit *u) {
goto oom; 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; goto oom;
free(p); free(p);
@ -405,7 +405,7 @@ void bus_unit_send_removed_signal(Unit *u) {
DBUS_TYPE_INVALID)) DBUS_TYPE_INVALID))
goto oom; 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; goto oom;
free(p); free(p);

197
dbus.c
View File

@ -31,14 +31,14 @@
#include "strv.h" #include "strv.h"
#include "cgroup.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; Manager *m = data;
assert(bus); assert(bus);
assert(m); 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) { 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)); 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; Manager *m = data;
DBusError error; DBusError error;
@ -305,8 +305,7 @@ static DBusHandlerResult bus_message_filter(DBusConnection *connection, DBusMes
if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
log_error("Warning! D-Bus connection terminated."); log_error("Warning! D-Bus connection terminated.");
bus_done_api(m);
/* FIXME: we probably should restart D-Bus here */
} else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) { } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
const char *name, *old, *new; 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")) { if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) {
log_error("Warning! D-Bus connection terminated."); log_error("Warning! D-Bus connection terminated.");
bus_done_system(m);
/* FIXME: we probably should restart D-Bus here */
} if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) { } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) {
const char *cgroup; const char *cgroup;
@ -365,9 +363,9 @@ static DBusHandlerResult system_bus_message_filter(DBusConnection *connection,
unsigned bus_dispatch(Manager *m) { unsigned bus_dispatch(Manager *m) {
assert(m); assert(m);
if (m->request_bus_dispatch) if (m->request_api_bus_dispatch)
if (dbus_connection_dispatch(m->bus) == DBUS_DISPATCH_COMPLETE) { if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE) {
m->request_bus_dispatch = false; m->request_api_bus_dispatch = false;
return 1; return 1;
} }
@ -445,7 +443,7 @@ static int request_name(Manager *m) {
return -ENOMEM; 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); dbus_message_unref(message);
return -ENOMEM; return -ENOMEM;
} }
@ -480,80 +478,40 @@ static int bus_setup_loop(Manager *m, DBusConnection *bus) {
return 0; return 0;
} }
int bus_init(Manager *m) { int bus_init_system(Manager *m) {
DBusError error; DBusError error;
char *id; char *id;
int r; int r;
assert(m); assert(m);
if (m->bus) dbus_error_init(&error);
if (m->system_bus)
return 0; return 0;
if (!(m->subscribed = set_new(string_hash_func, string_compare_func))) if (m->running_as != MANAGER_SESSION && m->api_bus)
return -ENOMEM; m->system_bus = m->api_bus;
else {
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->running_as == MANAGER_SESSION) {
if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) { 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); dbus_error_free(&error);
bus_done(m); return 0;
return -ECONNREFUSED;
} }
if ((r = bus_setup_loop(m, m->system_bus)) < 0) { if ((r = bus_setup_loop(m, m->system_bus)) < 0) {
bus_done(m); bus_done_system(m);
return r; return r;
} }
dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL); 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);
return -ENOMEM;
} }
dbus_bus_add_match(m->bus, if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) {
"type='signal'," bus_done_system(m);
"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);
return -ENOMEM; return -ENOMEM;
} }
if ((r = request_name(m)) < 0) {
bus_done(m);
return r;
}
dbus_bus_add_match(m->system_bus, dbus_bus_add_match(m->system_bus,
"type='signal'," "type='signal',"
"interface='org.freedesktop.systemd1.Agent'," "interface='org.freedesktop.systemd1.Agent',"
@ -563,39 +521,101 @@ int bus_init(Manager *m) {
if (dbus_error_is_set(&error)) { if (dbus_error_is_set(&error)) {
log_error("Failed to register match: %s", error.message); log_error("Failed to register match: %s", error.message);
dbus_error_free(&error); dbus_error_free(&error);
bus_done(m); bus_done_system(m);
return -ENOMEM; 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);
log_debug("Successfully connected to system D-Bus bus %s as %s", log_debug("Successfully connected to system D-Bus bus %s as %s",
strnull((id = dbus_connection_get_server_id(m->system_bus))), strnull((id = dbus_connection_get_server_id(m->system_bus))),
strnull(dbus_bus_get_unique_name(m->system_bus))); strnull(dbus_bus_get_unique_name(m->system_bus)));
dbus_free(id); dbus_free(id);
m->request_bus_dispatch = true;
m->request_system_bus_dispatch = true; m->request_system_bus_dispatch = true;
return 0; return 0;
} }
void bus_done(Manager *m) { int bus_init_api(Manager *m) {
DBusError error;
char *id;
int r;
assert(m); assert(m);
if (m->system_bus && m->system_bus != m->bus) { dbus_error_init(&error);
dbus_connection_close(m->system_bus);
dbus_connection_unref(m->system_bus); if (m->api_bus)
m->system_bus = NULL; 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);
} }
if (m->bus) { if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
dbus_connection_close(m->bus); !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
dbus_connection_unref(m->bus); !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) ||
m->bus = NULL; !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',"
"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_api(m);
return -ENOMEM;
}
if ((r = request_name(m)) < 0) {
bus_done_api(m);
return r;
}
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);
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_api(Manager *m) {
assert(m);
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->subscribed) { 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) { DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
DBusError error; DBusError error;
DBusMessage *reply = NULL; DBusMessage *reply = NULL;
@ -717,7 +750,7 @@ DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message,
} }
if (reply) { if (reply) {
if (!dbus_connection_send(m->bus, reply, NULL)) if (!dbus_connection_send(m->api_bus, reply, NULL))
goto oom; goto oom;
dbus_message_unref(reply); 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))) if (!(reply = dbus_message_new_error(message, name, text)))
goto oom; goto oom;
if (!dbus_connection_send(m->bus, reply, NULL)) if (!dbus_connection_send(m->api_bus, reply, NULL))
goto oom; goto oom;
dbus_message_unref(reply); dbus_message_unref(reply);

6
dbus.h
View File

@ -56,8 +56,10 @@ typedef struct BusProperty {
" </method>" \ " </method>" \
" </interface>" " </interface>"
int bus_init(Manager *m); int bus_init_system(Manager *m);
void bus_done(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); unsigned bus_dispatch(Manager *m);

View File

@ -315,8 +315,11 @@ int manager_new(Manager **_m) {
if ((r = manager_setup_cgroup(m)) < 0) if ((r = manager_setup_cgroup(m)) < 0)
goto fail; goto fail;
/* FIXME: this should be called only when the D-Bus bus daemon is running */ dbus_connection_set_change_sigpipe(FALSE);
if ((r = bus_init(m)) < 0)
/* Try to connect to the busses, if possible. */
if ((r = bus_init_system(m)) < 0 ||
(r = bus_init_api(m)) < 0)
goto fail; goto fail;
*_m = m; *_m = m;
@ -364,7 +367,8 @@ void manager_free(Manager *m) {
manager_shutdown_cgroup(m); manager_shutdown_cgroup(m);
bus_done(m); bus_done_api(m);
bus_done_system(m);
hashmap_free(m->units); hashmap_free(m->units);
hashmap_free(m->jobs); hashmap_free(m->jobs);

View File

@ -135,7 +135,7 @@ struct Manager {
bool dispatching_run_queue:1; bool dispatching_run_queue:1;
bool dispatching_dbus_queue:1; bool dispatching_dbus_queue:1;
bool request_bus_dispatch:1; bool request_api_bus_dispatch:1;
bool request_system_bus_dispatch:1; bool request_system_bus_dispatch:1;
Hashmap *watch_pids; /* pid => Unit object n:1 */ Hashmap *watch_pids; /* pid => Unit object n:1 */
@ -157,7 +157,7 @@ struct Manager {
Watch mount_watch; Watch mount_watch;
/* Data specific to the D-Bus subsystem */ /* Data specific to the D-Bus subsystem */
DBusConnection *bus, *system_bus; DBusConnection *api_bus, *system_bus;
Set *subscribed; Set *subscribed;
/* Data specific to the cgroup subsystem */ /* Data specific to the cgroup subsystem */

View File

@ -732,7 +732,6 @@ static void service_dump(Unit *u, FILE *f, const char *prefix) {
"%sPIDFile: %s\n", "%sPIDFile: %s\n",
prefix, s->pid_file); prefix, s->pid_file);
exec_context_dump(&s->exec_context, f, prefix); exec_context_dump(&s->exec_context, f, prefix);
for (c = 0; c < _SERVICE_EXEC_MAX; c++) { for (c = 0; c < _SERVICE_EXEC_MAX; c++) {

29
unit.c
View File

@ -131,6 +131,13 @@ Unit *unit_new(Manager *m) {
return u; 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) { int unit_add_name(Unit *u, const char *text) {
UnitType t; UnitType t;
char *s; 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)) else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
retroactively_stop_dependencies(u); 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 /* Maybe we finished startup and are now ready for being
* stopped because unneeded? */ * stopped because unneeded? */
unit_check_uneeded(u); unit_check_uneeded(u);

2
unit.h
View File

@ -284,6 +284,8 @@ int unit_load(Unit *unit);
const char* unit_id(Unit *u); const char* unit_id(Unit *u);
const char *unit_description(Unit *u); const char *unit_description(Unit *u);
bool unit_has_name(Unit *u, const char *name);
UnitActiveState unit_active_state(Unit *u); UnitActiveState unit_active_state(Unit *u);
void unit_dump(Unit *u, FILE *f, const char *prefix); void unit_dump(Unit *u, FILE *f, const char *prefix);