r25055: Add file_id_string_tos
[Samba.git] / source / lib / module.c
blob1bf305ea3f70ebbe4cb8e434215a6946e4842d5e
1 /*
2 Unix SMB/CIFS implementation.
3 module loading system
5 Copyright (C) Jelmer Vernooij 2002-2003
6 Copyright (C) Stefan (metze) Metzmacher 2003
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"
24 #ifdef HAVE_DLOPEN
26 /* Load a dynamic module. Only log a level 0 error if we are not checking
27 for the existence of a module (probling). */
29 static NTSTATUS do_smb_load_module(const char *module_name, BOOL is_probe)
31 void *handle;
32 init_module_function *init;
33 NTSTATUS status;
34 const char *error;
36 /* Always try to use LAZY symbol resolving; if the plugin has
37 * backwards compatibility, there might be symbols in the
38 * plugin referencing to old (removed) functions
40 handle = sys_dlopen(module_name, RTLD_LAZY);
42 /* This call should reset any possible non-fatal errors that
43 occured since last call to dl* functions */
44 error = sys_dlerror();
46 if(!handle) {
47 int level = is_probe ? 3 : 0;
48 DEBUG(level, ("Error loading module '%s': %s\n", module_name, error ? error : ""));
49 return NT_STATUS_UNSUCCESSFUL;
52 init = (init_module_function *)sys_dlsym(handle, "init_module");
54 /* we must check sys_dlerror() to determine if it worked, because
55 sys_dlsym() can validly return NULL */
56 error = sys_dlerror();
57 if (error) {
58 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n",
59 module_name, error));
60 return NT_STATUS_UNSUCCESSFUL;
63 DEBUG(2, ("Module '%s' loaded\n", module_name));
65 status = init();
66 if (!NT_STATUS_IS_OK(status)) {
67 DEBUG(0, ("Module '%s' initialization failed: %s\n",
68 module_name, get_friendly_nt_error_msg(status)));
71 return status;
74 NTSTATUS smb_load_module(const char *module_name)
76 return do_smb_load_module(module_name, False);
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)
83 int i;
84 int success = 0;
86 for(i = 0; modules[i]; i++){
87 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
88 success++;
92 DEBUG(2, ("%d modules successfully loaded\n", success));
94 return success;
97 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
99 pstring full_path;
101 /* Check for absolute path */
103 /* if we make any 'samba multibyte string'
104 calls here, we break
105 for loading string modules */
107 DEBUG(5, ("Probing module '%s'\n", module));
109 if (module[0] == '/')
110 return do_smb_load_module(module, True);
112 pstrcpy(full_path, lib_path(subsystem));
113 pstrcat(full_path, "/");
114 pstrcat(full_path, module);
115 pstrcat(full_path, ".");
116 pstrcat(full_path, shlib_ext());
118 DEBUG(5, ("Probing module '%s': Trying to load from %s\n", module, full_path));
120 return do_smb_load_module(full_path, True);
123 #else /* HAVE_DLOPEN */
125 NTSTATUS smb_load_module(const char *module_name)
127 DEBUG(0,("This samba executable has not been built with plugin support\n"));
128 return NT_STATUS_NOT_SUPPORTED;
131 int smb_load_modules(const char **modules)
133 DEBUG(0,("This samba executable has not been built with plugin support\n"));
134 return -1;
137 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
139 DEBUG(0,("This samba executable has not been built with plugin support, not probing\n"));
140 return NT_STATUS_NOT_SUPPORTED;
143 #endif /* HAVE_DLOPEN */
145 void init_modules(void)
147 /* FIXME: This can cause undefined symbol errors :
148 * smb_register_vfs() isn't available in nmbd, for example */
149 if(lp_preload_modules())
150 smb_load_modules(lp_preload_modules());