From: Kern Sibbald Date: Tue, 5 Feb 2008 10:56:20 +0000 (+0000) Subject: Plugin implementation X-Git-Tag: Release-7.0.0~5065 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=33b8d8823d5119895062bc3e6d4a76298278dd5a;p=bacula%2Fbacula Plugin implementation git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@6370 91ce42f0-d328-0410-95d8-f526ca767f89 --- diff --git a/bacula/src/filed/fd-plugins.c b/bacula/src/filed/fd-plugins.c index 72b4151573..12188cc685 100644 --- a/bacula/src/filed/fd-plugins.c +++ b/bacula/src/filed/fd-plugins.c @@ -38,7 +38,7 @@ const int dbglvl = 50; const char *plugin_type = "-fd.so"; -/* External functions */ +/* Function pointers to be set here */ extern int (*plugin_bopen)(JCR *jcr, const char *fname, int flags, mode_t mode); extern int (*plugin_bclose)(JCR *jcr); extern ssize_t (*plugin_bread)(JCR *jcr, void *buf, size_t count); @@ -46,12 +46,12 @@ extern ssize_t (*plugin_bwrite)(JCR *jcr, void *buf, size_t count); /* Forward referenced functions */ -static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value); -static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value); -static bpError baculaRegisterEvents(bpContext *ctx, ...); -static bpError baculaJobMsg(bpContext *ctx, const char *file, int line, +static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value); +static bRC baculaSetValue(bpContext *ctx, bVariable var, void *value); +static bRC baculaRegisterEvents(bpContext *ctx, ...); +static bRC baculaJobMsg(bpContext *ctx, const char *file, int line, int type, time_t mtime, const char *msg); -static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line, +static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line, int level, const char *msg); static int my_plugin_bopen(JCR *jcr, const char *fname, int flags, mode_t mode); @@ -240,12 +240,12 @@ static ssize_t my_plugin_bwrite(JCR *jcr, void *buf, size_t count) * * ============================================================== */ -static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value) +static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value) { JCR *jcr = (JCR *)(ctx->bContext); // Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var); if (!value) { - return 1; + return bRC_Error; } // Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr); switch (var) { @@ -265,16 +265,16 @@ static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value) case bVarSinceTime: break; } - return 0; + return bRC_OK; } -static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value) +static bRC baculaSetValue(bpContext *ctx, bVariable var, void *value) { Dmsg1(dbglvl, "bacula: baculaSetValue var=%d\n", var); - return 0; + return bRC_OK; } -static bpError baculaRegisterEvents(bpContext *ctx, ...) +static bRC baculaRegisterEvents(bpContext *ctx, ...) { va_list args; uint32_t event; @@ -284,23 +284,23 @@ static bpError baculaRegisterEvents(bpContext *ctx, ...) Dmsg1(dbglvl, "Plugin wants event=%u\n", event); } va_end(args); - return 0; + return bRC_OK; } -static bpError baculaJobMsg(bpContext *ctx, const char *file, int line, +static bRC baculaJobMsg(bpContext *ctx, const char *file, int line, int type, time_t mtime, const char *msg) { Dmsg5(dbglvl, "Job message: %s:%d type=%d time=%ld msg=%s\n", file, line, type, mtime, msg); - return 0; + return bRC_OK; } -static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line, +static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line, int level, const char *msg) { Dmsg4(dbglvl, "Debug message: %s:%d level=%d msg=%s\n", file, line, level, msg); - return 0; + return bRC_OK; } #ifdef TEST_PROGRAM diff --git a/bacula/src/filed/fd-plugins.h b/bacula/src/filed/fd-plugins.h index 513739603d..30f4d35103 100644 --- a/bacula/src/filed/fd-plugins.h +++ b/bacula/src/filed/fd-plugins.h @@ -64,6 +64,8 @@ struct io_pkt { int func; /* Function code */ int count; /* read/write count */ char *buf; /* read/write buffer */ + int status; /* return status */ + int io_errno; /* errno code */ }; /**************************************************************************** @@ -122,12 +124,12 @@ extern "C" { typedef struct s_baculaFuncs { uint32_t size; uint32_t version; - 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, + bRC (*registerBaculaEvents)(bpContext *ctx, ...); + bRC (*getBaculaValue)(bpContext *ctx, bVariable var, void *value); + bRC (*setBaculaValue)(bpContext *ctx, bVariable var, void *value); + bRC (*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, + bRC (*DebugMessage)(bpContext *ctx, const char *file, int line, int level, const char *msg); } bFuncs; @@ -163,13 +165,13 @@ typedef struct s_pluginInfo { typedef struct s_pluginFuncs { uint32_t size; uint32_t version; - 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, void *value); - bpError (*startPluginBackup)(bpContext *ctx, struct save_pkt *sp); - bpError (*pluginIO)(bpContext *ctx, struct io_pkt *io); + bRC (*newPlugin)(bpContext *ctx); + bRC (*freePlugin)(bpContext *ctx); + bRC (*getPluginValue)(bpContext *ctx, pVariable var, void *value); + bRC (*setPluginValue)(bpContext *ctx, pVariable var, void *value); + bRC (*handlePluginEvent)(bpContext *ctx, bEvent *event, void *value); + bRC (*startPluginBackup)(bpContext *ctx, struct save_pkt *sp); + bRC (*pluginIO)(bpContext *ctx, struct io_pkt *io); } pFuncs; #define plug_func(plugin) ((pFuncs *)(plugin->pfuncs)) diff --git a/bacula/src/lib/plugins.h b/bacula/src/lib/plugins.h index 0320832309..0c8332261b 100644 --- a/bacula/src/lib/plugins.h +++ b/bacula/src/lib/plugins.h @@ -57,8 +57,12 @@ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result); extern DLL_IMP_EXP alist *plugin_list; -/* Universal return code from all functions */ -typedef int32_t bpError; +/* Universal return codes from all functions */ +typedef enum { + bRC_OK = 0, /* OK */ + bRC_Stop = 1, /* Stop calling plugins */ + bRC_Error = 2, +} bRC; /* Context packet as first argument of all functions */ typedef struct s_bpContext { @@ -67,8 +71,8 @@ typedef struct s_bpContext { } bpContext; extern "C" { -typedef bpError (*t_loadPlugin)(void *binfo, void *bfuncs, void **pinfo, void **pfuncs); -typedef bpError (*t_unloadPlugin)(void); +typedef bRC (*t_loadPlugin)(void *binfo, void *bfuncs, void **pinfo, void **pfuncs); +typedef bRC (*t_unloadPlugin)(void); } class Plugin { diff --git a/bacula/src/plugins/fd/Makefile b/bacula/src/plugins/fd/Makefile index b925c6f749..df708819c9 100644 --- a/bacula/src/plugins/fd/Makefile +++ b/bacula/src/plugins/fd/Makefile @@ -13,7 +13,7 @@ LIBDIR=../../lib .c.o: $(CC) -I${SRCDIR} -I${FDDOR} -DTEST_PROGRAM -c $< -all: main example-plugin-fd.so +all: main example-plugin-fd.so bpipe-fd.so fd-plugins.o: ${FDDIR}/fd-plugins.h ${FDDIR}/fd-plugins.c $(CC) -I${SRCDIR} -I${FDDIR} -DTEST_PROGRAM -c ${FDDIR}/fd-plugins.c @@ -27,6 +27,11 @@ example-plugin-fd.o: example-plugin-fd.c ${FDDIR}/fd-plugins.h example-plugin-fd.so: example-plugin-fd.o $(CC) -shared example-plugin-fd.o -o example-plugin-fd.so +bpipe-fd.o: bpipe-fd.c ${FDDIR}/fd-plugins.h + $(CC) -fPIC -I../.. -I${FDDIR} -c bpipe-fd.c + +bpipe-fd.so: bpipe-fd.o + $(CC) -shared bpipe-fd.o -o bpipe-fd.so clean: rm -f main *.so *.o 1 2 3 diff --git a/bacula/src/plugins/fd/bpipe-fd.c b/bacula/src/plugins/fd/bpipe-fd.c new file mode 100644 index 0000000000..01e30d3d30 --- /dev/null +++ b/bacula/src/plugins/fd/bpipe-fd.c @@ -0,0 +1,203 @@ +/* + 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. +*/ +/* + * Sample Plugin program + * + * Kern Sibbald, October 2007 + * + */ +#include +#include "fd-plugins.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define PLUGIN_LICENSE "GPL" +#define PLUGIN_AUTHOR "Kern Sibbald" +#define PLUGIN_DATE "January 2008" +#define PLUGIN_VERSION "1" +#define PLUGIN_DESCRIPTION "Test File Daemon Plugin" + +/* Forward referenced functions */ +static bRC newPlugin(bpContext *ctx); +static bRC freePlugin(bpContext *ctx); +static bRC getPluginValue(bpContext *ctx, pVariable var, void *value); +static bRC setPluginValue(bpContext *ctx, pVariable var, void *value); +static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value); +static bRC startPluginBackup(bpContext *ctx, struct save_pkt *sp); +static bRC pluginIO(bpContext *ctx, struct io_pkt *io); + + +/* Pointers to Bacula functions */ +static bFuncs *bfuncs = NULL; +static bInfo *binfo = NULL; + +static pInfo pluginInfo = { + sizeof(pluginInfo), + PLUGIN_INTERFACE_VERSION, + PLUGIN_MAGIC, + PLUGIN_LICENSE, + PLUGIN_AUTHOR, + PLUGIN_DATE, + PLUGIN_VERSION, + PLUGIN_DESCRIPTION, +}; + +static pFuncs pluginFuncs = { + sizeof(pluginFuncs), + PLUGIN_INTERFACE_VERSION, + + /* Entry points into plugin */ + newPlugin, /* new plugin instance */ + freePlugin, /* free plugin instance */ + getPluginValue, + setPluginValue, + handlePluginEvent, + startPluginBackup, + pluginIO +}; + +bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs) +{ + bfuncs = lbfuncs; /* set Bacula funct pointers */ + binfo = lbinfo; + printf("plugin: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version); + + *pinfo = &pluginInfo; /* return pointer to our info */ + *pfuncs = &pluginFuncs; /* return pointer to our functions */ + + return bRC_OK; +} + +bRC unloadPlugin() +{ + printf("plugin: Unloaded\n"); + return bRC_OK; +} + +static bRC newPlugin(bpContext *ctx) +{ + int JobId = 0; + bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId); +// printf("plugin: newPlugin JobId=%d\n", JobId); + bfuncs->registerBaculaEvents(ctx, 1, 2, 0); + return bRC_OK; +} + +static bRC freePlugin(bpContext *ctx) +{ + int JobId = 0; + bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId); +// printf("plugin: freePlugin JobId=%d\n", JobId); + return bRC_OK; +} + +static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) +{ +// printf("plugin: getPluginValue var=%d\n", var); + return bRC_OK; +} + +static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) +{ +// printf("plugin: setPluginValue var=%d\n", var); + return bRC_OK; +} + +static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value) +{ + char *name; + + switch (event->eventType) { + case bEventJobStart: + printf("plugin: JobStart=%s\n", (char *)value); + break; + case bEventJobEnd: + printf("plugin: JobEnd\n"); + break; + case bEventBackupStart: + printf("plugin: BackupStart\n"); + break; + case bEventBackupEnd: + printf("plugin: BackupEnd\n"); + break; + case bEventLevel: + printf("plugin: JobLevel=%c %d\n", (int)value, (int)value); + break; + case bEventSince: + printf("plugin: since=%d\n", (int)value); + break; + + /* Plugin command e.g. plugin = ::command */ + case bEventPluginCommand: + printf("plugin: command=%s\n", (char *)value); + break; + + default: + printf("plugin: unknown event=%d\n", event->eventType); + } + bfuncs->getBaculaValue(ctx, bVarFDName, (void *)&name); +// printf("FD Name=%s\n", name); +// bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message"); +// bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message"); + return bRC_OK; +} + +static bRC startPluginBackup(bpContext *ctx, struct save_pkt *sp) +{ + return bRC_OK; +} + +/* + * Do actual I/O + */ +static bRC pluginIO(bpContext *ctx, struct io_pkt *io) +{ + io->status = 0; + io->io_errno = 0; + switch(io->func) { + case IO_OPEN: + printf("plugin: IO_OPEN\n"); + break; + case IO_READ: + printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count); + break; + case IO_WRITE: + printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count); + break; + case IO_CLOSE: + printf("plugin: IO_CLOSE\n"); + break; + } + return bRC_OK; +} + +#ifdef __cplusplus +} +#endif diff --git a/bacula/src/plugins/fd/example-plugin-fd.c b/bacula/src/plugins/fd/example-plugin-fd.c index b87a448264..01e30d3d30 100644 --- a/bacula/src/plugins/fd/example-plugin-fd.c +++ b/bacula/src/plugins/fd/example-plugin-fd.c @@ -45,11 +45,13 @@ extern "C" { #define PLUGIN_DESCRIPTION "Test File Daemon Plugin" /* Forward referenced functions */ -static bpError newPlugin(bpContext *ctx); -static bpError freePlugin(bpContext *ctx); -static bpError getPluginValue(bpContext *ctx, pVariable var, void *value); -static bpError setPluginValue(bpContext *ctx, pVariable var, void *value); -static bpError handlePluginEvent(bpContext *ctx, bEvent *event, void *value); +static bRC newPlugin(bpContext *ctx); +static bRC freePlugin(bpContext *ctx); +static bRC getPluginValue(bpContext *ctx, pVariable var, void *value); +static bRC setPluginValue(bpContext *ctx, pVariable var, void *value); +static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value); +static bRC startPluginBackup(bpContext *ctx, struct save_pkt *sp); +static bRC pluginIO(bpContext *ctx, struct io_pkt *io); /* Pointers to Bacula functions */ @@ -76,10 +78,12 @@ static pFuncs pluginFuncs = { freePlugin, /* free plugin instance */ getPluginValue, setPluginValue, - handlePluginEvent + handlePluginEvent, + startPluginBackup, + pluginIO }; -bpError loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs) +bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs) { bfuncs = lbfuncs; /* set Bacula funct pointers */ binfo = lbinfo; @@ -88,47 +92,48 @@ bpError loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfunc *pinfo = &pluginInfo; /* return pointer to our info */ *pfuncs = &pluginFuncs; /* return pointer to our functions */ - return 0; + return bRC_OK; } -bpError unloadPlugin() +bRC unloadPlugin() { printf("plugin: Unloaded\n"); - return 0; + return bRC_OK; } -static bpError newPlugin(bpContext *ctx) +static bRC newPlugin(bpContext *ctx) { int JobId = 0; bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId); // printf("plugin: newPlugin JobId=%d\n", JobId); bfuncs->registerBaculaEvents(ctx, 1, 2, 0); - return 0; + return bRC_OK; } -static bpError freePlugin(bpContext *ctx) +static bRC freePlugin(bpContext *ctx) { int JobId = 0; bfuncs->getBaculaValue(ctx, bVarJobId, (void *)&JobId); // printf("plugin: freePlugin JobId=%d\n", JobId); - return 0; + return bRC_OK; } -static bpError getPluginValue(bpContext *ctx, pVariable var, void *value) +static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) { // printf("plugin: getPluginValue var=%d\n", var); - return 0; + return bRC_OK; } -static bpError setPluginValue(bpContext *ctx, pVariable var, void *value) +static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) { // printf("plugin: setPluginValue var=%d\n", var); - return 0; + return bRC_OK; } -static bpError handlePluginEvent(bpContext *ctx, bEvent *event, void *value) +static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value) { char *name; + switch (event->eventType) { case bEventJobStart: printf("plugin: JobStart=%s\n", (char *)value); @@ -142,15 +147,18 @@ static bpError handlePluginEvent(bpContext *ctx, bEvent *event, void *value) case bEventBackupEnd: printf("plugin: BackupEnd\n"); break; - case bEventPluginCommand: - printf("plugin: command=%s\n", (char *)value); - break; case bEventLevel: printf("plugin: JobLevel=%c %d\n", (int)value, (int)value); break; case bEventSince: printf("plugin: since=%d\n", (int)value); break; + + /* Plugin command e.g. plugin = ::command */ + case bEventPluginCommand: + printf("plugin: command=%s\n", (char *)value); + break; + default: printf("plugin: unknown event=%d\n", event->eventType); } @@ -158,7 +166,36 @@ static bpError handlePluginEvent(bpContext *ctx, bEvent *event, void *value) // printf("FD Name=%s\n", name); // bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message"); // bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message"); - return 0; + return bRC_OK; +} + +static bRC startPluginBackup(bpContext *ctx, struct save_pkt *sp) +{ + return bRC_OK; +} + +/* + * Do actual I/O + */ +static bRC pluginIO(bpContext *ctx, struct io_pkt *io) +{ + io->status = 0; + io->io_errno = 0; + switch(io->func) { + case IO_OPEN: + printf("plugin: IO_OPEN\n"); + break; + case IO_READ: + printf("plugin: IO_READ buf=%p len=%d\n", io->buf, io->count); + break; + case IO_WRITE: + printf("plugin: IO_WRITE buf=%p len=%d\n", io->buf, io->count); + break; + case IO_CLOSE: + printf("plugin: IO_CLOSE\n"); + break; + } + return bRC_OK; } #ifdef __cplusplus