device: simplify config blob memory allocation handling
authorFelix Fietkau <nbd@openwrt.org>
Sat, 24 Jan 2015 00:09:23 +0000 (01:09 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sat, 24 Jan 2015 00:10:33 +0000 (01:10 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
config.c
device.c

index 7319b3c..43b9205 100644 (file)
--- a/config.c
+++ b/config.c
@@ -217,7 +217,7 @@ config_init_devices(void)
                        if (!dev)
                                continue;
 
                        if (!dev)
                                continue;
 
-                       device_apply_config(dev, dev->type, blob_memdup(b.head));
+                       device_apply_config(dev, dev->type, b.head);
                }
                dev->default_config = false;
        }
                }
                dev->default_config = false;
        }
index f50fb85..66eb261 100644 (file)
--- a/device.c
+++ b/device.c
@@ -631,6 +631,7 @@ device_apply_config(struct device *dev, const struct device_type *type,
                case DEV_CONFIG_RESTART:
                case DEV_CONFIG_APPLIED:
                        D(DEVICE, "Device '%s': config applied\n", dev->ifname);
                case DEV_CONFIG_RESTART:
                case DEV_CONFIG_APPLIED:
                        D(DEVICE, "Device '%s': config applied\n", dev->ifname);
+                       config = blob_memdup(config);
                        free(dev->config);
                        dev->config = config;
                        if (change == DEV_CONFIG_RESTART && dev->present) {
                        free(dev->config);
                        dev->config = config;
                        if (change == DEV_CONFIG_RESTART && dev->present) {
@@ -640,7 +641,6 @@ device_apply_config(struct device *dev, const struct device_type *type,
                        break;
                case DEV_CONFIG_NO_CHANGE:
                        D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
                        break;
                case DEV_CONFIG_NO_CHANGE:
                        D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
-                       free(config);
                        break;
                case DEV_CONFIG_RECREATE:
                        break;
                        break;
                case DEV_CONFIG_RECREATE:
                        break;
@@ -703,10 +703,6 @@ device_create(const char *name, const struct device_type *type,
        struct device *odev = NULL, *dev;
        enum dev_change_type change;
 
        struct device *odev = NULL, *dev;
        enum dev_change_type change;
 
-       config = blob_memdup(config);
-       if (!config)
-               return NULL;
-
        odev = device_get(name, false);
        if (odev) {
                odev->current_config = true;
        odev = device_get(name, false);
        if (odev) {
                odev->current_config = true;
@@ -722,6 +718,10 @@ device_create(const char *name, const struct device_type *type,
        } else
                D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
 
        } else
                D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
 
+       config = blob_memdup(config);
+       if (!config)
+               return NULL;
+
        dev = type->create(name, config);
        if (!dev)
                return NULL;
        dev = type->create(name, config);
        if (!dev)
                return NULL;