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