X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=device.c;h=ccf66d7b17288178c112ad2f87454c1da1e076a7;hp=c6765b0ce174e94509cf2a935584d72d34da48fa;hb=f1b77140237f2f6e7f6b755b5a60ab212782456e;hpb=cd51d94890e819e6e72fd9dc221716131e69dea7 diff --git a/device.c b/device.c index c6765b0..ccf66d7 100644 --- a/device.c +++ b/device.c @@ -208,7 +208,20 @@ int device_init(struct device *dev, const struct device_type *type, const char * return 0; } -struct device *device_get(const char *name, bool create) +static struct device * +device_create_default(const char *name) +{ + struct device *dev; + + D(DEVICE, "Create simple device '%s'\n", name); + dev = calloc(1, sizeof(*dev)); + device_init(dev, &simple_device_type, name); + dev->default_config = true; + return dev; +} + +struct device * +device_get(const char *name, bool create) { struct device *dev; @@ -222,10 +235,18 @@ struct device *device_get(const char *name, bool create) if (!create) return NULL; - dev = calloc(1, sizeof(*dev)); - device_init(dev, &simple_device_type, name); + return device_create_default(name); +} - return dev; +static void +device_delete(struct device *dev) +{ + if (!dev->avl.key) + return; + + D(DEVICE, "Delete device '%s' from list\n", dev->ifname); + avl_delete(&devices, &dev->avl); + dev->avl.key = NULL; } void device_cleanup(struct device *dev) @@ -240,8 +261,7 @@ void device_cleanup(struct device *dev) dep->cb(dep, DEV_EVENT_REMOVE); } - if (dev->avl.key) - avl_delete(&devices, &dev->avl); + device_delete(dev); } void device_set_present(struct device *dev, bool state) @@ -268,7 +288,7 @@ void device_add_user(struct device_user *dep, struct device *dev) static void __device_free_unused(struct device *dev) { - if (!list_empty(&dev->users)) + if (!list_empty(&dev->users) || dev->current_config) return; device_free(dev); @@ -333,8 +353,12 @@ device_reload_config(struct device *dev, struct blob_attr *attr) } static enum dev_change_type -device_check_config(struct device *dev, struct blob_attr *attr) +device_check_config(struct device *dev, const struct device_type *type, + struct blob_attr *attr) { + if (type != dev->type) + return DEV_CONFIG_RECREATE; + if (dev->type->reload) return dev->type->reload(dev, attr); @@ -360,6 +384,32 @@ device_replace(struct device *dev, struct device *odev) device_set_present(dev, true); } +void +device_reset_config(void) +{ + struct device *dev; + + avl_for_each_element(&devices, dev, avl) + dev->current_config = false; +} + +void +device_reset_old(void) +{ + struct device *dev, *tmp, *ndev; + + avl_for_each_element_safe(&devices, dev, avl, tmp) { + if (dev->current_config || dev->default_config) + continue; + + if (dev->type != &simple_device_type) + continue; + + ndev = device_create_default(dev->ifname); + device_replace(ndev, dev); + } +} + struct device * device_create(const char *name, const struct device_type *type, struct blob_attr *config) @@ -374,20 +424,25 @@ device_create(const char *name, const struct device_type *type, odev = device_get(name, false); if (odev) { - change = device_check_config(odev, config); + odev->current_config = true; + change = device_check_config(odev, type, config); switch (change) { case DEV_CONFIG_APPLIED: + D(DEVICE, "Device '%s': config applied\n", odev->ifname); free(odev->config); odev->config = config; if (odev->present) { device_set_present(odev, false); device_set_present(odev, true); } - /* fall through */ + return odev; case DEV_CONFIG_NO_CHANGE: + D(DEVICE, "Device '%s': no configuration change\n", odev->ifname); free(config); return odev; case DEV_CONFIG_RECREATE: + D(DEVICE, "Device '%s': recreate device\n", odev->ifname); + device_delete(odev); break; } } @@ -396,6 +451,7 @@ device_create(const char *name, const struct device_type *type, if (!dev) return NULL; + dev->current_config = true; dev->config = config; if (odev) device_replace(dev, odev);