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
verify_prctl(unsigned int n)31 static void verify_prctl(unsigned int n)
32 {
33 char buf[20];
34 char comm_path[40];
35 pid_t tid;
36 struct tcase *tc = &tcases[n];
37
38 TEST(prctl(PR_SET_NAME, tc->setname));
39 if (TST_RET == -1) {
40 tst_res(TFAIL | TTERRNO, "prctl(PR_SET_NAME) failed");
41 return;
42 }
43 tst_res(TPASS, "prctl(PR_SET_NAME, '%s') succeeded", tc->setname);
44
45 TEST(prctl(PR_GET_NAME, buf));
46 if (TST_RET == -1) {
47 tst_res(TFAIL | TTERRNO, "prctl(PR_GET_NAME) failed");
48 return;
49 }
50
51 if (strncmp(tc->expname, buf, sizeof(buf))) {
52 tst_res(TFAIL,
53 "prctl(PR_GET_NAME) failed, expected %s, got %s",
54 tc->expname, buf);
55 return;
56 }
57 tst_res(TPASS, "prctl(PR_GET_NAME) succeeded, thread name is %s", buf);
58
59 tid = tst_syscall(__NR_gettid);
60
61 sprintf(comm_path, "/proc/self/task/%d/comm", tid);
62 TST_ASSERT_STR(comm_path, tc->expname);
63 TST_ASSERT_STR("/proc/self/comm", tc->expname);
64 }
65
66 static struct tst_test test = {
67 .test = verify_prctl,
68 .tcnt = ARRAY_SIZE(tcases),
69 };
70