23298da344956d09fb72d6e1b7161171bb5c34aa
[obnox/samba/samba-obnox.git] / lib / util / modules.c
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
7
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.
12
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.
17
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/>.
20 */
21
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"
27
28 /**
29  * Obtain the init function from a shared library file
30  */
31 init_module_fn load_module(const char *path, bool is_probe, void **handle_out)
32 {
33         void *handle;
34         void *init_fn;
35         char *error;
36
37 #if _SAMBA_BUILD_ == 3
38         /* Always try to use LAZY symbol resolving; if the plugin has
39          * backwards compatibility, there might be symbols in the
40          * plugin referencing to old (removed) functions
41          */
42         handle = dlopen(path, RTLD_LAZY);
43 #else
44         /* This should be a WAF build, where modules should be built
45          * with no undefined symbols and are already linked against
46          * the libraries that they are loaded by */
47         handle = dlopen(path, RTLD_NOW);
48 #endif
49
50         /* This call should reset any possible non-fatal errors that
51            occured since last call to dl* functions */
52         error = dlerror();
53
54         if (handle == NULL) {
55                 int level = is_probe ? 5 : 0;
56                 DEBUG(level, ("Error loading module '%s': %s\n", path, error ? error : ""));
57                 return NULL;
58         }
59
60         init_fn = (init_module_fn)dlsym(handle, SAMBA_INIT_MODULE);
61
62         /* we could check dlerror() to determine if it worked, because
63            dlsym() can validly return NULL, but what would we do with
64            a NULL pointer as a module init function? */
65
66         if (init_fn == NULL) {
67                 DEBUG(0, ("Unable to find %s() in %s: %s\n",
68                           SAMBA_INIT_MODULE, path, dlerror()));
69                 DEBUG(1, ("Loading module '%s' failed\n", path));
70                 dlclose(handle);
71                 return NULL;
72         }
73
74         if (handle_out) {
75                 *handle_out = handle;
76         }
77
78         return (init_module_fn)init_fn;
79 }
80
81 /**
82  * Obtain list of init functions from the modules in the specified
83  * directory
84  */
85 static init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
86 {
87         DIR *dir;
88         struct dirent *entry;
89         char *filename;
90         int success = 0;
91         init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
92
93         ret[0] = NULL;
94
95         dir = opendir(path);
96         if (dir == NULL) {
97                 talloc_free(ret);
98                 return NULL;
99         }
100
101         while((entry = readdir(dir))) {
102                 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
103                         continue;
104
105                 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
106
107                 ret[success] = load_module(filename, true, NULL);
108                 if (ret[success]) {
109                         ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
110                         success++;
111                         ret[success] = NULL;
112                 }
113
114                 talloc_free(filename);
115         }
116
117         closedir(dir);
118
119         return ret;
120 }
121
122 /**
123  * Run the specified init functions.
124  *
125  * @return true if all functions ran successfully, false otherwise
126  */
127 bool run_init_functions(init_module_fn *fns)
128 {
129         int i;
130         bool ret = true;
131
132         if (fns == NULL)
133                 return true;
134
135         for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
136
137         return ret;
138 }
139
140 /**
141  * Load the initialization functions from DSO files for a specific subsystem.
142  *
143  * Will return an array of function pointers to initialization functions
144  */
145
146 init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
147 {
148         char *path = modules_path(mem_ctx, subsystem);
149         init_module_fn *ret;
150
151         ret = load_modules(mem_ctx, path);
152
153         talloc_free(path);
154
155         return ret;
156 }
157
158
159 /* Load a dynamic module.  Only log a level 0 error if we are not checking
160    for the existence of a module (probling). */
161
162 static NTSTATUS do_smb_load_module(const char *subsystem,
163                                    const char *module_name, bool is_probe)
164 {
165         void *handle;
166         init_module_fn init;
167         NTSTATUS status;
168
169         char *full_path = NULL;
170         TALLOC_CTX *ctx = talloc_stackframe();
171
172         /* Check for absolute path */
173
174         DEBUG(5, ("%s module '%s'\n", is_probe ? "Probing" : "Loading", module_name));
175
176         if (subsystem && module_name[0] != '/') {
177                 full_path = talloc_asprintf(ctx,
178                                             "%s/%s.%s",
179                                             modules_path(ctx, subsystem),
180                                             module_name,
181                                             shlib_ext());
182                 if (!full_path) {
183                         TALLOC_FREE(ctx);
184                         return NT_STATUS_NO_MEMORY;
185                 }
186
187                 DEBUG(5, ("%s module '%s': Trying to load from %s\n",
188                           is_probe ? "Probing": "Loading", module_name, full_path));
189                 init = load_module(full_path, is_probe, &handle);
190         } else {
191                 init = load_module(module_name, is_probe, &handle);
192         }
193
194         if (!init) {
195                 TALLOC_FREE(ctx);
196                 return NT_STATUS_UNSUCCESSFUL;
197         }
198
199         DEBUG(2, ("Module '%s' loaded\n", module_name));
200
201         status = init();
202         if (!NT_STATUS_IS_OK(status)) {
203                 DEBUG(0, ("Module '%s' initialization failed: %s\n",
204                           module_name, get_friendly_nt_error_msg(status)));
205                 dlclose(handle);
206         }
207         TALLOC_FREE(ctx);
208         return status;
209 }
210
211 /* Load all modules in list and return number of
212  * modules that has been successfully loaded */
213 int smb_load_modules(const char **modules)
214 {
215         int i;
216         int success = 0;
217
218         for(i = 0; modules[i]; i++){
219                 if(NT_STATUS_IS_OK(do_smb_load_module(NULL, modules[i], false))) {
220                         success++;
221                 }
222         }
223
224         DEBUG(2, ("%d modules successfully loaded\n", success));
225
226         return success;
227 }
228
229 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
230 {
231         return do_smb_load_module(subsystem, module, true);
232 }
233
234 NTSTATUS smb_load_module(const char *subsystem, const char *module)
235 {
236         return do_smb_load_module(subsystem, module, false);
237 }