Skip to content

Commit d837813

Browse files
osmtenshuahkh
authored andcommitted
selftests: prctl: Add prctl test for PR_GET_NAME
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> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 25cfe96 commit d837813

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

tools/testing/selftests/prctl/set-process-name.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define CHANGE_NAME "changename"
1313
#define EMPTY_NAME ""
1414
#define TASK_COMM_LEN 16
15+
#define MAX_PATH_LEN 50
1516

1617
int set_name(char *name)
1718
{
@@ -47,6 +48,35 @@ int check_null_pointer(char *check_name)
4748
return res;
4849
}
4950

51+
int check_name(void)
52+
{
53+
54+
int pid;
55+
56+
pid = getpid();
57+
FILE *fptr = NULL;
58+
char path[MAX_PATH_LEN] = {};
59+
char name[TASK_COMM_LEN] = {};
60+
char output[TASK_COMM_LEN] = {};
61+
int j;
62+
63+
j = snprintf(path, MAX_PATH_LEN, "/proc/self/task/%d/comm", pid);
64+
fptr = fopen(path, "r");
65+
if (!fptr)
66+
return -EIO;
67+
68+
fscanf(fptr, "%s", output);
69+
if (ferror(fptr))
70+
return -EIO;
71+
72+
int res = prctl(PR_GET_NAME, name, NULL, NULL, NULL);
73+
74+
if (res < 0)
75+
return -errno;
76+
77+
return !strcmp(output, name);
78+
}
79+
5080
TEST(rename_process) {
5181

5282
EXPECT_GE(set_name(CHANGE_NAME), 0);
@@ -57,6 +87,8 @@ TEST(rename_process) {
5787

5888
EXPECT_GE(set_name(CHANGE_NAME), 0);
5989
EXPECT_LT(check_null_pointer(CHANGE_NAME), 0);
90+
91+
EXPECT_TRUE(check_name());
6092
}
6193

6294
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)