2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2009 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
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, utime_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, NULL);
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]);
170 plugin_ctx_list[i].bContext = NULL;
171 plugin_ctx_list[i].pContext = NULL;
174 free(plugin_ctx_list);
175 jcr->plugin_ctx_list = NULL;
178 /* ==============================================================
180 * Callbacks from the plugin
182 * ==============================================================
184 static bRC baculaGetValue(bpContext *ctx, brVariable var, void *value)
191 JCR *jcr = (JCR *)(ctx->bContext);
192 // Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
193 if (!jcr || !value) {
196 // Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr);
199 *((int *)value) = jcr->JobId;
200 Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId);
203 *((char **)value) = jcr->Job;
204 Dmsg1(dbglvl, "Bacula: return bVarJobName=%s\n", jcr->Job);
207 *((char **)value) = jcr->job->hdr.name;
208 Dmsg1(dbglvl, "Bacula: return bVarJob=%s\n", jcr->job->hdr.name);
211 *((int *)value) = jcr->getJobLevel();
212 Dmsg1(dbglvl, "Bacula: return bVarLevel=%c\n", jcr->getJobLevel());
215 *((int *)value) = jcr->getJobType();
216 Dmsg1(dbglvl, "Bacula: return bVarType=%c\n", jcr->getJobType());
219 *((char **)value) = jcr->client->hdr.name;
220 Dmsg1(dbglvl, "Bacula: return bVarClient=%s\n", jcr->client->hdr.name);
224 memset(&pr, 0, sizeof(pr));
225 bstrncpy(pr.Name, jcr->pool->hdr.name, sizeof(pr.Name));
226 if (!db_get_pool_record(jcr, jcr->db, &pr)) {
229 *((int *)value) = pr.NumVols;
230 Dmsg1(dbglvl, "Bacula: return bVarNumVols=%d\n", pr.NumVols);
233 *((char **)value) = jcr->pool->hdr.name;
234 Dmsg1(dbglvl, "Bacula: return bVarPool=%s\n", jcr->pool->hdr.name);
238 *((char **)value) = jcr->wstore->hdr.name;
239 } else if (jcr->rstore) {
240 *((char **)value) = jcr->rstore->hdr.name;
242 *((char **)value) = NULL;
245 Dmsg1(dbglvl, "Bacula: return bVarStorage=%s\n", NPRT(*((char **)value)));
247 case bVarWriteStorage:
249 *((char **)value) = jcr->wstore->hdr.name;
251 *((char **)value) = NULL;
254 Dmsg1(dbglvl, "Bacula: return bVarWriteStorage=%s\n", NPRT(*((char **)value)));
256 case bVarReadStorage:
258 *((char **)value) = jcr->rstore->hdr.name;
260 *((char **)value) = NULL;
263 Dmsg1(dbglvl, "Bacula: return bVarReadStorage=%s\n", NPRT(*((char **)value)));
266 *((char **)value) = jcr->catalog->hdr.name;
267 Dmsg1(dbglvl, "Bacula: return bVarCatalog=%s\n", jcr->catalog->hdr.name);
271 *((char **)value) = jcr->wstore->media_type;
272 } else if (jcr->rstore) {
273 *((char **)value) = jcr->rstore->media_type;
275 *((char **)value) = NULL;
278 Dmsg1(dbglvl, "Bacula: return bVarMediaType=%s\n", NPRT(*((char **)value)));
281 *((int *)value) = jcr->JobStatus;
282 Dmsg1(dbglvl, "Bacula: return bVarJobStatus=%c\n", jcr->JobStatus);
285 *((int *)value) = jcr->JobPriority;
286 Dmsg1(dbglvl, "Bacula: return bVarPriority=%d\n", jcr->JobPriority);
289 *((char **)value) = jcr->VolumeName;
290 Dmsg1(dbglvl, "Bacula: return bVarVolumeName=%s\n", jcr->VolumeName);
296 *((int *)value) = jcr->JobErrors;
297 Dmsg1(dbglvl, "Bacula: return bVarErrors=%d\n", jcr->JobErrors);
300 *((int *)value) = jcr->JobFiles;
301 Dmsg1(dbglvl, "Bacula: return bVarFiles=%d\n", jcr->JobFiles);
304 *((int *)value) = jcr->SDJobFiles;
305 Dmsg1(dbglvl, "Bacula: return bVarSDFiles=%d\n", jcr->SDJobFiles);
308 *((int *)value) = jcr->SDErrors;
309 Dmsg1(dbglvl, "Bacula: return bVarSDErrors=%d\n", jcr->SDErrors);
311 case bVarFDJobStatus:
312 *((int *)value) = jcr->FDJobStatus;
313 Dmsg1(dbglvl, "Bacula: return bVarFDJobStatus=%c\n", jcr->FDJobStatus);
315 case bVarSDJobStatus:
316 *((int *)value) = jcr->SDJobStatus;
317 Dmsg1(dbglvl, "Bacula: return bVarSDJobStatus=%c\n", jcr->SDJobStatus);
326 extern struct s_jl joblevels[];
328 static bRC baculaSetValue(bpContext *ctx, bwVariable var, void *value)
332 if (!ctx || !var || !value) {
336 JCR *jcr = (JCR *)ctx->bContext;
337 int intval = *(int*)value;
338 char *strval = (char *)value;
343 Jmsg(jcr, M_INFO, 0, "%s", (char *)value);
346 case bwVarVolumeName:
347 /* Make sure VolumeName is valid and we are in VolumeName event */
348 if (jcr->eventType == bEventNewVolume &&
349 is_volume_name_legal(NULL, strval))
351 pm_strcpy(jcr->VolumeName, strval);
352 Dmsg1(100, "Set Vol=%s\n", strval);
354 jcr->VolumeName[0] = 0;
360 Dmsg1(000, "Set priority=%d\n", intval);
361 if (intval >= 1 && intval <= 100) {
362 jcr->JobPriority = intval;
370 if (jcr->eventType == bEventJobInit) {
371 for (int i=0; ok && joblevels[i].level_name; i++) {
372 if (strcasecmp(strval, joblevels[i].level_name) == 0) {
373 if (joblevels[i].job_type == jcr->getJobType()) {
374 jcr->set_JobLevel(joblevels[i].level);
375 jcr->jr.JobLevel = jcr->getJobLevel();
388 Dmsg1(dbglvl, "bacula: baculaSetValue var=%d\n", var);
392 static bRC baculaRegisterEvents(bpContext *ctx, ...)
398 while ((event = va_arg(args, uint32_t))) {
399 Dmsg1(dbglvl, "Plugin wants event=%u\n", event);
405 static bRC baculaJobMsg(bpContext *ctx, const char *file, int line,
406 int type, utime_t mtime, const char *msg)
408 Dmsg5(dbglvl, "Job message: %s:%d type=%d time=%lld msg=%s\n",
409 file, line, type, mtime, msg);
413 static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line,
414 int level, const char *msg)
416 Dmsg4(dbglvl, "Debug message: %s:%d level=%d msg=%s\n",
417 file, line, level, msg);
424 int main(int argc, char *argv[])
426 char plugin_dir[1000];
431 strcpy(my_name, "test-dir");
433 getcwd(plugin_dir, sizeof(plugin_dir)-1);
434 load_dir_plugins(plugin_dir);
442 generate_plugin_event(jcr1, bEventJobStart, (void *)"Start Job 1");
443 generate_plugin_event(jcr1, bEventJobEnd);
444 generate_plugin_event(jcr2, bEventJobInit, (void *)"Start Job 1");
445 generate_plugin_event(jcr2, bEventJobStart, (void *)"Start Job 1");
447 generate_plugin_event(jcr2, bEventJobEnd);
452 Dmsg0(dbglvl, "bacula: OK ...\n");
458 #endif /* TEST_PROGRAM */