]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/plugins.c
kes Apply Marco van Wieringen's set of patches, cleans up Migration/Copy
[bacula/bacula] / bacula / src / lib / plugins.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 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 Kern Sibbald.
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 "plugins.h"
35
36 static const int dbglvl = 50;
37
38 /* All loaded plugins */
39 alist *plugin_list = NULL;
40
41 /*
42  * Create a new plugin "class" entry and enter it in the
43  *  list of plugins.  Note, this is not the same as
44  *  an instance of the plugin. 
45  */
46 Plugin *new_plugin()
47 {
48    Plugin *plugin;
49
50    plugin = (Plugin *)malloc(sizeof(Plugin));
51    memset(plugin, 0, sizeof(Plugin));
52    plugin_list->append(plugin);
53    return plugin;
54 }
55
56
57 /*
58  * Load all the plugins in the specified directory.
59  */
60 bool load_plugins(void *binfo, void *bfuncs, const char *plugin_dir, const char *type)
61 {
62    bool found = false;
63    t_loadPlugin loadPlugin;
64    Plugin *plugin;
65    DIR* dp = NULL;
66    struct dirent *entry = NULL, *result;
67    int name_max;
68    struct stat statp;
69    POOL_MEM fname(PM_FNAME);
70    bool need_slash = false;
71    int len, type_len;
72
73
74    name_max = pathconf(".", _PC_NAME_MAX);
75    if (name_max < 1024) {
76       name_max = 1024;
77    }
78
79    if (!(dp = opendir(plugin_dir))) {
80       berrno be;
81       Jmsg(NULL, M_ERROR_TERM, 0, _("Failed to open Plugin directory %s: ERR=%s\n"), 
82             plugin_dir, be.bstrerror());
83       Dmsg2(dbglvl, "Failed to open Plugin directory %s: ERR=%s\n", 
84             plugin_dir, be.bstrerror());
85       goto get_out;
86    }
87    
88    len = strlen(plugin_dir);
89    if (len > 0) {
90       need_slash = !IsPathSeparator(plugin_dir[len - 1]);
91    }
92    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
93    for ( ;; ) {
94       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
95          if (!found) {
96             Jmsg(NULL, M_WARNING, 0, _("Failed to find any plugins in %s\n"), 
97                   plugin_dir);
98             Dmsg1(dbglvl, "Failed to find any plugins in %s\n", plugin_dir);
99          }
100          break;
101       }
102       if (strcmp(result->d_name, ".") == 0 || 
103           strcmp(result->d_name, "..") == 0) {
104          continue;
105       }
106
107       len = strlen(result->d_name);
108       type_len = strlen(type);
109       if (len < type_len+1 || strcmp(&result->d_name[len-type_len], type) != 0) {
110          Dmsg3(dbglvl, "Rejected plugin: want=%s name=%s len=%d\n", type, result->d_name, len);
111          continue;
112       }
113       Dmsg2(dbglvl, "Loaded plugin: name=%s len=%d\n", result->d_name, len);
114        
115       pm_strcpy(fname, plugin_dir);
116       if (need_slash) {
117          pm_strcat(fname, "/");
118       }
119       pm_strcat(fname, result->d_name);
120       if (lstat(fname.c_str(), &statp) != 0 || !S_ISREG(statp.st_mode)) {
121          continue;                 /* ignore directories & special files */
122       }
123
124       plugin = new_plugin();
125       plugin->file = bstrdup(result->d_name);
126       plugin->pHandle = dlopen(fname.c_str(), RTLD_NOW);
127       if (!plugin->pHandle) {
128          Jmsg(NULL, M_ERROR, 0, _("Plugin load %s failed: ERR=%s\n"), 
129               fname.c_str(), NPRT(dlerror()));
130          Dmsg2(dbglvl, "Plugin load %s failed: ERR=%s\n", fname.c_str(), 
131                NPRT(dlerror()));
132          goto get_out;
133       }
134
135       /* Get two global entry points */
136       loadPlugin = (t_loadPlugin)dlsym(plugin->pHandle, "loadPlugin");
137       if (!loadPlugin) {
138          Jmsg(NULL, M_ERROR, 0, _("Lookup of loadPlugin in plugin %s failed: ERR=%s\n"),
139             fname.c_str(), NPRT(dlerror()));
140          Dmsg2(dbglvl, "Lookup of loadPlugin in plugin %s failed: ERR=%s\n", 
141             fname.c_str(), NPRT(dlerror()));
142          goto get_out;
143       }
144       plugin->unloadPlugin = (t_unloadPlugin)dlsym(plugin->pHandle, "unloadPlugin");
145       if (!plugin->unloadPlugin) {
146          Jmsg(NULL, M_ERROR, 0, _("Lookup of unloadPlugin in plugin %s failed: ERR=%s\n"),
147             fname.c_str(), NPRT(dlerror()));
148          Dmsg2(dbglvl, "Lookup of unloadPlugin in plugin %s failed: ERR=%s\n",
149             fname.c_str(), NPRT(dlerror()));
150          goto get_out;
151       }
152
153       /* Initialize the plugin */
154       loadPlugin(binfo, bfuncs, &plugin->pinfo, &plugin->pfuncs);
155
156       found = true;                /* found a plugin */
157    }
158
159 get_out:
160    if (entry) {
161       free(entry);
162    }
163    if (dp) {
164       closedir(dp);
165    }
166    return found;
167 }
168
169 /*
170  * Unload all the loaded plugins 
171  */
172 void unload_plugins()
173 {
174    Plugin *plugin;
175
176    if (!plugin_list) {
177       return;
178    }
179    foreach_alist(plugin, plugin_list) {
180       /* Shut it down and unload it */
181       plugin->unloadPlugin();
182       dlclose(plugin->pHandle);
183       if (plugin->file) {
184          free(plugin->file);
185       }
186       free(plugin);
187    }
188    delete plugin_list;
189    plugin_list = NULL;
190 }