security_util: Use more VIR_AUTOFREE()

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
Michal Privoznik 2019-08-08 11:57:41 +02:00
parent 12da9f7ec6
commit 8ced0909e5

View File

@ -113,34 +113,32 @@ virSecurityGetRememberedLabel(const char *name,
const char *path, const char *path,
char **label) char **label)
{ {
char *ref_name = NULL; VIR_AUTOFREE(char *) ref_name = NULL;
char *attr_name = NULL; VIR_AUTOFREE(char *) attr_name = NULL;
char *value = NULL; VIR_AUTOFREE(char *) value = NULL;
unsigned int refcount = 0; unsigned int refcount = 0;
int ret = -1;
*label = NULL; *label = NULL;
if (!(ref_name = virSecurityGetRefCountAttrName(name))) if (!(ref_name = virSecurityGetRefCountAttrName(name)))
goto cleanup; return -1;
if (virFileGetXAttrQuiet(path, ref_name, &value) < 0) { if (virFileGetXAttrQuiet(path, ref_name, &value) < 0) {
if (errno == ENOSYS || errno == ENODATA || errno == ENOTSUP) { if (errno == ENOSYS || errno == ENODATA || errno == ENOTSUP)
ret = -2; return -2;
} else {
virReportSystemError(errno, virReportSystemError(errno,
_("Unable to get XATTR %s on %s"), _("Unable to get XATTR %s on %s"),
ref_name, ref_name,
path); path);
} return -1;
goto cleanup;
} }
if (virStrToLong_ui(value, NULL, 10, &refcount) < 0) { if (virStrToLong_ui(value, NULL, 10, &refcount) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("malformed refcount %s on %s"), _("malformed refcount %s on %s"),
value, path); value, path);
goto cleanup; return -1;
} }
VIR_FREE(value); VIR_FREE(value);
@ -149,30 +147,25 @@ virSecurityGetRememberedLabel(const char *name,
if (refcount > 0) { if (refcount > 0) {
if (virAsprintf(&value, "%u", refcount) < 0) if (virAsprintf(&value, "%u", refcount) < 0)
goto cleanup; return -1;
if (virFileSetXAttr(path, ref_name, value) < 0) if (virFileSetXAttr(path, ref_name, value) < 0)
goto cleanup; return -1;
} else { } else {
if (virFileRemoveXAttr(path, ref_name) < 0) if (virFileRemoveXAttr(path, ref_name) < 0)
goto cleanup; return -1;
if (!(attr_name = virSecurityGetAttrName(name))) if (!(attr_name = virSecurityGetAttrName(name)))
goto cleanup; return -1;
if (virFileGetXAttr(path, attr_name, label) < 0) if (virFileGetXAttr(path, attr_name, label) < 0)
goto cleanup; return -1;
if (virFileRemoveXAttr(path, attr_name) < 0) if (virFileRemoveXAttr(path, attr_name) < 0)
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
VIR_FREE(value);
VIR_FREE(attr_name);
VIR_FREE(ref_name);
return ret;
} }
@ -201,25 +194,23 @@ virSecuritySetRememberedLabel(const char *name,
const char *path, const char *path,
const char *label) const char *label)
{ {
char *ref_name = NULL; VIR_AUTOFREE(char *) ref_name = NULL;
char *attr_name = NULL; VIR_AUTOFREE(char *) attr_name = NULL;
char *value = NULL; VIR_AUTOFREE(char *) value = NULL;
unsigned int refcount = 0; unsigned int refcount = 0;
int ret = -1;
if (!(ref_name = virSecurityGetRefCountAttrName(name))) if (!(ref_name = virSecurityGetRefCountAttrName(name)))
goto cleanup; return -1;
if (virFileGetXAttrQuiet(path, ref_name, &value) < 0) { if (virFileGetXAttrQuiet(path, ref_name, &value) < 0) {
if (errno == ENOSYS || errno == ENOTSUP) { if (errno == ENOSYS || errno == ENOTSUP) {
ret = -2; return -2;
goto cleanup;
} else if (errno != ENODATA) { } else if (errno != ENODATA) {
virReportSystemError(errno, virReportSystemError(errno,
_("Unable to get XATTR %s on %s"), _("Unable to get XATTR %s on %s"),
ref_name, ref_name,
path); path);
goto cleanup; return -1;
} }
} }
@ -228,7 +219,7 @@ virSecuritySetRememberedLabel(const char *name,
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("malformed refcount %s on %s"), _("malformed refcount %s on %s"),
value, path); value, path);
goto cleanup; return -1;
} }
VIR_FREE(value); VIR_FREE(value);
@ -237,24 +228,19 @@ virSecuritySetRememberedLabel(const char *name,
if (refcount == 1) { if (refcount == 1) {
if (!(attr_name = virSecurityGetAttrName(name))) if (!(attr_name = virSecurityGetAttrName(name)))
goto cleanup; return -1;
if (virFileSetXAttr(path, attr_name, label) < 0) if (virFileSetXAttr(path, attr_name, label) < 0)
goto cleanup; return -1;
} }
if (virAsprintf(&value, "%u", refcount) < 0) if (virAsprintf(&value, "%u", refcount) < 0)
goto cleanup; return -1;
if (virFileSetXAttr(path, ref_name, value) < 0) if (virFileSetXAttr(path, ref_name, value) < 0)
goto cleanup; return -1;
ret = refcount; return refcount;
cleanup:
VIR_FREE(value);
VIR_FREE(attr_name);
VIR_FREE(ref_name);
return ret;
} }