2 * Job control and execution for Storage Daemon
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /* Imported variables */
31 extern uint32_t VolSessionTime;
33 /* Imported functions */
34 extern uint32_t newVolSessionId();
36 /* Forward referenced functions */
37 static int use_device_cmd(JCR *jcr);
39 /* Requests from the Director daemon */
40 static char jobcmd[] = "JobId=%d job=%127s job_name=%127s client_name=%127s \
41 type=%d level=%d FileSet=%127s NoAttr=%d SpoolAttr=%d FileSetMD5=%127s\n";
42 static char use_device[] = "use device=%s media_type=%s pool_name=%s pool_type=%s\n";
44 /* Responses sent to Director daemon */
45 static char OKjob[] = "3000 OK Job SDid=%u SDtime=%u Authorization=%s\n";
46 static char OK_device[] = "3000 OK use device\n";
47 static char NO_device[] = "3914 Device %s not available\n";
48 static char BAD_use[] = "3913 Bad use command: %s\n";
49 static char BAD_job[] = "3915 Bad Job command: %s\n";
54 * Director requests us to start a job
55 * Basic tasks done here:
56 * - We pickup the JobId to be run from the Director.
57 * - We pickup the device, media, and pool from the Director
58 * - Wait for a connection from the File Daemon (FD)
59 * - Accept commands from the FD (i.e. run the job)
60 * - Return when the connection is terminated or
67 BSOCK *dir = jcr->dir_bsock;
68 POOLMEM *job_name, *client_name, *job, *fileset_name, *fileset_md5;
69 int JobType, level, spool_attributes, no_attributes;
72 struct timespec timeout;
75 * Get JobId and permissions from Director
78 Dmsg1(130, "Job_cmd: %s\n", dir->msg);
79 job = get_memory(dir->msglen);
80 job_name = get_memory(dir->msglen);
81 client_name = get_memory(dir->msglen);
82 fileset_name = get_memory(dir->msglen);
83 fileset_md5 = get_memory(dir->msglen);
84 if (sscanf(dir->msg, jobcmd, &JobId, job, job_name, client_name,
85 &JobType, &level, fileset_name, &no_attributes,
86 &spool_attributes, fileset_md5) != 10) {
87 bnet_fsend(dir, BAD_job, dir->msg);
88 Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), dir->msg);
90 free_memory(job_name);
91 free_memory(client_name);
92 free_memory(fileset_name);
93 free_memory(fileset_md5);
94 jcr->JobStatus = JS_ErrorTerminated;
98 jcr->VolSessionId = newVolSessionId();
99 jcr->VolSessionTime = VolSessionTime;
100 strcpy(jcr->Job, job);
101 unbash_spaces(job_name);
102 jcr->job_name = get_memory(strlen(job_name) + 1);
103 strcpy(jcr->job_name, job_name);
104 unbash_spaces(client_name);
105 jcr->client_name = get_memory(strlen(client_name) + 1);
106 strcpy(jcr->client_name, client_name);
107 unbash_spaces(fileset_name);
108 jcr->fileset_name = get_memory(strlen(fileset_name) + 1);
109 strcpy(jcr->fileset_name, fileset_name);
110 jcr->JobType = JobType;
111 jcr->JobLevel = level;
112 jcr->no_attributes = no_attributes;
113 jcr->spool_attributes = spool_attributes;
114 jcr->fileset_md5 = get_memory(strlen(fileset_md5) + 1);
115 strcpy(jcr->fileset_md5, fileset_md5);
117 free_memory(job_name);
118 free_memory(client_name);
119 free_memory(fileset_name);
120 free_memory(fileset_md5);
122 /* Initialize FD start condition variable */
123 if ((errstat = pthread_cond_init(&jcr->job_start_wait, NULL)) != 0) {
124 Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
125 jcr->JobStatus = JS_ErrorTerminated;
128 jcr->authenticated = FALSE;
131 * Pass back an authorization key for the File daemon
133 #ifdef Old_way_not_so_good
134 gettimeofday(&tv, &tz);
135 srandom(tv.tv_usec + tv.tv_sec);
136 sprintf(auth_key, "%ld", (long)random());
138 makeSessionKey(auth_key, NULL, 1);
139 bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
140 Dmsg1(110, ">dird: %s", dir->msg);
141 jcr->sd_auth_key = bstrdup(auth_key);
144 * Wait for the device, media, and pool information
146 if (!use_device_cmd(jcr)) {
147 jcr->JobStatus = JS_ErrorTerminated;
151 jcr->JobStatus = JS_WaitFD; /* wait for FD to connect */
152 dir_send_job_status(jcr);
154 gettimeofday(&tv, &tz);
155 timeout.tv_nsec = tv.tv_usec * 1000;
156 timeout.tv_sec = tv.tv_sec + 30 * 60; /* wait 30 minutes */
159 Dmsg1(200, "%s waiting on job_start_wait\n", jcr->Job);
160 /* Wait for the File daemon to contact us to start the Job,
161 * when he does, we will be released, unless the 30 minutes
165 for ( ;!job_cancelled(jcr); ) {
166 errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
167 if (errstat == 0 || errstat == ETIMEDOUT) {
173 if (jcr->authenticated && !job_cancelled(jcr)) {
174 run_job(jcr); /* Run the job */
180 * This entry point is only called if we have a separate
181 * Storage Daemon Data port. Otherwise, the connection
182 * is made to the main port, and if it is a File daemon
183 * calling, handl_filed_connection() is called directly.
185 void connection_from_filed(void *arg)
187 BSOCK *fd = (BSOCK *)arg;
188 char job_name[MAX_NAME_LENGTH];
190 Dmsg0(110, "enter connection_from_filed\n");
191 if (bnet_recv(fd) <= 0) {
192 Emsg0(M_FATAL, 0, _("Unable to authenticate Client.\n"));
195 Dmsg1(100, "got: %s\n", fd->msg);
197 if (sscanf(fd->msg, "Hello Start Job %127s\n", job_name) != 1) {
198 Emsg1(M_FATAL, 0, _("Authentication failure: %s\n"), fd->msg);
201 handle_filed_connection(fd, job_name);
205 void handle_filed_connection(BSOCK *fd, char *job_name)
209 if (!(jcr=get_jcr_by_full_name(job_name))) {
210 Jmsg1(NULL, M_FATAL, 0, _("Job name not found: %s\n"), job_name);
214 jcr->file_bsock = fd;
215 jcr->file_bsock->jcr = (void *)jcr;
217 Dmsg1(110, "Found Job %s\n", job_name);
219 if (jcr->authenticated) {
220 Pmsg2(000, "Hey!!!! JobId %u Job %s already authenticated.\n",
221 jcr->JobId, jcr->Job);
225 * Authenticate the File daemon
227 if (jcr->authenticated || !authenticate_filed(jcr)) {
228 Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
229 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
231 jcr->authenticated = TRUE;
232 Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
236 if (!jcr->authenticated) {
237 jcr->JobStatus = JS_ErrorTerminated;
239 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
247 * Use Device command from Director
248 * He tells is what Device Name to use, the Media Type,
249 * the Pool Name, and the Pool Type.
251 * Ensure that the device exists and is opened, then store
252 * the media and pool info in the JCR.
254 static int use_device_cmd(JCR *jcr)
256 POOLMEM *dev_name, *media_type, *pool_name, *pool_type;
257 BSOCK *dir = jcr->dir_bsock;
260 if (bnet_recv(dir) <= 0) {
261 Jmsg0(jcr, M_FATAL, 0, _("No Device from Director\n"));
265 Dmsg1(120, "Use device: %s", dir->msg);
266 dev_name = get_memory(dir->msglen);
267 media_type = get_memory(dir->msglen);
268 pool_name = get_memory(dir->msglen);
269 pool_type = get_memory(dir->msglen);
270 if (sscanf(dir->msg, use_device, dev_name, media_type, pool_name, pool_type) == 4) {
271 unbash_spaces(dev_name);
272 unbash_spaces(media_type);
273 unbash_spaces(pool_name);
274 unbash_spaces(pool_type);
277 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
278 /* Find resource, and make sure we were able to open it */
279 if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
280 Dmsg1(120, "Found device %s\n", device->hdr.name);
281 jcr->pool_name = get_memory(strlen(pool_name) + 1);
282 strcpy(jcr->pool_name, pool_name);
283 jcr->pool_type = get_memory(strlen(pool_type) + 1);
284 strcpy(jcr->pool_type, pool_type);
285 jcr->media_type = get_memory(strlen(media_type) + 1);
286 strcpy(jcr->media_type, media_type);
287 jcr->dev_name = get_memory(strlen(dev_name) + 1);
288 strcpy(jcr->dev_name, dev_name);
289 jcr->device = device;
290 Dmsg4(120, use_device, dev_name, media_type, pool_name, pool_type);
291 free_memory(dev_name);
292 free_memory(media_type);
293 free_memory(pool_name);
294 free_memory(pool_type);
296 return bnet_fsend(dir, OK_device);
300 Jmsg(jcr, M_FATAL, 0, _("Requested device %s not found. Cannot continue.\n"),
302 bnet_fsend(dir, NO_device, dev_name);
304 Jmsg(jcr, M_FATAL, 0, _("store<dir: Bad Use Device command: %s\n"), dir->msg);
305 bnet_fsend(dir, BAD_use, dir->msg);
308 free_memory(dev_name);
309 free_memory(media_type);
310 free_memory(pool_name);
311 free_memory(pool_type);
312 return 0; /* ERROR return */
316 * Destroy the Job Control Record and associated
317 * resources (sockets).
319 void stored_free_jcr(JCR *jcr)
321 if (jcr->file_bsock) {
322 bnet_close(jcr->file_bsock);
323 jcr->file_bsock = NULL;
325 if (jcr->pool_name) {
326 free_memory(jcr->pool_name);
328 if (jcr->pool_type) {
329 free_memory(jcr->pool_type);
331 if (jcr->media_type) {
332 free_memory(jcr->media_type);
335 free_memory(jcr->dev_name);
338 free_pool_memory(jcr->job_name);
340 if (jcr->client_name) {
341 free_memory(jcr->client_name);
342 jcr->client_name = NULL;
344 if (jcr->fileset_name) {
345 free_memory(jcr->fileset_name);
347 if (jcr->fileset_md5) {
348 free_memory(jcr->fileset_md5);
354 if (jcr->RestoreBootstrap) {
355 unlink(jcr->RestoreBootstrap);
356 free_pool_memory(jcr->RestoreBootstrap);
357 jcr->RestoreBootstrap = NULL;
359 if (jcr->next_dev && jcr->prev_dev) {
360 Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));