diff --git a/ChangeLog b/ChangeLog index c256c180d9d79adc54c9a078df22141a374b2241..5737ece75d12a8bce013a323cbbe8398cd953c25 100644 --- a/ChangeLog +++ b/ChangeLog @@ -184,6 +184,9 @@ * fs/fs_opendir.c: Correct errors in semaphore usage that can cause deadlock. * lib/lib_getopt.c: Added getopt() support * examples/nsh: NSH now supports cat, mount, umount, and mkdir. ls supports - -l -s, and -R + -l -s, and -R + * Added basic OS support to manage environment variables: environment + storage, cloning on task creation, sharing on pthread creation, destruction + on thread/task exit. * Started m68322 diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html index b9de71c31cc3d5b7d5a7a4e90fb7c9e7ca6a578c..15b34a54d63d5aff19bd1843f20fa92fa528a411 100644 --- a/Documentation/NuttX.html +++ b/Documentation/NuttX.html @@ -619,7 +619,10 @@ Other memory: * fs/fs_opendir.c: Correct errors in semaphore usage that can cause deadlock. * lib/lib_getopt.c: Added getopt() support * examples/nsh: NSH now supports cat, mount, umount, and mkdir. ls supports - -l -s, and -R + -l -s, and -R + * Added basic OS support to manage environment variables: environment + storage, cloning on task creation, sharing on pthread creation, destruction + on thread/task exit. * Started m68322 </pre></ul> diff --git a/configs/README.txt b/configs/README.txt index a123a1f0157f6584fe57ecc81e44ba650a673c29..df1516c6619c071aa941328403e4f33781eb8447 100644 --- a/configs/README.txt +++ b/configs/README.txt @@ -164,7 +164,8 @@ defconfig -- This is a configuration file similar to the Linux up waiting tasks. CONFIG_DISABLE_CLOCK, CONFIG_DISABLE_POSIX_TIMERS, CONFIG_DISABLE_PTHREAD. - CONFIG_DISABLE_SIGNALS, CONFIG_DISABLE_MQUEUE, CONFIG_DISABLE_MOUNTPOUNT + CONFIG_DISABLE_SIGNALS, CONFIG_DISABLE_MQUEUE, CONFIG_DISABLE_MOUNTPOUNT, + CONFIG_DISABLE_ENVIRON Misc libc settings diff --git a/configs/c5471evm/defconfig b/configs/c5471evm/defconfig index e247d9e87ce237f89119b503f04900d6a63f0606..840b410ede9460e029994f984bc5acc39a29ddd6 100644 --- a/configs/c5471evm/defconfig +++ b/configs/c5471evm/defconfig @@ -163,6 +163,7 @@ CONFIG_DISABLE_PTHREAD=n CONFIG_DISABLE_SIGNALS=n CONFIG_DISABLE_MQUEUE=n CONFIG_DISABLE_MOUNTPOINT=y +CONFIG_DISABLE_ENVIRON=y # # Misc libc settings diff --git a/configs/m68332evb/defconfig b/configs/m68332evb/defconfig index 2185e760a244d432ba3c6b8105b9ddc58de752b1..5210453560e6c65bd412b65447bdd1c540fac7c2 100644 --- a/configs/m68332evb/defconfig +++ b/configs/m68332evb/defconfig @@ -152,6 +152,7 @@ CONFIG_DISABLE_PTHREAD=n CONFIG_DISABLE_SIGNALS=n CONFIG_DISABLE_MQUEUE=n CONFIG_DISABLE_MOUNTPOINT=y +CONFIG_DISABLE_ENVIRON=y # # Misc libc settings diff --git a/configs/mcu123-lpc214x/defconfig b/configs/mcu123-lpc214x/defconfig index ca0c9a15337854d0bb3e2e28d112de2a9061316a..43b575fcf5ae72eef130ad8039e73f8e2ead1e55 100644 --- a/configs/mcu123-lpc214x/defconfig +++ b/configs/mcu123-lpc214x/defconfig @@ -176,6 +176,7 @@ CONFIG_DISABLE_PTHREAD=n CONFIG_DISABLE_SIGNALS=n CONFIG_DISABLE_MQUEUE=n CONFIG_DISABLE_MOUNTPOINT=y +CONFIG_DISABLE_ENVIRON=y # # Misc libc settings diff --git a/configs/ntosd-dm320/defconfig b/configs/ntosd-dm320/defconfig index 640783cf1df0bb05139d65b0e17cd1059cba2cd6..55753fdc0fc8781213b8a1d1c1c2ce206551b66b 100644 --- a/configs/ntosd-dm320/defconfig +++ b/configs/ntosd-dm320/defconfig @@ -161,6 +161,7 @@ CONFIG_DISABLE_PTHREAD=n CONFIG_DISABLE_SIGNALS=n CONFIG_DISABLE_MQUEUE=n CONFIG_DISABLE_MOUNTPOINT=y +CONFIG_DISABLE_ENVIRON=y # # Misc libc settings diff --git a/configs/pjrc-8051/defconfig b/configs/pjrc-8051/defconfig index c9b8a481a333a53e45da3c975feca7d587cb9255..6ae70d5926631d53068ef83964ba9a3219361b33 100644 --- a/configs/pjrc-8051/defconfig +++ b/configs/pjrc-8051/defconfig @@ -149,6 +149,7 @@ CONFIG_DISABLE_PTHREAD=y CONFIG_DISABLE_SIGNALS=y CONFIG_DISABLE_MQUEUE=y CONFIG_DISABLE_MOUNTPOINT=y +CONFIG_DISABLE_ENVIRON=y # # Misc libc settings diff --git a/configs/sim/defconfig b/configs/sim/defconfig index e5940848ea2ddfeba43c9b6c65aa403bc9ffeaed..e60848b0a9f95eda680c98f2a7c3b45f53bcebda 100644 --- a/configs/sim/defconfig +++ b/configs/sim/defconfig @@ -117,6 +117,7 @@ CONFIG_DISABLE_PTHREAD=n CONFIG_DISABLE_SIGNALS=n CONFIG_DISABLE_MQUEUE=n CONFIG_DISABLE_MOUNTPOINT=n +CONFIG_DISABLE_ENVIRON=n # # Misc libc settings diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 494f5dcd9c0e96fabff5e8f69ab6575ff1922731..83f59e64dafc72f16fcded159222cf6ac3dccbb0 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -147,6 +147,19 @@ typedef void (*exitfunc_t)(void); typedef struct msgq_s msgq_t; +/* The structure used to maintain environment variables */ +#ifndef CONFIG_DISABLE_ENVIRON +struct environ_s +{ + unsigned int ev_crefs; /* Reference count used when environment + * is shared by threads */ + size_t ev_alloc; /* Number of bytes allocated in environment */ + ubyte ev_env[1]; /* Environment strings */ +}; +typedef struct environ_s environ_t; +# define SIZEOF_ENVIRON_T(alloc) (sizeof(environ_t) + alloc - 1) +#endif + /* This is the task control block (TCB) */ struct _TCB @@ -175,11 +188,14 @@ struct _TCB ubyte init_priority; /* Initial priority of the task */ char *argv[CONFIG_MAX_TASK_ARGS+1]; /* Name+start-up parameters */ +#ifndef CONFIG_DISABLE_ENVIRON + FAR environ_t *envp; /* Environment variables */ +#endif /* Stack-Related Fields *******************************************************/ #ifndef CONFIG_CUSTOM_STACK - size_t adj_stack_size; /* Stack size after adjustment */ + size_t adj_stack_size; /* Stack size after adjustment */ /* for hardware, processor, etc. */ /* (for debug purposes only) */ FAR void *stack_alloc_ptr; /* Pointer to allocated stack */ diff --git a/include/stdlib.h b/include/stdlib.h index 4a9f2ef0d6f52c14eeadc792a305d6279d46436a..6e7f44112240583891e1059b3ca53a4fa4e1c414 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -40,6 +40,7 @@ * Included Files ************************************************************/ +#include <nuttx/config.h> #include <sys/types.h> /************************************************************ @@ -54,6 +55,15 @@ #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 +/* The environ variable, normally 'extern char **environ;' is + * not implemented as a function call. However, get_environ_ptr() + * can be used in its place. + */ + +#ifndef CONFIG_DISABLE_ENIVRON +# define environ get_environ_ptr() +#endif + /************************************************************ * Global Type Definitions ************************************************************/ @@ -70,6 +80,10 @@ struct mallinfo * by free (not in use) chunks.*/ }; +/************************************************************ + * Global Function Prototypes + ************************************************************/ + /************************************************************ * Global Function Prototypes ************************************************************/ @@ -89,7 +103,14 @@ EXTERN int rand(void); /* Environment variable support */ -EXTERN char *getenv(const char *name); +#ifndef CONFIG_DISABLE_ENIVRON +EXTERN FAR char **get_environ_ptr( void ); +EXTERN FAR char *getenv(const char *name); +EXTERN int putenv(char *string); +EXTERN int clearenv(void); +EXTERN int setenv(const char *name, const char *value, int overwrite); +EXTERN int unsetenv(const char *name); +#endif /* Process exit functions */ diff --git a/sched/Makefile b/sched/Makefile index c007feaf42d1e2ddb87403ea4a577566fdab9d8b..97c59201e709f99814155188ce44a1a53c2159f7 100644 --- a/sched/Makefile +++ b/sched/Makefile @@ -55,6 +55,8 @@ SCHED_SRCS = sched_setparam.c sched_getparam.c \ sched_yield.c sched_rrgetinterval.c sched_foreach.c \ sched_getprioritymax.c sched_getprioritymin.c \ sched_lock.c sched_unlock.c sched_lockcount.c +ENV_SRCS = env_getenvironptr.c env_dup.c env_share.c env_release.c \ + env_clearenv.c WDOG_SRCS = wd_initialize.c wd_create.c wd_start.c wd_cancel.c wd_delete.c \ wd_gettime.c TIME_SRCS = sched_processtimer.c @@ -128,6 +130,9 @@ endif ifneq ($(CONFIG_DISABLE_MQUEUE),y) CSRCS += $(MQUEUE_SRCS) endif +ifneq ($(CONFIG_DISABLE_ENVIRON),y) +CSRCS += $(ENV_SRCS) +endif COBJS = $(CSRCS:.c=$(OBJEXT)) SRCS = $(ASRCS) $(CSRCS) diff --git a/sched/env_clearenv.c b/sched/env_clearenv.c new file mode 100644 index 0000000000000000000000000000000000000000..1181596ea1adbf2f67d4196f9f4386a19fc3311e --- /dev/null +++ b/sched/env_clearenv.c @@ -0,0 +1,83 @@ +/**************************************************************************** + * env_clearenv.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 Gregory Nutt 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 <nuttx/config.h> + +#ifndef CONFIG_DISABLE_ENVIRON + +#include <sched.h> +#include <stdlib.h> +#include "os_internal.h" +#include "env_internal.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: clearenv + * + * Description: + * The clearenv() function clears the environment of all name-value pairs + * and sets the value of the external variable environ to NULL. + + * Parameters: + * None + * + * Return Value: + * None + * + * Assumptions: + * Not called from an interrupt handler + * + ****************************************************************************/ + +int clearenv(void) +{ + return env_release((FAR _TCB*)g_readytorun.head); +} + +#endif /* CONFIG_DISABLE_ENVIRON */ + + + diff --git a/sched/env_dup.c b/sched/env_dup.c new file mode 100644 index 0000000000000000000000000000000000000000..0ee1cbc81d067f3cac235ff6fbd5e04233cbb0a2 --- /dev/null +++ b/sched/env_dup.c @@ -0,0 +1,127 @@ +/**************************************************************************** + * env_dup.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 Gregory Nutt 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 <nuttx/config.h> + +#ifndef CONFIG_DISABLE_ENVIRON + +#include <sched.h> +#include <string.h> +#include <errno.h> +#include "os_internal.h" +#include "env_internal.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: env_dup + * + * Description: + * Copy the internal environment structure of a task. This is the action + * that is performed when a new task is created: The new task has a private, + * exact duplicate of the parent task's environment. + * + * Parameters: + * ptcb The tcb to receive the newly allocated copy of the parspecifiedent + * TCB's environment structure with reference count equal to one + * + * Return Value: + * zero on success + * + * Assumptions: + * Not called from an interrupt handler. + * + ****************************************************************************/ + +int env_dup(FAR _TCB *ptcb) +{ + int ret = OK; + if (!ptcb ) + { + ret = -EINVAL; + } + else + { + FAR _TCB *parent = (FAR _TCB*)g_readytorun.head; + environ_t *envp = NULL; + + /* Pre-emption must be disabled throughout the following because the + * environment may be shared. + */ + + sched_lock(); + + /* Does the parent task have an environment? */ + + if (parent->envp) + { + /* Yes..The parent task has an environment, duplicate it */ + + size_t envlen = ptcb->envp->ev_alloc; + envp = (environ_t*)malloc(SIZEOF_ENVIRON_T( envlen )); + if (!envp) + { + ret = -ENOMEM; + } + else + { + envp->ev_crefs = 1; + envp->ev_alloc = envlen; + memcmp( envp->ev_env, ptcb->envp->ev_env, envlen ); + } + } + + /* Save the cloned environment in the new TCB */ + + ptcb->envp = envp; + sched_unlock(); + } + return ret; +} + +#endif /* CONFIG_DISABLE_ENVIRON */ + + + diff --git a/sched/env_dupenv.c b/sched/env_dupenv.c new file mode 100644 index 0000000000000000000000000000000000000000..748af878c85d939d3d9e9ddf6ab1be7da30e861d --- /dev/null +++ b/sched/env_dupenv.c @@ -0,0 +1,108 @@ +/**************************************************************************** + * env_dupenv.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 Gregory Nutt 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 <nuttx/config.h> + +#ifndef CONFIG_DISABLE_ENVIRON + +#include <sched.h> +#include <stdlib.h> +#include "os_internal.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: dupenv + * + * Description: + * Copy the internal environment structure of a task. This is the action + * that is performed when a new task is created: The new task has a private, + * exact duplicate of the parent task's environment. + * + * Parameters: + * None + * + * Return Value: + * A pointer to a newly allocated copy of the specified TCB's environment + * structure with reference count equal to one. + * + * Assumptions: + * Not called from an interrupt handler. + * + ****************************************************************************/ + +FAR environ_t *dupenv(FAR _TCB *ptcb) +{ + environ_t *envp = NULL; + + /* Pre-emption must be disabled throughout the following because the + * environment may be shared. + */ + + sched_lock(); + + /* Does the parent task have an environment? */ + + if (ptcb->envp) + { + /* Yes..The parent task has an environment, duplicate it */ + + size_t envlen = ptcb->envp->ev_alloc + envp = (environ_t*)malloc(SIZEOF_ENVIRON_T( envlen )); + if (envp) + { + envp->ev_crefs = 1; + envp->ev_alloc = envlen; + memcmp( envp->ev_env, ptcb->envp->ev_env, envlen ); + } + } + sched_unlock(); + return envp; +} + +#endif /* CONFIG_DISABLE_ENVIRON */ + + + diff --git a/sched/env_getenvironptr.c b/sched/env_getenvironptr.c new file mode 100644 index 0000000000000000000000000000000000000000..4c01250b42ccba17c249b4fe68bbeba1b2c4ecbc --- /dev/null +++ b/sched/env_getenvironptr.c @@ -0,0 +1,92 @@ +/**************************************************************************** + * env_getenvironptr.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 Gregory Nutt 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 <nuttx/config.h> + +#ifndef CONFIG_DISABLE_ENVIRON + +#include <sched.h> +#include <stdlib.h> +#include "os_internal.h" + +#undef get_environ_ptr + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: get_environ_ptr + * + * Description: + * Return a pointer to the thread specific environ variable. + * + * Parameters: + * None + * + * Return Value: + * A pointer to the per-thread environ variable. + * + * Assumptions: + * + ****************************************************************************/ + +FAR char **get_environ_ptr( void ) +{ + /* Return a reference to the thread-private environ in the TCB.*/ + + FAR _TCB *ptcb = (FAR _TCB*)g_readytorun.head; + if (ptcb->envp) + { + return &ptcb->envp->ev_env; + } + else + { + return NULL; + } +} + +#endif /* CONFIG_DISABLE_ENVIRON */ + + + diff --git a/sched/env_internal.h b/sched/env_internal.h new file mode 100644 index 0000000000000000000000000000000000000000..5961362df367abfe319f2503d51cb470729c6905 --- /dev/null +++ b/sched/env_internal.h @@ -0,0 +1,88 @@ +/**************************************************************************** + * env_internal.h + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 Gregory Nutt 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 __ENV_INTERNAL_H +#define __ENV_INTERNAL_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/sched.h> +#include "os_internal.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#ifdef CONFIG_DISABLE_ENVIRON +# define env_dup(ptcb) (0) +# define env_share(ptcb) (0) +# define env_release(ptcb) (0) +#endif + +/**************************************************************************** + * Public Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" { +#else +#define EXTERN extern +#endif + +#ifndef CONFIG_DISABLE_ENVIRON +EXTERN int env_dup(FAR _TCB *ptcb); +EXTERN int env_share(FAR _TCB *ptcb); +EXTERN int env_release(FAR _TCB *ptcb); +#endif + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __ENV_INTERNAL_H */ + diff --git a/sched/env_release.c b/sched/env_release.c new file mode 100644 index 0000000000000000000000000000000000000000..96490588e50de36ffd0844fd54cca1c59f5122e7 --- /dev/null +++ b/sched/env_release.c @@ -0,0 +1,122 @@ +/**************************************************************************** + * env_clearenv.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 Gregory Nutt 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 <nuttx/config.h> + +#ifndef CONFIG_DISABLE_ENVIRON + +#include <sched.h> +#include <errno.h> +#include "os_internal.h" +#include "env_internal.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: env_release + * + * Description: + * The env_release() function clears the environment of all name-value pairs + * and sets the value of the external variable environ to NULL. + + * Parameters: + * ptcb Identifies the TCB containing the environment structure + * + * Return Value: + * zero on success + * + * Assumptions: + * Not called from an interrupt handler + * + ****************************************************************************/ + +int env_release(FAR _TCB *ptcb) +{ + int ret = OK; + if (!ptcb) + { + ret = -EINVAL; + } + else + { + FAR environ_t *envp; + + /* Examine the environ data in the TCB. Preemption is disabled because the + * the environment could be shared among threads. + */ + + sched_lock(); + envp = ptcb->envp; + if (ptcb->envp) + { + /* Check the reference count on the environment structure */ + + if ( envp->ev_crefs <= 1) + { + /* Decrementing the reference count will destroy the environment */ + + sched_free( envp ); /* plain free() should be fine here */ + } + else + { + /* The environment will persist after decrementing the reference + * count */ + + envp->ev_crefs--; + } + + /* In any case, the environment is no longer accessible on this thread */ + + ptcb->envp = NULL; + } + sched_unlock(); + } + return ret; +} + +#endif /* CONFIG_DISABLE_ENVIRON */ + + + diff --git a/sched/env_share.c b/sched/env_share.c new file mode 100644 index 0000000000000000000000000000000000000000..a1c740aa78ca920cdb85e05b306ddc5ef0378e15 --- /dev/null +++ b/sched/env_share.c @@ -0,0 +1,115 @@ +/**************************************************************************** + * env_share.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 Gregory Nutt 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 <nuttx/config.h> + +#ifndef CONFIG_DISABLE_ENVIRON + +#include <sched.h> +#include <errno.h> +#include "os_internal.h" +#include "env_internal.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: env_share + * + * Description: + * Increment the reference count on the internal environment structure of + * a task. This is the action that is performed when a new pthread is + * created: The new pthread shares the environment with its parent. + * + * Parameters: + * ptcb The new TCB to receive the shared environment. + * + * Return Value: + * A pointer to a specified TCB's environment structure with an incremented + * reference count. + * + * Assumptions: + * Not called from an interrupt handler. + * + ****************************************************************************/ + +int env_share(FAR _TCB *ptcb) +{ + int ret = OK; + if (!ptcb) + { + ret = -EINVAL; + } + else + { + FAR _TCB *parent = (FAR _TCB*)g_readytorun.head; + environ_t *envp = parent->envp; + + /* Pre-emption must be disabled throughout the following because the + * environment is shared. + */ + + sched_lock(); + + /* Does the parent task have an environment? */ + + if (envp) + { + /* Yes.. increment the reference count on the environment */ + + envp++; + } + + /* Then share the environment */ + + ptcb->envp = envp; + sched_unlock(); + } + return ret; +} + +#endif /* CONFIG_DISABLE_ENVIRON */ + + + diff --git a/sched/pthread_create.c b/sched/pthread_create.c index 9988673491876acec8c4f393e6aff6e2b4642768..24768664bf21f118bbe74d7bbf52951e610b4b6c 100644 --- a/sched/pthread_create.c +++ b/sched/pthread_create.c @@ -49,6 +49,7 @@ #include <nuttx/arch.h> #include "os_internal.h" #include "clock_internal.h" +#include "env_internal.h" #include "pthread_internal.h" /************************************************************ @@ -273,6 +274,10 @@ int pthread_create(pthread_t *thread, pthread_attr_t *attr, return ERROR; } + /* Share the parent's envionment */ + + (void)env_share(ptcb); + /* Allocate a detachable structure to support pthread_join logic */ pjoin = (FAR join_t*)kzmalloc(sizeof(join_t)); diff --git a/sched/sched_releasetcb.c b/sched/sched_releasetcb.c index 5d3c8fd90b474d491fa3b94666dfffb1a45d3100..95f533c206f9d701c454f5721eb0a9e4f87b6dd1 100644 --- a/sched/sched_releasetcb.c +++ b/sched/sched_releasetcb.c @@ -46,6 +46,7 @@ #include <nuttx/arch.h> #include "os_internal.h" #include "timer_internal.h" +#include "env_internal.h" /************************************************************ * Private Functions @@ -150,6 +151,10 @@ int sched_releasetcb(FAR _TCB *tcb) ret = sched_releasefiles(tcb); + /* Release environment variables */ + + (void)env_release(tcb); + /* And, finally, release the TCB itself */ sched_free(tcb); diff --git a/sched/task_create.c b/sched/task_create.c index 008cc378a0fb863e52c1d8c2e166442411a60cca..ee0e86c6e18e32e68817a16f1d39cabd20f15afe 100644 --- a/sched/task_create.c +++ b/sched/task_create.c @@ -44,6 +44,7 @@ #include <debug.h> #include <nuttx/arch.h> #include "os_internal.h" +#include "env_internal.h" /************************************************************ * Definitions @@ -137,6 +138,10 @@ int task_create(const char *name, int priority, } #endif + /* Clone the parent's task environment */ + + (void)env_dup(tcb); + /* Allocate the stack for the TCB */ #ifndef CONFIG_CUSTOM_STACK