2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
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
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.
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
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.
29 * This file handles commands from the File daemon.
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.
45 /* Imported variables */
48 /* Static variables */
49 static char ferrmsg[] = "3900 Invalid command\n";
51 /* Imported functions */
52 extern bool do_append_data(JCR *jcr);
53 extern bool do_read_data(JCR *jcr);
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);
64 /* Exported function */
65 bool get_bootstrap_file(JCR *jcr, BSOCK *bs);
69 bool (*func)(JCR *jcr);
73 * The following are the recognized commands from the File daemon
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 */
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";
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";
97 /* Information sent to the Director */
98 static char Job_start[] = "3010 Job %s start\n";
100 "3099 Job %s end JobStatus=%d JobFiles=%d JobBytes=%s JobErrors=%u\n";
103 * Run a File daemon Job -- File daemon already authorized
104 * Director sends us this command.
106 * Basic task here is:
107 * - Read a command from the File daemon
111 void run_job(JCR *jcr)
113 BSOCK *dir = jcr->dir_bsock;
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 */
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 */
135 * Now talk to the FD and do what he says
137 void do_fd_commands(JCR *jcr)
141 BSOCK *fd = jcr->file_bsock;
144 for (quit=false; !quit;) {
147 /* Read command coming from the File daemon */
149 if (is_bnet_stop(fd)) { /* hardeof or error */
150 break; /* connection terminated */
153 continue; /* ignore signals and zero length msgs */
155 Dmsg1(110, "<filed: %s", fd->msg);
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 */
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"),
168 Jmsg0(jcr, M_FATAL, 0, _("Command error with FD, hanging up.\n"));
170 set_jcr_job_status(jcr, JS_ErrorTerminated);
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);
186 fd->signal(BNET_TERMINATE); /* signal to FD job is done */
190 * Append Data command
191 * Open Data Channel and receive Data for archiving
192 * Write the Data to the archive device
194 static bool append_data_cmd(JCR *jcr)
196 BSOCK *fd = jcr->file_bsock;
198 Dmsg1(120, "Append data: %s", fd->msg);
199 if (jcr->session_opened) {
200 Dmsg1(110, "<bfiled: %s", fd->msg);
201 jcr->set_JobType(JT_BACKUP);
202 if (do_append_data(jcr)) {
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);
210 pm_strcpy(jcr->errmsg, _("Attempt to append on non-open session.\n"));
211 fd->fsend(NOT_opened);
216 static bool append_end_session(JCR *jcr)
218 BSOCK *fd = jcr->file_bsock;
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);
226 return fd->fsend(OK_end);
231 * Append Open session command
234 static bool append_open_session(JCR *jcr)
236 BSOCK *fd = jcr->file_bsock;
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"));
245 jcr->session_opened = true;
247 /* Send "Ticket" to File Daemon */
248 fd->fsend(OK_open, jcr->VolSessionId);
249 Dmsg1(110, ">filed: %s", fd->msg);
255 * Append Close session command
256 * Close the append session and send back Statistics
257 * (need to fix statistics)
259 static bool append_close_session(JCR *jcr)
261 BSOCK *fd = jcr->file_bsock;
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);
269 /* Send final statistics to File daemon */
270 fd->fsend(OK_close, jcr->JobStatus);
271 Dmsg1(120, ">filed: %s", fd->msg);
273 fd->signal(BNET_EOD); /* send EOD to File daemon */
275 jcr->session_opened = false;
281 * Open Data Channel, read the data from
282 * the archive device and send to File
285 static bool read_data_cmd(JCR *jcr)
287 BSOCK *fd = jcr->file_bsock;
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);
294 pm_strcpy(jcr->errmsg, _("Attempt to read on non-open session.\n"));
295 fd->fsend(NOT_opened);
301 * Read Open session command
303 * We need to scan for the parameters of the job
306 static bool read_open_session(JCR *jcr)
308 BSOCK *fd = jcr->file_bsock;
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"));
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);
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,
333 jcr->session_opened = true;
334 jcr->set_JobType(JT_RESTORE);
336 /* Send "Ticket" to File Daemon */
337 fd->fsend(OK_open, jcr->VolSessionId);
338 Dmsg1(110, ">filed: %s", fd->msg);
344 * Read Close session command
345 * Close the read session
347 static bool read_close_session(JCR *jcr)
349 BSOCK *fd = jcr->file_bsock;
351 Dmsg1(120, "Read close session: %s\n", fd->msg);
352 if (!jcr->session_opened) {
353 fd->fsend(NOT_opened);
356 /* Send final close msg to File daemon */
357 fd->fsend(OK_close, jcr->JobStatus);
358 Dmsg1(160, ">filed: %s\n", fd->msg);
360 fd->signal(BNET_EOD); /* send EOD to File daemon */
362 jcr->session_opened = false;