672d8df7cefd587715d78111a2098cd2b34c514b
[samba.git] / source / 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "system/dir.h"
23
24 static void *load_module(TALLOC_CTX *mem_ctx, const char *dir, const char *name)
25 {
26         char *path;
27         void *handle;
28         void *init_fn;
29
30         path = talloc_asprintf(mem_ctx, "%s/%s", dir, name);
31
32         handle = dlopen(path, RTLD_NOW);
33         if (handle == NULL) {
34                 DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
35                 talloc_free(path);
36                 return NULL;
37         }
38
39         init_fn = dlsym(handle, "init_module");
40
41         if (init_fn == NULL) {
42                 DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror()));
43                 DEBUG(1, ("Loading module '%s' failed\n", path));
44                 dlclose(handle);
45                 talloc_free(path);
46                 return NULL;
47         }
48
49         talloc_free(path);
50
51         return init_fn;
52 }
53
54 init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
55 {
56         DIR *dir;
57         struct dirent *entry;
58         int success = 0;
59         init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
60
61         ret[0] = NULL;
62         
63         dir = opendir(path);
64         if (dir == NULL) {
65                 talloc_free(ret);
66                 return NULL;
67         }
68
69         while((entry = readdir(dir))) {
70                 if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
71                         continue;
72
73                 ret[success] = load_module(mem_ctx, path, entry->d_name);
74                 if (ret[success]) {
75                         ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
76                         success++;
77                         ret[success] = NULL;
78                 }
79         }
80
81         closedir(dir);
82
83         return ret;
84 }
85
86 BOOL run_init_functions(NTSTATUS (**fns) (void))
87 {
88         int i;
89         BOOL ret;
90         
91         if (fns == NULL)
92                 return True;
93         
94         for (i = 0; fns[i]; i++) { ret &= NT_STATUS_IS_OK(fns[i]()); }
95
96         return ret;
97 }