From f366b615e1b676ab3baa6adf7014c63214c01c91 Mon Sep 17 00:00:00 2001 From: Kern Sibbald Date: Sat, 26 Jan 2008 10:52:19 +0000 Subject: [PATCH] First integration of FD plugin git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@6325 91ce42f0-d328-0410-95d8-f526ca767f89 --- bacula/src/filed/fd-plugins.c | 52 +++++++++++++++++++---------- bacula/src/filed/fd-plugins.h | 9 +++++ bacula/src/filed/filed.c | 5 ++- bacula/src/filed/filed.h | 18 +++++----- bacula/src/filed/filed_conf.c | 3 ++ bacula/src/filed/job.c | 15 ++++++--- bacula/src/plugins/fd/fd-plugins.c | 53 ++++++++++++++++++++---------- bacula/src/plugins/fd/fd-plugins.h | 9 +++++ 8 files changed, 115 insertions(+), 49 deletions(-) diff --git a/bacula/src/filed/fd-plugins.c b/bacula/src/filed/fd-plugins.c index bf657f9fa8..ef6ecc3f4b 100644 --- a/bacula/src/filed/fd-plugins.c +++ b/bacula/src/filed/fd-plugins.c @@ -33,7 +33,6 @@ */ #include "bacula.h" #include "jcr.h" -#include "lib/plugin.h" #include "fd-plugins.h" const int dbglvl = 0; @@ -49,11 +48,6 @@ static bpError baculaJobMsg(bpContext *ctx, const char *file, int line, 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 = { @@ -71,15 +65,20 @@ static bFuncs bfuncs = { baculaJobMsg, baculaDebugMsg }; - - -void plugin_event(JCR *jcr, bEventType eventType) +/* + * Create a plugin event + */ +void generate_plugin_event(JCR *jcr, bEventType eventType) { bEvent event; Plugin *plugin; int i = 0; + if (!plugin_list) { + return; + } + bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId); event.eventType = eventType; @@ -95,7 +94,6 @@ void load_fd_plugins(const char *plugin_dir) } plugin_list = New(alist(10, not_owned_by_alist)); - load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type); } @@ -107,6 +105,10 @@ void new_plugins(JCR *jcr) Plugin *plugin; int i = 0; + if (!plugin_list) { + return; + } + int num = plugin_list->size(); if (num == 0) { @@ -116,7 +118,7 @@ void new_plugins(JCR *jcr) jcr->plugin_ctx = (void *)malloc(sizeof(bpContext) * num); bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; - Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId); + Dmsg2(dbglvl, "Instantiate plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId); foreach_alist(plugin, plugin_list) { /* Start a new instance of each plugin */ plugin_ctx[i].bContext = (void *)jcr; @@ -133,7 +135,12 @@ void free_plugins(JCR *jcr) Plugin *plugin; int i = 0; + if (!plugin_list) { + return; + } + bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; + Dmsg2(dbglvl, "Free instance plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId); foreach_alist(plugin, plugin_list) { /* Free the plugin instance */ plug_func(plugin)->freePlugin(&plugin_ctx[i++]); @@ -143,21 +150,28 @@ void free_plugins(JCR *jcr) } +/* ============================================================== + * + * Callbacks from the plugin + * + * ============================================================== + */ static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value) { JCR *jcr = (JCR *)(ctx->bContext); - Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var); +// Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var); if (!value) { return 1; } - Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr); +// Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr); switch (var) { case bVarJobId: *((int *)value) = jcr->JobId; Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId); break; case bVarFDName: - *((char **)value) = "FD Name"; + *((char **)value) = my_name; + Dmsg1(dbglvl, "Bacula: return my_name=%s\n", my_name); break; case bVarLevel: case bVarType: @@ -207,6 +221,8 @@ static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line, #ifdef TEST_PROGRAM +char my_name = "test-fd"; + int main(int argc, char *argv[]) { char plugin_dir[1000]; @@ -223,11 +239,11 @@ int main(int argc, char *argv[]) jcr2->JobId = 222; new_plugins(jcr2); - plugin_event(jcr1, bEventJobStart); - plugin_event(jcr1, bEventJobEnd); - plugin_event(jcr2, bEventJobStart); + generate_plugin_event(jcr1, bEventJobStart); + generate_plugin_event(jcr1, bEventJobEnd); + generate_plugin_event(jcr2, bEventJobStart); free_plugins(jcr1); - plugin_event(jcr2, bEventJobEnd); + generate_plugin_event(jcr2, bEventJobEnd); free_plugins(jcr2); unload_plugins(); diff --git a/bacula/src/filed/fd-plugins.h b/bacula/src/filed/fd-plugins.h index 80aea4ab78..af575c9841 100644 --- a/bacula/src/filed/fd-plugins.h +++ b/bacula/src/filed/fd-plugins.h @@ -48,6 +48,8 @@ extern "C" { #endif + + /**************************************************************************** * * * Bacula definitions * @@ -93,6 +95,13 @@ typedef struct s_baculaFuncs { int level, const char *msg); } bFuncs; +/* Bacula Subroutines */ +void load_fd_plugins(const char *plugin_dir); +void new_plugins(JCR *jcr); +void free_plugins(JCR *jcr); +void generate_plugin_event(JCR *jcr, bEventType event); + + /**************************************************************************** * * diff --git a/bacula/src/filed/filed.c b/bacula/src/filed/filed.c index 9fad17c43b..fc7d1fb9a5 100644 --- a/bacula/src/filed/filed.c +++ b/bacula/src/filed/filed.c @@ -1,7 +1,7 @@ /* 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. @@ -208,6 +208,8 @@ int main (int argc, char *argv[]) create_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs)); read_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs)); + load_fd_plugins(me->plugin_directory); + drop(uid, gid); #ifdef BOMB @@ -248,6 +250,7 @@ void terminate_filed(int sig) bnet_stop_thread_server(server_tid); generate_daemon_event(NULL, "Exit"); + unload_plugins(); write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs)); delete_pid_file(me->pid_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs)); diff --git a/bacula/src/filed/filed.h b/bacula/src/filed/filed.h index 2e6a5becee..5212530265 100644 --- a/bacula/src/filed/filed.h +++ b/bacula/src/filed/filed.h @@ -1,14 +1,7 @@ -/* - * Bacula File Daemon specific configuration and defines - * - * Kern Sibbald, Jan MMI - * - * Version $Id$ - */ /* Bacula® - The Network Backup Solution - Copyright (C) 2001-2006 Free Software Foundation Europe e.V. + Copyright (C) 2001-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. @@ -32,9 +25,18 @@ (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, Switzerland, email:ftf@fsfeurope.org. */ +/* + * Bacula File Daemon specific configuration and defines + * + * Kern Sibbald, Jan MMI + * + * Version $Id$ + */ + #define FILE_DAEMON 1 #include "filed_conf.h" +#include "fd-plugins.h" #include "findlib/find.h" #include "jcr.h" #include "acl.h" diff --git a/bacula/src/filed/filed_conf.c b/bacula/src/filed/filed_conf.c index 9721c3a48d..18f68d7605 100644 --- a/bacula/src/filed/filed_conf.c +++ b/bacula/src/filed/filed_conf.c @@ -260,6 +260,9 @@ void free_resource(RES *sres, int type) if (res->res_client.scripts_directory) { free(res->res_client.scripts_directory); } + if (res->res_client.plugin_directory) { + free(res->res_client.plugin_directory); + } if (res->res_client.FDaddrs) { free_addresses(res->res_client.FDaddrs); } diff --git a/bacula/src/filed/job.c b/bacula/src/filed/job.c index 4e697c1b7b..c6e1891801 100644 --- a/bacula/src/filed/job.c +++ b/bacula/src/filed/job.c @@ -212,6 +212,7 @@ void *handle_client_request(void *dirp) jcr->last_fname = get_pool_memory(PM_FNAME); jcr->last_fname[0] = 0; jcr->client_name = get_memory(strlen(my_name) + 1); + new_plugins(jcr); /* instantiate plugins for this jcr */ pm_strcpy(jcr->client_name, my_name); jcr->pki_sign = me->pki_sign; jcr->pki_encrypt = me->pki_encrypt; @@ -280,11 +281,14 @@ void *handle_client_request(void *dirp) } generate_daemon_event(jcr, "JobEnd"); + generate_plugin_event(jcr, bEventJobEnd); dequeue_messages(jcr); /* send any queued messages */ /* Inform Director that we are done */ - bnet_sig(dir, BNET_TERMINATE); + dir->signal(BNET_TERMINATE); + + free_plugins(jcr); /* release instantiated plugins */ /* Clean up fileset */ FF_PKT *ff = jcr->ff; @@ -1405,6 +1409,7 @@ static int backup_cmd(JCR *jcr) } generate_daemon_event(jcr, "JobStart"); + generate_plugin_event(jcr, bEventJobStart); #if defined(WIN32_VSS) /* START VSS ON WIN 32 */ @@ -1554,6 +1559,7 @@ static int verify_cmd(JCR *jcr) dir->fsend(OKverify); generate_daemon_event(jcr, "JobStart"); + generate_plugin_event(jcr, bEventJobStart); Dmsg1(110, "bfiled>dird: %s", dir->msg); @@ -1650,7 +1656,7 @@ static int restore_cmd(JCR *jcr) jcr->replace = replace; jcr->prefix_links = prefix_links; - bnet_fsend(dir, OKrestore); + dir->fsend(OKrestore); Dmsg1(110, "bfiled>dird: %s", dir->msg); jcr->JobType = JT_RESTORE; @@ -1669,6 +1675,7 @@ static int restore_cmd(JCR *jcr) */ start_dir_heartbeat(jcr); generate_daemon_event(jcr, "JobStart"); + generate_plugin_event(jcr, bEventJobStart); do_restore(jcr); stop_dir_heartbeat(jcr); @@ -1680,13 +1687,13 @@ static int restore_cmd(JCR *jcr) /* * 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); bget_msg(sd); /* get OK */ /* Inform Storage daemon that we are done */ - bnet_sig(sd, BNET_TERMINATE); + sd->signal(BNET_TERMINATE); bail_out: diff --git a/bacula/src/plugins/fd/fd-plugins.c b/bacula/src/plugins/fd/fd-plugins.c index bf657f9fa8..1547f14a52 100644 --- a/bacula/src/plugins/fd/fd-plugins.c +++ b/bacula/src/plugins/fd/fd-plugins.c @@ -33,7 +33,6 @@ */ #include "bacula.h" #include "jcr.h" -#include "lib/plugin.h" #include "fd-plugins.h" const int dbglvl = 0; @@ -49,11 +48,6 @@ static bpError baculaJobMsg(bpContext *ctx, const char *file, int line, 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 = { @@ -71,15 +65,20 @@ static bFuncs bfuncs = { baculaJobMsg, baculaDebugMsg }; - - -void plugin_event(JCR *jcr, bEventType eventType) +/* + * Create a plugin event + */ +void generate_plugin_event(JCR *jcr, bEventType eventType) { bEvent event; Plugin *plugin; int i = 0; + if (!plugin_list) { + return; + } + bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId); event.eventType = eventType; @@ -95,7 +94,6 @@ void load_fd_plugins(const char *plugin_dir) } plugin_list = New(alist(10, not_owned_by_alist)); - load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type); } @@ -107,6 +105,10 @@ void new_plugins(JCR *jcr) Plugin *plugin; int i = 0; + if (!plugin_list) { + return; + } + int num = plugin_list->size(); if (num == 0) { @@ -116,7 +118,7 @@ void new_plugins(JCR *jcr) jcr->plugin_ctx = (void *)malloc(sizeof(bpContext) * num); bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; - Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId); + Dmsg2(dbglvl, "Instantiate plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId); foreach_alist(plugin, plugin_list) { /* Start a new instance of each plugin */ plugin_ctx[i].bContext = (void *)jcr; @@ -133,7 +135,12 @@ void free_plugins(JCR *jcr) Plugin *plugin; int i = 0; + if (!plugin_list) { + return; + } + bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; + Dmsg2(dbglvl, "Free instance plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId); foreach_alist(plugin, plugin_list) { /* Free the plugin instance */ plug_func(plugin)->freePlugin(&plugin_ctx[i++]); @@ -143,21 +150,28 @@ void free_plugins(JCR *jcr) } +/* ============================================================== + * + * Callbacks from the plugin + * + * ============================================================== + */ static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value) { JCR *jcr = (JCR *)(ctx->bContext); - Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var); +// Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var); if (!value) { return 1; } - Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr); +// Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr); switch (var) { case bVarJobId: *((int *)value) = jcr->JobId; Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId); break; case bVarFDName: - *((char **)value) = "FD Name"; + *((char **)value) = my_name; + Dmsg1(dbglvl, "Bacula: return my_name=%s\n", my_name); break; case bVarLevel: case bVarType: @@ -207,12 +221,15 @@ static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line, #ifdef TEST_PROGRAM + int main(int argc, char *argv[]) { char plugin_dir[1000]; JCR mjcr1, mjcr2; JCR *jcr1 = &mjcr1; JCR *jcr2 = &mjcr2; + + strcpy(my_name, "test-fd"); getcwd(plugin_dir, sizeof(plugin_dir)-1); load_fd_plugins(plugin_dir); @@ -223,11 +240,11 @@ int main(int argc, char *argv[]) jcr2->JobId = 222; new_plugins(jcr2); - plugin_event(jcr1, bEventJobStart); - plugin_event(jcr1, bEventJobEnd); - plugin_event(jcr2, bEventJobStart); + generate_plugin_event(jcr1, bEventJobStart); + generate_plugin_event(jcr1, bEventJobEnd); + generate_plugin_event(jcr2, bEventJobStart); free_plugins(jcr1); - plugin_event(jcr2, bEventJobEnd); + generate_plugin_event(jcr2, bEventJobEnd); free_plugins(jcr2); unload_plugins(); diff --git a/bacula/src/plugins/fd/fd-plugins.h b/bacula/src/plugins/fd/fd-plugins.h index 80aea4ab78..af575c9841 100644 --- a/bacula/src/plugins/fd/fd-plugins.h +++ b/bacula/src/plugins/fd/fd-plugins.h @@ -48,6 +48,8 @@ extern "C" { #endif + + /**************************************************************************** * * * Bacula definitions * @@ -93,6 +95,13 @@ typedef struct s_baculaFuncs { int level, const char *msg); } bFuncs; +/* Bacula Subroutines */ +void load_fd_plugins(const char *plugin_dir); +void new_plugins(JCR *jcr); +void free_plugins(JCR *jcr); +void generate_plugin_event(JCR *jcr, bEventType event); + + /**************************************************************************** * * -- 2.39.5