From 4a1221f35d1ee49301abbd67a5f66413a9d09fc5 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 17 Jun 2009 20:25:27 +0000 Subject: [PATCH] Add exec_module git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1895 42af7a65-404d-4744-a932-0658087f49c3 --- ChangeLog | 6 ++ Documentation/NuttX.html | 7 +- binfmt/Makefile | 5 +- binfmt/binfmt_dumpmodule.c | 5 + binfmt/binfmt_execmodule.c | 184 +++++++++++++++++++++++++++++++++++ binfmt/binfmt_loadmodule.c | 24 +++-- binfmt/binfmt_register.c | 13 ++- binfmt/binfmt_unloadmodule.c | 13 ++- binfmt/binfmt_unregister.c | 14 ++- binfmt/nxflat.c | 12 +++ include/nuttx/binfmt.h | 68 +++++++++++-- include/nuttx/nxflat.h | 127 ++++++++++++++++++++---- sched/task_create.c | 2 +- sched/task_init.c | 92 +++++++++++------- 14 files changed, 490 insertions(+), 82 deletions(-) create mode 100644 binfmt/binfmt_execmodule.c diff --git a/ChangeLog b/ChangeLog index 488f31f3e3..ab8724cc53 100644 --- a/ChangeLog +++ b/ChangeLog @@ -784,5 +784,11 @@ CONFIG_FDCLONE_DISABLE, CONFIG_FDCLONE_STDIO, CONFIG_SDCLONE_DISABLE. * Use of C++ reserved word 'private' in C header files causes problems for C++ that include them. + * Added 'binfmt' support to allow execution of programs in a file system, + binding to NuttX symbols. A custom format call NXFLAT is used; this + derives from http://xflat.sourceforge.net. At present is supports on + XIP execution from ROMFS file systems. Initial check-in is untested + and probably breaks many builds. + diff --git a/Documentation/NuttX.html b/Documentation/NuttX.html index 1343756eb1..68b3d4d814 100644 --- a/Documentation/NuttX.html +++ b/Documentation/NuttX.html @@ -8,7 +8,7 @@

NuttX RTOS

-

Last Updated: June 16, 2009

+

Last Updated: June 17, 2009

