]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
More data spooling updates
[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-2003 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 NoAttr=%d SpoolAttr=%d FileSetMD5=%127s "
42          "SpoolData=%d";
43 static char use_device[] = "use device=%s media_type=%s pool_name=%s pool_type=%s\n";
44
45 /* Responses sent to Director daemon */
46 static char OKjob[]     = "3000 OK Job SDid=%u SDtime=%u Authorization=%s\n";
47 static char OK_device[] = "3000 OK use device\n";
48 static char NO_device[] = "3914 Device \"%s\" not in SD Device resources.\n";
49 static char BAD_use[]   = "3913 Bad use command: %s\n";
50 static char BAD_job[]   = "3915 Bad Job command: %s\n";
51
52
53
54 /*
55  * Director requests us to start a job
56  * Basic tasks done here:
57  *  - We pickup the JobId to be run from the Director.
58  *  - We pickup the device, media, and pool from the Director
59  *  - Wait for a connection from the File Daemon (FD)
60  *  - Accept commands from the FD (i.e. run the job)
61  *  - Return when the connection is terminated or
62  *    there is an error.
63  */
64 int job_cmd(JCR *jcr)
65 {
66    int JobId, errstat;
67    char auth_key[100];
68    BSOCK *dir = jcr->dir_bsock;
69    POOLMEM *job_name, *client_name, *job, *fileset_name, *fileset_md5;
70    int JobType, level, spool_attributes, no_attributes, spool_data;
71    struct timeval tv;
72    struct timezone tz;
73    struct timespec timeout;
74    JCR *ojcr;
75
76    /*
77     * Get JobId and permissions from Director
78     */
79
80    Dmsg1(100, "Job_cmd: %s\n", dir->msg);
81    job = get_memory(dir->msglen);
82    job_name = get_memory(dir->msglen);
83    client_name = get_memory(dir->msglen);
84    fileset_name = get_memory(dir->msglen);
85    fileset_md5 = get_memory(dir->msglen);
86    if (sscanf(dir->msg, jobcmd, &JobId, job, job_name, client_name,
87               &JobType, &level, fileset_name, &no_attributes,
88               &spool_attributes, fileset_md5, &spool_data) != 11) {
89       pm_strcpy(&jcr->errmsg, dir->msg);
90       bnet_fsend(dir, BAD_job, jcr->errmsg);
91       Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), jcr->errmsg);
92       free_memory(job);
93       free_memory(job_name);
94       free_memory(client_name);
95       free_memory(fileset_name);
96       free_memory(fileset_md5);
97       set_jcr_job_status(jcr, JS_ErrorTerminated);
98       return 0;
99    }
100    /*         
101     * Since this job could be rescheduled, we
102     *  check to see if we have it already. If so
103     *  free the old jcr and use the new one.
104     */
105    ojcr = get_jcr_by_full_name(job);
106    if (ojcr && !ojcr->authenticated) {
107       Dmsg2(100, "Found ojcr=0x%x Job %s\n", (unsigned)(long)ojcr, job);
108       free_jcr(ojcr);
109    }
110    jcr->JobId = JobId;
111    jcr->VolSessionId = newVolSessionId();
112    jcr->VolSessionTime = VolSessionTime;
113    bstrncpy(jcr->Job, job, sizeof(jcr->Job));
114    unbash_spaces(job_name);
115    jcr->job_name = get_pool_memory(PM_NAME);
116    pm_strcpy(&jcr->job_name, job_name);
117    unbash_spaces(client_name);
118    jcr->client_name = get_pool_memory(PM_NAME);
119    pm_strcpy(&jcr->client_name, client_name);
120    unbash_spaces(fileset_name);
121    jcr->fileset_name = get_pool_memory(PM_NAME);
122    pm_strcpy(&jcr->fileset_name, fileset_name);
123    jcr->JobType = JobType;
124    jcr->JobLevel = level;
125    jcr->no_attributes = no_attributes;
126    jcr->spool_attributes = spool_attributes;
127    jcr->spool_data = spool_data;
128    jcr->fileset_md5 = get_pool_memory(PM_NAME);
129    pm_strcpy(&jcr->fileset_md5, fileset_md5);
130    free_memory(job);
131    free_memory(job_name);
132    free_memory(client_name);
133    free_memory(fileset_name);
134    free_memory(fileset_md5);
135
136    /* Initialize FD start condition variable */
137    if ((errstat = pthread_cond_init(&jcr->job_start_wait, NULL)) != 0) {
138       Jmsg1(jcr, M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
139       set_jcr_job_status(jcr, JS_ErrorTerminated);
140       return 0;
141    }
142    jcr->authenticated = FALSE;
143
144    /*
145     * Pass back an authorization key for the File daemon
146     */
147    make_session_key(auth_key, NULL, 1);
148    bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
149    Dmsg1(110, ">dird: %s", dir->msg);
150    jcr->sd_auth_key = bstrdup(auth_key);
151    memset(auth_key, 0, sizeof(auth_key));    
152
153    /*
154     * Wait for the device, media, and pool information
155     */
156    if (!use_device_cmd(jcr)) {
157       set_jcr_job_status(jcr, JS_ErrorTerminated);
158       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
159       return 0;
160    }
161
162    set_jcr_job_status(jcr, JS_WaitFD);          /* wait for FD to connect */
163    dir_send_job_status(jcr);
164
165    gettimeofday(&tv, &tz);
166    timeout.tv_nsec = tv.tv_usec * 1000; 
167    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
168
169
170    Dmsg1(100, "%s waiting on job_start_wait\n", jcr->Job);
171    /* Wait for the File daemon to contact us to start the Job,
172     *  when he does, we will be released, unless the 30 minutes
173     *  expires.
174     */
175    P(jcr->mutex);
176    for ( ;!job_canceled(jcr); ) {
177       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
178       if (errstat == 0 || errstat == ETIMEDOUT) {
179          break;
180       }
181    }
182    V(jcr->mutex);
183
184    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
185
186    if (jcr->authenticated && !job_canceled(jcr)) {
187       Dmsg1(100, "Running job %s\n", jcr->Job);
188       run_job(jcr);                   /* Run the job */
189    }
190    return 0;
191 }
192
193 /*
194  * After receiving a connection (in job.c) if it is
195  *   from the File daemon, this routine is called.
196  */  
197 void handle_filed_connection(BSOCK *fd, char *job_name)
198 {
199    JCR *jcr;
200
201    bmicrosleep(0, 50000);             /* wait 50 millisecs */
202    if (!(jcr=get_jcr_by_full_name(job_name))) {
203       Jmsg1(NULL, M_FATAL, 0, _("Job name not found: %s\n"), job_name);
204       return;
205    }
206
207    jcr->file_bsock = fd;
208    jcr->file_bsock->jcr = jcr;
209
210    Dmsg1(110, "Found Job %s\n", job_name);
211
212    if (jcr->authenticated) {
213       Pmsg2(000, "Hey!!!! JobId %u Job %s already authenticated.\n", 
214          jcr->JobId, jcr->Job);
215    }
216   
217    /*
218     * Authenticate the File daemon
219     */
220    if (jcr->authenticated || !authenticate_filed(jcr)) {
221       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
222       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
223    } else {
224       jcr->authenticated = TRUE;
225       Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
226    }
227
228    P(jcr->mutex);
229    if (!jcr->authenticated) {
230       set_jcr_job_status(jcr, JS_ErrorTerminated);
231    }
232    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
233    V(jcr->mutex);
234    free_jcr(jcr);
235    return;
236 }
237
238
239 /*  
240  *   Use Device command from Director
241  *   He tells is what Device Name to use, the Media Type, 
242  *      the Pool Name, and the Pool Type.
243  *
244  *    Ensure that the device exists and is opened, then store
245  *      the media and pool info in the JCR.
246  */
247 static int use_device_cmd(JCR *jcr)
248 {
249    POOLMEM *dev_name, *media_type, *pool_name, *pool_type;
250    BSOCK *dir = jcr->dir_bsock;
251    DEVRES *device;
252
253    if (bnet_recv(dir) <= 0) {
254       Jmsg0(jcr, M_FATAL, 0, _("No Device from Director\n"));
255       return 0;
256    }
257    
258    Dmsg1(120, "Use device: %s", dir->msg);
259    dev_name = get_memory(dir->msglen);
260    media_type = get_memory(dir->msglen);
261    pool_name = get_memory(dir->msglen);
262    pool_type = get_memory(dir->msglen);
263    if (sscanf(dir->msg, use_device, dev_name, media_type, pool_name, pool_type) == 4) {
264       unbash_spaces(dev_name);
265       unbash_spaces(media_type);
266       unbash_spaces(pool_name);
267       unbash_spaces(pool_type);
268       device = NULL;
269       LockRes();
270       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
271          /* Find resource, and make sure we were able to open it */
272          if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
273             Dmsg1(120, "Found device %s\n", device->hdr.name);
274             jcr->pool_name = get_memory(strlen(pool_name) + 1);
275             strcpy(jcr->pool_name, pool_name);
276             jcr->pool_type = get_memory(strlen(pool_type) + 1);
277             strcpy(jcr->pool_type, pool_type);
278             jcr->media_type = get_memory(strlen(media_type) + 1);
279             strcpy(jcr->media_type, media_type);
280             jcr->dev_name = get_memory(strlen(dev_name) + 1);
281             strcpy(jcr->dev_name, dev_name);
282             jcr->device = device;
283             Dmsg4(120, use_device, dev_name, media_type, pool_name, pool_type);
284             free_memory(dev_name);
285             free_memory(media_type);
286             free_memory(pool_name);
287             free_memory(pool_type);
288             UnlockRes();
289             return bnet_fsend(dir, OK_device);
290          }
291       }
292       UnlockRes();
293       if (verbose) {
294          unbash_spaces(dir->msg);
295          pm_strcpy(&jcr->errmsg, dir->msg);
296          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
297       }
298       Jmsg(jcr, M_FATAL, 0, _("\n"
299          "     Device \"%s\" requested by Dir not found in SD Device resources.\n"),
300            dev_name);
301       bnet_fsend(dir, NO_device, dev_name);
302    } else {
303       unbash_spaces(dir->msg);
304       pm_strcpy(&jcr->errmsg, dir->msg);
305       if (verbose) {
306          Jmsg(jcr, M_INFO, 0, _("Failed command: %s\n"), jcr->errmsg);
307       }
308       Jmsg(jcr, M_FATAL, 0, _("Bad Use Device command: %s\n"), jcr->errmsg);
309       bnet_fsend(dir, BAD_use, jcr->errmsg);
310    }
311
312    free_memory(dev_name);
313    free_memory(media_type);
314    free_memory(pool_name);
315    free_memory(pool_type);
316    return 0;                          /* ERROR return */
317 }
318
319 /* 
320  * Destroy the Job Control Record and associated
321  * resources (sockets).
322  */
323 void stored_free_jcr(JCR *jcr) 
324 {
325    if (jcr->file_bsock) {
326       bnet_close(jcr->file_bsock);
327       jcr->file_bsock = NULL;
328    }
329    if (jcr->pool_name) {
330       free_memory(jcr->pool_name);
331    }
332    if (jcr->pool_type) {
333       free_memory(jcr->pool_type);
334    }
335    if (jcr->media_type) {
336       free_memory(jcr->media_type);
337    }
338    if (jcr->dev_name) {
339       free_memory(jcr->dev_name);
340    }
341    if (jcr->job_name) {
342       free_pool_memory(jcr->job_name);
343    }
344    if (jcr->client_name) {
345       free_memory(jcr->client_name);
346       jcr->client_name = NULL;
347    }
348    if (jcr->fileset_name) {
349       free_memory(jcr->fileset_name);
350    }
351    if (jcr->fileset_md5) {
352       free_memory(jcr->fileset_md5);
353    }
354    if (jcr->bsr) {
355       free_bsr(jcr->bsr);
356       jcr->bsr = NULL;
357    }
358    if (jcr->RestoreBootstrap) {
359       unlink(jcr->RestoreBootstrap);
360       free_pool_memory(jcr->RestoreBootstrap);
361       jcr->RestoreBootstrap = NULL;
362    }
363    if (jcr->next_dev && jcr->prev_dev) {
364       Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));
365    }
366    pthread_cond_destroy(&jcr->job_start_wait);
367    if (jcr->dcr) {
368       free_dcr(jcr->dcr);
369       jcr->dcr = NULL;
370    }
371    return;
372 }