Use NTSTATUS as return value for smb_register_*() functions and init_module()
[jra/samba/.git] / source3 / 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 NTSTATUS smb_load_module(const char *module_name)
26 {
27         void *handle;
28         init_module_function *init;
29         NTSTATUS 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 NT_STATUS_UNSUCCESSFUL;
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 NT_STATUS_UNSUCCESSFUL;
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(NT_STATUS_IS_OK(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 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
79 {
80         pstring full_path;
81         
82         /* Check for absolute path */
83         if(module[0] == '/')return smb_load_module(module);
84         
85         pstrcpy(full_path, lib_path(subsystem));
86         pstrcat(full_path, "/");
87         pstrcat(full_path, module);
88         pstrcat(full_path, ".");
89         pstrcat(full_path, shlib_ext());
90
91         DEBUG(5, ("Probing module %s: Trying to load from %s\n", module, full_path));
92         
93         return smb_load_module(full_path);
94 }
95
96 #else /* HAVE_DLOPEN */
97
98 NTSTATUS smb_load_module(const char *module_name)
99 {
100         DEBUG(0,("This samba executable has not been built with plugin support"));
101         return NT_STATUS_NOT_SUPPORTED;
102 }
103
104 int smb_load_modules(const char **modules)
105 {
106         DEBUG(0,("This samba executable has not been built with plugin support"));
107         return -1;
108 }
109
110 NTSTATUS smb_probe_module(const char *subsystem, const char *module)
111 {
112         DEBUG(0,("This samba executable has not been built with plugin support, not probing")); 
113         return NT_STATUS_NOT_SUPPORTED;
114 }
115
116 #endif /* HAVE_DLOPEN */
117
118 void init_modules(void)
119 {
120         /* FIXME: This can cause undefined symbol errors :
121          *  smb_register_vfs() isn't available in nmbd, for example */
122         if(lp_preload_modules()) 
123                 smb_load_modules(lp_preload_modules());
124 }
125
126
127 /*************************************************************************
128  * This functions /path/to/foobar.so -> foobar
129  ************************************************************************/
130 void module_path_get_name(const char *path, pstring name)
131 {
132         char *s;
133
134         /* First, make the path relative */
135         s = strrchr(path, '/');
136         if(s) pstrcpy(name, s+1);
137         else pstrcpy(name, path);
138         
139         if (dyn_SHLIBEXT && *dyn_SHLIBEXT && strlen(dyn_SHLIBEXT) < strlen(name)) {
140                 int n = strlen(name) - strlen(dyn_SHLIBEXT);
141                 
142                 /* Remove extension if necessary */
143                 if (name[n-1] == '.' && !strcmp(name+n, dyn_SHLIBEXT)) {
144                         name[n-1] = '\0';
145                 }
146         }
147 }