2 * Job control and execution for Storage Daemon
6 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public
19 License along with this program; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /* Imported variables */
29 extern uint32_t VolSessionTime;
31 /* Imported functions */
32 extern uint32_t newVolSessionId();
34 /* Forward referenced functions */
35 static int use_device_cmd(JCR *jcr);
37 /* Requests from the Director daemon */
38 static char jobcmd[] = "JobId=%d job=%127s job_name=%127s client_name=%127s Allow=";
39 static char use_device[] = "use device=%s media_type=%s pool_name=%s pool_type=%s\n";
41 /* Responses sent to Director daemon */
42 static char OKjob[] = "3000 OK Job SDid=%u SDtime=%u Authorization=%s\n";
43 static char OK_device[] = "3000 OK use device\n";
44 static char NO_device[] = "3904 Device not available\n";
45 static char BAD_use[] = "3903 Bad use command\n";
46 static char BAD_job[] = "3905 Bad Job command\n";
51 * Director requests us to start a job
52 * Basic tasks done here:
53 * - We pickup the JobId to be run from the Director.
54 * - We pickup the device, media, and pool from the Director
55 * - Wait for a connection from the File Daemon (FD)
56 * - Accept commands from the FD (i.e. run the job)
57 * - Return when the connection is terminated or
64 BSOCK *dir = jcr->dir_bsock;
65 char *job_name, *client_name, *job;
68 struct timespec timeout;
71 * Get JobId and permissions from Director
74 Dmsg1(30, "Job_cmd: %s\n", dir->msg);
75 job = (char *) get_memory(dir->msglen);
76 job_name = (char *) get_memory(dir->msglen);
77 client_name = (char *)get_memory(dir->msglen);
78 if (sscanf(dir->msg, jobcmd, &JobId, job, job_name, client_name) != 4) {
79 bnet_fsend(dir, BAD_job);
80 Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), dir->msg);
82 free_memory(job_name);
83 free_memory(client_name);
84 jcr->JobStatus = JS_ErrorTerminated;
88 jcr->VolSessionId = newVolSessionId();
89 jcr->VolSessionTime = VolSessionTime;
90 strcpy(jcr->Job, job);
91 unbash_spaces(job_name);
92 jcr->job_name = (char *) get_memory(strlen(job_name) + 1);
93 strcpy(jcr->job_name, job_name);
94 unbash_spaces(client_name);
95 jcr->client_name = bstrdup(client_name);
97 free_memory(job_name);
98 free_memory(client_name);
100 /* Initialize FD start condition variable */
101 if ((errstat = pthread_cond_init(&jcr->job_start_wait, NULL)) != 0) {
102 Emsg1(M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
103 jcr->JobStatus = JS_ErrorTerminated;
106 jcr->authenticated = FALSE;
109 * Pass back an authorization key for the File daemon
111 gettimeofday(&tv, &tz);
112 srandom(tv.tv_usec + tv.tv_sec);
113 sprintf(auth_key, "%ld", (long)random());
114 bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
115 Dmsg1(10, ">dird: %s", dir->msg);
116 jcr->sd_auth_key = bstrdup(auth_key);
119 * Wait for the device, media, and pool information
121 if (!use_device_cmd(jcr)) {
122 jcr->JobStatus = JS_ErrorTerminated;
126 jcr->JobStatus = JS_WaitFD; /* wait for FD to connect */
127 dir_send_job_status(jcr);
129 gettimeofday(&tv, &tz);
130 timeout.tv_nsec = tv.tv_usec * 1000;
131 timeout.tv_sec = tv.tv_sec + 30 * 60; /* wait 30 minutes */
134 Dmsg1(200, "%s waiting on job_start_wait\n", jcr->Job);
136 for ( ;!job_cancelled(jcr); ) {
137 errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
138 if (errstat == 0 || errstat == ETIMEDOUT) {
144 if (jcr->authenticated && !job_cancelled(jcr)) {
145 run_job(jcr); /* Run the job */
151 * This entry point is only called if we have a separate
152 * Storage Daemon Data port. Otherwise, the connection
153 * is made to the main port, and if it is a File daemon
154 * calling, handl_filed_connection() is called directly.
156 void connection_from_filed(void *arg)
158 BSOCK *fd = (BSOCK *)arg;
159 char job_name[MAX_NAME_LENGTH];
161 Dmsg0(200, "enter connection_from_filed\n");
162 if (bnet_recv(fd) <= 0) {
163 Emsg0(M_ERROR, 0, "Unable to authenticate Client.\n");
166 Dmsg1(100, "got: %s\n", fd->msg);
168 if (sscanf(fd->msg, "Hello Start Job %127s\n", job_name) != 1) {
169 Emsg1(M_ERROR, 0, _("Authentication failure: %s"), fd->msg);
172 handle_filed_connection(fd, job_name);
176 void handle_filed_connection(BSOCK *fd, char *job_name)
180 if (!(jcr=get_jcr_by_full_name(job_name))) {
181 Emsg1(M_ERROR, 0, "Job name not found: %s", job_name);
185 jcr->file_bsock = fd;
187 Dmsg1(100, "Found Job %s\n", job_name);
189 if (jcr->authenticated) {
190 Dmsg2(000, "Hey!!!! JobId %d Job %s already authenticated.\n",
191 jcr->JobId, jcr->Job);
195 * Authenticate the File daemon
197 if (jcr->authenticated || !authenticate_filed(jcr)) {
198 Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
199 Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
201 jcr->authenticated = TRUE;
202 Dmsg1(100, "OK Authentication Job %s\n", jcr->Job);
206 if (!jcr->authenticated) {
207 jcr->JobStatus = JS_ErrorTerminated;
209 pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
217 * Use Device command from Director
218 * He tells is what Device Name to use, the Media Type,
219 * the Pool Name, and the Pool Type.
221 * Ensure that the device exists and is opened, then store
222 * the media and pool info in the JCR.
224 static int use_device_cmd(JCR *jcr)
226 char *dev_name, *media_type, *pool_name, *pool_type;
227 BSOCK *dir = jcr->dir_bsock;
230 if (bnet_recv(dir) <= 0) {
231 Emsg0(M_FATAL, 0, "No Device from Director\n");
235 Dmsg1(20, "Use device: %s", dir->msg);
236 dev_name = (char *) get_memory(dir->msglen);
237 media_type = (char *) get_memory(dir->msglen);
238 pool_name = (char *) get_memory(dir->msglen);
239 pool_type = (char *) get_memory(dir->msglen);
240 if (sscanf(dir->msg, use_device, dev_name, media_type, pool_name, pool_type) == 4) {
241 unbash_spaces(dev_name);
242 unbash_spaces(media_type);
243 unbash_spaces(pool_name);
244 unbash_spaces(pool_type);
247 while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
248 /* Find resource, and make sure we were able to open it */
249 if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
250 Dmsg1(20, "Found device %s\n", device->hdr.name);
251 jcr->pool_name = (char *) get_memory(strlen(pool_name) + 1);
252 strcpy(jcr->pool_name, pool_name);
253 jcr->pool_type = (char *) get_memory(strlen(pool_type) + 1);
254 strcpy(jcr->pool_type, pool_type);
255 jcr->media_type = (char *) get_memory(strlen(media_type) + 1);
256 strcpy(jcr->media_type, media_type);
257 jcr->dev_name = (char *) get_memory(strlen(dev_name) + 1);
258 strcpy(jcr->dev_name, dev_name);
259 jcr->device = device;
260 Dmsg4(20, use_device, dev_name, media_type, pool_name, pool_type);
261 free_memory(dev_name);
262 free_memory(media_type);
263 free_memory(pool_name);
264 free_memory(pool_type);
266 return bnet_fsend(dir, OK_device);
270 Jmsg(jcr, M_FATAL, 0, _("Requested device %s not found. Cannot continue.\n"),
272 bnet_fsend(dir, NO_device);
274 Emsg1(M_FATAL, 0, _("store<dir: Bad Use Device command: %s\n"), dir->msg);
275 bnet_fsend(dir, BAD_use);
278 free_memory(dev_name);
279 free_memory(media_type);
280 free_memory(pool_name);
281 free_memory(pool_type);
282 return 0; /* ERROR return */
286 * Destroy the Job Control Record and associated
287 * resources (sockets).
289 void stored_free_jcr(JCR *jcr)
291 if (jcr->file_bsock) {
292 bnet_close(jcr->file_bsock);
294 if (jcr->pool_name) {
295 free_memory(jcr->pool_name);
297 if (jcr->pool_type) {
298 free_memory(jcr->pool_type);
300 if (jcr->media_type) {
301 free_memory(jcr->media_type);
304 free_memory(jcr->dev_name);
307 free_pool_memory(jcr->job_name);