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 EndJob[] = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s JobBytes=%s Errors=%u\n";
115 /* Responses received from Storage Daemon */
116 static char OK_end[] = "3000 OK end\n";
117 static char OK_close[] = "3000 OK close Status = %d\n";
118 static char OK_open[] = "3000 OK open ticket = %d\n";
119 static char OK_data[] = "3000 OK data\n";
120 static char OK_append[] = "3000 OK append data\n";
121 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
124 /* Commands sent to Storage Daemon */
125 static char append_open[] = "append open session\n";
126 static char append_data[] = "append data %d\n";
127 static char append_end[] = "append end session %d\n";
128 static char append_close[] = "append close session %d\n";
129 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
130 static char read_data[] = "read data %d\n";
131 static char read_close[] = "read close session %d\n";
134 * Accept requests from a Director
136 * NOTE! We are running as a separate thread
138 * Send output one line
139 * at a time followed by a zero length transmission.
141 * Return when the connection is terminated or there
144 * Basic task here is:
145 * Authenticate Director (during Hello command).
146 * Accept commands one at a time from the Director
150 void *handle_client_request(void *dirp)
154 BSOCK *dir = (BSOCK *)dirp;
156 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
157 jcr->dir_bsock = dir;
158 jcr->ff = init_find_files();
159 jcr->start_time = time(NULL);
160 jcr->last_fname = get_pool_memory(PM_FNAME);
161 jcr->last_fname[0] = 0;
162 jcr->client_name = get_memory(strlen(my_name) + 1);
163 pm_strcpy(&jcr->client_name, my_name);
165 enable_backup_privileges(NULL, 1 /* ignore_errors */);
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 Dmsg0(20, "Command error or Job done.\n");
192 if (!found) { /* command not found */
193 bnet_fsend(dir, errmsg);
199 /* Inform Storage daemon that we are done */
200 if (jcr->store_bsock) {
201 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
204 /* Inform Director that we are done */
205 bnet_sig(dir, BNET_TERMINATE);
207 Dmsg0(100, "Calling term_find_files\n");
208 term_find_files((FF_PKT *)jcr->ff);
209 Dmsg0(100, "Done with term_find_files\n");
210 free_jcr(jcr); /* destroy JCR record */
211 Dmsg0(100, "Done with free_jcr\n");
216 * Hello from Director he must identify himself and provide his
219 static int hello_cmd(JCR *jcr)
221 Dmsg0(120, "Calling Authenticate\n");
222 if (!authenticate_director(jcr)) {
225 Dmsg0(120, "OK Authenticate\n");
226 jcr->authenticated = TRUE;
233 static int cancel_cmd(JCR *jcr)
235 BSOCK *dir = jcr->dir_bsock;
236 char Job[MAX_NAME_LENGTH];
239 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
240 if (!(cjcr=get_jcr_by_full_name(Job))) {
241 bnet_fsend(dir, "2901 Job %s not found.\n", Job);
243 set_jcr_job_status(cjcr, JS_Canceled);
245 bnet_fsend(dir, "2001 Job %s marked to be canceled.\n", Job);
248 bnet_fsend(dir, "2902 Error scanning cancel command.\n");
250 bnet_sig(dir, BNET_EOD);
256 * Set debug level as requested by the Director
259 static int setdebug_cmd(JCR *jcr)
261 BSOCK *dir = jcr->dir_bsock;
264 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
265 if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
266 bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
270 return bnet_fsend(dir, OKsetdebug, level);
274 static int estimate_cmd(JCR *jcr)
276 BSOCK *dir = jcr->dir_bsock;
278 return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
282 * Get JobId and Storage Daemon Authorization key from Director
284 static int job_cmd(JCR *jcr)
286 BSOCK *dir = jcr->dir_bsock;
287 POOLMEM *sd_auth_key;
289 sd_auth_key = get_memory(dir->msglen);
290 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
291 &jcr->VolSessionId, &jcr->VolSessionTime,
293 bnet_fsend(dir, BADjob);
294 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
295 free_pool_memory(sd_auth_key);
298 jcr->sd_auth_key = bstrdup(sd_auth_key);
299 free_pool_memory(sd_auth_key);
300 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
301 return bnet_fsend(dir, OKjob);
307 static void add_fname_to_list(JCR *jcr, char *fname, int list)
310 if (list == INC_LIST) {
311 add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
313 /* Skip leading options -- currently ignored */
314 for (p=fname; *p && *p != ' '; p++)
317 for ( ; *p && *p == ' '; p++)
319 add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
325 * Get list of files/directories to include from Director
328 static int include_cmd(JCR *jcr)
330 BSOCK *dir = jcr->dir_bsock;
332 while (bnet_recv(dir) >= 0) {
333 dir->msg[dir->msglen] = 0;
334 strip_trailing_junk(dir->msg);
335 Dmsg1(010, "include file: %s\n", dir->msg);
336 add_fname_to_list(jcr, dir->msg, INC_LIST);
339 return bnet_fsend(dir, OKinc);
343 * Get list of files to exclude from Director
346 static int exclude_cmd(JCR *jcr)
348 BSOCK *dir = jcr->dir_bsock;
350 while (bnet_recv(dir) >= 0) {
351 dir->msg[dir->msglen] = 0;
352 strip_trailing_junk(dir->msg);
353 add_fname_to_list(jcr, dir->msg, EXC_LIST);
354 Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
357 return bnet_fsend(dir, OKexc);
361 static int bootstrap_cmd(JCR *jcr)
363 BSOCK *dir = jcr->dir_bsock;
364 POOLMEM *fname = get_pool_memory(PM_FNAME);
367 if (jcr->RestoreBootstrap) {
368 unlink(jcr->RestoreBootstrap);
369 free_pool_memory(jcr->RestoreBootstrap);
371 Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
373 Dmsg1(400, "bootstrap=%s\n", fname);
374 jcr->RestoreBootstrap = fname;
375 bs = fopen(fname, "a+"); /* create file */
378 * Suck up what he is sending to us so that he will then
379 * read our error message.
381 while (bnet_recv(dir) >= 0)
384 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
385 jcr->RestoreBootstrap, strerror(errno));
386 free_pool_memory(jcr->RestoreBootstrap);
387 jcr->RestoreBootstrap = NULL;
388 set_jcr_job_status(jcr, JS_ErrorTerminated);
392 while (bnet_recv(dir) >= 0) {
393 Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
398 return bnet_fsend(dir, OKbootstrap);
403 * Get backup level from Director
406 static int level_cmd(JCR *jcr)
408 BSOCK *dir = jcr->dir_bsock;
414 level = get_memory(dir->msglen+1);
415 Dmsg1(110, "level_cmd: %s", dir->msg);
416 if (sscanf(dir->msg, "level = %s ", level) != 1) {
417 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
421 /* Base backup requested? */
422 if (strcmp(level, "base") == 0) {
423 jcr->save_level = L_BASE;
424 /* Full backup requested? */
425 } else if (strcmp(level, "full") == 0) {
426 jcr->save_level = L_FULL;
428 * Backup requested since <date> <time>
429 * This form is also used for incremental and differential
431 } else if (strcmp(level, "since") == 0) {
432 jcr->save_level = L_SINCE;
433 if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d mtime_only=%d",
434 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
435 &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &mtime_only) != 7) {
436 Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
442 tm.tm_wday = tm.tm_yday = 0;
445 Dmsg2(100, "Got since time: %s mtime_only=%d\n", ctime(&mtime), mtime_only);
446 jcr->incremental = 1; /* set incremental or decremental backup */
447 jcr->mtime = mtime; /* set since time */
448 jcr->mtime_only = mtime_only; /* and what to compare */
450 Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
455 return bnet_fsend(dir, OKlevel);
459 * Get session parameters from Director -- this is for a Restore command
461 static int session_cmd(JCR *jcr)
463 BSOCK *dir = jcr->dir_bsock;
465 Dmsg1(100, "SessionCmd: %s", dir->msg);
466 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
467 &jcr->VolSessionId, &jcr->VolSessionTime,
468 &jcr->StartFile, &jcr->EndFile,
469 &jcr->StartBlock, &jcr->EndBlock) != 7) {
470 Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
474 return bnet_fsend(dir, OKsession);
478 * Get address of storage daemon from Director
481 static int storage_cmd(JCR *jcr)
483 int stored_port; /* storage daemon port */
484 int enable_ssl; /* enable ssl to sd */
485 BSOCK *dir = jcr->dir_bsock;
486 BSOCK *sd; /* storage daemon bsock */
488 Dmsg1(100, "StorageCmd: %s", dir->msg);
489 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
490 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
493 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
494 /* Open command communications with Storage daemon */
495 /* Try to connect for 1 hour at 10 second intervals */
496 sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"),
497 jcr->stored_addr, NULL, stored_port, 1);
499 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
500 jcr->stored_addr, stored_port);
504 jcr->store_bsock = sd;
506 bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
507 if (!authenticate_storagedaemon(jcr)) {
508 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
511 Dmsg0(110, "Authenticated with SD.\n");
513 /* Send OK to Director */
514 return bnet_fsend(dir, OKstore);
519 * Do a backup. For now, we handle only Full and Incremental.
521 static int backup_cmd(JCR *jcr)
523 BSOCK *dir = jcr->dir_bsock;
524 BSOCK *sd = jcr->store_bsock;
527 char ed1[50], ed2[50];
529 set_jcr_job_status(jcr, JS_Blocked);
530 jcr->JobType = JT_BACKUP;
531 Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
534 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
538 bnet_fsend(dir, OKbackup);
539 Dmsg1(110, "bfiled>dird: %s", dir->msg);
542 * Send Append Open Session to Storage daemon
544 bnet_fsend(sd, append_open);
545 Dmsg1(110, ">stored: %s", sd->msg);
547 * Expect to receive back the Ticket number
549 if (bget_msg(sd) >= 0) {
550 Dmsg1(110, "<stored: %s", sd->msg);
551 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
552 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
555 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
557 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
562 * Send Append data command to Storage daemon
564 bnet_fsend(sd, append_data, jcr->Ticket);
565 Dmsg1(110, ">stored: %s", sd->msg);
568 * Expect to get OK data
570 Dmsg1(110, "<stored: %s", sd->msg);
571 if (!response(jcr, sd, OK_data, "Append Data")) {
576 * Send Files to Storage daemon
578 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
579 if (!blast_data_to_storage_daemon(jcr, NULL)) {
580 set_jcr_job_status(jcr, JS_ErrorTerminated);
581 bnet_suppress_error_messages(sd, 1);
583 set_jcr_job_status(jcr, JS_Terminated);
584 if (jcr->JobStatus != JS_Terminated) {
585 bnet_suppress_error_messages(sd, 1);
586 goto cleanup; /* bail out now */
589 * Expect to get response to append_data from Storage daemon
591 if (!response(jcr, sd, OK_append, "Append Data")) {
592 set_jcr_job_status(jcr, JS_ErrorTerminated);
597 * Send Append End Data to Storage daemon
599 bnet_fsend(sd, append_end, jcr->Ticket);
601 if (!response(jcr, sd, OK_end, "Append End")) {
602 set_jcr_job_status(jcr, JS_ErrorTerminated);
607 * Send Append Close to Storage daemon
609 bnet_fsend(sd, append_close, jcr->Ticket);
610 while (bget_msg(sd) >= 0) { /* stop on signal or error */
611 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
613 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
617 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
620 if (SDJobStatus != JS_Terminated) {
621 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
628 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
629 edit_uint64(jcr->ReadBytes, ed1),
630 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
632 return 0; /* return and stop command loop */
636 * Do a Verify for Director
639 static int verify_cmd(JCR *jcr)
641 BSOCK *dir = jcr->dir_bsock;
642 BSOCK *sd = jcr->store_bsock;
643 char level[100], ed1[50], ed2[50];
645 jcr->JobType = JT_VERIFY;
646 if (sscanf(dir->msg, verifycmd, level) != 1) {
647 bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
650 if (strcasecmp(level, "init") == 0) {
651 jcr->JobLevel = L_VERIFY_INIT;
652 } else if (strcasecmp(level, "catalog") == 0){
653 jcr->JobLevel = L_VERIFY_CATALOG;
654 } else if (strcasecmp(level, "volume") == 0){
655 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
656 } else if (strcasecmp(level, "data") == 0){
657 jcr->JobLevel = L_VERIFY_DATA;
659 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
663 bnet_fsend(dir, OKverify);
664 Dmsg1(110, "bfiled>dird: %s", dir->msg);
666 switch (jcr->JobLevel) {
668 case L_VERIFY_CATALOG:
671 case L_VERIFY_VOLUME_TO_CATALOG:
672 if (!open_sd_read_session(jcr)) {
675 start_dir_heartbeat(jcr);
676 do_verify_volume(jcr);
677 stop_dir_heartbeat(jcr);
679 * Send Close session command to Storage daemon
681 bnet_fsend(sd, read_close, jcr->Ticket);
682 Dmsg1(130, "bfiled>stored: %s", sd->msg);
684 /* ****FIXME**** check response */
685 bget_msg(sd); /* get OK */
687 /* Inform Storage daemon that we are done */
688 bnet_sig(sd, BNET_TERMINATE);
692 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
696 bnet_sig(dir, BNET_EOD);
698 /* Send termination status back to Dir */
699 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
700 edit_uint64(jcr->ReadBytes, ed1),
701 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
703 /* Inform Director that we are done */
704 bnet_sig(dir, BNET_TERMINATE);
705 return 0; /* return and terminate command loop */
709 * Do a Restore for Director
712 static int restore_cmd(JCR *jcr)
714 BSOCK *dir = jcr->dir_bsock;
715 BSOCK *sd = jcr->store_bsock;
718 char ed1[50], ed2[50];
721 * Scan WHERE (base directory for restore) from command
723 Dmsg0(150, "restore command\n");
724 /* Pickup where string */
725 where = get_memory(dir->msglen+1);
728 if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
729 if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
730 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
735 /* Turn / into nothing */
736 if (where[0] == '/' && where[1] == 0) {
740 Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
741 unbash_spaces(where);
742 jcr->where = bstrdup(where);
743 free_pool_memory(where);
744 jcr->replace = replace;
746 bnet_fsend(dir, OKrestore);
747 Dmsg1(110, "bfiled>dird: %s", dir->msg);
749 jcr->JobType = JT_RESTORE;
751 set_jcr_job_status(jcr, JS_Blocked);
752 if (!open_sd_read_session(jcr)) {
753 set_jcr_job_status(jcr, JS_ErrorTerminated);
757 set_jcr_job_status(jcr, JS_Running);
760 * Do restore of files and data
762 start_dir_heartbeat(jcr);
764 stop_dir_heartbeat(jcr);
766 set_jcr_job_status(jcr, JS_Terminated);
767 if (jcr->JobStatus != JS_Terminated) {
768 bnet_suppress_error_messages(sd, 1);
772 * Send Close session command to Storage daemon
774 bnet_fsend(sd, read_close, jcr->Ticket);
775 Dmsg1(130, "bfiled>stored: %s", sd->msg);
777 bget_msg(sd); /* get OK */
779 /* Inform Storage daemon that we are done */
780 bnet_sig(sd, BNET_TERMINATE);
784 /* Send termination status back to Dir */
785 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
786 edit_uint64(jcr->ReadBytes, ed1),
787 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
789 /* Inform Director that we are done */
790 bnet_sig(dir, BNET_TERMINATE);
792 Dmsg0(130, "Done in job.c\n");
793 return 0; /* return and terminate command loop */
796 static int open_sd_read_session(JCR *jcr)
798 BSOCK *sd = jcr->store_bsock;
801 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
804 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
805 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
806 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
808 * Open Read Session with Storage daemon
810 bnet_fsend(sd, read_open, jcr->VolumeName,
811 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
812 jcr->StartBlock, jcr->EndBlock);
813 Dmsg1(110, ">stored: %s", sd->msg);
818 if (bget_msg(sd) >= 0) {
819 Dmsg1(110, "bfiled<stored: %s", sd->msg);
820 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
821 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
824 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
826 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
830 if (!send_bootstrap_file(jcr)) {
835 * Start read of data with Storage daemon
837 bnet_fsend(sd, read_data, jcr->Ticket);
838 Dmsg1(110, ">stored: %s", sd->msg);
843 if (!response(jcr, sd, OK_data, "Read Data")) {
850 * Destroy the Job Control Record and associated
851 * resources (sockets).
853 static void filed_free_jcr(JCR *jcr)
855 if (jcr->store_bsock) {
856 bnet_close(jcr->store_bsock);
858 if (jcr->RestoreBootstrap) {
859 unlink(jcr->RestoreBootstrap);
860 free_pool_memory(jcr->RestoreBootstrap);
862 if (jcr->last_fname) {
863 free_pool_memory(jcr->last_fname);
869 * Get response from Storage daemon to a command we
870 * sent. Check that the response is OK.
872 * Returns: 0 on failure
875 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
882 if ((n = bget_msg(sd)) > 0) {
884 if (strcmp(sd->msg, resp) == 0) {
888 if (is_bnet_error(sd)) {
889 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
890 cmd, bnet_strerror(sd));
892 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
898 static int send_bootstrap_file(JCR *jcr)
902 BSOCK *sd = jcr->store_bsock;
903 char *bootstrap = "bootstrap\n";
905 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
906 if (!jcr->RestoreBootstrap) {
909 bs = fopen(jcr->RestoreBootstrap, "r");
911 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
912 jcr->RestoreBootstrap, strerror(errno));
913 set_jcr_job_status(jcr, JS_ErrorTerminated);
916 strcpy(sd->msg, bootstrap);
917 sd->msglen = strlen(sd->msg);
919 while (fgets(buf, sizeof(buf), bs)) {
920 sd->msglen = Mmsg(&sd->msg, "%s", buf);
923 bnet_sig(sd, BNET_EOD);
925 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
926 set_jcr_job_status(jcr, JS_ErrorTerminated);