diff options
author | Hans Dedecker <dedeckeh@gmail.com> | 2016-06-06 14:58:31 +0200 |
---|---|---|
committer | Felix Fietkau <nbd@nbd.name> | 2016-06-06 15:42:00 +0200 |
commit | 29521f9a232f039318fd8525c410b3127836626f (patch) | |
tree | 10b3ea2876ecbf09de1c839ff9e1baa9935ee676 /device.c | |
parent | fef10041f3a495d79f03f99bdf416a68a0ee91aa (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.c | 29 |
1 files changed, 25 insertions, 4 deletions
@@ -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; |