util: convert virIdentity implementation and test suite to g_autoptr

To simplify the later conversion from virObject to GObject, introduce
the use of g_autoptr to the virIdentity implementnation and test suite.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2019-10-01 17:38:12 +01:00
parent 6c748c8e2d
commit c6825d8813
2 changed files with 33 additions and 47 deletions

View File

@ -105,7 +105,7 @@ virIdentityPtr virIdentityGetCurrent(void)
*/ */
int virIdentitySetCurrent(virIdentityPtr ident) int virIdentitySetCurrent(virIdentityPtr ident)
{ {
virIdentityPtr old; g_autoptr(virIdentity) old = NULL;
if (virIdentityInitialize() < 0) if (virIdentityInitialize() < 0)
return -1; return -1;
@ -120,8 +120,6 @@ int virIdentitySetCurrent(virIdentityPtr ident)
return -1; return -1;
} }
virObjectUnref(old);
return 0; return 0;
} }
@ -136,60 +134,56 @@ int virIdentitySetCurrent(virIdentityPtr ident)
*/ */
virIdentityPtr virIdentityGetSystem(void) virIdentityPtr virIdentityGetSystem(void)
{ {
VIR_AUTOFREE(char *) username = NULL; g_autofree char *username = NULL;
VIR_AUTOFREE(char *) groupname = NULL; g_autofree char *groupname = NULL;
unsigned long long startTime; unsigned long long startTime;
virIdentityPtr ret = NULL; g_autoptr(virIdentity) ret = NULL;
#if WITH_SELINUX #if WITH_SELINUX
security_context_t con; security_context_t con;
#endif #endif
if (!(ret = virIdentityNew())) if (!(ret = virIdentityNew()))
goto error; return NULL;
if (virIdentitySetProcessID(ret, getpid()) < 0) if (virIdentitySetProcessID(ret, getpid()) < 0)
goto error; return NULL;
if (virProcessGetStartTime(getpid(), &startTime) < 0) if (virProcessGetStartTime(getpid(), &startTime) < 0)
goto error; return NULL;
if (startTime != 0 && if (startTime != 0 &&
virIdentitySetProcessTime(ret, startTime) < 0) virIdentitySetProcessTime(ret, startTime) < 0)
goto error; return NULL;
if (!(username = virGetUserName(geteuid()))) if (!(username = virGetUserName(geteuid())))
return ret; return ret;
if (virIdentitySetUserName(ret, username) < 0) if (virIdentitySetUserName(ret, username) < 0)
goto error; return NULL;
if (virIdentitySetUNIXUserID(ret, getuid()) < 0) if (virIdentitySetUNIXUserID(ret, getuid()) < 0)
goto error; return NULL;
if (!(groupname = virGetGroupName(getegid()))) if (!(groupname = virGetGroupName(getegid())))
return ret; return ret;
if (virIdentitySetGroupName(ret, groupname) < 0) if (virIdentitySetGroupName(ret, groupname) < 0)
goto error; return NULL;
if (virIdentitySetUNIXGroupID(ret, getgid()) < 0) if (virIdentitySetUNIXGroupID(ret, getgid()) < 0)
goto error; return NULL;
#if WITH_SELINUX #if WITH_SELINUX
if (is_selinux_enabled() > 0) { if (is_selinux_enabled() > 0) {
if (getcon(&con) < 0) { if (getcon(&con) < 0) {
virReportSystemError(errno, "%s", virReportSystemError(errno, "%s",
_("Unable to lookup SELinux process context")); _("Unable to lookup SELinux process context"));
return ret; return NULL;
} }
if (virIdentitySetSELinuxContext(ret, con) < 0) { if (virIdentitySetSELinuxContext(ret, con) < 0) {
freecon(con); freecon(con);
goto error; return NULL;
} }
freecon(con); freecon(con);
} }
#endif #endif
return ret; return g_steal_pointer(&ret);
error:
virObjectUnref(ret);
return NULL;
} }

View File

