X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=device.c;h=ab8ff915ce68038de91c47ba0123e6e57c7d2d1d;hp=c02335c1b906c653331d72af5c8220215bcfae97;hb=338e6dc8a5e3d9c5eccc317e0476d43e6e4182c2;hpb=4e335f640fadda81aff9dd2dc06703961bea2826 diff --git a/device.c b/device.c index c02335c..ab8ff91 100644 --- a/device.c +++ b/device.c @@ -7,6 +7,10 @@ #include #include +#ifdef linux +#include +#endif + #include "netifd.h" #include "system.h" #include "config.h" @@ -179,7 +183,7 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con if (name) strncpy(dev->ifname, name, IFNAMSIZ); - fprintf(stderr, "Initialize device '%s'\n", dev->ifname); + DPRINTF("Initialize device '%s'\n", dev->ifname); INIT_LIST_HEAD(&dev->users); dev->type = type; } @@ -228,7 +232,7 @@ void device_cleanup(struct device *dev) { struct device_user *dep, *tmp; - fprintf(stderr, "Clean up device '%s'\n", dev->ifname); + DPRINTF("Clean up device '%s'\n", dev->ifname); list_for_each_entry_safe(dep, tmp, &dev->users, list) { if (!dep->cb) continue; @@ -261,6 +265,15 @@ void device_add_user(struct device_user *dep, struct device *dev) } } +static void +__device_free_unused(struct device *dev) +{ + if (!list_empty(&dev->users)) + return; + + device_free(dev); +} + void device_remove_user(struct device_user *dep) { struct device *dev = dep->dev; @@ -269,24 +282,100 @@ void device_remove_user(struct device_user *dep) device_release(dep); list_del(&dep->list); - - if (list_empty(&dev->users)) { - /* all references have gone away, remove this device */ - device_free(dev); - } - dep->dev = NULL; + __device_free_unused(dev); } void -device_free_all(void) +device_free_unused(struct device *dev) { - struct device *dev, *tmp; + struct device *tmp; - avl_for_each_element_safe(&devices, dev, avl, tmp) { - if (!list_empty(&dev->users)) - continue; + if (dev) + return __device_free_unused(dev); + + avl_for_each_element_safe(&devices, dev, avl, tmp) + __device_free_unused(dev); +} + +enum dev_change_type +device_reload_config(struct device *dev, struct blob_attr *attr) +{ + struct blob_attr *tb[__DEV_ATTR_MAX], *tb1[__DEV_ATTR_MAX]; + + blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, + blob_data(attr), blob_len(attr)); + if (dev->config) + blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb1, + blob_data(dev->config), blob_len(dev->config)); + else + memset(tb1, 0, sizeof(tb1)); + + if (!config_diff(tb, tb1, &device_attr_list, NULL)) + return DEV_CONFIG_NO_CHANGE; + + device_init_settings(dev, tb); + return DEV_CONFIG_APPLIED; +} + +static enum dev_change_type +device_check_config(struct device *dev, struct blob_attr *attr) +{ + if (dev->type->reload) + return dev->type->reload(dev, attr); + + return device_reload_config(dev, attr); +} + +static void +device_replace(struct device *dev, struct device *odev) +{ + struct device_user *dep, *tmp; + bool present = odev->present; + + if (present) + device_set_present(odev, false); - device_free(dev); + list_for_each_entry_safe(dep, tmp, &odev->users, list) { + list_move_tail(&dep->list, &dev->users); + dep->dev = dev; } + device_free(odev); + + if (present) + device_set_present(dev, true); +} + +struct device * +device_create(const char *name, const struct device_type *type, + struct blob_attr *config) +{ + struct device *odev = NULL, *dev; + enum dev_change_type change; + + odev = device_get(name, false); + if (odev) { + change = device_check_config(odev, config); + switch (change) { + case DEV_CONFIG_APPLIED: + free(odev->config); + odev->config = config_memdup(config); + if (odev->present) { + device_set_present(odev, false); + device_set_present(odev, true); + } + /* fall through */ + case DEV_CONFIG_NO_CHANGE: + return odev; + case DEV_CONFIG_RECREATE: + break; + } + } + + dev = type->create(config); + dev->config = config_memdup(config); + if (odev) + device_replace(dev, odev); + + return dev; }