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.
27 /* Load a dynamic module. Only log a level 0 error if we are not checking
28 for the existence of a module (probling). */
30 static NTSTATUS do_smb_load_module(const char *module_name, BOOL is_probe)
33 init_module_function *init;
37 /* Always try to use LAZY symbol resolving; if the plugin has
38 * backwards compatibility, there might be symbols in the
39 * plugin referencing to old (removed) functions
41 handle = sys_dlopen(module_name, RTLD_LAZY);
44 int level = is_probe ? 2 : 0;
45 DEBUG(level, ("Error loading module '%s': %s\n", module_name,
48 return NT_STATUS_UNSUCCESSFUL;
51 init = (init_module_function *)sys_dlsym(handle, "init_module");
53 /* we must check sys_dlerror() to determine if it worked, because
54 sys_dlsym() can validly return NULL */
55 error = sys_dlerror();
57 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n",
59 return NT_STATUS_UNSUCCESSFUL;
64 DEBUG(2, ("Module '%s' loaded\n", module_name));
69 NTSTATUS smb_load_module(const char *module_name)
71 return do_smb_load_module(module_name, False);
74 /* Load all modules in list and return number of
75 * modules that has been successfully loaded */
76 int smb_load_modules(const char **modules)
81 for(i = 0; modules[i]; i++){
82 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
87 DEBUG(2, ("%d modules successfully loaded\n", success));
92 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
96 /* Check for absolute path */
98 /* if we make any 'samba multibyte string'
100 for loading string modules */
102 DEBUG(5, ("Probing module '%s'\n", module));
104 if (module[0] == '/')
105 return do_smb_load_module(module, True);
107 pstrcpy(full_path, lib_path(subsystem));
108 pstrcat(full_path, "/");
109 pstrcat(full_path, module);
110 pstrcat(full_path, ".");
111 pstrcat(full_path, shlib_ext());
113 DEBUG(5, ("Probing module '%s': Trying to load from %s\n", module, full_path));
115 return do_smb_load_module(full_path, True);
118 #else /* HAVE_DLOPEN */
120 NTSTATUS smb_load_module(const char *module_name)
122 DEBUG(0,("This samba executable has not been built with plugin support\n"));
123 return NT_STATUS_NOT_SUPPORTED;
126 int smb_load_modules(const char **modules)
128 DEBUG(0,("This samba executable has not been built with plugin support\n"));
132 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
134 DEBUG(0,("This samba executable has not been built with plugin support, not probing\n"));
135 return NT_STATUS_NOT_SUPPORTED;
138 #endif /* HAVE_DLOPEN */
140 void init_modules(void)
142 /* FIXME: This can cause undefined symbol errors :
143 * smb_register_vfs() isn't available in nmbd, for example */
144 if(lp_preload_modules())
145 smb_load_modules(lp_preload_modules());
149 /***************************************************************************
150 * This Function registers a idle event
152 * the registered funtions are run periodically
153 * and maybe shutdown idle connections (e.g. to an LDAP server)
154 ***************************************************************************/
155 static smb_event_id_t smb_idle_event_id = 1;
157 struct smb_idle_list_ent {
158 struct smb_idle_list_ent *prev,*next;
160 smb_idle_event_fn *fn;
166 static struct smb_idle_list_ent *smb_idle_event_list = NULL;
168 smb_event_id_t smb_register_idle_event(smb_idle_event_fn *fn, void *data, time_t interval)
170 struct smb_idle_list_ent *event;
173 return SMB_EVENT_ID_INVALID;
176 event = (struct smb_idle_list_ent *)malloc(sizeof(struct smb_idle_list_ent));
178 DEBUG(0,("malloc() failed!\n"));
179 return SMB_EVENT_ID_INVALID;
183 event->interval = interval;
185 event->id = smb_idle_event_id++;
187 DLIST_ADD(smb_idle_event_list,event);
192 BOOL smb_unregister_idle_event(smb_event_id_t id)
194 struct smb_idle_list_ent *event = smb_idle_event_list;
197 if (event->id == id) {
198 DLIST_REMOVE(smb_idle_event_list,event);
208 void smb_run_idle_events(time_t now)
210 struct smb_idle_list_ent *event = smb_idle_event_list;
215 if (event->interval >= SMB_IDLE_EVENT_MIN_INTERVAL) {
216 interval = event->interval;
218 interval = SMB_IDLE_EVENT_MIN_INTERVAL;
220 if (now >(event->lastrun+interval)) {
221 event->fn(&event->data,&event->interval,now);
222 event->lastrun = now;
230 /***************************************************************************
231 * This Function registers a exit event
233 * the registered functions are run on exit()
234 * and maybe shutdown idle connections (e.g. to an LDAP server)
235 ***************************************************************************/
237 struct smb_exit_list_ent {
238 struct smb_exit_list_ent *prev,*next;
240 smb_exit_event_fn *fn;
244 static struct smb_exit_list_ent *smb_exit_event_list = NULL;
246 smb_event_id_t smb_register_exit_event(smb_exit_event_fn *fn, void *data)
248 struct smb_exit_list_ent *event;
249 static smb_event_id_t smb_exit_event_id = 1;
252 return SMB_EVENT_ID_INVALID;
255 event = (struct smb_exit_list_ent *)malloc(sizeof(struct smb_exit_list_ent));
257 DEBUG(0,("malloc() failed!\n"));
258 return SMB_EVENT_ID_INVALID;
262 event->id = smb_exit_event_id++;
264 DLIST_ADD(smb_exit_event_list,event);
269 BOOL smb_unregister_exit_event(smb_event_id_t id)
271 struct smb_exit_list_ent *event = smb_exit_event_list;
274 if (event->id == id) {
275 DLIST_REMOVE(smb_exit_event_list,event);
285 void smb_run_exit_events(void)
287 struct smb_exit_list_ent *event = smb_exit_event_list;
288 struct smb_exit_list_ent *tmp = NULL;
291 event->fn(&event->data);
294 /* exit event should only run one time :-)*/
298 /* the list is empty now...*/
299 smb_exit_event_list = NULL;