r23792: convert Samba4 to GPLv3
[bbaumbach/samba-autobuild/.git] / source4 / lib / util / module.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Jelmer Vernooij 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /**
21  * @file
22  * @brief Module initialization function handling
23  */
24
25 #include "includes.h"
26 #include "system/dir.h"
27
28 /**
29  * Obtain the init function from a shared library file
30  */
31 _PUBLIC_ init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
32 {
33         void *handle;
34         void *init_fn;
35
36         handle = dlopen(path, RTLD_NOW);
37         if (handle == NULL) {
38                 DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
39                 return NULL;
40         }
41
42         init_fn = dlsym(handle, "init_module");
43
44         if (init_fn == NULL) {
45                 DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror()));
46                 DEBUG(1, ("Loading module '%s' failed\n", path));
47                 dlclose(handle);
48                 return NULL;
49         }
50
51         return (init_module_fn)init_fn;
52 }
53
54 /**
55  * Obtain list of init functions from the modules in the specified
56  * directory
57  */
58 _PUBLIC_ init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
59 {
60         DIR *dir;
61         struct dirent *entry;
62         char *filename;
63         int success = 0;
64         init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
65
66         ret[0] = NULL;
67         
68         dir = opendir(path);
69         if (dir == NULL) {
70                 talloc_free(ret);
71                 return NULL;
72         }
73
74         while((entry = readdir(dir))) {
75                 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
76                         continue;
77
78                 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
79
80                 ret[success] = load_module(mem_ctx, filename);
81                 if (ret[success]) {
82                         ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
83                         success++;
84                         ret[success] = NULL;
85                 }
86
87                 talloc_free(filename);
88         }
89
90         closedir(dir);
91
92         return ret;
93 }
94
95 /**
96  * Run the specified init functions.
97  *
98  * @return True if all functions ran successfully, False otherwise
99  */
100 _PUBLIC_ BOOL run_init_functions(NTSTATUS (**fns) (void))
101 {
102         int i;
103         BOOL ret = True;
104         
105         if (fns == NULL)
106                 return True;
107         
108         for (i = 0; fns[i]; i++) { ret &= (BOOL)NT_STATUS_IS_OK(fns[i]()); }
109
110         return ret;
111 }