Switzerland, email:ftf@fsfeurope.org.
*/
/*
- * Bacula File daemon core code for loading and running plugins.
+ * Main program to test loading and running Bacula plugins.
+ * Destined to become Bacula pluginloader, ...
*
* Kern Sibbald, October 2007
*/
#include "bacula.h"
+#include "jcr.h"
#include "lib/plugin.h"
#include "fd-plugins.h"
+const int dbglvl = 0;
const char *plugin_type = "-fd.so";
static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line,
int level, const char *msg);
+void load_fd_plugins(const char *plugin_dir);
+void new_plugins(JCR *jcr);
+void free_plugins(JCR *jcr);
+void plugin_event(JCR *jcr, bEventType event);
+
+
/* Bacula info */
static bInfo binfo = {
sizeof(bFuncs),
};
-void init_fd_plugins(const char *plugin_dir)
+
+void plugin_event(JCR *jcr, bEventType eventType)
{
+ bEvent event;
Plugin *plugin;
+ int i = 0;
+
+ bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
+ Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId);
+ event.eventType = eventType;
+ foreach_alist(plugin, plugin_list) {
+ plug_func(plugin)->handlePluginEvent(&plugin_ctx[i++], &event);
+ }
+}
+void load_fd_plugins(const char *plugin_dir)
+{
if (!plugin_dir) {
return;
}
-
+
plugin_list = New(alist(10, not_owned_by_alist));
load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type);
/*
* Create a new instance of each plugin for this Job
*/
-void new_fd_plugins(JCR *jcr)
+void new_plugins(JCR *jcr)
{
- bpContext *ctx;
Plugin *plugin;
int i = 0;
return;
}
- jcr->plugin_ctx = (bpContext **)malloc(sizeof(bpContext) * num);
+ jcr->plugin_ctx = (void *)malloc(sizeof(bpContext) * num);
- ctx = jcr->plugin_ctx;
+ bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
+ Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId);
foreach_alist(plugin, plugin_list) {
/* Start a new instance of each plugin */
- ctx[i].bContext = (void *)jcr;
- ctx[i].pContext = NULL;
- plug_func(plugin)->newPlugin(ctx[i++]);
+ plugin_ctx[i].bContext = (void *)jcr;
+ plugin_ctx[i].pContext = NULL;
+ plug_func(plugin)->newPlugin(&plugin_ctx[i++]);
}
}
/*
* Free the plugin instances for this Job
*/
-void free_fd_plugins(JCR *jcr)
+void free_plugins(JCR *jcr)
{
- bpContext *ctx;
Plugin *plugin;
int i = 0;
- ctx = jcr->plugin_ctx;
- foreach_alist(plugin, jcr->plugin_list) {
+ bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
+ foreach_alist(plugin, plugin_list) {
/* Free the plugin instance */
- plug_func(plugin)->freePlugin(ctx[i++]);
+ plug_func(plugin)->freePlugin(&plugin_ctx[i++]);
}
- free(ctx);
+ free(plugin_ctx);
jcr->plugin_ctx = NULL;
}
-void term_fd_plugins()
-{
- unload_plugins();
-}
static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value)
{
- printf("bacula: baculaGetValue var=%d\n", var);
+ JCR *jcr = (JCR *)(ctx->bContext);
+ Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
if (!value) {
return 1;
}
+ Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr);
switch (var) {
case bVarJobId:
- *((int *)value) = 100;
+ *((int *)value) = jcr->JobId;
+ Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId);
break;
case bVarFDName:
*((char **)value) = "FD Name";
static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value)
{
- printf("bacula: baculaSetValue var=%d\n", var);
+ Dmsg1(dbglvl, "bacula: baculaSetValue var=%d\n", var);
return 0;
}
va_start(args, ctx);
while ((event = va_arg(args, uint32_t))) {
- printf("Plugin wants event=%u\n", event);
+ Dmsg1(dbglvl, "Plugin wants event=%u\n", event);
}
va_end(args);
return 0;
static bpError baculaJobMsg(bpContext *ctx, const char *file, int line,
int type, time_t mtime, const char *msg)
{
- printf("Job message: %s:%d type=%d time=%ld msg=%s\n",
+ Dmsg5(dbglvl, "Job message: %s:%d type=%d time=%ld msg=%s\n",
file, line, type, mtime, msg);
return 0;
}
static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line,
int level, const char *msg)
{
- printf("Debug message: %s:%d level=%d msg=%s\n",
+ Dmsg4(dbglvl, "Debug message: %s:%d level=%d msg=%s\n",
file, line, level, msg);
return 0;
}
+
+#ifdef TEST_PROGRAM
+
+int main(int argc, char *argv[])
+{
+ char plugin_dir[1000];
+ JCR mjcr1, mjcr2;
+ JCR *jcr1 = &mjcr1;
+ JCR *jcr2 = &mjcr2;
+
+ getcwd(plugin_dir, sizeof(plugin_dir)-1);
+ load_fd_plugins(plugin_dir);
+
+ jcr1->JobId = 111;
+ new_plugins(jcr1);
+
+ jcr2->JobId = 222;
+ new_plugins(jcr2);
+
+ plugin_event(jcr1, bEventJobStart);
+ plugin_event(jcr1, bEventJobEnd);
+ plugin_event(jcr2, bEventJobStart);
+ free_plugins(jcr1);
+ plugin_event(jcr2, bEventJobEnd);
+ free_plugins(jcr2);
+
+ unload_plugins();
+
+ Dmsg0(dbglvl, "bacula: OK ...\n");
+ close_memory_pool();
+ sm_dump(false);
+ return 0;
+}
+
+#endif /* TEST_PROGRAM */
/*
Bacula® - The Network Backup Solution
- Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
+ Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
The main author of Bacula is Kern Sibbald, with contributions from
many others, a complete list can be found in the file AUTHORS.
/* Run the after job */
run_scripts(jcr, jcr->RunScripts, "ClientAfterJob");
- if (jcr->JobId) { /* send EndJob if running a job */
+ if (jcr->JobId) { /* send EndJob if running a job */
char ed1[50], ed2[50];
/* Send termination status back to Dir */
bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
- edit_uint64(jcr->ReadBytes, ed1),
- edit_uint64(jcr->JobBytes, ed2), jcr->Errors, jcr->VSS,
- jcr->pki_encrypt);
+ edit_uint64(jcr->ReadBytes, ed1),
+ edit_uint64(jcr->JobBytes, ed2), jcr->Errors, jcr->VSS,
+ jcr->pki_encrypt);
Dmsg1(110, "End FD msg: %s\n", dir->msg);
}
if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
if (!(cjcr=get_jcr_by_full_name(Job))) {
- bnet_fsend(dir, _("2901 Job %s not found.\n"), Job);
+ dir->fsend(_("2901 Job %s not found.\n"), Job);
} else {
if (cjcr->store_bsock) {
cjcr->store_bsock->set_timed_out();
}
set_jcr_job_status(cjcr, JS_Canceled);
free_jcr(cjcr);
- bnet_fsend(dir, _("2001 Job %s marked to be canceled.\n"), Job);
+ dir->fsend(_("2001 Job %s marked to be canceled.\n"), Job);
}
} else {
- bnet_fsend(dir, _("2902 Error scanning cancel command.\n"));
+ dir->fsend(_("2902 Error scanning cancel command.\n"));
}
- bnet_sig(dir, BNET_EOD);
+ dir->signal(BNET_EOD);
return 1;
}
Dmsg1(110, "setdebug_cmd: %s", dir->msg);
if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
pm_strcpy(jcr->errmsg, dir->msg);
- bnet_fsend(dir, _("2991 Bad setdebug command: %s\n"), jcr->errmsg);
+ dir->fsend(_("2991 Bad setdebug command: %s\n"), jcr->errmsg);
return 0;
}
debug_level = level;
make_estimate(jcr);
dir->fsend(OKest, jcr->num_files_examined,
edit_uint64_with_commas(jcr->JobBytes, ed2));
- bnet_sig(dir, BNET_EOD);
+ dir->signal(BNET_EOD);
return 1;
}
sd_auth_key) != 5) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
- bnet_fsend(dir, BADjob);
+ dir->fsend(BADjob);
free_pool_memory(sd_auth_key);
return 0;
}
free_memory(cmd);
if (ok) {
- bnet_fsend(dir, OKRunBefore);
+ dir->fsend(OKRunBefore);
return 1;
} else {
- bnet_fsend(dir, _("2905 Bad RunBeforeJob command.\n"));
+ dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
return 0;
}
}
run_scripts(jcr, jcr->RunScripts, "ClientBeforeJob");
if (job_canceled(jcr)) {
- bnet_fsend(dir, _("2905 Bad RunBeforeNow command.\n"));
+ dir->fsend(_("2905 Bad RunBeforeNow command.\n"));
Dmsg0(100, "Back from run_scripts ClientBeforeJob now: FAILED\n");
return 0;
} else {
- bnet_fsend(dir, OKRunBeforeNow);
+ dir->fsend(OKRunBeforeNow);
Dmsg0(100, "Back from run_scripts ClientBeforeJob now: OK\n");
return 1;
}
if (sscanf(dir->msg, runafter, msg) != 1) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
- bnet_fsend(dir, _("2905 Bad RunAfterJob command.\n"));
+ dir->fsend(_("2905 Bad RunAfterJob command.\n"));
free_memory(msg);
return 0;
}
jcr->RunScripts->append(cmd);
free_pool_memory(msg);
- return bnet_fsend(dir, OKRunAfter);
+ return dir->fsend(OKRunAfter);
}
static int runscript_cmd(JCR *jcr)
msg) != 5) {
pm_strcpy(jcr->errmsg, dir->msg);
Jmsg1(jcr, M_FATAL, 0, _("Bad RunScript command: %s\n"), jcr->errmsg);
- bnet_fsend(dir, _("2905 Bad RunScript command.\n"));
+ dir->fsend(_("2905 Bad RunScript command.\n"));
free_runscript(cmd);
free_memory(msg);
return 0;
jcr->RunScripts->append(cmd);
free_pool_memory(msg);
- return bnet_fsend(dir, OKRunScript);
+ return dir->fsend(OKRunScript);
}
{
FF_PKT *ff = jcr->ff;
-#ifdef xxx
+#ifdef xxx_DEBUG_CODE
findFILESET *fileset = ff->fileset;
int i, j, k;
const char *p;
char strip[100];
+// Commented out as it is not backward compatible - KES
#ifdef HAVE_WIN32
- fo->flags |= FO_IGNORECASE; /* always ignorecase under windows */
+// fo->flags |= FO_IGNORECASE; /* always ignorecase under windows */
#endif
for (p=opts; *p; p++) {
if (!init_fileset(jcr)) {
return 0;
}
- while (bnet_recv(dir) >= 0) {
+ while (dir->recv() >= 0) {
strip_trailing_junk(dir->msg);
Dmsg1(500, "Fileset: %s\n", dir->msg);
add_fileset(jcr, dir->msg);
* Suck up what he is sending to us so that he will then
* read our error message.
*/
- while (bnet_recv(dir) >= 0)
+ while (dir->recv() >= 0)
{ }
free_bootstrap(jcr);
set_jcr_job_status(jcr, JS_ErrorTerminated);
return 0;
}
- while (bnet_recv(dir) >= 0) {
+ while (dir->recv() >= 0) {
Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
fputs(dir->msg, bs);
}
* Note, do not free the bootstrap yet -- it needs to be
* sent to the SD
*/
- return bnet_fsend(dir, OKbootstrap);
+ return dir->fsend(OKbootstrap);
}
*/
for (int i=0; i<10; i++) {
bt_start = get_current_btime();
- bnet_sig(dir, BNET_BTIME); /* poll for time */
- if (bnet_recv(dir) <= 0) { /* get response */
+ dir->signal(BNET_BTIME); /* poll for time */
+ if (dir->recv() <= 0) { /* get response */
goto bail_out;
}
if (sscanf(dir->msg, "btime %s", buf) != 1) {
}
Jmsg(jcr, type, 0, _("DIR and FD clocks differ by %d seconds, FD automatically compensating.\n"), adj);
}
- bnet_sig(dir, BNET_EOD);
+ dir->signal(BNET_EOD);
Dmsg2(100, "adj = %d since_time=%d\n", (int)adj, (int)since_time);
jcr->incremental = 1; /* set incremental or decremental backup */
if (buf) {
free_memory(buf);
}
- return bnet_fsend(dir, OKlevel);
+ return dir->fsend(OKlevel);
bail_out:
pm_strcpy(jcr->errmsg, dir->msg);
goto cleanup;
}
- bnet_fsend(dir, OKbackup);
+ dir->fsend(OKbackup);
Dmsg1(110, "bfiled>dird: %s", dir->msg);
/*
* Send Append Open Session to Storage daemon
*/
- bnet_fsend(sd, append_open);
+ sd->fsend(append_open);
Dmsg1(110, ">stored: %s", sd->msg);
/*
* Expect to receive back the Ticket number
/*
* Send Append data command to Storage daemon
*/
- bnet_fsend(sd, append_data, jcr->Ticket);
+ sd->fsend(append_data, jcr->Ticket);
Dmsg1(110, ">stored: %s", sd->msg);
/*
/*
* Send Append End Data to Storage daemon
*/
- bnet_fsend(sd, append_end, jcr->Ticket);
+ sd->fsend(append_end, jcr->Ticket);
/* Get end OK */
if (!response(jcr, sd, OK_end, "Append End")) {
set_jcr_job_status(jcr, JS_ErrorTerminated);
/*
* Send Append Close to Storage daemon
*/
- bnet_fsend(sd, append_close, jcr->Ticket);
+ sd->fsend(append_close, jcr->Ticket);
while (bget_msg(sd) >= 0) { /* stop on signal or error */
if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
ok = 1;
jcr->JobType = JT_VERIFY;
if (sscanf(dir->msg, verifycmd, level) != 1) {
- bnet_fsend(dir, _("2994 Bad verify command: %s\n"), dir->msg);
+ dir->fsend(_("2994 Bad verify command: %s\n"), dir->msg);
return 0;
}
} else if (strcasecmp(level, "disk_to_catalog") == 0) {
jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
} else {
- bnet_fsend(dir, _("2994 Bad verify level: %s\n"), dir->msg);
+ dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
return 0;
}
- bnet_fsend(dir, OKverify);
+ dir->fsend(OKverify);
generate_daemon_event(jcr, "JobStart");
/*
* Send Close session command to Storage daemon
*/
- bnet_fsend(sd, read_close, jcr->Ticket);
+ sd->fsend(read_close, jcr->Ticket);
Dmsg1(130, "bfiled>stored: %s", sd->msg);
/* ****FIXME**** check response */
bget_msg(sd); /* get OK */
/* Inform Storage daemon that we are done */
- bnet_sig(sd, BNET_TERMINATE);
+ sd->signal(BNET_TERMINATE);
break;
case L_VERIFY_DISK_TO_CATALOG:
do_verify(jcr);
break;
default:
- bnet_fsend(dir, _("2994 Bad verify level: %s\n"), dir->msg);
+ dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
return 0;
}
- bnet_sig(dir, BNET_EOD);
+ dir->signal(BNET_EOD);
return 0; /* return and terminate command loop */
}