fixed some warnings
[samba.git] / source4 / lib / module.c
1 /* 
2    Unix SMB/CIFS implementation.
3    module loading system
4
5    Copyright (C) Jelmer Vernooij 2002-2003
6    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23
24 #ifdef HAVE_DLOPEN
25
26 /* Load module (or directory with modules) recursively. 
27  * Includes running the init_module() function */
28 NTSTATUS smb_load_module(const char *module_name)
29 {
30         void *handle;
31         init_module_function init;
32         NTSTATUS status;
33         const char *error;
34         struct stat st;
35         DIR *dir;
36         struct dirent *dirent;
37
38         stat(module_name, &st);
39
40         /* If the argument is a directory, recursively load all files / 
41          * directories in it */
42
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);
49                 }
50         }
51
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
55          */
56         handle = sys_dlopen(module_name, RTLD_LAZY);
57
58         if(!handle) {
59                 DEBUG(0, ("Error loading module '%s': %s\n", module_name, sys_dlerror()));
60                 return NT_STATUS_UNSUCCESSFUL;
61         }
62
63         init = (init_module_function)sys_dlsym(handle, "init_module");
64
65         /* we must check sys_dlerror() to determine if it worked, because
66            sys_dlsym() can validly return NULL */
67         error = sys_dlerror();
68         if (error) {
69                 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n", module_name, error));
70                 return NT_STATUS_UNSUCCESSFUL;
71         }
72
73         status = init();
74
75         DEBUG(2, ("Module '%s' loaded\n", module_name));
76
77         return status;
78 }
79
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)
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
98 #else /* HAVE_DLOPEN */
99
100 NTSTATUS smb_load_module(const char *module_name)
101 {
102         DEBUG(0,("This samba executable has not been built with plugin support\n"));
103         return NT_STATUS_NOT_SUPPORTED;
104 }
105
106 int smb_load_modules(const char **modules)
107 {
108         DEBUG(0,("This samba executable has not been built with plugin support\n"));
109         return -1;
110 }
111
112 #endif /* HAVE_DLOPEN */
113
114 void init_modules(void)
115 {
116         if(lp_preload_modules()) 
117                 smb_load_modules(lp_preload_modules());
118 }
119
120 struct subsystem {
121         char *name;
122         register_backend_function callback;
123         struct subsystem *prev, *next;
124 };
125
126 struct subsystem *subsystems = NULL;
127
128 void register_subsystem(const char *name, register_backend_function callback) 
129 {
130         struct subsystem *s;
131
132         s = smb_xmalloc(sizeof(struct subsystem));
133
134         s->name = smb_xstrdup(name);
135         s->callback = callback;
136         s->prev = s->next = NULL;
137
138         DLIST_ADD(subsystems, s);
139 }
140
141 NTSTATUS register_backend(const char *subsystem, void *args)
142 {
143         /* Find the specified subsystem */
144         struct subsystem *s = subsystems;
145
146         while(s) {
147                 if(!strcmp(subsystem, s->name)) return s->callback(args);
148                 s = s->next;
149         }
150
151         return NT_STATUS_NOT_IMPLEMENTED;
152 }