Squashed commit of the following:

binfmt: Fix some compilation issues introduced in previous changes.  Verfied with the STM32F4-Discovery ELF configuration.

    binfmt:  schedule_unload() is an internal OS function and must not alter the errno variable.

    binfmt:  unload_module() is an internal OS function and must not alter the errno variable.

    binfmt:  load_module() is an internal OS function and must not alter the errno variable.

    binfmt:  exec_module() is an internal OS function and must not alter the errno variable.
This commit is contained in:
Gregory Nutt 2017-10-02 15:30:55 -06:00
parent 3688ea2f90
commit bc2cded397
10 changed files with 67 additions and 116 deletions

View file

@ -8,7 +8,7 @@
<tr align="center" bgcolor="#e4e4e4">
<td>
<h1><big><font color="#3c34ec"><i>NuttX Binary Loader</i></font></big></h1>
<p>Last Updated: August 22, 2014</p>
<p>Last Updated: October 1, 2017</p>
</td>
</tr>
</table>
@ -280,9 +280,7 @@ int load_module(FAR struct binary_s *bin);
</ul>
<p><b>Returned Value:</b></p>
<ul>
This is an end-user function, so it follows the normal convention:
Returns 0 (<code>OK</code>) on success.
On failure, it returns -1 (<code>ERROR</code>) with <code>errno</code> set appropriately.
This is a NuttX internal function so it follows the convention that 0 (<code>OK</code>) is returned on success and a negated <code>errno</code> is returned on failure.
</ul>
<h3>2.3.4 <a name="unload_module"><code>unload_module()</code></a></h3>
@ -304,7 +302,7 @@ int unload_module(FAR struct binary_s *bin);
</ul>
<p><b>Returned Value:</b></p>
<ul>
This is a NuttX internal function so it follows the convention that 0 (<code>OK</code>) is returned on success and a negated <code>errno</code> is returned on failure.
This is a NuttX internal function so it follows the convention that 0 (<code>OK</code>) is returned on success and a negated <code>errno</code> is returned on failure.
</ul>
<h3>2.3.5 <a name="exec_module"><code>exec_module()</code></a></h3>
@ -315,13 +313,11 @@ int exec_module(FAR const struct binary_s *bin);
</pre></ul>
<p><b>Description:</b></p>
<ul>
Execute a module that has been loaded into memory by <code>load_module()</code>.
Execute a module that has been loaded into memory by <code>load_module()</code>.
</ul>
<p><b>Returned Value:</b></p>
<ul>
This is an end-user function, so it follows the normal convention:
Returns 0 (<code>OK</code>) on success.
On failure, it returns -1 (<code>ERROR</code>) with <code>errno</code> set appropriately.
This is a NuttX internal function so it follows the convention that 0 (<code>OK</code>) is returned on success and a negated <code>errno</code> is returned on failure.
</ul>
<h3>2.3.7 <a name="exec"><code>exec()</code></a></h3>

View file

@ -9,7 +9,7 @@
<tr align="center" bgcolor="#e4e4e4">
<td>
<h1><big><font color="#3c34ec"><i>NXFLAT</i></font></big></h1>
<p>Last Updated: September 1, 2012</p>
<p>Last Updated: October 1, 2017</p>
</td>
</tr>
</table>
@ -652,9 +652,7 @@ cat ../syscall/syscall.csv ../libc/libc.csv | sort >tmp.csv
prep the module for execution.
</p>
<p><b>Returned Value:</b>
This is an end-user function, so it follows the normal convention:
Returns 0 (<code>OK</code>) on success. On failure, it returns -1 (<code>ERROR</code>) with
errno set appropriately.
This is a NuttX internal function so it follows the convention that 0 (<code>OK</code>) is returned on success and a negated <code>errno</code> is returned on failure.
</p>
</ul>
@ -665,9 +663,7 @@ cat ../syscall/syscall.csv ../libc/libc.csv | sort >tmp.csv
been started (via <code>exec_module()</code>), calling this will be fatal.
</p>
<p><b>Returned Value:</b>
This is a NuttX internal function so it follows the convention that
0 (<code>OK</code>) is returned on success and a negated errno is returned on
failure.
This is a NuttX internal function so it follows the convention that 0 (<code>OK</code>) is returned on success and a negated <code>errno</code> is returned on failure.
</p>
</ul>
@ -677,9 +673,7 @@ cat ../syscall/syscall.csv ../libc/libc.csv | sort >tmp.csv
Execute a module that has been loaded into memory by load_module().
</p>
<p><b>Returned Value:</b>
This is an end-user function, so it follows the normal convention:
Returns the PID of the exec'ed module. On failure, it.returns
-1 (<code>ERROR</code>) and sets errno appropriately.
This is a NuttX internal function so it follows the convention that 0 (<code>OK</code>) is returned on success and a negated <code>errno</code> is returned on failure.
</p>
</ul>
</ul>

