2 Unix SMB/CIFS implementation.
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 NTSTATUS smb_load_module(const char *module_name)
29 init_module_function *init;
33 /* Always try to use LAZY symbol resolving; if the plugin has
34 * backwards compatibility, there might be symbols in the
35 * plugin referencing to old (removed) functions
37 handle = sys_dlopen(module_name, RTLD_LAZY);
40 DEBUG(0, ("Error loading module '%s': %s\n", module_name, sys_dlerror()));
41 return NT_STATUS_UNSUCCESSFUL;
44 init = sys_dlsym(handle, "init_module");
46 /* we must check sys_dlerror() to determine if it worked, because
47 sys_dlsym() can validly return NULL */
48 error = sys_dlerror();
50 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n", module_name, error));
51 return NT_STATUS_UNSUCCESSFUL;
56 DEBUG(2, ("Module '%s' loaded\n", module_name));
61 /* Load all modules in list and return number of
62 * modules that has been successfully loaded */
63 int smb_load_modules(const char **modules)
68 for(i = 0; modules[i]; i++){
69 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
74 DEBUG(2, ("%d modules successfully loaded\n", success));
79 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
83 /* Check for absolute path */
85 /* if we make any 'samba multibyte string'
87 for loading string modules */
89 return smb_load_module(module);
91 pstrcpy(full_path, lib_path(subsystem));
92 pstrcat(full_path, "/");
93 pstrcat(full_path, module);
94 pstrcat(full_path, ".");
95 pstrcat(full_path, shlib_ext());
97 DEBUG(5, ("Probing module %s: Trying to load from %s\n", module, full_path));
99 return smb_load_module(full_path);
102 #else /* HAVE_DLOPEN */
104 NTSTATUS smb_load_module(const char *module_name)
106 DEBUG(0,("This samba executable has not been built with plugin support\n"));
107 return NT_STATUS_NOT_SUPPORTED;
110 int smb_load_modules(const char **modules)
112 DEBUG(0,("This samba executable has not been built with plugin support\n"));
116 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
118 DEBUG(0,("This samba executable has not been built with plugin support, not probing\n"));
119 return NT_STATUS_NOT_SUPPORTED;
122 #endif /* HAVE_DLOPEN */
124 void init_modules(void)
126 /* FIXME: This can cause undefined symbol errors :
127 * smb_register_vfs() isn't available in nmbd, for example */
128 if(lp_preload_modules())
129 smb_load_modules(lp_preload_modules());
133 /*************************************************************************
134 * This functions /path/to/foobar.so -> foobar
135 ************************************************************************/
136 void module_path_get_name(const char *path, pstring name)
140 /* First, make the path relative */
141 s = strrchr(path, '/');
142 if(s) pstrcpy(name, s+1);
143 else pstrcpy(name, path);
145 if (dyn_SHLIBEXT && *dyn_SHLIBEXT && strlen(dyn_SHLIBEXT) < strlen(name)) {
146 int n = strlen(name) - strlen(dyn_SHLIBEXT);
148 /* Remove extension if necessary */
149 if (name[n-1] == '.' && !strcmp(name+n, dyn_SHLIBEXT)) {
156 /***************************************************************************
157 * This Function registers a idle event
159 * the registered funtions are run periodically
160 * and maybe shutdown idle connections (e.g. to an LDAP server)
161 ***************************************************************************/
162 static smb_idle_event_struct *smb_idle_event_list = NULL;
163 NTSTATUS smb_register_idle_event(smb_idle_event_struct *idle_event)
166 return NT_STATUS_INVALID_PARAMETER;
169 idle_event->last_run = 0;
171 DLIST_ADD(smb_idle_event_list,idle_event);
176 NTSTATUS smb_unregister_idle_event(smb_idle_event_struct *idle_event)
179 return NT_STATUS_INVALID_PARAMETER;
182 DLIST_REMOVE(smb_idle_event_list,idle_event);
187 void smb_run_idle_events(time_t now)
189 smb_idle_event_struct *tmp_event = smb_idle_event_list;
195 if (tmp_event->interval >= SMB_IDLE_EVENT_MIN_INTERVAL) {
196 interval = tmp_event->interval;
198 interval = SMB_IDLE_EVENT_DEFAULT_INTERVAL;
200 if (now >(tmp_event->last_run+interval)) {
201 tmp_event->fn(&tmp_event,now);
202 tmp_event->last_run = now;
206 tmp_event = tmp_event->next;
212 /***************************************************************************
213 * This Function registers a exit event
215 * the registered funtions are run on exit()
216 * and maybe shutdown idle connections (e.g. to an LDAP server)
217 ***************************************************************************/
218 static smb_exit_event_struct *smb_exit_event_list = NULL;
219 NTSTATUS smb_register_exit_event(smb_exit_event_struct *exit_event)
222 return NT_STATUS_INVALID_PARAMETER;
225 DLIST_ADD(smb_exit_event_list,exit_event);
230 NTSTATUS smb_unregister_exit_event(smb_exit_event_struct *exit_event)
233 return NT_STATUS_INVALID_PARAMETER;
236 DLIST_REMOVE(smb_exit_event_list,exit_event);
241 void smb_run_exit_events(void)
243 smb_exit_event_struct *tmp_event = smb_exit_event_list;
247 tmp_event->fn(&tmp_event);
249 tmp_event = tmp_event->next;
252 /* run exit_events only once */
253 smb_exit_event_list = NULL;