clear all remaining addresses on interface down
[project/netifd.git] / device.c
index 0efdba5..ef70685 100644 (file)
--- a/device.c
+++ b/device.c
@@ -3,24 +3,17 @@
 #include <stdio.h>
 #include <assert.h>
 
-#include <libubox/uapi.h>
-
 #include "netifd.h"
 #include "system.h"
 
 static struct avl_tree devices;
 
-static int avl_strcmp(const void *k1, const void *k2, void *ptr)
-{
-       return strcmp(k1, k2);
-}
-
-static void API_CTOR dev_init(void)
+static void __init dev_init(void)
 {
        avl_init(&devices, avl_strcmp, false, NULL);
 }
 
-static void free_device(struct device *dev)
+static void free_simple_device(struct device *dev)
 {
        cleanup_device(dev);
        free(dev);
@@ -96,7 +89,7 @@ void init_virtual_device(struct device *dev, const struct device_type *type, con
        if (name)
                strncpy(dev->ifname, name, IFNAMSIZ);
 
-       fprintf(stderr, "Initialize interface '%s'\n", dev->ifname);
+       fprintf(stderr, "Initialize device '%s'\n", dev->ifname);
        INIT_LIST_HEAD(&dev->users);
        dev->type = type;
 }
@@ -126,7 +119,7 @@ struct device *get_device(const char *name, bool create)
        static const struct device_type simple_type = {
                .name = "Device",
                .check_state = system_if_check,
-               .free = free_device,
+               .free = free_simple_device,
        };
        struct device *dev;
 
@@ -151,7 +144,7 @@ void cleanup_device(struct device *dev)
 {
        struct device_user *dep, *tmp;
 
-       fprintf(stderr, "Clean up interface '%s'\n", dev->ifname);
+       fprintf(stderr, "Clean up device '%s'\n", dev->ifname);
        list_for_each_entry_safe(dep, tmp, &dev->users, list) {
                if (!dep->cb)
                        continue;
@@ -191,9 +184,22 @@ void remove_device_user(struct device_user *dep)
        list_del(&dep->list);
 
        if (list_empty(&dev->users)) {
-               /* all references have gone away, remove this interface */
-               dev->type->free(dev);
+               /* all references have gone away, remove this device */
+               free_device(dev);
        }
 
        dep->dev = NULL;
 }
+
+void
+cleanup_devices(void)
+{
+       struct device *dev, *tmp;
+
+       avl_for_each_element_safe(&devices, dev, avl, tmp) {
+               if (!list_empty(&dev->users))
+                       continue;
+
+               free_device(dev);
+       }
+}