2002-10-29 06:42:40 +03:00
|
|
|
/* Module manager */
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Copyright 2001, Thomas Kurschel. All rights reserved.
|
|
|
|
** Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-09-23 06:41:52 +04:00
|
|
|
#include <kmodule.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <lock.h>
|
|
|
|
#include <Errors.h>
|
|
|
|
#include <khash.h>
|
2002-10-30 02:07:06 +03:00
|
|
|
#include <malloc.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <elf.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2002-11-29 11:33:57 +03:00
|
|
|
#include <errno.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
#define MODULE_HASH_SIZE 16
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
static bool modules_disable_user_addons = false;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
#define TRACE_MODULE 0
|
|
|
|
#if TRACE_MODULE
|
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x) ;
|
|
|
|
#endif
|
|
|
|
#define FATAL(x) dprintf x
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
2002-11-29 11:33:57 +03:00
|
|
|
MODULE_QUERIED = 0,
|
|
|
|
MODULE_LOADED,
|
|
|
|
MODULE_INIT,
|
|
|
|
MODULE_READY,
|
|
|
|
MODULE_UNINIT,
|
|
|
|
MODULE_ERROR
|
2002-07-09 16:24:59 +04:00
|
|
|
} module_state;
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
|
|
|
|
/* Each loaded module image (which can export several modules) is put
|
|
|
|
* in a hash (gModuleImagesHash) to be easily found when you search
|
|
|
|
* for a specific file name.
|
|
|
|
* ToDo: should probably use the VFS to parse the path, and use only the
|
|
|
|
* inode number for hashing. Would probably a little bit slower, but would
|
|
|
|
* lower the memory foot print quite a lot.
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-12-03 20:54:09 +03:00
|
|
|
|
|
|
|
typedef struct module_image {
|
|
|
|
struct module_image *next;
|
|
|
|
module_info **info; /* the module_info we use */
|
|
|
|
char *path; /* the full path for the module */
|
|
|
|
image_id image;
|
|
|
|
int32 ref_count; /* how many ref's to this file */
|
|
|
|
bool keep_loaded;
|
|
|
|
} module_image;
|
|
|
|
|
|
|
|
/* Each known module will have this structure which is put in the
|
|
|
|
* gModulesHash, and looked up by name.
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-12-03 20:54:09 +03:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
typedef struct module {
|
2002-12-03 20:54:09 +03:00
|
|
|
struct module *next;
|
|
|
|
module_image *module_image;
|
|
|
|
char *name;
|
|
|
|
char *file;
|
|
|
|
int32 ref_count;
|
|
|
|
module_info *info; /* will only be valid if ref_count > 0 */
|
|
|
|
int offset; /* this is the offset in the headers */
|
|
|
|
module_state state; /* state of module */
|
|
|
|
bool keep_loaded;
|
2002-07-09 16:24:59 +04:00
|
|
|
} module;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct module_iterator {
|
2002-12-03 20:54:09 +03:00
|
|
|
const char **path_stack;
|
|
|
|
int stack_size;
|
|
|
|
int stack_current;
|
|
|
|
|
|
|
|
char *prefix;
|
|
|
|
DIR *current_dir;
|
|
|
|
int status;
|
|
|
|
int module_offset;
|
|
|
|
/* This is used to keep track of which module_info
|
|
|
|
* within a module we're addressing. */
|
|
|
|
module_image *module_image;
|
|
|
|
module_info **current_header;
|
|
|
|
const char *current_path;
|
|
|
|
const char *current_module_path;
|
2002-07-09 16:24:59 +04:00
|
|
|
} module_iterator;
|
|
|
|
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/* locking scheme: there is a global lock only; having several locks
|
2002-07-09 16:24:59 +04:00
|
|
|
* makes trouble if dependent modules get loaded concurrently ->
|
|
|
|
* they have to wait for each other, i.e. we need one lock per module;
|
|
|
|
* also we must detect circular references during init and not dead-lock
|
|
|
|
*/
|
2002-11-29 11:33:57 +03:00
|
|
|
static recursive_lock gModulesLock;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
/* These are the standard base paths where we start to look for modules
|
|
|
|
* to load. Order is important, the last entry here will be searched
|
|
|
|
* first.
|
|
|
|
* ToDo: these are not yet BeOS compatible (because the current bootfs is very limited)
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-12-03 20:54:09 +03:00
|
|
|
static const char * const gModulePaths[] = {
|
|
|
|
"/boot/addons",
|
|
|
|
"/boot/user-addons",
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
#define NUM_MODULE_PATHS (sizeof(gModulePaths) / sizeof(gModulePaths[0]))
|
2002-12-03 20:54:09 +03:00
|
|
|
#define USER_MODULE_PATHS 1 /* first user path */
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
/* we store the loaded modules by directory path, and all known modules by module name
|
|
|
|
* in a hash table for quick access
|
|
|
|
*/
|
2002-12-03 20:54:09 +03:00
|
|
|
static hash_table *gModuleImagesHash;
|
|
|
|
static hash_table *gModulesHash;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
/** calculates hash for a module using its name */
|
|
|
|
|
|
|
|
static uint32
|
|
|
|
module_hash(void *_module, const void *_key, uint32 range)
|
|
|
|
{
|
|
|
|
module *module = (struct module *)_module;
|
|
|
|
const char *name = (const char *)_key;
|
|
|
|
|
|
|
|
if (module != NULL)
|
|
|
|
return hash_hash_string(module->name) % range;
|
|
|
|
|
|
|
|
if (name != NULL)
|
|
|
|
return hash_hash_string(name) % range;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** compares a module to a given name */
|
|
|
|
|
|
|
|
static int
|
|
|
|
module_compare(void *_module, const void *_key)
|
|
|
|
{
|
|
|
|
module *module = (struct module *)_module;
|
|
|
|
const char *name = (const char *)_key;
|
|
|
|
if (name == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return strcmp(module->name, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
/** calculates the hash of a module image using its path */
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
static uint32
|
2002-12-03 20:54:09 +03:00
|
|
|
module_image_hash(void *_module, const void *_key, uint32 range)
|
2002-11-29 11:33:57 +03:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
module_image *image = (module_image *)_module;
|
2002-11-29 11:33:57 +03:00
|
|
|
const char *path = (const char *)_key;
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (image != NULL)
|
|
|
|
return hash_hash_string(image->path) % range;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
if (path != NULL)
|
|
|
|
return hash_hash_string(path) % range;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
/** compares a module image to a path */
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
static int
|
2002-12-03 20:54:09 +03:00
|
|
|
module_image_compare(void *_module, const void *_key)
|
2002-11-29 11:33:57 +03:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
module_image *image = (module_image *)_module;
|
2002-11-29 11:33:57 +03:00
|
|
|
const char *path = (const char *)_key;
|
|
|
|
if (path == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
return strcmp(image->path, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
inc_module_ref_count(struct module *module)
|
|
|
|
{
|
|
|
|
module->ref_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
dec_module_ref_count(struct module *module)
|
|
|
|
{
|
|
|
|
module->ref_count--;
|
2002-11-29 11:33:57 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
/** Try to load the module image at the specified location.
|
|
|
|
* If it could be loaded, it returns B_OK, and stores a pointer
|
|
|
|
* to the module_image object in "_moduleImage".
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
static status_t
|
|
|
|
load_module_image(const char *path, module_image **_moduleImage)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
module_image *moduleImage;
|
|
|
|
status_t status;
|
2003-08-18 07:41:26 +04:00
|
|
|
image_id image;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2003-08-18 07:41:26 +04:00
|
|
|
TRACE(("load_module_image(path = \"%s\", _image = %p)\n", path, _moduleImage));
|
|
|
|
ASSERT(_moduleImage != NULL);
|
|
|
|
|
|
|
|
image = elf_load_kspace(path, "");
|
2002-11-29 11:33:57 +03:00
|
|
|
if (image < 0) {
|
2002-12-03 20:54:09 +03:00
|
|
|
dprintf("load_module_image failed: %s\n", strerror(image));
|
|
|
|
return image;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
moduleImage = (module_image *)malloc(sizeof(module_image));
|
|
|
|
if (!moduleImage) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto err;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
moduleImage->info = (module_info **)elf_lookup_symbol(image, "modules");
|
|
|
|
if (!moduleImage->info) {
|
|
|
|
FATAL(("load_module_image: Failed to load %s due to lack of 'modules' symbol\n", path));
|
|
|
|
status = B_BAD_TYPE;
|
|
|
|
goto err1;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
moduleImage->path = strdup(path);
|
|
|
|
if (!moduleImage->path) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto err1;
|
2002-11-29 11:33:57 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
moduleImage->image = image;
|
|
|
|
moduleImage->ref_count = 0;
|
|
|
|
moduleImage->keep_loaded = false;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
recursive_lock_lock(&gModulesLock);
|
2002-12-03 20:54:09 +03:00
|
|
|
hash_insert(gModuleImagesHash, moduleImage);
|
2002-11-29 11:33:57 +03:00
|
|
|
recursive_lock_unlock(&gModulesLock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
*_moduleImage = moduleImage;
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
err1:
|
|
|
|
free(moduleImage);
|
|
|
|
err:
|
|
|
|
elf_unload_kspace(path);
|
|
|
|
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
static status_t
|
|
|
|
unload_module_image(module_image *moduleImage, const char *path)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
TRACE(("unload_module_image(image = %p, path = %s)\n", moduleImage, path));
|
2002-07-19 05:32:42 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (moduleImage == NULL) {
|
|
|
|
// if no image was specified, lookup it up in the hash table
|
|
|
|
moduleImage = (module_image *)hash_lookup(gModuleImagesHash, path);
|
|
|
|
if (moduleImage == NULL)
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (moduleImage->ref_count != 0) {
|
|
|
|
FATAL(("Can't unload %s due to ref_cnt = %ld\n", moduleImage->path, moduleImage->ref_count));
|
|
|
|
return B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
recursive_lock_lock(&gModulesLock);
|
2002-12-03 20:54:09 +03:00
|
|
|
hash_remove(gModuleImagesHash, moduleImage);
|
2002-11-29 11:33:57 +03:00
|
|
|
recursive_lock_unlock(&gModulesLock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
elf_unload_kspace(moduleImage->path);
|
|
|
|
free(moduleImage->path);
|
|
|
|
free(moduleImage);
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
put_module_image(module_image *image)
|
|
|
|
{
|
|
|
|
int32 refCount = atomic_add(&image->ref_count, -1);
|
|
|
|
ASSERT(refCount > 0);
|
|
|
|
|
|
|
|
if (refCount == 1 && !image->keep_loaded)
|
|
|
|
unload_module_image(image, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
|
|
|
get_module_image(const char *path, module_image **_image)
|
|
|
|
{
|
2003-08-18 07:41:26 +04:00
|
|
|
struct module_image *image;
|
|
|
|
|
|
|
|
TRACE(("get_module_image(path = \"%s\", _image = %p)\n", path, _image));
|
|
|
|
|
|
|
|
image = (module_image *)hash_lookup(gModuleImagesHash, path);
|
2002-12-03 20:54:09 +03:00
|
|
|
if (image == NULL) {
|
|
|
|
status_t status = load_module_image(path, &image);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic_add(&image->ref_count, 1);
|
|
|
|
*_image = image;
|
|
|
|
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Extract the information from the module_info structure pointed at
|
|
|
|
* by "info" and create the entries required for access to it's details.
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
static status_t
|
2002-11-29 11:33:57 +03:00
|
|
|
create_module(module_info *info, const char *file, int offset, module **_module)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-11-29 11:33:57 +03:00
|
|
|
module *module;
|
2002-07-19 05:32:42 +04:00
|
|
|
|
2003-08-18 07:41:26 +04:00
|
|
|
TRACE(("create_module(info = %p, file = \"%s\", offset = %d, _module = %p)\n",
|
|
|
|
info, file, offset, _module));
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
if (!info->name)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
module = (struct module *)hash_lookup(gModulesHash, info->name);
|
|
|
|
if (module) {
|
|
|
|
FATAL(("Duplicate module name (%s) detected... ignoring new one\n", info->name));
|
|
|
|
return B_FILE_EXISTS;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-07-19 05:32:42 +04:00
|
|
|
|
2003-08-18 07:41:26 +04:00
|
|
|
if ((module = (struct module *)malloc(sizeof(struct module))) == NULL)
|
2002-11-29 11:33:57 +03:00
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2003-08-18 07:41:26 +04:00
|
|
|
TRACE(("create_module: name = \"%s\", file = \"%s\"\n", info->name, file));
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
module->module_image = NULL;
|
2002-11-29 11:33:57 +03:00
|
|
|
module->name = strdup(info->name);
|
|
|
|
if (module->name == NULL) {
|
|
|
|
free(module);
|
|
|
|
return B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
module->file = strdup(file);
|
|
|
|
if (module->file == NULL) {
|
|
|
|
free(module->name);
|
|
|
|
free(module);
|
|
|
|
return B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
module->state = MODULE_QUERIED;
|
|
|
|
module->offset = offset;
|
|
|
|
// record where the module_info can be found in the module_info array
|
|
|
|
module->ref_count = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
if (info->flags & B_KEEP_LOADED) {
|
|
|
|
TRACE(("module %s wants to be kept loaded\n", module->name));
|
|
|
|
module->keep_loaded = true;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
recursive_lock_lock(&gModulesLock);
|
|
|
|
hash_insert(gModulesHash, module);
|
|
|
|
recursive_lock_unlock(&gModulesLock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
if (_module)
|
|
|
|
*_module = module;
|
|
|
|
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Loads the file at "path" and scans all modules contained therein.
|
|
|
|
* Returns B_OK if "searchedName" could be found under those modules,
|
|
|
|
* B_ENTRY_NOT_FOUND if not.
|
|
|
|
* Must only be called for files that haven't been scanned yet.
|
|
|
|
* "searchedName" is allowed to be NULL (if all modules should be scanned)
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
static status_t
|
2002-12-03 20:54:09 +03:00
|
|
|
check_module_image(const char *path, const char *searchedName)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
module_image *image;
|
|
|
|
module_info **info;
|
2002-11-29 11:33:57 +03:00
|
|
|
int index = 0, match = B_ENTRY_NOT_FOUND;
|
|
|
|
|
2003-08-18 07:41:26 +04:00
|
|
|
TRACE(("check_module_image(path = \"%s\", searchedName = \"%s\")\n", path, searchedName));
|
2002-12-03 20:54:09 +03:00
|
|
|
ASSERT(hash_lookup(gModuleImagesHash, path) == NULL);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (load_module_image(path, &image) < B_OK)
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
for (info = image->info; *info; info++) {
|
2002-11-29 11:33:57 +03:00
|
|
|
// try to create a module for every module_info, check if the
|
|
|
|
// name matches if it was a new entry
|
|
|
|
if (create_module(*info, path, index++, NULL) == B_OK) {
|
|
|
|
if (searchedName && !strcmp((*info)->name, searchedName))
|
|
|
|
match = B_OK;
|
2002-07-17 16:51:48 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-07-17 20:08:53 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// The module we looked for couldn't be found, so we can unload the
|
|
|
|
// loaded module at this point
|
|
|
|
if (match != B_OK) {
|
2003-08-18 07:41:26 +04:00
|
|
|
TRACE(("check_module_file: unloading module file \"%s\" (not used yet)\n", path));
|
2002-12-03 20:54:09 +03:00
|
|
|
unload_module_image(image, path);
|
2002-07-19 05:32:42 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-07-17 20:08:53 +04:00
|
|
|
return match;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Recursively scans through the provided path for the specified module
|
|
|
|
* named "searchedName".
|
|
|
|
* If "searchedName" is NULL, all modules will be scanned.
|
|
|
|
* Returns B_OK if the module could be found, B_ENTRY_NOT_FOUND if not,
|
|
|
|
* or some other error occured during scanning.
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
static status_t
|
|
|
|
recurse_directory(const char *path, const char *searchedName)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-11-29 11:33:57 +03:00
|
|
|
status_t status;
|
2003-08-18 07:41:26 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
DIR *dir = opendir(path);
|
2003-08-18 07:41:26 +04:00
|
|
|
if (dir == NULL)
|
2002-11-29 11:33:57 +03:00
|
|
|
return errno;
|
2002-07-11 01:48:27 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
errno = 0;
|
2002-07-11 01:48:27 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// loop until we have a match or we run out of entries
|
|
|
|
while (true) {
|
|
|
|
struct dirent *dirent;
|
2002-10-17 07:09:25 +04:00
|
|
|
struct stat st;
|
2002-11-29 11:33:57 +03:00
|
|
|
char *newPath;
|
|
|
|
size_t size = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
TRACE(("scanning %s\n", path));
|
2002-07-11 01:48:27 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
dirent = readdir(dir);
|
|
|
|
if (dirent == NULL) {
|
|
|
|
// we tell the upper layer we couldn't find anything in here
|
|
|
|
status = errno == 0 ? B_ENTRY_NOT_FOUND : errno;
|
|
|
|
goto exit;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
size = strlen(path) + strlen(dirent->d_name) + 2;
|
|
|
|
newPath = (char *)malloc(size);
|
|
|
|
if (newPath == NULL) {
|
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto exit;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
strlcpy(newPath, path, size);
|
|
|
|
strlcat(newPath, "/", size);
|
|
|
|
// two slashes wouldn't hurt
|
|
|
|
strlcat(newPath, dirent->d_name, size);
|
|
|
|
|
|
|
|
if (stat(newPath, &st) != 0) {
|
|
|
|
free(newPath);
|
|
|
|
errno = 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// If we couldn't stat the current file, we will just ignore it;
|
|
|
|
// it's a problem of the file system, not ours.
|
|
|
|
continue;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
if (S_ISREG(st.st_mode)) {
|
|
|
|
// if it's a file, check if we already have it in the hash table,
|
|
|
|
// because then we know it doesn't contain the module we are
|
|
|
|
// searching for (we are here because it couldn't be found in
|
|
|
|
// the first place)
|
2002-12-03 20:54:09 +03:00
|
|
|
if (hash_lookup(gModuleImagesHash, newPath) != NULL)
|
2002-11-29 11:33:57 +03:00
|
|
|
continue;
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
status = check_module_image(newPath, searchedName);
|
2002-11-29 11:33:57 +03:00
|
|
|
} else if (S_ISDIR(st.st_mode))
|
|
|
|
status = recurse_directory(newPath, searchedName);
|
|
|
|
else
|
|
|
|
status = B_ERROR;
|
|
|
|
|
|
|
|
if (status == B_OK)
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
free(newPath);
|
|
|
|
}
|
2002-07-11 01:48:27 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
exit:
|
|
|
|
closedir(dir);
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** This is only called if we fail to find a module already in our cache...
|
|
|
|
* saves us some extra checking here :)
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-08-09 21:03:03 +04:00
|
|
|
|
|
|
|
static module *
|
|
|
|
search_module(const char *name)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
status_t status = B_ENTRY_NOT_FOUND;
|
2003-09-06 21:35:05 +04:00
|
|
|
uint32 i;
|
2002-12-03 20:54:09 +03:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
TRACE(("search_module(%s)\n", name));
|
2002-12-03 20:54:09 +03:00
|
|
|
|
2003-09-08 08:06:14 +04:00
|
|
|
// ToDo: As Ingo found out, BeOS uses the module name to locate the module
|
|
|
|
// on disk. We now have the vfs_get_module_path() call to achieve this.
|
|
|
|
// As soon as we boot from a file system other than bootfs, we should
|
|
|
|
// change the loading behaviour to only use that function (bootfs has
|
|
|
|
// a very low maximum path length, which makes it unable to contain
|
|
|
|
// the standard module directories).
|
|
|
|
// The call to vfs_get_module_path() is only for testing purposes
|
2003-09-06 21:35:05 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
for (i = 0; i < NUM_MODULE_PATHS; i++) {
|
|
|
|
if (modules_disable_user_addons && i >= USER_MODULE_PATHS)
|
|
|
|
return NULL;
|
|
|
|
|
2003-09-08 08:06:14 +04:00
|
|
|
{
|
|
|
|
char path[B_FILE_NAME_LENGTH];
|
|
|
|
if (vfs_get_module_path(gModulePaths[i], name, path, sizeof(path)) < B_OK) {
|
|
|
|
TRACE(("vfs_get_module_path() failed for \"%s\"\n", name));
|
|
|
|
} else {
|
|
|
|
TRACE(("vfs_get_module_path(): found \"%s\" (for \"%s\")\n", path, name));
|
|
|
|
}
|
|
|
|
}
|
2002-12-03 20:54:09 +03:00
|
|
|
if ((status = recurse_directory(gModulePaths[i], name)) == B_OK)
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (status != B_OK)
|
2002-07-09 16:24:59 +04:00
|
|
|
return NULL;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
return (module *)hash_lookup(gModulesHash, name);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Initializes a loaded module depending on its state */
|
|
|
|
|
|
|
|
static inline status_t
|
2002-08-09 21:03:03 +04:00
|
|
|
init_module(module *module)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-11-29 11:33:57 +03:00
|
|
|
switch (module->state) {
|
|
|
|
case MODULE_QUERIED:
|
|
|
|
case MODULE_LOADED:
|
|
|
|
{
|
|
|
|
status_t status;
|
|
|
|
module->state = MODULE_INIT;
|
|
|
|
|
|
|
|
TRACE(("initing module %s... \n", module->name));
|
|
|
|
status = module->info->std_ops(B_MODULE_INIT);
|
|
|
|
TRACE(("...done (%s)\n", strerror(status)));
|
|
|
|
|
|
|
|
if (!status)
|
|
|
|
module->state = MODULE_READY;
|
2002-07-09 16:24:59 +04:00
|
|
|
else
|
2002-11-29 11:33:57 +03:00
|
|
|
module->state = MODULE_LOADED;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MODULE_READY:
|
|
|
|
return B_NO_ERROR;
|
|
|
|
|
|
|
|
case MODULE_INIT:
|
|
|
|
FATAL(("circular reference to %s\n", module->name));
|
|
|
|
return B_ERROR;
|
|
|
|
|
|
|
|
case MODULE_UNINIT:
|
|
|
|
FATAL(("tried to load module %s which is currently unloading\n", module->name));
|
|
|
|
return B_ERROR;
|
|
|
|
|
|
|
|
case MODULE_ERROR:
|
|
|
|
FATAL(("cannot load module %s because its earlier unloading failed\n", module->name));
|
|
|
|
return B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
default:
|
2002-11-29 11:33:57 +03:00
|
|
|
return B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-11-29 11:33:57 +03:00
|
|
|
// never trespasses here
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Uninitializes a module depeding on its state */
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
static inline int
|
|
|
|
uninit_module(module *module)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-11-29 11:33:57 +03:00
|
|
|
switch (module->state) {
|
|
|
|
case MODULE_QUERIED:
|
|
|
|
case MODULE_LOADED:
|
2002-07-09 16:24:59 +04:00
|
|
|
return B_NO_ERROR;
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
case MODULE_INIT:
|
|
|
|
panic("Trying to unload module %s which is initializing\n", module->name);
|
2002-07-12 02:21:56 +04:00
|
|
|
return B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
case MODULE_UNINIT:
|
|
|
|
panic("Trying to unload module %s which is un-initializing\n", module->name);
|
2002-07-12 02:21:56 +04:00
|
|
|
return B_ERROR;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
case MODULE_READY:
|
|
|
|
{
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
module->state = MODULE_UNINIT;
|
|
|
|
|
|
|
|
TRACE(("uniniting module %s...\n", module->name));
|
|
|
|
status = module->info->std_ops(B_MODULE_UNINIT);
|
|
|
|
TRACE(("...done (%s)\n", strerror(status)));
|
|
|
|
|
|
|
|
if (status == B_NO_ERROR) {
|
|
|
|
module->state = MODULE_LOADED;
|
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
FATAL(("Error unloading module %s (%s)\n", module->name, strerror(status)));
|
|
|
|
|
|
|
|
module->state = MODULE_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
module->keep_loaded = true;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
default:
|
2002-07-12 02:21:56 +04:00
|
|
|
return B_ERROR;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-11-29 11:33:57 +03:00
|
|
|
// never trespasses here
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
static const char *
|
|
|
|
iterator_pop_path_from_stack(module_iterator *iterator)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
if (iterator->stack_current > 0)
|
|
|
|
return iterator->path_stack[--iterator->stack_current];
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
return NULL;
|
2002-11-29 11:33:57 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
static status_t
|
|
|
|
iterator_push_path_on_stack(module_iterator *iterator, const char *path)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
if (iterator->stack_current + 1 > iterator->stack_size) {
|
|
|
|
// allocate new space on the stack
|
|
|
|
const char **stack = (const char **)malloc((iterator->stack_size + 8) * sizeof(char *));
|
|
|
|
if (stack == NULL)
|
|
|
|
return B_NO_MEMORY;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (iterator->path_stack != NULL) {
|
|
|
|
memcpy(stack, iterator->path_stack, iterator->stack_current * sizeof(char *));
|
|
|
|
free(iterator->path_stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator->path_stack = stack;
|
|
|
|
iterator->stack_size += 8;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator->path_stack[iterator->stack_current++] = path;
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
static status_t
|
|
|
|
iterator_get_next_module(module_iterator *iterator, char *buffer, size_t *_bufferSize)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
status_t status;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
TRACE(("iterator_get_next_module() -- start\n"));
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
nextDirectory:
|
|
|
|
if (iterator->current_dir == NULL) {
|
|
|
|
// get next directory path from the stack
|
|
|
|
const char *path = iterator_pop_path_from_stack(iterator);
|
|
|
|
if (path == NULL) {
|
|
|
|
// we are finished, there are no more entries on the stack
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
|
|
|
}
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
free((void *)iterator->current_path);
|
|
|
|
iterator->current_path = path;
|
|
|
|
iterator->current_dir = opendir(path);
|
|
|
|
TRACE(("open directory at %s -> %p\n", path, iterator->current_dir));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (iterator->current_dir == NULL) {
|
|
|
|
// we don't throw an error here, but silently go to
|
|
|
|
// the next directory on the stack
|
|
|
|
goto nextDirectory;
|
|
|
|
}
|
|
|
|
}
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
nextModuleImage:
|
|
|
|
if (iterator->current_header == NULL) {
|
|
|
|
// get next entry from the current directory
|
|
|
|
char path[SYS_MAX_PATH_LEN];
|
|
|
|
struct dirent *dirent;
|
|
|
|
struct stat st;
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
errno = 0;
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if ((dirent = readdir(iterator->current_dir)) == NULL) {
|
|
|
|
closedir(iterator->current_dir);
|
|
|
|
iterator->current_dir = NULL;
|
2002-10-17 07:09:25 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (errno < B_OK)
|
|
|
|
return errno;
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
goto nextDirectory;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (!strcmp(dirent->d_name, ".")
|
|
|
|
|| !strcmp(dirent->d_name, ".."))
|
|
|
|
goto nextModuleImage;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
// build absolute path to current file
|
|
|
|
strlcpy(path, iterator->current_path, sizeof(path));
|
|
|
|
strlcat(path, "/", sizeof(path));
|
|
|
|
strlcat(path, dirent->d_name, sizeof(path));
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
// find out if it's a directory or a file
|
|
|
|
if (stat(path, &st) < 0)
|
|
|
|
return errno;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator->current_module_path = strdup(path);
|
|
|
|
if (iterator->current_module_path == NULL)
|
|
|
|
return B_NO_MEMORY;
|
2002-10-17 07:09:25 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
status = iterator_push_path_on_stack(iterator, iterator->current_module_path);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator->current_module_path = NULL;
|
|
|
|
goto nextModuleImage;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
return B_BAD_TYPE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
TRACE(("open module at %s\n", path));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
status = get_module_image(path, &iterator->module_image);
|
|
|
|
if (status < B_OK) {
|
|
|
|
free((void *)iterator->current_module_path);
|
|
|
|
iterator->current_module_path = NULL;
|
|
|
|
goto nextModuleImage;
|
|
|
|
}
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator->current_header = iterator->module_image->info;
|
|
|
|
iterator->module_offset = 0;
|
|
|
|
}
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (*iterator->current_header == NULL) {
|
|
|
|
iterator->current_header = NULL;
|
|
|
|
free((void *)iterator->current_module_path);
|
|
|
|
iterator->current_module_path = NULL;
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
put_module_image(iterator->module_image);
|
|
|
|
iterator->module_image = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
goto nextModuleImage;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
// ToDo: we might want to create a module here and cache it in the hash table
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
*_bufferSize = strlcpy(buffer, (*iterator->current_header)->name, *_bufferSize);
|
|
|
|
|
|
|
|
iterator->current_header++;
|
|
|
|
iterator->module_offset++;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2003-08-18 07:41:26 +04:00
|
|
|
static int
|
|
|
|
dump_modules(int argc, char **argv)
|
|
|
|
{
|
|
|
|
hash_iterator iterator;
|
|
|
|
struct module_image *image;
|
|
|
|
struct module *module;
|
|
|
|
|
|
|
|
hash_rewind(gModulesHash, &iterator);
|
|
|
|
dprintf("-- known modules:\n");
|
|
|
|
|
|
|
|
while ((module = (struct module *)hash_next(gModulesHash, &iterator)) != NULL) {
|
|
|
|
dprintf("%p: \"%s\", \"%s\" (%d), refcount = %ld, state = %d, mimage = %p\n",
|
|
|
|
module, module->name, module->file, module->offset, module->ref_count,
|
|
|
|
module->state, module->module_image);
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_rewind(gModuleImagesHash, &iterator);
|
|
|
|
dprintf("\n-- loaded modules:\n");
|
|
|
|
|
|
|
|
while ((image = (struct module_image *)hash_next(gModuleImagesHash, &iterator)) != NULL) {
|
|
|
|
dprintf("%p: \"%s\" (image_id = %ld), info = %p, refcount = %ld, %s\n", image,
|
|
|
|
image->path, image->image, image->info, image->ref_count,
|
|
|
|
image->keep_loaded ? "keep loaded" : "can be unloaded");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// #pragma mark -
|
|
|
|
// Exported Kernel API (private part)
|
|
|
|
|
|
|
|
|
|
|
|
/** Setup the module structures and data for use - must be called
|
|
|
|
* before any other module call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
|
|
|
module_init(kernel_args *ka, module_info **sys_module_headers)
|
|
|
|
{
|
2003-06-27 07:25:24 +04:00
|
|
|
if (recursive_lock_init(&gModulesLock, "modules rlock") < B_OK)
|
|
|
|
return B_ERROR;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
gModulesHash = hash_init(MODULE_HASH_SIZE, 0, module_compare, module_hash);
|
|
|
|
if (gModulesHash == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
gModuleImagesHash = hash_init(MODULE_HASH_SIZE, 0, module_image_compare, module_image_hash);
|
|
|
|
if (gModuleImagesHash == NULL)
|
2002-11-29 11:33:57 +03:00
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (sys_module_headers) {
|
|
|
|
if (register_module_image("", "(built-in)", 0, sys_module_headers) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2003-08-18 07:41:26 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
add_debugger_command("modules", &dump_modules, "list all known & loaded modules");
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
module_test(void)
|
|
|
|
{
|
|
|
|
void *cookie;
|
|
|
|
|
|
|
|
dprintf("module_test() - start!\n");
|
|
|
|
|
|
|
|
cookie = open_module_list(NULL);
|
|
|
|
if (cookie == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
char name[SYS_MAX_PATH_LEN];
|
|
|
|
size_t size = sizeof(name);
|
|
|
|
|
|
|
|
if (read_next_module_name(cookie, name, &size) < B_OK)
|
|
|
|
break;
|
|
|
|
|
|
|
|
dprintf("module: %s\n", name);
|
|
|
|
}
|
|
|
|
close_module_list(cookie);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// #pragma mark -
|
|
|
|
// Exported Kernel API (public part)
|
|
|
|
|
|
|
|
|
|
|
|
/** This returns a pointer to a structure that can be used to
|
|
|
|
* iterate through a list of all modules available under
|
|
|
|
* a given prefix.
|
|
|
|
* All paths will be searched and the returned list will
|
|
|
|
* contain all modules available under the prefix.
|
|
|
|
* The structure is then used by read_next_module_name(), and
|
|
|
|
* must be freed by calling close_module_list().
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-08-09 21:03:03 +04:00
|
|
|
|
|
|
|
void *
|
|
|
|
open_module_list(const char *prefix)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
char path[SYS_MAX_PATH_LEN];
|
2002-11-29 11:33:57 +03:00
|
|
|
module_iterator *iterator;
|
2003-09-06 21:35:05 +04:00
|
|
|
uint32 i;
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
TRACE(("open_module_list(prefix = %s)\n", prefix));
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator = (module_iterator *)malloc(sizeof(module_iterator));
|
2002-11-29 11:33:57 +03:00
|
|
|
if (!iterator)
|
2002-07-09 16:24:59 +04:00
|
|
|
return NULL;
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
memset(iterator, 0, sizeof(module_iterator));
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// ToDo: possibly, the prefix don't have to be copied, just referenced
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator->prefix = strdup(prefix ? prefix : "");
|
2002-11-29 11:33:57 +03:00
|
|
|
if (iterator->prefix == NULL) {
|
|
|
|
free(iterator);
|
2002-07-09 16:24:59 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
// put all search paths on the stack
|
|
|
|
for (i = 0; i < NUM_MODULE_PATHS; i++) {
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (modules_disable_user_addons && i >= USER_MODULE_PATHS)
|
|
|
|
break;
|
|
|
|
|
|
|
|
strcpy(path, gModulePaths[i]);
|
|
|
|
if (prefix && *prefix) {
|
|
|
|
strcat(path, "/");
|
|
|
|
strlcat(path, prefix, sizeof(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
p = strdup(path);
|
|
|
|
if (p == NULL) {
|
|
|
|
// ToDo: should we abort the whole operation here?
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
iterator_push_path_on_stack(iterator, p);
|
|
|
|
}
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
return (void *)iterator;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Frees the cookie allocated by open_module_list()
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2002-09-23 06:41:52 +04:00
|
|
|
status_t
|
2002-11-29 11:33:57 +03:00
|
|
|
close_module_list(void *cookie)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-11-29 11:33:57 +03:00
|
|
|
module_iterator *iterator = (module_iterator *)cookie;
|
2002-12-03 20:54:09 +03:00
|
|
|
const char *path;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
TRACE(("close_module_list()\n"));
|
|
|
|
|
|
|
|
if (iterator == NULL)
|
|
|
|
return B_BAD_VALUE;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
// free stack
|
|
|
|
while ((path = iterator_pop_path_from_stack(iterator)) != NULL)
|
|
|
|
free((void *)path);
|
|
|
|
|
|
|
|
// close what have been left open
|
|
|
|
if (iterator->module_image != NULL)
|
|
|
|
put_module_image(iterator->module_image);
|
|
|
|
|
|
|
|
if (iterator->current_dir != NULL)
|
|
|
|
closedir(iterator->current_dir);
|
|
|
|
|
|
|
|
free(iterator->path_stack);
|
|
|
|
free((void *)iterator->current_path);
|
|
|
|
free((void *)iterator->current_module_path);
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
free(iterator->prefix);
|
|
|
|
free(iterator);
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
return 0;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Return the next module name from the available list, using
|
|
|
|
* a structure previously created by a call to open_module_list.
|
|
|
|
* Returns B_OK as long as it found another module, B_ENTRY_NOT_FOUND
|
|
|
|
* when done.
|
|
|
|
*/
|
|
|
|
|
2002-09-23 06:41:52 +04:00
|
|
|
status_t
|
2002-11-29 11:33:57 +03:00
|
|
|
read_next_module_name(void *cookie, char *buffer, size_t *_bufferSize)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-11-29 11:33:57 +03:00
|
|
|
module_iterator *iterator = (module_iterator *)cookie;
|
|
|
|
status_t status;
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
TRACE(("read_next_module_name: looking for next module\n"));
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
if (iterator == NULL || buffer == NULL || _bufferSize == NULL)
|
|
|
|
return B_BAD_VALUE;
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (iterator->status < B_OK)
|
|
|
|
return iterator->status;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
status = iterator->status;
|
|
|
|
recursive_lock_lock(&gModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
status = iterator_get_next_module(iterator, buffer, _bufferSize);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator->status = status;
|
2002-11-29 11:33:57 +03:00
|
|
|
recursive_lock_unlock(&gModulesLock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
TRACE(("read_next_module_name: finished with status %s\n", strerror(status)));
|
2002-11-29 11:33:57 +03:00
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Iterates through all loaded modules, and stores its path in "buffer".
|
|
|
|
* ToDo: check if the function in BeOS really does that (could also mean:
|
|
|
|
* iterate through all modules that are currently loaded; have a valid
|
2002-12-03 20:54:09 +03:00
|
|
|
* module_image pointer, which would be hard to test for)
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
status_t
|
|
|
|
get_next_loaded_module_name(uint32 *cookie, char *buffer, size_t *_bufferSize)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-11-29 11:33:57 +03:00
|
|
|
hash_iterator *iterator = (hash_iterator *)*cookie;
|
2002-12-03 20:54:09 +03:00
|
|
|
module_image *moduleImage;
|
2002-11-29 11:33:57 +03:00
|
|
|
status_t status;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
TRACE(("get_next_loaded_module_name()\n"));
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
if (cookie == NULL || buffer == NULL || _bufferSize == NULL)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
if (iterator == NULL) {
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator = hash_open(gModuleImagesHash, NULL);
|
2002-11-29 11:33:57 +03:00
|
|
|
if (iterator == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
*(hash_iterator **)cookie = iterator;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
recursive_lock_lock(&gModulesLock);
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
moduleImage = hash_next(gModuleImagesHash, iterator);
|
|
|
|
if (moduleImage != NULL) {
|
|
|
|
strlcpy(buffer, moduleImage->path, *_bufferSize);
|
|
|
|
*_bufferSize = strlen(moduleImage->path);
|
2002-11-29 11:33:57 +03:00
|
|
|
status = B_OK;
|
|
|
|
} else {
|
2002-12-03 20:54:09 +03:00
|
|
|
hash_close(gModuleImagesHash, iterator, true);
|
2002-11-29 11:33:57 +03:00
|
|
|
status = B_ENTRY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
recursive_lock_unlock(&gModulesLock);
|
|
|
|
|
|
|
|
return status;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-23 06:41:52 +04:00
|
|
|
status_t
|
2002-11-29 11:33:57 +03:00
|
|
|
get_module(const char *path, module_info **_info)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-12-03 20:54:09 +03:00
|
|
|
module_image *moduleImage;
|
2002-11-29 11:33:57 +03:00
|
|
|
module *module;
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
TRACE(("get_module(%s)\n", path));
|
2003-08-18 07:41:26 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
if (path == NULL)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
recursive_lock_lock(&gModulesLock);
|
|
|
|
|
|
|
|
module = (struct module *)hash_lookup(gModulesHash, path);
|
|
|
|
|
|
|
|
// if we don't have it cached yet, search for it
|
|
|
|
if (module == NULL) {
|
|
|
|
module = search_module(path);
|
|
|
|
if (module == NULL) {
|
|
|
|
FATAL(("module: Search for %s failed.\n", path));
|
|
|
|
goto err;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
/* We now need to find the module_image for the module. This should
|
2002-07-09 16:24:59 +04:00
|
|
|
* be in memory if we have just run search_modules, but may not be
|
2003-08-18 07:41:26 +04:00
|
|
|
* if we are using cached information.
|
2002-12-03 20:54:09 +03:00
|
|
|
* We can't use the module->module_image pointer, because it is not
|
|
|
|
* reliable at this point (it won't be set to NULL when the module_image
|
2002-11-29 11:33:57 +03:00
|
|
|
* is unloaded).
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2002-12-03 20:54:09 +03:00
|
|
|
if (get_module_image(module->file, &moduleImage) < B_OK)
|
|
|
|
goto err;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// (re)set in-memory data for the loaded module
|
2002-12-03 20:54:09 +03:00
|
|
|
module->info = moduleImage->info[module->offset];
|
|
|
|
module->module_image = moduleImage;
|
|
|
|
|
|
|
|
// the module image must not be unloaded anymore
|
|
|
|
if (module->keep_loaded)
|
|
|
|
module->module_image->keep_loaded = true;
|
|
|
|
|
|
|
|
inc_module_ref_count(module);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// The state will be adjusted by the call to init_module
|
|
|
|
// if we have just loaded the file
|
|
|
|
if (module->ref_count == 1)
|
|
|
|
status = init_module(module);
|
|
|
|
else
|
|
|
|
status = B_OK;
|
|
|
|
|
|
|
|
recursive_lock_unlock(&gModulesLock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
if (status == B_OK)
|
|
|
|
*_info = module->info;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
|
|
|
|
err:
|
|
|
|
recursive_lock_unlock(&gModulesLock);
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-09-23 06:41:52 +04:00
|
|
|
|
|
|
|
status_t
|
|
|
|
put_module(const char *path)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2002-11-29 11:33:57 +03:00
|
|
|
module *module;
|
|
|
|
|
|
|
|
TRACE(("put_module(path = %s)\n", path));
|
|
|
|
|
|
|
|
recursive_lock_lock(&gModulesLock);
|
|
|
|
|
|
|
|
module = (struct module *)hash_lookup(gModulesHash, path);
|
|
|
|
if (module == NULL) {
|
|
|
|
FATAL(("module: We don't seem to have a reference to module %s\n", path));
|
|
|
|
recursive_lock_unlock(&gModulesLock);
|
|
|
|
return B_BAD_VALUE;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-12-03 20:54:09 +03:00
|
|
|
dec_module_ref_count(module);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
// ToDo: not sure if this should ever be called for keep_loaded modules...
|
|
|
|
if (module->ref_count == 0)
|
2002-11-29 11:33:57 +03:00
|
|
|
uninit_module(module);
|
2002-12-03 20:54:09 +03:00
|
|
|
|
|
|
|
put_module_image(module->module_image);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
recursive_lock_unlock(&gModulesLock);
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|