From: Felix Fietkau Date: Thu, 5 Jul 2012 18:28:31 +0000 (+0200) Subject: alias: use a callback for managing device presence state, track alias deps in a separ... X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=51b3f21fef7f581a7df6d8a73d8b973f9eb355d2;p=oweals%2Fnetifd.git alias: use a callback for managing device presence state, track alias deps in a separate list to avoid recursion issues --- diff --git a/alias.c b/alias.c index 338ece6..07ed5fe 100644 --- a/alias.c +++ b/alias.c @@ -48,6 +48,24 @@ alias_device_set_state(struct device *dev, bool state) return 0; } +static void alias_device_cb(struct device_user *dep, enum device_event ev) +{ + struct alias_device *alias; + bool present = false; + + alias = container_of(dep, struct alias_device, dep); + switch (ev) { + case DEV_EVENT_ADD: + present = true; + case DEV_EVENT_REMOVE: + device_set_present(&alias->dev, present); + break; + default: + device_broadcast_event(&alias->dev, ev); + break; + } +} + static struct device * alias_device_create(const char *name, struct blob_attr *attr) { @@ -60,6 +78,8 @@ alias_device_create(const char *name, struct blob_attr *attr) device_init_virtual(&alias->dev, &alias_device_type, NULL); alias->avl.key = alias->name; avl_insert(&aliases, &alias->avl); + alias->dep.alias = true; + alias->dep.cb = alias_device_cb; return &alias->dev; } @@ -101,8 +121,6 @@ alias_notify_device(const char *name, struct device *dev) } } - device_set_present(&alias->dev, !!dev); - if (!dev && alias->dep.dev && !alias->dep.dev->active) { device_remove_user(&alias->dep); alias->dev.hidden = true; diff --git a/device.c b/device.c index 34ad9ad..80881d2 100644 --- a/device.c +++ b/device.c @@ -180,11 +180,12 @@ static void __init dev_init(void) avl_init(&devices, avl_strcmp, true, NULL); } -void device_broadcast_event(struct device *dev, enum device_event ev) + +static void __device_broadcast_event(struct list_head *head, enum device_event ev) { struct device_user *dep, *tmp; - list_for_each_entry_safe(dep, tmp, &dev->users, list) { + list_for_each_entry_safe(dep, tmp, head, list) { if (!dep->cb) continue; @@ -192,6 +193,12 @@ void device_broadcast_event(struct device *dev, enum device_event ev) } } +void device_broadcast_event(struct device *dev, enum device_event ev) +{ + __device_broadcast_event(&dev->aliases, ev); + __device_broadcast_event(&dev->users, ev); +} + int device_claim(struct device_user *dep) { struct device *dev = dep->dev; @@ -257,6 +264,7 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con D(DEVICE, "Initialize device '%s'\n", dev->ifname); INIT_LIST_HEAD(&dev->users); + INIT_LIST_HEAD(&dev->aliases); dev->type = type; if (!dev->set_state) @@ -378,6 +386,8 @@ void device_set_present(struct device *dev, bool state) void device_add_user(struct device_user *dep, struct device *dev) { + struct list_head *head; + if (dep->dev) device_remove_user(dep); @@ -385,7 +395,13 @@ void device_add_user(struct device_user *dep, struct device *dev) return; dep->dev = dev; - list_add_tail(&dep->list, &dev->users); + + if (dep->alias) + head = &dev->aliases; + else + head = &dev->users; + list_add_tail(&dep->list, head); + if (dep->cb && dev->present) { dep->cb(dep, DEV_EVENT_ADD); if (dev->active) diff --git a/device.h b/device.h index 88a421f..8cedfba 100644 --- a/device.h +++ b/device.h @@ -85,6 +85,7 @@ struct device_user { bool claimed; bool hotplug; + bool alias; struct device *dev; void (*cb)(struct device_user *, enum device_event); @@ -106,6 +107,7 @@ struct device { struct avl_node avl; struct list_head users; + struct list_head aliases; char ifname[IFNAMSIZ + 1]; int ifindex;