selftests/bpf: Add cgroup helper remove_cgroup()
authorHou Tao <houtao1@huawei.com>
Mon, 21 Nov 2022 07:34:39 +0000 (15:34 +0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Mon, 21 Nov 2022 16:40:42 +0000 (17:40 +0100)
Add remove_cgroup() to remove a cgroup which doesn't have any children
or live processes. It will be used by the following patch to test cgroup
iterator on a dead cgroup.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20221121073440.1828292-3-houtao@huaweicloud.com
tools/testing/selftests/bpf/cgroup_helpers.c
tools/testing/selftests/bpf/cgroup_helpers.h

index dd1aa5afcf5a6cca77dac36ecfe94bd93de0ecb8..9e95b37a7dff201909b596042eed9f718bd0884c 100644 (file)
@@ -333,6 +333,25 @@ int get_root_cgroup(void)
        return fd;
 }
 
+/*
+ * remove_cgroup() - Remove a cgroup
+ * @relative_path: The cgroup path, relative to the workdir, to remove
+ *
+ * This function expects a cgroup to already be created, relative to the cgroup
+ * work dir. It also expects the cgroup doesn't have any children or live
+ * processes and it removes the cgroup.
+ *
+ * On failure, it will print an error to stderr.
+ */
+void remove_cgroup(const char *relative_path)
+{
+       char cgroup_path[PATH_MAX + 1];
+
+       format_cgroup_path(cgroup_path, relative_path);
+       if (rmdir(cgroup_path))
+               log_err("rmdiring cgroup %s .. %s", relative_path, cgroup_path);
+}
+
 /**
  * create_and_get_cgroup() - Create a cgroup, relative to workdir, and get the FD
  * @relative_path: The cgroup path, relative to the workdir, to join
index 3358734356ab7fd09d062bc4f11bc4f64a9ddded..f099a166c94d45342ef8d8fae2d53dd5f3edcf53 100644 (file)
@@ -18,6 +18,7 @@ int write_cgroup_file_parent(const char *relative_path, const char *file,
 int cgroup_setup_and_join(const char *relative_path);
 int get_root_cgroup(void);
 int create_and_get_cgroup(const char *relative_path);
+void remove_cgroup(const char *relative_path);
 unsigned long long get_cgroup_id(const char *relative_path);
 
 int join_cgroup(const char *relative_path);