]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/job.c
First cut new SD lock + restore buffer bug fix
[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 NoAttr=%d SpoolAttr=%d\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 available\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;
69    int JobType, level, spool_attributes, no_attributes;
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(130, "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, &no_attributes,
85               &spool_attributes) != 9) {
86       bnet_fsend(dir, BAD_job, dir->msg);
87       Emsg1(M_FATAL, 0, _("Bad Job Command from Director: %s\n"), dir->msg);
88       free_memory(job);
89       free_memory(job_name);
90       free_memory(client_name);
91       free_memory(fileset_name);
92       jcr->JobStatus = JS_ErrorTerminated;
93       return 0;
94    }
95    jcr->JobId = JobId;
96    jcr->VolSessionId = newVolSessionId();
97    jcr->VolSessionTime = VolSessionTime;
98    strcpy(jcr->Job, job);
99    unbash_spaces(job_name);
100    jcr->job_name = get_memory(strlen(job_name) + 1);
101    strcpy(jcr->job_name, job_name);
102    unbash_spaces(client_name);
103    jcr->client_name = get_memory(strlen(client_name) + 1);
104    strcpy(jcr->client_name, client_name);
105    unbash_spaces(fileset_name);
106    jcr->fileset_name = get_memory(strlen(fileset_name) + 1);
107    strcpy(jcr->fileset_name, fileset_name);
108    jcr->JobType = JobType;
109    jcr->JobLevel = level;
110    jcr->no_attributes = no_attributes;
111    jcr->spool_attributes = spool_attributes;
112    free_memory(job);
113    free_memory(job_name);
114    free_memory(client_name);
115    free_memory(fileset_name);
116
117    /* Initialize FD start condition variable */
118    if ((errstat = pthread_cond_init(&jcr->job_start_wait, NULL)) != 0) {
119       Emsg1(M_FATAL, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
120       jcr->JobStatus = JS_ErrorTerminated;
121       return 0;
122    }
123    jcr->authenticated = FALSE;
124
125    /*
126     * Pass back an authorization key for the File daemon
127     */
128    gettimeofday(&tv, &tz);
129    srandom(tv.tv_usec + tv.tv_sec);
130    sprintf(auth_key, "%ld", (long)random());
131    bnet_fsend(dir, OKjob, jcr->VolSessionId, jcr->VolSessionTime, auth_key);
132    Dmsg1(110, ">dird: %s", dir->msg);
133    jcr->sd_auth_key = bstrdup(auth_key);
134
135    /*
136     * Wait for the device, media, and pool information
137     */
138    if (!use_device_cmd(jcr)) {
139       jcr->JobStatus = JS_ErrorTerminated;
140       return 0;
141    }
142
143    jcr->JobStatus = JS_WaitFD;        /* wait for FD to connect */
144    dir_send_job_status(jcr);
145
146    gettimeofday(&tv, &tz);
147    timeout.tv_nsec = tv.tv_usec * 1000; 
148    timeout.tv_sec = tv.tv_sec + 30 * 60;        /* wait 30 minutes */
149
150
151    Dmsg1(200, "%s waiting on job_start_wait\n", jcr->Job);
152    P(jcr->mutex);
153    for ( ;!job_cancelled(jcr); ) {
154       errstat = pthread_cond_timedwait(&jcr->job_start_wait, &jcr->mutex, &timeout);
155       if (errstat == 0 || errstat == ETIMEDOUT) {
156          break;
157       }
158    }
159    V(jcr->mutex);
160
161    if (jcr->authenticated && !job_cancelled(jcr)) {
162       run_job(jcr);                   /* Run the job */
163    }
164    return 0;
165 }
166
167 /*
168  * This entry point is only called if we have a separate
169  *   Storage Daemon Data port. Otherwise, the connection
170  *   is made to the main port, and if it is a File daemon
171  *   calling, handl_filed_connection() is called directly.
172  */
173 void connection_from_filed(void *arg)
174 {
175    BSOCK *fd = (BSOCK *)arg;
176    char job_name[MAX_NAME_LENGTH];
177
178    Dmsg0(110, "enter connection_from_filed\n");
179    if (bnet_recv(fd) <= 0) {
180       Emsg0(M_FATAL, 0, _("Unable to authenticate Client.\n"));
181       return;
182    }
183    Dmsg1(100, "got: %s\n", fd->msg);
184
185    if (sscanf(fd->msg, "Hello Start Job %127s\n", job_name) != 1) {
186       Emsg1(M_FATAL, 0, _("Authentication failure: %s\n"), fd->msg);
187       return;
188    }
189    handle_filed_connection(fd, job_name);
190    return;
191 }
192
193 void handle_filed_connection(BSOCK *fd, char *job_name)
194 {
195    JCR *jcr;
196
197    if (!(jcr=get_jcr_by_full_name(job_name))) {
198       Jmsg1(NULL, M_FATAL, 0, _("Job name not found: %s\n"), job_name);
199       return;
200    }
201
202    jcr->file_bsock = fd;
203    jcr->file_bsock->jcr = (void *)jcr;
204
205    Dmsg1(110, "Found Job %s\n", job_name);
206
207    if (jcr->authenticated) {
208       Pmsg2(000, "Hey!!!! JobId %d Job %s already authenticated.\n", 
209          jcr->JobId, jcr->Job);
210    }
211   
212    /*
213     * Authenticate the File daemon
214     */
215    if (jcr->authenticated || !authenticate_filed(jcr)) {
216       Dmsg1(100, "Authentication failed Job %s\n", jcr->Job);
217       Jmsg(jcr, M_FATAL, 0, _("Unable to authenticate File daemon\n"));
218    } else {
219       jcr->authenticated = TRUE;
220       Dmsg1(110, "OK Authentication Job %s\n", jcr->Job);
221    }
222
223    P(jcr->mutex);
224    if (!jcr->authenticated) {
225       jcr->JobStatus = JS_ErrorTerminated;
226    }
227    pthread_cond_signal(&jcr->job_start_wait); /* wake waiting job */
228    V(jcr->mutex);
229    free_jcr(jcr);
230    return;
231 }
232
233
234 /*  
235  *   Use Device command from Director
236  *   He tells is what Device Name to use, the Media Type, 
237  *      the Pool Name, and the Pool Type.
238  *
239  *    Ensure that the device exists and is opened, then store
240  *      the media and pool info in the JCR.
241  */
242 static int use_device_cmd(JCR *jcr)
243 {
244    POOLMEM *dev_name, *media_type, *pool_name, *pool_type;
245    BSOCK *dir = jcr->dir_bsock;
246    DEVRES *device;
247
248    if (bnet_recv(dir) <= 0) {
249       Emsg0(M_FATAL, 0, "No Device from Director\n");
250       return 0;
251    }
252    
253    Dmsg1(120, "Use device: %s", dir->msg);
254    dev_name = get_memory(dir->msglen);
255    media_type = get_memory(dir->msglen);
256    pool_name = get_memory(dir->msglen);
257    pool_type = get_memory(dir->msglen);
258    if (sscanf(dir->msg, use_device, dev_name, media_type, pool_name, pool_type) == 4) {
259       unbash_spaces(dev_name);
260       unbash_spaces(media_type);
261       unbash_spaces(pool_name);
262       unbash_spaces(pool_type);
263       device = NULL;
264       LockRes();
265       while ((device=(DEVRES *)GetNextRes(R_DEVICE, (RES *)device))) {
266          /* Find resource, and make sure we were able to open it */
267          if (strcmp(device->hdr.name, dev_name) == 0 && device->dev) {
268             Dmsg1(120, "Found device %s\n", device->hdr.name);
269             jcr->pool_name = get_memory(strlen(pool_name) + 1);
270             strcpy(jcr->pool_name, pool_name);
271             jcr->pool_type = get_memory(strlen(pool_type) + 1);
272             strcpy(jcr->pool_type, pool_type);
273             jcr->media_type = get_memory(strlen(media_type) + 1);
274             strcpy(jcr->media_type, media_type);
275             jcr->dev_name = get_memory(strlen(dev_name) + 1);
276             strcpy(jcr->dev_name, dev_name);
277             jcr->device = device;
278             Dmsg4(120, use_device, dev_name, media_type, pool_name, pool_type);
279             free_memory(dev_name);
280             free_memory(media_type);
281             free_memory(pool_name);
282             free_memory(pool_type);
283             UnlockRes();
284             return bnet_fsend(dir, OK_device);
285          }
286       }
287       UnlockRes();
288       Jmsg(jcr, M_FATAL, 0, _("Requested device %s not found. Cannot continue.\n"),
289            dev_name);
290       bnet_fsend(dir, NO_device, dev_name);
291    } else {
292       Jmsg(jcr, M_FATAL, 0, _("store<dir: Bad Use Device command: %s\n"), dir->msg);
293       bnet_fsend(dir, BAD_use, dir->msg);
294    }
295
296    free_memory(dev_name);
297    free_memory(media_type);
298    free_memory(pool_name);
299    free_memory(pool_type);
300    return 0;                          /* ERROR return */
301 }
302
303 /* 
304  * Destroy the Job Control Record and associated
305  * resources (sockets).
306  */
307 void stored_free_jcr(JCR *jcr) 
308 {
309    if (jcr->file_bsock) {
310       bnet_close(jcr->file_bsock);
311       jcr->file_bsock = NULL;
312    }
313    if (jcr->pool_name) {
314       free_memory(jcr->pool_name);
315    }
316    if (jcr->pool_type) {
317       free_memory(jcr->pool_type);
318    }
319    if (jcr->media_type) {
320       free_memory(jcr->media_type);
321    }
322    if (jcr->dev_name) {
323       free_memory(jcr->dev_name);
324    }
325    if (jcr->job_name) {
326       free_pool_memory(jcr->job_name);
327    }
328    if (jcr->client_name) {
329       free_memory(jcr->client_name);
330       jcr->client_name = NULL;
331    }
332    if (jcr->fileset_name) {
333       free_memory(jcr->fileset_name);
334    }
335    if (jcr->bsr) {
336       free_bsr(jcr->bsr);
337       jcr->bsr = NULL;
338    }
339    if (jcr->RestoreBootstrap) {
340       unlink(jcr->RestoreBootstrap);
341       free_pool_memory(jcr->RestoreBootstrap);
342       jcr->RestoreBootstrap = NULL;
343    }
344    if (jcr->next_dev && jcr->prev_dev) {
345       Emsg0(M_FATAL, 0, _("In free_jcr(), but still attached to device!!!!\n"));
346    }
347                       
348    return;
349 }