vfs_glusterfs: Implement AIO support
[Samba.git] / lib / util / modules.c
blob828f33a0e1639095a255833b4e83da035d7f0003
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Jelmer Vernooij 2002-2003,2005-2007
5 Copyright (C) Stefan (metze) Metzmacher 2003
6 Copyright (C) Andrew Bartlett 2011
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "dynconfig/dynconfig.h"
24 #include "lib/util/samba_modules.h"
25 #include "system/filesys.h"
26 #include "system/dir.h"
28 /**
29 * Obtain the init function from a shared library file
31 init_module_fn load_module(const char *path, bool is_probe, void **handle_out)
33 void *handle;
34 void *init_fn;
35 char *error;
37 /* This should be a WAF build, where modules should be built
38 * with no undefined symbols and are already linked against
39 * the libraries that they are loaded by */
40 handle = dlopen(path, RTLD_NOW);
42 /* This call should reset any possible non-fatal errors that
43 occured since last call to dl* functions */
44 error = dlerror();
46 if (handle == NULL) {
47 int level = is_probe ? 5 : 0;
48 DEBUG(level, ("Error loading module '%s': %s\n", path, error ? error : ""));
49 return NULL;
52 init_fn = (init_module_fn)dlsym(handle, SAMBA_INIT_MODULE);
54 /* we could check dlerror() to determine if it worked, because
55 dlsym() can validly return NULL, but what would we do with
56 a NULL pointer as a module init function? */
58 if (init_fn == NULL) {
59 DEBUG(0, ("Unable to find %s() in %s: %s\n",
60 SAMBA_INIT_MODULE, path, dlerror()));
61 DEBUG(1, ("Loading module '%s' failed\n", path));
62 dlclose(handle);
63 return NULL;
66 if (handle_out) {
67 *handle_out = handle;
70 return (init_module_fn)init_fn;
73 /**
74 * Obtain list of init functions from the modules in the specified
75 * directory
77 static init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
79 DIR *dir;
80 struct dirent *entry;
81 char *filename;
82 int success = 0;
83 init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
85 ret[0] = NULL;
87 dir = opendir(path);
88 if (dir == NULL) {
89 talloc_free(ret);
90 return NULL;
93 while((entry = readdir(dir))) {
94 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
95 continue;
97 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
99 ret[success] = load_module(filename, true, NULL);
100 if (ret[success]) {
101 ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
102 success++;
103 ret[success] = NULL;
106 talloc_free(filename);
109 closedir(dir);
111 return ret;
115 * Run the specified init functions.
117 * @return true if all functions ran successfully, false otherwise
119 bool run_init_functions(init_module_fn *fns)
121 int i;
122 bool ret = true;
124 if (fns == NULL)
125 return true;
127 for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
129 return ret;
133 * Load the initialization functions from DSO files for a specific subsystem.
135 * Will return an array of function pointers to initialization functions
138 init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
140 char *path = modules_path(mem_ctx, subsystem);
141 init_module_fn *ret;
143 ret = load_modules(mem_ctx, path);
145 talloc_free(path);
147 return ret;
151 /* Load a dynamic module. Only log a level 0 error if we are not checking
152 for the existence of a module (probling). */
154 static NTSTATUS do_smb_load_module(const char *subsystem,
155 const char *module_name, bool is_probe)
157 void *handle;
158 init_module_fn init;
159 NTSTATUS status;
161 char *full_path = NULL;
162 TALLOC_CTX *ctx = talloc_stackframe();
164 /* Check for absolute path */
166 DEBUG(5, ("%s module '%s'\n", is_probe ? "Probing" : "Loading", module_name));
168 if (subsystem && module_name[0] != '/') {
169 full_path = talloc_asprintf(ctx,
170 "%s/%s.%s",
171 modules_path(ctx, subsystem),
172 module_name,
173 shlib_ext());
174 if (!full_path) {
175 TALLOC_FREE(ctx);
176 return NT_STATUS_NO_MEMORY;
179 DEBUG(5, ("%s module '%s': Trying to load from %s\n",
180 is_probe ? "Probing": "Loading", module_name, full_path));
181 init = load_module(full_path, is_probe, &handle);
182 } else {
183 init = load_module(module_name, is_probe, &handle);
186 if (!init) {
187 TALLOC_FREE(ctx);
188 return NT_STATUS_UNSUCCESSFUL;
191 DEBUG(2, ("Module '%s' loaded\n", module_name));
193 status = init();
194 if (!NT_STATUS_IS_OK(status)) {
195 DEBUG(0, ("Module '%s' initialization failed: %s\n",
196 module_name, get_friendly_nt_error_msg(status)));
197 dlclose(handle);
199 TALLOC_FREE(ctx);
200 return status;
203 /* Load all modules in list and return number of
204 * modules that has been successfully loaded */
205 int smb_load_modules(const char **modules)
207 int i;
208 int success = 0;
210 for(i = 0; modules[i]; i++){
211 if(NT_STATUS_IS_OK(do_smb_load_module(NULL, modules[i], false))) {
212 success++;
216 DEBUG(2, ("%d modules successfully loaded\n", success));
218 return success;
221 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
223 return do_smb_load_module(subsystem, module, true);
226 NTSTATUS smb_load_module(const char *subsystem, const char *module)
228 return do_smb_load_module(subsystem, module, false);