]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd-plugins.c
Add core fd plugin code
[bacula/bacula] / bacula / src / dird / fd-plugins.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2007-2008 Free Software Foundation Europe e.V.
5
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.
12
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.
17
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
21    02110-1301, USA.
22
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.
27 */
28 /*
29  * Bacula File daemon core code for loading and running plugins.
30  *
31  * Kern Sibbald, October 2007
32  */
33 #include "bacula.h"
34 #include "lib/plugin.h"
35 #include "fd-plugins.h"
36
37 const char *plugin_type = "-fd.so";
38
39
40 /* Forward referenced functions */
41 static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value);
42 static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value);
43 static bpError baculaRegisterEvents(bpContext *ctx, ...);
44 static bpError baculaJobMsg(bpContext *ctx, const char *file, int line,
45   int type, time_t mtime, const char *msg);
46 static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line,
47   int level, const char *msg);
48
49 /* Bacula info */
50 static bInfo binfo = {
51    sizeof(bFuncs),
52    PLUGIN_INTERFACE,
53 };
54
55 /* Bacula entry points */
56 static bFuncs bfuncs = {
57    sizeof(bFuncs),
58    PLUGIN_INTERFACE,
59    baculaRegisterEvents,
60    baculaGetValue,
61    baculaSetValue,
62    baculaJobMsg,
63    baculaDebugMsg
64 };
65     
66
67 void init_fd_plugins(const char *plugin_dir)
68 {
69    Plugin *plugin;
70
71    if (!plugin_dir) {
72       return;
73    }
74     
75    plugin_list = New(alist(10, not_owned_by_alist));
76
77    load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type);
78 }
79
80 /*
81  * Create a new instance of each plugin for this Job
82  */
83 void new_fd_plugins(JCR *jcr)
84 {
85    bpContext *ctx;
86    Plugin *plugin;
87    int i = 0;
88
89    int num = plugin_list->size();
90
91    if (num == 0) {
92       return;
93    }
94
95    jcr->plugin_ctx = (bpContext **)malloc(sizeof(bpContext) * num);
96
97    ctx = jcr->plugin_ctx;
98    foreach_alist(plugin, plugin_list) {
99       /* Start a new instance of each plugin */
100       ctx[i].bContext = (void *)jcr;
101       ctx[i].pContext = NULL;
102       plug_func(plugin)->newPlugin(ctx[i++]);
103    }
104 }
105
106 /*
107  * Free the plugin instances for this Job
108  */
109 void free_fd_plugins(JCR *jcr)
110 {
111    bpContext *ctx;
112    Plugin *plugin;
113    int i = 0;
114
115    ctx = jcr->plugin_ctx;
116    foreach_alist(plugin, jcr->plugin_list) {
117       /* Free the plugin instance */
118       plug_func(plugin)->freePlugin(ctx[i++]);
119    }
120    free(ctx);
121    jcr->plugin_ctx = NULL;
122 }
123
124 void term_fd_plugins()
125 {
126    unload_plugins();
127 }
128
129 static bpError baculaGetValue(bpContext *ctx, bVariable var, void *value)
130 {
131    printf("bacula: baculaGetValue var=%d\n", var);
132    if (!value) {
133       return 1;
134    }
135    switch (var) {
136    case bVarJobId:
137       *((int *)value) = 100;
138       break;
139    case bVarFDName:
140       *((char **)value) = "FD Name";
141       break;
142    case bVarLevel:
143    case bVarType:
144    case bVarClient:
145    case bVarJobName:
146    case bVarJobStatus:
147    case bVarSinceTime:
148       break;
149    }
150    return 0;
151 }
152
153 static bpError baculaSetValue(bpContext *ctx, bVariable var, void *value)
154 {
155    printf("bacula: baculaSetValue var=%d\n", var);
156    return 0;
157 }
158
159 static bpError baculaRegisterEvents(bpContext *ctx, ...)
160 {
161    va_list args;
162    uint32_t event;
163
164    va_start(args, ctx);
165    while ((event = va_arg(args, uint32_t))) {
166       printf("Plugin wants event=%u\n", event);
167    }
168    va_end(args);
169    return 0;
170 }
171
172 static bpError baculaJobMsg(bpContext *ctx, const char *file, int line,
173   int type, time_t mtime, const char *msg)
174 {
175    printf("Job message: %s:%d type=%d time=%ld msg=%s\n",
176       file, line, type, mtime, msg);
177    return 0;
178 }
179
180 static bpError baculaDebugMsg(bpContext *ctx, const char *file, int line,
181   int level, const char *msg)
182 {
183    printf("Debug message: %s:%d level=%d msg=%s\n",
184       file, line, level, msg);
185    return 0;
186 }