discover: allow separate lifetimes for network interfaces and discover devices
authorJeremy Kerr <jk@ozlabs.org>
Mon, 21 Jul 2014 07:51:58 +0000 (15:51 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 21 Jul 2014 09:20:08 +0000 (17:20 +0800)
We want the network code's network interfaces to (potentially) persist
remove events. For example, discover devices may be removed by a user
event (this happens during a udhcpc deconfig). In this case, we want the
boot options to be removed, but the struct interface needs to stay
present.

This change adds network_(un)_register_device functions, to allow the
device handler to detach from and attach to interfaces.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/device-handler.c
discover/network.c
discover/network.h

index c21eb286d2bd8b47d361fad1c537c883e50fdbbc..7f7081df5f6fc27abd033788d2202c5cca1988c2 100644 (file)
@@ -344,6 +344,11 @@ void device_handler_remove(struct device_handler *handler,
                talloc_free(opt);
        }
 
+       /* if this is a network device, we have to unregister it from the
+        * network code */
+       if (device->device->type == DEVICE_TYPE_NETWORK)
+               network_unregister_device(handler->network, device);
+
        handler->n_devices--;
        memmove(&handler->devices[i], &handler->devices[i + 1],
                (handler->n_devices - i) * sizeof(handler->devices[0]));
@@ -646,6 +651,8 @@ void device_handler_add_device(struct device_handler *handler,
                                struct discover_device *, handler->n_devices);
        handler->devices[handler->n_devices - 1] = device;
 
+       if (device->device->type == DEVICE_TYPE_NETWORK)
+               network_register_device(handler->network, device);
 }
 
 /* Start discovery on a hotplugged device. The device will be in our devices
index ad17a417e4f48d7fed992d48291807b71f1a1221..c0bd2af01e84a0321be6f511fdfbc64e6046b07f 100644 (file)
@@ -97,6 +97,18 @@ static struct interface *find_interface_by_ifindex(struct network *network,
        return NULL;
 }
 
+static struct interface *find_interface_by_name(struct network *network,
+               const char *name)
+{
+       struct interface *interface;
+
+       list_for_each_entry(&network->interfaces, interface, list)
+               if (!strcmp(interface->name, name))
+                       return interface;
+
+       return NULL;
+}
+
 static int network_init_netlink(struct network *network)
 {
        struct sockaddr_nl addr;
@@ -184,11 +196,38 @@ static void add_interface(struct network *network,
 static void remove_interface(struct network *network,
                struct interface *interface)
 {
-       device_handler_remove(network->handler, interface->dev);
+       if (interface->dev)
+               device_handler_remove(network->handler, interface->dev);
        list_remove(&interface->list);
        talloc_free(interface);
 }
 
+void network_register_device(struct network *network,
+               struct discover_device *dev)
+{
+       struct interface *iface;
+
+       iface = find_interface_by_name(network, dev->device->id);
+       if (!iface)
+               return;
+
+       iface->dev = dev;
+       dev->uuid = mac_bytes_to_string(iface->dev, iface->hwaddr,
+                       sizeof(iface->hwaddr));
+}
+
+void network_unregister_device(struct network *network,
+               struct discover_device *dev)
+{
+       struct interface *iface;
+
+       iface = find_interface_by_name(network, dev->device->id);
+       if (!iface)
+               return;
+
+       iface->dev = NULL;
+}
+
 static int interface_change(struct interface *interface, bool up)
 {
        const char *statestr = up ? "up" : "down";
index 912174f4392badc0e03bd44ffdc65a9571ad97ea..bfd1ab12ecea2a013080b85856b960bb715e2723 100644 (file)
@@ -3,11 +3,17 @@
 
 struct network;
 struct device_handler;
+struct discover_device;
 struct waitset;
 
 struct network *network_init(struct device_handler *handler,
                struct waitset *waitset, bool dry_run);
 int network_shutdown(struct network *network);
 
+void network_register_device(struct network *network,
+               struct discover_device *dev);
+void network_unregister_device(struct network *network,
+               struct discover_device *dev);
+
 #endif /* NETWORK_H */