[PATCH v3 3/5] lxc: Replacing default strings definitions by g_autofree statement.

Julio Faracco jcfaracco at gmail.com
Mon Feb 24 04:09:43 UTC 2020


There are a lots of strings being handled inside some LXC functions.
They can be moved to g_autofree to avoid declaring a return value to get
proper code cleanups. This commit is changing functions from
lxc_{controller,cgroup,fuse} only.

Signed-off-by: Julio Faracco <jcfaracco at gmail.com>
---
 src/lxc/lxc_cgroup.c     | 15 +++----
 src/lxc/lxc_controller.c | 96 ++++++++++++++--------------------------
 src/lxc/lxc_fuse.c       | 23 +++-------
 3 files changed, 44 insertions(+), 90 deletions(-)

diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 997a5c3dfa..d29b65092a 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -54,8 +54,7 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def,
                                        virCgroupPtr cgroup,
                                        virBitmapPtr nodemask)
 {
-    int ret = -1;
-    char *mask = NULL;
+    g_autofree char *mask = NULL;
     virDomainNumatuneMemMode mode;
 
     if (def->placement_mode != VIR_DOMAIN_CPU_PLACEMENT_MODE_AUTO &&
@@ -66,21 +65,17 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def,
 
     if (virDomainNumatuneGetMode(def->numa, -1, &mode) < 0 ||
         mode == VIR_DOMAIN_NUMATUNE_MEM_STRICT) {
-        ret = 0;
-        goto cleanup;
+        return 0;
     }
 
     if (virDomainNumatuneMaybeFormatNodeset(def->numa, nodemask,
                                             &mask, -1) < 0)
-        goto cleanup;
+        return -1;
 
     if (mask && virCgroupSetCpusetMems(cgroup, mask) < 0)
-        goto cleanup;
+        return -1;
 
-    ret = 0;
- cleanup:
-    VIR_FREE(mask);
-    return ret;
+    return 0;
 }
 
 
diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c
index 518967ee83..c580b17f5f 100644
--- a/src/lxc/lxc_controller.c
+++ b/src/lxc/lxc_controller.c
@@ -802,8 +802,7 @@ static int virLXCControllerGetNumadAdvice(virLXCControllerPtr ctrl,
                                           virBitmapPtr *mask)
 {
     virBitmapPtr nodemask = NULL;
-    char *nodeset = NULL;
-    int ret = -1;
+    g_autofree char *nodeset = NULL;
 
     /* Get the advisory nodeset from numad if 'placement' of
      * either <vcpu> or <numatune> is 'auto'.
@@ -812,20 +811,17 @@ static int virLXCControllerGetNumadAdvice(virLXCControllerPtr ctrl,
         nodeset = virNumaGetAutoPlacementAdvice(virDomainDefGetVcpus(ctrl->def),
                                                 ctrl->def->mem.cur_balloon);
         if (!nodeset)
-            goto cleanup;
+            return -1;
 
         VIR_DEBUG("Nodeset returned from numad: %s", nodeset);
 
         if (virBitmapParse(nodeset, &nodemask, VIR_DOMAIN_CPUMASK_LEN) < 0)
-            goto cleanup;
+            return -1;
     }
 
-    ret = 0;
     *mask = nodemask;
 
- cleanup:
-    VIR_FREE(nodeset);
-    return ret;
+    return 0;
 }
 
 
@@ -1434,9 +1430,8 @@ virLXCControllerSetupUsernsMap(virDomainIdMapEntryPtr map,
  */
 static int virLXCControllerSetupUserns(virLXCControllerPtr ctrl)
 {
-    char *uid_map = NULL;
-    char *gid_map = NULL;
-    int ret = -1;
+    g_autofree char *uid_map = NULL;
+    g_autofree char *gid_map = NULL;
 
     /* User namespace is disabled for container */
     if (ctrl->def->idmap.nuidmap == 0) {
@@ -1450,28 +1445,23 @@ static int virLXCControllerSetupUserns(virLXCControllerPtr ctrl)
     if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.uidmap,
                                        ctrl->def->idmap.nuidmap,
                                        uid_map) < 0)
-        goto cleanup;
+        return -1;
 
     gid_map = g_strdup_printf("/proc/%d/gid_map", ctrl->initpid);
 
     if (virLXCControllerSetupUsernsMap(ctrl->def->idmap.gidmap,
                                        ctrl->def->idmap.ngidmap,
                                        gid_map) < 0)
-        goto cleanup;
+        return -1;
 
-    ret = 0;
- cleanup:
-    VIR_FREE(uid_map);
-    VIR_FREE(gid_map);
-    return ret;
+    return 0;
 }
 
 static int virLXCControllerSetupDev(virLXCControllerPtr ctrl)
 {
-    char *mount_options = NULL;
-    char *opts = NULL;
-    char *dev = NULL;
-    int ret = -1;
+    g_autofree char *mount_options = NULL;
+    g_autofree char *opts = NULL;
+    g_autofree char *dev = NULL;
 
     VIR_DEBUG("Setting up /dev/ for container");
 
@@ -1488,24 +1478,18 @@ static int virLXCControllerSetupDev(virLXCControllerPtr ctrl)
     opts = g_strdup_printf("mode=755,size=65536%s", mount_options);
 
     if (virFileSetupDev(dev, opts) < 0)
-        goto cleanup;
+        return -1;
 
     if (lxcContainerChown(ctrl->def, dev) < 0)
-        goto cleanup;
+        return -1;
 
-    ret = 0;
- cleanup:
-    VIR_FREE(opts);
-    VIR_FREE(mount_options);
-    VIR_FREE(dev);
-    return ret;
+    return 0;
 }
 
 static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl)
 {
     size_t i;
-    int ret = -1;
-    char *path = NULL;
+    g_autofree char *path = NULL;
     const struct {
         int maj;
         int min;
@@ -1521,7 +1505,7 @@ static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl)
     };
 
     if (virLXCControllerSetupDev(ctrl) < 0)
