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-2004 Kern Sibbald and John Walker
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 inc[] = "include\n";
40 static char exc[] = "exclude\n";
41 static char fileset[] = "fileset\n"; /* set full fileset */
42 static char jobcmd[] = "JobId=%d Job=%s SDid=%u SDtime=%u Authorization=%s\n";
43 /* Note, mtime_only is not used here -- implemented as file option */
44 static char levelcmd[] = "level = %s%s mtime_only=%d\n";
45 static char runbefore[] = "RunBeforeJob %s\n";
46 static char runafter[] = "RunAfterJob %s\n";
49 /* Responses received from File daemon */
50 static char OKinc[] = "2000 OK include\n";
51 static char OKexc[] = "2000 OK exclude\n";
52 static char OKjob[] = "2000 OK Job";
53 static char OKbootstrap[] = "2000 OK bootstrap\n";
54 static char OKlevel[] = "2000 OK level\n";
55 static char OKRunBefore[] = "2000 OK RunBefore\n";
56 static char OKRunAfter[] = "2000 OK RunAfter\n";
58 /* Forward referenced functions */
60 /* External functions */
61 extern int debug_level;
62 extern DIRRES *director;
63 extern int FDConnectTimeout;
69 * Open connection with File daemon.
70 * Try connecting every retry_interval (default 10 sec), and
71 * give up after max_retry_time (default 30 mins).
74 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
79 fd = bnet_connect(jcr, retry_interval, max_retry_time,
80 _("File daemon"), jcr->client->address,
81 NULL, jcr->client->FDport, verbose);
83 set_jcr_job_status(jcr, JS_ErrorTerminated);
86 Dmsg0(10, "Opened connection with File daemon\n");
87 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
89 set_jcr_job_status(jcr, JS_Running);
91 if (!authenticate_file_daemon(jcr)) {
92 set_jcr_job_status(jcr, JS_ErrorTerminated);
97 * Now send JobId and authorization key
99 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
100 jcr->VolSessionTime, jcr->sd_auth_key);
101 if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
102 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
104 Dmsg1(100, ">filed: %s", fd->msg);
105 if (bget_dirmsg(fd) > 0) {
106 Dmsg1(110, "<filed: %s", fd->msg);
107 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
108 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
109 jcr->client->hdr.name, fd->msg);
110 set_jcr_job_status(jcr, JS_ErrorTerminated);
112 } else if (jcr->db) {
114 memset(&cr, 0, sizeof(cr));
115 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
116 cr.AutoPrune = jcr->client->AutoPrune;
117 cr.FileRetention = jcr->client->FileRetention;
118 cr.JobRetention = jcr->client->JobRetention;
119 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
120 if (!db_update_client_record(jcr, jcr->db, &cr)) {
121 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
122 db_strerror(jcr->db));
126 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
128 set_jcr_job_status(jcr, JS_ErrorTerminated);
135 * This subroutine edits the last job start time into a
136 * "since=date/time" buffer that is returned in the
137 * variable since. This is used for display purposes in
138 * the job report. The time in jcr->stime is later
139 * passed to tell the File daemon what to do.
141 void get_level_since_time(JCR *jcr, char *since, int since_len)
143 /* Lookup the last FULL backup job to get the time/date for a
144 * differential or incremental save.
147 jcr->stime = get_pool_memory(PM_MESSAGE);
151 switch (jcr->JobLevel) {
154 /* Look up start time of last job */
155 jcr->jr.JobId = 0; /* flag for db_find_job_start time */
156 if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
157 Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
158 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
159 bsnprintf(since, since_len, " (upgraded from %s)",
160 level_to_str(jcr->JobLevel));
161 jcr->JobLevel = jcr->jr.JobLevel = L_FULL;
163 bstrncpy(since, ", since=", since_len);
164 bstrncat(since, jcr->stime, since_len);
166 jcr->jr.JobId = jcr->JobId;
169 Dmsg2(100, "Level=%c last start time=%s\n", jcr->JobLevel, jcr->stime);
174 * Send level command to FD.
175 * Used for backup jobs and estimate command.
177 int send_level_command(JCR *jcr)
179 BSOCK *fd = jcr->file_bsock;
183 * Send Level command to File daemon
185 switch (jcr->JobLevel) {
187 bnet_fsend(fd, levelcmd, "base", " ", 0);
189 /* L_NONE is the console, sending something off to the FD */
192 bnet_fsend(fd, levelcmd, "full", " ", 0);
196 // bnet_fsend(fd, levelcmd, "since ", jcr->stime, 0); /* old code, deprecated */
197 stime = str_to_utime(jcr->stime);
198 bnet_fsend(fd, levelcmd, "since_utime ", edit_uint64(stime, ed1), 0);
199 while (bget_dirmsg(fd) >= 0) { /* allow him to poll us to sync clocks */
200 Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
205 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
206 jcr->JobLevel, jcr->JobLevel);
209 Dmsg1(120, ">filed: %s", fd->msg);
210 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
218 * Send either an Included or an Excluded list to FD
220 static int send_list(JCR *jcr, int list)
226 fd = jcr->file_bsock;
227 fileset = jcr->fileset;
229 if (list == INC_LIST) {
230 num = fileset->num_includes;
232 num = fileset->num_excludes;
235 for (int i=0; i<num; i++) {
244 if (list == INC_LIST) {
245 ie = fileset->include_items[i];
247 ie = fileset->exclude_items[i];
249 for (int j=0; j<ie->name_list.size(); j++) {
250 p = (char *)ie->name_list.get(j);
253 p++; /* skip over the | */
254 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
255 bpipe = open_bpipe(fd->msg, 0, "r");
258 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
262 /* Copy File options */
264 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
265 bstrncat(buf, " ", sizeof(buf));
267 bstrncpy(buf, "0 ", sizeof(buf));
269 Dmsg1(500, "Opts=%s\n", buf);
270 optlen = strlen(buf);
271 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
272 fd->msglen = Mmsg(&fd->msg, "%s", buf);
273 Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
274 if (!bnet_send(fd)) {
275 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
279 if ((stat=close_bpipe(bpipe)) != 0) {
282 Jmsg(jcr, M_FATAL, 0, _("Error running program %p: ERR=%s\n"),
288 p++; /* skip over < */
289 if ((ffd = fopen(p, "r")) == NULL) {
290 Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
291 list==INC_LIST?"included":"excluded", p, strerror(errno));
294 /* Copy File options */
296 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
297 bstrncat(buf, " ", sizeof(buf));
299 bstrncpy(buf, "0 ", sizeof(buf));
301 Dmsg1(500, "Opts=%s\n", buf);
302 optlen = strlen(buf);
303 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
304 fd->msglen = Mmsg(&fd->msg, "%s", buf);
305 if (!bnet_send(fd)) {
306 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
313 p++; /* skip over \ */
314 /* Note, fall through wanted */
317 Dmsg2(500, "numopts=%d opts=%s\n", ie->num_opts, NPRT(ie->opts_list[0]->opts));
318 pm_strcpy(&fd->msg, ie->opts_list[0]->opts);
319 pm_strcat(&fd->msg, " ");
321 pm_strcpy(&fd->msg, "0 ");
323 fd->msglen = pm_strcat(&fd->msg, p);
324 Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
325 if (!bnet_send(fd)) {
326 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
333 bnet_sig(fd, BNET_EOD); /* end of data */
334 if (list == INC_LIST) {
335 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
338 } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
344 set_jcr_job_status(jcr, JS_ErrorTerminated);
351 * Send either an Included or an Excluded list to FD
353 static int send_fileset(JCR *jcr)
355 FILESET *fileset = jcr->fileset;
356 BSOCK *fd = jcr->file_bsock;
362 num = fileset->num_includes;
364 num = fileset->num_excludes;
366 for (int i=0; i<num; i++) {
376 ie = fileset->include_items[i];
377 bnet_fsend(fd, "I\n");
379 ie = fileset->exclude_items[i];
380 bnet_fsend(fd, "E\n");
382 for (j=0; j<ie->num_opts; j++) {
383 FOPTS *fo = ie->opts_list[j];
384 bnet_fsend(fd, "O %s\n", fo->opts);
385 for (k=0; k<fo->regex.size(); k++) {
386 bnet_fsend(fd, "R %s\n", fo->regex.get(k));
388 for (k=0; k<fo->wild.size(); k++) {
389 bnet_fsend(fd, "W %s\n", fo->wild.get(k));
391 for (k=0; k<fo->base.size(); k++) {
392 bnet_fsend(fd, "B %s\n", fo->base.get(k));
394 bnet_fsend(fd, "N\n");
397 for (j=0; j<ie->name_list.size(); j++) {
398 p = (char *)ie->name_list.get(j);
401 p++; /* skip over the | */
402 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
403 bpipe = open_bpipe(fd->msg, 0, "r");
405 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
409 bstrncpy(buf, "F ", sizeof(buf));
410 Dmsg1(500, "Opts=%s\n", buf);
411 optlen = strlen(buf);
412 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
413 fd->msglen = Mmsg(&fd->msg, "%s", buf);
414 Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
415 if (!bnet_send(fd)) {
416 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
420 if ((stat=close_bpipe(bpipe)) != 0) {
423 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
429 p++; /* skip over < */
430 if ((ffd = fopen(p, "r")) == NULL) {
431 Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
435 bstrncpy(buf, "F ", sizeof(buf));
436 Dmsg1(500, "Opts=%s\n", buf);
437 optlen = strlen(buf);
438 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
439 fd->msglen = Mmsg(&fd->msg, "%s", buf);
440 if (!bnet_send(fd)) {
441 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
448 p++; /* skip over \ */
449 /* Note, fall through wanted */
451 pm_strcpy(&fd->msg, "F ");
452 fd->msglen = pm_strcat(&fd->msg, p);
453 Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
454 if (!bnet_send(fd)) {
455 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
461 bnet_fsend(fd, "N\n");
463 if (!include) { /* If we just did excludes */
464 break; /* all done */
466 include = false; /* Now do excludes */
469 bnet_sig(fd, BNET_EOD); /* end of data */
470 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
476 set_jcr_job_status(jcr, JS_ErrorTerminated);
483 * Send include list to File daemon
485 int send_include_list(JCR *jcr)
487 BSOCK *fd = jcr->file_bsock;
488 if (jcr->fileset->new_include) {
489 bnet_fsend(fd, fileset);
490 return send_fileset(jcr);
494 return send_list(jcr, INC_LIST);
499 * Send exclude list to File daemon
501 int send_exclude_list(JCR *jcr)
503 BSOCK *fd = jcr->file_bsock;
504 if (jcr->fileset->new_include) {
508 return send_list(jcr, EXC_LIST);
513 * Send bootstrap file if any to the File daemon.
514 * This is used for restore and verify VolumeToCatalog
516 int send_bootstrap_file(JCR *jcr)
520 BSOCK *fd = jcr->file_bsock;
521 const char *bootstrap = "bootstrap\n";
523 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
524 if (!jcr->RestoreBootstrap) {
527 bs = fopen(jcr->RestoreBootstrap, "r");
529 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
530 jcr->RestoreBootstrap, strerror(errno));
531 set_jcr_job_status(jcr, JS_ErrorTerminated);
534 bnet_fsend(fd, bootstrap);
535 while (fgets(buf, sizeof(buf), bs)) {
536 bnet_fsend(fd, "%s", buf);
538 bnet_sig(fd, BNET_EOD);
540 if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
541 set_jcr_job_status(jcr, JS_ErrorTerminated);
548 * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
550 int send_run_before_and_after_commands(JCR *jcr)
552 POOLMEM *msg = get_pool_memory(PM_FNAME);
553 BSOCK *fd = jcr->file_bsock;
554 if (jcr->job->ClientRunBeforeJob) {
555 pm_strcpy(&msg, jcr->job->ClientRunBeforeJob);
557 bnet_fsend(fd, runbefore, msg);
558 if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
559 set_jcr_job_status(jcr, JS_ErrorTerminated);
560 free_pool_memory(msg);
564 if (jcr->job->ClientRunAfterJob) {
565 fd->msglen = pm_strcpy(&msg, jcr->job->ClientRunAfterJob);
567 bnet_fsend(fd, runafter, msg);
568 if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
569 set_jcr_job_status(jcr, JS_ErrorTerminated);
570 free_pool_memory(msg);
574 free_pool_memory(msg);
580 * Read the attributes from the File daemon for
581 * a Verify job and store them in the catalog.
583 int get_attributes_and_put_in_catalog(JCR *jcr)
589 fd = jcr->file_bsock;
590 jcr->jr.FirstIndex = 1;
591 memset(&ar, 0, sizeof(ar));
594 Dmsg0(120, "bdird: waiting to receive file attributes\n");
595 /* Pickup file attributes and signature */
596 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
598 /*****FIXME****** improve error handling to stop only on
599 * really fatal problems, or the number of errors is too
605 char Opts_SIG[MAXSTRING]; /* either Verify opts or MD5/SHA1 signature */
608 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
609 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
610 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
611 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
612 set_jcr_job_status(jcr, JS_ErrorTerminated);
616 skip_nonspaces(&p); /* skip FileIndex */
618 skip_nonspaces(&p); /* skip Stream */
620 skip_nonspaces(&p); /* skip Opts_SHA1 */
621 p++; /* skip space */
624 *fn++ = *p++; /* copy filename */
626 *fn = *p++; /* term filename and point to attribs */
629 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
631 jcr->FileIndex = file_index;
633 ar.fname = jcr->fname;
634 ar.FileIndex = file_index;
637 ar.JobId = jcr->JobId;
638 ar.ClientId = jcr->ClientId;
642 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
643 Dmsg1(120, "dird<filed: attr=%s\n", attr);
645 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
646 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
647 set_jcr_job_status(jcr, JS_Error);
650 jcr->FileId = ar.FileId;
651 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
652 if (jcr->FileIndex != (uint32_t)file_index) {
653 Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
654 file_index, jcr->FileIndex);
655 set_jcr_job_status(jcr, JS_Error);
658 db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
659 Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
660 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
661 stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
662 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
663 set_jcr_job_status(jcr, JS_Error);
666 jcr->jr.JobFiles = jcr->JobFiles = file_index;
667 jcr->jr.LastIndex = file_index;
669 if (is_bnet_error(fd)) {
670 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
672 set_jcr_job_status(jcr, JS_ErrorTerminated);
676 set_jcr_job_status(jcr, JS_Terminated);