2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 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 two of the GNU 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 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 John Walker.
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.
46 /* Imported variables */
49 /* Static variables */
50 static char ferrmsg[] = "3900 Invalid command\n";
52 /* Imported functions */
53 extern bool do_append_data(JCR *jcr);
54 extern bool do_read_data(JCR *jcr);
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);
66 /* Exported function */
67 bool get_bootstrap_file(JCR *jcr, BSOCK *bs);
71 bool (*func)(JCR *jcr);
75 * The following are the recognized commands from the File daemon
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 */
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";
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";
102 /* Information sent to the Director */
103 static char Job_start[] = "3010 Job %s start\n";
105 "3099 Job %s end JobStatus=%d JobFiles=%d JobBytes=%s\n";
108 * Run a File daemon Job -- File daemon already authorized
109 * Director sends us this command.
111 * Basic task here is:
112 * - Read a command from the File daemon
116 void run_job(JCR *jcr)
118 BSOCK *dir = jcr->dir_bsock;
122 Dmsg1(120, "Start run Job=%s\n", jcr->Job);
123 dir->fsend(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 */
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 dir->fsend(Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
134 edit_uint64(jcr->JobBytes, ec1));
135 dir->signal(BNET_EOD); /* send EOD to Director daemon */
140 * Now talk to the FD and do what he says
142 void do_fd_commands(JCR *jcr)
146 BSOCK *fd = jcr->file_bsock;
149 for (quit=false; !quit;) {
152 /* Read command coming from the File daemon */
154 if (is_bnet_stop(fd)) { /* hardeof or error */
155 break; /* connection terminated */
158 continue; /* ignore signals and zero length msgs */
160 Dmsg1(110, "<filed: %s", fd->msg);
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);
172 if (!found) { /* command not found */
173 Dmsg1(110, "<filed: Command not found: %s\n", fd->msg);
178 fd->signal(BNET_TERMINATE); /* signal to FD job is done */
182 * Append Data command
183 * Open Data Channel and receive Data for archiving
184 * Write the Data to the archive device
186 static bool append_data_cmd(JCR *jcr)
188 BSOCK *fd = jcr->file_bsock;
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)) {
197 bnet_suppress_error_messages(fd, 1); /* ignore errors at this point */
198 fd->fsend(ERROR_append);
201 fd->fsend(NOT_opened);
206 static bool append_end_session(JCR *jcr)
208 BSOCK *fd = jcr->file_bsock;
210 Dmsg1(120, "store<file: %s", fd->msg);
211 if (!jcr->session_opened) {
212 fd->fsend(NOT_opened);
215 set_jcr_job_status(jcr, JS_Terminated);
216 return fd->fsend(OK_end);
221 * Append Open session command
224 static bool append_open_session(JCR *jcr)
226 BSOCK *fd = jcr->file_bsock;
228 Dmsg1(120, "Append open session: %s", fd->msg);
229 if (jcr->session_opened) {
234 jcr->session_opened = true;
236 /* Send "Ticket" to File Daemon */
237 fd->fsend(OK_open, jcr->VolSessionId);
238 Dmsg1(110, ">filed: %s", fd->msg);
244 * Append Close session command
245 * Close the append session and send back Statistics
246 * (need to fix statistics)
248 static bool append_close_session(JCR *jcr)
250 BSOCK *fd = jcr->file_bsock;
252 Dmsg1(120, "<filed: %s", fd->msg);
253 if (!jcr->session_opened) {
254 fd->fsend(NOT_opened);
257 /* Send final statistics to File daemon */
258 fd->fsend(OK_close, jcr->JobStatus);
259 Dmsg1(120, ">filed: %s", fd->msg);
261 fd->signal(BNET_EOD); /* send EOD to File daemon */
263 jcr->session_opened = false;
269 * Open Data Channel, read the data from
270 * the archive device and send to File
273 static bool read_data_cmd(JCR *jcr)
275 BSOCK *fd = jcr->file_bsock;
277 Dmsg1(120, "Read data: %s", fd->msg);
278 if (jcr->session_opened) {
279 Dmsg1(120, "<bfiled: %s", fd->msg);
280 return do_read_data(jcr);
282 fd->fsend(NOT_opened);
288 * Read Open session command
290 * We need to scan for the parameters of the job
293 static bool read_open_session(JCR *jcr)
295 BSOCK *fd = jcr->file_bsock;
297 Dmsg1(120, "%s\n", fd->msg);
298 if (jcr->session_opened) {
303 if (sscanf(fd->msg, read_open, jcr->read_dcr->VolumeName, &jcr->read_VolSessionId,
304 &jcr->read_VolSessionTime, &jcr->read_StartFile, &jcr->read_EndFile,
305 &jcr->read_StartBlock, &jcr->read_EndBlock) == 7) {
306 if (jcr->session_opened) {
307 fd->fsend(NOT_opened);
310 Dmsg4(100, "read_open_session got: JobId=%d Vol=%s VolSessId=%ld VolSessT=%ld\n",
311 jcr->JobId, jcr->read_dcr->VolumeName, jcr->read_VolSessionId,
312 jcr->read_VolSessionTime);
313 Dmsg4(100, " StartF=%ld EndF=%ld StartB=%ld EndB=%ld\n",
314 jcr->read_StartFile, jcr->read_EndFile, jcr->read_StartBlock,
318 jcr->session_opened = true;
319 jcr->JobType = JT_RESTORE;
321 /* Send "Ticket" to File Daemon */
322 fd->fsend(OK_open, jcr->VolSessionId);
323 Dmsg1(110, ">filed: %s", fd->msg);
328 static bool bootstrap_cmd(JCR *jcr)
330 return get_bootstrap_file(jcr, jcr->file_bsock);
333 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
334 static uint32_t bsr_uniq = 0;
336 bool get_bootstrap_file(JCR *jcr, BSOCK *sock)
338 POOLMEM *fname = get_pool_memory(PM_FNAME);
342 if (jcr->RestoreBootstrap) {
343 unlink(jcr->RestoreBootstrap);
344 free_pool_memory(jcr->RestoreBootstrap);
348 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
351 Dmsg1(400, "bootstrap=%s\n", fname);
352 jcr->RestoreBootstrap = fname;
353 bs = fopen(fname, "a+b"); /* create file */
356 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
357 jcr->RestoreBootstrap, be.bstrerror());
360 Dmsg0(10, "=== Bootstrap file ===\n");
361 while (sock->recv() >= 0) {
362 Dmsg1(10, "%s", sock->msg);
363 fputs(sock->msg, bs);
366 Dmsg0(10, "=== end bootstrap file ===\n");
367 jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
369 Jmsg(jcr, M_FATAL, 0, _("Error parsing bootstrap file.\n"));
372 if (debug_level >= 10) {
373 dump_bsr(jcr->bsr, true);
378 unlink(jcr->RestoreBootstrap);
379 free_pool_memory(jcr->RestoreBootstrap);
380 jcr->RestoreBootstrap = NULL;
382 sock->fsend(ERROR_bootstrap);
385 return sock->fsend(OK_bootstrap);
390 * Read Close session command
391 * Close the read session
393 static bool read_close_session(JCR *jcr)
395 BSOCK *fd = jcr->file_bsock;
397 Dmsg1(120, "Read close session: %s\n", fd->msg);
398 if (!jcr->session_opened) {
399 fd->fsend(NOT_opened);
402 /* Send final close msg to File daemon */
403 fd->fsend(OK_close, jcr->JobStatus);
404 Dmsg1(160, ">filed: %s\n", fd->msg);
406 fd->signal(BNET_EOD); /* send EOD to File daemon */
408 jcr->session_opened = false;