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 if (!jcr->file_bsock) {
80 fd = bnet_connect(jcr, retry_interval, max_retry_time,
81 _("File daemon"), jcr->client->address,
82 NULL, jcr->client->FDport, verbose);
84 set_jcr_job_status(jcr, JS_ErrorTerminated);
87 Dmsg0(10, "Opened connection with File daemon\n");
89 fd = jcr->file_bsock; /* use existing connection */
91 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
93 set_jcr_job_status(jcr, JS_Running);
95 if (!authenticate_file_daemon(jcr)) {
96 set_jcr_job_status(jcr, JS_ErrorTerminated);
101 * Now send JobId and authorization key
103 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
104 jcr->VolSessionTime, jcr->sd_auth_key);
105 if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
106 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
108 Dmsg1(100, ">filed: %s", fd->msg);
109 if (bget_dirmsg(fd) > 0) {
110 Dmsg1(110, "<filed: %s", fd->msg);
111 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
112 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
113 jcr->client->hdr.name, fd->msg);
114 set_jcr_job_status(jcr, JS_ErrorTerminated);
116 } else if (jcr->db) {
118 memset(&cr, 0, sizeof(cr));
119 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
120 cr.AutoPrune = jcr->client->AutoPrune;
121 cr.FileRetention = jcr->client->FileRetention;
122 cr.JobRetention = jcr->client->JobRetention;
123 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
124 if (!db_update_client_record(jcr, jcr->db, &cr)) {
125 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
126 db_strerror(jcr->db));
130 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
132 set_jcr_job_status(jcr, JS_ErrorTerminated);
139 * This subroutine edits the last job start time into a
140 * "since=date/time" buffer that is returned in the
141 * variable since. This is used for display purposes in
142 * the job report. The time in jcr->stime is later
143 * passed to tell the File daemon what to do.
145 void get_level_since_time(JCR *jcr, char *since, int since_len)
147 /* Lookup the last FULL backup job to get the time/date for a
148 * differential or incremental save.
151 jcr->stime = get_pool_memory(PM_MESSAGE);
155 switch (jcr->JobLevel) {
158 /* Look up start time of last job */
159 jcr->jr.JobId = 0; /* flag for db_find_job_start time */
160 if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
161 Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
162 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
163 bsnprintf(since, since_len, " (upgraded from %s)",
164 level_to_str(jcr->JobLevel));
165 jcr->JobLevel = jcr->jr.JobLevel = L_FULL;
167 bstrncpy(since, ", since=", since_len);
168 bstrncat(since, jcr->stime, since_len);
170 jcr->jr.JobId = jcr->JobId;
173 Dmsg2(100, "Level=%c last start time=%s\n", jcr->JobLevel, jcr->stime);
178 * Send level command to FD.
179 * Used for backup jobs and estimate command.
181 int send_level_command(JCR *jcr)
183 BSOCK *fd = jcr->file_bsock;
187 * Send Level command to File daemon
189 switch (jcr->JobLevel) {
191 bnet_fsend(fd, levelcmd, "base", " ", 0);
193 /* L_NONE is the console, sending something off to the FD */
196 bnet_fsend(fd, levelcmd, "full", " ", 0);
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);
208 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
209 jcr->JobLevel, jcr->JobLevel);
212 Dmsg1(120, ">filed: %s", fd->msg);
213 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
221 * Send either an Included or an Excluded list to FD
223 static int send_list(JCR *jcr, int list)
229 fd = jcr->file_bsock;
230 fileset = jcr->fileset;
232 if (list == INC_LIST) {
233 num = fileset->num_includes;
235 num = fileset->num_excludes;
238 for (int i=0; i<num; i++) {
247 if (list == INC_LIST) {
248 ie = fileset->include_items[i];
250 ie = fileset->exclude_items[i];
252 for (int j=0; j<ie->name_list.size(); j++) {
253 p = (char *)ie->name_list.get(j);
256 p++; /* skip over the | */
257 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
258 bpipe = open_bpipe(fd->msg, 0, "r");
261 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
265 /* Copy File options */
267 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
268 bstrncat(buf, " ", sizeof(buf));
270 bstrncpy(buf, "0 ", sizeof(buf));
272 Dmsg1(500, "Opts=%s\n", buf);
273 optlen = strlen(buf);
274 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
275 fd->msglen = Mmsg(fd->msg, "%s", buf);
276 Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
277 if (!bnet_send(fd)) {
278 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
282 if ((stat=close_bpipe(bpipe)) != 0) {
284 Jmsg(jcr, M_FATAL, 0, _("Error running program %p: ERR=%s\n"),
285 p, be.strerror(stat));
290 p++; /* skip over < */
291 if ((ffd = fopen(p, "r")) == NULL) {
292 Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
293 list==INC_LIST?"included":"excluded", p, strerror(errno));
296 /* Copy File options */
298 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
299 bstrncat(buf, " ", sizeof(buf));
301 bstrncpy(buf, "0 ", sizeof(buf));
303 Dmsg1(500, "Opts=%s\n", buf);
304 optlen = strlen(buf);
305 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
306 fd->msglen = Mmsg(fd->msg, "%s", buf);
307 if (!bnet_send(fd)) {
308 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
315 p++; /* skip over \ */
316 /* Note, fall through wanted */
319 Dmsg2(500, "numopts=%d opts=%s\n", ie->num_opts, NPRT(ie->opts_list[0]->opts));
320 pm_strcpy(fd->msg, ie->opts_list[0]->opts);
321 pm_strcat(fd->msg, " ");
323 pm_strcpy(fd->msg, "0 ");
325 fd->msglen = pm_strcat(fd->msg, p);
326 Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
327 if (!bnet_send(fd)) {
328 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
335 bnet_sig(fd, BNET_EOD); /* end of data */
336 if (list == INC_LIST) {
337 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
340 } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
346 set_jcr_job_status(jcr, JS_ErrorTerminated);
353 * Send either an Included or an Excluded list to FD
355 static int send_fileset(JCR *jcr)
357 FILESET *fileset = jcr->fileset;
358 BSOCK *fd = jcr->file_bsock;
364 num = fileset->num_includes;
366 num = fileset->num_excludes;
368 for (int i=0; i<num; i++) {
378 ie = fileset->include_items[i];
379 bnet_fsend(fd, "I\n");
381 ie = fileset->exclude_items[i];
382 bnet_fsend(fd, "E\n");
384 for (j=0; j<ie->num_opts; j++) {
385 FOPTS *fo = ie->opts_list[j];
386 bnet_fsend(fd, "O %s\n", fo->opts);
387 for (k=0; k<fo->regex.size(); k++) {
388 bnet_fsend(fd, "R %s\n", fo->regex.get(k));
390 for (k=0; k<fo->wild.size(); k++) {
391 bnet_fsend(fd, "W %s\n", fo->wild.get(k));
393 for (k=0; k<fo->base.size(); k++) {
394 bnet_fsend(fd, "B %s\n", fo->base.get(k));
397 bnet_fsend(fd, "D %s\n", fo->reader);
400 bnet_fsend(fd, "T %s\n", fo->writer);
402 bnet_fsend(fd, "N\n");
405 for (j=0; j<ie->name_list.size(); j++) {
406 p = (char *)ie->name_list.get(j);
409 p++; /* skip over the | */
410 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
411 bpipe = open_bpipe(fd->msg, 0, "r");
414 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
418 bstrncpy(buf, "F ", sizeof(buf));
419 Dmsg1(500, "Opts=%s\n", buf);
420 optlen = strlen(buf);
421 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
422 fd->msglen = Mmsg(fd->msg, "%s", buf);
423 Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
424 if (!bnet_send(fd)) {
425 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
429 if ((stat=close_bpipe(bpipe)) != 0) {
431 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
432 p, be.strerror(stat));
437 p++; /* skip over < */
438 if ((ffd = fopen(p, "r")) == NULL) {
440 Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
444 bstrncpy(buf, "F ", sizeof(buf));
445 Dmsg1(500, "Opts=%s\n", buf);
446 optlen = strlen(buf);
447 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
448 fd->msglen = Mmsg(fd->msg, "%s", buf);
449 if (!bnet_send(fd)) {
450 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
457 p++; /* skip over \ */
458 /* Note, fall through wanted */
460 pm_strcpy(fd->msg, "F ");
461 fd->msglen = pm_strcat(fd->msg, p);
462 Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
463 if (!bnet_send(fd)) {
464 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
470 bnet_fsend(fd, "N\n");
472 if (!include) { /* If we just did excludes */
473 break; /* all done */
475 include = false; /* Now do excludes */
478 bnet_sig(fd, BNET_EOD); /* end of data */
479 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
485 set_jcr_job_status(jcr, JS_ErrorTerminated);
492 * Send include list to File daemon
494 int send_include_list(JCR *jcr)
496 BSOCK *fd = jcr->file_bsock;
497 if (jcr->fileset->new_include) {
498 bnet_fsend(fd, fileset);
499 return send_fileset(jcr);
503 return send_list(jcr, INC_LIST);
508 * Send exclude list to File daemon
510 int send_exclude_list(JCR *jcr)
512 BSOCK *fd = jcr->file_bsock;
513 if (jcr->fileset->new_include) {
517 return send_list(jcr, EXC_LIST);
522 * Send bootstrap file if any to the File daemon.
523 * This is used for restore and verify VolumeToCatalog
525 int send_bootstrap_file(JCR *jcr)
529 BSOCK *fd = jcr->file_bsock;
530 const char *bootstrap = "bootstrap\n";
532 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
533 if (!jcr->RestoreBootstrap) {
536 bs = fopen(jcr->RestoreBootstrap, "r");
539 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
540 jcr->RestoreBootstrap, be.strerror());
541 set_jcr_job_status(jcr, JS_ErrorTerminated);
544 bnet_fsend(fd, bootstrap);
545 while (fgets(buf, sizeof(buf), bs)) {
546 bnet_fsend(fd, "%s", buf);
548 bnet_sig(fd, BNET_EOD);
550 if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
551 set_jcr_job_status(jcr, JS_ErrorTerminated);
558 * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
560 int send_run_before_and_after_commands(JCR *jcr)
562 POOLMEM *msg = get_pool_memory(PM_FNAME);
563 BSOCK *fd = jcr->file_bsock;
564 if (jcr->job->ClientRunBeforeJob) {
565 pm_strcpy(msg, jcr->job->ClientRunBeforeJob);
567 bnet_fsend(fd, runbefore, msg);
568 if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
569 set_jcr_job_status(jcr, JS_ErrorTerminated);
570 free_pool_memory(msg);
574 if (jcr->job->ClientRunAfterJob) {
575 fd->msglen = pm_strcpy(msg, jcr->job->ClientRunAfterJob);
577 bnet_fsend(fd, runafter, msg);
578 if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
579 set_jcr_job_status(jcr, JS_ErrorTerminated);
580 free_pool_memory(msg);
584 free_pool_memory(msg);
590 * Read the attributes from the File daemon for
591 * a Verify job and store them in the catalog.
593 int get_attributes_and_put_in_catalog(JCR *jcr)
599 fd = jcr->file_bsock;
600 jcr->jr.FirstIndex = 1;
601 memset(&ar, 0, sizeof(ar));
604 Dmsg0(120, "bdird: waiting to receive file attributes\n");
605 /* Pickup file attributes and signature */
606 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
608 /*****FIXME****** improve error handling to stop only on
609 * really fatal problems, or the number of errors is too
615 char Opts_SIG[MAXSTRING]; /* either Verify opts or MD5/SHA1 signature */
618 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
619 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
620 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
621 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
622 set_jcr_job_status(jcr, JS_ErrorTerminated);
626 skip_nonspaces(&p); /* skip FileIndex */
628 skip_nonspaces(&p); /* skip Stream */
630 skip_nonspaces(&p); /* skip Opts_SHA1 */
631 p++; /* skip space */
634 *fn++ = *p++; /* copy filename */
636 *fn = *p++; /* term filename and point to attribs */
639 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
641 jcr->FileIndex = file_index;
643 ar.fname = jcr->fname;
644 ar.FileIndex = file_index;
647 ar.JobId = jcr->JobId;
648 ar.ClientId = jcr->ClientId;
652 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
653 Dmsg1(120, "dird<filed: attr=%s\n", attr);
655 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
656 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
657 set_jcr_job_status(jcr, JS_Error);
660 jcr->FileId = ar.FileId;
661 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
662 if (jcr->FileIndex != (uint32_t)file_index) {
663 Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
664 file_index, jcr->FileIndex);
665 set_jcr_job_status(jcr, JS_Error);
668 db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
669 Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
670 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
671 stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
672 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
673 set_jcr_job_status(jcr, JS_Error);
676 jcr->jr.JobFiles = jcr->JobFiles = file_index;
677 jcr->jr.LastIndex = file_index;
679 if (is_bnet_error(fd)) {
680 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
682 set_jcr_job_status(jcr, JS_ErrorTerminated);
686 set_jcr_job_status(jcr, JS_Terminated);