diff options
author | Ray Strode <rstrode@redhat.com> | 2014-04-29 13:32:43 -0400 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2015-01-14 14:26:48 +1000 |
commit | 4f91646582b7ab338d12094386c0e0808bbcd64e (patch) | |
tree | c5110a33eb9c9c589445c6a6aa5877fcc6cc0e84 | |
parent | 87a611bf1a33cf461ea273e9ca88e0315bb81154 (diff) |
dbus: make dbus_core_init idempotent
Right now calling the function multiple times will lead to
resource leaks and handlers getting registered multiple times.
This commit changes it to be idempotent, which will be useful
for fixing server regeneration in a future commit.
Reviewed-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r-- | config/dbus-core.c | 58 |
1 files changed, 29 insertions, 29 deletions
diff --git a/config/dbus-core.c b/config/dbus-core.c index 8351ea4b3..60e9a250a 100644 --- a/config/dbus-core.c +++ b/config/dbus-core.c @@ -138,39 +138,42 @@ connect_to_bus(void) DBusError error; struct dbus_core_hook *hook; - dbus_error_init(&error); - bus_info.connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); - if (!bus_info.connection || dbus_error_is_set(&error)) { - LogMessage(X_ERROR, "dbus-core: error connecting to system bus: %s (%s)\n", - error.name, error.message); - goto err_begin; - } + if (!bus_info.connection) { + dbus_error_init(&error); + bus_info.connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); + if (!bus_info.connection || dbus_error_is_set(&error)) { + LogMessage(X_ERROR, "dbus-core: error connecting to system bus: %s (%s)\n", + error.name, error.message); + goto err_begin; + } - /* Thankyou. Really, thankyou. */ - dbus_connection_set_exit_on_disconnect(bus_info.connection, FALSE); + /* Thankyou. Really, thankyou. */ + dbus_connection_set_exit_on_disconnect(bus_info.connection, FALSE); - if (!dbus_connection_get_unix_fd(bus_info.connection, &bus_info.fd)) { - ErrorF("[dbus-core] couldn't get fd for system bus\n"); - goto err_unref; - } + if (!dbus_connection_get_unix_fd(bus_info.connection, &bus_info.fd)) { + ErrorF("[dbus-core] couldn't get fd for system bus\n"); + goto err_unref; + } - if (!dbus_connection_add_filter(bus_info.connection, message_filter, - &bus_info, NULL)) { - ErrorF("[dbus-core] couldn't add filter: %s (%s)\n", error.name, - error.message); - goto err_fd; - } + if (!dbus_connection_add_filter(bus_info.connection, message_filter, + &bus_info, NULL)) { + ErrorF("[dbus-core] couldn't add filter: %s (%s)\n", error.name, + error.message); + goto err_fd; + } - dbus_error_free(&error); - AddGeneralSocket(bus_info.fd); + dbus_error_free(&error); + AddGeneralSocket(bus_info.fd); - RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, &bus_info); - - for (hook = bus_info.hooks; hook; hook = hook->next) { - if (hook->connect) - hook->connect(bus_info.connection, hook->data); + for (hook = bus_info.hooks; hook; hook = hook->next) { + if (hook->connect) + hook->connect(bus_info.connection, hook->data); + } } + RemoveBlockAndWakeupHandlers(block_handler, wakeup_handler, &bus_info); + RegisterBlockAndWakeupHandlers(block_handler, wakeup_handler, &bus_info); + return 1; err_fd: @@ -230,9 +233,6 @@ dbus_core_remove_hook(struct dbus_core_hook *hook) int dbus_core_init(void) { - memset(&bus_info, 0, sizeof(bus_info)); - bus_info.fd = -1; - bus_info.hooks = NULL; if (!connect_to_bus()) bus_info.timer = TimerSet(NULL, 0, 1, reconnect_timer, NULL); |