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 "dir-plugins.h"
39 const char *plugin_type = "-dir.so";
42 /* Forward referenced functions */
43 static bRC baculaGetValue(bpContext *ctx, brVariable var, void *value);
44 static bRC baculaSetValue(bpContext *ctx, bwVariable var, void *value);
45 static bRC baculaRegisterEvents(bpContext *ctx, ...);
46 static bRC baculaJobMsg(bpContext *ctx, const char *file, int line,
47 int type, time_t mtime, const char *msg);
48 static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line,
49 int level, const char *msg);
53 static bInfo binfo = {
58 /* Bacula entry points */
59 static bFuncs bfuncs = {
70 * Create a plugin event
72 void generate_plugin_event(JCR *jcr, bEventType eventType)
82 bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
83 Dmsg2(dbglvl, "plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
84 event.eventType = eventType;
85 foreach_alist(plugin, plugin_list) {
86 plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i++], &event);
90 void load_dir_plugins(const char *plugin_dir)
96 plugin_list = New(alist(10, not_owned_by_alist));
97 load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type);
101 * Create a new instance of each plugin for this Job
103 void new_plugins(JCR *jcr)
112 int num = plugin_list->size();
118 jcr->plugin_ctx_list = (void *)malloc(sizeof(bpContext) * num);
120 bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
121 Dmsg2(dbglvl, "Instantiate plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
122 foreach_alist(plugin, plugin_list) {
123 /* Start a new instance of each plugin */
124 plugin_ctx_list[i].bContext = (void *)jcr;
125 plugin_ctx_list[i].pContext = NULL;
126 plug_func(plugin)->newPlugin(&plugin_ctx_list[i++]);
131 * Free the plugin instances for this Job
133 void free_plugins(JCR *jcr)
142 bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
143 Dmsg2(dbglvl, "Free instance plugin_ctx_list=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
144 foreach_alist(plugin, plugin_list) {
145 /* Free the plugin instance */
146 plug_func(plugin)->freePlugin(&plugin_ctx_list[i++]);
148 free(plugin_ctx_list);
149 jcr->plugin_ctx_list = NULL;
153 /* ==============================================================
155 * Callbacks from the plugin
157 * ==============================================================
159 static bRC baculaGetValue(bpContext *ctx, brVariable var, void *value)
161 JCR *jcr = (JCR *)(ctx->bContext);
162 // Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
166 // Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr);
169 *((int *)value) = jcr->JobId;
170 Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId);
178 static bRC baculaSetValue(bpContext *ctx, bwVariable var, void *value)
180 Dmsg1(dbglvl, "bacula: baculaSetValue var=%d\n", var);
184 static bRC baculaRegisterEvents(bpContext *ctx, ...)
190 while ((event = va_arg(args, uint32_t))) {
191 Dmsg1(dbglvl, "Plugin wants event=%u\n", event);
197 static bRC baculaJobMsg(bpContext *ctx, const char *file, int line,
198 int type, time_t mtime, const char *msg)
200 Dmsg5(dbglvl, "Job message: %s:%d type=%d time=%ld msg=%s\n",
201 file, line, type, mtime, msg);
205 static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line,
206 int level, const char *msg)
208 Dmsg4(dbglvl, "Debug message: %s:%d level=%d msg=%s\n",
209 file, line, level, msg);
216 int main(int argc, char *argv[])
218 char plugin_dir[1000];
223 strcpy(my_name, "test-dir");
225 getcwd(plugin_dir, sizeof(plugin_dir)-1);
226 load_dir_plugins(plugin_dir);
234 generate_plugin_event(jcr1, bEventJobStart);
235 generate_plugin_event(jcr1, bEventJobEnd);
236 generate_plugin_event(jcr2, bEventJobStart);
238 generate_plugin_event(jcr2, bEventJobEnd);
243 Dmsg0(dbglvl, "bacula: OK ...\n");
249 #endif /* TEST_PROGRAM */