summaryrefslogtreecommitdiffhomepage
path: root/device.c
diff options
context:
space:
mode:
authorHans Dedecker <dedeckeh@gmail.com>2016-06-06 14:58:31 +0200
committerFelix Fietkau <nbd@nbd.name>2016-06-06 15:42:00 +0200
commit29521f9a232f039318fd8525c410b3127836626f (patch)
tree10b3ea2876ecbf09de1c839ff9e1baa9935ee676 /device.c
parentfef10041f3a495d79f03f99bdf416a68a0ee91aa (diff)
device: Fix device find failure in avl list due to device name change
As device name is used as key in avl list a device name change will break the avl find logic. Function device_set_ifname offers api to set the device name and re-inserts the avl node in the list when the avl key value is changed. Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Diffstat (limited to 'device.c')
-rw-r--r--device.c29
1 files changed, 25 insertions, 4 deletions
diff --git a/device.c b/device.c
index 06c4820..a4d804e 100644
--- a/device.c
+++ b/device.c
@@ -422,14 +422,14 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con
assert(dev);
assert(type);
- if (name)
- strncpy(dev->ifname, name, IFNAMSIZ);
-
- D(DEVICE, "Initialize device '%s'\n", dev->ifname);
+ D(DEVICE, "Initialize device '%s'\n", name ? name : "");
INIT_SAFE_LIST(&dev->users);
INIT_SAFE_LIST(&dev->aliases);
dev->type = type;
+ if (name)
+ device_set_ifname(dev, name);
+
if (!dev->set_state)
dev->set_state = set_device_state;
}
@@ -582,6 +582,27 @@ void device_set_ifindex(struct device *dev, int ifindex)
device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
}
+int device_set_ifname(struct device *dev, const char *name)
+{
+ int ret = 0;
+
+ if (!strcmp(dev->ifname, name))
+ return 0;
+
+ if (dev->avl.key)
+ avl_delete(&devices, &dev->avl);
+
+ strncpy(dev->ifname, name, IFNAMSIZ);
+
+ if (dev->avl.key)
+ ret = avl_insert(&devices, &dev->avl);
+
+ if (ret == 0)
+ device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
+
+ return ret;
+}
+
static int device_refcount(struct device *dev)
{
struct list_head *list;