From b1236cf9e33dc521db487911b38d5441a220d7ba Mon Sep 17 00:00:00 2001 From: patacongo Date: Thu, 26 Apr 2012 15:00:35 +0000 Subject: Add interfaces to support getting/setting of thread/task names git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4658 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/ChangeLog | 2 + nuttx/include/pthread.h | 12 ++++ nuttx/include/sys/prctl.h | 115 ++++++++++++++++++++++++++++++++++++ nuttx/include/sys/syscall.h | 17 ++++-- nuttx/include/syscall.h | 4 +- nuttx/sched/Makefile | 2 +- nuttx/sched/prctl.c | 138 ++++++++++++++++++++++++++++++++++++++++++++ nuttx/syscall/stub_lookup.c | 8 ++- nuttx/syscall/stub_lookup.h | 6 ++ nuttx/syscall/syscall.csv | 1 + 10 files changed, 296 insertions(+), 9 deletions(-) create mode 100644 nuttx/include/sys/prctl.h create mode 100644 nuttx/sched/prctl.c diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 919ad1778..31b84665a 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -2686,4 +2686,6 @@ * arch/arm/src/stm32/stm32_otgfs.c: The driver needs to reset the software (in order to flush the requests) and to disable the software connection when the device is unregistered. + * include/sys/prctl.h, sched/prctl.c, include/pthread.h: Add interfaces to + support getting and setting of the thread or task name. diff --git a/nuttx/include/pthread.h b/nuttx/include/pthread.h index 092cfad93..5a08f13b6 100644 --- a/nuttx/include/pthread.h +++ b/nuttx/include/pthread.h @@ -136,6 +136,18 @@ #define PTHREAD_BARRIER_SERIAL_THREAD 0x1000 +/* 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 sucess and -1 on failure with the + * err number in errno. This is an inconsistency with out pthread interfaces. + */ + +#define pthread_setname_np(thread, name) \ + prctl((int)PR_SET_NAME, (char*)name, (int)thread) + +#define pthread_getname_np(thread, name) \ + prctl((int)PR_GET_NAME, (char*)name, (int)thread) + /******************************************************************************** * Global Type Declarations ********************************************************************************/ diff --git a/nuttx/include/sys/prctl.h b/nuttx/include/sys/prctl.h new file mode 100644 index 000000000..6af684562 --- /dev/null +++ b/nuttx/include/sys/prctl.h @@ -0,0 +1,115 @@ +/**************************************************************************** + * include/sys/prctl.h + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __INCLUDE_SYS_PRCTL_H +#define __INCLUDE_SYS_PRCTL_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +/* Supported prctl() commands. + * + * + * PR_SET_NAME + * 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); + * + * PR_GET_NAME + * 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: + * + * char myname[CONFIG_TASK_NAME_SIZE]; + * prctl(PR_GET_NAME, myname, 0); + */ + + #define PR_SET_NAME 1 + #define PR_GET_NAME 2 + +/**************************************************************************** + * Public Type Definitions + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" { +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Name: prctl + * + * Description: + * prctl() is called with a first argument describing what to do (with + * values PR_* defined above) and with additional arguments depending on + * the specific command. + * + * Returned Value: + * The returned value may depend on the specific commnand. For PR_SET_NAME + * and PR_GET_NAME, the returned value of 0 indicates successful operation. + * On any failure, -1 is retruend and the errno value is set appropriately. + * + * EINVAL The value of 'option' is not recognized. + * EFAULT optional arg1 is not a valid address. + * ESRCH No task/thread can be found corresponding to that specified + * by optional arg1. + * + ****************************************************************************/ + +EXTERN int prctl(int option, ...); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __INCLUDE_SYS_PRCTL_H */ diff --git a/nuttx/include/sys/syscall.h b/nuttx/include/sys/syscall.h index 616c39546..96650eaea 100644 --- a/nuttx/include/sys/syscall.h +++ b/nuttx/include/sys/syscall.h @@ -2,8 +2,8 @@ * include/sys/syscall.h * This file contains the system call numbers. * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -315,9 +315,18 @@ # define SYS_sendto (__SYS_network+8) # define SYS_setsockopt (__SYS_network+9) # define SYS_socket (__SYS_network+10) -# define SYS_maxsyscall (__SYS_network+11) +# define SYS_nnetsocket (__SYS_network+11) #else -# define SYS_maxsyscall __SYS_network +# define SYS_nnetsocket __SYS_network +#endif + +/* The following is defined only if CONFIG_TASK_NAME_SIZE > 0 */ + +#if CONFIG_TASK_NAME_SIZE > 0 +# define SYS_prctl (SYS_nnetsocket+0) +# define SYS_maxsyscall (SYS_nnetsocket+1) +#else +# define SYS_maxsyscall SYS_nnetsocket #endif /* Note that the reported number of system calls does *NOT* include the diff --git a/nuttx/include/syscall.h b/nuttx/include/syscall.h index 59f888fb1..ae888e9c2 100644 --- a/nuttx/include/syscall.h +++ b/nuttx/include/syscall.h @@ -1,8 +1,8 @@ /**************************************************************************** * include/syscall.h * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions diff --git a/nuttx/sched/Makefile b/nuttx/sched/Makefile index b8a1bb519..dfdc6f68b 100644 --- a/nuttx/sched/Makefile +++ b/nuttx/sched/Makefile @@ -43,7 +43,7 @@ MISC_SRCS = os_start.c os_bringup.c errno_getptr.c errno_get.c errno_set.c \ sched_getstreams.c sched_setupidlefiles.c sched_setuptaskfiles.c \ sched_setuppthreadfiles.c sched_releasefiles.c -TSK_SRCS = task_create.c task_init.c task_setup.c task_activate.c \ +TSK_SRCS = prctl.c task_create.c task_init.c task_setup.c task_activate.c \ task_start.c task_delete.c task_deletecurrent.c task_exithook.c \ task_restart.c exit.c getpid.c sched_addreadytorun.c \ sched_removereadytorun.c sched_addprioritized.c sched_mergepending.c \ diff --git a/nuttx/sched/prctl.c b/nuttx/sched/prctl.c new file mode 100644 index 000000000..93d69ee02 --- /dev/null +++ b/nuttx/sched/prctl.c @@ -0,0 +1,138 @@ +/************************************************************************ + * sched/prctl.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ************************************************************************/ + +/************************************************************************ + * Included Files + ************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include +#include "os_internal.h" + +/************************************************************************ + * Private Functions + ************************************************************************/ + +/************************************************************************ + * Public Functions + ************************************************************************/ + +/**************************************************************************** + * Name: prctl + * + * Description: + * prctl() is called with a first argument describing what to do (with + * values PR_* defined above) and with additional arguments depending on + * the specific command. + * + * Returned Value: + * The returned value may depend on the specific commnand. For PR_SET_NAME + * and PR_GET_NAME, the returned value of 0 indicates successful operation. + * On any failure, -1 is retruend and the errno value is set appropriately. + * + * EINVAL The value of 'option' is not recognized. + * EFAULT optional arg1 is not a valid address. + * ESRCH No task/thread can be found corresponding to that specified + * by optional arg1. + * + ****************************************************************************/ + +int prctl(int option, ...) +{ + va_list ap; + int err; + + va_start(ap, option); + switch (option) + { + case PR_SET_NAME: + case PR_GET_NAME: +#if CONFIG_TASK_NAME_SIZE > 0 + { + int pid = va_arg(ap, int); + char *name = va_arg(ap, char *); + FAR _TCB *tcb = sched_gettcb(pid); + if (!tcb) + { + sdbg("Pid does not correspond to a task: %d\n", pid); + err = ESRCH; + goto errout; + } + + if (!name) + { + sdbg("No name provide\n"); + err = EFAULT; + goto errout; + } + + if (option == PR_SET_NAME) + { + strncpy(tcb->name, name, CONFIG_TASK_NAME_SIZE); + } + else + { + strncpy(name, tcb->name, CONFIG_TASK_NAME_SIZE-1); + name[CONFIG_TASK_NAME_SIZE-1]; + } + } + break; +#else + sdbg("Option not enabled: %d\n", option); + err = ENOSYS; + goto errout; +#endif + + default: + sdbg("Unrecognized option: %d\n", option); + err = EINVAL; + goto errout; + } + + va_end(ap); + return OK; + +errout: + va_end(ap); + set_errno(err); + return ERROR; +} diff --git a/nuttx/syscall/stub_lookup.c b/nuttx/syscall/stub_lookup.c index 0c4f4451c..9d6bf18ae 100644 --- a/nuttx/syscall/stub_lookup.c +++ b/nuttx/syscall/stub_lookup.c @@ -1,8 +1,8 @@ /**************************************************************************** * syscall/syscall_stublookup.c * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -230,6 +230,10 @@ extern uintptr_t STUB_sendto(uintptr_t parm1, uintptr_t parm2, uintptr_t parm3, extern uintptr_t STUB_setsockopt(uintptr_t parm1, uintptr_t parm2, uintptr_t parm3, uintptr_t parm4, uintptr_t parm5); extern uintptr_t STUB_socket(uintptr_t parm1, uintptr_t parm2, uintptr_t parm3); +/* The following is defined only if CONFIG_TASK_NAME_SIZE > 0 */ + +extern uintptr_t STUB_prctl(uintptr_t parm1, uintptr_t parm2, uintptr_t parm3, uintptr_t parm4, uintptr_t parm5); + /**************************************************************************** * Public Data ****************************************************************************/ diff --git a/nuttx/syscall/stub_lookup.h b/nuttx/syscall/stub_lookup.h index f9b33938b..4f75c1696 100644 --- a/nuttx/syscall/stub_lookup.h +++ b/nuttx/syscall/stub_lookup.h @@ -247,6 +247,12 @@ STUB_LOOKUP(3, STUB_up_assert_code) /* SYS_up_assert_code */ STUB_LOOKUP(3, STUB_socket) /* SYS_socket */ #endif +/* The following is defined only if CONFIG_TASK_NAME_SIZE > 0 */ + +#if CONFIG_TASK_NAME_SIZE > 0 + STUB_LOOKUP(5, STUB_prctl) /* SYS_prctl */ +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ diff --git a/nuttx/syscall/syscall.csv b/nuttx/syscall/syscall.csv index bff8c50ca..b323eb95f 100644 --- a/nuttx/syscall/syscall.csv +++ b/nuttx/syscall/syscall.csv @@ -39,6 +39,7 @@ "open","fcntl.h","CONFIG_NFILE_DESCRIPTORS > 0","int","const char*","int","..." "opendir","dirent.h","CONFIG_NFILE_DESCRIPTORS > 0","FAR DIR*","FAR const char*" "pipe","unistd.h","CONFIG_NFILE_DESCRIPTORS > 0","int","int [2]|int*" +"prctl", sys/prctl.h, "CONFIG_TASK_NAME_SIZE > 0","int","int","..." "clock_systimer","nuttx/clock.h","!defined(CONFIG_DISABLE_CLOCK)","uint32_t" "poll","poll.h","!defined(CONFIG_DISABLE_POLL) && (CONFIG_NSOCKET_DESCRIPTORS > 0 || CONFIG_NFILE_DESCRIPTORS > 0)","int","FAR struct pollfd*","nfds_t","int" "pthread_barrier_destroy","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_barrier_t*" -- cgit v1.2.3