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 2020/05/03 18:16:11 UTC

[incubator-nuttx] 06/09: binfmt: exec_spawn as internal function shouldn't modify errno

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

commit 8d1a0c2761c0a104afcd7802765f99b386b03482
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun May 3 19:33:42 2020 +0800

    binfmt: exec_spawn as internal function shouldn't modify errno
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 binfmt/binfmt_exec.c          | 30 +++++++++++++++++-------------
 include/nuttx/binfmt/binfmt.h |  3 +--
 sched/task/task_posixspawn.c  |  2 +-
 3 files changed, 19 insertions(+), 16 deletions(-)

diff --git a/binfmt/binfmt_exec.c b/binfmt/binfmt_exec.c
index b2d0dec..22a9b45 100644
--- a/binfmt/binfmt_exec.c
+++ b/binfmt/binfmt_exec.c
@@ -79,9 +79,8 @@
  *   attr     - The spawn attributes.
  *
  * Returned Value:
- *   This is an end-user function, so it follows the normal convention:
  *   It returns the PID of the exec'ed module.  On failure, it returns
- *   -1 (ERROR) and sets errno appropriately.
+ *   the negative errno value appropriately.
  *
  ****************************************************************************/
 
@@ -91,7 +90,6 @@ int exec_spawn(FAR const char *filename, FAR char * const *argv,
 {
   FAR struct binary_s *bin;
   int pid;
-  int errcode;
   int ret;
 
   /* Allocate the load information */
@@ -100,7 +98,7 @@ int exec_spawn(FAR const char *filename, FAR char * const *argv,
   if (!bin)
     {
       berr("ERROR: Failed to allocate binary_s\n");
-      errcode = ENOMEM;
+      ret = -ENOMEM;
       goto errout;
     }
 
@@ -115,8 +113,7 @@ int exec_spawn(FAR const char *filename, FAR char * const *argv,
   ret = binfmt_copyargv(bin, argv);
   if (ret < 0)
     {
-      errcode = -ret;
-      berr("ERROR: Failed to copy argv[]: %d\n", errcode);
+      berr("ERROR: Failed to copy argv[]: %d\n", ret);
       goto errout_with_bin;
     }
 
@@ -125,8 +122,7 @@ int exec_spawn(FAR const char *filename, FAR char * const *argv,
   ret = load_module(bin);
   if (ret < 0)
     {
-      errcode = -ret;
-      berr("ERROR: Failed to load program '%s': %d\n", filename, errcode);
+      berr("ERROR: Failed to load program '%s': %d\n", filename, ret);
       goto errout_with_argv;
     }
 
@@ -159,9 +155,9 @@ int exec_spawn(FAR const char *filename, FAR char * const *argv,
   pid = exec_module(bin);
   if (pid < 0)
     {
-      errcode = -pid;
+      ret = pid;
       berr("ERROR: Failed to execute program '%s': %d\n",
-           filename, errcode);
+           filename, ret);
       goto errout_with_lock;
     }
 
@@ -197,8 +193,7 @@ errout_with_argv:
 errout_with_bin:
   kmm_free(bin);
 errout:
-  set_errno(errcode);
-  return ERROR;
+  return ret;
 }
 
 /****************************************************************************
@@ -265,7 +260,16 @@ errout:
 int exec(FAR const char *filename, FAR char * const *argv,
          FAR const struct symtab_s *exports, int nexports)
 {
-  return exec_spawn(filename, argv, exports, nexports, NULL);
+  int ret;
+
+  ret = exec_spawn(filename, argv, exports, nexports, NULL);
+  if (ret < 0)
+    {
+      set_errno(-ret);
+      ret = ERROR;
+    }
+
+  return ret;
 }
 
 #endif /* !CONFIG_BINFMT_DISABLE */
diff --git a/include/nuttx/binfmt/binfmt.h b/include/nuttx/binfmt/binfmt.h
index b3f22ec..668157d 100644
--- a/include/nuttx/binfmt/binfmt.h
+++ b/include/nuttx/binfmt/binfmt.h
@@ -306,9 +306,8 @@ int exec_module(FAR const struct binary_s *bin);
  *   nexports - The number of symbols in the exports table.
  *
  * Returned Value:
- *   This is an end-user function, so it follows the normal convention:
  *   It returns the PID of the exec'ed module.  On failure, it returns
- *   -1 (ERROR) and sets errno appropriately.
+ *   the negative errno value appropriately.
  *
  ****************************************************************************/
 
diff --git a/sched/task/task_posixspawn.c b/sched/task/task_posixspawn.c
index 15850ba..a7b8feb 100644
--- a/sched/task/task_posixspawn.c
+++ b/sched/task/task_posixspawn.c
@@ -108,7 +108,7 @@ static int nxposix_spawn_exec(FAR pid_t *pidp, FAR const char *path,
   pid = exec_spawn(path, (FAR char * const *)argv, symtab, nsymbols, attr);
   if (pid < 0)
     {
-      ret = get_errno();
+      ret = -pid;
       serr("ERROR: exec failed: %d\n", ret);
       goto errout;
     }