2 * Bacula File Daemon Job processing
4 * Kern Sibbald, October MM
10 Copyright (C) 2000-2005 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License
14 version 2 as ammended with additional clauses defined in the
15 file LICENSE in the main source directory.
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
20 the file LICENSE for additional details.
31 extern char my_name[];
32 extern CLIENT *me; /* our client resource */
34 int enable_vss = 0; /* set to use vss */
36 /* Imported functions */
37 extern int status_cmd(JCR *jcr);
38 extern int qstatus_cmd(JCR *jcr);
40 /* Forward referenced functions */
41 static int backup_cmd(JCR *jcr);
42 static int bootstrap_cmd(JCR *jcr);
43 static int cancel_cmd(JCR *jcr);
44 static int setdebug_cmd(JCR *jcr);
45 static int estimate_cmd(JCR *jcr);
46 static int hello_cmd(JCR *jcr);
47 static int job_cmd(JCR *jcr);
48 static int fileset_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, const 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);
58 static int runbefore_cmd(JCR *jcr);
59 static int runafter_cmd(JCR *jcr);
60 static bool run_cmd(JCR *jcr, char *cmd, const char *name);
61 static void set_options(findFOPTS *fo, const char *opts);
64 /* Exported functions */
69 int monitoraccess; /* specify if monitors have access to this function */
73 * The following are the recognized commands from the Director.
75 static struct s_cmds cmds[] = {
76 {"backup", backup_cmd, 0},
77 {"cancel", cancel_cmd, 0},
78 {"setdebug=", setdebug_cmd, 0},
79 {"estimate", estimate_cmd, 0},
80 {"Hello", hello_cmd, 1},
81 {"fileset", fileset_cmd, 0},
82 {"JobId=", job_cmd, 0},
83 {"level = ", level_cmd, 0},
84 {"restore", restore_cmd, 0},
85 {"session", session_cmd, 0},
86 {"status", status_cmd, 1},
87 {".status", qstatus_cmd, 1},
88 {"storage ", storage_cmd, 0},
89 {"verify", verify_cmd, 0},
90 {"bootstrap", bootstrap_cmd, 0},
91 {"RunBeforeJob", runbefore_cmd, 0},
92 {"RunAfterJob", runafter_cmd, 0},
93 {NULL, NULL} /* list terminator */
96 /* Commands received from director that need scanning */
97 static char jobcmd[] = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
98 static char storaddr[] = "storage address=%s port=%d ssl=%d\n";
99 static char sessioncmd[] = "session %127s %ld %ld %ld %ld %ld %ld\n";
100 static char restorecmd[] = "restore replace=%c prelinks=%d where=%s\n";
101 static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
102 static char verifycmd[] = "verify level=%30s\n";
103 static char estimatecmd[] = "estimate listing=%d\n";
104 static char runbefore[] = "RunBeforeJob %s\n";
105 static char runafter[] = "RunAfterJob %s\n";
107 /* Responses sent to Director */
108 static char errmsg[] = "2999 Invalid command\n";
109 static char no_auth[] = "2998 No Authorization\n";
110 static char illegal_cmd[] = "2997 Illegal command for a Director with Monitor directive enabled\n";
111 static char OKinc[] = "2000 OK include\n";
112 static char OKest[] = "2000 OK estimate files=%u bytes=%s\n";
113 static char OKlevel[] = "2000 OK level\n";
114 static char OKbackup[] = "2000 OK backup\n";
115 static char OKbootstrap[] = "2000 OK bootstrap\n";
116 static char OKverify[] = "2000 OK verify\n";
117 static char OKrestore[] = "2000 OK restore\n";
118 static char OKsession[] = "2000 OK session\n";
119 static char OKstore[] = "2000 OK storage\n";
120 static char OKjob[] = "2000 OK Job %s,%s,%s";
121 static char OKsetdebug[] = "2000 OK setdebug=%d\n";
122 static char BADjob[] = "2901 Bad Job\n";
123 static char EndJob[] = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s JobBytes=%s Errors=%u\n";
124 static char OKRunBefore[] = "2000 OK RunBefore\n";
125 static char OKRunAfter[] = "2000 OK RunAfter\n";
127 /* Responses received from Storage Daemon */
128 static char OK_end[] = "3000 OK end\n";
129 static char OK_close[] = "3000 OK close Status = %d\n";
130 static char OK_open[] = "3000 OK open ticket = %d\n";
131 static char OK_data[] = "3000 OK data\n";
132 static char OK_append[] = "3000 OK append data\n";
133 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
136 /* Commands sent to Storage Daemon */
137 static char append_open[] = "append open session\n";
138 static char append_data[] = "append data %d\n";
139 static char append_end[] = "append end session %d\n";
140 static char append_close[] = "append close session %d\n";
141 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
142 static char read_data[] = "read data %d\n";
143 static char read_close[] = "read close session %d\n";
146 * Accept requests from a Director
148 * NOTE! We are running as a separate thread
150 * Send output one line
151 * at a time followed by a zero length transmission.
153 * Return when the connection is terminated or there
156 * Basic task here is:
157 * Authenticate Director (during Hello command).
158 * Accept commands one at a time from the Director
162 void *handle_client_request(void *dirp)
167 BSOCK *dir = (BSOCK *)dirp;
169 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
170 jcr->dir_bsock = dir;
171 jcr->ff = init_find_files();
172 jcr->start_time = time(NULL);
173 jcr->last_fname = get_pool_memory(PM_FNAME);
174 jcr->last_fname[0] = 0;
175 jcr->client_name = get_memory(strlen(my_name) + 1);
176 pm_strcpy(jcr->client_name, my_name);
178 enable_backup_privileges(NULL, 1 /* ignore_errors */);
180 /**********FIXME******* add command handler error code */
182 for (quit=false; !quit;) {
185 if (bnet_recv(dir) < 0) {
186 break; /* connection terminated */
188 dir->msg[dir->msglen] = 0;
189 Dmsg1(100, "<dird: %s", dir->msg);
191 for (i=0; cmds[i].cmd; i++) {
192 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
193 found = true; /* indicate command found */
194 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
195 bnet_fsend(dir, no_auth);
196 bnet_sig(dir, BNET_EOD);
199 if ((jcr->authenticated) && (!cmds[i].monitoraccess) && (jcr->director->monitor)) {
200 Dmsg1(100, "Command %s illegal.\n", cmds[i].cmd);
201 bnet_fsend(dir, illegal_cmd);
202 bnet_sig(dir, BNET_EOD);
205 Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
206 if (!cmds[i].func(jcr)) { /* do command */
207 quit = true; /* error or fully terminated, get out */
208 Dmsg1(20, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
213 if (!found) { /* command not found */
214 bnet_fsend(dir, errmsg);
220 /* Inform Storage daemon that we are done */
221 if (jcr->store_bsock) {
222 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
225 if (jcr->RunAfterJob && !job_canceled(jcr)) {
226 run_cmd(jcr, jcr->RunAfterJob, "ClientRunAfterJob");
228 generate_daemon_event(jcr, "JobEnd");
230 dequeue_messages(jcr); /* send any queued messages */
232 /* Inform Director that we are done */
233 bnet_sig(dir, BNET_TERMINATE);
235 /* Clean up fileset */
236 FF_PKT *ff = (FF_PKT *)jcr->ff;
237 findFILESET *fileset = ff->fileset;
240 /* Delete FileSet Include lists */
241 for (i=0; i<fileset->include_list.size(); i++) {
242 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
243 for (j=0; j<incexe->opts_list.size(); j++) {
244 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
245 for (k=0; k<fo->regex.size(); k++) {
246 regfree((regex_t *)fo->regex.get(k));
249 fo->regexdir.destroy();
250 fo->regexfile.destroy();
252 fo->wilddir.destroy();
253 fo->wildfile.destroy();
255 fo->fstype.destroy();
263 incexe->opts_list.destroy();
264 incexe->name_list.destroy();
266 fileset->include_list.destroy();
268 /* Delete FileSet Exclude lists */
269 for (i=0; i<fileset->exclude_list.size(); i++) {
270 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
271 for (j=0; j<incexe->opts_list.size(); j++) {
272 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
274 fo->regexdir.destroy();
275 fo->regexfile.destroy();
277 fo->wilddir.destroy();
278 fo->wildfile.destroy();
280 fo->fstype.destroy();
282 incexe->opts_list.destroy();
283 incexe->name_list.destroy();
285 fileset->exclude_list.destroy();
289 Dmsg0(100, "Calling term_find_files\n");
290 term_find_files((FF_PKT *)jcr->ff);
292 Dmsg0(100, "Done with term_find_files\n");
293 free_jcr(jcr); /* destroy JCR record */
294 Dmsg0(100, "Done with free_jcr\n");
299 * Hello from Director he must identify himself and provide his
302 static int hello_cmd(JCR *jcr)
304 Dmsg0(120, "Calling Authenticate\n");
305 if (!authenticate_director(jcr)) {
308 Dmsg0(120, "OK Authenticate\n");
309 jcr->authenticated = true;
316 static int cancel_cmd(JCR *jcr)
318 BSOCK *dir = jcr->dir_bsock;
319 char Job[MAX_NAME_LENGTH];
322 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
323 if (!(cjcr=get_jcr_by_full_name(Job))) {
324 bnet_fsend(dir, "2901 Job %s not found.\n", Job);
326 if (cjcr->store_bsock) {
328 cjcr->store_bsock->timed_out = 1;
329 cjcr->store_bsock->terminated = 1;
331 * #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
333 #if !defined(HAVE_CYGWIN)
334 pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
338 set_jcr_job_status(cjcr, JS_Canceled);
340 bnet_fsend(dir, _("2001 Job %s marked to be canceled.\n"), Job);
343 bnet_fsend(dir, _("2902 Error scanning cancel command.\n"));
345 bnet_sig(dir, BNET_EOD);
351 * Set debug level as requested by the Director
354 static int setdebug_cmd(JCR *jcr)
356 BSOCK *dir = jcr->dir_bsock;
357 int level, trace_flag;
359 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
360 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
361 pm_strcpy(jcr->errmsg, dir->msg);
362 bnet_fsend(dir, "2991 Bad setdebug command: %s\n", jcr->errmsg);
366 set_trace(trace_flag);
367 return bnet_fsend(dir, OKsetdebug, level);
371 static int estimate_cmd(JCR *jcr)
373 BSOCK *dir = jcr->dir_bsock;
376 if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
377 pm_strcpy(jcr->errmsg, dir->msg);
378 Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
379 bnet_fsend(dir, "2992 Bad estimate command.\n");
383 bnet_fsend(dir, OKest, jcr->num_files_examined,
384 edit_uint64_with_commas(jcr->JobBytes, ed2));
385 bnet_sig(dir, BNET_EOD);
390 * Get JobId and Storage Daemon Authorization key from Director
392 static int job_cmd(JCR *jcr)
394 BSOCK *dir = jcr->dir_bsock;
395 POOLMEM *sd_auth_key;
397 sd_auth_key = get_memory(dir->msglen);
398 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
399 &jcr->VolSessionId, &jcr->VolSessionTime,
401 pm_strcpy(jcr->errmsg, dir->msg);
402 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
403 bnet_fsend(dir, BADjob);
404 free_pool_memory(sd_auth_key);
407 jcr->sd_auth_key = bstrdup(sd_auth_key);
408 free_pool_memory(sd_auth_key);
409 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
410 return bnet_fsend(dir, OKjob, HOST_OS, DISTNAME, DISTVER);
413 static int runbefore_cmd(JCR *jcr)
416 BSOCK *dir = jcr->dir_bsock;
417 POOLMEM *cmd = get_memory(dir->msglen+1);
419 Dmsg1(100, "runbefore_cmd: %s", dir->msg);
420 if (sscanf(dir->msg, runbefore, cmd) != 1) {
421 pm_strcpy(jcr->errmsg, dir->msg);
422 Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
423 bnet_fsend(dir, "2905 Bad RunBeforeJob command.\n");
429 /* Run the command now */
430 ok = run_cmd(jcr, cmd, "ClientRunBeforeJob");
433 bnet_fsend(dir, OKRunBefore);
436 bnet_fsend(dir, "2905 Bad RunBeforeJob command.\n");
441 static int runafter_cmd(JCR *jcr)
443 BSOCK *dir = jcr->dir_bsock;
444 POOLMEM *msg = get_memory(dir->msglen+1);
446 Dmsg1(100, "runafter_cmd: %s", dir->msg);
447 if (sscanf(dir->msg, runafter, msg) != 1) {
448 pm_strcpy(jcr->errmsg, dir->msg);
449 Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
450 bnet_fsend(dir, "2905 Bad RunAfterJob command.\n");
455 if (jcr->RunAfterJob) {
456 free_pool_memory(jcr->RunAfterJob);
458 jcr->RunAfterJob = get_pool_memory(PM_FNAME);
459 pm_strcpy(jcr->RunAfterJob, msg);
460 free_pool_memory(msg);
461 return bnet_fsend(dir, OKRunAfter);
464 static bool run_cmd(JCR *jcr, char *cmd, const char *name)
466 POOLMEM *ecmd = get_pool_memory(PM_FNAME);
469 char line[MAXSTRING];
471 ecmd = edit_job_codes(jcr, ecmd, cmd, "");
472 bpipe = open_bpipe(ecmd, 0, "r");
473 free_pool_memory(ecmd);
476 Jmsg(jcr, M_FATAL, 0, _("%s could not execute. ERR=%s\n"), name,
480 while (fgets(line, sizeof(line), bpipe->rfd)) {
481 int len = strlen(line);
482 if (len > 0 && line[len-1] == '\n') {
485 Jmsg(jcr, M_INFO, 0, _("%s: %s\n"), name, line);
487 status = close_bpipe(bpipe);
490 Jmsg(jcr, M_FATAL, 0, _("%s returned non-zero status=%d. ERR=%s\n"), name,
491 status, be.strerror(status));
497 static bool init_fileset(JCR *jcr)
500 findFILESET *fileset;
505 ff = (FF_PKT *)jcr->ff;
509 fileset = (findFILESET *)malloc(sizeof(findFILESET));
510 memset(fileset, 0, sizeof(findFILESET));
511 ff->fileset = fileset;
512 fileset->state = state_none;
513 fileset->include_list.init(1, true);
514 fileset->exclude_list.init(1, true);
518 static findFOPTS *start_options(FF_PKT *ff)
520 int state = ff->fileset->state;
521 findINCEXE *incexe = ff->fileset->incexe;
523 if (state != state_options) {
524 ff->fileset->state = state_options;
525 findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS));
526 memset(fo, 0, sizeof(findFOPTS));
527 fo->regex.init(1, true);
528 fo->regexdir.init(1, true);
529 fo->regexfile.init(1, true);
530 fo->wild.init(1, true);
531 fo->wilddir.init(1, true);
532 fo->wildfile.init(1, true);
533 fo->base.init(1, true);
534 fo->fstype.init(1, true);
535 incexe->current_opts = fo;
536 incexe->opts_list.append(fo);
538 return incexe->current_opts;
543 * Add fname to include/exclude fileset list. First check for
544 * | and < and if necessary perform command.
546 static void add_file_to_fileset(JCR *jcr, const char *fname, findFILESET *fileset)
560 p++; /* skip over | */
561 fn = get_pool_memory(PM_FNAME);
562 fn = edit_job_codes(jcr, fn, p, "");
563 bpipe = open_bpipe(fn, 0, "r");
564 free_pool_memory(fn);
566 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
570 while (fgets(buf, sizeof(buf), bpipe->rfd)) {
571 strip_trailing_junk(buf);
572 fileset->incexe->name_list.append(bstrdup(buf));
574 if ((stat=close_bpipe(bpipe)) != 0) {
575 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
576 p, stat, strerror(errno));
581 Dmsg0(100, "Doing < include on client.\n");
582 p++; /* skip over < */
583 if ((ffd = fopen(p, "r")) == NULL) {
585 Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"),
589 while (fgets(buf, sizeof(buf), ffd)) {
590 strip_trailing_junk(buf);
591 Dmsg1(100, "%s\n", buf);
592 fileset->incexe->name_list.append(bstrdup(buf));
597 fileset->incexe->name_list.append(bstrdup(fname));
603 static void add_fileset(JCR *jcr, const char *item)
605 FF_PKT *ff = (FF_PKT *)jcr->ff;
606 findFILESET *fileset = ff->fileset;
607 int state = fileset->state;
608 findFOPTS *current_opts;
610 /* Get code, optional subcode, and position item past the dividing space */
611 Dmsg1(100, "%s\n", item);
616 int subcode = ' '; /* A space is always a valid subcode */
617 if (item[0] != '\0' && item[0] != ' ') {
625 /* Skip all lines we receive after an error */
626 if (state == state_error) {
631 * The switch tests the code for validity.
632 * The subcode is always good if it is a space, otherwise we must confirm.
633 * We set state to state_error first assuming the subcode is invalid,
634 * requiring state to be set in cases below that handle subcodes.
636 if (subcode != ' ') {
642 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
643 memset(fileset->incexe, 0, sizeof(findINCEXE));
644 fileset->incexe->opts_list.init(1, true);
645 fileset->incexe->name_list.init(1, true);
646 fileset->include_list.append(fileset->incexe);
650 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
651 memset(fileset->incexe, 0, sizeof(findINCEXE));
652 fileset->incexe->opts_list.init(1, true);
653 fileset->incexe->name_list.init(1, true);
654 fileset->exclude_list.append(fileset->incexe);
660 /* File item to either include/include list */
661 state = state_include;
662 add_file_to_fileset(jcr, item, fileset);
665 current_opts = start_options(ff);
669 preg = (regex_t *)malloc(sizeof(regex_t));
670 if (current_opts->flags & FO_IGNORECASE) {
671 rc = regcomp(preg, item, REG_EXTENDED|REG_ICASE);
673 rc = regcomp(preg, item, REG_EXTENDED);
676 regerror(rc, preg, prbuf, sizeof(prbuf));
679 Jmsg(jcr, M_FATAL, 0, "REGEX %s compile error. ERR=%s\n", item, prbuf);
683 state = state_options;
684 if (subcode == ' ') {
685 current_opts->regex.append(preg);
686 } else if (subcode == 'D') {
687 current_opts->regexdir.append(preg);
688 } else if (subcode == 'F') {
689 current_opts->regexfile.append(preg);
695 current_opts = start_options(ff);
696 current_opts->base.append(bstrdup(item));
697 state = state_options;
700 current_opts = start_options(ff);
701 current_opts->fstype.append(bstrdup(item));
702 state = state_options;
705 current_opts = start_options(ff);
706 state = state_options;
707 if (subcode == ' ') {
708 current_opts->wild.append(bstrdup(item));
709 } else if (subcode == 'D') {
710 current_opts->wilddir.append(bstrdup(item));
711 } else if (subcode == 'F') {
712 current_opts->wildfile.append(bstrdup(item));
718 current_opts = start_options(ff);
719 set_options(current_opts, item);
720 state = state_options;
723 current_opts = start_options(ff);
724 current_opts->reader = bstrdup(item);
725 state = state_options;
728 current_opts = start_options(ff);
729 current_opts->writer = bstrdup(item);
730 state = state_options;
733 Jmsg(jcr, M_FATAL, 0, "Invalid FileSet command: %s\n", item);
737 ff->fileset->state = state;
740 static bool term_fileset(JCR *jcr)
742 FF_PKT *ff = (FF_PKT *)jcr->ff;
743 findFILESET *fileset = ff->fileset;
746 for (i=0; i<fileset->include_list.size(); i++) {
747 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
749 for (j=0; j<incexe->opts_list.size(); j++) {
750 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
751 for (k=0; k<fo->regex.size(); k++) {
752 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
754 for (k=0; k<fo->regexdir.size(); k++) {
755 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
757 for (k=0; k<fo->regexfile.size(); k++) {
758 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
760 for (k=0; k<fo->wild.size(); k++) {
761 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
763 for (k=0; k<fo->wilddir.size(); k++) {
764 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
766 for (k=0; k<fo->wildfile.size(); k++) {
767 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
769 for (k=0; k<fo->base.size(); k++) {
770 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
772 for (k=0; k<fo->fstype.size(); k++) {
773 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
776 Dmsg1(400, "D %s\n", fo->reader);
779 Dmsg1(400, "T %s\n", fo->writer);
782 for (j=0; j<incexe->name_list.size(); j++) {
783 Dmsg1(400, "F %s\n", (char *)incexe->name_list.get(j));
786 for (i=0; i<fileset->exclude_list.size(); i++) {
787 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
789 for (j=0; j<incexe->opts_list.size(); j++) {
790 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
791 for (k=0; k<fo->regex.size(); k++) {
792 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
794 for (k=0; k<fo->regexdir.size(); k++) {
795 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
797 for (k=0; k<fo->regexfile.size(); k++) {
798 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
800 for (k=0; k<fo->wild.size(); k++) {
801 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
803 for (k=0; k<fo->wilddir.size(); k++) {
804 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
806 for (k=0; k<fo->wildfile.size(); k++) {
807 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
809 for (k=0; k<fo->base.size(); k++) {
810 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
812 for (k=0; k<fo->fstype.size(); k++) {
813 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
816 for (j=0; j<incexe->name_list.size(); j++) {
817 Dmsg1(400, "F %s\n", (char *)incexe->name_list.get(j));
820 return ff->fileset->state != state_error;
825 * As an optimization, we should do this during
826 * "compile" time in filed/job.c, and keep only a bit mask
827 * and the Verify options.
829 static void set_options(findFOPTS *fo, const char *opts)
834 for (p=opts; *p; p++) {
836 case 'a': /* alway replace */
837 case '0': /* no option */
840 fo->flags |= FO_EXCLUDE;
843 fo->flags |= FO_MULTIFS;
845 case 'h': /* no recursion */
846 fo->flags |= FO_NO_RECURSION;
848 case 'H': /* no hard link handling */
849 fo->flags |= FO_NO_HARDLINK;
852 fo->flags |= FO_IGNORECASE;
858 fo->flags |= FO_NOREPLACE;
860 case 'p': /* use portable data format */
861 fo->flags |= FO_PORTABLE;
863 case 'R': /* Resource forks and Finder Info */
864 fo->flags |= FO_HFSPLUS;
865 case 'r': /* read fifo */
866 fo->flags |= FO_READFIFO;
869 fo->flags |= FO_SHA1;
872 fo->flags |= FO_SPARSE;
875 fo->flags |= FO_MTIMEONLY;
878 fo->flags |= FO_KEEPATIME;
883 case 'V': /* verify options */
884 /* Copy Verify Options */
885 for (j=0; *p && *p != ':'; p++) {
886 fo->VerifyOpts[j] = *p;
887 if (j < (int)sizeof(fo->VerifyOpts) - 1) {
891 fo->VerifyOpts[j] = 0;
894 fo->flags |= FO_IF_NEWER;
896 case 'Z': /* gzip compression */
897 fo->flags |= FO_GZIP;
898 fo->GZIP_level = *++p - '0';
899 Dmsg1(200, "Compression level=%d\n", fo->GZIP_level);
902 Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *p);
910 * Director is passing his Fileset
912 static int fileset_cmd(JCR *jcr)
914 BSOCK *dir = jcr->dir_bsock;
917 sscanf(dir->msg, "fileset vss=%d", &vss);
920 if (!init_fileset(jcr)) {
923 while (bnet_recv(dir) >= 0) {
924 strip_trailing_junk(dir->msg);
925 Dmsg1(400, "Fileset: %s\n", dir->msg);
926 add_fileset(jcr, dir->msg);
928 if (!term_fileset(jcr)) {
931 return bnet_fsend(dir, OKinc);
936 * The Director sends us the bootstrap file, which
937 * we will in turn pass to the SD.
939 static int bootstrap_cmd(JCR *jcr)
941 BSOCK *dir = jcr->dir_bsock;
942 POOLMEM *fname = get_pool_memory(PM_FNAME);
945 if (jcr->RestoreBootstrap) {
946 unlink(jcr->RestoreBootstrap);
947 free_pool_memory(jcr->RestoreBootstrap);
949 Mmsg(fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
951 Dmsg1(400, "bootstrap=%s\n", fname);
952 jcr->RestoreBootstrap = fname;
953 bs = fopen(fname, "a+"); /* create file */
957 * Suck up what he is sending to us so that he will then
958 * read our error message.
960 while (bnet_recv(dir) >= 0)
963 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
964 jcr->RestoreBootstrap, be.strerror());
965 free_pool_memory(jcr->RestoreBootstrap);
966 jcr->RestoreBootstrap = NULL;
967 set_jcr_job_status(jcr, JS_ErrorTerminated);
971 while (bnet_recv(dir) >= 0) {
972 Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
977 return bnet_fsend(dir, OKbootstrap);
982 * Get backup level from Director
985 static int level_cmd(JCR *jcr)
987 BSOCK *dir = jcr->dir_bsock;
988 POOLMEM *level, *buf = NULL;
991 level = get_memory(dir->msglen+1);
992 Dmsg1(110, "level_cmd: %s", dir->msg);
993 if (sscanf(dir->msg, "level = %s ", level) != 1) {
996 /* Base backup requested? */
997 if (strcmp(level, "base") == 0) {
998 jcr->JobLevel = L_BASE;
999 /* Full backup requested? */
1000 } else if (strcmp(level, "full") == 0) {
1001 jcr->JobLevel = L_FULL;
1002 } else if (strcmp(level, "differential") == 0) {
1003 jcr->JobLevel = L_DIFFERENTIAL;
1006 } else if (strcmp(level, "incremental") == 0) {
1007 jcr->JobLevel = L_INCREMENTAL;
1011 * We get his UTC since time, then sync the clocks and correct it
1012 * to agree with our clock.
1014 } else if (strcmp(level, "since_utime") == 0) {
1015 buf = get_memory(dir->msglen+1);
1016 utime_t since_time, adj;
1017 btime_t his_time, bt_start, rt=0, bt_adj=0;
1018 if (jcr->JobLevel == L_NONE) {
1019 jcr->JobLevel = L_SINCE; /* if no other job level set, do it now */
1021 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
1022 buf, &mtime_only) != 2) {
1025 since_time = str_to_uint64(buf); /* this is the since time */
1026 char ed1[50], ed2[50];
1028 * Sync clocks by polling him for the time. We take
1029 * 10 samples of his time throwing out the first two.
1031 for (int i=0; i<10; i++) {
1032 bt_start = get_current_btime();
1033 bnet_sig(dir, BNET_BTIME); /* poll for time */
1034 if (bnet_recv(dir) <= 0) { /* get response */
1037 if (sscanf(dir->msg, "btime %s", buf) != 1) {
1040 if (i < 2) { /* toss first two results */
1043 his_time = str_to_uint64(buf);
1044 rt = get_current_btime() - bt_start; /* compute round trip time */
1045 bt_adj -= his_time - bt_start - rt/2;
1046 Dmsg2(200, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1049 bt_adj = bt_adj / 8; /* compute average time */
1050 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1051 adj = btime_to_utime(bt_adj);
1052 since_time += adj; /* adjust for clock difference */
1054 Jmsg(jcr, M_INFO, 0, _("DIR and FD clocks differ by %d seconds, FD automatically adjusting.\n"), adj);
1056 bnet_sig(dir, BNET_EOD);
1058 Dmsg2(100, "adj = %d since_time=%d\n", (int)adj, (int)since_time);
1059 jcr->incremental = 1; /* set incremental or decremental backup */
1060 jcr->mtime = (time_t)since_time; /* set since time */
1062 Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
1070 return bnet_fsend(dir, OKlevel);
1073 pm_strcpy(jcr->errmsg, dir->msg);
1074 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
1083 * Get session parameters from Director -- this is for a Restore command
1085 static int session_cmd(JCR *jcr)
1087 BSOCK *dir = jcr->dir_bsock;
1089 Dmsg1(100, "SessionCmd: %s", dir->msg);
1090 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
1091 &jcr->VolSessionId, &jcr->VolSessionTime,
1092 &jcr->StartFile, &jcr->EndFile,
1093 &jcr->StartBlock, &jcr->EndBlock) != 7) {
1094 pm_strcpy(jcr->errmsg, dir->msg);
1095 Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", jcr->errmsg);
1099 return bnet_fsend(dir, OKsession);
1103 * Get address of storage daemon from Director
1106 static int storage_cmd(JCR *jcr)
1108 int stored_port; /* storage daemon port */
1109 int enable_ssl; /* enable ssl to sd */
1110 BSOCK *dir = jcr->dir_bsock;
1111 BSOCK *sd; /* storage daemon bsock */
1113 Dmsg1(100, "StorageCmd: %s", dir->msg);
1114 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
1115 pm_strcpy(jcr->errmsg, dir->msg);
1116 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
1119 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
1120 /* Open command communications with Storage daemon */
1121 /* Try to connect for 1 hour at 10 second intervals */
1122 sd = bnet_connect(jcr, 10, (int)me->SDConnectTimeout, _("Storage daemon"),
1123 jcr->stored_addr, NULL, stored_port, 1);
1125 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
1126 jcr->stored_addr, stored_port);
1127 Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
1128 jcr->stored_addr, stored_port);
1131 Dmsg0(110, "Connection OK to SD.\n");
1133 jcr->store_bsock = sd;
1135 bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
1136 if (!authenticate_storagedaemon(jcr)) {
1137 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
1140 Dmsg0(110, "Authenticated with SD.\n");
1142 /* Send OK to Director */
1143 return bnet_fsend(dir, OKstore);
1148 * Do a backup. For now, we handle only Full and Incremental.
1150 static int backup_cmd(JCR *jcr)
1152 BSOCK *dir = jcr->dir_bsock;
1153 BSOCK *sd = jcr->store_bsock;
1156 char ed1[50], ed2[50];
1158 set_jcr_job_status(jcr, JS_Blocked);
1159 jcr->JobType = JT_BACKUP;
1160 Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
1163 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
1167 bnet_fsend(dir, OKbackup);
1168 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1171 * Send Append Open Session to Storage daemon
1173 bnet_fsend(sd, append_open);
1174 Dmsg1(110, ">stored: %s", sd->msg);
1176 * Expect to receive back the Ticket number
1178 if (bget_msg(sd) >= 0) {
1179 Dmsg1(110, "<stored: %s", sd->msg);
1180 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1181 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
1184 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
1186 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
1191 * Send Append data command to Storage daemon
1193 bnet_fsend(sd, append_data, jcr->Ticket);
1194 Dmsg1(110, ">stored: %s", sd->msg);
1197 * Expect to get OK data
1199 Dmsg1(110, "<stored: %s", sd->msg);
1200 if (!response(jcr, sd, OK_data, "Append Data")) {
1204 generate_daemon_event(jcr, "JobStart");
1207 /* START VSS ON WIN 32 */
1208 if (g_pVSSClient && enable_vss == 1) {
1209 if (g_pVSSClient->InitializeForBackup()) {
1210 /* tell vss which drives to snapshot */
1211 char szWinDriveLetters[27];
1212 if (get_win32_driveletters((FF_PKT *)jcr->ff, szWinDriveLetters)) {
1213 Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
1215 if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {
1216 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed\n"));
1219 /* tell user if snapshot creation of a specific drive failed */
1220 for (size_t i=0; i<strlen (szWinDriveLetters); i++) {
1221 if (islower(szWinDriveLetters[i]))
1222 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed\n"), szWinDriveLetters[i]);
1224 /* inform user about writer states */
1225 for (size_t i=0; i<g_pVSSClient->GetWriterCount(); i++) {
1226 int msg_type = M_INFO;
1227 if (g_pVSSClient->GetWriterState(i) < 0)
1228 msg_type = M_WARNING;
1230 Jmsg(jcr, msg_type, 0, _("VSS Writer: %s\n"), g_pVSSClient->GetWriterInfo(i));
1235 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled."));
1241 * Send Files to Storage daemon
1243 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
1244 if (!blast_data_to_storage_daemon(jcr, NULL)) {
1245 set_jcr_job_status(jcr, JS_ErrorTerminated);
1246 bnet_suppress_error_messages(sd, 1);
1247 bget_msg(sd); /* Read final response from append_data */
1248 Dmsg0(110, "Error in blast_data.\n");
1250 set_jcr_job_status(jcr, JS_Terminated);
1251 if (jcr->JobStatus != JS_Terminated) {
1252 bnet_suppress_error_messages(sd, 1);
1253 goto cleanup; /* bail out now */
1256 * Expect to get response to append_data from Storage daemon
1258 if (!response(jcr, sd, OK_append, "Append Data")) {
1259 set_jcr_job_status(jcr, JS_ErrorTerminated);
1264 * Send Append End Data to Storage daemon
1266 bnet_fsend(sd, append_end, jcr->Ticket);
1268 if (!response(jcr, sd, OK_end, "Append End")) {
1269 set_jcr_job_status(jcr, JS_ErrorTerminated);
1274 * Send Append Close to Storage daemon
1276 bnet_fsend(sd, append_close, jcr->Ticket);
1277 while (bget_msg(sd) >= 0) { /* stop on signal or error */
1278 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
1280 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
1284 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
1287 if (SDJobStatus != JS_Terminated) {
1288 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
1295 /* STOP VSS ON WIN 32 */
1296 /* tell vss to close the backup session */
1297 if (g_pVSSClient && enable_vss == 1)
1298 g_pVSSClient->CloseBackup();
1301 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1302 edit_uint64(jcr->ReadBytes, ed1),
1303 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
1304 Dmsg1(110, "End FD msg: %s\n", dir->msg);
1306 return 0; /* return and stop command loop */
1310 * Do a Verify for Director
1313 static int verify_cmd(JCR *jcr)
1315 BSOCK *dir = jcr->dir_bsock;
1316 BSOCK *sd = jcr->store_bsock;
1317 char level[100], ed1[50], ed2[50];
1319 jcr->JobType = JT_VERIFY;
1320 if (sscanf(dir->msg, verifycmd, level) != 1) {
1321 bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
1325 if (strcasecmp(level, "init") == 0) {
1326 jcr->JobLevel = L_VERIFY_INIT;
1327 } else if (strcasecmp(level, "catalog") == 0){
1328 jcr->JobLevel = L_VERIFY_CATALOG;
1329 } else if (strcasecmp(level, "volume") == 0){
1330 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
1331 } else if (strcasecmp(level, "data") == 0){
1332 jcr->JobLevel = L_VERIFY_DATA;
1333 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
1334 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
1336 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
1340 bnet_fsend(dir, OKverify);
1342 generate_daemon_event(jcr, "JobStart");
1344 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1346 switch (jcr->JobLevel) {
1348 case L_VERIFY_CATALOG:
1351 case L_VERIFY_VOLUME_TO_CATALOG:
1352 if (!open_sd_read_session(jcr)) {
1355 start_dir_heartbeat(jcr);
1356 do_verify_volume(jcr);
1357 stop_dir_heartbeat(jcr);
1359 * Send Close session command to Storage daemon
1361 bnet_fsend(sd, read_close, jcr->Ticket);
1362 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1364 /* ****FIXME**** check response */
1365 bget_msg(sd); /* get OK */
1367 /* Inform Storage daemon that we are done */
1368 bnet_sig(sd, BNET_TERMINATE);
1371 case L_VERIFY_DISK_TO_CATALOG:
1375 bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
1379 bnet_sig(dir, BNET_EOD);
1381 /* Send termination status back to Dir */
1382 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1383 edit_uint64(jcr->ReadBytes, ed1),
1384 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
1386 /* Inform Director that we are done */
1387 bnet_sig(dir, BNET_TERMINATE);
1388 return 0; /* return and terminate command loop */
1392 * Do a Restore for Director
1395 static int restore_cmd(JCR *jcr)
1397 BSOCK *dir = jcr->dir_bsock;
1398 BSOCK *sd = jcr->store_bsock;
1402 char ed1[50], ed2[50];
1405 * Scan WHERE (base directory for restore) from command
1407 Dmsg0(150, "restore command\n");
1408 /* Pickup where string */
1409 where = get_memory(dir->msglen+1);
1412 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, where) != 3) {
1413 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
1414 pm_strcpy(jcr->errmsg, dir->msg);
1415 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
1420 /* Turn / into nothing */
1421 if (where[0] == '/' && where[1] == 0) {
1425 Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
1426 unbash_spaces(where);
1427 jcr->where = bstrdup(where);
1428 free_pool_memory(where);
1429 jcr->replace = replace;
1430 jcr->prefix_links = prefix_links;
1432 bnet_fsend(dir, OKrestore);
1433 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1435 jcr->JobType = JT_RESTORE;
1437 set_jcr_job_status(jcr, JS_Blocked);
1439 if (!open_sd_read_session(jcr)) {
1440 set_jcr_job_status(jcr, JS_ErrorTerminated);
1444 set_jcr_job_status(jcr, JS_Running);
1447 * Do restore of files and data
1449 start_dir_heartbeat(jcr);
1450 generate_daemon_event(jcr, "JobStart");
1452 stop_dir_heartbeat(jcr);
1454 set_jcr_job_status(jcr, JS_Terminated);
1455 if (jcr->JobStatus != JS_Terminated) {
1456 bnet_suppress_error_messages(sd, 1);
1460 * Send Close session command to Storage daemon
1462 bnet_fsend(sd, read_close, jcr->Ticket);
1463 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1465 bget_msg(sd); /* get OK */
1467 /* Inform Storage daemon that we are done */
1468 bnet_sig(sd, BNET_TERMINATE);
1473 set_jcr_job_status(jcr, JS_ErrorTerminated);
1475 /* Send termination status back to Dir */
1476 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1477 edit_uint64(jcr->ReadBytes, ed1),
1478 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
1480 /* Inform Director that we are done */
1481 bnet_sig(dir, BNET_TERMINATE);
1483 Dmsg0(130, "Done in job.c\n");
1484 return 0; /* return and terminate command loop */
1487 static int open_sd_read_session(JCR *jcr)
1489 BSOCK *sd = jcr->store_bsock;
1492 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
1495 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
1496 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
1497 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
1499 * Open Read Session with Storage daemon
1501 bnet_fsend(sd, read_open, jcr->VolumeName,
1502 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
1503 jcr->StartBlock, jcr->EndBlock);
1504 Dmsg1(110, ">stored: %s", sd->msg);
1509 if (bget_msg(sd) >= 0) {
1510 Dmsg1(110, "bfiled<stored: %s", sd->msg);
1511 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1512 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
1515 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
1517 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
1521 if (!send_bootstrap_file(jcr)) {
1526 * Start read of data with Storage daemon
1528 bnet_fsend(sd, read_data, jcr->Ticket);
1529 Dmsg1(110, ">stored: %s", sd->msg);
1534 if (!response(jcr, sd, OK_data, "Read Data")) {
1541 * Destroy the Job Control Record and associated
1542 * resources (sockets).
1544 static void filed_free_jcr(JCR *jcr)
1546 if (jcr->store_bsock) {
1547 bnet_close(jcr->store_bsock);
1549 if (jcr->RestoreBootstrap) {
1550 unlink(jcr->RestoreBootstrap);
1551 free_pool_memory(jcr->RestoreBootstrap);
1552 jcr->RestoreBootstrap = NULL;
1554 if (jcr->last_fname) {
1555 free_pool_memory(jcr->last_fname);
1557 if (jcr->RunAfterJob) {
1558 free_pool_memory(jcr->RunAfterJob);
1566 * Get response from Storage daemon to a command we
1567 * sent. Check that the response is OK.
1569 * Returns: 0 on failure
1572 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
1577 if (bget_msg(sd) > 0) {
1578 Dmsg0(110, sd->msg);
1579 if (strcmp(sd->msg, resp) == 0) {
1583 if (job_canceled(jcr)) {
1584 return 0; /* if canceled avoid useless error messages */
1586 if (is_bnet_error(sd)) {
1587 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
1588 cmd, bnet_strerror(sd));
1590 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
1591 cmd, resp, sd->msg);
1596 static int send_bootstrap_file(JCR *jcr)
1600 BSOCK *sd = jcr->store_bsock;
1601 const char *bootstrap = "bootstrap\n";
1604 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
1605 if (!jcr->RestoreBootstrap) {
1608 bs = fopen(jcr->RestoreBootstrap, "r");
1611 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
1612 jcr->RestoreBootstrap, be.strerror());
1613 set_jcr_job_status(jcr, JS_ErrorTerminated);
1616 sd->msglen = pm_strcpy(sd->msg, bootstrap);
1618 while (fgets(buf, sizeof(buf), bs)) {
1619 sd->msglen = Mmsg(sd->msg, "%s", buf);
1622 bnet_sig(sd, BNET_EOD);
1624 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
1625 set_jcr_job_status(jcr, JS_ErrorTerminated);
1631 if (jcr->RestoreBootstrap) {
1632 unlink(jcr->RestoreBootstrap);
1633 free_pool_memory(jcr->RestoreBootstrap);
1634 jcr->RestoreBootstrap = NULL;