From 37441269fbc27fc9af0562c6a4af50c1d6d353dd Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Thu, 6 Oct 2011 01:51:31 +0200 Subject: [PATCH] prevent deletion devices that are part of the current configuration --- config.c | 2 ++ device.c | 51 ++++++++++++++++++++++++++++++++++++++++++++------- device.h | 6 ++++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/config.c b/config.c index c4a6759..23902ff 100644 --- a/config.c +++ b/config.c @@ -329,6 +329,7 @@ config_init_interfaces(const char *name) uci_network = p; config_init = true; + device_reset_config(); config_init_devices(); uci_foreach_element(&p->sections, e) { @@ -343,6 +344,7 @@ config_init_interfaces(const char *name) device_free_unused(NULL); config_init = false; + device_reset_old(); device_init_pending(); interface_start_pending(); } diff --git a/device.c b/device.c index ce6f175..f856f22 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,11 +235,7 @@ struct device *device_get(const char *name, bool create) if (!create) return NULL; - D(DEVICE, "Create simple device '%s'\n", name); - dev = calloc(1, sizeof(*dev)); - device_init(dev, &simple_device_type, name); - - return dev; + return device_create_default(name); } static void @@ -279,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); @@ -375,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) @@ -389,6 +424,7 @@ device_create(const char *name, const struct device_type *type, odev = device_get(name, false); if (odev) { + odev->current_config = true; change = device_check_config(odev, type, config); switch (change) { case DEV_CONFIG_APPLIED: @@ -416,6 +452,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); diff --git a/device.h b/device.h index 45af150..176d1ef 100644 --- a/device.h +++ b/device.h @@ -63,6 +63,9 @@ struct device { bool present; int active; + bool current_config; + bool default_config; + /* set interface up or down */ device_state_cb set_state; @@ -115,6 +118,9 @@ struct device *device_create(const char *name, const struct device_type *type, void device_init_settings(struct device *dev, struct blob_attr **tb); void device_init_pending(void); +void device_reset_config(void); +void device_reset_old(void); + void device_init_virtual(struct device *dev, const struct device_type *type, const char *name); int device_init(struct device *iface, const struct device_type *type, const char *ifname); void device_cleanup(struct device *iface); -- 2.25.1