Apply the changes that Derrell Lipman supplied ...
[Samba/gebeck_regimport.git] / source3 / lib / module.c
blob38fcf0f3297599d55117d48bd011a6cb35880aa2
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 if(!handle) {
44 int level = is_probe ? 3 : 0;
45 DEBUG(level, ("Error loading module '%s': %s\n", module_name,
46 sys_dlerror()));
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();
56 if (error) {
57 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n",
58 module_name, error));
59 return NT_STATUS_UNSUCCESSFUL;
62 status = init();
64 DEBUG(2, ("Module '%s' loaded\n", module_name));
66 return status;
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)
78 int i;
79 int success = 0;
81 for(i = 0; modules[i]; i++){
82 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
83 success++;
87 DEBUG(2, ("%d modules successfully loaded\n", success));
89 return success;
92 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
94 pstring full_path;
96 /* Check for absolute path */
98 /* if we make any 'samba multibyte string'
99 calls here, we break
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"));
129 return -1;
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;
159 smb_event_id_t id;
160 smb_idle_event_fn *fn;
161 void *data;
162 time_t interval;
163 time_t lastrun;
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;
172 if (!fn) {
173 return SMB_EVENT_ID_INVALID;
176 event = (struct smb_idle_list_ent *)malloc(sizeof(struct smb_idle_list_ent));
177 if (!event) {
178 DEBUG(0,("malloc() failed!\n"));
179 return SMB_EVENT_ID_INVALID;
181 event->fn = fn;
182 event->data = data;
183 event->interval = interval;
184 event->lastrun = 0;
185 event->id = smb_idle_event_id++;
187 DLIST_ADD(smb_idle_event_list,event);
189 return event->id;
192 BOOL smb_unregister_idle_event(smb_event_id_t id)
194 struct smb_idle_list_ent *event = smb_idle_event_list;
196 while(event) {
197 if (event->id == id) {
198 DLIST_REMOVE(smb_idle_event_list,event);
199 SAFE_FREE(event);
200 return True;
202 event = event->next;
205 return False;
208 void smb_run_idle_events(time_t now)
210 struct smb_idle_list_ent *event = smb_idle_event_list;
212 while (event) {
213 struct smb_idle_list_ent *next = event->next;
214 time_t interval;
216 if (event->interval <= 0) {
217 interval = SMB_IDLE_EVENT_DEFAULT_INTERVAL;
218 } else if (event->interval >= SMB_IDLE_EVENT_MIN_INTERVAL) {
219 interval = event->interval;
220 } else {
221 interval = SMB_IDLE_EVENT_MIN_INTERVAL;
223 if (now >(event->lastrun+interval)) {
224 event->lastrun = now;
225 event->fn(&event->data,&event->interval,now);
227 event = next;
230 return;
233 /***************************************************************************
234 * This Function registers a exit event
236 * the registered functions are run on exit()
237 * and maybe shutdown idle connections (e.g. to an LDAP server)
238 ***************************************************************************/
240 struct smb_exit_list_ent {
241 struct smb_exit_list_ent *prev,*next;
242 smb_event_id_t id;
243 smb_exit_event_fn *fn;
244 void *data;
247 static struct smb_exit_list_ent *smb_exit_event_list = NULL;
249 smb_event_id_t smb_register_exit_event(smb_exit_event_fn *fn, void *data)
251 struct smb_exit_list_ent *event;
252 static smb_event_id_t smb_exit_event_id = 1;
254 if (!fn) {
255 return SMB_EVENT_ID_INVALID;
258 event = (struct smb_exit_list_ent *)malloc(sizeof(struct smb_exit_list_ent));
259 if (!event) {
260 DEBUG(0,("malloc() failed!\n"));
261 return SMB_EVENT_ID_INVALID;
263 event->fn = fn;
264 event->data = data;
265 event->id = smb_exit_event_id++;
267 DLIST_ADD(smb_exit_event_list,event);
269 return event->id;
272 BOOL smb_unregister_exit_event(smb_event_id_t id)
274 struct smb_exit_list_ent *event = smb_exit_event_list;
276 while(event) {
277 if (event->id == id) {
278 DLIST_REMOVE(smb_exit_event_list,event);
279 SAFE_FREE(event);
280 return True;
282 event = event->next;
285 return False;
288 void smb_run_exit_events(void)
290 struct smb_exit_list_ent *event = smb_exit_event_list;
291 struct smb_exit_list_ent *tmp = NULL;
293 while (event) {
294 event->fn(&event->data);
295 tmp = event;
296 event = event->next;
297 /* exit event should only run one time :-)*/
298 SAFE_FREE(tmp);
301 /* the list is empty now...*/
302 smb_exit_event_list = NULL;
304 return;