fix the ubus hotplug add/remove interface to properly clean up devices
authorFelix Fietkau <nbd@openwrt.org>
Mon, 5 Sep 2011 01:56:10 +0000 (03:56 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 5 Sep 2011 01:56:10 +0000 (03:56 +0200)
ubus.c

diff --git a/ubus.c b/ubus.c
index 8b9bc3a19d76175ce7985da5cf71a05a4ba74b17..2c4e4e6823ad6d8c009cf9c8c79dfb8bc9d9b40d 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -207,33 +207,35 @@ netifd_iface_handle_device(struct ubus_context *ctx, struct ubus_object *obj,
        if (!tb[DEV_NAME])
                return UBUS_STATUS_INVALID_ARGUMENT;
 
-       dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
-       if (!dev)
-               return UBUS_STATUS_NOT_FOUND;
-
        main_dev = iface->main_dev.dev;
        if (!main_dev)
                return UBUS_STATUS_NOT_FOUND;
 
-       if (main_dev == dev)
-               return UBUS_STATUS_INVALID_ARGUMENT;
-
        if (!main_dev->hotplug_ops)
                return UBUS_STATUS_NOT_SUPPORTED;
 
-       if (add)
-               ret = main_dev->hotplug_ops->add(main_dev, dev);
-       else
-               ret = main_dev->hotplug_ops->del(main_dev, dev);
+       dev = device_get(blobmsg_data(tb[DEV_NAME]), add);
+       if (!dev)
+               return UBUS_STATUS_NOT_FOUND;
+
+       if (main_dev != dev) {
+               if (add)
+                       ret = main_dev->hotplug_ops->add(main_dev, dev);
+               else
+                       ret = main_dev->hotplug_ops->del(main_dev, dev);
+               if (ret)
+                       ret = UBUS_STATUS_UNKNOWN_ERROR;
+       } else {
+               ret = UBUS_STATUS_INVALID_ARGUMENT;
+       }
 
-       if (ret)
-               return UBUS_STATUS_UNKNOWN_ERROR;
+       if (add)
+               device_free_unused(dev);
 
-       return 0;
+       return ret;
 }
 
 
-
 static struct ubus_method iface_object_methods[] = {
        { .name = "up", .handler = netifd_handle_up },
        { .name = "down", .handler = netifd_handle_down },