]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
Implement GZIP, new tape format, -- see kes03Jun02
[bacula/bacula] / bacula / src / stored / job.c
1 /*
2  *   Job control and execution for Storage Daemon
3  *
4  *   Version $Id$
5  *
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
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.
14
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.
19
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,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29
30 /* Imported variables */
31 extern uint32_t VolSessionTime;
32
33 /* Imported functions */
34 extern uint32_t newVolSessionId();
35
36 /* Forward referenced functions */
37 static int use_device_cmd(JCR *jcr);
38
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 Allow=";
42 static char use_device[] = "use device=%s media_type=%s pool_name=%s pool_type=%s\n";
43
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[] = "3904 Device not available\n";
48 static char BAD_use[]   = "3903 Bad use command\n";
49 static char BAD_job[]   = "3905 Bad Job command\n";
50
51
52
53 /*
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
61  *    there is an error.
62  */
63 int job_cmd(JCR *jcr)
64 {
65    int JobId, errstat;
66    char auth_key[100];
67    BSOCK *dir = jcr->dir_bsock;
68    POOLMEM *job_name, *client_name, *job, *fileset_name;
69    int JobType, level;
70    struct timeval tv;
71    struct timezone tz;
72    struct timespec timeout;
73
74    /*
75     * Get JobId and permissions from Director
76     */
77
78    Dmsg1(30, "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    if (sscanf(dir->msg, jobcmd, &JobId, job, job_name, client_name,
84               &JobType, &level, fileset_name) != 7) {
85       bnet_fsend(dir, BAD_job);
86       Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), dir->msg);
87       free_memory(job);
88       free_memory(job_name);
89       free_memory(client_name);
90       free_memory(fileset_name);
91       jcr->JobStatus = JS_ErrorTerminated;
92       return 0;
93    }
94    jcr->JobId = JobId;
95    jcr->VolSessionId = newVolSessionId();
96    jcr->VolSessionTime = VolSessionTime;
97    strcpy(jcr->Job, job);
98    unbash_spaces(job_name);
99    jcr->job_name = get_memory(strlen(job_name) + 1);
100    strcpy(jcr->job_name, job_name);
101    unbash_spaces(client_name);
102    jcr->client_name = get_memory(strlen(client_name) + 1);
103    strcpy(jcr->client_name, client_name);
104    unbash_spaces(fileset_name);
105    jcr->fileset_name = get_memory(strlen(fileset_name) + 1);
106    strcpy(jcr->fileset_name, fileset_name);
107    jcr->JobType = JobType;
108    jcr->JobLevel = level;
109    free_memory(job);
110    free_memory(job_name);
111    free_memory(client_name);
112    free_memory(fileset_name);
113
114    /* Initialize FD start condition variable */
115    if ((errstat = pthread_cond_init(&jcr->job_start_wait, NULL)) != 0) {
116       Emsg1(M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
117       jcr->JobStatus = JS_ErrorTerminated;
118       return 0;
119    }
120    jcr->authenticated = FALSE;
121
122    /*
123     * Pass back an authorization key for the File daemon
124     */
125    gettimeofday(&tv, &tz);
126    srandom(tv.tv_usec + tv.tv_sec);
127    sprintf(auth_key, "%ld", (long)random());
128    bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
129    Dmsg1(10, ">dird: %s", dir->msg);
130    jcr->sd_auth_key = bstrdup(auth_key);
131
132    /*
133     * Wait for the device, media, and pool information
134     */
135    if (!use_device_cmd(jcr)) {
136       jcr->JobStatus = JS_ErrorTerminated;
137       return 0;
138    }
139
140    jcr->JobStatus = JS_WaitFD;        /* wait for FD to connect */
141    dir_send_job_status(jcr);
142
143    gettimeofday(&tv, &tz);
144    timeout.tv_nsec = tv.tv_usec * 1000; 
145    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
146
147
148    Dmsg1(200, "%s waiting on job_start_wait\n", jcr->Job);
149    P(jcr->mutex);
150    for ( ;!job_cancelled(jcr); ) {
151       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
152       if (errstat == 0 || errstat == ETIMEDOUT) {
153          break;
154       }
155    }
156    V(jcr->mutex);
157
158    if (jcr->authenticated && !job_cancelled(jcr)) {
159       run_job(jcr);                   /* Run the job */
160    }
161    return 0;
162 }
163
164 /*
165  * This entry point is only called if we have a separate
166  *   Storage Daemon Data port. Otherwise, the connection
167  *   is made to the main port, and if it is a File daemon
168  *   calling, handl_filed_connection() is called directly.
169  */
170 void connection_from_filed(void *arg)
171 {
172    BSOCK *fd = (BSOCK *)arg;
173    char job_name[MAX_NAME_LENGTH];
174
175    Dmsg0(200, "enter connection_from_filed\n");
176    if (bnet_recv(fd) <= 0) {
177       Emsg0(M_ERROR, 0, "Unable to authenticate Client.\n");
178       return;
179    }
180    Dmsg1(100, "got: %s\n", fd->msg);
181
182    if (sscanf(fd->msg, "Hello Start Job %127s\n", job_name) != 1) {
183       Emsg1(M_ERROR, 0, _("Authentication failure: %s"), fd->msg);
184       return;
185    }
186    handle_filed_connection(fd, job_name);
187    return;
188 }
189
190 void handle_filed_connection(BSOCK *fd, char *job_name)
191 {
192    JCR *jcr;
193
194    if (!(jcr=get_jcr_by_full_name(job_name))) {
195       Emsg1(M_ERROR, 0, "Job name not found: %s", job_name);
196       return;
197    }
198
199    jcr->file_bsock = fd;
200
201    Dmsg1(100, "Found Job %s\n", job_name);
202
203    if (jcr->authenticated) {
204       Dmsg2(000, "Hey!!!! JobId %d Job %s already authenticated.\n", 
205          jcr->JobId, jcr->Job);
206    }
207   
208    /*
209     * Authenticate the File daemon
210     */
211    if (jcr->authenticated || !authenticate_filed(jcr)) {
212       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
213       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
214    } else {
215       jcr->authenticated = TRUE;
216       Dmsg1(100, "OK Authentication Job %s\n", jcr->Job);
217    }
218
219    P(jcr->mutex);
220    if (!jcr->authenticated) {
221       jcr->JobStatus = JS_ErrorTerminated;
222    }
223    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
224    V(jcr->mutex);
225    free_jcr(jcr);
226    return;
227 }
228
229
230 /*  
231  *   Use Device command from Director
232  *   He tells is what Device Name to use, the Media Type, 
233  *      the Pool Name, and the Pool Type.
234  *
235  *    Ensure that the device exists and is opened, then store
236  *      the media and pool info in the JCR.
237  */
238 static int use_device_cmd(JCR *jcr)
239 {
240    POOLMEM *dev_name, *media_type, *pool_name, *pool_type;
241    BSOCK *dir = jcr->dir_bsock;
242    DEVRES *device;
243
244    if (bnet_recv(dir) <= 0) {
245       Emsg0(M_FATAL, 0, "No Device from Director\n");
246       return 0;
247    }
248    
249    Dmsg1(20, "Use device: %s", dir->msg);
250    dev_name = get_memory(dir->msglen);
251    media_type = get_memory(dir->msglen);
252    pool_name = get_memory(dir->msglen);
253    pool_type = get_memory(dir->msglen);
254    if (sscanf(dir->msg, use_device, dev_name, media_type, pool_name, pool_type) == 4) {
255       unbash_spaces(dev_name);
256       unbash_spaces(media_type);
257       unbash_spaces(pool_name);
258       unbash_spaces(pool_type);
259       device = NULL;
260       LockRes();
261       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
262          /* Find resource, and make sure we were able to open it */
263          if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
264             Dmsg1(20, "Found device %s\n", device->hdr.name);
265             jcr->pool_name = get_memory(strlen(pool_name) + 1);
266             strcpy(jcr->pool_name, pool_name);
267             jcr->pool_type = get_memory(strlen(pool_type) + 1);
268             strcpy(jcr->pool_type, pool_type);
269             jcr->media_type = get_memory(strlen(media_type) + 1);
270             strcpy(jcr->media_type, media_type);
271             jcr->dev_name = get_memory(strlen(dev_name) + 1);
272             strcpy(jcr->dev_name, dev_name);
273             jcr->device = device;
274             Dmsg4(20, use_device, dev_name, media_type, pool_name, pool_type);
275             free_memory(dev_name);
276             free_memory(media_type);
277             free_memory(pool_name);
278             free_memory(pool_type);
279             UnlockRes();
280             return bnet_fsend(dir, OK_device);
281          }
282       }
283       UnlockRes();
284       Jmsg(jcr, M_FATAL, 0, _("Requested device %s not found. Cannot continue.\n"),
285            dev_name);
286       bnet_fsend(dir, NO_device);
287    } else {
288       Emsg1(M_FATAL, 0, _("store<dir: Bad Use Device command: %s\n"), dir->msg);
289       bnet_fsend(dir, BAD_use);
290    }
291
292    free_memory(dev_name);
293    free_memory(media_type);
294    free_memory(pool_name);
295    free_memory(pool_type);
296    return 0;                          /* ERROR return */
297 }
298
299 /* 
300  * Destroy the Job Control Record and associated
301  * resources (sockets).
302  */
303 void stored_free_jcr(JCR *jcr) 
304 {
305    if (jcr->file_bsock) {
306       bnet_close(jcr->file_bsock);
307    }
308    if (jcr->pool_name) {
309       free_memory(jcr->pool_name);
310    }
311    if (jcr->pool_type) {
312       free_memory(jcr->pool_type);
313    }
314    if (jcr->media_type) {
315       free_memory(jcr->media_type);
316    }
317    if (jcr->dev_name) {
318       free_memory(jcr->dev_name);
319    }
320    if (jcr->job_name) {
321       free_pool_memory(jcr->job_name);
322    }
323    if (jcr->client_name) {
324       free_memory(jcr->client_name);
325       jcr->client_name = NULL;
326    }
327    if (jcr->fileset_name) {
328       free_memory(jcr->fileset_name);
329    }
330    return;
331 }