From 29521f9a232f039318fd8525c410b3127836626f Mon Sep 17 00:00:00 2001 From: Hans Dedecker Date: Mon, 6 Jun 2016 14:58:31 +0200 Subject: 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 --- device.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'device.c') 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; -- cgit v1.2.3