s3-module allow libreplace to provide dlopen replacement
[samba.git] / source3 / lib / module.c
1 /* 
2    Unix SMB/CIFS implementation.
3    module loading system
4
5    Copyright (C) Jelmer Vernooij 2002-2003
6    Copyright (C) Stefan (metze) Metzmacher 2003
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
24 /* Load a dynamic module.  Only log a level 0 error if we are not checking 
25    for the existence of a module (probling). */
26
27 static NTSTATUS do_smb_load_module(const char *module_name, bool is_probe)
28 {
29         void *handle;
30         init_module_function *init;
31         NTSTATUS status;
32         const char *error;
33
34         /* Always try to use LAZY symbol resolving; if the plugin has 
35          * backwards compatibility, there might be symbols in the 
36          * plugin referencing to old (removed) functions
37          */
38         handle = dlopen(module_name, RTLD_LAZY);
39
40         /* This call should reset any possible non-fatal errors that 
41            occured since last call to dl* functions */
42         error = dlerror();
43
44         if(!handle) {
45                 int level = is_probe ? 3 : 0;
46                 DEBUG(level, ("Error loading module '%s': %s\n", module_name, error ? error : ""));
47                 return NT_STATUS_UNSUCCESSFUL;
48         }
49
50         init = (init_module_function *)dlsym(handle, "init_samba_module");
51
52         /* we must check dlerror() to determine if it worked, because
53            dlsym() can validly return NULL */
54         error = dlerror();
55         if (error) {
56                 DEBUG(0, ("Error trying to resolve symbol 'init_samba_module' "
57                           "in %s: %s\n", module_name, error));
58                 dlclose(handle);
59                 return NT_STATUS_UNSUCCESSFUL;
60         }
61
62         DEBUG(2, ("Module '%s' loaded\n", module_name));
63
64         status = init();
65         if (!NT_STATUS_IS_OK(status)) {
66                 DEBUG(0, ("Module '%s' initialization failed: %s\n",
67                             module_name, get_friendly_nt_error_msg(status)));
68                 dlclose(handle);
69         }
70
71         return status;
72 }
73
74 NTSTATUS smb_load_module(const char *module_name)
75 {
76         return do_smb_load_module(module_name, False);
77 }
78
79 /* Load all modules in list and return number of
80  * modules that has been successfully loaded */
81 int smb_load_modules(const char **modules)
82 {
83         int i;
84         int success = 0;
85
86         for(i = 0; modules[i]; i++){
87                 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
88                         success++;
89                 }
90         }
91
92         DEBUG(2, ("%d modules successfully loaded\n", success));
93
94         return success;
95 }
96
97 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
98 {
99         char *full_path = NULL;
100         TALLOC_CTX *ctx = talloc_stackframe();
101         NTSTATUS status;
102
103         /* Check for absolute path */
104
105         /* if we make any 'samba multibyte string'
106            calls here, we break
107            for loading string modules */
108
109         DEBUG(5, ("Probing module '%s'\n", module));
110
111         if (module[0] == '/') {
112                 status = do_smb_load_module(module, True);
113                 TALLOC_FREE(ctx);
114                 return status;
115         }
116
117         full_path = talloc_asprintf(ctx,
118                                     "%s/%s.%s",
119                                     modules_path(ctx, subsystem),
120                                     module,
121                                     shlib_ext());
122         if (!full_path) {
123                 TALLOC_FREE(ctx);
124                 return NT_STATUS_NO_MEMORY;
125         }
126
127         DEBUG(5, ("Probing module '%s': Trying to load from %s\n",
128                 module, full_path));
129
130         status = do_smb_load_module(full_path, True);
131
132         TALLOC_FREE(ctx);
133         return status;
134 }
135
136 void init_modules(void)
137 {
138         /* FIXME: This can cause undefined symbol errors :
139          *  smb_register_vfs() isn't available in nmbd, for example */
140         if(lp_preload_modules()) 
141                 smb_load_modules(lp_preload_modules());
142 }