2 * This file handles commands from the File daemon.
6 * We get here because the Director has initiated a Job with
7 * the Storage daemon, then done the same with the File daemon,
8 * then when the Storage daemon receives a proper connection from
9 * the File daemon, control is passed here to handle the
10 * subsequent File daemon commands.
16 Copyright (C) 2000-2004 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 /* Imported variables */
41 /* Static variables */
42 static char ferrmsg[] = "3900 Invalid command\n";
44 /* Imported functions */
45 extern bool do_append_data(JCR *jcr);
46 extern bool do_read_data(JCR *jcr);
48 /* Forward referenced FD commands */
49 static bool append_open_session(JCR *jcr);
50 static bool append_close_session(JCR *jcr);
51 static bool append_data_cmd(JCR *jcr);
52 static bool append_end_session(JCR *jcr);
53 static bool read_open_session(JCR *jcr);
54 static bool read_data_cmd(JCR *jcr);
55 static bool read_close_session(JCR *jcr);
57 /* Exported function */
58 bool bootstrap_cmd(JCR *jcr);
62 bool (*func)(JCR *jcr);
66 * The following are the recognized commands from the File daemon
68 static struct s_cmds fd_cmds[] = {
69 {"append open", append_open_session},
70 {"append data", append_data_cmd},
71 {"append end", append_end_session},
72 {"append close", append_close_session},
73 {"read open", read_open_session},
74 {"read data", read_data_cmd},
75 {"read close", read_close_session},
76 {"bootstrap", bootstrap_cmd},
77 {NULL, NULL} /* list terminator */
80 /* Commands from the File daemon that require additional scanning */
81 static char read_open[] = "read open session = %127s %ld %ld %ld %ld %ld %ld\n";
83 /* Responses sent to the File daemon */
84 static char NO_open[] = "3901 Error session already open\n";
85 static char NOT_opened[] = "3902 Error session not opened\n";
86 static char OK_end[] = "3000 OK end\n";
87 static char OK_close[] = "3000 OK close Status = %d\n";
88 static char OK_open[] = "3000 OK open ticket = %d\n";
89 static char OK_append[] = "3000 OK append data\n";
90 static char ERROR_append[] = "3903 Error append data\n";
91 static char OK_bootstrap[] = "3000 OK bootstrap\n";
92 static char ERROR_bootstrap[] = "3904 Error bootstrap\n";
94 /* Information sent to the Director */
95 static char Job_start[] = "3010 Job %s start\n";
96 static char Job_end[] =
97 "3099 Job %s end JobStatus=%d JobFiles=%d JobBytes=%s\n";
100 * Run a File daemon Job -- File daemon already authorized
102 * Basic task here is:
103 * - Read a command from the File daemon
107 void run_job(JCR *jcr)
111 BSOCK *fd = jcr->file_bsock;
112 BSOCK *dir = jcr->dir_bsock;
118 Dmsg1(120, "Start run Job=%s\n", jcr->Job);
119 bnet_fsend(dir, Job_start, jcr->Job);
120 jcr->start_time = time(NULL);
121 jcr->run_time = jcr->start_time;
122 set_jcr_job_status(jcr, JS_Running);
123 dir_send_job_status(jcr); /* update director */
124 for (quit=false; !quit;) {
127 /* Read command coming from the File daemon */
128 stat = bnet_recv(fd);
129 if (is_bnet_stop(fd)) { /* hardeof or error */
130 break; /* connection terminated */
133 continue; /* ignore signals and zero length msgs */
135 Dmsg1(110, "<filed: %s", fd->msg);
137 for (i=0; fd_cmds[i].cmd; i++) {
138 if (strncmp(fd_cmds[i].cmd, fd->msg, strlen(fd_cmds[i].cmd)) == 0) {
139 found = true; /* indicate command found */
140 if (!fd_cmds[i].func(jcr)) { /* do command */
141 set_jcr_job_status(jcr, JS_ErrorTerminated);
147 if (!found) { /* command not found */
148 Dmsg1(110, "<filed: Command not found: %s\n", fd->msg);
149 bnet_fsend(fd, ferrmsg);
153 bnet_sig(fd, BNET_TERMINATE); /* signal to FD job is done */
154 jcr->end_time = time(NULL);
155 dequeue_messages(jcr); /* send any queued messages */
156 set_jcr_job_status(jcr, JS_Terminated);
157 bnet_fsend(dir, Job_end, jcr->Job, jcr->JobStatus, jcr->JobFiles,
158 edit_uint64(jcr->JobBytes, ec1));
159 bnet_sig(dir, BNET_EOD); /* send EOD to Director daemon */
165 * Append Data command
166 * Open Data Channel and receive Data for archiving
167 * Write the Data to the archive device
169 static bool append_data_cmd(JCR *jcr)
171 BSOCK *fd = jcr->file_bsock;
173 Dmsg1(120, "Append data: %s", fd->msg);
174 if (jcr->session_opened) {
175 Dmsg1(110, "<bfiled: %s", fd->msg);
176 jcr->JobType = JT_BACKUP;
177 if (do_append_data(jcr)) {
178 return bnet_fsend(fd, OK_append);
180 bnet_suppress_error_messages(fd, 1); /* ignore errors at this point */
181 bnet_fsend(fd, ERROR_append);
184 bnet_fsend(fd, NOT_opened);
189 static bool append_end_session(JCR *jcr)
191 BSOCK *fd = jcr->file_bsock;
193 Dmsg1(120, "store<file: %s", fd->msg);
194 if (!jcr->session_opened) {
195 bnet_fsend(fd, NOT_opened);
198 set_jcr_job_status(jcr, JS_Terminated);
199 return bnet_fsend(fd, OK_end);
204 * Append Open session command
207 static bool append_open_session(JCR *jcr)
209 BSOCK *fd = jcr->file_bsock;
211 Dmsg1(120, "Append open session: %s", fd->msg);
212 if (jcr->session_opened) {
213 bnet_fsend(fd, NO_open);
217 Dmsg1(110, "Append open session: %s\n", dev_name(jcr->device->dev));
218 jcr->session_opened = TRUE;
220 /* Send "Ticket" to File Daemon */
221 bnet_fsend(fd, OK_open, jcr->VolSessionId);
222 Dmsg1(110, ">filed: %s", fd->msg);
228 * Append Close session command
229 * Close the append session and send back Statistics
230 * (need to fix statistics)
232 static bool append_close_session(JCR *jcr)
234 BSOCK *fd = jcr->file_bsock;
236 Dmsg1(120, "<filed: %s\n", fd->msg);
237 if (!jcr->session_opened) {
238 bnet_fsend(fd, NOT_opened);
241 /* Send final statistics to File daemon */
242 bnet_fsend(fd, OK_close, jcr->JobStatus);
243 Dmsg1(120, ">filed: %s\n", fd->msg);
245 bnet_sig(fd, BNET_EOD); /* send EOD to File daemon */
247 Dmsg1(110, "Append close session: %s\n", dev_name(jcr->device->dev));
249 jcr->session_opened = false;
255 * Open Data Channel, read the data from
256 * the archive device and send to File
259 static bool read_data_cmd(JCR *jcr)
261 BSOCK *fd = jcr->file_bsock;
263 Dmsg1(120, "Read data: %s\n", fd->msg);
264 if (jcr->session_opened) {
265 Dmsg1(120, "<bfiled: %s", fd->msg);
266 return do_read_data(jcr);
268 bnet_fsend(fd, NOT_opened);
274 * Read Open session command
276 * We need to scan for the parameters of the job
279 static bool read_open_session(JCR *jcr)
281 BSOCK *fd = jcr->file_bsock;
283 Dmsg1(120, "%s\n", fd->msg);
284 if (jcr->session_opened) {
285 bnet_fsend(fd, NO_open);
289 if (sscanf(fd->msg, read_open, jcr->dcr->VolumeName, &jcr->read_VolSessionId,
290 &jcr->read_VolSessionTime, &jcr->read_StartFile, &jcr->read_EndFile,
291 &jcr->read_StartBlock, &jcr->read_EndBlock) == 7) {
292 if (jcr->session_opened) {
293 bnet_fsend(fd, NOT_opened);
296 Dmsg4(100, "read_open_session got: JobId=%d Vol=%s VolSessId=%ld VolSessT=%ld\n",
297 jcr->JobId, jcr->dcr->VolumeName, jcr->read_VolSessionId,
298 jcr->read_VolSessionTime);
299 Dmsg4(100, " StartF=%ld EndF=%ld StartB=%ld EndB=%ld\n",
300 jcr->read_StartFile, jcr->read_EndFile, jcr->read_StartBlock,
304 Dmsg1(110, "Read open session: %s\n", dev_name(jcr->device->dev));
306 jcr->session_opened = TRUE;
307 jcr->JobType = JT_RESTORE;
309 /* Send "Ticket" to File Daemon */
310 bnet_fsend(fd, OK_open, jcr->VolSessionId);
311 Dmsg1(110, ">filed: %s", fd->msg);
316 bool bootstrap_cmd(JCR *jcr)
318 BSOCK *fd = jcr->file_bsock;
319 POOLMEM *fname = get_pool_memory(PM_FNAME);
323 if (jcr->RestoreBootstrap) {
324 unlink(jcr->RestoreBootstrap);
325 free_pool_memory(jcr->RestoreBootstrap);
327 Mmsg(fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
329 Dmsg1(400, "bootstrap=%s\n", fname);
330 jcr->RestoreBootstrap = fname;
331 bs = fopen(fname, "a+"); /* create file */
333 Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
334 jcr->RestoreBootstrap, strerror(errno));
337 while (bnet_recv(fd) >= 0) {
338 Dmsg1(400, "stored<filed: bootstrap file %s\n", fd->msg);
342 jcr->bsr = parse_bsr(jcr, jcr->RestoreBootstrap);
344 Jmsg(jcr, M_FATAL, 0, _("Error parsing bootstrap file.\n"));
347 if (debug_level > 20) {
348 dump_bsr(jcr->bsr, true);
353 unlink(jcr->RestoreBootstrap);
354 free_pool_memory(jcr->RestoreBootstrap);
355 jcr->RestoreBootstrap = NULL;
357 bnet_fsend(fd, ERROR_bootstrap);
360 return bnet_fsend(fd, OK_bootstrap);
365 * Read Close session command
366 * Close the read session
368 static bool read_close_session(JCR *jcr)
370 BSOCK *fd = jcr->file_bsock;
372 Dmsg1(120, "Read close session: %s\n", fd->msg);
373 if (!jcr->session_opened) {
374 bnet_fsend(fd, NOT_opened);
377 /* Send final statistics to File daemon */
378 bnet_fsend(fd, OK_close);
379 Dmsg1(160, ">filed: %s\n", fd->msg);
381 bnet_sig(fd, BNET_EOD); /* send EOD to File daemon */
383 jcr->session_opened = FALSE;