[LTP] [PATCH v3] syscalls/prctl05.c: New test for prctl() with PR_{SET, GET}_NAME

Xiao Yang ice_yangxiao@163.com
Fri May 24 15:45:36 CEST 2019


On 05/24/2019 03:50 PM, Yang Xu wrote:
> Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> ---
>   include/lapi/prctl.h                       |  5 ++
>   runtest/syscalls                           |  1 +
>   testcases/kernel/syscalls/prctl/.gitignore |  1 +
>   testcases/kernel/syscalls/prctl/prctl05.c  | 76 ++++++++++++++++++++++
>   4 files changed, 83 insertions(+)
>   create mode 100644 testcases/kernel/syscalls/prctl/prctl05.c
>
> diff --git a/include/lapi/prctl.h b/include/lapi/prctl.h
> index f42bd6459..ad0b12bce 100644
> --- a/include/lapi/prctl.h
> +++ b/include/lapi/prctl.h
> @@ -9,6 +9,11 @@
>   
>   #include <sys/prctl.h>
>   
> +#ifndef PR_SET_NAME
> +# define PR_SET_NAME 15
> +# define PR_GET_NAME 16
> +#endif
> +
>   #ifndef PR_SET_SECCOMP
>   # define PR_GET_SECCOMP  21
>   # define PR_SET_SECCOMP  22
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 04558a580..d2dcd2152 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -862,6 +862,7 @@ prctl01 prctl01
>   prctl02 prctl02
>   prctl03 prctl03
>   prctl04 prctl04
> +prctl05 prctl05
>   
>   pread01 pread01
>   pread01_64 pread01_64
> diff --git a/testcases/kernel/syscalls/prctl/.gitignore b/testcases/kernel/syscalls/prctl/.gitignore
> index 1c3da3052..9ecaf9854 100644
> --- a/testcases/kernel/syscalls/prctl/.gitignore
> +++ b/testcases/kernel/syscalls/prctl/.gitignore
> @@ -2,3 +2,4 @@
>   /prctl02
>   /prctl03
>   /prctl04
> +/prctl05
> diff --git a/testcases/kernel/syscalls/prctl/prctl05.c b/testcases/kernel/syscalls/prctl/prctl05.c
> new file mode 100644
> index 000000000..e1287abdf
> --- /dev/null
> +++ b/testcases/kernel/syscalls/prctl/prctl05.c
> @@ -0,0 +1,76 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + *
> + * Test PR_GET_NAME and PR_SET_NAME of prctl(2).
> + * 1)Set the name of the calling thread, the name can be up to 16 bytes
> + *   long, including the terminating null byte. If exceeds 16 bytes, the
> + *   string is silently truncated.
> + * 2)Return the name of the calling thread, the buffer should allow space
> + *   for up to 16 bytes, the returned string will be null-terminated.
> + * 3)Check /proc/self/task/[tid]/comm name whether matches the thread name.
> + */
> +
> +#include <sys/prctl.h>
> +#include <string.h>
> +#include <stdio.h>
> +#include "lapi/syscalls.h"
> +#include "lapi/prctl.h"
> +#include "tst_test.h"
> +
> +static struct tcase {
> +	char setname[20];
> +	char expname[20];
> +} tcases[] = {
> +	{"prctl05_test", "prctl05_test"},
> +	{"prctl05_test_xxxxx", "prctl05_test_xx"}
> +};
> +
> +static void verify_prctl(unsigned int n)
> +{
> +	char buf[20];
> +	char comm_buf[20];
> +	char PROC_NAME_PATH[40];
> +	pid_t tid;
> +	struct tcase *tc = &tcases[n];
> +
> +	TEST(prctl(PR_SET_NAME, tc->setname));
> +	if (TST_RET == -1) {
> +		tst_res(TFAIL | TTERRNO, "prctl(PR_SET_NAME) failed");
> +		return;
> +	}
> +	tst_res(TPASS,
> +		"prctl(PR_SET_NAME) succeeded, set thread name as %s", tc->setname);
> +
> +	TEST(prctl(PR_GET_NAME, buf));
> +	if (TST_RET == -1) {
> +		tst_res(TFAIL | TTERRNO, "prctl(PR_GET_NAME) failed");
> +		return;
> +	}
> +
> +	if (strcmp(tc->expname, buf))
> +		tst_res(TFAIL,
> +			"prctl(PR_GET_NAME) failed ,expected %s, got %s", tc->expname, buf);
> +	else
> +		tst_res(TPASS,
> +			"prctl(PR_GET_NAME) succeeded, thread name is %s", buf);
Hi Xu,

It is not necessary to continue if buf mismatches tc->expname.

> +
> +	tid = tst_syscall(__NR_gettid);
> +
> +	sprintf(PROC_NAME_PATH, "/proc/self/task/%d/comm", tid);
> +	SAFE_FILE_SCANF(PROC_NAME_PATH, "%s", comm_buf);

You didn't create multiple threads here so it seems simpler to use 
/proc/self/comm
instead of /proc/self/task/<tid>/comm. do you think so?
Please see the following url for details:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/proc.txt

BTW:
Should we print three TPASS messages for one test?
Can we replace previous TPASS with TINFO and then print TPASS at the end?

Best Regards,
Xiao Yang
> +	if (strcmp(tc->expname, comm_buf))
> +		tst_res(TFAIL,
> +			"%s sets thread name failed, expected %s, got %s",
> +			PROC_NAME_PATH, tc->expname, comm_buf);
> +	else
> +		tst_res(TPASS, "%s sets thread name succeed as %s",
> +			PROC_NAME_PATH, comm_buf);
> +
> +}
> +
> +static struct tst_test test = {
> +	.test = verify_prctl,
> +	.tcnt = ARRAY_SIZE(tcases),
> +};




More information about the ltp mailing list