]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/plugin.c
Implement Plugin Directory and plugin events.
[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 *binfo, void *bfuncs, const char *plugin_dir, const char *type)
60 {
61    t_loadPlugin loadPlugin;
62    Plugin *plugin;
63    DIR* dp = NULL;
64    struct dirent *entry = NULL, *result;
65    int name_max;
66    struct stat statp;
67    bool found = false;
68    POOL_MEM fname(PM_FNAME);
69    bool need_slash = false;
70    int len, type_len;
71
72    plugin = new_plugin();
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, 0, _("Failed to open Plugin directory %s: ERR=%s\n"), 
82             plugin_dir, be.bstrerror());
83       goto get_out;
84    }
85    
86    len = strlen(plugin_dir);
87    if (len > 0) {
88       need_slash = !IsPathSeparator(plugin_dir[len - 1]);
89    }
90    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
91    for ( ;; ) {
92       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
93          if (!found) {
94             Jmsg(NULL, M_INFO, 0, _("Failed to find suitable plugin in %s\n"), 
95                   plugin_dir);
96          }
97          break;
98       }
99       if (strcmp(result->d_name, ".") == 0 || 
100           strcmp(result->d_name, "..") == 0) {
101          continue;
102       }
103
104       len = strlen(result->d_name);
105       type_len = strlen(type);
106       if (len < type_len+1 || strcmp(&result->d_name[len-type_len], type) != 0) {
107          continue;
108       }
109       printf("Got: 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->file = bstrdup(result->d_name);
121       plugin->pHandle = dlopen(fname.c_str(), RTLD_NOW);
122       if (!plugin->pHandle) {
123          Jmsg(NULL, M_ERROR, 0, _("Plugin load %s failed: ERR=%s\n"), 
124               fname.c_str(), dlerror());
125          goto get_out;
126       }
127
128       /* Get two global entry points */
129       loadPlugin = (t_loadPlugin)dlsym(plugin->pHandle, "loadPlugin");
130       if (!loadPlugin) {
131          Jmsg(NULL, M_ERROR, 0, _("Lookup of loadPlugin in plugin %s failed: ERR=%s\n"),
132             fname.c_str(), dlerror());
133          goto get_out;
134       }
135       plugin->unloadPlugin = (t_unloadPlugin)dlsym(plugin->pHandle, "unloadPlugin");
136       if (!plugin->unloadPlugin) {
137          Jmsg(NULL, M_ERROR, 0, _("Lookup of unloadPlugin in plugin %s failed: ERR=%s\n"),
138             fname.c_str(), dlerror());
139          goto get_out;
140       }
141
142       /* Initialize the plugin */
143       loadPlugin(binfo, bfuncs, &plugin->pinfo, &plugin->pfuncs);
144
145       found = true;                /* found a plugin */
146    }
147
148 get_out:
149    if (entry) {
150       free(entry);
151    }
152    if (dp) {
153       closedir(dp);
154    }
155    return found;
156 }
157
158 /*
159  * Unload all the loaded plugins 
160  */
161 void unload_plugins()
162 {
163    Plugin *plugin;
164
165    foreach_alist(plugin, plugin_list) {
166       /* Shut it down and unload it */
167       plugin->unloadPlugin();
168       dlclose(plugin->pHandle);
169       if (plugin->file) {
170          free(plugin->file);
171       }
172       free(plugin);
173    }
174    delete plugin_list;
175    plugin_list = NULL;
176 }