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-apps.git

commit 11c1c6becee5cd3254448c6857bc0f4e2f330d05
Author: wangjianyu3 <[email protected]>
AuthorDate: Wed Aug 26 22:54:16 2026 +0800

    system/nxinit: add 'set' builtin command for environment variables
    
    Previously, 'set KEY VALUE' in init.rc was not recognized as a builtin
    command. It fell through to posix_spawnp(), which ran it in a
    temporary child shell. The environment variable was set only in the
    child process and lost when it exited, so services started afterward
    never inherited it.
    
    Register cmd_set as an init builtin that calls setenv(key, value, 1)
    directly in the init process. The command takes exactly 2 arguments
    (key and value). All code is guarded by CONFIG_DISABLE_ENVIRON so it
    compiles out when environment support is disabled.
    
    Since child processes inherit init's environment, 'set TZ Asia/Shanghai'
    in init.rc now correctly propagates to all subsequently started
    services.
    
    Signed-off-by: wangjianyu3 <[email protected]>
---
 system/nxinit/builtin.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/system/nxinit/builtin.c b/system/nxinit/builtin.c
index f0097d90c..5ed7f3a29 100644
--- a/system/nxinit/builtin.c
+++ b/system/nxinit/builtin.c
@@ -64,6 +64,10 @@ static int cmd_start(FAR struct action_manager_s *am,
                      int argc, FAR char **argv);
 static int cmd_stop(FAR struct action_manager_s *am,
                     int argc, FAR char **argv);
+#ifndef CONFIG_DISABLE_ENVIRON
+static int cmd_set(FAR struct action_manager_s *am,
+                   int argc, FAR char **argv);
+#endif
 #if CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX > 1
 static int cmd_setprop(FAR struct action_manager_s *am,
                        int argc, FAR char **argv);
@@ -107,6 +111,9 @@ static const struct cmd_map_s g_builtin[] =
   {"class_stop", 2, 2, cmd_class_stop},
   {"exec", 3, 99, cmd_exec},
   {"exec_start", 2, 2, cmd_exec_start},
+#ifndef CONFIG_DISABLE_ENVIRON
+  {"set", 3, 3, cmd_set},
+#endif
 #if CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX > 1
   {"setprop", 3, 3, cmd_setprop},
 #endif
@@ -221,6 +228,16 @@ static int cmd_stop(FAR struct action_manager_s *am,
   return init_service_stop(service);
 }
 
+#ifndef CONFIG_DISABLE_ENVIRON
+static int cmd_set(FAR struct action_manager_s *am,
+                   int argc, FAR char **argv)
+{
+  UNUSED(am);
+  init_info("setenv '%s' '%s'", argv[1], argv[2]);
+  return setenv(argv[1], argv[2], 1);
+}
+#endif
+
 #if CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX > 1
 static int cmd_setprop(FAR struct action_manager_s *am, int argc,
                        FAR char **argv)

Reply via email to