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-2003 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 jobcmd[] = "JobId=%d Job=%s SDid=%u SDtime=%u Authorization=%s\n";
42 static char levelcmd[] = "level = %s%s mtime_only=%d\n";
45 /* Responses received from File daemon */
46 static char OKinc[] = "2000 OK include\n";
47 static char OKexc[] = "2000 OK exclude\n";
48 static char OKjob[] = "2000 OK Job";
49 static char OKbootstrap[] = "2000 OK bootstrap\n";
50 static char OKlevel[] = "2000 OK level\n";
52 /* Forward referenced functions */
54 /* External functions */
55 extern int debug_level;
56 extern DIRRES *director;
57 extern int FDConnectTimeout;
63 * Open connection with File daemon.
64 * Try connecting every 10 seconds, give up after 1 hour.
67 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
72 fd = bnet_connect(jcr, retry_interval, max_retry_time,
73 _("File daemon"), jcr->client->address,
74 NULL, jcr->client->FDport, verbose);
76 set_jcr_job_status(jcr, JS_ErrorTerminated);
79 Dmsg0(10, "Opened connection with File daemon\n");
80 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
82 set_jcr_job_status(jcr, JS_Running);
84 if (!authenticate_file_daemon(jcr)) {
85 set_jcr_job_status(jcr, JS_ErrorTerminated);
90 * Now send JobId and authorization key
92 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
93 jcr->VolSessionTime, jcr->sd_auth_key);
94 if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
95 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
97 Dmsg1(100, ">filed: %s", fd->msg);
98 if (bnet_recv(fd) > 0) {
99 Dmsg1(110, "<filed: %s", fd->msg);
100 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
101 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
102 jcr->client->hdr.name, fd->msg);
103 set_jcr_job_status(jcr, JS_ErrorTerminated);
105 } else if (jcr->db) {
107 memset(&cr, 0, sizeof(cr));
108 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
109 cr.AutoPrune = jcr->client->AutoPrune;
110 cr.FileRetention = jcr->client->FileRetention;
111 cr.JobRetention = jcr->client->JobRetention;
112 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
113 if (!db_update_client_record(jcr, jcr->db, &cr)) {
114 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
115 db_strerror(jcr->db));
119 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
121 set_jcr_job_status(jcr, JS_ErrorTerminated);
128 void get_level_since_time(JCR *jcr, char *since, int since_len)
131 * FULL backup job to get the time/date for a
132 * differential or incremental save.
135 jcr->stime = get_pool_memory(PM_MESSAGE);
139 switch (jcr->JobLevel) {
142 /* Look up start time of last job */
144 if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
145 Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
146 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
147 bsnprintf(since, since_len, " (upgraded from %s)",
148 level_to_str(jcr->JobLevel));
149 jcr->JobLevel = jcr->jr.Level = L_FULL;
151 bstrncpy(since, ", since=", sizeof(since));
152 bstrncat(since, jcr->stime, sizeof(since));
154 Dmsg1(115, "Last start time = %s\n", jcr->stime);
161 * Send level command for backup and estimate
163 int send_level_command(JCR *jcr)
165 BSOCK *fd = jcr->file_bsock;
167 * Send Level command to File daemon
169 switch (jcr->JobLevel) {
171 bnet_fsend(fd, levelcmd, "base", " ", 0);
174 bnet_fsend(fd, levelcmd, "full", " ", 0);
178 bnet_fsend(fd, levelcmd, "since ", jcr->stime, 0);
182 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
183 jcr->JobLevel, jcr->JobLevel);
186 Dmsg1(120, ">filed: %s", fd->msg);
187 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
195 * Send either an Included or an Excluded list to FD
197 static int send_list(JCR *jcr, int list)
203 fd = jcr->file_bsock;
204 fileset = jcr->fileset;
206 if (list == INC_LIST) {
207 num = fileset->num_includes;
209 num = fileset->num_excludes;
212 for (int i=0; i < num; i++) {
220 if (list == INC_LIST) {
221 ie = fileset->include_items[i];
223 ie = fileset->exclude_items[i];
225 for (int j=0; j<ie->num_names; j++) {
226 p = ie->name_list[j];
229 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
230 bpipe = open_bpipe(fd->msg, 0, "r");
232 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
236 /* Copy File options */
238 strcpy(buf, ie->opts_list[0]->opts);
243 optlen = strlen(buf);
244 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
245 fd->msglen = Mmsg(&fd->msg, "%s", buf);
246 Dmsg2(200, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
247 if (!bnet_send(fd)) {
248 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
252 if ((stat=close_bpipe(bpipe)) != 0) {
253 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
254 p, stat, strerror(errno));
259 p++; /* skip over < */
260 if ((ffd = fopen(p, "r")) == NULL) {
261 Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
262 list==INC_LIST?"included":"excluded", p, strerror(errno));
265 /* Copy File options */
267 strcpy(buf, ie->opts_list[0]->opts);
272 optlen = strlen(buf);
273 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
274 fd->msglen = Mmsg(&fd->msg, "%s", buf);
275 if (!bnet_send(fd)) {
276 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
283 p++; /* skip over \ */
284 /* Note, fall through wanted */
287 pm_strcpy(&fd->msg, ie->opts_list[0]->opts);
288 pm_strcat(&fd->msg, " ");
290 pm_strcpy(&fd->msg, "0 ");
292 pm_strcat(&fd->msg, p);
293 Dmsg1(100, "Inc/Exc name=%s\n", fd->msg);
294 fd->msglen = strlen(fd->msg);
295 if (!bnet_send(fd)) {
296 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
303 bnet_sig(fd, BNET_EOD); /* end of data */
304 if (list == INC_LIST) {
305 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
308 } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
314 set_jcr_job_status(jcr, JS_ErrorTerminated);
320 * Send include list to File daemon
322 int send_include_list(JCR *jcr)
324 BSOCK *fd = jcr->file_bsock;
325 fd->msglen = sprintf(fd->msg, inc);
327 return send_list(jcr, INC_LIST);
332 * Send exclude list to File daemon
334 int send_exclude_list(JCR *jcr)
336 BSOCK *fd = jcr->file_bsock;
337 fd->msglen = sprintf(fd->msg, exc);
339 return send_list(jcr, EXC_LIST);
344 * Send bootstrap file if any to the File daemon.
345 * This is used for restore and verify VolumeToCatalog
347 int send_bootstrap_file(JCR *jcr)
351 BSOCK *fd = jcr->file_bsock;
352 char *bootstrap = "bootstrap\n";
354 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
355 if (!jcr->RestoreBootstrap) {
358 bs = fopen(jcr->RestoreBootstrap, "r");
360 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
361 jcr->RestoreBootstrap, strerror(errno));
362 set_jcr_job_status(jcr, JS_ErrorTerminated);
365 strcpy(fd->msg, bootstrap);
366 fd->msglen = strlen(fd->msg);
368 while (fgets(buf, sizeof(buf), bs)) {
369 fd->msglen = Mmsg(&fd->msg, "%s", buf);
372 bnet_sig(fd, BNET_EOD);
374 if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
375 set_jcr_job_status(jcr, JS_ErrorTerminated);
383 * Read the attributes from the File daemon for
384 * a Verify job and store them in the catalog.
386 int get_attributes_and_put_in_catalog(JCR *jcr)
392 fd = jcr->file_bsock;
393 jcr->jr.FirstIndex = 1;
394 memset(&ar, 0, sizeof(ar));
397 Dmsg0(120, "bdird: waiting to receive file attributes\n");
398 /* Pickup file attributes and signature */
399 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
401 /*****FIXME****** improve error handling to stop only on
402 * really fatal problems, or the number of errors is too
408 char Opts_SIG[MAXSTRING]; /* either Verify opts or MD5/SHA1 signature */
411 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
412 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
413 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
414 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
415 set_jcr_job_status(jcr, JS_ErrorTerminated);
419 skip_nonspaces(&p); /* skip FileIndex */
421 skip_nonspaces(&p); /* skip Stream */
423 skip_nonspaces(&p); /* skip Opts_SHA1 */
424 p++; /* skip space */
427 *fn++ = *p++; /* copy filename */
429 *fn = *p++; /* term filename and point to attribs */
432 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
434 jcr->FileIndex = file_index;
436 ar.fname = jcr->fname;
437 ar.FileIndex = file_index;
440 ar.JobId = jcr->JobId;
441 ar.ClientId = jcr->ClientId;
445 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
446 Dmsg1(120, "dird<filed: attr=%s\n", attr);
448 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
449 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
450 set_jcr_job_status(jcr, JS_Error);
453 jcr->FileId = ar.FileId;
454 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
455 if (jcr->FileIndex != (uint32_t)file_index) {
456 Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
457 file_index, jcr->FileIndex);
458 set_jcr_job_status(jcr, JS_Error);
461 db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
462 Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
463 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
464 stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
465 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
466 set_jcr_job_status(jcr, JS_Error);
469 jcr->jr.JobFiles = jcr->JobFiles = file_index;
470 jcr->jr.LastIndex = file_index;
472 if (is_bnet_error(fd)) {
473 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
475 set_jcr_job_status(jcr, JS_ErrorTerminated);
479 set_jcr_job_status(jcr, JS_Terminated);