-        goto cleanup;
+        return -1;
 
     /* Populate /dev/ with a few important bits */
     for (i = 0; i < G_N_ELEMENTS(devs); i++) {
@@ -1534,19 +1518,14 @@ static int virLXCControllerPopulateDevices(virLXCControllerPtr ctrl)
             virReportSystemError(errno,
                                  _("Failed to make device %s"),
                                  path);
-            goto cleanup;
+            return -1;
         }
 
         if (lxcContainerChown(ctrl->def, path) < 0)
-            goto cleanup;
-
-        VIR_FREE(path);
+            return -1;
     }
 
-    ret = 0;
- cleanup:
-    VIR_FREE(path);
-    return ret;
+    return 0;
 }
 
 
@@ -2202,10 +2181,9 @@ virLXCControllerSetupPrivateNS(void)
 static int
 virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl)
 {
-    char *mount_options = NULL;
-    char *opts = NULL;
-    char *devpts = NULL;
-    int ret = -1;
+    g_autofree char *mount_options = NULL;
+    g_autofree char *opts = NULL;
+    g_autofree char *devpts = NULL;
     gid_t ptsgid = 5;
 
     VIR_DEBUG("Setting up private /dev/pts");
@@ -2220,7 +2198,7 @@ virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl)
         virReportSystemError(errno,
                              _("Failed to make path %s"),
                              devpts);
-        goto cleanup;
+        return -1;
     }
 
     if (ctrl->def->idmap.ngidmap)
@@ -2239,26 +2217,20 @@ virLXCControllerSetupDevPTS(virLXCControllerPtr ctrl)
         virReportSystemError(errno,
                              _("Failed to mount devpts on %s"),
                              devpts);
-        goto cleanup;
+        return -1;
     }
 
     if (access(ctrl->devptmx, R_OK) < 0) {
         virReportSystemError(ENOSYS, "%s",
                              _("Kernel does not support private devpts"));
-        goto cleanup;
+        return -1;
     }
 
     if ((lxcContainerChown(ctrl->def, ctrl->devptmx) < 0) ||
         (lxcContainerChown(ctrl->def, devpts) < 0))
-        goto cleanup;
-
-    ret = 0;
+        return -1;
 
- cleanup:
-    VIR_FREE(opts);
-    VIR_FREE(devpts);
-    VIR_FREE(mount_options);
-    return ret;
+    return 0;
 }
 
 