@ -38,58 +38,53 @@ VIR_LOG_INIT("tests.identitytest");
static int testIdentityAttrs(const void *data ATTRIBUTE_UNUSED) static int testIdentityAttrs(const void *data ATTRIBUTE_UNUSED)
{ {
int ret = -1; g_autoptr(virIdentity) ident = NULL;
virIdentityPtr ident;
const char *val; const char *val;
int rc; int rc;
if (!(ident = virIdentityNew())) if (!(ident = virIdentityNew()))
goto cleanup; return -1;
if (virIdentitySetUserName(ident, "fred") < 0) if (virIdentitySetUserName(ident, "fred") < 0)
goto cleanup; return -1;
if ((rc = virIdentityGetUserName(ident, &val)) < 0) if ((rc = virIdentityGetUserName(ident, &val)) < 0)
goto cleanup; return -1;
if (STRNEQ_NULLABLE(val, "fred") || rc != 1) { if (STRNEQ_NULLABLE(val, "fred") || rc != 1) {
VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val)); VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val));
goto cleanup; return -1;
} }
if ((rc = virIdentityGetGroupName(ident, &val)) < 0) if ((rc = virIdentityGetGroupName(ident, &val)) < 0)
goto cleanup; return -1;
if (val != NULL || rc != 0) { if (val != NULL || rc != 0) {
VIR_DEBUG("Unexpected groupname attribute"); VIR_DEBUG("Unexpected groupname attribute");
goto cleanup; return -1;
} }
if (virIdentitySetUserName(ident, "joe") >= 0) { if (virIdentitySetUserName(ident, "joe") >= 0) {
VIR_DEBUG("Unexpectedly overwrote attribute"); VIR_DEBUG("Unexpectedly overwrote attribute");
goto cleanup; return -1;
} }
if ((rc = virIdentityGetUserName(ident, &val)) < 0) if ((rc = virIdentityGetUserName(ident, &val)) < 0)
goto cleanup; return -1;
if (STRNEQ_NULLABLE(val, "fred") || rc != 1) { if (STRNEQ_NULLABLE(val, "fred") || rc != 1) {
VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val)); VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val));
goto cleanup; return -1;
} }
ret = 0; return 0;
cleanup:
virObjectUnref(ident);
return ret;
} }
static int testIdentityGetSystem(const void *data) static int testIdentityGetSystem(const void *data)
{ {
const char *context = data; const char *context = data;
int ret = -1; g_autoptr(virIdentity) ident = NULL;
virIdentityPtr ident = NULL;
const char *val; const char *val;
int rc; int rc;
@ -97,35 +92,32 @@ static int testIdentityGetSystem(const void *data)
if (context) { if (context) {
VIR_DEBUG("libvirt not compiled with SELinux, skipping this test"); VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
ret = EXIT_AM_SKIP; ret = EXIT_AM_SKIP;
goto cleanup; return -1;
} }
#endif #endif
if (!(ident = virIdentityGetSystem())) { if (!(ident = virIdentityGetSystem())) {
VIR_DEBUG("Unable to get system identity"); VIR_DEBUG("Unable to get system identity");
goto cleanup; return -1;
} }
if ((rc = virIdentityGetSELinuxContext(ident, &val)) < 0) if ((rc = virIdentityGetSELinuxContext(ident, &val)) < 0)
goto cleanup; return -1;
if (context == NULL) { if (context == NULL) {
if (val != NULL || rc != 0) { if (val != NULL || rc != 0) {
VIR_DEBUG("Unexpected SELinux context %s", NULLSTR(val)); VIR_DEBUG("Unexpected SELinux context %s", NULLSTR(val));
goto cleanup; return -1;
} }
} else { } else {
if (STRNEQ_NULLABLE(val, context) || rc != 1) { if (STRNEQ_NULLABLE(val, context) || rc != 1) {
VIR_DEBUG("Want SELinux context '%s' got '%s'", VIR_DEBUG("Want SELinux context '%s' got '%s'",
context, val); context, val);
goto cleanup; return -1;
} }
} }
ret = 0; return 0;
cleanup:
virObjectUnref(ident);
return ret;
} }
static int testSetFakeSELinuxContext(const void *data ATTRIBUTE_UNUSED) static int testSetFakeSELinuxContext(const void *data ATTRIBUTE_UNUSED)