mirror of
https://github.com/apache/nuttx.git
synced 2026-06-04 23:03:27 +08:00
fs/proc and sched/environ: Add support for a procfs entry that will permit examining the environment of any task.
This commit is contained in:
@@ -37,7 +37,7 @@ ifneq ($(CONFIG_DISABLE_ENVIRON),y)
|
||||
|
||||
CSRCS += env_getenvironptr.c env_dup.c env_release.c env_findvar.c
|
||||
CSRCS += env_removevar.c env_clearenv.c env_getenv.c env_putenv.c
|
||||
CSRCS += env_setenv.c env_unsetenv.c
|
||||
CSRCS += env_setenv.c env_unsetenv.c env_foreach.c
|
||||
|
||||
# Include environ build support
|
||||
|
||||
|
||||
@@ -64,8 +64,8 @@
|
||||
* exact duplicate of the parent task's environment.
|
||||
*
|
||||
* Input Parameters:
|
||||
* group The child task group to receive the newly allocated copy of the
|
||||
* parent task groups environment structure.
|
||||
* group - The child task group to receive the newly allocated copy of the
|
||||
* parent task groups environment structure.
|
||||
*
|
||||
* Returned Value:
|
||||
* zero on success
|
||||
|
||||
+11
-12
@@ -59,9 +59,11 @@ 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++);
|
||||
for (; *pszname == *peqname; pszname++, peqname++)
|
||||
{
|
||||
}
|
||||
|
||||
/* On sucess, pszname will end with '\0' and peqname with '=' */
|
||||
/* On success, pszname will end with '\0' and peqname with '=' */
|
||||
|
||||
if (*pszname == '\0' && *peqname == '=')
|
||||
{
|
||||
@@ -83,26 +85,26 @@ static bool env_cmpname(const char *pszname, const char *peqname)
|
||||
* specified name.
|
||||
*
|
||||
* Input Parameters:
|
||||
* group The task group containging environment array to be searched.
|
||||
* pname The variable name to find
|
||||
* group - The task group containing environment array to be searched.
|
||||
* pname - The variable name to find
|
||||
*
|
||||
* Returned Value:
|
||||
* A pointer to the name=value string in the environment
|
||||
*
|
||||
* Assumptions:
|
||||
* - Not called from an interrupt handler
|
||||
* - Pre-emptions is disabled by caller
|
||||
* - Pre-emption is disabled by caller
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *env_findvar(FAR struct task_group_s *group, const char *pname)
|
||||
FAR char *env_findvar(FAR struct task_group_s *group, FAR const char *pname)
|
||||
{
|
||||
char *ptr;
|
||||
char *end;
|
||||
FAR char *ptr;
|
||||
FAR char *end;
|
||||
|
||||
/* Verify input parameters */
|
||||
|
||||
DEBUGASSERT(group && pname);
|
||||
DEBUGASSERT(group != NULL && pname != NULL);
|
||||
|
||||
/* Search for a name=value string with matching name */
|
||||
|
||||
@@ -117,6 +119,3 @@ FAR char *env_findvar(FAR struct task_group_s *group, const char *pname)
|
||||
}
|
||||
|
||||
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/****************************************************************************
|
||||
* sched/environ/env_foreach.c
|
||||
*
|
||||
* Copyright (C) 2018 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 <nuttx/environ.h>
|
||||
|
||||
#include "environ/environ.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: env_foreach
|
||||
*
|
||||
* Description:
|
||||
* Search the provided environment structure for the variable of the
|
||||
* specified name.
|
||||
*
|
||||
* Input Parameters:
|
||||
* group - The task group containing environment array to be searched.
|
||||
* cb - The callback function to be invoked for each environment
|
||||
* variable.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero if the all environment variables have been traversed. A non-zero
|
||||
* value means that the callback function requested early termination by
|
||||
* returning a nonzero value.
|
||||
*
|
||||
* Assumptions:
|
||||
* - Not called from an interrupt handler
|
||||
* - Pre-emptions is disabled by caller
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int env_foreach(FAR struct task_group_s *group, env_foreach_t cb, FAR void *arg)
|
||||
{
|
||||
FAR char *ptr;
|
||||
FAR char *end;
|
||||
int ret = OK;
|
||||
|
||||
/* Verify input parameters */
|
||||
|
||||
DEBUGASSERT(group != NULL && cb != NULL);
|
||||
|
||||
/* Search for a name=value string with matching name */
|
||||
|
||||
end = &group->tg_envp[group->tg_envsize];
|
||||
for (ptr = group->tg_envp; ptr < end; ptr += (strlen(ptr) + 1))
|
||||
{
|
||||
/* Perform the callback */
|
||||
|
||||
ret = cb(arg, ptr);
|
||||
|
||||
/* Terminate the traversal early if the callback so requests by
|
||||
* returning a non-zero value.
|
||||
*/
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||
@@ -71,7 +71,7 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *getenv(const char *name)
|
||||
FAR char *getenv(FAR const char *name)
|
||||
{
|
||||
FAR struct tcb_s *rtcb;
|
||||
FAR struct task_group_s *group;
|
||||
|
||||
@@ -62,8 +62,8 @@
|
||||
* environ to NULL.
|
||||
*
|
||||
* Input Parameters:
|
||||
* group Identifies the task group containing the environment structure
|
||||
* to be released.
|
||||
* group - Identifies the task group containing the environment structure
|
||||
* to be released.
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
@@ -75,7 +75,7 @@
|
||||
|
||||
void env_release(FAR struct task_group_s *group)
|
||||
{
|
||||
DEBUGASSERT(group);
|
||||
DEBUGASSERT(group != NULL);
|
||||
|
||||
/* Free any allocate environment strings */
|
||||
|
||||
|
||||
@@ -57,15 +57,16 @@
|
||||
* Remove the referenced name=value pair from the environment
|
||||
*
|
||||
* Input Parameters:
|
||||
* group The task group with the environment containing the name=value pair
|
||||
* pvar A pointer to the name=value pair in the restroom
|
||||
* group - The task group with the environment containing the name=value
|
||||
* pair
|
||||
* pvar - A pointer to the name=value pair in the restroom
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero on success
|
||||
*
|
||||
* Assumptions:
|
||||
* - Not called from an interrupt handler
|
||||
* - Caller has pre-emptions disabled
|
||||
* - Caller has pre-emption disabled
|
||||
* - Caller will reallocate the environment structure to the correct size
|
||||
*
|
||||
****************************************************************************/
|
||||
@@ -76,7 +77,7 @@ int env_removevar(FAR struct task_group_s *group, FAR char *pvar)
|
||||
int alloc; /* Size of the allocated environment */
|
||||
int ret = ERROR;
|
||||
|
||||
DEBUGASSERT(group && pvar);
|
||||
DEBUGASSERT(group != NULL && pvar != NULL);
|
||||
|
||||
/* Verify that the pointer lies within the environment region */
|
||||
|
||||
|
||||
+86
-5
@@ -1,7 +1,8 @@
|
||||
/****************************************************************************
|
||||
* sched/environ/environ.h
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2013-2014 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2007, 2009, 2013-2014, 2018 Gregory Nutt. All rights
|
||||
* reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -48,8 +49,8 @@
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_DISABLE_ENVIRON
|
||||
# define env_dup(group) (0)
|
||||
# define env_release(group) (0)
|
||||
# define env_dup(group) (0)
|
||||
# define env_release(group) (0)
|
||||
#else
|
||||
|
||||
/****************************************************************************
|
||||
@@ -68,14 +69,94 @@ extern "C"
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/* Functions used by the task/pthread creation and destruction logic */
|
||||
/****************************************************************************
|
||||
* 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.
|
||||
*
|
||||
* Input Parameters:
|
||||
* group - The child task group to receive the newly allocated copy of the
|
||||
* parent task groups environment structure.
|
||||
*
|
||||
* Returned Value:
|
||||
* zero on success
|
||||
*
|
||||
* Assumptions:
|
||||
* Not called from an interrupt handler.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int env_dup(FAR struct task_group_s *group);
|
||||
|
||||
/****************************************************************************
|
||||
* 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.
|
||||
*
|
||||
* Input Parameters:
|
||||
* group - Identifies the task group containing the environment structure
|
||||
* to be released.
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Not called from an interrupt handler
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void env_release(FAR struct task_group_s *group);
|
||||
|
||||
/* Functions used internally by the environment handling logic */
|
||||
/****************************************************************************
|
||||
* Name: env_findvar
|
||||
*
|
||||
* Description:
|
||||
* Search the provided environment structure for the variable of the
|
||||
* specified name.
|
||||
*
|
||||
* Input Parameters:
|
||||
* group - The task group containing environment array to be searched.
|
||||
* pname - The variable name to find
|
||||
*
|
||||
* Returned Value:
|
||||
* A pointer to the name=value string in the environment
|
||||
*
|
||||
* Assumptions:
|
||||
* - Not called from an interrupt handler
|
||||
* - Pre-emption is disabled by caller
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR char *env_findvar(FAR struct task_group_s *group, FAR const char *pname);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: env_removevar
|
||||
*
|
||||
* Description:
|
||||
* Remove the referenced name=value pair from the environment
|
||||
*
|
||||
* Input Parameters:
|
||||
* group - The task group with the environment containing the name=value
|
||||
* pair
|
||||
* pvar - A pointer to the name=value pair in the restroom
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero on success
|
||||
*
|
||||
* Assumptions:
|
||||
* - Not called from an interrupt handler
|
||||
* - Caller has pre-emption disabled
|
||||
* - Caller will reallocate the environment structure to the correct size
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int env_removevar(FAR struct task_group_s *group, FAR char *pvar);
|
||||
|
||||
#undef EXTERN
|
||||
|
||||
Reference in New Issue
Block a user