src/module.c
changeset 108 50ff7ac8a725
parent 103 454aea1e4f11
child 110 43e9a7984955
--- a/src/module.c	Wed Apr 01 17:29:29 2009 +0300
+++ b/src/module.c	Wed Apr 01 18:30:47 2009 +0300
@@ -2,6 +2,7 @@
 #include "log.h"
 
 #include <stdlib.h>
+#include <unistd.h>
 #include <dlfcn.h>
 #include <string.h>
 #include <assert.h>
@@ -26,6 +27,17 @@
     return SUCCESS;
 }
 
+const char* modules_path (struct modules *modules, const char *path)
+{
+    const char *old_path = modules->path;
+
+    if (path)
+        // replace
+        modules->path = path;
+
+    return old_path;
+}
+
 const char* module_name (struct module *module)
 {
     return module->info.name;
@@ -53,27 +65,105 @@
     return SUCCESS;
 }
 
-err_t module_load (struct modules *modules, struct module **module_ptr, const struct module_info *info, struct error_info *err)
+/**
+ * XXX: ugly function to format to a dynamically allocated string
+ */
+char *strfmt (const char *fmt, ...)
+{
+    va_list vargs;
+    size_t len;
+    char *buf;
+
+    // figure out the length of the resulting string
+    va_start(vargs, fmt);
+
+    len = vsnprintf(NULL, 0, fmt, vargs) + 1;
+
+    va_end(vargs);
+
+    // malloc
+    if ((buf = malloc(len)) == NULL)
+        return NULL;
+
+    // format
+    va_start(vargs, fmt);
+
+    vsnprintf(buf, len, fmt, vargs);
+
+    va_end(vargs);
+
+    // ok
+    return buf;
+}
+
+/**
+ * Looks up a module's path by name, returning a dynamically allocated string with the path on success
+ */
+static char* module_find (struct modules *modules, struct module_info *info, struct error_info *err)
+{
+    char *path = NULL;
+
+    // build the path...
+    if ((path = strfmt("%s/mod_%s.so", modules->path, info->name)) == NULL)
+        JUMP_SET_ERROR(err, ERR_STRDUP);
+
+    // exists and readable?
+    if (access(path, R_OK))
+        // XXX: this doesn't contain the path...
+        JUMP_SET_ERROR_STR(err, ERR_MODULE_PATH, "module not found in search path");
+    
+    // ok
+    return path;
+
+error:
+    // release the dynamically allocated path
+    free(path);
+
+    return NULL;
+}
+
+err_t module_load (struct modules *modules, struct module **module_ptr, const struct module_info *_info, struct error_info *err)
 {
     struct module *module;
 
     // validate the module name
-    if (strlen(info->name) > MODULE_NAME_MAX)
+    if (strlen(_info->name) > MODULE_NAME_MAX)
         return SET_ERROR(err, ERR_MODULE_NAME);
     
+    // already open with the same name?
+    if (module_get(modules, _info->name))
+        return SET_ERROR(err, ERR_MODULE_DUP);
+
     // alloc
     if ((module = calloc(1, sizeof(*module))) == NULL)
         return SET_ERROR(err, ERR_CALLOC);
     
     // store
-    module->info = *info;
+    module->info = *_info;
     module->modules = modules;
+ 
+    // add to modules list
+    TAILQ_INSERT_TAIL(&modules->list, module, modules_list);
+   
+    // lookup path?
+    if (!module->info.path) {
+        // have a search path?
+        if (!modules->path)
+            JUMP_SET_ERROR_STR(err, ERR_MODULE_PATH, "no module search path defined");
+
+        // try and resolve the path automatically
+        if ((module->path_buf = module_find(modules, &module->info, err)) == NULL)
+            goto error;
+        
+        // use that path
+        module->info.path = module->path_buf;
+    }
 
     // clear dlerrors
     (void) dlerror();
 
     // load it
-    if ((module->handle = dlopen(info->path, RTLD_NOW)) == NULL)
+    if ((module->handle = dlopen(module->info.path, RTLD_NOW)) == NULL)
         JUMP_SET_ERROR_STR(err, ERR_MODULE_OPEN, dlerror());
     
     // load the funcs symbol
@@ -84,9 +174,6 @@
     if ((module->desc->init(modules->nexus, &module->ctx, err)))
         goto error;
 
-    // add to modules list
-    TAILQ_INSERT_TAIL(&modules->list, module, modules_list);
-
     // ok
     if (module_ptr)
         *module_ptr = module;
@@ -94,8 +181,8 @@
     return SUCCESS;
 
 error:
-    // XXX: cleanup
-    free(module);
+    // cleanup
+    module_destroy(module);
 
     return ERROR_CODE(err);    
 }
@@ -172,9 +259,12 @@
     TAILQ_REMOVE(&module->modules->list, module, modules_list);
 
     // unload the dl handle
-    if (dlclose(module->handle))
+    if (module->handle && dlclose(module->handle))
         log_error("dlclose(%s): %s", module->info.name, dlerror());
     
+    // release the path buf, if any
+    free(module->path_buf);
+
     // free the module info
     free(module);
 }