提交 c6825d88 编写于 作者: D Daniel P. Berrangé

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: NJán Tomko <jtomko@redhat.com>
Signed-off-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 6c748c8e
......@@ -105,7 +105,7 @@ virIdentityPtr virIdentityGetCurrent(void)
*/
int virIdentitySetCurrent(virIdentityPtr ident)
{
virIdentityPtr old;
g_autoptr(virIdentity) old = NULL;
if (virIdentityInitialize() < 0)
return -1;
......@@ -120,8 +120,6 @@ int virIdentitySetCurrent(virIdentityPtr ident)
return -1;
}
virObjectUnref(old);
return 0;
}
......@@ -136,60 +134,56 @@ int virIdentitySetCurrent(virIdentityPtr ident)
*/
virIdentityPtr virIdentityGetSystem(void)
{
VIR_AUTOFREE(char *) username = NULL;
VIR_AUTOFREE(char *) groupname = NULL;
g_autofree char *username = NULL;
g_autofree char *groupname = NULL;
unsigned long long startTime;
virIdentityPtr ret = NULL;
g_autoptr(virIdentity) ret = NULL;
#if WITH_SELINUX
security_context_t con;
#endif
if (!(ret = virIdentityNew()))
goto error;
return NULL;
if (virIdentitySetProcessID(ret, getpid()) < 0)
goto error;
return NULL;
if (virProcessGetStartTime(getpid(), &startTime) < 0)
goto error;
return NULL;
if (startTime != 0 &&
virIdentitySetProcessTime(ret, startTime) < 0)
goto error;
return NULL;
if (!(username = virGetUserName(geteuid())))
return ret;
if (virIdentitySetUserName(ret, username) < 0)
goto error;
return NULL;
if (virIdentitySetUNIXUserID(ret, getuid()) < 0)
goto error;
return NULL;
if (!(groupname = virGetGroupName(getegid())))
return ret;
if (virIdentitySetGroupName(ret, groupname) < 0)
goto error;
return NULL;
if (virIdentitySetUNIXGroupID(ret, getgid()) < 0)
goto error;
return NULL;
#if WITH_SELINUX
if (is_selinux_enabled() > 0) {
if (getcon(&con) < 0) {
virReportSystemError(errno, "%s",
_("Unable to lookup SELinux process context"));
return ret;
return NULL;
}
if (virIdentitySetSELinuxContext(ret, con) < 0) {
freecon(con);
goto error;
return NULL;
}
freecon(con);
}
#endif
return ret;
error:
virObjectUnref(ret);
return NULL;
return g_steal_pointer(&ret);
}
......
......@@ -38,58 +38,53 @@ VIR_LOG_INIT("tests.identitytest");
static int testIdentityAttrs(const void *data ATTRIBUTE_UNUSED)
{
int ret = -1;
virIdentityPtr ident;
g_autoptr(virIdentity) ident = NULL;
const char *val;
int rc;
if (!(ident = virIdentityNew()))
goto cleanup;
return -1;
if (virIdentitySetUserName(ident, "fred") < 0)
goto cleanup;
return -1;
if ((rc = virIdentityGetUserName(ident, &val)) < 0)
goto cleanup;
return -1;
if (STRNEQ_NULLABLE(val, "fred") || rc != 1) {
VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val));
goto cleanup;
return -1;
}
if ((rc = virIdentityGetGroupName(ident, &val)) < 0)
goto cleanup;
return -1;
if (val != NULL || rc != 0) {
VIR_DEBUG("Unexpected groupname attribute");
goto cleanup;
return -1;
}
if (virIdentitySetUserName(ident, "joe") >= 0) {
VIR_DEBUG("Unexpectedly overwrote attribute");
goto cleanup;
return -1;
}
if ((rc = virIdentityGetUserName(ident, &val)) < 0)
goto cleanup;
return -1;
if (STRNEQ_NULLABLE(val, "fred") || rc != 1) {
VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val));
goto cleanup;
return -1;
}
ret = 0;
cleanup:
virObjectUnref(ident);
return ret;
return 0;
}
static int testIdentityGetSystem(const void *data)
{
const char *context = data;
int ret = -1;
virIdentityPtr ident = NULL;
g_autoptr(virIdentity) ident = NULL;
const char *val;
int rc;
......@@ -97,35 +92,32 @@ static int testIdentityGetSystem(const void *data)
if (context) {
VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
ret = EXIT_AM_SKIP;
goto cleanup;
return -1;
}
#endif
if (!(ident = virIdentityGetSystem())) {
VIR_DEBUG("Unable to get system identity");
goto cleanup;
return -1;
}
if ((rc = virIdentityGetSELinuxContext(ident, &val)) < 0)
goto cleanup;
return -1;
if (context == NULL) {
if (val != NULL || rc != 0) {
VIR_DEBUG("Unexpected SELinux context %s", NULLSTR(val));
goto cleanup;
return -1;
}
} else {
if (STRNEQ_NULLABLE(val, context) || rc != 1) {
VIR_DEBUG("Want SELinux context '%s' got '%s'",
context, val);
goto cleanup;
return -1;
}
}
ret = 0;
cleanup:
virObjectUnref(ident);
return ret;
return 0;
}
static int testSetFakeSELinuxContext(const void *data ATTRIBUTE_UNUSED)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册