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