浏览代码

[add] filter by hub device bus and port

B Thibault 7 年之前
父节点
当前提交
5e29351a48
共有 1 个文件被更改,包括 24 次插入11 次删除
  1. 24 11
      main.c

+ 24 - 11
main.c

@@ -9,13 +9,15 @@
 #include <sys/types.h>
 #include <dirent.h>
 
+#define LOGFILE "/var/log/pve-hubhotplug.log"
+
 void errExit(const char *err)
 {
     fprintf(stderr, "%s\n", err);
     exit(1);
 }
 
-char *addDevice(const char *bus, const char *dev, const char *vmid)
+char *addDevice(const char *bus, const char *dev, const char *vmid, const char *parentHub, FILE *debug)
 {
     DIR *dirfd = opendir("/sys/bus/usb/devices/");
 
@@ -23,12 +25,23 @@ char *addDevice(const char *bus, const char *dev, const char *vmid)
     {
         struct dirent *dirEntry;
         char *devaddr = NULL;
-        char dirStartWith[32];
-        snprintf(dirStartWith, 32, "%s-", bus);
+        char *hubMatch = NULL;
+
+        if (parentHub)
+        {
+            const int len = strlen(parentHub) +1;
+            hubMatch = malloc(sizeof(*hubMatch) * len);
+            snprintf(hubMatch, len, "%s.", parentHub);
+        }
+        else
+        {
+            hubMatch = malloc(sizeof(*hubMatch) * 32);
+            snprintf(hubMatch, 31, "%s-", bus);
+        }
 
         while (dirEntry = readdir(dirfd))
         {
-            if (strncmp(dirStartWith, dirEntry->d_name, strlen(dirStartWith)))
+            if (strncmp(hubMatch, dirEntry->d_name, strlen(hubMatch)))
                 continue;
             char path[1024];
             snprintf(path, 1024, "/sys/bus/usb/devices/%s/devnum", dirEntry->d_name);
@@ -40,16 +53,16 @@ char *addDevice(const char *bus, const char *dev, const char *vmid)
                 close(fic);
                 if (rd)
                     devnum[rd -1] = 0;
-                printf("{cmp %s vs %s at %s}\n", devnum, dev, dirEntry->d_name);
                 if (!strcmp(devnum, dev))
                 {
-                    devaddr = strdup(dirEntry->d_name +strlen(dirStartWith));
-                    printf("Found device %s\n", dirEntry->d_name);
+                    fprintf(debug, "Found device %s\n", dirEntry->d_name);
+                    devaddr = strdup(dirEntry->d_name +strlen(bus) +1);
                     break;
                 }
             }
         }
         closedir(dirfd);
+        free(hubMatch);
         if (devaddr)
         {
             char *res = malloc(sizeof(*res) * 1024);
@@ -71,6 +84,7 @@ char *delDevice(const char *bus, const char *dev, const char *vmid)
 int main(int ac, char **av)
 {
     const char *vmid = av[1];
+    const char *parentHub = av[1] ? av[2] : NULL;
     const char *devtypeStr = getenv("DEVTYPE");
     const char *actionStr = getenv("ACTION");
     const char *busNumStr = getenv("BUSNUM");
@@ -78,7 +92,7 @@ int main(int ac, char **av)
 
     if (!vmid)
     {
-        fprintf(stderr, "Usage: %s VM-id\n", *av);
+        fprintf(stderr, "Usage: %s VM-id [parentBus-parentPort]\n", *av);
         exit(1);
     }
     if (!devtypeStr)
@@ -99,10 +113,9 @@ int main(int ac, char **av)
         ++devNumStr;
 
     char *action;
-    FILE *f = fopen("/tmp/log", "w+");
-    fprintf(f, "%s:%s\n", busNumStr, devNumStr);
+    FILE *f = fopen(LOGFILE, "a");
     if (!strcmp(actionStr, "add"))
-        action = addDevice(busNumStr, devNumStr, vmid);
+        action = addDevice(busNumStr, devNumStr, vmid, parentHub, f);
     else if (!strcmp(actionStr, "remove"))
         action = delDevice(busNumStr, devNumStr, vmid);
     else