]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/sd/example-plugin-sd.c
21b674a2863bdf5719414dab4eb8d4f64958504c
[bacula/bacula] / bacula / src / plugins / sd / example-plugin-sd.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2007-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  * Sample Storage daemon Plugin program
22  *
23  *  Kern Sibbald, October 2007
24  *
25  */
26 #include "bacula.h"         /* General Bacula headers */
27 #include "stored.h"         /* Pull in storage daemon headers */
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #define PLUGIN_LICENSE      "AGPLv3"
34 #define PLUGIN_AUTHOR       "Kern Sibbald"
35 #define PLUGIN_DATE         "November 2011"
36 #define PLUGIN_VERSION      "2"
37 #define PLUGIN_DESCRIPTION  "Test Storage Daemon Plugin"
38
39 /* Forward referenced functions */
40 static bRC newPlugin(bpContext *ctx);
41 static bRC freePlugin(bpContext *ctx);
42 static bRC getPluginValue(bpContext *ctx, psdVariable var, void *value);
43 static bRC setPluginValue(bpContext *ctx, psdVariable var, void *value);
44 static bRC handlePluginEvent(bpContext *ctx, bsdEvent *event, void *value);
45 static bRC handleGlobalPluginEvent(bsdEvent *event, void *value);
46
47
48 /* Pointers to Bacula functions */
49 static bsdFuncs *bfuncs = NULL;
50 static bsdInfo  *binfo = NULL;
51
52 static psdInfo pluginInfo = {
53    sizeof(pluginInfo),
54    SD_PLUGIN_INTERFACE_VERSION,
55    SD_PLUGIN_MAGIC,
56    PLUGIN_LICENSE,
57    PLUGIN_AUTHOR,
58    PLUGIN_DATE,
59    PLUGIN_VERSION,
60    PLUGIN_DESCRIPTION
61 };
62
63 static psdFuncs pluginFuncs = {
64    sizeof(pluginFuncs),
65    SD_PLUGIN_INTERFACE_VERSION,
66
67    /* Entry points into plugin */
68    newPlugin,                         /* new plugin instance */
69    freePlugin,                        /* free plugin instance */
70    getPluginValue,
71    setPluginValue,
72    handlePluginEvent,
73    handleGlobalPluginEvent
74 };
75
76 /*
77  * loadPlugin() and unloadPlugin() are entry points that are
78  *  exported, so Bacula can directly call these two entry points
79  *  they are common to all Bacula plugins.
80  *
81  * External entry point called by Bacula to "load the plugin
82  */
83 bRC DLL_IMP_EXP
84 loadPlugin(bsdInfo *lbinfo, bsdFuncs *lbfuncs, psdInfo **pinfo, psdFuncs **pfuncs)
85 {
86    bfuncs = lbfuncs;                /* set Bacula funct pointers */
87    binfo  = lbinfo;
88    printf("example-plugin-sd: Loaded: size=%d version=%d\n", bfuncs->size, bfuncs->version);
89    *pinfo  = &pluginInfo;             /* return pointer to our info */
90    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
91    printf("example-plugin-sd: Loaded\n");
92    return bRC_OK;
93 }
94
95 /*
96  * External entry point to unload the plugin 
97  */
98 bRC DLL_IMP_EXP
99 unloadPlugin() 
100 {
101    printf("example-plugin-sd: Unloaded\n");
102    return bRC_OK;
103 }
104
105 /*
106  * The following entry points are accessed through the function 
107  *   pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
108  *   has its own set of entry points that the plugin must define.
109  */
110 /*
111  * Create a new instance of the plugin i.e. allocate our private storage
112  */
113 static bRC newPlugin(bpContext *ctx)
114 {
115    int JobId = 0;
116    bfuncs->getBaculaValue(ctx, bsdVarJobId, (void *)&JobId);
117    printf("example-plugin-sd: newPlugin JobId=%d\n", JobId);
118    bfuncs->registerBaculaEvents(ctx, 1, 2, 0);
119    return bRC_OK;
120 }
121
122 /*
123  * Free a plugin instance, i.e. release our private storage
124  */
125 static bRC freePlugin(bpContext *ctx)
126 {
127    int JobId = 0;
128    bfuncs->getBaculaValue(ctx, bsdVarJobId, (void *)&JobId);
129    printf("example-plugin-sd: freePlugin JobId=%d\n", JobId);
130    return bRC_OK;
131 }
132
133 /*
134  * Return some plugin value (none defined)
135  */
136 static bRC getPluginValue(bpContext *ctx, psdVariable var, void *value) 
137 {
138    printf("example-plugin-sd: getPluginValue var=%d\n", var);
139    return bRC_OK;
140 }
141
142 /*
143  * Set a plugin value (none defined)
144  */
145 static bRC setPluginValue(bpContext *ctx, psdVariable var, void *value) 
146 {
147    printf("example-plugin-sd: setPluginValue var=%d\n", var);
148    return bRC_OK;
149 }
150
151 /*
152  * Handle an event that was generated in Bacula
153  */
154 static bRC handlePluginEvent(bpContext *ctx, bsdEvent *event, void *value)
155 {
156    char *name;
157    switch (event->eventType) {
158    case bsdEventJobStart:
159       printf("example-plugin-sd: HandleEvent JobStart :%s:\n", (char *)value);
160       break;
161    case bsdEventJobEnd:
162       printf("example-plugin-sd: HandleEvent JobEnd\n");
163       break;
164    }
165    bfuncs->getBaculaValue(ctx, bsdVarJobName, (void *)&name);
166    printf("Job Name=%s\n", name);
167    bfuncs->JobMessage(ctx, __FILE__, __LINE__, 1, 0, "JobMesssage message");
168    bfuncs->DebugMessage(ctx, __FILE__, __LINE__, 1, "DebugMesssage message");
169    return bRC_OK;
170 }
171
172 /*
173  * Handle a Global event -- no context
174  */
175 static bRC handleGlobalPluginEvent(bsdEvent *event, void *value)
176 {
177    return bRC_OK;
178 }
179
180
181 #ifdef __cplusplus
182 }
183 #endif