2 * Job control and execution for Storage Daemon
10 Copyright (C) 2000-2005 Kern Sibbald
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* Imported variables */
33 extern uint32_t VolSessionTime;
35 /* Imported functions */
36 extern uint32_t newVolSessionId();
38 /* Forward referenced functions */
39 static bool use_device_cmd(JCR *jcr);
41 /* Requests from the Director daemon */
42 static char jobcmd[] = "JobId=%d job=%127s job_name=%127s client_name=%127s "
43 "type=%d level=%d FileSet=%127s NoAttr=%d SpoolAttr=%d FileSetMD5=%127s "
44 "SpoolData=%d WritePartAfterJob=%d";
45 static char use_device[] = "use device=%127s media_type=%127s "
46 "pool_name=%127s pool_type=%127s append=%d";
47 static char query_device[] = "query device=%127s";
50 /* Responses sent to Director daemon */
51 static char OKjob[] = "3000 OK Job SDid=%u SDtime=%u Authorization=%s\n";
52 static char OK_device[] = "3000 OK use device\n";
53 static char NO_device[] = "3914 Device \"%s\" not in SD Device resources.\n";
54 static char BAD_use[] = "3913 Bad use command: %s\n";
55 static char BAD_job[] = "3915 Bad Job command: %s\n";
56 static char OK_query[] = "3001 OK query append=%d read=%d num_writers=%d "
57 "num_waiting=%d open=%d use_count=%d labeled=%d "
58 "media_type=%s volume_name=%s";
59 static char BAD_query[] = "3917 Bad query command: %s\n";
62 * Director requests us to start a job
63 * Basic tasks done here:
64 * - We pickup the JobId to be run from the Director.
65 * - We pickup the device, media, and pool from the Director
66 * - Wait for a connection from the File Daemon (FD)
67 * - Accept commands from the FD (i.e. run the job)
68 * - Return when the connection is terminated or
71 bool job_cmd(JCR *jcr)
75 BSOCK *dir = jcr->dir_bsock;
76 POOL_MEM job_name, client_name, job, fileset_name, fileset_md5;
77 int JobType, level, spool_attributes, no_attributes, spool_data, write_part_after_job;
81 * Get JobId and permissions from Director
83 Dmsg1(100, "Job_cmd: %s\n", dir->msg);
84 if (sscanf(dir->msg, jobcmd, &JobId, job.c_str(), job_name.c_str(),
86 &JobType, &level, fileset_name.c_str(), &no_attributes,
87 &spool_attributes, fileset_md5.c_str(), &spool_data, &write_part_after_job) != 12) {
88 pm_strcpy(jcr->errmsg, dir->msg);
89 bnet_fsend(dir, BAD_job, jcr->errmsg);
90 Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), jcr->errmsg);
91 set_jcr_job_status(jcr, JS_ErrorTerminated);
95 * Since this job could be rescheduled, we
96 * check to see if we have it already. If so
97 * free the old jcr and use the new one.
99 ojcr = get_jcr_by_full_name(job.c_str());
100 if (ojcr && !ojcr->authenticated) {
101 Dmsg2(100, "Found ojcr=0x%x Job %s\n", (unsigned)(long)ojcr, job.c_str());
105 jcr->VolSessionId = newVolSessionId();
106 jcr->VolSessionTime = VolSessionTime;
107 bstrncpy(jcr->Job, job, sizeof(jcr->Job));
108 unbash_spaces(job_name);
109 jcr->job_name = get_pool_memory(PM_NAME);
110 pm_strcpy(jcr->job_name, job_name);
111 unbash_spaces(client_name);
112 jcr->client_name = get_pool_memory(PM_NAME);
113 pm_strcpy(jcr->client_name, client_name);
114 unbash_spaces(fileset_name);
115 jcr->fileset_name = get_pool_memory(PM_NAME);
116 pm_strcpy(jcr->fileset_name, fileset_name);
117 jcr->JobType = JobType;
118 jcr->JobLevel = level;
119 jcr->no_attributes = no_attributes;
120 jcr->spool_attributes = spool_attributes;
121 jcr->spool_data = spool_data;
122 jcr->write_part_after_job = write_part_after_job;
123 jcr->fileset_md5 = get_pool_memory(PM_NAME);
124 pm_strcpy(jcr->fileset_md5, fileset_md5);
126 jcr->authenticated = false;
129 * Pass back an authorization key for the File daemon
131 make_session_key(auth_key, NULL, 1);
132 bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
133 Dmsg1(110, ">dird: %s", dir->msg);
134 jcr->sd_auth_key = bstrdup(auth_key);
135 memset(auth_key, 0, sizeof(auth_key));
139 bool use_cmd(JCR *jcr)
142 * Wait for the device, media, and pool information
144 if (!use_device_cmd(jcr)) {
145 set_jcr_job_status(jcr, JS_ErrorTerminated);
146 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
152 bool run_cmd(JCR *jcr)
156 struct timespec timeout;
159 /* The following jobs don't need the FD */
160 switch (jcr->JobType) {
164 jcr->authenticated = true;
169 set_jcr_job_status(jcr, JS_WaitFD); /* wait for FD to connect */
170 dir_send_job_status(jcr);
172 gettimeofday(&tv, &tz);
173 timeout.tv_nsec = tv.tv_usec * 1000;
174 timeout.tv_sec = tv.tv_sec + 30 * 60; /* wait 30 minutes */
176 Dmsg1(100, "%s waiting on FD to contact SD\n", jcr->Job);
178 * Wait for the File daemon to contact us to start the Job,
179 * when he does, we will be released, unless the 30 minutes
183 for ( ;!job_canceled(jcr); ) {
184 errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
185 if (errstat == 0 || errstat == ETIMEDOUT) {
191 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
193 if (jcr->authenticated && !job_canceled(jcr)) {
194 Dmsg1(100, "Running job %s\n", jcr->Job);
195 run_job(jcr); /* Run the job */
201 * After receiving a connection (in job.c) if it is
202 * from the File daemon, this routine is called.
204 void handle_filed_connection(BSOCK *fd, char *job_name)
208 bmicrosleep(0, 50000); /* wait 50 millisecs */
209 if (!(jcr=get_jcr_by_full_name(job_name))) {
210 Jmsg1(NULL, M_FATAL, 0, _("Job name not found: %s\n"), job_name);
211 Dmsg1(100, "Job name not found: %s\n", job_name);
215 jcr->file_bsock = fd;
216 jcr->file_bsock->jcr = jcr;
218 Dmsg1(110, "Found Job %s\n", job_name);
220 if (jcr->authenticated) {
221 Jmsg2(jcr, M_FATAL, 0, "Hey!!!! JobId %u Job %s already authenticated.\n",
222 jcr->JobId, jcr->Job);
228 * Authenticate the File daemon
230 if (jcr->authenticated || !authenticate_filed(jcr)) {
231 Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
232 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
234 jcr->authenticated = true;
235 Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
239 if (!jcr->authenticated) {
240 set_jcr_job_status(jcr, JS_ErrorTerminated);
242 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
250 * Use Device command from Director
251 * He tells is what Device Name to use, the Media Type,
252 * the Pool Name, and the Pool Type.
254 * Ensure that the device exists and is opened, then store
255 * the media and pool info in the JCR.
257 static bool use_device_cmd(JCR *jcr)
259 POOL_MEM dev_name, media_type, pool_name, pool_type;
260 BSOCK *dir = jcr->dir_bsock;
265 Dmsg1(120, "Use device: %s", dir->msg);
267 * If there are multiple devices, the director sends us
268 * use_device for each device that it wants to use.
270 ok = sscanf(dir->msg, use_device, dev_name.c_str(), media_type.c_str(),
271 pool_name.c_str(), pool_type.c_str(), &append) == 5;
273 unbash_spaces(dev_name);
274 unbash_spaces(media_type);
275 unbash_spaces(pool_name);
276 unbash_spaces(pool_type);
278 foreach_res(device, R_DEVICE) {
279 /* Find resource, and make sure we were able to open it */
280 if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0 &&
281 device->dev && strcmp(device->media_type, media_type.c_str()) == 0) {
282 const int name_len = MAX_NAME_LENGTH;
283 DCR *dcr = new_dcr(jcr, device->dev);
285 Dmsg1(120, "Found device %s\n", device->hdr.name);
286 bstrncpy(dcr->pool_name, pool_name, name_len);
287 bstrncpy(dcr->pool_type, pool_type, name_len);
288 bstrncpy(dcr->media_type, media_type, name_len);
289 bstrncpy(dcr->dev_name, dev_name, name_len);
292 bnet_fsend(dir, _("Could not get dcr for device: %s\n"), dev_name.c_str());
295 if (append == SD_APPEND) {
296 ok = reserve_device_for_append(jcr, device->dev);
298 ok = reserve_device_for_read(jcr, device->dev);
301 bnet_fsend(dir, _("Could not get dcr for device: %s\n"), dev_name.c_str());
305 Dmsg1(220, "Got: %s", dir->msg);
306 return bnet_fsend(dir, OK_device);
311 unbash_spaces(dir->msg);
312 pm_strcpy(jcr->errmsg, dir->msg);
313 Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
315 Jmsg(jcr, M_FATAL, 0, _("\n"
316 " Device \"%s\" with MediaType \"%s\" requested by DIR not found in SD Device resources.\n"),
317 dev_name.c_str(), media_type.c_str());
318 bnet_fsend(dir, NO_device, dev_name.c_str());
320 unbash_spaces(dir->msg);
321 pm_strcpy(jcr->errmsg, dir->msg);
323 Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
325 Jmsg(jcr, M_FATAL, 0, _("Bad Use Device command: %s\n"), jcr->errmsg);
326 bnet_fsend(dir, BAD_use, jcr->errmsg);
329 return false; /* ERROR return */
333 * Query Device command from Director
334 * Sends Storage Daemon's information on the device to the
335 * caller (presumably the Director).
336 * This command always returns "true" so that the line is
337 * not closed on an error.
340 bool query_cmd(JCR *jcr)
343 BSOCK *dir = jcr->dir_bsock;
347 Dmsg1(120, "Query: %s", dir->msg);
349 ok = sscanf(dir->msg, query_device, dev_name.c_str()) == 1;
351 unbash_spaces(dev_name);
353 foreach_res(device, R_DEVICE) {
354 /* Find resource, and make sure we were able to open it */
355 if (fnmatch(dev_name.c_str(), device->hdr.name, 0) == 0 &&
357 DEVICE *dev = device->dev;
358 POOL_MEM VolumeName, MediaType;
360 if (dev->is_labeled()) {
361 pm_strcpy(VolumeName, dev->VolHdr.VolName);
363 pm_strcpy(VolumeName, "");
365 bash_spaces(VolumeName);
366 pm_strcpy(MediaType, device->media_type);
367 bash_spaces(MediaType);
368 return bnet_fsend(dir, OK_query, dev->can_append()!=0,
369 dev->can_read()!=0, dev->num_writers, dev->num_waiting,
370 dev->is_open()!=0, dev->use_count, dev->is_labeled()!=0,
371 MediaType.c_str(), VolumeName.c_str());
375 unbash_spaces(dir->msg);
376 pm_strcpy(jcr->errmsg, dir->msg);
377 bnet_fsend(dir, NO_device, dev_name.c_str());
379 unbash_spaces(dir->msg);
380 pm_strcpy(jcr->errmsg, dir->msg);
381 bnet_fsend(dir, BAD_query, jcr->errmsg);
388 * Destroy the Job Control Record and associated
389 * resources (sockets).
391 void stored_free_jcr(JCR *jcr)
393 if (jcr->file_bsock) {
394 bnet_close(jcr->file_bsock);
395 jcr->file_bsock = NULL;
398 free_pool_memory(jcr->job_name);
400 if (jcr->client_name) {
401 free_memory(jcr->client_name);
402 jcr->client_name = NULL;
404 if (jcr->fileset_name) {
405 free_memory(jcr->fileset_name);
407 if (jcr->fileset_md5) {
408 free_memory(jcr->fileset_md5);
414 if (jcr->RestoreBootstrap) {
415 unlink(jcr->RestoreBootstrap);
416 free_pool_memory(jcr->RestoreBootstrap);
417 jcr->RestoreBootstrap = NULL;
419 if (jcr->next_dev || jcr->prev_dev) {
420 Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));
422 pthread_cond_destroy(&jcr->job_start_wait);