mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
util: identity: use VIR_AUTOFREE instead of VIR_FREE for scalar types
By making use of GNU C's cleanup attribute handled by the VIR_AUTOFREE macro for declaring scalar variables, majority of the VIR_FREE calls can be dropped, which in turn leads to getting rid of most of our cleanup sections. Signed-off-by: Sukrit Bhatnagar <skrtbhtngr@gmail.com> Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
parent
c10ffc37a7
commit
e83da1990c
@ -133,8 +133,8 @@ int virIdentitySetCurrent(virIdentityPtr ident)
|
|||||||
*/
|
*/
|
||||||
virIdentityPtr virIdentityGetSystem(void)
|
virIdentityPtr virIdentityGetSystem(void)
|
||||||
{
|
{
|
||||||
char *username = NULL;
|
VIR_AUTOFREE(char *) username = NULL;
|
||||||
char *groupname = NULL;
|
VIR_AUTOFREE(char *) groupname = NULL;
|
||||||
unsigned long long startTime;
|
unsigned long long startTime;
|
||||||
virIdentityPtr ret = NULL;
|
virIdentityPtr ret = NULL;
|
||||||
#if WITH_SELINUX
|
#if WITH_SELINUX
|
||||||
@ -154,14 +154,14 @@ virIdentityPtr virIdentityGetSystem(void)
|
|||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (!(username = virGetUserName(geteuid())))
|
if (!(username = virGetUserName(geteuid())))
|
||||||
goto cleanup;
|
return ret;
|
||||||
if (virIdentitySetUNIXUserName(ret, username) < 0)
|
if (virIdentitySetUNIXUserName(ret, username) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
if (virIdentitySetUNIXUserID(ret, getuid()) < 0)
|
if (virIdentitySetUNIXUserID(ret, getuid()) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (!(groupname = virGetGroupName(getegid())))
|
if (!(groupname = virGetGroupName(getegid())))
|
||||||
goto cleanup;
|
return ret;
|
||||||
if (virIdentitySetUNIXGroupName(ret, groupname) < 0)
|
if (virIdentitySetUNIXGroupName(ret, groupname) < 0)
|
||||||
goto error;
|
goto error;
|
||||||
if (virIdentitySetUNIXGroupID(ret, getgid()) < 0)
|
if (virIdentitySetUNIXGroupID(ret, getgid()) < 0)
|
||||||
@ -172,7 +172,7 @@ virIdentityPtr virIdentityGetSystem(void)
|
|||||||
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"));
|
||||||
goto cleanup;
|
return ret;
|
||||||
}
|
}
|
||||||
if (virIdentitySetSELinuxContext(ret, con) < 0) {
|
if (virIdentitySetSELinuxContext(ret, con) < 0) {
|
||||||
freecon(con);
|
freecon(con);
|
||||||
@ -182,15 +182,11 @@ virIdentityPtr virIdentityGetSystem(void)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FREE(username);
|
|
||||||
VIR_FREE(groupname);
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
virObjectUnref(ret);
|
virObjectUnref(ret);
|
||||||
ret = NULL;
|
return NULL;
|
||||||
goto cleanup;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -461,15 +457,14 @@ int virIdentitySetUNIXUserName(virIdentityPtr ident,
|
|||||||
int virIdentitySetUNIXUserID(virIdentityPtr ident,
|
int virIdentitySetUNIXUserID(virIdentityPtr ident,
|
||||||
uid_t uid)
|
uid_t uid)
|
||||||
{
|
{
|
||||||
char *val;
|
VIR_AUTOFREE(char *) val = NULL;
|
||||||
int ret;
|
|
||||||
if (virAsprintf(&val, "%d", (int)uid) < 0)
|
if (virAsprintf(&val, "%d", (int)uid) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
ret = virIdentitySetAttr(ident,
|
|
||||||
|
return virIdentitySetAttr(ident,
|
||||||
VIR_IDENTITY_ATTR_UNIX_USER_ID,
|
VIR_IDENTITY_ATTR_UNIX_USER_ID,
|
||||||
val);
|
val);
|
||||||
VIR_FREE(val);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -485,45 +480,42 @@ int virIdentitySetUNIXGroupName(virIdentityPtr ident,
|
|||||||
int virIdentitySetUNIXGroupID(virIdentityPtr ident,
|
int virIdentitySetUNIXGroupID(virIdentityPtr ident,
|
||||||
gid_t gid)
|
gid_t gid)
|
||||||
{
|
{
|
||||||
char *val;
|
VIR_AUTOFREE(char *) val = NULL;
|
||||||
int ret;
|
|
||||||
if (virAsprintf(&val, "%d", (int)gid) < 0)
|
if (virAsprintf(&val, "%d", (int)gid) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
ret = virIdentitySetAttr(ident,
|
|
||||||
|
return virIdentitySetAttr(ident,
|
||||||
VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
|
VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
|
||||||
val);
|
val);
|
||||||
VIR_FREE(val);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int virIdentitySetUNIXProcessID(virIdentityPtr ident,
|
int virIdentitySetUNIXProcessID(virIdentityPtr ident,
|
||||||
pid_t pid)
|
pid_t pid)
|
||||||
{
|
{
|
||||||
char *val;
|
VIR_AUTOFREE(char *) val = NULL;
|
||||||
int ret;
|
|
||||||
if (virAsprintf(&val, "%lld", (long long) pid) < 0)
|
if (virAsprintf(&val, "%lld", (long long) pid) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
ret = virIdentitySetAttr(ident,
|
|
||||||
|
return virIdentitySetAttr(ident,
|
||||||
VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
|
VIR_IDENTITY_ATTR_UNIX_PROCESS_ID,
|
||||||
val);
|
val);
|
||||||
VIR_FREE(val);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int virIdentitySetUNIXProcessTime(virIdentityPtr ident,
|
int virIdentitySetUNIXProcessTime(virIdentityPtr ident,
|
||||||
unsigned long long timestamp)
|
unsigned long long timestamp)
|
||||||
{
|
{
|
||||||
char *val;
|
VIR_AUTOFREE(char *) val = NULL;
|
||||||
int ret;
|
|
||||||
if (virAsprintf(&val, "%llu", timestamp) < 0)
|
if (virAsprintf(&val, "%llu", timestamp) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
ret = virIdentitySetAttr(ident,
|
|
||||||
|
return virIdentitySetAttr(ident,
|
||||||
VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
|
VIR_IDENTITY_ATTR_UNIX_PROCESS_TIME,
|
||||||
val);
|
val);
|
||||||
VIR_FREE(val);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user