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";
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";
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(110, ">filed: %s", fd->msg);
89 if (bnet_recv(fd) > 0) {
90 Dmsg1(110, "<filed: %s", fd->msg);
91 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
92 Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Job command: %s\n"), fd->msg);
93 jcr->JobStatus = JS_ErrorTerminated;
96 /***** ***FIXME***** update Client Uname */
99 Jmsg(jcr, M_FATAL, 0, _("<filed: bad response to JobId command: %s\n"),
101 jcr->JobStatus = JS_ErrorTerminated;
109 * Send include list to File daemon
111 int send_include_list(JCR *jcr)
118 fd = jcr->file_bsock;
119 fileset = jcr->fileset;
123 fd->msglen = sprintf(fd->msg, inc);
125 for (i=0; i < fileset->num_includes; i++) {
126 fd->msglen = strlen(fileset->include_array[i]);
127 Dmsg1(120, "dird>filed: include file: %s\n", fileset->include_array[i]);
128 fd->msg = fileset->include_array[i];
129 if (!bnet_send(fd)) {
131 Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
132 jcr->JobStatus = JS_ErrorTerminated;
136 bnet_sig(fd, BNET_EOD); /* end of data */
138 if (!response(fd, OKinc, "Include")) {
139 jcr->JobStatus = JS_ErrorTerminated;
146 * Send exclude list to File daemon
148 int send_exclude_list(JCR *jcr)
155 fd = jcr->file_bsock;
156 fileset = jcr->fileset;
159 fd->msglen = sprintf(fd->msg, exc);
161 for (i=0; i < fileset->num_excludes; i++) {
162 fd->msglen = strlen(fileset->exclude_array[i]);
163 Dmsg1(120, "dird>filed: exclude file: %s\n", fileset->exclude_array[i]);
164 fd->msg = fileset->exclude_array[i];
165 if (!bnet_send(fd)) {
166 Emsg0(M_FATAL, 0, _(">filed: write error on socket\n"));
167 jcr->JobStatus = JS_ErrorTerminated;
171 bnet_sig(fd, BNET_EOD);
173 if (!response(fd, OKexc, "Exclude")) {
174 jcr->JobStatus = JS_ErrorTerminated;
182 * Read the attributes from the File daemon for
183 * a Verify job and store them in the catalog.
185 int get_attributes_and_put_in_catalog(JCR *jcr)
191 fd = jcr->file_bsock;
192 jcr->jr.FirstIndex = 1;
193 memset(&ar, 0, sizeof(ar));
196 Dmsg0(120, "bdird: waiting to receive file attributes\n");
197 /* Pickup file attributes and signature */
198 while (!fd->errors && (n = bget_msg(fd, 0)) > 0) {
200 /*****FIXME****** improve error handling to stop only on
201 * really fatal problems, or the number of errors is too
207 char Opts_MD5[MAXSTRING]; /* either Verify opts or MD5 signature */
210 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
211 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_MD5)) != 3) {
212 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n\
213 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
214 jcr->JobStatus = JS_ErrorTerminated;
218 skip_nonspaces(&p); /* skip FileIndex */
220 skip_nonspaces(&p); /* skip Stream */
222 skip_nonspaces(&p); /* skip Opts_MD5 */
223 p++; /* skip space */
226 *fn++ = *p++; /* copy filename */
228 *fn = *p++; /* term filename and point to attribs */
231 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
233 jcr->FileIndex = file_index;
235 ar.fname = jcr->fname;
236 ar.FileIndex = file_index;
239 ar.JobId = jcr->JobId;
240 ar.ClientId = jcr->ClientId;
244 Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
245 Dmsg1(120, "dird<filed: attr=%s\n", attr);
247 if (!db_create_file_attributes_record(jcr->db, &ar)) {
248 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
249 jcr->JobStatus = JS_Error;
252 jcr->FileId = ar.FileId;
253 } else if (stream == STREAM_MD5_SIGNATURE) {
254 if (jcr->FileIndex != (uint32_t)file_index) {
255 Jmsg2(jcr, M_ERROR, 0, _("MD5 index %d not same as attributes %d\n"),
256 file_index, jcr->FileIndex);
257 jcr->JobStatus = JS_Error;
260 db_escape_string(MD5, Opts_MD5, strlen(Opts_MD5));
261 Dmsg2(120, "MD5len=%d MD5=%s\n", strlen(MD5), MD5);
262 if (!db_add_MD5_to_file_record(jcr->db, jcr->FileId, MD5)) {
263 Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
264 jcr->JobStatus = JS_Error;
267 jcr->jr.JobFiles = jcr->JobFiles = file_index;
268 jcr->jr.LastIndex = file_index;
270 if (is_bnet_error(fd)) {
271 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
273 jcr->JobStatus = JS_ErrorTerminated;
277 jcr->JobStatus = JS_Terminated;