dbus: Get rid of unnecessary string duplication in dbus_path
authorJouni Malinen <j@w1.fi>
Fri, 1 Jan 2010 16:56:07 +0000 (18:56 +0200)
committerJouni Malinen <j@w1.fi>
Fri, 1 Jan 2010 16:56:07 +0000 (18:56 +0200)
There is no point in making it so complex to initialize a string
with two allocations and a function call.

wpa_supplicant/dbus/dbus_new.c
wpa_supplicant/dbus/dbus_old.c
wpa_supplicant/dbus/dbus_old.h

index a37419e..2941567 100644 (file)
 #include "dbus_common.h"
 #include "dbus_common_i.h"
 
-/**
- * wpas_dbus_set_path - Assign a dbus path to an interface
- * @wpa_s: wpa_supplicant interface structure
- * @path: dbus path to set on the interface
- * Returns: 0 on success, -1 on error
- */
-static int wpas_dbus_set_path(struct wpa_supplicant *wpa_s,
-                             const char *path)
-{
-       u32 len = os_strlen(path);
-       if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
-               return -1;
-       if (wpa_s->dbus_new_path)
-               return -1;
-       wpa_s->dbus_new_path = os_strdup(path);
-       return 0;
-}
-
 
 /**
  * wpas_dbus_signal_interface - Send a interface related event signal
@@ -1766,7 +1748,6 @@ int wpas_dbus_register_interface(struct wpa_supplicant *wpa_s)
 {
 
        struct wpa_dbus_object_desc *obj_desc = NULL;
-       char *path;
        struct wpas_dbus_priv *ctrl_iface = wpa_s->global->dbus;
        int next;
 
@@ -1775,19 +1756,13 @@ int wpas_dbus_register_interface(struct wpa_supplicant *wpa_s)
                return 0;
 
        /* Create and set the interface's object path */
-       path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
-       if (path == NULL)
+       wpa_s->dbus_new_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
+       if (wpa_s->dbus_new_path == NULL)
                return -1;
        next = ctrl_iface->next_objid++;
-       os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
+       os_snprintf(wpa_s->dbus_new_path, WPAS_DBUS_OBJECT_PATH_MAX,
                    WPAS_DBUS_NEW_PATH_INTERFACES "/%u",
                    next);
-       if (wpas_dbus_set_path(wpa_s, path)) {
-               wpa_printf(MSG_DEBUG,
-                          "Failed to set dbus path for interface %s",
-                          wpa_s->ifname);
-               goto err;
-       }
 
        obj_desc = os_zalloc(sizeof(struct wpa_dbus_object_desc));
        if (!obj_desc) {
@@ -1800,19 +1775,21 @@ int wpas_dbus_register_interface(struct wpa_supplicant *wpa_s)
                           wpas_dbus_interface_properties,
                           wpas_dbus_interface_signals);
 
-       wpa_printf(MSG_DEBUG, "dbus: Register interface object '%s'", path);
-       if (wpa_dbus_register_object_per_iface(ctrl_iface, path, wpa_s->ifname,
-                                              obj_desc))
+       wpa_printf(MSG_DEBUG, "dbus: Register interface object '%s'",
+                  wpa_s->dbus_new_path);
+       if (wpa_dbus_register_object_per_iface(ctrl_iface,
+                                              wpa_s->dbus_new_path,
+                                              wpa_s->ifname, obj_desc))
                goto err;
 
        wpas_dbus_signal_interface_added(wpa_s);
 
-       os_free(path);
        return 0;
 
 err:
+       os_free(wpa_s->dbus_new_path);
+       wpa_s->dbus_new_path = NULL;
        os_free(obj_desc);
-       os_free(path);
        return -1;
 }
 
index 2cf7ec6..674b9ff 100644 (file)
@@ -696,7 +696,6 @@ int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
        DBusObjectPathVTable vtable = {
                NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
        };
-       char *path;
        int ret = -1;
 
        /* Do nothing if the control interface is not turned on */
@@ -707,21 +706,16 @@ int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
        next = ctrl_iface->next_objid++;
 
        /* Create and set the interface's object path */
-       path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
-       if (path == NULL)
+       wpa_s->dbus_path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
+       if (wpa_s->dbus_path == NULL)
                return -1;
-       snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
-                WPAS_DBUS_PATH_INTERFACES "/%u",
-                next);
-       if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
-               wpa_printf(MSG_DEBUG,
-                          "Failed to set dbus path for interface %s",
-                          wpa_s->ifname);
-               goto out;
-       }
+       os_snprintf(wpa_s->dbus_path, WPAS_DBUS_OBJECT_PATH_MAX,
+                   WPAS_DBUS_PATH_INTERFACES "/%u",
+                   next);
 
        /* Register the message handler for the interface functions */
-       if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
+       if (!dbus_connection_register_fallback(con, wpa_s->dbus_path, &vtable,
+                                              wpa_s)) {
                perror("wpas_dbus_register_iface [dbus]");
                wpa_printf(MSG_ERROR, "Could not set up DBus message "
                           "handler for interface %s.", wpa_s->ifname);
@@ -730,7 +724,6 @@ int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
        ret = 0;
 
 out:
-       os_free(path);
        return ret;
 }
 
@@ -788,25 +781,6 @@ struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
 
 
 /**
- * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
- * @wpa_s: wpa_supplicant interface structure
- * @path: dbus path to set on the interface
- * Returns: 0 on succes, -1 on error
- */
-int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
-                                 const char *path)
-{
-       u32 len = strlen (path);
-       if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
-               return -1;
-       if (wpa_s->dbus_path)
-               return -1;
-       wpa_s->dbus_path = os_strdup(path);
-       return 0;
-}
-
-
-/**
  * wpa_supplicant_get_dbus_path - Get an interface's dbus path
  * @wpa_s: %wpa_supplicant interface structure
  * Returns: Interface's dbus object path, or %NULL on error
index 5aef01d..4c0b50d 100644 (file)
@@ -91,8 +91,6 @@ int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s);
 
 
 /* Methods internal to the dbus control interface */
-int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
-                                const char *path);
 const char *wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s);
 struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
        struct wpa_global *global, const char *path);