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, 2001, 2002 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";
44 /* Responses received from File daemon */
45 static char OKinc[] = "2000 OK include\n";
46 static char OKexc[] = "2000 OK exclude\n";
47 static char OKjob[] = "2000 OK Job\n";
49 /* Forward referenced functions */
51 /* External functions */
52 extern int debug_level;
53 extern DIRRES *director;
54 extern int FDConnectTimeout;
57 * Open connection with File daemon.
58 * Try connecting every 10 seconds, give up after 1 hour.
61 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
66 fd = bnet_connect(jcr, retry_interval, max_retry_time,
67 _("File daemon"), jcr->client->address,
68 NULL, jcr->client->FDport, verbose);
70 jcr->JobStatus = JS_ErrorTerminated;
73 Dmsg0(10, "Opened connection with File daemon\n");
74 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
76 jcr->JobStatus = JS_Running;
78 if (!authenticate_file_daemon(jcr)) {
79 jcr->JobStatus = JS_ErrorTerminated;
84 * Now send JobId and authorization key
86 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
87 jcr->VolSessionTime, jcr->sd_auth_key);
88 Dmsg1(10, ">filed: %s", fd->msg);
89 if (bnet_recv(fd) > 0) {
90 Dmsg1(10, "<filed: %s", fd->msg);
91 if (strcmp(fd->msg, OKjob) != 0) {
92 Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Job command: %s\n"), fd->msg);
93 jcr->JobStatus = JS_ErrorTerminated;
97 Jmsg(jcr, M_FATAL, 0, _("<filed: bad response to JobId command: %s\n"),
99 jcr->JobStatus = JS_ErrorTerminated;
107 * Send include list to File daemon
109 int send_include_list(JCR *jcr)
116 fd = jcr->file_bsock;
117 fileset = jcr->fileset;
121 fd->msglen = sprintf(fd->msg, inc);
123 for (i=0; i < fileset->num_includes; i++) {
124 fd->msglen = strlen(fileset->include_array[i]);
125 Dmsg1(20, "dird>filed: include file: %s\n", fileset->include_array[i]);
126 fd->msg = fileset->include_array[i];
127 if (!bnet_send(fd)) {
128 Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
129 jcr->JobStatus = JS_ErrorTerminated;
133 bnet_sig(fd, BNET_EOF);
135 if (!response(fd, OKinc, "Include")) {
136 jcr->JobStatus = JS_ErrorTerminated;
143 * Send exclude list to File daemon
145 int send_exclude_list(JCR *jcr)
152 fd = jcr->file_bsock;
153 fileset = jcr->fileset;
156 fd->msglen = sprintf(fd->msg, exc);
158 for (i=0; i < fileset->num_excludes; i++) {
159 fd->msglen = strlen(fileset->exclude_array[i]);
160 Dmsg1(20, "dird>filed: exclude file: %s\n", fileset->exclude_array[i]);
161 fd->msg = fileset->exclude_array[i];
162 if (!bnet_send(fd)) {
163 Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
164 jcr->JobStatus = JS_ErrorTerminated;
168 bnet_sig(fd, BNET_EOF);
170 if (!response(fd, OKexc, "Exclude")) {
171 jcr->JobStatus = JS_ErrorTerminated;
179 * Read the attributes from the File daemon for
180 * a Verify job and store them in the catalog.
182 int get_attributes_and_put_in_catalog(JCR *jcr)
188 fd = jcr->file_bsock;
189 jcr->jr.FirstIndex = 1;
190 memset(&ar, 0, sizeof(ar));
192 Dmsg0(20, "bdird: waiting to receive file attributes\n");
193 /* Pickup file attributes and signature */
194 while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
196 /*****FIXME****** improve error handling to stop only on
197 * really fatal problems, or the number of errors is too
202 char *attr, buf[MAXSTRING];
203 char Opts[MAXSTRING]; /* either Verify opts or MD5 signature */
205 /* ***FIXME*** check fname length */
206 if ((len = sscanf(fd->msg, "%ld %d %s %s", &file_index, &stream,
207 Opts, jcr->fname)) != 4) {
208 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 4 fields got %d\n\
209 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
210 jcr->JobStatus = JS_ErrorTerminated;
214 if (stream == STREAM_UNIX_ATTRIBUTES) {
215 len = strlen(fd->msg); /* length before attributes */
216 ar.attr = &fd->msg[len+1];
217 ar.fname = jcr->fname;
218 ar.FileIndex = 0; /* used as mark field during compare */
221 ar.JobId = jcr->JobId;
222 ar.ClientId = jcr->ClientId;
226 Dmsg2(11, "dird<filed: stream=%d %s\n", stream, jcr->fname);
227 Dmsg1(20, "dird<filed: attr=%s\n", attr);
229 /* ***FIXME*** fix link field */
230 if (!db_create_file_attributes_record(jcr->db, &ar)) {
231 Jmsg1(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
232 jcr->JobStatus = JS_ErrorTerminated;
233 /* This probably should not really be fatal *****FIXME****** */
236 jcr->FileIndex = file_index;
237 jcr->FileId = ar.FileId;
238 } else if (stream == STREAM_MD5_SIGNATURE) {
239 if (jcr->FileIndex != (uint32_t)file_index) {
240 Jmsg0(jcr, M_FATAL, 0, _("Got MD5 but not same block as attributes\n"));
241 jcr->JobStatus = JS_ErrorTerminated;
244 db_escape_string(buf, Opts, strlen(Opts));
245 Dmsg2(20, "MD5len=%d MD5=%s\n", strlen(buf), buf);
246 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, buf)) {
247 Jmsg1(jcr, M_WARNING, 0, "%s", db_strerror(jcr->db));
250 jcr->jr.JobFiles = file_index;
251 jcr->jr.LastIndex = file_index;
254 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
256 jcr->JobStatus = JS_ErrorTerminated;
260 jcr->JobStatus = JS_Terminated;