1
0

Adapt to VIR_STRDUP and VIR_STRNDUP in src/node_device/*

This commit is contained in:
Michal Privoznik 2013-05-03 14:44:20 +02:00
parent 6b936bd79c
commit be2636fd0b
3 changed files with 52 additions and 101 deletions

View File

@ -89,13 +89,8 @@ static int update_driver_name(virNodeDeviceObjPtr dev)
} }
p = strrchr(devpath, '/'); p = strrchr(devpath, '/');
if (p) { if (p && VIR_STRDUP(dev->def->driver, p + 1) < 0)
dev->def->driver = strdup(p+1);
if (!dev->def->driver) {
virReportOOMError();
goto cleanup; goto cleanup;
}
}
ret = 0; ret = 0;
cleanup: cleanup:
@ -162,9 +157,8 @@ nodeListDevices(virConnectPtr conn,
virNodeDeviceObjLock(driver->devs.objs[i]); virNodeDeviceObjLock(driver->devs.objs[i]);
if (cap == NULL || if (cap == NULL ||
virNodeDeviceHasCap(driver->devs.objs[i], cap)) { virNodeDeviceHasCap(driver->devs.objs[i], cap)) {
if ((names[ndevs++] = strdup(driver->devs.objs[i]->def->name)) == NULL) { if (VIR_STRDUP(names[ndevs++], driver->devs.objs[i]->def->name) < 0) {
virNodeDeviceObjUnlock(driver->devs.objs[i]); virNodeDeviceObjUnlock(driver->devs.objs[i]);
virReportOOMError();
goto failure; goto failure;
} }
} }
@ -322,9 +316,8 @@ nodeDeviceGetParent(virNodeDevicePtr dev)
} }
if (obj->def->parent) { if (obj->def->parent) {
ret = strdup(obj->def->parent); if (VIR_STRDUP(ret, obj->def->parent) < 0)
if (!ret) goto cleanup;
virReportOOMError();
} else { } else {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("no parent for this device")); "%s", _("no parent for this device"));
@ -389,12 +382,9 @@ nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
} }
for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) { for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
names[ncaps] = strdup(virNodeDevCapTypeToString(caps->type)); if (VIR_STRDUP(names[ncaps], virNodeDevCapTypeToString(caps->type)) < 0)
if (names[ncaps++] == NULL) {
virReportOOMError();
goto cleanup; goto cleanup;
} }
}
ret = ncaps; ret = ncaps;
cleanup: cleanup:
@ -555,19 +545,18 @@ nodeDeviceDestroy(virNodeDevicePtr dev)
goto out; goto out;
} }
parent_name = strdup(obj->def->parent);
/* virNodeDeviceGetParentHost will cause the device object's lock to be /* virNodeDeviceGetParentHost will cause the device object's lock to be
* taken, so we have to dup the parent's name and drop the lock * taken, so we have to dup the parent's name and drop the lock
* before calling it. We don't need the reference to the object * before calling it. We don't need the reference to the object
* any more once we have the parent's name. */ * any more once we have the parent's name. */
if (VIR_STRDUP(parent_name, obj->def->parent) < 0) {
virNodeDeviceObjUnlock(obj); virNodeDeviceObjUnlock(obj);
obj = NULL; obj = NULL;
if (parent_name == NULL) {
virReportOOMError();
goto out; goto out;
} }
virNodeDeviceObjUnlock(obj);
obj = NULL;
if (virNodeDeviceGetParentHost(&driver->devs, if (virNodeDeviceGetParentHost(&driver->devs,
dev->name, dev->name,

View File

@ -446,10 +446,10 @@ static void dev_create(const char *udi)
virNodeDeviceDefPtr def = NULL; virNodeDeviceDefPtr def = NULL;
const char *name = hal_name(udi); const char *name = hal_name(udi);
int rv; int rv;
char *privData = strdup(udi); char *privData;
char *devicePath = NULL; char *devicePath = NULL;
if (!privData) if (VIR_STRDUP(privData, udi) < 0)
return; return;
nodeDeviceLock(driverState); nodeDeviceLock(driverState);
@ -458,15 +458,16 @@ static void dev_create(const char *udi)
if (VIR_ALLOC(def) < 0) if (VIR_ALLOC(def) < 0)
goto failure; goto failure;
if ((def->name = strdup(name)) == NULL) if (VIR_STRDUP(def->name, name) < 0)
goto failure; goto failure;
if (get_str_prop(ctx, udi, "info.parent", &parent_key) == 0) { if (get_str_prop(ctx, udi, "info.parent", &parent_key) == 0) {
def->parent = strdup(hal_name(parent_key)); if (VIR_STRDUP(def->parent, hal_name(parent_key)) < 0) {
VIR_FREE(parent_key); VIR_FREE(parent_key);
if (def->parent == NULL)
goto failure; goto failure;
} }
VIR_FREE(parent_key);
}
rv = gather_capabilities(ctx, udi, &def->caps); rv = gather_capabilities(ctx, udi, &def->caps);
if (rv != 0) goto failure; if (rv != 0) goto failure;

View File

@ -125,12 +125,10 @@ static int udevGetDeviceProperty(struct udev_device *udev_device,
/* If this allocation is changed, the comment at the beginning /* If this allocation is changed, the comment at the beginning
* of the function must also be changed. */ * of the function must also be changed. */
*property_value = strdup(udev_value); if (VIR_STRDUP(*property_value, udev_value) < 0) {
if (*property_value == NULL) {
VIR_ERROR(_("Failed to allocate memory for property value for " VIR_ERROR(_("Failed to allocate memory for property value for "
"property key '%s' on device with sysname '%s'"), "property key '%s' on device with sysname '%s'"),
property_key, udev_device_get_sysname(udev_device)); property_key, udev_device_get_sysname(udev_device));
virReportOOMError();
ret = PROPERTY_ERROR; ret = PROPERTY_ERROR;
goto out; goto out;
} }
@ -214,12 +212,10 @@ static int udevGetDeviceSysfsAttr(struct udev_device *udev_device,
/* If this allocation is changed, the comment at the beginning /* If this allocation is changed, the comment at the beginning
* of the function must also be changed. */ * of the function must also be changed. */
*attr_value = strdup(udev_value); if (VIR_STRDUP(*attr_value, udev_value) < 0) {
if (*attr_value == NULL) {
VIR_ERROR(_("Failed to allocate memory for sysfs attribute value for " VIR_ERROR(_("Failed to allocate memory for sysfs attribute value for "
"sysfs attribute '%s' on device with sysname '%s'"), "sysfs attribute '%s' on device with sysname '%s'"),
attr_name, udev_device_get_sysname(udev_device)); attr_name, udev_device_get_sysname(udev_device));
virReportOOMError();
ret = PROPERTY_ERROR; ret = PROPERTY_ERROR;
goto out; goto out;
} }
@ -387,21 +383,9 @@ static int udevTranslatePCIIds(unsigned int vendor,
NULL, NULL,
NULL); NULL);
if (vendor_name != NULL) { if (VIR_STRDUP(*vendor_string, vendor_name) < 0||
*vendor_string = strdup(vendor_name); VIR_STRDUP(*product_string, device_name) < 0)
if (*vendor_string == NULL) {
virReportOOMError();
goto out; goto out;
}
}
if (device_name != NULL) {
*product_string = strdup(device_name);
if (*product_string == NULL) {
virReportOOMError();
goto out;
}
}
ret = 0; ret = 0;
@ -691,11 +675,8 @@ static int udevProcessSCSITarget(struct udev_device *device ATTRIBUTE_UNUSED,
sysname = udev_device_get_sysname(device); sysname = udev_device_get_sysname(device);
data->scsi_target.name = strdup(sysname); if (VIR_STRDUP(data->scsi_target.name, sysname) < 0)
if (data->scsi_target.name == NULL) {
virReportOOMError();
goto out; goto out;
}
if (udevGenerateDeviceName(device, def, NULL) != 0) { if (udevGenerateDeviceName(device, def, NULL) != 0) {
goto out; goto out;
@ -718,34 +699,34 @@ static int udevGetSCSIType(virNodeDeviceDefPtr def ATTRIBUTE_UNUSED,
switch (type) { switch (type) {
case TYPE_DISK: case TYPE_DISK:
*typestring = strdup("disk"); ignore_value(VIR_STRDUP(*typestring, "disk"));
break; break;
case TYPE_TAPE: case TYPE_TAPE:
*typestring = strdup("tape"); ignore_value(VIR_STRDUP(*typestring, "tape"));
break; break;
case TYPE_PROCESSOR: case TYPE_PROCESSOR:
*typestring = strdup("processor"); ignore_value(VIR_STRDUP(*typestring, "processor"));
break; break;
case TYPE_WORM: case TYPE_WORM:
*typestring = strdup("worm"); ignore_value(VIR_STRDUP(*typestring, "worm"));
break; break;
case TYPE_ROM: case TYPE_ROM:
*typestring = strdup("cdrom"); ignore_value(VIR_STRDUP(*typestring, "cdrom"));
break; break;
case TYPE_SCANNER: case TYPE_SCANNER:
*typestring = strdup("scanner"); ignore_value(VIR_STRDUP(*typestring, "scanner"));
break; break;
case TYPE_MOD: case TYPE_MOD:
*typestring = strdup("mod"); ignore_value(VIR_STRDUP(*typestring, "mod"));
break; break;
case TYPE_MEDIUM_CHANGER: case TYPE_MEDIUM_CHANGER:
*typestring = strdup("changer"); ignore_value(VIR_STRDUP(*typestring, "changer"));
break; break;
case TYPE_ENCLOSURE: case TYPE_ENCLOSURE:
*typestring = strdup("enclosure"); ignore_value(VIR_STRDUP(*typestring, "enclosure"));
break; break;
case TYPE_RAID: case TYPE_RAID:
*typestring = strdup("raid"); ignore_value(VIR_STRDUP(*typestring, "raid"));
break; break;
case TYPE_NO_LUN: case TYPE_NO_LUN:
default: default:
@ -756,7 +737,6 @@ static int udevGetSCSIType(virNodeDeviceDefPtr def ATTRIBUTE_UNUSED,
if (*typestring == NULL) { if (*typestring == NULL) {
if (foundtype == 1) { if (foundtype == 1) {
ret = -1; ret = -1;
virReportOOMError();
} else { } else {
VIR_DEBUG("Failed to find SCSI device type %d for %s", VIR_DEBUG("Failed to find SCSI device type %d for %s",
type, def->sysfs_path); type, def->sysfs_path);
@ -917,11 +897,8 @@ static int udevProcessCDROM(struct udev_device *device,
* change it to cdrom to preserve compatibility with earlier * change it to cdrom to preserve compatibility with earlier
* versions of libvirt. */ * versions of libvirt. */
VIR_FREE(def->caps->data.storage.drive_type); VIR_FREE(def->caps->data.storage.drive_type);
def->caps->data.storage.drive_type = strdup("cdrom"); if (VIR_STRDUP(def->caps->data.storage.drive_type, "cdrom") < 0)
if (def->caps->data.storage.drive_type == NULL) {
virReportOOMError();
goto out; goto out;
}
if ((udevGetIntProperty(device, "ID_CDROM_MEDIA", if ((udevGetIntProperty(device, "ID_CDROM_MEDIA",
&tmp_int, 0) == PROPERTY_FOUND)) &tmp_int, 0) == PROPERTY_FOUND))
@ -996,10 +973,7 @@ static int udevKludgeStorageType(virNodeDeviceDefPtr def)
if (STRPREFIX(def->caps->data.storage.block, "/dev/vd")) { if (STRPREFIX(def->caps->data.storage.block, "/dev/vd")) {
/* virtio disk */ /* virtio disk */
def->caps->data.storage.drive_type = strdup("disk"); ret = VIR_STRDUP(def->caps->data.storage.drive_type, "disk");
if (def->caps->data.storage.drive_type != NULL) {
ret = 0;
}
} }
if (ret != 0) { if (ret != 0) {
@ -1043,7 +1017,9 @@ static int udevProcessStorage(struct udev_device *device,
VIR_DEBUG("No devnode for '%s'", udev_device_get_devpath(device)); VIR_DEBUG("No devnode for '%s'", udev_device_get_devpath(device));
goto out; goto out;
} }
data->storage.block = strdup(devnode);
if (VIR_STRDUP(data->storage.block, devnode) < 0)
goto out;
if (udevGetStringProperty(device, if (udevGetStringProperty(device,
"ID_BUS", "ID_BUS",
@ -1083,15 +1059,13 @@ static int udevProcessStorage(struct udev_device *device,
&tmp_int, 0) == PROPERTY_FOUND) && &tmp_int, 0) == PROPERTY_FOUND) &&
(tmp_int == 1)) { (tmp_int == 1)) {
data->storage.drive_type = strdup("floppy"); if (VIR_STRDUP(data->storage.drive_type, "floppy") < 0)
if (!data->storage.drive_type)
goto out; goto out;
} else if ((udevGetIntProperty(device, "ID_DRIVE_FLASH_SD", } else if ((udevGetIntProperty(device, "ID_DRIVE_FLASH_SD",
&tmp_int, 0) == PROPERTY_FOUND) && &tmp_int, 0) == PROPERTY_FOUND) &&
(tmp_int == 1)) { (tmp_int == 1)) {
data->storage.drive_type = strdup("sd"); if (VIR_STRDUP(data->storage.drive_type, "sd") < 0)
if (!data->storage.drive_type)
goto out; goto out;
} else { } else {
@ -1294,32 +1268,20 @@ static int udevSetParent(struct udev_device *device,
dev = virNodeDeviceFindBySysfsPath(&driverState->devs, dev = virNodeDeviceFindBySysfsPath(&driverState->devs,
parent_sysfs_path); parent_sysfs_path);
if (dev != NULL) { if (dev != NULL) {
def->parent = strdup(dev->def->name); if (VIR_STRDUP(def->parent, dev->def->name) < 0) {
virNodeDeviceObjUnlock(dev);
goto out;
}
virNodeDeviceObjUnlock(dev); virNodeDeviceObjUnlock(dev);
if (def->parent == NULL) { if (VIR_STRDUP(def->parent_sysfs_path, parent_sysfs_path) < 0)
virReportOOMError();
goto out; goto out;
} }
def->parent_sysfs_path = strdup(parent_sysfs_path);
if (def->parent_sysfs_path == NULL) {
virReportOOMError();
goto out;
}
}
} while (def->parent == NULL && parent_device != NULL); } while (def->parent == NULL && parent_device != NULL);
if (def->parent == NULL) { if (!def->parent && VIR_STRDUP(def->parent, "computer") < 0)
def->parent = strdup("computer");
}
if (def->parent == NULL) {
virReportOOMError();
goto out; goto out;
}
ret = 0; ret = 0;
@ -1339,7 +1301,9 @@ static int udevAddOneDevice(struct udev_device *device)
goto out; goto out;
} }
def->sysfs_path = strdup(udev_device_get_syspath(device)); if (VIR_STRDUP(def->sysfs_path, udev_device_get_syspath(device)) < 0)
goto out;
if (udevGetStringProperty(device, if (udevGetStringProperty(device,
"DRIVER", "DRIVER",
&def->driver) == PROPERTY_ERROR) { &def->driver) == PROPERTY_ERROR) {
@ -1379,7 +1343,7 @@ static int udevAddOneDevice(struct udev_device *device)
out: out:
if (ret != 0) { if (ret != 0) {
VIR_DEBUG("Discarding device %d %p %s", ret, def, VIR_DEBUG("Discarding device %d %p %s", ret, def,
def ? def->sysfs_path : ""); def ? NULLSTR(def->sysfs_path) : "");
virNodeDeviceDefFree(def); virNodeDeviceDefFree(def);
} }
@ -1617,11 +1581,8 @@ static int udevSetupSystemDev(void)
goto out; goto out;
} }
def->name = strdup("computer"); if (VIR_STRDUP(def->name, "computer") < 0)
if (def->name == NULL) {
virReportOOMError();
goto out; goto out;
}
if (VIR_ALLOC(def->caps) != 0) { if (VIR_ALLOC(def->caps) != 0) {
virReportOOMError(); virReportOOMError();