]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/fd_cmds.c
f77fc060697451c8e210486c645ef97e7890fc3a
[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, 2001, 2002 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 int do_append_data(JCR *jcr);
44 extern int 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    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 = %s %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 Volumes = %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    Dmsg1(20, "Start run Job=%s\n", jcr->Job);
115    bnet_fsend(dir, Job_start, jcr->Job); 
116    time(&jcr->start_time);
117    jcr->run_time = jcr->start_time;
118    jcr->JobStatus = JS_Running;
119    dir_send_job_status(jcr);          /* update director */
120    for (quit=0; !quit;) {
121
122       /* Read command coming from the File daemon */
123       if (bnet_recv(fd) <= 0) {
124          break;                       /* connection terminated */
125       }
126       Dmsg1(110, "<filed: %s", fd->msg);
127       found = 0;
128       for (i=0; fd_cmds[i].cmd; i++) {
129          if (strncmp(fd_cmds[i].cmd, fd->msg, strlen(fd_cmds[i].cmd)) == 0) {
130             found = 1;               /* indicate command found */
131             if (!fd_cmds[i].func(jcr)) {    /* do command */
132                jcr->JobStatus = JS_ErrorTerminated;
133                quit = 1;
134             }
135             break;
136          }
137       }
138       if (!found) {                   /* command not found */
139          Dmsg1(110, "<filed: Command not found: %s\n", fd->msg);
140          bnet_fsend(fd, ferrmsg);
141          break;
142       }
143    }
144    time(&jcr->end_time);
145    if (!job_cancelled(jcr)) {
146       jcr->JobStatus = JS_Terminated;
147    }
148    bnet_fsend(dir, Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
149       edit_uint64(jcr->JobBytes, ec1));
150
151    bnet_sig(dir, BNET_EOD);           /* send EOD to Director daemon */
152    return;
153 }
154
155         
156 /*
157  *   Append Data command
158  *     Open Data Channel and receive Data for archiving
159  *     Write the Data to the archive device
160  */
161 static int append_data_cmd(JCR *jcr)
162 {
163    BSOCK *fd = jcr->file_bsock;
164
165    Dmsg1(120, "Append data: %s", fd->msg);
166    if (jcr->session_opened) {
167       Dmsg1(10, "<bfiled: %s", fd->msg);
168       if (do_append_data(jcr)) {
169          bnet_fsend(fd, OK_append);
170          jcr->JobType = JT_BACKUP;
171          return 1;
172       } else {
173          bnet_fsend(fd, ERROR_append);
174          return 0;
175       }
176    } else {
177       bnet_fsend(fd, NOT_opened);
178       return 0;
179    }
180 }
181
182 static int append_end_session(JCR *jcr)
183 {
184    BSOCK *fd = jcr->file_bsock;
185
186    Dmsg1(120, "store<file: %s", fd->msg);
187    if (!jcr->session_opened) {
188       bnet_fsend(fd, NOT_opened);
189       return 0;
190    }
191    return bnet_fsend(fd, OK_end);
192 }
193
194
195 /* 
196  * Append Open session command
197  *
198  */
199 static int append_open_session(JCR *jcr)
200 {
201    BSOCK *fd = jcr->file_bsock;
202
203    Dmsg1(120, "Append open session: %s", fd->msg);
204    if (jcr->session_opened) {
205       bnet_fsend(fd, NO_open);
206       return 0;
207    }
208
209    Dmsg1(110, "Append open session: %s\n", dev_name(jcr->device->dev));
210    jcr->session_opened = TRUE;
211
212    /* Send "Ticket" to File Daemon */
213    bnet_fsend(fd, OK_open, jcr->VolSessionId);
214    Dmsg1(110, ">filed: %s", fd->msg);
215
216    return 1;
217 }
218
219 /*
220  *   Append Close session command
221  *      Close the append session and send back Statistics     
222  *         (need to fix statistics)
223  */
224 static int append_close_session(JCR *jcr)
225 {
226    BSOCK *fd = jcr->file_bsock;
227
228    Dmsg1(120, "<filed: %s\n", fd->msg);
229    if (!jcr->session_opened) {
230       bnet_fsend(fd, NOT_opened);
231       return 0;
232    }
233    /* Send final statistics to File daemon */
234    bnet_fsend(fd, OK_close, jcr->NumVolumes);
235    Dmsg1(160, ">filed: %s\n", fd->msg);
236
237    bnet_sig(fd, BNET_EOD);            /* send EOD to File daemon */
238        
239    Dmsg1(110, "Append close session: %s\n", dev_name(jcr->device->dev));
240
241    if (jcr->JobStatus != JS_ErrorTerminated) {
242       jcr->JobStatus = JS_Terminated;
243    }
244    jcr->session_opened = FALSE;
245    return 1;
246 }
247
248 /*
249  *   Read Data command
250  *     Open Data Channel, read the data from  
251  *     the archive device and send to File
252  *     daemon.
253  */
254 static int read_data_cmd(JCR *jcr)
255 {
256    BSOCK *fd = jcr->file_bsock;
257
258    Dmsg1(120, "Read data: %s\n", fd->msg);
259    if (jcr->session_opened) {
260       Dmsg1(120, "<bfiled: %s", fd->msg);
261       return do_read_data(jcr);
262    } else {
263       bnet_fsend(fd, NOT_opened);
264       return 0;
265    }
266 }
267
268 /* 
269  * Read Open session command
270  *
271  *  We need to scan for the parameters of the job
272  *    to be restored.
273  */
274 static int read_open_session(JCR *jcr)
275 {
276    BSOCK *fd = jcr->file_bsock;
277
278    Dmsg1(120, "%s\n", fd->msg);
279    if (jcr->session_opened) {
280       bnet_fsend(fd, NO_open);
281       return 0;
282    }
283
284    if (sscanf(fd->msg, read_open, jcr->VolumeName, &jcr->read_VolSessionId,
285          &jcr->read_VolSessionTime, &jcr->read_StartFile, &jcr->read_EndFile,
286          &jcr->read_StartBlock, &jcr->read_EndBlock) == 7) {
287       if (jcr->session_opened) {
288          bnet_fsend(fd, NOT_opened);
289          return 0;
290       }
291       Dmsg4(100, "read_open_session got: JobId=%d Vol=%s VolSessId=%ld VolSessT=%ld\n",
292          jcr->JobId, jcr->VolumeName, jcr->read_VolSessionId, 
293          jcr->read_VolSessionTime);
294       Dmsg4(100, "  StartF=%ld EndF=%ld StartB=%ld EndB=%ld\n",
295          jcr->read_StartFile, jcr->read_EndFile, jcr->read_StartBlock,
296          jcr->read_EndBlock);
297    }
298
299    Dmsg1(10, "Read open session: %s\n", dev_name(jcr->device->dev));
300
301    jcr->session_opened = TRUE;
302    jcr->JobType = JT_RESTORE;
303
304    /* Send "Ticket" to File Daemon */
305    bnet_fsend(fd, OK_open, jcr->VolSessionId);
306    Dmsg1(110, ">filed: %s", fd->msg);
307
308    return 1;
309 }
310
311 static int bootstrap_cmd(JCR *jcr)
312 {
313    BSOCK *fd = jcr->file_bsock;
314    POOLMEM *fname = get_pool_memory(PM_FNAME);
315    FILE *bs;
316
317    if (jcr->RestoreBootstrap) {
318       unlink(jcr->RestoreBootstrap);
319       free_pool_memory(jcr->RestoreBootstrap);
320    }
321    Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
322       jcr->Job);
323    Dmsg1(400, "bootstrap=%s\n", fname);
324    jcr->RestoreBootstrap = fname;
325    bs = fopen(fname, "a+");           /* create file */
326    if (!bs) {
327       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
328          jcr->RestoreBootstrap, strerror(errno));
329       goto bail_out;
330    }
331    while (bnet_recv(fd) > 0) {
332        Dmsg1(400, "stored<filed: bootstrap file %s\n", fd->msg);
333        fputs(fd->msg, bs);
334    }
335    fclose(bs);
336    jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
337    if (!jcr->bsr) {
338       Jmsg(jcr, M_FATAL, 0, _("Error parsing bootstrap file.\n"));
339       goto bail_out;
340    }
341    if (debug_level > 20) {
342       dump_bsr(jcr->bsr);
343    }
344    return bnet_fsend(fd, OK_bootstrap);
345
346 bail_out:
347    unlink(jcr->RestoreBootstrap);
348    free_pool_memory(jcr->RestoreBootstrap);
349    jcr->RestoreBootstrap = NULL;
350    bnet_fsend(fd, ERROR_bootstrap);
351    return 0;
352
353 }
354
355
356 /*
357  *   Read Close session command
358  *      Close the read session
359  */
360 static int read_close_session(JCR *jcr)
361 {
362    BSOCK *fd = jcr->file_bsock;
363
364    Dmsg1(120, "Read close session: %s\n", fd->msg);
365    if (!jcr->session_opened) {
366       bnet_fsend(fd, NOT_opened);
367       return 0;
368    }
369    /* Send final statistics to File daemon */
370    bnet_fsend(fd, OK_close);
371    Dmsg1(160, ">filed: %s\n", fd->msg);
372
373    bnet_sig(fd, BNET_EOD);          /* send EOD to File daemon */
374        
375    jcr->session_opened = FALSE;
376    return 1;
377 }