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