提交 5073aa99 编写于 作者: C Cole Robinson

Implement path lookup for USB by vendor:product

Based off how QEMU does it, look through /sys/bus/usb/devices/* for
matching vendor:product info, and if found, use info from the surrounding
files to build the device's /dev/bus/usb path.

This fixes USB device assignment by vendor:product when running qemu
as non-root (well, it should, but for some reason I couldn't reproduce
the failure people are seeing in [1], but it appears to work properly)

[1] https://bugzilla.redhat.com/show_bug.cgi?id=542450
上级 438fa793
...@@ -55,6 +55,7 @@ src/uml/uml_conf.c ...@@ -55,6 +55,7 @@ src/uml/uml_conf.c
src/uml/uml_driver.c src/uml/uml_driver.c
src/util/bridge.c src/util/bridge.c
src/util/conf.c src/util/conf.c
src/util/hostusb.c
src/util/json.c src/util/json.c
src/util/logging.c src/util/logging.c
src/util/pci.c src/util/pci.c
......
...@@ -2105,14 +2105,11 @@ static int qemuDomainSetHostdevUSBOwnership(virConnectPtr conn, ...@@ -2105,14 +2105,11 @@ static int qemuDomainSetHostdevUSBOwnership(virConnectPtr conn,
struct qemuFileOwner owner = { uid, gid }; struct qemuFileOwner owner = { uid, gid };
int ret = -1; int ret = -1;
/* XXX what todo for USB devs assigned based on product/vendor ? Doom :-( */
if (!def->source.subsys.u.usb.bus ||
!def->source.subsys.u.usb.device)
return 0;
usbDevice *dev = usbGetDevice(conn, usbDevice *dev = usbGetDevice(conn,
def->source.subsys.u.usb.bus, def->source.subsys.u.usb.bus,
def->source.subsys.u.usb.device); def->source.subsys.u.usb.device,
def->source.subsys.u.usb.vendor,
def->source.subsys.u.usb.product);
if (!dev) if (!dev)
goto cleanup; goto cleanup;
......
...@@ -481,20 +481,17 @@ SELinuxSetSecurityHostdevLabel(virConnectPtr conn, ...@@ -481,20 +481,17 @@ SELinuxSetSecurityHostdevLabel(virConnectPtr conn,
switch (dev->source.subsys.type) { switch (dev->source.subsys.type) {
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
if (dev->source.subsys.u.usb.bus && dev->source.subsys.u.usb.device) { usbDevice *usb = usbGetDevice(conn,
usbDevice *usb = usbGetDevice(conn, dev->source.subsys.u.usb.bus,
dev->source.subsys.u.usb.bus, dev->source.subsys.u.usb.device,
dev->source.subsys.u.usb.device); dev->source.subsys.u.usb.vendor,
dev->source.subsys.u.usb.product);
if (!usb) if (!usb)
goto done; goto done;
ret = usbDeviceFileIterate(conn, usb, SELinuxSetSecurityUSBLabel, vm); ret = usbDeviceFileIterate(conn, usb, SELinuxSetSecurityUSBLabel, vm);
usbFreeDevice(conn, usb); usbFreeDevice(conn, usb);
} else {
/* XXX deal with product/vendor better */
ret = 0;
}
break; break;
} }
...@@ -556,7 +553,9 @@ SELinuxRestoreSecurityHostdevLabel(virConnectPtr conn, ...@@ -556,7 +553,9 @@ SELinuxRestoreSecurityHostdevLabel(virConnectPtr conn,
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
usbDevice *usb = usbGetDevice(conn, usbDevice *usb = usbGetDevice(conn,
dev->source.subsys.u.usb.bus, dev->source.subsys.u.usb.bus,
dev->source.subsys.u.usb.device); dev->source.subsys.u.usb.device,
dev->source.subsys.u.usb.vendor,
dev->source.subsys.u.usb.product);
if (!usb) if (!usb)
goto done; goto done;
......
...@@ -836,24 +836,22 @@ get_files(vahControl * ctl) ...@@ -836,24 +836,22 @@ get_files(vahControl * ctl)
virDomainHostdevDefPtr dev = ctl->def->hostdevs[i]; virDomainHostdevDefPtr dev = ctl->def->hostdevs[i];
switch (dev->source.subsys.type) { switch (dev->source.subsys.type) {
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: {
if (dev->source.subsys.u.usb.bus && usbDevice *usb = usbGetDevice(NULL,
dev->source.subsys.u.usb.device) { dev->source.subsys.u.usb.bus,
usbDevice *usb = usbGetDevice(NULL, dev->source.subsys.u.usb.device,
dev->source.subsys.u.usb.bus, dev->source.subsys.u.usb.vendor,
dev->source.subsys.u.usb.device); dev->source.subsys.u.usb.product);
if (usb == NULL)
continue; if (usb == NULL)
rc = usbDeviceFileIterate(NULL, usb, continue;
file_iterate_cb, &buf);
usbFreeDevice(NULL, usb); rc = usbDeviceFileIterate(NULL, usb,
if (rc != 0) file_iterate_cb, &buf);
goto clean; usbFreeDevice(NULL, usb);
else { if (rc != 0)
/* TODO: deal with product/vendor better */ goto clean;
rc = 0;
}
}
break; break;
}
} }
/* TODO: update so files in /sys are readonly /* TODO: update so files in /sys are readonly
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: { case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: {
......
...@@ -37,9 +37,10 @@ ...@@ -37,9 +37,10 @@
#include "util.h" #include "util.h"
#include "virterror_internal.h" #include "virterror_internal.h"
#define USB_SYSFS "/sys/bus/usb"
#define USB_DEVFS "/dev/bus/usb/" #define USB_DEVFS "/dev/bus/usb/"
#define USB_ID_LEN 10 /* "XXXX XXXX" */ #define USB_ID_LEN 10 /* "1234 5678" */
#define USB_ADDR_LEN 8 /* "XXX:XXX" */ #define USB_ADDR_LEN 8 /* "123:456" */
struct _usbDevice { struct _usbDevice {
unsigned bus; unsigned bus;
...@@ -57,11 +58,108 @@ struct _usbDevice { ...@@ -57,11 +58,108 @@ struct _usbDevice {
virReportErrorHelper(conn, VIR_FROM_NONE, code, __FILE__, \ virReportErrorHelper(conn, VIR_FROM_NONE, code, __FILE__, \
__FUNCTION__, __LINE__, fmt) __FUNCTION__, __LINE__, fmt)
static int usbSysReadFile(virConnectPtr conn,
const char *f_name, const char *d_name,
int base, unsigned *value)
{
int ret = -1, tmp;
char *buf = NULL;
char *filename = NULL;
char *ignore = NULL;
tmp = virAsprintf(&filename, USB_SYSFS "/devices/%s/%s", d_name, f_name);
if (tmp < 0) {
virReportOOMError(conn);
goto error;
}
if (virFileReadAll(filename, 1024, &buf) < 0)
goto error;
if (virStrToLong_ui(buf, &ignore, base, value) < 0) {
usbReportError(conn, VIR_ERR_INTERNAL_ERROR,
_("Could not parse usb file %s"), filename);
goto error;
}
ret = 0;
error:
VIR_FREE(filename);
VIR_FREE(buf);
return ret;
}
static int usbFindBusByVendor(virConnectPtr conn,
unsigned vendor, unsigned product,
unsigned *bus, unsigned *devno)
{
DIR *dir = NULL;
int ret = -1, found = 0;
char *ignore = NULL;
struct dirent *de;
dir = opendir(USB_SYSFS "/devices");
if (!dir) {
virReportSystemError(conn, errno,
_("Could not open directory %s"),
USB_SYSFS "/devices");
goto error;
}
while ((de = readdir(dir))) {
unsigned found_prod, found_vend;
if (de->d_name[0] == '.' || strchr(de->d_name, ':'))
continue;
if (usbSysReadFile(conn, "idVendor", de->d_name,
16, &found_vend) < 0)
goto error;
if (usbSysReadFile(conn, "idProduct", de->d_name,
16, &found_prod) < 0)
goto error;
if (found_prod == product && found_vend == vendor) {
/* Lookup bus.addr info */
char *tmpstr = de->d_name;
unsigned found_bus, found_addr;
if (STREQ(de->d_name, "usb"))
tmpstr += 3;
if (virStrToLong_ui(tmpstr, &ignore, 10, &found_bus) < 0) {
usbReportError(conn, VIR_ERR_INTERNAL_ERROR,
_("Failed to parse dir name '%s'"),
de->d_name);
goto error;
}
if (usbSysReadFile(conn, "devnum", de->d_name,
10, &found_addr) < 0)
goto error;
*bus = found_bus;
*devno = found_addr;
found = 1;
break;
}
}
if (!found)
usbReportError(conn, VIR_ERR_INTERNAL_ERROR,
_("Did not find USB device %x:%x"), vendor, product);
else
ret = 0;
error:
return ret;
}
usbDevice * usbDevice *
usbGetDevice(virConnectPtr conn, usbGetDevice(virConnectPtr conn,
unsigned bus, unsigned bus,
unsigned devno) unsigned devno,
unsigned vendor,
unsigned product)
{ {
usbDevice *dev; usbDevice *dev;
...@@ -70,6 +168,12 @@ usbGetDevice(virConnectPtr conn, ...@@ -70,6 +168,12 @@ usbGetDevice(virConnectPtr conn,
return NULL; return NULL;
} }
if (vendor) {
/* Look up bus.dev by vendor:product */
if (usbFindBusByVendor(conn, vendor, product, &bus, &devno) < 0)
return NULL;
}
dev->bus = bus; dev->bus = bus;
dev->dev = devno; dev->dev = devno;
......
...@@ -28,7 +28,9 @@ typedef struct _usbDevice usbDevice; ...@@ -28,7 +28,9 @@ typedef struct _usbDevice usbDevice;
usbDevice *usbGetDevice (virConnectPtr conn, usbDevice *usbGetDevice (virConnectPtr conn,
unsigned bus, unsigned bus,
unsigned devno); unsigned devno,
unsigned vendor,
unsigned product);
void usbFreeDevice (virConnectPtr conn, void usbFreeDevice (virConnectPtr conn,
usbDevice *dev); usbDevice *dev);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册