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.
15 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
17 This program is free software; you can redistribute it and/or
18 modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation; either version 2 of
20 the License, or (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public
28 License along with this program; if not, write to the Free
29 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 /* Commands sent to File daemon */
38 static char inc[] = "include\n";
39 static char exc[] = "exclude\n";
40 static char jobcmd[] = "JobId=%d Job=%s SDid=%u SDtime=%u Authorization=%s\n";
43 /* Responses received from File daemon */
44 static char OKinc[] = "2000 OK include\n";
45 static char OKexc[] = "2000 OK exclude\n";
46 static char OKjob[] = "2000 OK Job\n";
48 /* Forward referenced functions */
50 /* External functions */
51 extern int debug_level;
52 extern DIRRES *director;
53 extern int FDConnectTimeout;
56 * Open connection with File daemon.
57 * Try connecting every 10 seconds, give up after 1 hour.
60 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
65 fd = bnet_connect(jcr, retry_interval, max_retry_time,
66 _("File daemon"), jcr->client->address,
67 NULL, jcr->client->FDport, verbose);
69 jcr->JobStatus = JS_ErrorTerminated;
72 Dmsg0(10, "Opened connection with File daemon\n");
73 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
75 jcr->JobStatus = JS_Running;
77 if (!authenticate_file_daemon(jcr)) {
78 jcr->JobStatus = JS_ErrorTerminated;
83 * Now send JobId and authorization key
85 bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
86 jcr->VolSessionTime, jcr->sd_auth_key);
87 Dmsg1(10, ">filed: %s", fd->msg);
88 if (bnet_recv(fd) > 0) {
89 Dmsg1(10, "<filed: %s", fd->msg);
90 if (strcmp(fd->msg, OKjob) != 0) {
91 Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Job command: %s\n"), fd->msg);
92 jcr->JobStatus = JS_ErrorTerminated;
96 Jmsg(jcr, M_FATAL, 0, _("<filed: bad response to JobId command: %s\n"),
98 jcr->JobStatus = JS_ErrorTerminated;
106 * Send include list to File daemon
108 int send_include_list(JCR *jcr)
115 fd = jcr->file_bsock;
116 fileset = jcr->fileset;
120 fd->msglen = sprintf(fd->msg, inc);
122 for (i=0; i < fileset->num_includes; i++) {
123 fd->msglen = strlen(fileset->include_array[i]);
124 Dmsg1(20, "dird>filed: include file: %s\n", fileset->include_array[i]);
125 fd->msg = fileset->include_array[i];
126 if (!bnet_send(fd)) {
127 Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
128 jcr->JobStatus = JS_ErrorTerminated;
132 bnet_sig(fd, BNET_EOF);
134 if (!response(fd, OKinc, "Include")) {
135 jcr->JobStatus = JS_ErrorTerminated;
142 * Send exclude list to File daemon
144 int send_exclude_list(JCR *jcr)
151 fd = jcr->file_bsock;
152 fileset = jcr->fileset;
155 fd->msglen = sprintf(fd->msg, exc);
157 for (i=0; i < fileset->num_excludes; i++) {
158 fd->msglen = strlen(fileset->exclude_array[i]);
159 Dmsg1(20, "dird>filed: exclude file: %s\n", fileset->exclude_array[i]);
160 fd->msg = fileset->exclude_array[i];
161 if (!bnet_send(fd)) {
162 Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
163 jcr->JobStatus = JS_ErrorTerminated;
167 bnet_sig(fd, BNET_EOF);
169 if (!response(fd, OKexc, "Exclude")) {
170 jcr->JobStatus = JS_ErrorTerminated;
178 * Read the attributes from the File daemon for
179 * a Verify job and store them in the catalog.
181 int get_attributes_and_put_in_catalog(JCR *jcr)
187 fd = jcr->file_bsock;
188 jcr->jr.FirstIndex = 1;
189 memset(&ar, 0, sizeof(ar));
191 Dmsg0(20, "bdird: waiting to receive file attributes\n");
192 /* Pickup file attributes and signature */
193 while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
195 /*****FIXME****** improve error handling to stop only on
196 * really fatal problems, or the number of errors is too
201 char *attr, buf[MAXSTRING];
202 char Opts[MAXSTRING]; /* either Verify opts or MD5 signature */
204 /* ***FIXME*** check fname length */
205 if ((len = sscanf(fd->msg, "%ld %d %s %s", &file_index, &stream,
206 Opts, jcr->fname)) != 4) {
207 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 4 fields got %d\n\
208 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
209 jcr->JobStatus = JS_ErrorTerminated;
213 if (stream == STREAM_UNIX_ATTRIBUTES) {
214 len = strlen(fd->msg); /* length before attributes */
215 ar.attr = &fd->msg[len+1];
216 ar.fname = jcr->fname;
217 ar.FileIndex = 0; /* used as mark field during compare */
220 ar.JobId = jcr->JobId;
221 ar.ClientId = jcr->ClientId;
225 Dmsg2(11, "dird<filed: stream=%d %s\n", stream, jcr->fname);
226 Dmsg1(20, "dird<filed: attr=%s\n", attr);
228 /* ***FIXME*** fix link field */
229 if (!db_create_file_attributes_record(jcr->db, &ar)) {
230 Jmsg1(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
231 jcr->JobStatus = JS_ErrorTerminated;
232 /* This probably should not really be fatal *****FIXME****** */
235 jcr->FileIndex = file_index;
236 jcr->FileId = ar.FileId;
237 } else if (stream == STREAM_MD5_SIGNATURE) {
238 if (jcr->FileIndex != (uint32_t)file_index) {
239 Jmsg0(jcr, M_FATAL, 0, _("Got MD5 but not same block as attributes\n"));
240 jcr->JobStatus = JS_ErrorTerminated;
243 db_escape_string(buf, Opts, strlen(Opts));
244 Dmsg2(20, "MD5len=%d MD5=%s\n", strlen(buf), buf);
245 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, buf)) {
246 Jmsg1(jcr, M_WARNING, 0, "%s", db_strerror(jcr->db));
249 jcr->jr.JobFiles = file_index;
250 jcr->jr.LastIndex = file_index;
253 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
255 jcr->JobStatus = JS_ErrorTerminated;
259 jcr->JobStatus = JS_Terminated;