You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by gn...@apache.org on 2021/06/14 13:13:03 UTC

[incubator-nuttx-apps] branch master updated: Call posix_spawn with filename as the first argv entry

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

gnutt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 99e1674  Call posix_spawn with filename as the first argv entry
99e1674 is described below

commit 99e1674912995b9f764c9d2e80e67b31a14275ff
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Jun 13 14:10:24 2021 +0800

    Call posix_spawn with filename as the first argv entry
    
    pair with the kernel side change to follow the standard defintion:
    https://pubs.opengroup.org/onlinepubs/009695399/functions/posix_spawn.html
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 builtin/exec_builtin.c            |  4 +---
 examples/posix_spawn/spawn_main.c | 11 +++++------
 nshlib/nsh_fileapps.c             |  2 +-
 system/popen/popen.c              | 16 ++++++++--------
 system/system/system.c            | 13 +++++++------
 5 files changed, 22 insertions(+), 24 deletions(-)

diff --git a/builtin/exec_builtin.c b/builtin/exec_builtin.c
index 7687feb..58141fb 100644
--- a/builtin/exec_builtin.c
+++ b/builtin/exec_builtin.c
@@ -162,9 +162,7 @@ int exec_builtin(FAR const char *appname, FAR char * const *argv,
 #ifdef CONFIG_LIBC_EXECFUNCS
   /* Load and execute the application. */
 
-  ret = posix_spawn(&pid, builtin->name, &file_actions, &attr,
-                    (argv) ? &argv[1] : (FAR char * const *)NULL, NULL);
-
+  ret = posix_spawn(&pid, builtin->name, &file_actions, &attr, argv, NULL);
   if (ret != 0 && builtin->main != NULL)
 #endif
     {
diff --git a/examples/posix_spawn/spawn_main.c b/examples/posix_spawn/spawn_main.c
index 23ec90a..4d5e88c 100644
--- a/examples/posix_spawn/spawn_main.c
+++ b/examples/posix_spawn/spawn_main.c
@@ -118,14 +118,13 @@ static const char delimiter[] =
   "**************************************"
   "**************************************";
 static const char g_redirect[] = "redirect";
-static const char g_hello[]    = "hello";
 static const char g_data[]     = "testdata.txt";
 
 static char fullpath[128];
 
-static char * const g_argv[4] =
+static char * const g_argv[5] =
 {
-  "Argument 1", "Argument 2", "Argument 3", NULL
+  "hello", "Argument 1", "Argument 2", "Argument 3", NULL
 };
 
 /****************************************************************************
@@ -271,7 +270,7 @@ int main(int argc, FAR char *argv[])
    * this program from the others.
    */
 
-  testheader(g_hello);
+  testheader(g_argv[0]);
 
   /* Initialize the attributes file actions structure */
 
@@ -300,9 +299,9 @@ int main(int argc, FAR char *argv[])
    */
 
 #ifdef CONFIG_LIB_ENVPATH
-  filepath = g_hello;
+  filepath = g_argv[0];
 #else
-  snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_hello);
+  snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_argv[0]);
   filepath = fullpath;
 #endif
 
diff --git a/nshlib/nsh_fileapps.c b/nshlib/nsh_fileapps.c
index 0c8501c..3fc48c1 100644
--- a/nshlib/nsh_fileapps.c
+++ b/nshlib/nsh_fileapps.c
@@ -126,7 +126,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
    * failure.
    */
 
-  ret = posix_spawnp(&pid, cmd, &file_actions, &attr, &argv[1], NULL);
+  ret = posix_spawnp(&pid, cmd, &file_actions, &attr, argv, NULL);
   if (ret == OK)
     {
       /* The application was successfully started with pre-emption disabled.
diff --git a/system/popen/popen.c b/system/popen/popen.c
index d9f7757..eb950cc 100644
--- a/system/popen/popen.c
+++ b/system/popen/popen.c
@@ -113,7 +113,7 @@ FILE *popen(FAR const char *command, FAR const char *mode)
   struct sched_param param;
   posix_spawnattr_t attr;
   posix_spawn_file_actions_t file_actions;
-  FAR char *argv[3];
+  FAR char *argv[4];
   int fd[2];
   int oldfd;
   int newfd;
@@ -244,17 +244,17 @@ FILE *popen(FAR const char *command, FAR const char *mode)
    * appropriately.
    */
 
-  argv[0] = "-c";
-  argv[1] = (FAR char *)command;
-  argv[2] = NULL;
+  argv[1] = "-c";
+  argv[2] = (FAR char *)command;
+  argv[3] = NULL;
 
 #ifdef CONFIG_SYSTEM_POPEN_SHPATH
-  errcode = posix_spawn(&container->shell, CONFIG_SYSTEM_POPEN_SHPATH,
-                        &file_actions, &attr, argv,
-                        (FAR char * const *)NULL);
+  argv[0] = CONFIG_SYSTEM_POPEN_SHPATH;
+  errcode = posix_spawn(&container->shell, argv[0], &file_actions,
+                        &attr, argv, (FAR char * const *)NULL);
 #else
   container->shell = task_spawn("popen", nsh_system, &file_actions,
-                                &attr, argv, (FAR char * const *)NULL);
+                                &attr, argv + 1, (FAR char * const *)NULL);
   if (container->shell < 0)
     {
       errcode = -container->shell;
diff --git a/system/system/system.c b/system/system/system.c
index b48e91d..a0df808 100644
--- a/system/system/system.c
+++ b/system/system/system.c
@@ -60,7 +60,7 @@
 
 int system(FAR const char *cmd)
 {
-  FAR char *argv[3];
+  FAR char *argv[4];
   struct sched_param param;
   posix_spawnattr_t attr;
   pid_t pid;
@@ -128,16 +128,17 @@ int system(FAR const char *cmd)
 
   /* Spawn nsh_system() which will execute the command under the shell. */
 
-  argv[0] = "-c";
-  argv[1] = (FAR char *)cmd;
-  argv[2] = NULL;
+  argv[1] = "-c";
+  argv[2] = (FAR char *)cmd;
+  argv[3] = NULL;
 
 #ifdef CONFIG_SYSTEM_SYSTEM_SHPATH
-  errcode = posix_spawn(&pid, CONFIG_SYSTEM_SYSTEM_SHPATH,  NULL, &attr,
+  argv[0] = CONFIG_SYSTEM_SYSTEM_SHPATH;
+  errcode = posix_spawn(&pid, argv[0],  NULL, &attr,
                         argv, (FAR char * const *)NULL);
 #else
   pid = task_spawn("system", nsh_system, NULL, &attr,
-                   argv, (FAR char * const *)NULL);
+                   argv + 1, (FAR char * const *)NULL);
   if (pid < 0)
     {
       errcode = -pid;