netifd: set current_config for simple devices when parsing device config
[project/netifd.git] / config.c
index 9fb9c01..22dd835 100644 (file)
--- a/config.c
+++ b/config.c
@@ -11,6 +11,7 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  */
+#define _GNU_SOURCE
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -104,7 +105,7 @@ config_parse_interface(struct uci_section *s, bool alias)
        if (iface->proto_handler && iface->proto_handler->config_params)
                uci_to_blob(&b, s, iface->proto_handler->config_params);
 
-       if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params))
+       if (!bridge && uci_to_blob(&b, s, simple_device_type.config_params) > 1)
                iface->device_config = true;
 
        config = blob_memdup(b.head);
@@ -135,7 +136,8 @@ config_parse_interface(struct uci_section *s, bool alias)
        if (blob_len(b.head) == 0)
                return;
 
-       device_set_config(dev, dev->type, b.head);
+       if (iface->device_config && dev->default_config)
+               device_set_config(dev, dev->type, b.head);
        return;
 error_free_config:
        free(config);
@@ -173,8 +175,10 @@ config_init_devices(void)
        struct uci_element *e;
 
        uci_foreach_element(&uci_network->sections, e) {
+               const struct uci_blob_param_list *params = NULL;
                struct uci_section *s = uci_to_section(e);
                const struct device_type *devtype = NULL;
+               struct device *dev;
                const char *type, *name;
 
                if (strcmp(s->type, "device") != 0)
@@ -198,12 +202,26 @@ config_init_devices(void)
                                devtype = &tunnel_device_type;
                }
 
-               if (!devtype)
-                       devtype = &simple_device_type;
+               if (devtype)
+                       params = devtype->config_params;
+               if (!params)
+                       params = simple_device_type.config_params;
 
                blob_buf_init(&b, 0);
-               uci_to_blob(&b, s, devtype->config_params);
-               device_create(name, devtype, b.head);
+               uci_to_blob(&b, s, params);
+               if (devtype) {
+                       dev = device_create(name, devtype, b.head);
+                       if (!dev)
+                               continue;
+               } else {
+                       dev = device_get(name, 1);
+                       if (!dev)
+                               continue;
+
+                       dev->current_config = true;
+                       device_apply_config(dev, dev->type, b.head);
+               }
+               dev->default_config = false;
        }
 }
 
@@ -332,16 +350,14 @@ config_parse_wireless_device(struct uci_section *s)
 static void
 config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
 {
-       char *name = NULL;
+       char *name;
 
-       if (s->anonymous) {
-               name = alloca(strlen(s->type) + 16);
-               asprintf(&name, "@%s[%d]", s->type, config_section_idx(s));
-       }
+       name = alloca(strlen(s->type) + 16);
+       sprintf(name, "@%s[%d]", s->type, config_section_idx(s));
 
        blob_buf_init(&b, 0);
        uci_to_blob(&b, s, wdev->drv->interface.config);
-       wireless_interface_create(wdev, b.head, name ? name : s->e.name);
+       wireless_interface_create(wdev, b.head, s->anonymous ? name : s->e.name);
 }
 
 static void