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