Move environment files from sched/ to sched/environ

This commit is contained in:
Gregory Nutt
2014-08-08 13:53:29 -06:00
parent cb79407ced
commit e10a23ae50
36 changed files with 271 additions and 78 deletions
+47
View File
@@ -0,0 +1,47 @@
############################################################################
# sched/environ/Make.defs
#
# Copyright (C) 2014 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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.
#
############################################################################
ifneq ($(CONFIG_DISABLE_ENVIRON),y)
ENV_SRCS = env_getenvironptr.c env_dup.c env_release.c env_findvar.c
ENV_SRCS += env_removevar.c env_clearenv.c env_getenv.c env_putenv.c
ENV_SRCS += env_setenv.c env_unsetenv.c
# Include environ build support
DEPPATH += --dep-path environ
VPATH += :environ
endif
+85
View File
@@ -0,0 +1,85 @@
/****************************************************************************
* sched/environ/env_clearenv.c
*
* Copyright (C) 2007, 2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <sched.h>
#include <stdlib.h>
#include "os_internal.h"
#include "environ/environ.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: 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)
{
FAR struct tcb_s *tcb = (FAR struct tcb_s*)g_readytorun.head;
DEBUGASSERT(tcb->group);
env_release(tcb->group);
return OK;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+124
View File
@@ -0,0 +1,124 @@
/****************************************************************************
* sched/environ/env_dup.c
*
* Copyright (C) 2007, 2009, 2011, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <sys/types.h>
#include <sched.h>
#include <string.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include "os_internal.h"
#include "environ/environ.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: 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:
* group The child task group to receive the newly allocated copy of the
* parent task groups environment structure.
*
* Return Value:
* zero on success
*
* Assumptions:
* Not called from an interrupt handler.
*
****************************************************************************/
int env_dup(FAR struct task_group_s *group)
{
FAR struct tcb_s *ptcb = (FAR struct tcb_s*)g_readytorun.head;
FAR char *envp = NULL;
size_t envlen;
int ret = OK;
DEBUGASSERT(group && ptcb && ptcb->group);
/* 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->group && ptcb->group->tg_envp)
{
/* Yes..The parent task has an environment, duplicate it */
envlen = ptcb->group->tg_envsize;
envp = (FAR char *)kumalloc(envlen);
if (!envp)
{
ret = -ENOMEM;
}
else
{
group->tg_envsize = envlen;
group->tg_envp = envp;
memcpy(envp, ptcb->group->tg_envp, envlen);
}
}
sched_unlock();
return ret;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+126
View File
@@ -0,0 +1,126 @@
/****************************************************************************
* sched/environ/env_findvar.c
*
* Copyright (C) 2007, 2009, 2013-2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <stdbool.h>
#include <string.h>
#include <sched.h>
#include "environ/environ.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: env_cmpname
****************************************************************************/
static bool env_cmpname(const char *pszname, const char *peqname)
{
/* Search until we find anything different in the two names */
for (; *pszname == *peqname; pszname++, peqname++);
/* On sucess, pszname will end with '\0' and peqname with '=' */
if ( *pszname == '\0' && *peqname == '=' )
{
return true;
}
return false;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: env_findvar
*
* Description:
* Search the provided environment structure for the variable of the
* specified name.
*
* Parameters:
* group The task group containging environment array to be searched.
* pname The variable name to find
*
* Return Value:
* A pointer to the name=value string in the environment
*
* Assumptions:
* - Not called from an interrupt handler
* - Pre-emptions is disabled by caller
*
****************************************************************************/
FAR char *env_findvar(FAR struct task_group_s *group, const char *pname)
{
char *ptr;
char *end;
/* Verify input parameters */
DEBUGASSERT(group && pname);
/* Search for a name=value string with matching name */
end = &group->tg_envp[group->tg_envsize];
for (ptr = group->tg_envp;
ptr < end && !env_cmpname(pname, ptr);
ptr += (strlen(ptr) + 1));
/* Check for success */
return (ptr < end) ? ptr : NULL;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+133
View File
@@ -0,0 +1,133 @@
/****************************************************************************
* env_getenv.c
*
* Copyright (C) 2007, 2008, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <sched.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "os_internal.h"
#include "environ/environ.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: getenv
*
* Description:
* The getenv() function searches the environment list for a string that
* matches the string pointed to by name.
*
* Parameters:
* name - The name of the variable to find.
*
* Return Value:
* The value of the valiable (read-only) or NULL on failure
*
* Assumptions:
* Not called from an interrupt handler
*
****************************************************************************/
FAR char *getenv(const char *name)
{
FAR struct tcb_s *rtcb;
FAR struct task_group_s *group;
FAR char *pvar;
FAR char *pvalue = NULL;
int ret = OK;
/* Verify that a string was passed */
if (!name)
{
ret = EINVAL;
goto errout;
}
/* Get a reference to the thread-private environ in the TCB. */
sched_lock();
rtcb = (FAR struct tcb_s*)g_readytorun.head;
group = rtcb->group;
/* Check if the variable exists */
if ( !group || (pvar = env_findvar(group, name)) == NULL)
{
ret = ENOENT;
goto errout_with_lock;
}
/* It does! Get the value sub-string from the name=value string */
pvalue = strchr(pvar, '=');
if (!pvalue)
{
/* The name=value string has no '=' This is a bug! */
ret = EINVAL;
goto errout_with_lock;
}
/* Adjust the pointer so that it points to the value right after the '=' */
pvalue++;
sched_unlock();
return pvalue;
errout_with_lock:
sched_unlock();
errout:
set_errno(ret);
return NULL;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+102
View File
@@ -0,0 +1,102 @@
/****************************************************************************
* env_getenvironptr.c
*
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <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
****************************************************************************/
/****************************************************************************
* Name: 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 )
{
#if 1
/* Type of internal representation of environment is incompatible with
* char ** return value.
*/
return NULL;
#else
/* Return a reference to the thread-private environ in the TCB. */
FAR struct tcb_s *ptcb = (FAR struct tcb_s*)g_readytorun.head;
if (ptcb->envp)
{
return &ptcb->envp->ev_env;
}
else
{
return NULL;
}
#endif
}
#endif /* CONFIG_DISABLE_ENVIRON */
+123
View File
@@ -0,0 +1,123 @@
/****************************************************************************
* sched/environ/env_putenv.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <stdlib.h>
#include <sched.h>
#include <string.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: putenv
*
* Description:
* The putenv() function adds or changes the value of environment variables.
* The argument string is of the form name=value. If name does not already
* exist in the environment, then string is added to the environment. If
* name does exist, then the value of name in the environment is changed to
* value.
*
* Parameters:
* name=value string describing the environment setting to add/modify
*
* Return Value:
* Zero on sucess
*
* Assumptions:
* Not called from an interrupt handler
*
****************************************************************************/
int putenv(FAR const char *string)
{
char *pname;
char *pequal;
int ret = OK;
/* Verify that a string was passed */
if (!string)
{
ret = EINVAL;
goto errout;
}
/* Parse the name=value string */
pname = strdup(string);
if (!pname)
{
ret = ENOMEM;
goto errout;
}
pequal = strchr( pname, '=');
if (pequal)
{
/* Then let setenv do all of the work */
*pequal = '\0';
ret = setenv(pname, pequal+1, TRUE);
}
kfree(pname);
return ret;
errout:
errno = ret;
return ERROR;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+102
View File
@@ -0,0 +1,102 @@
/****************************************************************************
* sched/environ/env_release.c
*
* Copyright (C) 2007, 2009, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <sched.h>
#include <errno.h>
#include "os_internal.h"
#include "environ/environ.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: env_release
*
* Description:
* env_release() is called only from group_leave() when the last member of
* a task group exits. The env_release() function clears the environment
* of all name-value pairs and sets the value of the external variable
* environ to NULL.
*
* Parameters:
* group Identifies the task group containing the environment structure
* to be released.
*
* Return Value:
* None
*
* Assumptions:
* Not called from an interrupt handler
*
****************************************************************************/
void env_release(FAR struct task_group_s *group)
{
DEBUGASSERT(group);
/* Free any allocate environment strings */
if (group->tg_envp)
{
/* Free the environment */
sched_ufree(group->tg_envp);
}
/* In any event, make sure that all environment-related varialbles in the
* task group structure are reset to initial values.
*/
group->tg_envsize = 0;
group->tg_envp = NULL;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+123
View File
@@ -0,0 +1,123 @@
/****************************************************************************
* sched/environ/env_removevar.c
*
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <string.h>
#include <sched.h>
#include "environ/environ.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: env_removevar
*
* Description:
* Remove the referenced name=value pair from the environment
*
* Parameters:
* group The task group with the environment containing the name=value pair
* pvar A pointer to the name=value pair in the restroom
*
* Return Value:
* Zero on success
*
* Assumptions:
* - Not called from an interrupt handler
* - Caller has pre-emptions disabled
* - Caller will reallocate the environment structure to the correct size
*
****************************************************************************/
int env_removevar(FAR struct task_group_s *group, FAR char *pvar)
{
FAR char *end; /* Pointer to the end+1 of the environment */
int alloc; /* Size of the allocated environment */
int ret = ERROR;
DEBUGASSERT(group && pvar);
/* Verify that the pointer lies within the environment region */
alloc = group->tg_envsize; /* Size of the allocated environment */
end = &group->tg_envp[alloc]; /* Pointer to the end+1 of the environment */
if (pvar >= group->tg_envp && pvar < end)
{
/* Set up for the removal */
int len = strlen(pvar) + 1; /* Length of name=value string to remove */
char *src = &pvar[len]; /* Address of name=value string after */
char *dest = pvar; /* Location to move the next string */
int count = end - src; /* Number of bytes to move (might be zero) */
/* Move all of the environment strings after the removed one 'down.'
* this is inefficient, but robably not high duty.
*/
while (count-- > 0)
{
*dest++ = *src++;
}
/* Then set to the new allocation size. The caller is expected to
* call realloc at some point but we don't do that here because the
* caller may add more stuff to the environment.
*/
group->tg_envsize -= len;
ret = OK;
}
return ret;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+205
View File
@@ -0,0 +1,205 @@
/****************************************************************************
* sched/environ/env_setenv.c
*
* Copyright (C) 2007, 2009, 2011, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <string.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include "os_internal.h"
#include "environ/environ.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: setenv
*
* Description:
* The setenv() function adds the variable name to the environment with the
* specified 'value' if the varialbe 'name" does not exist. If the 'name'
* does exist in the environment, then its value is changed to 'value' if
* 'overwrite' is non-zero; if 'overwrite' is zero, then the value of name
* unaltered.
*
* Parameters:
* name - The name of the variable to change
* value - The new value of the variable
* overwrite - Replace any existing value if non-zero.
*
* Return Value:
* Zero on success
*
* Assumptions:
* Not called from an interrupt handler
*
****************************************************************************/
int setenv(FAR const char *name, FAR const char *value, int overwrite)
{
FAR struct tcb_s *rtcb;
FAR struct task_group_s *group;
FAR char *pvar;
FAR char *newenvp;
int newsize;
int varlen;
int ret = OK;
/* Verify input parameter */
if (!name)
{
ret = EINVAL;
goto errout;
}
/* if no value is provided, then this is the same as unsetenv (unless
* overwrite is false)
*/
if (!value || *value == '\0')
{
/* If overwite is set then this is the same as unsetenv */
if (overwrite)
{
return unsetenv(name);
}
else
{
/* Otherwise, it is a request to remove a variable without altering it? */
return OK;
}
}
/* Get a reference to the thread-private environ in the TCB. */
sched_lock();
rtcb = (FAR struct tcb_s*)g_readytorun.head;
group = rtcb->group;
DEBUGASSERT(group);
/* Check if the variable already exists */
if (group->tg_envp && (pvar = env_findvar(group, name)) != NULL)
{
/* It does! Do we have permission to overwrite the existing value? */
if (!overwrite)
{
/* No.. then just return success */
sched_unlock();
return OK;
}
/* Yes.. just remove the name=value pair from the environment. It will
* be added again below. Note that we are responsible for reallocating
* the environment buffer; this will happen below.
*/
(void)env_removevar(group, pvar);
}
/* Get the size of the new name=value string. The +2 is for the '=' and for
* null terminator
*/
varlen = strlen(name) + strlen(value) + 2;
/* Then allocate or reallocate the environment buffer */
if (group->tg_envp)
{
newsize = group->tg_envsize + varlen;
newenvp = (FAR char *)kurealloc(group->tg_envp, newsize);
if (!newenvp)
{
ret = ENOMEM;
goto errout_with_lock;
}
pvar = &newenvp[group->tg_envsize];
}
else
{
newsize = varlen;
newenvp = (FAR char *)kumalloc(varlen);
if (!newenvp)
{
ret = ENOMEM;
goto errout_with_lock;
}
pvar = newenvp;
}
/* Save the new buffer and size */
group->tg_envp = newenvp;
group->tg_envsize = newsize;
/* Now, put the new name=value string into the environment buffer */
sprintf(pvar, "%s=%s", name, value);
sched_unlock();
return OK;
errout_with_lock:
sched_unlock();
errout:
errno = ret;
return ERROR;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+124
View File
@@ -0,0 +1,124 @@
/****************************************************************************
* sched/environ/env_unsetenv.c
*
* Copyright (C) 2007, 2009, 2011, 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#ifndef CONFIG_DISABLE_ENVIRON
#include <sched.h>
#include <string.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include "os_internal.h"
#include "environ/environ.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: unsetenv
*
* Description:
* The unsetenv() function deletes the variable name from the environment.
*
* Parameters:
* name - The name of the variable to delete
*
* Return Value:
* Zero on success
*
* Assumptions:
* Not called from an interrupt handler
*
****************************************************************************/
int unsetenv(FAR const char *name)
{
FAR struct tcb_s *rtcb = (FAR struct tcb_s*)g_readytorun.head;
FAR struct task_group_s *group = rtcb->group;
FAR char *pvar;
FAR char *newenvp;
int newsize;
int ret = OK;
DEBUGASSERT(name && group);
/* Check if the variable exists */
sched_lock();
if (group && (pvar = env_findvar(group, name)) != NULL)
{
/* It does! Remove the name=value pair from the environment. */
(void)env_removevar(group, pvar);
/* Reallocate the new environment buffer */
newsize = group->tg_envsize;
newenvp = (FAR char *)kurealloc(group->tg_envp, newsize);
if (!newenvp)
{
set_errno(ENOMEM);
ret = ERROR;
}
else
{
/* Save the new environment pointer (it might have changed due to
* reallocation.
*/
group->tg_envp = newenvp;
}
}
sched_unlock();
return ret;
}
#endif /* CONFIG_DISABLE_ENVIRON */
+93
View File
@@ -0,0 +1,93 @@
/****************************************************************************
* sched/environ/environ.h
*
* Copyright (C) 2007, 2009, 2013-2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 __SCHED_ENVIRON_ENVIRON_H
#define __SCHED_ENVIRON_ENVIRON_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/sched.h>
#include "os_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
#ifdef CONFIG_DISABLE_ENVIRON
# define env_dup(group) (0)
# define env_release(group) (0)
#else
/****************************************************************************
* Public Type Declarations
****************************************************************************/
/****************************************************************************
* Public Variables
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* Functions used by the task/pthread creation and destruction logic */
int env_dup(FAR struct task_group_s *group);
void env_release(FAR struct task_group_s *group);
/* Functions used internally by the environment handling logic */
FAR char *env_findvar(FAR struct task_group_s *group, FAR const char *pname);
int env_removevar(FAR struct task_group_s *group, FAR char *pvar);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* !CONFIG_DISABLE_ENVIRON */
#endif /* __SCHED_ENVIRON_ENVIRON_H */