]> err.no Git - systemd/commitdiff
hostnamed: split dbus initialization into a separate function
authorLennart Poettering <lennart@poettering.net>
Mon, 23 May 2011 19:38:20 +0000 (21:38 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 21 Jun 2011 17:29:43 +0000 (19:29 +0200)
src/hostnamed.c

index ce690453445ba7db8c8605cf483aa7733ce051c7..cf2172fd2c127d4ebc6347a5b607c3dd5b8b3eab 100644 (file)
@@ -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;
 }