first public release of samba4 code
[samba.git] / source4 / lib / module.c
1 /* 
2    Unix SMB/CIFS implementation.
3    module loading system
4
5    Copyright (C) Jelmer Vernooij 2002
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 int smb_load_module(const char *module_name)
26 {
27         void *handle;
28         init_module_function *init;
29         int status;
30         const char *error;
31
32         /* Always try to use LAZY symbol resolving; if the plugin has 
33          * backwards compatibility, there might be symbols in the 
34          * plugin referencing to old (removed) functions
35          */
36         handle = sys_dlopen(module_name, RTLD_LAZY);
37
38         if(!handle) {
39                 DEBUG(0, ("Error loading module '%s': %s\n", module_name, sys_dlerror()));
40                 return False;
41         }
42
43         init = sys_dlsym(handle, "init_module");
44
45         /* we must check sys_dlerror() to determine if it worked, because
46            sys_dlsym() can validly return NULL */
47         error = sys_dlerror();
48         if (error) {
49                 DEBUG(0, ("Error trying to resolve symbol 'init_module' in %s: %s\n", module_name, error));
50                 return False;
51         }
52
53         status = init();
54
55         DEBUG(2, ("Module '%s' loaded\n", module_name));
56
57         return status;
58 }
59
60 /* Load all modules in list and return number of 
61  * modules that has been successfully loaded */
62 int smb_load_modules(const char **modules)
63 {
64         int i;
65         int success = 0;
66
67         for(i = 0; modules[i]; i++){
68                 if(smb_load_module(modules[i])) {
69                         success++;
70                 }
71         }
72
73         DEBUG(2, ("%d modules successfully loaded\n", success));
74
75         return success;
76 }
77
78 int smb_probe_module(const char *subsystem, const char *module)
79 {
80         char *full_path;
81         int rc;
82         TALLOC_CTX *mem_ctx;
83         
84         /* Check for absolute path */
85         if(module[0] == '/')return smb_load_module(module);
86         
87         mem_ctx = talloc_init("smb_probe_module");
88         if (!mem_ctx) {
89                 DEBUG(0,("No memory for loading modules\n"));
90                 return False;
91         }
92         full_path = talloc_strdup(mem_ctx, lib_path(mem_ctx, subsystem));
93         full_path = talloc_asprintf(mem_ctx, "%s/%s.%s", 
94                 full_path, module, shlib_ext());
95         
96         rc = smb_load_module(full_path);
97         talloc_destroy(mem_ctx);
98         return rc;
99 }
100
101 #else /* HAVE_DLOPEN */
102
103 int smb_load_module(const char *module_name)
104 {
105         DEBUG(0,("This samba executable has not been built with plugin support"));
106         return False;
107 }
108
109 int smb_load_modules(const char **modules)
110 {
111         DEBUG(0,("This samba executable has not been built with plugin support"));
112         return False;
113 }
114
115 int smb_probe_module(const char *subsystem, const char *module)
116 {
117         DEBUG(0,("This samba executable has not been built with plugin support, not probing")); 
118         return False;
119 }
120
121 #endif /* HAVE_DLOPEN */
122
123 void init_modules(void)
124 {
125         if(lp_preload_modules()) 
126                 smb_load_modules(lp_preload_modules());
127         /* FIXME: load static modules */
128 }