From 87354c9a6de95d5dcebace77a35fc21a73d599ab Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 24 Oct 2011 19:39:53 +1100 Subject: lib/util Split samba-modules library into public and private parts This will allow OpenChange to get at the symbols it needs, without exposing any more of this as a public API than we must. Andrew Bartlett --- lib/util/internal_module.c | 203 +++++++++++++++++++++++++++++++++++++ lib/util/internal_module.h | 42 ++++++++ lib/util/modules.c | 240 -------------------------------------------- lib/util/samba-module.pc.in | 11 ++ lib/util/samba_module.c | 62 ++++++++++++ lib/util/samba_module.h | 49 +++++++++ lib/util/samba_modules.h | 59 ----------- lib/util/wscript_build | 14 ++- 8 files changed, 378 insertions(+), 302 deletions(-) create mode 100644 lib/util/internal_module.c create mode 100644 lib/util/internal_module.h delete mode 100644 lib/util/modules.c create mode 100644 lib/util/samba-module.pc.in create mode 100644 lib/util/samba_module.c create mode 100644 lib/util/samba_module.h delete mode 100644 lib/util/samba_modules.h (limited to 'lib') diff --git a/lib/util/internal_module.c b/lib/util/internal_module.c new file mode 100644 index 0000000000..04c02f0760 --- /dev/null +++ b/lib/util/internal_module.c @@ -0,0 +1,203 @@ +/* + 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 . +*/ + +#include "includes.h" +#include "dynconfig/dynconfig.h" +#include "lib/util/internal_module.h" +#include "system/filesys.h" +#include "system/dir.h" + +/** + * Obtain the init function from a shared library file + */ +samba_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 = (samba_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 (samba_init_module_fn)init_fn; +} + +/** + * Obtain list of init functions from the modules in the specified + * directory + */ +samba_init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path) +{ + DIR *dir; + struct dirent *entry; + char *filename; + int success = 0; + samba_init_module_fn *ret = talloc_array(mem_ctx, samba_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, samba_init_module_fn, success+2); + success++; + ret[success] = NULL; + } + + talloc_free(filename); + } + + closedir(dir); + + 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; + samba_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/internal_module.h b/lib/util/internal_module.h new file mode 100644 index 0000000000..7d1acc52a3 --- /dev/null +++ b/lib/util/internal_module.h @@ -0,0 +1,42 @@ +/* + 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 . +*/ + +#ifndef _INTERNAL_MODULES_H +#define _INTERNAL_MODULES_H + +#include "lib/util/samba_module.h" + +/** + * 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 + */ +samba_init_module_fn load_module(const char *path, bool is_probe, void **handle); + +int smb_load_modules(const char **modules); +NTSTATUS smb_probe_module(const char *subsystem, const char *module); + +/** + * Obtain list of init functions from the modules in the specified + * directory + */ +samba_init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path); + +#endif /* _INTERNAL_MODULES_H */ diff --git a/lib/util/modules.c b/lib/util/modules.c deleted file mode 100644 index e9e6bf5dc8..0000000000 --- a/lib/util/modules.c +++ /dev/null @@ -1,240 +0,0 @@ -/* - 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 . -*/ - -#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 - */ -samba_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 = (samba_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 (samba_init_module_fn)init_fn; -} - -/** - * Obtain list of init functions from the modules in the specified - * directory - */ -static samba_init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path) -{ - DIR *dir; - struct dirent *entry; - char *filename; - int success = 0; - samba_init_module_fn *ret = talloc_array(mem_ctx, samba_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, samba_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 samba_init_module_fns_run(samba_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 - */ - -samba_init_module_fn *samba_modules_load(TALLOC_CTX *mem_ctx, const char *subsystem) -{ - char *path = modules_path(mem_ctx, subsystem); - samba_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; - samba_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-module.pc.in b/lib/util/samba-module.pc.in new file mode 100644 index 0000000000..8f22988cd0 --- /dev/null +++ b/lib/util/samba-module.pc.in @@ -0,0 +1,11 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: samba-modules +Description: Samba module loading utility functions +Requires: talloc +Version: 0.0.1 +Libs: @LIB_RPATH@ -L${libdir} -lsamba-modules +Cflags: -I${includedir} -DHAVE_IMMEDIATE_STRUCTURES=1 diff --git a/lib/util/samba_module.c b/lib/util/samba_module.c new file mode 100644 index 0000000000..f555eea8b6 --- /dev/null +++ b/lib/util/samba_module.c @@ -0,0 +1,62 @@ +/* + 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 . +*/ + +#include "includes.h" +#include "dynconfig/dynconfig.h" +#include "lib/util/internal_module.h" +#include "system/filesys.h" +#include "system/dir.h" + +/** + * Run the specified init functions. + * + * @return true if all functions ran successfully, false otherwise + */ +bool samba_init_module_fns_run(samba_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 + */ + +samba_init_module_fn *samba_modules_load(TALLOC_CTX *mem_ctx, const char *subsystem) +{ + char *path = modules_path(mem_ctx, subsystem); + samba_init_module_fn *ret; + + ret = load_modules(mem_ctx, path); + + talloc_free(path); + + return ret; +} diff --git a/lib/util/samba_module.h b/lib/util/samba_module.h new file mode 100644 index 0000000000..32b5c4b6ad --- /dev/null +++ b/lib/util/samba_module.h @@ -0,0 +1,49 @@ +/* + 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 . +*/ + +#ifndef _SAMBA_MODULES_H +#define _SAMBA_MODULES_H + +/* Module support */ +typedef NTSTATUS (*samba_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" + +/** + * Run the specified init functions. + * + * @return true if all functions ran successfully, false otherwise + */ +bool samba_init_module_fns_run(samba_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 + */ +samba_init_module_fn *samba_modules_load(TALLOC_CTX *mem_ctx, const char *subsystem); + +#endif /* _SAMBA_MODULES_H */ diff --git a/lib/util/samba_modules.h b/lib/util/samba_modules.h deleted file mode 100644 index a3d7609cb6..0000000000 --- a/lib/util/samba_modules.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - 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 . -*/ - -#ifndef _SAMBA_MODULES_H -#define _SAMBA_MODULES_H - -/* Module support */ -typedef NTSTATUS (*samba_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 - */ -samba_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 samba_init_module_fns_run(samba_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 - */ -samba_init_module_fn *samba_modules_load(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 1dc65fab5e..990887aeab 100755 --- a/lib/util/wscript_build +++ b/lib/util/wscript_build @@ -17,11 +17,19 @@ bld.SAMBA_LIBRARY('samba-util', pc_files='samba-util.pc' ) -bld.SAMBA_LIBRARY('samba-modules', - source='modules.c', +bld.SAMBA_LIBRARY('samba-module', + source='samba_module.c', + deps='errors samba-util samba-internal-module', + local_include=False, + public_headers='samba_module.h', + vnum='0.0.1', + pc_files='samba-module.pc') + +bld.SAMBA_LIBRARY('samba-internal-module', + source='internal_module.c', deps='errors samba-util', local_include=False, - private_library=True) + private_library=True) bld.SAMBA_LIBRARY('asn1util', source='asn1.c', -- cgit