Просмотр исходного кода

ncd: net.watch.interfaces: port to NCDUdevManager

ambrop7 15 лет назад
Родитель
Сommit
127bb197f1
1 измененных файлов с 227 добавлено и 117 удалено
  1. 227 117
      ncd/modules/net_watch_interfaces.c

+ 227 - 117
ncd/modules/net_watch_interfaces.c

@@ -38,111 +38,246 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
-#include <ctype.h>
 
-#include <ncd/NCDInterfaceMonitor.h>
+#include <misc/debug.h>
+#include <misc/offset.h>
+#include <misc/parse_number.h>
+#include <structure/LinkedList1.h>
+#include <udevmonitor/NCDUdevManager.h>
 #include <ncd/NCDModule.h>
+#include <ncd/modules/event_template.h>
 
 #include <generated/blog_channel_ncd_net_watch_interfaces.h>
 
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 
+struct device {
+    char *ifname;
+    char *devpath;
+    uintmax_t ifindex;
+    LinkedList1Node devices_list_node;
+};
+
 struct instance {
     NCDModuleInst *i;
-    NCDInterfaceMonitor monitor;
-    FILE *net_file;
-    char line_buf[256];
-    int processing;
-    const char *processing_type;
-    char processing_name[256];
+    NCDUdevClient client;
+    LinkedList1 devices_list;
+    event_template templ;
 };
 
 struct nextevent_instance {
     NCDModuleInst *i;
 };
 
