fix interrupted read checks in log collection
[project/netifd.git] / device.c
index bceba62..4d0de67 100644 (file)
--- a/device.c
+++ b/device.c
@@ -19,7 +19,6 @@ static struct avl_tree devices;
 
 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
        [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
-       [DEV_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
        [DEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
        [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 },
        [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
@@ -32,20 +31,12 @@ const struct config_param_list device_attr_list = {
 };
 
 static struct device *
-simple_device_create(struct blob_attr *attr)
+simple_device_create(const char *name, struct blob_attr *attr)
 {
        struct blob_attr *tb[__DEV_ATTR_MAX];
        struct device *dev = NULL;
-       const char *name;
 
        blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
-       if (!tb[DEV_ATTR_NAME])
-               return NULL;
-
-       name = blobmsg_data(tb[DEV_ATTR_NAME]);
-       if (!name)
-               return NULL;
-
        dev = device_get(name, true);
        if (!dev)
                return NULL;
@@ -206,6 +197,7 @@ int device_init(struct device *dev, const struct device_type *type, const char *
        if (ret < 0)
                return ret;
 
+       system_if_clear_state(dev);
        device_check_state(dev);
 
        return 0;
@@ -292,7 +284,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)
+       if (!list_empty(&dev->users) || dev->current_config || config_init)
                return;
 
        device_free(dev);
@@ -454,7 +446,7 @@ device_create(const char *name, const struct device_type *type,
        } else
                D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
 
-       dev = type->create(config);
+       dev = type->create(name, config);
        if (!dev)
                return NULL;
 
@@ -468,3 +460,36 @@ device_create(const char *name, const struct device_type *type,
 
        return dev;
 }
+
+void
+device_dump_status(struct blob_buf *b, struct device *dev)
+{
+       void *c, *s;
+
+       if (!dev) {
+               avl_for_each_element(&devices, dev, avl) {
+                       if (!dev->present)
+                               continue;
+                       c = blobmsg_open_table(b, dev->ifname);
+                       device_dump_status(b, dev);
+                       blobmsg_close_table(b, c);
+               }
+
+               return;
+       }
+
+       if (!dev->present)
+               return;
+
+       blobmsg_add_string(b, "type", dev->type->name);
+       blobmsg_add_u8(b, "up", !!dev->active);
+       if (dev->type->dump_info)
+               dev->type->dump_info(dev, b);
+
+       s = blobmsg_open_table(b, "statistics");
+       if (dev->type->dump_stats)
+               dev->type->dump_stats(dev, b);
+       else
+               system_if_dump_stats(dev, b);
+       blobmsg_close_table(b, s);
+}