View file

@ -12,7 +12,7 @@
<h1><big><font color="#3c34ec">
<i>NuttX RTOS Porting Guide</i>
</font></big></h1>
<p>Last Updated: August 30, 2017</p>
<p>Last Updated: October 1, 2017</p>
</td>
</tr>
</table>

View file

@ -1,7 +1,7 @@
/****************************************************************************
* binfmt/binfmt_exec.c
*
* Copyright (C) 2009, 2013-2014 Gregory Nutt. All rights reserved.
* Copyright (C) 2009, 2013-2014, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -120,7 +120,7 @@ int exec(FAR const char *filename, FAR char * const *argv,
ret = load_module(bin);
if (ret < 0)
{
errcode = get_errno();
errcode = -ret;
berr("ERROR: Failed to load program '%s': %d\n", filename, errcode);
goto errout_with_argv;
}
@ -137,7 +137,7 @@ int exec(FAR const char *filename, FAR char * const *argv,
pid = exec_module(bin);
if (pid < 0)
{
errcode = get_errno();
errcode = -pid;
berr("ERROR: Failed to execute program '%s': %d\n", filename, errcode);
goto errout_with_lock;
}
@ -149,8 +149,7 @@ int exec(FAR const char *filename, FAR char * const *argv,
ret = schedule_unload(pid, bin);
if (ret < 0)
{
errcode = get_errno();
berr("ERROR: Failed to schedule unload '%s': %d\n", filename, errcode);
berr("ERROR: Failed to schedule unload '%s': %d\n", filename, ret);
}
sched_unlock();
@ -158,7 +157,7 @@ int exec(FAR const char *filename, FAR char * const *argv,
errout_with_lock:
sched_unlock();
unload_module(bin);
(void)unload_module(bin);
errout_with_argv:
binfmt_freeargv(bin);
errout_with_bin:
@ -182,7 +181,7 @@ errout:
ret = load_module(&bin);
if (ret < 0)
{
errcode = get_errno();
errcode = -ret;
berr("ERROR: Failed to load program '%s': %d\n", filename, errcode);
goto errout;
}
@ -192,7 +191,7 @@ errout:
ret = exec_module(&bin);
if (ret < 0)
{
errcode = get_errno();
errcode = -ret;
berr("ERROR: Failed to execute program '%s': %d\n", filename, errcode);
goto errout_with_module;
}
@ -202,7 +201,7 @@ errout:
return ret;
errout_with_module:
unload_module(&bin);
(void)unload_module(&bin);
errout:
set_errno(errcode);
return ERROR;

View file

@ -1,7 +1,7 @@
/****************************************************************************
* binfmt/binfmt_execmodule.c
*
* Copyright (C) 2009, 2013-2014 Gregory Nutt. All rights reserved.
* Copyright (C) 2009, 2013-2014, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -69,14 +69,6 @@
# errror "CONFIG_SCHED_STARTHOOK must be defined to use constructors"
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
@ -128,9 +120,9 @@ static void exec_ctors(FAR void *arg)
* Execute a module that has been loaded into memory by load_module().
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* Returns the PID of the exec'ed module. On failure, it returns
* -1 (ERROR) and sets errno appropriately.
* This is a NuttX internal function so it follows the convention that
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
@ -142,7 +134,6 @@ int exec_module(FAR const struct binary_s *binp)
#endif
FAR uint32_t *stack;
pid_t pid;
int errcode;
int ret;
/* Sanity checking */
@ -150,8 +141,7 @@ int exec_module(FAR const struct binary_s *binp)
#ifdef CONFIG_DEBUG_FEATURES
if (!binp || !binp->entrypt || binp->stacksize <= 0)
{
errcode = EINVAL;
goto errout;
return -EINVAL;
}
#endif
@ -162,8 +152,7 @@ int exec_module(FAR const struct binary_s *binp)
tcb = (FAR struct task_tcb_s *)kmm_zalloc(sizeof(struct task_tcb_s));
if (!tcb)
{
errcode = ENOMEM;
goto errout;
return -ENOMEM;
}
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
@ -173,7 +162,6 @@ int exec_module(FAR const struct binary_s *binp)
if (ret < 0)
{
berr("ERROR: up_addrenv_select() failed: %d\n", ret);
errcode = -ret;
goto errout_with_tcb;
}
@ -192,7 +180,7 @@ int exec_module(FAR const struct binary_s *binp)
stack = (FAR uint32_t *)kumm_malloc(binp->stacksize);
if (!stack)
{
errcode = ENOMEM;
ret = -ENOMEM;
goto errout_with_addrenv;
}
@ -202,8 +190,8 @@ int exec_module(FAR const struct binary_s *binp)
stack, binp->stacksize, binp->entrypt, binp->argv);
if (ret < 0)
{
errcode = get_errno();
berr("task_init() failed: %d\n", errcode);
ret = -get_errno();
berr("task_init() failed: %d\n", ret);
goto errout_with_addrenv;
}
@ -225,7 +213,6 @@ int exec_module(FAR const struct binary_s *binp)
if (ret < 0)
{
berr("ERROR: up_addrenv_select() failed: %d\n", ret);
errcode = -ret;
goto errout_with_tcbinit;
}
#endif
@ -237,7 +224,6 @@ int exec_module(FAR const struct binary_s *binp)
if (ret < 0)
{
berr("ERROR: shm_group_initialize() failed: %d\n", ret);
errcode = -ret;
goto errout_with_tcbinit;
}
#endif
@ -260,7 +246,6 @@ int exec_module(FAR const struct binary_s *binp)
ret = up_addrenv_clone(&binp->addrenv, &tcb->cmn.group->tg_addrenv);
if (ret < 0)
{
errcode = -ret;
berr("ERROR: up_addrenv_clone() failed: %d\n", ret);
goto errout_with_tcbinit;
}
@ -291,8 +276,8 @@ int exec_module(FAR const struct binary_s *binp)
ret = task_activate((FAR struct tcb_s *)tcb);
if (ret < 0)
{
errcode = get_errno();
berr("task_activate() failed: %d\n", errcode);
ret = -get_errno();
berr("task_activate() failed: %d\n", ret);
goto errout_with_tcbinit;
}
@ -303,7 +288,6 @@ int exec_module(FAR const struct binary_s *binp)
if (ret < 0)
{
berr("ERROR: up_addrenv_select() failed: %d\n", ret);
errcode = -ret;
goto errout_with_tcbinit;
}
#endif
@ -314,7 +298,7 @@ errout_with_tcbinit:
tcb->cmn.stack_alloc_ptr = NULL;
sched_releasetcb(&tcb->cmn, TCB_FLAG_TTYPE_TASK);
kumm_free(stack);
goto errout;
return ret;
errout_with_addrenv:
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL)
@ -323,11 +307,7 @@ errout_with_addrenv:
errout_with_tcb:
#endif
kmm_free(tcb);
errout:
set_errno(errcode);
berr("returning errno: %d\n", errcode);
return ERROR;
return ret;
}
#endif /* CONFIG_BINFMT_DISABLE */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* binfmt/binfmt_loadmodule.c
*
* Copyright (C) 2009, 2014 Gregory Nutt. All rights reserved.
* Copyright (C) 2009, 2014, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -76,8 +76,8 @@
* between calls to load_module() and exec_module().
*
* Returned Value:
* Zero (OK) is returned on success; Otherwise, -1 (ERROR) is returned and
* the errno variable is set appropriately.
* Zero (OK) is returned on success; Otherwise a negated errno value is
* returned.
*
****************************************************************************/
@ -91,8 +91,9 @@ static int load_default_priority(FAR struct binary_s *bin)
ret = sched_getparam(0, &param);
if (ret < 0)
{
berr("ERROR: sched_getparam failed: %d\n", get_errno());
return ERROR;
ret = -get_errno();
berr("ERROR: sched_getparam failed: %d\n", ret);
return ret;
}
/* Save that as the priority of child thread */
@ -168,9 +169,9 @@ static int load_absmodule(FAR struct binary_s *bin)
* prep the module for execution.
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with
* errno set appropriately.
* This is a NuttX internal function so it follows the convention that
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
@ -189,9 +190,7 @@ int load_module(FAR struct binary_s *bin)
ret = load_default_priority(bin);
if (ret < 0)
{
/* The errno is already set in this case */
return ERROR;
return ret;
}
/* Were we given a relative path? Or an absolute path to the file to
@ -258,16 +257,7 @@ int load_module(FAR struct binary_s *bin)
}
}
/* This is an end-user function. Return failures via errno */
if (ret < 0)
{
berr("ERROR: Returning errno %d\n", -ret);
set_errno(-ret);
return ERROR;
}
return OK;
return ret;
}
#endif /* CONFIG_BINFMT_DISABLE */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* binfmt/binfmt_schedunload.c
*
* Copyright (C) 2013, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2013, 2016-2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -227,7 +227,7 @@ static void unload_callback(int signo, siginfo_t *info, void *ucontext)
ret = unload_module(bin);
if (ret < 0)
{
berr("ERROR: unload_module failed: %d\n", get_errno());
berr("ERROR: unload_module() failed: %d\n", ret);
}
/* Free the load structure */
@ -256,9 +256,9 @@ static void unload_callback(int signo, siginfo_t *info, void *ucontext)
* persist until the task unloads
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* It returns 0 (OK) if the callback was successfully scheduled. On
* failure, it returns -1 (ERROR) and sets errno appropriately.
* This is a NuttX internal function so it follows the convention that
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
* On failures, the 'bin' structure will not be deallocated and the
* module not not be unloaded.
@ -271,7 +271,6 @@ int schedule_unload(pid_t pid, FAR struct binary_s *bin)
struct sigaction oact;
sigset_t set;
irqstate_t flags;
int errorcode;
int ret;
/* Make sure that SIGCHLD is unmasked */
@ -283,9 +282,9 @@ int schedule_unload(pid_t pid, FAR struct binary_s *bin)
{
/* The errno value will get trashed by the following debug output */
errorcode = get_errno();
ret = -get_errno();
berr("ERROR: sigprocmask failed: %d\n", ret);
goto errout;
return ret;
}
/* Add the structure to the list. We want to do this *before* connecting
@ -309,7 +308,7 @@ int schedule_unload(pid_t pid, FAR struct binary_s *bin)
{
/* The errno value will get trashed by the following debug output */
errorcode = get_errno();
ret = -get_errno();
berr("ERROR: sigaction failed: %d\n" , ret);
/* Emergency removal from the list */
@ -321,14 +320,9 @@ int schedule_unload(pid_t pid, FAR struct binary_s *bin)
}
leave_critical_section(flags);
goto errout;
}
return OK;
errout:
set_errno(errorcode);
return ERROR;
return ret;
}
#endif /* !CONFIG_BINFMT_DISABLE && CONFIG_SCHED_HAVE_PARENT */

