From 4ee5ff81ddac608900f342a2c18029a804ea36d1 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sat, 5 Feb 2022 17:06:51 +0800 Subject: [PATCH] pthread: Add len argument to pthread_getname_np follow the spec: https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html Signed-off-by: Xiang Xiao --- include/pthread.h | 19 +++----- libs/libc/pthread/Make.defs | 1 + libs/libc/pthread/pthread_getname_np.c | 60 ++++++++++++++++++++++++++ libs/libc/pthread/pthread_setname_np.c | 54 +++++++++++++++++++++++ 4 files changed, 120 insertions(+), 14 deletions(-) create mode 100644 libs/libc/pthread/pthread_getname_np.c create mode 100644 libs/libc/pthread/pthread_setname_np.c diff --git a/include/pthread.h b/include/pthread.h index 602f6af0cb8..c679e5cb2b9 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -29,8 +29,6 @@ #include /* Compiler settings, noreturn_function */ #include /* Needed for general types */ -#include /* Needed by pthread_[set|get]name_np */ - #include /* C99 fixed width integer types */ #include /* C99 boolean types */ #include /* For getpid */ @@ -188,18 +186,6 @@ #define _PTHREAD_MFLAGS_INCONSISTENT (1 << 1) /* Mutex is in an inconsistent state */ #define _PTHREAD_MFLAGS_NRECOVERABLE (1 << 2) /* Inconsistent mutex has been unlocked */ -/* Definitions to map some non-standard, BSD thread management interfaces to - * the non-standard Linux-like prctl() interface. Since these are simple - * mappings to prctl, they will return 0 on success and -1 on failure with the - * error number in errno. This is an inconsistency with the pthread interfaces. - */ - -#define pthread_setname_np(thread, name) \ - prctl((int)PR_SET_NAME_EXT, (char*)name, (int)thread) - -#define pthread_getname_np(thread, name) \ - prctl((int)PR_GET_NAME_EXT, (char*)name, (int)thread) - /******************************************************************************** * Public Type Definitions ********************************************************************************/ @@ -484,6 +470,11 @@ int pthread_attr_setstack(FAR pthread_attr_t *attr, int pthread_attr_getstack(FAR pthread_attr_t *attr, FAR void **stackaddr, FAR long *stacksize); +/* Set or get the name of a thread */ + +int pthread_setname_np(pthread_t thread, FAR const char *name); +int pthread_getname_np(pthread_t thread, FAR char *name, size_t len); + /* Get run-time stack address and size */ FAR void *pthread_get_stackaddr_np(pthread_t thread); diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs index 33a5e8b9bee..3ae2f03c667 100644 --- a/libs/libc/pthread/Make.defs +++ b/libs/libc/pthread/Make.defs @@ -41,6 +41,7 @@ CSRCS += pthread_condattr_init.c pthread_condattr_destroy.c CSRCS += pthread_condattr_setclock.c pthread_condattr_getclock.c CSRCS += pthread_condinit.c pthread_conddestroy.c pthread_condtimedwait.c CSRCS += pthread_create.c pthread_exit.c +CSRCS += pthread_setname_np.c pthread_getname_np.c CSRCS += pthread_get_stackaddr_np.c pthread_get_stacksize_np.c CSRCS += pthread_mutexattr_init.c pthread_mutexattr_destroy.c CSRCS += pthread_mutexattr_getpshared.c pthread_mutexattr_setpshared.c diff --git a/libs/libc/pthread/pthread_getname_np.c b/libs/libc/pthread/pthread_getname_np.c new file mode 100644 index 00000000000..ab29c6d2acd --- /dev/null +++ b/libs/libc/pthread/pthread_getname_np.c @@ -0,0 +1,60 @@ +/**************************************************************************** + * libs/libc/pthread/pthread_getname_np.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_getname_np + * + * Description: + * Get the name of a thread + * + * Parameters: + * thread - The thread whose name is to be retrieved + * name - The buffer to return the thread name + * len - The number of bytes available in name + * + * Returned Value: + * On success, these functions return 0; + * on error, they return a nonzero error number. + * + ****************************************************************************/ + +int pthread_getname_np(pthread_t thread, FAR char *name, size_t len) +{ + if (len <= CONFIG_TASK_NAME_SIZE) + { + return ERANGE; + } + + return prctl(PR_GET_NAME_EXT, name, (int)thread) < 0 ? get_errno() : 0; +} diff --git a/libs/libc/pthread/pthread_setname_np.c b/libs/libc/pthread/pthread_setname_np.c new file mode 100644 index 00000000000..7e24ab812ae --- /dev/null +++ b/libs/libc/pthread/pthread_setname_np.c @@ -0,0 +1,54 @@ +/**************************************************************************** + * libs/libc/pthread/pthread_setname_np.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_setname_np + * + * Description: + * Set the name of a thread + * + * Parameters: + * thread - The thread whose name is to be changed + * name - The new thread name + * + * Returned Value: + * On success, these functions return 0; + * on error, they return a nonzero error number. + * + ****************************************************************************/ + +int pthread_setname_np(pthread_t thread, FAR const char *name) +{ + return prctl(PR_SET_NAME_EXT, name, (int)thread) < 0 ? get_errno() : 0; +}