The branch, master has been updated via f6b7bd4 lib/util: consolidate module loading via 355b5e3 modules: standardise on samba_init_module as the hook symbol to resolve via a718b5a lib/util: Use only init_module_fn typedef in module loading via c894527 lib/util: Remove unused module loading functions via 7d33ec3 lib/util: consolidate module loading into common code via 040055b s3-module allow libreplace to provide dlopen replacement from 3fca66e samba-tool: add support for fixing broken backlinks in dbcheck
http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit f6b7bd4a01205e442d3aa06d8edc3634a12945b2 Author: Andrew Bartlett <abart...@samba.org> Date: Fri Sep 9 22:41:28 2011 +1000 lib/util: consolidate module loading Autobuild-User: Andrew Bartlett <abart...@samba.org> Autobuild-Date: Thu Oct 6 08:52:30 CEST 2011 on sn-devel-104 commit 355b5e3a831415d9bef97de4b2d83e353de4ab0f Author: Andrew Bartlett <abart...@samba.org> Date: Fri Sep 9 22:25:43 2011 +1000 modules: standardise on samba_init_module as the hook symbol to resolve commit a718b5a4f1ebfb63aec2f43b654d8e97f099fe85 Author: Andrew Bartlett <abart...@samba.org> Date: Fri Sep 9 22:18:42 2011 +1000 lib/util: Use only init_module_fn typedef in module loading commit c8945279e72aee5511739b2ce0d7ddd5535fd1cb Author: Andrew Bartlett <abart...@samba.org> Date: Fri Sep 9 22:10:12 2011 +1000 lib/util: Remove unused module loading functions commit 7d33ec3dfe78723d62f4941684060baeb9c4bda6 Author: Andrew Bartlett <abart...@samba.org> Date: Fri Sep 9 21:24:49 2011 +1000 lib/util: consolidate module loading into common code This creates a samba-modules private libary that handles the details. Andrew Bartlett commit 040055bb6a578516007ab6f56ebe2ee77b0f8605 Author: Andrew Bartlett <abart...@samba.org> Date: Fri Sep 9 20:29:32 2011 +1000 s3-module allow libreplace to provide dlopen replacement Given that we have replacement dlopen() etc, use this from libreplace. The dlerror() from libreplace gives a good error if the platform really does not have dlopen(). Andrew Bartlett ----------------------------------------------------------------------- Summary of changes: lib/util/modules.c | 240 +++++++++++++++++++++++++++++++++++ lib/util/samba_modules.h | 59 +++++++++ lib/util/wscript_build | 6 + source3/Makefile.in | 2 +- source3/exports/modules-darwin.syms | 2 +- source3/include/includes.h | 2 +- source3/include/module.h | 28 ---- source3/include/proto.h | 11 +-- source3/lib/module.c | 144 --------------------- source3/lib/util.c | 9 ++ source3/m4/aclocal.m4 | 2 +- source3/modules/perfcount_test.c | 2 +- source3/wscript | 2 +- source3/wscript_build | 2 +- source4/auth/gensec/gensec_start.c | 1 + source4/auth/gensec/wscript_build | 2 +- source4/auth/ntlm/auth.c | 1 + source4/auth/ntlm/wscript_build | 2 +- source4/ntptr/ntptr_base.c | 1 + source4/ntvfs/ntvfs_base.c | 1 + source4/ntvfs/posix/pvfs_acl.c | 1 + source4/ntvfs/posix/wscript_build | 2 +- source4/ntvfs/sysdep/sys_lease.c | 1 + source4/ntvfs/sysdep/sys_notify.c | 1 + source4/ntvfs/wscript_build | 2 +- source4/param/param.h | 32 ----- source4/param/share.c | 1 + source4/param/util.c | 104 --------------- source4/param/wscript_build | 2 +- source4/rpc_server/dcerpc_server.c | 1 + source4/smbd/process_model.c | 1 + source4/smbd/server.c | 1 + source4/smbd/wscript_build | 2 +- source4/torture/smbtorture.c | 3 +- source4/torture/torture.c | 1 + 35 files changed, 342 insertions(+), 332 deletions(-) create mode 100644 lib/util/modules.c create mode 100644 lib/util/samba_modules.h delete mode 100644 source3/include/module.h Changeset truncated at 500 lines: diff --git a/lib/util/modules.c b/lib/util/modules.c new file mode 100644 index 0000000..52a04be --- /dev/null +++ b/lib/util/modules.c @@ -0,0 +1,240 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + Copyright (C) Jelmer Vernooij 2002-2003,2005-2007 + Copyright (C) Stefan (metze) Metzmacher 2003 + Copyright (C) Andrew Bartlett 2011 + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "dynconfig/dynconfig.h" +#include "lib/util/samba_modules.h" +#include "system/filesys.h" +#include "system/dir.h" + +/** + * Obtain the init function from a shared library file + */ +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) { + int level = is_probe ? 5 : 0; + DEBUG(level, ("Error loading module '%s': %s\n", path, error ? error : "")); + return NULL; + } + + 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", + SAMBA_INIT_MODULE, path, dlerror())); + DEBUG(1, ("Loading module '%s' failed\n", path)); + dlclose(handle); + return NULL; + } + + if (handle_out) { + *handle_out = handle; + } + + return (init_module_fn)init_fn; +} + +/** + * Obtain list of init functions from the modules in the specified + * directory + */ +static init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path) +{ + DIR *dir; + struct dirent *entry; + char *filename; + 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 (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name)) + continue; + + filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name); + + ret[success] = load_module(filename, true, NULL); + if (ret[success]) { + ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2); + success++; + ret[success] = NULL; + } + + talloc_free(filename); + } + + closedir(dir); + + return ret; +} + +/** + * Run the specified init functions. + * + * @return true if all functions ran successfully, false otherwise + */ +bool run_init_functions(init_module_fn *fns) +{ + int i; + bool ret = true; + + if (fns == NULL) + return true; + + for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); } + + return ret; +} + +/** + * Load the initialization functions from DSO files for a specific subsystem. + * + * Will return an array of function pointers to initialization functions + */ + +init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem) +{ + char *path = modules_path(mem_ctx, subsystem); + init_module_fn *ret; + + ret = load_modules(mem_ctx, path); + + talloc_free(path); + + return ret; +} + + +/* Load a dynamic module. Only log a level 0 error if we are not checking + for the existence of a module (probling). */ + +static NTSTATUS do_smb_load_module(const char *module_name, bool is_probe) +{ + void *handle; + init_module_fn init; + NTSTATUS status; + + init = load_module(module_name, is_probe, &handle); + if (!init) { + return NT_STATUS_UNSUCCESSFUL; + } + + DEBUG(2, ("Module '%s' loaded\n", module_name)); + + status = init(); + if (!NT_STATUS_IS_OK(status)) { + DEBUG(0, ("Module '%s' initialization failed: %s\n", + module_name, get_friendly_nt_error_msg(status))); + dlclose(handle); + } + + return status; +} + +/* Load all modules in list and return number of + * modules that has been successfully loaded */ +int smb_load_modules(const char **modules) +{ + int i; + int success = 0; + + for(i = 0; modules[i]; i++){ + if(NT_STATUS_IS_OK(do_smb_load_module(modules[i], false))) { + success++; + } + } + + DEBUG(2, ("%d modules successfully loaded\n", success)); + + return success; +} + +NTSTATUS smb_probe_module(const char *subsystem, const char *module) +{ + char *full_path = NULL; + TALLOC_CTX *ctx = talloc_stackframe(); + NTSTATUS status; + + /* Check for absolute path */ + + /* if we make any 'samba multibyte string' + calls here, we break + for loading string modules */ + + DEBUG(5, ("Probing module '%s'\n", module)); + + if (module[0] == '/') { + status = do_smb_load_module(module, true); + TALLOC_FREE(ctx); + return status; + } + + full_path = talloc_asprintf(ctx, + "%s/%s.%s", + modules_path(ctx, subsystem), + module, + shlib_ext()); + if (!full_path) { + TALLOC_FREE(ctx); + return NT_STATUS_NO_MEMORY; + } + + DEBUG(5, ("Probing module '%s': Trying to load from %s\n", + module, full_path)); + + status = do_smb_load_module(full_path, true); + + TALLOC_FREE(ctx); + return status; +} diff --git a/lib/util/samba_modules.h b/lib/util/samba_modules.h new file mode 100644 index 0000000..5eb2a0d --- /dev/null +++ b/lib/util/samba_modules.h @@ -0,0 +1,59 @@ +/* + Unix SMB/CIFS implementation. + Handling of idle/exit events + Copyright (C) Stefan (metze) Metzmacher 2003 + Copyright (C) Andrew Bartlett 2011 + + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef _SAMBA_MODULES_H +#define _SAMBA_MODULES_H + +/* Module support */ +typedef NTSTATUS (*init_module_fn) (void); + +NTSTATUS samba_init_module(void); + +/* this needs to be a string which is not in the C library. We + previously used "init_module", but that meant that modules which + did not define this function ended up calling the C library + function init_module() which makes a system call */ +#define SAMBA_INIT_MODULE "samba_init_module" + +/** + * 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(const char *path, bool is_probe, void **handle); + +/** + * Run the specified init functions. + * + * @return true if all functions ran successfully, false otherwise + */ +bool run_init_functions(init_module_fn *fns); + +/** + * Load the initialization functions from DSO files for a specific subsystem. + * + * Will return an array of function pointers to initialization functions + */ +init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem); + +int smb_load_modules(const char **modules); +NTSTATUS smb_probe_module(const char *subsystem, const char *module); + +#endif /* _SAMBA_MODULES_H */ diff --git a/lib/util/wscript_build b/lib/util/wscript_build index 70a639d..f4bd885 100755 --- a/lib/util/wscript_build +++ b/lib/util/wscript_build @@ -17,6 +17,12 @@ bld.SAMBA_LIBRARY('samba-util', pc_files='samba-util.pc' ) +bld.SAMBA_LIBRARY('samba-modules', + source='modules.c', + deps='errors samba-util', + local_include=False, + private_library=True) + bld.SAMBA_LIBRARY('asn1util', source='asn1.c', deps='talloc samba-util', diff --git a/source3/Makefile.in b/source3/Makefile.in index c736ae8..a048090 100644 --- a/source3/Makefile.in +++ b/source3/Makefile.in @@ -464,7 +464,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) $(LIBTSOCKET_OBJ) \ ../lib/util/charset/charset_macosxfs.o intl/lang_tdb.o \ lib/conn_tdb.o lib/adt_tree.o lib/gencache.o \ lib/sessionid_tdb.o \ - lib/module.o lib/events.o @LIBTEVENT_OBJ0@ \ + ../lib/util/modules.o lib/events.o @LIBTEVENT_OBJ0@ \ @CCAN_OBJ@ \ lib/server_contexts.o \ lib/server_prefork.o \ diff --git a/source3/exports/modules-darwin.syms b/source3/exports/modules-darwin.syms index be45761..276543b 100644 --- a/source3/exports/modules-darwin.syms +++ b/source3/exports/modules-darwin.syms @@ -1 +1 @@ -_init_samba_module +_samba_init_module diff --git a/source3/include/includes.h b/source3/include/includes.h index b644089..786fb4e 100644 --- a/source3/include/includes.h +++ b/source3/include/includes.h @@ -524,7 +524,7 @@ typedef char fstring[FSTRING_LEN]; #include "smb.h" #include "../lib/util/byteorder.h" -#include "module.h" +#include "../lib/util/samba_modules.h" #include "../lib/util/talloc_stack.h" #include "../lib/util/smb_threads.h" #include "../lib/util/smb_threads_internal.h" diff --git a/source3/include/module.h b/source3/include/module.h deleted file mode 100644 index 667fac6..0000000 --- a/source3/include/module.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - Unix SMB/CIFS implementation. - Handling of idle/exit events - Copyright (C) Stefan (metze) Metzmacher 2003 - - 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 3 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, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef _MODULE_H -#define _MODULE_H - -/* Module support */ -typedef NTSTATUS (init_module_function) (void); - -NTSTATUS init_samba_module(void); - -#endif /* _MODULE_H */ diff --git a/source3/include/proto.h b/source3/include/proto.h index f6e7236..ffc90ff 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -185,16 +185,6 @@ void init_ldap_debugging(void); char *escape_ldap_string(TALLOC_CTX *mem_ctx, const char *s); char *escape_rdn_val_string_alloc(const char *s); -/* The following definitions come from lib/module.c */ - -NTSTATUS smb_load_module(const char *module_name); -int smb_load_modules(const char **modules); -NTSTATUS smb_probe_module(const char *subsystem, const char *module); -NTSTATUS smb_load_module(const char *module_name); -int smb_load_modules(const char **modules); -NTSTATUS smb_probe_module(const char *subsystem, const char *module); -void init_modules(void); - /* The following definitions come from lib/ms_fnmatch.c */ int ms_fnmatch(const char *pattern, const char *string, bool translate_pattern, @@ -604,6 +594,7 @@ bool map_open_params_to_ntcreate(const char *smb_base_fname, uint32 *pcreate_disposition, uint32 *pcreate_options, uint32_t *pprivate_flags); +void init_modules(void); /* The following definitions come from lib/util_cmdline.c */ diff --git a/source3/lib/module.c b/source3/lib/module.c index bec4fdd..a85d7d0 100644 --- a/source3/lib/module.c +++ b/source3/lib/module.c @@ -20,147 +20,3 @@ */ #include "includes.h" - -#ifdef HAVE_DLOPEN - -/* Load a dynamic module. Only log a level 0 error if we are not checking - for the existence of a module (probling). */ - -static NTSTATUS do_smb_load_module(const char *module_name, bool is_probe) -{ - void *handle; - init_module_function *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_function *)dlsym(handle, "init_samba_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 'init_samba_module' " - "in %s: %s\n", module_name, error)); - dlclose(handle); - return NT_STATUS_UNSUCCESSFUL; - } - - DEBUG(2, ("Module '%s' loaded\n", module_name)); - - status = init(); - if (!NT_STATUS_IS_OK(status)) { - DEBUG(0, ("Module '%s' initialization failed: %s\n", - module_name, get_friendly_nt_error_msg(status))); - dlclose(handle); - } - - return status; -} - -NTSTATUS smb_load_module(const char *module_name) -{ - return do_smb_load_module(module_name, False); -} - -/* Load all modules in list and return number of - * modules that has been successfully loaded */ -int smb_load_modules(const char **modules) -{ - int i; - int success = 0; - - for(i = 0; modules[i]; i++){ - if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) { - success++; -- Samba Shared Repository