diff mbox series

[v2] selftests: prctl: Add prctl test for PR_GET_NAME

Message ID 20230820141354.29687-1-osmtendev@gmail.com
State Accepted
Commit d837813ff42ef86dac8596dd491bf6869ac7a0e8
Headers show
Series [v2] selftests: prctl: Add prctl test for PR_GET_NAME | expand

Commit Message

Osama Muhammad Aug. 20, 2023, 2:13 p.m. UTC
This patch covers the testing of PR_GET_NAME by
reading it's value from proc/self/task/pid/comm
and matching it with the value returned by PR_GET_NAME.
If the values are matched then it's successful, otherwise
it fails.

changes since v1:
	- Handled fscanf,fopen error checking.
	- Defined MAX_PATH_LEN.

Signed-off-by: Osama Muhammad <osmtendev@gmail.com>
---
 .../selftests/prctl/set-process-name.c        | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)

Comments

Osama Muhammad Oct. 3, 2023, 4:01 p.m. UTC | #1
On Sun, 20 Aug 2023 at 19:14, Osama Muhammad <osmtendev@gmail.com> wrote:
>
> This patch covers the testing of PR_GET_NAME by
> reading it's value from proc/self/task/pid/comm
> and matching it with the value returned by PR_GET_NAME.
> If the values are matched then it's successful, otherwise
> it fails.

Any Feedback on this patch?

