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