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