+static void
+handle_hotplug_msg(char *data, int size)
+{
+ const char *subsystem = NULL, *interface = NULL;
+ char *cur, *end, *sep;
+ struct device *dev;
+ int skip;
+ bool add;
+
+ if (!strncmp(data, "add@", 4))
+ add = true;
+ else if (!strncmp(data, "remove@", 7))
+ add = false;
+ else
+ return;
+
+ skip = strlen(data) + 1;
+ end = data + size;
+
+ for (cur = data + skip; cur < end; cur += skip) {
+ skip = strlen(cur) + 1;
+
+ sep = strchr(cur, '=');
+ if (!sep)
+ continue;
+
+ *sep = 0;
+ if (!strcmp(cur, "INTERFACE"))
+ interface = sep + 1;
+ else if (!strcmp(cur, "SUBSYSTEM")) {
+ subsystem = sep + 1;
+ if (strcmp(subsystem, "net") != 0)
+ return;
+ }
+ if (subsystem && interface)
+ goto found;
+ }
+ return;
+
+found:
+ dev = device_get(interface, false);
+ if (!dev)
+ return;
+
+ if (dev->type != &simple_device_type)
+ return;
+
+ device_set_present(dev, add);
+}
+
+static void
+handle_hotplug_event(struct uloop_fd *u, unsigned int events)
+{
+ struct event_socket *ev = container_of(u, struct event_socket, uloop);
+ struct sockaddr_nl nla;
+ unsigned char *buf = NULL;
+ int size;
+
+ while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
+ if (nla.nl_pid == 0)
+ handle_hotplug_msg((char *) buf, size);
+
+ free(buf);
+ }
+}
+