c480ed
From d7a8cd2fdafd168eb1672e7ecde1700463792518 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <d7a8cd2fdafd168eb1672e7ecde1700463792518@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:05:50 +0200
c480ed
Subject: [PATCH] vircgroup: Extract file link resolving into separate function
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit 00a0085aa17677003e89cdafceb6a0260af43fb8)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <6d3c5594a53824ceac258a0f54162ec8af5aef37.1561993099.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroup.c | 85 +++++++++++++++++++++++++-------------------
c480ed
 1 file changed, 48 insertions(+), 37 deletions(-)
c480ed
c480ed
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c480ed
index 5144b33d43..8ddedbfe7c 100644
c480ed
--- a/src/util/vircgroup.c
c480ed
+++ b/src/util/vircgroup.c
c480ed
@@ -356,6 +356,51 @@ virCgroupCopyMounts(virCgroupPtr group,
c480ed
 }
c480ed
 
c480ed
 
c480ed
+static int
c480ed
+virCgroupResolveMountLink(char *mntDir,
c480ed
+                          const char *typeStr,
c480ed
+                          virCgroupControllerPtr controller)
c480ed
+{
c480ed
+    VIR_AUTOFREE(char *) linkSrc = NULL;
c480ed
+    char *dirName;
c480ed
+    struct stat sb;
c480ed
+
c480ed
+    dirName = strrchr(mntDir, '/');
c480ed
+    if (!dirName) {
c480ed
+        virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
+                       _("Missing '/' separator in cgroup mount '%s'"), mntDir);
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    if (!strchr(dirName + 1, ','))
c480ed
+        return 0;
c480ed
+
c480ed
+    *dirName = '\0';
c480ed
+    if (virAsprintf(&linkSrc, "%s/%s", mntDir, typeStr) < 0)
c480ed
+        return -1;
c480ed
+    *dirName = '/';
c480ed
+
c480ed
+    if (lstat(linkSrc, &sb) < 0) {
c480ed
+        if (errno == ENOENT) {
c480ed
+            VIR_WARN("Controller %s co-mounted at %s is missing symlink at %s",
c480ed
+                     typeStr, mntDir, linkSrc);
c480ed
+        } else {
c480ed
+            virReportSystemError(errno, _("Cannot stat %s"), linkSrc);
c480ed
+            return -1;
c480ed
+        }
c480ed
+    } else {
c480ed
+        if (!S_ISLNK(sb.st_mode)) {
c480ed
+            VIR_WARN("Expecting a symlink at %s for controller %s",
c480ed
+                     linkSrc, typeStr);
c480ed
+        } else {
c480ed
+            VIR_STEAL_PTR(controller->linkPoint, linkSrc);
c480ed
+        }
c480ed
+    }
c480ed
+
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 /*
c480ed
  * Process /proc/mounts figuring out what controllers are
c480ed
  * mounted and where
c480ed
@@ -397,8 +442,6 @@ virCgroupDetectMountsFromFile(virCgroupPtr group,
c480ed
                 }
c480ed
 
c480ed
                 if (typelen == len && STREQLEN(typestr, tmp, len)) {
c480ed
-                    struct stat sb;
c480ed
-                    char *tmp2;
c480ed
 
c480ed
                     /* Note that the lines in /proc/mounts have the same
c480ed
                      * order than the mount operations, and that there may
c480ed
@@ -412,44 +455,12 @@ virCgroupDetectMountsFromFile(virCgroupPtr group,
c480ed
                     if (VIR_STRDUP(controller->mountPoint, entry.mnt_dir) < 0)
c480ed
                         goto cleanup;
c480ed
 
c480ed
-                    tmp2 = strrchr(entry.mnt_dir, '/');
c480ed
-                    if (!tmp2) {
c480ed
-                        virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
-                                       _("Missing '/' separator in cgroup mount '%s'"),
c480ed
-                                       entry.mnt_dir);
c480ed
-                        goto cleanup;
c480ed
-                    }
c480ed
-
c480ed
                     /* If it is a co-mount it has a filename like "cpu,cpuacct"
c480ed
                      * and we must identify the symlink path */
c480ed
-                    if (checkLinks && strchr(tmp2 + 1, ',')) {
c480ed
-                        VIR_AUTOFREE(char *) linksrc = NULL;
c480ed
-
c480ed
-                        *tmp2 = '\0';
c480ed
-                        if (virAsprintf(&linksrc, "%s/%s",
c480ed
-                                        entry.mnt_dir, typestr) < 0)
c480ed
+                    if (checkLinks &&
c480ed
+                        virCgroupResolveMountLink(entry.mnt_dir, typestr,
c480ed
+                                                  controller) < 0) {
c480ed
                             goto cleanup;
c480ed
-                        *tmp2 = '/';
c480ed
-
c480ed
-                        if (lstat(linksrc, &sb) < 0) {
c480ed
-                            if (errno == ENOENT) {
c480ed
-                                VIR_WARN("Controller %s co-mounted at %s is missing symlink at %s",
c480ed
-                                         typestr, entry.mnt_dir, linksrc);
c480ed
-                            } else {
c480ed
-                                virReportSystemError(errno,
c480ed
-                                                     _("Cannot stat %s"),
c480ed
-                                                     linksrc);
c480ed
-                                goto cleanup;
c480ed
-                            }
c480ed
-                        } else {
c480ed
-                            if (!S_ISLNK(sb.st_mode)) {
c480ed
-                                VIR_WARN("Expecting a symlink at %s for controller %s",
c480ed
-                                         linksrc, typestr);
c480ed
-                            } else {
c480ed
-                                controller->linkPoint = linksrc;
c480ed
-                                linksrc = NULL;
c480ed
-                            }
c480ed
-                        }
c480ed
                     }
c480ed
                 }
c480ed
                 tmp = next;
c480ed
-- 
c480ed
2.22.0
c480ed