* Preparations for a module listener API - this will be used in the (disk)

device managers.
* Cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@26239 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2008-07-03 11:52:36 +00:00
parent 9380c00233
commit 0875ae984f
2 changed files with 344 additions and 151 deletions
+11 -2
View File
@@ -1,6 +1,5 @@
/* /*
* Copyright 2005, Axel Dörfler, [email protected]. All rights reserved. * Copyright 2005-2008, Axel Dörfler, [email protected].
*
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _KERNEL_MODULE_H #ifndef _KERNEL_MODULE_H
@@ -14,6 +13,16 @@ struct kernel_args;
#ifdef __cplusplus #ifdef __cplusplus
// C++ only part
class NotificationListener;
extern status_t start_watching_modules(const char *prefix,
NotificationListener &listener);
extern status_t stop_watching_modules(const char *prefix,
NotificationListener &listener);
extern "C" { extern "C" {
#endif #endif
+222 -38
View File
@@ -17,16 +17,18 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <NodeMonitor.h>
#include <boot_device.h> #include <boot_device.h>
#include <elf.h>
#include <lock.h>
#include <vfs.h>
#include <boot/elf.h> #include <boot/elf.h>
#include <elf.h>
#include <fs/KPath.h> #include <fs/KPath.h>
#include <lock.h>
#include <Notifications.h>
#include <safemode.h> #include <safemode.h>
#include <util/AutoLock.h> #include <util/AutoLock.h>
#include <util/khash.h> #include <util/khash.h>
#include <vfs.h>
//#define TRACE_MODULE //#define TRACE_MODULE
@@ -74,17 +76,17 @@ enum module_state {
/* Each loaded module image (which can export several modules) is put /* Each loaded module image (which can export several modules) is put
* in a hash (gModuleImagesHash) to be easily found when you search * in a hash (gModuleImagesHash) to be easily found when you search
* for a specific file name. * for a specific file name.
* ToDo: Could use only the inode number for hashing. Would probably be * 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. * a little bit slower, but would lower the memory foot print quite a lot.
*/ */
struct module_image { struct module_image {
struct module_image* next; struct module_image* next;
module_info **info; /* the module_info we use */ module_info** info; // the module_info we use
module_dependency* dependencies; module_dependency* dependencies;
char *path; /* the full path for the module */ char* path; // the full path for the module
image_id image; image_id image;
int32 ref_count; /* how many ref's to this file */ int32 ref_count; // how many ref's to this file
bool keep_loaded; bool keep_loaded;
}; };
@@ -98,9 +100,9 @@ struct module {
char* name; char* name;
char* file; char* file;
int32 ref_count; int32 ref_count;
module_info *info; /* will only be valid if ref_count > 0 */ module_info* info; // will only be valid if ref_count > 0
int32 offset; /* this is the offset in the headers */ int32 offset; // this is the offset in the headers
module_state state; /* state of module */ module_state state;
uint32 flags; uint32 flags;
}; };
@@ -123,8 +125,8 @@ typedef struct module_iterator {
DIR* current_dir; DIR* current_dir;
status_t status; status_t status;
int32 module_offset; int32 module_offset;
/* This is used to keep track of which module_info // This is used to keep track of which module_info
* within a module we're addressing. */ // within a module we're addressing.
::module_image* module_image; ::module_image* module_image;
module_info** current_header; module_info** current_header;
const char* current_path; const char* current_path;
@@ -134,15 +136,57 @@ typedef struct module_iterator {
bool loaded_modules; bool loaded_modules;
} module_iterator; } module_iterator;
namespace Module {
static bool sDisableUserAddOns = false; struct module_listener : DoublyLinkedListLink<module_listener> {
NotificationListener* listener;
const char* prefix;
};
/* locking scheme: there is a global lock only; having several locks typedef DoublyLinkedList<module_listener> ModuleListenerList;
* makes trouble if dependent modules get loaded concurrently ->
* they have to wait for each other, i.e. we need one lock per module; class ModuleNotificationService : public NotificationService {
* also we must detect circular references during init and not dead-lock public:
*/ ModuleNotificationService();
static recursive_lock sModulesLock; virtual ~ModuleNotificationService();
status_t InitCheck();
status_t AddListener(const KMessage* eventSpecifier,
NotificationListener& listener);
status_t UpdateListener(const KMessage* eventSpecifier,
NotificationListener& listener);
status_t RemoveListener(const KMessage* eventSpecifier,
NotificationListener& listener);
virtual const char* Name() { return "modules"; }
private:
recursive_lock fLock;
ModuleListenerList fListeners;
};
class DirectoryWatcher : public NotificationListener {
public:
DirectoryWatcher();
virtual ~DirectoryWatcher();
virtual void EventOccured(NotificationService& service,
const KMessage* event);
};
class ModuleWatcher : public NotificationListener {
public:
ModuleWatcher();
virtual ~ModuleWatcher();
virtual void EventOccured(NotificationService& service,
const KMessage* event);
};
} // namespace Module
using namespace Module;
/* These are the standard base paths where we start to look for modules /* 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 * to load. Order is important, the last entry here will be searched
@@ -158,6 +202,17 @@ static const uint32 kNumModulePaths = sizeof(kModulePaths)
/ sizeof(kModulePaths[0]); / sizeof(kModulePaths[0]);
static const uint32 kFirstNonSystemModulePath = 1; static const uint32 kFirstNonSystemModulePath = 1;
static ModuleNotificationService sModuleNotificationService;
static bool sDisableUserAddOns = false;
/* locking scheme: there is a global lock only; having several locks
* 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
*/
static recursive_lock sModulesLock;
/* We store the loaded modules by directory path, and all known modules /* We store the loaded modules by directory path, and all known modules
* by module name in a hash table for quick access * by module name in a hash table for quick access
*/ */
@@ -246,7 +301,7 @@ load_module_image(const char *path, module_image **_moduleImage)
} }
moduleImage = (module_image*)malloc(sizeof(module_image)); moduleImage = (module_image*)malloc(sizeof(module_image));
if (!moduleImage) { if (moduleImage == NULL) {
status = B_NO_MEMORY; status = B_NO_MEMORY;
goto err; goto err;
} }
@@ -674,7 +729,8 @@ iterator_pop_path_from_stack(module_iterator *iterator, uint32 *_baseLength)
static status_t static status_t
iterator_push_path_on_stack(module_iterator *iterator, const char *path, uint32 baseLength) iterator_push_path_on_stack(module_iterator* iterator, const char* path,
uint32 baseLength)
{ {
if (iterator->stack_current + 1 > iterator->stack_size) { if (iterator->stack_current + 1 > iterator->stack_size) {
// allocate new space on the stack // allocate new space on the stack
@@ -772,7 +828,7 @@ nextPath:
return B_ENTRY_NOT_FOUND; return B_ENTRY_NOT_FOUND;
} }
free((void *)iterator->current_path); free((char*)iterator->current_path);
iterator->current_path = path; iterator->current_path = path;
iterator->current_dir = opendir(path); iterator->current_dir = opendir(path);
TRACE(("open directory at %s -> %p\n", path, iterator->current_dir)); TRACE(("open directory at %s -> %p\n", path, iterator->current_dir));
@@ -832,15 +888,15 @@ nextModuleImage:
return B_BUFFER_OVERFLOW; return B_BUFFER_OVERFLOW;
// find out if it's a directory or a file // find out if it's a directory or a file
struct stat st; struct stat stat;
if (stat(path.Path(), &st) < 0) if (::stat(path.Path(), &stat) < 0)
return errno; return errno;
iterator->current_module_path = strdup(path.Path()); iterator->current_module_path = strdup(path.Path());
if (iterator->current_module_path == NULL) if (iterator->current_module_path == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(stat.st_mode)) {
status = iterator_push_path_on_stack(iterator, status = iterator_push_path_on_stack(iterator,
iterator->current_module_path, iterator->path_base_length); iterator->current_module_path, iterator->path_base_length);
if (status < B_OK) if (status < B_OK)
@@ -850,14 +906,14 @@ nextModuleImage:
goto nextModuleImage; goto nextModuleImage;
} }
if (!S_ISREG(st.st_mode)) if (!S_ISREG(stat.st_mode))
return B_BAD_TYPE; return B_BAD_TYPE;
TRACE(("open module at %s\n", path.Path())); TRACE(("open module at %s\n", path.Path()));
status = get_module_image(path.Path(), &iterator->module_image); status = get_module_image(path.Path(), &iterator->module_image);
if (status < B_OK) { if (status < B_OK) {
free((void *)iterator->current_module_path); free((char*)iterator->current_module_path);
iterator->current_module_path = NULL; iterator->current_module_path = NULL;
goto nextModuleImage; goto nextModuleImage;
} }
@@ -870,7 +926,7 @@ nextModuleImage:
while (*iterator->current_header != NULL) { while (*iterator->current_header != NULL) {
module_info* info = *iterator->current_header; module_info* info = *iterator->current_header;
// TODO: we might want to create a module here and cache it in the // TODO: we mightS want to create a module here and cache it in the
// hash table // hash table
iterator->current_header++; iterator->current_header++;
@@ -887,7 +943,7 @@ nextModuleImage:
// leave this module and get the next one // leave this module and get the next one
iterator->current_header = NULL; iterator->current_header = NULL;
free((void *)iterator->current_module_path); free((char*)iterator->current_module_path);
iterator->current_module_path = NULL; iterator->current_module_path = NULL;
put_module_image(iterator->module_image); put_module_image(iterator->module_image);
@@ -951,7 +1007,8 @@ register_preloaded_module_image(struct preloaded_image *image)
// (it always assumes the preloaded add-ons to be in the system // (it always assumes the preloaded add-ons to be in the system
// directory) // directory)
char path[B_FILE_NAME_LENGTH]; char path[B_FILE_NAME_LENGTH];
const char *name, *suffix; const char* suffix;
const char* name;
if (moduleImage->info[0] if (moduleImage->info[0]
&& (suffix = strstr(name = moduleImage->info[0]->name, && (suffix = strstr(name = moduleImage->info[0]->name,
image->name)) != NULL) { image->name)) != NULL) {
@@ -1042,6 +1099,108 @@ dump_modules(int argc, char **argv)
} }
// #pragma mark - DirectoryWatcher
DirectoryWatcher::DirectoryWatcher()
{
}
DirectoryWatcher::~DirectoryWatcher()
{
}
void
DirectoryWatcher::EventOccured(NotificationService& service,
const KMessage* event)
{
int32 opcode = event->GetInt32("opcode", -1);
dev_t device = event->GetInt32("device", -1);
ino_t directory = event->GetInt64("directory", -1);
const char *name = event->GetString("name", NULL);
if (opcode == B_ENTRY_MOVED) {
// Determine wether it's a move within, out of, or into one
// of our watched directories.
ino_t from = event->GetInt64("from directory", -1);
ino_t to = event->GetInt64("to directory", -1);
}
KPath path(B_PATH_NAME_LENGTH + 1);
if (path.InitCheck() != B_OK || vfs_entry_ref_to_path(device, directory,
name, path.LockBuffer(), path.BufferSize()) != B_OK)
return;
path.UnlockBuffer();
dprintf("module \"%s\" %s\n", path.Leaf(),
opcode == B_ENTRY_CREATED ? "added" : "removed");
}
// #pragma mark - ModuleWatcher
ModuleWatcher::ModuleWatcher()
{
}
ModuleWatcher::~ModuleWatcher()
{
}
void
ModuleWatcher::EventOccured(NotificationService& service, const KMessage* event)
{
if (event->GetInt32("opcode", -1) != B_STAT_CHANGED
|| (event->GetInt32("fields", 0) & B_STAT_MODIFICATION_TIME) == 0)
return;
}
// #pragma mark - ModuleNotificationService
ModuleNotificationService::ModuleNotificationService()
{
recursive_lock_init(&fLock, "module notifications");
}
ModuleNotificationService::~ModuleNotificationService()
{
recursive_lock_destroy(&fLock);
}
status_t
ModuleNotificationService::AddListener(const KMessage* eventSpecifier,
NotificationListener& listener)
{
return B_ERROR;
}
status_t
ModuleNotificationService::UpdateListener(const KMessage* eventSpecifier,
NotificationListener& listener)
{
return B_ERROR;
}
status_t
ModuleNotificationService::RemoveListener(const KMessage* eventSpecifier,
NotificationListener& listener)
{
return B_ERROR;
}
// #pragma mark - Exported Kernel API (private part) // #pragma mark - Exported Kernel API (private part)
@@ -1086,6 +1245,30 @@ load_module(const char *path, module_info ***_modules)
} }
status_t
start_watching_modules(const char* prefix, NotificationListener& listener)
{
KMessage specifier;
status_t status = specifier.AddString("prefix", prefix);
if (status != B_OK)
return status;
return sModuleNotificationService.AddListener(&specifier, listener);
}
status_t
stop_watching_modules(const char* prefix, NotificationListener& listener)
{
KMessage specifier;
status_t status = specifier.AddString("prefix", prefix);
if (status != B_OK)
return status;
return sModuleNotificationService.RemoveListener(&specifier, listener);
}
/*! Setup the module structures and data for use - must be called /*! Setup the module structures and data for use - must be called
before any other module call. before any other module call.
*/ */
@@ -1119,6 +1302,8 @@ module_init(kernel_args *args)
} }
} }
new(&sModuleNotificationService) ModuleNotificationService();
sDisableUserAddOns = get_safemode_boolean(B_SAFEMODE_DISABLE_USER_ADD_ONS, sDisableUserAddOns = get_safemode_boolean(B_SAFEMODE_DISABLE_USER_ADD_ONS,
false); false);
@@ -1152,7 +1337,7 @@ open_module_list_etc(const char *prefix, const char *suffix)
module_iterator* iterator = (module_iterator*)malloc( module_iterator* iterator = (module_iterator*)malloc(
sizeof(module_iterator)); sizeof(module_iterator));
if (!iterator) if (iterator == NULL)
return NULL; return NULL;
memset(iterator, 0, sizeof(module_iterator)); memset(iterator, 0, sizeof(module_iterator));
@@ -1248,7 +1433,7 @@ close_module_list(void *cookie)
// free stack // free stack
while ((path = iterator_pop_path_from_stack(iterator, NULL)) != NULL) while ((path = iterator_pop_path_from_stack(iterator, NULL)) != NULL)
free((void *)path); free((char*)path);
// close what have been left open // close what have been left open
if (iterator->module_image != NULL) if (iterator->module_image != NULL)
@@ -1258,8 +1443,8 @@ close_module_list(void *cookie)
closedir(iterator->current_dir); closedir(iterator->current_dir);
free(iterator->stack); free(iterator->stack);
free((void *)iterator->current_path); free((char*)iterator->current_path);
free((void *)iterator->current_module_path); free((char*)iterator->current_module_path);
free(iterator->prefix); free(iterator->prefix);
free(iterator); free(iterator);
@@ -1302,9 +1487,9 @@ read_next_module_name(void *cookie, char *buffer, size_t *_bufferSize)
/*! Iterates through all loaded modules, and stores its path in "buffer". /*! Iterates through all loaded modules, and stores its path in "buffer".
ToDo: check if the function in BeOS really does that (could also mean: TODO: check if the function in BeOS really does that (could also mean:
iterate through all modules that are currently loaded; have a valid iterate through all modules that are currently loaded; have a valid
module_image pointer, which would be hard to test for) module_image pointer)
*/ */
status_t status_t
get_next_loaded_module_name(uint32* _cookie, char* buffer, size_t* _bufferSize) get_next_loaded_module_name(uint32* _cookie, char* buffer, size_t* _bufferSize)
@@ -1326,8 +1511,7 @@ get_next_loaded_module_name(uint32 *_cookie, char *buffer, size_t *_bufferSize)
hash_iterator iterator; hash_iterator iterator;
hash_open(sModulesHash, &iterator); hash_open(sModulesHash, &iterator);
struct module *module = (struct module *)hash_next(sModulesHash, struct module* module = (struct module*)hash_next(sModulesHash, &iterator);
&iterator);
for (uint32 i = 0; module != NULL; i++) { for (uint32 i = 0; module != NULL; i++) {
if (i >= offset) { if (i >= offset) {