[PATCH 12/13] lxcContainerSetReadOnly: Refactor cleanup handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Turn 'mounts' into a proper GStrv after sorting so that automatic
cleanup can be used and shuffle around the cleanup steps so that jumps
can be avoided in favor of direct return of error code.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/lxc/lxc_container.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index 1cadfe70e0..d788e77196 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -773,8 +773,7 @@ static int lxcContainerSetReadOnly(void)
     FILE *procmnt;
     struct mntent mntent;
     char mntbuf[1024];
-    int ret = -1;
-    char **mounts = NULL;
+    g_auto(GStrv) mounts = NULL;
     size_t nmounts = 0;
     size_t i;

@@ -797,13 +796,16 @@ static int lxcContainerSetReadOnly(void)
         VIR_APPEND_ELEMENT(mounts, nmounts, tmp);
     }

-    if (!mounts) {
-        ret = 0;
-        goto cleanup;
-    }
+    endmntent(procmnt);
+
+    if (!mounts)
+        return 0;
+
+    qsort(mounts, nmounts, sizeof(mounts[0]), virStringSortRevCompare);

-    qsort(mounts, nmounts, sizeof(mounts[0]),
-          virStringSortRevCompare);
+    /* turn 'mounts' into a proper GStrv */
+    VIR_EXPAND_N(mounts, nmounts, 1);
+    nmounts--;

     for (i = 0; i < nmounts; i++) {
         VIR_DEBUG("Bind readonly %s", mounts[i]);
@@ -811,16 +813,11 @@ static int lxcContainerSetReadOnly(void)
             virReportSystemError(errno,
                                  _("Failed to make mount %s readonly"),
                                  mounts[i]);
-            goto cleanup;
+            return -1;
         }
     }

-    ret = 0;
- cleanup:
-    virStringListFreeCount(mounts, nmounts);
-    endmntent(procmnt);
-    return ret;
-
+    return 0;
 }


-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux