You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2026/10/17 15:01:54 UTC

(nuttx) branch master updated (c6e866578ec -> 9fa99caa581)

This is an automated email from the ASF dual-hosted git repository.

acassis pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


    from c6e866578ec boards/nrf5x: register /dev/timer0 only if its TIMER is enabled
     new 4182f6faa54 fs/procfs/fs_procfsproc.c: Indent the switch statements.
     new e1dc2741b39 environ: change the tg_en* from task_group_s to task_info_s
     new 52bf792d02e environ: move the environ from sched/environ to libc/environ
     new 483eea3d086 environ: make the environ variable addressable
     new 36562a7c2ef libs/libc/environ: Allocate the environment in the heap of its task.
     new 9fa99caa581 libs/libc/environ: Keep the strings that libc allocated until exit.

The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 binfmt/binfmt_execmodule.c                         |   4 +-
 fs/procfs/fs_procfsproc.c                          |  92 ++++-----
 include/nuttx/environ.h                            |  71 ++++++-
 include/nuttx/sched.h                              |   8 -
 include/nuttx/tls.h                                |  13 +-
 include/stdlib.h                                   |   4 +-
 include/sys/syscall_lookup.h                       |  11 --
 libs/libc/Makefile                                 |   1 +
 {sched => libs/libc}/environ/CMakeLists.txt        |  31 ++--
 {sched => libs/libc}/environ/Make.defs             |   3 +-
 libs/libc/environ/env_alloc.c                      | 206 +++++++++++++++++++++
 {sched => libs/libc}/environ/env_clearenv.c        |  36 ++--
 {sched => libs/libc}/environ/env_dup.c             | 100 +++++-----
 {sched => libs/libc}/environ/env_findvar.c         |  49 ++---
 {sched => libs/libc}/environ/env_foreach.c         |  26 +--
 {sched => libs/libc}/environ/env_getenv.c          |  50 ++---
 {sched => libs/libc}/environ/env_getenvironptr.c   |  18 +-
 {sched => libs/libc}/environ/env_putenv.c          |  49 ++---
 {sched => libs/libc}/environ/env_release.c         |  48 ++---
 .../pthread_exit.c => environ/env_removevar.c}     |  43 ++---
 {sched => libs/libc}/environ/env_secureexec.c      |  35 +++-
 {sched => libs/libc}/environ/env_setenv.c          | 206 +++++++++------------
 {sched => libs/libc}/environ/env_unsetenv.c        |  41 ++--
 {sched => libs/libc}/environ/environ.h             | 118 +++++++-----
 libs/libc/pthread/pthread_atfork.c                 |   4 +-
 libs/libc/pthread/pthread_keycreate.c              |   4 +-
 libs/libc/pthread/pthread_keydelete.c              |   4 +-
 libs/libc/stdlib/lib_atexit.c                      |   8 +-
 libs/libc/unistd/lib_fork.c                        |  12 +-
 sched/Makefile                                     |   1 -
 sched/environ/env_removevar.c                      | 106 -----------
 sched/group/group_join.c                           |   1 -
 sched/group/group_leave.c                          |   5 -
 sched/task/task_fork.c                             |   4 +-
 sched/task/task_init.c                             |   4 +-
 sched/tls/task_initinfo.c                          |   2 +-
 sched/tls/task_uninitinfo.c                        |   7 +-
 syscall/syscall.csv                                |   6 -
 38 files changed, 746 insertions(+), 685 deletions(-)
 rename {sched => libs/libc}/environ/CMakeLists.txt (73%)
 rename {sched => libs/libc}/environ/Make.defs (96%)
 create mode 100644 libs/libc/environ/env_alloc.c
 rename {sched => libs/libc}/environ/env_clearenv.c (76%)
 rename {sched => libs/libc}/environ/env_dup.c (61%)
 rename {sched => libs/libc}/environ/env_findvar.c (73%)
 rename {sched => libs/libc}/environ/env_foreach.c (82%)
 rename {sched => libs/libc}/environ/env_getenv.c (77%)
 rename {sched => libs/libc}/environ/env_getenvironptr.c (85%)
 rename {sched => libs/libc}/environ/env_putenv.c (79%)
 rename {sched => libs/libc}/environ/env_release.c (66%)
 copy libs/libc/{pthread/pthread_exit.c => environ/env_removevar.c} (65%)
 rename {sched => libs/libc}/environ/env_secureexec.c (82%)
 rename {sched => libs/libc}/environ/env_setenv.c (59%)
 rename {sched => libs/libc}/environ/env_unsetenv.c (79%)
 rename {sched => libs/libc}/environ/environ.h (56%)
 delete mode 100644 sched/environ/env_removevar.c


(nuttx) 02/06: environ: change the tg_en* from task_group_s to task_info_s

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit e1dc2741b39c524121d44b7484c48d98039a489f
Author: guoshichao <gu...@xiaomi.com>
AuthorDate: Mon Nov 10 21:58:29 2025 +0800

    environ: change the tg_en* from task_group_s to task_info_s
    
    Since tg_envp is placed in task_info_s, both tg_envp itself and the
    memory it points to (group_malloc) are userspace memory.
    There's no need to switch to kernel mode to access tg_env, which can
    improve access speed.
    
    Signed-off-by: guoshichao <gu...@xiaomi.com>
    (cherry picked from commit e60e6de82f6bf1e72139fcdba73ff3e26b951869
     in https://github.com/open-vela/nuttx)
    
    Port to Apache master: env_sanitize_secure() (env_secureexec.c, added
    later on master) also reads the environment from the task info.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 include/nuttx/sched.h             |  8 --------
 include/nuttx/tls.h               |  7 +++++++
 sched/environ/env_dup.c           | 17 ++++++++++------
 sched/environ/env_findvar.c       | 13 +++++++++----
 sched/environ/env_foreach.c       | 14 ++++++++-----
 sched/environ/env_getenv.c        |  5 ++++-
 sched/environ/env_getenvironptr.c |  4 +++-
 sched/environ/env_release.c       | 20 +++++++++++--------
 sched/environ/env_removevar.c     | 41 +++++++++++++++++++++++----------------
 sched/environ/env_secureexec.c    | 11 ++++++++---
 sched/environ/env_setenv.c        | 33 ++++++++++++++++++-------------
 sched/group/group_leave.c         |  8 ++++----
 12 files changed, 110 insertions(+), 71 deletions(-)

diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index ddb650e2d9f..76ea460379e 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -535,14 +535,6 @@ struct task_group_s
   sigset_t tg_sigdefault;           /* Set of signals set to the default action */
 #endif
 
-#ifndef CONFIG_DISABLE_ENVIRON
-  /* Environment variables **************************************************/
-
-  FAR char **tg_envp;               /* Allocated environment strings        */
-  ssize_t    tg_envpc;              /* Maximum entries of environment array */
-  ssize_t    tg_envc;               /* Number of environment strings        */
-#endif
-
 #ifndef CONFIG_DISABLE_POSIX_TIMERS
   /* Interval timer *********************************************************/
 
diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index 256bfd15a14..e0464b3248e 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -162,6 +162,13 @@ struct task_info_s
   struct list_node ta_atfork; /* Holds the pthread_atfork_s list */
 #endif
   pid_t ta_pid; /* Process ID */
+#ifndef CONFIG_DISABLE_ENVIRON
+  /* Environment variables **************************************************/
+
+  FAR char            **ta_envp;    /* Allocated environment strings        */
+  ssize_t               ta_envpc;   /* Maximum entries of environment array */
+  ssize_t               ta_envc;    /* Number of environment strings        */
+#endif
 };
 
 /* struct tls_cleanup_s *****************************************************/
diff --git a/sched/environ/env_dup.c b/sched/environ/env_dup.c
index ae58b2a63f8..d2f16bc8dd0 100644
--- a/sched/environ/env_dup.c
+++ b/sched/environ/env_dup.c
@@ -35,6 +35,8 @@
 #include <errno.h>
 
 #include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/tls.h>
 
 #include "sched/sched.h"
 #include "environ/environ.h"
