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