mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
util: process: 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
d00fc4178c
commit
ce814ed392
@ -158,7 +158,7 @@ virProcessAbort(pid_t pid)
|
|||||||
int saved_errno;
|
int saved_errno;
|
||||||
int ret;
|
int ret;
|
||||||
int status;
|
int status;
|
||||||
char *tmp = NULL;
|
VIR_AUTOFREE(char *) tmp = NULL;
|
||||||
|
|
||||||
if (pid <= 0)
|
if (pid <= 0)
|
||||||
return;
|
return;
|
||||||
@ -199,7 +199,6 @@ virProcessAbort(pid_t pid)
|
|||||||
VIR_DEBUG("failed to reap child %lld, abandoning it", (long long) pid);
|
VIR_DEBUG("failed to reap child %lld, abandoning it", (long long) pid);
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FREE(tmp);
|
|
||||||
errno = saved_errno;
|
errno = saved_errno;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@ -238,6 +237,7 @@ virProcessWait(pid_t pid, int *exitstatus, bool raw)
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
int status;
|
int status;
|
||||||
|
VIR_AUTOFREE(char *) st = NULL;
|
||||||
|
|
||||||
if (pid <= 0) {
|
if (pid <= 0) {
|
||||||
if (pid != -1)
|
if (pid != -1)
|
||||||
@ -270,13 +270,10 @@ virProcessWait(pid_t pid, int *exitstatus, bool raw)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
{
|
st = virProcessTranslateStatus(status);
|
||||||
char *st = virProcessTranslateStatus(status);
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
_("Child process (%lld) unexpected %s"),
|
||||||
_("Child process (%lld) unexpected %s"),
|
(long long) pid, NULLSTR(st));
|
||||||
(long long) pid, NULLSTR(st));
|
|
||||||
VIR_FREE(st);
|
|
||||||
}
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -603,10 +600,10 @@ virProcessGetAffinity(pid_t pid ATTRIBUTE_UNUSED)
|
|||||||
int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
|
int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
char *taskPath = NULL;
|
|
||||||
DIR *dir = NULL;
|
DIR *dir = NULL;
|
||||||
int value;
|
int value;
|
||||||
struct dirent *ent;
|
struct dirent *ent;
|
||||||
|
VIR_AUTOFREE(char *) taskPath = NULL;
|
||||||
|
|
||||||
*npids = 0;
|
*npids = 0;
|
||||||
*pids = NULL;
|
*pids = NULL;
|
||||||
@ -636,7 +633,6 @@ int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
|
|||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_DIR_CLOSE(dir);
|
VIR_DIR_CLOSE(dir);
|
||||||
VIR_FREE(taskPath);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
VIR_FREE(*pids);
|
VIR_FREE(*pids);
|
||||||
return ret;
|
return ret;
|
||||||
@ -648,7 +644,6 @@ int virProcessGetNamespaces(pid_t pid,
|
|||||||
int **fdlist)
|
int **fdlist)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
char *nsfile = NULL;
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
const char *ns[] = { "user", "ipc", "uts", "net", "pid", "mnt" };
|
const char *ns[] = { "user", "ipc", "uts", "net", "pid", "mnt" };
|
||||||
|
|
||||||
@ -657,6 +652,7 @@ int virProcessGetNamespaces(pid_t pid,
|
|||||||
|
|
||||||
for (i = 0; i < ARRAY_CARDINALITY(ns); i++) {
|
for (i = 0; i < ARRAY_CARDINALITY(ns); i++) {
|
||||||
int fd;
|
int fd;
|
||||||
|
VIR_AUTOFREE(char *) nsfile = NULL;
|
||||||
|
|
||||||
if (virAsprintf(&nsfile, "/proc/%llu/ns/%s",
|
if (virAsprintf(&nsfile, "/proc/%llu/ns/%s",
|
||||||
(long long) pid,
|
(long long) pid,
|
||||||
@ -671,14 +667,11 @@ int virProcessGetNamespaces(pid_t pid,
|
|||||||
|
|
||||||
(*fdlist)[(*nfdlist)-1] = fd;
|
(*fdlist)[(*nfdlist)-1] = fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
VIR_FREE(nsfile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
VIR_FREE(nsfile);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
for (i = 0; i < *nfdlist; i++)
|
for (i = 0; i < *nfdlist; i++)
|
||||||
VIR_FORCE_CLOSE((*fdlist)[i]);
|
VIR_FORCE_CLOSE((*fdlist)[i]);
|
||||||
@ -977,12 +970,12 @@ virProcessSetMaxCoreSize(pid_t pid ATTRIBUTE_UNUSED,
|
|||||||
int virProcessGetStartTime(pid_t pid,
|
int virProcessGetStartTime(pid_t pid,
|
||||||
unsigned long long *timestamp)
|
unsigned long long *timestamp)
|
||||||
{
|
{
|
||||||
char *filename = NULL;
|
|
||||||
char *buf = NULL;
|
|
||||||
char *tmp;
|
char *tmp;
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
int len;
|
int len;
|
||||||
char **tokens = NULL;
|
char **tokens = NULL;
|
||||||
|
VIR_AUTOFREE(char *) filename = NULL;
|
||||||
|
VIR_AUTOFREE(char *) buf = NULL;
|
||||||
|
|
||||||
if (virAsprintf(&filename, "/proc/%llu/stat", (long long) pid) < 0)
|
if (virAsprintf(&filename, "/proc/%llu/stat", (long long) pid) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
@ -1032,8 +1025,6 @@ int virProcessGetStartTime(pid_t pid,
|
|||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
virStringListFree(tokens);
|
virStringListFree(tokens);
|
||||||
VIR_FREE(filename);
|
|
||||||
VIR_FREE(buf);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||||
@ -1080,9 +1071,9 @@ static int virProcessNamespaceHelper(int errfd,
|
|||||||
virProcessNamespaceCallback cb,
|
virProcessNamespaceCallback cb,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
char *path;
|
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
VIR_AUTOFREE(char *) path = NULL;
|
||||||
|
|
||||||
if (virAsprintf(&path, "/proc/%lld/ns/mnt", (long long) pid) < 0)
|
if (virAsprintf(&path, "/proc/%lld/ns/mnt", (long long) pid) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -1109,7 +1100,6 @@ static int virProcessNamespaceHelper(int errfd,
|
|||||||
ignore_value(safewrite(errfd, err->message, len));
|
ignore_value(safewrite(errfd, err->message, len));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
VIR_FREE(path);
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -1145,8 +1135,8 @@ virProcessRunInMountNamespace(pid_t pid,
|
|||||||
VIR_FORCE_CLOSE(errfd[1]);
|
VIR_FORCE_CLOSE(errfd[1]);
|
||||||
_exit(ret < 0 ? EXIT_CANCELED : ret);
|
_exit(ret < 0 ? EXIT_CANCELED : ret);
|
||||||
} else {
|
} else {
|
||||||
char *buf = NULL;
|
|
||||||
int status;
|
int status;
|
||||||
|
VIR_AUTOFREE(char *) buf = NULL;
|
||||||
|
|
||||||
VIR_FORCE_CLOSE(errfd[1]);
|
VIR_FORCE_CLOSE(errfd[1]);
|
||||||
ignore_value(virFileReadHeaderFD(errfd[0], 1024, &buf));
|
ignore_value(virFileReadHeaderFD(errfd[0], 1024, &buf));
|
||||||
@ -1159,7 +1149,6 @@ virProcessRunInMountNamespace(pid_t pid,
|
|||||||
NULLSTR(buf));
|
NULLSTR(buf));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
VIR_FREE(buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
@ -1226,8 +1215,8 @@ virProcessNamespaceAvailable(unsigned int ns)
|
|||||||
int flags = 0;
|
int flags = 0;
|
||||||
int cpid;
|
int cpid;
|
||||||
char *childStack;
|
char *childStack;
|
||||||
char *stack;
|
|
||||||
int stacksize = getpagesize() * 4;
|
int stacksize = getpagesize() * 4;
|
||||||
|
VIR_AUTOFREE(char *)stack = NULL;
|
||||||
|
|
||||||
if (ns & VIR_PROCESS_NAMESPACE_MNT)
|
if (ns & VIR_PROCESS_NAMESPACE_MNT)
|
||||||
flags |= CLONE_NEWNS;
|
flags |= CLONE_NEWNS;
|
||||||
@ -1251,7 +1240,7 @@ virProcessNamespaceAvailable(unsigned int ns)
|
|||||||
childStack = stack + stacksize;
|
childStack = stack + stacksize;
|
||||||
|
|
||||||
cpid = clone(virProcessDummyChild, childStack, flags, NULL);
|
cpid = clone(virProcessDummyChild, childStack, flags, NULL);
|
||||||
VIR_FREE(stack);
|
|
||||||
if (cpid < 0) {
|
if (cpid < 0) {
|
||||||
char ebuf[1024] ATTRIBUTE_UNUSED;
|
char ebuf[1024] ATTRIBUTE_UNUSED;
|
||||||
VIR_DEBUG("clone call returned %s, container support is not enabled",
|
VIR_DEBUG("clone call returned %s, container support is not enabled",
|
||||||
|
Loading…
Reference in New Issue
Block a user