util: pci: use VIR_AUTOFREE instead of VIR_FREE for scalar types

By making use of GNU C's cleanup attribute handled by the
VIR_AUTOFREE macro for declaring scalar variables, majority
of the VIR_FREE calls can be dropped, which in turn leads to
getting rid of most of our cleanup sections.

Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Sukrit Bhatnagar 2018-07-24 21:22:21 +05:30 committed by Erik Skultety
parent 3dee174b4d
commit 9ea90206ef

View File

@ -253,7 +253,7 @@ int
virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **name) virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **name)
{ {
int ret = -1; int ret = -1;
char *drvlink = NULL; VIR_AUTOFREE(char *) drvlink = NULL;
*path = *name = NULL; *path = *name = NULL;
/* drvlink = "/sys/bus/pci/dddd:bb:ss.ff/driver" */ /* drvlink = "/sys/bus/pci/dddd:bb:ss.ff/driver" */
@ -285,7 +285,6 @@ virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **name)
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(drvlink);
if (ret < 0) { if (ret < 0) {
VIR_FREE(*path); VIR_FREE(*path);
VIR_FREE(*name); VIR_FREE(*name);
@ -375,32 +374,27 @@ virPCIDeviceRead32(virPCIDevicePtr dev, int cfgfd, unsigned int pos)
static int static int
virPCIDeviceReadClass(virPCIDevicePtr dev, uint16_t *device_class) virPCIDeviceReadClass(virPCIDevicePtr dev, uint16_t *device_class)
{ {
char *path = NULL; VIR_AUTOFREE(char *) path = NULL;
char *id_str = NULL; VIR_AUTOFREE(char *) id_str = NULL;
int ret = -1;
unsigned int value; unsigned int value;
if (!(path = virPCIFile(dev->name, "class"))) if (!(path = virPCIFile(dev->name, "class")))
return ret; return -1;
/* class string is '0xNNNNNN\n' ... i.e. 9 bytes */ /* class string is '0xNNNNNN\n' ... i.e. 9 bytes */
if (virFileReadAll(path, 9, &id_str) < 0) if (virFileReadAll(path, 9, &id_str) < 0)
goto cleanup; return -1;
id_str[8] = '\0'; id_str[8] = '\0';
if (virStrToLong_ui(id_str, NULL, 16, &value) < 0) { if (virStrToLong_ui(id_str, NULL, 16, &value) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unusual value in %s/devices/%s/class: %s"), _("Unusual value in %s/devices/%s/class: %s"),
PCI_SYSFS, dev->name, id_str); PCI_SYSFS, dev->name, id_str);
goto cleanup; return -1;
} }
*device_class = (value >> 8) & 0xFFFF; *device_class = (value >> 8) & 0xFFFF;
ret = 0; return 0;
cleanup:
VIR_FREE(id_str);
VIR_FREE(path);
return ret;
} }
static int static int
@ -574,7 +568,7 @@ virPCIDeviceDetectFunctionLevelReset(virPCIDevicePtr dev, int cfgfd)
{ {
uint32_t caps; uint32_t caps;
uint8_t pos; uint8_t pos;
char *path; VIR_AUTOFREE(char *) path = NULL;
int found; int found;
/* The PCIe Function Level Reset capability allows /* The PCIe Function Level Reset capability allows
@ -614,7 +608,6 @@ virPCIDeviceDetectFunctionLevelReset(virPCIDevicePtr dev, int cfgfd)
return -1; return -1;
found = virFileExists(path); found = virFileExists(path);
VIR_FREE(path);
if (found) { if (found) {
VIR_DEBUG("%s %s: buggy device didn't advertise FLR, but is a VF; forcing flr on", VIR_DEBUG("%s %s: buggy device didn't advertise FLR, but is a VF; forcing flr on",
dev->id, dev->name); dev->id, dev->name);
@ -926,8 +919,8 @@ virPCIDeviceReset(virPCIDevicePtr dev,
virPCIDeviceList *activeDevs, virPCIDeviceList *activeDevs,
virPCIDeviceList *inactiveDevs) virPCIDeviceList *inactiveDevs)
{ {
char *drvPath = NULL; VIR_AUTOFREE(char *) drvPath = NULL;
char *drvName = NULL; VIR_AUTOFREE(char *) drvName = NULL;
int ret = -1; int ret = -1;
int fd = -1; int fd = -1;
int hdrType = -1; int hdrType = -1;
@ -1000,8 +993,6 @@ virPCIDeviceReset(virPCIDevicePtr dev,
} }
cleanup: cleanup:
VIR_FREE(drvPath);
VIR_FREE(drvName);
virPCIDeviceConfigClose(dev, fd); virPCIDeviceConfigClose(dev, fd);
return ret; return ret;
} }
@ -1011,7 +1002,7 @@ static int
virPCIProbeStubDriver(virPCIStubDriver driver) virPCIProbeStubDriver(virPCIStubDriver driver)
{ {
const char *drvname = NULL; const char *drvname = NULL;
char *drvpath = NULL; VIR_AUTOFREE(char *) drvpath = NULL;
bool probed = false; bool probed = false;
if (driver == VIR_PCI_STUB_DRIVER_NONE || if (driver == VIR_PCI_STUB_DRIVER_NONE ||
@ -1023,20 +1014,15 @@ virPCIProbeStubDriver(virPCIStubDriver driver)
} }
recheck: recheck:
if ((drvpath = virPCIDriverDir(drvname)) && virFileExists(drvpath)) { if ((drvpath = virPCIDriverDir(drvname)) && virFileExists(drvpath))
/* driver already loaded, return */ /* driver already loaded, return */
VIR_FREE(drvpath);
return 0; return 0;
}
VIR_FREE(drvpath);
if (!probed) { if (!probed) {
char *errbuf = NULL; VIR_AUTOFREE(char *) errbuf = NULL;
probed = true; probed = true;
if ((errbuf = virKModLoad(drvname, true))) { if ((errbuf = virKModLoad(drvname, true))) {
VIR_WARN("failed to load driver %s: %s", drvname, errbuf); VIR_WARN("failed to load driver %s: %s", drvname, errbuf);
VIR_FREE(errbuf);
goto cleanup; goto cleanup;
} }
@ -1064,38 +1050,30 @@ virPCIProbeStubDriver(virPCIStubDriver driver)
int int
virPCIDeviceUnbind(virPCIDevicePtr dev) virPCIDeviceUnbind(virPCIDevicePtr dev)
{ {
char *path = NULL; VIR_AUTOFREE(char *) path = NULL;
char *drvpath = NULL; VIR_AUTOFREE(char *) drvpath = NULL;
char *driver = NULL; VIR_AUTOFREE(char *) driver = NULL;
int ret = -1;
if (virPCIDeviceGetDriverPathAndName(dev, &drvpath, &driver) < 0) if (virPCIDeviceGetDriverPathAndName(dev, &drvpath, &driver) < 0)
goto cleanup; return -1;
if (!driver) { if (!driver)
/* The device is not bound to any driver */ /* The device is not bound to any driver */
ret = 0; return 0;
goto cleanup;
}
if (!(path = virPCIFile(dev->name, "driver/unbind"))) if (!(path = virPCIFile(dev->name, "driver/unbind")))
goto cleanup; return -1;
if (virFileExists(path)) { if (virFileExists(path)) {
if (virFileWriteStr(path, dev->name, 0) < 0) { if (virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to unbind PCI device '%s' from %s"), _("Failed to unbind PCI device '%s' from %s"),
dev->name, driver); dev->name, driver);
goto cleanup; return -1;
} }
} }
ret = 0; return 0;
cleanup:
VIR_FREE(path);
VIR_FREE(drvpath);
VIR_FREE(driver);
return ret;
} }
@ -1138,8 +1116,7 @@ static int
virPCIDeviceBindWithDriverOverride(virPCIDevicePtr dev, virPCIDeviceBindWithDriverOverride(virPCIDevicePtr dev,
const char *driverName) const char *driverName)
{ {
int ret = -1; VIR_AUTOFREE(char *) path = NULL;
char *path;
if (!(path = virPCIFile(dev->name, "driver_override"))) if (!(path = virPCIFile(dev->name, "driver_override")))
return -1; return -1;
@ -1149,26 +1126,22 @@ virPCIDeviceBindWithDriverOverride(virPCIDevicePtr dev,
_("Failed to add driver '%s' to driver_override " _("Failed to add driver '%s' to driver_override "
" interface of PCI device '%s'"), " interface of PCI device '%s'"),
driverName, dev->name); driverName, dev->name);
goto cleanup; return -1;
} }
if (virPCIDeviceRebind(dev) < 0) if (virPCIDeviceRebind(dev) < 0)
goto cleanup; return -1;
ret = 0; return 0;
cleanup:
VIR_FREE(path);
return ret;
} }
static int static int
virPCIDeviceUnbindFromStubWithNewid(virPCIDevicePtr dev) virPCIDeviceUnbindFromStubWithNewid(virPCIDevicePtr dev)
{ {
int result = -1; int result = -1;
char *drvdir = NULL; VIR_AUTOFREE(char *) drvdir = NULL;
char *path = NULL; VIR_AUTOFREE(char *) path = NULL;
char *driver = NULL; VIR_AUTOFREE(char *) driver = NULL;
/* If the device is currently bound to one of the "well known" /* If the device is currently bound to one of the "well known"
* stub drivers, then unbind it, otherwise ignore it. * stub drivers, then unbind it, otherwise ignore it.
@ -1257,10 +1230,6 @@ virPCIDeviceUnbindFromStubWithNewid(virPCIDevicePtr dev)
dev->remove_slot = false; dev->remove_slot = false;
dev->reprobe = false; dev->reprobe = false;
VIR_FREE(drvdir);
VIR_FREE(path);
VIR_FREE(driver);
return result; return result;
} }
@ -1278,8 +1247,7 @@ virPCIDeviceUnbindFromStubWithOverride(virPCIDevicePtr dev)
static int static int
virPCIDeviceUnbindFromStub(virPCIDevicePtr dev) virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
{ {
int ret; VIR_AUTOFREE(char *) path = NULL;
char *path;
/* /*
* Prefer using the device's driver_override interface, falling back * Prefer using the device's driver_override interface, falling back
@ -1289,12 +1257,9 @@ virPCIDeviceUnbindFromStub(virPCIDevicePtr dev)
return -1; return -1;
if (virFileExists(path)) if (virFileExists(path))
ret = virPCIDeviceUnbindFromStubWithOverride(dev); return virPCIDeviceUnbindFromStubWithOverride(dev);
else
ret = virPCIDeviceUnbindFromStubWithNewid(dev);
VIR_FREE(path); return virPCIDeviceUnbindFromStubWithNewid(dev);
return ret;
} }
static int static int
@ -1302,9 +1267,9 @@ virPCIDeviceBindToStubWithNewid(virPCIDevicePtr dev)
{ {
int result = -1; int result = -1;
bool reprobe = false; bool reprobe = false;
char *stubDriverPath = NULL; VIR_AUTOFREE(char *) stubDriverPath = NULL;
char *driverLink = NULL; VIR_AUTOFREE(char *) driverLink = NULL;
char *path = NULL; /* reused for different purposes */ VIR_AUTOFREE(char *) path = NULL; /* reused for different purposes */
const char *stubDriverName = NULL; const char *stubDriverName = NULL;
virErrorPtr err = NULL; virErrorPtr err = NULL;
@ -1436,10 +1401,6 @@ virPCIDeviceBindToStubWithNewid(virPCIDevicePtr dev)
} }
cleanup: cleanup:
VIR_FREE(stubDriverPath);
VIR_FREE(driverLink);
VIR_FREE(path);
if (result < 0) if (result < 0)
virPCIDeviceUnbindFromStub(dev); virPCIDeviceUnbindFromStub(dev);
@ -1453,10 +1414,9 @@ virPCIDeviceBindToStubWithNewid(virPCIDevicePtr dev)
static int static int
virPCIDeviceBindToStubWithOverride(virPCIDevicePtr dev) virPCIDeviceBindToStubWithOverride(virPCIDevicePtr dev)
{ {
int ret = -1;
const char *stubDriverName; const char *stubDriverName;
char *stubDriverPath = NULL; VIR_AUTOFREE(char *) stubDriverPath = NULL;
char *driverLink = NULL; VIR_AUTOFREE(char *) driverLink = NULL;
/* Check the device is configured to use one of the known stub drivers */ /* Check the device is configured to use one of the known stub drivers */
if (dev->stubDriver == VIR_PCI_STUB_DRIVER_NONE) { if (dev->stubDriver == VIR_PCI_STUB_DRIVER_NONE) {
@ -1473,35 +1433,28 @@ virPCIDeviceBindToStubWithOverride(virPCIDevicePtr dev)
if (!(stubDriverPath = virPCIDriverDir(stubDriverName)) || if (!(stubDriverPath = virPCIDriverDir(stubDriverName)) ||
!(driverLink = virPCIFile(dev->name, "driver"))) !(driverLink = virPCIFile(dev->name, "driver")))
goto cleanup; return -1;
if (virFileExists(driverLink)) { if (virFileExists(driverLink)) {
if (virFileLinkPointsTo(driverLink, stubDriverPath)) { if (virFileLinkPointsTo(driverLink, stubDriverPath)) {
/* The device is already bound to the correct driver */ /* The device is already bound to the correct driver */
VIR_DEBUG("Device %s is already bound to %s", VIR_DEBUG("Device %s is already bound to %s",
dev->name, stubDriverName); dev->name, stubDriverName);
ret = 0; return 0;
goto cleanup;
} }
} }
if (virPCIDeviceBindWithDriverOverride(dev, stubDriverName) < 0) if (virPCIDeviceBindWithDriverOverride(dev, stubDriverName) < 0)
goto cleanup; return -1;
dev->unbind_from_stub = true; dev->unbind_from_stub = true;
ret = 0; return 0;
cleanup:
VIR_FREE(stubDriverPath);
VIR_FREE(driverLink);
return ret;
} }
static int static int
virPCIDeviceBindToStub(virPCIDevicePtr dev) virPCIDeviceBindToStub(virPCIDevicePtr dev)
{ {
int ret; VIR_AUTOFREE(char *) path = NULL;
char *path;
/* /*
* Prefer using the device's driver_override interface, falling back * Prefer using the device's driver_override interface, falling back
@ -1511,12 +1464,9 @@ virPCIDeviceBindToStub(virPCIDevicePtr dev)
return -1; return -1;
if (virFileExists(path)) if (virFileExists(path))
ret = virPCIDeviceBindToStubWithOverride(dev); return virPCIDeviceBindToStubWithOverride(dev);
else
ret = virPCIDeviceBindToStubWithNewid(dev);
VIR_FREE(path); return virPCIDeviceBindToStubWithNewid(dev);
return ret;
} }
/* virPCIDeviceDetach: /* virPCIDeviceDetach:
@ -1700,19 +1650,15 @@ virPCIDeviceWaitForCleanup(virPCIDevicePtr dev, const char *matcher)
static char * static char *
virPCIDeviceReadID(virPCIDevicePtr dev, const char *id_name) virPCIDeviceReadID(virPCIDevicePtr dev, const char *id_name)
{ {
char *path = NULL; VIR_AUTOFREE(char *) path = NULL;
char *id_str; char *id_str;
if (!(path = virPCIFile(dev->name, id_name))) if (!(path = virPCIFile(dev->name, id_name)))
return NULL; return NULL;
/* ID string is '0xNNNN\n' ... i.e. 7 bytes */ /* ID string is '0xNNNN\n' ... i.e. 7 bytes */
if (virFileReadAll(path, 7, &id_str) < 0) { if (virFileReadAll(path, 7, &id_str) < 0)
VIR_FREE(path);
return NULL; return NULL;
}
VIR_FREE(path);
/* Check for 0x suffix */ /* Check for 0x suffix */
if (id_str[0] != '0' || id_str[1] != 'x') { if (id_str[0] != '0' || id_str[1] != 'x') {
@ -1755,8 +1701,8 @@ virPCIDeviceNew(unsigned int domain,
unsigned int function) unsigned int function)
{ {
virPCIDevicePtr dev; virPCIDevicePtr dev;
char *vendor = NULL; VIR_AUTOFREE(char *) vendor = NULL;
char *product = NULL; VIR_AUTOFREE(char *) product = NULL;
if (VIR_ALLOC(dev) < 0) if (VIR_ALLOC(dev) < 0)
return NULL; return NULL;
@ -1806,8 +1752,6 @@ virPCIDeviceNew(unsigned int domain,
VIR_DEBUG("%s %s: initialized", dev->id, dev->name); VIR_DEBUG("%s %s: initialized", dev->id, dev->name);
cleanup: cleanup:
VIR_FREE(product);
VIR_FREE(vendor);
return dev; return dev;
error: error:
@ -2129,8 +2073,7 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
virPCIDeviceFileActor actor, virPCIDeviceFileActor actor,
void *opaque) void *opaque)
{ {
char *pcidir = NULL; VIR_AUTOFREE(char *) pcidir = NULL;
char *file = NULL;
DIR *dir = NULL; DIR *dir = NULL;
int ret = -1; int ret = -1;
struct dirent *ent; struct dirent *ent;
@ -2145,6 +2088,7 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
goto cleanup; goto cleanup;
while ((direrr = virDirRead(dir, &ent, pcidir)) > 0) { while ((direrr = virDirRead(dir, &ent, pcidir)) > 0) {
VIR_AUTOFREE(char *) file = NULL;
/* Device assignment requires: /* Device assignment requires:
* $PCIDIR/config, $PCIDIR/resource, $PCIDIR/resourceNNN, * $PCIDIR/config, $PCIDIR/resource, $PCIDIR/resourceNNN,
* $PCIDIR/rom, $PCIDIR/reset, $PCIDIR/vendor, $PCIDIR/device * $PCIDIR/rom, $PCIDIR/reset, $PCIDIR/vendor, $PCIDIR/device
@ -2159,8 +2103,6 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
goto cleanup; goto cleanup;
if ((actor)(dev, file, opaque) < 0) if ((actor)(dev, file, opaque) < 0)
goto cleanup; goto cleanup;
VIR_FREE(file);
} }
} }
if (direrr < 0) if (direrr < 0)
@ -2170,8 +2112,6 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev,
cleanup: cleanup:
VIR_DIR_CLOSE(dir); VIR_DIR_CLOSE(dir);
VIR_FREE(file);
VIR_FREE(pcidir);
return ret; return ret;
} }
@ -2186,7 +2126,7 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
virPCIDeviceAddressActor actor, virPCIDeviceAddressActor actor,
void *opaque) void *opaque)
{ {
char *groupPath = NULL; VIR_AUTOFREE(char *) groupPath = NULL;
DIR *groupDir = NULL; DIR *groupDir = NULL;
int ret = -1; int ret = -1;
struct dirent *ent; struct dirent *ent;
@ -2222,7 +2162,6 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddressPtr orig,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(groupPath);
VIR_DIR_CLOSE(groupDir); VIR_DIR_CLOSE(groupDir);
return ret; return ret;
} }
@ -2341,28 +2280,25 @@ virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDeviceAddressPtr devAddr,
int int
virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr) virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
{ {
char *devName = NULL; VIR_AUTOFREE(char *) devName = NULL;
char *devPath = NULL; VIR_AUTOFREE(char *) devPath = NULL;
char *groupPath = NULL; VIR_AUTOFREE(char *) groupPath = NULL;
const char *groupNumStr; const char *groupNumStr;
unsigned int groupNum; unsigned int groupNum;
int ret = -1;
if (virAsprintf(&devName, "%.4x:%.2x:%.2x.%.1x", addr->domain, if (virAsprintf(&devName, "%.4x:%.2x:%.2x.%.1x", addr->domain,
addr->bus, addr->slot, addr->function) < 0) addr->bus, addr->slot, addr->function) < 0)
goto cleanup; return -1;
if (!(devPath = virPCIFile(devName, "iommu_group"))) if (!(devPath = virPCIFile(devName, "iommu_group")))
goto cleanup; return -1;
if (virFileIsLink(devPath) != 1) { if (virFileIsLink(devPath) != 1)
ret = -2; return -2;
goto cleanup;
}
if (virFileResolveLink(devPath, &groupPath) < 0) { if (virFileResolveLink(devPath, &groupPath) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to resolve device %s iommu_group symlink %s"), _("Unable to resolve device %s iommu_group symlink %s"),
devName, devPath); devName, devPath);
goto cleanup; return -1;
} }
groupNumStr = last_component(groupPath); groupNumStr = last_component(groupPath);
@ -2371,16 +2307,10 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
_("device %s iommu_group symlink %s has " _("device %s iommu_group symlink %s has "
"invalid group number %s"), "invalid group number %s"),
devName, groupPath, groupNumStr); devName, groupPath, groupNumStr);
ret = -1; return -1;
goto cleanup;
} }
ret = groupNum; return groupNum;
cleanup:
VIR_FREE(devName);
VIR_FREE(devPath);
VIR_FREE(groupPath);
return ret;
} }
@ -2390,30 +2320,28 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr)
char * char *
virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev) virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev)
{ {
char *devPath = NULL; VIR_AUTOFREE(char *) devPath = NULL;
char *groupPath = NULL; VIR_AUTOFREE(char *) groupPath = NULL;
char *groupDev = NULL; char *groupDev = NULL;
if (!(devPath = virPCIFile(dev->name, "iommu_group"))) if (!(devPath = virPCIFile(dev->name, "iommu_group")))
goto cleanup; return NULL;
if (virFileIsLink(devPath) != 1) { if (virFileIsLink(devPath) != 1) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Invalid device %s iommu_group file %s is not a symlink"), _("Invalid device %s iommu_group file %s is not a symlink"),
dev->name, devPath); dev->name, devPath);
goto cleanup; return NULL;
} }
if (virFileResolveLink(devPath, &groupPath) < 0) { if (virFileResolveLink(devPath, &groupPath) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to resolve device %s iommu_group symlink %s"), _("Unable to resolve device %s iommu_group symlink %s"),
dev->name, devPath); dev->name, devPath);
goto cleanup; return NULL;
} }
if (virAsprintf(&groupDev, "/dev/vfio/%s", if (virAsprintf(&groupDev, "/dev/vfio/%s",
last_component(groupPath)) < 0) last_component(groupPath)) < 0)
goto cleanup; return NULL;
cleanup:
VIR_FREE(devPath);
VIR_FREE(groupPath);
return groupDev; return groupDev;
} }
@ -2614,7 +2542,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
{ {
virPCIDeviceAddressPtr bdf = NULL; virPCIDeviceAddressPtr bdf = NULL;
char *config_address = NULL; char *config_address = NULL;
char *device_path = NULL; VIR_AUTOFREE(char *) device_path = NULL;
if (!virFileExists(device_link)) { if (!virFileExists(device_link)) {
VIR_DEBUG("'%s' does not exist", device_link); VIR_DEBUG("'%s' does not exist", device_link);
@ -2631,19 +2559,16 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
config_address = last_component(device_path); config_address = last_component(device_path);
if (VIR_ALLOC(bdf) < 0) if (VIR_ALLOC(bdf) < 0)
goto out; return NULL;
if (virPCIDeviceAddressParse(config_address, bdf) < 0) { if (virPCIDeviceAddressParse(config_address, bdf) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to parse PCI config address '%s'"), _("Failed to parse PCI config address '%s'"),
config_address); config_address);
VIR_FREE(bdf); VIR_FREE(bdf);
goto out; return NULL;
} }
out:
VIR_FREE(device_path);
return bdf; return bdf;
} }
@ -2665,7 +2590,7 @@ int
virPCIGetPhysicalFunction(const char *vf_sysfs_path, virPCIGetPhysicalFunction(const char *vf_sysfs_path,
virPCIDeviceAddressPtr *pf) virPCIDeviceAddressPtr *pf)
{ {
char *device_link = NULL; VIR_AUTOFREE(char *) device_link = NULL;
*pf = NULL; *pf = NULL;
@ -2678,7 +2603,6 @@ virPCIGetPhysicalFunction(const char *vf_sysfs_path,
VIR_DEBUG("PF for VF device '%s': %.4x:%.2x:%.2x.%.1x", vf_sysfs_path, VIR_DEBUG("PF for VF device '%s': %.4x:%.2x:%.2x.%.1x", vf_sysfs_path,
(*pf)->domain, (*pf)->bus, (*pf)->slot, (*pf)->function); (*pf)->domain, (*pf)->bus, (*pf)->slot, (*pf)->function);
} }
VIR_FREE(device_link);
return 0; return 0;
} }
@ -2695,9 +2619,9 @@ virPCIGetVirtualFunctions(const char *sysfs_path,
{ {
int ret = -1; int ret = -1;
size_t i; size_t i;
char *device_link = NULL; VIR_AUTOFREE(char *) totalvfs_file = NULL;
VIR_AUTOFREE(char *) totalvfs_str = NULL;
virPCIDeviceAddressPtr config_addr = NULL; virPCIDeviceAddressPtr config_addr = NULL;
char *totalvfs_file = NULL, *totalvfs_str = NULL;
*virtual_functions = NULL; *virtual_functions = NULL;
*num_virtual_functions = 0; *num_virtual_functions = 0;
@ -2719,6 +2643,7 @@ virPCIGetVirtualFunctions(const char *sysfs_path,
} }
do { do {
VIR_AUTOFREE(char *) device_link = NULL;
/* look for virtfn%d links until one isn't found */ /* look for virtfn%d links until one isn't found */
if (virAsprintf(&device_link, "%s/virtfn%zu", sysfs_path, *num_virtual_functions) < 0) if (virAsprintf(&device_link, "%s/virtfn%zu", sysfs_path, *num_virtual_functions) < 0)
goto error; goto error;
@ -2736,18 +2661,13 @@ virPCIGetVirtualFunctions(const char *sysfs_path,
if (VIR_APPEND_ELEMENT(*virtual_functions, *num_virtual_functions, if (VIR_APPEND_ELEMENT(*virtual_functions, *num_virtual_functions,
config_addr) < 0) config_addr) < 0)
goto error; goto error;
VIR_FREE(device_link);
} while (1); } while (1);
VIR_DEBUG("Found %zu virtual functions for %s", VIR_DEBUG("Found %zu virtual functions for %s",
*num_virtual_functions, sysfs_path); *num_virtual_functions, sysfs_path);
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(device_link);
VIR_FREE(config_addr); VIR_FREE(config_addr);
VIR_FREE(totalvfs_file);
VIR_FREE(totalvfs_str);
return ret; return ret;
error: error:
@ -2765,18 +2685,13 @@ virPCIGetVirtualFunctions(const char *sysfs_path,
int int
virPCIIsVirtualFunction(const char *vf_sysfs_device_link) virPCIIsVirtualFunction(const char *vf_sysfs_device_link)
{ {
char *vf_sysfs_physfn_link = NULL; VIR_AUTOFREE(char *) vf_sysfs_physfn_link = NULL;
int ret = -1;
if (virAsprintf(&vf_sysfs_physfn_link, "%s/physfn", if (virAsprintf(&vf_sysfs_physfn_link, "%s/physfn",
vf_sysfs_device_link) < 0) vf_sysfs_device_link) < 0)
return ret; return -1;
ret = virFileExists(vf_sysfs_physfn_link); return virFileExists(vf_sysfs_physfn_link);
VIR_FREE(vf_sysfs_physfn_link);
return ret;
} }
/* /*
@ -2868,12 +2783,12 @@ virPCIGetNetName(const char *device_link_sysfs_path,
char *physPortID, char *physPortID,
char **netname) char **netname)
{ {
char *pcidev_sysfs_net_path = NULL; VIR_AUTOFREE(char *) pcidev_sysfs_net_path = NULL;
VIR_AUTOFREE(char *) firstEntryName = NULL;
VIR_AUTOFREE(char *) thisPhysPortID = NULL;
int ret = -1; int ret = -1;
DIR *dir = NULL; DIR *dir = NULL;
struct dirent *entry = NULL; struct dirent *entry = NULL;
char *firstEntryName = NULL;
char *thisPhysPortID = NULL;
size_t i = 0; size_t i = 0;
if (virBuildPath(&pcidev_sysfs_net_path, device_link_sysfs_path, if (virBuildPath(&pcidev_sysfs_net_path, device_link_sysfs_path,
@ -2946,9 +2861,6 @@ virPCIGetNetName(const char *device_link_sysfs_path,
} }
cleanup: cleanup:
VIR_DIR_CLOSE(dir); VIR_DIR_CLOSE(dir);
VIR_FREE(pcidev_sysfs_net_path);
VIR_FREE(thisPhysPortID);
VIR_FREE(firstEntryName);
return ret; return ret;
} }
@ -2959,9 +2871,9 @@ virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path,
int *vf_index) int *vf_index)
{ {
virPCIDeviceAddressPtr pf_config_address = NULL; virPCIDeviceAddressPtr pf_config_address = NULL;
char *pf_sysfs_device_path = NULL; VIR_AUTOFREE(char *) pf_sysfs_device_path = NULL;
char *vfname = NULL; VIR_AUTOFREE(char *) vfname = NULL;
char *vfPhysPortID = NULL; VIR_AUTOFREE(char *) vfPhysPortID = NULL;
int ret = -1; int ret = -1;
if (virPCIGetPhysicalFunction(vf_sysfs_device_path, &pf_config_address) < 0) if (virPCIGetPhysicalFunction(vf_sysfs_device_path, &pf_config_address) < 0)
@ -3016,9 +2928,6 @@ virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(pf_config_address); VIR_FREE(pf_config_address);
VIR_FREE(pf_sysfs_device_path);
VIR_FREE(vfname);
VIR_FREE(vfPhysPortID);
return ret; return ret;
} }
@ -3032,8 +2941,7 @@ virPCIGetMdevTypes(const char *sysfspath,
int dirret = -1; int dirret = -1;
DIR *dir = NULL; DIR *dir = NULL;
struct dirent *entry; struct dirent *entry;
char *types_path = NULL; VIR_AUTOFREE(char *) types_path = NULL;
char *tmppath = NULL;
virMediatedDeviceTypePtr mdev_type = NULL; virMediatedDeviceTypePtr mdev_type = NULL;
virMediatedDeviceTypePtr *mdev_types = NULL; virMediatedDeviceTypePtr *mdev_types = NULL;
size_t ntypes = 0; size_t ntypes = 0;
@ -3051,6 +2959,7 @@ virPCIGetMdevTypes(const char *sysfspath,
} }
while ((dirret = virDirRead(dir, &entry, types_path)) > 0) { while ((dirret = virDirRead(dir, &entry, types_path)) > 0) {
VIR_AUTOFREE(char *) tmppath = NULL;
/* append the type id to the path and read the attributes from there */ /* append the type id to the path and read the attributes from there */
if (virAsprintf(&tmppath, "%s/%s", types_path, entry->d_name) < 0) if (virAsprintf(&tmppath, "%s/%s", types_path, entry->d_name) < 0)
goto cleanup; goto cleanup;
@ -3060,8 +2969,6 @@ virPCIGetMdevTypes(const char *sysfspath,
if (VIR_APPEND_ELEMENT(mdev_types, ntypes, mdev_type) < 0) if (VIR_APPEND_ELEMENT(mdev_types, ntypes, mdev_type) < 0)
goto cleanup; goto cleanup;
VIR_FREE(tmppath);
} }
if (dirret < 0) if (dirret < 0)
@ -3075,8 +2982,6 @@ virPCIGetMdevTypes(const char *sysfspath,
for (i = 0; i < ntypes; i++) for (i = 0; i < ntypes; i++)
virMediatedDeviceTypeFree(mdev_types[i]); virMediatedDeviceTypeFree(mdev_types[i]);
VIR_FREE(mdev_types); VIR_FREE(mdev_types);
VIR_FREE(types_path);
VIR_FREE(tmppath);
VIR_DIR_CLOSE(dir); VIR_DIR_CLOSE(dir);
return ret; return ret;
} }