@@ -1476,6 +1476,11 @@ nuttx-0.4.9 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> via task_create(). * Use of C++ reserved word 'private' in C header files causes problems for C++ that include them. + * Added 'binfmt' support to allow execution of programs in a file system, + binding to NuttX symbols. A custom format call NXFLAT is used; this + derives from http://xflat.sourceforge.net. At present is supports on + XIP execution from ROMFS file systems. Initial check-in is untested + and probably breaks many builds. pascal-0.1.3 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> diff --git a/binfmt/Makefile b/binfmt/Makefile index 4b3905e067..015981d4e2 100644 --- a/binfmt/Makefile +++ b/binfmt/Makefile @@ -35,6 +35,8 @@ -include $(TOPDIR)/Make.defs +CFLAGS += -I$(TOPDIR)/sched + ifeq ($(CONFIG_NXFLAT),y) include libnxflat/Make.defs LIBNXFLAT_CSRCS += nxflat.c @@ -42,7 +44,8 @@ endif BINFMT_ASRCS = BINFMT_CSRCS = binfmt_globals.c binfmt_register.c binfmt_unregister.c \ - binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_dumpmodule.c + binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_execmodule.c \ + binfmt_dumpmodule.c SUBDIRS = libnxflat diff --git a/binfmt/binfmt_dumpmodule.c b/binfmt/binfmt_dumpmodule.c index 5b7bd1fe9f..190889d64e 100644 --- a/binfmt/binfmt_dumpmodule.c +++ b/binfmt/binfmt_dumpmodule.c @@ -76,6 +76,11 @@ * Description: * Load a module into memory and prep it for execution. * + * Returned Value: + * 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. + * ***********************************************************************/ int dump_module(FAR const struct binary_s *bin) diff --git a/binfmt/binfmt_execmodule.c b/binfmt/binfmt_execmodule.c new file mode 100644 index 0000000000..21c1bf6ac5 --- /dev/null +++ b/binfmt/binfmt_execmodule.c @@ -0,0 +1,184 @@ +/**************************************************************************** + * binfmt/binfmt_execmodule.c + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include "os_internal.h" +#include "binfmt_internal.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: exec_module + * + * Description: + * 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. + * + ****************************************************************************/ + +int exec_module(FAR const struct binary_s *bin, int priority) +{ + FAR _TCB *tcb; +#ifndef CONFIG_CUSTOM_STACK + FAR uint32 *stack; +#endif + pid_t pid; + int err; + int ret; + + /* Sanity checking */ + +#ifdef CONFIG_DEBUG + if (!bin || !bin->ispace || !bin->entrypt || bin->stacksize <= 0) + { + err = EINVAL; + goto errout; + } +#endif + + bdbg("Executing %s\n", bin->filename); + + /* Allocate a TCB for the new task. */ + + tcb = (FAR _TCB*)zalloc(sizeof(_TCB)); + if (!tcb) + { + err = ENOMEM; + goto errout; + } + + /* Allocate the stack for the new task */ + +#ifndef CONFIG_CUSTOM_STACK + stack = (FAR uint32*)malloc(bin->stacksize); + if (!tcb) + { + err = ENOMEM; + goto errout_with_tcb; + } + + /* Initialize the task */ + + ret = task_init(tcb, bin->filename, priority, stack, bin->stacksize, bin->entrypt, bin->argv); +#else + /* Initialize the task */ + + ret = task_init(tcb, bin->filename, priority, stack, bin->entrypt, bin->argv); +#endif + if (ret < 0) + { + err = errno; + dbg("task_init() failed: %d\n", err); + goto errout_with_stack; + } + + /* Add the DSpace address as the PIC base address */ + + tcb->picbase = bin->dspace; + + /* Re-initialize the task's initial state to account for the new PIC base */ + + up_initial_state(tcb); + + /* Get the assigned pid before we start the task */ + + pid = tcb->pid; + + /* Then activate the task at the provided priority */ + + ret = task_activate(tcb); + if (ret < 0) + { + err = errno; + dbg("task_activate() failed: %d\n", err); + goto errout_with_stack; + } + return (int)pid; + +errout_with_stack: +#ifndef CONFIG_CUSTOM_STACK + tcb->stack_alloc_ptr = NULL; + sched_releasetcb(tcb); + free(stack); +#else + sched_releasetcb(tcb); +#endif + goto errout; + +errout_with_tcb: + free(tcb); +errout: + errno = err; + dbg("returning errno: %d\n", err); + return ERROR; +} + + diff --git a/binfmt/binfmt_loadmodule.c b/binfmt/binfmt_loadmodule.c index 37910ae7c0..b73b6223eb 100644 --- a/binfmt/binfmt_loadmodule.c +++ b/binfmt/binfmt_loadmodule.c @@ -64,17 +64,22 @@ * Private Functions ****************************************************************************/ -/*********************************************************************** +/**************************************************************************** * Public Functions - ***********************************************************************/ + ****************************************************************************/ -/*********************************************************************** +/**************************************************************************** * Name: load_module * * Description: * Load a module into memory and prep it 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. + * + ****************************************************************************/ int load_module(const char *filename, FAR struct binary_s *bin) { @@ -120,8 +125,15 @@ int load_module(const char *filename, FAR struct binary_s *bin) sched_unlock(); } - if (ret < 0) bdbg("Returning %d\n", ret); - return ret; + /* This is an end-user function. Return failures via errno */ + + if (ret < 0) + { + bdbg("Returning errno %d\n", -ret); + errno = -ret; + return ERROR; + } + return OK; } diff --git a/binfmt/binfmt_register.c b/binfmt/binfmt_register.c index 309c081e69..2323062ec1 100644 --- a/binfmt/binfmt_register.c +++ b/binfmt/binfmt_register.c @@ -65,17 +65,22 @@ * Private Functions ****************************************************************************/ -/*********************************************************************** +/**************************************************************************** * Public Functions - ***********************************************************************/ + ****************************************************************************/ -/*********************************************************************** +/**************************************************************************** * Name: register_binfmt * * Description: * Register a loader for a binary format * - ***********************************************************************/ + * Returned Value: + * 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. + * + ****************************************************************************/ int register_binfmt(FAR struct binfmt_s *binfmt) { diff --git a/binfmt/binfmt_unloadmodule.c b/binfmt/binfmt_unloadmodule.c index f5d7ec92c3..ff745171aa 100644 --- a/binfmt/binfmt_unloadmodule.c +++ b/binfmt/binfmt_unloadmodule.c @@ -66,18 +66,23 @@ * Private Functions ****************************************************************************/ -/*********************************************************************** +/**************************************************************************** * Public Functions - ***********************************************************************/ + ****************************************************************************/ -/*********************************************************************** +/**************************************************************************** * Name: unload_module * * Description: * Unload a (non-executing) module from memory. If the module has * been started (via exec_module), calling this will be fatal. * - ***********************************************************************/ + * Returned Value: + * 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. + * + ****************************************************************************/ int unload_module(FAR const struct binary_s *bin) { diff --git a/binfmt/binfmt_unregister.c b/binfmt/binfmt_unregister.c index 1eedffcfc1..62c156a611 100644 --- a/binfmt/binfmt_unregister.c +++ b/binfmt/binfmt_unregister.c @@ -65,17 +65,22 @@ * Private Functions ****************************************************************************/ -/*********************************************************************** +/**************************************************************************** * Public Functions - ***********************************************************************/ + ****************************************************************************/ -/*********************************************************************** +/**************************************************************************** * Name: unregister_binfmt * * Description: * Register a loader for a binary format * - ***********************************************************************/ + * Returned Value: + * 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. + * + ****************************************************************************/ int unregister_binfmt(FAR struct binfmt_s *binfmt) { @@ -124,6 +129,7 @@ int unregister_binfmt(FAR struct binfmt_s *binfmt) sched_unlock(); } + return ret; } diff --git a/binfmt/nxflat.c b/binfmt/nxflat.c index 711be93535..94b15a32cc 100644 --- a/binfmt/nxflat.c +++ b/binfmt/nxflat.c @@ -197,6 +197,11 @@ static int nxflat_loadbinary(struct binary_s *binp) * use this binary format, this function must be called during system * format in order to register the NXFLAT binary format. * + * Returned Value: + * 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. + * ***********************************************************************/ int nxflat_initialize(void) @@ -216,6 +221,13 @@ int nxflat_initialize(void) /**************************************************************************** * Name: nxflat_uninitialize + * + * Description: + * Unregister the NXFLAT binary loader + * + * Returned Value: + * None + * ****************************************************************************/ void nxflat_uninitialize(void) diff --git a/include/nuttx/binfmt.h b/include/nuttx/binfmt.h index 4f9e0884a5..855cc3359f 100644 --- a/include/nuttx/binfmt.h +++ b/include/nuttx/binfmt.h @@ -90,25 +90,81 @@ extern "C" { #define EXTERN extern #endif -/* Register a binary format handler */ +/**************************************************************************** + * Name: register_binfmt + * + * Description: + * Register a loader for a binary format + * + * Returned Value: + * 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. + * + ****************************************************************************/ EXTERN int register_binfmt(FAR struct binfmt_s *binfmt); -/* Unregister a binary format handler */ +/**************************************************************************** + * Name: unregister_binfmt + * + * Description: + * Register a loader for a binary format + * + * Returned Value: + * 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. + * + ****************************************************************************/ EXTERN int unregister_binfmt(FAR struct binfmt_s *binfmt); -/* Load a module into memory */ +/**************************************************************************** + * Name: load_module + * + * Description: + * Load a module into memory and prep it 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. + * + ****************************************************************************/ EXTERN int load_module(const char *filename, FAR struct binary_s *bin); -/* Unload a (non-running) module from memory */ +/**************************************************************************** + * Name: unload_module + * + * Description: + * Unload a (non-executing) module from memory. If the module has + * been started (via exec_module), calling this will be fatal. + * + * Returned Value: + * 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. + * + ****************************************************************************/ EXTERN int unload_module(FAR const struct binary_s *bin); -/* Execute a module that has been loaded into memory */ +/**************************************************************************** + * Name: exec_module + * + * Description: + * 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. + * + ****************************************************************************/ -EXTERN int exec_module(FAR const struct binary_s *bin); +EXTERN int exec_module(FAR const struct binary_s *bin, int priority); #undef EXTERN #if defined(__cplusplus) diff --git a/include/nuttx/nxflat.h b/include/nuttx/nxflat.h index 17f6d9f039..f78c79dcd4 100644 --- a/include/nuttx/nxflat.h +++ b/include/nuttx/nxflat.h @@ -100,43 +100,134 @@ extern "C" { #define EXTERN extern #endif -/* Given the header from a possible NXFLAT executable, verify that it - * is an NXFLAT executable. - */ +/**************************************************************************** + * These are APIs exported by libnxflat (and may be used outside of NuttX): + ****************************************************************************/ + +/*********************************************************************** + * Name: + * + * Description: + * Given the header from a possible NXFLAT executable, verify that it + * is an NXFLAT executable. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ***********************************************************************/ EXTERN int nxflat_verifyheader(const struct nxflat_hdr_s *header); -/* This function is called to configure the library to process an NXFLAT - * program binary. - */ +/*********************************************************************** + * Name: + * + * Description: + * This function is called to configure the library to process an NXFLAT + * program binary. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ***********************************************************************/ -EXTERN int nxflat_init(const char *filename, - struct nxflat_hdr_s *header, +EXTERN int nxflat_init(const char *filename, struct nxflat_hdr_s *header, struct nxflat_loadinfo_s *loadinfo); -/* Releases any resources committed by nxflat_init(). This essentially - * undoes the actions of nxflat_init. - */ +/*********************************************************************** + * Name: + * + * Description: + * Releases any resources committed by nxflat_init(). This essentially + * undoes the actions of nxflat_init. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ***********************************************************************/ EXTERN int nxflat_uninit(struct nxflat_loadinfo_s *loadinfo); -/* Loads the binary specified by nxflat_init into memory, - * Completes all relocations, and clears BSS. - */ +/*********************************************************************** + * Name: + * + * Description: + * Loads the binary specified by nxflat_init into memory, + * Completes all relocations, and clears BSS. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ***********************************************************************/ EXTERN int nxflat_load(struct nxflat_loadinfo_s *loadinfo); -/* Read 'readsize' bytes from the object file at 'offset' */ +/*********************************************************************** + * Name: + * + * Description: + * Read 'readsize' bytes from the object file at 'offset' + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ***********************************************************************/ EXTERN int nxflat_read(struct nxflat_loadinfo_s *loadinfo, char *buffer, int readsize, int offset); -/* This function unloads the object from memory. This essentially - * undoes the actions of nxflat_load. - */ +/*********************************************************************** + * Name: + * + * Description: + * This function unloads the object from memory. This essentially + * undoes the actions of nxflat_load. + * + * Returned Value: + * 0 (OK) is returned on success and a negated errno is returned on + * failure. + * + ***********************************************************************/ EXTERN int nxflat_unload(struct nxflat_loadinfo_s *loadinfo); +/**************************************************************************** + * These are APIs used internally only by NuttX: + ****************************************************************************/ +/*********************************************************************** + * Name: nxflat_initialize + * + * Description: + * NXFLAT support is built unconditionally. However, it order to + * use this binary format, this function must be called during system + * format in order to register the NXFLAT binary format. + * + * Returned Value: + * 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. + * + ***********************************************************************/ + +EXTERN int nxflat_initialize(void); + +/**************************************************************************** + * Name: nxflat_uninitialize + * + * Description: + * Unregister the NXFLAT binary loader + * + * Returned Value: + * None + * + ****************************************************************************/ + +EXTERN void nxflat_uninitialize(void); + #undef EXTERN #if defined(__cplusplus) } diff --git a/sched/task_create.c b/sched/task_create.c index 31b50da0af..5d7a5872bc 100644 --- a/sched/task_create.c +++ b/sched/task_create.c @@ -1,7 +1,7 @@ /**************************************************************************** * task_create.c * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without diff --git a/sched/task_init.c b/sched/task_init.c index 0b4a3a54d1..6d53955943 100644 --- a/sched/task_init.c +++ b/sched/task_init.c @@ -1,7 +1,7 @@ -/************************************************************ +/**************************************************************************** * task_init.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 3. Neither the name NuttX nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * @@ -31,56 +31,60 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Included Files - ************************************************************/ + ****************************************************************************/ #include #include #include #include + #include "os_internal.h" +#include "env_internal.h" -/************************************************************ +/**************************************************************************** * Definitions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Type Declarations - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Global Variables - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Variables - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Function Prototypes - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Public Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Name: task_init * * Description: - * This function initializes a Task Control Block (TCB) - * in preparation for starting a new thread. It performs a - * subset of the functionality of task_create() + * This function initializes a Task Control Block (TCB) in preparation for + * starting a new thread. It performs a subset of the functionality of + * task_create() * - * Unlike task_create(), task_init() does not activate the - * task. This must be done by calling task_activate(). + * Unlike task_create(): + * 1. Allocate the TCB. The pre-allocated TCB is passed in the arguments. + * 2. Allocate the stack. The pre-allocated stack is passed in the arguments. + * 3. Activate the task. This must be done by calling task_activate(). * * Input Parameters: * tcb - Address of the new task's TCB @@ -89,19 +93,19 @@ * stack - Start of the pre-allocated stack * stack_size - Size (in bytes) of the stack allocated * entry - Application start point of the new task - * arg - A pointer to an array of input parameters. - * Up to CONFIG_MAX_TASK_ARG parameters may - * be provided. If fewer than CONFIG_MAX_TASK_ARG - * parameters are passed, the list should be - * terminated with a NULL argv[] value. - * If no parameters are required, argv may be - * NULL. + * arg - A pointer to an array of input parameters. Up to + * CONFIG_MAX_TASK_ARG parameters may be provided. If fewer + * than CONFIG_MAX_TASK_ARG parameters are passed, the list + * should be terminated with a NULL argv[] value. If no + * parameters are required, argv may be NULL. * * Return Value: - * OK on success; ERROR on failure. (See task_schedsetup() - * for possible failure conditions). + * OK on success; ERROR on failure. (See task_schedsetup() for possible + * failure conditions). On failure, the caller is responsible for freeing + * the stack memory and for calling sched_releasetcb() to free the TCB + * (which could be in most any state). * - ************************************************************/ + ****************************************************************************/ #ifndef CONFIG_CUSTOM_STACK STATUS task_init(FAR _TCB *tcb, const char *name, int priority, @@ -114,6 +118,19 @@ STATUS task_init(FAR _TCB *tcb, const char *name, int priority, { STATUS ret; + /* Associate file descriptors with the new task */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 || CONFIG_NSOCKET_DESCRIPTORS > 0 + if (sched_setuptaskfiles(tcb) != OK) + { + return ERROR; + } +#endif + + /* Clone the parent's task environment */ + + (void)env_dup(tcb); + /* Configure the user provided stack region */ #ifndef CONFIG_CUSTOM_STACK @@ -130,4 +147,5 @@ STATUS task_init(FAR _TCB *tcb, const char *name, int priority, (void)task_argsetup(tcb, name, argv); } return ret; - } +} +