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