From: Kern Sibbald Date: Thu, 3 Jan 2008 20:31:44 +0000 (+0000) Subject: plugin update X-Git-Tag: Release-7.0.0~5210 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=67c827a9d868a4eeba29449c98da1408c22055a6;p=bacula%2Fbacula plugin update git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@6190 91ce42f0-d328-0410-95d8-f526ca767f89 --- diff --git a/bacula/src/plugins/fd/Makefile b/bacula/src/plugins/fd/Makefile index 6d6e68830a..d6a13e01c8 100644 --- a/bacula/src/plugins/fd/Makefile +++ b/bacula/src/plugins/fd/Makefile @@ -11,10 +11,10 @@ CC = g++ -g -O0 -Wall all: main example-plugin-fd.so -main: main.o plugin-fd.h +main: main.o fd-plugins.h $(CC) -L../../lib main.o -o main -lbac -lpthread -lssl -l crypto -ldl -example-plugin-fd.o: example-plugin-fd.c plugin-fd.h +example-plugin-fd.o: example-plugin-fd.c fd-plugins.h $(CC) -fPIC -I../.. -c example-plugin-fd.c example-plugin-fd.so: example-plugin-fd.o diff --git a/bacula/src/plugins/fd/example-plugin-fd.c b/bacula/src/plugins/fd/example-plugin-fd.c index 13315586f5..b7816033ef 100644 --- a/bacula/src/plugins/fd/example-plugin-fd.c +++ b/bacula/src/plugins/fd/example-plugin-fd.c @@ -32,7 +32,7 @@ * */ #include -#include "plugin-fd.h" +#include "fd-plugins.h" #ifdef __cplusplus extern "C" { diff --git a/bacula/src/plugins/fd/fd-plugins.h b/bacula/src/plugins/fd/fd-plugins.h new file mode 100644 index 0000000000..80aea4ab78 --- /dev/null +++ b/bacula/src/plugins/fd/fd-plugins.h @@ -0,0 +1,140 @@ +/* + Bacula® - The Network Backup Solution + + Copyright (C) 2007-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. + This program is Free Software; you can redistribute it and/or + modify it under the terms of version two of the GNU General Public + License as published by the Free Software Foundation, which is + listed in the file LICENSE. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. + + Bacula® is a registered trademark of John Walker. + The licensor of Bacula is the Free Software Foundation Europe + (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, + Switzerland, email:ftf@fsfeurope.org. +*/ +/* + * Interface definition for Bacula Plugins + * + * Kern Sibbald, October 2007 + * + */ + +#ifndef __PLUGIN_FD_H +#define __PLUGIN_FD_H + +#include +#ifndef __CONFIG_H +#define __CONFIG_H +#include "config.h" +#endif +#include "bc_types.h" +#include "lib/plugin.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/**************************************************************************** + * * + * Bacula definitions * + * * + ****************************************************************************/ + +/* Bacula Variable Ids */ +typedef enum { + bVarJobId = 1, + bVarFDName = 2, + bVarLevel = 3, + bVarType = 4, + bVarClient = 5, + bVarJobName = 6, + bVarJobStatus = 7, + bVarSinceTime = 8 +} bVariable; + +typedef enum { + bEventJobStart = 1, + bEventJobEnd = 2, +} bEventType; + +typedef struct s_bEvent { + uint32_t eventType; +} bEvent; + +typedef struct s_baculaInfo { + uint32_t size; + uint32_t interface; +} bInfo; + +/* Bacula interface version and function pointers */ +typedef struct s_baculaFuncs { + uint32_t size; + uint32_t interface; + bpError (*registerBaculaEvents)(bpContext *ctx, ...); + bpError (*getBaculaValue)(bpContext *ctx, bVariable var, void *value); + bpError (*setBaculaValue)(bpContext *ctx, bVariable var, void *value); + bpError (*JobMessage)(bpContext *ctx, const char *file, int line, + int type, time_t mtime, const char *msg); + bpError (*DebugMessage)(bpContext *ctx, const char *file, int line, + int level, const char *msg); +} bFuncs; + + +/**************************************************************************** + * * + * Plugin definitions * + * * + ****************************************************************************/ + +typedef enum { + pVarName = 1, + pVarDescription = 2 +} pVariable; + + +#define PLUGIN_MAGIC "*PluginData*" +#define PLUGIN_INTERFACE 1 + +typedef struct s_pluginInfo { + uint32_t size; + uint32_t interface; + char *plugin_magic; + char *plugin_license; + char *plugin_author; + char *plugin_date; + char *plugin_version; + char *plugin_description; +} pInfo; + +typedef struct s_pluginFuncs { + uint32_t size; + uint32_t interface; + bpError (*newPlugin)(bpContext *ctx); + bpError (*freePlugin)(bpContext *ctx); + bpError (*getPluginValue)(bpContext *ctx, pVariable var, void *value); + bpError (*setPluginValue)(bpContext *ctx, pVariable var, void *value); + bpError (*handlePluginEvent)(bpContext *ctx, bEvent *event); +} pFuncs; + +#define plug_func(plugin) ((pFuncs *)(plugin->pfuncs)) +#define plug_info(plugin) ((pInfo *)(plugin->pinfo)) + +#ifdef __cplusplus +} +#endif + +#endif /* __PLUGIN_FD_H */ diff --git a/bacula/src/plugins/fd/main.c b/bacula/src/plugins/fd/main.c index 9c6710be6c..797deb73fd 100644 --- a/bacula/src/plugins/fd/main.c +++ b/bacula/src/plugins/fd/main.c @@ -32,8 +32,9 @@ * Kern Sibbald, October 2007 */ #include "bacula.h" +#include "jcr.h" #include "lib/plugin.h" -#include "plugin-fd.h" +#include "fd-plugins.h" const char *plugin_type = "-fd.so"; @@ -47,6 +48,12 @@ 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 = { sizeof(bFuncs), @@ -65,49 +72,28 @@ static bFuncs bfuncs = { }; - - int main(int argc, char *argv[]) { char plugin_dir[1000]; - bpContext ctx; - bEvent event; - Plugin *plugin; + JCR mjcr1, mjcr2; + JCR *jcr1 = &mjcr1; + JCR *jcr2 = &mjcr2; - plugin_list = New(alist(10, not_owned_by_alist)); - - ctx.bContext = NULL; - ctx.pContext = NULL; getcwd(plugin_dir, sizeof(plugin_dir)-1); + load_fd_plugins(plugin_dir); - load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type); + jcr1->JobId = 1; + new_plugins(jcr1); - foreach_alist(plugin, plugin_list) { - printf("bacula: plugin_size=%d plugin_version=%d\n", - plug_func(plugin)->size, plug_func(plugin)->interface); - printf("License: %s\nAuthor: %s\nDate: %s\nVersion: %s\nDescription: %s\n", - plug_info(plugin)->plugin_license, plug_info(plugin)->plugin_author, - plug_info(plugin)->plugin_date, plug_info(plugin)->plugin_version, - plug_info(plugin)->plugin_description); - - /* Start a new instance of the plugin */ - plug_func(plugin)->newPlugin(&ctx); - event.eventType = bEventJobStart; - plug_func(plugin)->handlePluginEvent(&ctx, &event); - event.eventType = bEventJobEnd; - plug_func(plugin)->handlePluginEvent(&ctx, &event); - /* Free the plugin instance */ - plug_func(plugin)->freePlugin(&ctx); - - /* Start a new instance of the plugin */ - plug_func(plugin)->newPlugin(&ctx); - event.eventType = bEventJobStart; - plug_func(plugin)->handlePluginEvent(&ctx, &event); - event.eventType = bEventJobEnd; - plug_func(plugin)->handlePluginEvent(&ctx, &event); - /* Free the plugin instance */ - plug_func(plugin)->freePlugin(&ctx); - } + jcr2->JobId = 2; + 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(); @@ -117,6 +103,73 @@ int main(int argc, char *argv[]) return 0; } +void plugin_event(JCR *jcr, bEventType eventType) +{ + bEvent event; + Plugin *plugin; + int i = 0; + + bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; + 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_plugins(JCR *jcr) +{ + Plugin *plugin; + int i = 0; + + int num = plugin_list->size(); + + if (num == 0) { + return; + } + + jcr->plugin_ctx = (void *)malloc(sizeof(bpContext) * num); + + bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; + foreach_alist(plugin, plugin_list) { + /* Start a new instance of each plugin */ + 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_plugins(JCR *jcr) +{ + Plugin *plugin; + int i = 0; + + bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx; + foreach_alist(plugin, plugin_list) { + /* Free the plugin instance */ + plug_func(plugin)->freePlugin(&plugin_ctx[i++]); + } + free(plugin_ctx); + jcr->plugin_ctx = NULL; +} + + static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value) { printf("bacula: baculaGetValue var=%d\n", var); @@ -125,7 +178,7 @@ static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value) } switch (var) { case bVarJobId: - *((int *)value) = 100; + *((int *)value) = ((JCR *)ctx)->JobId; break; case bVarFDName: *((char **)value) = "FD Name"; diff --git a/bacula/src/plugins/fd/plugin-fd.h b/bacula/src/plugins/fd/plugin-fd.h deleted file mode 100644 index 80aea4ab78..0000000000 --- a/bacula/src/plugins/fd/plugin-fd.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - Bacula® - The Network Backup Solution - - Copyright (C) 2007-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. - This program is Free Software; you can redistribute it and/or - modify it under the terms of version two of the GNU General Public - License as published by the Free Software Foundation, which is - listed in the file LICENSE. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. - - Bacula® is a registered trademark of John Walker. - The licensor of Bacula is the Free Software Foundation Europe - (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich, - Switzerland, email:ftf@fsfeurope.org. -*/ -/* - * Interface definition for Bacula Plugins - * - * Kern Sibbald, October 2007 - * - */ - -#ifndef __PLUGIN_FD_H -#define __PLUGIN_FD_H - -#include -#ifndef __CONFIG_H -#define __CONFIG_H -#include "config.h" -#endif -#include "bc_types.h" -#include "lib/plugin.h" - -#ifdef __cplusplus -extern "C" { -#endif - - -/**************************************************************************** - * * - * Bacula definitions * - * * - ****************************************************************************/ - -/* Bacula Variable Ids */ -typedef enum { - bVarJobId = 1, - bVarFDName = 2, - bVarLevel = 3, - bVarType = 4, - bVarClient = 5, - bVarJobName = 6, - bVarJobStatus = 7, - bVarSinceTime = 8 -} bVariable; - -typedef enum { - bEventJobStart = 1, - bEventJobEnd = 2, -} bEventType; - -typedef struct s_bEvent { - uint32_t eventType; -} bEvent; - -typedef struct s_baculaInfo { - uint32_t size; - uint32_t interface; -} bInfo; - -/* Bacula interface version and function pointers */ -typedef struct s_baculaFuncs { - uint32_t size; - uint32_t interface; - bpError (*registerBaculaEvents)(bpContext *ctx, ...); - bpError (*getBaculaValue)(bpContext *ctx, bVariable var, void *value); - bpError (*setBaculaValue)(bpContext *ctx, bVariable var, void *value); - bpError (*JobMessage)(bpContext *ctx, const char *file, int line, - int type, time_t mtime, const char *msg); - bpError (*DebugMessage)(bpContext *ctx, const char *file, int line, - int level, const char *msg); -} bFuncs; - - -/**************************************************************************** - * * - * Plugin definitions * - * * - ****************************************************************************/ - -typedef enum { - pVarName = 1, - pVarDescription = 2 -} pVariable; - - -#define PLUGIN_MAGIC "*PluginData*" -#define PLUGIN_INTERFACE 1 - -typedef struct s_pluginInfo { - uint32_t size; - uint32_t interface; - char *plugin_magic; - char *plugin_license; - char *plugin_author; - char *plugin_date; - char *plugin_version; - char *plugin_description; -} pInfo; - -typedef struct s_pluginFuncs { - uint32_t size; - uint32_t interface; - bpError (*newPlugin)(bpContext *ctx); - bpError (*freePlugin)(bpContext *ctx); - bpError (*getPluginValue)(bpContext *ctx, pVariable var, void *value); - bpError (*setPluginValue)(bpContext *ctx, pVariable var, void *value); - bpError (*handlePluginEvent)(bpContext *ctx, bEvent *event); -} pFuncs; - -#define plug_func(plugin) ((pFuncs *)(plugin->pfuncs)) -#define plug_info(plugin) ((pInfo *)(plugin->pinfo)) - -#ifdef __cplusplus -} -#endif - -#endif /* __PLUGIN_FD_H */