r15186: Introduce ISDOT and ISDOTDOT macros for testing whether a filename is
[metze/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 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 /**
22  * @file
23  * @brief Module initialization function handling
24  */
25
26 #include "includes.h"
27 #include "system/dir.h"
28
29 /**
30  * Obtain the init function from a shared library file
31  */
32 _PUBLIC_ init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
33 {
34         void *handle;
35         void *init_fn;
36
37         handle = dlopen(path, RTLD_NOW);
38         if (handle == NULL) {
39                 DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
40                 return NULL;
41         }
42
43         init_fn = dlsym(handle, "init_module");
44
45         if (init_fn == NULL) {
46                 DEBUG(0, ("Unable to find init_module() in %s: %s\n", path, dlerror()));
47                 DEBUG(1, ("Loading module '%s' failed\n", path));
48                 dlclose(handle);
49                 return NULL;
50         }
51
52         return (init_module_fn)init_fn;
53 }
54
55 /**
56  * Obtain list of init functions from the modules in the specified
57  * directory
58  */
59 _PUBLIC_ init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
60 {
61         DIR *dir;
62         struct dirent *entry;
63         char *filename;
64         int success = 0;
65         init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
66
67         ret[0] = NULL;
68         
69         dir = opendir(path);
70         if (dir == NULL) {
71                 talloc_free(ret);
72                 return NULL;
73         }
74
75         while((entry = readdir(dir))) {
76                 if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
77                         continue;
78
79                 filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
80
81                 ret[success] = load_module(mem_ctx, filename);
82                 if (ret[success]) {
83                         ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
84                         success++;
85                         ret[success] = NULL;
86                 }
87
88                 talloc_free(filename);
89         }
90
91         closedir(dir);
92
93         return ret;
94 }
95
96 /**
97  * Run the specified init functions.
98  *
99  * @return True if all functions ran successfully, False otherwise
100  */
101 _PUBLIC_ BOOL run_init_functions(NTSTATUS (**fns) (void))
102 {
103         int i;
104         BOOL ret = True;
105         
106         if (fns == NULL)
107                 return True;
108         
109         for (i = 0; fns[i]; i++) { ret &= (BOOL)NT_STATUS_IS_OK(fns[i]()); }
110
111         return ret;
112 }