Fix error detection in virStorageBackendISCSIGetHostNumber

In the unlikely case the iSCSI session path exists, but does not
contain an entry starting with "target", we would silently use
an initialized value.

Rewrite the function to correctly report errors.
This commit is contained in:
Ján Tomko 2016-06-21 15:13:41 +02:00
parent 0f79480b9f
commit 83066f99a9

View File

@ -91,7 +91,7 @@ static int
virStorageBackendISCSIGetHostNumber(const char *sysfs_path, virStorageBackendISCSIGetHostNumber(const char *sysfs_path,
uint32_t *host) uint32_t *host)
{ {
int retval = 0; int ret = -1;
DIR *sysdir = NULL; DIR *sysdir = NULL;
struct dirent *dirent = NULL; struct dirent *dirent = NULL;
int direrr; int direrr;
@ -100,27 +100,32 @@ virStorageBackendISCSIGetHostNumber(const char *sysfs_path,
virFileWaitForDevices(); virFileWaitForDevices();
if (virDirOpen(&sysdir, sysfs_path) < 0) { if (virDirOpen(&sysdir, sysfs_path) < 0)
retval = -1; goto cleanup;
goto out;
}
while ((direrr = virDirRead(sysdir, &dirent, sysfs_path)) > 0) { while ((direrr = virDirRead(sysdir, &dirent, sysfs_path)) > 0) {
if (STRPREFIX(dirent->d_name, "target")) { if (STRPREFIX(dirent->d_name, "target")) {
if (sscanf(dirent->d_name, if (sscanf(dirent->d_name, "target%u:", host) == 1) {
"target%u:", host) != 1) { ret = 0;
VIR_DEBUG("Failed to parse target '%s'", dirent->d_name); goto cleanup;
retval = -1; } else {
break; virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to parse target '%s'"), dirent->d_name);
goto cleanup;
} }
} }
} }
if (direrr < 0)
retval = -1;
if (direrr == 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to get host number for iSCSI session "
"with path '%s'"), sysfs_path);
goto cleanup;
}
cleanup:
VIR_DIR_CLOSE(sysdir); VIR_DIR_CLOSE(sysdir);
out: return ret;
return retval;
} }
static int static int
@ -135,13 +140,8 @@ virStorageBackendISCSIFindLUs(virStoragePoolObjPtr pool,
"/sys/class/iscsi_session/session%s/device", session) < 0) "/sys/class/iscsi_session/session%s/device", session) < 0)
goto cleanup; goto cleanup;
if (virStorageBackendISCSIGetHostNumber(sysfs_path, &host) < 0) { if (virStorageBackendISCSIGetHostNumber(sysfs_path, &host) < 0)
virReportSystemError(errno,
_("Failed to get host number for iSCSI session "
"with path '%s'"),
sysfs_path);
goto cleanup; goto cleanup;
}
if (virStorageBackendSCSIFindLUs(pool, host) < 0) if (virStorageBackendSCSIFindLUs(pool, host) < 0)
goto cleanup; goto cleanup;