static int myInit(void) { size_t i; for (i = 0; i < nhostdevs; i++) { virDomainHostdevSubsys subsys; hostdevs[i] = virDomainHostdevDefAlloc(); if (!hostdevs[i]) goto cleanup; hostdevs[i]->mode = VIR_DOMAIN_HOSTDEV_MODE_SUBSYS; subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI; subsys.u.pci.addr.domain = 0; subsys.u.pci.addr.bus = 0; subsys.u.pci.addr.slot = i + 1; subsys.u.pci.addr.function = 0; subsys.u.pci.backend = VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM; hostdevs[i]->source.subsys = subsys; } for (i = 0; i < nhostdevs; i++) { if (!(dev[i] = virPCIDeviceNew(0, 0, i + 1, 0))) goto cleanup; virPCIDeviceSetStubDriver(dev[i], VIR_PCI_STUB_DRIVER_KVM); } if (VIR_ALLOC(mgr) < 0) goto cleanup; if ((mgr->activePCIHostdevs = virPCIDeviceListNew()) == NULL) goto cleanup; if ((mgr->activeUSBHostdevs = virUSBDeviceListNew()) == NULL) goto cleanup; if ((mgr->inactivePCIHostdevs = virPCIDeviceListNew()) == NULL) goto cleanup; if ((mgr->activeSCSIHostdevs = virSCSIDeviceListNew()) == NULL) goto cleanup; if (VIR_STRDUP(mgr->stateDir, TEST_STATE_DIR) < 0) goto cleanup; if (virFileMakePath(mgr->stateDir) < 0) goto cleanup; return 0; cleanup: myCleanup(); return -1; }
static virDomainHostdevDefPtr lxcCreateHostdevDef(int mode, int type, const char *data) { virDomainHostdevDefPtr hostdev = virDomainHostdevDefAlloc(NULL); if (!hostdev) return NULL; hostdev->mode = mode; hostdev->source.caps.type = type; if (type == VIR_DOMAIN_HOSTDEV_CAPS_TYPE_NET && VIR_STRDUP(hostdev->source.caps.u.net.iface, data) < 0) { virDomainHostdevDefFree(hostdev); hostdev = NULL; } return hostdev; }
static int xenParseXLUSB(virConfPtr conf, virDomainDefPtr def) { virConfValuePtr list = virConfGetValue(conf, "usbdev"); virDomainHostdevDefPtr hostdev = NULL; if (list && list->type == VIR_CONF_LIST) { list = list->list; while (list) { char bus[3]; char device[3]; char *key; int busNum; int devNum; bus[0] = device[0] = '\0'; if ((list->type != VIR_CONF_STRING) || (list->str == NULL)) goto skipusb; /* usbdev=['hostbus=1,hostaddr=3'] */ key = list->str; while (key) { char *data; char *nextkey = strchr(key, ','); if (!(data = strchr(key, '='))) goto skipusb; data++; if (STRPREFIX(key, "hostbus=")) { int len = nextkey ? (nextkey - data) : sizeof(bus) - 1; if (virStrncpy(bus, data, len, sizeof(bus)) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("bus %s too big for destination"), data); goto skipusb; } } else if (STRPREFIX(key, "hostaddr=")) { int len = nextkey ? (nextkey - data) : sizeof(device) - 1; if (virStrncpy(device, data, len, sizeof(device)) == NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("device %s too big for destination"), data); goto skipusb; } } while (nextkey && (nextkey[0] == ',' || nextkey[0] == ' ' || nextkey[0] == '\t')) nextkey++; key = nextkey; } if (virStrToLong_i(bus, NULL, 16, &busNum) < 0) goto skipusb; if (virStrToLong_i(device, NULL, 16, &devNum) < 0) goto skipusb; if (!(hostdev = virDomainHostdevDefAlloc(NULL))) return -1; hostdev->managed = false; hostdev->source.subsys.type = VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB; hostdev->source.subsys.u.usb.bus = busNum; hostdev->source.subsys.u.usb.device = devNum; if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0) { virDomainHostdevDefFree(hostdev); return -1; } skipusb: list = list->next; } } return 0; }