]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/fd_cmds.c
replace set_Jobxxx by setJobxxx
[bacula/bacula] / bacula / src / stored / fd_cmds.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2009 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 three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    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 Affero 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 Kern Sibbald.
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  * This file handles commands from the File daemon.
30  *
31  *  Kern Sibbald, MM
32  *
33  * We get here because the Director has initiated a Job with
34  *  the Storage daemon, then done the same with the File daemon,
35  *  then when the Storage daemon receives a proper connection from
36  *  the File daemon, control is passed here to handle the
37  *  subsequent File daemon commands.
38  *
39  *
40  */
41
42 #include "bacula.h"
43 #include "stored.h"
44
45 /* Imported variables */
46 extern STORES *me;
47
48 /* Static variables */
49 static char ferrmsg[]      = "3900 Invalid command\n";
50
51 /* Imported functions */
52 extern bool do_append_data(JCR *jcr);
53 extern bool do_read_data(JCR *jcr);
54
55 /* Forward referenced FD commands */
56 static bool append_open_session(JCR *jcr);
57 static bool append_close_session(JCR *jcr);
58 static bool append_data_cmd(JCR *jcr);
59 static bool append_end_session(JCR *jcr);
60 static bool read_open_session(JCR *jcr);
61 static bool read_data_cmd(JCR *jcr);
62 static bool read_close_session(JCR *jcr);
63
64 /* Exported function */
65 bool get_bootstrap_file(JCR *jcr, BSOCK *bs);
66
67 struct s_cmds {
68    const char *cmd;
69    bool (*func)(JCR *jcr);
70 };
71
72 /*
73  * The following are the recognized commands from the File daemon
74  */
75 static struct s_cmds fd_cmds[] = {
76    {"append open",  append_open_session},
77    {"append data",  append_data_cmd},
78    {"append end",   append_end_session},
79    {"append close", append_close_session},
80    {"read open",    read_open_session},
81    {"read data",    read_data_cmd},
82    {"read close",   read_close_session},
83    {NULL,           NULL}                  /* list terminator */
84 };
85
86 /* Commands from the File daemon that require additional scanning */
87 static char read_open[]       = "read open session = %127s %ld %ld %ld %ld %ld %ld\n";
88
89 /* Responses sent to the File daemon */
90 static char NO_open[]         = "3901 Error session already open\n";
91 static char NOT_opened[]      = "3902 Error session not opened\n";
92 static char OK_end[]          = "3000 OK end\n";
93 static char OK_close[]        = "3000 OK close Status = %d\n";
94 static char OK_open[]         = "3000 OK open ticket = %d\n";
95 static char ERROR_append[]    = "3903 Error append data\n";
96
97 /* Information sent to the Director */
98 static char Job_start[] = "3010 Job %s start\n";
99 char Job_end[]   =
100    "3099 Job %s end JobStatus=%d JobFiles=%d JobBytes=%s JobErrors=%u\n";
101
102 /*
103  * Run a File daemon Job -- File daemon already authorized
104  *  Director sends us this command.
105  *
106  * Basic task here is:
107  * - Read a command from the File daemon
108  * - Execute it
109  *
110  */
111 void run_job(JCR *jcr)
112 {
113    BSOCK *dir = jcr->dir_bsock;
114    char ec1[30];
115
116    dir->set_jcr(jcr);
117    Dmsg1(120, "Start run Job=%s\n", jcr->Job);
118    dir->fsend(Job_start, jcr->Job);
119    jcr->start_time = time(NULL);
120    jcr->run_time = jcr->start_time;
121    set_jcr_job_status(jcr, JS_Running);
122    dir_send_job_status(jcr);          /* update director */
123    do_fd_commands(jcr);
124    jcr->end_time = time(NULL);
125    dequeue_messages(jcr);             /* send any queued messages */
126    set_jcr_job_status(jcr, JS_Terminated);
127    generate_daemon_event(jcr, "JobEnd");
128    dir->fsend(Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
129       edit_uint64(jcr->JobBytes, ec1), jcr->JobErrors);
130    dir->signal(BNET_EOD);             /* send EOD to Director daemon */
131    return;
132 }
133
134 /*
135  * Now talk to the FD and do what he says
136  */
137 void do_fd_commands(JCR *jcr)
138 {
139    int i;
140    bool found, quit;
141    BSOCK *fd = jcr->file_bsock;
142
143    fd->set_jcr(jcr);
144    for (quit=false; !quit;) {
145       int stat;
146
147       /* Read command coming from the File daemon */
148       stat = fd->recv();
149       if (is_bnet_stop(fd)) {         /* hardeof or error */
150          break;                       /* connection terminated */
151       }
152       if (stat <= 0) {
153          continue;                    /* ignore signals and zero length msgs */
154       }
155       Dmsg1(110, "<filed: %s", fd->msg);
156       found = false;
157       for (i=0; fd_cmds[i].cmd; i++) {
158          if (strncmp(fd_cmds[i].cmd, fd->msg, strlen(fd_cmds[i].cmd)) == 0) {
159             found = true;               /* indicate command found */
160             jcr->errmsg[0] = 0;
161             if (!fd_cmds[i].func(jcr)) {    /* do command */
162                /* Note fd->msg command may be destroyed by comm activity */
163                if (!job_canceled(jcr)) {
164                   if (jcr->errmsg[0]) {
165                      Jmsg1(jcr, M_FATAL, 0, _("Command error with FD, hanging up. %s\n"),
166                            jcr->errmsg);
167                   } else {
168                      Jmsg0(jcr, M_FATAL, 0, _("Command error with FD, hanging up.\n"));
169                   }
170                   set_jcr_job_status(jcr, JS_ErrorTerminated);
171                }
172                quit = true;
173             }
174             break;
175          }
176       }
177       if (!found) {                   /* command not found */
178          if (!job_canceled(jcr)) {
179             Jmsg1(jcr, M_FATAL, 0, _("FD command not found: %s\n"), fd->msg);
180             Dmsg1(110, "<filed: Command not found: %s\n", fd->msg);
181          }
182          fd->fsend(ferrmsg);
183          break;
184       }
185    }
186    fd->signal(BNET_TERMINATE);        /* signal to FD job is done */
187 }
188
189 /*
190  *   Append Data command
191  *     Open Data Channel and receive Data for archiving
192  *     Write the Data to the archive device
193  */
194 static bool append_data_cmd(JCR *jcr)
195 {
196    BSOCK *fd = jcr->file_bsock;
197
198    Dmsg1(120, "Append data: %s", fd->msg);
199    if (jcr->session_opened) {
200       Dmsg1(110, "<bfiled: %s", fd->msg);
201       jcr->setJobType(JT_BACKUP);
202       if (do_append_data(jcr)) {
203          return true;
204       } else {
205          pm_strcpy(jcr->errmsg, _("Append data error.\n"));
206          bnet_suppress_error_messages(fd, 1); /* ignore errors at this point */
207          fd->fsend(ERROR_append);
208       }
209    } else {
210       pm_strcpy(jcr->errmsg, _("Attempt to append on non-open session.\n"));
211       fd->fsend(NOT_opened);
212    }
213    return false;
214 }
215
216 static bool append_end_session(JCR *jcr)
217 {
218    BSOCK *fd = jcr->file_bsock;
219
220    Dmsg1(120, "store<file: %s", fd->msg);
221    if (!jcr->session_opened) {
222       pm_strcpy(jcr->errmsg, _("Attempt to close non-open session.\n"));
223       fd->fsend(NOT_opened);
224       return false;
225    }
226    return fd->fsend(OK_end);
227 }
228
229
230 /*
231  * Append Open session command
232  *
233  */
234 static bool append_open_session(JCR *jcr)
235 {
236    BSOCK *fd = jcr->file_bsock;
237
238    Dmsg1(120, "Append open session: %s", fd->msg);
239    if (jcr->session_opened) {
240       pm_strcpy(jcr->errmsg, _("Attempt to open already open session.\n"));
241       fd->fsend(NO_open);
242       return false;
243    }
244
245    jcr->session_opened = true;
246
247    /* Send "Ticket" to File Daemon */
248    fd->fsend(OK_open, jcr->VolSessionId);
249    Dmsg1(110, ">filed: %s", fd->msg);
250
251    return true;
252 }
253
254 /*
255  *   Append Close session command
256  *      Close the append session and send back Statistics
257  *         (need to fix statistics)
258  */
259 static bool append_close_session(JCR *jcr)
260 {
261    BSOCK *fd = jcr->file_bsock;
262
263    Dmsg1(120, "<filed: %s", fd->msg);
264    if (!jcr->session_opened) {
265       pm_strcpy(jcr->errmsg, _("Attempt to close non-open session.\n"));
266       fd->fsend(NOT_opened);
267       return false;
268    }
269    /* Send final statistics to File daemon */
270    fd->fsend(OK_close, jcr->JobStatus);
271    Dmsg1(120, ">filed: %s", fd->msg);
272
273    fd->signal(BNET_EOD);              /* send EOD to File daemon */
274
275    jcr->session_opened = false;
276    return true;
277 }
278
279 /*
280  *   Read Data command
281  *     Open Data Channel, read the data from
282  *     the archive device and send to File
283  *     daemon.
284  */
285 static bool read_data_cmd(JCR *jcr)
286 {
287    BSOCK *fd = jcr->file_bsock;
288
289    Dmsg1(120, "Read data: %s", fd->msg);
290    if (jcr->session_opened) {
291       Dmsg1(120, "<bfiled: %s", fd->msg);
292       return do_read_data(jcr);
293    } else {
294       pm_strcpy(jcr->errmsg, _("Attempt to read on non-open session.\n"));
295       fd->fsend(NOT_opened);
296       return false;
297    }
298 }
299
300 /*
301  * Read Open session command
302  *
303  *  We need to scan for the parameters of the job
304  *    to be restored.
305  */
306 static bool read_open_session(JCR *jcr)
307 {
308    BSOCK *fd = jcr->file_bsock;
309
310    Dmsg1(120, "%s\n", fd->msg);
311    if (jcr->session_opened) {
312       pm_strcpy(jcr->errmsg, _("Attempt to open read on non-open session.\n"));
313       fd->fsend(NO_open);
314       return false;
315    }
316
317    if (sscanf(fd->msg, read_open, jcr->read_dcr->VolumeName, &jcr->read_VolSessionId,
318          &jcr->read_VolSessionTime, &jcr->read_StartFile, &jcr->read_EndFile,
319          &jcr->read_StartBlock, &jcr->read_EndBlock) == 7) {
320       if (jcr->session_opened) {
321          pm_strcpy(jcr->errmsg, _("Attempt to open read on non-open session.\n"));
322          fd->fsend(NOT_opened);
323          return false;
324       }
325       Dmsg4(100, "read_open_session got: JobId=%d Vol=%s VolSessId=%ld VolSessT=%ld\n",
326          jcr->JobId, jcr->read_dcr->VolumeName, jcr->read_VolSessionId,
327          jcr->read_VolSessionTime);
328       Dmsg4(100, "  StartF=%ld EndF=%ld StartB=%ld EndB=%ld\n",
329          jcr->read_StartFile, jcr->read_EndFile, jcr->read_StartBlock,
330          jcr->read_EndBlock);
331    }
332
333    jcr->session_opened = true;
334    jcr->setJobType(JT_RESTORE);
335
336    /* Send "Ticket" to File Daemon */
337    fd->fsend(OK_open, jcr->VolSessionId);
338    Dmsg1(110, ">filed: %s", fd->msg);
339
340    return true;
341 }
342
343 /*
344  *   Read Close session command
345  *      Close the read session
346  */
347 static bool read_close_session(JCR *jcr)
348 {
349    BSOCK *fd = jcr->file_bsock;
350
351    Dmsg1(120, "Read close session: %s\n", fd->msg);
352    if (!jcr->session_opened) {
353       fd->fsend(NOT_opened);
354       return false;
355    }
356    /* Send final close msg to File daemon */
357    fd->fsend(OK_close, jcr->JobStatus);
358    Dmsg1(160, ">filed: %s\n", fd->msg);
359
360    fd->signal(BNET_EOD);            /* send EOD to File daemon */
361
362    jcr->session_opened = false;
363    return true;
364 }