• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5  *
6  * Test PR_GET_NAME and PR_SET_NAME of prctl(2).
7  * 1)Set the name of the calling thread, the name can be up to 16 bytes
8  *   long, including the terminating null byte. If exceeds 16 bytes, the
9  *   string is silently truncated.
10  * 2)Return the name of the calling thread, the buffer should allow space
11  *   for up to 16 bytes, the returned string will be null-terminated.
12  * 3)Check /proc/self/task/[tid]/comm and /proc/self/comm name whether
13  *   matches the thread name.
14  */
15 
16 #include <sys/prctl.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include "lapi/syscalls.h"
20 #include "lapi/prctl.h"
21 #include "tst_test.h"
22 
23 static struct tcase {
24 	char setname[20];
25 	char expname[20];
26 } tcases[] = {
27 	{"prctl05_test", "prctl05_test"},
28 	{"prctl05_test_xxxxx", "prctl05_test_xx"}
29 };
30 
check_proc_comm(char * path,char * name)31 static void check_proc_comm(char *path, char *name)
32 {
33 	char comm_buf[20];
34 
35 	SAFE_FILE_SCANF(path, "%s", comm_buf);
36 	if (strcmp(name, comm_buf))
37 		tst_res(TFAIL,
38 			"%s has %s, expected %s", path, comm_buf, name);
39 	else
40 		tst_res(TPASS, "%s sets to %s", path, comm_buf);
41 }
42 
verify_prctl(unsigned int n)43 static void verify_prctl(unsigned int n)
44 {
45 	char buf[20];
46 	char comm_path[40];
47 	pid_t tid;
48 	struct tcase *tc = &tcases[n];
49 
50 	TEST(prctl(PR_SET_NAME, tc->setname));
51 	if (TST_RET == -1) {
52 		tst_res(TFAIL | TTERRNO, "prctl(PR_SET_NAME) failed");
53 		return;
54 	}
55 	tst_res(TPASS, "prctl(PR_SET_NAME, '%s') succeeded", tc->setname);
56 
57 	TEST(prctl(PR_GET_NAME, buf));
58 	if (TST_RET == -1) {
59 		tst_res(TFAIL | TTERRNO, "prctl(PR_GET_NAME) failed");
60 		return;
61 	}
62 
63 	if (strncmp(tc->expname, buf, sizeof(buf))) {
64 		tst_res(TFAIL,
65 		        "prctl(PR_GET_NAME) failed, expected %s, got %s",
66 		        tc->expname, buf);
67 		return;
68 	}
69 	tst_res(TPASS, "prctl(PR_GET_NAME) succeeded, thread name is %s", buf);
70 
71 	tid = tst_syscall(__NR_gettid);
72 
73 	sprintf(comm_path, "/proc/self/task/%d/comm", tid);
74 	check_proc_comm(comm_path, tc->expname);
75 
76 	check_proc_comm("/proc/self/comm", tc->expname);
77 }
78 
79 static struct tst_test test = {
80 	.test = verify_prctl,
81 	.tcnt = ARRAY_SIZE(tcases),
82 };
83