2 * Bacula File Daemon Job processing
4 * Kern Sibbald, October MM
10 Copyright (C) 2000-2006 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 amended 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.
28 static pthread_mutex_t vss_mutex = PTHREAD_MUTEX_INITIALIZER;
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);
177 jcr->pki_sign = me->pki_sign;
178 jcr->pki_encrypt = me->pki_encrypt;
179 jcr->pki_keypair = me->pki_keypair;
180 jcr->pki_signers = me->pki_signers;
181 jcr->pki_recipients = me->pki_recipients;
183 enable_backup_privileges(NULL, 1 /* ignore_errors */);
185 /**********FIXME******* add command handler error code */
187 for (quit=false; !quit;) {
190 if (bnet_recv(dir) < 0) {
191 break; /* connection terminated */
193 dir->msg[dir->msglen] = 0;
194 Dmsg1(100, "<dird: %s", dir->msg);
196 for (i=0; cmds[i].cmd; i++) {
197 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
198 found = true; /* indicate command found */
199 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
200 bnet_fsend(dir, no_auth);
201 bnet_sig(dir, BNET_EOD);
204 if ((jcr->authenticated) && (!cmds[i].monitoraccess) && (jcr->director->monitor)) {
205 Dmsg1(100, "Command %s illegal.\n", cmds[i].cmd);
206 bnet_fsend(dir, illegal_cmd);
207 bnet_sig(dir, BNET_EOD);
210 Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
211 if (!cmds[i].func(jcr)) { /* do command */
212 quit = true; /* error or fully terminated, get out */
213 Dmsg1(20, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
218 if (!found) { /* command not found */
219 bnet_fsend(dir, errmsg);
225 /* Inform Storage daemon that we are done */
226 if (jcr->store_bsock) {
227 bnet_sig(jcr->store_bsock, BNET_TERMINATE);
230 if (jcr->RunAfterJob && !job_canceled(jcr)) {
231 run_cmd(jcr, jcr->RunAfterJob, "ClientRunAfterJob");
233 generate_daemon_event(jcr, "JobEnd");
235 dequeue_messages(jcr); /* send any queued messages */
237 /* Inform Director that we are done */
238 bnet_sig(dir, BNET_TERMINATE);
240 /* Clean up fileset */
241 FF_PKT *ff = jcr->ff;
242 findFILESET *fileset = ff->fileset;
245 /* Delete FileSet Include lists */
246 for (i=0; i<fileset->include_list.size(); i++) {
247 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
248 for (j=0; j<incexe->opts_list.size(); j++) {
249 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
250 for (k=0; k<fo->regex.size(); k++) {
251 regfree((regex_t *)fo->regex.get(k));
254 fo->regexdir.destroy();
255 fo->regexfile.destroy();
257 fo->wilddir.destroy();
258 fo->wildfile.destroy();
260 fo->fstype.destroy();
268 incexe->opts_list.destroy();
269 incexe->name_list.destroy();
271 fileset->include_list.destroy();
273 /* Delete FileSet Exclude lists */
274 for (i=0; i<fileset->exclude_list.size(); i++) {
275 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
276 for (j=0; j<incexe->opts_list.size(); j++) {
277 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
279 fo->regexdir.destroy();
280 fo->regexfile.destroy();
282 fo->wilddir.destroy();
283 fo->wildfile.destroy();
285 fo->fstype.destroy();
287 incexe->opts_list.destroy();
288 incexe->name_list.destroy();
290 fileset->exclude_list.destroy();
294 Dmsg0(100, "Calling term_find_files\n");
295 term_find_files(jcr->ff);
297 Dmsg0(100, "Done with term_find_files\n");
298 free_jcr(jcr); /* destroy JCR record */
299 Dmsg0(100, "Done with free_jcr\n");
304 * Hello from Director he must identify himself and provide his
307 static int hello_cmd(JCR *jcr)
309 Dmsg0(120, "Calling Authenticate\n");
310 if (!authenticate_director(jcr)) {
313 Dmsg0(120, "OK Authenticate\n");
314 jcr->authenticated = true;
321 static int cancel_cmd(JCR *jcr)
323 BSOCK *dir = jcr->dir_bsock;
324 char Job[MAX_NAME_LENGTH];
327 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
328 if (!(cjcr=get_jcr_by_full_name(Job))) {
329 bnet_fsend(dir, _("2901 Job %s not found.\n"), Job);
331 if (cjcr->store_bsock) {
332 cjcr->store_bsock->timed_out = 1;
333 cjcr->store_bsock->terminated = 1;
334 #if !defined(HAVE_CYGWIN)
335 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;
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 = 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 = jcr->ff;
745 findFILESET *fileset = ff->fileset;
748 for (i=0; i<fileset->include_list.size(); i++) {
749 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
751 for (j=0; j<incexe->opts_list.size(); j++) {
752 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
753 for (k=0; k<fo->regex.size(); k++) {
754 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
756 for (k=0; k<fo->regexdir.size(); k++) {
757 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
759 for (k=0; k<fo->regexfile.size(); k++) {
760 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
762 for (k=0; k<fo->wild.size(); k++) {
763 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
765 for (k=0; k<fo->wilddir.size(); k++) {
766 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
768 for (k=0; k<fo->wildfile.size(); k++) {
769 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
771 for (k=0; k<fo->base.size(); k++) {
772 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
774 for (k=0; k<fo->fstype.size(); k++) {
775 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
778 Dmsg1(400, "D %s\n", fo->reader);
781 Dmsg1(400, "T %s\n", fo->writer);
784 for (j=0; j<incexe->name_list.size(); j++) {
785 Dmsg1(400, "F %s\n", (char *)incexe->name_list.get(j));
788 for (i=0; i<fileset->exclude_list.size(); i++) {
789 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
791 for (j=0; j<incexe->opts_list.size(); j++) {
792 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
793 for (k=0; k<fo->regex.size(); k++) {
794 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
796 for (k=0; k<fo->regexdir.size(); k++) {
797 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
799 for (k=0; k<fo->regexfile.size(); k++) {
800 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
802 for (k=0; k<fo->wild.size(); k++) {
803 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
805 for (k=0; k<fo->wilddir.size(); k++) {
806 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
808 for (k=0; k<fo->wildfile.size(); k++) {
809 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
811 for (k=0; k<fo->base.size(); k++) {
812 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
814 for (k=0; k<fo->fstype.size(); k++) {
815 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
818 for (j=0; j<incexe->name_list.size(); j++) {
819 Dmsg1(400, "F %s\n", (char *)incexe->name_list.get(j));
823 return ff->fileset->state != state_error;
828 * As an optimization, we should do this during
829 * "compile" time in filed/job.c, and keep only a bit mask
830 * and the Verify options.
832 static void set_options(findFOPTS *fo, const char *opts)
837 for (p=opts; *p; p++) {
839 case 'a': /* alway replace */
840 case '0': /* no option */
843 fo->flags |= FO_EXCLUDE;
846 fo->flags |= FO_MULTIFS;
848 case 'h': /* no recursion */
849 fo->flags |= FO_NO_RECURSION;
851 case 'H': /* no hard link handling */
852 fo->flags |= FO_NO_HARDLINK;
855 fo->flags |= FO_IGNORECASE;
861 fo->flags |= FO_NOREPLACE;
863 case 'p': /* use portable data format */
864 fo->flags |= FO_PORTABLE;
866 case 'R': /* Resource forks and Finder Info */
867 fo->flags |= FO_HFSPLUS;
868 case 'r': /* read fifo */
869 fo->flags |= FO_READFIFO;
874 /* Old director did not specify SHA variant */
875 fo->flags |= FO_SHA1;
878 fo->flags |= FO_SHA1;
883 fo->flags |= FO_SHA256;
887 fo->flags |= FO_SHA512;
892 /* Automatically downgrade to SHA-1 if an unsupported
893 * SHA variant is specified */
894 fo->flags |= FO_SHA1;
900 fo->flags |= FO_SPARSE;
903 fo->flags |= FO_MTIMEONLY;
906 fo->flags |= FO_KEEPATIME;
911 case 'V': /* verify options */
912 /* Copy Verify Options */
913 for (j=0; *p && *p != ':'; p++) {
914 fo->VerifyOpts[j] = *p;
915 if (j < (int)sizeof(fo->VerifyOpts) - 1) {
919 fo->VerifyOpts[j] = 0;
922 fo->flags |= FO_IF_NEWER;
924 case 'Z': /* gzip compression */
925 fo->flags |= FO_GZIP;
926 fo->GZIP_level = *++p - '0';
927 Dmsg1(200, "Compression level=%d\n", fo->GZIP_level);
930 fo->flags |= FO_NOATIME;
933 Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *p);
941 * Director is passing his Fileset
943 static int fileset_cmd(JCR *jcr)
945 BSOCK *dir = jcr->dir_bsock;
948 sscanf(dir->msg, "fileset vss=%d", &vss);
951 if (!init_fileset(jcr)) {
954 while (bnet_recv(dir) >= 0) {
955 strip_trailing_junk(dir->msg);
956 Dmsg1(500, "Fileset: %s\n", dir->msg);
957 add_fileset(jcr, dir->msg);
959 if (!term_fileset(jcr)) {
962 return bnet_fsend(dir, OKinc);
965 static void free_bootstrap(JCR *jcr)
967 if (jcr->RestoreBootstrap) {
968 unlink(jcr->RestoreBootstrap);
969 free_pool_memory(jcr->RestoreBootstrap);
970 jcr->RestoreBootstrap = NULL;
976 * The Director sends us the bootstrap file, which
977 * we will in turn pass to the SD.
979 static int bootstrap_cmd(JCR *jcr)
981 BSOCK *dir = jcr->dir_bsock;
982 POOLMEM *fname = get_pool_memory(PM_FNAME);
986 Mmsg(fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
988 Dmsg1(400, "bootstrap=%s\n", fname);
989 jcr->RestoreBootstrap = fname;
990 bs = fopen(fname, "a+"); /* create file */
993 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
994 jcr->RestoreBootstrap, be.strerror());
996 * Suck up what he is sending to us so that he will then
997 * read our error message.
999 while (bnet_recv(dir) >= 0)
1001 free_bootstrap(jcr);
1002 set_jcr_job_status(jcr, JS_ErrorTerminated);
1006 while (bnet_recv(dir) >= 0) {
1007 Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
1008 fputs(dir->msg, bs);
1012 * Note, do not free the bootstrap yet -- it needs to be
1015 return bnet_fsend(dir, OKbootstrap);
1020 * Get backup level from Director
1023 static int level_cmd(JCR *jcr)
1025 BSOCK *dir = jcr->dir_bsock;
1026 POOLMEM *level, *buf = NULL;
1029 level = get_memory(dir->msglen+1);
1030 Dmsg1(110, "level_cmd: %s", dir->msg);
1031 if (sscanf(dir->msg, "level = %s ", level) != 1) {
1034 /* Base backup requested? */
1035 if (strcmp(level, "base") == 0) {
1036 jcr->JobLevel = L_BASE;
1037 /* Full backup requested? */
1038 } else if (strcmp(level, "full") == 0) {
1039 jcr->JobLevel = L_FULL;
1040 } else if (strcmp(level, "differential") == 0) {
1041 jcr->JobLevel = L_DIFFERENTIAL;
1044 } else if (strcmp(level, "incremental") == 0) {
1045 jcr->JobLevel = L_INCREMENTAL;
1049 * We get his UTC since time, then sync the clocks and correct it
1050 * to agree with our clock.
1052 } else if (strcmp(level, "since_utime") == 0) {
1053 buf = get_memory(dir->msglen+1);
1054 utime_t since_time, adj;
1055 btime_t his_time, bt_start, rt=0, bt_adj=0;
1056 if (jcr->JobLevel == L_NONE) {
1057 jcr->JobLevel = L_SINCE; /* if no other job level set, do it now */
1059 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
1060 buf, &mtime_only) != 2) {
1063 since_time = str_to_uint64(buf); /* this is the since time */
1064 Dmsg1(100, "since_time=%d\n", (int)since_time);
1065 char ed1[50], ed2[50];
1067 * Sync clocks by polling him for the time. We take
1068 * 10 samples of his time throwing out the first two.
1070 for (int i=0; i<10; i++) {
1071 bt_start = get_current_btime();
1072 bnet_sig(dir, BNET_BTIME); /* poll for time */
1073 if (bnet_recv(dir) <= 0) { /* get response */
1076 if (sscanf(dir->msg, "btime %s", buf) != 1) {
1079 if (i < 2) { /* toss first two results */
1082 his_time = str_to_uint64(buf);
1083 rt = get_current_btime() - bt_start; /* compute round trip time */
1084 Dmsg2(100, "Dirtime=%s FDtime=%s\n", edit_uint64(his_time, ed1),
1085 edit_uint64(bt_start, ed2));
1086 bt_adj += bt_start - his_time - rt/2;
1087 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1090 bt_adj = bt_adj / 8; /* compute average time */
1091 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1092 adj = btime_to_utime(bt_adj);
1093 since_time += adj; /* adjust for clock difference */
1095 Jmsg(jcr, M_INFO, 0, _("DIR and FD clocks differ by %d seconds, FD automatically adjusting.\n"), adj);
1097 bnet_sig(dir, BNET_EOD);
1099 Dmsg2(100, "adj = %d since_time=%d\n", (int)adj, (int)since_time);
1100 jcr->incremental = 1; /* set incremental or decremental backup */
1101 jcr->mtime = (time_t)since_time; /* set since time */
1103 Jmsg1(jcr, M_FATAL, 0, _("Unknown backup level: %s\n"), level);
1111 return bnet_fsend(dir, OKlevel);
1114 pm_strcpy(jcr->errmsg, dir->msg);
1115 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
1124 * Get session parameters from Director -- this is for a Restore command
1126 static int session_cmd(JCR *jcr)
1128 BSOCK *dir = jcr->dir_bsock;
1130 Dmsg1(100, "SessionCmd: %s", dir->msg);
1131 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
1132 &jcr->VolSessionId, &jcr->VolSessionTime,
1133 &jcr->StartFile, &jcr->EndFile,
1134 &jcr->StartBlock, &jcr->EndBlock) != 7) {
1135 pm_strcpy(jcr->errmsg, dir->msg);
1136 Jmsg(jcr, M_FATAL, 0, _("Bad session command: %s"), jcr->errmsg);
1140 return bnet_fsend(dir, OKsession);
1144 * Get address of storage daemon from Director
1147 static int storage_cmd(JCR *jcr)
1149 int stored_port; /* storage daemon port */
1150 int enable_ssl; /* enable ssl to sd */
1151 BSOCK *dir = jcr->dir_bsock;
1152 BSOCK *sd; /* storage daemon bsock */
1154 Dmsg1(100, "StorageCmd: %s", dir->msg);
1155 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
1156 pm_strcpy(jcr->errmsg, dir->msg);
1157 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
1160 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
1161 /* Open command communications with Storage daemon */
1162 /* Try to connect for 1 hour at 10 second intervals */
1163 sd = bnet_connect(jcr, 10, (int)me->SDConnectTimeout, _("Storage daemon"),
1164 jcr->stored_addr, NULL, stored_port, 1);
1166 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
1167 jcr->stored_addr, stored_port);
1168 Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
1169 jcr->stored_addr, stored_port);
1172 Dmsg0(110, "Connection OK to SD.\n");
1174 jcr->store_bsock = sd;
1176 bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
1177 if (!authenticate_storagedaemon(jcr)) {
1178 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
1181 Dmsg0(110, "Authenticated with SD.\n");
1183 /* Send OK to Director */
1184 return bnet_fsend(dir, OKstore);
1189 * Do a backup. For now, we handle only Full and Incremental.
1191 static int backup_cmd(JCR *jcr)
1193 BSOCK *dir = jcr->dir_bsock;
1194 BSOCK *sd = jcr->store_bsock;
1197 char ed1[50], ed2[50];
1200 // capture state here, if client is backed up by multiple directors
1201 // and one enables vss and the other does not then enable_vss can change
1202 // between here and where its evaluated after the job completes.
1203 bool bDoVSS = false;
1205 bDoVSS = g_pVSSClient && enable_vss;
1207 /* Run only one at a time */
1211 set_jcr_job_status(jcr, JS_Blocked);
1212 jcr->JobType = JT_BACKUP;
1213 Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
1216 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
1220 bnet_fsend(dir, OKbackup);
1221 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1224 * Send Append Open Session to Storage daemon
1226 bnet_fsend(sd, append_open);
1227 Dmsg1(110, ">stored: %s", sd->msg);
1229 * Expect to receive back the Ticket number
1231 if (bget_msg(sd) >= 0) {
1232 Dmsg1(110, "<stored: %s", sd->msg);
1233 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1234 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
1237 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
1239 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
1244 * Send Append data command to Storage daemon
1246 bnet_fsend(sd, append_data, jcr->Ticket);
1247 Dmsg1(110, ">stored: %s", sd->msg);
1250 * Expect to get OK data
1252 Dmsg1(110, "<stored: %s", sd->msg);
1253 if (!response(jcr, sd, OK_data, "Append Data")) {
1257 generate_daemon_event(jcr, "JobStart");
1260 /* START VSS ON WIN 32 */
1262 if (g_pVSSClient->InitializeForBackup()) {
1263 /* tell vss which drives to snapshot */
1264 char szWinDriveLetters[27];
1265 if (get_win32_driveletters(jcr->ff, szWinDriveLetters)) {
1266 Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
1267 if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {
1268 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed.\n"));
1271 /* tell user if snapshot creation of a specific drive failed */
1273 for (i=0; i<strlen (szWinDriveLetters); i++) {
1274 if (islower(szWinDriveLetters[i])) {
1275 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed. VSS support is disabled on this drive.\n"), szWinDriveLetters[i]);
1279 /* inform user about writer states */
1280 for (i=0; i<g_pVSSClient->GetWriterCount(); i++)
1281 if (g_pVSSClient->GetWriterState(i) < 1) {
1282 Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PrepareForBackup): %s\n"), g_pVSSClient->GetWriterInfo(i));
1287 Jmsg(jcr, M_INFO, 0, _("No drive letters found for generating VSS snapshots.\n"));
1291 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled. ERR=%s\n"), be.strerror());
1297 * Send Files to Storage daemon
1299 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
1300 if (!blast_data_to_storage_daemon(jcr, NULL)) {
1301 set_jcr_job_status(jcr, JS_ErrorTerminated);
1302 bnet_suppress_error_messages(sd, 1);
1303 bget_msg(sd); /* Read final response from append_data */
1304 Dmsg0(110, "Error in blast_data.\n");
1306 set_jcr_job_status(jcr, JS_Terminated);
1307 if (jcr->JobStatus != JS_Terminated) {
1308 bnet_suppress_error_messages(sd, 1);
1309 goto cleanup; /* bail out now */
1312 * Expect to get response to append_data from Storage daemon
1314 if (!response(jcr, sd, OK_append, "Append Data")) {
1315 set_jcr_job_status(jcr, JS_ErrorTerminated);
1320 * Send Append End Data to Storage daemon
1322 bnet_fsend(sd, append_end, jcr->Ticket);
1324 if (!response(jcr, sd, OK_end, "Append End")) {
1325 set_jcr_job_status(jcr, JS_ErrorTerminated);
1330 * Send Append Close to Storage daemon
1332 bnet_fsend(sd, append_close, jcr->Ticket);
1333 while (bget_msg(sd) >= 0) { /* stop on signal or error */
1334 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
1336 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
1340 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
1343 if (SDJobStatus != JS_Terminated) {
1344 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
1351 /* STOP VSS ON WIN 32 */
1352 /* tell vss to close the backup session */
1354 if (g_pVSSClient->CloseBackup()) {
1355 /* inform user about writer states */
1356 for (size_t i=0; i<g_pVSSClient->GetWriterCount(); i++) {
1357 int msg_type = M_INFO;
1358 if (g_pVSSClient->GetWriterState(i) < 1) {
1359 msg_type = M_WARNING;
1362 Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
1369 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1370 edit_uint64(jcr->ReadBytes, ed1),
1371 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
1372 Dmsg1(110, "End FD msg: %s\n", dir->msg);
1374 return 0; /* return and stop command loop */
1378 * Do a Verify for Director
1381 static int verify_cmd(JCR *jcr)
1383 BSOCK *dir = jcr->dir_bsock;
1384 BSOCK *sd = jcr->store_bsock;
1385 char level[100], ed1[50], ed2[50];
1387 jcr->JobType = JT_VERIFY;
1388 if (sscanf(dir->msg, verifycmd, level) != 1) {
1389 bnet_fsend(dir, _("2994 Bad verify command: %s\n"), dir->msg);
1393 if (strcasecmp(level, "init") == 0) {
1394 jcr->JobLevel = L_VERIFY_INIT;
1395 } else if (strcasecmp(level, "catalog") == 0){
1396 jcr->JobLevel = L_VERIFY_CATALOG;
1397 } else if (strcasecmp(level, "volume") == 0){
1398 jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
1399 } else if (strcasecmp(level, "data") == 0){
1400 jcr->JobLevel = L_VERIFY_DATA;
1401 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
1402 jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
1404 bnet_fsend(dir, _("2994 Bad verify level: %s\n"), dir->msg);
1408 bnet_fsend(dir, OKverify);
1410 generate_daemon_event(jcr, "JobStart");
1412 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1414 switch (jcr->JobLevel) {
1416 case L_VERIFY_CATALOG:
1419 case L_VERIFY_VOLUME_TO_CATALOG:
1420 if (!open_sd_read_session(jcr)) {
1423 start_dir_heartbeat(jcr);
1424 do_verify_volume(jcr);
1425 stop_dir_heartbeat(jcr);
1427 * Send Close session command to Storage daemon
1429 bnet_fsend(sd, read_close, jcr->Ticket);
1430 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1432 /* ****FIXME**** check response */
1433 bget_msg(sd); /* get OK */
1435 /* Inform Storage daemon that we are done */
1436 bnet_sig(sd, BNET_TERMINATE);
1439 case L_VERIFY_DISK_TO_CATALOG:
1443 bnet_fsend(dir, _("2994 Bad verify level: %s\n"), dir->msg);
1447 bnet_sig(dir, BNET_EOD);
1449 /* Send termination status back to Dir */
1450 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1451 edit_uint64(jcr->ReadBytes, ed1),
1452 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
1454 /* Inform Director that we are done */
1455 bnet_sig(dir, BNET_TERMINATE);
1456 return 0; /* return and terminate command loop */
1460 * Do a Restore for Director
1463 static int restore_cmd(JCR *jcr)
1465 BSOCK *dir = jcr->dir_bsock;
1466 BSOCK *sd = jcr->store_bsock;
1470 char ed1[50], ed2[50];
1473 * Scan WHERE (base directory for restore) from command
1475 Dmsg0(150, "restore command\n");
1476 /* Pickup where string */
1477 where = get_memory(dir->msglen+1);
1480 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, where) != 3) {
1481 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
1482 pm_strcpy(jcr->errmsg, dir->msg);
1483 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
1488 /* Turn / into nothing */
1489 if (where[0] == '/' && where[1] == 0) {
1493 Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
1494 unbash_spaces(where);
1495 jcr->where = bstrdup(where);
1496 free_pool_memory(where);
1497 jcr->replace = replace;
1498 jcr->prefix_links = prefix_links;
1500 bnet_fsend(dir, OKrestore);
1501 Dmsg1(110, "bfiled>dird: %s", dir->msg);
1503 jcr->JobType = JT_RESTORE;
1505 set_jcr_job_status(jcr, JS_Blocked);
1507 if (!open_sd_read_session(jcr)) {
1508 set_jcr_job_status(jcr, JS_ErrorTerminated);
1512 set_jcr_job_status(jcr, JS_Running);
1515 * Do restore of files and data
1517 start_dir_heartbeat(jcr);
1518 generate_daemon_event(jcr, "JobStart");
1520 stop_dir_heartbeat(jcr);
1522 set_jcr_job_status(jcr, JS_Terminated);
1523 if (jcr->JobStatus != JS_Terminated) {
1524 bnet_suppress_error_messages(sd, 1);
1528 * Send Close session command to Storage daemon
1530 bnet_fsend(sd, read_close, jcr->Ticket);
1531 Dmsg1(130, "bfiled>stored: %s", sd->msg);
1533 bget_msg(sd); /* get OK */
1535 /* Inform Storage daemon that we are done */
1536 bnet_sig(sd, BNET_TERMINATE);
1541 set_jcr_job_status(jcr, JS_ErrorTerminated);
1543 /* Send termination status back to Dir */
1544 bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles,
1545 edit_uint64(jcr->ReadBytes, ed1),
1546 edit_uint64(jcr->JobBytes, ed2), jcr->Errors);
1548 /* Inform Director that we are done */
1549 bnet_sig(dir, BNET_TERMINATE);
1551 Dmsg0(130, "Done in job.c\n");
1552 return 0; /* return and terminate command loop */
1555 static int open_sd_read_session(JCR *jcr)
1557 BSOCK *sd = jcr->store_bsock;
1560 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
1563 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
1564 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
1565 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
1567 * Open Read Session with Storage daemon
1569 bnet_fsend(sd, read_open, "DummyVolume",
1570 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
1571 jcr->StartBlock, jcr->EndBlock);
1572 Dmsg1(110, ">stored: %s", sd->msg);
1577 if (bget_msg(sd) >= 0) {
1578 Dmsg1(110, "bfiled<stored: %s", sd->msg);
1579 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1580 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
1583 Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
1585 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
1589 if (!send_bootstrap_file(jcr)) {
1594 * Start read of data with Storage daemon
1596 bnet_fsend(sd, read_data, jcr->Ticket);
1597 Dmsg1(110, ">stored: %s", sd->msg);
1602 if (!response(jcr, sd, OK_data, "Read Data")) {
1609 * Destroy the Job Control Record and associated
1610 * resources (sockets).
1612 static void filed_free_jcr(JCR *jcr)
1614 if (jcr->store_bsock) {
1615 bnet_close(jcr->store_bsock);
1617 free_bootstrap(jcr);
1618 if (jcr->last_fname) {
1619 free_pool_memory(jcr->last_fname);
1621 if (jcr->RunAfterJob) {
1622 free_pool_memory(jcr->RunAfterJob);
1630 * Get response from Storage daemon to a command we
1631 * sent. Check that the response is OK.
1633 * Returns: 0 on failure
1636 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
1641 if (bget_msg(sd) > 0) {
1642 Dmsg0(110, sd->msg);
1643 if (strcmp(sd->msg, resp) == 0) {
1647 if (job_canceled(jcr)) {
1648 return 0; /* if canceled avoid useless error messages */
1650 if (is_bnet_error(sd)) {
1651 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
1652 cmd, bnet_strerror(sd));
1654 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
1655 cmd, resp, sd->msg);
1660 static int send_bootstrap_file(JCR *jcr)
1664 BSOCK *sd = jcr->store_bsock;
1665 const char *bootstrap = "bootstrap\n";
1668 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
1669 if (!jcr->RestoreBootstrap) {
1672 bs = fopen(jcr->RestoreBootstrap, "r");
1675 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
1676 jcr->RestoreBootstrap, be.strerror());
1677 set_jcr_job_status(jcr, JS_ErrorTerminated);
1680 sd->msglen = pm_strcpy(sd->msg, bootstrap);
1682 while (fgets(buf, sizeof(buf), bs)) {
1683 sd->msglen = Mmsg(sd->msg, "%s", buf);
1686 bnet_sig(sd, BNET_EOD);
1688 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
1689 set_jcr_job_status(jcr, JS_ErrorTerminated);
1695 free_bootstrap(jcr);