Modules: Move first five of many C files from sched/module to libc/modlib

This commit is contained in:
Gregory Nutt
2017-01-29 10:05:15 -06:00
parent 00e46b5966
commit 5e94dd22bb
22 changed files with 241 additions and 250 deletions
+4
View File
@@ -7,6 +7,10 @@ config LIBC_MODLIB
bool
default n
config MODLIB_NAMES
bool
default n
menu "Module library configuration"
depends on LIBC_MODLIB
+3
View File
@@ -37,6 +37,9 @@ ifeq ($(CONFIG_LIBC_MODLIB),y)
# Add the nuttx/lib/modlib.h files to the build
CSRCS += modlib_bind.c modlib_depend.c modlib_init.c modlib_registry.c
CSRCS += modlib_uninit.c
# Add the modlib directory to the build
DEPPATH += --dep-path modlib
+304
View File
@@ -0,0 +1,304 @@
/****************************************************************************
* libc/modlib/modlib_bind.c
*
* Copyright (C) 2015, 2017 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 <stdint.h>
#include <string.h>
#include <elf32.h>
#include <errno.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
#include <nuttx/binfmt/symtab.h>
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: modlib_readrel
*
* Description:
* Read the ELF32_Rel structure into memory.
*
****************************************************************************/
static inline int modlib_readrel(FAR struct mod_loadinfo_s *loadinfo,
FAR const Elf32_Shdr *relsec,
int index, FAR Elf32_Rel *rel)
{
off_t offset;
/* Verify that the symbol table index lies within symbol table */
if (index < 0 || index > (relsec->sh_size / sizeof(Elf32_Rel)))
{
serr("ERROR: Bad relocation symbol index: %d\n", index);
return -EINVAL;
}
/* Get the file offset to the symbol table entry */
offset = relsec->sh_offset + sizeof(Elf32_Rel) * index;
/* And, finally, read the symbol table entry into memory */
return mod_read(loadinfo, (FAR uint8_t *)rel, sizeof(Elf32_Rel), offset);
}
/****************************************************************************
* Name: modlib_relocate and modlib_relocateadd
*
* Description:
* Perform all relocations associated with a section.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
static int modlib_relocate(FAR struct module_s *modp,
FAR struct mod_loadinfo_s *loadinfo, int relidx)
{
FAR Elf32_Shdr *relsec = &loadinfo->shdr[relidx];
FAR Elf32_Shdr *dstsec = &loadinfo->shdr[relsec->sh_info];
Elf32_Rel rel;
Elf32_Sym sym;
FAR Elf32_Sym *psym;
uintptr_t addr;
int symidx;
int ret;
int i;
/* Examine each relocation in the section. 'relsec' is the section
* containing the relations. 'dstsec' is the section containing the data
* to be relocated.
*/
for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++)
{
psym = &sym;
/* Read the relocation entry into memory */
ret = modlib_readrel(loadinfo, relsec, i, &rel);
if (ret < 0)
{
serr("ERROR: Section %d reloc %d: Failed to read relocation entry: %d\n",
relidx, i, ret);
return ret;
}
/* Get the symbol table index for the relocation. This is contained
* in a bit-field within the r_info element.
*/
symidx = ELF32_R_SYM(rel.r_info);
/* Read the symbol table entry into memory */
ret = mod_readsym(loadinfo, symidx, &sym);
if (ret < 0)
{
serr("ERROR: Section %d reloc %d: Failed to read symbol[%d]: %d\n",
relidx, i, symidx, ret);
return ret;
}
/* Get the value of the symbol (in sym.st_value) */
ret = mod_symvalue(modp, loadinfo, &sym);
if (ret < 0)
{
/* The special error -ESRCH is returned only in one condition: The
* symbol has no name.
*
* There are a few relocations for a few architectures that do
* no depend upon a named symbol. We don't know if that is the
* case here, but we will use a NULL symbol pointer to indicate
* that case to up_relocate(). That function can then do what
* is best.
*/
if (ret == -ESRCH)
{
serr("ERROR: Section %d reloc %d: Undefined symbol[%d] has no name: %d\n",
relidx, i, symidx, ret);
psym = NULL;
}
else
{
serr("ERROR: Section %d reloc %d: Failed to get value of symbol[%d]: %d\n",
relidx, i, symidx, ret);
return ret;
}
}
/* Calculate the relocation address. */
if (rel.r_offset < 0 || rel.r_offset > dstsec->sh_size - sizeof(uint32_t))
{
serr("ERROR: Section %d reloc %d: Relocation address out of range, offset %d size %d\n",
relidx, i, rel.r_offset, dstsec->sh_size);
return -EINVAL;
}
addr = dstsec->sh_addr + rel.r_offset;
/* Now perform the architecture-specific relocation */
ret = up_relocate(&rel, psym, addr);
if (ret < 0)
{
serr("ERROR: Section %d reloc %d: Relocation failed: %d\n", relidx, i, ret);
return ret;
}
}
return OK;
}
static int modlib_relocateadd(FAR struct module_s *modp,
FAR struct mod_loadinfo_s *loadinfo, int relidx)
{
serr("ERROR: Not implemented\n");
return -ENOSYS;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: modlib_bind
*
* Description:
* Bind the imported symbol names in the loaded module described by
* 'loadinfo' using the exported symbol values provided by mod_setsymtab().
*
* Input Parameters:
* modp - Module state information
* loadinfo - Load state information
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int modlib_bind(FAR struct module_s *modp, FAR struct mod_loadinfo_s *loadinfo)
{
int ret;
int i;
/* Find the symbol and string tables */
ret = mod_findsymtab(loadinfo);
if (ret < 0)
{
return ret;
}
/* Allocate an I/O buffer. This buffer is used by mod_symname() to
* accumulate the variable length symbol name.
*/
ret = mod_allocbuffer(loadinfo);
if (ret < 0)
{
serr("ERROR: mod_allocbuffer failed: %d\n", ret);
return -ENOMEM;
}
/* Process relocations in every allocated section */
for (i = 1; i < loadinfo->ehdr.e_shnum; i++)
{
/* Get the index to the relocation section */
int infosec = loadinfo->shdr[i].sh_info;
if (infosec >= loadinfo->ehdr.e_shnum)
{
continue;
}
/* Make sure that the section is allocated. We can't relocate
* sections that were not loaded into memory.
*/
if ((loadinfo->shdr[infosec].sh_flags & SHF_ALLOC) == 0)
{
continue;
}
/* Process the relocations by type */
if (loadinfo->shdr[i].sh_type == SHT_REL)
{
ret = modlib_relocate(modp, loadinfo, i);
}
else if (loadinfo->shdr[i].sh_type == SHT_RELA)
{
ret = modlib_relocateadd(modp, loadinfo, i);
}
if (ret < 0)
{
break;
}
}
#if defined(CONFIG_ARCH_HAVE_COHERENT_DCACHE)
/* Ensure that the I and D caches are coherent before starting the newly
* loaded module by cleaning the D cache (i.e., flushing the D cache
* contents to memory and invalidating the I cache).
*/
up_coherent_dcache(loadinfo->textalloc, loadinfo->textsize);
up_coherent_dcache(loadinfo->datastart, loadinfo->datasize);
#endif
return ret;
}
+198
View File
@@ -0,0 +1,198 @@
/****************************************************************************
* libc/modlib/modlib_depend.c
*
* Copyright (C) 2017 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 <stdlib.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: modlib_depend
*
* Description:
* Set up module dependencies between the exporter and the importer of a
* symbol. The happens when the module is installed via insmod and a
* symbol is imported from another module.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
* Assumptions:
* Caller holds the registry lock.
*
****************************************************************************/
int modlib_depend(FAR struct module_s *importer, FAR struct module_s *exporter)
{
#if CONFIG_MODLIB_MAXDEPEND > 0
int freendx;
int i;
DEBUGASSERT(importer != NULL && exporter != NULL);
/* First checker if the exported is already in our list if dependencies.
* This would happen if we are importing multiple symbols from the same
* exporting module. In that case, the module would already be in the
* list of dependencies.
*
* The list dependency list is a a dumb, upacked array of pointers. This
* should not be too inefficient if the number of CONFIG_MODLIB_MAXDEPEND
* is small. Otherwise, a more dynamic data structure would be in order.
*/
for (i = 0, freendx = -1; i < CONFIG_MODLIB_MAXDEPEND; i++)
{
FAR const struct module_s *modp;
/* Check if this dependency slot is available. */
modp = importer->dependencies[i];
if (modp == NULL)
{
/* Remember this slot for use the module is NOT already in the
* list of dependencies.
*/
freendx = i;
}
else if (modp == exporter)
{
/* Yes, we are already importing symbols from this module. Nothing
* more needs to be done.
*/
return OK;
}
}
/* If we get here, then (1) this is a new exporting module that does not
* already appear in the list of dependencies, and (2) freendx is the
* index to the last free slot in the dependency list. If freendx is
* negative, then the dependency list is full.
*/
if (freendx >= 0)
{
/* Increment the count of dependencies on the exporter module */
DEBUGASSERT(exporter->dependents < UINT8_MAX);
if (exporter->dependents >= UINT8_MAX)
{
return -ENOSPC;
}
exporter->dependents++;
/* And remember the exporter so that we can decrement the count of
* dependents if the importer is removed.
*/
DEBUGASSERT(importer->dependencies[freendx] == NULL);
importer->dependencies[freendx] = exporter;
return OK;
}
/* If we get there then the list of dependencies is full. */
DEBUGPANIC();
return -ENFILE;
#else
return OK;
#endif
}
/****************************************************************************
* Name: modlib_undepend
*
* Description:
* Tear down module dependencies between the exporters and the importer of
* symbols. This happens when the module is removed via rmmod (and on
* error handling cases in insmod).
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
* Assumptions:
* Caller holds the registry lock.
*
****************************************************************************/
int modlib_undepend(FAR struct module_s *importer)
{
#if CONFIG_MODLIB_MAXDEPEND > 0
FAR struct module_s *exporter;
int i;
DEBUGASSERT(importer != NULL && importer->dependents == 0);
/* Decrement the dependency count on each of exporters of symbols used by
* this importer module. This is an upacked array of pointers. This
* should not be too inefficient if the number of CONFIG_MODLIB_MAXDEPEND
* is small. Otherwise, a more dynamic data structure would be in order.
*/
for (i = 0; i < CONFIG_MODLIB_MAXDEPEND; i++)
{
exporter = importer->dependencies[i];
if (exporter != NULL)
{
DEBUGASSERT(exporter->dependents > 0);
if (exporter->dependents > 0)
{
exporter->dependents--;
}
importer->dependencies[i] = NULL;
}
}
#endif
return OK;
}
+199
View File
@@ -0,0 +1,199 @@
/****************************************************************************
* libc/modlib/modlib_init.c
*
* Copyright (C) 2015, 2017 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/stat.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <elf32.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_MODLIB_DUMPBUFFER
* have to be defined or CONFIG_MODLIB_DUMPBUFFER does nothing.
*/
#if !defined(CONFIG_DEBUG_INFO) || !defined (CONFIG_MODLIB_DUMPBUFFER)
# undef CONFIG_MODLIB_DUMPBUFFER
#endif
#ifdef CONFIG_MODLIB_DUMPBUFFER
# define mod_dumpbuffer(m,b,n) sinfodumpbuffer(m,b,n)
#else
# define mod_dumpbuffer(m,b,n)
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: mod_filelen
*
* Description:
* Get the size of the ELF file
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
static inline int mod_filelen(FAR struct mod_loadinfo_s *loadinfo,
FAR const char *filename)
{
struct stat buf;
int ret;
/* Get the file stats */
ret = stat(filename, &buf);
if (ret < 0)
{
int errval = errno;
serr("ERROR: Failed to stat file: %d\n", errval);
return -errval;
}
/* Verify that it is a regular file */
if (!S_ISREG(buf.st_mode))
{
serr("ERROR: Not a regular file. mode: %d\n", buf.st_mode);
return -ENOENT;
}
/* TODO: Verify that the file is readable. Not really important because
* we will detect this when we try to open the file read-only.
*/
/* Return the size of the file in the loadinfo structure */
loadinfo->filelen = buf.st_size;
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: modlib_initialize
*
* Description:
* This function is called to configure the library to process an ELF
* program binary.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int modlib_initialize(FAR const char *filename,
FAR struct mod_loadinfo_s *loadinfo)
{
int ret;
sinfo("filename: %s loadinfo: %p\n", filename, loadinfo);
/* Clear the load info structure */
memset(loadinfo, 0, sizeof(struct mod_loadinfo_s));
/* Get the length of the file. */
ret = mod_filelen(loadinfo, filename);
if (ret < 0)
{
serr("ERROR: mod_filelen failed: %d\n", ret);
return ret;
}
/* Open the binary file for reading (only) */
loadinfo->filfd = open(filename, O_RDONLY);
if (loadinfo->filfd < 0)
{
int errval = errno;
serr("ERROR: Failed to open ELF binary %s: %d\n", filename, errval);
return -errval;
}
/* Read the ELF ehdr from offset 0 */
ret = mod_read(loadinfo, (FAR uint8_t *)&loadinfo->ehdr,
sizeof(Elf32_Ehdr), 0);
if (ret < 0)
{
serr("ERROR: Failed to read ELF header: %d\n", ret);
return ret;
}
mod_dumpbuffer("ELF header", (FAR const uint8_t *)&loadinfo->ehdr,
sizeof(Elf32_Ehdr));
/* Verify the ELF header */
ret = mod_verifyheader(&loadinfo->ehdr);
if (ret < 0)
{
/* This may not be an error because we will be called to attempt loading
* EVERY binary. If mod_verifyheader() does not recognize the ELF header,
* it will -ENOEXEC whcih simply informs the system that the file is not an
* ELF file. mod_verifyheader() will return other errors if the ELF header
* is not correctly formed.
*/
serr("ERROR: Bad ELF header: %d\n", ret);
return ret;
}
return OK;
}
+345
View File
@@ -0,0 +1,345 @@
/****************************************************************************
* libc/modlib/modlib_registry.c
*
* Copyright (C) 2015, 2017 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 <string.h>
#include <semaphore.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NO_HOLDER ((pid_t)-1)
/****************************************************************************
* Private Types
****************************************************************************/
struct mod_registrylock_s
{
sem_t lock; /* The actual registry lock */
pid_t holder; /* The PID of the current holder of the lock */
int16_t count; /* The number of nested calls to modlib_registry_lock */
};
/****************************************************************************
* Private Data
****************************************************************************/
static struct mod_registrylock_s g_modlock =
{
SEM_INITIALIZER(1), /* lock */
0, /* pid */
0 /* count */
};
static FAR struct module_s *g_mod_registry;
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: modlib_registry_lock
*
* Description:
* Get exclusive access to the module registry.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
void modlib_registry_lock(void)
{
pid_t me;
/* Do we already hold the semaphore? */
me = getpid();
if (me == g_modlock.holder)
{
/* Yes... just increment the count */
g_modlock.count++;
DEBUGASSERT(g_modlock.count > 0);
}
/* Take the semaphore (perhaps waiting) */
else
{
while (sem_wait(&g_modlock.lock) != 0)
{
/* The only case that an error should occr here is if
* the wait was awakened by a signal.
*/
ASSERT(get_errno() == EINTR);
}
/* No we hold the semaphore */
g_modlock.holder = me;
g_modlock.count = 1;
}
}
/****************************************************************************
* Name: modlib_registry_unlock
*
* Description:
* Relinquish the lock on the module registry
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/
void modlib_registry_unlock(void)
{
DEBUGASSERT(g_modlock.holder == getpid());
/* Is this our last count on the semaphore? */
if (g_modlock.count > 1)
{
/* No.. just decrement the count */
g_modlock.count--;
}
/* Yes.. then we can really release the semaphore */
else
{
g_modlock.holder = NO_HOLDER;
g_modlock.count = 0;
sem_post(&g_modlock.lock);
}
}
/****************************************************************************
* Name: modlib_registry_add
*
* Description:
* Add a new entry to the module registry.
*
* Input Parameters:
* modp - The module data structure to be registered.
*
* Returned Value:
* None
*
* Assumptions:
* The caller holds the lock on the module registry.
*
****************************************************************************/
void modlib_registry_add(FAR struct module_s *modp)
{
DEBUGASSERT(modp);
modp->flink = g_mod_registry;
g_mod_registry = modp;
}
/****************************************************************************
* Name: modlib_registry_del
*
* Description:
* Remove a module entry from the registry
*
* Input Parameters:
* modp - The registry entry to be removed.
*
* Returned Value:
* Zero (OK) is returned if the registry entry was deleted. Otherwise,
* a negated errno value is returned.
*
* Assumptions:
* The caller holds the lock on the module registry.
*
****************************************************************************/
int modlib_registry_del(FAR struct module_s *modp)
{
FAR struct module_s *prev;
FAR struct module_s *curr;
for (prev = NULL, curr = g_mod_registry;
curr != NULL && curr != modp;
prev = curr, curr = curr->flink);
if (curr == NULL)
{
serr("ERROR: Could not find module entry\n");
return -ENOENT;
}
if (prev == NULL)
{
g_mod_registry = modp->flink;
}
else
{
prev->flink = modp->flink;
}
modp->flink = NULL;
return OK;
}
/****************************************************************************
* Name: modlib_registry_find
*
* Description:
* Find an entry in the module registry using the name of the module.
*
* Input Parameters:
* modulename - The name of the module to be found
*
* Returned Value:
* If the registry entry is found, a pointer to the module entry is
* returned. NULL is returned if the they entry is not found.
*
* Assumptions:
* The caller holds the lock on the module registry.
*
****************************************************************************/
FAR struct module_s *modlib_registry_find(FAR const char *modulename)
{
FAR struct module_s *modp;
for (modp = g_mod_registry;
modp != NULL && strncmp(modp->modulename, modulename, MODULENAME_MAX) != 0;
modp = modp->flink);
return modp;
}
/****************************************************************************
* Name: modlib_registry_verify
*
* Description:
* Verify that a module handle is valid by traversing the module list and
* assuring that the module still resides in the list. If it does not,
* the handle is probably a stale pointer.
*
* Input Parameters:
* modp - The registry entry to be verified.
*
* Returned Value:
* Returns OK is the module is valid; -ENOENT otherwise.
*
* Assumptions:
* The caller holds the lock on the module registry.
*
****************************************************************************/
int modlib_registry_verify(FAR struct module_s *modp)
{
FAR struct module_s *node;
for (node = g_mod_registry; node != NULL; node = node->flink)
{
if (node == modp)
{
return OK;
}
}
return -ENOENT;
}
/****************************************************************************
* Name: modlib_registry_foreach
*
* Description:
* Visit each module in the registry
*
* Input Parameters:
* callback - This callback function was be called for each entry in the
* registry.
* arg - This opaque argument will be passed to the callback function.
*
* Returned Value:
* This function normally returns zero (OK). If, however, any callback
* function returns a non-zero value, the traversal will be terminated and
* that non-zero value will be returned.
*
****************************************************************************/
int modlib_registry_foreach(mod_callback_t callback, FAR void *arg)
{
FAR struct module_s *modp;
int ret = OK;
/* Get exclusive access to the module registry */
modlib_registry_lock();
/* Visit each installed module */
for (modp = g_mod_registry; modp != NULL; modp = modp->flink)
{
/* Perform the callback */
ret = callback(modp, arg);
if (ret != 0)
{
break;
}
}
modlib_registry_unlock();
return ret;
}
+115
View File
@@ -0,0 +1,115 @@
/****************************************************************************
* libc/modlib/modlib_uninit.c
*
* Copyright (C) 2015. 2017 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 <unistd.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/kmalloc.h>
#include <nuttx/module.h>
#include <nuttx/lib/modlib.h>
#include "libc.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: modlib_uninitialize
*
* Description:
* Releases any resources committed by modlib_initialize(). This
* essentially undoes the actions of modlib_initialize.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int modlib_uninitialize(struct mod_loadinfo_s *loadinfo)
{
/* Free all working buffers */
mod_freebuffers(loadinfo);
/* Close the ELF file */
if (loadinfo->filfd >= 0)
{
close(loadinfo->filfd);
}
return OK;
}
/****************************************************************************
* Name: mod_freebuffers
*
* Description:
* Release all working buffers.
*
* Returned Value:
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int mod_freebuffers(struct mod_loadinfo_s *loadinfo)
{
/* Release all working allocations */
if (loadinfo->shdr != NULL)
{
lib_free((FAR void *)loadinfo->shdr);
loadinfo->shdr = NULL;
}
if (loadinfo->iobuffer != NULL)
{
lib_free((FAR void *)loadinfo->iobuffer);
loadinfo->iobuffer = NULL;
loadinfo->buflen = 0;
}
return OK;
}