X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=device.c;h=428d0019d752edb813b269b18da538b631e06277;hp=11363807b834b69985204e02e7b52266affad082;hb=986c4a1434cc61d2de560690ecc0d532a157d40a;hpb=ccca61c97d460d73f29750abdf38cea20ac440f3 diff --git a/device.c b/device.c index 1136380..428d001 100644 --- a/device.c +++ b/device.c @@ -41,6 +41,20 @@ const struct config_param_list device_attr_list = { .params = dev_attrs, }; +static int __devlock = 0; + +void device_lock(void) +{ + __devlock++; +} + +void device_unlock(void) +{ + __devlock--; + if (!__devlock) + device_free_unused(NULL); +} + static struct device * simple_device_create(const char *name, struct blob_attr *attr) { @@ -172,6 +186,8 @@ alias_notify_device(const char *name, struct device *dev) { struct alias_device *alias; + device_lock(); + alias = avl_find_element(&aliases, name, alias, avl); if (!alias) return; @@ -189,6 +205,8 @@ alias_notify_device(const char *name, struct device *dev) if (!dev && alias->dep.dev && !alias->dep.dev->active) device_remove_user(&alias->dep); + + device_unlock(); } static int set_device_state(struct device *dev, bool state) @@ -290,12 +308,13 @@ int device_init(struct device *dev, const struct device_type *type, const char * } static struct device * -device_create_default(const char *name) +device_create_default(const char *name, bool external) { struct device *dev; D(DEVICE, "Create simple device '%s'\n", name); dev = calloc(1, sizeof(*dev)); + dev->external = external; device_init(dev, &simple_device_type, name); dev->default_config = true; return dev; @@ -314,7 +333,7 @@ device_alias_get(const char *name) } struct device * -device_get(const char *name, bool create) +device_get(const char *name, int create) { struct device *dev; @@ -331,7 +350,7 @@ device_get(const char *name, bool create) if (!create) return NULL; - return device_create_default(name); + return device_create_default(name, create > 1); } static void @@ -385,7 +404,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) || dev->current_config || config_init) + if (!list_empty(&dev->users) || dev->current_config || __devlock) return; device_free(dev); @@ -398,6 +417,7 @@ void device_remove_user(struct device_user *dep) if (!dep->dev) return; + dep->hotplug = false; if (dep->claimed) device_release(dep); @@ -508,7 +528,7 @@ device_reset_old(void) if (dev->type != &simple_device_type) continue; - ndev = device_create_default(dev->ifname); + ndev = device_create_default(dev->ifname, dev->external); device_replace(ndev, dev); } }