View file

@ -1,7 +1,7 @@
/****************************************************************************
* binfmt/binfmt_loadmodule.c
*
* Copyright (C) 2009, 2012-2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2009, 2012-2013, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -162,8 +162,7 @@ int unload_module(FAR struct binary_s *binp)
if (ret < 0)
{
berr("binp->unload() failed: %d\n", ret);
set_errno(-ret);
return ERROR;
return ret;
}
}
@ -174,8 +173,7 @@ int unload_module(FAR struct binary_s *binp)
if (ret < 0)
{
berr("exec_ctors() failed: %d\n", ret);
set_errno(-ret);
return ERROR;
return ret;
}
#endif

View file

@ -300,7 +300,7 @@ static void pcode_onexit(int exitcode, FAR void *arg)
/* And unload the module */
unload_module(binp);
(void)unload_module(binp);
}
#endif

View file

@ -1,7 +1,7 @@
/****************************************************************************
* include/nuttx/binfmt/binfmt.h
*
* Copyright (C) 2009, 2012, 2014 Gregory Nutt. All rights reserved.
* Copyright (C) 2009, 2012, 2014, 2017 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -215,9 +215,9 @@ int unregister_binfmt(FAR struct binfmt_s *binfmt);
* prep the module for execution.
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with
* errno set appropriately.
* This is a NuttX internal function so it follows the convention that
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
@ -251,9 +251,9 @@ int unload_module(FAR struct binary_s *bin);
* Execute a module that has been loaded into memory by load_module().
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* Returns the PID of the exec'ed module. On failure, it returns
* -1 (ERROR) and sets errno appropriately.
* This is a NuttX internal function so it follows the convention that
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
@ -267,8 +267,8 @@ int exec_module(FAR const struct binary_s *bin);
* the parent of the newly created task to automatically unload the
* module when the task exits. This assumes that (1) the caller is the
* parent of the created task, (2) that bin was allocated with kmm_malloc()
* or friends. It will also automatically free the structure with kmm_free()
* after unloading the module.
* or friends. It will also automatically free the structure with
* kmm_free() after unloading the module.
*
* Input Parameter:
* pid - The task ID of the child task
@ -276,9 +276,9 @@ int exec_module(FAR const struct binary_s *bin);
* persist until the task unloads
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* It returns 0 (OK) if the callback was successfully scheduled. On
* failure, it returns -1 (ERROR) and sets errno appropriately.
* This is a NuttX internal function so it follows the convention that
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/