@@ -2279,8 +2251,7 @@ virLXCControllerSetupConsoles(virLXCControllerPtr ctrl,
                               char **containerTTYPaths)
 {
     size_t i;
-    int ret = -1;
-    char *ttyHostPath = NULL;
+    g_autofree char *ttyHostPath = NULL;
 
     for (i = 0; i < ctrl->nconsoles; i++) {
         VIR_DEBUG("Opening tty on private %s", ctrl->devptmx);
@@ -2289,20 +2260,17 @@ virLXCControllerSetupConsoles(virLXCControllerPtr ctrl,
                          &containerTTYPaths[i], &ttyHostPath) < 0) {
             virReportSystemError(errno, "%s",
                                  _("Failed to allocate tty"));
-            goto cleanup;
+            return -1;
         }
 
         /* Change the owner of tty device to the root user of container */
         if (lxcContainerChown(ctrl->def, ttyHostPath) < 0)
-            goto cleanup;
+            return -1;
 
         VIR_FREE(ttyHostPath);
     }
 
-    ret = 0;
- cleanup:
-    VIR_FREE(ttyHostPath);
-    return ret;
+    return 0;
 }
 
 
diff --git a/src/lxc/lxc_fuse.c b/src/lxc/lxc_fuse.c
index 44f240a0b5..8cfccdd7e0 100644
--- a/src/lxc/lxc_fuse.c
+++ b/src/lxc/lxc_fuse.c
@@ -40,8 +40,7 @@ static const char *fuse_meminfo_path = "/meminfo";
 
 static int lxcProcGetattr(const char *path, struct stat *stbuf)
 {
-    int res;
-    char *mempath = NULL;
+    g_autofree char *mempath = NULL;
     struct stat sb;
     struct fuse_context *context = fuse_get_context();
     virDomainDefPtr def = (virDomainDefPtr)context->private_data;
@@ -49,16 +48,12 @@ static int lxcProcGetattr(const char *path, struct stat *stbuf)
     memset(stbuf, 0, sizeof(struct stat));
     mempath = g_strdup_printf("/proc/%s", path);
 
-    res = 0;
-
     if (STREQ(path, "/")) {
         stbuf->st_mode = S_IFDIR | 0755;
         stbuf->st_nlink = 2;
     } else if (STREQ(path, fuse_meminfo_path)) {
-        if (stat(mempath, &sb) < 0) {
-            res = -errno;
-            goto cleanup;
-        }
+        if (stat(mempath, &sb) < 0)
+            return -errno;
 
         stbuf->st_uid = def->idmap.uidmap ? def->idmap.uidmap[0].target : 0;
         stbuf->st_gid = def->idmap.gidmap ? def->idmap.gidmap[0].target : 0;
@@ -71,12 +66,10 @@ static int lxcProcGetattr(const char *path, struct stat *stbuf)
         stbuf->st_ctime = sb.st_ctime;
         stbuf->st_mtime = sb.st_mtime;
     } else {
-        res = -ENOENT;
+        return -ENOENT;
     }
 
- cleanup:
-    VIR_FREE(mempath);
-    return res;
+    return 0;
 }
 
 static int lxcProcReaddir(const char *path, void *buf,
@@ -127,7 +120,7 @@ static int lxcProcReadMeminfo(char *hostpath, virDomainDefPtr def,
 {
     int res;
     FILE *fd = NULL;
-    char *line = NULL;
+    g_autofree char *line = NULL;
     size_t n;
     struct virLXCMeminfo meminfo;
     virBuffer buffer = VIR_BUFFER_INITIALIZER;
@@ -229,7 +222,6 @@ static int lxcProcReadMeminfo(char *hostpath, virDomainDefPtr def,
     memcpy(buf, virBufferCurrentContent(new_meminfo), res);
 
  cleanup:
-    VIR_FREE(line);
     virBufferFreeAndReset(new_meminfo);
     VIR_FORCE_FCLOSE(fd);
     return res;
@@ -242,7 +234,7 @@ static int lxcProcRead(const char *path G_GNUC_UNUSED,
                        struct fuse_file_info *fi G_GNUC_UNUSED)
 {
     int res = -ENOENT;
-    char *hostpath = NULL;
+    g_autofree char *hostpath = NULL;
     struct fuse_context *context = NULL;
     virDomainDefPtr def = NULL;
 
@@ -256,7 +248,6 @@ static int lxcProcRead(const char *path G_GNUC_UNUSED,
             res = lxcProcHostRead(hostpath, buf, size, offset);
     }
 
-    VIR_FREE(hostpath);
     return res;
 }
 
-- 
2.20.1





More information about the libvir-list mailing list