2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2009 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Bacula File Daemon Job processing
31 * Kern Sibbald, October MM
40 #if defined(WIN32_VSS)
43 static pthread_mutex_t vss_mutex = PTHREAD_MUTEX_INITIALIZER;
44 static int enable_vss = 0;
47 extern CLIENT *me; /* our client resource */
49 /* Imported functions */
50 extern int status_cmd(JCR *jcr);
51 extern int qstatus_cmd(JCR *jcr);
52 extern int accurate_cmd(JCR *jcr);
54 /* Forward referenced functions */
55 static int backup_cmd(JCR *jcr);
56 static int bootstrap_cmd(JCR *jcr);
57 static int cancel_cmd(JCR *jcr);
58 static int setdebug_cmd(JCR *jcr);
59 static int estimate_cmd(JCR *jcr);
60 static int hello_cmd(JCR *jcr);
61 static int job_cmd(JCR *jcr);
62 static int fileset_cmd(JCR *jcr);
63 static int level_cmd(JCR *jcr);
64 static int verify_cmd(JCR *jcr);
65 static int restore_cmd(JCR *jcr);
66 static int storage_cmd(JCR *jcr);
67 static int session_cmd(JCR *jcr);
68 static int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd);
69 static void filed_free_jcr(JCR *jcr);
70 static int open_sd_read_session(JCR *jcr);
71 static int send_bootstrap_file(JCR *jcr);
72 static int runscript_cmd(JCR *jcr);
73 static int runbefore_cmd(JCR *jcr);
74 static int runafter_cmd(JCR *jcr);
75 static int runbeforenow_cmd(JCR *jcr);
76 static void set_options(findFOPTS *fo, const char *opts);
79 /* Exported functions */
84 int monitoraccess; /* specify if monitors have access to this function */
88 * The following are the recognized commands from the Director.
90 static struct s_cmds cmds[] = {
91 {"backup", backup_cmd, 0},
92 {"cancel", cancel_cmd, 0},
93 {"setdebug=", setdebug_cmd, 0},
94 {"estimate", estimate_cmd, 0},
95 {"Hello", hello_cmd, 1},
96 {"fileset", fileset_cmd, 0},
97 {"JobId=", job_cmd, 0},
98 {"level = ", level_cmd, 0},
99 {"restore", restore_cmd, 0},
100 {"session", session_cmd, 0},
101 {"status", status_cmd, 1},
102 {".status", qstatus_cmd, 1},
103 {"storage ", storage_cmd, 0},
104 {"verify", verify_cmd, 0},
105 {"bootstrap", bootstrap_cmd, 0},
106 {"RunBeforeNow", runbeforenow_cmd, 0},
107 {"RunBeforeJob", runbefore_cmd, 0},
108 {"RunAfterJob", runafter_cmd, 0},
109 {"Run", runscript_cmd, 0},
110 {"accurate", accurate_cmd, 0},
111 {NULL, NULL} /* list terminator */
114 /* Commands received from director that need scanning */
115 static char jobcmd[] = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
116 static char storaddr[] = "storage address=%s port=%d ssl=%d";
117 static char sessioncmd[] = "session %127s %ld %ld %ld %ld %ld %ld\n";
118 static char restorecmd[] = "restore replace=%c prelinks=%d where=%s\n";
119 static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
120 static char restorecmdR[] = "restore replace=%c prelinks=%d regexwhere=%s\n";
121 static char verifycmd[] = "verify level=%30s";
122 static char estimatecmd[] = "estimate listing=%d";
123 static char runbefore[] = "RunBeforeJob %s";
124 static char runafter[] = "RunAfterJob %s";
125 static char runscript[] = "Run OnSuccess=%d OnFailure=%d AbortOnError=%d When=%d Command=%s";
127 /* Responses sent to Director */
128 static char errmsg[] = "2999 Invalid command\n";
129 static char no_auth[] = "2998 No Authorization\n";
130 static char invalid_cmd[] = "2997 Invalid command for a Director with Monitor directive enabled.\n";
131 static char OKinc[] = "2000 OK include\n";
132 static char OKest[] = "2000 OK estimate files=%u bytes=%s\n";
133 static char OKlevel[] = "2000 OK level\n";
134 static char OKbackup[] = "2000 OK backup\n";
135 static char OKbootstrap[] = "2000 OK bootstrap\n";
136 static char OKverify[] = "2000 OK verify\n";
137 static char OKrestore[] = "2000 OK restore\n";
138 static char OKsession[] = "2000 OK session\n";
139 static char OKstore[] = "2000 OK storage\n";
140 static char OKjob[] = "2000 OK Job %s (%s) %s,%s,%s";
141 static char OKsetdebug[] = "2000 OK setdebug=%d\n";
142 static char BADjob[] = "2901 Bad Job\n";
143 static char EndJob[] = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s"
144 " JobBytes=%s Errors=%u VSS=%d Encrypt=%d\n";
145 static char OKRunBefore[] = "2000 OK RunBefore\n";
146 static char OKRunBeforeNow[] = "2000 OK RunBeforeNow\n";
147 static char OKRunAfter[] = "2000 OK RunAfter\n";
148 static char OKRunScript[] = "2000 OK RunScript\n";
149 static char BADcmd[] = "2902 Bad %s\n";
152 /* Responses received from Storage Daemon */
153 static char OK_end[] = "3000 OK end\n";
154 static char OK_close[] = "3000 OK close Status = %d\n";
155 static char OK_open[] = "3000 OK open ticket = %d\n";
156 static char OK_data[] = "3000 OK data\n";
157 static char OK_append[] = "3000 OK append data\n";
158 static char OKSDbootstrap[]= "3000 OK bootstrap\n";
161 /* Commands sent to Storage Daemon */
162 static char append_open[] = "append open session\n";
163 static char append_data[] = "append data %d\n";
164 static char append_end[] = "append end session %d\n";
165 static char append_close[] = "append close session %d\n";
166 static char read_open[] = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
167 static char read_data[] = "read data %d\n";
168 static char read_close[] = "read close session %d\n";
171 * Accept requests from a Director
173 * NOTE! We are running as a separate thread
175 * Send output one line
176 * at a time followed by a zero length transmission.
178 * Return when the connection is terminated or there
181 * Basic task here is:
182 * Authenticate Director (during Hello command).
183 * Accept commands one at a time from the Director
186 * Concerning ClientRunBefore/After, the sequence of events
187 * is rather critical. If they are not done in the right
188 * order one can easily get FD->SD timeouts if the script
191 * The current sequence of events is:
192 * 1. Dir starts job with FD
193 * 2. Dir connects to SD
194 * 3. Dir connects to FD
195 * 4. FD connects to SD
196 * 5. FD gets/runs ClientRunBeforeJob and sends ClientRunAfterJob
197 * 6. Dir sends include/exclude
198 * 7. FD sends data to SD
199 * 8. SD/FD disconnects while SD despools data and attributes (optionnal)
200 * 9. FD runs ClientRunAfterJob
203 void *handle_client_request(void *dirp)
208 BSOCK *dir = (BSOCK *)dirp;
209 const char jobname[12] = "*Director*";
211 jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
212 jcr->dir_bsock = dir;
213 jcr->ff = init_find_files();
214 jcr->start_time = time(NULL);
215 jcr->RunScripts = New(alist(10, not_owned_by_alist));
216 jcr->last_fname = get_pool_memory(PM_FNAME);
217 jcr->last_fname[0] = 0;
218 jcr->client_name = get_memory(strlen(my_name) + 1);
219 pm_strcpy(jcr->client_name, my_name);
220 bstrncpy(jcr->Job, jobname, sizeof(jobname)); /* dummy */
221 jcr->crypto.pki_sign = me->pki_sign;
222 jcr->crypto.pki_encrypt = me->pki_encrypt;
223 jcr->crypto.pki_keypair = me->pki_keypair;
224 jcr->crypto.pki_signers = me->pki_signers;
225 jcr->crypto.pki_recipients = me->pki_recipients;
227 enable_backup_privileges(NULL, 1 /* ignore_errors */);
229 /**********FIXME******* add command handler error code */
231 for (quit=false; !quit;) {
233 if (dir->recv() < 0) {
234 break; /* connection terminated */
236 dir->msg[dir->msglen] = 0;
237 Dmsg1(100, "<dird: %s", dir->msg);
239 for (i=0; cmds[i].cmd; i++) {
240 if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
241 found = true; /* indicate command found */
242 if (!jcr->authenticated && cmds[i].func != hello_cmd) {
244 dir->signal(BNET_EOD);
247 if ((jcr->authenticated) && (!cmds[i].monitoraccess) && (jcr->director->monitor)) {
248 Dmsg1(100, "Command \"%s\" is invalid.\n", cmds[i].cmd);
249 dir->fsend(invalid_cmd);
250 dir->signal(BNET_EOD);
253 Dmsg1(100, "Executing %s command.\n", cmds[i].cmd);
254 if (!cmds[i].func(jcr)) { /* do command */
255 quit = true; /* error or fully terminated, get out */
256 Dmsg1(100, "Quit command loop. Canceled=%d\n", job_canceled(jcr));
261 if (!found) { /* command not found */
268 /* Inform Storage daemon that we are done */
269 if (jcr->store_bsock) {
270 jcr->store_bsock->signal(BNET_TERMINATE);
273 /* Run the after job */
274 run_scripts(jcr, jcr->RunScripts, "ClientAfterJob");
276 if (jcr->JobId) { /* send EndJob if running a job */
277 char ed1[50], ed2[50];
278 /* Send termination status back to Dir */
279 dir->fsend(EndJob, jcr->JobStatus, jcr->JobFiles,
280 edit_uint64(jcr->ReadBytes, ed1),
281 edit_uint64(jcr->JobBytes, ed2), jcr->JobErrors, jcr->VSS,
282 jcr->crypto.pki_encrypt);
283 Dmsg1(110, "End FD msg: %s\n", dir->msg);
286 generate_daemon_event(jcr, "JobEnd");
287 generate_plugin_event(jcr, bEventJobEnd);
289 dequeue_messages(jcr); /* send any queued messages */
291 /* Inform Director that we are done */
292 dir->signal(BNET_TERMINATE);
294 free_plugins(jcr); /* release instantiated plugins */
296 /* Clean up fileset */
297 FF_PKT *ff = jcr->ff;
298 findFILESET *fileset = ff->fileset;
301 /* Delete FileSet Include lists */
302 for (i=0; i<fileset->include_list.size(); i++) {
303 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
304 for (j=0; j<incexe->opts_list.size(); j++) {
305 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
306 for (k=0; k<fo->regex.size(); k++) {
307 regfree((regex_t *)fo->regex.get(k));
309 for (k=0; k<fo->regexdir.size(); k++) {
310 regfree((regex_t *)fo->regexdir.get(k));
312 for (k=0; k<fo->regexfile.size(); k++) {
313 regfree((regex_t *)fo->regexfile.get(k));
316 fo->regexdir.destroy();
317 fo->regexfile.destroy();
319 fo->wilddir.destroy();
320 fo->wildfile.destroy();
321 fo->wildbase.destroy();
323 fo->fstype.destroy();
324 fo->drivetype.destroy();
325 if (fo->ignoredir != NULL) {
329 incexe->opts_list.destroy();
330 incexe->name_list.destroy();
331 incexe->plugin_list.destroy();
333 fileset->include_list.destroy();
335 /* Delete FileSet Exclude lists */
336 for (i=0; i<fileset->exclude_list.size(); i++) {
337 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
338 for (j=0; j<incexe->opts_list.size(); j++) {
339 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
341 fo->regexdir.destroy();
342 fo->regexfile.destroy();
344 fo->wilddir.destroy();
345 fo->wildfile.destroy();
346 fo->wildbase.destroy();
348 fo->fstype.destroy();
349 fo->drivetype.destroy();
351 incexe->opts_list.destroy();
352 incexe->name_list.destroy();
353 incexe->plugin_list.destroy();
355 fileset->exclude_list.destroy();
359 Dmsg0(100, "Calling term_find_files\n");
360 term_find_files(jcr->ff);
362 Dmsg0(100, "Done with term_find_files\n");
363 free_jcr(jcr); /* destroy JCR record */
364 Dmsg0(100, "Done with free_jcr\n");
370 * Hello from Director he must identify himself and provide his
373 static int hello_cmd(JCR *jcr)
375 Dmsg0(120, "Calling Authenticate\n");
376 if (!authenticate_director(jcr)) {
379 Dmsg0(120, "OK Authenticate\n");
380 jcr->authenticated = true;
387 static int cancel_cmd(JCR *jcr)
389 BSOCK *dir = jcr->dir_bsock;
390 char Job[MAX_NAME_LENGTH];
393 if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
394 if (!(cjcr=get_jcr_by_full_name(Job))) {
395 dir->fsend(_("2901 Job %s not found.\n"), Job);
397 if (cjcr->store_bsock) {
398 cjcr->store_bsock->set_timed_out();
399 cjcr->store_bsock->set_terminated();
400 pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
402 generate_plugin_event(cjcr, bEventCancelCommand, NULL);
403 set_jcr_job_status(cjcr, JS_Canceled);
405 dir->fsend(_("2001 Job %s marked to be canceled.\n"), Job);
408 dir->fsend(_("2902 Error scanning cancel command.\n"));
410 dir->signal(BNET_EOD);
416 * Set debug level as requested by the Director
419 static int setdebug_cmd(JCR *jcr)
421 BSOCK *dir = jcr->dir_bsock;
422 int level, trace_flag;
424 Dmsg1(110, "setdebug_cmd: %s", dir->msg);
425 if (sscanf(dir->msg, "setdebug=%d trace=%d", &level, &trace_flag) != 2 || level < 0) {
426 pm_strcpy(jcr->errmsg, dir->msg);
427 dir->fsend(_("2991 Bad setdebug command: %s\n"), jcr->errmsg);
431 set_trace(trace_flag);
432 return dir->fsend(OKsetdebug, level);
436 static int estimate_cmd(JCR *jcr)
438 BSOCK *dir = jcr->dir_bsock;
441 if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
442 pm_strcpy(jcr->errmsg, dir->msg);
443 Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
444 dir->fsend(_("2992 Bad estimate command.\n"));
448 dir->fsend(OKest, jcr->num_files_examined,
449 edit_uint64_with_commas(jcr->JobBytes, ed2));
450 dir->signal(BNET_EOD);
455 * Get JobId and Storage Daemon Authorization key from Director
457 static int job_cmd(JCR *jcr)
459 BSOCK *dir = jcr->dir_bsock;
460 POOLMEM *sd_auth_key;
462 sd_auth_key = get_memory(dir->msglen);
463 if (sscanf(dir->msg, jobcmd, &jcr->JobId, jcr->Job,
464 &jcr->VolSessionId, &jcr->VolSessionTime,
466 pm_strcpy(jcr->errmsg, dir->msg);
467 Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
469 free_pool_memory(sd_auth_key);
472 jcr->sd_auth_key = bstrdup(sd_auth_key);
473 free_pool_memory(sd_auth_key);
474 Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
475 Mmsg(jcr->errmsg, "JobId=%d Job=%s", jcr->JobId, jcr->Job);
476 new_plugins(jcr); /* instantiate plugins for this jcr */
477 generate_plugin_event(jcr, bEventJobStart, (void *)jcr->errmsg);
478 return dir->fsend(OKjob, VERSION, LSMDATE, HOST_OS, DISTNAME, DISTVER);
481 static int runbefore_cmd(JCR *jcr)
484 BSOCK *dir = jcr->dir_bsock;
485 POOLMEM *cmd = get_memory(dir->msglen+1);
488 Dmsg1(100, "runbefore_cmd: %s", dir->msg);
489 if (sscanf(dir->msg, runbefore, cmd) != 1) {
490 pm_strcpy(jcr->errmsg, dir->msg);
491 Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
492 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
498 /* Run the command now */
499 script = new_runscript();
500 script->set_command(cmd);
501 script->when = SCRIPT_Before;
502 ok = script->run(jcr, "ClientRunBeforeJob");
503 free_runscript(script);
507 dir->fsend(OKRunBefore);
510 dir->fsend(_("2905 Bad RunBeforeJob command.\n"));
515 static int runbeforenow_cmd(JCR *jcr)
517 BSOCK *dir = jcr->dir_bsock;
519 run_scripts(jcr, jcr->RunScripts, "ClientBeforeJob");
520 if (job_canceled(jcr)) {
521 dir->fsend(_("2905 Bad RunBeforeNow command.\n"));
522 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: FAILED\n");
525 dir->fsend(OKRunBeforeNow);
526 Dmsg0(100, "Back from run_scripts ClientBeforeJob now: OK\n");
531 static int runafter_cmd(JCR *jcr)
533 BSOCK *dir = jcr->dir_bsock;
534 POOLMEM *msg = get_memory(dir->msglen+1);
537 Dmsg1(100, "runafter_cmd: %s", dir->msg);
538 if (sscanf(dir->msg, runafter, msg) != 1) {
539 pm_strcpy(jcr->errmsg, dir->msg);
540 Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
541 dir->fsend(_("2905 Bad RunAfterJob command.\n"));
547 cmd = new_runscript();
548 cmd->set_command(msg);
549 cmd->on_success = true;
550 cmd->on_failure = false;
551 cmd->when = SCRIPT_After;
553 jcr->RunScripts->append(cmd);
555 free_pool_memory(msg);
556 return dir->fsend(OKRunAfter);
559 static int runscript_cmd(JCR *jcr)
561 BSOCK *dir = jcr->dir_bsock;
562 POOLMEM *msg = get_memory(dir->msglen+1);
563 int on_success, on_failure, fail_on_error;
565 RUNSCRIPT *cmd = new_runscript() ;
567 Dmsg1(100, "runscript_cmd: '%s'\n", dir->msg);
568 /* Note, we cannot sscanf into bools */
569 if (sscanf(dir->msg, runscript, &on_success,
574 pm_strcpy(jcr->errmsg, dir->msg);
575 Jmsg1(jcr, M_FATAL, 0, _("Bad RunScript command: %s\n"), jcr->errmsg);
576 dir->fsend(_("2905 Bad RunScript command.\n"));
581 cmd->on_success = on_success;
582 cmd->on_failure = on_failure;
583 cmd->fail_on_error = fail_on_error;
586 cmd->set_command(msg);
588 jcr->RunScripts->append(cmd);
590 free_pool_memory(msg);
591 return dir->fsend(OKRunScript);
595 static bool init_fileset(JCR *jcr)
598 findFILESET *fileset;
607 fileset = (findFILESET *)malloc(sizeof(findFILESET));
608 memset(fileset, 0, sizeof(findFILESET));
609 ff->fileset = fileset;
610 fileset->state = state_none;
611 fileset->include_list.init(1, true);
612 fileset->exclude_list.init(1, true);
616 static findFOPTS *start_options(FF_PKT *ff)
618 int state = ff->fileset->state;
619 findINCEXE *incexe = ff->fileset->incexe;
621 if (state != state_options) {
622 ff->fileset->state = state_options;
623 findFOPTS *fo = (findFOPTS *)malloc(sizeof(findFOPTS));
624 memset(fo, 0, sizeof(findFOPTS));
625 fo->regex.init(1, true);
626 fo->regexdir.init(1, true);
627 fo->regexfile.init(1, true);
628 fo->wild.init(1, true);
629 fo->wilddir.init(1, true);
630 fo->wildfile.init(1, true);
631 fo->wildbase.init(1, true);
632 fo->base.init(1, true);
633 fo->fstype.init(1, true);
634 fo->drivetype.init(1, true);
635 incexe->current_opts = fo;
636 incexe->opts_list.append(fo);
638 return incexe->current_opts;
643 * Add fname to include/exclude fileset list. First check for
644 * | and < and if necessary perform command.
646 static void add_file_to_fileset(JCR *jcr, const char *fname, findFILESET *fileset,
661 p++; /* skip over | */
662 fn = get_pool_memory(PM_FNAME);
663 fn = edit_job_codes(jcr, fn, p, "");
664 bpipe = open_bpipe(fn, 0, "r");
667 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
669 free_pool_memory(fn);
672 free_pool_memory(fn);
673 while (fgets(buf, sizeof(buf), bpipe->rfd)) {
674 strip_trailing_junk(buf);
676 fileset->incexe->name_list.append(new_dlistString(buf));
678 fileset->incexe->plugin_list.append(new_dlistString(buf));
681 if ((stat=close_bpipe(bpipe)) != 0) {
683 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. stat=%d: ERR=%s\n"),
684 p, be.code(stat), be.bstrerror(stat));
689 Dmsg1(100, "Doing < of '%s' include on client.\n", p + 1);
690 p++; /* skip over < */
691 if ((ffd = fopen(p, "rb")) == NULL) {
693 Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"),
697 while (fgets(buf, sizeof(buf), ffd)) {
698 strip_trailing_junk(buf);
699 Dmsg1(100, "%s\n", buf);
701 fileset->incexe->name_list.append(new_dlistString(buf));
703 fileset->incexe->plugin_list.append(new_dlistString(buf));
710 fileset->incexe->name_list.append(new_dlistString(fname));
712 if (me->plugin_directory) {
713 fileset->incexe->plugin_list.append(new_dlistString(fname));
715 Jmsg(jcr, M_FATAL, 0, _("Plugin Directory not defined. Cannot use plugin: \"%\"\n"),
724 static void add_fileset(JCR *jcr, const char *item)
726 FF_PKT *ff = jcr->ff;
727 findFILESET *fileset = ff->fileset;
728 int state = fileset->state;
729 findFOPTS *current_opts;
731 /* Get code, optional subcode, and position item past the dividing space */
732 Dmsg1(100, "%s\n", item);
737 int subcode = ' '; /* A space is always a valid subcode */
738 if (item[0] != '\0' && item[0] != ' ') {
746 /* Skip all lines we receive after an error */
747 if (state == state_error) {
748 Dmsg0(100, "State=error return\n");
753 * The switch tests the code for validity.
754 * The subcode is always good if it is a space, otherwise we must confirm.
755 * We set state to state_error first assuming the subcode is invalid,
756 * requiring state to be set in cases below that handle subcodes.
758 if (subcode != ' ') {
760 Dmsg0(100, "Set state=error or double code.\n");
765 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
766 memset(fileset->incexe, 0, sizeof(findINCEXE));
767 fileset->incexe->opts_list.init(1, true);
768 fileset->incexe->name_list.init(); /* for dlist; was 1,true for alist */
769 fileset->incexe->plugin_list.init();
770 fileset->include_list.append(fileset->incexe);
774 fileset->incexe = (findINCEXE *)malloc(sizeof(findINCEXE));
775 memset(fileset->incexe, 0, sizeof(findINCEXE));
776 fileset->incexe->opts_list.init(1, true);
777 fileset->incexe->name_list.init();
778 fileset->incexe->plugin_list.init();
779 fileset->exclude_list.append(fileset->incexe);
785 /* File item to include or exclude list */
786 state = state_include;
787 add_file_to_fileset(jcr, item, fileset, true);
790 /* Plugin item to include list */
791 state = state_include;
792 add_file_to_fileset(jcr, item, fileset, false);
795 current_opts = start_options(ff);
799 preg = (regex_t *)malloc(sizeof(regex_t));
800 if (current_opts->flags & FO_IGNORECASE) {
801 rc = regcomp(preg, item, REG_EXTENDED|REG_ICASE);
803 rc = regcomp(preg, item, REG_EXTENDED);
806 regerror(rc, preg, prbuf, sizeof(prbuf));
809 Jmsg(jcr, M_FATAL, 0, _("REGEX %s compile error. ERR=%s\n"), item, prbuf);
813 state = state_options;
814 if (subcode == ' ') {
815 current_opts->regex.append(preg);
816 } else if (subcode == 'D') {
817 current_opts->regexdir.append(preg);
818 } else if (subcode == 'F') {
819 current_opts->regexfile.append(preg);
825 current_opts = start_options(ff);
826 current_opts->base.append(bstrdup(item));
827 state = state_options;
830 current_opts = start_options(ff);
831 state = state_options;
832 if (subcode == ' ') {
833 current_opts->fstype.append(bstrdup(item));
834 } else if (subcode == 'D') {
835 current_opts->drivetype.append(bstrdup(item));
841 current_opts = start_options(ff);
842 state = state_options;
843 if (subcode == ' ') {
844 current_opts->wild.append(bstrdup(item));
845 } else if (subcode == 'D') {
846 current_opts->wilddir.append(bstrdup(item));
847 } else if (subcode == 'F') {
848 current_opts->wildfile.append(bstrdup(item));
849 } else if (subcode == 'B') {
850 current_opts->wildbase.append(bstrdup(item));
856 current_opts = start_options(ff);
857 set_options(current_opts, item);
858 state = state_options;
861 current_opts = start_options(ff);
862 current_opts->ignoredir = bstrdup(item);
863 state = state_options;
866 current_opts = start_options(ff);
867 // current_opts->reader = bstrdup(item);
868 state = state_options;
871 current_opts = start_options(ff);
872 // current_opts->writer = bstrdup(item);
873 state = state_options;
876 Jmsg(jcr, M_FATAL, 0, _("Invalid FileSet command: %s\n"), item);
880 ff->fileset->state = state;
883 static bool term_fileset(JCR *jcr)
885 FF_PKT *ff = jcr->ff;
887 #ifdef xxx_DEBUG_CODE
888 findFILESET *fileset = ff->fileset;
891 for (i=0; i<fileset->include_list.size(); i++) {
892 findINCEXE *incexe = (findINCEXE *)fileset->include_list.get(i);
894 for (j=0; j<incexe->opts_list.size(); j++) {
895 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
896 for (k=0; k<fo->regex.size(); k++) {
897 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
899 for (k=0; k<fo->regexdir.size(); k++) {
900 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
902 for (k=0; k<fo->regexfile.size(); k++) {
903 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
905 for (k=0; k<fo->wild.size(); k++) {
906 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
908 for (k=0; k<fo->wilddir.size(); k++) {
909 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
911 for (k=0; k<fo->wildfile.size(); k++) {
912 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
914 for (k=0; k<fo->wildbase.size(); k++) {
915 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
917 for (k=0; k<fo->base.size(); k++) {
918 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
920 for (k=0; k<fo->fstype.size(); k++) {
921 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
923 for (k=0; k<fo->drivetype.size(); k++) {
924 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
927 Dmsg1(400, "Z %s\n", fo->ignoredir);
931 foreach_dlist(node, &incexe->name_list) {
932 Dmsg1(400, "F %s\n", node->c_str());
934 foreach_dlist(node, &incexe->plugin_list) {
935 Dmsg1(400, "P %s\n", node->c_str());
938 for (i=0; i<fileset->exclude_list.size(); i++) {
939 findINCEXE *incexe = (findINCEXE *)fileset->exclude_list.get(i);
941 for (j=0; j<incexe->opts_list.size(); j++) {
942 findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
943 for (k=0; k<fo->regex.size(); k++) {
944 Dmsg1(400, "R %s\n", (char *)fo->regex.get(k));
946 for (k=0; k<fo->regexdir.size(); k++) {
947 Dmsg1(400, "RD %s\n", (char *)fo->regexdir.get(k));
949 for (k=0; k<fo->regexfile.size(); k++) {
950 Dmsg1(400, "RF %s\n", (char *)fo->regexfile.get(k));
952 for (k=0; k<fo->wild.size(); k++) {
953 Dmsg1(400, "W %s\n", (char *)fo->wild.get(k));
955 for (k=0; k<fo->wilddir.size(); k++) {
956 Dmsg1(400, "WD %s\n", (char *)fo->wilddir.get(k));
958 for (k=0; k<fo->wildfile.size(); k++) {
959 Dmsg1(400, "WF %s\n", (char *)fo->wildfile.get(k));
961 for (k=0; k<fo->wildbase.size(); k++) {
962 Dmsg1(400, "WB %s\n", (char *)fo->wildbase.get(k));
964 for (k=0; k<fo->base.size(); k++) {
965 Dmsg1(400, "B %s\n", (char *)fo->base.get(k));
967 for (k=0; k<fo->fstype.size(); k++) {
968 Dmsg1(400, "X %s\n", (char *)fo->fstype.get(k));
970 for (k=0; k<fo->drivetype.size(); k++) {
971 Dmsg1(400, "XD %s\n", (char *)fo->drivetype.get(k));
975 foreach_dlist(node, incexe->name_list) {
976 Dmsg1(400, "F %s\n", node->c_str());
978 foreach_dlist(node, &incexe->plugin_list) {
979 Dmsg1(400, "P %s\n", node->c_str());
983 return ff->fileset->state != state_error;
988 * As an optimization, we should do this during
989 * "compile" time in filed/job.c, and keep only a bit mask
990 * and the Verify options.
992 static void set_options(findFOPTS *fo, const char *opts)
998 // Commented out as it is not backward compatible - KES
1000 // fo->flags |= FO_IGNORECASE; /* always ignorecase under windows */
1003 for (p=opts; *p; p++) {
1005 case 'a': /* alway replace */
1006 case '0': /* no option */
1009 fo->flags |= FO_EXCLUDE;
1012 fo->flags |= FO_MULTIFS;
1014 case 'h': /* no recursion */
1015 fo->flags |= FO_NO_RECURSION;
1017 case 'H': /* no hard link handling */
1018 fo->flags |= FO_NO_HARDLINK;
1021 fo->flags |= FO_IGNORECASE;
1024 fo->flags |= FO_MD5;
1027 fo->flags |= FO_NOREPLACE;
1029 case 'p': /* use portable data format */
1030 fo->flags |= FO_PORTABLE;
1032 case 'R': /* Resource forks and Finder Info */
1033 fo->flags |= FO_HFSPLUS;
1034 case 'r': /* read fifo */
1035 fo->flags |= FO_READFIFO;
1040 fo->flags |= FO_SHA1;
1045 fo->flags |= FO_SHA256;
1049 fo->flags |= FO_SHA512;
1055 * If 2 or 3 is seen here, SHA2 is not configured, so
1056 * eat the option, and drop back to SHA-1.
1058 if (p[1] == '2' || p[1] == '3') {
1061 fo->flags |= FO_SHA1;
1066 fo->flags |= FO_SPARSE;
1069 fo->flags |= FO_MTIMEONLY;
1072 fo->flags |= FO_KEEPATIME;
1075 fo->flags |= FO_ACL;
1077 case 'V': /* verify options */
1078 /* Copy Verify Options */
1079 for (j=0; *p && *p != ':'; p++) {
1080 fo->VerifyOpts[j] = *p;
1081 if (j < (int)sizeof(fo->VerifyOpts) - 1) {
1085 fo->VerifyOpts[j] = 0;
1087 case 'C': /* accurate options */
1088 /* Copy Accurate Options */
1089 for (j=0; *p && *p != ':'; p++) {
1090 fo->AccurateOpts[j] = *p;
1091 if (j < (int)sizeof(fo->AccurateOpts) - 1) {
1095 fo->AccurateOpts[j] = 0;
1097 case 'P': /* strip path */
1100 for (j=0; *p && *p != ':'; p++) {
1102 if (j < (int)sizeof(strip) - 1) {
1107 fo->strip_path = atoi(strip);
1108 fo->flags |= FO_STRIPPATH;
1109 Dmsg2(100, "strip=%s strip_path=%d\n", strip, fo->strip_path);
1112 fo->flags |= FO_IF_NEWER;
1115 fo->flags |= FO_ENHANCEDWILD;
1117 case 'Z': /* gzip compression */
1118 fo->flags |= FO_GZIP;
1119 fo->GZIP_level = *++p - '0';
1122 fo->flags |= FO_NOATIME;
1125 fo->flags |= FO_CHKCHANGES;
1128 fo->flags |= FO_HONOR_NODUMP;
1131 fo->flags |= FO_XATTR;
1134 Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *p);
1142 * Director is passing his Fileset
1144 static int fileset_cmd(JCR *jcr)
1146 BSOCK *dir = jcr->dir_bsock;
1148 #if defined(WIN32_VSS)
1151 sscanf(dir->msg, "fileset vss=%d", &vss);
1155 if (!init_fileset(jcr)) {
1158 while (dir->recv() >= 0) {
1159 strip_trailing_junk(dir->msg);
1160 Dmsg1(500, "Fileset: %s\n", dir->msg);
1161 add_fileset(jcr, dir->msg);
1163 if (!term_fileset(jcr)) {
1166 return dir->fsend(OKinc);
1169 static void free_bootstrap(JCR *jcr)
1171 if (jcr->RestoreBootstrap) {
1172 unlink(jcr->RestoreBootstrap);
1173 free_pool_memory(jcr->RestoreBootstrap);
1174 jcr->RestoreBootstrap = NULL;
1179 static pthread_mutex_t bsr_mutex = PTHREAD_MUTEX_INITIALIZER;
1180 static uint32_t bsr_uniq = 0;
1183 * The Director sends us the bootstrap file, which
1184 * we will in turn pass to the SD.
1186 static int bootstrap_cmd(JCR *jcr)
1188 BSOCK *dir = jcr->dir_bsock;
1189 POOLMEM *fname = get_pool_memory(PM_FNAME);
1192 free_bootstrap(jcr);
1195 Mmsg(fname, "%s/%s.%s.%d.bootstrap", me->working_directory, me->hdr.name,
1196 jcr->Job, bsr_uniq);
1198 Dmsg1(400, "bootstrap=%s\n", fname);
1199 jcr->RestoreBootstrap = fname;
1200 bs = fopen(fname, "a+b"); /* create file */
1203 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
1204 jcr->RestoreBootstrap, be.bstrerror());
1206 * Suck up what he is sending to us so that he will then
1207 * read our error message.
1209 while (dir->recv() >= 0)
1211 free_bootstrap(jcr);
1212 set_jcr_job_status(jcr, JS_ErrorTerminated);
1216 while (dir->recv() >= 0) {
1217 Dmsg1(200, "filed<dird: bootstrap: %s", dir->msg);
1218 fputs(dir->msg, bs);
1222 * Note, do not free the bootstrap yet -- it needs to be
1225 return dir->fsend(OKbootstrap);
1230 * Get backup level from Director
1233 static int level_cmd(JCR *jcr)
1235 BSOCK *dir = jcr->dir_bsock;
1236 POOLMEM *level, *buf = NULL;
1239 level = get_memory(dir->msglen+1);
1240 Dmsg1(100, "level_cmd: %s", dir->msg);
1241 if (strstr(dir->msg, "accurate")) {
1242 jcr->accurate = true;
1244 if (sscanf(dir->msg, "level = %s ", level) != 1) {
1247 /* Base backup requested? */
1248 if (strcmp(level, "base") == 0) {
1249 jcr->set_JobLevel(L_BASE);
1250 /* Full backup requested? */
1251 } else if (strcmp(level, "full") == 0) {
1252 jcr->set_JobLevel(L_FULL);
1253 } else if (strstr(level, "differential")) {
1254 jcr->set_JobLevel(L_DIFFERENTIAL);
1257 } else if (strstr(level, "incremental")) {
1258 jcr->set_JobLevel(L_INCREMENTAL);
1262 * We get his UTC since time, then sync the clocks and correct it
1263 * to agree with our clock.
1265 } else if (strcmp(level, "since_utime") == 0) {
1266 buf = get_memory(dir->msglen+1);
1267 utime_t since_time, adj;
1268 btime_t his_time, bt_start, rt=0, bt_adj=0;
1269 if (jcr->get_JobLevel() == L_NONE) {
1270 jcr->set_JobLevel(L_SINCE); /* if no other job level set, do it now */
1272 if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d",
1273 buf, &mtime_only) != 2) {
1276 since_time = str_to_uint64(buf); /* this is the since time */
1277 Dmsg1(100, "since_time=%lld\n", since_time);
1278 char ed1[50], ed2[50];
1280 * Sync clocks by polling him for the time. We take
1281 * 10 samples of his time throwing out the first two.
1283 for (int i=0; i<10; i++) {
1284 bt_start = get_current_btime();
1285 dir->signal(BNET_BTIME); /* poll for time */
1286 if (dir->recv() <= 0) { /* get response */
1289 if (sscanf(dir->msg, "btime %s", buf) != 1) {
1292 if (i < 2) { /* toss first two results */
1295 his_time = str_to_uint64(buf);
1296 rt = get_current_btime() - bt_start; /* compute round trip time */
1297 Dmsg2(100, "Dirtime=%s FDtime=%s\n", edit_uint64(his_time, ed1),
1298 edit_uint64(bt_start, ed2));
1299 bt_adj += bt_start - his_time - rt/2;
1300 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1303 bt_adj = bt_adj / 8; /* compute average time */
1304 Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
1305 adj = btime_to_utime(bt_adj);
1306 since_time += adj; /* adjust for clock difference */
1307 /* Don't notify if time within 3 seconds */
1308 if (adj > 3 || adj < -3) {
1310 if (adj > 600 || adj < -600) {
1315 Jmsg(jcr, type, 0, _("DIR and FD clocks differ by %lld seconds, FD automatically compensating.\n"), adj);
1317 dir->signal(BNET_EOD);
1319 Dmsg2(100, "adj=%lld since_time=%lld\n", adj, since_time);
1320 jcr->incremental = 1; /* set incremental or decremental backup */
1321 jcr->mtime = since_time; /* set since time */
1322 generate_plugin_event(jcr, bEventSince, (void *)(time_t)jcr->mtime);
1324 Jmsg1(jcr, M_FATAL, 0, _("Unknown backup level: %s\n"), level);
1332 generate_plugin_event(jcr, bEventLevel, (void *)jcr->get_JobLevel());
1333 return dir->fsend(OKlevel);
1336 pm_strcpy(jcr->errmsg, dir->msg);
1337 Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
1346 * Get session parameters from Director -- this is for a Restore command
1348 static int session_cmd(JCR *jcr)
1350 BSOCK *dir = jcr->dir_bsock;
1352 Dmsg1(100, "SessionCmd: %s", dir->msg);
1353 if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
1354 &jcr->VolSessionId, &jcr->VolSessionTime,
1355 &jcr->StartFile, &jcr->EndFile,
1356 &jcr->StartBlock, &jcr->EndBlock) != 7) {
1357 pm_strcpy(jcr->errmsg, dir->msg);
1358 Jmsg(jcr, M_FATAL, 0, _("Bad session command: %s"), jcr->errmsg);
1362 return dir->fsend(OKsession);
1366 * Get address of storage daemon from Director
1369 static int storage_cmd(JCR *jcr)
1371 int stored_port; /* storage daemon port */
1372 int enable_ssl; /* enable ssl to sd */
1373 BSOCK *dir = jcr->dir_bsock;
1374 BSOCK *sd = new_bsock(); /* storage daemon bsock */
1376 Dmsg1(100, "StorageCmd: %s", dir->msg);
1377 if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
1378 pm_strcpy(jcr->errmsg, dir->msg);
1379 Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
1382 Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
1383 /* Open command communications with Storage daemon */
1384 /* Try to connect for 1 hour at 10 second intervals */
1386 sd->set_source_address(me->FDsrc_addr);
1387 if (!sd->connect(jcr, 10, (int)me->SDConnectTimeout, me->heartbeat_interval,
1388 _("Storage daemon"), jcr->stored_addr, NULL, stored_port, 1)) {
1394 Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
1395 jcr->stored_addr, stored_port);
1396 Dmsg2(100, "Failed to connect to Storage daemon: %s:%d\n",
1397 jcr->stored_addr, stored_port);
1400 Dmsg0(110, "Connection OK to SD.\n");
1402 jcr->store_bsock = sd;
1404 sd->fsend("Hello Start Job %s\n", jcr->Job);
1405 if (!authenticate_storagedaemon(jcr)) {
1406 Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
1409 Dmsg0(110, "Authenticated with SD.\n");
1411 /* Send OK to Director */
1412 return dir->fsend(OKstore);
1415 dir->fsend(BADcmd, "storage");
1424 static int backup_cmd(JCR *jcr)
1426 BSOCK *dir = jcr->dir_bsock;
1427 BSOCK *sd = jcr->store_bsock;
1431 #if defined(WIN32_VSS)
1432 // capture state here, if client is backed up by multiple directors
1433 // and one enables vss and the other does not then enable_vss can change
1434 // between here and where its evaluated after the job completes.
1435 jcr->VSS = g_pVSSClient && enable_vss;
1437 /* Run only one at a time */
1442 set_jcr_job_status(jcr, JS_Blocked);
1443 jcr->set_JobType(JT_BACKUP);
1444 Dmsg1(100, "begin backup ff=%p\n", jcr->ff);
1447 Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
1448 dir->fsend(BADcmd, "backup");
1452 dir->fsend(OKbackup);
1453 Dmsg1(110, "filed>dird: %s", dir->msg);
1456 * Send Append Open Session to Storage daemon
1458 sd->fsend(append_open);
1459 Dmsg1(110, ">stored: %s", sd->msg);
1461 * Expect to receive back the Ticket number
1463 if (bget_msg(sd) >= 0) {
1464 Dmsg1(110, "<stored: %s", sd->msg);
1465 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1466 Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
1469 Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
1471 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
1476 * Send Append data command to Storage daemon
1478 sd->fsend(append_data, jcr->Ticket);
1479 Dmsg1(110, ">stored: %s", sd->msg);
1482 * Expect to get OK data
1484 Dmsg1(110, "<stored: %s", sd->msg);
1485 if (!response(jcr, sd, OK_data, "Append Data")) {
1489 generate_daemon_event(jcr, "JobStart");
1490 generate_plugin_event(jcr, bEventStartBackupJob);
1492 #if defined(WIN32_VSS)
1493 /* START VSS ON WIN32 */
1495 if (g_pVSSClient->InitializeForBackup()) {
1496 /* tell vss which drives to snapshot */
1497 char szWinDriveLetters[27];
1498 if (get_win32_driveletters(jcr->ff, szWinDriveLetters)) {
1499 Jmsg(jcr, M_INFO, 0, _("Generate VSS snapshots. Driver=\"%s\", Drive(s)=\"%s\"\n"), g_pVSSClient->GetDriverName(), szWinDriveLetters);
1500 if (!g_pVSSClient->CreateSnapshots(szWinDriveLetters)) {
1501 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshots failed.\n"));
1504 /* tell user if snapshot creation of a specific drive failed */
1506 for (i=0; i < (int)strlen(szWinDriveLetters); i++) {
1507 if (islower(szWinDriveLetters[i])) {
1508 Jmsg(jcr, M_WARNING, 0, _("Generate VSS snapshot of drive \"%c:\\\" failed. VSS support is disabled on this drive.\n"), szWinDriveLetters[i]);
1512 /* inform user about writer states */
1513 for (i=0; i < (int)g_pVSSClient->GetWriterCount(); i++)
1514 if (g_pVSSClient->GetWriterState(i) < 1) {
1515 Jmsg(jcr, M_WARNING, 0, _("VSS Writer (PrepareForBackup): %s\n"), g_pVSSClient->GetWriterInfo(i));
1520 Jmsg(jcr, M_INFO, 0, _("No drive letters found for generating VSS snapshots.\n"));
1524 Jmsg(jcr, M_WARNING, 0, _("VSS was not initialized properly. VSS support is disabled. ERR=%s\n"), be.bstrerror());
1526 run_scripts(jcr, jcr->RunScripts, "ClientAfterVSS");
1531 * Send Files to Storage daemon
1533 Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
1534 if (!blast_data_to_storage_daemon(jcr, NULL)) {
1535 set_jcr_job_status(jcr, JS_ErrorTerminated);
1536 bnet_suppress_error_messages(sd, 1);
1537 bget_msg(sd); /* Read final response from append_data */
1538 Dmsg0(110, "Error in blast_data.\n");
1540 set_jcr_job_status(jcr, JS_Terminated);
1541 /* Note, the above set status will not override an error */
1542 if (!(jcr->JobStatus == JS_Terminated || jcr->JobStatus == JS_Warnings)) {
1543 bnet_suppress_error_messages(sd, 1);
1544 goto cleanup; /* bail out now */
1547 * Expect to get response to append_data from Storage daemon
1549 if (!response(jcr, sd, OK_append, "Append Data")) {
1550 set_jcr_job_status(jcr, JS_ErrorTerminated);
1555 * Send Append End Data to Storage daemon
1557 sd->fsend(append_end, jcr->Ticket);
1559 if (!response(jcr, sd, OK_end, "Append End")) {
1560 set_jcr_job_status(jcr, JS_ErrorTerminated);
1565 * Send Append Close to Storage daemon
1567 sd->fsend(append_close, jcr->Ticket);
1568 while (bget_msg(sd) >= 0) { /* stop on signal or error */
1569 if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
1571 Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
1575 Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
1578 if (!(SDJobStatus == JS_Terminated || SDJobStatus == JS_Warnings)) {
1579 Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
1585 #if defined(WIN32_VSS)
1586 /* STOP VSS ON WIN32 */
1587 /* tell vss to close the backup session */
1589 if (g_pVSSClient->CloseBackup()) {
1590 /* inform user about writer states */
1591 for (int i=0; i<(int)g_pVSSClient->GetWriterCount(); i++) {
1592 int msg_type = M_INFO;
1593 if (g_pVSSClient->GetWriterState(i) < 1) {
1594 msg_type = M_WARNING;
1597 Jmsg(jcr, msg_type, 0, _("VSS Writer (BackupComplete): %s\n"), g_pVSSClient->GetWriterInfo(i));
1600 Win32ConvCleanupCache();
1605 generate_plugin_event(jcr, bEventEndBackupJob);
1606 return 0; /* return and stop command loop */
1610 * Do a Verify for Director
1613 static int verify_cmd(JCR *jcr)
1615 BSOCK *dir = jcr->dir_bsock;
1616 BSOCK *sd = jcr->store_bsock;
1619 jcr->set_JobType(JT_VERIFY);
1620 if (sscanf(dir->msg, verifycmd, level) != 1) {
1621 dir->fsend(_("2994 Bad verify command: %s\n"), dir->msg);
1625 if (strcasecmp(level, "init") == 0) {
1626 jcr->set_JobLevel(L_VERIFY_INIT);
1627 } else if (strcasecmp(level, "catalog") == 0){
1628 jcr->set_JobLevel(L_VERIFY_CATALOG);
1629 } else if (strcasecmp(level, "volume") == 0){
1630 jcr->set_JobLevel(L_VERIFY_VOLUME_TO_CATALOG);
1631 } else if (strcasecmp(level, "data") == 0){
1632 jcr->set_JobLevel(L_VERIFY_DATA);
1633 } else if (strcasecmp(level, "disk_to_catalog") == 0) {
1634 jcr->set_JobLevel(L_VERIFY_DISK_TO_CATALOG);
1636 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1640 dir->fsend(OKverify);
1642 generate_daemon_event(jcr, "JobStart");
1643 generate_plugin_event(jcr, bEventLevel, (void *)jcr->get_JobLevel());
1644 generate_plugin_event(jcr, bEventStartVerifyJob);
1646 Dmsg1(110, "filed>dird: %s", dir->msg);
1648 switch (jcr->get_JobLevel()) {
1650 case L_VERIFY_CATALOG:
1653 case L_VERIFY_VOLUME_TO_CATALOG:
1654 if (!open_sd_read_session(jcr)) {
1657 start_dir_heartbeat(jcr);
1658 do_verify_volume(jcr);
1659 stop_dir_heartbeat(jcr);
1661 * Send Close session command to Storage daemon
1663 sd->fsend(read_close, jcr->Ticket);
1664 Dmsg1(130, "filed>stored: %s", sd->msg);
1666 /* ****FIXME**** check response */
1667 bget_msg(sd); /* get OK */
1669 /* Inform Storage daemon that we are done */
1670 sd->signal(BNET_TERMINATE);
1673 case L_VERIFY_DISK_TO_CATALOG:
1677 dir->fsend(_("2994 Bad verify level: %s\n"), dir->msg);
1681 dir->signal(BNET_EOD);
1682 generate_plugin_event(jcr, bEventEndVerifyJob);
1683 return 0; /* return and terminate command loop */
1687 * Do a Restore for Director
1690 static int restore_cmd(JCR *jcr)
1692 BSOCK *dir = jcr->dir_bsock;
1693 BSOCK *sd = jcr->store_bsock;
1695 bool use_regexwhere=false;
1700 * Scan WHERE (base directory for restore) from command
1702 Dmsg0(150, "restore command\n");
1703 /* Pickup where string */
1704 args = get_memory(dir->msglen+1);
1707 if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, args) != 3) {
1708 if (sscanf(dir->msg, restorecmdR, &replace, &prefix_links, args) != 3){
1709 if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
1710 pm_strcpy(jcr->errmsg, dir->msg);
1711 Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
1716 use_regexwhere = true;
1718 /* Turn / into nothing */
1719 if (IsPathSeparator(args[0]) && args[1] == '\0') {
1723 Dmsg2(150, "Got replace %c, where=%s\n", replace, args);
1724 unbash_spaces(args);
1726 if (use_regexwhere) {
1727 jcr->where_bregexp = get_bregexps(args);
1728 if (!jcr->where_bregexp) {
1729 Jmsg(jcr, M_FATAL, 0, _("Bad where regexp. where=%s\n"), args);
1730 free_pool_memory(args);
1734 jcr->where = bstrdup(args);
1737 free_pool_memory(args);
1738 jcr->replace = replace;
1739 jcr->prefix_links = prefix_links;
1741 dir->fsend(OKrestore);
1742 Dmsg1(110, "filed>dird: %s", dir->msg);
1744 jcr->set_JobType(JT_RESTORE);
1746 set_jcr_job_status(jcr, JS_Blocked);
1748 if (!open_sd_read_session(jcr)) {
1749 set_jcr_job_status(jcr, JS_ErrorTerminated);
1753 set_jcr_job_status(jcr, JS_Running);
1756 * Do restore of files and data
1758 start_dir_heartbeat(jcr);
1759 generate_daemon_event(jcr, "JobStart");
1760 generate_plugin_event(jcr, bEventStartRestoreJob);
1762 stop_dir_heartbeat(jcr);
1764 set_jcr_job_status(jcr, JS_Terminated);
1765 if (jcr->JobStatus != JS_Terminated) {
1766 bnet_suppress_error_messages(sd, 1);
1770 * Send Close session command to Storage daemon
1772 sd->fsend(read_close, jcr->Ticket);
1773 Dmsg1(130, "filed>stored: %s", sd->msg);
1775 bget_msg(sd); /* get OK */
1777 /* Inform Storage daemon that we are done */
1778 sd->signal(BNET_TERMINATE);
1782 if (jcr->JobErrors) {
1783 set_jcr_job_status(jcr, JS_ErrorTerminated);
1786 Dmsg0(130, "Done in job.c\n");
1787 generate_plugin_event(jcr, bEventEndRestoreJob);
1788 return 0; /* return and terminate command loop */
1791 static int open_sd_read_session(JCR *jcr)
1793 BSOCK *sd = jcr->store_bsock;
1796 Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
1799 Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
1800 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
1801 Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
1803 * Open Read Session with Storage daemon
1805 sd->fsend(read_open, "DummyVolume",
1806 jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile,
1807 jcr->StartBlock, jcr->EndBlock);
1808 Dmsg1(110, ">stored: %s", sd->msg);
1813 if (bget_msg(sd) >= 0) {
1814 Dmsg1(110, "filed<stored: %s", sd->msg);
1815 if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
1816 Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
1819 Dmsg1(110, "filed: got Ticket=%d\n", jcr->Ticket);
1821 Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
1825 if (!send_bootstrap_file(jcr)) {
1830 * Start read of data with Storage daemon
1832 sd->fsend(read_data, jcr->Ticket);
1833 Dmsg1(110, ">stored: %s", sd->msg);
1838 if (!response(jcr, sd, OK_data, "Read Data")) {
1845 * Destroy the Job Control Record and associated
1846 * resources (sockets).
1848 static void filed_free_jcr(JCR *jcr)
1850 if (jcr->store_bsock) {
1851 jcr->store_bsock->close();
1853 free_bootstrap(jcr);
1854 if (jcr->last_fname) {
1855 free_pool_memory(jcr->last_fname);
1857 free_runscripts(jcr->RunScripts);
1858 delete jcr->RunScripts;
1860 if (jcr->JobId != 0)
1861 write_state_file(me->working_directory, "bacula-fd", get_first_port_host_order(me->FDaddrs));
1867 * Get response from Storage daemon to a command we
1868 * sent. Check that the response is OK.
1870 * Returns: 0 on failure
1873 int response(JCR *jcr, BSOCK *sd, char *resp, const char *cmd)
1878 if (bget_msg(sd) > 0) {
1879 Dmsg0(110, sd->msg);
1880 if (strcmp(sd->msg, resp) == 0) {
1884 if (job_canceled(jcr)) {
1885 return 0; /* if canceled avoid useless error messages */
1887 if (is_bnet_error(sd)) {
1888 Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
1889 cmd, bnet_strerror(sd));
1891 Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
1892 cmd, resp, sd->msg);
1897 static int send_bootstrap_file(JCR *jcr)
1901 BSOCK *sd = jcr->store_bsock;
1902 const char *bootstrap = "bootstrap\n";
1905 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
1906 if (!jcr->RestoreBootstrap) {
1909 bs = fopen(jcr->RestoreBootstrap, "rb");
1912 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
1913 jcr->RestoreBootstrap, be.bstrerror());
1914 set_jcr_job_status(jcr, JS_ErrorTerminated);
1917 sd->msglen = pm_strcpy(sd->msg, bootstrap);
1919 while (fgets(buf, sizeof(buf), bs)) {
1920 sd->msglen = Mmsg(sd->msg, "%s", buf);
1923 sd->signal(BNET_EOD);
1925 if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
1926 set_jcr_job_status(jcr, JS_ErrorTerminated);
1932 free_bootstrap(jcr);