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";
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 OKexc[] = "2000 OK exclude\n";
50 static char OKjob[] = "2000 OK Job";
51 static char OKbootstrap[] = "2000 OK bootstrap\n";
52 static char OKlevel[] = "2000 OK level\n";
53 static char OKRunBefore[] = "2000 OK RunBefore\n";
54 static char OKRunAfter[] = "2000 OK RunAfter\n";
56 /* Forward referenced functions */
58 /* External functions */
59 extern int debug_level;
60 extern DIRRES *director;
61 extern int FDConnectTimeout;
67 * Open connection with File daemon.
68 * Try connecting every retry_interval (default 10 sec), and
69 * give up after max_retry_time (default 30 mins).
72 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
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");
85 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
87 set_jcr_job_status(jcr, JS_Running);
89 if (!authenticate_file_daemon(jcr)) {
90 set_jcr_job_status(jcr, JS_ErrorTerminated);
95 * Now send JobId and authorization key
97 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
98 jcr->VolSessionTime, jcr->sd_auth_key);
99 if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
100 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
102 Dmsg1(100, ">filed: %s", fd->msg);
103 if (bget_dirmsg(fd) > 0) {
104 Dmsg1(110, "<filed: %s", fd->msg);
105 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
106 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
107 jcr->client->hdr.name, fd->msg);
108 set_jcr_job_status(jcr, JS_ErrorTerminated);
110 } else if (jcr->db) {
112 memset(&cr, 0, sizeof(cr));
113 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
114 cr.AutoPrune = jcr->client->AutoPrune;
115 cr.FileRetention = jcr->client->FileRetention;
116 cr.JobRetention = jcr->client->JobRetention;
117 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
118 if (!db_update_client_record(jcr, jcr->db, &cr)) {
119 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
120 db_strerror(jcr->db));
124 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
126 set_jcr_job_status(jcr, JS_ErrorTerminated);
133 * This subroutine edits the last job start time into a
134 * "since=date/time" buffer that is returned in the
135 * variable since. This is used for display purposes in
136 * the job report. The time in jcr->stime is later
137 * passed to tell the File daemon what to do.
139 void get_level_since_time(JCR *jcr, char *since, int since_len)
142 * FULL backup job to get the time/date for a
143 * differential or incremental save.
146 jcr->stime = get_pool_memory(PM_MESSAGE);
150 switch (jcr->JobLevel) {
153 /* Look up start time of last job */
155 if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
156 Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
157 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
158 bsnprintf(since, since_len, " (upgraded from %s)",
159 level_to_str(jcr->JobLevel));
160 jcr->JobLevel = jcr->jr.Level = L_FULL;
162 bstrncpy(since, ", since=", since_len);
163 bstrncat(since, jcr->stime, since_len);
165 Dmsg1(100, "Last start time = %s\n", jcr->stime);
172 * Send level command to FD.
173 * Used for backup jobs and estimate command.
175 int send_level_command(JCR *jcr)
177 BSOCK *fd = jcr->file_bsock;
181 * Send Level command to File daemon
183 switch (jcr->JobLevel) {
185 bnet_fsend(fd, levelcmd, "base", " ", 0);
188 bnet_fsend(fd, levelcmd, "full", " ", 0);
192 // bnet_fsend(fd, levelcmd, "since ", jcr->stime, 0); /* old code, deprecated */
193 stime = str_to_utime(jcr->stime);
194 bnet_fsend(fd, levelcmd, "since_utime ", edit_uint64(stime, ed1), 0);
195 while (bget_dirmsg(fd) >= 0) { /* allow him to poll us to sync clocks */
196 Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
201 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
202 jcr->JobLevel, jcr->JobLevel);
205 Dmsg1(120, ">filed: %s", fd->msg);
206 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
214 * Send either an Included or an Excluded list to FD
216 static int send_list(JCR *jcr, int list)
222 fd = jcr->file_bsock;
223 fileset = jcr->fileset;
225 if (list == INC_LIST) {
226 num = fileset->num_includes;
228 num = fileset->num_excludes;
231 for (int i=0; i < num; i++) {
241 if (list == INC_LIST) {
242 ie = fileset->include_items[i];
244 ie = fileset->exclude_items[i];
246 fo = ie->opts_list[0];
247 for (int j=0; j<fo->match.size(); j++) {
248 Dmsg1(100, "Match=%s\n", fo->match.get(j));
250 for (int j=0; j<ie->name_list.size(); j++) {
251 p = (char *)ie->name_list.get(j);
254 p++; /* skip over the | */
255 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
256 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(100, "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(200, "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) {
280 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
281 p, stat, strerror(errno));
286 p++; /* skip over < */
287 if ((ffd = fopen(p, "r")) == NULL) {
288 Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
289 list==INC_LIST?"included":"excluded", p, strerror(errno));
292 /* Copy File options */
294 bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
295 bstrncat(buf, " ", sizeof(buf));
297 bstrncpy(buf, "0 ", sizeof(buf));
299 Dmsg1(100, "Opts=%s\n", buf);
300 optlen = strlen(buf);
301 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
302 fd->msglen = Mmsg(&fd->msg, "%s", buf);
303 if (!bnet_send(fd)) {
304 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
311 p++; /* skip over \ */
312 /* Note, fall through wanted */
314 Dmsg2(100, "numopts=%d opts=%s\n", ie->num_opts, NPRT(ie->opts_list[0]->opts));
316 pm_strcpy(&fd->msg, ie->opts_list[0]->opts);
317 pm_strcat(&fd->msg, " ");
319 pm_strcpy(&fd->msg, "0 ");
321 fd->msglen = pm_strcat(&fd->msg, p);
322 Dmsg1(100, "Inc/Exc name=%s\n", fd->msg);
323 if (!bnet_send(fd)) {
324 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
331 bnet_sig(fd, BNET_EOD); /* end of data */
332 if (list == INC_LIST) {
333 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
336 } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
342 set_jcr_job_status(jcr, JS_ErrorTerminated);
348 * Send include list to File daemon
350 int send_include_list(JCR *jcr)
352 BSOCK *fd = jcr->file_bsock;
353 fd->msglen = pm_strcpy(&fd->msg, inc);
355 return send_list(jcr, INC_LIST);
360 * Send exclude list to File daemon
362 int send_exclude_list(JCR *jcr)
364 BSOCK *fd = jcr->file_bsock;
365 fd->msglen = pm_strcpy(&fd->msg, exc);
367 return send_list(jcr, EXC_LIST);
372 * Send bootstrap file if any to the File daemon.
373 * This is used for restore and verify VolumeToCatalog
375 int send_bootstrap_file(JCR *jcr)
379 BSOCK *fd = jcr->file_bsock;
380 char *bootstrap = "bootstrap\n";
382 Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
383 if (!jcr->RestoreBootstrap) {
386 bs = fopen(jcr->RestoreBootstrap, "r");
388 Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
389 jcr->RestoreBootstrap, strerror(errno));
390 set_jcr_job_status(jcr, JS_ErrorTerminated);
393 bnet_fsend(fd, bootstrap);
394 while (fgets(buf, sizeof(buf), bs)) {
395 bnet_fsend(fd, "%s", buf);
397 bnet_sig(fd, BNET_EOD);
399 if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
400 set_jcr_job_status(jcr, JS_ErrorTerminated);
407 * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
409 int send_run_before_and_after_commands(JCR *jcr)
411 POOLMEM *msg = get_pool_memory(PM_FNAME);
412 BSOCK *fd = jcr->file_bsock;
413 if (jcr->job->ClientRunBeforeJob) {
414 pm_strcpy(&msg, jcr->job->ClientRunBeforeJob);
416 bnet_fsend(fd, runbefore, msg);
417 if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
418 set_jcr_job_status(jcr, JS_ErrorTerminated);
419 free_pool_memory(msg);
423 if (jcr->job->ClientRunAfterJob) {
424 fd->msglen = pm_strcpy(&msg, jcr->job->ClientRunAfterJob);
426 bnet_fsend(fd, runafter, msg);
427 if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
428 set_jcr_job_status(jcr, JS_ErrorTerminated);
429 free_pool_memory(msg);
433 free_pool_memory(msg);
439 * Read the attributes from the File daemon for
440 * a Verify job and store them in the catalog.
442 int get_attributes_and_put_in_catalog(JCR *jcr)
448 fd = jcr->file_bsock;
449 jcr->jr.FirstIndex = 1;
450 memset(&ar, 0, sizeof(ar));
453 Dmsg0(120, "bdird: waiting to receive file attributes\n");
454 /* Pickup file attributes and signature */
455 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
457 /*****FIXME****** improve error handling to stop only on
458 * really fatal problems, or the number of errors is too
464 char Opts_SIG[MAXSTRING]; /* either Verify opts or MD5/SHA1 signature */
467 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
468 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
469 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
470 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
471 set_jcr_job_status(jcr, JS_ErrorTerminated);
475 skip_nonspaces(&p); /* skip FileIndex */
477 skip_nonspaces(&p); /* skip Stream */
479 skip_nonspaces(&p); /* skip Opts_SHA1 */
480 p++; /* skip space */
483 *fn++ = *p++; /* copy filename */
485 *fn = *p++; /* term filename and point to attribs */
488 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
490 jcr->FileIndex = file_index;
492 ar.fname = jcr->fname;
493 ar.FileIndex = file_index;
496 ar.JobId = jcr->JobId;
497 ar.ClientId = jcr->ClientId;
501 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
502 Dmsg1(120, "dird<filed: attr=%s\n", attr);
504 if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
505 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
506 set_jcr_job_status(jcr, JS_Error);
509 jcr->FileId = ar.FileId;
510 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
511 if (jcr->FileIndex != (uint32_t)file_index) {
512 Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
513 file_index, jcr->FileIndex);
514 set_jcr_job_status(jcr, JS_Error);
517 db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
518 Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
519 if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
520 stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
521 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
522 set_jcr_job_status(jcr, JS_Error);
525 jcr->jr.JobFiles = jcr->JobFiles = file_index;
526 jcr->jr.LastIndex = file_index;
528 if (is_bnet_error(fd)) {
529 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
531 set_jcr_job_status(jcr, JS_ErrorTerminated);
535 set_jcr_job_status(jcr, JS_Terminated);