From 8d1a0c2761c0a104afcd7802765f99b386b03482 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Sun, 3 May 2020 19:33:42 +0800 Subject: [PATCH] binfmt: exec_spawn as internal function shouldn't modify errno Signed-off-by: Xiang Xiao --- 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 b2d0dec37b..22a9b459c7 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 b3f22ec87a..668157d1a1 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 15850bac3a..a7b8feb78e 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; }