2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2011 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 three of the GNU Affero 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 Affero 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
36 #include "dir_plugins.h"
38 const int dbglvl = 50;
39 const char *plugin_type = "-dir.so";
42 /* Forward referenced functions */
43 static bRC baculaGetValue(bpContext *ctx, brDirVariable var, void *value);
44 static bRC baculaSetValue(bpContext *ctx, bwDirVariable var, void *value);
45 static bRC baculaRegisterEvents(bpContext *ctx, ...);
46 static bRC baculaJobMsg(bpContext *ctx, const char *file, int line,
47 int type, utime_t mtime, const char *fmt, ...);
48 static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line,
49 int level, const char *fmt, ...);
50 static bool is_plugin_compatible(Plugin *plugin);
54 static bDirInfo binfo = {
56 DIR_PLUGIN_INTERFACE_VERSION
59 /* Bacula entry points */
60 static bDirFuncs bfuncs = {
62 DIR_PLUGIN_INTERFACE_VERSION,
71 * Bacula private context
74 JCR *jcr; /* jcr for plugin */
75 bRC rc; /* last return code */
76 bool disabled; /* set if plugin disabled */
79 static bool is_plugin_disabled(bpContext *plugin_ctx)
85 b_ctx = (bacula_ctx *)plugin_ctx->bContext;
86 return b_ctx->disabled;
90 static bool is_plugin_disabled(JCR *jcr)
92 return is_plugin_disabled(jcr->plugin_ctx);
97 * Create a plugin event
99 int generate_plugin_event(JCR *jcr, bDirEventType eventType, void *value)
101 bpContext *plugin_ctx;
107 if (!bplugin_list || !jcr || !jcr->plugin_ctx_list) {
108 return bRC_OK; /* Return if no plugins loaded */
110 if (jcr->is_job_canceled()) {
114 bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
115 event.eventType = eventType;
117 Dmsg2(dbglvl, "dir-plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
119 foreach_alist_index(i, plugin, bplugin_list) {
120 plugin_ctx = &plugin_ctx_list[i];
121 if (is_plugin_disabled(plugin_ctx)) {
124 rc = dirplug_func(plugin)->handlePluginEvent(plugin_ctx, &event, value);
134 * Print to file the plugin info.
136 void dump_dir_plugin(Plugin *plugin, FILE *fp)
141 pDirInfo *info = (pDirInfo *) plugin->pinfo;
142 fprintf(fp, "\tversion=%d\n", info->version);
143 fprintf(fp, "\tdate=%s\n", NPRTB(info->plugin_date));
144 fprintf(fp, "\tmagic=%s\n", NPRTB(info->plugin_magic));
145 fprintf(fp, "\tauthor=%s\n", NPRTB(info->plugin_author));
146 fprintf(fp, "\tlicence=%s\n", NPRTB(info->plugin_license));
147 fprintf(fp, "\tversion=%s\n", NPRTB(info->plugin_version));
148 fprintf(fp, "\tdescription=%s\n", NPRTB(info->plugin_description));
152 * This entry point is called internally by Bacula to ensure
153 * that the plugin IO calls come into this code.
155 void load_dir_plugins(const char *plugin_dir)
160 Dmsg0(dbglvl, "Load dir plugins\n");
162 Dmsg0(dbglvl, "No dir plugin dir!\n");
165 bplugin_list = New(alist(10, not_owned_by_alist));
166 if (!load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type,
167 is_plugin_compatible)) {
168 /* Either none found, or some error */
169 if (bplugin_list->size() == 0) {
172 Dmsg0(dbglvl, "No plugins loaded\n");
177 * Verify that the plugin is acceptable, and print information
180 foreach_alist_index(i, plugin, bplugin_list) {
181 Jmsg(NULL, M_INFO, 0, _("Loaded plugin: %s\n"), plugin->file);
182 Dmsg1(dbglvl, "Loaded plugin: %s\n", plugin->file);
185 Dmsg1(dbglvl, "num plugins=%d\n", bplugin_list->size());
186 dbg_plugin_add_hook(dump_dir_plugin);
190 * Check if a plugin is compatible. Called by the load_plugin function
191 * to allow us to verify the plugin.
193 static bool is_plugin_compatible(Plugin *plugin)
195 pDirInfo *info = (pDirInfo *)plugin->pinfo;
196 Dmsg0(50, "is_plugin_compatible called\n");
197 if (debug_level >= 50) {
198 dump_dir_plugin(plugin, stdin);
200 if (strcmp(info->plugin_magic, DIR_PLUGIN_MAGIC) != 0) {
201 Jmsg(NULL, M_ERROR, 0, _("Plugin magic wrong. Plugin=%s wanted=%s got=%s\n"),
202 plugin->file, DIR_PLUGIN_MAGIC, info->plugin_magic);
203 Dmsg3(50, "Plugin magic wrong. Plugin=%s wanted=%s got=%s\n",
204 plugin->file, DIR_PLUGIN_MAGIC, info->plugin_magic);
208 if (info->version != DIR_PLUGIN_INTERFACE_VERSION) {
209 Jmsg(NULL, M_ERROR, 0, _("Plugin version incorrect. Plugin=%s wanted=%d got=%d\n"),
210 plugin->file, DIR_PLUGIN_INTERFACE_VERSION, info->version);
211 Dmsg3(50, "Plugin version incorrect. Plugin=%s wanted=%d got=%d\n",
212 plugin->file, DIR_PLUGIN_INTERFACE_VERSION, info->version);
215 if (strcmp(info->plugin_license, "Bacula AGPLv3") != 0 &&
216 strcmp(info->plugin_license, "AGPLv3") != 0 &&
217 strcmp(info->plugin_license, "Bacula Systems(R) SA") != 0) {
218 Jmsg(NULL, M_ERROR, 0, _("Plugin license incompatible. Plugin=%s license=%s\n"),
219 plugin->file, info->plugin_license);
220 Dmsg2(50, "Plugin license incompatible. Plugin=%s license=%s\n",
221 plugin->file, info->plugin_license);
224 if (info->size != sizeof(pDirInfo)) {
225 Jmsg(NULL, M_ERROR, 0,
226 _("Plugin size incorrect. Plugin=%s wanted=%d got=%d\n"),
227 plugin->file, sizeof(pDirInfo), info->size);
236 * Create a new instance of each plugin for this Job
238 void new_plugins(JCR *jcr)
243 Dmsg0(dbglvl, "=== enter new_plugins ===\n");
245 Dmsg0(dbglvl, "No dir plugin list!\n");
248 if (jcr->is_job_canceled()) {
252 int num = bplugin_list->size();
254 Dmsg1(dbglvl, "dir-plugin-list size=%d\n", num);
259 jcr->plugin_ctx_list = (bpContext *)malloc(sizeof(bpContext) * num);
261 bpContext *plugin_ctx_list = jcr->plugin_ctx_list;
262 Dmsg2(dbglvl, "Instantiate dir-plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
263 foreach_alist_index(i, plugin, bplugin_list) {
264 /* Start a new instance of each plugin */
265 bacula_ctx *b_ctx = (bacula_ctx *)malloc(sizeof(bacula_ctx));
266 memset(b_ctx, 0, sizeof(bacula_ctx));
268 plugin_ctx_list[i].bContext = (void *)b_ctx;
269 plugin_ctx_list[i].pContext = NULL;
270 if (dirplug_func(plugin)->newPlugin(&plugin_ctx_list[i]) != bRC_OK) {
271 b_ctx->disabled = true;
277 * Free the plugin instances for this Job
279 void free_plugins(JCR *jcr)
284 if (!bplugin_list || !jcr->plugin_ctx_list) {
288 bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
289 Dmsg2(dbglvl, "Free instance dir-plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
290 foreach_alist_index(i, plugin, bplugin_list) {
291 /* Free the plugin instance */
292 dirplug_func(plugin)->freePlugin(&plugin_ctx_list[i]);
293 free(plugin_ctx_list[i].bContext); /* free Bacula private context */
295 free(plugin_ctx_list);
296 jcr->plugin_ctx_list = NULL;
300 /* ==============================================================
302 * Callbacks from the plugin
304 * ==============================================================
306 static bRC baculaGetValue(bpContext *ctx, brDirVariable var, void *value)
314 jcr = ((bacula_ctx *)ctx->bContext)->jcr;
323 *((int *)value) = jcr->JobId;
324 Dmsg1(dbglvl, "dir-plugin: return bDirVarJobId=%d\n", jcr->JobId);
327 *((char **)value) = jcr->Job;
328 Dmsg1(dbglvl, "Bacula: return Job name=%s\n", jcr->Job);
331 *((char **)value) = jcr->job->hdr.name;
332 Dmsg1(dbglvl, "Bacula: return bDirVarJob=%s\n", jcr->job->hdr.name);
335 *((int *)value) = jcr->getJobLevel();
336 Dmsg1(dbglvl, "Bacula: return bDirVarLevel=%c\n", jcr->getJobLevel());
339 *((int *)value) = jcr->getJobType();
340 Dmsg1(dbglvl, "Bacula: return bDirVarType=%c\n", jcr->getJobType());
343 *((char **)value) = jcr->client->hdr.name;
344 Dmsg1(dbglvl, "Bacula: return bDirVarClient=%s\n", jcr->client->hdr.name);
348 memset(&pr, 0, sizeof(pr));
349 bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
350 if (!db_get_pool_record(jcr, jcr->db, &pr)) {
353 *((int *)value) = pr.NumVols;
354 Dmsg1(dbglvl, "Bacula: return bDirVarNumVols=%d\n", pr.NumVols);
357 *((char **)value) = jcr->pool->hdr.name;
358 Dmsg1(dbglvl, "Bacula: return bDirVarPool=%s\n", jcr->pool->hdr.name);
362 *((char **)value) = jcr->wstore->hdr.name;
363 } else if (jcr->rstore) {
364 *((char **)value) = jcr->rstore->hdr.name;
366 *((char **)value) = NULL;
369 Dmsg1(dbglvl, "Bacula: return bDirVarStorage=%s\n", NPRT(*((char **)value)));
371 case bDirVarWriteStorage:
373 *((char **)value) = jcr->wstore->hdr.name;
375 *((char **)value) = NULL;
378 Dmsg1(dbglvl, "Bacula: return bDirVarWriteStorage=%s\n", NPRT(*((char **)value)));
380 case bDirVarReadStorage:
382 *((char **)value) = jcr->rstore->hdr.name;
384 *((char **)value) = NULL;
387 Dmsg1(dbglvl, "Bacula: return bDirVarReadStorage=%s\n", NPRT(*((char **)value)));
390 *((char **)value) = jcr->catalog->hdr.name;
391 Dmsg1(dbglvl, "Bacula: return bDirVarCatalog=%s\n", jcr->catalog->hdr.name);
393 case bDirVarMediaType:
395 *((char **)value) = jcr->wstore->media_type;
396 } else if (jcr->rstore) {
397 *((char **)value) = jcr->rstore->media_type;
399 *((char **)value) = NULL;
402 Dmsg1(dbglvl, "Bacula: return bDirVarMediaType=%s\n", NPRT(*((char **)value)));
404 case bDirVarJobStatus:
405 *((int *)value) = jcr->JobStatus;
406 Dmsg1(dbglvl, "Bacula: return bDirVarJobStatus=%c\n", jcr->JobStatus);
408 case bDirVarPriority:
409 *((int *)value) = jcr->JobPriority;
410 Dmsg1(dbglvl, "Bacula: return bDirVarPriority=%d\n", jcr->JobPriority);
412 case bDirVarVolumeName:
413 *((char **)value) = jcr->VolumeName;
414 Dmsg1(dbglvl, "Bacula: return bDirVarVolumeName=%s\n", jcr->VolumeName);
416 case bDirVarCatalogRes:
419 case bDirVarJobErrors:
420 *((int *)value) = jcr->JobErrors;
421 Dmsg1(dbglvl, "Bacula: return bDirVarErrors=%d\n", jcr->JobErrors);
423 case bDirVarJobFiles:
424 *((int *)value) = jcr->JobFiles;
425 Dmsg1(dbglvl, "Bacula: return bDirVarFiles=%d\n", jcr->JobFiles);
427 case bDirVarSDJobFiles:
428 *((int *)value) = jcr->SDJobFiles;
429 Dmsg1(dbglvl, "Bacula: return bDirVarSDFiles=%d\n", jcr->SDJobFiles);
431 case bDirVarSDErrors:
432 *((int *)value) = jcr->SDErrors;
433 Dmsg1(dbglvl, "Bacula: return bDirVarSDErrors=%d\n", jcr->SDErrors);
435 case bDirVarFDJobStatus:
436 *((int *)value) = jcr->FDJobStatus;
437 Dmsg1(dbglvl, "Bacula: return bDirVarFDJobStatus=%c\n", jcr->FDJobStatus);
439 case bDirVarSDJobStatus:
440 *((int *)value) = jcr->SDJobStatus;
441 Dmsg1(dbglvl, "Bacula: return bDirVarSDJobStatus=%c\n", jcr->SDJobStatus);
449 static bRC baculaSetValue(bpContext *ctx, bwDirVariable var, void *value)
452 if (!value || !ctx) {
455 // Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
456 jcr = ((bacula_ctx *)ctx->bContext)->jcr;
460 // Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr);
461 /* Nothing implemented yet */
462 Dmsg1(dbglvl, "dir-plugin: baculaSetValue var=%d\n", var);
466 static bRC baculaRegisterEvents(bpContext *ctx, ...)
472 while ((event = va_arg(args, uint32_t))) {
473 Dmsg1(dbglvl, "dir-Plugin wants event=%u\n", event);
479 static bRC baculaJobMsg(bpContext *ctx, const char *file, int line,
480 int type, utime_t mtime, const char *fmt, ...)
487 jcr = ((bacula_ctx *)ctx->bContext)->jcr;
492 va_start(arg_ptr, fmt);
493 bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
495 Jmsg(jcr, type, mtime, "%s", buf);
499 static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line,
500 int level, const char *fmt, ...)
505 va_start(arg_ptr, fmt);
506 bvsnprintf(buf, sizeof(buf), fmt, arg_ptr);
508 d_msg(file, line, level, "%s", buf);
514 int main(int argc, char *argv[])
516 char plugin_dir[1000];
521 strcpy(my_name, "test-dir");
523 getcwd(plugin_dir, sizeof(plugin_dir)-1);
524 load_dir_plugins(plugin_dir);
532 generate_plugin_event(jcr1, bDirEventJobStart, (void *)"Start Job 1");
533 generate_plugin_event(jcr1, bDirEventJobEnd);
534 generate_plugin_event(jcr2, bDirEventJobStart, (void *)"Start Job 1");
536 generate_plugin_event(jcr2, bDirEventJobEnd);
541 Dmsg0(dbglvl, "dir-plugin: OK ...\n");
547 #endif /* TEST_PROGRAM */