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