@@ -65,16 +67,19 @@
 
 int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
 {
+  FAR struct task_info_s *info;
   FAR char **envp = NULL;
   size_t envc = 0;
   size_t size;
   int ret = OK;
 
-  DEBUGASSERT(group != NULL);
+  DEBUGASSERT(group != NULL && group->tg_info != NULL);
+
+  info = group->tg_info;
 
   /* Is there an environment ? */
 
-  if (envcp != NULL && group->tg_envp == NULL)
+  if (envcp != NULL && info->ta_envp == NULL)
     {
       /* Pre-emption must be disabled throughout the following because the
        * environment may be shared.
@@ -89,8 +94,8 @@ int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
           envc++;
         }
 
-      group->tg_envc = envc;
-      group->tg_envpc = (envc + SCHED_ENVIRON_RESERVED + 1);
+      info->ta_envc = envc;
+      info->ta_envpc = (envc + SCHED_ENVIRON_RESERVED + 1);
 
       /* A special case is that the parent has an "empty" environment
        * allocation, i.e., there is an allocation in place but it
@@ -101,7 +106,7 @@ int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
         {
           /* There is an environment, duplicate it */
 
-          envp = group_malloc(group, sizeof(*envp) * group->tg_envpc);
+          envp = group_malloc(group, sizeof(*envp) * info->ta_envpc);
           if (envp == NULL)
             {
               /* The parent's environment can not be inherited due to a
@@ -140,7 +145,7 @@ int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
 
       /* Save the child environment allocation. */
 
-      group->tg_envp = envp;
+      info->ta_envp = envp;
       nxrmutex_unlock(&group->tg_mutex);
     }
 
diff --git a/sched/environ/env_findvar.c b/sched/environ/env_findvar.c
index 0a2f5413420..a0879a5454a 100644
--- a/sched/environ/env_findvar.c
+++ b/sched/environ/env_findvar.c
@@ -33,6 +33,8 @@
 #include <sched.h>
 #include <assert.h>
 
+#include <nuttx/tls.h>
+
 #include "environ/environ.h"
 
 /****************************************************************************
@@ -87,22 +89,25 @@ static bool env_cmpname(const char *pszname, const char *peqname)
 
 ssize_t env_findvar(FAR struct task_group_s *group, FAR const char *pname)
 {
+  FAR struct task_info_s *info;
   ssize_t i;
 
   /* Verify input parameters */
 
-  DEBUGASSERT(group != NULL && pname != NULL);
+  DEBUGASSERT(group != NULL && group->tg_info != NULL && pname != NULL);
+
+  info = group->tg_info;
 
-  if (group->tg_envp == NULL)
+  if (info->ta_envp == NULL)
     {
       return -ENOENT;
     }
 
   /* Search for a name=value string with matching name */
 
-  for (i = 0; group->tg_envp[i] != NULL; i++)
+  for (i = 0; info->ta_envp[i] != NULL; i++)
     {
-      if (env_cmpname(pname, group->tg_envp[i]))
+      if (env_cmpname(pname, info->ta_envp[i]))
         {
           return i;
         }
diff --git a/sched/environ/env_foreach.c b/sched/environ/env_foreach.c
index 5569bc2ca25..b552549bc4d 100644
--- a/sched/environ/env_foreach.c
+++ b/sched/environ/env_foreach.c
@@ -34,6 +34,7 @@
 #include <assert.h>
 
 #include <nuttx/environ.h>
+#include <nuttx/tls.h>
 
 #include "environ/environ.h"
 
@@ -67,23 +68,26 @@ int env_foreach(FAR struct task_group_s *group,
                 env_foreach_t cb,
                 FAR void *arg)
 {
+  FAR struct task_info_s *info;
   int ret = OK;
   size_t i;
 
   /* Verify input parameters */
 
-  DEBUGASSERT(group != NULL && cb != NULL);
+  DEBUGASSERT(group != NULL && group->tg_info != NULL && cb != NULL);
 
-  if (group->tg_envp == NULL)
+  info = group->tg_info;
+
+  if (info->ta_envp == NULL)
     {
       return ret;
     }
 
-  for (i = 0; group->tg_envp[i] != NULL; i++)
+  for (i = 0; info->ta_envp[i] != NULL; i++)
     {
       /* Perform the callback */
 
-      ret = cb(arg, group->tg_envp[i]);
+      ret = cb(arg, info->ta_envp[i]);
 
       /* Terminate the traversal early if the callback so requests by
        * returning a non-zero value.
@@ -95,7 +99,7 @@ int env_foreach(FAR struct task_group_s *group,
         }
     }
 
-  DEBUGASSERT(ret != OK || group->tg_envc == i);
+  DEBUGASSERT(ret != OK || info->ta_envc == i);
 
   return ret;
 }
diff --git a/sched/environ/env_getenv.c b/sched/environ/env_getenv.c
index 248fc4198f3..34142490f7a 100644
--- a/sched/environ/env_getenv.c
+++ b/sched/environ/env_getenv.c
@@ -33,6 +33,9 @@
 #include <stdlib.h>
 #include <errno.h>
 
+#include <nuttx/mutex.h>
+#include <nuttx/tls.h>
+
 #include "sched/sched.h"
 #include "environ/environ.h"
 
@@ -95,7 +98,7 @@ FAR char *getenv(FAR const char *name)
 
   /* It does!  Get the value sub-string from the name=value string */
 
-  pvalue = strchr(group->tg_envp[ret], '=');
+  pvalue = strchr(group->tg_info->ta_envp[ret], '=');
   if (pvalue == NULL)
     {
       nxrmutex_unlock(&group->tg_mutex);
diff --git a/sched/environ/env_getenvironptr.c b/sched/environ/env_getenvironptr.c
index d1e930f566e..bf5be54bc30 100644
--- a/sched/environ/env_getenvironptr.c
+++ b/sched/environ/env_getenvironptr.c
@@ -30,6 +30,8 @@
 
 #include <sched.h>
 #include <stdlib.h>
+#include <nuttx/tls.h>
+
 #include "sched/sched.h"
 
 #undef get_environ_ptr
@@ -58,7 +60,7 @@ FAR char **get_environ_ptr(void)
 {
   FAR struct tcb_s *tcb = this_task();
 
-  return tcb->group->tg_envp;
+  return tcb->group->tg_info->ta_envp;
 }
 
 #endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_release.c b/sched/environ/env_release.c
index 6ada8bf7908..d9b090662c7 100644
--- a/sched/environ/env_release.c
+++ b/sched/environ/env_release.c
@@ -33,6 +33,7 @@
 #include <errno.h>
 
 #include <nuttx/kmalloc.h>
+#include <nuttx/tls.h>
 
 #include "environ/environ.h"
 
@@ -63,31 +64,34 @@
 
 void env_release(FAR struct task_group_s *group)
 {
+  FAR struct task_info_s *info;
   int i;
 
-  DEBUGASSERT(group != NULL);
+  DEBUGASSERT(group != NULL && group->tg_info != NULL);
 
-  if (group->tg_envp)
+  info = group->tg_info;
+
+  if (info->ta_envp)
     {
       /* Free any allocate environment strings */
 
-      for (i = 0; group->tg_envp[i] != NULL; i++)
+      for (i = 0; info->ta_envp[i] != NULL; i++)
         {
-          group_free(group, group->tg_envp[i]);
+          group_free(group, info->ta_envp[i]);
         }
 
       /* Free the environment */
 
-      group_free(group, group->tg_envp);
+      group_free(group, info->ta_envp);
     }
 
   /* In any event, make sure that all environment-related variables in the
    * task group structure are reset to initial values.
    */
 
-  group->tg_envp  = NULL;
-  group->tg_envpc = 0;
-  group->tg_envc  = 0;
+  info->ta_envp  = NULL;
+  info->ta_envpc = 0;
+  info->ta_envc  = 0;
 }
 
 #endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_removevar.c b/sched/environ/env_removevar.c
index 5a08042ac4e..fd6eb0ef994 100644
--- a/sched/environ/env_removevar.c
+++ b/sched/environ/env_removevar.c
@@ -33,6 +33,7 @@
 #include <assert.h>
 
 #include <nuttx/kmalloc.h>
+#include <nuttx/tls.h>
 
 #include "environ/environ.h"
 
@@ -63,43 +64,49 @@
 
 void env_removevar(FAR struct task_group_s *group, ssize_t index)
 {
-  DEBUGASSERT(group != NULL && index >= 0 && index < group->tg_envc);
+  FAR struct task_info_s *info;
+
+  DEBUGASSERT(group != NULL && group->tg_info != NULL);
+
+  info = group->tg_info;
+
+  DEBUGASSERT(index >= 0 && index < info->ta_envc);
 
   /* Free the allocate environment string */
 
-  group_free(group, group->tg_envp[index]);
+  group_free(group, info->ta_envp[index]);
 
   /* Exchange the last env and the index env */
 
-  group->tg_envc--;
-  if (index == group->tg_envc)
+  info->ta_envc--;
+  if (index == info->ta_envc)
     {
-      group->tg_envp[index] = NULL;
+      info->ta_envp[index] = NULL;
     }
   else
     {
-      group->tg_envp[index] = group->tg_envp[group->tg_envc];
-      group->tg_envp[group->tg_envc] = NULL;
+      info->ta_envp[index] = info->ta_envp[info->ta_envc];
+      info->ta_envp[info->ta_envc] = NULL;
     }
 
   /* Free the old environment (if there was one) */
 
-  if (group->tg_envc == 0)
+  if (info->ta_envc == 0)
     {
-      group_free(group, group->tg_envp);
-      group->tg_envp = NULL;
-      group->tg_envpc = 0;
+      group_free(group, info->ta_envp);
+      info->ta_envp = NULL;
+      info->ta_envpc = 0;
     }
-  else if (group->tg_envc <=
-           (group->tg_envpc - SCHED_ENVIRON_RESERVED * 2))
+  else if (info->ta_envc <=
+           (info->ta_envpc - SCHED_ENVIRON_RESERVED * 2))
     {
       /* Reallocate the environment to reclaim a little memory */
 
-      group->tg_envpc = group->tg_envc + SCHED_ENVIRON_RESERVED + 1;
+      info->ta_envpc = info->ta_envc + SCHED_ENVIRON_RESERVED + 1;
 
-      group->tg_envp = group_realloc(group, group->tg_envp,
-         sizeof(*group->tg_envp) * group->tg_envpc);
-      DEBUGASSERT(group->tg_envp != NULL);
+      info->ta_envp = group_realloc(group, info->ta_envp,
+         sizeof(*info->ta_envp) * info->ta_envpc);
+      DEBUGASSERT(info->ta_envp != NULL);
     }
 }
 
diff --git a/sched/environ/env_secureexec.c b/sched/environ/env_secureexec.c
index 5315f496c14..231fd54ba37 100644
--- a/sched/environ/env_secureexec.c
+++ b/sched/environ/env_secureexec.c
@@ -31,6 +31,8 @@
 #include <stdlib.h>
 #include <assert.h>
 
+#include <nuttx/tls.h>
+
 #include "sched/sched.h"
 #include "environ/environ.h"
 
@@ -99,15 +101,18 @@ static bool env_is_unsafe(FAR const char *pair)
 #ifdef CONFIG_SCHED_USER_IDENTITY
 void env_sanitize_secure(FAR struct task_group_s *group)
 {
+  FAR struct task_info_s *info;
   ssize_t index;
 
-  DEBUGASSERT(group != NULL);
+  DEBUGASSERT(group != NULL && group->tg_info != NULL);
+
+  info = group->tg_info;
 
   nxrmutex_lock(&group->tg_mutex);
 
-  for (index = group->tg_envc - 1; index >= 0; )
+  for (index = info->ta_envc - 1; index >= 0; )
     {
-      FAR char *pair = group->tg_envp[index];
+      FAR char *pair = info->ta_envp[index];
 
       if (pair != NULL && env_is_unsafe(pair))
         {
diff --git a/sched/environ/env_setenv.c b/sched/environ/env_setenv.c
index ea24bdad99e..115a45a68d6 100644
--- a/sched/environ/env_setenv.c
+++ b/sched/environ/env_setenv.c
@@ -36,6 +36,8 @@
 #include <errno.h>
 
 #include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/tls.h>
 
 #include "sched/sched.h"
 #include "environ/environ.h"
@@ -71,6 +73,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
 {
   FAR struct tcb_s *rtcb;
   FAR struct task_group_s *group;
+  FAR struct task_info_s *info;
   FAR char *pvar;
   FAR char **envp;
   ssize_t envc;
@@ -112,13 +115,15 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
 
   rtcb  = this_task();
   group = rtcb->group;
-  DEBUGASSERT(group);
+  DEBUGASSERT(group && group->tg_info);
+
+  info = group->tg_info;
 
   nxrmutex_lock(&group->tg_mutex);
 
   /* Check if the variable already exists */
 
-  if (group->tg_envp && (ret = env_findvar(group, name)) >= 0)
+  if (info->ta_envp && (ret = env_findvar(group, name)) >= 0)
     {
       /* It does! Do we have permission to overwrite the existing value? */
 
@@ -140,7 +145,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
 
   /* Check current envirments count */
 
-  DEBUGASSERT(group->tg_envc < SSIZE_MAX);
+  DEBUGASSERT(info->ta_envc < SSIZE_MAX);
 
   /* Get the size of the new name=value string.
    * The +2 is for the '=' and for null terminator
@@ -157,9 +162,9 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
       goto errout_with_lock;
     }
 
-  envc = group->tg_envc;
+  envc = info->ta_envc;
 
-  if (group->tg_envp == NULL)
+  if (info->ta_envp == NULL)
     {
       envpc = SCHED_ENVIRON_RESERVED + 2;
 
@@ -170,29 +175,29 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
           goto errout_with_var;
         }
 
-      group->tg_envp  = envp;
-      group->tg_envpc = envpc;
+      info->ta_envp  = envp;
+      info->ta_envpc = envpc;
     }
-  else if (envc >= group->tg_envpc - 1)
+  else if (envc >= info->ta_envpc - 1)
     {
       envpc = envc + SCHED_ENVIRON_RESERVED + 2;
 
-      envp = group_realloc(group, group->tg_envp, sizeof(*envp) * envpc);
+      envp = group_realloc(group, info->ta_envp, sizeof(*envp) * envpc);
       if (envp == NULL)
         {
           ret = ENOMEM;
           goto errout_with_var;
         }
 
-      group->tg_envp  = envp;
-      group->tg_envpc = envpc;
+      info->ta_envp  = envp;
+      info->ta_envpc = envpc;
     }
 
   /* Save the new buffer and count */
 
-  group->tg_envp[envc++] = pvar;
-  group->tg_envp[envc]   = NULL;
-  group->tg_envc = envc;
+  info->ta_envp[envc++] = pvar;
+  info->ta_envp[envc]   = NULL;
+  info->ta_envc = envc;
 
   /* Now, put the new name=value string into the environment buffer */
 
diff --git a/sched/group/group_leave.c b/sched/group/group_leave.c
index 466f44be481..5752d1c4375 100644
--- a/sched/group/group_leave.c
+++ b/sched/group/group_leave.c
@@ -81,6 +81,10 @@ static inline void group_release(FAR struct task_group_s *group)
 
   nxrmutex_destroy(&group->tg_mutex);
 
+  /* Release all shared environment variables */
+
+  env_release(group);
+
   task_uninit_info(group);
 
 #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
@@ -119,10 +123,6 @@ static inline void group_release(FAR struct task_group_s *group)
     }
 #endif
 
-  /* Release all shared environment variables */
-
-  env_release(group);
-
   /* Destroy the mm_map list */
 
   mm_map_destroy(&group->tg_mm_map);


(nuttx) 06/06: libs/libc/environ: Keep the strings that libc allocated until exit.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 9fa99caa5810d599e434702e83ffb3a9f073edb1
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Tue Oct 13 06:42:53 2026 +0200

    libs/libc/environ: Keep the strings that libc allocated until exit.
    
    environ is an lvalue, so a program can assign it, as POSIX allows, and
    change its entries.  Both happen: gnulib's putenv() replaces an entry in
    place, and GNU make installs its own arrays.  libc then freed a string
    that the program put there, or an array on the program's stack.  For
    example, setenv() after an in-place replacement made free() assert.
    
    libc now frees no environment string before the task exits.  Each block
    that env_alloc() gives for the environment goes on a list of the task,
    and env_release() frees the list.  glibc also keeps these strings, so a
    pointer that getenv() returned stays valid after setenv() or unsetenv().
    env_alloc.c chooses the heap of the task and keeps the block header
    private.
    
    Before a change, env_reserve() makes environ an array that libc
    allocated.  If the program assigned its own array, libc copies it and
    does not change it.  The array of libc that it replaced stays until
    exit, because the program can assign it again.  env_dup() allocates the
    array and all of the strings in two blocks.
    
    putenv() passes the name part of its string to env_setvar(), a setenv()
    that takes the length of the name, so it needs no copy.  The counts of
    the environment are size_t.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 include/nuttx/tls.h                |  10 +-
 libs/libc/environ/CMakeLists.txt   |   3 +-
 libs/libc/environ/Make.defs        |   1 +
 libs/libc/environ/env_alloc.c      | 206 +++++++++++++++++++++++++++++++++++++
 libs/libc/environ/env_clearenv.c   |  17 ++-
 libs/libc/environ/env_dup.c        |  74 +++++++------
 libs/libc/environ/env_findvar.c    |  29 ++----
 libs/libc/environ/env_foreach.c    |   2 -
 libs/libc/environ/env_getenv.c     |   2 +-
 libs/libc/environ/env_putenv.c     |  39 +++----
 libs/libc/environ/env_release.c    |  24 ++---
 libs/libc/environ/env_removevar.c  |  44 ++------
 libs/libc/environ/env_secureexec.c |  21 +++-
 libs/libc/environ/env_setenv.c     | 187 +++++++++++++++------------------
 libs/libc/environ/env_unsetenv.c   |  16 ++-
 libs/libc/environ/environ.h        | 113 ++++++++++++++------
 16 files changed, 504 insertions(+), 284 deletions(-)

diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index 2e802dd05cf..bd103a73dca 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -165,9 +165,13 @@ struct task_info_s
 #ifndef CONFIG_DISABLE_ENVIRON
   /* Environment variables **************************************************/
 
-  FAR char            **ta_envp;    /* Allocated environment strings        */
-  ssize_t               ta_envpc;   /* Maximum entries of environment array */
-  ssize_t               ta_envc;    /* Number of environment strings        */
+  FAR char            **ta_envp;    /* The array that environ names         */
+  FAR char            **ta_envarr;  /* The array that libc allocated        */
+  FAR void             *ta_envmem;  /* Blocks that libc allocated, freed at
+                                     * exit
+                                     */
+  size_t                ta_envpc;   /* Maximum entries of environment array */
+  size_t                ta_envc;    /* Number of environment strings        */
 #endif
 };
 
diff --git a/libs/libc/environ/CMakeLists.txt b/libs/libc/environ/CMakeLists.txt
index a721bcfb091..316c854b9f8 100644
--- a/libs/libc/environ/CMakeLists.txt
+++ b/libs/libc/environ/CMakeLists.txt
@@ -33,6 +33,7 @@ if(NOT CONFIG_DISABLE_ENVIRON)
       env_setenv.c
       env_unsetenv.c
       env_foreach.c
-      env_secureexec.c)
+      env_secureexec.c
+      env_alloc.c)
   target_sources(c PRIVATE ${SRCS})
 endif()
diff --git a/libs/libc/environ/Make.defs b/libs/libc/environ/Make.defs
index 3c74a82f5f7..37e9fcb7ec3 100644
--- a/libs/libc/environ/Make.defs
+++ b/libs/libc/environ/Make.defs
@@ -25,6 +25,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 env_foreach.c env_secureexec.c
+CSRCS += env_alloc.c
 
 # Include environ build support
 
diff --git a/libs/libc/environ/env_alloc.c b/libs/libc/environ/env_alloc.c
new file mode 100644
index 00000000000..fb4a7b97213
--- /dev/null
+++ b/libs/libc/environ/env_alloc.c
@@ -0,0 +1,206 @@
+/****************************************************************************
+ * libs/libc/environ/env_alloc.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include <nuttx/tls.h>
+
+#ifdef __KERNEL__
+#  include <nuttx/kmalloc.h>
+#endif
+
+#include "environ/environ.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* Each block that env_alloc() gives starts with this header.  The list at
+ * ta_envmem and flink hold the address after the header, the address that
+ * env_alloc() returned.
+ */
+
+struct env_block_s
+{
+  FAR void *flink;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: env_heap_alloc
+ *
+ * Description:
+ *   The environment is in the heap that holds the task information: the
+ *   kernel heap for a kernel thread, the user heap for a user task.  Kernel
+ *   code also changes the environment of kernel threads (the idle task at
+ *   boot) and of a new user task.
+ *
+ ****************************************************************************/
+
+static FAR struct env_block_s *env_heap_alloc(FAR struct task_info_s *info,
+                                              size_t size)
+{
+#ifdef __KERNEL__
+  return kmm_heapmember(info) ? kmm_malloc(size) : kumm_malloc(size);
+#else
+  UNUSED(info);
+  return lib_umalloc(size);
+#endif
+}
+
+/****************************************************************************
+ * Name: env_heap_free
+ ****************************************************************************/
+
+static void env_heap_free(FAR struct task_info_s *info,
+                          FAR struct env_block_s *block)
+{
+#ifdef __KERNEL__
+  if (kmm_heapmember(info))
+    {
+      kmm_free(block);
+    }
+  else
+    {
+      kumm_free(block);
+    }
+#else
+  UNUSED(info);
+  lib_ufree(block);
+#endif
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: env_alloc
+ ****************************************************************************/
+
+FAR void *env_alloc(FAR struct task_info_s *info, size_t size)
+{
+  FAR struct env_block_s *block;
+
+  block = env_heap_alloc(info, sizeof(*block) + size);
+  if (block == NULL)
+    {
+      return NULL;
+    }
+
+  block->flink    = info->ta_envmem;
+  info->ta_envmem = block + 1;
+  return block + 1;
+}
+
+/****************************************************************************
+ * Name: env_free
+ ****************************************************************************/
+
+void env_free(FAR struct task_info_s *info, FAR void *mem)
+{
+  FAR struct env_block_s *block = (FAR struct env_block_s *)mem - 1;
+  FAR void **prev;
+
+  for (prev = &info->ta_envmem;
+       *prev != NULL;
+       prev = &((FAR struct env_block_s *)*prev - 1)->flink)
+    {
+      if (*prev == mem)
+        {
+          *prev = block->flink;
+          env_heap_free(info, block);
+          return;
+        }
+    }
+
+  DEBUGPANIC();
+}
+
+/****************************************************************************
+ * Name: env_reserve
+ ****************************************************************************/
+
+int env_reserve(FAR struct task_info_s *info, size_t more)
+{
+  FAR char **envp = info->ta_envp;
+  FAR char **arr;
+  size_t envc = 0;
+  size_t envpc;
+
+  /* The program can change the entries of environ in place, so count
+   * them again.
+   */
+
+  while (envp != NULL && envp[envc] != NULL)
+    {
+      envc++;
+    }
+
+  info->ta_envc = envc;
+
+  if (envp != NULL && envp == info->ta_envarr &&
+      envc + more < info->ta_envpc)
+    {
+      return OK;
+    }
+
+  envpc = envc + more + SCHED_ENVIRON_RESERVED + 1;
+  arr   = env_alloc(info, sizeof(*arr) * envpc);
+  if (arr == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  if (envc > 0)
+    {
+      memcpy(arr, envp, sizeof(*arr) * envc);
+    }
+
+  arr[envc] = NULL;
+
+  /* A full array of libc is not used again.  An array of libc that the
+   * program replaced stays until exit: the program can assign it to
+   * environ again.
+   */
+
+  if (envp != NULL && envp == info->ta_envarr)
+    {
+      env_free(info, envp);
+    }
+
+  info->ta_envp   = arr;
+  info->ta_envarr = arr;
+  info->ta_envpc  = envpc;
+  return OK;
+}
diff --git a/libs/libc/environ/env_clearenv.c b/libs/libc/environ/env_clearenv.c
index a87bb3f7f94..a37fabab739 100644
--- a/libs/libc/environ/env_clearenv.c
+++ b/libs/libc/environ/env_clearenv.c
@@ -27,6 +27,8 @@
 #include <nuttx/environ.h>
 #include <nuttx/tls.h>
 
+#include "environ/environ.h"
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -54,7 +56,20 @@ int clearenv(void)
   FAR struct task_info_s *info = task_get_info();
 
   nxrmutex_lock(&info->ta_lock);
-  env_release(info);
+
+  /* Free the array of libc if environ still names it.  The strings stay
+   * until exit, as a pointer that getenv() returned can still be in use.
+   */
+
+  if (info->ta_envarr != NULL && info->ta_envp == info->ta_envarr)
+    {
+      env_free(info, info->ta_envarr);
+    }
+
+  info->ta_envp   = NULL;
+  info->ta_envarr = NULL;
+  info->ta_envpc  = 0;
+  info->ta_envc   = 0;
   nxrmutex_unlock(&info->ta_lock);
   return OK;
 }
diff --git a/libs/libc/environ/env_dup.c b/libs/libc/environ/env_dup.c
index be85a18f564..351f66134bc 100644
--- a/libs/libc/environ/env_dup.c
+++ b/libs/libc/environ/env_dup.c
@@ -59,9 +59,11 @@
 
 int env_dup(FAR struct task_info_s *info, FAR char * const *envcp)
 {
-  FAR char **envp = NULL;
+  FAR char **envp;
+  FAR char *str;
   size_t envc = 0;
-  size_t size;
+  size_t size = 0;
+  size_t i;
   int ret = OK;
 
   DEBUGASSERT(info != NULL);
@@ -70,22 +72,15 @@ int env_dup(FAR struct task_info_s *info, FAR char * const *envcp)
 
   if (envcp != NULL && info->ta_envp == NULL)
     {
-      /* Pre-emption must be disabled throughout the following because the
-       * environment may be shared.
-       */
-
       nxrmutex_lock(&info->ta_lock);
 
-      /* Count the strings */
+      /* Count the strings and their size */
 
       while (envcp[envc] != NULL)
         {
-          envc++;
+          size += strlen(envcp[envc++]) + 1;
         }
 
-      info->ta_envc = envc;
-      info->ta_envpc = (envc + SCHED_ENVIRON_RESERVED + 1);
-
       /* A special case is that the parent has an "empty" environment
        * allocation, i.e., there is an allocation in place but it
        * contains no variable definitions and, hence, envc == 0.
@@ -93,48 +88,49 @@ int env_dup(FAR struct task_info_s *info, FAR char * const *envcp)
 
       if (envc > 0)
         {
-          /* There is an environment, duplicate it */
-
-          envp = env_malloc(info, sizeof(*envp) * info->ta_envpc);
-          if (envp == NULL)
+          /* Duplicate the parent environment: one block for the array and
+           * one for all of the strings.
+           */
+
+          info->ta_envpc = envc + SCHED_ENVIRON_RESERVED + 1;
+          envp = env_alloc(info, sizeof(*envp) * info->ta_envpc);
+          str  = env_alloc(info, size);
+          if (envp == NULL || str == NULL)
             {
               /* The parent's environment can not be inherited due to a
                * failure in the allocation of the child environment.
                */
 
+              if (envp != NULL)
+                {
+                  env_free(info, envp);
+                }
+
+              if (str != NULL)
+                {
+                  env_free(info, str);
+                }
+
+              info->ta_envpc = 0;
               ret = -ENOMEM;
             }
           else
             {
-              envp[envc] = NULL;
-
-              /* Duplicate the parent environment. */
-
-              while (envc-- > 0)
+              for (i = 0; i < envc; i++)
                 {
-                  size = strlen(envcp[envc]) + 1;
-                  envp[envc] = env_malloc(info, size);
-                  if (envp[envc] == NULL)
-                    {
-                      while (envp[++envc] != NULL)
-                        {
-                          env_free(info, envp[envc]);
-                        }
-
-                      env_free(info, envp);
-                      envp = NULL;
-                      ret = -ENOMEM;
-                      break;
-                    }
-
-                  strlcpy(envp[envc], envcp[envc], size);
+                  size = strlen(envcp[i]) + 1;
+                  memcpy(str, envcp[i], size);
+                  envp[i] = str;
+                  str += size;
                 }
+
+              envp[envc]      = NULL;
+              info->ta_envp   = envp;
+              info->ta_envarr = envp;
+              info->ta_envc   = envc;
             }
         }
 
-      /* Save the child environment allocation. */
-
-      info->ta_envp = envp;
       nxrmutex_unlock(&info->ta_lock);
     }
 
diff --git a/libs/libc/environ/env_findvar.c b/libs/libc/environ/env_findvar.c
index 38d6245efd5..4150204c2f3 100644
--- a/libs/libc/environ/env_findvar.c
+++ b/libs/libc/environ/env_findvar.c
@@ -26,6 +26,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <string.h>
 
 #include <nuttx/tls.h>
 
@@ -39,22 +40,10 @@
  * Name: env_cmpname
  ****************************************************************************/
 
-static bool env_cmpname(const char *pszname, const char *peqname)
+static bool env_cmpname(FAR const char *pname, size_t namelen,
+                        FAR const char *peqname)
 {
-  /* Search until we find anything different in the two names */
-
-  for (; *pszname == *peqname; pszname++, peqname++)
-    {
-    }
-
-  /* On success, pszname will end with '\0' and peqname with '=' */
-
-  if (*pszname == '\0' && *peqname == '=')
-    {
-      return true;
-    }
-
-  return false;
+  return strncmp(pname, peqname, namelen) == 0 && peqname[namelen] == '=';
 }
 
 /****************************************************************************
@@ -69,8 +58,9 @@ static bool env_cmpname(const char *pszname, const char *peqname)
  *   specified name.
  *
  * Input Parameters:
- *   group - The task group containing environment array to be searched.
- *   pname - The variable name to find
+ *   info    - The task containing environment array to be searched.
+ *   pname   - The variable name to find.  It needs no terminating NUL.
+ *   namelen - The length of the name.
  *
  * Returned Value:
  *   A index to the name=value string in the environment
@@ -81,7 +71,8 @@ static bool env_cmpname(const char *pszname, const char *peqname)
  *
  ****************************************************************************/
 
-ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname)
+ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname,
+                    size_t namelen)
 {
   ssize_t i;
 
@@ -98,7 +89,7 @@ ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname)
 
   for (i = 0; info->ta_envp[i] != NULL; i++)
     {
-      if (env_cmpname(pname, info->ta_envp[i]))
+      if (env_cmpname(pname, namelen, info->ta_envp[i]))
         {
           return i;
         }
diff --git a/libs/libc/environ/env_foreach.c b/libs/libc/environ/env_foreach.c
index b26eb812ced..b5af887e6ec 100644
--- a/libs/libc/environ/env_foreach.c
+++ b/libs/libc/environ/env_foreach.c
@@ -89,7 +89,5 @@ int env_foreach(FAR struct task_info_s *info,
         }
     }
 
-  DEBUGASSERT(ret != OK || info->ta_envc == i);
-
   return ret;
 }
diff --git a/libs/libc/environ/env_getenv.c b/libs/libc/environ/env_getenv.c
index 486f18fc3d9..2e8b8bd4894 100644
--- a/libs/libc/environ/env_getenv.c
+++ b/libs/libc/environ/env_getenv.c
@@ -73,7 +73,7 @@ FAR char *getenv(FAR const char *name)
     }
 
   nxrmutex_lock(&info->ta_lock);
-  ret = env_findvar(info, name);
+  ret = env_findvar(info, name, strlen(name));
   if (ret < 0)
     {
       nxrmutex_unlock(&info->ta_lock);
diff --git a/libs/libc/environ/env_putenv.c b/libs/libc/environ/env_putenv.c
index 5f0e29155af..579119bc7c4 100644
--- a/libs/libc/environ/env_putenv.c
+++ b/libs/libc/environ/env_putenv.c
@@ -27,7 +27,6 @@
 #include <string.h>
 #include <errno.h>
 
-#include <nuttx/lib/lib.h>
 #include <nuttx/tls.h>
 
 #include "environ.h"
@@ -60,45 +59,33 @@
 
 int putenv(FAR const char *string)
 {
-  FAR struct task_info_s *info = task_get_info();
-  FAR char *pname;
-  FAR char *pequal;
-  size_t len;
-  int ret = OK;
+  FAR const char *pequal;
+  int ret;
 
   /* Verify that a string was passed */
 
   if (!string)
     {
-      ret = EINVAL;
-      goto errout;
+      set_errno(EINVAL);
+      return ERROR;
     }
 
   /* Parse the name=value string */
 
-  len   = strlen(string) + 1;
-  pname = env_malloc(info, len);
-  if (!pname)
+  pequal = strchr(string, '=');
+  if (pequal == NULL)
     {
-      ret = ENOMEM;
-      goto errout;
+      return OK;
     }
 
-  memcpy(pname, string, len);
+  /* Then let env_setvar do all of the work */
 
-  pequal = strchr(pname, '=');
-  if (pequal)
+  ret = env_setvar(string, pequal - string, pequal + 1, TRUE);
+  if (ret < 0)
     {
-      /* Then let setenv do all of the work */
-
-      *pequal = '\0';
-      ret = setenv(pname, pequal + 1, TRUE);
+      set_errno(-ret);
+      return ERROR;
     }
 
-  env_free(info, pname);
-  return ret;
-
-errout:
-  set_errno(ret);
-  return ERROR;
+  return OK;
 }
diff --git a/libs/libc/environ/env_release.c b/libs/libc/environ/env_release.c
index 4c1abfcd49a..91fe4e8ce97 100644
--- a/libs/libc/environ/env_release.c
+++ b/libs/libc/environ/env_release.c
@@ -57,29 +57,21 @@
 
 void env_release(FAR struct task_info_s *info)
 {
-  int i;
-
   DEBUGASSERT(info != NULL);
 
-  if (info->ta_envp)
-    {
-      /* Free any allocate environment strings */
-
-      for (i = 0; info->ta_envp[i] != NULL; i++)
-        {
-          env_free(info, info->ta_envp[i]);
-        }
+  /* Free every block that libc allocated for the environment */
 
-      /* Free the environment */
-
-      env_free(info, info->ta_envp);
+  while (info->ta_envmem != NULL)
+    {
+      env_free(info, info->ta_envmem);
     }
 
   /* In any event, make sure that all environment-related variables in the
    * task group structure are reset to initial values.
    */
 
-  info->ta_envp  = NULL;
-  info->ta_envpc = 0;
-  info->ta_envc  = 0;
+  info->ta_envp   = NULL;
+  info->ta_envarr = NULL;
+  info->ta_envpc  = 0;
+  info->ta_envc   = 0;
 }
diff --git a/libs/libc/environ/env_removevar.c b/libs/libc/environ/env_removevar.c
index 207e0468b01..e1b5c9f43f0 100644
--- a/libs/libc/environ/env_removevar.c
+++ b/libs/libc/environ/env_removevar.c
@@ -55,44 +55,16 @@
  *
  ****************************************************************************/
 
-void env_removevar(FAR struct task_info_s *info, ssize_t index)
+void env_removevar(FAR struct task_info_s *info, size_t index)
 {
-  DEBUGASSERT(info != NULL && index >= 0 && index < info->ta_envc);
+  DEBUGASSERT(info != NULL && index < info->ta_envc);
+  DEBUGASSERT(info->ta_envp == info->ta_envarr);
 
-  /* Free the allocate environment string */
-
-  env_free(info, info->ta_envp[index]);
-
-  /* Exchange the last env and the index env */
+  /* Move the last entry into its place.  The string is not freed: the
+   * program can still use it.  env_release() frees it at exit.
+   */
 
   info->ta_envc--;
-  if (index == info->ta_envc)
-    {
-      info->ta_envp[index] = NULL;
-    }
-  else
-    {
-      info->ta_envp[index] = info->ta_envp[info->ta_envc];
-      info->ta_envp[info->ta_envc] = NULL;
-    }
-
-  /* Free the old environment (if there was one) */
-
-  if (info->ta_envc == 0)
-    {
-      env_free(info, info->ta_envp);
-      info->ta_envp = NULL;
-      info->ta_envpc = 0;
-    }
-  else if (info->ta_envc <=
-           (info->ta_envpc - SCHED_ENVIRON_RESERVED * 2))
-    {
-      /* Reallocate the environment to reclaim a little memory */
-
-      info->ta_envpc = info->ta_envc + SCHED_ENVIRON_RESERVED + 1;
-
-      info->ta_envp = env_realloc(info, info->ta_envp,
-         sizeof(*info->ta_envp) * info->ta_envpc);
-      DEBUGASSERT(info->ta_envp != NULL);
-    }
+  info->ta_envp[index] = info->ta_envp[info->ta_envc];
+  info->ta_envp[info->ta_envc] = NULL;
 }
diff --git a/libs/libc/environ/env_secureexec.c b/libs/libc/environ/env_secureexec.c
index f3ae0fcc3ec..12454262c4b 100644
--- a/libs/libc/environ/env_secureexec.c
+++ b/libs/libc/environ/env_secureexec.c
@@ -101,19 +101,32 @@ static bool env_is_unsafe(FAR const char *pair)
 #ifdef CONFIG_SCHED_USER_IDENTITY
 void env_sanitize_secure(FAR struct task_info_s *info)
 {
-  ssize_t index;
+  size_t index;
 
   DEBUGASSERT(info != NULL);
 
   nxrmutex_lock(&info->ta_lock);
 
-  for (index = info->ta_envc - 1; index >= 0; )
+  if (env_reserve(info, 0) < 0)
     {
-      FAR char *pair = info->ta_envp[index];
+      /* Without an array of libc to change, drop the whole environment */
+
+      info->ta_envp = NULL;
+      info->ta_envc = 0;
+    }
+
+  /* Walk down from the last entry.  env_removevar() moves the last entry
+   * into the removed one, so the same index is checked again.
+   */
+
+  index = info->ta_envc;
+  while (index > 0)
+    {
+      FAR char *pair = info->ta_envp[index - 1];
 
       if (pair != NULL && env_is_unsafe(pair))
         {
-          env_removevar(info, index);
+          env_removevar(info, index - 1);
         }
       else
         {
diff --git a/libs/libc/environ/env_setenv.c b/libs/libc/environ/env_setenv.c
index 7bd5a1748db..edf4619587b 100644
--- a/libs/libc/environ/env_setenv.c
+++ b/libs/libc/environ/env_setenv.c
@@ -38,6 +38,82 @@
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: env_setvar
+ ****************************************************************************/
+
+int env_setvar(FAR const char *name, size_t namelen,
+               FAR const char *value, int overwrite)
+{
+  FAR struct task_info_s *info = task_get_info();
+  FAR char *pvar;
+  ssize_t idx;
+  size_t varlen;
+  int ret;
+
+  DEBUGASSERT(info != NULL);
+
+  nxrmutex_lock(&info->ta_lock);
+
+  /* environ must be an array of libc with room for one more entry */
+
+  ret = env_reserve(info, 1);
+  if (ret < 0)
+    {
+      goto out;
+    }
+
+  /* Check if the variable already exists */
+
+  idx = env_findvar(info, name, namelen);
+  if (idx >= 0)
+    {
+      /* It does! Do we have permission to overwrite the existing value? */
+
+      if (!overwrite)
+        {
+          goto out;
+        }
+
+      /* Yes.. just remove the name=value pair from the environment.  It will
+       * be added again below.
+       */
+
+      env_removevar(info, idx);
+    }
+
+  /* An empty value removes the variable */
+
+  if (*value == '\0')
+    {
+      goto out;
+    }
+
+  /* Get the size of the new name=value string.
+   * The +2 is for the '=' and for null terminator
+   */
+
+  varlen = namelen + strlen(value) + 2;
+
+  pvar = env_alloc(info, varlen);
+  if (pvar == NULL)
+    {
+      ret = -ENOMEM;
+      goto out;
+    }
+
+  /* Now, put the new name=value string into the environment */
+
+  snprintf(pvar, varlen, "%.*s=%s", (int)namelen, name, value);
+
+  info->ta_envp[info->ta_envc++] = pvar;
+  info->ta_envp[info->ta_envc]   = NULL;
+
+out:
+  nxrmutex_unlock(&info->ta_lock);
+  return ret;
+}
+
 /****************************************************************************
  * Name: setenv
  *
@@ -63,20 +139,14 @@
 
 int setenv(FAR const char *name, FAR const char *value, int overwrite)
 {
-  FAR struct task_info_s *info;
-  FAR char *pvar;
-  FAR char **envp;
-  ssize_t envc;
-  ssize_t envpc;
-  ssize_t ret = OK;
-  int varlen;
+  int ret;
 
   /* Verify input parameter */
 
   if (!name)
     {
-      ret = EINVAL;
-      goto errout;
+      set_errno(EINVAL);
+      return ERROR;
     }
 
   /* if no value is provided, then this is the same as unsetenv (unless
@@ -101,103 +171,12 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
         }
     }
 
-  /* Get a reference to the thread-private environ in the TCB. */
-
-  info = task_get_info();
-
-  DEBUGASSERT(info != NULL);
-
-  nxrmutex_lock(&info->ta_lock);
-
-  /* Check if the variable already exists */
-
-  if (info->ta_envp && (ret = env_findvar(info, name)) >= 0)
+  ret = env_setvar(name, strlen(name), value, overwrite);
+  if (ret < 0)
     {
-      /* It does! Do we have permission to overwrite the existing value? */
-
-      if (!overwrite)
-        {
-          /* No.. then just return success */
-
-          nxrmutex_unlock(&info->ta_lock);
-          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.
-       */
-
-      env_removevar(info, ret);
+      set_errno(-ret);
+      return ERROR;
     }
 
-  /* Check current envirments count */
-
-  DEBUGASSERT(info->ta_envc < SSIZE_MAX);
-
-  /* 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 */
-
-  pvar = env_malloc(info, varlen);
-  if (pvar == NULL)
-    {
-      ret = ENOMEM;
-      goto errout_with_lock;
-    }
-
-  envc = info->ta_envc;
-
-  if (info->ta_envp == NULL)
-    {
-      envpc = SCHED_ENVIRON_RESERVED + 2;
-
-      envp = env_malloc(info, sizeof(*envp) * envpc);
-      if (envp == NULL)
-        {
-          ret = ENOMEM;
-          goto errout_with_var;
-        }
-
-      info->ta_envp  = envp;
-      info->ta_envpc = envpc;
-    }
-  else if (envc >= info->ta_envpc - 1)
-    {
-      envpc = envc + SCHED_ENVIRON_RESERVED + 2;
-
-      envp = env_realloc(info, info->ta_envp, sizeof(*envp) * envpc);
-      if (envp == NULL)
-        {
-          ret = ENOMEM;
-          goto errout_with_var;
-        }
-
-      info->ta_envp  = envp;
-      info->ta_envpc = envpc;
-    }
-
-  /* Save the new buffer and count */
-
-  info->ta_envp[envc++] = pvar;
-  info->ta_envp[envc]   = NULL;
-  info->ta_envc = envc;
-
-  /* Now, put the new name=value string into the environment buffer */
-
-  snprintf(pvar, varlen, "%s=%s", name, value);
-  nxrmutex_unlock(&info->ta_lock);
   return OK;
-
-errout_with_var:
-  env_free(info, pvar);
-errout_with_lock:
-  nxrmutex_unlock(&info->ta_lock);
-errout:
-  set_errno(ret);
-  return ERROR;
 }
diff --git a/libs/libc/environ/env_unsetenv.c b/libs/libc/environ/env_unsetenv.c
index 9e2dbfc48ae..3711213e740 100644
--- a/libs/libc/environ/env_unsetenv.c
+++ b/libs/libc/environ/env_unsetenv.c
@@ -57,6 +57,7 @@ int unsetenv(FAR const char *name)
 {
   FAR struct task_info_s *info = task_get_info();
   ssize_t idx;
+  int ret;
 
   /* Check the incoming parameter */
 
@@ -66,12 +67,21 @@ int unsetenv(FAR const char *name)
       return ERROR;
     }
 
+  nxrmutex_lock(&info->ta_lock);
+
   /* Check if the variable exists */
 
-  nxrmutex_lock(&info->ta_lock);
-  if (info && (idx = env_findvar(info, name)) >= 0)
+  if ((idx = env_findvar(info, name, strlen(name))) >= 0)
     {
-      /* It does!  Remove the name=value pair from the environment. */
+      /* It does!  Remove the name=value pair from an array of libc. */
+
+      ret = env_reserve(info, 0);
+      if (ret < 0)
+        {
+          nxrmutex_unlock(&info->ta_lock);
+          set_errno(-ret);
+          return ERROR;
+        }
 
       env_removevar(info, idx);
     }
diff --git a/libs/libc/environ/environ.h b/libs/libc/environ/environ.h
index 2b5f2888b5b..2618731054e 100644
--- a/libs/libc/environ/environ.h
+++ b/libs/libc/environ/environ.h
@@ -33,10 +33,6 @@
 
 #include <nuttx/lib/lib.h>
 
-#ifdef __KERNEL__
-#  include <nuttx/kmalloc.h>
-#endif
-
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -45,25 +41,6 @@
 
 #  define SCHED_ENVIRON_RESERVED (4)
 
-/* The environment is in the heap that holds the task information: the
- * kernel heap for a kernel thread, the user heap for a user task.  Kernel
- * code also changes the environment of kernel threads (the idle task at
- * boot) and of a new user task.
- */
-
-#  ifdef __KERNEL__
-#    define env_malloc(info, n) \
-       (kmm_heapmember(info) ? kmm_malloc(n) : kumm_malloc(n))
-#    define env_realloc(info, p, n) \
-       (kmm_heapmember(info) ? kmm_realloc(p, n) : kumm_realloc(p, n))
-#    define env_free(info, p) \
-       (kmm_heapmember(info) ? kmm_free(p) : kumm_free(p))
-#  else
-#    define env_malloc(info, n)     ((void)(info), lib_umalloc(n))
-#    define env_realloc(info, p, n) ((void)(info), lib_urealloc(p, n))
-#    define env_free(info, p)       ((void)(info), lib_ufree(p))
-#  endif
-
 /****************************************************************************
  * Public Data
  ****************************************************************************/
@@ -88,8 +65,9 @@ extern "C"
  *   specified name.
  *
  * Input Parameters:
- *   info  - The task containing environment array to be searched.
- *   pname - The variable name to find
+ *   info    - The task containing environment array to be searched.
+ *   pname   - The variable name to find.  It needs no terminating NUL.
+ *   namelen - The length of the name.
  *
  * Returned Value:
  *   A index to the name=value string in the environment
@@ -100,7 +78,85 @@ extern "C"
  *
  ****************************************************************************/
 
-ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname);
+ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname,
+                    size_t namelen);
+
+/****************************************************************************
+ * Name: env_setvar
+ *
+ * Description:
+ *   setenv() with the length of the name, so that putenv() can pass the
+ *   name part of its string without a copy.  An empty value removes the
+ *   variable, as in setenv().
+ *
+ * Input Parameters:
+ *   name      - The name of the variable.  It needs no terminating NUL.
+ *   namelen   - The length of the name.
+ *   value     - The new value of the variable.
+ *   overwrite - Replace any existing value if non-zero.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int env_setvar(FAR const char *name, size_t namelen,
+               FAR const char *value, int overwrite);
+
+/****************************************************************************
+ * Name: env_alloc
+ *
+ * Description:
+ *   Allocate a block for the environment of a task.  The block goes on a
+ *   list of the task, and env_release() frees it when the task exits.
+ *   libc does not free the strings before then, so a pointer that getenv()
+ *   returned stays valid, and the program can keep a string or an array
+ *   that it took from environ.
+ *
+ * Input Parameters:
+ *   info - The task that owns the environment.
+ *   size - The size of the block.
+ *
+ * Returned Value:
+ *   The block, or NULL if there is no memory.
+ *
+ ****************************************************************************/
+
+FAR void *env_alloc(FAR struct task_info_s *info, size_t size);
+
+/****************************************************************************
+ * Name: env_free
+ *
+ * Description:
+ *   Free a block from env_alloc().  Before the task exits, only for a block
+ *   that nothing else can reach.
+ *
+ ****************************************************************************/
+
+void env_free(FAR struct task_info_s *info, FAR void *mem);
+
+/****************************************************************************
+ * Name: env_reserve
+ *
+ * Description:
+ *   Make environ an array that libc allocated, with room for more entries.
+ *   The program can assign environ, as POSIX allows.  Then the array is
+ *   copied, and the array of the program is not changed.  The strings are
+ *   not copied.
+ *
+ * Input Parameters:
+ *   info - The task whose environment is changed.
+ *   more - The number of entries to add.
+ *
+ * Returned Value:
+ *   Zero (OK) on success; -ENOMEM if there is no memory.
+ *
+ * Assumptions:
+ *   The caller holds ta_lock.
+ *
+ ****************************************************************************/
+
+int env_reserve(FAR struct task_info_s *info, size_t more);
 
 /****************************************************************************
  * Name: env_removevar
@@ -118,12 +174,11 @@ ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname);
  *
  * Assumptions:
  *   - Not called from an interrupt handler
- *   - Caller has pre-emption disabled
- *   - Caller will reallocate the environment structure to the correct size
+ *   - The caller holds ta_lock and called env_reserve()
  *
  ****************************************************************************/
 
-void env_removevar(FAR struct task_info_s *info, ssize_t index);
+void env_removevar(FAR struct task_info_s *info, size_t index);
 
 #undef EXTERN
 #ifdef __cplusplus


(nuttx) 03/06: environ: move the environ from sched/environ to libc/environ

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 52bf792d02e9ad42f58494713c4ec15c2e3f59c8
Author: guoshichao <gu...@xiaomi.com>
AuthorDate: Mon Nov 10 17:25:55 2025 +0800

    environ: move the environ from sched/environ to libc/environ
    
    change the environ implementation from sched/environ to
    libc/environ, thus we can access the environ related API can vai libc
    directl, which could improve the access speed in kernel build.
    And in order to support this modification, we change the
    struct task_info_s's ta_lock from nxmutex_t to nxrmutex_t,
    this is because the env_*-related APIs require the ta_lock
    to be reentrant.
    If recursive locks are not supported, it may lead to deadlocks
    in certain scenarios. For example, during initialization on the
    qemu-miwear platform, it is necessary to call getenv while already
    holding the ta_lock. To support such cases, we need to change
    the ta_lock from nxmutex_t to nxrmutex_t.
    
    Signed-off-by: guoshichao <gu...@xiaomi.com>
    (cherry picked from commit 2c27af036bc927617fad72b85607bccad3c2564d
     in https://github.com/open-vela/nuttx)
    
    Port to Apache master: keep the master code around each conflict and
    change only the environ calls; move env_secureexec.c (env_sanitize_secure()
    takes the task info, declared in nuttx/environ.h); keep the clearenv()
    lock from dc4c846d329; task_uninit_info() releases the environment and
    stays where group_release() calls it; include the headers that openvela
    gets indirectly (nuttx/lib/lib.h, stdio.h, string.h).
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 binfmt/binfmt_execmodule.c                       |  4 +-
 fs/procfs/fs_procfsproc.c                        |  2 +-
 include/nuttx/environ.h                          | 71 ++++++++++++++++++-
 include/nuttx/tls.h                              |  2 +-
 include/sys/syscall_lookup.h                     | 11 ---
 libs/libc/Makefile                               |  1 +
 {sched => libs/libc}/environ/CMakeLists.txt      | 30 ++++-----
 {sched => libs/libc}/environ/Make.defs           |  2 +-
 {sched => libs/libc}/environ/env_clearenv.c      | 27 ++------
 {sched => libs/libc}/environ/env_dup.c           | 33 +++------
 {sched => libs/libc}/environ/env_findvar.c       | 19 ++----
 {sched => libs/libc}/environ/env_foreach.c       | 20 ++----
 {sched => libs/libc}/environ/env_getenv.c        | 47 ++++---------
 {sched => libs/libc}/environ/env_getenvironptr.c | 16 +----
 {sched => libs/libc}/environ/env_putenv.c        | 12 +---
 {sched => libs/libc}/environ/env_release.c       | 32 +++------
 {sched => libs/libc}/environ/env_removevar.c     | 29 ++------
 {sched => libs/libc}/environ/env_secureexec.c    | 17 ++---
 {sched => libs/libc}/environ/env_setenv.c        | 44 +++++-------
 {sched => libs/libc}/environ/env_unsetenv.c      | 29 +++-----
 {sched => libs/libc}/environ/environ.h           | 86 ++++--------------------
 libs/libc/pthread/pthread_atfork.c               |  4 +-
 libs/libc/pthread/pthread_keycreate.c            |  4 +-
 libs/libc/pthread/pthread_keydelete.c            |  4 +-
 libs/libc/stdlib/lib_atexit.c                    |  8 +--
 libs/libc/unistd/lib_fork.c                      | 12 ++--
 sched/Makefile                                   |  1 -
 sched/group/group_join.c                         |  1 -
 sched/group/group_leave.c                        |  5 --
 sched/task/task_fork.c                           |  4 +-
 sched/task/task_init.c                           |  4 +-
 sched/tls/task_initinfo.c                        |  2 +-
 sched/tls/task_uninitinfo.c                      |  7 +-
 syscall/syscall.csv                              |  6 --
 34 files changed, 220 insertions(+), 376 deletions(-)

diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c
index dc33d9baee1..42b705d5634 100644
--- a/binfmt/binfmt_execmodule.c
+++ b/binfmt/binfmt_execmodule.c
@@ -44,9 +44,9 @@
 #include <task/spawn.h>
 #include <nuttx/spawn.h>
 #include <nuttx/binfmt/binfmt.h>
+#include <nuttx/environ.h>
 
 #include "binfmt.h"
-#include "environ/environ.h"
 
 #ifndef CONFIG_BINFMT_DISABLE
 
@@ -354,7 +354,7 @@ int exec_module(FAR struct binary_s *binp,
       group->tg_flags |= GROUP_FLAG_SECURE_EXEC;
       group->tg_flags &= ~(GROUP_FLAG_FD_BACKTRACE | GROUP_FLAG_DUMPABLE);
 #ifndef CONFIG_DISABLE_ENVIRON
-      env_sanitize_secure(group);
+      env_sanitize_secure(group->tg_info);
 #endif
     }
 #endif
diff --git a/fs/procfs/fs_procfsproc.c b/fs/procfs/fs_procfsproc.c
index 26b31d2e7e4..e5e83f1933c 100644
--- a/fs/procfs/fs_procfsproc.c
+++ b/fs/procfs/fs_procfsproc.c
@@ -1568,7 +1568,7 @@ static ssize_t proc_groupenv(FAR struct proc_file_s *procfile,
 
   /* Generate output for each environment variable */
 
-  env_foreach(group, proc_groupenv_callback, &info);
+  env_foreach(group->tg_info, proc_groupenv_callback, &info);
 
 #ifdef CONFIG_ARCH_ADDRENV
   if (tcb->addrenv_own != NULL)
diff --git a/include/nuttx/environ.h b/include/nuttx/environ.h
index e3ff666a3fd..4cbe7276658 100644
--- a/include/nuttx/environ.h
+++ b/include/nuttx/environ.h
@@ -28,8 +28,12 @@
  ****************************************************************************/
 
 #include <nuttx/config.h>
+#include <nuttx/compiler.h>
 
-#ifndef CONFIG_DISABLE_ENVIRON
+#ifdef CONFIG_DISABLE_ENVIRON
+#  define env_dup(info, envp) (0)
+#  define env_release(info)   ((void)0)
+#else
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -55,6 +59,8 @@ extern "C"
 #define EXTERN extern
 #endif
 
+struct task_info_s;  /* Forward reference */
+
 /****************************************************************************
  * Name: env_foreach
  *
@@ -80,10 +86,69 @@ extern "C"
  *
  ****************************************************************************/
 
-struct task_group_s;  /* Forward reference */
-int env_foreach(FAR struct task_group_s *group, env_foreach_t cb,
+int env_foreach(FAR struct task_info_s *info, env_foreach_t cb,
                 FAR void *arg);
 
+/****************************************************************************
+ * 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:
+ *   info  - The child task to receive the newly allocated copy of the
+ *           parent task groups environment structure.
+ *   envp  - Pointer to the environment strings.
+ *
+ * Returned Value:
+ *   zero on success
+ *
+ * Assumptions:
+ *   Not called from an interrupt handler.
+ *
+ ****************************************************************************/
+
+int env_dup(FAR struct task_info_s *info, FAR char * const *envp);
+
+/****************************************************************************
+ * 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:
+ *   info - Identifies the task containing the environment structure
+ *          to be released.
+ *
+ * Returned Value:
+ *   None
+ *
+ * Assumptions:
+ *   Not called from an interrupt handler
+ *
+ ****************************************************************************/
+
+void env_release(FAR struct task_info_s *info);
+
+#ifdef CONFIG_SCHED_USER_IDENTITY
+/****************************************************************************
+ * Name: env_sanitize_secure
+ *
+ * Description:
+ *   Remove environment variables that must not be inherited by a secure
+ *   (set-user-ID or set-group-ID) executable.
+ *
+ ****************************************************************************/
+
+void env_sanitize_secure(FAR struct task_info_s *info);
+#endif
+
 #undef EXTERN
 #ifdef __cplusplus
 }
diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index e0464b3248e..2e802dd05cf 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -137,7 +137,7 @@ struct pthread_atfork_s
 
 struct task_info_s
 {
-  mutex_t         ta_lock;
+  rmutex_t        ta_lock;
 #if CONFIG_TLS_TASK_NELEM > 0
   uintptr_t       ta_telem[CONFIG_TLS_TASK_NELEM]; /* Task local storage elements */
 #endif
diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index 9b106b5e997..dfdbe44316d 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -363,17 +363,6 @@ SYSCALL_LOOKUP(munlockall,                 0)
   SYSCALL_LOOKUP(mq_unlink,                1)
 #endif
 
-/* The following are defined only if environment variables are supported */
-
-#ifndef CONFIG_DISABLE_ENVIRON
-  SYSCALL_LOOKUP(get_environ_ptr,          0)
-  SYSCALL_LOOKUP(clearenv,                 0)
-  SYSCALL_LOOKUP(getenv,                   1)
-  SYSCALL_LOOKUP(putenv,                   1)
-  SYSCALL_LOOKUP(setenv,                   3)
-  SYSCALL_LOOKUP(unsetenv,                 1)
-#endif
-
 /* The following are defined only if networking AND sockets are supported */
 
 #ifdef CONFIG_NET
diff --git a/libs/libc/Makefile b/libs/libc/Makefile
index a04fb9d393c..9cedb332839 100644
--- a/libs/libc/Makefile
+++ b/libs/libc/Makefile
@@ -29,6 +29,7 @@ include builtin/Make.defs
 include ctype/Make.defs
 include dirent/Make.defs
 include dlfcn/Make.defs
+include environ/Make.defs
 include errno/Make.defs
 include eventfd/Make.defs
 include fixedmath/Make.defs
diff --git a/sched/environ/CMakeLists.txt b/libs/libc/environ/CMakeLists.txt
similarity index 73%
rename from sched/environ/CMakeLists.txt
rename to libs/libc/environ/CMakeLists.txt
index dafb5d1fece..a721bcfb091 100644
--- a/sched/environ/CMakeLists.txt
+++ b/libs/libc/environ/CMakeLists.txt
@@ -1,5 +1,5 @@
 # ##############################################################################
-# sched/environ/CMakeLists.txt
+# libs/libc/environ/CMakeLists.txt
 #
 # SPDX-License-Identifier: Apache-2.0
 #
@@ -21,18 +21,18 @@
 # ##############################################################################
 
 if(NOT CONFIG_DISABLE_ENVIRON)
-  target_sources(
-    sched
-    PRIVATE env_getenvironptr.c
-            env_dup.c
-            env_release.c
-            env_findvar.c
-            env_removevar.c
-            env_clearenv.c
-            env_getenv.c
-            env_putenv.c
-            env_setenv.c
-            env_unsetenv.c
-            env_foreach.c
-            env_secureexec.c)
+  set(SRCS
+      env_getenvironptr.c
+      env_dup.c
+      env_release.c
+      env_findvar.c
+      env_removevar.c
+      env_clearenv.c
+      env_getenv.c
+      env_putenv.c
+      env_setenv.c
+      env_unsetenv.c
+      env_foreach.c
+      env_secureexec.c)
+  target_sources(c PRIVATE ${SRCS})
 endif()
diff --git a/sched/environ/Make.defs b/libs/libc/environ/Make.defs
similarity index 97%
rename from sched/environ/Make.defs
rename to libs/libc/environ/Make.defs
index 28e3d187171..3c74a82f5f7 100644
--- a/sched/environ/Make.defs
+++ b/libs/libc/environ/Make.defs
@@ -1,5 +1,5 @@
 ############################################################################
-# sched/environ/Make.defs
+# libs/libc/environ/Make.defs
 #
 # SPDX-License-Identifier: Apache-2.0
 #
diff --git a/sched/environ/env_clearenv.c b/libs/libc/environ/env_clearenv.c
similarity index 80%
rename from sched/environ/env_clearenv.c
rename to libs/libc/environ/env_clearenv.c
index df04888ce1b..a87bb3f7f94 100644
--- a/sched/environ/env_clearenv.c
+++ b/libs/libc/environ/env_clearenv.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_clearenv.c
+ * libs/libc/environ/env_clearenv.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,16 +24,8 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <sched.h>
-#include <stdlib.h>
-#include <assert.h>
-
-#include "sched/sched.h"
-#include "environ/environ.h"
+#include <nuttx/environ.h>
+#include <nuttx/tls.h>
 
 /****************************************************************************
  * Public Functions
@@ -59,15 +51,10 @@
 
 int clearenv(void)
 {
-  FAR struct tcb_s *tcb = this_task();
-  FAR struct task_group_s *group = tcb->group;
+  FAR struct task_info_s *info = task_get_info();
 
-  DEBUGASSERT(group);
-
-  nxrmutex_lock(&group->tg_mutex);
-  env_release(group);
-  nxrmutex_unlock(&group->tg_mutex);
+  nxrmutex_lock(&info->ta_lock);
+  env_release(info);
+  nxrmutex_unlock(&info->ta_lock);
   return OK;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_dup.c b/libs/libc/environ/env_dup.c
similarity index 83%
rename from sched/environ/env_dup.c
rename to libs/libc/environ/env_dup.c
index d2f16bc8dd0..220392d24a5 100644
--- a/sched/environ/env_dup.c
+++ b/libs/libc/environ/env_dup.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_dup.c
+ * libs/libc/environ/env_dup.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,21 +24,13 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <sys/types.h>
-#include <sched.h>
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
 
-#include <nuttx/kmalloc.h>
 #include <nuttx/mutex.h>
 #include <nuttx/tls.h>
 
-#include "sched/sched.h"
 #include "environ/environ.h"
 
 /****************************************************************************
@@ -54,7 +46,7 @@
  *   private, exact duplicate of the parent task's environment.
  *
  * Input Parameters:
- *   group - The child task group to receive the newly allocated copy of
+ *   info  - The child task to receive the newly allocated copy of
  *           the parent task groups environment structure.
  *   envcp - Pointer to the environment strings to copy.
  * Returned Value:
@@ -65,17 +57,14 @@
  *
  ****************************************************************************/
 
-int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
+int env_dup(FAR struct task_info_s *info, FAR char * const *envcp)
 {
-  FAR struct task_info_s *info;
   FAR char **envp = NULL;
   size_t envc = 0;
   size_t size;
   int ret = OK;
 
-  DEBUGASSERT(group != NULL && group->tg_info != NULL);
-
-  info = group->tg_info;
+  DEBUGASSERT(info != NULL);
 
   /* Is there an environment ? */
 
@@ -85,7 +74,7 @@ int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
        * environment may be shared.
        */
 
-      nxrmutex_lock(&group->tg_mutex);
+      nxrmutex_lock(&info->ta_lock);
 
       /* Count the strings */
 
@@ -106,7 +95,7 @@ int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
         {
           /* There is an environment, duplicate it */
 
-          envp = group_malloc(group, sizeof(*envp) * info->ta_envpc);
+          envp = lib_umalloc(sizeof(*envp) * info->ta_envpc);
           if (envp == NULL)
             {
               /* The parent's environment can not be inherited due to a
@@ -124,15 +113,15 @@ int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
               while (envc-- > 0)
                 {
                   size = strlen(envcp[envc]) + 1;
-                  envp[envc] = group_malloc(group, size);
+                  envp[envc] = lib_umalloc(size);
                   if (envp[envc] == NULL)
                     {
                       while (envp[++envc] != NULL)
                         {
-                          group_free(group, envp[envc]);
+                          lib_ufree(envp[envc]);
                         }
 
-                      group_free(group, envp);
+                      lib_ufree(envp);
                       envp = NULL;
                       ret = -ENOMEM;
                       break;
@@ -146,10 +135,8 @@ int env_dup(FAR struct task_group_s *group, FAR char * const *envcp)
       /* Save the child environment allocation. */
 
       info->ta_envp = envp;
-      nxrmutex_unlock(&group->tg_mutex);
+      nxrmutex_unlock(&info->ta_lock);
     }
 
   return ret;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_findvar.c b/libs/libc/environ/env_findvar.c
similarity index 88%
rename from sched/environ/env_findvar.c
rename to libs/libc/environ/env_findvar.c
index a0879a5454a..38d6245efd5 100644
--- a/sched/environ/env_findvar.c
+++ b/libs/libc/environ/env_findvar.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_findvar.c
+ * libs/libc/environ/env_findvar.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,14 +24,8 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <stdbool.h>
-#include <string.h>
-#include <sched.h>
 #include <assert.h>
+#include <errno.h>
 
 #include <nuttx/tls.h>
 
@@ -87,16 +81,13 @@ static bool env_cmpname(const char *pszname, const char *peqname)
  *
  ****************************************************************************/
 
-ssize_t env_findvar(FAR struct task_group_s *group, FAR const char *pname)
+ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname)
 {
-  FAR struct task_info_s *info;
   ssize_t i;
 
   /* Verify input parameters */
 
-  DEBUGASSERT(group != NULL && group->tg_info != NULL && pname != NULL);
-
-  info = group->tg_info;
+  DEBUGASSERT(info != NULL && pname != NULL);
 
   if (info->ta_envp == NULL)
     {
@@ -115,5 +106,3 @@ ssize_t env_findvar(FAR struct task_group_s *group, FAR const char *pname)
 
   return -ENOENT;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_foreach.c b/libs/libc/environ/env_foreach.c
similarity index 86%
rename from sched/environ/env_foreach.c
rename to libs/libc/environ/env_foreach.c
index b552549bc4d..b26eb812ced 100644
--- a/sched/environ/env_foreach.c
+++ b/libs/libc/environ/env_foreach.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_foreach.c
+ * libs/libc/environ/env_foreach.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,13 +24,6 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <stdbool.h>
-#include <string.h>
-#include <sched.h>
 #include <assert.h>
 
 #include <nuttx/environ.h>
@@ -49,7 +42,7 @@
  *   Visit each name-value pair in the environment.
  *
  * Input Parameters:
- *   group - The task group containing environment array to be searched.
+ *   info  - The task containing environment array to be searched.
  *   cb    - The callback function to be invoked for each environment
  *           variable.
  *
@@ -64,19 +57,16 @@
  *
  ****************************************************************************/
 
-int env_foreach(FAR struct task_group_s *group,
+int env_foreach(FAR struct task_info_s *info,
                 env_foreach_t cb,
                 FAR void *arg)
 {
-  FAR struct task_info_s *info;
   int ret = OK;
   size_t i;
 
   /* Verify input parameters */
 
-  DEBUGASSERT(group != NULL && group->tg_info != NULL && cb != NULL);
-
-  info = group->tg_info;
+  DEBUGASSERT(info != NULL && cb != NULL);
 
   if (info->ta_envp == NULL)
     {
@@ -103,5 +93,3 @@ int env_foreach(FAR struct task_group_s *group,
 
   return ret;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_getenv.c b/libs/libc/environ/env_getenv.c
similarity index 77%
rename from sched/environ/env_getenv.c
rename to libs/libc/environ/env_getenv.c
index 34142490f7a..486f18fc3d9 100644
--- a/sched/environ/env_getenv.c
+++ b/libs/libc/environ/env_getenv.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_getenv.c
+ * libs/libc/environ/env_getenv.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,19 +24,12 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <sched.h>
-#include <string.h>
-#include <stdlib.h>
 #include <errno.h>
+#include <string.h>
 
 #include <nuttx/mutex.h>
 #include <nuttx/tls.h>
 
-#include "sched/sched.h"
 #include "environ/environ.h"
 
 /****************************************************************************
@@ -63,48 +56,38 @@
 
 FAR char *getenv(FAR const char *name)
 {
-  FAR struct tcb_s *rtcb;
-  FAR struct task_group_s *group;
+  FAR struct task_info_s *info = task_get_info();
   FAR char *pvalue = NULL;
   ssize_t ret = OK;
 
   /* Verify that a string was passed */
 
-  if (name == NULL || up_interrupt_context())
+  if (name == NULL || info == NULL
+#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
+      || up_interrupt_context() || sched_idletask()
+#endif
+     )
     {
       ret = -EINVAL;
       goto errout;
     }
 
-  /* Get a reference to the thread-private environ in the TCB. */
-
-  rtcb  = this_task();
-  group = rtcb->group;
-
-  /* Check if the variable exists */
-
-  if (group == NULL)
-    {
-      goto errout;
-    }
-
-  nxrmutex_lock(&group->tg_mutex);
-  ret = env_findvar(group, name);
+  nxrmutex_lock(&info->ta_lock);
+  ret = env_findvar(info, name);
   if (ret < 0)
     {
-      nxrmutex_unlock(&group->tg_mutex);
+      nxrmutex_unlock(&info->ta_lock);
       goto errout;
     }
 
   /* It does!  Get the value sub-string from the name=value string */
 
-  pvalue = strchr(group->tg_info->ta_envp[ret], '=');
+  pvalue = strchr(info->ta_envp[ret], '=');
   if (pvalue == NULL)
     {
-      nxrmutex_unlock(&group->tg_mutex);
-
       /* The name=value string has no '='  This is a bug! */
 
+      nxrmutex_unlock(&info->ta_lock);
       ret = -EINVAL;
       goto errout;
     }
@@ -112,12 +95,10 @@ FAR char *getenv(FAR const char *name)
   /* Adjust the pointer so that it points to the value right after the '=' */
 
   pvalue++;
-  nxrmutex_unlock(&group->tg_mutex);
+  nxrmutex_unlock(&info->ta_lock);
   return pvalue;
 
 errout:
   set_errno(-ret);
   return NULL;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_getenvironptr.c b/libs/libc/environ/env_getenvironptr.c
similarity index 86%
rename from sched/environ/env_getenvironptr.c
rename to libs/libc/environ/env_getenvironptr.c
index bf5be54bc30..81ed2ebfe0c 100644
--- a/sched/environ/env_getenvironptr.c
+++ b/libs/libc/environ/env_getenvironptr.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_getenvironptr.c
+ * libs/libc/environ/env_getenvironptr.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,16 +24,8 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <sched.h>
-#include <stdlib.h>
 #include <nuttx/tls.h>
 
-#include "sched/sched.h"
-
 #undef get_environ_ptr
 
 /****************************************************************************
@@ -58,9 +50,5 @@
 
 FAR char **get_environ_ptr(void)
 {
-  FAR struct tcb_s *tcb = this_task();
-
-  return tcb->group->tg_info->ta_envp;
+  return task_get_info()->ta_envp;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_putenv.c b/libs/libc/environ/env_putenv.c
similarity index 93%
rename from sched/environ/env_putenv.c
rename to libs/libc/environ/env_putenv.c
index 8a7aad23002..bc4c330d232 100644
--- a/sched/environ/env_putenv.c
+++ b/libs/libc/environ/env_putenv.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_putenv.c
+ * libs/libc/environ/env_putenv.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,12 +24,6 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <stdlib.h>
-#include <sched.h>
 #include <string.h>
 #include <errno.h>
 
@@ -93,12 +87,10 @@ int putenv(FAR const char *string)
       ret = setenv(pname, pequal + 1, TRUE);
     }
 
-  kmm_free(pname);
+  lib_ufree(pname);
   return ret;
 
 errout:
   set_errno(ret);
   return ERROR;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_release.c b/libs/libc/environ/env_release.c
similarity index 73%
rename from sched/environ/env_release.c
rename to libs/libc/environ/env_release.c
index d9b090662c7..2728fd95aa1 100644
--- a/sched/environ/env_release.c
+++ b/libs/libc/environ/env_release.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_release.c
+ * libs/libc/environ/env_release.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,15 +24,8 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <sched.h>
 #include <assert.h>
-#include <errno.h>
 
-#include <nuttx/kmalloc.h>
 #include <nuttx/tls.h>
 
 #include "environ/environ.h"
@@ -45,14 +38,14 @@
  * 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
+ *   env_release() is called only from task_uninit_info() when the task is
+ *   uninit. 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.
+ *   info - Identifies the task containing the environment structure
+ *          to be released.
  *
  * Returned Value:
  *   None
@@ -62,14 +55,11 @@
  *
  ****************************************************************************/
 
-void env_release(FAR struct task_group_s *group)
+void env_release(FAR struct task_info_s *info)
 {
-  FAR struct task_info_s *info;
   int i;
 
-  DEBUGASSERT(group != NULL && group->tg_info != NULL);
-
-  info = group->tg_info;
+  DEBUGASSERT(info != NULL);
 
   if (info->ta_envp)
     {
@@ -77,12 +67,12 @@ void env_release(FAR struct task_group_s *group)
 
       for (i = 0; info->ta_envp[i] != NULL; i++)
         {
-          group_free(group, info->ta_envp[i]);
+          lib_ufree(info->ta_envp[i]);
         }
 
       /* Free the environment */
 
-      group_free(group, info->ta_envp);
+      lib_ufree(info->ta_envp);
     }
 
   /* In any event, make sure that all environment-related variables in the
@@ -93,5 +83,3 @@ void env_release(FAR struct task_group_s *group)
   info->ta_envpc = 0;
   info->ta_envc  = 0;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_removevar.c b/libs/libc/environ/env_removevar.c
similarity index 81%
rename from sched/environ/env_removevar.c
rename to libs/libc/environ/env_removevar.c
index fd6eb0ef994..4c4f5e5432b 100644
--- a/sched/environ/env_removevar.c
+++ b/libs/libc/environ/env_removevar.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_removevar.c
+ * libs/libc/environ/env_removevar.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,15 +24,8 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <string.h>
-#include <sched.h>
 #include <assert.h>
 
-#include <nuttx/kmalloc.h>
 #include <nuttx/tls.h>
 
 #include "environ/environ.h"
@@ -48,7 +41,7 @@
  *   Remove the referenced name=value pair from the environment
  *
  * Input Parameters:
- *   group - The task group with the environment containing the name=value
+ *   info  - The task with the environment containing the name=value
  *           pair
  *   index - A index to the name=value pair in the restroom
  *
@@ -62,19 +55,13 @@
  *
  ****************************************************************************/
 
-void env_removevar(FAR struct task_group_s *group, ssize_t index)
+void env_removevar(FAR struct task_info_s *info, ssize_t index)
 {
-  FAR struct task_info_s *info;
-
-  DEBUGASSERT(group != NULL && group->tg_info != NULL);
-
-  info = group->tg_info;
-
-  DEBUGASSERT(index >= 0 && index < info->ta_envc);
+  DEBUGASSERT(info != NULL && index >= 0 && index < info->ta_envc);
 
   /* Free the allocate environment string */
 
-  group_free(group, info->ta_envp[index]);
+  lib_ufree(info->ta_envp[index]);
 
   /* Exchange the last env and the index env */
 
@@ -93,7 +80,7 @@ void env_removevar(FAR struct task_group_s *group, ssize_t index)
 
   if (info->ta_envc == 0)
     {
-      group_free(group, info->ta_envp);
+      lib_ufree(info->ta_envp);
       info->ta_envp = NULL;
       info->ta_envpc = 0;
     }
@@ -104,10 +91,8 @@ void env_removevar(FAR struct task_group_s *group, ssize_t index)
 
       info->ta_envpc = info->ta_envc + SCHED_ENVIRON_RESERVED + 1;
 
-      info->ta_envp = group_realloc(group, info->ta_envp,
+      info->ta_envp = lib_urealloc(info->ta_envp,
          sizeof(*info->ta_envp) * info->ta_envpc);
       DEBUGASSERT(info->ta_envp != NULL);
     }
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_secureexec.c b/libs/libc/environ/env_secureexec.c
similarity index 91%
rename from sched/environ/env_secureexec.c
rename to libs/libc/environ/env_secureexec.c
index 231fd54ba37..f3ae0fcc3ec 100644
--- a/sched/environ/env_secureexec.c
+++ b/libs/libc/environ/env_secureexec.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_secureexec.c
+ * libs/libc/environ/env_secureexec.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -31,9 +31,9 @@
 #include <stdlib.h>
 #include <assert.h>
 
+#include <nuttx/environ.h>
 #include <nuttx/tls.h>
 
-#include "sched/sched.h"
 #include "environ/environ.h"
 
 /****************************************************************************
@@ -99,16 +99,13 @@ static bool env_is_unsafe(FAR const char *pair)
  ****************************************************************************/
 
 #ifdef CONFIG_SCHED_USER_IDENTITY
-void env_sanitize_secure(FAR struct task_group_s *group)
+void env_sanitize_secure(FAR struct task_info_s *info)
 {
-  FAR struct task_info_s *info;
   ssize_t index;
 
-  DEBUGASSERT(group != NULL && group->tg_info != NULL);
+  DEBUGASSERT(info != NULL);
 
-  info = group->tg_info;
-
-  nxrmutex_lock(&group->tg_mutex);
+  nxrmutex_lock(&info->ta_lock);
 
   for (index = info->ta_envc - 1; index >= 0; )
     {
@@ -116,7 +113,7 @@ void env_sanitize_secure(FAR struct task_group_s *group)
 
       if (pair != NULL && env_is_unsafe(pair))
         {
-          env_removevar(group, index);
+          env_removevar(info, index);
         }
       else
         {
@@ -124,7 +121,7 @@ void env_sanitize_secure(FAR struct task_group_s *group)
         }
     }
 
-  nxrmutex_unlock(&group->tg_mutex);
+  nxrmutex_unlock(&info->ta_lock);
 }
 #endif
 
diff --git a/sched/environ/env_setenv.c b/libs/libc/environ/env_setenv.c
similarity index 85%
rename from sched/environ/env_setenv.c
rename to libs/libc/environ/env_setenv.c
index 115a45a68d6..0d2564b1b57 100644
--- a/sched/environ/env_setenv.c
+++ b/libs/libc/environ/env_setenv.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_setenv.c
+ * libs/libc/environ/env_setenv.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,22 +24,14 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <sched.h>
-#include <string.h>
 #include <assert.h>
 #include <errno.h>
+#include <stdio.h>
+#include <string.h>
 
-#include <nuttx/kmalloc.h>
 #include <nuttx/mutex.h>
 #include <nuttx/tls.h>
 
-#include "sched/sched.h"
 #include "environ/environ.h"
 
 /****************************************************************************
@@ -71,8 +63,6 @@
 
 int setenv(FAR const char *name, FAR const char *value, int overwrite)
 {
-  FAR struct tcb_s *rtcb;
-  FAR struct task_group_s *group;
   FAR struct task_info_s *info;
   FAR char *pvar;
   FAR char **envp;
@@ -113,17 +103,15 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
 
   /* Get a reference to the thread-private environ in the TCB. */
 
-  rtcb  = this_task();
-  group = rtcb->group;
-  DEBUGASSERT(group && group->tg_info);
+  info = task_get_info();
 
-  info = group->tg_info;
+  DEBUGASSERT(info != NULL);
 
-  nxrmutex_lock(&group->tg_mutex);
+  nxrmutex_lock(&info->ta_lock);
 
   /* Check if the variable already exists */
 
-  if (info->ta_envp && (ret = env_findvar(group, name)) >= 0)
+  if (info->ta_envp && (ret = env_findvar(info, name)) >= 0)
     {
       /* It does! Do we have permission to overwrite the existing value? */
 
@@ -131,7 +119,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
         {
           /* No.. then just return success */
 
-          nxrmutex_unlock(&group->tg_mutex);
+          nxrmutex_unlock(&info->ta_lock);
           return OK;
         }
 
@@ -140,7 +128,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
        * the environment buffer; this will happen below.
        */
 
-      env_removevar(group, ret);
+      env_removevar(info, ret);
     }
 
   /* Check current envirments count */
@@ -155,7 +143,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
 
   /* Then allocate or reallocate the environment buffer */
 
-  pvar = group_malloc(group, varlen);
+  pvar = lib_umalloc(varlen);
   if (pvar == NULL)
     {
       ret = ENOMEM;
@@ -168,7 +156,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
     {
       envpc = SCHED_ENVIRON_RESERVED + 2;
 
-      envp = group_malloc(group, sizeof(*envp) * envpc);
+      envp = lib_umalloc(sizeof(*envp) * envpc);
       if (envp == NULL)
         {
           ret = ENOMEM;
@@ -182,7 +170,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
     {
       envpc = envc + SCHED_ENVIRON_RESERVED + 2;
 
-      envp = group_realloc(group, info->ta_envp, sizeof(*envp) * envpc);
+      envp = lib_urealloc(info->ta_envp, sizeof(*envp) * envpc);
       if (envp == NULL)
         {
           ret = ENOMEM;
@@ -202,16 +190,14 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
   /* Now, put the new name=value string into the environment buffer */
 
   snprintf(pvar, varlen, "%s=%s", name, value);
-  nxrmutex_unlock(&group->tg_mutex);
+  nxrmutex_unlock(&info->ta_lock);
   return OK;
 
 errout_with_var:
-  group_free(group, pvar);
+  lib_ufree(pvar);
 errout_with_lock:
-  nxrmutex_unlock(&group->tg_mutex);
+  nxrmutex_unlock(&info->ta_lock);
 errout:
   set_errno(ret);
   return ERROR;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/env_unsetenv.c b/libs/libc/environ/env_unsetenv.c
similarity index 82%
rename from sched/environ/env_unsetenv.c
rename to libs/libc/environ/env_unsetenv.c
index 5008ef7e01b..9e2dbfc48ae 100644
--- a/sched/environ/env_unsetenv.c
+++ b/libs/libc/environ/env_unsetenv.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/env_unsetenv.c
+ * libs/libc/environ/env_unsetenv.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,18 +24,12 @@
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/config.h>
-
-#ifndef CONFIG_DISABLE_ENVIRON
-
-#include <sched.h>
-#include <string.h>
-#include <assert.h>
 #include <errno.h>
+#include <string.h>
 
-#include <nuttx/kmalloc.h>
+#include <nuttx/mutex.h>
+#include <nuttx/tls.h>
 
-#include "sched/sched.h"
 #include "environ/environ.h"
 
 /****************************************************************************
@@ -61,12 +55,9 @@
 
 int unsetenv(FAR const char *name)
 {
-  FAR struct tcb_s *rtcb = this_task();
-  FAR struct task_group_s *group = rtcb->group;
+  FAR struct task_info_s *info = task_get_info();
   ssize_t idx;
 
-  DEBUGASSERT(group);
-
   /* Check the incoming parameter */
 
   if (name == NULL || *name == '\0' || strchr(name, '=') != NULL)
@@ -77,16 +68,14 @@ int unsetenv(FAR const char *name)
 
   /* Check if the variable exists */
 
-  nxrmutex_lock(&group->tg_mutex);
-  if (group && (idx = env_findvar(group, name)) >= 0)
+  nxrmutex_lock(&info->ta_lock);
+  if (info && (idx = env_findvar(info, name)) >= 0)
     {
       /* It does!  Remove the name=value pair from the environment. */
 
-      env_removevar(group, idx);
+      env_removevar(info, idx);
     }
 
-  nxrmutex_unlock(&group->tg_mutex);
+  nxrmutex_unlock(&info->ta_lock);
   return OK;
 }
-
-#endif /* CONFIG_DISABLE_ENVIRON */
diff --git a/sched/environ/environ.h b/libs/libc/environ/environ.h
similarity index 55%
rename from sched/environ/environ.h
rename to libs/libc/environ/environ.h
index 31ad20e83b8..475007c9754 100644
--- a/sched/environ/environ.h
+++ b/libs/libc/environ/environ.h
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/environ/environ.h
+ * libs/libc/environ/environ.h
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -20,24 +20,24 @@
  *
  ****************************************************************************/
 
-#ifndef __SCHED_ENVIRON_ENVIRON_H
-#define __SCHED_ENVIRON_ENVIRON_H
+#ifndef __LIBS_LIBC_ENVIRON_ENVIRON_H
+#define __LIBS_LIBC_ENVIRON_ENVIRON_H
 
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
 #include <nuttx/config.h>
-#include <nuttx/sched.h>
+
+#include <sys/types.h>
+
+#include <nuttx/lib/lib.h>
 
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-#ifdef CONFIG_DISABLE_ENVIRON
-#  define env_dup(group, envp) (0)
-#  define env_release(group)   ((void)0)
-#else
+#ifndef CONFIG_DISABLE_ENVIRON
 
 #  define SCHED_ENVIRON_RESERVED (4)
 
@@ -57,66 +57,6 @@ extern "C"
  * Public Function Prototypes
  ****************************************************************************/
 
-/****************************************************************************
- * 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.
- *   envp  - Pointer to the environment strings.
- *
- * Returned Value:
- *   zero on success
- *
- * Assumptions:
- *   Not called from an interrupt handler.
- *
- ****************************************************************************/
-
-int env_dup(FAR struct task_group_s *group, FAR char * const *envp);
-
-/****************************************************************************
- * 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);
-
-#ifdef CONFIG_SCHED_USER_IDENTITY
-/****************************************************************************
- * Name: env_sanitize_secure
- *
- * Description:
- *   Remove environment variables that must not be inherited by a secure
- *   (set-user-ID or set-group-ID) executable.
- *
- ****************************************************************************/
-
-void env_sanitize_secure(FAR struct task_group_s *group);
-#endif
-
 /****************************************************************************
  * Name: env_findvar
  *
@@ -125,7 +65,7 @@ void env_sanitize_secure(FAR struct task_group_s *group);
  *   specified name.
  *
  * Input Parameters:
- *   group - The task group containing environment array to be searched.
+ *   info  - The task containing environment array to be searched.
  *   pname - The variable name to find
  *
  * Returned Value:
@@ -137,7 +77,7 @@ void env_sanitize_secure(FAR struct task_group_s *group);
  *
  ****************************************************************************/
 
-ssize_t env_findvar(FAR struct task_group_s *group, FAR const char *pname);
+ssize_t env_findvar(FAR struct task_info_s *info, FAR const char *pname);
 
 /****************************************************************************
  * Name: env_removevar
@@ -146,7 +86,7 @@ ssize_t env_findvar(FAR struct task_group_s *group, FAR const char *pname);
  *   Remove the referenced name=value pair from the environment
  *
  * Input Parameters:
- *   group - The task group with the environment containing the name=value
+ *   info  - The task with the environment containing the name=value
  *           pair
  *   index - A index to the name=value pair in the restroom
  *
@@ -160,7 +100,7 @@ ssize_t env_findvar(FAR struct task_group_s *group, FAR const char *pname);
  *
  ****************************************************************************/
 
-void env_removevar(FAR struct task_group_s *group, ssize_t index);
+void env_removevar(FAR struct task_info_s *info, ssize_t index);
 
 #undef EXTERN
 #ifdef __cplusplus
@@ -168,4 +108,4 @@ void env_removevar(FAR struct task_group_s *group, ssize_t index);
 #endif
 
 #endif /* !CONFIG_DISABLE_ENVIRON */
-#endif /* __SCHED_ENVIRON_ENVIRON_H */
+#endif /* __LIBS_LIBC_ENVIRON_ENVIRON_H */
diff --git a/libs/libc/pthread/pthread_atfork.c b/libs/libc/pthread/pthread_atfork.c
index 9cc1082cb89..f2f835235c1 100644
--- a/libs/libc/pthread/pthread_atfork.c
+++ b/libs/libc/pthread/pthread_atfork.c
@@ -79,9 +79,9 @@ int pthread_atfork(CODE void (*prepare)(void),
   entry->parent = parent;
   entry->child = child;
 
-  nxmutex_lock(&info->ta_lock);
+  nxrmutex_lock(&info->ta_lock);
   list_add_head(list, &entry->node);
-  nxmutex_unlock(&info->ta_lock);
+  nxrmutex_unlock(&info->ta_lock);
 #endif
 
   return OK;
diff --git a/libs/libc/pthread/pthread_keycreate.c b/libs/libc/pthread/pthread_keycreate.c
index f819dd98e0a..1ea4cedc698 100644
--- a/libs/libc/pthread/pthread_keycreate.c
+++ b/libs/libc/pthread/pthread_keycreate.c
@@ -98,7 +98,7 @@ int pthread_key_create(FAR pthread_key_t *key,
    * avoid concurrent modification of the group TLS index set.
    */
 
-  ret = nxmutex_lock(&info->ta_lock);
+  ret = nxrmutex_lock(&info->ta_lock);
   if (ret < 0)
     {
       return -ret;
@@ -129,7 +129,7 @@ int pthread_key_create(FAR pthread_key_t *key,
         }
     }
 
-  nxmutex_unlock(&info->ta_lock);
+  nxrmutex_unlock(&info->ta_lock);
   return ret;
 }
 
diff --git a/libs/libc/pthread/pthread_keydelete.c b/libs/libc/pthread/pthread_keydelete.c
index a8e5ceaffbc..805c943801f 100644
--- a/libs/libc/pthread/pthread_keydelete.c
+++ b/libs/libc/pthread/pthread_keydelete.c
@@ -71,12 +71,12 @@ int pthread_key_delete(pthread_key_t key)
        * modification of the group TLS index set.
        */
 
-      ret = nxmutex_lock(&info->ta_lock);
+      ret = nxrmutex_lock(&info->ta_lock);
       if (ret == OK)
         {
           info->ta_tlsdtor[key] = NULL;
           tls->tl_elem[key]     = 0;
-          nxmutex_unlock(&info->ta_lock);
+          nxrmutex_unlock(&info->ta_lock);
         }
       else
         {
diff --git a/libs/libc/stdlib/lib_atexit.c b/libs/libc/stdlib/lib_atexit.c
index cbc1e072b7f..132ab302ff4 100644
--- a/libs/libc/stdlib/lib_atexit.c
+++ b/libs/libc/stdlib/lib_atexit.c
@@ -82,7 +82,7 @@ int atexit_register(int type, CODE void (*func)(void), FAR void *arg,
 
   if (func)
     {
-      ret = nxmutex_lock(&info->ta_lock);
+      ret = nxrmutex_lock(&info->ta_lock);
       if (ret < 0)
         {
           return -ret;
@@ -101,7 +101,7 @@ int atexit_register(int type, CODE void (*func)(void), FAR void *arg,
           ret = ERROR;
         }
 
-      nxmutex_unlock(&info->ta_lock);
+      nxrmutex_unlock(&info->ta_lock);
     }
 
   return ret;
@@ -128,7 +128,7 @@ void atexit_call_exitfuncs(int status, bool quick)
        * before the older remaining ones, as documented in exit(3).
        */
 
-      if (nxmutex_lock(&info->ta_lock) < 0)
+      if (nxrmutex_lock(&info->ta_lock) < 0)
         {
           break;
         }
@@ -145,7 +145,7 @@ void atexit_call_exitfuncs(int status, bool quick)
       aehead->funcs[idx].arg  = NULL;
       aehead->nfuncs--;
 
-      nxmutex_unlock(&info->ta_lock);
+      nxrmutex_unlock(&info->ta_lock);
 
       if (!func)
         {
diff --git a/libs/libc/unistd/lib_fork.c b/libs/libc/unistd/lib_fork.c
index 84813db3265..29cd14a123f 100644
--- a/libs/libc/unistd/lib_fork.c
+++ b/libs/libc/unistd/lib_fork.c
@@ -58,7 +58,7 @@ static void atfork_prepare(void)
    * so we iterate over the func list in reverse order
    */
 
-  nxmutex_lock(&info->ta_lock);
+  nxrmutex_lock(&info->ta_lock);
   list_for_every_entry_reverse(list, entry,
                                struct pthread_atfork_s, node)
     {
@@ -68,7 +68,7 @@ static void atfork_prepare(void)
          }
     }
 
-  nxmutex_unlock(&info->ta_lock);
+  nxrmutex_unlock(&info->ta_lock);
 }
 
 /****************************************************************************
@@ -87,7 +87,7 @@ static void atfork_child(void)
 
   /* The parent handlers are called in the order of registration */
 
-  nxmutex_lock(&info->ta_lock);
+  nxrmutex_lock(&info->ta_lock);
   list_for_every_entry(list, entry,
                        struct pthread_atfork_s, node)
     {
@@ -97,7 +97,7 @@ static void atfork_child(void)
          }
     }
 
-  nxmutex_unlock(&info->ta_lock);
+  nxrmutex_unlock(&info->ta_lock);
 }
 
 /****************************************************************************
@@ -116,7 +116,7 @@ static void atfork_parent(void)
 
   /* The child handlers are called in the order of registration */
 
-  nxmutex_lock(&info->ta_lock);
+  nxrmutex_lock(&info->ta_lock);
   list_for_every_entry(list, entry,
                        struct pthread_atfork_s, node)
     {
@@ -126,7 +126,7 @@ static void atfork_parent(void)
         }
     }
 
-  nxmutex_unlock(&info->ta_lock);
+  nxrmutex_unlock(&info->ta_lock);
 }
 #endif
 
diff --git a/sched/Makefile b/sched/Makefile
index 79e81e0bbe2..b042dec2e3c 100644
--- a/sched/Makefile
+++ b/sched/Makefile
@@ -24,7 +24,6 @@ include $(TOPDIR)/Make.defs
 
 include addrenv/Make.defs
 include clock/Make.defs
-include environ/Make.defs
 include event/Make.defs
 include group/Make.defs
 include init/Make.defs
diff --git a/sched/group/group_join.c b/sched/group/group_join.c
index e2986b2bd09..8e22b5618b3 100644
--- a/sched/group/group_join.c
+++ b/sched/group/group_join.c
@@ -36,7 +36,6 @@
 
 #include "sched/sched.h"
 #include "group/group.h"
-#include "environ/environ.h"
 
 #ifndef CONFIG_DISABLE_PTHREAD
 
diff --git a/sched/group/group_leave.c b/sched/group/group_leave.c
index 5752d1c4375..80a8f1c7880 100644
--- a/sched/group/group_leave.c
+++ b/sched/group/group_leave.c
@@ -42,7 +42,6 @@
 #  include <nuttx/binfmt/binfmt.h>
 #endif
 
-#include "environ/environ.h"
 #include "signal/signal.h"
 #include "pthread/pthread.h"
 #include "mqueue/mqueue.h"
@@ -81,10 +80,6 @@ static inline void group_release(FAR struct task_group_s *group)
 
   nxrmutex_destroy(&group->tg_mutex);
 
-  /* Release all shared environment variables */
-
-  env_release(group);
-
   task_uninit_info(group);
 
 #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
diff --git a/sched/task/task_fork.c b/sched/task/task_fork.c
index ca7ff728636..0aabb955da7 100644
--- a/sched/task/task_fork.c
+++ b/sched/task/task_fork.c
@@ -37,9 +37,9 @@
 #include <nuttx/kmalloc.h>
 #include <nuttx/queue.h>
 #include <nuttx/semaphore.h>
+#include <nuttx/environ.h>
 
 #include "sched/sched.h"
-#include "environ/environ.h"
 #include "group/group.h"
 #include "task/task.h"
 #include "tls/tls.h"
@@ -317,7 +317,7 @@ FAR struct tcb_s *nxtask_setup_fork(start_t retaddr, bool vfork)
 
   /* Duplicate the parent tasks environment */
 
-  ret = env_dup(child->group, environ);
+  ret = env_dup(child->group->tg_info, environ);
   if (ret < 0)
     {
       goto errout_with_tcb;
diff --git a/sched/task/task_init.c b/sched/task/task_init.c
index 6460dff6205..8af86f498cf 100644
--- a/sched/task/task_init.c
+++ b/sched/task/task_init.c
@@ -36,9 +36,9 @@
 #include <nuttx/queue.h>
 #include <nuttx/sched.h>
 #include <nuttx/trace.h>
+#include <nuttx/environ.h>
 
 #include "sched/sched.h"
-#include "environ/environ.h"
 #include "group/group.h"
 #include "task/task.h"
 #include "tls/tls.h"
@@ -128,7 +128,7 @@ int nxtask_init(FAR struct tcb_s *tcb, const char *name, int priority,
 
   /* Duplicate the parent tasks environment */
 
-  ret = env_dup(tcb->group, envp);
+  ret = env_dup(tcb->group->tg_info, envp);
   if (ret < 0)
     {
       goto errout_with_group;
diff --git a/sched/tls/task_initinfo.c b/sched/tls/task_initinfo.c
index 20953aaa6a6..e268454608f 100644
--- a/sched/tls/task_initinfo.c
+++ b/sched/tls/task_initinfo.c
@@ -125,7 +125,7 @@ int task_init_info(FAR struct task_group_s *group)
 
   /* Initialize user space mutex */
 
-  nxmutex_init(&info->ta_lock);
+  nxrmutex_init(&info->ta_lock);
   group->tg_info = info;
 
 #ifdef CONFIG_PTHREAD_ATFORK
diff --git a/sched/tls/task_uninitinfo.c b/sched/tls/task_uninitinfo.c
index da525445583..c6ff0c79c54 100644
--- a/sched/tls/task_uninitinfo.c
+++ b/sched/tls/task_uninitinfo.c
@@ -29,6 +29,7 @@
 #include <nuttx/mutex.h>
 #include <nuttx/list.h>
 #include <nuttx/lib/lib.h>
+#include <nuttx/environ.h>
 
 #include "tls.h"
 
@@ -145,7 +146,11 @@ void task_uninit_info(FAR struct task_group_s *group)
   task_uninit_stream(group);
 #endif /* CONFIG_FILE_STREAM */
 
-  nxmutex_destroy(&info->ta_lock);
+  /* Release all environment variables */
+
+  env_release(info);
+
+  nxrmutex_destroy(&info->ta_lock);
 #ifdef CONFIG_MM_KERNEL_HEAP
   group_free(group, info);
 #endif
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 1b93dcc4d2d..1a88bd0045d 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -11,7 +11,6 @@
 "chmod","sys/stat.h","","int","FAR const char *","mode_t"
 "chown","unistd.h","","int","FAR const char *","uid_t","gid_t"
 "chroot","unistd.h","defined(CONFIG_FS_CHROOT)","int","FAR const char *"
-"clearenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int"
 "clock","time.h","","clock_t"
 "clock_adjtime","sys/timex.h","defined(CONFIG_CLOCK_ADJTIME)","int","clockid_t","struct timex *"
 "clock_getres","time.h","","int","clockid_t","FAR struct timespec *"
@@ -39,11 +38,9 @@
 "fsync","unistd.h","","int","int"
 "ftruncate","unistd.h","","int","int","off_t"
 "futimens","sys/stat.h","","int","int","const struct timespec [2]|FAR const struct timespec *"
-"get_environ_ptr","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char **"
 "getegid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","gid_t"
 "getresgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","gid_t *","gid_t *","gid_t *"
 "getresuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","uid_t *","uid_t *","uid_t *"
-"getenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char *","FAR const char *"
 "geteuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","uid_t"
 "getgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","gid_t"
 "getgroups","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY) && (CONFIG_SCHED_NGROUPS > 0)","int","int","FAR gid_t *"
@@ -128,7 +125,6 @@
 "pthread_setschedparam","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","int","FAR const struct sched_param *"
 "pthread_setschedprio","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_t","int"
 "pthread_sigmask","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","int","FAR const sigset_t *","FAR sigset_t *"
-"putenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char *"
 "pwrite","unistd.h","","ssize_t","int","FAR const void *","size_t","off_t"
 "read","unistd.h","","ssize_t","int","FAR void *","size_t"
 "readv","sys/uio.h","","ssize_t","int","FAR const struct iovec *","int"
@@ -165,7 +161,6 @@
 "setreuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","uid_t","uid_t"
 "setresgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","gid_t","gid_t","gid_t"
 "setresuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","uid_t","uid_t","uid_t"
-"setenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char *","FAR const char *","int"
 "seteuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","uid_t"
 "setgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","gid_t"
 "sethostname","unistd.h","","int","FAR const char *","size_t"
@@ -212,7 +207,6 @@
 "timerfd_settime","sys/timerfd.h","defined(CONFIG_TIMER_FD)","int","int","int","FAR const struct itimerspec *","FAR struct itimerspec *"
 "umount2","sys/mount.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char *","unsigned int"
 "unlink","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char *"
-"unsetenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char *"
 "up_fork","nuttx/arch.h","defined(CONFIG_ARCH_HAVE_VFORK) || defined(CONFIG_ARCH_HAVE_FORK)","pid_t","bool"
 "utimens","sys/stat.h","","int","FAR const char *","const struct timespec [2]|FAR const struct timespec *"
 "wait","sys/wait.h","defined(CONFIG_SCHED_WAITPID) && defined(CONFIG_SCHED_HAVE_PARENT)","pid_t","FAR int *"


(nuttx) 05/06: libs/libc/environ: Allocate the environment in the heap of its task.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 36562a7c2efd63d7b109c7719e845602a3cdca45
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 12 05:55:30 2026 +0200

    libs/libc/environ: Allocate the environment in the heap of its task.
    
    The environment functions now run in kernel code as well as in user
    code.  In kernel code lib_umalloc() is kumm_malloc(), the heap of the
    current user process.  A kernel build then faulted at boot: the idle
    task sets PATH before any user process exists.  The environment of a
    kernel thread is in the kernel heap, with its task information.
    
    Allocate and free each environment in the heap that holds its task
    information: the kernel heap for a kernel thread, the user heap for a
    user task.  User code still uses lib_umalloc().  putenv() allocates
    and frees its copy of the string the same way.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 libs/libc/environ/env_dup.c       |  8 ++++----
 libs/libc/environ/env_putenv.c    | 12 ++++++++++--
 libs/libc/environ/env_release.c   |  4 ++--
 libs/libc/environ/env_removevar.c |  6 +++---
 libs/libc/environ/env_setenv.c    |  8 ++++----
 libs/libc/environ/environ.h       | 23 +++++++++++++++++++++++
 6 files changed, 46 insertions(+), 15 deletions(-)

diff --git a/libs/libc/environ/env_dup.c b/libs/libc/environ/env_dup.c
index 220392d24a5..be85a18f564 100644
--- a/libs/libc/environ/env_dup.c
+++ b/libs/libc/environ/env_dup.c
@@ -95,7 +95,7 @@ int env_dup(FAR struct task_info_s *info, FAR char * const *envcp)
         {
           /* There is an environment, duplicate it */
 
-          envp = lib_umalloc(sizeof(*envp) * info->ta_envpc);
+          envp = env_malloc(info, sizeof(*envp) * info->ta_envpc);
           if (envp == NULL)
             {
               /* The parent's environment can not be inherited due to a
@@ -113,15 +113,15 @@ int env_dup(FAR struct task_info_s *info, FAR char * const *envcp)
               while (envc-- > 0)
                 {
                   size = strlen(envcp[envc]) + 1;
-                  envp[envc] = lib_umalloc(size);
+                  envp[envc] = env_malloc(info, size);
                   if (envp[envc] == NULL)
                     {
                       while (envp[++envc] != NULL)
                         {
-                          lib_ufree(envp[envc]);
+                          env_free(info, envp[envc]);
                         }
 
-                      lib_ufree(envp);
+                      env_free(info, envp);
                       envp = NULL;
                       ret = -ENOMEM;
                       break;
diff --git a/libs/libc/environ/env_putenv.c b/libs/libc/environ/env_putenv.c
index bc4c330d232..5f0e29155af 100644
--- a/libs/libc/environ/env_putenv.c
+++ b/libs/libc/environ/env_putenv.c
@@ -28,6 +28,9 @@
 #include <errno.h>
 
 #include <nuttx/lib/lib.h>
+#include <nuttx/tls.h>
+
+#include "environ.h"
 
 /****************************************************************************
  * Public Functions
@@ -57,8 +60,10 @@
 
 int putenv(FAR const char *string)
 {
+  FAR struct task_info_s *info = task_get_info();
   FAR char *pname;
   FAR char *pequal;
+  size_t len;
   int ret = OK;
 
   /* Verify that a string was passed */
@@ -71,13 +76,16 @@ int putenv(FAR const char *string)
 
   /* Parse the name=value string */
 
-  pname = strdup(string);
+  len   = strlen(string) + 1;
+  pname = env_malloc(info, len);
   if (!pname)
     {
       ret = ENOMEM;
       goto errout;
     }
 
+  memcpy(pname, string, len);
+
   pequal = strchr(pname, '=');
   if (pequal)
     {
@@ -87,7 +95,7 @@ int putenv(FAR const char *string)
       ret = setenv(pname, pequal + 1, TRUE);
     }
 
-  lib_ufree(pname);
+  env_free(info, pname);
   return ret;
 
 errout:
diff --git a/libs/libc/environ/env_release.c b/libs/libc/environ/env_release.c
index 2728fd95aa1..4c1abfcd49a 100644
--- a/libs/libc/environ/env_release.c
+++ b/libs/libc/environ/env_release.c
@@ -67,12 +67,12 @@ void env_release(FAR struct task_info_s *info)
 
       for (i = 0; info->ta_envp[i] != NULL; i++)
         {
-          lib_ufree(info->ta_envp[i]);
+          env_free(info, info->ta_envp[i]);
         }
 
       /* Free the environment */
 
-      lib_ufree(info->ta_envp);
+      env_free(info, info->ta_envp);
     }
 
   /* In any event, make sure that all environment-related variables in the
diff --git a/libs/libc/environ/env_removevar.c b/libs/libc/environ/env_removevar.c
index 4c4f5e5432b..207e0468b01 100644
--- a/libs/libc/environ/env_removevar.c
+++ b/libs/libc/environ/env_removevar.c
@@ -61,7 +61,7 @@ void env_removevar(FAR struct task_info_s *info, ssize_t index)
 
   /* Free the allocate environment string */
 
-  lib_ufree(info->ta_envp[index]);
+  env_free(info, info->ta_envp[index]);
 
   /* Exchange the last env and the index env */
 
@@ -80,7 +80,7 @@ void env_removevar(FAR struct task_info_s *info, ssize_t index)
 
   if (info->ta_envc == 0)
     {
-      lib_ufree(info->ta_envp);
+      env_free(info, info->ta_envp);
       info->ta_envp = NULL;
       info->ta_envpc = 0;
     }
@@ -91,7 +91,7 @@ void env_removevar(FAR struct task_info_s *info, ssize_t index)
 
       info->ta_envpc = info->ta_envc + SCHED_ENVIRON_RESERVED + 1;
 
-      info->ta_envp = lib_urealloc(info->ta_envp,
+      info->ta_envp = env_realloc(info, info->ta_envp,
          sizeof(*info->ta_envp) * info->ta_envpc);
       DEBUGASSERT(info->ta_envp != NULL);
     }
diff --git a/libs/libc/environ/env_setenv.c b/libs/libc/environ/env_setenv.c
index 0d2564b1b57..7bd5a1748db 100644
--- a/libs/libc/environ/env_setenv.c
+++ b/libs/libc/environ/env_setenv.c
@@ -143,7 +143,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
 
   /* Then allocate or reallocate the environment buffer */
 
-  pvar = lib_umalloc(varlen);
+  pvar = env_malloc(info, varlen);
   if (pvar == NULL)
     {
       ret = ENOMEM;
@@ -156,7 +156,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
     {
       envpc = SCHED_ENVIRON_RESERVED + 2;
 
-      envp = lib_umalloc(sizeof(*envp) * envpc);
+      envp = env_malloc(info, sizeof(*envp) * envpc);
       if (envp == NULL)
         {
           ret = ENOMEM;
@@ -170,7 +170,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
     {
       envpc = envc + SCHED_ENVIRON_RESERVED + 2;
 
-      envp = lib_urealloc(info->ta_envp, sizeof(*envp) * envpc);
+      envp = env_realloc(info, info->ta_envp, sizeof(*envp) * envpc);
       if (envp == NULL)
         {
           ret = ENOMEM;
@@ -194,7 +194,7 @@ int setenv(FAR const char *name, FAR const char *value, int overwrite)
   return OK;
 
 errout_with_var:
-  lib_ufree(pvar);
+  env_free(info, pvar);
 errout_with_lock:
   nxrmutex_unlock(&info->ta_lock);
 errout:
diff --git a/libs/libc/environ/environ.h b/libs/libc/environ/environ.h
index 475007c9754..2b5f2888b5b 100644
--- a/libs/libc/environ/environ.h
+++ b/libs/libc/environ/environ.h
@@ -33,6 +33,10 @@
 
 #include <nuttx/lib/lib.h>
 
+#ifdef __KERNEL__
+#  include <nuttx/kmalloc.h>
+#endif
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -41,6 +45,25 @@
 
 #  define SCHED_ENVIRON_RESERVED (4)
 
+/* The environment is in the heap that holds the task information: the
+ * kernel heap for a kernel thread, the user heap for a user task.  Kernel
+ * code also changes the environment of kernel threads (the idle task at
+ * boot) and of a new user task.
+ */
+
+#  ifdef __KERNEL__
+#    define env_malloc(info, n) \
+       (kmm_heapmember(info) ? kmm_malloc(n) : kumm_malloc(n))
+#    define env_realloc(info, p, n) \
+       (kmm_heapmember(info) ? kmm_realloc(p, n) : kumm_realloc(p, n))
+#    define env_free(info, p) \
+       (kmm_heapmember(info) ? kmm_free(p) : kumm_free(p))
+#  else
+#    define env_malloc(info, n)     ((void)(info), lib_umalloc(n))
+#    define env_realloc(info, p, n) ((void)(info), lib_urealloc(p, n))
+#    define env_free(info, p)       ((void)(info), lib_ufree(p))
+#  endif
+
 /****************************************************************************
  * Public Data
  ****************************************************************************/


(nuttx) 01/06: fs/procfs/fs_procfsproc.c: Indent the switch statements.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 4182f6faa541777508b505fd03961308bfd8f028
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 12 06:06:09 2026 +0200

    fs/procfs/fs_procfsproc.c: Indent the switch statements.
    
    Indent the case labels of two switch statements two columns inside
    the braces, as nxstyle checks.  Whitespace only.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 fs/procfs/fs_procfsproc.c | 90 +++++++++++++++++++++++------------------------
 1 file changed, 45 insertions(+), 45 deletions(-)

diff --git a/fs/procfs/fs_procfsproc.c b/fs/procfs/fs_procfsproc.c
index 6b515b182e7..26b31d2e7e4 100644
--- a/fs/procfs/fs_procfsproc.c
+++ b/fs/procfs/fs_procfsproc.c
@@ -1738,55 +1738,55 @@ static ssize_t proc_read(FAR struct file *filep, FAR char *buffer,
 
   switch (procfile->node->node)
     {
-    case PROC_STATUS: /* Task/thread status */
-      ret = proc_status(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_STATUS: /* Task/thread status */
+        ret = proc_status(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 
-    case PROC_CMDLINE: /* Task command line */
-      ret = proc_cmdline(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_CMDLINE: /* Task command line */
+        ret = proc_cmdline(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 
 #ifndef CONFIG_SCHED_CPULOAD_NONE
-    case PROC_LOADAVG: /* Average CPU utilization */
-      ret = proc_loadavg(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_LOADAVG: /* Average CPU utilization */
+        ret = proc_loadavg(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 #endif
 #ifdef CONFIG_SCHED_CRITMONITOR
-    case PROC_CRITMON: /* Critical section monitor */
-      ret = proc_critmon(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_CRITMON: /* Critical section monitor */
+        ret = proc_critmon(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 #endif
 #if CONFIG_MM_BACKTRACE >= 0
-    case PROC_HEAP: /* Task heap info */
-      ret = proc_heap(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_HEAP: /* Task heap info */
+        ret = proc_heap(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 #endif
 #ifdef CONFIG_DEBUG_MM
-    case PROC_HEAP_CHECK: /* Task heap check flag */
-      ret = proc_heapcheck(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_HEAP_CHECK: /* Task heap check flag */
+        ret = proc_heapcheck(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 #endif
-    case PROC_STACK: /* Task stack info */
-      ret = proc_stack(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_STACK: /* Task stack info */
+        ret = proc_stack(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 
-    case PROC_GROUP_STATUS: /* Task group status */
-      ret = proc_groupstatus(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_GROUP_STATUS: /* Task group status */
+        ret = proc_groupstatus(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 
-    case PROC_GROUP_FD: /* Group file descriptors */
-      ret = proc_groupfd(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_GROUP_FD: /* Group file descriptors */
+        ret = proc_groupfd(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 
 #if !defined(CONFIG_DISABLE_ENVIRON) && !defined(CONFIG_FS_PROCFS_EXCLUDE_ENVIRON)
-    case PROC_GROUP_ENV: /* Group environment variables */
-      ret = proc_groupenv(procfile, tcb, buffer, buflen, filep->f_pos);
-      break;
+      case PROC_GROUP_ENV: /* Group environment variables */
+        ret = proc_groupenv(procfile, tcb, buffer, buflen, filep->f_pos);
+        break;
 #endif
 
-     default:
-      ret = -EINVAL;
-      break;
+      default:
+        ret = -EINVAL;
+        break;
     }
 
   leave_critical_section(flags);
@@ -2083,18 +2083,18 @@ static int proc_readdir(FAR struct fs_dirent_s *dir,
 
       switch (procdir->node->node)
         {
-         case PROC_LEVEL0: /* Top level directory */
-           DEBUGASSERT(procdir->base.level == 1);
-           node = g_level0info[index];
-           break;
-
-         case PROC_GROUP:  /* Group sub-directory */
-           DEBUGASSERT(procdir->base.level == 2);
-           node = g_groupinfo[index];
-           break;
-
-         default:
-           return -ENOENT;
+          case PROC_LEVEL0: /* Top level directory */
+            DEBUGASSERT(procdir->base.level == 1);
+            node = g_level0info[index];
+            break;
+
+          case PROC_GROUP:  /* Group sub-directory */
+            DEBUGASSERT(procdir->base.level == 2);
+            node = g_groupinfo[index];
+            break;
+
+          default:
+            return -ENOENT;
         }
 
       /* Save the filename and file type */


(nuttx) 04/06: environ: make the environ variable addressable

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 483eea3d0864f673df7a1248164742313bf1b4c8
Author: guoshichao <gu...@xiaomi.com>
AuthorDate: Fri Feb 6 11:50:00 2026 +0800

    environ: make the environ variable addressable
    
    this modification will enable us can access environ in the following format:
    ```
    vputenv(&environ, "TZ=LOC-3");
    ```
    
    Signed-off-by: guoshichao <gu...@xiaomi.com>
    (cherry picked from commit 0c13138a9863b1a4c34ad434efc743d852162eb4
     in https://github.com/xiaoxiang781216/nuttx)
    
    Port to Apache master: keep master's named parameters and the
    secure_getenv() declaration in stdlib.h.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 include/stdlib.h                      | 4 ++--
 libs/libc/environ/env_getenvironptr.c | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/include/stdlib.h b/include/stdlib.h
index 06a56128258..1fc622fec7a 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -67,7 +67,7 @@
 #ifdef CONFIG_DISABLE_ENVIRON
 #  define environ NULL
 #else
-#  define environ get_environ_ptr()
+#  define environ (*get_environ_ptr())
 #endif
 
 #if defined(CONFIG_FS_LARGEFILE)
@@ -171,7 +171,7 @@ uint32_t  arc4random(void);
 
 /* Environment variable support */
 
-FAR char **get_environ_ptr(void);
+FAR char ***get_environ_ptr(void);
 FAR char *getenv(FAR const char *name);
 FAR char *secure_getenv(FAR const char *name);
 int       putenv(FAR const char *string);
diff --git a/libs/libc/environ/env_getenvironptr.c b/libs/libc/environ/env_getenvironptr.c
index 81ed2ebfe0c..33c7389a03e 100644
--- a/libs/libc/environ/env_getenvironptr.c
+++ b/libs/libc/environ/env_getenvironptr.c
@@ -48,7 +48,7 @@
  *
  ****************************************************************************/
 
-FAR char **get_environ_ptr(void)
+FAR char ***get_environ_ptr(void)
 {
-  return task_get_info()->ta_envp;
+  return &(task_get_info()->ta_envp);
 }