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