r9998: starting content for maintainers file
[Samba.git] / source / lib / module.c
blob49121d12ca177d8617ad221be419200bc70972c3
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 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.
23 #include "includes.h"
25 #ifdef HAVE_DLOPEN
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)
32 void *handle;
33 init_module_function *init;
34 NTSTATUS status;
35 const char *error;
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);
43 /* This call should reset any possible non-fatal errors that
44 occured since last call to dl* functions */
45 error = sys_dlerror();
47 if(!handle) {
48 int level = is_probe ? 3 : 0;
49 DEBUG(level, ("Error loading module '%s': %s\n", module_name, error ? error : ""));
50 return NT_STATUS_UNSUCCESSFUL;
53 init = (init_module_function *)sys_dlsym(handle, "init_module");
55 /* we must check sys_dlerror() to determine if it worked, because
56 sys_dlsym() can validly return NULL */
57 error = sys_dlerror();
58 if (error) {
59 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n",
60 module_name, error));
61 return NT_STATUS_UNSUCCESSFUL;
64 status = init();
66 DEBUG(2, ("Module '%s' loaded\n", module_name));
68 return status;
71 NTSTATUS smb_load_module(const char *module_name)
73 return do_smb_load_module(module_name, False);
76 /* Load all modules in list and return number of
77 * modules that has been successfully loaded */
78 int smb_load_modules(const char **modules)
80 int i;
81 int success = 0;
83 for(i = 0; modules[i]; i++){
84 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
85 success++;
89 DEBUG(2, ("%d modules successfully loaded\n", success));
91 return success;
94 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
96 pstring full_path;
98 /* Check for absolute path */
100 /* if we make any 'samba multibyte string'
101 calls here, we break
102 for loading string modules */
104 DEBUG(5, ("Probing module '%s'\n", module));
106 if (module[0] == '/')
107 return do_smb_load_module(module, True);
109 pstrcpy(full_path, lib_path(subsystem));
110 pstrcat(full_path, "/");
111 pstrcat(full_path, module);
112 pstrcat(full_path, ".");
113 pstrcat(full_path, shlib_ext());
115 DEBUG(5, ("Probing module '%s': Trying to load from %s\n", module, full_path));
117 return do_smb_load_module(full_path, True);
120 #else /* HAVE_DLOPEN */
122 NTSTATUS smb_load_module(const char *module_name)
124 DEBUG(0,("This samba executable has not been built with plugin support\n"));
125 return NT_STATUS_NOT_SUPPORTED;
128 int smb_load_modules(const char **modules)
130 DEBUG(0,("This samba executable has not been built with plugin support\n"));
131 return -1;
134 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
136 DEBUG(0,("This samba executable has not been built with plugin support, not probing\n"));
137 return NT_STATUS_NOT_SUPPORTED;
140 #endif /* HAVE_DLOPEN */
142 void init_modules(void)
144 /* FIXME: This can cause undefined symbol errors :
145 * smb_register_vfs() isn't available in nmbd, for example */
146 if(lp_preload_modules())
147 smb_load_modules(lp_preload_modules());
151 /***************************************************************************
152 * This Function registers a idle event
154 * the registered funtions are run periodically
155 * and maybe shutdown idle connections (e.g. to an LDAP server)
156 ***************************************************************************/
157 static smb_event_id_t smb_idle_event_id = 1;
159 struct smb_idle_list_ent {
160 struct smb_idle_list_ent *prev,*next;
161 smb_event_id_t id;
162 smb_idle_event_fn *fn;
163 void *data;
164 time_t interval;
165 time_t lastrun;
168 static struct smb_idle_list_ent *smb_idle_event_list = NULL;
170 smb_event_id_t smb_register_idle_event(smb_idle_event_fn *fn, void *data, time_t interval)
172 struct smb_idle_list_ent *event;
174 if (!fn) {
175 return SMB_EVENT_ID_INVALID;
178 event = SMB_MALLOC_P(struct smb_idle_list_ent);
179 if (!event) {
180 DEBUG(0,("malloc() failed!\n"));
181 return SMB_EVENT_ID_INVALID;
183 event->fn = fn;
184 event->data = data;
185 event->interval = interval;
186 event->lastrun = 0;
187 event->id = smb_idle_event_id++;
189 DLIST_ADD(smb_idle_event_list,event);
191 return event->id;
194 BOOL smb_unregister_idle_event(smb_event_id_t id)
196 struct smb_idle_list_ent *event = smb_idle_event_list;
198 while(event) {
199 if (event->id == id) {
200 DLIST_REMOVE(smb_idle_event_list,event);
201 SAFE_FREE(event);
202 return True;
204 event = event->next;
207 return False;
210 void smb_run_idle_events(time_t now)
212 struct smb_idle_list_ent *event = smb_idle_event_list;
214 while (event) {
215 struct smb_idle_list_ent *next = event->next;
216 time_t interval;
218 if (event->interval <= 0) {
219 interval = SMB_IDLE_EVENT_DEFAULT_INTERVAL;
220 } else if (event->interval >= SMB_IDLE_EVENT_MIN_INTERVAL) {
221 interval = event->interval;
222 } else {
223 interval = SMB_IDLE_EVENT_MIN_INTERVAL;
225 if (now >(event->lastrun+interval)) {
226 event->lastrun = now;
227 event->fn(&event->data,&event->interval,now);
229 event = next;
232 return;
235 /***************************************************************************
236 * This Function registers a exit event
238 * the registered functions are run on exit()
239 * and maybe shutdown idle connections (e.g. to an LDAP server)
240 ***************************************************************************/
242 struct smb_exit_list_ent {
243 struct smb_exit_list_ent *prev,*next;
244 smb_event_id_t id;
245 smb_exit_event_fn *fn;
246 void *data;
249 static struct smb_exit_list_ent *smb_exit_event_list = NULL;
251 smb_event_id_t smb_register_exit_event(smb_exit_event_fn *fn, void *data)
253 struct smb_exit_list_ent *event;
254 static smb_event_id_t smb_exit_event_id = 1;
256 if (!fn) {
257 return SMB_EVENT_ID_INVALID;
260 event = SMB_MALLOC_P(struct smb_exit_list_ent);
261 if (!event) {
262 DEBUG(0,("malloc() failed!\n"));
263 return SMB_EVENT_ID_INVALID;
265 event->fn = fn;
266 event->data = data;
267 event->id = smb_exit_event_id++;
269 DLIST_ADD(smb_exit_event_list,event);
271 return event->id;
274 BOOL smb_unregister_exit_event(smb_event_id_t id)
276 struct smb_exit_list_ent *event = smb_exit_event_list;
278 while(event) {
279 if (event->id == id) {
280 DLIST_REMOVE(smb_exit_event_list,event);
281 SAFE_FREE(event);
282 return True;
284 event = event->next;
287 return False;
290 void smb_run_exit_events(void)
292 struct smb_exit_list_ent *event = smb_exit_event_list;
293 struct smb_exit_list_ent *tmp = NULL;
295 while (event) {
296 event->fn(&event->data);
297 tmp = event;
298 event = event->next;
299 /* exit event should only run one time :-)*/
300 SAFE_FREE(tmp);
303 /* the list is empty now...*/
304 smb_exit_event_list = NULL;
306 return;