Fix scripts to generate correct tables for compilers which have character constants...
[Samba/gebeck_regimport.git] / source3 / lib / module.c
blob941a6cfbe3945ffbd3d5435084719e2235eaa8a5
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 time_t interval;
215 if (event->interval >= SMB_IDLE_EVENT_MIN_INTERVAL) {
216 interval = event->interval;
217 } else {
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;
224 event = event->next;
227 return;
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;
239 smb_event_id_t id;
240 smb_exit_event_fn *fn;
241 void *data;
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;
251 if (!fn) {
252 return SMB_EVENT_ID_INVALID;
255 event = (struct smb_exit_list_ent *)malloc(sizeof(struct smb_exit_list_ent));
256 if (!event) {
257 DEBUG(0,("malloc() failed!\n"));
258 return SMB_EVENT_ID_INVALID;
260 event->fn = fn;
261 event->data = data;
262 event->id = smb_exit_event_id++;
264 DLIST_ADD(smb_exit_event_list,event);
266 return event->id;
269 BOOL smb_unregister_exit_event(smb_event_id_t id)
271 struct smb_exit_list_ent *event = smb_exit_event_list;
273 while(event) {
274 if (event->id == id) {
275 DLIST_REMOVE(smb_exit_event_list,event);
276 SAFE_FREE(event);
277 return True;
279 event = event->next;
282 return False;
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;
290 while (event) {
291 event->fn(&event->data);
292 tmp = event;
293 event = event->next;
294 /* exit event should only run one time :-)*/
295 SAFE_FREE(tmp);
298 /* the list is empty now...*/
299 smb_exit_event_list = NULL;
301 return;