]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/plugin.c
Zeroth integration of plugins
[bacula/bacula] / bacula / src / lib / plugin.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation, which is 
11    listed in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *    Plugin load/unloader for all Bacula daemons
30  *
31  * Kern Sibbald, October 2007
32  */
33 #include "bacula.h"
34 #include <dlfcn.h>
35 #include "plugin.h"
36
37 /* All loaded plugins */
38 alist *plugin_list;
39
40 /*
41  * Create a new plugin "class" entry and enter it in the
42  *  list of plugins.  Note, this is not the same as
43  *  an instance of the plugin. 
44  */
45 Plugin *new_plugin()
46 {
47    Plugin *plugin;
48
49    plugin = (Plugin *)malloc(sizeof(Plugin));
50    memset(plugin, 0, sizeof(Plugin));
51    plugin_list->append(plugin);
52    return plugin;
53 }
54
55
56 /*
57  * Load all the plugins in the specified directory.
58  */
59 bool load_plugins(void *bfuncs, const char *plugin_dir, const char *type)
60 {
61    t_loadPlugin loadPlugin;
62    Plugin *plugin;
63    char *error;
64    DIR* dp = NULL;
65    struct dirent *entry, *result;
66    int name_max;
67    struct stat statp;
68    bool found = false;
69    POOL_MEM fname(PM_FNAME);
70    bool need_slash = false;
71    int len, type_len;
72
73    plugin = new_plugin();
74
75    name_max = pathconf(".", _PC_NAME_MAX);
76    if (name_max < 1024) {
77       name_max = 1024;
78    }
79
80    if (!(dp = opendir(plugin_dir))) {
81       berrno be;
82       Dmsg2(29, "load_plugins: failed to open dir %s: ERR=%s\n", 
83             plugin_dir, be.bstrerror());
84       goto get_out;
85    }
86    
87    len = strlen(plugin_dir);
88    if (len > 0) {
89       need_slash = !IsPathSeparator(plugin_dir[len - 1]);
90    }
91    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
92    for ( ;; ) {
93       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
94          Dmsg1(129, "load_plugins: failed to find suitable file in dir %s\n", 
95                plugin_dir);
96          break;
97       }
98       if (strcmp(result->d_name, ".") == 0 || 
99           strcmp(result->d_name, "..") == 0) {
100          continue;
101       }
102
103       len = strlen(result->d_name);
104       type_len = strlen(type);
105       if (len < type_len+1 || strcmp(&result->d_name[len-type_len], type) != 0) {
106          continue;
107       }
108       printf("Got: name=%s len=%d\n", result->d_name, len);
109        
110       pm_strcpy(fname, plugin_dir);
111       if (need_slash) {
112          pm_strcat(fname, "/");
113       }
114       pm_strcat(fname, result->d_name);
115       if (lstat(fname.c_str(), &statp) != 0 || !S_ISREG(statp.st_mode)) {
116          continue;                 /* ignore directories & special files */
117       }
118
119       plugin->file = bstrdup(result->d_name);
120       plugin->pHandle = dlopen(fname.c_str(), RTLD_NOW);
121       if (!plugin->pHandle) {
122          printf("dlopen of %s failed: ERR=%s\n", fname.c_str(), dlerror());
123          goto get_out;
124       }
125
126       /* Get two global entry points */
127       loadPlugin = (t_loadPlugin)dlsym(plugin->pHandle, "loadPlugin");
128       if ((error=dlerror()) != NULL) {
129          printf("dlsym failed: ERR=%s\n", error);
130          goto get_out;
131       }
132       plugin->unloadPlugin = (t_unloadPlugin)dlsym(plugin->pHandle, "unloadPlugin");
133       if ((error=dlerror()) != NULL) {
134          printf("dlsym failed: ERR=%s\n", error);
135          goto get_out;
136       }
137
138       /* Initialize the plugin */
139       loadPlugin(bfuncs, &plugin->pfuncs);
140
141       found = true;                /* found a plugin */
142    }
143
144 get_out:
145    free(entry);
146    if (dp) {
147       closedir(dp);
148    }
149    return found;
150 }
151
152 /*
153  * Unload all the loaded plugins 
154  */
155 void unload_plugins()
156 {
157    Plugin *plugin;
158
159    foreach_alist(plugin, plugin_list) {
160       /* Shut it down and unload it */
161       plugin->unloadPlugin();
162       dlclose(plugin->pHandle);
163       if (plugin->file) {
164          free(plugin->file);
165       }
166       free(plugin);
167    }
168    delete plugin_list;
169    plugin_list = NULL;
170 }