9
0
Fork 0

Add basic tasking support for environment variables

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@291 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2007-06-30 17:05:44 +00:00
parent 3e6dbe195f
commit 1cbe5e978e
22 changed files with 810 additions and 5 deletions

View File

@ -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

View File

@ -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>

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 */

View File

@ -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 */

View File

@ -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)

View File

@ -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 */

127
nuttx/sched/env_dup.c Normal file
View File

@ -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 */

108
nuttx/sched/env_dupenv.c Normal file
View File

@ -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 */

View File

@ -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 */

View File

@ -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 */

122
nuttx/sched/env_release.c Normal file
View File

@ -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 */

115
nuttx/sched/env_share.c Normal file
View File

@ -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 */

View File

@ -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));

View File

@ -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);

View File

@ -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