From 3fbdc213b79474b1dfb9ad7580b348569c23361d Mon Sep 17 00:00:00 2001 From: "chao.an" Date: Tue, 30 Jun 2020 19:32:41 +0800 Subject: [PATCH] syscall/prctl: fix PR_SET_NAME failure if without arg add PR_SET_NAME_EXT/PR_GET_NAME_EXT extension to avoid semantic conflicts, use extened version for pthread_setname_np/pthread_getname_np Change-Id: I40404c737977a623130dcd37feb4061b5526e466 Signed-off-by: chao.an --- include/pthread.h | 4 ++-- include/sys/prctl.h | 37 +++++++++++++++++++++++++++++-------- sched/task/task_prctl.c | 12 ++++++++++-- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/include/pthread.h b/include/pthread.h index 70b188ebbd..6ff5cd84a2 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -207,10 +207,10 @@ */ #define pthread_setname_np(thread, name) \ - prctl((int)PR_SET_NAME, (char*)name, (int)thread) + prctl((int)PR_SET_NAME_EXT, (char*)name, (int)thread) #define pthread_getname_np(thread, name) \ - prctl((int)PR_GET_NAME, (char*)name, (int)thread) + prctl((int)PR_GET_NAME_EXT, (char*)name, (int)thread) /******************************************************************************** * Public Type Definitions diff --git a/include/sys/prctl.h b/include/sys/prctl.h index 44cf20b34c..678fbbfc92 100644 --- a/include/sys/prctl.h +++ b/include/sys/prctl.h @@ -47,26 +47,47 @@ /* Supported prctl() commands. * * PR_SET_NAME + * Set the name of the calling thread, using the value in the location + * pointed to by (char *) arg2. The name can be up to + * CONFIG_TASK_NAME_SIZE long, including the terminating null byte. + * (If the length of the string, including the terminating null byte, + * exceeds CONFIG_TASK_NAME_SIZE bytes, the string is silently truncated.) + * As an example: + * + * prctl(PR_SET_NAME, "MyName"); + * + * PR_GET_NAME + * Return the name of the calling thread, in the buffer pointed to by + * (char *) arg2. The buffer should allow space for up to + * CONFIG_TASK_NAME_SIZE bytes; the returned string will be + * null-terminated. As an example: + * + * char myname[CONFIG_TASK_NAME_SIZE]; + * prctl(PR_GET_NAME, myname); + * + * PR_SET_NAME_EXT * Set the task (or thread) name for the thread whose ID is in required * arg2 (int), using the value in the location pointed to by required arg1 * (char*). The name can be up to CONFIG_TASK_NAME_SIZE long (including * any null termination). The thread ID of 0 will set the name of the * calling thread. As an example: * - * prctl(PR_SET_NAME, "MyName", 0); + * prctl(PR_SET_NAME_EXT, "MyName", pid); * - * PR_GET_NAME + * PR_GET_NAME_EXT * Return the task (or thread) name for the for the thread whose ID is - * optional arg2 (int), in the buffer pointed to by optional arg1 (char *). - * The buffer must be CONFIG_TASK_NAME_SIZE long (including any null - * termination). As an example: + * optional arg2 (int), in the buffer pointed to by optional arg1 + * (char *). The buffer must be CONFIG_TASK_NAME_SIZE long (including + * any null termination). As an example: * * char myname[CONFIG_TASK_NAME_SIZE]; - * prctl(PR_GET_NAME, myname, 0); + * prctl(PR_GET_NAME_EXT, myname, pid); */ -#define PR_SET_NAME 1 -#define PR_GET_NAME 2 +#define PR_SET_NAME 1 +#define PR_GET_NAME 2 +#define PR_SET_NAME_EXT 3 +#define PR_GET_NAME_EXT 4 /**************************************************************************** * Public Type Definitions diff --git a/sched/task/task_prctl.c b/sched/task/task_prctl.c index 8afbe1d145..b2370989d8 100644 --- a/sched/task/task_prctl.c +++ b/sched/task/task_prctl.c @@ -84,13 +84,21 @@ int prctl(int option, ...) { case PR_SET_NAME: case PR_GET_NAME: + case PR_SET_NAME_EXT: + case PR_GET_NAME_EXT: #if CONFIG_TASK_NAME_SIZE > 0 { /* Get the prctl arguments */ FAR char *name = va_arg(ap, FAR char *); - int pid = va_arg(ap, int); FAR struct tcb_s *tcb; + int pid = 0; + + if (option == PR_SET_NAME_EXT || + option == PR_GET_NAME_EXT) + { + pid = va_arg(ap, int); + } /* Get the TCB associated with the PID (handling the special case * of pid==0 meaning "this thread") @@ -127,7 +135,7 @@ int prctl(int option, ...) /* Now get or set the task name */ - if (option == PR_SET_NAME) + if (option == PR_SET_NAME || option == PR_SET_NAME_EXT) { /* Ensure that tcb->name will be null-terminated, truncating if * necessary.