2 Unix SMB/CIFS implementation.
5 Copyright (C) Jelmer Vernooij 2002-2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* Load module (or directory with modules) recursively.
27 * Includes running the init_module() function */
28 NTSTATUS smb_load_module(const char *module_name)
31 init_module_function init;
36 struct dirent *dirent;
38 stat(module_name, &st);
40 /* If the argument is a directory, recursively load all files /
41 * directories in it */
43 /* How about symlinks pointing to themselves - wouldn't we rather
44 * want to use wildcards here? */
45 if(S_ISDIR(st.st_mode)) {
46 dir = opendir(module_name);
47 while ((dirent = readdir(dir))) {
48 smb_load_module(dirent->d_name);
52 /* Always try to use LAZY symbol resolving; if the plugin has
53 * backwards compatibility, there might be symbols in the
54 * plugin referencing to old (removed) functions
56 handle = sys_dlopen(module_name, RTLD_LAZY);
59 DEBUG(0, ("Error loading module '%s': %s\n", module_name, sys_dlerror()));
60 return NT_STATUS_UNSUCCESSFUL;
63 init = (init_module_function)sys_dlsym(handle, "init_module");
65 /* we must check sys_dlerror() to determine if it worked, because
66 sys_dlsym() can validly return NULL */
67 error = sys_dlerror();
69 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n", module_name, error));
70 return NT_STATUS_UNSUCCESSFUL;
75 DEBUG(2, ("Module '%s' loaded\n", module_name));
80 /* Load all modules in list and return number of
81 * modules that has been successfully loaded */
82 int smb_load_modules(const char **modules)
87 for(i = 0; modules[i]; i++){
88 if(NT_STATUS_IS_OK(smb_load_module(modules[i]))) {
93 DEBUG(2, ("%d modules successfully loaded\n", success));
98 #else /* HAVE_DLOPEN */
100 NTSTATUS smb_load_module(const char *module_name)
102 DEBUG(0,("This samba executable has not been built with plugin support\n"));
103 return NT_STATUS_NOT_SUPPORTED;
106 int smb_load_modules(const char **modules)
108 DEBUG(0,("This samba executable has not been built with plugin support\n"));
112 #endif /* HAVE_DLOPEN */
114 void init_modules(void)
116 if(lp_preload_modules())
117 smb_load_modules(lp_preload_modules());
122 register_backend_function callback;
123 struct subsystem *prev, *next;
126 struct subsystem *subsystems = NULL;
128 void register_subsystem(const char *name, register_backend_function callback)
132 s = smb_xmalloc(sizeof(struct subsystem));
134 s->name = smb_xstrdup(name);
135 s->callback = callback;
136 s->prev = s->next = NULL;
138 DLIST_ADD(subsystems, s);
141 NTSTATUS register_backend(const char *subsystem, void *args)
143 /* Find the specified subsystem */
144 struct subsystem *s = subsystems;
147 if(!strcmp(subsystem, s->name)) return s->callback(args);
151 return NT_STATUS_NOT_IMPLEMENTED;