source: branches/samba-3.0/source/lib/module.c@ 160

Last change on this file since 160 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

File size: 6.0 KB
Line 
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
30static 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 /* This call should reset any possible non-fatal errors that
44 occured since last call to dl* functions */
45 error = sys_dlerror();
46
47 if(!handle) {
48 int level = is_probe ? 3 : 0;
49 DEBUG(level, ("Error loading module '%s': %s\n", module_name, error ? error : ""));
50 return NT_STATUS_UNSUCCESSFUL;
51 }
52
53 init = (init_module_function *)sys_dlsym(handle, "init_module");
54
55 /* we must check sys_dlerror() to determine if it worked, because
56 sys_dlsym() can validly return NULL */
57 error = sys_dlerror();
58 if (error) {
59 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n",
60 module_name, error));
61 return NT_STATUS_UNSUCCESSFUL;
62 }
63
64 DEBUG(2, ("Module '%s' loaded\n", module_name));
65
66 status = init();
67 if (!NT_STATUS_IS_OK(status)) {
68 DEBUG(0, ("Module '%s' initialization failed: %s\n",
69 module_name, get_friendly_nt_error_msg(status)));
70 }
71
72 return status;
73}
74
75NTSTATUS smb_load_module(const char *module_name)
76{
77 return do_smb_load_module(module_name, False);
78}
79
80/* Load all modules in list and return number of
81 * modules that has been successfully loaded */
82int smb_load_modules(const char **modules)
83{
84 int i;
85 int success = 0;
86
87 for(i = 0; modules[i]; i++){
88 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
89 success++;
90 }
91 }
92
93 DEBUG(2, ("%d modules successfully loaded\n", success));
94
95 return success;
96}
97
98NTSTATUS smb_probe_module(const char *subsystem, const char *module)
99{
100 pstring full_path;
101
102 /* Check for absolute path */
103
104 /* if we make any 'samba multibyte string'
105 calls here, we break
106 for loading string modules */
107
108 DEBUG(5, ("Probing module '%s'\n", module));
109
110 if (module[0] == '/')
111 return do_smb_load_module(module, True);
112
113 pstrcpy(full_path, lib_path(subsystem));
114 pstrcat(full_path, "/");
115 pstrcat(full_path, module);
116 pstrcat(full_path, ".");
117 pstrcat(full_path, shlib_ext());
118
119 DEBUG(5, ("Probing module '%s': Trying to load from %s\n", module, full_path));
120
121 return do_smb_load_module(full_path, True);
122}
123
124#else /* HAVE_DLOPEN */
125
126NTSTATUS smb_load_module(const char *module_name)
127{
128 DEBUG(0,("This samba executable has not been built with plugin support\n"));
129 return NT_STATUS_NOT_SUPPORTED;
130}
131
132int smb_load_modules(const char **modules)
133{
134 DEBUG(0,("This samba executable has not been built with plugin support\n"));
135 return -1;
136}
137
138NTSTATUS smb_probe_module(const char *subsystem, const char *module)
139{
140 DEBUG(0,("This samba executable has not been built with plugin support, not probing\n"));
141 return NT_STATUS_NOT_SUPPORTED;
142}
143
144#endif /* HAVE_DLOPEN */
145
146void init_modules(void)
147{
148 /* FIXME: This can cause undefined symbol errors :
149 * smb_register_vfs() isn't available in nmbd, for example */
150 if(lp_preload_modules())
151 smb_load_modules(lp_preload_modules());
152}
153
154
155/***************************************************************************
156 * This Function registers a idle event
157 *
158 * the registered funtions are run periodically
159 * and maybe shutdown idle connections (e.g. to an LDAP server)
160 ***************************************************************************/
161static smb_event_id_t smb_idle_event_id = 1;
162
163struct smb_idle_list_ent {
164 struct smb_idle_list_ent *prev,*next;
165 smb_event_id_t id;
166 smb_idle_event_fn *fn;
167 void *data;
168 time_t interval;
169 time_t lastrun;
170};
171
172static struct smb_idle_list_ent *smb_idle_event_list = NULL;
173
174smb_event_id_t smb_register_idle_event(smb_idle_event_fn *fn, void *data, time_t interval)
175{
176 struct smb_idle_list_ent *event;
177
178 if (!fn) {
179 return SMB_EVENT_ID_INVALID;
180 }
181
182 event = SMB_MALLOC_P(struct smb_idle_list_ent);
183 if (!event) {
184 DEBUG(0,("malloc() failed!\n"));
185 return SMB_EVENT_ID_INVALID;
186 }
187 event->fn = fn;
188 event->data = data;
189 event->interval = interval;
190 event->lastrun = 0;
191 event->id = smb_idle_event_id++;
192
193 DLIST_ADD(smb_idle_event_list,event);
194
195 return event->id;
196}
197
198BOOL smb_unregister_idle_event(smb_event_id_t id)
199{
200 struct smb_idle_list_ent *event = smb_idle_event_list;
201
202 while(event) {
203 if (event->id == id) {
204 DLIST_REMOVE(smb_idle_event_list,event);
205 SAFE_FREE(event);
206 return True;
207 }
208 event = event->next;
209 }
210
211 return False;
212}
213
214void smb_run_idle_events(time_t now)
215{
216 struct smb_idle_list_ent *event = smb_idle_event_list;
217
218 while (event) {
219 struct smb_idle_list_ent *next = event->next;
220 time_t interval;
221
222 if (event->interval <= 0) {
223 interval = SMB_IDLE_EVENT_DEFAULT_INTERVAL;
224 } else if (event->interval >= SMB_IDLE_EVENT_MIN_INTERVAL) {
225 interval = event->interval;
226 } else {
227 interval = SMB_IDLE_EVENT_MIN_INTERVAL;
228 }
229 if (now >(event->lastrun+interval)) {
230 event->lastrun = now;
231 event->fn(&event->data,&event->interval,now);
232 }
233 event = next;
234 }
235
236 return;
237}
Note: See TracBrowser for help on using the repository browser.