xref: /linux/tools/testing/selftests/prctl/set-process-name.c (revision e0606daeaab4fba3cc418a202da81aa7a57c0342)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This test covers the PR_SET_NAME functionality of prctl calls
4  */
5 
6 #include <errno.h>
7 #include <sys/prctl.h>
8 #include <string.h>
9 
10 #include "../kselftest_harness.h"
11 
12 #define CHANGE_NAME "changename"
13 #define EMPTY_NAME ""
14 #define TASK_COMM_LEN 16
15 
16 int set_name(char *name)
17 {
18 	int res;
19 
20 	res = prctl(PR_SET_NAME, name, NULL, NULL, NULL);
21 
22 	if (res < 0)
23 		return -errno;
24 	return res;
25 }
26 
27 int check_is_name_correct(char *check_name)
28 {
29 	char name[TASK_COMM_LEN];
30 	int res;
31 
32 	res = prctl(PR_GET_NAME, name, NULL, NULL, NULL);
33 
34 	if (res < 0)
35 		return -errno;
36 
37 	return !strcmp(name, check_name);
38 }
39 
40 int check_null_pointer(char *check_name)
41 {
42 	char *name = NULL;
43 	int res;
44 
45 	res = prctl(PR_GET_NAME, name, NULL, NULL, NULL);
46 
47 	return res;
48 }
49 
50 TEST(rename_process) {
51 
52 	EXPECT_GE(set_name(CHANGE_NAME), 0);
53 	EXPECT_TRUE(check_is_name_correct(CHANGE_NAME));
54 
55 	EXPECT_GE(set_name(EMPTY_NAME), 0);
56 	EXPECT_TRUE(check_is_name_correct(EMPTY_NAME));
57 
58 	EXPECT_GE(set_name(CHANGE_NAME), 0);
59 	EXPECT_LT(check_null_pointer(CHANGE_NAME), 0);
60 }
61 
62 TEST_HARNESS_MAIN
63