2015-12-13 00:42:25 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/modlib/modlib_registry.c
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
2017-01-22 21:26:22 +01:00
|
|
|
* Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
|
2015-12-13 00:42:25 +01:00
|
|
|
* 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 <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2017-10-08 19:52:32 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-01-29 15:24:42 +01:00
|
|
|
#include <nuttx/lib/modlib.h>
|
2015-12-13 00:42:25 +01:00
|
|
|
|
2017-01-27 18:43:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|
2017-01-29 17:05:15 +01:00
|
|
|
int16_t count; /* The number of nested calls to modlib_registry_lock */
|
2017-01-27 18:43:27 +01:00
|
|
|
};
|
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-27 18:43:27 +01:00
|
|
|
static struct mod_registrylock_s g_modlock =
|
|
|
|
{
|
|
|
|
SEM_INITIALIZER(1), /* lock */
|
2019-01-26 18:18:45 +01:00
|
|
|
NO_HOLDER, /* pid */
|
2017-01-27 18:43:27 +01:00
|
|
|
0 /* count */
|
|
|
|
};
|
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
static FAR struct module_s *g_mod_registry;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 17:05:15 +01:00
|
|
|
* Name: modlib_registry_lock
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get exclusive access to the module registry.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
void modlib_registry_lock(void)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
2017-01-27 18:43:27 +01:00
|
|
|
pid_t me;
|
2017-10-08 19:52:32 +02:00
|
|
|
int ret;
|
2017-01-27 18:43:27 +01:00
|
|
|
|
|
|
|
/* Do we already hold the semaphore? */
|
|
|
|
|
|
|
|
me = getpid();
|
|
|
|
if (me == g_modlock.holder)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
2017-01-27 18:43:27 +01:00
|
|
|
/* Yes... just increment the count */
|
|
|
|
|
|
|
|
g_modlock.count++;
|
|
|
|
DEBUGASSERT(g_modlock.count > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2017-10-08 19:52:32 +02:00
|
|
|
while ((ret = _SEM_WAIT(&g_modlock.lock)) < 0)
|
2017-01-27 18:43:27 +01:00
|
|
|
{
|
2019-01-26 18:18:45 +01:00
|
|
|
/* The only case that an error should occur here is if
|
2017-01-27 18:43:27 +01:00
|
|
|
* the wait was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
2020-03-10 06:52:58 +01:00
|
|
|
DEBUGASSERT(_SEM_ERRNO(ret) == EINTR ||
|
|
|
|
_SEM_ERRNO(ret) == ECANCELED);
|
2017-10-08 19:52:32 +02:00
|
|
|
UNUSED(ret);
|
2017-01-27 18:43:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* No we hold the semaphore */
|
|
|
|
|
|
|
|
g_modlock.holder = me;
|
|
|
|
g_modlock.count = 1;
|
2015-12-13 00:42:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 17:05:15 +01:00
|
|
|
* Name: modlib_registry_unlock
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Relinquish the lock on the module registry
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
void modlib_registry_unlock(void)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
2017-01-27 18:43:27 +01:00
|
|
|
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;
|
2020-01-02 17:49:34 +01:00
|
|
|
_SEM_POST(&g_modlock.lock);
|
2017-01-27 18:43:27 +01:00
|
|
|
}
|
2015-12-13 00:42:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 17:05:15 +01:00
|
|
|
* Name: modlib_registry_add
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
void modlib_registry_add(FAR struct module_s *modp)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
|
|
|
DEBUGASSERT(modp);
|
|
|
|
modp->flink = g_mod_registry;
|
|
|
|
g_mod_registry = modp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 17:05:15 +01:00
|
|
|
* Name: modlib_registry_del
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
int modlib_registry_del(FAR struct module_s *modp)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2018-06-01 18:10:17 +02:00
|
|
|
berr("ERROR: Could not find module entry\n");
|
2015-12-13 00:42:25 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prev == NULL)
|
|
|
|
{
|
|
|
|
g_mod_registry = modp->flink;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev->flink = modp->flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
modp->flink = NULL;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-01-29 17:05:15 +01:00
|
|
|
* Name: modlib_registry_find
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find an entry in the module registry using the name of the module.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-01-29 19:23:24 +01:00
|
|
|
* modname - The name of the module to be found
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* If the registry entry is found, a pointer to the module entry is
|
2017-05-11 21:35:56 +02:00
|
|
|
* returned. NULL is returned if the entry is not found.
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the lock on the module registry.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 19:23:24 +01:00
|
|
|
#ifdef HAVE_MODLIB_NAMES
|
|
|
|
FAR struct module_s *modlib_registry_find(FAR const char *modname)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
|
|
|
FAR struct module_s *modp;
|
|
|
|
|
|
|
|
for (modp = g_mod_registry;
|
2017-01-29 19:23:24 +01:00
|
|
|
modp != NULL && strncmp(modp->modname, modname, MODLIB_NAMEMAX) != 0;
|
2015-12-13 00:42:25 +01:00
|
|
|
modp = modp->flink);
|
|
|
|
|
|
|
|
return modp;
|
|
|
|
}
|
2017-01-29 19:23:24 +01:00
|
|
|
#endif
|
2015-12-13 00:42:25 +01:00
|
|
|
|
2017-01-22 21:26:22 +01:00
|
|
|
/****************************************************************************
|
2017-01-29 17:05:15 +01:00
|
|
|
* Name: modlib_registry_verify
|
2017-01-22 21:26:22 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
int modlib_registry_verify(FAR struct module_s *modp)
|
2017-01-22 21:26:22 +01:00
|
|
|
{
|
|
|
|
FAR struct module_s *node;
|
|
|
|
|
|
|
|
for (node = g_mod_registry; node != NULL; node = node->flink)
|
|
|
|
{
|
|
|
|
if (node == modp)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2015-12-13 00:42:25 +01:00
|
|
|
/****************************************************************************
|
2017-01-29 17:05:15 +01:00
|
|
|
* Name: modlib_registry_foreach
|
2015-12-13 00:42:25 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
int modlib_registry_foreach(mod_callback_t callback, FAR void *arg)
|
2015-12-13 00:42:25 +01:00
|
|
|
{
|
|
|
|
FAR struct module_s *modp;
|
2017-01-27 18:43:27 +01:00
|
|
|
int ret = OK;
|
2015-12-13 00:42:25 +01:00
|
|
|
|
|
|
|
/* Get exclusive access to the module registry */
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
modlib_registry_lock();
|
2015-12-13 00:42:25 +01:00
|
|
|
|
|
|
|
/* Visit each installed module */
|
|
|
|
|
|
|
|
for (modp = g_mod_registry; modp != NULL; modp = modp->flink)
|
|
|
|
{
|
|
|
|
/* Perform the callback */
|
|
|
|
|
|
|
|
ret = callback(modp, arg);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2017-01-27 18:43:27 +01:00
|
|
|
break;
|
2015-12-13 00:42:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 17:05:15 +01:00
|
|
|
modlib_registry_unlock();
|
2017-01-27 18:43:27 +01:00
|
|
|
return ret;
|
2017-05-11 21:35:56 +02:00
|
|
|
}
|