[LTP] [PATCH v3 2/3] tst_cgroup: Add safe_cg_open and safe_cg_fchown functions
Yang Xu
xuyang2018.jy@fujitsu.com
Tue Aug 23 13:01:18 CEST 2022
safe_cg_open is used to open the sub control's file ie cgroup.procs
and returns the opened fd number. The opened fd array is stored in
fds argument.
safe_cg_fchown is used to use fchownat to change file's uid and gid.
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
include/tst_cgroup.h | 44 +++++++++++++++++++++++++++++
lib/tst_cgroup.c | 66 +++++++++++++++++++++++++++-----------------
2 files changed, 85 insertions(+), 25 deletions(-)
diff --git a/include/tst_cgroup.h b/include/tst_cgroup.h
index d06847cc6..70c5b3fd4 100644
--- a/include/tst_cgroup.h
+++ b/include/tst_cgroup.h
@@ -89,6 +89,32 @@ enum tst_cg_ver {
TST_CG_V2 = 2,
};
+/* Controller sub-systems */
+enum cgroup_ctrl_indx {
+ CTRL_MEMORY = 1,
+ CTRL_CPU,
+ CTRL_CPUSET,
+ CTRL_IO,
+ CTRL_PIDS,
+ CTRL_HUGETLB,
+ CTRL_CPUACCT,
+ CTRL_DEVICES,
+ CTRL_FREEZER,
+ CTRL_NETCLS,
+ CTRL_NETPRIO,
+ CTRL_BLKIO,
+ CTRL_MISC,
+ CTRL_PERFEVENT,
+ CTRL_DEBUG
+};
+
+#define CTRLS_MAX CTRL_DEBUG
+
+/* At most we can have one cgroup V1 tree for each controller and one
+ * (empty) v2 tree.
+ */
+#define ROOTS_MAX (CTRLS_MAX + 1)
+
/* Used to specify CGroup hierarchy configuration options, allowing a
* test to request a particular CGroup structure.
*/
@@ -201,6 +227,24 @@ void safe_cg_printf(const char *const file, const int lineno,
const char *const fmt, ...)
__attribute__ ((format (printf, 5, 6), nonnull));
+#define SAFE_CG_OPEN(cg, file_name, flags, fds) \
+ safe_cg_open(__FILE__, __LINE__, (cg), (file_name), (flags), (fds))
+
+int safe_cg_open(const char *const file, const int lineno,
+ const struct tst_cg_group *const cg,
+ const char *const file_name,
+ int flags, int *fds)
+ __attribute__ ((nonnull));
+
+#define SAFE_CG_FCHOWN(cg, file_name, owner, group) \
+ safe_cg_fchown(__FILE__, __LINE__, (cg), (file_name), (owner), (group))
+
+void safe_cg_fchown(const char *const file, const int lineno,
+ const struct tst_cg_group *const cg,
+ const char *const file_name,
+ uid_t owner, gid_t group)
+ __attribute__ ((nonnull));
+
#define SAFE_CG_SCANF(cg, file_name, fmt, ...) \
safe_cg_scanf(__FILE__, __LINE__, \
(cg), (file_name), (fmt), __VA_ARGS__)
diff --git a/lib/tst_cgroup.c b/lib/tst_cgroup.c
index 1cfd79243..3d9568efe 100644
--- a/lib/tst_cgroup.c
+++ b/lib/tst_cgroup.c
@@ -77,31 +77,6 @@ struct cgroup_root {
int no_cpuset_prefix:1;
};
-/* Controller sub-systems */
-enum cgroup_ctrl_indx {
- CTRL_MEMORY = 1,
- CTRL_CPU,
- CTRL_CPUSET,
- CTRL_IO,
- CTRL_PIDS,
- CTRL_HUGETLB,
- CTRL_CPUACCT,
- CTRL_DEVICES,
- CTRL_FREEZER,
- CTRL_NETCLS,
- CTRL_NETPRIO,
- CTRL_BLKIO,
- CTRL_MISC,
- CTRL_PERFEVENT,
- CTRL_DEBUG
-};
-#define CTRLS_MAX CTRL_DEBUG
-
-/* At most we can have one cgroup V1 tree for each controller and one
- * (empty) v2 tree.
- */
-#define ROOTS_MAX (CTRLS_MAX + 1)
-
/* Describes a controller file or knob
*
* The primary purpose of this is to map V2 names to V1
@@ -1320,6 +1295,47 @@ void safe_cg_printf(const char *const file, const int lineno,
}
}
+int safe_cg_open(const char *const file, const int lineno,
+ const struct tst_cg_group *cg,
+ const char *const file_name, int flags, int *fds)
+{
+ const struct cgroup_file *const cfile =
+ cgroup_file_find(file, lineno, file_name);
+ struct cgroup_dir *const *dir;
+ const char *alias;
+ int i = 0;
+
+ for_each_dir(cg, cfile->ctrl_indx, dir) {
+ alias = cgroup_file_alias(cfile, *dir);
+ if (!alias)
+ continue;
+
+ fds[i++] = safe_openat(file, lineno, (*dir)->dir_fd, alias, flags);
+ }
+
+ return i;
+}
+
+void safe_cg_fchown(const char *const file, const int lineno,
+ const struct tst_cg_group *cg,
+ const char *const file_name,
+ uid_t owner, gid_t group)
+{
+ const struct cgroup_file *const cfile =
+ cgroup_file_find(file, lineno, file_name);
+ struct cgroup_dir *const *dir;
+ const char *alias;
+
+ for_each_dir(cg, cfile->ctrl_indx, dir) {
+ alias = cgroup_file_alias(cfile, *dir);
+ if (!alias)
+ continue;
+
+ safe_fchownat(file, lineno, (*dir)->dir_fd, alias, owner, group, 0);
+ }
+}
+
+
void safe_cg_scanf(const char *const file, const int lineno,
const struct tst_cg_group *const cg,
const char *const file_name,
--
2.27.0
More information about the ltp
mailing list