diff options
author | Lennart Poettering <lennart@poettering.net> | 2011-05-23 21:38:20 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2011-06-21 19:29:43 +0200 |
commit | d0baa06fda52905c24d93df27a9f3e879fab4d0d (patch) | |
tree | 16a1be4dbe156976e846a1f93773fddcd304e215 /src | |
parent | 3df5bf612325b0f6a19acb67befe28d6b07d3804 (diff) |
hostnamed: split dbus initialization into a separate function
Diffstat (limited to 'src')
-rw-r--r-- | src/hostnamed.c | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/src/hostnamed.c b/src/hostnamed.c index ce69045344..cf2172fd2c 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; } |