]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/fd-plugins.c
Alpha integration of Dir plugin
[bacula/bacula] / bacula / src / filed / fd-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  * Main program to test loading and running Bacula plugins.
30  *   Destined to become Bacula pluginloader, ...
31  *
32  * Kern Sibbald, October 2007
33  */
34 #include "bacula.h"
35 #include "jcr.h"
36 #include "fd-plugins.h"
37
38 const int dbglvl = 0;
39 const char *plugin_type = "-fd.so";
40
41
42 /* Forward referenced functions */
43 static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value);
44 static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value);
45 static bpError baculaRegisterEvents(bpContext *ctx, ...);
46 static bpError baculaJobMsg(bpContext *ctx, const char *file, int line,
47   int type, time_t mtime, const char *msg);
48 static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line,
49   int level, const char *msg);
50
51
52 /* Bacula info */
53 static bInfo binfo = {
54    sizeof(bFuncs),
55    PLUGIN_INTERFACE,
56 };
57
58 /* Bacula entry points */
59 static bFuncs bfuncs = {
60    sizeof(bFuncs),
61    PLUGIN_INTERFACE,
62    baculaRegisterEvents,
63    baculaGetValue,
64    baculaSetValue,
65    baculaJobMsg,
66    baculaDebugMsg
67 };
68
69 /*
70  * Create a plugin event 
71  */
72 void generate_plugin_event(JCR *jcr, bEventType eventType) 
73 {
74    bEvent event;
75    Plugin *plugin;
76    int i = 0;
77
78    if (!plugin_list) {
79       return;
80    }
81
82    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
83    Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId);
84    event.eventType = eventType;
85    foreach_alist(plugin, plugin_list) {
86       plug_func(plugin)->handlePluginEvent(&plugin_ctx[i++], &event);
87    }
88 }
89
90 void load_fd_plugins(const char *plugin_dir)
91 {
92    if (!plugin_dir) {
93       return;
94    }
95
96    plugin_list = New(alist(10, not_owned_by_alist));
97    load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type);
98 }
99
100 /*
101  * Create a new instance of each plugin for this Job
102  */
103 void new_plugins(JCR *jcr)
104 {
105    Plugin *plugin;
106    int i = 0;
107
108    if (!plugin_list) {
109       return;
110    }
111
112    int num = plugin_list->size();
113
114    if (num == 0) {
115       return;
116    }
117
118    jcr->plugin_ctx = (void *)malloc(sizeof(bpContext) * num);
119
120    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
121    Dmsg2(dbglvl, "Instantiate plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId);
122    foreach_alist(plugin, plugin_list) {
123       /* Start a new instance of each plugin */
124       plugin_ctx[i].bContext = (void *)jcr;
125       plugin_ctx[i].pContext = NULL;
126       plug_func(plugin)->newPlugin(&plugin_ctx[i++]);
127    }
128 }
129
130 /*
131  * Free the plugin instances for this Job
132  */
133 void free_plugins(JCR *jcr)
134 {
135    Plugin *plugin;
136    int i = 0;
137
138    if (!plugin_list) {
139       return;
140    }
141
142    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
143    Dmsg2(dbglvl, "Free instance plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId);
144    foreach_alist(plugin, plugin_list) {
145       /* Free the plugin instance */
146       plug_func(plugin)->freePlugin(&plugin_ctx[i++]);
147    }
148    free(plugin_ctx);
149    jcr->plugin_ctx = NULL;
150 }
151
152
153 /* ==============================================================
154  *
155  * Callbacks from the plugin
156  *
157  * ==============================================================
158  */
159 static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value)
160 {
161    JCR *jcr = (JCR *)(ctx->bContext);
162 // Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
163    if (!value) {
164       return 1;
165    }
166 // Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr); 
167    switch (var) {
168    case bVarJobId:
169       *((int *)value) = jcr->JobId;
170       Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId);
171       break;
172    case bVarFDName:
173       *((char **)value) = my_name;
174       Dmsg1(dbglvl, "Bacula: return my_name=%s\n", my_name);
175       break;
176    case bVarLevel:
177    case bVarType:
178    case bVarClient:
179    case bVarJobName:
180    case bVarJobStatus:
181    case bVarSinceTime:
182       break;
183    }
184    return 0;
185 }
186
187 static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value)
188 {
189    Dmsg1(dbglvl, "bacula: baculaSetValue var=%d\n", var);
190    return 0;
191 }
192
193 static bpError baculaRegisterEvents(bpContext *ctx, ...)
194 {
195    va_list args;
196    uint32_t event;
197
198    va_start(args, ctx);
199    while ((event = va_arg(args, uint32_t))) {
200       Dmsg1(dbglvl, "Plugin wants event=%u\n", event);
201    }
202    va_end(args);
203    return 0;
204 }
205
206 static bpError baculaJobMsg(bpContext *ctx, const char *file, int line,
207   int type, time_t mtime, const char *msg)
208 {
209    Dmsg5(dbglvl, "Job message: %s:%d type=%d time=%ld msg=%s\n",
210       file, line, type, mtime, msg);
211    return 0;
212 }
213
214 static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line,
215   int level, const char *msg)
216 {
217    Dmsg4(dbglvl, "Debug message: %s:%d level=%d msg=%s\n",
218       file, line, level, msg);
219    return 0;
220 }
221
222 #ifdef TEST_PROGRAM
223
224
225 int main(int argc, char *argv[])
226 {
227    char plugin_dir[1000];
228    JCR mjcr1, mjcr2;
229    JCR *jcr1 = &mjcr1;
230    JCR *jcr2 = &mjcr2;
231
232    strcpy(my_name, "test-fd");
233     
234    getcwd(plugin_dir, sizeof(plugin_dir)-1);
235    load_fd_plugins(plugin_dir);
236
237    jcr1->JobId = 111;
238    new_plugins(jcr1);
239
240    jcr2->JobId = 222;
241    new_plugins(jcr2);
242
243    generate_plugin_event(jcr1, bEventJobStart);
244    generate_plugin_event(jcr1, bEventJobEnd);
245    generate_plugin_event(jcr2, bEventJobStart);
246    free_plugins(jcr1);
247    generate_plugin_event(jcr2, bEventJobEnd);
248    free_plugins(jcr2);
249
250    unload_plugins();
251
252    Dmsg0(dbglvl, "bacula: OK ...\n");
253    close_memory_pool();
254    sm_dump(false);
255    return 0;
256 }
257
258 #endif /* TEST_PROGRAM */