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\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->use_win_backup_api = 0;
161 jcr->start_time = time(NULL);
162 jcr->last_fname = get_pool_memory(PM_FNAME);
163 jcr->last_fname[0] = 0;
164 jcr->client_name = get_memory(strlen(my_name) + 1);
165 pm_strcpy(&jcr->client_name, my_name);
166 dir->jcr = (void *)jcr;
168 /**********FIXME******* add command handler error code */
170 for (quit=0; !quit;) {
173 if (bnet_recv(dir) < 0) {
174 break; /* connection terminated */
176 dir->msg[dir->msglen] = 0;
177 Dmsg1(100, "<dird: %s", dir->msg);
179 for (i=0; cmds[i].cmd; i++) {
180 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
181 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
182 bnet_fsend(dir, no_auth);
185 found = TRUE; /* indicate command found */
186 if (!cmds[i].func(jcr)) { /* do command */
187 quit = TRUE; /* error or fully terminated, get out */
188 Pmsg0(20, "Command error\n");
193 if (!found) { /* command not found */
194 bnet_fsend(dir, errmsg);
199 Dmsg0(100, "Calling term_find_files\n");
200 term_find_files((FF_PKT *)jcr->ff);
201 Dmsg0(100, "Done with term_find_files\n");
202 free_jcr(jcr); /* destroy JCR record */
203 Dmsg0(100, "Done with free_jcr\n");
208 * Hello from Director he must identify himself and provide his
211 static int hello_cmd(JCR *jcr)
213 Dmsg0(120, "Calling Authenticate\n");
214 if (!authenticate_director(jcr)) {
217 Dmsg0(120, "OK Authenticate\n");
218 jcr->authenticated = TRUE;
225 static int cancel_cmd(JCR *jcr)
227 BSOCK *dir = jcr->dir_bsock;
228 char Job[MAX_NAME_LENGTH];
231 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
232 if (!(cjcr=get_jcr_by_full_name(Job))) {
233 bnet_fsend(dir, "2901 Job %s not found.\n", Job);
235 set_jcr_job_status(cjcr, JS_Canceled);
237 bnet_fsend(dir, "2001 Job %s marked to be canceled.\n", Job);
240 bnet_fsend(dir, "2902 Error scanning cancel command.\n");
242 bnet_sig(dir, BNET_EOD);
248 * Set debug level as requested by the Director
251 static int setdebug_cmd(JCR *jcr)
253 BSOCK *dir = jcr->dir_bsock;
256 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
257 if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
258 bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
262 return bnet_fsend(dir, OKsetdebug, level);
266 static int estimate_cmd(JCR *jcr)
268 BSOCK *dir = jcr->dir_bsock;
270 return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
274 * Get JobId and Storage Daemon Authorization key from Director
276 static int job_cmd(JCR *jcr)
278 BSOCK *dir = jcr->dir_bsock;
279 POOLMEM *sd_auth_key;
281 sd_auth_key = get_memory(dir->msglen);
282 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
283 &jcr->VolSessionId, &jcr->VolSessionTime,
285 bnet_fsend(dir, BADjob);
286 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
287 free_pool_memory(sd_auth_key);
290 jcr->sd_auth_key = bstrdup(sd_auth_key);
291 free_pool_memory(sd_auth_key);
292 if (jcr->use_win_backup_api) {
293 SetServicePrivileges(jcr);
295 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
296 return bnet_fsend(dir, OKjob);
302 static void add_fname_to_list(JCR *jcr, char *fname, int list)
305 if (list == INC_LIST) {
306 add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
308 /* Skip leading options -- currently ignored */
309 for (p=fname; *p && *p != ' '; p++)
312 for ( ; *p && *p == ' '; p++)
314 add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
320 * Get list of files/directories to include from Director
323 static int include_cmd(JCR *jcr)
325 BSOCK *dir = jcr->dir_bsock;
327 while (bnet_recv(dir) >= 0) {
328 dir->msg[dir->msglen] = 0;
329 strip_trailing_junk(dir->msg);
330 Dmsg1(010, "include file: %s\n", dir->msg);
331 add_fname_to_list(jcr, dir->msg, INC_LIST);
334 return bnet_fsend(dir, OKinc);
338 * Get list of files to exclude from Director
341 static int exclude_cmd(JCR *jcr)
343 BSOCK *dir = jcr->dir_bsock;
345 while (bnet_recv(dir) >= 0) {
346 dir->msg[dir->msglen] = 0;
347 strip_trailing_junk(dir->msg);
348 add_fname_to_list(jcr, dir->msg, EXC_LIST);
349 Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
352 return bnet_fsend(dir, OKexc);
356 static int bootstrap_cmd(JCR *jcr)
358 BSOCK *dir = jcr->dir_bsock;
359 POOLMEM *fname = get_pool_memory(PM_FNAME);
362 if (jcr->RestoreBootstrap) {
363 unlink(jcr->RestoreBootstrap);
364 free_pool_memory(jcr->RestoreBootstrap);
366 Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
368 Dmsg1(400, "bootstrap=%s\n", fname);
369 jcr->RestoreBootstrap = fname;
370 bs = fopen(fname, "a+"); /* create file */
372 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
373 jcr->RestoreBootstrap, strerror(errno));
374 free_pool_memory(jcr->RestoreBootstrap);
375 jcr->RestoreBootstrap = NULL;
376 set_jcr_job_status(jcr, JS_ErrorTerminated);
380 while (bnet_recv(dir) >= 0) {
381 Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
386 return bnet_fsend(dir, OKbootstrap);
391 * Get backup level from Director
394 static int level_cmd(JCR *jcr)
396 BSOCK *dir = jcr->dir_bsock;
401 level = get_memory(dir->msglen+1);
402 Dmsg1(110, "level_cmd: %s", dir->msg);
403 if (sscanf(dir->msg, "level = %s ", level) != 1) {
404 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
409 * Full backup requested
411 if (strcmp(level, "full") == 0) {
412 jcr->save_level = L_FULL;
414 * Backup requested since <date> <time>
415 * This form is also used for incremental and differential
417 } else if (strcmp(level, "since") == 0) {
418 jcr->save_level = L_SINCE;
419 if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d",
420 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
421 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
422 Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
428 tm.tm_wday = tm.tm_yday = 0;
431 Dmsg1(100, "Got since time: %s", ctime(&mtime));
432 jcr->incremental = 1;
435 Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
440 return bnet_fsend(dir, OKlevel);
444 * Get session parameters from Director -- this is for a Restore command
446 static int session_cmd(JCR *jcr)
448 BSOCK *dir = jcr->dir_bsock;
450 Dmsg1(100, "SessionCmd: %s", dir->msg);
451 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
452 &jcr->VolSessionId, &jcr->VolSessionTime,
453 &jcr->StartFile, &jcr->EndFile,
454 &jcr->StartBlock, &jcr->EndBlock) != 7) {
455 Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
459 return bnet_fsend(dir, OKsession);
463 * Get address of storage daemon from Director
466 static int storage_cmd(JCR *jcr)
468 int stored_port; /* storage daemon port */
469 BSOCK *dir = jcr->dir_bsock;
470 BSOCK *sd; /* storage daemon bsock */
472 Dmsg1(100, "StorageCmd: %s", dir->msg);
473 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port) != 2) {
474 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
477 Dmsg2(110, "Open storage: %s:%d\n", jcr->stored_addr, stored_port);
478 /* Open command communications with Storage daemon */
479 /* Try to connect for 1 hour at 10 second intervals */
480 sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"),
481 jcr->stored_addr, NULL, stored_port, 1);
483 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
484 jcr->stored_addr, stored_port);
488 jcr->store_bsock = sd;
490 bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
491 if (!authenticate_storagedaemon(jcr)) {
492 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
495 Dmsg0(110, "Authenticated with SD.\n");
497 /* Send OK to Director */
498 return bnet_fsend(dir, OKstore);
503 * Do a backup. For now, we handle only Full and Incremental.
505 static int backup_cmd(JCR *jcr)
507 BSOCK *dir = jcr->dir_bsock;
508 BSOCK *sd = jcr->store_bsock;
512 set_jcr_job_status(jcr, JS_Blocked);
513 jcr->JobType = JT_BACKUP;
514 Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
517 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
518 set_jcr_job_status(jcr, JS_ErrorTerminated);
522 bnet_fsend(dir, OKbackup);
523 Dmsg1(110, "bfiled>dird: %s", dir->msg);
526 * Send Append Open Session to Storage daemon
528 bnet_fsend(sd, append_open);
529 Dmsg1(110, ">stored: %s", sd->msg);
531 * Expect to receive back the Ticket number
533 if (bnet_recv(sd) >= 0) {
534 Dmsg1(110, "<stored: %s", sd->msg);
535 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
536 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
537 set_jcr_job_status(jcr, JS_ErrorTerminated);
540 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
542 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
543 set_jcr_job_status(jcr, JS_ErrorTerminated);
548 * Send Append data command to Storage daemon
550 bnet_fsend(sd, append_data, jcr->Ticket);
551 Dmsg1(110, ">stored: %s", sd->msg);
554 * Expect to get OK data
556 Dmsg1(110, "<stored: %s", sd->msg);
557 if (!response(jcr, sd, OK_data, "Append Data")) {
558 set_jcr_job_status(jcr, JS_ErrorTerminated);
563 * Send Files to Storage daemon
565 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
566 if (!blast_data_to_storage_daemon(jcr, NULL)) {
567 set_jcr_job_status(jcr, JS_ErrorTerminated);
568 bnet_suppress_error_messages(sd, 1);
570 set_jcr_job_status(jcr, JS_Terminated);
571 if (jcr->JobStatus != JS_Terminated) {
572 bnet_suppress_error_messages(sd, 1);
573 goto cleanup; /* bail out now */
576 * Expect to get response to append_data from Storage daemon
578 if (!response(jcr, sd, OK_append, "Append Data")) {
579 set_jcr_job_status(jcr, JS_ErrorTerminated);
584 * Send Append End Data to Storage daemon
586 bnet_fsend(sd, append_end, jcr->Ticket);
588 if (!response(jcr, sd, OK_end, "Append End")) {
589 set_jcr_job_status(jcr, JS_ErrorTerminated);
594 * Send Append Close to Storage daemon
596 bnet_fsend(sd, append_close, jcr->Ticket);
597 while (bnet_recv(sd) >= 0) { /* stop on signal or error */
598 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
600 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
604 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
605 set_jcr_job_status(jcr, JS_ErrorTerminated);
608 if (SDJobStatus != JS_Terminated) {
609 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
611 set_jcr_job_status(jcr, JS_ErrorTerminated);
617 /* Inform Storage daemon that we are done */
619 bnet_sig(sd, BNET_TERMINATE);
622 bnet_fsend(dir, EndBackup, jcr->JobStatus, jcr->JobFiles, jcr->ReadBytes, jcr->JobBytes);
624 /* Inform Director that we are done */
625 bnet_sig(dir, BNET_TERMINATE);
627 return 0; /* return and stop command loop */
631 * Do a Verify for Director
634 static int verify_cmd(JCR *jcr)
636 BSOCK *dir = jcr->dir_bsock;
637 BSOCK *sd = jcr->store_bsock;
640 jcr->JobType = JT_VERIFY;
641 if (sscanf(dir->msg, verifycmd, level) != 1) {
642 bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
645 if (strcasecmp(level, "init") == 0) {
646 jcr->JobLevel = L_VERIFY_INIT;
647 } else if (strcasecmp(level, "catalog") == 0){
648 jcr->JobLevel = L_VERIFY_CATALOG;
649 } else if (strcasecmp(level, "volume") == 0){
650 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
651 } else if (strcasecmp(level, "data") == 0){
652 jcr->JobLevel = L_VERIFY_DATA;
654 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
658 bnet_fsend(dir, OKverify);
659 Dmsg1(110, "bfiled>dird: %s", dir->msg);
661 switch (jcr->JobLevel) {
663 case L_VERIFY_CATALOG:
666 case L_VERIFY_VOLUME_TO_CATALOG:
667 if (!open_sd_read_session(jcr)) {
670 do_verify_volume(jcr);
672 * Send Close session command to Storage daemon
674 bnet_fsend(sd, read_close, jcr->Ticket);
675 Dmsg1(130, "bfiled>stored: %s", sd->msg);
677 /* ****FIXME**** check response */
678 bnet_recv(sd); /* get OK */
680 /* Inform Storage daemon that we are done */
681 bnet_sig(sd, BNET_TERMINATE);
685 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
689 /* Inform Director that we are done */
690 bnet_sig(dir, BNET_TERMINATE);
691 return 0; /* return and terminate command loop */
695 * Do a Restore for Director
698 static int restore_cmd(JCR *jcr)
700 BSOCK *dir = jcr->dir_bsock;
701 BSOCK *sd = jcr->store_bsock;
706 * Scan WHERE (base directory for restore) from command
708 Dmsg0(150, "restore command\n");
709 /* Pickup where string */
710 where = get_memory(dir->msglen+1);
713 if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
714 if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
715 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
720 /* Turn / into nothing */
721 if (where[0] == '/' && where[1] == 0) {
725 Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
726 unbash_spaces(where);
728 jcr->replace = replace;
730 bnet_fsend(dir, OKrestore);
731 Dmsg1(110, "bfiled>dird: %s", dir->msg);
733 jcr->JobType = JT_RESTORE;
735 set_jcr_job_status(jcr, JS_Blocked);
736 if (!open_sd_read_session(jcr)) {
737 set_jcr_job_status(jcr, JS_ErrorTerminated);
741 set_jcr_job_status(jcr, JS_Running);
744 * Do restore of files and data
748 set_jcr_job_status(jcr, JS_Terminated);
749 if (jcr->JobStatus != JS_Terminated) {
750 bnet_suppress_error_messages(sd, 1);
754 * Send Close session command to Storage daemon
756 bnet_fsend(sd, read_close, jcr->Ticket);
757 Dmsg1(130, "bfiled>stored: %s", sd->msg);
759 bnet_recv(sd); /* get OK */
761 /* Inform Storage daemon that we are done */
762 bnet_sig(sd, BNET_TERMINATE);
765 /* Send termination status back to Dir */
766 bnet_fsend(dir, EndRestore, jcr->JobStatus, jcr->num_files_examined,
769 /* Inform Director that we are done */
770 bnet_sig(dir, BNET_TERMINATE);
772 Dmsg0(130, "Done in job.c\n");
773 return 0; /* return and terminate command loop */
776 static int open_sd_read_session(JCR *jcr)
778 BSOCK *sd = jcr->store_bsock;
781 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
784 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
785 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
786 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
788 * Open Read Session with Storage daemon
790 bnet_fsend(sd, read_open, jcr->VolumeName,
791 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
792 jcr->StartBlock, jcr->EndBlock);
793 Dmsg1(110, ">stored: %s", sd->msg);
798 if (bnet_recv(sd) >= 0) {
799 Dmsg1(110, "bfiled<stored: %s", sd->msg);
800 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
801 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
804 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
806 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
810 if (!send_bootstrap_file(jcr)) {
815 * Start read of data with Storage daemon
817 bnet_fsend(sd, read_data, jcr->Ticket);
818 Dmsg1(110, ">stored: %s", sd->msg);
823 if (!response(jcr, sd, OK_data, "Read Data")) {
830 * Destroy the Job Control Record and associated
831 * resources (sockets).
833 static void filed_free_jcr(JCR *jcr)
835 if (jcr->store_bsock) {
836 bnet_close(jcr->store_bsock);
839 free_pool_memory(jcr->where);
841 if (jcr->RestoreBootstrap) {
842 unlink(jcr->RestoreBootstrap);
843 free_pool_memory(jcr->RestoreBootstrap);
845 if (jcr->last_fname) {
846 free_pool_memory(jcr->last_fname);
852 * Get response from Storage daemon to a command we
853 * sent. Check that the response is OK.
855 * Returns: 0 on failure
858 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
865 if ((n = bnet_recv(sd)) > 0) {
867 if (strcmp(sd->msg, resp) == 0) {
871 if (is_bnet_error(sd)) {
872 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
873 cmd, bnet_strerror(sd));
875 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
881 static int send_bootstrap_file(JCR *jcr)
885 BSOCK *sd = jcr->store_bsock;
886 char *bootstrap = "bootstrap\n";
888 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
889 if (!jcr->RestoreBootstrap) {
892 bs = fopen(jcr->RestoreBootstrap, "r");
894 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
895 jcr->RestoreBootstrap, strerror(errno));
896 set_jcr_job_status(jcr, JS_ErrorTerminated);
899 strcpy(sd->msg, bootstrap);
900 sd->msglen = strlen(sd->msg);
902 while (fgets(buf, sizeof(buf), bs)) {
903 sd->msglen = Mmsg(&sd->msg, "%s", buf);
906 bnet_sig(sd, BNET_EOD);
908 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
909 set_jcr_job_status(jcr, JS_ErrorTerminated);