mirror of
https://github.com/apache/nuttx.git
synced 2026-06-05 07:12:54 +08:00
Add an implementation of rmmod
This commit is contained in:
@@ -11237,4 +11237,5 @@
|
|||||||
just the ELF module support with name changes (2015-12-10).
|
just the ELF module support with name changes (2015-12-10).
|
||||||
* configs/samv71-xult/module: Add configuration for testing OS
|
* configs/samv71-xult/module: Add configuration for testing OS
|
||||||
modules (2015-12-12).
|
modules (2015-12-12).
|
||||||
|
* sched/module: Add an implementation of rmmod() (2015-11-12).
|
||||||
|
|
||||||
|
|||||||
+37
-8
@@ -76,25 +76,43 @@
|
|||||||
* Public Types
|
* Public Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This is the type of the function that is called to uninitialize the
|
||||||
|
* the loaded module. This may mean, for example, un-registering a device
|
||||||
|
* driver. If the module is successfully initialized, its memory will be
|
||||||
|
* deallocated.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* arg - An opaque argument that was previously returned by the initializer
|
||||||
|
* function.
|
||||||
|
*
|
||||||
|
* Returned Value:
|
||||||
|
* Zero (OK) on success; a negated errno value on any failure to
|
||||||
|
* initialize the module. If zero is returned, then the module memory
|
||||||
|
* will be deallocated. If the module is still in use (for example with
|
||||||
|
* open driver instances), the uninitialization function should fail with
|
||||||
|
* -EBUSY
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef CODE int (*mod_uninitializer_t)(FAR void *arg);
|
||||||
|
|
||||||
/* A NuttX module is expected to export a function called module_initialize()
|
/* A NuttX module is expected to export a function called module_initialize()
|
||||||
* that has the following function prototype. This function should appear as
|
* that has the following function prototype. This function should appear as
|
||||||
* the entry point in the ELF module file and will be called bythe binfmt
|
* the entry point in the ELF module file and will be called bythe binfmt
|
||||||
* logic after the module has been loaded into kernel memory.
|
* logic after the module has been loaded into kernel memory.
|
||||||
*
|
*
|
||||||
* As an alternative using GCC, the module may mark a function with the
|
|
||||||
* "constructor" attribute and the module initializer will be called along
|
|
||||||
* with any other C++ constructors. The "destructor" attribute may also
|
|
||||||
* be used to mark an module uninitialization function.
|
|
||||||
*
|
|
||||||
* Input Parameters:
|
* Input Parameters:
|
||||||
* None
|
* uninitializer - The pointer to the uninitialization function. NULL may
|
||||||
|
* be returned if no uninitialization is needed (i.e, the the module
|
||||||
|
* memory can be deallocated at any time).
|
||||||
|
* arg - An argument that will be passed to the uninitialization function.
|
||||||
*
|
*
|
||||||
* Returned Value:
|
* Returned Value:
|
||||||
* Zero (OK) on success; a negated errno value on any failure to
|
* Zero (OK) on success; a negated errno value on any failure to
|
||||||
* initialize the module.
|
* initialize the module.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
typedef CODE int (*mod_initializer_t)(void);
|
typedef CODE int (*mod_initializer_t)(mod_uninitializer_t *uninitializer,
|
||||||
|
FAR void **arg);
|
||||||
|
|
||||||
/* This describes the file to be loaded. */
|
/* This describes the file to be loaded. */
|
||||||
|
|
||||||
@@ -111,7 +129,8 @@ struct module_s
|
|||||||
* resources used by the loaded module.
|
* resources used by the loaded module.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mod_initializer_t initializer; /* Module initializer function */
|
mod_uninitializer_t uninitializer; /* Module initializer function */
|
||||||
|
FAR void *arg; /* Uninitializer argument */
|
||||||
FAR void *alloc; /* Allocated kernel memory */
|
FAR void *alloc; /* Allocated kernel memory */
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -139,6 +158,16 @@ extern "C"
|
|||||||
|
|
||||||
int insmod(FAR struct module_s *modp);
|
int insmod(FAR struct module_s *modp);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: rmmod
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Remove a previously installed module from memory.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int rmmod(FAR struct module_s *modp);
|
||||||
|
|
||||||
#undef EXTERN
|
#undef EXTERN
|
||||||
#if defined(__cplusplus)
|
#if defined(__cplusplus)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ ifeq ($(CONFIG_MODULE),y)
|
|||||||
|
|
||||||
# OS module interfaces
|
# OS module interfaces
|
||||||
|
|
||||||
CSRCS += mod_insmod.c
|
CSRCS += mod_insmod.c mod_rmmod.c
|
||||||
|
|
||||||
# loadable module library
|
# loadable module library
|
||||||
|
|
||||||
|
|||||||
+12
-15
@@ -1,5 +1,5 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* sched/module/module.c
|
* sched/module/mod_insmod.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
@@ -46,8 +46,6 @@
|
|||||||
#include <debug.h>
|
#include <debug.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
|
|
||||||
#include <nuttx/arch.h>
|
#include <nuttx/arch.h>
|
||||||
#include <nuttx/module.h>
|
#include <nuttx/module.h>
|
||||||
|
|
||||||
@@ -172,8 +170,10 @@ static void mod_dumpinitializer(mod_initializer_t initializer,
|
|||||||
int insmod(FAR struct module_s *modp)
|
int insmod(FAR struct module_s *modp)
|
||||||
{
|
{
|
||||||
struct mod_loadinfo_s loadinfo;
|
struct mod_loadinfo_s loadinfo;
|
||||||
|
mod_initializer_t initializer;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
DEBUGASSERT(modp != NULL && modp->filename != NULL);
|
||||||
bvdbg("Loading file: %s\n", modp->filename);
|
bvdbg("Loading file: %s\n", modp->filename);
|
||||||
|
|
||||||
/* Initialize the ELF library to load the program binary. */
|
/* Initialize the ELF library to load the program binary. */
|
||||||
@@ -207,23 +207,20 @@ int insmod(FAR struct module_s *modp)
|
|||||||
|
|
||||||
/* Return the load information */
|
/* Return the load information */
|
||||||
|
|
||||||
modp->initializer = (mod_initializer_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
|
|
||||||
modp->alloc = (FAR void *)loadinfo.textalloc;
|
modp->alloc = (FAR void *)loadinfo.textalloc;
|
||||||
|
|
||||||
/* Get the module initializer entry point */
|
/* Get the module initializer entry point */
|
||||||
|
|
||||||
if (modp->initializer)
|
initializer = (mod_initializer_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
|
||||||
|
mod_dumpinitializer(initializer, &loadinfo);
|
||||||
|
|
||||||
|
/* Call the module initializer */
|
||||||
|
|
||||||
|
ret = initializer(&modp->uninitializer, &modp->arg);
|
||||||
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
mod_dumpinitializer(modp->initializer, &loadinfo);
|
bdbg("Failed to initialize the module: %d\n", ret);
|
||||||
|
goto errout_with_load;
|
||||||
/* Call the module initializer */
|
|
||||||
|
|
||||||
ret = modp->initializer();
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
bdbg("Failed to initialize the module: %d\n", ret);
|
|
||||||
goto errout_with_load;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mod_uninitialize(&loadinfo);
|
mod_uninitialize(&loadinfo);
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* sched/module/module.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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 <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <nuttx/kmalloc.h>
|
||||||
|
#include <nuttx/module.h>
|
||||||
|
|
||||||
|
#include "module/module.h"
|
||||||
|
|
||||||
|
#ifdef CONFIG_MODULE
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: rmmod
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Remove a previously installed module from memory.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int rmmod(FAR struct module_s *modp)
|
||||||
|
{
|
||||||
|
int ret = OK;
|
||||||
|
|
||||||
|
DEBUGASSERT(modp != NULL);
|
||||||
|
|
||||||
|
/* Is there an uninitializer? */
|
||||||
|
|
||||||
|
if (modp->uninitializer != NULL)
|
||||||
|
{
|
||||||
|
/* Try to uninitializer the module */
|
||||||
|
|
||||||
|
ret = modp->uninitializer(modp->arg);
|
||||||
|
|
||||||
|
/* Did the module sucessfully uninitialize? */
|
||||||
|
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
sdbg("ERROR: Failed to uninitialize the module: %d\n", ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Free the module memory */
|
||||||
|
|
||||||
|
/* Release memory holding the relocated ELF image */
|
||||||
|
|
||||||
|
if (modp->alloc != 0)
|
||||||
|
{
|
||||||
|
kmm_free((FAR void *)modp->alloc);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_MODULE */
|
||||||
Reference in New Issue
Block a user