2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
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.
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.
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
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.
29 * Main program to test loading and running Bacula plugins.
30 * Destined to become Bacula pluginloader, ...
32 * Kern Sibbald, October 2007
38 const char *plugin_type = "-dir.so";
41 /* Forward referenced functions */
42 static bRC baculaGetValue(bpContext *ctx, brVariable var, void *value);
43 static bRC baculaSetValue(bpContext *ctx, bwVariable var, void *value);
44 static bRC baculaRegisterEvents(bpContext *ctx, ...);
45 static bRC baculaJobMsg(bpContext *ctx, const char *file, int line,
46 int type, time_t mtime, const char *msg);
47 static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line,
48 int level, const char *msg);
52 static bInfo binfo = {
54 DIR_PLUGIN_INTERFACE_VERSION,
57 /* Bacula entry points */
58 static bFuncs bfuncs = {
60 DIR_PLUGIN_INTERFACE_VERSION,
69 * Create a plugin event
71 void generate_plugin_event(JCR *jcr, bEventType eventType, void *value)
77 if (!plugin_list || !jcr || !jcr->plugin_ctx_list) {
81 bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
82 jcr->eventType = event.eventType = eventType;
84 Dmsg2(dbglvl, "plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
86 foreach_alist(plugin, plugin_list) {
88 rc = plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i++], &event, value);
97 static void dump_dir_plugin(Plugin *plugin, FILE *fp)
102 pInfo *info = (pInfo *) plugin->pinfo;
103 fprintf(fp, "\tversion=%d\n", info->version);
104 fprintf(fp, "\tdate=%s\n", NPRTB(info->plugin_date));
105 fprintf(fp, "\tmagic=%s\n", NPRTB(info->plugin_magic));
106 fprintf(fp, "\tauthor=%s\n", NPRTB(info->plugin_author));
107 fprintf(fp, "\tlicence=%s\n", NPRTB(info->plugin_license));
108 fprintf(fp, "\tversion=%s\n", NPRTB(info->plugin_version));
109 fprintf(fp, "\tdescription=%s\n", NPRTB(info->plugin_description));
112 void load_dir_plugins(const char *plugin_dir)
118 plugin_list = New(alist(10, not_owned_by_alist));
119 load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type);
120 dbg_plugin_add_hook(dump_dir_plugin);
124 * Create a new instance of each plugin for this Job
126 void new_plugins(JCR *jcr)
135 int num = plugin_list->size();
141 jcr->plugin_ctx_list = (bpContext *)malloc(sizeof(bpContext) * num);
143 bpContext *plugin_ctx_list = jcr->plugin_ctx_list;
144 Dmsg2(dbglvl, "Instantiate plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
145 foreach_alist(plugin, plugin_list) {
146 /* Start a new instance of each plugin */
147 plugin_ctx_list[i].bContext = (void *)jcr;
148 plugin_ctx_list[i].pContext = NULL;
149 plug_func(plugin)->newPlugin(&plugin_ctx_list[i++]);
154 * Free the plugin instances for this Job
156 void free_plugins(JCR *jcr)
161 if (!plugin_list || !jcr->plugin_ctx_list) {
165 bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
166 Dmsg2(dbglvl, "Free instance plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
167 foreach_alist(plugin, plugin_list) {
168 /* Free the plugin instance */
169 plug_func(plugin)->freePlugin(&plugin_ctx_list[i++]);
171 free(plugin_ctx_list);
172 jcr->plugin_ctx_list = NULL;
175 /* ==============================================================
177 * Callbacks from the plugin
179 * ==============================================================
181 static bRC baculaGetValue(bpContext *ctx, brVariable var, void *value)
188 JCR *jcr = (JCR *)(ctx->bContext);
189 // Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
190 if (!jcr || !value) {
193 // Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr);
196 *((int *)value) = jcr->JobId;
197 Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId);
200 *((char **)value) = jcr->Job;
201 Dmsg1(dbglvl, "Bacula: return bVarJobName=%s\n", jcr->Job);
204 *((char **)value) = jcr->job->hdr.name;
205 Dmsg1(dbglvl, "Bacula: return bVarJob=%s\n", jcr->job->hdr.name);
208 *((int *)value) = jcr->get_JobLevel();
209 Dmsg1(dbglvl, "Bacula: return bVarLevel=%c\n", jcr->get_JobLevel());
212 *((int *)value) = jcr->get_JobType();
213 Dmsg1(dbglvl, "Bacula: return bVarType=%c\n", jcr->get_JobType());
216 *((char **)value) = jcr->client->hdr.name;
217 Dmsg1(dbglvl, "Bacula: return bVarClient=%s\n", jcr->client->hdr.name);
221 memset(&pr, 0, sizeof(pr));
222 bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
223 if (!db_get_pool_record(jcr, jcr->db, &pr)) {
226 *((int *)value) = pr.NumVols;
227 Dmsg1(dbglvl, "Bacula: return bVarNumVols=%d\n", pr.NumVols);
230 *((char **)value) = jcr->pool->hdr.name;
231 Dmsg1(dbglvl, "Bacula: return bVarPool=%s\n", jcr->pool->hdr.name);
235 *((char **)value) = jcr->wstore->hdr.name;
236 } else if (jcr->rstore) {
237 *((char **)value) = jcr->rstore->hdr.name;
239 *((char **)value) = NULL;
242 Dmsg1(dbglvl, "Bacula: return bVarStorage=%s\n", NPRT(*((char **)value)));
244 case bVarWriteStorage:
246 *((char **)value) = jcr->wstore->hdr.name;
248 *((char **)value) = NULL;
251 Dmsg1(dbglvl, "Bacula: return bVarWriteStorage=%s\n", NPRT(*((char **)value)));
253 case bVarReadStorage:
255 *((char **)value) = jcr->rstore->hdr.name;
257 *((char **)value) = NULL;
260 Dmsg1(dbglvl, "Bacula: return bVarReadStorage=%s\n", NPRT(*((char **)value)));
263 *((char **)value) = jcr->catalog->hdr.name;
264 Dmsg1(dbglvl, "Bacula: return bVarCatalog=%s\n", jcr->catalog->hdr.name);
268 *((char **)value) = jcr->wstore->media_type;
269 } else if (jcr->rstore) {
270 *((char **)value) = jcr->rstore->media_type;
272 *((char **)value) = NULL;
275 Dmsg1(dbglvl, "Bacula: return bVarMediaType=%s\n", NPRT(*((char **)value)));
278 *((int *)value) = jcr->JobStatus;
279 Dmsg1(dbglvl, "Bacula: return bVarJobStatus=%c\n", jcr->JobStatus);
282 *((int *)value) = jcr->JobPriority;
283 Dmsg1(dbglvl, "Bacula: return bVarPriority=%d\n", jcr->JobPriority);
286 *((char **)value) = jcr->VolumeName;
287 Dmsg1(dbglvl, "Bacula: return bVarVolumeName=%s\n", jcr->VolumeName);
293 *((int *)value) = jcr->JobErrors;
294 Dmsg1(dbglvl, "Bacula: return bVarErrors=%d\n", jcr->JobErrors);
297 *((int *)value) = jcr->JobFiles;
298 Dmsg1(dbglvl, "Bacula: return bVarFiles=%d\n", jcr->JobFiles);
301 *((int *)value) = jcr->SDJobFiles;
302 Dmsg1(dbglvl, "Bacula: return bVarSDFiles=%d\n", jcr->SDJobFiles);
305 *((int *)value) = jcr->SDErrors;
306 Dmsg1(dbglvl, "Bacula: return bVarSDErrors=%d\n", jcr->SDErrors);
308 case bVarFDJobStatus:
309 *((int *)value) = jcr->FDJobStatus;
310 Dmsg1(dbglvl, "Bacula: return bVarFDJobStatus=%c\n", jcr->FDJobStatus);
312 case bVarSDJobStatus:
313 *((int *)value) = jcr->SDJobStatus;
314 Dmsg1(dbglvl, "Bacula: return bVarSDJobStatus=%c\n", jcr->SDJobStatus);
323 extern struct s_jl joblevels[];
325 static bRC baculaSetValue(bpContext *ctx, bwVariable var, void *value)
329 if (!ctx || !var || !value) {
333 JCR *jcr = (JCR *)ctx->bContext;
334 int intval = *(int*)value;
335 char *strval = (char *)value;
340 Jmsg(jcr, M_INFO, 0, "%s", (char *)value);
343 case bwVarVolumeName:
344 /* Make sure VolumeName is valid and we are in VolumeName event */
345 if (jcr->eventType == bEventNewVolume &&
346 is_volume_name_legal(NULL, strval))
348 pm_strcpy(jcr->VolumeName, strval);
349 Dmsg1(100, "Set Vol=%s\n", strval);
351 jcr->VolumeName[0] = 0;
357 Dmsg1(000, "Set priority=%d\n", intval);
358 if (intval >= 1 && intval <= 100) {
359 jcr->JobPriority = intval;
367 if (jcr->eventType == bEventJobInit) {
368 for (int i=0; ok && joblevels[i].level_name; i++) {
369 if (strcasecmp(strval, joblevels[i].level_name) == 0) {
370 if (joblevels[i].job_type == jcr->get_JobType()) {
371 jcr->set_JobLevel(joblevels[i].level);
372 jcr->jr.JobLevel = jcr->get_JobLevel();
385 Dmsg1(dbglvl, "bacula: baculaSetValue var=%d\n", var);
389 static bRC baculaRegisterEvents(bpContext *ctx, ...)
395 while ((event = va_arg(args, uint32_t))) {
396 Dmsg1(dbglvl, "Plugin wants event=%u\n", event);
402 static bRC baculaJobMsg(bpContext *ctx, const char *file, int line,
403 int type, time_t mtime, const char *msg)
405 Dmsg5(dbglvl, "Job message: %s:%d type=%d time=%ld msg=%s\n",
406 file, line, type, mtime, msg);
410 static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line,
411 int level, const char *msg)
413 Dmsg4(dbglvl, "Debug message: %s:%d level=%d msg=%s\n",
414 file, line, level, msg);
421 int main(int argc, char *argv[])
423 char plugin_dir[1000];
428 strcpy(my_name, "test-dir");
430 getcwd(plugin_dir, sizeof(plugin_dir)-1);
431 load_dir_plugins(plugin_dir);
439 generate_plugin_event(jcr1, bEventJobStart, (void *)"Start Job 1");
440 generate_plugin_event(jcr1, bEventJobEnd);
441 generate_plugin_event(jcr2, bEventJobInit, (void *)"Start Job 1");
442 generate_plugin_event(jcr2, bEventJobStart, (void *)"Start Job 1");
444 generate_plugin_event(jcr2, bEventJobEnd);
449 Dmsg0(dbglvl, "bacula: OK ...\n");
455 #endif /* TEST_PROGRAM */