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 static char levelcmd[] = "level = %s%s mtime_only=%d\n";
44 static char runbefore[] = "RunBeforeJob %s\n";
45 static char runafter[] = "RunAfterJob %s\n";
48 /* Responses received from File daemon */
49 static char OKinc[] = "2000 OK include\n";
50 static char OKexc[] = "2000 OK exclude\n";
51 static char OKjob[] = "2000 OK Job";
52 static char OKbootstrap[] = "2000 OK bootstrap\n";
53 static char OKlevel[] = "2000 OK level\n";
54 static char OKRunBefore[] = "2000 OK RunBefore\n";
55 static char OKRunAfter[] = "2000 OK RunAfter\n";
57 /* Forward referenced functions */
59 /* External functions */
60 extern int debug_level;
61 extern DIRRES *director;
62 extern int FDConnectTimeout;
68 * Open connection with File daemon.
69 * Try connecting every retry_interval (default 10 sec), and
70 * give up after max_retry_time (default 30 mins).
73 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
78 fd = bnet_connect(jcr, retry_interval, max_retry_time,
79 _("File daemon"), jcr->client->address,
80 NULL, jcr->client->FDport, verbose);
82 set_jcr_job_status(jcr, JS_ErrorTerminated);
85 Dmsg0(10, "Opened connection with File daemon\n");
86 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
88 set_jcr_job_status(jcr, JS_Running);
90 if (!authenticate_file_daemon(jcr)) {
91 set_jcr_job_status(jcr, JS_ErrorTerminated);
96 * Now send JobId and authorization key
98 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
99 jcr->VolSessionTime, jcr->sd_auth_key);
100 if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
101 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
103 Dmsg1(100, ">filed: %s", fd->msg);
104 if (bget_dirmsg(fd) > 0) {
105 Dmsg1(110, "<filed: %s", fd->msg);
106 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
107 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
108 jcr->client->hdr.name, fd->msg);
109 set_jcr_job_status(jcr, JS_ErrorTerminated);
111 } else if (jcr->db) {
113 memset(&cr, 0, sizeof(cr));
114 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
115 cr.AutoPrune = jcr->client->AutoPrune;
116 cr.FileRetention = jcr->client->FileRetention;
117 cr.JobRetention = jcr->client->JobRetention;
118 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
119 if (!db_update_client_record(jcr, jcr->db, &cr)) {
120 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
121 db_strerror(jcr->db));
125 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
127 set_jcr_job_status(jcr, JS_ErrorTerminated);
134 * This subroutine edits the last job start time into a
135 * "since=date/time" buffer that is returned in the
136 * variable since. This is used for display purposes in
137 * the job report. The time in jcr->stime is later
138 * passed to tell the File daemon what to do.
140 void get_level_since_time(JCR *jcr, char *since, int since_len)
143 * 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 */
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.Level = L_FULL;
163 bstrncpy(since, ", since=", since_len);
164 bstrncat(since, jcr->stime, since_len);
166 Dmsg1(100, "Last start time = %s\n", jcr->stime);
173 * Send level command to FD.
174 * Used for backup jobs and estimate command.
176 int send_level_command(JCR *jcr)
178 BSOCK *fd = jcr->file_bsock;
182 * Send Level command to File daemon
184 switch (jcr->JobLevel) {
186 bnet_fsend(fd, levelcmd, "base", " ", 0);
188 /* L_NONE is the console, sending something off to the FD */
191 bnet_fsend(fd, levelcmd, "full", " ", 0);
195 // bnet_fsend(fd, levelcmd, "since ", jcr->stime, 0); /* old code, deprecated */
196 stime = str_to_utime(jcr->stime);
197 bnet_fsend(fd, levelcmd, "since_utime ", edit_uint64(stime, ed1), 0);
198 while (bget_dirmsg(fd) >= 0) { /* allow him to poll us to sync clocks */
199 Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
204 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
205 jcr->JobLevel, jcr->JobLevel);
208 Dmsg1(120, ">filed: %s", fd->msg);
209 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
217 * Send either an Included or an Excluded list to FD
219 static int send_list(JCR *jcr, int list)
225 fd = jcr->file_bsock;
226 fileset = jcr->fileset;
228 if (list == INC_LIST) {
229 num = fileset->num_includes;
231 num = fileset->num_excludes;
234 for (int i=0; i<num; i++) {
243 if (list == INC_LIST) {
244 ie = fileset->include_items[i];
246 ie = fileset->exclude_items[i];
248 for (int j=0; j<ie->name_list.size(); j++) {
249 p = (char *)ie->name_list.get(j);
252 p++; /* skip over the | */
253 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
254 bpipe = open_bpipe(fd->msg, 0, "r");
256 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
260 /* Copy File options */
262 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
263 bstrncat(buf, " ", sizeof(buf));
265 bstrncpy(buf, "0 ", sizeof(buf));
267 Dmsg1(100, "Opts=%s\n", buf);
268 optlen = strlen(buf);
269 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
270 fd->msglen = Mmsg(&fd->msg, "%s", buf);
271 Dmsg2(200, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
272 if (!bnet_send(fd)) {
273 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
277 if ((stat=close_bpipe(bpipe)) != 0) {
278 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
279 p, stat, strerror(errno));
284 p++; /* skip over < */
285 if ((ffd = fopen(p, "r")) == NULL) {
286 Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
287 list==INC_LIST?"included":"excluded", p, strerror(errno));
290 /* Copy File options */
292 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
293 bstrncat(buf, " ", sizeof(buf));
295 bstrncpy(buf, "0 ", sizeof(buf));
297 Dmsg1(100, "Opts=%s\n", buf);
298 optlen = strlen(buf);
299 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
300 fd->msglen = Mmsg(&fd->msg, "%s", buf);
301 if (!bnet_send(fd)) {
302 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
309 p++; /* skip over \ */
310 /* Note, fall through wanted */
313 Dmsg2(100, "numopts=%d opts=%s\n", ie->num_opts, NPRT(ie->opts_list[0]->opts));
314 pm_strcpy(&fd->msg, ie->opts_list[0]->opts);
315 pm_strcat(&fd->msg, " ");
317 pm_strcpy(&fd->msg, "0 ");
319 fd->msglen = pm_strcat(&fd->msg, p);
320 Dmsg1(100, "Inc/Exc name=%s\n", fd->msg);
321 if (!bnet_send(fd)) {
322 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
329 bnet_sig(fd, BNET_EOD); /* end of data */
330 if (list == INC_LIST) {
331 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
334 } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
340 set_jcr_job_status(jcr, JS_ErrorTerminated);
347 * Send either an Included or an Excluded list to FD
349 static int send_fileset(JCR *jcr)
351 FILESET *fileset = jcr->fileset;
352 BSOCK *fd = jcr->file_bsock;
353 int num = fileset->num_includes;
355 for (int i=0; i<num; i++) {
364 ie = fileset->include_items[i];
365 bnet_fsend(fd, "I\n");
367 for (j=0; j<ie->num_opts; j++) {
368 FOPTS *fo = ie->opts_list[j];
369 bnet_fsend(fd, "O %s\n", fo->opts);
370 for (k=0; k<fo->regex.size(); k++) {
371 bnet_fsend(fd, "R %s\n", fo->regex.get(k));
373 for (k=0; k<fo->wild.size(); k++) {
374 bnet_fsend(fd, "W %s\n", fo->wild.get(k));
376 for (k=0; k<fo->base.size(); k++) {
377 bnet_fsend(fd, "B %s\n", fo->base.get(k));
379 bnet_fsend(fd, "N\n");
382 for (j=0; j<ie->name_list.size(); j++) {
383 p = (char *)ie->name_list.get(j);
386 p++; /* skip over the | */
387 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
388 bpipe = open_bpipe(fd->msg, 0, "r");
390 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
394 bstrncpy(buf, "F ", sizeof(buf));
395 Dmsg1(100, "Opts=%s\n", buf);
396 optlen = strlen(buf);
397 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
398 fd->msglen = Mmsg(&fd->msg, "%s", buf);
399 Dmsg2(200, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
400 if (!bnet_send(fd)) {
401 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
405 if ((stat=close_bpipe(bpipe)) != 0) {
406 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
407 p, stat, strerror(errno));
412 p++; /* skip over < */
413 if ((ffd = fopen(p, "r")) == NULL) {
414 Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
418 bstrncpy(buf, "F ", sizeof(buf));
419 Dmsg1(100, "Opts=%s\n", buf);
420 optlen = strlen(buf);
421 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
422 fd->msglen = Mmsg(&fd->msg, "%s", buf);
423 if (!bnet_send(fd)) {
424 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
431 p++; /* skip over \ */
432 /* Note, fall through wanted */
434 pm_strcpy(&fd->msg, "F ");
435 fd->msglen = pm_strcat(&fd->msg, p);
436 Dmsg1(100, "Inc/Exc name=%s\n", fd->msg);
437 if (!bnet_send(fd)) {
438 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
444 bnet_fsend(fd, "N\n");
447 bnet_sig(fd, BNET_EOD); /* end of data */
448 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
454 set_jcr_job_status(jcr, JS_ErrorTerminated);
461 * Send include list to File daemon
463 int send_include_list(JCR *jcr)
465 BSOCK *fd = jcr->file_bsock;
466 if (jcr->fileset->new_include) {
467 bnet_fsend(fd, fileset);
468 return send_fileset(jcr);
472 return send_list(jcr, INC_LIST);
477 * Send exclude list to File daemon
479 int send_exclude_list(JCR *jcr)
481 BSOCK *fd = jcr->file_bsock;
483 return send_list(jcr, EXC_LIST);
488 * Send bootstrap file if any to the File daemon.
489 * This is used for restore and verify VolumeToCatalog
491 int send_bootstrap_file(JCR *jcr)
495 BSOCK *fd = jcr->file_bsock;
496 char *bootstrap = "bootstrap\n";
498 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
499 if (!jcr->RestoreBootstrap) {
502 bs = fopen(jcr->RestoreBootstrap, "r");
504 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
505 jcr->RestoreBootstrap, strerror(errno));
506 set_jcr_job_status(jcr, JS_ErrorTerminated);
509 bnet_fsend(fd, bootstrap);
510 while (fgets(buf, sizeof(buf), bs)) {
511 bnet_fsend(fd, "%s", buf);
513 bnet_sig(fd, BNET_EOD);
515 if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
516 set_jcr_job_status(jcr, JS_ErrorTerminated);
523 * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
525 int send_run_before_and_after_commands(JCR *jcr)
527 POOLMEM *msg = get_pool_memory(PM_FNAME);
528 BSOCK *fd = jcr->file_bsock;
529 if (jcr->job->ClientRunBeforeJob) {
530 pm_strcpy(&msg, jcr->job->ClientRunBeforeJob);
532 bnet_fsend(fd, runbefore, msg);
533 if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
534 set_jcr_job_status(jcr, JS_ErrorTerminated);
535 free_pool_memory(msg);
539 if (jcr->job->ClientRunAfterJob) {
540 fd->msglen = pm_strcpy(&msg, jcr->job->ClientRunAfterJob);
542 bnet_fsend(fd, runafter, msg);
543 if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
544 set_jcr_job_status(jcr, JS_ErrorTerminated);
545 free_pool_memory(msg);
549 free_pool_memory(msg);
555 * Read the attributes from the File daemon for
556 * a Verify job and store them in the catalog.
558 int get_attributes_and_put_in_catalog(JCR *jcr)
564 fd = jcr->file_bsock;
565 jcr->jr.FirstIndex = 1;
566 memset(&ar, 0, sizeof(ar));
569 Dmsg0(120, "bdird: waiting to receive file attributes\n");
570 /* Pickup file attributes and signature */
571 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
573 /*****FIXME****** improve error handling to stop only on
574 * really fatal problems, or the number of errors is too
580 char Opts_SIG[MAXSTRING]; /* either Verify opts or MD5/SHA1 signature */
583 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
584 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
585 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
586 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
587 set_jcr_job_status(jcr, JS_ErrorTerminated);
591 skip_nonspaces(&p); /* skip FileIndex */
593 skip_nonspaces(&p); /* skip Stream */
595 skip_nonspaces(&p); /* skip Opts_SHA1 */
596 p++; /* skip space */
599 *fn++ = *p++; /* copy filename */
601 *fn = *p++; /* term filename and point to attribs */
604 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
606 jcr->FileIndex = file_index;
608 ar.fname = jcr->fname;
609 ar.FileIndex = file_index;
612 ar.JobId = jcr->JobId;
613 ar.ClientId = jcr->ClientId;
617 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
618 Dmsg1(120, "dird<filed: attr=%s\n", attr);
620 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
621 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
622 set_jcr_job_status(jcr, JS_Error);
625 jcr->FileId = ar.FileId;
626 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
627 if (jcr->FileIndex != (uint32_t)file_index) {
628 Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
629 file_index, jcr->FileIndex);
630 set_jcr_job_status(jcr, JS_Error);
633 db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
634 Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
635 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
636 stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
637 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
638 set_jcr_job_status(jcr, JS_Error);
641 jcr->jr.JobFiles = jcr->JobFiles = file_index;
642 jcr->jr.LastIndex = file_index;
644 if (is_bnet_error(fd)) {
645 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
647 set_jcr_job_status(jcr, JS_ErrorTerminated);
651 set_jcr_job_status(jcr, JS_Terminated);