2 Bacula® - The Network Backup Solution
4 Copyright (C) 2007-2008 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 two of the GNU 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 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 John Walker.
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
36 #include "lib/plugin.h"
37 #include "fd-plugins.h"
40 const char *plugin_type = "-fd.so";
43 /* Forward referenced functions */
44 static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value);
45 static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value);
46 static bpError baculaRegisterEvents(bpContext *ctx, ...);
47 static bpError baculaJobMsg(bpContext *ctx, const char *file, int line,
48 int type, time_t mtime, const char *msg);
49 static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line,
50 int level, const char *msg);
52 void load_fd_plugins(const char *plugin_dir);
53 void new_plugins(JCR *jcr);
54 void free_plugins(JCR *jcr);
55 void plugin_event(JCR *jcr, bEventType event);
59 static bInfo binfo = {
64 /* Bacula entry points */
65 static bFuncs bfuncs = {
77 void plugin_event(JCR *jcr, bEventType eventType)
83 bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
84 Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId);
85 event.eventType = eventType;
86 foreach_alist(plugin, plugin_list) {
87 plug_func(plugin)->handlePluginEvent(&plugin_ctx[i++], &event);
91 void load_fd_plugins(const char *plugin_dir)
97 plugin_list = New(alist(10, not_owned_by_alist));
99 load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type);
103 * Create a new instance of each plugin for this Job
105 void new_plugins(JCR *jcr)
110 int num = plugin_list->size();
116 jcr->plugin_ctx = (void *)malloc(sizeof(bpContext) * num);
118 bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
119 Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx, jcr->JobId);
120 foreach_alist(plugin, plugin_list) {
121 /* Start a new instance of each plugin */
122 plugin_ctx[i].bContext = (void *)jcr;
123 plugin_ctx[i].pContext = NULL;
124 plug_func(plugin)->newPlugin(&plugin_ctx[i++]);
129 * Free the plugin instances for this Job
131 void free_plugins(JCR *jcr)
136 bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
137 foreach_alist(plugin, plugin_list) {
138 /* Free the plugin instance */
139 plug_func(plugin)->freePlugin(&plugin_ctx[i++]);
142 jcr->plugin_ctx = NULL;
146 static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value)
148 JCR *jcr = (JCR *)(ctx->bContext);
149 Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
153 Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr);
156 *((int *)value) = jcr->JobId;
157 Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId);
160 *((char **)value) = "FD Name";
173 static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value)
175 Dmsg1(dbglvl, "bacula: baculaSetValue var=%d\n", var);
179 static bpError baculaRegisterEvents(bpContext *ctx, ...)
185 while ((event = va_arg(args, uint32_t))) {
186 Dmsg1(dbglvl, "Plugin wants event=%u\n", event);
192 static bpError baculaJobMsg(bpContext *ctx, const char *file, int line,
193 int type, time_t mtime, const char *msg)
195 Dmsg5(dbglvl, "Job message: %s:%d type=%d time=%ld msg=%s\n",
196 file, line, type, mtime, msg);
200 static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line,
201 int level, const char *msg)
203 Dmsg4(dbglvl, "Debug message: %s:%d level=%d msg=%s\n",
204 file, line, level, msg);
210 int main(int argc, char *argv[])
212 char plugin_dir[1000];
217 getcwd(plugin_dir, sizeof(plugin_dir)-1);
218 load_fd_plugins(plugin_dir);
226 plugin_event(jcr1, bEventJobStart);
227 plugin_event(jcr1, bEventJobEnd);
228 plugin_event(jcr2, bEventJobStart);
230 plugin_event(jcr2, bEventJobEnd);
235 Dmsg0(dbglvl, "bacula: OK ...\n");
241 #endif /* TEST_PROGRAM */