1
0
mirror of https://github.com/samba-team/samba.git synced 2025-12-03 04:23:50 +03:00

r14992: Allow load_module() to be used externally

This commit is contained in:
Jelmer Vernooij
2006-04-08 13:44:40 +00:00
committed by Gerald (Jerry) Carter
parent eb34d5b8b1
commit a9d5d7ab58

View File

@@ -26,18 +26,17 @@
#include "includes.h" #include "includes.h"
#include "system/dir.h" #include "system/dir.h"
static void *load_module(TALLOC_CTX *mem_ctx, const char *dir, const char *name) /**
* Obtain the init function from a shared library file
*/
_PUBLIC_ init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
{ {
char *path;
void *handle; void *handle;
void *init_fn; void *init_fn;
path = talloc_asprintf(mem_ctx, "%s/%s", dir, name);
handle = dlopen(path, RTLD_NOW); handle = dlopen(path, RTLD_NOW);
if (handle == NULL) { if (handle == NULL) {
DEBUG(0, ("Unable to open %s: %s\n", path, dlerror())); DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
talloc_free(path);
return NULL; return NULL;
} }
@@ -47,13 +46,10 @@ static void *load_module(TALLOC_CTX *mem_ctx, const char *dir, const char *name)
DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror())); DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror()));
DEBUG(1, ("Loading module '%s' failed\n", path)); DEBUG(1, ("Loading module '%s' failed\n", path));
dlclose(handle); dlclose(handle);
talloc_free(path);
return NULL; return NULL;
} }
talloc_free(path); return (init_module_fn)init_fn;
return init_fn;
} }
/** /**
@@ -64,6 +60,7 @@ _PUBLIC_ init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
{ {
DIR *dir; DIR *dir;
struct dirent *entry; struct dirent *entry;
char *filename;
int success = 0; int success = 0;
init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2); init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
@@ -79,12 +76,16 @@ _PUBLIC_ init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue; continue;
ret[success] = load_module(mem_ctx, path, entry->d_name); filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
ret[success] = load_module(mem_ctx, filename);
if (ret[success]) { if (ret[success]) {
ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2); ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
success++; success++;
ret[success] = NULL; ret[success] = NULL;
} }
talloc_free(filename);
} }
closedir(dir); closedir(dir);