Fix the events API. Patch by metze with some minor modifications.
[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 NTSTATUS smb_load_module(const char *module_name)
27 {
28         void *handle;
29         init_module_function *init;
30         NTSTATUS status;
31         const char *error;
32
33         /* Always try to use LAZY symbol resolving; if the plugin has 
34          * backwards compatibility, there might be symbols in the 
35          * plugin referencing to old (removed) functions
36          */
37         handle = sys_dlopen(module_name, RTLD_LAZY);
38
39         if(!handle) {
40                 DEBUG(0, ("Error loading module '%s': %s\n", module_name, sys_dlerror()));
41                 return NT_STATUS_UNSUCCESSFUL;
42         }
43
44         init = sys_dlsym(handle, "init_module");
45
46         /* we must check sys_dlerror() to determine if it worked, because
47            sys_dlsym() can validly return NULL */
48         error = sys_dlerror();
49         if (error) {
50                 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n", module_name, error));
51                 return NT_STATUS_UNSUCCESSFUL;
52         }
53
54         status = init();
55
56         DEBUG(2, ("Module '%s' loaded\n", module_name));
57
58         return status;
59 }
60
61 /* Load all modules in list and return number of 
62  * modules that has been successfully loaded */
63 int smb_load_modules(const char **modules)
64 {
65         int i;
66         int success = 0;
67
68         for(i = 0; modules[i]; i++){
69                 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
70                         success++;
71                 }
72         }
73
74         DEBUG(2, ("%d modules successfully loaded\n", success));
75
76         return success;
77 }
78
79 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
80 {
81         pstring full_path;
82         
83         /* Check for absolute path */
84
85         /* if we make any 'samba multibyte string' 
86            calls here, we break 
87            for loading string modules */
88         if (module[0] == '/')
89                 return smb_load_module(module);
90         
91         pstrcpy(full_path, lib_path(subsystem));
92         pstrcat(full_path, "/");
93         pstrcat(full_path, module);
94         pstrcat(full_path, ".");
95         pstrcat(full_path, shlib_ext());
96
97         DEBUG(5, ("Probing module %s: Trying to load from %s\n", module, full_path));
98         
99         return smb_load_module(full_path);
100 }
101
102 #else /* HAVE_DLOPEN */
103
104 NTSTATUS smb_load_module(const char *module_name)
105 {
106         DEBUG(0,("This samba executable has not been built with plugin support\n"));
107         return NT_STATUS_NOT_SUPPORTED;
108 }
109
110 int smb_load_modules(const char **modules)
111 {
112         DEBUG(0,("This samba executable has not been built with plugin support\n"));
113         return -1;
114 }
115
116 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
117 {
118         DEBUG(0,("This samba executable has not been built with plugin support, not probing\n")); 
119         return NT_STATUS_NOT_SUPPORTED;
120 }
121
122 #endif /* HAVE_DLOPEN */
123
124 void init_modules(void)
125 {
126         /* FIXME: This can cause undefined symbol errors :
127          *  smb_register_vfs() isn't available in nmbd, for example */
128         if(lp_preload_modules()) 
129                 smb_load_modules(lp_preload_modules());
130 }
131
132
133 /*************************************************************************
134  * This functions /path/to/foobar.so -> foobar
135  ************************************************************************/
136 void module_path_get_name(const char *path, pstring name)
137 {
138         char *s;
139
140         /* First, make the path relative */
141         s = strrchr(path, '/');
142         if(s) pstrcpy(name, s+1);
143         else pstrcpy(name, path);
144         
145         if (dyn_SHLIBEXT && *dyn_SHLIBEXT && strlen(dyn_SHLIBEXT) < strlen(name)) {
146                 int n = strlen(name) - strlen(dyn_SHLIBEXT);
147                 
148                 /* Remove extension if necessary */
149                 if (name[n-1] == '.' && !strcmp(name+n, dyn_SHLIBEXT)) {
150                         name[n-1] = '\0';
151                 }
152         }
153 }
154
155
156 /***************************************************************************
157  * This Function registers a idle event
158  *
159  * the registered funtions are run periodically
160  * and maybe shutdown idle connections (e.g. to an LDAP server)
161  ***************************************************************************/
162 static smb_event_id_t smb_idle_event_id = 1;
163
164 struct smb_idle_list_ent {
165         struct smb_idle_list_ent *prev,*next;
166         smb_event_id_t id;
167         smb_idle_event_fn *fn;
168         void *data;
169         time_t interval;
170         time_t lastrun;
171 };
172
173 static struct smb_idle_list_ent *smb_idle_event_list = NULL;
174
175 smb_event_id_t smb_register_idle_event(smb_idle_event_fn *fn, void *data, time_t interval)
176 {
177         struct smb_idle_list_ent *event;
178
179         if (!fn) {      
180                 return SMB_EVENT_ID_INVALID;
181         }
182
183         event = (struct smb_idle_list_ent *)malloc(sizeof(struct smb_idle_list_ent));
184         if (!event) {
185                 DEBUG(0,("malloc() failed!\n"));
186                 return SMB_EVENT_ID_INVALID;
187         }
188         event->fn = fn;
189         event->data = data;
190         event->interval = interval;
191         event->lastrun = 0;
192         event->id = smb_idle_event_id++;
193
194         DLIST_ADD(smb_idle_event_list,event);
195
196         return event->id;
197 }
198
199 BOOL smb_unregister_idle_event(smb_event_id_t id)
200 {
201         struct smb_idle_list_ent *event = smb_idle_event_list;
202         
203         while(event) {
204                 if (event->id == id) {
205                         DLIST_REMOVE(smb_idle_event_list,event);
206                         SAFE_FREE(event);
207                         return True;
208                 }
209                 event = event->next;
210         }
211         
212         return False;
213 }
214
215 void smb_run_idle_events(time_t now)
216 {
217         struct smb_idle_list_ent *event = smb_idle_event_list;
218
219         while (event) {
220                 time_t interval;
221
222                 if (event->interval >= SMB_IDLE_EVENT_MIN_INTERVAL) {
223                         interval = event->interval;
224                 } else {
225                         interval = SMB_IDLE_EVENT_MIN_INTERVAL;
226                 }
227                 if (now >(event->lastrun+interval)) {
228                         event->fn(&event->data,&event->interval,now);
229                         event->lastrun = now;
230                 }
231                 event = event->next;
232         }
233
234         return;
235 }
236
237 /***************************************************************************
238  * This Function registers a exit event
239  *
240  * the registered functions are run on exit()
241  * and maybe shutdown idle connections (e.g. to an LDAP server)
242  ***************************************************************************/
243
244 struct smb_exit_list_ent {
245         struct smb_exit_list_ent *prev,*next;
246         smb_event_id_t id;
247         smb_exit_event_fn *fn;
248         void *data;
249 };
250
251 static struct smb_exit_list_ent *smb_exit_event_list = NULL;
252
253 smb_event_id_t smb_register_exit_event(smb_exit_event_fn *fn, void *data)
254 {
255         struct smb_exit_list_ent *event;
256         static smb_event_id_t smb_exit_event_id = 1;
257
258         if (!fn) {      
259                 return SMB_EVENT_ID_INVALID;
260         }
261
262         event = (struct smb_exit_list_ent *)malloc(sizeof(struct smb_exit_list_ent));
263         if (!event) {
264                 DEBUG(0,("malloc() failed!\n"));
265                 return SMB_EVENT_ID_INVALID;
266         }
267         event->fn = fn;
268         event->data = data;
269         event->id = smb_exit_event_id++;
270
271         DLIST_ADD(smb_exit_event_list,event);
272
273         return event->id;
274 }
275
276 BOOL smb_unregister_exit_event(smb_event_id_t id)
277 {
278         struct smb_exit_list_ent *event = smb_exit_event_list;
279         
280         while(event) {
281                 if (event->id == id) {
282                         DLIST_REMOVE(smb_exit_event_list,event);
283                         SAFE_FREE(event);
284                         return True;
285                 }
286                 event = event->next;
287         }
288         
289         return False;
290 }
291
292 void smb_run_exit_events(void)
293 {
294         struct smb_exit_list_ent *event = smb_exit_event_list;
295         struct smb_exit_list_ent *tmp = NULL;
296
297         while (event) {
298                 event->fn(&event->data);
299                 tmp = event;
300                 event = event->next;
301                 /* exit event should only run one time :-)*/
302                 SAFE_FREE(tmp);
303         }
304
305         /* the list is empty now...*/
306         smb_exit_event_list = NULL;
307
308         return;
309 }