3 * Bacula Director -- fd_cmds.c -- send commands to File daemon
5 * Kern Sibbald, October MM
7 * This routine is run as a separate thread. There may be more
8 * work to be done to make it totally reentrant!!!!
10 * Utility functions for sending info to File Daemon.
11 * These functions are used by both backup and verify.
16 Copyright (C) 2000-2005 Kern Sibbald
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation; either version 2 of
21 the License, or (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public
29 License along with this program; if not, write to the Free
30 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38 /* Commands sent to File daemon */
39 static char fileset[] = "fileset\n"; /* set full fileset */
40 static char jobcmd[] = "JobId=%d Job=%s SDid=%u SDtime=%u Authorization=%s\n";
41 /* Note, mtime_only is not used here -- implemented as file option */
42 static char levelcmd[] = "level = %s%s mtime_only=%d\n";
43 static char runbefore[] = "RunBeforeJob %s\n";
44 static char runafter[] = "RunAfterJob %s\n";
47 /* Responses received from File daemon */
48 static char OKinc[] = "2000 OK include\n";
49 static char OKjob[] = "2000 OK Job";
50 static char OKbootstrap[] = "2000 OK bootstrap\n";
51 static char OKlevel[] = "2000 OK level\n";
52 static char OKRunBefore[] = "2000 OK RunBefore\n";
53 static char OKRunAfter[] = "2000 OK RunAfter\n";
55 /* Forward referenced functions */
57 /* External functions */
58 extern int debug_level;
59 extern DIRRES *director;
60 extern int FDConnectTimeout;
66 * Open connection with File daemon.
67 * Try connecting every retry_interval (default 10 sec), and
68 * give up after max_retry_time (default 30 mins).
71 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
76 if (!jcr->file_bsock) {
77 fd = bnet_connect(jcr, retry_interval, max_retry_time,
78 _("File daemon"), jcr->client->address,
79 NULL, jcr->client->FDport, verbose);
81 set_jcr_job_status(jcr, JS_ErrorTerminated);
84 Dmsg0(10, "Opened connection with File daemon\n");
86 fd = jcr->file_bsock; /* use existing connection */
88 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
90 set_jcr_job_status(jcr, JS_Running);
92 if (!authenticate_file_daemon(jcr)) {
93 set_jcr_job_status(jcr, JS_ErrorTerminated);
98 * Now send JobId and authorization key
100 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
101 jcr->VolSessionTime, jcr->sd_auth_key);
102 if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
103 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
105 Dmsg1(100, ">filed: %s", fd->msg);
106 if (bget_dirmsg(fd) > 0) {
107 Dmsg1(110, "<filed: %s", fd->msg);
108 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
109 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
110 jcr->client->hdr.name, fd->msg);
111 set_jcr_job_status(jcr, JS_ErrorTerminated);
113 } else if (jcr->db) {
115 memset(&cr, 0, sizeof(cr));
116 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
117 cr.AutoPrune = jcr->client->AutoPrune;
118 cr.FileRetention = jcr->client->FileRetention;
119 cr.JobRetention = jcr->client->JobRetention;
120 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
121 if (!db_update_client_record(jcr, jcr->db, &cr)) {
122 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
123 db_strerror(jcr->db));
127 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
129 set_jcr_job_status(jcr, JS_ErrorTerminated);
136 * This subroutine edits the last job start time into a
137 * "since=date/time" buffer that is returned in the
138 * variable since. This is used for display purposes in
139 * the job report. The time in jcr->stime is later
140 * passed to tell the File daemon what to do.
142 void get_level_since_time(JCR *jcr, char *since, int since_len)
148 if ( jcr->stime && jcr->stime[0]) {
149 bstrncpy(since, ", since=", since_len);
150 bstrncat(since, jcr->stime, since_len);
155 jcr->stime = get_pool_memory(PM_MESSAGE);
158 /* Lookup the last FULL backup job to get the time/date for a
159 * differential or incremental save.
161 switch (jcr->JobLevel) {
164 /* Look up start time of last job */
165 jcr->jr.JobId = 0; /* flag for db_find_job_start time */
166 if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
167 /* No job found, so upgrade this one to Full */
168 Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
169 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
170 bsnprintf(since, since_len, " (upgraded from %s)",
171 level_to_str(jcr->JobLevel));
172 jcr->JobLevel = jcr->jr.JobLevel = L_FULL;
174 if (jcr->job->rerun_failed_levels) {
175 if (db_find_failed_job_since(jcr, jcr->db, &jcr->jr, jcr->stime, JobLevel)) {
176 Jmsg(jcr, M_INFO, 0, _("Prior failed job found. Upgrading to %s.\n"),
177 level_to_str(JobLevel));
178 bsnprintf(since, since_len, " (upgraded from %s)",
179 level_to_str(jcr->JobLevel));
180 jcr->JobLevel = jcr->jr.JobLevel = JobLevel;
181 jcr->jr.JobId = jcr->JobId;
185 bstrncpy(since, ", since=", since_len);
186 bstrncat(since, jcr->stime, since_len);
188 jcr->jr.JobId = jcr->JobId;
191 Dmsg2(100, "Level=%c last start time=%s\n", jcr->JobLevel, jcr->stime);
194 static void send_since_time(JCR *jcr)
196 BSOCK *fd = jcr->file_bsock;
200 stime = str_to_utime(jcr->stime);
201 bnet_fsend(fd, levelcmd, "since_utime ", edit_uint64(stime, ed1), 0);
202 while (bget_dirmsg(fd) >= 0) { /* allow him to poll us to sync clocks */
203 Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
209 * Send level command to FD.
210 * Used for backup jobs and estimate command.
212 bool send_level_command(JCR *jcr)
214 BSOCK *fd = jcr->file_bsock;
216 * Send Level command to File daemon
218 switch (jcr->JobLevel) {
220 bnet_fsend(fd, levelcmd, "base", " ", 0);
222 /* L_NONE is the console, sending something off to the FD */
225 bnet_fsend(fd, levelcmd, "full", " ", 0);
228 bnet_fsend(fd, levelcmd, "differential", " ", 0);
229 send_since_time(jcr);
232 bnet_fsend(fd, levelcmd, "incremental", " ", 0);
233 send_since_time(jcr);
237 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
238 jcr->JobLevel, jcr->JobLevel);
241 Dmsg1(120, ">filed: %s", fd->msg);
242 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
249 * Send either an Included or an Excluded list to FD
251 static int send_fileset(JCR *jcr)
253 FILESET *fileset = jcr->fileset;
254 BSOCK *fd = jcr->file_bsock;
260 num = fileset->num_includes;
262 num = fileset->num_excludes;
264 for (int i=0; i<num; i++) {
274 ie = fileset->include_items[i];
275 bnet_fsend(fd, "I\n");
277 ie = fileset->exclude_items[i];
278 bnet_fsend(fd, "E\n");
280 for (j=0; j<ie->num_opts; j++) {
281 FOPTS *fo = ie->opts_list[j];
282 bnet_fsend(fd, "O %s\n", fo->opts);
283 for (k=0; k<fo->regex.size(); k++) {
284 bnet_fsend(fd, "R %s\n", fo->regex.get(k));
286 for (k=0; k<fo->regexdir.size(); k++) {
287 bnet_fsend(fd, "RD %s\n", fo->regexdir.get(k));
289 for (k=0; k<fo->regexfile.size(); k++) {
290 bnet_fsend(fd, "RF %s\n", fo->regexfile.get(k));
292 for (k=0; k<fo->wild.size(); k++) {
293 bnet_fsend(fd, "W %s\n", fo->wild.get(k));
295 for (k=0; k<fo->wilddir.size(); k++) {
296 bnet_fsend(fd, "WD %s\n", fo->wilddir.get(k));
298 for (k=0; k<fo->wildfile.size(); k++) {
299 bnet_fsend(fd, "WF %s\n", fo->wildfile.get(k));
301 for (k=0; k<fo->base.size(); k++) {
302 bnet_fsend(fd, "B %s\n", fo->base.get(k));
304 for (k=0; k<fo->fstype.size(); k++) {
305 bnet_fsend(fd, "X %s\n", fo->fstype.get(k));
308 bnet_fsend(fd, "D %s\n", fo->reader);
311 bnet_fsend(fd, "T %s\n", fo->writer);
313 bnet_fsend(fd, "N\n");
316 for (j=0; j<ie->name_list.size(); j++) {
317 p = (char *)ie->name_list.get(j);
320 p++; /* skip over the | */
321 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
322 bpipe = open_bpipe(fd->msg, 0, "r");
325 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
329 bstrncpy(buf, "F ", sizeof(buf));
330 Dmsg1(500, "Opts=%s\n", buf);
331 optlen = strlen(buf);
332 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
333 fd->msglen = Mmsg(fd->msg, "%s", buf);
334 Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
335 if (!bnet_send(fd)) {
336 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
340 if ((stat=close_bpipe(bpipe)) != 0) {
342 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
343 p, be.strerror(stat));
348 p++; /* skip over < */
349 if ((ffd = fopen(p, "r")) == NULL) {
351 Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
355 bstrncpy(buf, "F ", sizeof(buf));
356 Dmsg1(500, "Opts=%s\n", buf);
357 optlen = strlen(buf);
358 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
359 fd->msglen = Mmsg(fd->msg, "%s", buf);
360 if (!bnet_send(fd)) {
361 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
368 p++; /* skip over \ */
369 /* Note, fall through wanted */
371 pm_strcpy(fd->msg, "F ");
372 fd->msglen = pm_strcat(fd->msg, p);
373 Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
374 if (!bnet_send(fd)) {
375 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
381 bnet_fsend(fd, "N\n");
383 if (!include) { /* If we just did excludes */
384 break; /* all done */
386 include = false; /* Now do excludes */
389 bnet_sig(fd, BNET_EOD); /* end of data */
390 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
396 set_jcr_job_status(jcr, JS_ErrorTerminated);
403 * Send include list to File daemon
405 bool send_include_list(JCR *jcr)
407 BSOCK *fd = jcr->file_bsock;
408 if (jcr->fileset->new_include) {
409 bnet_fsend(fd, fileset);
410 return send_fileset(jcr);
417 * Send exclude list to File daemon
418 * Under the new scheme, the Exclude list
419 * is part of the FileSet sent with the
420 * "include_list" above.
422 bool send_exclude_list(JCR *jcr)
429 * Send bootstrap file if any to the File daemon.
430 * This is used for restore and verify VolumeToCatalog
432 bool send_bootstrap_file(JCR *jcr)
436 BSOCK *fd = jcr->file_bsock;
437 const char *bootstrap = "bootstrap\n";
439 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
440 if (!jcr->RestoreBootstrap) {
443 bs = fopen(jcr->RestoreBootstrap, "r");
446 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
447 jcr->RestoreBootstrap, be.strerror());
448 set_jcr_job_status(jcr, JS_ErrorTerminated);
451 bnet_fsend(fd, bootstrap);
452 while (fgets(buf, sizeof(buf), bs)) {
453 bnet_fsend(fd, "%s", buf);
455 bnet_sig(fd, BNET_EOD);
457 if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
458 set_jcr_job_status(jcr, JS_ErrorTerminated);
465 * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
467 int send_run_before_and_after_commands(JCR *jcr)
469 POOLMEM *msg = get_pool_memory(PM_FNAME);
470 BSOCK *fd = jcr->file_bsock;
471 if (jcr->job->ClientRunBeforeJob) {
472 pm_strcpy(msg, jcr->job->ClientRunBeforeJob);
474 bnet_fsend(fd, runbefore, msg);
475 if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
476 set_jcr_job_status(jcr, JS_ErrorTerminated);
477 free_pool_memory(msg);
481 if (jcr->job->ClientRunAfterJob) {
482 fd->msglen = pm_strcpy(msg, jcr->job->ClientRunAfterJob);
484 bnet_fsend(fd, runafter, msg);
485 if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
486 set_jcr_job_status(jcr, JS_ErrorTerminated);
487 free_pool_memory(msg);
491 free_pool_memory(msg);
497 * Read the attributes from the File daemon for
498 * a Verify job and store them in the catalog.
500 int get_attributes_and_put_in_catalog(JCR *jcr)
506 fd = jcr->file_bsock;
507 jcr->jr.FirstIndex = 1;
508 memset(&ar, 0, sizeof(ar));
511 Dmsg0(120, "bdird: waiting to receive file attributes\n");
512 /* Pickup file attributes and signature */
513 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
515 /*****FIXME****** improve error handling to stop only on
516 * really fatal problems, or the number of errors is too
522 char Opts_SIG[MAXSTRING]; /* either Verify opts or MD5/SHA1 signature */
525 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
526 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
527 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n"
528 "msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
529 set_jcr_job_status(jcr, JS_ErrorTerminated);
533 skip_nonspaces(&p); /* skip FileIndex */
535 skip_nonspaces(&p); /* skip Stream */
537 skip_nonspaces(&p); /* skip Opts_SHA1 */
538 p++; /* skip space */
541 *fn++ = *p++; /* copy filename */
543 *fn = *p++; /* term filename and point to attribs */
546 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
548 jcr->FileIndex = file_index;
550 ar.fname = jcr->fname;
551 ar.FileIndex = file_index;
554 ar.JobId = jcr->JobId;
555 ar.ClientId = jcr->ClientId;
559 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
560 Dmsg1(120, "dird<filed: attr=%s\n", attr);
562 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
563 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
564 set_jcr_job_status(jcr, JS_Error);
567 jcr->FileId = ar.FileId;
568 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
569 if (jcr->FileIndex != (uint32_t)file_index) {
570 Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
571 file_index, jcr->FileIndex);
572 set_jcr_job_status(jcr, JS_Error);
575 db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
576 Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
577 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
578 stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
579 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
580 set_jcr_job_status(jcr, JS_Error);
583 jcr->jr.JobFiles = jcr->JobFiles = file_index;
584 jcr->jr.LastIndex = file_index;
586 if (is_bnet_error(fd)) {
587 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
589 set_jcr_job_status(jcr, JS_ErrorTerminated);
593 set_jcr_job_status(jcr, JS_Terminated);