Unregister event fix from metze.
[amitay/samba.git] / source3 / lib / module.c
1 /* 
2    Unix SMB/CIFS implementation.
3    module loading system
4
5    Copyright (C) Jelmer Vernooij 2002-2003
6    Copyright (C) Stefan (metze) Metzmacher 2003
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24
25 #ifdef HAVE_DLOPEN
26
27 /* Load a dynamic module.  Only log a level 0 error if we are not checking 
28    for the existence of a module (probling). */
29
30 static NTSTATUS do_smb_load_module(const char *module_name, BOOL is_probe)
31 {
32         void *handle;
33         init_module_function *init;
34         NTSTATUS status;
35         const char *error;
36
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
40          */
41         handle = sys_dlopen(module_name, RTLD_LAZY);
42
43         if(!handle) {
44                 int level = is_probe ? 3 : 0;
45                 DEBUG(level, ("Error loading module '%s': %s\n", module_name, 
46                               sys_dlerror()));
47
48                 return NT_STATUS_UNSUCCESSFUL;
49         }
50
51         init = (init_module_function *)sys_dlsym(handle, "init_module");
52
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;
60         }
61
62         status = init();
63
64         DEBUG(2, ("Module '%s' loaded\n", module_name));
65
66         return status;
67 }
68
69 NTSTATUS smb_load_module(const char *module_name)
70 {
71         return do_smb_load_module(module_name, False);
72 }
73
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)
77 {
78         int i;
79         int success = 0;
80
81         for(i = 0; modules[i]; i++){
82                 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
83                         success++;
84                 }
85         }
86
87         DEBUG(2, ("%d modules successfully loaded\n", success));
88
89         return success;
90 }
91
92 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
93 {
94         pstring full_path;
95         
96         /* Check for absolute path */
97
98         /* if we make any 'samba multibyte string' 
99            calls here, we break 
100            for loading string modules */
101
102         DEBUG(5, ("Probing module '%s'\n", module));
103
104         if (module[0] == '/')
105                 return do_smb_load_module(module, True);
106         
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());
112
113         DEBUG(5, ("Probing module '%s': Trying to load from %s\n", module, full_path));
114         
115         return do_smb_load_module(full_path, True);
116 }
117
118 #else /* HAVE_DLOPEN */
119
120 NTSTATUS smb_load_module(const char *module_name)
121 {
122         DEBUG(0,("This samba executable has not been built with plugin support\n"));
123         return NT_STATUS_NOT_SUPPORTED;
124 }
125
126 int smb_load_modules(const char **modules)
127 {
128         DEBUG(0,("This samba executable has not been built with plugin support\n"));
129         return -1;
130 }
131
132 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
133 {
134         DEBUG(0,("This samba executable has not been built with plugin support, not probing\n")); 
135         return NT_STATUS_NOT_SUPPORTED;
136 }
137
138 #endif /* HAVE_DLOPEN */
139
140 void init_modules(void)
141 {
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());
146 }
147
148
149 /***************************************************************************
150  * This Function registers a idle event
151  *
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;
156
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;
164 };
165
166 static struct smb_idle_list_ent *smb_idle_event_list = NULL;
167
168 smb_event_id_t smb_register_idle_event(smb_idle_event_fn *fn, void *data, time_t interval)
169 {
170         struct smb_idle_list_ent *event;
171
172         if (!fn) {      
173                 return SMB_EVENT_ID_INVALID;
174         }
175
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;
180         }
181         event->fn = fn;
182         event->data = data;
183         event->interval = interval;
184         event->lastrun = 0;
185         event->id = smb_idle_event_id++;
186
187         DLIST_ADD(smb_idle_event_list,event);
188
189         return event->id;
190 }
191
192 BOOL smb_unregister_idle_event(smb_event_id_t id)
193 {
194         struct smb_idle_list_ent *event = smb_idle_event_list;
195         
196         while(event) {
197                 if (event->id == id) {
198                         DLIST_REMOVE(smb_idle_event_list,event);
199                         SAFE_FREE(event);
200                         return True;
201                 }
202                 event = event->next;
203         }
204         
205         return False;
206 }
207
208 void smb_run_idle_events(time_t now)
209 {
210         struct smb_idle_list_ent *event = smb_idle_event_list;
211
212         while (event) {
213                 struct smb_idle_list_ent *next = event->next;
214                 time_t interval;
215
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;
222                 }
223                 if (now >(event->lastrun+interval)) {
224                         event->lastrun = now;
225                         event->fn(&event->data,&event->interval,now);
226                 }
227                 event = next;
228         }
229
230         return;
231 }
232
233 /***************************************************************************
234  * This Function registers a exit event
235  *
236  * the registered functions are run on exit()
237  * and maybe shutdown idle connections (e.g. to an LDAP server)
238  ***************************************************************************/
239
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;
245 };
246
247 static struct smb_exit_list_ent *smb_exit_event_list = NULL;
248
249 smb_event_id_t smb_register_exit_event(smb_exit_event_fn *fn, void *data)
250 {
251         struct smb_exit_list_ent *event;
252         static smb_event_id_t smb_exit_event_id = 1;
253
254         if (!fn) {      
255                 return SMB_EVENT_ID_INVALID;
256         }
257
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;
262         }
263         event->fn = fn;
264         event->data = data;
265         event->id = smb_exit_event_id++;
266
267         DLIST_ADD(smb_exit_event_list,event);
268
269         return event->id;
270 }
271
272 BOOL smb_unregister_exit_event(smb_event_id_t id)
273 {
274         struct smb_exit_list_ent *event = smb_exit_event_list;
275         
276         while(event) {
277                 if (event->id == id) {
278                         DLIST_REMOVE(smb_exit_event_list,event);
279                         SAFE_FREE(event);
280                         return True;
281                 }
282                 event = event->next;
283         }
284         
285         return False;
286 }
287
288 void smb_run_exit_events(void)
289 {
290         struct smb_exit_list_ent *event = smb_exit_event_list;
291         struct smb_exit_list_ent *tmp = NULL;
292
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);
299         }
300
301         /* the list is empty now...*/
302         smb_exit_event_list = NULL;
303
304         return;
305 }