1
0

main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <dirent.h>
  10. #define LOGFILE "/var/log/pve-hubhotplug.log"
  11. void errExit(const char *err)
  12. {
  13. fprintf(stderr, "%s\n", err);
  14. exit(1);
  15. }
  16. char *addDevice(const char *bus, const char *dev, const char *vmid, const char *parentHub, FILE *debug)
  17. {
  18. DIR *dirfd = opendir("/sys/bus/usb/devices/");
  19. if (dirfd)
  20. {
  21. struct dirent *dirEntry;
  22. char *devaddr = NULL;
  23. char *hubMatch = NULL;
  24. if (parentHub)
  25. {
  26. const int len = strlen(parentHub) +1;
  27. hubMatch = malloc(sizeof(*hubMatch) * len);
  28. snprintf(hubMatch, len, "%s.", parentHub);
  29. }
  30. else
  31. {
  32. hubMatch = malloc(sizeof(*hubMatch) * 32);
  33. snprintf(hubMatch, 31, "%s-", bus);
  34. }
  35. while (dirEntry = readdir(dirfd))
  36. {
  37. if (strncmp(hubMatch, dirEntry->d_name, strlen(hubMatch)))
  38. continue;
  39. char path[1024];
  40. snprintf(path, 1024, "/sys/bus/usb/devices/%s/devnum", dirEntry->d_name);
  41. int fic = open(path, O_RDONLY);
  42. if (fic >= 0)
  43. {
  44. char devnum[32];
  45. int rd = read(fic, devnum, 32);
  46. close(fic);
  47. if (rd)
  48. devnum[rd -1] = 0;
  49. if (!strcmp(devnum, dev))
  50. {
  51. fprintf(debug, "Found device %s\n", dirEntry->d_name);
  52. devaddr = strdup(dirEntry->d_name +strlen(bus) +1);
  53. break;
  54. }
  55. }
  56. }
  57. closedir(dirfd);
  58. free(hubMatch);
  59. if (devaddr)
  60. {
  61. char *res = malloc(sizeof(*res) * 1024);
  62. snprintf(res, 1024, "echo device_add usb-host,hostbus=%s,hostport=%s,id=hubhotplug-%s-%s | qm monitor %s\n", bus, devaddr, bus, dev, vmid);
  63. free(devaddr);
  64. return res;
  65. }
  66. }
  67. return NULL;
  68. }
  69. char *delDevice(const char *bus, const char *dev, const char *vmid)
  70. {
  71. char *res = malloc(sizeof(*res) * 1024);
  72. snprintf(res, 1024, "echo device_del hubhotplug-%s-%s | qm monitor %s\n", bus, dev, vmid);
  73. return res;
  74. }
  75. int main(int ac, char **av)
  76. {
  77. const char *vmid = av[1];
  78. const char *parentHub = av[1] ? av[2] : NULL;
  79. const char *devtypeStr = getenv("DEVTYPE");
  80. const char *actionStr = getenv("ACTION");
  81. const char *busNumStr = getenv("BUSNUM");
  82. const char *devNumStr = getenv("DEVNUM");
  83. if (!vmid)
  84. {
  85. fprintf(stderr, "Usage: %s VM-id [parentBus-parentPort]\n", *av);
  86. exit(1);
  87. }
  88. if (!devtypeStr)
  89. errExit("Missing udev DEVTYPE");
  90. if (!actionStr)
  91. errExit("Missing udev ACTION");
  92. if (!busNumStr)
  93. errExit("Missing udev BUSNUM");
  94. if (!devNumStr)
  95. errExit("Missing udev DEVNUM");
  96. if (strcmp(devtypeStr, "usb_device"))
  97. errExit("Ignoring, devtype not matched");
  98. while (*busNumStr == '0')
  99. ++busNumStr;
  100. while (*devNumStr == '0')
  101. ++devNumStr;
  102. char *action;
  103. FILE *f = fopen(LOGFILE, "a");
  104. if (!strcmp(actionStr, "add"))
  105. action = addDevice(busNumStr, devNumStr, vmid, parentHub, f);
  106. else if (!strcmp(actionStr, "remove"))
  107. action = delDevice(busNumStr, devNumStr, vmid);
  108. else
  109. errExit("Invalid ACTION");
  110. if (action)
  111. {
  112. fprintf(f, ">>> %s\n", action);
  113. system(action);
  114. free(action);
  115. }
  116. fclose(f);
  117. }