From d0baa06fda52905c24d93df27a9f3e879fab4d0d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 23 May 2011 21:38:20 +0200 Subject: [PATCH] hostnamed: split dbus initialization into a separate function --- src/hostnamed.c | 67 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/src/hostnamed.c b/src/hostnamed.c index ce690453..cf2172fd 100644 --- a/src/hostnamed.c +++ b/src/hostnamed.c @@ -655,17 +655,55 @@ oom: return DBUS_HANDLER_RESULT_NEED_MEMORY; } -int main(int argc, char *argv[]) { - const DBusObjectPathVTable hostname_vtable = { +static int connect_bus(DBusConnection **_bus) { + static const DBusObjectPathVTable hostname_vtable = { .message_function = hostname_message_handler }; - - DBusConnection *bus = NULL; DBusError error; + DBusConnection *bus = NULL; int r; + assert(_bus); + dbus_error_init(&error); + bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); + if (!bus) { + log_error("Failed to get system D-Bus connection: %s", error.message); + r = -ECONNREFUSED; + goto fail; + } + + if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL)) { + log_error("Not enough memory"); + r = -ENOMEM; + goto fail; + } + + if (dbus_bus_request_name(bus, "org.freedesktop.hostname1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) { + log_error("Failed to register name on bus: %s", error.message); + r = -EEXIST; + goto fail; + } + + if (_bus) + *_bus = bus; + + return 0; + +fail: + dbus_connection_close(bus); + dbus_connection_unref(bus); + + dbus_error_free(&error); + + return r; +} + +int main(int argc, char *argv[]) { + int r; + DBusConnection *bus = NULL; + log_set_target(LOG_TARGET_AUTO); log_parse_environment(); log_open(); @@ -687,24 +725,9 @@ int main(int argc, char *argv[]) { goto finish; } - bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); - if (!bus) { - log_error("Failed to get system D-Bus connection: %s", error.message); - r = -ECONNREFUSED; - goto finish; - } - - if (!dbus_connection_register_object_path(bus, "/org/freedesktop/hostname1", &hostname_vtable, NULL)) { - log_error("Not enough memory"); - r = -ENOMEM; - goto finish; - } - - if (dbus_bus_request_name(bus, "org.freedesktop.hostname1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) { - log_error("Failed to register name on bus: %s", error.message); - r = -EEXIST; + r = connect_bus(&bus); + if (r < 0) goto finish; - } while (dbus_connection_read_write_dispatch(bus, -1)) ; @@ -720,7 +743,5 @@ finish: dbus_connection_unref(bus); } - dbus_error_free(&error); - return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; } -- 2.39.5