2002-07-09 16:24:59 +04:00
|
|
|
/*
|
2005-01-18 05:37:38 +03:00
|
|
|
* Copyright 2002-2005, Haiku Inc. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Copyright 2001, Thomas Kurschel. All rights reserved.
|
|
|
|
* Distributed under the terms of the NewOS License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The Module manager
|
|
|
|
* Manages kernel add-ons and their exported modules.
|
|
|
|
*/
|
2002-07-09 16:24:59 +04:00
|
|
|
|
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 <elf.h>
|
2004-12-14 02:02:18 +03:00
|
|
|
#include <vfs.h>
|
2005-03-19 04:58:05 +03:00
|
|
|
#include <util/khash.h>
|
2004-05-12 03:44:58 +04:00
|
|
|
#include <boot/elf.h>
|
2004-04-26 21:13:36 +04:00
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
#include <sys/stat.h>
|
2004-04-26 21:13:36 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-11-29 11:33:57 +03:00
|
|
|
#include <errno.h>
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2005-01-06 23:31:22 +03:00
|
|
|
|
|
|
|
//#define TRACE_MODULE
|
|
|
|
#ifdef TRACE_MODULE
|
|
|
|
# define TRACE(x) dprintf x
|
|
|
|
#else
|
|
|
|
# define TRACE(x) ;
|
|
|
|
#endif
|
|
|
|
#define FATAL(x) dprintf x
|
|
|
|
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
#define MODULE_HASH_SIZE 16
|
2005-01-06 23:31:22 +03:00
|
|
|
#define SUPPORT_BOOTFS
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
/** The modules referenced by this structure are built-in
|
|
|
|
* modules that can't be loaded from disk.
|
|
|
|
*/
|
|
|
|
|
2004-05-10 17:29:47 +04:00
|
|
|
extern module_info gDeviceManagerModule;
|
|
|
|
extern module_info gDeviceRootModule;
|
2004-06-07 05:21:08 +04:00
|
|
|
extern module_info gDeviceForDriversModule;
|
2005-02-15 03:18:27 +03:00
|
|
|
extern module_info gFrameBufferConsoleModule;
|
2004-05-10 17:29:47 +04:00
|
|
|
|
2004-06-07 21:33:18 +04:00
|
|
|
// file systems
|
|
|
|
extern module_info gRootFileSystem;
|
|
|
|
extern module_info gDeviceFileSystem;
|
|
|
|
extern module_info gBootFileSystem;
|
|
|
|
extern module_info gPipeFileSystem;
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
static module_info *sBuiltInModules[] = {
|
2004-05-10 17:29:47 +04:00
|
|
|
&gDeviceManagerModule,
|
|
|
|
&gDeviceRootModule,
|
2004-06-07 05:21:08 +04:00
|
|
|
&gDeviceForDriversModule,
|
2005-02-15 03:18:27 +03:00
|
|
|
&gFrameBufferConsoleModule,
|
2004-06-07 21:33:18 +04:00
|
|
|
|
|
|
|
&gRootFileSystem,
|
|
|
|
&gDeviceFileSystem,
|
|
|
|
&gBootFileSystem,
|
|
|
|
&gPipeFileSystem,
|
2004-04-27 01:02:27 +04:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
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.
|
2004-05-10 04:09:21 +04:00
|
|
|
* ToDo: Could use only the inode number for hashing. Would probably be
|
|
|
|
* 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 */
|
2004-04-26 21:13:36 +04:00
|
|
|
module_dependency *dependencies;
|
2002-12-03 20:54:09 +03:00
|
|
|
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 */
|
2004-04-27 01:02:27 +04:00
|
|
|
int32 offset; /* this is the offset in the headers */
|
2002-12-03 20:54:09 +03:00
|
|
|
module_state state; /* state of module */
|
2004-04-27 01:02:27 +04:00
|
|
|
uint32 flags;
|
2002-07-09 16:24:59 +04:00
|
|
|
} module;
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
#define B_BUILT_IN_MODULE 2
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
typedef struct module_path {
|
|
|
|
const char *name;
|
|
|
|
uint32 base_length;
|
|
|
|
} module_path;
|
|
|
|
|
2002-07-09 16:24:59 +04:00
|
|
|
typedef struct module_iterator {
|
2004-05-10 04:02:20 +04:00
|
|
|
module_path *stack;
|
2004-05-09 04:41:19 +04:00
|
|
|
int32 stack_size;
|
|
|
|
int32 stack_current;
|
2002-12-03 20:54:09 +03:00
|
|
|
|
|
|
|
char *prefix;
|
2004-05-10 04:02:20 +04:00
|
|
|
size_t prefix_length;
|
2002-12-03 20:54:09 +03:00
|
|
|
DIR *current_dir;
|
2004-05-09 04:41:19 +04:00
|
|
|
status_t status;
|
|
|
|
int32 module_offset;
|
2002-12-03 20:54:09 +03:00
|
|
|
/* 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;
|
2004-05-10 04:02:20 +04:00
|
|
|
int32 path_base_length;
|
2002-12-03 20:54:09 +03:00
|
|
|
const char *current_module_path;
|
2004-05-09 04:41:19 +04:00
|
|
|
bool builtin_modules;
|
2002-07-09 16:24:59 +04:00
|
|
|
} module_iterator;
|
|
|
|
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
static bool sDisableUserAddOns = false;
|
|
|
|
|
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
|
|
|
|
*/
|
2004-05-10 04:09:21 +04:00
|
|
|
static recursive_lock sModulesLock;
|
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.
|
2005-01-06 23:31:22 +03:00
|
|
|
* ToDo: the first entry is only there for bootfs compatibility
|
2004-05-10 04:09:21 +04:00
|
|
|
* ToDo: these should probably be retrieved by using find_directory().
|
2002-07-09 16:24:59 +04:00
|
|
|
*/
|
2004-05-10 04:09:21 +04:00
|
|
|
static const char * const sModulePaths[] = {
|
2005-01-06 23:31:22 +03:00
|
|
|
#ifdef SUPPORT_BOOTFS
|
2004-05-10 04:02:20 +04:00
|
|
|
"/boot/addons/kernel",
|
2005-01-06 23:31:22 +03:00
|
|
|
#endif
|
|
|
|
"/boot/beos/system/add-ons/kernel",
|
2004-05-10 04:02:20 +04:00
|
|
|
"/boot/home/config/add-ons/kernel",
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
#define NUM_MODULE_PATHS (sizeof(sModulePaths) / sizeof(sModulePaths[0]))
|
2005-01-06 23:31:22 +03:00
|
|
|
#define FIRST_USER_MODULE_PATH (NUM_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
|
|
|
|
*/
|
2004-05-10 04:09:21 +04:00
|
|
|
static hash_table *sModuleImagesHash;
|
|
|
|
static hash_table *sModulesHash;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2004-06-26 05:40:46 +04:00
|
|
|
extern dev_t gBootDevice;
|
|
|
|
|
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);
|
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
image = load_kernel_add_on(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
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
if (get_image_symbol(image, "modules", B_SYMBOL_TYPE_DATA,
|
|
|
|
(void **)&moduleImage->info) != B_OK) {
|
2005-01-18 05:37:38 +03:00
|
|
|
TRACE(("load_module_image: Failed to load \"%s\" due to lack of 'modules' symbol\n", path));
|
2002-12-03 20:54:09 +03:00
|
|
|
status = B_BAD_TYPE;
|
|
|
|
goto err1;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
moduleImage->dependencies = NULL;
|
|
|
|
get_image_symbol(image, "module_dependencies", B_SYMBOL_TYPE_DATA,
|
|
|
|
(void **)&moduleImage->dependencies);
|
2004-04-26 21:13:36 +04:00
|
|
|
// this is allowed to be NULL
|
|
|
|
|
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
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_lock(&sModulesLock);
|
|
|
|
hash_insert(sModuleImagesHash, moduleImage);
|
|
|
|
recursive_lock_unlock(&sModulesLock);
|
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:
|
2004-05-11 23:52:38 +04:00
|
|
|
unload_kernel_add_on(image);
|
2002-12-03 20:54:09 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2005-01-18 05:37:38 +03:00
|
|
|
recursive_lock_lock(&sModulesLock);
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
if (moduleImage == NULL) {
|
|
|
|
// if no image was specified, lookup it up in the hash table
|
2004-05-10 04:09:21 +04:00
|
|
|
moduleImage = (module_image *)hash_lookup(sModuleImagesHash, path);
|
2005-01-18 05:37:38 +03:00
|
|
|
if (moduleImage == NULL) {
|
|
|
|
recursive_lock_unlock(&sModulesLock);
|
2002-12-03 20:54:09 +03:00
|
|
|
return B_ENTRY_NOT_FOUND;
|
2005-01-18 05:37:38 +03:00
|
|
|
}
|
2002-12-03 20:54:09 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
hash_remove(sModuleImagesHash, moduleImage);
|
|
|
|
recursive_lock_unlock(&sModulesLock);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-11 23:52:38 +04:00
|
|
|
unload_kernel_add_on(moduleImage->image);
|
2002-12-03 20:54:09 +03:00
|
|
|
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);
|
|
|
|
|
2004-06-26 05:40:46 +04:00
|
|
|
// Don't unload anything when there is no boot device yet
|
|
|
|
// (because chances are that we will never be able to access it again)
|
|
|
|
|
|
|
|
if (refCount == 1 && !image->keep_loaded && gBootDevice > 0)
|
2002-12-03 20:54:09 +03:00
|
|
|
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));
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
image = (module_image *)hash_lookup(sModuleImagesHash, 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;
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
module = (struct module *)hash_lookup(sModulesHash, info->name);
|
2002-11-29 11:33:57 +03:00
|
|
|
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;
|
2004-04-27 01:02:27 +04:00
|
|
|
module->info = info;
|
2002-11-29 11:33:57 +03:00
|
|
|
module->offset = offset;
|
|
|
|
// record where the module_info can be found in the module_info array
|
|
|
|
module->ref_count = 0;
|
2004-04-27 01:02:27 +04:00
|
|
|
module->flags = info->flags;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_lock(&sModulesLock);
|
|
|
|
hash_insert(sModulesHash, module);
|
|
|
|
recursive_lock_unlock(&sModulesLock);
|
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));
|
2004-05-10 04:09:21 +04:00
|
|
|
ASSERT(hash_lookup(sModuleImagesHash, 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
|
|
|
|
2005-01-07 00:46:03 +03:00
|
|
|
|
|
|
|
#ifdef SUPPORT_BOOTFS
|
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
|
|
|
|
2005-01-07 00:46:03 +03:00
|
|
|
if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..") || !strcmp(dirent->d_name, "boot"))
|
2004-06-08 09:58:23 +04:00
|
|
|
continue;
|
|
|
|
|
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)
|
2004-05-10 04:09:21 +04:00
|
|
|
if (hash_lookup(sModuleImagesHash, 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
|
|
|
}
|
2005-01-07 00:46:03 +03:00
|
|
|
#endif
|
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
|
|
|
|
|
|
|
for (i = 0; i < NUM_MODULE_PATHS; i++) {
|
2005-01-06 23:31:22 +03:00
|
|
|
char path[B_FILE_NAME_LENGTH];
|
|
|
|
|
|
|
|
if (sDisableUserAddOns && i >= FIRST_USER_MODULE_PATH)
|
2002-12-03 20:54:09 +03:00
|
|
|
return NULL;
|
|
|
|
|
2005-01-06 23:31:22 +03:00
|
|
|
// let's the VFS find that module for us
|
|
|
|
|
|
|
|
status = vfs_get_module_path(sModulePaths[i], name, path, sizeof(path));
|
|
|
|
if (status == B_OK) {
|
|
|
|
status = check_module_image(path, name);
|
|
|
|
if (status == B_OK)
|
|
|
|
break;
|
2003-09-08 08:06:14 +04:00
|
|
|
}
|
2005-01-06 23:31:22 +03:00
|
|
|
|
|
|
|
#ifdef SUPPORT_BOOTFS
|
|
|
|
// BeOS uses the module name to locate the module on disk. We now have the
|
|
|
|
// above vfs_get_module_path() call to achieve this.
|
|
|
|
// "bootfs" has a very low maximum path length, which makes it unable to
|
|
|
|
// contain the standard module directories).
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
if ((status = recurse_directory(sModulePaths[i], name)) == B_OK)
|
2002-07-09 16:24:59 +04:00
|
|
|
break;
|
2005-01-06 23:31:22 +03:00
|
|
|
#endif
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
return (module *)hash_lookup(sModulesHash, name);
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-26 21:13:36 +04:00
|
|
|
static status_t
|
2004-04-27 01:02:27 +04:00
|
|
|
put_dependent_modules(struct module *module)
|
2004-04-26 21:13:36 +04:00
|
|
|
{
|
2004-04-27 01:02:27 +04:00
|
|
|
module_dependency *dependencies;
|
2004-04-26 21:13:36 +04:00
|
|
|
int32 i = 0;
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
if (module->module_image == NULL
|
|
|
|
|| (dependencies = module->module_image->dependencies) == NULL)
|
2004-04-26 21:13:36 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
for (; dependencies[i].name != NULL; i++) {
|
|
|
|
status_t status = put_module(dependencies[i].name);
|
|
|
|
if (status < B_OK)
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static status_t
|
2004-04-27 01:02:27 +04:00
|
|
|
get_dependent_modules(struct module *module)
|
2004-04-26 21:13:36 +04:00
|
|
|
{
|
2004-04-27 01:02:27 +04:00
|
|
|
module_dependency *dependencies;
|
2004-04-26 21:13:36 +04:00
|
|
|
int32 i = 0;
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
// built-in modules don't have a module_image structure
|
|
|
|
if (module->module_image == NULL
|
|
|
|
|| (dependencies = module->module_image->dependencies) == NULL)
|
2004-04-26 21:13:36 +04:00
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
TRACE(("resolving module dependencies...\n"));
|
|
|
|
|
|
|
|
for (; dependencies[i].name != NULL; i++) {
|
|
|
|
status_t status = get_module(dependencies[i].name, dependencies[i].info);
|
|
|
|
if (status < B_OK) {
|
|
|
|
TRACE(("loading dependent module \"%s\" failed!\n", dependencies[i].name));
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-04-26 21:13:36 +04:00
|
|
|
// resolve dependencies
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
status = get_dependent_modules(module);
|
2004-04-26 21:13:36 +04:00
|
|
|
if (status < B_OK) {
|
|
|
|
module->state = MODULE_LOADED;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// init module
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
TRACE(("initializing module %s (at %p)... \n", module->name, module->info->std_ops));
|
2002-11-29 11:33:57 +03:00
|
|
|
status = module->info->std_ops(B_MODULE_INIT);
|
|
|
|
TRACE(("...done (%s)\n", strerror(status)));
|
|
|
|
|
2004-04-26 21:13:36 +04:00
|
|
|
if (status >= B_OK)
|
2002-11-29 11:33:57 +03:00
|
|
|
module->state = MODULE_READY;
|
2004-04-26 21:13:36 +04:00
|
|
|
else {
|
2004-04-27 01:02:27 +04:00
|
|
|
put_dependent_modules(module);
|
2002-11-29 11:33:57 +03:00
|
|
|
module->state = MODULE_LOADED;
|
2004-04-26 21:13:36 +04:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MODULE_READY:
|
2004-04-26 21:13:36 +04:00
|
|
|
return B_OK;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
TRACE(("uninitializing module %s...\n", module->name));
|
2002-11-29 11:33:57 +03:00
|
|
|
status = module->info->std_ops(B_MODULE_UNINIT);
|
|
|
|
TRACE(("...done (%s)\n", strerror(status)));
|
|
|
|
|
|
|
|
if (status == B_NO_ERROR) {
|
|
|
|
module->state = MODULE_LOADED;
|
2004-04-26 21:13:36 +04:00
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
put_dependent_modules(module);
|
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
|
|
|
|
|
|
|
FATAL(("Error unloading module %s (%s)\n", module->name, strerror(status)));
|
|
|
|
|
|
|
|
module->state = MODULE_ERROR;
|
2004-04-27 01:02:27 +04:00
|
|
|
module->flags |= B_KEEP_LOADED;
|
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 *
|
2004-05-10 04:02:20 +04:00
|
|
|
iterator_pop_path_from_stack(module_iterator *iterator, uint32 *_baseLength)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-05-10 04:02:20 +04:00
|
|
|
if (iterator->stack_current <= 0)
|
|
|
|
return NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
if (_baseLength)
|
|
|
|
*_baseLength = iterator->stack[iterator->stack_current - 1].base_length;
|
|
|
|
|
|
|
|
return iterator->stack[--iterator->stack_current].name;
|
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
|
2004-05-10 04:02:20 +04:00
|
|
|
iterator_push_path_on_stack(module_iterator *iterator, const char *path, uint32 baseLength)
|
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
|
2004-05-10 04:02:20 +04:00
|
|
|
module_path *stack = (module_path *)realloc(iterator->stack,
|
|
|
|
(iterator->stack_size + 8) * sizeof(module_path));
|
2002-12-03 20:54:09 +03:00
|
|
|
if (stack == NULL)
|
|
|
|
return B_NO_MEMORY;
|
2004-05-10 04:02:20 +04:00
|
|
|
|
|
|
|
iterator->stack = stack;
|
2002-12-03 20:54:09 +03:00
|
|
|
iterator->stack_size += 8;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2004-05-10 04:02:20 +04:00
|
|
|
|
|
|
|
iterator->stack[iterator->stack_current].name = path;
|
|
|
|
iterator->stack[iterator->stack_current++].base_length = baseLength;
|
2002-12-03 20:54:09 +03:00
|
|
|
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
|
|
|
|
2004-05-09 04:41:19 +04:00
|
|
|
if (iterator->builtin_modules) {
|
2004-05-10 04:02:20 +04:00
|
|
|
int32 i;
|
|
|
|
|
|
|
|
for (i = iterator->module_offset; sBuiltInModules[i] != NULL; i++) {
|
|
|
|
// the module name must fit the prefix
|
|
|
|
if (strncmp(sBuiltInModules[i]->name, iterator->prefix, iterator->prefix_length))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
*_bufferSize = strlcpy(buffer, sBuiltInModules[i]->name, *_bufferSize);
|
|
|
|
iterator->module_offset = i + 1;
|
2004-05-09 04:41:19 +04:00
|
|
|
return B_OK;
|
2004-05-10 04:02:20 +04:00
|
|
|
}
|
|
|
|
iterator->builtin_modules = false;
|
2004-05-09 04:41:19 +04:00
|
|
|
}
|
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
nextPath:
|
2002-12-03 20:54:09 +03:00
|
|
|
if (iterator->current_dir == NULL) {
|
|
|
|
// get next directory path from the stack
|
2004-05-10 04:02:20 +04:00
|
|
|
const char *path = iterator_pop_path_from_stack(iterator, &iterator->path_base_length);
|
2002-12-03 20:54:09 +03:00
|
|
|
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
|
2004-05-10 04:02:20 +04:00
|
|
|
goto nextPath;
|
2002-12-03 20:54:09 +03:00
|
|
|
}
|
|
|
|
}
|
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
|
2004-05-10 04:02:20 +04:00
|
|
|
char path[B_PATH_NAME_LENGTH];
|
2002-12-03 20:54:09 +03:00
|
|
|
struct dirent *dirent;
|
|
|
|
struct stat st;
|
2004-05-10 04:02:20 +04:00
|
|
|
int32 passedOffset, commonLength;
|
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
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
goto nextPath;
|
2002-12-03 20:54:09 +03:00
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
// check if the prefix matches
|
|
|
|
passedOffset = strlen(iterator->current_path) + 1;
|
|
|
|
commonLength = iterator->path_base_length + iterator->prefix_length - passedOffset;
|
|
|
|
|
|
|
|
if (commonLength > 0) {
|
|
|
|
// the prefix still reaches into the new path part
|
|
|
|
int32 length = strlen(dirent->d_name);
|
|
|
|
if (commonLength > length)
|
|
|
|
commonLength = length;
|
|
|
|
|
|
|
|
if (strncmp(dirent->d_name,
|
|
|
|
iterator->prefix + passedOffset - iterator->path_base_length, commonLength))
|
|
|
|
goto nextModuleImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're not interested in traversing these again
|
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)) {
|
2004-05-10 04:02:20 +04:00
|
|
|
status = iterator_push_path_on_stack(iterator, iterator->current_module_path,
|
|
|
|
iterator->path_base_length);
|
2002-12-03 20:54:09 +03:00
|
|
|
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
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
// search the current module image until we've got a match
|
|
|
|
while (*iterator->current_header != NULL) {
|
|
|
|
module_info *info = *iterator->current_header;
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
// ToDo: we might want to create a module here and cache it in the hash table
|
|
|
|
|
|
|
|
iterator->current_header++;
|
|
|
|
iterator->module_offset++;
|
|
|
|
|
|
|
|
if (strncmp(info->name, iterator->prefix, iterator->prefix_length))
|
|
|
|
continue;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
*_bufferSize = strlcpy(buffer, info->name, *_bufferSize);
|
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
// leave this module and get the next one
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
iterator->current_header = NULL;
|
|
|
|
free((void *)iterator->current_module_path);
|
|
|
|
iterator->current_module_path = NULL;
|
2002-12-03 20:54:09 +03:00
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
put_module_image(iterator->module_image);
|
|
|
|
iterator->module_image = NULL;
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
goto nextModuleImage;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
|
|
|
|
2002-08-09 21:03:03 +04:00
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
static void
|
|
|
|
register_builtin_modules(struct module_info **info)
|
|
|
|
{
|
|
|
|
for (; *info; info++) {
|
|
|
|
(*info)->flags |= B_BUILT_IN_MODULE;
|
|
|
|
// this is an internal flag, it doesn't have to be set by modules itself
|
|
|
|
|
|
|
|
if (create_module(*info, "", -1, NULL) != B_OK)
|
|
|
|
dprintf("creation of built-in module \"%s\" failed!\n", (*info)->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-12 03:44:58 +04:00
|
|
|
static status_t
|
|
|
|
register_preloaded_module_image(struct preloaded_image *image)
|
|
|
|
{
|
|
|
|
module_image *moduleImage;
|
|
|
|
struct module_info **info;
|
|
|
|
status_t status;
|
|
|
|
int32 index = 0;
|
|
|
|
|
|
|
|
if (image->id < 0)
|
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
moduleImage = (module_image *)malloc(sizeof(module_image));
|
|
|
|
if (moduleImage == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
if (get_image_symbol(image->id, "modules", B_SYMBOL_TYPE_DATA,
|
|
|
|
(void **)&moduleImage->info) != B_OK) {
|
|
|
|
status = B_BAD_TYPE;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleImage->dependencies = NULL;
|
|
|
|
get_image_symbol(image->id, "module_dependencies", B_SYMBOL_TYPE_DATA,
|
|
|
|
(void **)&moduleImage->dependencies);
|
|
|
|
// this is allowed to be NULL
|
|
|
|
|
2005-01-07 00:46:03 +03:00
|
|
|
// Try to recreate the full module path, so that we don't try to load the
|
|
|
|
// image again when asked for a module it does not export (would only be
|
|
|
|
// problematic if it had got replaced and the new file actually exports
|
|
|
|
// that module). Also helpful for recurse_directory().
|
|
|
|
{
|
|
|
|
// ToDo: this is kind of a hack to have the full path in the hash
|
|
|
|
// (it always assumes the preloaded add-ons to be in the system directory)
|
|
|
|
char path[B_FILE_NAME_LENGTH];
|
|
|
|
const char *name, *suffix;
|
|
|
|
if (moduleImage->info[0]
|
|
|
|
&& (suffix = strstr(name = moduleImage->info[0]->name, image->name)) != NULL) {
|
|
|
|
// even if strlcpy() is used here, it's by no means safe against buffer overflows
|
|
|
|
size_t length = strlcpy(path, "/boot/beos/system/add-ons/kernel/", sizeof(path));
|
|
|
|
strlcpy(path + length, name, strlen(image->name) + 1 + (suffix - name));
|
|
|
|
|
|
|
|
moduleImage->path = strdup(path);
|
|
|
|
} else
|
|
|
|
moduleImage->path = strdup(image->name);
|
|
|
|
}
|
|
|
|
if (moduleImage->path == NULL) {
|
2004-05-12 03:44:58 +04:00
|
|
|
status = B_NO_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleImage->image = image->id;
|
|
|
|
moduleImage->ref_count = 0;
|
|
|
|
moduleImage->keep_loaded = false;
|
|
|
|
|
|
|
|
hash_insert(sModuleImagesHash, moduleImage);
|
|
|
|
|
|
|
|
for (info = moduleImage->info; *info; info++) {
|
2005-01-07 00:46:03 +03:00
|
|
|
create_module(*info, moduleImage->path, index++, NULL);
|
2004-05-12 03:44:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
|
|
|
|
error:
|
|
|
|
free(moduleImage);
|
|
|
|
|
|
|
|
// we don't need this image anymore
|
|
|
|
unload_kernel_add_on(image->id);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
hash_rewind(sModulesHash, &iterator);
|
2003-08-18 07:41:26 +04:00
|
|
|
dprintf("-- known modules:\n");
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
while ((module = (struct module *)hash_next(sModulesHash, &iterator)) != NULL) {
|
2004-04-27 01:02:27 +04:00
|
|
|
dprintf("%p: \"%s\", \"%s\" (%ld), refcount = %ld, state = %d, mimage = %p\n",
|
2003-08-18 07:41:26 +04:00
|
|
|
module, module->name, module->file, module->offset, module->ref_count,
|
|
|
|
module->state, module->module_image);
|
|
|
|
}
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
hash_rewind(sModuleImagesHash, &iterator);
|
2004-05-12 03:44:58 +04:00
|
|
|
dprintf("\n-- loaded module images:\n");
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
while ((image = (struct module_image *)hash_next(sModuleImagesHash, &iterator)) != NULL) {
|
2003-08-18 07:41:26 +04:00
|
|
|
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");
|
|
|
|
}
|
2003-09-13 00:44:45 +04:00
|
|
|
return 0;
|
2003-08-18 07:41:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
// #pragma mark -
|
|
|
|
// Exported Kernel API (private part)
|
|
|
|
|
|
|
|
|
2005-01-18 05:37:38 +03:00
|
|
|
/** Unloads a module in case it's not in use. This is the counterpart
|
|
|
|
* to load_module().
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
|
|
|
unload_module(const char *path)
|
|
|
|
{
|
|
|
|
struct module_image *moduleImage;
|
|
|
|
|
|
|
|
recursive_lock_lock(&sModulesLock);
|
|
|
|
moduleImage = (module_image *)hash_lookup(sModuleImagesHash, path);
|
|
|
|
recursive_lock_unlock(&sModulesLock);
|
|
|
|
|
|
|
|
if (moduleImage == NULL)
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
|
|
|
|
|
|
|
put_module_image(moduleImage);
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Unlike get_module(), this function lets you specify the add-on to
|
|
|
|
* be loaded by path.
|
|
|
|
* However, you must not use the exported modules without having called
|
|
|
|
* get_module() on them. When you're done with the NULL terminated
|
|
|
|
* \a modules array, you have to call unload_module(), no matter if
|
|
|
|
* you're actually using any of the modules or not - of course, the
|
|
|
|
* add-on won't be unloaded until the last put_module().
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
|
|
|
load_module(const char *path, module_info ***_modules)
|
|
|
|
{
|
|
|
|
module_image *moduleImage;
|
|
|
|
status_t status = get_module_image(path, &moduleImage);
|
|
|
|
if (status != B_OK)
|
|
|
|
return status;
|
|
|
|
|
|
|
|
*_modules = moduleImage->info;
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-29 11:33:57 +03:00
|
|
|
/** Setup the module structures and data for use - must be called
|
|
|
|
* before any other module call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_t
|
2004-04-27 01:02:27 +04:00
|
|
|
module_init(kernel_args *args)
|
2002-11-29 11:33:57 +03:00
|
|
|
{
|
2004-05-12 03:44:58 +04:00
|
|
|
struct preloaded_image *image;
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
if (recursive_lock_init(&sModulesLock, "modules rlock") < B_OK)
|
2003-06-27 07:25:24 +04:00
|
|
|
return B_ERROR;
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
sModulesHash = hash_init(MODULE_HASH_SIZE, 0, module_compare, module_hash);
|
|
|
|
if (sModulesHash == NULL)
|
2002-11-29 11:33:57 +03:00
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
sModuleImagesHash = hash_init(MODULE_HASH_SIZE, 0, module_image_compare, module_image_hash);
|
|
|
|
if (sModuleImagesHash == NULL)
|
2002-11-29 11:33:57 +03:00
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
// register built-in modules
|
|
|
|
|
|
|
|
register_builtin_modules(sBuiltInModules);
|
|
|
|
|
|
|
|
// register preloaded images
|
|
|
|
|
2004-05-12 03:44:58 +04:00
|
|
|
for (image = args->preloaded_images; image != NULL; image = image->next) {
|
|
|
|
register_preloaded_module_image(image);
|
|
|
|
}
|
2003-08-18 07:41:26 +04:00
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
// ToDo: set sDisableUserAddOns from kernel_args!
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// #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().
|
2004-06-26 05:40:46 +04:00
|
|
|
*
|
|
|
|
* During early boot, when there is no boot device accessible,
|
|
|
|
* it will only find built-in modules, not those that have
|
|
|
|
* been preloaded - use get_next_loaded_module_name() instead
|
|
|
|
* if you need to have a list of loaded modules.
|
|
|
|
* ToDo: this could be changed
|
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-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;
|
|
|
|
}
|
2004-05-10 04:02:20 +04:00
|
|
|
iterator->prefix_length = strlen(prefix);
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2004-05-09 04:41:19 +04:00
|
|
|
// first, we'll traverse over the built-in modules
|
|
|
|
iterator->builtin_modules = true;
|
|
|
|
|
2002-12-03 20:54:09 +03:00
|
|
|
// put all search paths on the stack
|
|
|
|
for (i = 0; i < NUM_MODULE_PATHS; i++) {
|
2004-05-10 04:02:20 +04:00
|
|
|
const char *path;
|
2002-12-03 20:54:09 +03:00
|
|
|
|
2005-01-06 23:31:22 +03:00
|
|
|
if (sDisableUserAddOns && i >= FIRST_USER_MODULE_PATH)
|
2002-12-03 20:54:09 +03:00
|
|
|
break;
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
path = strdup(sModulePaths[i]);
|
2004-05-10 04:02:20 +04:00
|
|
|
if (path == NULL) {
|
2002-12-03 20:54:09 +03:00
|
|
|
// ToDo: should we abort the whole operation here?
|
2004-05-10 04:02:20 +04:00
|
|
|
// if we do, don't forget to empty the stack
|
2002-12-03 20:54:09 +03:00
|
|
|
continue;
|
|
|
|
}
|
2004-05-10 04:02:20 +04:00
|
|
|
iterator_push_path_on_stack(iterator, path, strlen(path) + 1);
|
2002-12-03 20:54:09 +03:00
|
|
|
}
|
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
|
2004-05-10 04:02:20 +04:00
|
|
|
while ((path = iterator_pop_path_from_stack(iterator, NULL)) != NULL)
|
2002-12-03 20:54:09 +03:00
|
|
|
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);
|
|
|
|
|
2004-05-10 04:02:20 +04:00
|
|
|
free(iterator->stack);
|
2002-12-03 20:54:09 +03:00
|
|
|
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;
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_lock(&sModulesLock);
|
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;
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_unlock(&sModulesLock);
|
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
|
2004-05-09 04:41:19 +04:00
|
|
|
get_next_loaded_module_name(uint32 *_cookie, char *buffer, size_t *_bufferSize)
|
2002-07-09 16:24:59 +04:00
|
|
|
{
|
2004-05-09 04:41:19 +04:00
|
|
|
hash_iterator *iterator = (hash_iterator *)*_cookie;
|
|
|
|
struct module *module;
|
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
|
|
|
|
2004-05-09 04:41:19 +04:00
|
|
|
if (_cookie == NULL || buffer == NULL || _bufferSize == NULL)
|
2002-11-29 11:33:57 +03:00
|
|
|
return B_BAD_VALUE;
|
|
|
|
|
|
|
|
if (iterator == NULL) {
|
2004-05-10 04:09:21 +04:00
|
|
|
iterator = hash_open(sModulesHash, NULL);
|
2002-11-29 11:33:57 +03:00
|
|
|
if (iterator == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
2004-05-09 04:41:19 +04:00
|
|
|
*(hash_iterator **)_cookie = iterator;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|
2002-11-17 04:38:35 +03:00
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_lock(&sModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
module = hash_next(sModulesHash, iterator);
|
2004-05-09 04:41:19 +04:00
|
|
|
if (module != NULL) {
|
|
|
|
*_bufferSize = strlcpy(buffer, module->name, *_bufferSize);
|
2002-11-29 11:33:57 +03:00
|
|
|
status = B_OK;
|
|
|
|
} else {
|
2004-05-10 04:09:21 +04:00
|
|
|
hash_close(sModulesHash, iterator, true);
|
2002-11-29 11:33:57 +03:00
|
|
|
status = B_ENTRY_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_unlock(&sModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_lock(&sModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
module = (struct module *)hash_lookup(sModulesHash, path);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
if ((module->flags & B_BUILT_IN_MODULE) == 0) {
|
|
|
|
/* We now need to find the module_image for the module. This should
|
2005-01-07 00:46:03 +03:00
|
|
|
* be in memory if we have just run search_module(), but may not be
|
2004-04-27 01:02:27 +04:00
|
|
|
* if we are using cached information.
|
|
|
|
* 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
|
|
|
|
* is unloaded).
|
|
|
|
*/
|
|
|
|
if (get_module_image(module->file, &moduleImage) < B_OK)
|
|
|
|
goto err;
|
2005-01-07 00:46:03 +03:00
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
// (re)set in-memory data for the loaded module
|
|
|
|
module->info = moduleImage->info[module->offset];
|
|
|
|
module->module_image = moduleImage;
|
2005-01-07 00:46:03 +03:00
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
// the module image must not be unloaded anymore
|
|
|
|
if (module->flags & B_KEEP_LOADED)
|
|
|
|
module->module_image->keep_loaded = true;
|
|
|
|
}
|
2002-12-03 20:54:09 +03: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
|
2004-06-08 09:58:23 +04:00
|
|
|
if (module->ref_count == 0)
|
2002-11-29 11:33:57 +03:00
|
|
|
status = init_module(module);
|
|
|
|
else
|
|
|
|
status = B_OK;
|
|
|
|
|
2004-06-08 09:58:23 +04:00
|
|
|
if (status == B_OK) {
|
|
|
|
inc_module_ref_count(module);
|
2002-11-29 11:33:57 +03:00
|
|
|
*_info = module->info;
|
2004-06-08 09:58:23 +04:00
|
|
|
}
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2004-06-08 09:58:23 +04:00
|
|
|
recursive_lock_unlock(&sModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
return status;
|
|
|
|
|
|
|
|
err:
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_unlock(&sModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
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));
|
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_lock(&sModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
module = (struct module *)hash_lookup(sModulesHash, path);
|
2002-11-29 11:33:57 +03:00
|
|
|
if (module == NULL) {
|
|
|
|
FATAL(("module: We don't seem to have a reference to module %s\n", path));
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_unlock(&sModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
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
|
|
|
|
2004-04-27 01:02:27 +04:00
|
|
|
if ((module->flags & B_BUILT_IN_MODULE) == 0)
|
|
|
|
put_module_image(module->module_image);
|
2002-11-29 11:33:57 +03:00
|
|
|
|
2004-05-10 04:09:21 +04:00
|
|
|
recursive_lock_unlock(&sModulesLock);
|
2002-11-29 11:33:57 +03:00
|
|
|
return B_OK;
|
2002-07-09 16:24:59 +04:00
|
|
|
}
|