-static void next_file_event (struct instance *o)
+static void templ_func_free (struct instance *o);
+
+struct device * find_device_by_ifname (struct instance *o, const char *ifname)
 {
-    ASSERT(!o->processing)
-    ASSERT(o->net_file)
-    
-    char *name;
-    
-    while (1) {
-        if (!fgets(o->line_buf, sizeof(o->line_buf), o->net_file)) {
-            // close file
-            fclose(o->net_file);
-            
-            // set no net file
-            o->net_file = NULL;
-            
-            // start processing monitor events
-            NCDInterfaceMonitor_Continue(&o->monitor);
-            
-            return;
+    LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
+    while (list_node) {
+        struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
+        if (!strcmp(device->ifname, ifname)) {
+            return device;
         }
-        
-        // parse line to get interface name
-        char *c = o->line_buf;
-        while (*c && isspace(*c)) {
-            c++;
-        }
-        name = c;
-        while (*c && *c != ':') {
-            c++;
-        }
-        if (*c != ':') {
-            continue;
+        list_node = LinkedList1Node_Next(list_node);
+    }
+    
+    return NULL;
+}
+
+struct device * find_device_by_devpath (struct instance *o, const char *devpath)
+{
+    LinkedList1Node *list_node = LinkedList1_GetFirst(&o->devices_list);
+    while (list_node) {
+        struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
+        if (!strcmp(device->devpath, devpath)) {
+            return device;
         }
-        *c = '\0';
-        
-        break;
+        list_node = LinkedList1Node_Next(list_node);
     }
     
-    // set event
-    o->processing_type = "added";
-    snprintf(o->processing_name, sizeof(o->processing_name), "%s", name);
-    o->processing = 1;
+    return NULL;
+}
+
+static void free_device (struct instance *o, struct device *device)
+{
+    // remove from devices list
+    LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
     
-    // signal up
-    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    // free devpath
+    free(device->devpath);
+    
+    // free ifname
+    free(device->ifname);
+    
+    // free structure
+    free(device);
 }
 
-static void next_event (struct instance *o)
+static int queue_event (struct instance *o, int added, const char *ifname)
 {
-    ASSERT(o->processing)
+    // init map
+    BStringMap map;
+    BStringMap_Init(&map);
     
-    // set not processing
-    o->processing = 0;
+    // set type
+    if (!BStringMap_Set(&map, "event_type", (added ? "added" : "removed"))) {
+        ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
+        goto fail1;
+    }
     
-    // signal down
-    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
+    // set ifname
+    if (!BStringMap_Set(&map, "devname", ifname)) {
+        ModuleLog(o->i, BLOG_ERROR, "BStringMap_Set failed");
+        goto fail1;
+    }
+    
+    // pass event to template
+    int was_empty;
+    if (!event_template_queue(&o->templ, map, &was_empty)) {
+        ModuleLog(o->i, BLOG_ERROR, "event_template_queue failed");
+        goto fail1;
+    }
     
-    if (o->net_file) {
-        next_file_event(o);
-        return;
-    } else {
-        // continue processing monitor events
-        NCDInterfaceMonitor_Continue(&o->monitor);
+    // if event queue was empty, stop receiving udev events
+    if (was_empty) {
+        NCDUdevClient_Pause(&o->client);
     }
+    
+    return 1;
+    
+fail1:
+    BStringMap_Free(&map);
+    ModuleLog(o->i, BLOG_ERROR, "failed to queue event");
+    return 0;
 }
 
-static void monitor_handler (struct instance *o, const char *ifname, int if_flags)
+static void add_device (struct instance *o, const char *ifname, const char *devpath, uintmax_t ifindex)
 {
-    ASSERT(!o->processing)
-    ASSERT(!o->net_file)
+    ASSERT(!find_device_by_ifname(o, ifname))
+    ASSERT(!find_device_by_devpath(o, devpath))
     
-    // pause monitor
-    NCDInterfaceMonitor_Pause(&o->monitor);
+    // allocate structure
+    struct device *device = malloc(sizeof(*device));
+    if (!device) {
+        ModuleLog(o->i, BLOG_ERROR, "malloc failed");
+        goto fail0;
+    }
     
-    // set event
-    o->processing_type = ((if_flags & NCDIFCONFIG_FLAG_EXISTS) ? "added" : "removed");
-    snprintf(o->processing_name, sizeof(o->processing_name), "%s", ifname);
-    o->processing = 1;
+    // init ifname
+    if (!(device->ifname = strdup(ifname))) {
+        ModuleLog(o->i, BLOG_ERROR, "strdup failed");
+        goto fail1;
+    }
     
-    // signal up
-    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+    // init devpath
+    if (!(device->devpath = strdup(devpath))) {
+        ModuleLog(o->i, BLOG_ERROR, "strdup failed");
+        goto fail2;
+    }
+    
+    // set ifindex
+    device->ifindex = ifindex;
+    
+    // insert to devices list
+    LinkedList1_Append(&o->devices_list, &device->devices_list_node);
+    
+    // queue event
+    if (!queue_event(o, 1, device->ifname)) {
+        goto fail3;
+    }
+    
+    return;
+    
+fail3:
+    LinkedList1_Remove(&o->devices_list, &device->devices_list_node);
+    free(device->devpath);
+fail2:
+    free(device->ifname);
+fail1:
+    free(device);
+fail0:
+    ModuleLog(o->i, BLOG_ERROR, "failed to add device %s", ifname);
+}
+
+static int remove_device (struct instance *o, struct device *device)
+{
+    if (!queue_event(o, 0, device->ifname)) {
+        goto fail0;
+    }
+    
+    free_device(o, device);
+    
+    return 1;
+    
+fail0:
+    ModuleLog(o->i, BLOG_ERROR, "failed to remove device %s", device->ifname);
+    return 0;
+}
+
+static void next_event (struct instance *o)
+{
+    event_template_assert_enabled(&o->templ);
+    
+    // order template to finish the current event
+    int is_empty;
+    event_template_next(&o->templ, &is_empty);
+    
+    // if template has no events, continue udev events
+    if (is_empty) {
+        NCDUdevClient_Continue(&o->client);
+    }
+}
+
+static void client_handler (struct instance *o, char *devpath, int have_map, BStringMap map)
+{
+    // lookup existing device with this devpath
+    struct device *ex_device = find_device_by_devpath(o, devpath);
+    // lookup cache entry
+    const BStringMap *cache_map = NCDUdevManager_Query(o->i->umanager, devpath);
+    
+    if (!cache_map) {
+        if (ex_device) {
+            remove_device(o, ex_device);
+        }
+        goto out;
+    }
+    
+    const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
+    const char *interface = BStringMap_Get(cache_map, "INTERFACE");
+    const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
+    
+    uintmax_t ifindex;
+    if (!(subsystem && !strcmp(subsystem, "net") && interface && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
+        if (ex_device) {
+            remove_device(o, ex_device);
+        }
+        goto out;
+    }
+    
+    if (ex_device && (strcmp(ex_device->ifname, interface) || ex_device->ifindex != ifindex)) {
+        if (!remove_device(o, ex_device)) {
+            goto out;
+        }
+        ex_device = NULL;
+    }
+    
+    if (!ex_device) {
+        struct device *ex_ifname_device = find_device_by_ifname(o, interface);
+        if (ex_ifname_device) {
+            if (!remove_device(o, ex_ifname_device)) {
+                goto out;
+            }
+        }
+        
+        add_device(o, interface, devpath, ifindex);
+    }
+    
+out:
+    free(devpath);
+    if (have_map) {
+        BStringMap_Free(&map);
+    }
 }
 
 static void func_new (NCDModuleInst *i)
@@ -164,27 +299,15 @@ static void func_new (NCDModuleInst *i)
         goto fail1;
     }
     
-    // init monitor
-    if (!NCDInterfaceMonitor_Init(&o->monitor, o->i->reactor, (NCDInterfaceMonitor_handler)monitor_handler, o)) {
-        ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
-        goto fail1;
-    }
-    NCDInterfaceMonitor_Pause(&o->monitor);
+    // init client
+    NCDUdevClient_Init(&o->client, o->i->umanager, o, (NCDUdevClient_handler)client_handler);
     
-    // open /proc/net/dev
-    if (!(o->net_file = fopen("/proc/net/dev", "r"))) {
-        ModuleLog(o->i, BLOG_ERROR, "fopen(/proc/net/dev) failed");
-        goto fail2;
-    }
+    // init devices list
+    LinkedList1_Init(&o->devices_list);
     
-    // set not processing
-    o->processing = 0;
-    
-    next_file_event(o);
+    event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, o, (event_template_func_free)templ_func_free);
     return;
     
-fail2:
-    NCDInterfaceMonitor_Free(&o->monitor);
 fail1:
     free(o);
 fail0:
@@ -192,18 +315,19 @@ fail0:
     NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
 }
 
-static void func_die (void *vo)
+static void templ_func_free (struct instance *o)
 {
-    struct instance *o = vo;
     NCDModuleInst *i = o->i;
     
-    // close /proc/net/dev
-    if (o->net_file) {
-        fclose(o->net_file);
+    // free devices
+    LinkedList1Node *list_node;
+    while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
+        struct device *device = UPPER_OBJECT(list_node, struct device, devices_list_node);
+        free_device(o, device);
     }
     
-    // free monitor
-    NCDInterfaceMonitor_Free(&o->monitor);
+    // free client
+    NCDUdevClient_Free(&o->client);
     
     // free instance
     free(o);
@@ -211,30 +335,16 @@ static void func_die (void *vo)
     NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
 }
 
+static void func_die (void *vo)
+{
+    struct instance *o = vo;
+    event_template_die(&o->templ);
+}
+
 static int func_getvar (void *vo, const char *name, NCDValue *out)
 {
     struct instance *o = vo;
-    ASSERT(o->processing)
-    
-    if (!strcmp(name, "event_type")) {
-        if (!NCDValue_InitString(out, o->processing_type)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
-            return 0;
-        }
-        
-        return 1;
-    }
-    
-    if (!strcmp(name, "devname")) {
-        if (!NCDValue_InitString(out, o->processing_name)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
-            return 0;
-        }
-        
-        return 1;
-    }
-    
-    return 0;
+    return event_template_getvar(&o->templ, name, out);
 }
 
 static void nextevent_func_new (NCDModuleInst *i)
@@ -258,7 +368,7 @@ static void nextevent_func_new (NCDModuleInst *i)
     
     // get method object
     struct instance *mo = i->method_object->inst_user;
-    ASSERT(mo->processing)
+    event_template_assert_enabled(&mo->templ);
     
     // signal up.
     // Do it before finishing the event so our process does not advance any further if