lib/util: consolidate module loading
authorAndrew Bartlett <abartlet@samba.org>
Fri, 9 Sep 2011 12:41:28 +0000 (22:41 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 6 Oct 2011 06:52:30 +0000 (08:52 +0200)
Autobuild-User: Andrew Bartlett <abartlet@samba.org>
Autobuild-Date: Thu Oct  6 08:52:30 CEST 2011 on sn-devel-104

lib/util/modules.c
lib/util/samba_modules.h
source4/torture/smbtorture.c

index 2c4622510ba6818a23ac1ec79edb2c1ebd7d2ede..52a04be457468d2aa38f52b69e11d8b401b3f833 100644 (file)
 /**
  * Obtain the init function from a shared library file
  */
-init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
+init_module_fn load_module(const char *path, bool is_probe, void **handle_out)
 {
        void *handle;
        void *init_fn;
+       char *error;
 
+#if _SAMBA_BUILD_ == 3
+       /* Always try to use LAZY symbol resolving; if the plugin has
+        * backwards compatibility, there might be symbols in the
+        * plugin referencing to old (removed) functions
+        */
+       handle = dlopen(path, RTLD_LAZY);
+#else
+       /* This should be a WAF build, where modules should be built
+        * with no undefined symbols and are already linked against
+        * the libraries that they are loaded by */
        handle = dlopen(path, RTLD_NOW);
+#endif
+
+       /* This call should reset any possible non-fatal errors that
+          occured since last call to dl* functions */
+       error = dlerror();
+
        if (handle == NULL) {
-               DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
+               int level = is_probe ? 5 : 0;
+               DEBUG(level, ("Error loading module '%s': %s\n", path, error ? error : ""));
                return NULL;
        }
 
-       init_fn = dlsym(handle, SAMBA_INIT_MODULE);
+       init_fn = (init_module_fn)dlsym(handle, SAMBA_INIT_MODULE);
+
+       /* we could check dlerror() to determine if it worked, because
+           dlsym() can validly return NULL, but what would we do with
+           a NULL pointer as a module init function? */
 
        if (init_fn == NULL) {
                DEBUG(0, ("Unable to find %s() in %s: %s\n",
@@ -49,6 +71,10 @@ init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
                return NULL;
        }
 
+       if (handle_out) {
+               *handle_out = handle;
+       }
+
        return (init_module_fn)init_fn;
 }
 
@@ -78,7 +104,7 @@ static init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
 
                filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
 
-               ret[success] = load_module(mem_ctx, filename);
+               ret[success] = load_module(filename, true, NULL);
                if (ret[success]) {
                        ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
                        success++;
@@ -138,33 +164,9 @@ static NTSTATUS do_smb_load_module(const char *module_name, bool is_probe)
        void *handle;
        init_module_fn init;
        NTSTATUS status;
-       const char *error;
 
-       /* Always try to use LAZY symbol resolving; if the plugin has
-        * backwards compatibility, there might be symbols in the
-        * plugin referencing to old (removed) functions
-        */
-       handle = dlopen(module_name, RTLD_LAZY);
-
-       /* This call should reset any possible non-fatal errors that
-          occured since last call to dl* functions */
-       error = dlerror();
-
-       if(!handle) {
-               int level = is_probe ? 3 : 0;
-               DEBUG(level, ("Error loading module '%s': %s\n", module_name, error ? error : ""));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       init = (init_module_fn)dlsym(handle, SAMBA_INIT_MODULE);
-
-       /* we must check dlerror() to determine if it worked, because
-           dlsym() can validly return NULL */
-       error = dlerror();
-       if (error) {
-               DEBUG(0, ("Error trying to resolve symbol '" SAMBA_INIT_MODULE
-                         "' in %s: %s\n", module_name, error));
-               dlclose(handle);
+       init = load_module(module_name, is_probe, &handle);
+       if (!init) {
                return NT_STATUS_UNSUCCESSFUL;
        }
 
index ae7895eb1240efb3ba81a74f969da015f5ae3cb0..5eb2a0dd1cfa3a305928ecbede402aff7927d01f 100644 (file)
@@ -33,9 +33,11 @@ NTSTATUS samba_init_module(void);
 #define SAMBA_INIT_MODULE "samba_init_module"
 
 /**
- * Obtain the init function from a shared library file
+ * Obtain the init function from a shared library file.  
+ *
+ * The handle to dlclose() in case of error is returns in *handle if handle is not NULL
  */
-init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path);
+init_module_fn load_module(const char *path, bool is_probe, void **handle);
 
 /**
  * Run the specified init functions.
index 81ae11253c938d55b685cc5771523df33016a4ab..934e0a7b2fa4b06d5cd84d1f3c6f35cd967db83b 100644 (file)
@@ -602,7 +602,7 @@ int main(int argc,char *argv[])
        }
 
        if (extra_module != NULL) {
-           init_module_fn fn = load_module(talloc_autofree_context(), poptGetOptArg(pc));
+               init_module_fn fn = load_module(poptGetOptArg(pc), false, NULL);
 
                if (fn == NULL) 
                        d_printf("Unable to load module from %s\n", poptGetOptArg(pc));