strcpy(alias->name, name);
alias->dev.set_state = alias_device_set_state;
alias->dev.hidden = true;
- device_init_virtual(&alias->dev, devtype, NULL);
+ if (device_init_virtual(&alias->dev, devtype, NULL) < 0) {
+ free(alias);
+ return NULL;
+ }
+
alias->avl.key = alias->name;
avl_insert(&aliases, &alias->avl);
alias->dep.alias = true;
return NULL;
dev = &bst->dev;
- device_init(dev, devtype, name);
+
+ if (device_init(dev, devtype, name) < 0) {
+ device_cleanup(dev);
+ free(bst);
+ return NULL;
+ }
+
dev->config_pending = true;
bst->retry.cb = bridge_retry_members;
return dev->type->check_state(dev);
}
-void device_init_virtual(struct device *dev, struct device_type *type, const char *name)
+int device_init_virtual(struct device *dev, struct device_type *type, const char *name)
{
assert(dev);
assert(type);
INIT_SAFE_LIST(&dev->aliases);
dev->type = type;
- if (name)
- device_set_ifname(dev, name);
+ if (name) {
+ int ret;
+
+ ret = device_set_ifname(dev, name);
+ if (ret < 0)
+ return ret;
+ }
if (!dev->set_state)
dev->set_state = set_device_state;
+
+ return 0;
}
int device_init(struct device *dev, struct device_type *type, const char *ifname)
{
int ret;
- device_init_virtual(dev, type, ifname);
+ ret = device_init_virtual(dev, type, ifname);
+ if (ret < 0)
+ return ret;
dev->avl.key = dev->ifname;
dev->external = external;
dev->set_state = simple_device_set_state;
- device_init(dev, &simple_device_type, name);
+
+ if (device_init(dev, &simple_device_type, name) < 0) {
+ device_cleanup(dev);
+ free(dev);
+ return NULL;
+ }
+
dev->default_config = true;
if (external)
system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
if (!strcmp(dev->ifname, name))
return 0;
+ if (strlen(name) > sizeof(dev->ifname) - 1)
+ return -1;
+
if (dev->avl.key)
avl_delete(&devices, &dev->avl);
- strncpy(dev->ifname, name, IFNAMSIZ);
+ strcpy(dev->ifname, name);
if (dev->avl.key)
ret = avl_insert(&devices, &dev->avl);
void device_reset_config(void);
void device_reset_old(void);
-void device_init_virtual(struct device *dev, struct device_type *type, const char *name);
+int device_init_virtual(struct device *dev, struct device_type *type, const char *name);
int device_init(struct device *dev, struct device_type *type, const char *ifname);
void device_cleanup(struct device *dev);
struct device *device_find(const char *name);
return NULL;
dev = &mvdev->dev;
- device_init(dev, devtype, name);
+ if (device_init(dev, devtype, name) < 0) {
+ device_cleanup(dev);
+ free(mvdev);
+ return NULL;
+ }
+
dev->config_pending = true;
mvdev->set_state = dev->set_state;
return NULL;
dev = &tun->dev;
- device_init(dev, devtype, name);
+
+ if (device_init(dev, devtype, name) < 0) {
+ device_cleanup(dev);
+ free(tun);
+ return NULL;
+ }
+
tun->set_state = dev->set_state;
dev->set_state = tunnel_set_state;
device_apply_config(dev, devtype, attr);
return NULL;
dev = &veth->dev;
- device_init(dev, devtype, name);
+ if (device_init(dev, devtype, name) < 0) {
+ device_cleanup(dev);
+ free(veth);
+ return NULL;
+ }
+
dev->config_pending = true;
veth->set_state = dev->set_state;
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;
vldev->dev.hidden = dev->hidden;
sprintf(name, "%s.%d", dev->ifname, vldev->id);
- device_set_ifname(&vldev->dev, name);
+ return device_set_ifname(&vldev->dev, name);
}
static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
device_set_present(&vldev->dev, false);
break;
case DEV_EVENT_UPDATE_IFNAME:
- vlan_dev_set_name(vldev, dep->dev);
+ if (vlan_dev_set_name(vldev, dep->dev) < 0)
+ free_vlan_if(&vldev->dev);
break;
case DEV_EVENT_TOPO_CHANGE:
/* Propagate topo changes */
struct vlan_device *vldev;
struct device_user *dep;
- if (strlen(dev->ifname) > (IFNAMSIZ - 6))
- return NULL;
-
/* look for an existing interface before creating a new one */
list_for_each_entry(dep, &dev->users.list, list.list) {
if (dep->cb != vlan_dev_cb)
vldev->id = id;
- device_init(&vldev->dev, &vlan_type, NULL);
+ if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
+ goto error;
+
+ if (vlan_dev_set_name(vldev, dev) < 0)
+ goto error;
- vlan_dev_set_name(vldev, dev);
vldev->dev.default_config = true;
vldev->set_state = vldev->dev.set_state;
device_add_user(&vldev->dep, dev);
return &vldev->dev;
+
+error:
+ device_cleanup(&vldev->dev);
+ free(vldev);
+ return NULL;
}
static char *split_vlan(char *s)
return NULL;
dev = &mvdev->dev;
- device_init(dev, devtype, name);
+
+ if (device_init(dev, devtype, name) < 0) {
+ device_cleanup(dev);
+ free(mvdev);
+ return NULL;
+ }
+
dev->config_pending = true;
mvdev->set_state = dev->set_state;