[lxc-devel] [lxcfs/master] tree-wide: add and use must_make_path_relative()
brauner on Github
lxc-bot at linuxcontainers.org
Mon Mar 16 21:18:03 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 365 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200316/dc9af17e/attachment.bin>
-------------- next part --------------
From 925d5849986f5a3d5a819eecdbaa066c94a2d1bb Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Mon, 16 Mar 2020 22:16:51 +0100
Subject: [PATCH] tree-wide: add and use must_make_path_relative()
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
src/cgroups/cgfsng.c | 29 +++++------------------------
src/cgroups/cgroup_utils.h | 10 ++++++++++
src/proc_cpuview.c | 6 +-----
3 files changed, 16 insertions(+), 29 deletions(-)
diff --git a/src/cgroups/cgfsng.c b/src/cgroups/cgfsng.c
index 4d4d4b2..8d3c53d 100644
--- a/src/cgroups/cgfsng.c
+++ b/src/cgroups/cgfsng.c
@@ -537,11 +537,7 @@ static bool cgfsng_get(struct cgroup_ops *ops, const char *controller,
if (!h)
return false;
- if (is_relative(cgroup))
- path = must_make_path(cgroup, file, NULL);
- else
- path = must_make_path(".", cgroup, file, NULL);
-
+ path = must_make_path_relative(cgroup, file, NULL);
*value = readat_file(h->fd, path);
return *value != NULL;
}
@@ -571,11 +567,7 @@ static int cgfsng_get_memory(struct cgroup_ops *ops, const char *cgroup,
ret = CGROUP2_SUPER_MAGIC;
}
- if (is_relative(cgroup))
- path = must_make_path(cgroup, file, NULL);
- else
- path = must_make_path(".", cgroup, file, NULL);
-
+ path = must_make_path_relative(cgroup, file, NULL);
*value = readat_file(h->fd, path);
if (!*value)
ret = -1;
@@ -592,11 +584,7 @@ static int cgfsng_get_memory_stats_fd(struct cgroup_ops *ops, const char *cgroup
if (!h)
return -1;
- if (is_relative(cgroup))
- path = must_make_path(cgroup, "memory.stat", NULL);
- else
- path = must_make_path(".", cgroup, "memory.stat", NULL);
-
+ path = must_make_path_relative(cgroup, "memory.stat", NULL);
return openat(h->fd, path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
}
@@ -665,10 +653,7 @@ static int cgfsng_get_cpuset_cpus(struct cgroup_ops *ops, const char *cgroup,
ret = CGROUP2_SUPER_MAGIC;
*value = NULL;
- if (is_relative(cgroup))
- path = must_make_path(cgroup, NULL);
- else
- path = must_make_path(".", cgroup, NULL);
+ path = must_make_path_relative(cgroup, NULL);
cgroup_fd = openat_safe(h->fd, path);
if (cgroup_fd < 0) {
return -1;
@@ -718,11 +703,7 @@ static int cgfsng_get_io(struct cgroup_ops *ops, const char *cgroup,
else
ret = CGROUP2_SUPER_MAGIC;
- if (is_relative(cgroup))
- path = must_make_path(cgroup, file, NULL);
- else
- path = must_make_path(".", cgroup, file, NULL);
-
+ path = must_make_path_relative(cgroup, file, NULL);
*value = readat_file(h->fd, path);
if (!*value) {
if (errno == ENOENT)
diff --git a/src/cgroups/cgroup_utils.h b/src/cgroups/cgroup_utils.h
index 35e5a98..e626e3f 100644
--- a/src/cgroups/cgroup_utils.h
+++ b/src/cgroups/cgroup_utils.h
@@ -107,4 +107,14 @@ static inline bool is_relative(const char *s)
return s && *s != '/';
}
+#define must_make_path_relative(__first__, ...) \
+ ({ \
+ char *__ptr__; \
+ if (*__first__ == '/') \
+ __ptr__ = must_make_path(".", __first__, __VA_ARGS__); \
+ else \
+ __ptr__ = must_make_path(__first__, __VA_ARGS__); \
+ __ptr__; \
+ })
+
#endif /* __LXC_CGROUP_UTILS_H */
diff --git a/src/proc_cpuview.c b/src/proc_cpuview.c
index 129e192..5c21bc4 100644
--- a/src/proc_cpuview.c
+++ b/src/proc_cpuview.c
@@ -243,11 +243,7 @@ static bool cgfs_param_exist(const char *controller, const char *cgroup,
if (cfd < 0)
return false;
- if (is_relative(cgroup))
- path = must_make_path(cgroup, file, NULL);
- else
- path = must_make_path(".", cgroup, file, NULL);
-
+ path = must_make_path_relative(cgroup, file, NULL);
return (faccessat(cfd, path, F_OK, 0) == 0);
}
More information about the lxc-devel
mailing list