diff mbox series

[v3,2/3] tst_cgroup: Add safe_cg_open and safe_cg_fchown functions

Message ID 1661252479-2363-2-git-send-email-xuyang2018.jy@fujitsu.com
State Superseded
Headers show
Series [v3,1/3] tst_safe_file_at: Add SAFE_FCHOWNAT macro | expand

Commit Message

Yang Xu \(Fujitsu\) Aug. 23, 2022, 11:01 a.m. UTC
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(-)

Comments

Richard Palethorpe Aug. 25, 2022, 2:57 p.m. UTC | #1
Hello,

Yang Xu <xuyang2018.jy@fujitsu.com> writes:

> 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)

These need TST_CG_ prepending to them to stop name collisions.
Richard Palethorpe Aug. 25, 2022, 4:08 p.m. UTC | #2
Richard Palethorpe <rpalethorpe@suse.de> writes:

> Hello,
>
> Yang Xu <xuyang2018.jy@fujitsu.com> writes:
>
>> 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)
>
> These need TST_CG_ prepending to them to stop name collisions.

Actually, I think the easiest thing to do is not export these. Instead
you could leave these alone and export "#define TST_CG_ROOTS_MAX 32".

The rest of the patch-set looks great.
Yang Xu \(Fujitsu\) Aug. 26, 2022, 2:04 a.m. UTC | #3
Hi Richard

> 
> Richard Palethorpe <rpalethorpe@suse.de> writes:
> 
>> Hello,
>>
>> Yang Xu <xuyang2018.jy@fujitsu.com> writes:
>>
>>> 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)
>>
>> These need TST_CG_ prepending to them to stop name collisions.
> 
> Actually, I think the easiest thing to do is not export these. Instead
> you could leave these alone and export "#define TST_CG_ROOTS_MAX 32".

Yes, I forgot ltp name rules when export it.

Will use  TST_CG_ROOTS_MAX in v4  and add your reviewed-by.


Best Regards
Yang Xu
> 
> The rest of the patch-set looks great.
>
diff mbox series

Patch

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,