system-linux: improve handling of device rename
[oweals/netifd.git] / vlan.c
diff --git a/vlan.c b/vlan.c
index 3d0a4cf3dae187c4ba090aed00dc8844b249ee28..8748b3040d511f554ec335071f39b4b0a120df63 100644 (file)
--- a/vlan.c
+++ b/vlan.c
@@ -61,32 +61,32 @@ static int vlan_set_device_state(struct device *dev, bool up)
        return ret;
 }
 
-static void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev)
+static int vlan_dev_set_name(struct vlan_device *vldev, struct device *dev)
 {
+       char *name;
+
+       name = alloca(strlen(dev->ifname) + sizeof(".2147483647\0"));
        vldev->dev.hidden = dev->hidden;
-       snprintf(vldev->dev.ifname, IFNAMSIZ, "%s.%d", dev->ifname, vldev->id);
+       sprintf(name, "%s.%d", dev->ifname, vldev->id);
+
+       return device_set_ifname(&vldev->dev, name);
 }
 
 static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
 {
        struct vlan_device *vldev;
-       bool new_state = false;
 
        vldev = container_of(dep, struct vlan_device, dep);
        switch(ev) {
        case DEV_EVENT_ADD:
-               new_state = true;
-       case DEV_EVENT_REMOVE:
-               device_set_present(&vldev->dev, new_state);
+               device_set_present(&vldev->dev, true);
                break;
-       case DEV_EVENT_LINK_UP:
-               new_state = true;
-       case DEV_EVENT_LINK_DOWN:
-               device_set_link(&vldev->dev, new_state);
+       case DEV_EVENT_REMOVE:
+               device_set_present(&vldev->dev, false);
                break;
        case DEV_EVENT_UPDATE_IFNAME:
-               vlan_dev_set_name(vldev, dep->dev);
-               device_broadcast_event(&vldev->dev, ev);
+               if (vlan_dev_set_name(vldev, dep->dev) < 0)
+                       free_vlan_if(&vldev->dev);
                break;
        case DEV_EVENT_TOPO_CHANGE:
                /* Propagate topo changes */
@@ -99,10 +99,9 @@ static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
 
 static struct device *get_vlan_device(struct device *dev, int id, bool create)
 {
-       static const struct device_type vlan_type = {
+       static struct device_type vlan_type = {
                .name = "VLAN",
                .config_params = &device_attr_list,
-               .keep_link_status = true,
                .free = free_vlan_if,
        };
        struct vlan_device *vldev;
@@ -126,11 +125,17 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create)
        D(DEVICE, "Create vlan device '%s.%d'\n", dev->ifname, id);
 
        vldev = calloc(1, sizeof(*vldev));
+       if (!vldev)
+               return NULL;
 
        vldev->id = id;
-       vlan_dev_set_name(vldev, dev);
 
-       device_init(&vldev->dev, &vlan_type, vldev->dev.ifname);
+       if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
+               goto error;
+
+       if (vlan_dev_set_name(vldev, dev) < 0)
+               goto error;
+
        vldev->dev.default_config = true;
 
        vldev->set_state = vldev->dev.set_state;
@@ -140,6 +145,11 @@ static struct device *get_vlan_device(struct device *dev, int id, bool create)
        device_add_user(&vldev->dep, dev);
 
        return &vldev->dev;
+
+error:
+       device_cleanup(&vldev->dev);
+       free(vldev);
+       return NULL;
 }
 
 static char *split_vlan(char *s)