>
> changes since v1:
>         - Handled fscanf,fopen error checking.
>         - Defined MAX_PATH_LEN.
>
> Signed-off-by: Osama Muhammad <osmtendev@gmail.com>
> ---
>  .../selftests/prctl/set-process-name.c        | 32 +++++++++++++++++++
>  1 file changed, 32 insertions(+)
>
> diff --git a/tools/testing/selftests/prctl/set-process-name.c b/tools/testing/selftests/prctl/set-process-name.c
> index 3bc5e0e09..562f707ba 100644
> --- a/tools/testing/selftests/prctl/set-process-name.c
> +++ b/tools/testing/selftests/prctl/set-process-name.c
> @@ -12,6 +12,7 @@
>  #define CHANGE_NAME "changename"
>  #define EMPTY_NAME ""
>  #define TASK_COMM_LEN 16
> +#define MAX_PATH_LEN 50
>
>  int set_name(char *name)
>  {
> @@ -47,6 +48,35 @@ int check_null_pointer(char *check_name)
>         return res;
>  }
>
> +int check_name(void)
> +{
> +
> +       int pid;
> +
> +       pid = getpid();
> +       FILE *fptr = NULL;
> +       char path[MAX_PATH_LEN] = {};
> +       char name[TASK_COMM_LEN] = {};
> +       char output[TASK_COMM_LEN] = {};
> +       int j;
> +
> +       j = snprintf(path, MAX_PATH_LEN, "/proc/self/task/%d/comm", pid);
> +       fptr = fopen(path, "r");
> +       if (!fptr)
> +               return -EIO;
> +
> +       fscanf(fptr, "%s", output);
> +       if (ferror(fptr))
> +               return -EIO;
> +
> +       int res = prctl(PR_GET_NAME, name, NULL, NULL, NULL);
> +
> +       if (res < 0)
> +               return -errno;
> +
> +       return !strcmp(output, name);
> +}
> +
>  TEST(rename_process) {
>
>         EXPECT_GE(set_name(CHANGE_NAME), 0);
> @@ -57,6 +87,8 @@ TEST(rename_process) {
>
>         EXPECT_GE(set_name(CHANGE_NAME), 0);
>         EXPECT_LT(check_null_pointer(CHANGE_NAME), 0);
> +
> +       EXPECT_TRUE(check_name());
>  }
>
>  TEST_HARNESS_MAIN
> --
> 2.34.1
>
Osama Muhammad Nov. 11, 2023, 7:23 p.m. UTC | #2
On Tue, 3 Oct 2023 at 21:01, Osama Muhammad <osmtendev@gmail.com> wrote:
>
> On Sun, 20 Aug 2023 at 19:14, Osama Muhammad <osmtendev@gmail.com> wrote:
> >
> > This patch covers the testing of PR_GET_NAME by
> > reading it's value from proc/self/task/pid/comm
> > and matching it with the value returned by PR_GET_NAME.
> > If the values are matched then it's successful, otherwise
> > it fails.
Hi Shuah,
Any Feedback on this patch.

Thanks,
Osama
>

> Any Feedback on this patch?
>
> >
> > changes since v1:
> >         - Handled fscanf,fopen error checking.
> >         - Defined MAX_PATH_LEN.
> >
> > Signed-off-by: Osama Muhammad <osmtendev@gmail.com>
> > ---
> >  .../selftests/prctl/set-process-name.c        | 32 +++++++++++++++++++
> >  1 file changed, 32 insertions(+)
> >
> > diff --git a/tools/testing/selftests/prctl/set-process-name.c b/tools/testing/selftests/prctl/set-process-name.c
> > index 3bc5e0e09..562f707ba 100644
> > --- a/tools/testing/selftests/prctl/set-process-name.c
> > +++ b/tools/testing/selftests/prctl/set-process-name.c
> > @@ -12,6 +12,7 @@
> >  #define CHANGE_NAME "changename"
> >  #define EMPTY_NAME ""
> >  #define TASK_COMM_LEN 16
> > +#define MAX_PATH_LEN 50
> >
> >  int set_name(char *name)
> >  {
> > @@ -47,6 +48,35 @@ int check_null_pointer(char *check_name)
> >         return res;
> >  }
> >
> > +int check_name(void)
> > +{
> > +
> > +       int pid;
> > +
> > +       pid = getpid();
> > +       FILE *fptr = NULL;
> > +       char path[MAX_PATH_LEN] = {};
> > +       char name[TASK_COMM_LEN] = {};
> > +       char output[TASK_COMM_LEN] = {};
> > +       int j;
> > +
> > +       j = snprintf(path, MAX_PATH_LEN, "/proc/self/task/%d/comm", pid);
> > +       fptr = fopen(path, "r");
> > +       if (!fptr)
> > +               return -EIO;
> > +
> > +       fscanf(fptr, "%s", output);
> > +       if (ferror(fptr))
> > +               return -EIO;
> > +
> > +       int res = prctl(PR_GET_NAME, name, NULL, NULL, NULL);
> > +
> > +       if (res < 0)
> > +               return -errno;
> > +
> > +       return !strcmp(output, name);
> > +}
> > +
> >  TEST(rename_process) {
> >
> >         EXPECT_GE(set_name(CHANGE_NAME), 0);
> > @@ -57,6 +87,8 @@ TEST(rename_process) {
> >
> >         EXPECT_GE(set_name(CHANGE_NAME), 0);
> >         EXPECT_LT(check_null_pointer(CHANGE_NAME), 0);
> > +
> > +       EXPECT_TRUE(check_name());
> >  }
> >
> >  TEST_HARNESS_MAIN
> > --
> > 2.34.1
> >
Shuah Khan Nov. 30, 2023, 11:31 p.m. UTC | #3
On 11/11/23 12:23, Osama Muhammad wrote:

>>>
>>> Signed-off-by: Osama Muhammad <osmtendev@gmail.com>
>>> ---
>>>   .../selftests/prctl/set-process-name.c        | 32 +++++++++++++++++++
>>>   1 file changed, 32 insertions(+)
>>>

Applied to linux-kselftest next for Linux 6.8-rc1.

thanks,
-- Shuah
diff mbox series

Patch

diff --git a/tools/testing/selftests/prctl/set-process-name.c b/tools/testing/selftests/prctl/set-process-name.c
index 3bc5e0e09..562f707ba 100644
--- a/tools/testing/selftests/prctl/set-process-name.c
+++ b/tools/testing/selftests/prctl/set-process-name.c
@@ -12,6 +12,7 @@ 
 #define CHANGE_NAME "changename"
 #define EMPTY_NAME ""
 #define TASK_COMM_LEN 16
+#define MAX_PATH_LEN 50
 
 int set_name(char *name)
 {
@@ -47,6 +48,35 @@  int check_null_pointer(char *check_name)
 	return res;
 }
 
+int check_name(void)
+{
+
+	int pid;
+
+	pid = getpid();
+	FILE *fptr = NULL;
+	char path[MAX_PATH_LEN] = {};
+	char name[TASK_COMM_LEN] = {};
+	char output[TASK_COMM_LEN] = {};
+	int j;
+
+	j = snprintf(path, MAX_PATH_LEN, "/proc/self/task/%d/comm", pid);
+	fptr = fopen(path, "r");
+	if (!fptr)
+		return -EIO;
+
+	fscanf(fptr, "%s", output);
+	if (ferror(fptr))
+		return -EIO;
+
+	int res = prctl(PR_GET_NAME, name, NULL, NULL, NULL);
+
+	if (res < 0)
+		return -errno;
+
+	return !strcmp(output, name);
+}
+
 TEST(rename_process) {
 
 	EXPECT_GE(set_name(CHANGE_NAME), 0);
@@ -57,6 +87,8 @@  TEST(rename_process) {
 
 	EXPECT_GE(set_name(CHANGE_NAME), 0);
 	EXPECT_LT(check_null_pointer(CHANGE_NAME), 0);
+
+	EXPECT_TRUE(check_name());
 }
 
 TEST_HARNESS_MAIN