2 * Bacula File Daemon Job processing
4 * Kern Sibbald, October MM
10 Copyright (C) 2000-2003 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
33 extern char my_name[];
34 extern CLIENT *me; /* our client resource */
36 /* Imported functions */
37 extern int status_cmd(JCR *jcr);
39 /* Forward referenced functions */
40 static int backup_cmd(JCR *jcr);
41 static int bootstrap_cmd(JCR *jcr);
42 static int cancel_cmd(JCR *jcr);
43 static int setdebug_cmd(JCR *jcr);
44 static int estimate_cmd(JCR *jcr);
45 static int exclude_cmd(JCR *jcr);
46 static int hello_cmd(JCR *jcr);
47 static int job_cmd(JCR *jcr);
48 static int include_cmd(JCR *jcr);
49 static int level_cmd(JCR *jcr);
50 static int verify_cmd(JCR *jcr);
51 static int restore_cmd(JCR *jcr);
52 static int storage_cmd(JCR *jcr);
53 static int session_cmd(JCR *jcr);
54 static int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd);
55 static void filed_free_jcr(JCR *jcr);
56 static int open_sd_read_session(JCR *jcr);
57 static int send_bootstrap_file(JCR *jcr);
60 /* Exported functions */
68 * The following are the recognized commands from the Director.
70 static struct s_cmds cmds[] = {
71 {"backup", backup_cmd},
72 {"cancel", cancel_cmd},
73 {"setdebug=", setdebug_cmd},
74 {"estimate", estimate_cmd},
75 {"exclude", exclude_cmd},
77 {"include", include_cmd},
79 {"level = ", level_cmd},
80 {"restore", restore_cmd},
81 {"session", session_cmd},
82 {"status", status_cmd},
83 {"storage ", storage_cmd},
84 {"verify", verify_cmd},
85 {"bootstrap",bootstrap_cmd},
86 {NULL, NULL} /* list terminator */
89 /* Commands received from director that need scanning */
90 static char jobcmd[] = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
91 static char storaddr[] = "storage address=%s port=%d ssl=%d\n";
92 static char sessioncmd[] = "session %127s %ld %ld %ld %ld %ld %ld\n";
93 static char restorecmd[] = "restore replace=%c where=%s\n";
94 static char restorecmd1[] = "restore replace=%c where=\n";
95 static char verifycmd[] = "verify level=%30s\n";
97 /* Responses sent to Director */
98 static char errmsg[] = "2999 Invalid command\n";
99 static char no_auth[] = "2998 No Authorization\n";
100 static char OKinc[] = "2000 OK include\n";
101 static char OKest[] = "2000 OK estimate files=%ld bytes=%ld\n";
102 static char OKexc[] = "2000 OK exclude\n";
103 static char OKlevel[] = "2000 OK level\n";
104 static char OKbackup[] = "2000 OK backup\n";
105 static char OKbootstrap[] = "2000 OK bootstrap\n";
106 static char OKverify[] = "2000 OK verify\n";
107 static char OKrestore[] = "2000 OK restore\n";
108 static char OKsession[] = "2000 OK session\n";
109 static char OKstore[] = "2000 OK storage\n";
110 static char OKjob[] = "2000 OK Job " FDHOST "," DISTNAME "," DISTVER;
111 static char OKsetdebug[] = "2000 OK setdebug=%d\n";
112 static char BADjob[] = "2901 Bad Job\n";
113 static char EndRestore[] = "2800 End Job TermCode=%d JobFiles=%u JobBytes=%" lld "\n";
114 static char EndBackup[] = "2801 End Backup Job TermCode=%d JobFiles=%u ReadBytes=%" lld " JobBytes=%" lld "\n";
116 /* Responses received from Storage Daemon */
117 static char OK_end[] = "3000 OK end\n";
118 static char OK_close[] = "3000 OK close Status = %d\n";
119 static char OK_open[] = "3000 OK open ticket = %d\n";
120 static char OK_data[] = "3000 OK data\n";
121 static char OK_append[] = "3000 OK append data\n";
122 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
125 /* Commands sent to Storage Daemon */
126 static char append_open[] = "append open session\n";
127 static char append_data[] = "append data %d\n";
128 static char append_end[] = "append end session %d\n";
129 static char append_close[] = "append close session %d\n";
130 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
131 static char read_data[] = "read data %d\n";
132 static char read_close[] = "read close session %d\n";
135 * Accept requests from a Director
137 * NOTE! We are running as a separate thread
139 * Send output one line
140 * at a time followed by a zero length transmission.
142 * Return when the connection is terminated or there
145 * Basic task here is:
146 * Authenticate Director (during Hello command).
147 * Accept commands one at a time from the Director
151 void *handle_client_request(void *dirp)
155 BSOCK *dir = (BSOCK *)dirp;
157 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
158 jcr->dir_bsock = dir;
159 jcr->ff = init_find_files();
160 jcr->start_time = time(NULL);
161 jcr->last_fname = get_pool_memory(PM_FNAME);
162 jcr->last_fname[0] = 0;
163 jcr->client_name = get_memory(strlen(my_name) + 1);
164 pm_strcpy(&jcr->client_name, my_name);
165 dir->jcr = (void *)jcr;
167 /**********FIXME******* add command handler error code */
169 for (quit=0; !quit;) {
172 if (bnet_recv(dir) < 0) {
173 break; /* connection terminated */
175 dir->msg[dir->msglen] = 0;
176 Dmsg1(100, "<dird: %s", dir->msg);
178 for (i=0; cmds[i].cmd; i++) {
179 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
180 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
181 bnet_fsend(dir, no_auth);
184 found = TRUE; /* indicate command found */
185 if (!cmds[i].func(jcr)) { /* do command */
186 quit = TRUE; /* error or fully terminated, get out */
187 Pmsg0(20, "Command error or Job done.\n");
192 if (!found) { /* command not found */
193 bnet_fsend(dir, errmsg);
198 Dmsg0(100, "Calling term_find_files\n");
199 term_find_files((FF_PKT *)jcr->ff);
200 Dmsg0(100, "Done with term_find_files\n");
201 free_jcr(jcr); /* destroy JCR record */
202 Dmsg0(100, "Done with free_jcr\n");
207 * Hello from Director he must identify himself and provide his
210 static int hello_cmd(JCR *jcr)
212 Dmsg0(120, "Calling Authenticate\n");
213 if (!authenticate_director(jcr)) {
216 Dmsg0(120, "OK Authenticate\n");
217 jcr->authenticated = TRUE;
224 static int cancel_cmd(JCR *jcr)
226 BSOCK *dir = jcr->dir_bsock;
227 char Job[MAX_NAME_LENGTH];
230 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
231 if (!(cjcr=get_jcr_by_full_name(Job))) {
232 bnet_fsend(dir, "2901 Job %s not found.\n", Job);
234 set_jcr_job_status(cjcr, JS_Canceled);
236 bnet_fsend(dir, "2001 Job %s marked to be canceled.\n", Job);
239 bnet_fsend(dir, "2902 Error scanning cancel command.\n");
241 bnet_sig(dir, BNET_EOD);
247 * Set debug level as requested by the Director
250 static int setdebug_cmd(JCR *jcr)
252 BSOCK *dir = jcr->dir_bsock;
255 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
256 if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
257 bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
261 return bnet_fsend(dir, OKsetdebug, level);
265 static int estimate_cmd(JCR *jcr)
267 BSOCK *dir = jcr->dir_bsock;
269 return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
273 * Get JobId and Storage Daemon Authorization key from Director
275 static int job_cmd(JCR *jcr)
277 BSOCK *dir = jcr->dir_bsock;
278 POOLMEM *sd_auth_key;
280 sd_auth_key = get_memory(dir->msglen);
281 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
282 &jcr->VolSessionId, &jcr->VolSessionTime,
284 bnet_fsend(dir, BADjob);
285 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
286 free_pool_memory(sd_auth_key);
289 jcr->sd_auth_key = bstrdup(sd_auth_key);
290 free_pool_memory(sd_auth_key);
291 get_backup_privileges(jcr, 1 /* ignore_errors */);
292 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
293 return bnet_fsend(dir, OKjob);
299 static void add_fname_to_list(JCR *jcr, char *fname, int list)
302 if (list == INC_LIST) {
303 add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
305 /* Skip leading options -- currently ignored */
306 for (p=fname; *p && *p != ' '; p++)
309 for ( ; *p && *p == ' '; p++)
311 add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
317 * Get list of files/directories to include from Director
320 static int include_cmd(JCR *jcr)
322 BSOCK *dir = jcr->dir_bsock;
324 while (bnet_recv(dir) >= 0) {
325 dir->msg[dir->msglen] = 0;
326 strip_trailing_junk(dir->msg);
327 Dmsg1(010, "include file: %s\n", dir->msg);
328 add_fname_to_list(jcr, dir->msg, INC_LIST);
331 return bnet_fsend(dir, OKinc);
335 * Get list of files to exclude from Director
338 static int exclude_cmd(JCR *jcr)
340 BSOCK *dir = jcr->dir_bsock;
342 while (bnet_recv(dir) >= 0) {
343 dir->msg[dir->msglen] = 0;
344 strip_trailing_junk(dir->msg);
345 add_fname_to_list(jcr, dir->msg, EXC_LIST);
346 Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
349 return bnet_fsend(dir, OKexc);
353 static int bootstrap_cmd(JCR *jcr)
355 BSOCK *dir = jcr->dir_bsock;
356 POOLMEM *fname = get_pool_memory(PM_FNAME);
359 if (jcr->RestoreBootstrap) {
360 unlink(jcr->RestoreBootstrap);
361 free_pool_memory(jcr->RestoreBootstrap);
363 Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
365 Dmsg1(400, "bootstrap=%s\n", fname);
366 jcr->RestoreBootstrap = fname;
367 bs = fopen(fname, "a+"); /* create file */
369 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
370 jcr->RestoreBootstrap, strerror(errno));
371 free_pool_memory(jcr->RestoreBootstrap);
372 jcr->RestoreBootstrap = NULL;
373 set_jcr_job_status(jcr, JS_ErrorTerminated);
377 while (bnet_recv(dir) >= 0) {
378 Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
383 return bnet_fsend(dir, OKbootstrap);
388 * Get backup level from Director
391 static int level_cmd(JCR *jcr)
393 BSOCK *dir = jcr->dir_bsock;
398 level = get_memory(dir->msglen+1);
399 Dmsg1(110, "level_cmd: %s", dir->msg);
400 if (sscanf(dir->msg, "level = %s ", level) != 1) {
401 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
405 /* Base backup requested? */
406 if (strcmp(level, "base") == 0) {
407 jcr->save_level = L_BASE;
408 /* Full backup requested? */
409 } else if (strcmp(level, "full") == 0) {
410 jcr->save_level = L_FULL;
412 * Backup requested since <date> <time>
413 * This form is also used for incremental and differential
415 } else if (strcmp(level, "since") == 0) {
416 jcr->save_level = L_SINCE;
417 if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d",
418 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
419 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
420 Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
426 tm.tm_wday = tm.tm_yday = 0;
429 Dmsg1(100, "Got since time: %s", ctime(&mtime));
430 jcr->incremental = 1; /* set incremental or decremental backup */
431 jcr->mtime = mtime; /* set since time */
433 Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
438 return bnet_fsend(dir, OKlevel);
442 * Get session parameters from Director -- this is for a Restore command
444 static int session_cmd(JCR *jcr)
446 BSOCK *dir = jcr->dir_bsock;
448 Dmsg1(100, "SessionCmd: %s", dir->msg);
449 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
450 &jcr->VolSessionId, &jcr->VolSessionTime,
451 &jcr->StartFile, &jcr->EndFile,
452 &jcr->StartBlock, &jcr->EndBlock) != 7) {
453 Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
457 return bnet_fsend(dir, OKsession);
461 * Get address of storage daemon from Director
464 static int storage_cmd(JCR *jcr)
466 int stored_port; /* storage daemon port */
467 int enable_ssl; /* enable ssl to sd */
468 BSOCK *dir = jcr->dir_bsock;
469 BSOCK *sd; /* storage daemon bsock */
471 Dmsg1(100, "StorageCmd: %s", dir->msg);
472 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
473 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
476 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
477 /* Open command communications with Storage daemon */
478 /* Try to connect for 1 hour at 10 second intervals */
479 sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"),
480 jcr->stored_addr, NULL, stored_port, 1);
482 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
483 jcr->stored_addr, stored_port);
487 jcr->store_bsock = sd;
489 bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
490 if (!authenticate_storagedaemon(jcr)) {
491 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
494 Dmsg0(110, "Authenticated with SD.\n");
496 /* Send OK to Director */
497 return bnet_fsend(dir, OKstore);
502 * Do a backup. For now, we handle only Full and Incremental.
504 static int backup_cmd(JCR *jcr)
506 BSOCK *dir = jcr->dir_bsock;
507 BSOCK *sd = jcr->store_bsock;
511 set_jcr_job_status(jcr, JS_Blocked);
512 jcr->JobType = JT_BACKUP;
513 Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
516 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
517 set_jcr_job_status(jcr, JS_ErrorTerminated);
521 bnet_fsend(dir, OKbackup);
522 Dmsg1(110, "bfiled>dird: %s", dir->msg);
525 * Send Append Open Session to Storage daemon
527 bnet_fsend(sd, append_open);
528 Dmsg1(110, ">stored: %s", sd->msg);
530 * Expect to receive back the Ticket number
532 if (bget_msg(sd) >= 0) {
533 Dmsg1(110, "<stored: %s", sd->msg);
534 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
535 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
536 set_jcr_job_status(jcr, JS_ErrorTerminated);
539 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
541 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
542 set_jcr_job_status(jcr, JS_ErrorTerminated);
547 * Send Append data command to Storage daemon
549 bnet_fsend(sd, append_data, jcr->Ticket);
550 Dmsg1(110, ">stored: %s", sd->msg);
553 * Expect to get OK data
555 Dmsg1(110, "<stored: %s", sd->msg);
556 if (!response(jcr, sd, OK_data, "Append Data")) {
557 set_jcr_job_status(jcr, JS_ErrorTerminated);
562 * Send Files to Storage daemon
564 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
565 if (!blast_data_to_storage_daemon(jcr, NULL)) {
566 set_jcr_job_status(jcr, JS_ErrorTerminated);
567 bnet_suppress_error_messages(sd, 1);
569 set_jcr_job_status(jcr, JS_Terminated);
570 if (jcr->JobStatus != JS_Terminated) {
571 bnet_suppress_error_messages(sd, 1);
572 goto cleanup; /* bail out now */
575 * Expect to get response to append_data from Storage daemon
577 if (!response(jcr, sd, OK_append, "Append Data")) {
578 set_jcr_job_status(jcr, JS_ErrorTerminated);
583 * Send Append End Data to Storage daemon
585 bnet_fsend(sd, append_end, jcr->Ticket);
587 if (!response(jcr, sd, OK_end, "Append End")) {
588 set_jcr_job_status(jcr, JS_ErrorTerminated);
593 * Send Append Close to Storage daemon
595 bnet_fsend(sd, append_close, jcr->Ticket);
596 while (bget_msg(sd) >= 0) { /* stop on signal or error */
597 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
599 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
603 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
604 set_jcr_job_status(jcr, JS_ErrorTerminated);
607 if (SDJobStatus != JS_Terminated) {
608 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
610 set_jcr_job_status(jcr, JS_ErrorTerminated);
616 /* Inform Storage daemon that we are done */
618 bnet_sig(sd, BNET_TERMINATE);
621 bnet_fsend(dir, EndBackup, jcr->JobStatus, jcr->JobFiles, jcr->ReadBytes, jcr->JobBytes);
623 /* Inform Director that we are done */
624 bnet_sig(dir, BNET_TERMINATE);
626 return 0; /* return and stop command loop */
630 * Do a Verify for Director
633 static int verify_cmd(JCR *jcr)
635 BSOCK *dir = jcr->dir_bsock;
636 BSOCK *sd = jcr->store_bsock;
639 jcr->JobType = JT_VERIFY;
640 if (sscanf(dir->msg, verifycmd, level) != 1) {
641 bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
644 if (strcasecmp(level, "init") == 0) {
645 jcr->JobLevel = L_VERIFY_INIT;
646 } else if (strcasecmp(level, "catalog") == 0){
647 jcr->JobLevel = L_VERIFY_CATALOG;
648 } else if (strcasecmp(level, "volume") == 0){
649 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
650 } else if (strcasecmp(level, "data") == 0){
651 jcr->JobLevel = L_VERIFY_DATA;
653 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
657 bnet_fsend(dir, OKverify);
658 Dmsg1(110, "bfiled>dird: %s", dir->msg);
660 switch (jcr->JobLevel) {
662 case L_VERIFY_CATALOG:
665 case L_VERIFY_VOLUME_TO_CATALOG:
666 if (!open_sd_read_session(jcr)) {
669 start_dir_heartbeat(jcr);
670 do_verify_volume(jcr);
671 stop_dir_heartbeat(jcr);
673 * Send Close session command to Storage daemon
675 bnet_fsend(sd, read_close, jcr->Ticket);
676 Dmsg1(130, "bfiled>stored: %s", sd->msg);
678 /* ****FIXME**** check response */
679 bget_msg(sd); /* get OK */
681 /* Inform Storage daemon that we are done */
682 bnet_sig(sd, BNET_TERMINATE);
686 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
690 /* Inform Director that we are done */
691 bnet_sig(dir, BNET_TERMINATE);
692 return 0; /* return and terminate command loop */
696 * Do a Restore for Director
699 static int restore_cmd(JCR *jcr)
701 BSOCK *dir = jcr->dir_bsock;
702 BSOCK *sd = jcr->store_bsock;
707 * Scan WHERE (base directory for restore) from command
709 Dmsg0(150, "restore command\n");
710 /* Pickup where string */
711 where = get_memory(dir->msglen+1);
714 if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
715 if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
716 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
721 /* Turn / into nothing */
722 if (where[0] == '/' && where[1] == 0) {
726 Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
727 unbash_spaces(where);
729 jcr->replace = replace;
731 bnet_fsend(dir, OKrestore);
732 Dmsg1(110, "bfiled>dird: %s", dir->msg);
734 jcr->JobType = JT_RESTORE;
736 set_jcr_job_status(jcr, JS_Blocked);
737 if (!open_sd_read_session(jcr)) {
738 set_jcr_job_status(jcr, JS_ErrorTerminated);
742 set_jcr_job_status(jcr, JS_Running);
745 * Do restore of files and data
747 start_dir_heartbeat(jcr);
749 stop_dir_heartbeat(jcr);
751 set_jcr_job_status(jcr, JS_Terminated);
752 if (jcr->JobStatus != JS_Terminated) {
753 bnet_suppress_error_messages(sd, 1);
757 * Send Close session command to Storage daemon
759 bnet_fsend(sd, read_close, jcr->Ticket);
760 Dmsg1(130, "bfiled>stored: %s", sd->msg);
762 bget_msg(sd); /* get OK */
764 /* Inform Storage daemon that we are done */
765 bnet_sig(sd, BNET_TERMINATE);
768 /* Send termination status back to Dir */
769 bnet_fsend(dir, EndRestore, jcr->JobStatus, jcr->num_files_examined,
772 /* Inform Director that we are done */
773 bnet_sig(dir, BNET_TERMINATE);
775 Dmsg0(130, "Done in job.c\n");
776 return 0; /* return and terminate command loop */
779 static int open_sd_read_session(JCR *jcr)
781 BSOCK *sd = jcr->store_bsock;
784 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
787 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
788 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
789 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
791 * Open Read Session with Storage daemon
793 bnet_fsend(sd, read_open, jcr->VolumeName,
794 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
795 jcr->StartBlock, jcr->EndBlock);
796 Dmsg1(110, ">stored: %s", sd->msg);
801 if (bget_msg(sd) >= 0) {
802 Dmsg1(110, "bfiled<stored: %s", sd->msg);
803 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
804 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
807 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
809 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
813 if (!send_bootstrap_file(jcr)) {
818 * Start read of data with Storage daemon
820 bnet_fsend(sd, read_data, jcr->Ticket);
821 Dmsg1(110, ">stored: %s", sd->msg);
826 if (!response(jcr, sd, OK_data, "Read Data")) {
833 * Destroy the Job Control Record and associated
834 * resources (sockets).
836 static void filed_free_jcr(JCR *jcr)
838 if (jcr->store_bsock) {
839 bnet_close(jcr->store_bsock);
842 free_pool_memory(jcr->where);
844 if (jcr->RestoreBootstrap) {
845 unlink(jcr->RestoreBootstrap);
846 free_pool_memory(jcr->RestoreBootstrap);
848 if (jcr->last_fname) {
849 free_pool_memory(jcr->last_fname);
855 * Get response from Storage daemon to a command we
856 * sent. Check that the response is OK.
858 * Returns: 0 on failure
861 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
868 if ((n = bget_msg(sd)) > 0) {
870 if (strcmp(sd->msg, resp) == 0) {
874 if (is_bnet_error(sd)) {
875 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
876 cmd, bnet_strerror(sd));
878 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
884 static int send_bootstrap_file(JCR *jcr)
888 BSOCK *sd = jcr->store_bsock;
889 char *bootstrap = "bootstrap\n";
891 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
892 if (!jcr->RestoreBootstrap) {
895 bs = fopen(jcr->RestoreBootstrap, "r");
897 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
898 jcr->RestoreBootstrap, strerror(errno));
899 set_jcr_job_status(jcr, JS_ErrorTerminated);
902 strcpy(sd->msg, bootstrap);
903 sd->msglen = strlen(sd->msg);
905 while (fgets(buf, sizeof(buf), bs)) {
906 sd->msglen = Mmsg(&sd->msg, "%s", buf);
909 bnet_sig(sd, BNET_EOD);
911 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
912 set_jcr_job_status(jcr, JS_ErrorTerminated);