summaryrefslogtreecommitdiff
path: root/source4/lib/util/module.c
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2006-02-23 15:52:24 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:52:05 -0500
commitdfc517b05395d925a4d7b1ce9633a849f9468e70 (patch)
treea81c020c89022980e6fbb68116648da217fbb0fc /source4/lib/util/module.c
parent80c8a522861068e7b0974986936f65052dd6f70f (diff)
downloadsamba-dfc517b05395d925a4d7b1ce9633a849f9468e70.tar.gz
samba-dfc517b05395d925a4d7b1ce9633a849f9468e70.tar.bz2
samba-dfc517b05395d925a4d7b1ce9633a849f9468e70.zip
r13658: More moving around of files:
- Collect the generic utility functions into a lib/util/ (a la GLib is for the GNOME folks) - Remove even more files from include/ (This used to be commit ba62880f5b05c2a505dc7f54676b231197a7e707)
Diffstat (limited to 'source4/lib/util/module.c')
-rw-r--r--source4/lib/util/module.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/source4/lib/util/module.c b/source4/lib/util/module.c
new file mode 100644
index 0000000000..672d8df7ce
--- /dev/null
+++ b/source4/lib/util/module.c
@@ -0,0 +1,97 @@
+/*
+ Unix SMB/CIFS implementation.
+
+ Copyright (C) Jelmer Vernooij 2005
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "system/dir.h"
+
+static void *load_module(TALLOC_CTX *mem_ctx, const char *dir, const char *name)
+{
+ char *path;
+ void *handle;
+ void *init_fn;
+
+ path = talloc_asprintf(mem_ctx, "%s/%s", dir, name);
+
+ handle = dlopen(path, RTLD_NOW);
+ if (handle == NULL) {
+ DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
+ talloc_free(path);
+ return NULL;
+ }
+
+ init_fn = dlsym(handle, "init_module");
+
+ if (init_fn == NULL) {
+ DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror()));
+ DEBUG(1, ("Loading module '%s' failed\n", path));
+ dlclose(handle);
+ talloc_free(path);
+ return NULL;
+ }
+
+ talloc_free(path);
+
+ return init_fn;
+}
+
+init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
+{
+ DIR *dir;
+ struct dirent *entry;
+ int success = 0;
+ init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
+
+ ret[0] = NULL;
+
+ dir = opendir(path);
+ if (dir == NULL) {
+ talloc_free(ret);
+ return NULL;
+ }
+
+ while((entry = readdir(dir))) {
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+
+ ret[success] = load_module(mem_ctx, path, entry->d_name);
+ if (ret[success]) {
+ ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
+ success++;
+ ret[success] = NULL;
+ }
+ }
+
+ closedir(dir);
+
+ return ret;
+}
+
+BOOL run_init_functions(NTSTATUS (**fns) (void))
+{
+ int i;
+ BOOL ret;
+
+ if (fns == NULL)
+ return True;
+
+ for (i = 0; fns[i]; i++) { ret &= NT_STATUS_IS_OK(fns[i]()); }
+
+ return ret;
+}