use a common function for creating devices that avoids creating duplicates
authorFelix Fietkau <nbd@openwrt.org>
Sun, 2 Oct 2011 22:24:01 +0000 (00:24 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 2 Oct 2011 22:24:01 +0000 (00:24 +0200)
config.c
device.c
device.h

index d332e57a4e4359da1b8310d7208349400a0ecc68..65eaa81fe008f8aa0a2abd941e708c74d31cb0cd 100644 (file)
--- a/config.c
+++ b/config.c
@@ -131,7 +131,7 @@ config_parse_bridge_interface(struct uci_section *s)
        blobmsg_add_string(&b, "name", name);
 
        uci_to_blob(&b, s, bridge_device_type.config_params);
-       if (!bridge_device_type.create(b.head)) {
+       if (!device_create(name, &bridge_device_type, b.head)) {
                DPRINTF("Failed to create bridge for interface '%s'\n", s->e.name);
                return -EINVAL;
        }
@@ -185,20 +185,24 @@ config_init_devices(void)
        uci_foreach_element(&uci_network->sections, e) {
                struct uci_section *s = uci_to_section(e);
                const struct device_type *devtype;
-               const char *type;
+               const char *type, *name;
 
                if (strcmp(s->type, "device") != 0)
                        continue;
 
-               blob_buf_init(&b, 0);
+               name = uci_lookup_option_string(uci_ctx, s, "name");
+               if (!name)
+                       continue;
+
                type = uci_lookup_option_string(uci_ctx, s, "type");
                if (type && !strcmp(type, "bridge"))
                        devtype = &bridge_device_type;
                else
                        devtype = &simple_device_type;
 
+               blob_buf_init(&b, 0);
                uci_to_blob(&b, s, devtype->config_params);
-               devtype->create(b.head);
+               device_create(name, devtype, b.head);
        }
 }
 
index 6b333fb56938fd71aacaf366b115203194c78655..77594be367ce1dfb6ba70c176b05ea4135d9128d 100644 (file)
--- a/device.c
+++ b/device.c
@@ -297,3 +297,16 @@ device_free_unused(struct device *dev)
        avl_for_each_element_safe(&devices, dev, avl, tmp)
                __device_free_unused(dev);
 }
+
+struct device *
+device_create(const char *name, const struct device_type *type,
+             struct blob_attr *config)
+{
+       struct device *dev;
+
+       dev = device_get(name, false);
+       if (dev)
+               return dev;
+
+       return type->create(config);
+}
index cb7146a5f0be5584f0c5bc72ae45502818ea1a83..f1cdda00efaeb06aeb37fa9beff4eb727536eff8 100644 (file)
--- a/device.h
+++ b/device.h
@@ -102,6 +102,8 @@ extern const struct config_param_list device_attr_list;
 extern const struct device_type simple_device_type;
 extern const struct device_type bridge_device_type;
 
+struct device *device_create(const char *name, const struct device_type *type,
+                            struct blob_attr *config);
 void device_init_settings(struct device *dev, struct blob_attr **tb);
 
 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name);