[libvirt] [PATCH v2 32/35] util: process: use VIR_AUTOFREE instead of VIR_FREE for scalar types

Sukrit Bhatnagar skrtbhtngr at gmail.com
Sun Aug 5 20:43:59 UTC 2018


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 at gmail.com>
---
 src/util/virprocess.c | 39 ++++++++++++++-------------------------
 1 file changed, 14 insertions(+), 25 deletions(-)

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




More information about the libvir-list mailing list