]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/plugins/fd/bpipe-fd.c
66f83c21431deb15b127347d8a10c3ff508f4efc
[bacula/bacula] / bacula / src / plugins / fd / bpipe-fd.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  * A simple pipe plugin for the Bacula File Daemon
30  *
31  *  Kern Sibbald, October 2007
32  *
33  */
34 #include "fd-plugins.h"
35
36 #undef malloc
37 #undef free
38 #undef strdup
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #define PLUGIN_LICENSE      "GPLv2"
45 #define PLUGIN_AUTHOR       "Kern Sibbald"
46 #define PLUGIN_DATE         "January 2008"
47 #define PLUGIN_VERSION      "1"
48 #define PLUGIN_DESCRIPTION  "Pipe File Daemon Plugin"
49
50 /* Forward referenced functions */
51 static bRC newPlugin(bpContext *ctx);
52 static bRC freePlugin(bpContext *ctx);
53 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value);
54 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value);
55 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value);
56 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp);
57 static bRC endBackupFile(bpContext *ctx);
58 static bRC pluginIO(bpContext *ctx, struct io_pkt *io);
59 static bRC startRestoreFile(bpContext *ctx, const char *cmd);
60 static bRC endRestoreFile(bpContext *ctx);
61 static bRC createFile(bpContext *ctx, struct restore_pkt *rp);
62 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp);
63
64
65 /* Pointers to Bacula functions */
66 static bFuncs *bfuncs = NULL;
67 static bInfo  *binfo = NULL;
68
69 /* Plugin Information block */
70 static pInfo pluginInfo = {
71    sizeof(pluginInfo),
72    PLUGIN_INTERFACE_VERSION,
73    PLUGIN_MAGIC,
74    PLUGIN_LICENSE,
75    PLUGIN_AUTHOR,
76    PLUGIN_DATE,
77    PLUGIN_VERSION,
78    PLUGIN_DESCRIPTION,
79 };
80
81 /* Plugin entry points for Bacula */
82 static pFuncs pluginFuncs = {
83    sizeof(pluginFuncs),
84    PLUGIN_INTERFACE_VERSION,
85
86    /* Entry points into plugin */
87    newPlugin,                         /* new plugin instance */
88    freePlugin,                        /* free plugin instance */
89    getPluginValue,
90    setPluginValue,
91    handlePluginEvent,
92    startBackupFile,
93    endBackupFile,
94    startRestoreFile,
95    endRestoreFile,
96    pluginIO,
97    createFile,
98    setFileAttributes
99 };
100
101 /*
102  * Plugin private context
103  */
104 struct plugin_ctx {
105    boffset_t offset;
106    FILE *fd;                          /* pipe file descriptor */
107    bool backup;                       /* set for backup (not needed) */
108    char *cmd;                         /* plugin command line */
109    char *fname;                       /* filename to "backup/restore" */
110    char *reader;                      /* reader program for backup */
111    char *writer;                      /* writer program for backup */
112 };
113
114 /*
115  * loadPlugin() and unloadPlugin() are entry points that are
116  *  exported, so Bacula can directly call these two entry points
117  *  they are common to all Bacula plugins.
118  */
119 /*
120  * External entry point called by Bacula to "load the plugin
121  */
122 bRC loadPlugin(bInfo *lbinfo, bFuncs *lbfuncs, pInfo **pinfo, pFuncs **pfuncs)
123 {
124    bfuncs = lbfuncs;                  /* set Bacula funct pointers */
125    binfo  = lbinfo;
126    *pinfo  = &pluginInfo;             /* return pointer to our info */
127    *pfuncs = &pluginFuncs;            /* return pointer to our functions */
128
129    return bRC_OK;
130 }
131
132 /*
133  * External entry point to unload the plugin 
134  */
135 bRC unloadPlugin() 
136 {
137 // printf("bpipe-fd: Unloaded\n");
138    return bRC_OK;
139 }
140
141 /*
142  * The following entry points are accessed through the function 
143  *   pointers we supplied to Bacula. Each plugin type (dir, fd, sd)
144  *   has its own set of entry points that the plugin must define.
145  */
146 /*
147  * Create a new instance of the plugin i.e. allocate our private storage
148  */
149 static bRC newPlugin(bpContext *ctx)
150 {
151    struct plugin_ctx *p_ctx = (struct plugin_ctx *)malloc(sizeof(struct plugin_ctx));
152    memset(p_ctx, 0, sizeof(struct plugin_ctx));
153    ctx->pContext = (void *)p_ctx;        /* set our context pointer */
154    return bRC_OK;
155 }
156
157 /*
158  * Free a plugin instance, i.e. release our private storage
159  */
160 static bRC freePlugin(bpContext *ctx)
161 {
162    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
163    if (p_ctx->cmd) {
164       free(p_ctx->cmd);                  /* free any allocated command string */
165    }
166    free(p_ctx);                          /* free our private context */
167    return bRC_OK;
168 }
169
170 /*
171  * Return some plugin value (none defined)
172  */
173 static bRC getPluginValue(bpContext *ctx, pVariable var, void *value) 
174 {
175    return bRC_OK;
176 }
177
178 /*
179  * Set a plugin value (none defined)
180  */
181 static bRC setPluginValue(bpContext *ctx, pVariable var, void *value) 
182 {
183    return bRC_OK;
184 }
185
186 /*
187  * Handle an event that was generated in Bacula
188  */
189 static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
190 {
191    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
192 // char *name;
193
194    switch (event->eventType) {
195    case bEventJobStart:
196 //    printf("bpipe-fd: JobStart=%s\n", (char *)value);
197       break;
198    case bEventJobEnd:
199 //    printf("bpipe-fd: JobEnd\n");
200       break;
201    case bEventStartBackupJob:
202 //    printf("bpipe-fd: BackupStart\n");
203       break;
204    case bEventEndBackupJob:
205 //    printf("bpipe-fd: BackupEnd\n");
206       break;
207    case bEventLevel:
208 //    printf("bpipe-fd: JobLevel=%c %d\n", (int)value, (int)value);
209       break;
210    case bEventSince:
211 //    printf("bpipe-fd: since=%d\n", (int)value);
212       break;
213
214    case bEventStartRestoreJob:
215       break;
216
217    case bEventEndRestoreJob:
218       break;
219
220    /* Plugin command e.g. plugin = <plugin-name>:<name-space>:read command:write command */
221    case bEventRestoreCommand:
222       printf("bpipe-fd: EventRestoreCommand cmd=%s\n", (char *)value);
223    case bEventBackupCommand:
224       char *p;
225       printf("bpipe-fd: pluginEvent cmd=%s\n", (char *)value);
226       p_ctx->cmd = strdup((char *)value);
227       p = strchr(p_ctx->cmd, ':');
228       if (!p) {
229          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Plugin terminator not found: %s\n", (char *)value);
230          return bRC_Error;
231       }
232       *p++ = 0;           /* terminate plugin */
233       p_ctx->fname = p;
234       p = strchr(p, ':');
235       if (!p) {
236          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "File terminator not found: %s\n", (char *)value);
237          return bRC_Error;
238       }
239       *p++ = 0;           /* terminate file */
240       p_ctx->reader = p;
241       p = strchr(p, ':');
242       if (!p) {
243          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Reader terminator not found: %s\n", (char *)value);
244          return bRC_Error;
245       }
246       *p++ = 0;           /* terminate reader string */
247       p_ctx->writer = p;
248       printf("bpipe-fd: plugin=%s fname=%s reader=%s writer=%s\n", 
249          p_ctx->cmd, p_ctx->fname, p_ctx->reader, p_ctx->writer);
250       break;
251
252    default:
253       printf("bpipe-fd: unknown event=%d\n", event->eventType);
254    }
255    return bRC_OK;
256 }
257
258 /* 
259  * Start the backup of a specific file
260  */
261 static bRC startBackupFile(bpContext *ctx, struct save_pkt *sp)
262 {
263    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
264    time_t now = time(NULL);
265    sp->fname = p_ctx->fname;
266    sp->statp.st_mode = 0700 | S_IFREG;
267    sp->statp.st_ctime = now;
268    sp->statp.st_mtime = now;
269    sp->statp.st_atime = now;
270    sp->statp.st_size = -1;
271    sp->statp.st_blksize = 4096;
272    sp->statp.st_blocks = 1;
273    p_ctx->backup = true;
274 // printf("bpipe-fd: startBackupFile\n");
275    return bRC_OK;
276 }
277
278 /*
279  * Done with backup of this file
280  */
281 static bRC endBackupFile(bpContext *ctx)
282 {
283    /*
284     * We would return bRC_More if we wanted startBackupFile to be
285     * called again to backup another file
286     */
287    return bRC_OK;
288 }
289
290 /*
291  * Bacula is calling us to do the actual I/O
292  */
293 static bRC pluginIO(bpContext *ctx, struct io_pkt *io)
294 {
295    struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
296     
297    io->status = 0;
298    io->io_errno = 0;
299    switch(io->func) {
300    case IO_OPEN:
301 //    printf("bpipe-fd: IO_OPEN\n");
302       if (io->flags & (O_CREAT | O_WRONLY)) {
303          p_ctx->fd = popen(p_ctx->writer, "w");
304          printf("bpipe-fd: IO_OPEN writer=%s\n", p_ctx->writer);
305          if (!p_ctx->fd) {
306             io->io_errno = errno;
307             bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, 
308                "Open pipe writer=%s failed: ERR=%s\n", p_ctx->writer, strerror(errno));
309             return bRC_Error;
310          }
311       } else {
312          p_ctx->fd = popen(p_ctx->reader, "r");
313 //       printf("bpipe-fd: IO_OPEN reader=%s\n", p_ctx->reader);
314          if (!p_ctx->fd) {
315             io->io_errno = errno;
316             bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, 
317                "Open pipe reader=%s failed: ERR=%s\n", p_ctx->reader, strerror(errno));
318             return bRC_Error;
319          }
320       }
321       sleep(1);                 /* let pipe connect */
322       break;
323
324    case IO_READ:
325       if (!p_ctx->fd) {
326          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL read FD\n");
327          return bRC_Error;
328       }
329       io->status = fread(io->buf, 1, io->count, p_ctx->fd);
330 //    printf("bpipe-fd: IO_READ buf=%p len=%d\n", io->buf, io->status);
331       if (io->status == 0 && ferror(p_ctx->fd)) {
332          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, 
333             "Pipe read error: ERR=%s\n", strerror(errno));
334 //       printf("Error reading pipe\n");
335          return bRC_Error;
336       }
337       break;
338
339    case IO_WRITE:
340       if (!p_ctx->fd) {
341          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL write FD\n");
342          return bRC_Error;
343       }
344 //    printf("bpipe-fd: IO_WRITE fd=%p buf=%p len=%d\n", p_ctx->fd, io->buf, io->count);
345       io->status = fwrite(io->buf, 1, io->count, p_ctx->fd);
346 //    printf("bpipe-fd: IO_WRITE buf=%p len=%d\n", io->buf, io->status);
347       if (io->status == 0 && ferror(p_ctx->fd)) {
348          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, 
349             "Pipe write error\n");
350 //       printf("Error writing pipe\n");
351          return bRC_Error;
352       }
353       break;
354
355    case IO_CLOSE:
356       if (!p_ctx->fd) {
357          bfuncs->JobMessage(ctx, __FILE__, __LINE__, M_FATAL, 0, "Logic error: NULL FD\n");
358          return bRC_Error;
359       }
360       io->status = pclose(p_ctx->fd);
361       break;
362
363    case IO_SEEK:
364       io->offset = p_ctx->offset;
365       break;
366    }
367    return bRC_OK;
368 }
369
370 static bRC startRestoreFile(bpContext *ctx, const char *cmd)
371 {
372 // printf("bpipe-fd: startRestoreFile cmd=%s\n", cmd);
373    return bRC_OK;
374 }
375
376 static bRC endRestoreFile(bpContext *ctx)
377 {
378 // printf("bpipe-fd: endRestoreFile\n");
379    return bRC_OK;
380 }
381
382 static bRC createFile(bpContext *ctx, struct restore_pkt *rp)
383 {
384 // printf("bpipe-fd: createFile\n");
385    return bRC_OK;
386 }
387
388 static bRC setFileAttributes(bpContext *ctx, struct restore_pkt *rp)
389 {
390 // printf("bpipe-fd: setFileAttributes\n");
391    return bRC_OK;
392 }
393
394
395 #ifdef __cplusplus
396 }
397 #endif