]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/fd_cmds.c
39961dbfaedfaea11c5a9f02241a8aaec0ba78fe
[bacula/bacula] / bacula / src / stored / fd_cmds.c
1 /*
2  * This file handles commands from the File daemon.
3  *
4  * We get here because the Director has initiated a Job with
5  *  the Storage daemon, then done the same with the File daemon,
6  *  then when the Storage daemon receives a proper connection from
7  *  the File daemon, control is passed here to handle the 
8  *  subsequent File daemon commands.
9  *
10  *   Version $Id$
11  * 
12  */
13 /*
14    Copyright (C) 2000-2004 Kern Sibbald and John Walker
15
16    This program is free software; you can redistribute it and/or
17    modify it under the terms of the GNU General Public License as
18    published by the Free Software Foundation; either version 2 of
19    the License, or (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public
27    License along with this program; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29    MA 02111-1307, USA.
30
31  */
32
33 #include "bacula.h"
34 #include "stored.h"
35
36 /* Imported variables */
37 extern STORES *me;
38
39 /* Static variables */
40 static char ferrmsg[]      = "3900 Invalid command\n";
41
42 /* Imported functions */
43 extern bool do_append_data(JCR *jcr);
44 extern bool do_read_data(JCR *jcr);
45
46 /* Forward referenced functions */
47
48
49 /* Forward referenced FD commands */
50 static int append_open_session(JCR *jcr);
51 static int append_close_session(JCR *jcr);
52 static int append_data_cmd(JCR *jcr);
53 static int append_end_session(JCR *jcr);
54 static int read_open_session(JCR *jcr);
55 static int read_data_cmd(JCR *jcr);
56 static int read_close_session(JCR *jcr);
57 static int bootstrap_cmd(JCR *jcr);
58
59 struct s_cmds {
60    const char *cmd;
61    int (*func)(JCR *jcr);
62 };
63
64 /*  
65  * The following are the recognized commands from the File daemon
66  */
67 static struct s_cmds fd_cmds[] = {
68    {"append open",  append_open_session},
69    {"append data",  append_data_cmd},
70    {"append end",   append_end_session},
71    {"append close", append_close_session},
72    {"read open",    read_open_session},
73    {"read data",    read_data_cmd},
74    {"read close",   read_close_session},
75    {"bootstrap",    bootstrap_cmd},
76    {NULL,           NULL}                  /* list terminator */
77 };
78
79 /* Commands from the File daemon that require additional scanning */
80 static char read_open[]       = "read open session = %127s %ld %ld %ld %ld %ld %ld\n";
81
82 /* Responses sent to the File daemon */
83 static char NO_open[]         = "3901 Error session already open\n";
84 static char NOT_opened[]      = "3902 Error session not opened\n";
85 static char OK_end[]          = "3000 OK end\n";
86 static char OK_close[]        = "3000 OK close Status = %d\n";
87 static char OK_open[]         = "3000 OK open ticket = %d\n";
88 static char OK_append[]       = "3000 OK append data\n";
89 static char ERROR_append[]    = "3903 Error append data\n";
90 static char OK_bootstrap[]    = "3000 OK bootstrap\n";
91 static char ERROR_bootstrap[] = "3904 Error bootstrap\n";
92
93 /* Information sent to the Director */
94 static char Job_start[] = "3010 Job %s start\n";
95 static char Job_end[]   = 
96    "3099 Job %s end JobStatus=%d JobFiles=%d JobBytes=%s\n";
97
98 /*
99  * Run a File daemon Job -- File daemon already authorized
100  *
101  * Basic task here is:
102  * - Read a command from the File daemon
103  * - Execute it
104  *
105  */
106 void run_job(JCR *jcr)
107 {
108    int i, found, quit;
109    BSOCK *fd = jcr->file_bsock;
110    BSOCK *dir = jcr->dir_bsock;
111    char ec1[30];
112
113
114    fd->jcr = jcr;
115    dir->jcr = jcr;
116    Dmsg1(120, "Start run Job=%s\n", jcr->Job);
117    bnet_fsend(dir, Job_start, jcr->Job); 
118    jcr->start_time = time(NULL);
119    jcr->run_time = jcr->start_time;
120    set_jcr_job_status(jcr, JS_Running);
121    dir_send_job_status(jcr);          /* update director */
122    for (quit=0; !quit;) {
123       int stat;
124
125       /* Read command coming from the File daemon */
126       stat = bnet_recv(fd);
127       if (is_bnet_stop(fd)) {         /* hardeof or error */
128          break;                       /* connection terminated */
129       }
130       if (stat <= 0) {
131          continue;                    /* ignore signals and zero length msgs */
132       }
133       Dmsg1(110, "<filed: %s", fd->msg);
134       found = 0;
135       for (i=0; fd_cmds[i].cmd; i++) {
136          if (strncmp(fd_cmds[i].cmd, fd->msg, strlen(fd_cmds[i].cmd)) == 0) {
137             found = 1;               /* indicate command found */
138             if (!fd_cmds[i].func(jcr)) {    /* do command */
139                set_jcr_job_status(jcr, JS_ErrorTerminated);
140                quit = 1;
141             }
142             break;
143          }
144       }
145       if (!found) {                   /* command not found */
146          Dmsg1(110, "<filed: Command not found: %s\n", fd->msg);
147          bnet_fsend(fd, ferrmsg);
148          break;
149       }
150    }
151    bnet_sig(fd, BNET_TERMINATE);      /* signal to FD job is done */
152    jcr->end_time = time(NULL);
153    dequeue_messages(jcr);             /* send any queued messages */
154    set_jcr_job_status(jcr, JS_Terminated);
155    bnet_fsend(dir, Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
156       edit_uint64(jcr->JobBytes, ec1));
157    bnet_sig(dir, BNET_EOD);           /* send EOD to Director daemon */
158    return;
159 }
160
161         
162 /*
163  *   Append Data command
164  *     Open Data Channel and receive Data for archiving
165  *     Write the Data to the archive device
166  */
167 static int append_data_cmd(JCR *jcr)
168 {
169    BSOCK *fd = jcr->file_bsock;
170
171    Dmsg1(120, "Append data: %s", fd->msg);
172    if (jcr->session_opened) {
173       Dmsg1(110, "<bfiled: %s", fd->msg);
174       jcr->JobType = JT_BACKUP;
175       if (do_append_data(jcr)) {
176          bnet_fsend(fd, OK_append);
177          return 1;
178       } else {
179          bnet_suppress_error_messages(fd, 1); /* ignore errors at this point */
180          bnet_fsend(fd, ERROR_append);
181          return 0;
182       }
183    } else {
184       bnet_fsend(fd, NOT_opened);
185       return 0;
186    }
187 }
188
189 static int append_end_session(JCR *jcr)
190 {
191    BSOCK *fd = jcr->file_bsock;
192
193    Dmsg1(120, "store<file: %s", fd->msg);
194    if (!jcr->session_opened) {
195       bnet_fsend(fd, NOT_opened);
196       return 0;
197    }
198    set_jcr_job_status(jcr, JS_Terminated);
199    return bnet_fsend(fd, OK_end);
200 }
201
202
203 /* 
204  * Append Open session command
205  *
206  */
207 static int append_open_session(JCR *jcr)
208 {
209    BSOCK *fd = jcr->file_bsock;
210
211    Dmsg1(120, "Append open session: %s", fd->msg);
212    if (jcr->session_opened) {
213       bnet_fsend(fd, NO_open);
214       return 0;
215    }
216
217    Dmsg1(110, "Append open session: %s\n", dev_name(jcr->device->dev));
218    jcr->session_opened = TRUE;
219
220    /* Send "Ticket" to File Daemon */
221    bnet_fsend(fd, OK_open, jcr->VolSessionId);
222    Dmsg1(110, ">filed: %s", fd->msg);
223
224    return 1;
225 }
226
227 /*
228  *   Append Close session command
229  *      Close the append session and send back Statistics     
230  *         (need to fix statistics)
231  */
232 static int append_close_session(JCR *jcr)
233 {
234    BSOCK *fd = jcr->file_bsock;
235
236    Dmsg1(120, "<filed: %s\n", fd->msg);
237    if (!jcr->session_opened) {
238       bnet_fsend(fd, NOT_opened);
239       return 0;
240    }
241    /* Send final statistics to File daemon */
242    bnet_fsend(fd, OK_close, jcr->JobStatus);
243    Dmsg1(120, ">filed: %s\n", fd->msg);
244
245    bnet_sig(fd, BNET_EOD);            /* send EOD to File daemon */
246        
247    Dmsg1(110, "Append close session: %s\n", dev_name(jcr->device->dev));
248
249    jcr->session_opened = FALSE;
250    return 1;
251 }
252
253 /*
254  *   Read Data command
255  *     Open Data Channel, read the data from  
256  *     the archive device and send to File
257  *     daemon.
258  */
259 static int read_data_cmd(JCR *jcr)
260 {
261    BSOCK *fd = jcr->file_bsock;
262
263    Dmsg1(120, "Read data: %s\n", fd->msg);
264    if (jcr->session_opened) {
265       Dmsg1(120, "<bfiled: %s", fd->msg);
266       return do_read_data(jcr);
267    } else {
268       bnet_fsend(fd, NOT_opened);
269       return 0;
270    }
271 }
272
273 /* 
274  * Read Open session command
275  *
276  *  We need to scan for the parameters of the job
277  *    to be restored.
278  */
279 static int read_open_session(JCR *jcr)
280 {
281    BSOCK *fd = jcr->file_bsock;
282
283    Dmsg1(120, "%s\n", fd->msg);
284    if (jcr->session_opened) {
285       bnet_fsend(fd, NO_open);
286       return 0;
287    }
288
289    if (sscanf(fd->msg, read_open, jcr->VolumeName, &jcr->read_VolSessionId,
290          &jcr->read_VolSessionTime, &jcr->read_StartFile, &jcr->read_EndFile,
291          &jcr->read_StartBlock, &jcr->read_EndBlock) == 7) {
292       if (jcr->session_opened) {
293          bnet_fsend(fd, NOT_opened);
294          return 0;
295       }
296       Dmsg4(100, "read_open_session got: JobId=%d Vol=%s VolSessId=%ld VolSessT=%ld\n",
297          jcr->JobId, jcr->VolumeName, jcr->read_VolSessionId, 
298          jcr->read_VolSessionTime);
299       Dmsg4(100, "  StartF=%ld EndF=%ld StartB=%ld EndB=%ld\n",
300          jcr->read_StartFile, jcr->read_EndFile, jcr->read_StartBlock,
301          jcr->read_EndBlock);
302    }
303
304    Dmsg1(110, "Read open session: %s\n", dev_name(jcr->device->dev));
305
306    jcr->session_opened = TRUE;
307    jcr->JobType = JT_RESTORE;
308
309    /* Send "Ticket" to File Daemon */
310    bnet_fsend(fd, OK_open, jcr->VolSessionId);
311    Dmsg1(110, ">filed: %s", fd->msg);
312
313    return 1;
314 }
315
316 static int bootstrap_cmd(JCR *jcr)
317 {
318    BSOCK *fd = jcr->file_bsock;
319    POOLMEM *fname = get_pool_memory(PM_FNAME);
320    FILE *bs;
321    int stat = 0;
322
323    if (jcr->RestoreBootstrap) {
324       unlink(jcr->RestoreBootstrap);
325       free_pool_memory(jcr->RestoreBootstrap);
326    }
327    Mmsg(fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
328       jcr->Job);
329    Dmsg1(400, "bootstrap=%s\n", fname);
330    jcr->RestoreBootstrap = fname;
331    bs = fopen(fname, "a+");           /* create file */
332    if (!bs) {
333       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
334          jcr->RestoreBootstrap, strerror(errno));
335       goto bail_out;
336    }
337    while (bnet_recv(fd) >= 0) {
338        Dmsg1(400, "stored<filed: bootstrap file %s\n", fd->msg);
339        fputs(fd->msg, bs);
340    }
341    fclose(bs);
342    jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
343    if (!jcr->bsr) {
344       Jmsg(jcr, M_FATAL, 0, _("Error parsing bootstrap file.\n"));
345       goto bail_out;
346    }
347    if (debug_level > 20) {
348       dump_bsr(jcr->bsr, true);
349    }
350    stat = 1;
351
352 bail_out:
353    unlink(jcr->RestoreBootstrap);
354    free_pool_memory(jcr->RestoreBootstrap);
355    jcr->RestoreBootstrap = NULL;
356    if (stat) {
357       return bnet_fsend(fd, OK_bootstrap);
358    } else {
359       bnet_fsend(fd, ERROR_bootstrap);
360       return 0;
361    }
362 }
363
364
365 /*
366  *   Read Close session command
367  *      Close the read session
368  */
369 static int read_close_session(JCR *jcr)
370 {
371    BSOCK *fd = jcr->file_bsock;
372
373    Dmsg1(120, "Read close session: %s\n", fd->msg);
374    if (!jcr->session_opened) {
375       bnet_fsend(fd, NOT_opened);
376       return 0;
377    }
378    /* Send final statistics to File daemon */
379    bnet_fsend(fd, OK_close);
380    Dmsg1(160, ">filed: %s\n", fd->msg);
381
382    bnet_sig(fd, BNET_EOD);          /* send EOD to File daemon */
383        
384    jcr->session_opened = FALSE;
385    return 1;
386 }